Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/STMath.cpp

    r2345 r2467  
    5757using namespace asap;
    5858
     59// 2012/02/17 TN
     60// Since STGrid is implemented, average doesn't consider direction
     61// when accumulating
    5962// tolerance for direction comparison (rad)
    60 #define TOL_OTF    1.0e-15
    61 #define TOL_POINT  2.9088821e-4  // 1 arcmin
     63// #define TOL_OTF    1.0e-15
     64// #define TOL_POINT  2.9088821e-4  // 1 arcmin
    6265
    6366STMath::STMath(bool insitu) :
     
    8386  WeightType wtype = stringToWeight(weight);
    8487
     88  // 2012/02/17 TN
     89  // Since STGrid is implemented, average doesn't consider direction
     90  // when accumulating
    8591  // check if OTF observation
    86   String obstype = in[0]->getHeader().obstype ;
    87   Double tol = 0.0 ;
    88   if ( (obstype.find( "OTF" ) != String::npos) || (obstype.find( "OBSERVE_TARGET" ) != String::npos) ) {
    89     tol = TOL_OTF ;
    90   }
    91   else {
    92     tol = TOL_POINT ;
    93   }
     92//   String obstype = in[0]->getHeader().obstype ;
     93//   Double tol = 0.0 ;
     94//   if ( (obstype.find( "OTF" ) != String::npos) || (obstype.find( "OBSERVE_TARGET" ) != String::npos) ) {
     95//     tol = TOL_OTF ;
     96//   }
     97//   else {
     98//     tol = TOL_POINT ;
     99//   }
    94100
    95101  // output
     
    142148  while (!iter.pastEnd()) {
    143149    Table subt = iter.table();
    144 //     // copy the first row of this selection into the new table
    145 //     tout.addRow();
    146 //     TableCopy::copyRows(tout, subt, outrowCount, 0, 1);
    147 //     // re-index to 0
    148 //     if ( avmode != "SCAN" && avmode != "SOURCE" ) {
    149 //       scanColOut.put(outrowCount, uInt(0));
     150    // copy the first row of this selection into the new table
     151    tout.addRow();
     152    TableCopy::copyRows(tout, subt, outrowCount, 0, 1);
     153    // re-index to 0
     154    if ( avmode != "SCAN" && avmode != "SOURCE" ) {
     155      scanColOut.put(outrowCount, uInt(0));
     156    }
     157    ++outrowCount;
     158    // 2012/02/17 TN
     159    // Since STGrid is implemented, average doesn't consider direction
     160    // when accumulating
     161//     MDirection::ScalarColumn dircol ;
     162//     dircol.attach( subt, "DIRECTION" ) ;
     163//     Int length = subt.nrow() ;
     164//     vector< Vector<Double> > dirs ;
     165//     vector<int> indexes ;
     166//     for ( Int i = 0 ; i < length ; i++ ) {
     167//       Vector<Double> t = dircol(i).getAngle(Unit(String("rad"))).getValue() ;
     168//       //os << << count++ << ": " ;
     169//       //os << "[" << t[0] << "," << t[1] << "]" << LogIO::POST ;
     170//       bool adddir = true ;
     171//       for ( uInt j = 0 ; j < dirs.size() ; j++ ) {
     172//         //if ( allTrue( t == dirs[j] ) ) {
     173//         Double dx = t[0] - dirs[j][0] ;
     174//         Double dy = t[1] - dirs[j][1] ;
     175//         Double dd = sqrt( dx * dx + dy * dy ) ;
     176//         //if ( allNearAbs( t, dirs[j], tol ) ) {
     177//         if ( dd <= tol ) {
     178//           adddir = false ;
     179//           break ;
     180//         }
     181//       }
     182//       if ( adddir ) {
     183//         dirs.push_back( t ) ;
     184//         indexes.push_back( i ) ;
     185//       }
    150186//     }
    151 //     ++outrowCount;
    152     MDirection::ScalarColumn dircol ;
    153     dircol.attach( subt, "DIRECTION" ) ;
    154     Int length = subt.nrow() ;
    155     vector< Vector<Double> > dirs ;
    156     vector<int> indexes ;
    157     for ( Int i = 0 ; i < length ; i++ ) {
    158       Vector<Double> t = dircol(i).getAngle(Unit(String("rad"))).getValue() ;
    159       //os << << count++ << ": " ;
    160       //os << "[" << t[0] << "," << t[1] << "]" << LogIO::POST ;
    161       bool adddir = true ;
    162       for ( uInt j = 0 ; j < dirs.size() ; j++ ) {
    163         //if ( allTrue( t == dirs[j] ) ) {
    164         Double dx = t[0] - dirs[j][0] ;
    165         Double dy = t[1] - dirs[j][1] ;
    166         Double dd = sqrt( dx * dx + dy * dy ) ;
    167         //if ( allNearAbs( t, dirs[j], tol ) ) {
    168         if ( dd <= tol ) {
    169           adddir = false ;
    170           break ;
    171         }
    172       }
    173       if ( adddir ) {
    174         dirs.push_back( t ) ;
    175         indexes.push_back( i ) ;
    176       }
    177     }
    178     uInt rowNum = dirs.size() ;
    179     tout.addRow( rowNum ) ;
    180     for ( uInt i = 0 ; i < rowNum ; i++ ) {
    181       TableCopy::copyRows( tout, subt, outrowCount+i, indexes[i], 1 ) ;
    182       // re-index to 0
    183       if ( avmode != "SCAN" && avmode != "SOURCE" ) {
    184         scanColOut.put(outrowCount+i, uInt(0));
    185       }       
    186     }
    187     outrowCount += rowNum ;
     187//     uInt rowNum = dirs.size() ;
     188//     tout.addRow( rowNum ) ;
     189//     for ( uInt i = 0 ; i < rowNum ; i++ ) {
     190//       TableCopy::copyRows( tout, subt, outrowCount+i, indexes[i], 1 ) ;
     191//       // re-index to 0
     192//       if ( avmode != "SCAN" && avmode != "SOURCE" ) {
     193//         scanColOut.put(outrowCount+i, uInt(0));
     194//       }       
     195//     }
     196//     outrowCount += rowNum ;
    188197    ++iter;
    189198  }
     
    218227      }
    219228
    220       vector<uInt> removeRows ;
    221       uInt nrsubt = subt.nrow() ;
    222       for ( uInt irow = 0 ; irow < nrsubt ; irow++ ) {
    223         //if ( !allTrue((subt.col("DIRECTION").getArrayDouble(TableExprId(irow)))==rec.asArrayDouble("DIRECTION")) ) {
    224         Vector<Double> x0 = (subt.col("DIRECTION").getArrayDouble(TableExprId(irow))) ;
    225         Vector<Double> x1 = rec.asArrayDouble("DIRECTION") ;
    226         double dx = x0[0] - x1[0];
    227         double dy = x0[1] - x1[1];
    228         Double dd = sqrt( dx * dx + dy * dy ) ;
    229         //if ( !allNearAbs((subt.col("DIRECTION").getArrayDouble(TableExprId(irow))), rec.asArrayDouble("DIRECTION"), tol ) ) {
    230         if ( dd > tol ) {
    231           removeRows.push_back( irow ) ;
    232         }
    233       }
    234       if ( removeRows.size() != 0 ) {
    235         subt.removeRow( removeRows ) ;
    236       }
     229      // 2012/02/17 TN
     230      // Since STGrid is implemented, average doesn't consider direction
     231      // when accumulating
     232//       vector<uInt> removeRows ;
     233//       uInt nrsubt = subt.nrow() ;
     234//       for ( uInt irow = 0 ; irow < nrsubt ; irow++ ) {
     235//         //if ( !allTrue((subt.col("DIRECTION").getArrayDouble(TableExprId(irow)))==rec.asArrayDouble("DIRECTION")) ) {
     236//         Vector<Double> x0 = (subt.col("DIRECTION").getArrayDouble(TableExprId(irow))) ;
     237//         Vector<Double> x1 = rec.asArrayDouble("DIRECTION") ;
     238//         double dx = x0[0] - x1[0];
     239//         double dy = x0[1] - x1[1];
     240//         Double dd = sqrt( dx * dx + dy * dy ) ;
     241//         //if ( !allNearAbs((subt.col("DIRECTION").getArrayDouble(TableExprId(irow))), rec.asArrayDouble("DIRECTION"), tol ) ) {
     242//         if ( dd > tol ) {
     243//           removeRows.push_back( irow ) ;
     244//         }
     245//       }
     246//       if ( removeRows.size() != 0 ) {
     247//         subt.removeRow( removeRows ) ;
     248//       }
    237249     
    238       if ( nrsubt == removeRows.size() )
    239         throw(AipsError("Averaging data is empty.")) ;
     250//       if ( nrsubt == removeRows.size() )
     251//         throw(AipsError("Averaging data is empty.")) ;
    240252
    241253      specCol.attach(subt,"SPECTRA");
     
    321333{
    322334  (void) mode; // currently unused
     335  // 2012/02/17 TN
     336  // Since STGrid is implemented, average doesn't consider direction
     337  // when accumulating
    323338  // check if OTF observation
    324   String obstype = in->getHeader().obstype ;
    325   Double tol = 0.0 ;
    326   if ( obstype.find( "OTF" ) != String::npos ) {
    327     tol = TOL_OTF ;
    328   }
    329   else {
    330     tol = TOL_POINT ;
    331   }
     339//   String obstype = in->getHeader().obstype ;
     340//   Double tol = 0.0 ;
     341//   if ( obstype.find( "OTF" ) != String::npos ) {
     342//     tol = TOL_OTF ;
     343//   }
     344//   else {
     345//     tol = TOL_POINT ;
     346//   }
    332347
    333348  // clone as this is non insitu
     
    362377    flagCol.attach(subt,"FLAGTRA");
    363378    tsysCol.attach(subt,"TSYS");
    364 //     tout.addRow();
    365 //     TableCopy::copyRows(tout, subt, outrowCount, 0, 1);
    366 //     if ( avmode != "SCAN") {
    367 //       scanColOut.put(outrowCount, uInt(0));
     379
     380    tout.addRow();
     381    TableCopy::copyRows(tout, subt, outrowCount, 0, 1);
     382    if ( avmode != "SCAN") {
     383      scanColOut.put(outrowCount, uInt(0));
     384    }
     385    Vector<Float> tmp;
     386    specCol.get(0, tmp);
     387    uInt nchan = tmp.nelements();
     388    // have to do channel by channel here as MaskedArrMath
     389    // doesn't have partialMedians
     390    Vector<uChar> flags = flagCol.getColumn(Slicer(Slice(0)));
     391    Vector<Float> outspec(nchan);
     392    Vector<uChar> outflag(nchan,0);
     393    Vector<Float> outtsys(1);/// @fixme when tsys is channel based
     394    for (uInt i=0; i<nchan; ++i) {
     395      Vector<Float> specs = specCol.getColumn(Slicer(Slice(i)));
     396      MaskedArray<Float> ma = maskedArray(specs,flags);
     397      outspec[i] = median(ma);
     398      if ( allEQ(ma.getMask(), False) )
     399        outflag[i] = userflag;// flag data
     400    }
     401    outtsys[0] = median(tsysCol.getColumn());
     402    specColOut.put(outrowCount, outspec);
     403    flagColOut.put(outrowCount, outflag);
     404    tsysColOut.put(outrowCount, outtsys);
     405    Double intsum = sum(intCol.getColumn());
     406    intColOut.put(outrowCount, intsum);
     407    ++outrowCount;
     408    ++iter;
     409
     410    // 2012/02/17 TN
     411    // Since STGrid is implemented, average doesn't consider direction
     412    // when accumulating
     413//     MDirection::ScalarColumn dircol ;
     414//     dircol.attach( subt, "DIRECTION" ) ;
     415//     Int length = subt.nrow() ;
     416//     vector< Vector<Double> > dirs ;
     417//     vector<int> indexes ;
     418//     // Handle MX mode averaging
     419//     if (in->nbeam() > 1 ) {     
     420//       length = 1;
    368421//     }
    369 //     Vector<Float> tmp;
    370 //     specCol.get(0, tmp);
    371 //     uInt nchan = tmp.nelements();
    372 //     // have to do channel by channel here as MaskedArrMath
    373 //     // doesn't have partialMedians
    374 //     Vector<uChar> flags = flagCol.getColumn(Slicer(Slice(0)));
    375 //     Vector<Float> outspec(nchan);
    376 //     Vector<uChar> outflag(nchan,0);
    377 //     Vector<Float> outtsys(1);/// @fixme when tsys is channel based
    378 //     for (uInt i=0; i<nchan; ++i) {
    379 //       Vector<Float> specs = specCol.getColumn(Slicer(Slice(i)));
    380 //       MaskedArray<Float> ma = maskedArray(specs,flags);
    381 //       outspec[i] = median(ma);
    382 //       if ( allEQ(ma.getMask(), False) )
    383 //         outflag[i] = userflag;// flag data
     422//     for ( Int i = 0 ; i < length ; i++ ) {
     423//       Vector<Double> t = dircol(i).getAngle(Unit(String("rad"))).getValue() ;
     424//       bool adddir = true ;
     425//       for ( uInt j = 0 ; j < dirs.size() ; j++ ) {
     426//         //if ( allTrue( t == dirs[j] ) ) {
     427//         Double dx = t[0] - dirs[j][0] ;
     428//         Double dy = t[1] - dirs[j][1] ;
     429//         Double dd = sqrt( dx * dx + dy * dy ) ;
     430//         //if ( allNearAbs( t, dirs[j], tol ) ) {
     431//         if ( dd <= tol ) {
     432//           adddir = false ;
     433//           break ;
     434//         }
     435//       }
     436//       if ( adddir ) {
     437//         dirs.push_back( t ) ;
     438//         indexes.push_back( i ) ;
     439//       }
    384440//     }
    385 //     outtsys[0] = median(tsysCol.getColumn());
    386 //     specColOut.put(outrowCount, outspec);
    387 //     flagColOut.put(outrowCount, outflag);
    388 //     tsysColOut.put(outrowCount, outtsys);
    389 //     Double intsum = sum(intCol.getColumn());
    390 //     intColOut.put(outrowCount, intsum);
    391 //     ++outrowCount;
     441//     uInt rowNum = dirs.size() ;
     442//     tout.addRow( rowNum );
     443//     for ( uInt i = 0 ; i < rowNum ; i++ ) {
     444//       TableCopy::copyRows(tout, subt, outrowCount+i, indexes[i], 1) ;
     445//       // Handle MX mode averaging
     446//       if ( avmode != "SCAN") {
     447//         scanColOut.put(outrowCount+i, uInt(0));
     448//       }
     449//     }
     450//     MDirection::ScalarColumn dircolOut ;
     451//     dircolOut.attach( tout, "DIRECTION" ) ;
     452//     for ( uInt irow = 0 ; irow < rowNum ; irow++ ) {
     453//       Vector<Double> t = \
     454//      dircolOut(outrowCount+irow).getAngle(Unit(String("rad"))).getValue() ;
     455//       Vector<Float> tmp;
     456//       specCol.get(0, tmp);
     457//       uInt nchan = tmp.nelements();
     458//       // have to do channel by channel here as MaskedArrMath
     459//       // doesn't have partialMedians
     460//       Vector<uChar> flags = flagCol.getColumn(Slicer(Slice(0)));
     461//       // mask spectra for different DIRECTION
     462//       for ( uInt jrow = 0 ; jrow < subt.nrow() ; jrow++ ) {
     463//         Vector<Double> direction = \
     464//        dircol(jrow).getAngle(Unit(String("rad"))).getValue() ;
     465//         //if ( t[0] != direction[0] || t[1] != direction[1] ) {
     466//         Double dx = t[0] - direction[0];
     467//         Double dy = t[1] - direction[1];
     468//         Double dd = sqrt(dx*dx + dy*dy);
     469//         //if ( !allNearAbs( t, direction, tol ) ) {
     470//         if ( dd > tol &&  in->nbeam() < 2 ) {
     471//           flags[jrow] = userflag ;
     472//         }
     473//       }
     474//       Vector<Float> outspec(nchan);
     475//       Vector<uChar> outflag(nchan,0);
     476//       Vector<Float> outtsys(1);/// @fixme when tsys is channel based
     477//       for (uInt i=0; i<nchan; ++i) {
     478//         Vector<Float> specs = specCol.getColumn(Slicer(Slice(i)));
     479//         MaskedArray<Float> ma = maskedArray(specs,flags);
     480//         outspec[i] = median(ma);
     481//         if ( allEQ(ma.getMask(), False) )
     482//           outflag[i] = userflag;// flag data
     483//       }
     484//       outtsys[0] = median(tsysCol.getColumn());
     485//       specColOut.put(outrowCount+irow, outspec);
     486//       flagColOut.put(outrowCount+irow, outflag);
     487//       tsysColOut.put(outrowCount+irow, outtsys);
     488//       Vector<Double> integ = intCol.getColumn() ;
     489//       MaskedArray<Double> mi = maskedArray( integ, flags ) ;
     490//       Double intsum = sum(mi);
     491//       intColOut.put(outrowCount+irow, intsum);
     492//     }
     493//     outrowCount += rowNum ;
    392494//     ++iter;
    393     MDirection::ScalarColumn dircol ;
    394     dircol.attach( subt, "DIRECTION" ) ;
    395     Int length = subt.nrow() ;
    396     vector< Vector<Double> > dirs ;
    397     vector<int> indexes ;
    398     // Handle MX mode averaging
    399     if (in->nbeam() > 1 ) {     
    400       length = 1;
    401     }
    402     for ( Int i = 0 ; i < length ; i++ ) {
    403       Vector<Double> t = dircol(i).getAngle(Unit(String("rad"))).getValue() ;
    404       bool adddir = true ;
    405       for ( uInt j = 0 ; j < dirs.size() ; j++ ) {
    406         //if ( allTrue( t == dirs[j] ) ) {
    407         Double dx = t[0] - dirs[j][0] ;
    408         Double dy = t[1] - dirs[j][1] ;
    409         Double dd = sqrt( dx * dx + dy * dy ) ;
    410         //if ( allNearAbs( t, dirs[j], tol ) ) {
    411         if ( dd <= tol ) {
    412           adddir = false ;
    413           break ;
    414         }
    415       }
    416       if ( adddir ) {
    417         dirs.push_back( t ) ;
    418         indexes.push_back( i ) ;
    419       }
    420     }
    421     uInt rowNum = dirs.size() ;
    422     tout.addRow( rowNum );
    423     for ( uInt i = 0 ; i < rowNum ; i++ ) {
    424       TableCopy::copyRows(tout, subt, outrowCount+i, indexes[i], 1) ;
    425       // Handle MX mode averaging
    426       if ( avmode != "SCAN") {
    427         scanColOut.put(outrowCount+i, uInt(0));
    428       }
    429     }
    430     MDirection::ScalarColumn dircolOut ;
    431     dircolOut.attach( tout, "DIRECTION" ) ;
    432     for ( uInt irow = 0 ; irow < rowNum ; irow++ ) {
    433       Vector<Double> t = \
    434         dircolOut(outrowCount+irow).getAngle(Unit(String("rad"))).getValue() ;
    435       Vector<Float> tmp;
    436       specCol.get(0, tmp);
    437       uInt nchan = tmp.nelements();
    438       // have to do channel by channel here as MaskedArrMath
    439       // doesn't have partialMedians
    440       Vector<uChar> flags = flagCol.getColumn(Slicer(Slice(0)));
    441       // mask spectra for different DIRECTION
    442       for ( uInt jrow = 0 ; jrow < subt.nrow() ; jrow++ ) {
    443         Vector<Double> direction = \
    444           dircol(jrow).getAngle(Unit(String("rad"))).getValue() ;
    445         //if ( t[0] != direction[0] || t[1] != direction[1] ) {
    446         Double dx = t[0] - direction[0];
    447         Double dy = t[1] - direction[1];
    448         Double dd = sqrt(dx*dx + dy*dy);
    449         //if ( !allNearAbs( t, direction, tol ) ) {
    450         if ( dd > tol &&  in->nbeam() < 2 ) {
    451           flags[jrow] = userflag ;
    452         }
    453       }
    454       Vector<Float> outspec(nchan);
    455       Vector<uChar> outflag(nchan,0);
    456       Vector<Float> outtsys(1);/// @fixme when tsys is channel based
    457       for (uInt i=0; i<nchan; ++i) {
    458         Vector<Float> specs = specCol.getColumn(Slicer(Slice(i)));
    459         MaskedArray<Float> ma = maskedArray(specs,flags);
    460         outspec[i] = median(ma);
    461         if ( allEQ(ma.getMask(), False) )
    462           outflag[i] = userflag;// flag data
    463       }
    464       outtsys[0] = median(tsysCol.getColumn());
    465       specColOut.put(outrowCount+irow, outspec);
    466       flagColOut.put(outrowCount+irow, outflag);
    467       tsysColOut.put(outrowCount+irow, outtsys);
    468       Vector<Double> integ = intCol.getColumn() ;
    469       MaskedArray<Double> mi = maskedArray( integ, flags ) ;
    470       Double intsum = sum(mi);
    471       intColOut.put(outrowCount+irow, intsum);
    472     }
    473     outrowCount += rowNum ;
    474     ++iter;
    475495  }
    476496  return out;
     
    19101930  ArrayColumn<Float> specCol(tout, "SPECTRA");
    19111931  ArrayColumn<uChar> flagCol(tout, "FLAGTRA");
     1932  ArrayColumn<Float> tsysCol(tout, "TSYS");
     1933
    19121934  for (uInt i=0; i < tout.nrow(); ++i ) {
    19131935    MaskedArray<Float> main  = maskedArray(specCol(i), flagCol(i));
    19141936    MaskedArray<Float> maout;
    19151937    LatticeUtilities::bin(maout, main, 0, Int(width));
    1916     /// @todo implement channel based tsys binning
    19171938    specCol.put(i, maout.getArray());
    19181939    flagCol.put(i, flagsFromMA(maout));
     1940    if (tsysCol(i).nelements() == specCol(i).nelements()) {
     1941      MaskedArray<Float> matsysin = maskedArray(tsysCol(i), flagCol(i));
     1942      MaskedArray<Float> matsysout;
     1943      LatticeUtilities::bin(matsysout, matsysin, 0, Int(width));
     1944      tsysCol.put(i, matsysout.getArray());
     1945    }
    19191946    // take only the first binned spectrum's length for the deprecated
    19201947    // global header item nChan
     
    27502777                             mask, timeCol(i), !first,
    27512778                             interp, False);
    2752           (void) ok; // unused stop compiler nagging     
     2779          (void) ok; // unused stop compiler nagging
    27532780          // back into scantable
    27542781          flagOut.resize(maskOut.nelements());
     
    29542981                    "Use merge first."));
    29552982 
     2983  // 2012/02/17 TN
     2984  // Since STGrid is implemented, average doesn't consider direction
     2985  // when accumulating
    29562986  // check if OTF observation
    2957   String obstype = in[0]->getHeader().obstype ;
    2958   Double tol = 0.0 ;
    2959   if ( obstype.find( "OTF" ) != String::npos ) {
    2960     tol = TOL_OTF ;
    2961   }
    2962   else {
    2963     tol = TOL_POINT ;
    2964   }
     2987//   String obstype = in[0]->getHeader().obstype ;
     2988//   Double tol = 0.0 ;
     2989//   if ( obstype.find( "OTF" ) != String::npos ) {
     2990//     tol = TOL_OTF ;
     2991//   }
     2992//   else {
     2993//     tol = TOL_POINT ;
     2994//   }
    29652995
    29662996  CountedPtr<Scantable> out ;     // processed result
     
    30153045      uInt rows = tmpin[itable]->nrow() ;
    30163046      uInt freqnrows = tmpin[itable]->frequencies().table().nrow() ;
     3047      freqIDCol.attach( tmpin[itable]->table(), "FREQ_ID" ) ;
     3048      //ifnoCol.attach( tmpin[itable]->table(), "IFNO" ) ;
    30173049      for ( uInt irow = 0 ; irow < rows ; irow++ ) {
    30183050        if ( freqid[itable].size() == freqnrows ) {
     
    30203052        }
    30213053        else {
    3022           freqIDCol.attach( tmpin[itable]->table(), "FREQ_ID" ) ;
    3023           ifnoCol.attach( tmpin[itable]->table(), "IFNO" ) ;
    30243054          uInt id = freqIDCol( irow ) ;
    30253055          if ( freqid[itable].size() == 0 || count( freqid[itable].begin(), freqid[itable].end(), id ) == 0 ) {
    30263056            //os << "itable = " << itable << ": IF " << id << " is included in the list" << LogIO::POST ;
    30273057            vector<double> abcissa = tmpin[itable]->getAbcissa( irow ) ;
     3058            double lbedge, rbedge;
    30283059            freqid[itable].push_back( id ) ;
    3029             iffreq[itable].push_back( abcissa[0] - 0.5 * ( abcissa[1] - abcissa[0] ) ) ;
    3030             iffreq[itable].push_back( abcissa[abcissa.size()-1] + 0.5 * ( abcissa[1] - abcissa[0] ) ) ;
     3060            lbedge = abcissa[0] - 0.5 * ( abcissa[1] - abcissa[0] );
     3061            rbedge = abcissa[abcissa.size()-1] + 0.5 * ( abcissa[1] - abcissa[0] );
     3062            iffreq[itable].push_back( min(lbedge, rbedge) ) ;
     3063            iffreq[itable].push_back( max(lbedge, rbedge) ) ;
    30313064          }
    30323065        }
     
    31133146    // Grouping continuous IF groups (without frequency gap)
    31143147    // freqgrp: list of IF group indexes in each frequency group
    3115     // freqrange: list of minimum and maximum frequency in each frequency group
    31163148    // freqgrp[numgrp][nummember]
    31173149    // freqgrp: [[ifgrp00, ifgrp01, ifgrp02, ...],
     
    31193151    //           ...
    31203152    //           [ifgrpn0, ifgrpn1, ifgrpn2, ...]]
    3121     // freqrange[numgrp*2]
    3122     // freqrange: [min_grp0, max_grp0, min_grp1, max_grp1, ...]
    31233153    vector< vector<uInt> > freqgrp ;
    31243154    double freqrange = 0.0 ;
     
    31363166      freqrange = ifgfreq[2*i+1] ;
    31373167    }
    3138        
     3168
    31393169
    31403170    // print IF groups
     
    33253355
    33263356    // save column values in the vector
    3327     vector< vector<uInt> > freqTableIdVec( insize ) ;
    33283357    vector< vector<uInt> > freqIdVec( insize ) ;
    33293358    vector< vector<uInt> > ifNoVec( insize ) ;
    33303359    for ( uInt itable = 0 ; itable < insize ; itable++ ) {
    3331       ScalarColumn<uInt> freqIDs ;
    3332       freqIDs.attach( newin[itable]->frequencies().table(), "ID" ) ;
    33333360      ifnoCol.attach( newin[itable]->table(), "IFNO" ) ;
    33343361      freqIDCol.attach( newin[itable]->table(), "FREQ_ID" ) ;
    3335       for ( uInt irow = 0 ; irow < newin[itable]->frequencies().table().nrow() ; irow++ ) {
    3336         freqTableIdVec[itable].push_back( freqIDs( irow ) ) ;
    3337       }
    33383362      for ( uInt irow = 0 ; irow < newin[itable]->table().nrow() ; irow++ ) {
    33393363        freqIdVec[itable].push_back( freqIDCol( irow ) ) ;
     
    33503374      vector<uInt> freqIdUpdate ;
    33513375      for ( uInt irow = 0 ; irow < rows ; irow++ ) {
    3352         uInt ifno = ifNoVec[itable][irow] ;  // IFNO is reset by group index
     3376        uInt ifno = ifNoVec[itable][irow] ;  // IFNO is reset by group index (IF group index)
    33533377        double minfreq = ifgfreq[2*ifno] ;
    33543378        double maxfreq = ifgfreq[2*ifno+1] ;
     
    33583382        double resol = abcissa[1] - abcissa[0] ;
    33593383        //os << "abcissa range  : [" << abcissa[0] << "," << abcissa[nchan-1] << "]" << LogIO::POST ;
    3360         if ( minfreq <= abcissa[0] )
    3361           nminchan = 0 ;
     3384        uInt imin, imax;
     3385        int sigres;
     3386        if ( resol >= 0. ) {
     3387          imin = 0;
     3388          imax = nchan - 1 ;
     3389          sigres = 1 ;
     3390        } else {
     3391          imin = nchan - 1 ;
     3392          imax = 0 ;
     3393          sigres = -1 ;
     3394          resol = abs(resol) ;
     3395        }
     3396        if ( minfreq <= abcissa[imin] )
     3397          nminchan = imin ;
    33623398        else {
    33633399          //double cfreq = ( minfreq - abcissa[0] ) / resol ;
    3364           double cfreq = ( minfreq - abcissa[0] + 0.5 * resol ) / resol ;
    3365           nminchan = int(cfreq) + ( ( cfreq - int(cfreq) <= 0.5 ) ? 0 : 1 ) ;
     3400          double cfreq = ( minfreq - abcissa[imin] + 0.5 * resol ) / resol ;
     3401          nminchan = imin + sigres * (int(cfreq) + ( ( cfreq - int(cfreq) <= 0.5 ) ? 0 : 1 )) ;
    33663402        }
    3367         if ( maxfreq >= abcissa[abcissa.size()-1] )
    3368           nmaxchan = abcissa.size() - 1 ;
     3403        if ( maxfreq >= abcissa[imax] )
     3404          nmaxchan = imax;
    33693405        else {
    33703406          //double cfreq = ( abcissa[abcissa.size()-1] - maxfreq ) / resol ;
    3371           double cfreq = ( abcissa[abcissa.size()-1] - maxfreq + 0.5 * resol ) / resol ;
    3372           nmaxchan = abcissa.size() - 1 - int(cfreq) - ( ( cfreq - int(cfreq) >= 0.5 ) ? 1 : 0 ) ;
     3407          double cfreq = ( abcissa[imax] - maxfreq + 0.5 * resol ) / resol ;
     3408          nmaxchan = imax - sigres * (int(cfreq) +( ( cfreq - int(cfreq) >= 0.5 ) ? 1 : 0 )) ;
    33733409        }
    33743410        //os << "channel range (" << irow << "): [" << nminchan << "," << nmaxchan << "]" << LogIO::POST ;
     3411        if ( nmaxchan < nminchan ) {
     3412          int tmp = nmaxchan ;
     3413          nmaxchan = nminchan ;
     3414          nminchan = tmp ;
     3415        }
    33753416        if ( nmaxchan > nminchan ) {
    33763417          newin[itable]->reshapeSpectrum( nminchan, nmaxchan, irow ) ;
    33773418          int newchan = nmaxchan - nminchan + 1 ;
    3378           if ( count( freqIdUpdate.begin(), freqIdUpdate.end(), freqIdVec[itable][irow] ) == 0 && newchan < nchan )
     3419          if ( count( freqIdUpdate.begin(), freqIdUpdate.end(), freqIdVec[itable][irow] ) == 0 && newchan < nchan ) {
    33793420            freqIdUpdate.push_back( freqIdVec[itable][irow] ) ;
     3421
     3422            // Update before nminchan is lost
     3423            uInt freqId = freqIdVec[itable][irow] ;
     3424            Double refpix ;
     3425            Double refval ;
     3426            Double increment ;
     3427            newin[itable]->frequencies().getEntry( refpix, refval, increment, freqId ) ;
     3428            //refval = refval - ( refpix - nminchan ) * increment ;
     3429            refval = abcissa[nminchan] ;
     3430            refpix = 0 ;
     3431            newin[itable]->frequencies().setEntry( refpix, refval, increment, freqId ) ;
     3432          }
    33803433        }
    33813434        else {
     
    33833436        }
    33843437      }
    3385       for ( uInt i = 0 ; i < freqIdUpdate.size() ; i++ ) {
    3386         uInt freqId = freqIdUpdate[i] ;
    3387         Double refpix ;
    3388         Double refval ;
    3389         Double increment ;
     3438//       for ( uInt i = 0 ; i < freqIdUpdate.size() ; i++ ) {
     3439//      uInt freqId = freqIdUpdate[i] ;
     3440//      Double refpix ;
     3441//      Double refval ;
     3442//      Double increment ;
    33903443       
    3391         // update row
    3392         newin[itable]->frequencies().getEntry( refpix, refval, increment, freqId ) ;
    3393         refval = refval - ( refpix - nminchan ) * increment ;
    3394         refpix = 0 ;
    3395         newin[itable]->frequencies().setEntry( refpix, refval, increment, freqId ) ;
    3396       }   
     3444//      // update row
     3445//      newin[itable]->frequencies().getEntry( refpix, refval, increment, freqId ) ;
     3446//      refval = refval - ( refpix - nminchan ) * increment ;
     3447//      refpix = 0 ;
     3448//      newin[itable]->frequencies().setEntry( refpix, refval, increment, freqId ) ;
     3449//       }   
    33973450    }
    33983451
     
    34313484            if ( nchan < minchan ) {
    34323485              minchan = nchan ;
    3433               maxdnu = dnu ;
     3486              maxdnu = abs(dnu) ;
    34343487              newin[tableid]->frequencies().getEntry( refpixref, refvalref, refinc, rowid ) ;
    34353488              refreqid = rowid ;
    34363489              reftable = tableid ;
     3490              // Spectra are reversed when dnu < 0
     3491              if (dnu < 0)
     3492                refpixref = minchan - 1 -refpixref ;
    34373493            }
    34383494          }
     
    34493505          //os << "   regridChannel applied to " ;
    34503506          //if ( tableid != reftable )
    3451           refreqid = newin[tableid]->frequencies().addEntry( refpixref, refvalref, refinc ) ;
     3507          refreqid = newin[tableid]->frequencies().addEntry( refpixref, refvalref, maxdnu ) ;
    34523508          for ( uInt irow = 0 ; irow < newin[tableid]->table().nrow() ; irow++ ) {
    34533509            uInt tfreqid = freqIdVec[tableid][irow] ;
     
    34703526          vector<double> abcissa = newin[tableid]->getAbcissa( index ) ;
    34713527          minchan = abcissa.size() ;
    3472           maxdnu = abcissa[1] - abcissa[0] ;
     3528          maxdnu = abs( abcissa[1] - abcissa[0]) ;
    34733529        }
    34743530      }
     
    35403596          vector<double> abcissa = tmpout->getAbcissa( irow ) ;
    35413597          double bw = ( abcissa[1] - abcissa[0] ) * abcissa.size() ;
    3542           int nchan = (int)( bw / gmaxdnu[igrp] ) ;
     3598          int nchan = (int) abs( bw / gmaxdnu[igrp] ) ;
     3599          // All spectra will have positive frequency increments
    35433600          tmpout->regridChannel( nchan, gmaxdnu[igrp], irow ) ;
    35443601          break ;
     
    35573614    ScalarColumn<uInt> freqidColOut ;
    35583615    freqidColOut.attach( out->table(), "FREQ_ID" ) ;
    3559     MDirection::ScalarColumn dirColOut ;
    3560     dirColOut.attach( out->table(), "DIRECTION" ) ;
     3616//     MDirection::ScalarColumn dirColOut ;
     3617//     dirColOut.attach( out->table(), "DIRECTION" ) ;
    35613618    Table &tab = tmpout->table() ;
    35623619    Block<String> cols(1);
    35633620    cols[0] = String("POLNO") ;
    35643621    TableIterator iter( tab, cols ) ;
    3565     bool done = false ;
    35663622    vector< vector<uInt> > sizes( freqgrp.size() ) ;
    35673623    while( !iter.pastEnd() ) {
     
    35753631      ScalarColumn<uInt> polnos ;
    35763632      polnos.attach( iter.table(), "POLNO" ) ;
    3577       MDirection::ScalarColumn dircol ;
    3578       dircol.attach( iter.table(), "DIRECTION" ) ;
     3633//       MDirection::ScalarColumn dircol ;
     3634//       dircol.attach( iter.table(), "DIRECTION" ) ;
    35793635      uInt polno = polnos( 0 ) ;
    35803636      //os << "POLNO iteration: " << polno << LogIO::POST ;
     
    36143670//       }
    36153671      // get a list of number of channels for each frequency group member
    3616       if ( !done ) {
    3617         for ( uInt igrp = 0 ; igrp < freqgrp.size() ; igrp++ ) {
    3618           sizes[igrp].resize( freqgrp[igrp].size() ) ;
    3619           for ( uInt imem = 0 ; imem < freqgrp[igrp].size() ; imem++ ) {
    3620             for ( uInt irow = 0 ; irow < iter.table().nrow() ; irow++ ) {
    3621               uInt ifno = ifnoCol( irow ) ;
    3622               if ( ifno == freqgrp[igrp][imem] ) {
    3623                 Vector<Float> spec = specCols( irow ) ;
    3624                 sizes[igrp][imem] = spec.nelements() ;
    3625                 break ;
    3626               }               
    3627             }
    3628           }
    3629         }
    3630         done = true ;
     3672      for ( uInt igrp = 0 ; igrp < freqgrp.size() ; igrp++ ) {
     3673        sizes[igrp].resize( freqgrp[igrp].size() ) ;
     3674        for ( uInt imem = 0 ; imem < freqgrp[igrp].size() ; imem++ ) {
     3675          for ( uInt irow = 0 ; irow < iter.table().nrow() ; irow++ ) {
     3676            uInt ifno = ifnoCol( irow ) ;
     3677            if ( ifno == freqgrp[igrp][imem] ) {
     3678              Vector<Float> spec = specCols( irow ) ;
     3679              sizes[igrp][imem] = spec.nelements() ;
     3680              break ;
     3681            }
     3682          }
     3683        }
    36313684      }
    36323685      // combine spectra
     
    36353688        if ( polout == polno ) {
    36363689          uInt ifout = ifnoColOut( irow ) ;
    3637           Vector<Double> direction = dirColOut(irow).getAngle(Unit(String("rad"))).getValue() ;
     3690//           Vector<Double> direction = dirColOut(irow).getAngle(Unit(String("rad"))).getValue() ;
    36383691          uInt igrp ;
    36393692          for ( uInt jgrp = 0 ; jgrp < freqgrp.size() ; jgrp++ ) {
     
    36463699            for ( uInt jrow = 0 ; jrow < iter.table().nrow() ; jrow++ ) {
    36473700              uInt ifno = ifnoCol( jrow ) ;
    3648               Vector<Double> tdir = dircol(jrow).getAngle(Unit(String("rad"))).getValue() ;
    3649               //if ( ifno == freqgrp[igrp][imem] && allTrue( tdir == direction  ) ) {
    3650               Double dx = tdir[0] - direction[0] ;
    3651               Double dy = tdir[1] - direction[1] ;
    3652               Double dd = sqrt( dx * dx + dy * dy ) ;
     3701              // 2012/02/17 TN
     3702              // Since STGrid is implemented, average doesn't consider direction
     3703              // when accumulating
     3704//               Vector<Double> tdir = dircol(jrow).getAngle(Unit(String("rad"))).getValue() ;
     3705//               //if ( ifno == freqgrp[igrp][imem] && allTrue( tdir == direction  ) ) {
     3706//               Double dx = tdir[0] - direction[0] ;
     3707//               Double dy = tdir[1] - direction[1] ;
     3708//               Double dd = sqrt( dx * dx + dy * dy ) ;
    36533709              //if ( ifno == freqgrp[igrp][imem] && allNearAbs( tdir, direction, tol ) ) {
    3654               if ( ifno == freqgrp[igrp][imem] && dd <= tol ) {
     3710//               if ( ifno == freqgrp[igrp][imem] && dd <= tol ) {
     3711              if ( ifno == freqgrp[igrp][imem] ) {
    36553712                Vector<Float> spec = specCols( jrow ) ;
    36563713                Vector<uChar> flag = flagCols( jrow ) ;
     
    36713728          specColOut.put( irow, newspec ) ;
    36723729          flagColOut.put( irow, newflag ) ;
    3673           // IFNO renumbering
     3730          // IFNO renumbering (renumbered as frequency group ID)
    36743731          ifnoColOut.put( irow, igrp ) ;
    36753732        }
     
    36823739      uInt index = 0 ;
    36833740      uInt pixShift = 0 ;
     3741
    36843742      while ( freqgrp[igrp][index] != gmemid[igrp] ) {
    36853743        pixShift += sizes[igrp][index++] ;
    36863744      }
    36873745      for ( uInt irow = 0 ; irow < out->table().nrow() ; irow++ ) {
    3688         if ( ifnoColOut( irow ) == gmemid[igrp] && !updated[igrp] ) {
     3746        //if ( ifnoColOut( irow ) == gmemid[igrp] && !updated[igrp] ) {
     3747        if ( ifnoColOut( irow ) == igrp && !updated[igrp] ) {
    36893748          uInt freqidOut = freqidColOut( irow ) ;
    36903749          //os << "freqgrp " << igrp << " freqidOut = " << freqidOut << LogIO::POST ;
     
    36933752          double increm ;
    36943753          out->frequencies().getEntry( refpix, refval, increm, freqidOut ) ;
     3754          if (increm < 0)
     3755            refpix = sizes[igrp][index] - 1 - refpix ; // reversed
    36953756          refpix += pixShift ;
    3696           out->frequencies().setEntry( refpix, refval, increm, freqidOut ) ;
     3757          out->frequencies().setEntry( refpix, refval, gmaxdnu[igrp], freqidOut ) ;
    36973758          updated[igrp] = true ;
    36983759        }
Note: See TracChangeset for help on using the changeset viewer.