| 1 | #include <vector>
|
|---|
| 2 |
|
|---|
| 3 | #include <casa/Arrays/Vector.h>
|
|---|
| 4 | #include <casa/BasicSL/String.h>
|
|---|
| 5 | #include <casa/Utilities/CountedPtr.h>
|
|---|
| 6 |
|
|---|
| 7 | #include "Scantable.h"
|
|---|
| 8 | #include "STTcal.h"
|
|---|
| 9 | #include "STIdxIter.h"
|
|---|
| 10 | #include "STSelector.h"
|
|---|
| 11 |
|
|---|
| 12 | using namespace casa;
|
|---|
| 13 | using namespace asap;
|
|---|
| 14 |
|
|---|
| 15 | namespace {
|
|---|
| 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 |
|
|---|
| 51 | // Interpolation Helper
|
|---|
| 52 | class TcalData
|
|---|
| 53 | {
|
|---|
| 54 | public:
|
|---|
| 55 | TcalData(CountedPtr<Scantable> s)
|
|---|
| 56 | : table_(s)
|
|---|
| 57 | {}
|
|---|
| 58 | ~TcalData() {}
|
|---|
| 59 | const String method_name() const {return "getTcalFromTime";}
|
|---|
| 60 | uInt nrow() const {return table_->nrow();}
|
|---|
| 61 | Vector<Float> GetEntry(int idx) const
|
|---|
| 62 | {
|
|---|
| 63 | String time;
|
|---|
| 64 | uInt tcalid = table_->getTcalId(idx);
|
|---|
| 65 | Vector<Float> return_value;
|
|---|
| 66 | table_->tcal().getEntry(time, return_value, tcalid);
|
|---|
| 67 | return return_value;
|
|---|
| 68 | }
|
|---|
| 69 | private:
|
|---|
| 70 | CountedPtr<Scantable> table_;
|
|---|
| 71 | };
|
|---|
| 72 |
|
|---|
| 73 | class TsysData
|
|---|
| 74 | {
|
|---|
| 75 | public:
|
|---|
| 76 | TsysData(CountedPtr<Scantable> s)
|
|---|
| 77 | : tsyscolumn_(s->table(), "TSYS")
|
|---|
| 78 | {}
|
|---|
| 79 | ~TsysData() {}
|
|---|
| 80 | const String method_name() const {return "getTsysFromTime";}
|
|---|
| 81 | uInt nrow() const {return tsyscolumn_.nrow();}
|
|---|
| 82 | Vector<Float> GetEntry(int idx) const
|
|---|
| 83 | {
|
|---|
| 84 | return tsyscolumn_(idx);
|
|---|
| 85 | }
|
|---|
| 86 | private:
|
|---|
| 87 | ROArrayColumn<Float> tsyscolumn_;
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 | class SpectralData
|
|---|
| 91 | {
|
|---|
| 92 | public:
|
|---|
| 93 | SpectralData(Matrix<Float> s)
|
|---|
| 94 | : data_(s)
|
|---|
| 95 | {}
|
|---|
| 96 | ~SpectralData() {}
|
|---|
| 97 | const String method_name() const {return "getSpectraFromTime";}
|
|---|
| 98 | uInt nrow() const {return data_.ncolumn();}
|
|---|
| 99 | Vector<Float> GetEntry(int idx) const
|
|---|
| 100 | {
|
|---|
| 101 | return data_.column(idx);
|
|---|
| 102 | }
|
|---|
| 103 | private:
|
|---|
| 104 | Matrix<Float> data_;
|
|---|
| 105 | };
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | vector<int> getRowIdFromTime(double reftime, const Vector<Double> &t)
|
|---|
| 109 | {
|
|---|
| 110 | // double reft = reftime ;
|
|---|
| 111 | double dtmin = 1.0e100 ;
|
|---|
| 112 | double dtmax = -1.0e100 ;
|
|---|
| 113 | // vector<double> dt ;
|
|---|
| 114 | int just_before = -1 ;
|
|---|
| 115 | int just_after = -1 ;
|
|---|
| 116 | Vector<Double> dt = t - reftime ;
|
|---|
| 117 | for ( unsigned int i = 0 ; i < dt.size() ; i++ ) {
|
|---|
| 118 | if ( dt[i] > 0.0 ) {
|
|---|
| 119 | // after reftime
|
|---|
| 120 | if ( dt[i] < dtmin ) {
|
|---|
| 121 | just_after = i ;
|
|---|
| 122 | dtmin = dt[i] ;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | else if ( dt[i] < 0.0 ) {
|
|---|
| 126 | // before reftime
|
|---|
| 127 | if ( dt[i] > dtmax ) {
|
|---|
| 128 | just_before = i ;
|
|---|
| 129 | dtmax = dt[i] ;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | else {
|
|---|
| 133 | // just a reftime
|
|---|
| 134 | just_before = i ;
|
|---|
| 135 | just_after = i ;
|
|---|
| 136 | dtmax = 0 ;
|
|---|
| 137 | dtmin = 0 ;
|
|---|
| 138 | break ;
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | vector<int> v(2) ;
|
|---|
| 143 | v[0] = just_before ;
|
|---|
| 144 | v[1] = just_after ;
|
|---|
| 145 |
|
|---|
| 146 | return v ;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | template<class T>
|
|---|
| 150 | class SimpleInterpolationHelper
|
|---|
| 151 | {
|
|---|
| 152 | public:
|
|---|
| 153 | static Vector<Float> GetFromTime(double reftime,
|
|---|
| 154 | const Vector<Double> &timeVec,
|
|---|
| 155 | const vector<int> &idx,
|
|---|
| 156 | const T &data,
|
|---|
| 157 | const string mode)
|
|---|
| 158 | {
|
|---|
| 159 | Vector<Float> return_value;
|
|---|
| 160 | LogIO os_;
|
|---|
| 161 | LogIO os( LogOrigin( "STMath", data.method_name(), WHERE ) ) ;
|
|---|
| 162 | if ( data.nrow() == 0 ) {
|
|---|
| 163 | os << LogIO::SEVERE << "No row in the input scantable. Return empty tcal." << LogIO::POST ;
|
|---|
| 164 | }
|
|---|
| 165 | else if ( data.nrow() == 1 ) {
|
|---|
| 166 | return_value = data.GetEntry(0);
|
|---|
| 167 | }
|
|---|
| 168 | else {
|
|---|
| 169 | if ( mode == "before" ) {
|
|---|
| 170 | int id = -1 ;
|
|---|
| 171 | if ( idx[0] != -1 ) {
|
|---|
| 172 | id = idx[0] ;
|
|---|
| 173 | }
|
|---|
| 174 | else if ( idx[1] != -1 ) {
|
|---|
| 175 | os << LogIO::WARN << "Failed to find a scan before reftime. return a spectrum just after the reftime." << LogIO::POST ;
|
|---|
| 176 | id = idx[1] ;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | return_value = data.GetEntry(id);
|
|---|
| 180 | }
|
|---|
| 181 | else if ( mode == "after" ) {
|
|---|
| 182 | int id = -1 ;
|
|---|
| 183 | if ( idx[1] != -1 ) {
|
|---|
| 184 | id = idx[1] ;
|
|---|
| 185 | }
|
|---|
| 186 | else if ( idx[0] != -1 ) {
|
|---|
| 187 | os << LogIO::WARN << "Failed to find a scan after reftime. return a spectrum just before the reftime." << LogIO::POST ;
|
|---|
| 188 | id = idx[1] ;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | return_value = data.GetEntry(id);
|
|---|
| 192 | }
|
|---|
| 193 | else if ( mode == "nearest" ) {
|
|---|
| 194 | int id = -1 ;
|
|---|
| 195 | if ( idx[0] == -1 ) {
|
|---|
| 196 | id = idx[1] ;
|
|---|
| 197 | }
|
|---|
| 198 | else if ( idx[1] == -1 ) {
|
|---|
| 199 | id = idx[0] ;
|
|---|
| 200 | }
|
|---|
| 201 | else if ( idx[0] == idx[1] ) {
|
|---|
| 202 | id = idx[0] ;
|
|---|
| 203 | }
|
|---|
| 204 | else {
|
|---|
| 205 | double t0 = timeVec[idx[0]] ;
|
|---|
| 206 | double t1 = timeVec[idx[1]] ;
|
|---|
| 207 | if ( abs( t0 - reftime ) > abs( t1 - reftime ) ) {
|
|---|
| 208 | id = idx[1] ;
|
|---|
| 209 | }
|
|---|
| 210 | else {
|
|---|
| 211 | id = idx[0] ;
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | return_value = data.GetEntry(id);
|
|---|
| 215 | }
|
|---|
| 216 | else if ( mode == "linear" ) {
|
|---|
| 217 | if ( idx[0] == -1 ) {
|
|---|
| 218 | // use after
|
|---|
| 219 | os << LogIO::WARN << "Failed to interpolate. return a spectrum just after the reftime." << LogIO::POST ;
|
|---|
| 220 | int id = idx[1] ;
|
|---|
| 221 | return_value = data.GetEntry(id);
|
|---|
| 222 | }
|
|---|
| 223 | else if ( idx[1] == -1 ) {
|
|---|
| 224 | // use before
|
|---|
| 225 | os << LogIO::WARN << "Failed to interpolate. return a spectrum just before the reftime." << LogIO::POST ;
|
|---|
| 226 | int id = idx[0] ;
|
|---|
| 227 | return_value = data.GetEntry(id);
|
|---|
| 228 | }
|
|---|
| 229 | else if ( idx[0] == idx[1] ) {
|
|---|
| 230 | // use before
|
|---|
| 231 | //os << "No need to interporate." << LogIO::POST ;
|
|---|
| 232 | int id = idx[0] ;
|
|---|
| 233 | return_value = data.GetEntry(id);
|
|---|
| 234 | }
|
|---|
| 235 | else {
|
|---|
| 236 | // do interpolation
|
|---|
| 237 |
|
|---|
| 238 | double t0 = timeVec[idx[0]] ;
|
|---|
| 239 | double t1 = timeVec[idx[1]] ;
|
|---|
| 240 | Vector<Float> value0 = data.GetEntry(idx[0]);
|
|---|
| 241 | Vector<Float> value1 = data.GetEntry(idx[1]);
|
|---|
| 242 | double tfactor = (reftime - t0) / (t1 - t0) ;
|
|---|
| 243 | for ( unsigned int i = 0 ; i < value0.size() ; i++ ) {
|
|---|
| 244 | value1[i] = ( value1[i] - value0[i] ) * tfactor + value0[i] ;
|
|---|
| 245 | }
|
|---|
| 246 | return_value = value1;
|
|---|
| 247 | }
|
|---|
| 248 | }
|
|---|
| 249 | else {
|
|---|
| 250 | os << LogIO::SEVERE << "Unknown mode" << LogIO::POST ;
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 | return return_value ;
|
|---|
| 254 | }
|
|---|
| 255 | };
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 | } // anonymous namespace
|
|---|