Ignore:
Timestamp:
11/12/08 17:04:01 (16 years ago)
Author:
TakTsutsumi
Message:

Merged recent updates (since 2007) from nrao-asap

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/alma/src/Scantable.cpp

    r1400 r1446  
    1111//
    1212#include <map>
     13#include <fstream>
    1314
    1415#include <casa/aips.h>
     
    2425#include <casa/Arrays/Vector.h>
    2526#include <casa/Arrays/VectorSTLIterator.h>
     27#include <casa/Arrays/Slice.h>
    2628#include <casa/BasicMath/Math.h>
    2729#include <casa/BasicSL/Constants.h>
     
    533535    return int(n);
    534536  } else {
    535     // take the first SCANNO,POLNO,BEAMNO,CYCLENO as nbeam shouldn't
    536     // vary with these
     537    // take the first SCANNO,POLNO,BEAMNO,CYCLENO as nbeam shouldn't vary with these
    537538    Table t = table_(table_.col("IFNO") == ifno);
    538539    if ( t.nrow() == 0 ) return 0;
     
    786787  table_.keywordSet().get("FluxUnit", tmp);
    787788  oss << setw(15) << "Flux Unit:" << tmp << endl;
    788   Vector<Double> vec(moleculeTable_.getRestFrequencies());
     789  //Vector<Double> vec(moleculeTable_.getRestFrequencies());
     790  int nid = moleculeTable_.nrow();
     791  Bool firstline = True;
    789792  oss << setw(15) << "Rest Freqs:";
    790   if (vec.nelements() > 0) {
    791       oss << setprecision(10) << vec << " [Hz]" << endl;
    792   } else {
    793       oss << "none" << endl;
     793  for (int i=0; i<nid; i++) {
     794      Table t = table_(table_.col("MOLECULE_ID") == i);
     795      if (t.nrow() >  0) {
     796          Vector<Double> vec(moleculeTable_.getRestFrequency(i));
     797          if (vec.nelements() > 0) {
     798               if (firstline) {
     799                   oss << setprecision(10) << vec << " [Hz]" << endl;
     800                   firstline=False;
     801               }
     802               else{
     803                   oss << setw(15)<<" " << setprecision(10) << vec << " [Hz]" << endl;
     804               }
     805          } else {
     806              oss << "none" << endl;
     807          }
     808      }
    794809  }
    795810
     
    843858        oss << setw(10) << "";
    844859        oss << setw(3) << std::right << irec.asuInt("IFNO") << std::left
    845             << setw(2) << "" << frequencies().print(irec.asuInt("FREQ_ID"))
    846             << endl;
     860            << setw(2) << "" << frequencies().print(irec.asuInt("FREQ_ID"));
    847861
    848862        ++iiter;
     
    891905  const MDirection& md = getDirection(whichrow);
    892906  const MEpoch& me = timeCol_(whichrow);
    893   Double rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
     907  //Double rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
     908  Vector<Double> rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
    894909  SpectralCoordinate spc =
    895910    freqTable_.getSpectralCoordinate(md, mp, me, rf, mfreqidCol_(whichrow));
     
    950965  const MDirection& md = getDirection(whichrow);
    951966  const MEpoch& me = timeCol_(whichrow);
    952   const Double& rf = mmolidCol_(whichrow);
     967  //const Double& rf = mmolidCol_(whichrow);
     968  const Vector<Double> rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
    953969  SpectralCoordinate spc =
    954970    freqTable_.getSpectralCoordinate(md, mp, me, rf, mfreqidCol_(whichrow));
     
    967983}
    968984
    969 void Scantable::setRestFrequencies( double rf, const std::string& name,
     985/**
     986void asap::Scantable::setRestFrequencies( double rf, const std::string& name,
    970987                                          const std::string& unit )
     988**/
     989void Scantable::setRestFrequencies( vector<double> rf, const vector<std::string>& name,
     990                                          const std::string& unit )
     991
    971992{
    972993  ///@todo lookup in line table to fill in name and formattedname
    973994  Unit u(unit);
    974   Quantum<Double> urf(rf, u);
    975   uInt id = moleculeTable_.addEntry(urf.getValue("Hz"), name, "");
     995  //Quantum<Double> urf(rf, u);
     996  Quantum<Vector<Double> >urf(rf, u);
     997  Vector<String> formattedname(0);
     998  //cerr<<"Scantable::setRestFrequnecies="<<urf<<endl;
     999 
     1000  //uInt id = moleculeTable_.addEntry(urf.getValue("Hz"), name, "");
     1001  uInt id = moleculeTable_.addEntry(urf.getValue("Hz"), mathutil::toVectorString(name), formattedname);
    9761002  TableVector<uInt> tabvec(table_, "MOLECULE_ID");
    9771003  tabvec = id;
    9781004}
    9791005
    980 void Scantable::setRestFrequencies( const std::string& name )
     1006/**
     1007void asap::Scantable::setRestFrequencies( const std::string& name )
    9811008{
    9821009  throw(AipsError("setRestFrequencies( const std::string& name ) NYI"));
     1010  ///@todo implement
     1011}
     1012**/
     1013void Scantable::setRestFrequencies( const vector<std::string>& name )
     1014{
     1015  throw(AipsError("setRestFrequencies( const vector<std::string>& name ) NYI"));
    9831016  ///@todo implement
    9841017}
     
    11171150}
    11181151
    1119 }
    1120  //namespace asap
     1152void asap::Scantable::reshapeSpectrum( int nmin, int nmax )
     1153  throw( casa::AipsError )
     1154{
     1155  // assumed that all rows have same nChan
     1156  Vector<Float> arr = specCol_( 0 ) ;
     1157  int nChan = arr.nelements() ;
     1158 
     1159  // if nmin < 0 or nmax < 0, nothing to do
     1160  if (  nmin < 0 ) {
     1161    throw( casa::indexError<int>( nmin, "asap::Scantable::reshapeSpectrum: Invalid range. Negative index is specified." ) ) ;
     1162    }
     1163  if (  nmax < 0  ) {
     1164    throw( casa::indexError<int>( nmax, "asap::Scantable::reshapeSpectrum: Invalid range. Negative index is specified." ) ) ;
     1165  }
     1166 
     1167  // if nmin > nmax, exchange values
     1168  if ( nmin > nmax ) {
     1169    int tmp = nmax ;
     1170    nmax = nmin ;
     1171    nmin = tmp ;
     1172    cout << "Swap values. Applied range is ["
     1173         << nmin << ", " << nmax << "]" << endl ;
     1174  }
     1175 
     1176  // if nmin exceeds nChan, nothing to do
     1177  if ( nmin >= nChan ) {
     1178    throw( casa::indexError<int>( nmin, "asap::Scantable::reshapeSpectrum: Invalid range. Specified minimum exceeds nChan." ) ) ;
     1179  }
     1180 
     1181  // if nmax exceeds nChan, reset nmax to nChan
     1182  if ( nmax >= nChan ) {
     1183    if ( nmin == 0 ) {
     1184      // nothing to do
     1185      cout << "Whole range is selected. Nothing to do." << endl ;
     1186      return ;
     1187    }
     1188    else {
     1189      cout << "Specified maximum exceeds nChan. Applied range is ["
     1190           << nmin << ", " << nChan-1 << "]." << endl ;
     1191      nmax = nChan - 1 ;
     1192    }
     1193  }
     1194 
     1195  // reshape specCol_ and flagCol_
     1196  for ( int irow = 0 ; irow < nrow() ; irow++ ) {
     1197    reshapeSpectrum( nmin, nmax, irow ) ;
     1198  }
     1199
     1200  // update FREQUENCIES subtable
     1201  Double refpix ;
     1202  Double refval ;
     1203  Double increment ;
     1204  int freqnrow = freqTable_.table().nrow() ;
     1205  Vector<uInt> oldId( freqnrow ) ;
     1206  Vector<uInt> newId( freqnrow ) ;
     1207  for ( int irow = 0 ; irow < freqnrow ; irow++ ) {
     1208    freqTable_.getEntry( refpix, refval, increment, irow ) ;
     1209    /***
     1210     * need to shift refpix to nmin
     1211     * note that channel nmin in old index will be channel 0 in new one
     1212     ***/
     1213    refval = refval - ( refpix - nmin ) * increment ;
     1214    refpix = 0 ; 
     1215    freqTable_.setEntry( refpix, refval, increment, irow ) ;
     1216  }
     1217 
     1218  // update nchan
     1219  int newsize = nmax - nmin + 1 ;
     1220  table_.rwKeywordSet().define( "nChan", newsize ) ;
     1221 
     1222  // update bandwidth
     1223  // assumed all spectra in the scantable have same bandwidth
     1224  table_.rwKeywordSet().define( "Bandwidth", increment * newsize ) ;
     1225 
     1226  return ;
     1227}
     1228
     1229void asap::Scantable::reshapeSpectrum( int nmin, int nmax, int irow )
     1230{
     1231  // reshape specCol_ and flagCol_
     1232  Vector<Float> oldspec = specCol_( irow ) ;
     1233  Vector<uChar> oldflag = flagsCol_( irow ) ;
     1234  uInt newsize = nmax - nmin + 1 ;
     1235  specCol_.put( irow, oldspec( Slice( nmin, newsize, 1 ) ) ) ;
     1236  flagsCol_.put( irow, oldflag( Slice( nmin, newsize, 1 ) ) ) ;
     1237
     1238  return ;
     1239}
     1240
     1241void asap::Scantable::regridChannel( int nChan, double dnu )
     1242{
     1243  cout << "Regrid abcissa with channel number " << nChan << " and spectral resoultion " << dnu << "Hz." << endl ;
     1244  // assumed that all rows have same nChan
     1245  Vector<Float> arr = specCol_( 0 ) ;
     1246  int oldsize = arr.nelements() ;
     1247
     1248  // if oldsize == nChan, nothing to do
     1249  if ( oldsize == nChan ) {
     1250    cout << "Specified channel number is same as current one. Nothing to do." << endl ;
     1251    return ;
     1252  }
     1253
     1254  // if oldChan < nChan, unphysical operation
     1255  if ( oldsize < nChan ) {
     1256    cout << "Unphysical operation. Nothing to do." << endl ;
     1257    return ;
     1258  }
     1259
     1260  // change channel number for specCol_ and flagCol_
     1261  Vector<Float> newspec( nChan, 0 ) ;
     1262  Vector<uChar> newflag( nChan, false ) ;
     1263  vector<string> coordinfo = getCoordInfo() ;
     1264  string oldinfo = coordinfo[0] ;
     1265  coordinfo[0] = "Hz" ;
     1266  setCoordInfo( coordinfo ) ;
     1267  for ( int irow = 0 ; irow < nrow() ; irow++ ) {
     1268    regridChannel( nChan, dnu, irow ) ;
     1269  }
     1270  coordinfo[0] = oldinfo ;
     1271  setCoordInfo( coordinfo ) ;
     1272
     1273
     1274  // NOTE: this method does not update metadata such as
     1275  //       FREQUENCIES subtable, nChan, Bandwidth, etc.
     1276
     1277  return ;
     1278}
     1279
     1280void asap::Scantable::regridChannel( int nChan, double dnu, int irow )
     1281{
     1282  // logging
     1283  //ofstream ofs( "average.log", std::ios::out | std::ios::app ) ;
     1284  //ofs << "IFNO = " << getIF( irow ) << " irow = " << irow << endl ;
     1285
     1286  Vector<Float> oldspec = specCol_( irow ) ;
     1287  Vector<uChar> oldflag = flagsCol_( irow ) ;
     1288  Vector<Float> newspec( nChan, 0 ) ;
     1289  Vector<uChar> newflag( nChan, false ) ;
     1290 
     1291  // regrid
     1292  vector<double> abcissa = getAbcissa( irow ) ;
     1293  int oldsize = abcissa.size() ;
     1294  double olddnu = abcissa[1] - abcissa[0] ;
     1295  int refChan = 0 ;
     1296  double frac = 0.0 ;
     1297  double wedge = 0.0 ;
     1298  double pile = 0.0 ;
     1299  double wsum = 0.0 ;
     1300  /***
     1301   * ichan = 0
     1302   ***/
     1303  //ofs << "olddnu = " << olddnu << ", dnu = " << dnu << endl ;
     1304  pile += dnu ;
     1305  wedge = olddnu * ( refChan + 1 ) ;
     1306  while ( wedge < pile ) {
     1307    newspec[0] += olddnu * oldspec[refChan] ;
     1308    newflag[0] = newflag[0] || oldflag[refChan] ;
     1309    //ofs << "channel " << refChan << " is included in new channel 0" << endl ;
     1310    refChan++ ;
     1311    wedge += olddnu ;
     1312    wsum += olddnu ;
     1313    //ofs << "newspec[0] = " << newspec[0] << " wsum = " << wsum << endl ;
     1314  }
     1315  frac = ( wedge - pile ) / olddnu ;
     1316  wsum += ( 1.0 - frac ) * olddnu ;
     1317  newspec[0] += ( 1.0 - frac ) * olddnu * oldspec[refChan] ;
     1318  newflag[0] = newflag[0] || oldflag[refChan] ;
     1319  //ofs << "channel " << refChan << " is partly included in new channel 0" << " with fraction of " << ( 1.0 - frac ) << endl ;
     1320  //ofs << "newspec[0] = " << newspec[0] << " wsum = " << wsum << endl ;
     1321  newspec[0] /= wsum ;
     1322  //ofs << "newspec[0] = " << newspec[0] << endl ;
     1323  //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
     1324
     1325  /***
     1326   * ichan = 1 - nChan-2
     1327   ***/
     1328  for ( int ichan = 1 ; ichan < nChan - 1 ; ichan++ ) {
     1329    pile += dnu ;
     1330    newspec[ichan] += frac * olddnu * oldspec[refChan] ;
     1331    newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
     1332    //ofs << "channel " << refChan << " is partly included in new channel " << ichan << " with fraction of " << frac << endl ;
     1333    refChan++ ;
     1334    wedge += olddnu ;
     1335    wsum = frac * olddnu ;
     1336    //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
     1337    while ( wedge < pile ) {
     1338      newspec[ichan] += olddnu * oldspec[refChan] ;
     1339      newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
     1340      //ofs << "channel " << refChan << " is included in new channel " << ichan << endl ;
     1341      refChan++ ;
     1342      wedge += olddnu ;
     1343      wsum += olddnu ;
     1344      //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
     1345    }
     1346    frac = ( wedge - pile ) / olddnu ;
     1347    wsum += ( 1.0 - frac ) * olddnu ;
     1348    newspec[ichan] += ( 1.0 - frac ) * olddnu * oldspec[refChan] ;
     1349    newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
     1350    //ofs << "channel " << refChan << " is partly included in new channel " << ichan << " with fraction of " << ( 1.0 - frac ) << endl ;
     1351    //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
     1352    //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
     1353    newspec[ichan] /= wsum ;
     1354    //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << endl ;
     1355  }
     1356
     1357  /***
     1358   * ichan = nChan-1
     1359   ***/
     1360  // NOTE: Assumed that all spectra have the same bandwidth
     1361  pile += dnu ;
     1362  newspec[nChan-1] += frac * olddnu * oldspec[refChan] ;
     1363  newflag[nChan-1] = newflag[nChan-1] || oldflag[refChan] ;
     1364  //ofs << "channel " << refChan << " is partly included in new channel " << nChan-1 << " with fraction of " << frac << endl ;
     1365  refChan++ ;
     1366  wedge += olddnu ;
     1367  wsum = frac * olddnu ;
     1368  //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
     1369  for ( int jchan = refChan ; jchan < oldsize ; jchan++ ) {
     1370    newspec[nChan-1] += olddnu * oldspec[jchan] ;
     1371    newflag[nChan-1] = newflag[nChan-1] || oldflag[jchan] ;
     1372    wsum += olddnu ;
     1373    //ofs << "channel " << jchan << " is included in new channel " << nChan-1 << " with fraction of " << frac << endl ;
     1374    //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
     1375  }
     1376  //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
     1377  //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
     1378  newspec[nChan-1] /= wsum ;
     1379  //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << endl ;
     1380 
     1381  specCol_.put( irow, newspec ) ;
     1382  flagsCol_.put( irow, newflag ) ;
     1383
     1384  // ofs.close() ;
     1385
     1386  return ;
     1387}
     1388
     1389}
     1390//namespace asap
Note: See TracChangeset for help on using the changeset viewer.