Ignore:
Timestamp:
04/04/14 20:08:17 (10 years ago)
Author:
Takeshi Nakazato
Message:

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No?

Module(s): Module Names change impacts.

Description: Describe your changes here...

Refactoring.


File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/CalibrationHelper.h

    r2917 r2919  
    1414
    1515namespace {
    16 // Iteration Helper
    17 
    18 // template<class T>
    19 // class IterationHelper
    20 // {
    21 // public:
    22 //   static void Iterate(T &calibrator)
    23 //   {
    24 //     vector<string> cols( 3 ) ;
    25 //     cols[0] = "BEAMNO" ;
    26 //     cols[1] = "POLNO" ;
    27 //     cols[2] = "IFNO" ;
    28 //     STIdxIter2 calibrator.GetIterator(cols);//iter( out, cols ) ;
    29 //     STSelector sel ;
    30 //     while ( !iter.pastEnd() ) {
    31 //       Record ids = iter.currentValue() ;
    32 //       stringstream ss ;
    33 //       ss << "SELECT FROM $1 WHERE "
    34 //          << "BEAMNO==" << ids.asuInt(cols[0]) << "&&"
    35 //          << "POLNO==" << ids.asuInt(cols[1]) << "&&"
    36 //          << "IFNO==" << ids.asuInt(cols[2]) ;
    37 //       //cout << "TaQL string: " << ss.str() << endl ;
    38 //       sel.setTaQL( ss.str() ) ;
    39 //       Vector<uInt> rows = iter.getRows( SHARE ) ;
    40 //       calibrator.Calibrate(sel, rows);
    41 //       aoff->setSelection( sel ) ;
    42 //       // out should be an exact copy of s except that SPECTRA column is empty
    43 //       calibrateALMA( out, s, aoff, rows ) ;
    44 //       aoff->unsetSelection() ;
    45 //       sel.reset() ;
    46 //       iter.next() ;
    47 //     }   
    48 //   }
    49 // };
    50 
    5116// Interpolation Helper
    5217class TcalData
     
    8045  const String method_name() const {return "getTsysFromTime";}
    8146  uInt nrow() const {return tsyscolumn_.nrow();}
    82   Vector<Float> GetEntry(int idx) const
    83   {
    84     return tsyscolumn_(idx);
    85   } 
     47  Vector<Float> GetEntry(int idx) const {return tsyscolumn_(idx);}
    8648private:
    8749  ROArrayColumn<Float> tsyscolumn_;
     
    9759  const String method_name() const {return "getSpectraFromTime";}
    9860  uInt nrow() const {return data_.ncolumn();}
    99   Vector<Float> GetEntry(int idx) const
    100   {
    101     return data_.column(idx);
    102   } 
     61  Vector<Float> GetEntry(int idx) const {return data_.column(idx);}
    10362private:
    10463  Matrix<Float> data_;
     
    255214};
    256215 
     216// Calibration Helper
     217class CalibrationHelper
     218{
     219public:
     220  static void CalibrateALMA( CountedPtr<Scantable>& out,
     221                             const CountedPtr<Scantable>& on,
     222                             const CountedPtr<Scantable>& off,
     223                             const Vector<uInt>& rows )
     224  {
     225    // 2012/05/22 TN
     226    // Assume that out has empty SPECTRA column
     227
     228    // if rows is empty, just return
     229    if ( rows.nelements() == 0 )
     230      return ;
     231
     232    ROArrayColumn<Float> in_spectra_column(on->table(), "SPECTRA");
     233    ROArrayColumn<Float> in_tsys_column(on->table(), "TSYS");
     234    ROArrayColumn<uChar> in_flagtra_column(on->table(), "FLAGTRA");
     235    ArrayColumn<Float> out_spectra_column(out->table(), "SPECTRA");
     236    ArrayColumn<uChar> out_flagtra_column(out->table(), "FLAGTRA");
     237   
     238    Vector<Double> timeVec = GetScalarColumn<Double>(off->table(), "TIME");
     239    Vector<Double> refTimeVec = GetScalarColumn<Double>(on->table(), "TIME");
     240    SpectralData offspectra(Matrix<Float>(GetArrayColumn<Float>(off->table(), "SPECTRA")));
     241    unsigned int spsize = on->nchan( on->getIF(rows[0]) ) ;
     242    vector<int> ids( 2 ) ;
     243    for ( int irow = 0 ; irow < rows.nelements() ; irow++ ) {
     244      uInt row = rows[irow];
     245      double reftime = refTimeVec[row];
     246      ids = getRowIdFromTime( reftime, timeVec ) ;
     247      Vector<Float> spoff = SimpleInterpolationHelper<SpectralData>::GetFromTime(reftime, timeVec, ids, offspectra, "linear");
     248      Vector<Float> spec = in_spectra_column(row);
     249      Vector<Float> tsys = in_tsys_column(row);
     250      Vector<uChar> flag = in_flagtra_column(row);
     251      // ALMA Calibration
     252      //
     253      // Ta* = Tsys * ( ON - OFF ) / OFF
     254      //
     255      // 2010/01/07 Takeshi Nakazato
     256      unsigned int tsyssize = tsys.nelements() ;
     257      for ( unsigned int j = 0 ; j < spsize ; j++ ) {
     258        if ( spoff[j] == 0.0 ) {
     259          spec[j] = 0.0 ;
     260          flag[j] = (uChar)True;
     261        }
     262        else {
     263          spec[j] = ( spec[j] - spoff[j] ) / spoff[j] ;
     264        }
     265        spec[j] *= (tsyssize == spsize) ? tsys[j] : tsys[0];
     266      }
     267      out_spectra_column.put(row, spec);
     268      out_flagtra_column.put(row, flag);
     269    }
     270  }
     271  static void CalibrateChopperWheel( CountedPtr<Scantable> &out,
     272                                     const CountedPtr<Scantable>& on,
     273                                     const CountedPtr<Scantable>& off,
     274                                     const CountedPtr<Scantable>& sky,
     275                                     const CountedPtr<Scantable>& hot,
     276                                     const CountedPtr<Scantable>& cold,
     277                                     const Vector<uInt> &rows )
     278  {
     279    // 2012/05/22 TN
     280    // Assume that out has empty SPECTRA column
     281   
     282    // if rows is empty, just return
     283    if ( rows.nelements() == 0 )
     284      return ;
     285
     286    string antenna_name = out->getAntennaName();
     287    ROArrayColumn<Float> in_spectra_column(on->table(), "SPECTRA");
     288    ROArrayColumn<uChar> in_flagtra_column(on->table(), "FLAGTRA");
     289    ArrayColumn<Float> out_spectra_column(out->table(), "SPECTRA");
     290    ArrayColumn<uChar> out_flagtra_column(out->table(), "FLAGTRA");
     291    ArrayColumn<Float> out_tsys_column(out->table(), "TSYS");
     292       
     293    Vector<Double> timeOff = GetScalarColumn<Double>(off->table(), "TIME");
     294    Vector<Double> timeSky = GetScalarColumn<Double>(sky->table(), "TIME");
     295    Vector<Double> timeHot = GetScalarColumn<Double>(hot->table(), "TIME");
     296    Vector<Double> timeOn = GetScalarColumn<Double>(on->table(), "TIME");
     297    SpectralData offspectra(Matrix<Float>(GetArrayColumn<Float>(off->table(), "SPECTRA")));
     298    SpectralData skyspectra(Matrix<Float>(GetArrayColumn<Float>(sky->table(), "SPECTRA")));
     299    SpectralData hotspectra(Matrix<Float>(GetArrayColumn<Float>(hot->table(), "SPECTRA")));
     300    TcalData tcaldata(sky);
     301    TsysData tsysdata(sky);
     302    unsigned int spsize = on->nchan( on->getIF(rows[0]) ) ;
     303    vector<int> ids( 2 ) ;
     304    for ( int irow = 0 ; irow < rows.nelements() ; irow++ ) {
     305      uInt row = rows[irow];
     306      double reftime = timeOn[row];
     307      ids = getRowIdFromTime( reftime, timeOff ) ;
     308      Vector<Float> spoff = SimpleInterpolationHelper<SpectralData>::GetFromTime(reftime, timeOff, ids, offspectra, "linear");
     309      ids = getRowIdFromTime( reftime, timeSky ) ;
     310      Vector<Float> spsky = SimpleInterpolationHelper<SpectralData>::GetFromTime(reftime, timeSky, ids, skyspectra, "linear");
     311      Vector<Float> tcal = SimpleInterpolationHelper<TcalData>::GetFromTime(reftime, timeSky, ids, tcaldata, "linear");
     312      Vector<Float> tsys = SimpleInterpolationHelper<TsysData>::GetFromTime(reftime, timeSky, ids, tsysdata, "linear");
     313      ids = getRowIdFromTime( reftime, timeHot ) ;
     314      Vector<Float> sphot = SimpleInterpolationHelper<SpectralData>::GetFromTime(reftime, timeHot, ids, hotspectra, "linear");
     315      Vector<Float> spec = in_spectra_column(row);
     316      Vector<uChar> flag = in_flagtra_column(row);
     317      if ( antenna_name.find( "APEX" ) != String::npos ) {
     318        // using gain array
     319        for ( unsigned int j = 0 ; j < tcal.size() ; j++ ) {
     320          if ( spoff[j] == 0.0 || (sphot[j]-spsky[j]) == 0.0 ) {
     321            spec[j] = 0.0 ;
     322            flag[j] = (uChar)True;
     323          }
     324          else {
     325            spec[j] = ( ( spec[j] - spoff[j] ) / spoff[j] )
     326              * ( spsky[j] / ( sphot[j] - spsky[j] ) ) * tcal[j] ;
     327          }
     328        }
     329      }
     330      else {
     331        // Chopper-Wheel calibration (Ulich & Haas 1976)
     332        for ( unsigned int j = 0 ; j < tcal.size() ; j++ ) {
     333          if ( (sphot[j]-spsky[j]) == 0.0 ) {
     334            spec[j] = 0.0 ;
     335            flag[j] = (uChar)True;
     336          }
     337          else {
     338            spec[j] = ( spec[j] - spoff[j] ) / ( sphot[j] - spsky[j] ) * tcal[j] ;
     339          }
     340        }
     341      }
     342      out_spectra_column.put(row, spec);
     343      out_flagtra_column.put(row, flag);
     344      out_tsys_column.put(row, tsys);
     345    }
     346  }
     347  static void GetSelector(STSelector &sel, const vector<string> &names, const Record &values)
     348  {
     349    stringstream ss ;
     350    ss << "SELECT FROM $1 WHERE ";
     351    string separator = "";
     352    for (vector<string>::const_iterator i = names.begin(); i != names.end(); ++i) {
     353      ss << separator << *i << "==";
     354      switch (values.dataType(*i)) {
     355      case TpUInt:
     356        ss << values.asuInt(*i);
     357        break;
     358      case TpInt:
     359        ss << values.asInt(*i);
     360        break;
     361      case TpFloat:
     362        ss << values.asFloat(*i);
     363        break;
     364      case TpDouble:
     365        ss << values.asDouble(*i);
     366        break;
     367      case TpComplex:
     368        ss << values.asComplex(*i);
     369        break;
     370      case TpString:
     371        ss << values.asString(*i);
     372        break;
     373      default:
     374        break;
     375      }
     376      separator = "&&";
     377    }
     378    sel.setTaQL(ss.str());
     379  }
     380private:
     381  template<class T>
     382  static Vector<T> GetScalarColumn(const Table &table, const String &name)
     383  {
     384    ROScalarColumn<T> column(table, name);
     385    return column.getColumn();
     386  }
     387  template<class T>
     388  static Array<T> GetArrayColumn(const Table &table, const String &name)
     389  {
     390    ROArrayColumn<T> column(table, name);
     391    return column.getColumn();
     392  }
     393};
     394 
     395class AlmaCalibrator
     396{
     397public:
     398  AlmaCalibrator(CountedPtr<Scantable> &out,
     399                 const CountedPtr<Scantable> &on,
     400                 const CountedPtr<Scantable> &off)
     401    : target_(out),
     402      selector_(),
     403      on_(on),
     404      off_(off)
     405  {}
     406  ~AlmaCalibrator() {}
     407  CountedPtr<Scantable> target() const {return target_;}
     408  void Process(const vector<string> &cols, const Record &values, const Vector<uInt> &rows) {
     409    CalibrationHelper::GetSelector(selector_, cols, values);
     410    off_->setSelection(selector_);
     411    CalibrationHelper::CalibrateALMA(target_, on_, off_, rows);
     412    off_->unsetSelection();
     413  }
     414private:
     415  CountedPtr<Scantable> target_;
     416  STSelector selector_;
     417  const CountedPtr<Scantable> on_;
     418  const CountedPtr<Scantable> off_;
     419};
     420
     421class ChopperWheelCalibrator
     422{
     423public:
     424  ChopperWheelCalibrator(CountedPtr<Scantable> &out,
     425                         const CountedPtr<Scantable> &on,
     426                         const CountedPtr<Scantable> &sky,
     427                         const CountedPtr<Scantable> &off,
     428                         const CountedPtr<Scantable> &hot,
     429                         const CountedPtr<Scantable> &cold)
     430    : target_(out),
     431      selector_(),
     432      on_(on),
     433      off_(off),
     434      sky_(sky),
     435      hot_(hot),
     436      cold_(cold)
     437  {}
     438  ~ChopperWheelCalibrator() {}
     439  CountedPtr<Scantable> target() const {return target_;}
     440  void Process(const vector<string> &cols, const Record &values, const Vector<uInt> &rows) {
     441    CalibrationHelper::GetSelector(selector_, cols, values);
     442    off_->setSelection(selector_);
     443    sky_->setSelection(selector_);
     444    hot_->setSelection(selector_);
     445    CalibrationHelper::CalibrateChopperWheel(target_, on_, off_, sky_, hot_, cold_, rows);
     446    off_->unsetSelection();
     447    sky_->unsetSelection();
     448    hot_->unsetSelection();
     449  }
     450private:
     451  CountedPtr<Scantable> target_;
     452  STSelector selector_;
     453  const CountedPtr<Scantable> on_;
     454  const CountedPtr<Scantable> off_;
     455  const CountedPtr<Scantable> sky_;
     456  const CountedPtr<Scantable> hot_;
     457  const CountedPtr<Scantable> cold_;
     458};
    257459 
    258460} // anonymous namespace
Note: See TracChangeset for help on using the changeset viewer.