Changeset 926


Ignore:
Timestamp:
03/24/06 13:35:32 (18 years ago)
Author:
mar637
Message:

added frequencyAlign(). fixed bug in gainElevation. Was assigning spectrum from tsys

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/STMath.cpp

    r917 r926  
    6060
    6161CountedPtr<Scantable>
    62 STMath::average( const std::vector<CountedPtr<Scantable> >& in,
     62STMath::average( const std::vector<CountedPtr<Scantable> >& scantbls,
    6363                 const std::vector<bool>& mask,
    6464                 const std::string& weight,
     
    6666                 bool alignfreq)
    6767{
    68   if ( avmode == "SCAN" && in.size() != 1 )
     68  if ( avmode == "SCAN" && scantbls.size() != 1 )
    6969    throw(AipsError("Can't perform 'SCAN' averaging on multiple tables"));
    7070  WeightType wtype = stringToWeight(weight);
     71
     72  std::vector<CountedPtr<Scantable> >* in = const_cast<std::vector<CountedPtr<Scantable> >*>(&scantbls);
     73  std::vector<CountedPtr<Scantable> > inaligned;
     74  if ( alignfreq ) {
     75    // take first row of first scantable as reference epoch
     76    String epoch = scantbls[0]->getTime(0);
     77    for (int i=0; i< scantbls.size(); ++i ) {
     78      inaligned.push_back(frequencyAlign(scantbls[i], epoch));
     79    }
     80    in = &inaligned;
     81  }
     82
    7183  // output
    7284  // clone as this is non insitu
    7385  bool insitu = insitu_;
    7486  setInsitu(false);
    75   CountedPtr< Scantable > out = getScantable(in[0], true);
     87  CountedPtr< Scantable > out = getScantable((*in)[0], true);
    7688  setInsitu(insitu);
    77   std::vector<CountedPtr<Scantable> >::const_iterator stit = in.begin();
     89  std::vector<CountedPtr<Scantable> >::const_iterator stit = in->begin();
    7890  ++stit;
    79   while ( stit != in.end() ) {
     91  while ( stit != in->end() ) {
    8092    out->appendToHistoryTable((*stit)->history());
    8193    ++stit;
     
    94106  // set up the output table rows. These are based on the structure of the
    95107  // FIRST scantable in the vector
    96   const Table& baset = in[0]->table();
     108  const Table& baset = (*in)[0]->table();
    97109
    98110  Block<String> cols(3);
     
    104116    cols[3] = String("SRCNAME");
    105117  }
    106   if ( avmode == "SCAN"  && in.size() == 1) {
     118  if ( avmode == "SCAN"  && in->size() == 1) {
    107119    cols.resize(4);
    108120    cols[3] = String("SCANNO");
     
    128140
    129141  for (uInt i=0; i < tout.nrow(); ++i) {
    130     for ( int j=0; j < in.size(); ++j ) {
    131       const Table& tin = in[j]->table();
     142    for ( int j=0; j < in->size(); ++j ) {
     143      const Table& tin = (*in)[j]->table();
    132144      const TableRecord& rec = row.get(i);
    133145      ROScalarColumn<Double> tmp(tin, "TIME");
     
    270282  ROArrayColumn<Float> outtsysCol(tout, "TSYS");
    271283  ArrayColumn<uChar> outflagCol(tout, "FLAGTRA");
    272 
    273284  for (uInt i=0; i < tout.nrow(); ++i) {
    274285    const TableRecord& rec = row.get(i);
     
    303314    outspecCol.put(i, quot.getArray());
    304315    outflagCol.put(i, flagsFromMA(quot));
     316  }
     317  // renumber scanno
     318  TableIterator it(tout, "SCANNO");
     319  uInt i = 0;
     320  while ( !it.pastEnd() ) {
     321    Table t = it.table();
     322    TableVector<uInt> vec(t, "SCANNO");
     323    vec = i;
     324    ++i;
     325    ++it;
    305326  }
    306327  return out;
     
    480501  // initialize the lookup table if necessary
    481502  if ( lookup.empty() ) {
    482     lookup["NEAR"]   = InterpolateArray1D<Double,Float>::nearestNeighbour;
    483     lookup["LIN"] = InterpolateArray1D<Double,Float>::linear;
    484     lookup["CUB"]  = InterpolateArray1D<Double,Float>::cubic;
    485     lookup["SPL"]  = InterpolateArray1D<Double,Float>::spline;
     503    lookup["nearest"]   = InterpolateArray1D<Double,Float>::nearestNeighbour;
     504    lookup["linear"] = InterpolateArray1D<Double,Float>::linear;
     505    lookup["cubic"]  = InterpolateArray1D<Double,Float>::cubic;
     506    lookup["spline"]  = InterpolateArray1D<Double,Float>::spline;
    486507  }
    487508
     
    526547  // Get elevation data from Scantable and convert to degrees
    527548  CountedPtr< Scantable > out = getScantable(in, false);
    528   Table& tab = in->table();
     549  Table& tab = out->table();
    529550  ROScalarColumn<Float> elev(tab, "ELEVATION");
    530551  Vector<Float> x = elev.getColumn();
     
    641662    flagCol.put(i, flagsFromMA(ma));
    642663    if ( dotsys ) {
    643       Vector<Float> tsys;
    644       tsysCol.get(i, tsys);
     664      Vector<Float> tsys = tsysCol(i);
    645665      tsys *= factor[i];
    646       specCol.put(i,tsys);
     666      tsysCol.put(i,tsys);
    647667    }
    648668  }
     
    759779{
    760780  CountedPtr< Scantable > out = getScantable(in, false);
    761   Table& tab = in->table();
     781
     782  Table tab = out->table();
    762783  ROScalarColumn<Float> elev(tab, "ELEVATION");
    763784  ArrayColumn<Float> specCol(tab, "SPECTRA");
     
    766787    Float zdist = Float(C::pi_2) - elev(i);
    767788    Float factor = exp(tau)/cos(zdist);
    768     MaskedArray<Float> ma  = maskedArray(specCol(i), flagCol(i));
     789    MaskedArray<Float> ma = maskedArray(specCol(i), flagCol(i));
    769790    ma *= factor;
    770791    specCol.put(i, ma.getArray());
     
    830851  ScalarColumn<uInt> scannocol(tout,"SCANNO"), focusidcol(tout,"FOCUS_ID");
    831852  // Renumber SCANNO to be 0-based
    832   uInt offset = min(scannocol.getColumn());
    833   TableVector<uInt> scannos(tout, "SCANNO");
     853  Vector<uInt> scannos = scannocol.getColumn();
     854  uInt offset = min(scannos);
    834855  scannos -= offset;
    835   uInt newscanno = max(scannocol.getColumn())+1;
     856  scannocol.putColumn(scannos);
     857  uInt newscanno = max(scannos)+1;
    836858  ++it;
    837859  while ( it != in.end() ){
     
    958980  asap::STMath::frequencyAlign( const CountedPtr< Scantable > & in,
    959981                                const std::string & refTime,
    960                                 const std::string & method, bool perfreqid )
     982                                const std::string & method)
    961983{
    962984  CountedPtr< Scantable > out = getScantable(in, false);
     
    9941016  }
    9951017  MFrequency::Types system = in->frequencies().getFrame();
     1018  out->frequencies().setFrame(system);
    9961019  // set up the iterator
    997   Block<String> cols(3);
    998   // select the by constant direction
     1020  Block<String> cols(4);
     1021  // select by constant direction
    9991022  cols[0] = String("SRCNAME");
    10001023  cols[1] = String("BEAMNO");
    10011024  // select by IF ( no of channels varies over this )
    10021025  cols[2] = String("IFNO");
    1003   // handle freqid based alignment
    1004   if ( perfreqid ) {
    1005     cols.resize(4);
    1006     cols[3] = String("FREQ_ID");
    1007   }
     1026  // select by restfrequency
     1027  cols[3] = String("MOLECULE_ID");
    10081028  TableIterator iter(tout, cols);
    1009   while (!iter.pastEnd()) {
     1029  while ( !iter.pastEnd() ) {
    10101030    Table t = iter.table();
    10111031    MDirection::ROScalarColumn dirCol(t, "DIRECTION");
    1012     ScalarColumn<uInt> freqidCol(t, "FREQ_ID");
    1013     // get the SpectralCoordinate for the freqid, which we are iterating over
    1014     SpectralCoordinate sC = in->frequencies().getSpectralCoordinate(freqidCol(0));
     1032    TableIterator fiter(t, "FREQ_ID");
    10151033    // determine nchan from the first row. This should work as
    1016     // we are iterting over IFNO
     1034    // we are iterating over BEAMNO and IFNO    // we should have constant direction
     1035
    10171036    ROArrayColumn<Float> sCol(t, "SPECTRA");
     1037    MDirection direction = dirCol(0);
    10181038    uInt nchan = sCol(0).nelements();
    1019     // we should have constant direction
    1020     FrequencyAligner<Float> fa(sC, nchan, refEpoch, dirCol(0), refPos, system);
    1021     // realign the SpectralCOordinate and put into the output Scantable
    1022     Vector<String> units(1);
    1023     units = String("Hz");
    1024     Bool linear=True;
    1025     SpectralCoordinate sc2 = fa.alignedSpectralCoordinate(linear);
    1026     sc2.setWorldAxisUnits(units);
    1027     out->frequencies().addEntry(sc2.referencePixel()[0],
    1028                                 sc2.referenceValue()[0],
    1029                                 sc2.increment()[0]);
    1030     // create the abcissa for IF based alignment
    1031     Vector<Double> abc;
    1032     if ( !perfreqid ) {
    1033       abc.resize(nchan);
     1039    while ( !fiter.pastEnd() ) {
     1040      Table ftab = fiter.table();
     1041      ScalarColumn<uInt> freqidCol(ftab, "FREQ_ID");
     1042      // get the SpectralCoordinate for the freqid, which we are iterating over
     1043      cout << freqidCol.getColumn() << endl;
     1044      SpectralCoordinate sC = in->frequencies().getSpectralCoordinate(freqidCol(0));
     1045      FrequencyAligner<Float> fa( sC, nchan, refEpoch,
     1046                                  direction, refPos, system );
     1047      // realign the SpectralCoordinate and put into the output Scantable
     1048      Vector<String> units(1);
     1049      units = String("Hz");
     1050      Bool linear=True;
     1051      SpectralCoordinate sc2 = fa.alignedSpectralCoordinate(linear);
     1052      sc2.setWorldAxisUnits(units);
     1053      out->frequencies().addEntry(sc2.referencePixel()[0],
     1054                                  sc2.referenceValue()[0],
     1055                                  sc2.increment()[0]);
     1056      // create the "global" abcissa for alignment with same FREQ_ID
     1057      Vector<Double> abc(nchan);
    10341058      Double w;
    10351059      for (uInt i=0; i<nchan; i++) {
     
    10371061        abc[i] = w;
    10381062      }
    1039     }
    1040     // cache abcissa for same time stamps, so iterate over those
    1041     TableIterator timeiter(t, "TIME");
    1042     while ( !timeiter.pastEnd() ) {
    1043       Table tab = timeiter.table();
    1044       ArrayColumn<Float> specCol(tab, "SPECTRA");
    1045       ArrayColumn<uChar> flagCol(tab, "FLAGTRA");
    1046       MEpoch::ROScalarColumn timeCol(tab, "TIME");
    1047       // use align abcissa cache after the first row
    1048       bool first = true;
    1049       // these rows should be just be POLNO
    1050       for (int i=0; i<tab.nrow(); ++i) {
    1051         // input values
    1052         Vector<uChar> flag = flagCol(i);
    1053         Vector<Bool> mask(flag.shape());
    1054         Vector<Float> specOut;
    1055         Vector<Bool> maskOut;Vector<uChar> flagOut;
    1056         convertArray(mask, flag);
    1057         // alignment
    1058         if ( perfreqid ) {
    1059           Bool ok = fa.align(specOut, maskOut, specCol(i),
    1060                         mask, timeCol(i), !first,
    1061                         interp, False);
    1062         } else {
    1063           Bool ok = fa.align(specOut, maskOut, abc, specCol(i),
    1064                         mask, timeCol(i), !first,
    1065                         interp, False);
     1063      // cache abcissa for same time stamps, so iterate over those
     1064      TableIterator timeiter(ftab, "TIME");
     1065      while ( !timeiter.pastEnd() ) {
     1066        Table tab = timeiter.table();
     1067        ArrayColumn<Float> specCol(tab, "SPECTRA");
     1068        ArrayColumn<uChar> flagCol(tab, "FLAGTRA");
     1069        MEpoch::ROScalarColumn timeCol(tab, "TIME");
     1070        // use align abcissa cache after the first row
     1071        bool first = true;
     1072        // these rows should be just be POLNO
     1073        for (int i=0; i<tab.nrow(); ++i) {
     1074          // input values
     1075          Vector<uChar> flag = flagCol(i);
     1076          Vector<Bool> mask(flag.shape());
     1077          Vector<Float> specOut, spec;
     1078          spec  = specCol(i);
     1079          Vector<Bool> maskOut;Vector<uChar> flagOut;
     1080          convertArray(mask, flag);
     1081          // alignment
     1082          Bool ok = fa.align(specOut, maskOut, abc, spec,
     1083                             mask, timeCol(i), !first,
     1084                             interp, False);
     1085          // back into scantable
     1086          cout << spec[spec.nelements()/2] << " --- " << specOut[specOut.nelements()/2] << endl;
     1087          flagOut.resize(maskOut.nelements());
     1088          convertArray(flagOut, maskOut);
     1089          flagCol.put(i, flagOut);
     1090          specCol.put(i, specOut);
     1091          // start abcissa caching
     1092          first = false;
    10661093        }
    1067         // back into scantable
    1068         flagOut.resize(maskOut.nelements());
    1069         convertArray(flagOut, maskOut);
    1070         flagCol.put(i, flagOut);
    1071         specCol.put(i, specOut);
    1072         // start ancissa caching
    1073         first = false;
     1094        // next timestamp
     1095        ++timeiter;
    10741096      }
    1075       ++timeiter;
     1097      // nect FREQ_ID
     1098      ++fiter;
    10761099    }
    10771100    // next aligner
Note: See TracChangeset for help on using the changeset viewer.