Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/STMath.cpp

    r2467 r2345  
    5757using namespace asap;
    5858
    59 // 2012/02/17 TN
    60 // Since STGrid is implemented, average doesn't consider direction
    61 // when accumulating
    6259// tolerance for direction comparison (rad)
    63 // #define TOL_OTF    1.0e-15
    64 // #define TOL_POINT  2.9088821e-4  // 1 arcmin
     60#define TOL_OTF    1.0e-15
     61#define TOL_POINT  2.9088821e-4  // 1 arcmin
    6562
    6663STMath::STMath(bool insitu) :
     
    8683  WeightType wtype = stringToWeight(weight);
    8784
    88   // 2012/02/17 TN
    89   // Since STGrid is implemented, average doesn't consider direction
    90   // when accumulating
    9185  // check if OTF observation
    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 //   }
     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  }
    10094
    10195  // output
     
    148142  while (!iter.pastEnd()) {
    149143    Table subt = iter.table();
    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 //       }
     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));
    186150//     }
    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 ;
     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 ;
    197188    ++iter;
    198189  }
     
    227218      }
    228219
    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 //       }
     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      }
    249237     
    250 //       if ( nrsubt == removeRows.size() )
    251 //         throw(AipsError("Averaging data is empty.")) ;
     238      if ( nrsubt == removeRows.size() )
     239        throw(AipsError("Averaging data is empty.")) ;
    252240
    253241      specCol.attach(subt,"SPECTRA");
     
    333321{
    334322  (void) mode; // currently unused
    335   // 2012/02/17 TN
    336   // Since STGrid is implemented, average doesn't consider direction
    337   // when accumulating
    338323  // check if OTF observation
    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 //   }
     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  }
    347332
    348333  // clone as this is non insitu
     
    377362    flagCol.attach(subt,"FLAGTRA");
    378363    tsysCol.attach(subt,"TSYS");
    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;
     364//     tout.addRow();
     365//     TableCopy::copyRows(tout, subt, outrowCount, 0, 1);
     366//     if ( avmode != "SCAN") {
     367//       scanColOut.put(outrowCount, uInt(0));
     368//     }
     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
     384//     }
     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;
     392//     ++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 ;
    408474    ++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;
    421 //     }
    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 //       }
    440 //     }
    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 ;
    494 //     ++iter;
    495475  }
    496476  return out;
     
    19301910  ArrayColumn<Float> specCol(tout, "SPECTRA");
    19311911  ArrayColumn<uChar> flagCol(tout, "FLAGTRA");
    1932   ArrayColumn<Float> tsysCol(tout, "TSYS");
    1933 
    19341912  for (uInt i=0; i < tout.nrow(); ++i ) {
    19351913    MaskedArray<Float> main  = maskedArray(specCol(i), flagCol(i));
    19361914    MaskedArray<Float> maout;
    19371915    LatticeUtilities::bin(maout, main, 0, Int(width));
     1916    /// @todo implement channel based tsys binning
    19381917    specCol.put(i, maout.getArray());
    19391918    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     }
    19461919    // take only the first binned spectrum's length for the deprecated
    19471920    // global header item nChan
     
    27772750                             mask, timeCol(i), !first,
    27782751                             interp, False);
    2779           (void) ok; // unused stop compiler nagging
     2752          (void) ok; // unused stop compiler nagging     
    27802753          // back into scantable
    27812754          flagOut.resize(maskOut.nelements());
     
    29812954                    "Use merge first."));
    29822955 
    2983   // 2012/02/17 TN
    2984   // Since STGrid is implemented, average doesn't consider direction
    2985   // when accumulating
    29862956  // check if OTF observation
    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 //   }
     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  }
    29952965
    29962966  CountedPtr<Scantable> out ;     // processed result
     
    30453015      uInt rows = tmpin[itable]->nrow() ;
    30463016      uInt freqnrows = tmpin[itable]->frequencies().table().nrow() ;
    3047       freqIDCol.attach( tmpin[itable]->table(), "FREQ_ID" ) ;
    3048       //ifnoCol.attach( tmpin[itable]->table(), "IFNO" ) ;
    30493017      for ( uInt irow = 0 ; irow < rows ; irow++ ) {
    30503018        if ( freqid[itable].size() == freqnrows ) {
     
    30523020        }
    30533021        else {
     3022          freqIDCol.attach( tmpin[itable]->table(), "FREQ_ID" ) ;
     3023          ifnoCol.attach( tmpin[itable]->table(), "IFNO" ) ;
    30543024          uInt id = freqIDCol( irow ) ;
    30553025          if ( freqid[itable].size() == 0 || count( freqid[itable].begin(), freqid[itable].end(), id ) == 0 ) {
    30563026            //os << "itable = " << itable << ": IF " << id << " is included in the list" << LogIO::POST ;
    30573027            vector<double> abcissa = tmpin[itable]->getAbcissa( irow ) ;
    3058             double lbedge, rbedge;
    30593028            freqid[itable].push_back( id ) ;
    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) ) ;
     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] ) ) ;
    30643031          }
    30653032        }
     
    31463113    // Grouping continuous IF groups (without frequency gap)
    31473114    // freqgrp: list of IF group indexes in each frequency group
     3115    // freqrange: list of minimum and maximum frequency in each frequency group
    31483116    // freqgrp[numgrp][nummember]
    31493117    // freqgrp: [[ifgrp00, ifgrp01, ifgrp02, ...],
     
    31513119    //           ...
    31523120    //           [ifgrpn0, ifgrpn1, ifgrpn2, ...]]
     3121    // freqrange[numgrp*2]
     3122    // freqrange: [min_grp0, max_grp0, min_grp1, max_grp1, ...]
    31533123    vector< vector<uInt> > freqgrp ;
    31543124    double freqrange = 0.0 ;
     
    31663136      freqrange = ifgfreq[2*i+1] ;
    31673137    }
    3168 
     3138       
    31693139
    31703140    // print IF groups
     
    33553325
    33563326    // save column values in the vector
     3327    vector< vector<uInt> > freqTableIdVec( insize ) ;
    33573328    vector< vector<uInt> > freqIdVec( insize ) ;
    33583329    vector< vector<uInt> > ifNoVec( insize ) ;
    33593330    for ( uInt itable = 0 ; itable < insize ; itable++ ) {
     3331      ScalarColumn<uInt> freqIDs ;
     3332      freqIDs.attach( newin[itable]->frequencies().table(), "ID" ) ;
    33603333      ifnoCol.attach( newin[itable]->table(), "IFNO" ) ;
    33613334      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      }
    33623338      for ( uInt irow = 0 ; irow < newin[itable]->table().nrow() ; irow++ ) {
    33633339        freqIdVec[itable].push_back( freqIDCol( irow ) ) ;
     
    33743350      vector<uInt> freqIdUpdate ;
    33753351      for ( uInt irow = 0 ; irow < rows ; irow++ ) {
    3376         uInt ifno = ifNoVec[itable][irow] ;  // IFNO is reset by group index (IF group index)
     3352        uInt ifno = ifNoVec[itable][irow] ;  // IFNO is reset by group index
    33773353        double minfreq = ifgfreq[2*ifno] ;
    33783354        double maxfreq = ifgfreq[2*ifno+1] ;
     
    33823358        double resol = abcissa[1] - abcissa[0] ;
    33833359        //os << "abcissa range  : [" << abcissa[0] << "," << abcissa[nchan-1] << "]" << LogIO::POST ;
    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 ;
     3360        if ( minfreq <= abcissa[0] )
     3361          nminchan = 0 ;
    33983362        else {
    33993363          //double cfreq = ( minfreq - abcissa[0] ) / resol ;
    3400           double cfreq = ( minfreq - abcissa[imin] + 0.5 * resol ) / resol ;
    3401           nminchan = imin + sigres * (int(cfreq) + ( ( cfreq - int(cfreq) <= 0.5 ) ? 0 : 1 )) ;
     3364          double cfreq = ( minfreq - abcissa[0] + 0.5 * resol ) / resol ;
     3365          nminchan = int(cfreq) + ( ( cfreq - int(cfreq) <= 0.5 ) ? 0 : 1 ) ;
    34023366        }
    3403         if ( maxfreq >= abcissa[imax] )
    3404           nmaxchan = imax;
     3367        if ( maxfreq >= abcissa[abcissa.size()-1] )
     3368          nmaxchan = abcissa.size() - 1 ;
    34053369        else {
    34063370          //double cfreq = ( abcissa[abcissa.size()-1] - maxfreq ) / resol ;
    3407           double cfreq = ( abcissa[imax] - maxfreq + 0.5 * resol ) / resol ;
    3408           nmaxchan = imax - sigres * (int(cfreq) +( ( cfreq - int(cfreq) >= 0.5 ) ? 1 : 0 )) ;
     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 ) ;
    34093373        }
    34103374        //os << "channel range (" << irow << "): [" << nminchan << "," << nmaxchan << "]" << LogIO::POST ;
    3411         if ( nmaxchan < nminchan ) {
    3412           int tmp = nmaxchan ;
    3413           nmaxchan = nminchan ;
    3414           nminchan = tmp ;
    3415         }
    34163375        if ( nmaxchan > nminchan ) {
    34173376          newin[itable]->reshapeSpectrum( nminchan, nmaxchan, irow ) ;
    34183377          int newchan = nmaxchan - nminchan + 1 ;
    3419           if ( count( freqIdUpdate.begin(), freqIdUpdate.end(), freqIdVec[itable][irow] ) == 0 && newchan < nchan ) {
     3378          if ( count( freqIdUpdate.begin(), freqIdUpdate.end(), freqIdVec[itable][irow] ) == 0 && newchan < nchan )
    34203379            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           }
    34333380        }
    34343381        else {
     
    34363383        }
    34373384      }
    3438 //       for ( uInt i = 0 ; i < freqIdUpdate.size() ; i++ ) {
    3439 //      uInt freqId = freqIdUpdate[i] ;
    3440 //      Double refpix ;
    3441 //      Double refval ;
    3442 //      Double increment ;
     3385      for ( uInt i = 0 ; i < freqIdUpdate.size() ; i++ ) {
     3386        uInt freqId = freqIdUpdate[i] ;
     3387        Double refpix ;
     3388        Double refval ;
     3389        Double increment ;
    34433390       
    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 //       }   
     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      }   
    34503397    }
    34513398
     
    34843431            if ( nchan < minchan ) {
    34853432              minchan = nchan ;
    3486               maxdnu = abs(dnu) ;
     3433              maxdnu = dnu ;
    34873434              newin[tableid]->frequencies().getEntry( refpixref, refvalref, refinc, rowid ) ;
    34883435              refreqid = rowid ;
    34893436              reftable = tableid ;
    3490               // Spectra are reversed when dnu < 0
    3491               if (dnu < 0)
    3492                 refpixref = minchan - 1 -refpixref ;
    34933437            }
    34943438          }
     
    35053449          //os << "   regridChannel applied to " ;
    35063450          //if ( tableid != reftable )
    3507           refreqid = newin[tableid]->frequencies().addEntry( refpixref, refvalref, maxdnu ) ;
     3451          refreqid = newin[tableid]->frequencies().addEntry( refpixref, refvalref, refinc ) ;
    35083452          for ( uInt irow = 0 ; irow < newin[tableid]->table().nrow() ; irow++ ) {
    35093453            uInt tfreqid = freqIdVec[tableid][irow] ;
     
    35263470          vector<double> abcissa = newin[tableid]->getAbcissa( index ) ;
    35273471          minchan = abcissa.size() ;
    3528           maxdnu = abs( abcissa[1] - abcissa[0]) ;
     3472          maxdnu = abcissa[1] - abcissa[0] ;
    35293473        }
    35303474      }
     
    35963540          vector<double> abcissa = tmpout->getAbcissa( irow ) ;
    35973541          double bw = ( abcissa[1] - abcissa[0] ) * abcissa.size() ;
    3598           int nchan = (int) abs( bw / gmaxdnu[igrp] ) ;
    3599           // All spectra will have positive frequency increments
     3542          int nchan = (int)( bw / gmaxdnu[igrp] ) ;
    36003543          tmpout->regridChannel( nchan, gmaxdnu[igrp], irow ) ;
    36013544          break ;
     
    36143557    ScalarColumn<uInt> freqidColOut ;
    36153558    freqidColOut.attach( out->table(), "FREQ_ID" ) ;
    3616 //     MDirection::ScalarColumn dirColOut ;
    3617 //     dirColOut.attach( out->table(), "DIRECTION" ) ;
     3559    MDirection::ScalarColumn dirColOut ;
     3560    dirColOut.attach( out->table(), "DIRECTION" ) ;
    36183561    Table &tab = tmpout->table() ;
    36193562    Block<String> cols(1);
    36203563    cols[0] = String("POLNO") ;
    36213564    TableIterator iter( tab, cols ) ;
     3565    bool done = false ;
    36223566    vector< vector<uInt> > sizes( freqgrp.size() ) ;
    36233567    while( !iter.pastEnd() ) {
     
    36313575      ScalarColumn<uInt> polnos ;
    36323576      polnos.attach( iter.table(), "POLNO" ) ;
    3633 //       MDirection::ScalarColumn dircol ;
    3634 //       dircol.attach( iter.table(), "DIRECTION" ) ;
     3577      MDirection::ScalarColumn dircol ;
     3578      dircol.attach( iter.table(), "DIRECTION" ) ;
    36353579      uInt polno = polnos( 0 ) ;
    36363580      //os << "POLNO iteration: " << polno << LogIO::POST ;
     
    36703614//       }
    36713615      // get a list of number of channels for each frequency group member
    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         }
     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 ;
    36843631      }
    36853632      // combine spectra
     
    36883635        if ( polout == polno ) {
    36893636          uInt ifout = ifnoColOut( irow ) ;
    3690 //           Vector<Double> direction = dirColOut(irow).getAngle(Unit(String("rad"))).getValue() ;
     3637          Vector<Double> direction = dirColOut(irow).getAngle(Unit(String("rad"))).getValue() ;
    36913638          uInt igrp ;
    36923639          for ( uInt jgrp = 0 ; jgrp < freqgrp.size() ; jgrp++ ) {
     
    36993646            for ( uInt jrow = 0 ; jrow < iter.table().nrow() ; jrow++ ) {
    37003647              uInt ifno = ifnoCol( jrow ) ;
    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 ) ;
     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 ) ;
    37093653              //if ( ifno == freqgrp[igrp][imem] && allNearAbs( tdir, direction, tol ) ) {
    3710 //               if ( ifno == freqgrp[igrp][imem] && dd <= tol ) {
    3711               if ( ifno == freqgrp[igrp][imem] ) {
     3654              if ( ifno == freqgrp[igrp][imem] && dd <= tol ) {
    37123655                Vector<Float> spec = specCols( jrow ) ;
    37133656                Vector<uChar> flag = flagCols( jrow ) ;
     
    37283671          specColOut.put( irow, newspec ) ;
    37293672          flagColOut.put( irow, newflag ) ;
    3730           // IFNO renumbering (renumbered as frequency group ID)
     3673          // IFNO renumbering
    37313674          ifnoColOut.put( irow, igrp ) ;
    37323675        }
     
    37393682      uInt index = 0 ;
    37403683      uInt pixShift = 0 ;
    3741 
    37423684      while ( freqgrp[igrp][index] != gmemid[igrp] ) {
    37433685        pixShift += sizes[igrp][index++] ;
    37443686      }
    37453687      for ( uInt irow = 0 ; irow < out->table().nrow() ; irow++ ) {
    3746         //if ( ifnoColOut( irow ) == gmemid[igrp] && !updated[igrp] ) {
    3747         if ( ifnoColOut( irow ) == igrp && !updated[igrp] ) {
     3688        if ( ifnoColOut( irow ) == gmemid[igrp] && !updated[igrp] ) {
    37483689          uInt freqidOut = freqidColOut( irow ) ;
    37493690          //os << "freqgrp " << igrp << " freqidOut = " << freqidOut << LogIO::POST ;
     
    37523693          double increm ;
    37533694          out->frequencies().getEntry( refpix, refval, increm, freqidOut ) ;
    3754           if (increm < 0)
    3755             refpix = sizes[igrp][index] - 1 - refpix ; // reversed
    37563695          refpix += pixShift ;
    3757           out->frequencies().setEntry( refpix, refval, gmaxdnu[igrp], freqidOut ) ;
     3696          out->frequencies().setEntry( refpix, refval, increm, freqidOut ) ;
    37583697          updated[igrp] = true ;
    37593698        }
Note: See TracChangeset for help on using the changeset viewer.