| 1 | // | 
|---|
| 2 | // C++ Implementation: STApplyCal | 
|---|
| 3 | // | 
|---|
| 4 | // Description: | 
|---|
| 5 | // | 
|---|
| 6 | // | 
|---|
| 7 | // Author: Takeshi Nakazato <takeshi.nakazato@nao.ac.jp> (C) 2012 | 
|---|
| 8 | // | 
|---|
| 9 | // Copyright: See COPYING file that comes with this distribution | 
|---|
| 10 | // | 
|---|
| 11 | // | 
|---|
| 12 | #include <assert.h> | 
|---|
| 13 |  | 
|---|
| 14 | #include <casa/Arrays/Array.h> | 
|---|
| 15 | #include <casa/Arrays/Vector.h> | 
|---|
| 16 | #include <casa/Arrays/Matrix.h> | 
|---|
| 17 | #include <casa/Arrays/ArrayIO.h> | 
|---|
| 18 | #include <casa/Arrays/ArrayMath.h> | 
|---|
| 19 | #include <casa/BasicSL/String.h> | 
|---|
| 20 | #include <casa/Logging/LogIO.h> | 
|---|
| 21 | #include <casa/Exceptions/Error.h> | 
|---|
| 22 | #include <casa/Utilities/CountedPtr.h> | 
|---|
| 23 | #include <casa/Utilities/Sort.h> | 
|---|
| 24 | #include <casa/Utilities/Assert.h> | 
|---|
| 25 | #include <tables/Tables/Table.h> | 
|---|
| 26 |  | 
|---|
| 27 | #include "Scantable.h" | 
|---|
| 28 | #include "STApplyCal.h" | 
|---|
| 29 | #include "STApplyTable.h" | 
|---|
| 30 | #include "STCalTsysTable.h" | 
|---|
| 31 | #include "STCalSkyTable.h" | 
|---|
| 32 | #include "STCalEnum.h" | 
|---|
| 33 | #include "STIdxIter.h" | 
|---|
| 34 | #include "Calibrator.h" | 
|---|
| 35 | #include "PSAlmaCalibrator.h" | 
|---|
| 36 | #include "Interpolator1D.h" | 
|---|
| 37 | #include "NearestInterpolator1D.h" | 
|---|
| 38 | #include "BufferedLinearInterpolator1D.h" | 
|---|
| 39 | #include "PolynomialInterpolator1D.h" | 
|---|
| 40 | #include "CubicSplineInterpolator1D.h" | 
|---|
| 41 | #include <atnf/PKSIO/SrcType.h> | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 | using namespace casa; | 
|---|
| 45 | using namespace std; | 
|---|
| 46 |  | 
|---|
| 47 | namespace { | 
|---|
| 48 | template<class Accessor, class Type> | 
|---|
| 49 | class AccessorInterface | 
|---|
| 50 | { | 
|---|
| 51 | public: | 
|---|
| 52 | typedef Type TableType; | 
|---|
| 53 | static void GetSortedData(const vector<TableType *> &tablelist, | 
|---|
| 54 | const Vector<uInt> &tableIndex, | 
|---|
| 55 | const uInt nrow, | 
|---|
| 56 | const uInt nchan, | 
|---|
| 57 | Vector<Double> &time, | 
|---|
| 58 | Matrix<Float> &data, | 
|---|
| 59 | Matrix<uChar> &flag) | 
|---|
| 60 | { | 
|---|
| 61 | Vector<Double> timeUnsorted; | 
|---|
| 62 | Matrix<Float> dataUnsorted; | 
|---|
| 63 | Matrix<uChar> flagUnsorted; | 
|---|
| 64 | GetFromTable(tablelist, tableIndex, nrow, nchan, | 
|---|
| 65 | timeUnsorted, dataUnsorted, flagUnsorted); | 
|---|
| 66 | SortData(timeUnsorted, dataUnsorted, flagUnsorted, | 
|---|
| 67 | time, data, flag); | 
|---|
| 68 | } | 
|---|
| 69 | private: | 
|---|
| 70 | static void GetFromTable(const vector<TableType *> &tableList, | 
|---|
| 71 | const Vector<uInt> &tableIndex, | 
|---|
| 72 | const uInt nrow, | 
|---|
| 73 | const uInt nchan, | 
|---|
| 74 | Vector<Double> &time, | 
|---|
| 75 | Matrix<Float> &data, | 
|---|
| 76 | Matrix<uChar> &flag) | 
|---|
| 77 | { | 
|---|
| 78 | time.resize(nrow, False); | 
|---|
| 79 | const IPosition shape(2, nrow, nchan); | 
|---|
| 80 | data.resize(shape, False); | 
|---|
| 81 | flag.resize(shape, False); | 
|---|
| 82 | uInt rowIndex = 0; | 
|---|
| 83 | for (uInt i = 0 ; i < tableIndex.nelements(); i++) { | 
|---|
| 84 | TableType *p = tableList[tableIndex[i]]; | 
|---|
| 85 | Vector<Double> t = Accessor::GetTime(p); | 
|---|
| 86 | Matrix<Float> dt = Accessor::GetData(p); | 
|---|
| 87 | Matrix<uChar> fl = Accessor::GetFlag(p); | 
|---|
| 88 | for (uInt j = 0; j < t.nelements(); j++) { | 
|---|
| 89 | time[rowIndex] = t[j]; | 
|---|
| 90 | data.row(rowIndex) = dt.column(j); | 
|---|
| 91 | flag.row(rowIndex) = fl.column(j); | 
|---|
| 92 | rowIndex++; | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | static Vector<uInt> IndexSort(const Vector<Double> &t) | 
|---|
| 98 | { | 
|---|
| 99 | Sort sort; | 
|---|
| 100 | sort.sortKey(&t[0], TpDouble, 0, Sort::Ascending); | 
|---|
| 101 | Vector<uInt> idx; | 
|---|
| 102 | sort.sort(idx, t.nelements(), Sort::QuickSort|Sort::NoDuplicates); | 
|---|
| 103 | return idx; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | static void SortData(const Vector<Double> &key, const Matrix<Float> &data, | 
|---|
| 107 | const Matrix<uChar> &flag, | 
|---|
| 108 | Vector<Double> &sortedKey, Matrix<Float> &sortedData, | 
|---|
| 109 | Matrix<uChar> &sortedFlag) | 
|---|
| 110 | { | 
|---|
| 111 | Vector<uInt> sortIndex = IndexSort(key); | 
|---|
| 112 | uInt len = sortIndex.nelements(); | 
|---|
| 113 | IPosition shape = data.shape(); | 
|---|
| 114 | shape[0] = len; | 
|---|
| 115 | Int64 nelements = shape.product(); | 
|---|
| 116 | sortedKey.takeStorage(IPosition(1, len), new Double[len], TAKE_OVER); | 
|---|
| 117 | sortedData.takeStorage(shape, new Float[nelements], TAKE_OVER); | 
|---|
| 118 | sortedFlag.takeStorage(shape, new uChar[nelements], TAKE_OVER); | 
|---|
| 119 | for (uInt i = 0 ; i < len; i++) { | 
|---|
| 120 | sortedKey[i] = key[sortIndex[i]]; | 
|---|
| 121 | } | 
|---|
| 122 | for (uInt i = 0; i < len; ++i) { | 
|---|
| 123 | sortedData.row(i) = data.row(sortIndex[i]); | 
|---|
| 124 | sortedFlag.row(i) = flag.row(sortIndex[i]); | 
|---|
| 125 | } | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | }; | 
|---|
| 129 |  | 
|---|
| 130 | class SkyTableAccessor : public AccessorInterface<SkyTableAccessor, asap::STCalSkyTable> | 
|---|
| 131 | { | 
|---|
| 132 | public: | 
|---|
| 133 | static Vector<Double> GetTime(const TableType *t) {return t->getTime();} | 
|---|
| 134 | static Matrix<Float> GetData(const TableType *t) {return t->getSpectra();} | 
|---|
| 135 | static Matrix<uChar> GetFlag(const TableType *t) {return t->getFlagtra();} | 
|---|
| 136 | }; | 
|---|
| 137 |  | 
|---|
| 138 | class TsysTableAccessor : public AccessorInterface<TsysTableAccessor, asap::STCalTsysTable> | 
|---|
| 139 | { | 
|---|
| 140 | public: | 
|---|
| 141 | static Vector<Double> GetTime(const TableType *t) {return t->getTime();} | 
|---|
| 142 | static Matrix<Float> GetData(const TableType *t) {return t->getTsys();} | 
|---|
| 143 | static Matrix<uChar> GetFlag(const TableType *t) {return t->getFlagtra();} | 
|---|
| 144 | }; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | namespace asap { | 
|---|
| 148 | STApplyCal::STApplyCal() | 
|---|
| 149 | { | 
|---|
| 150 | init(); | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | STApplyCal::STApplyCal(CountedPtr<Scantable> target) | 
|---|
| 154 | : target_(target) | 
|---|
| 155 | { | 
|---|
| 156 | init(); | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | STApplyCal::~STApplyCal() | 
|---|
| 160 | { | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void STApplyCal::init() | 
|---|
| 164 | { | 
|---|
| 165 | caltype_ = STCalEnum::NoType; | 
|---|
| 166 | doTsys_ = False; | 
|---|
| 167 | iTime_ = STCalEnum::DefaultInterpolation; | 
|---|
| 168 | iFreq_ = STCalEnum::DefaultInterpolation; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | void STApplyCal::reset() | 
|---|
| 172 | { | 
|---|
| 173 | // call init | 
|---|
| 174 | init(); | 
|---|
| 175 |  | 
|---|
| 176 | // clear apply tables | 
|---|
| 177 | // do not delete object here | 
|---|
| 178 | skytable_.resize(0); | 
|---|
| 179 | tsystable_.resize(0); | 
|---|
| 180 |  | 
|---|
| 181 | // clear mapping for Tsys transfer | 
|---|
| 182 | spwmap_.clear(); | 
|---|
| 183 |  | 
|---|
| 184 | // reset selector | 
|---|
| 185 | sel_.reset(); | 
|---|
| 186 |  | 
|---|
| 187 | // delete interpolators | 
|---|
| 188 | interpolatorT_ = 0; | 
|---|
| 189 | interpolatorS_ = 0; | 
|---|
| 190 | interpolatorF_ = 0; | 
|---|
| 191 |  | 
|---|
| 192 | // clear working scantable | 
|---|
| 193 | work_ = 0; | 
|---|
| 194 |  | 
|---|
| 195 | // clear calibrator | 
|---|
| 196 | calibrator_ = 0; | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | void STApplyCal::completeReset() | 
|---|
| 200 | { | 
|---|
| 201 | reset(); | 
|---|
| 202 | target_ = 0; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | void STApplyCal::setTarget(CountedPtr<Scantable> target) | 
|---|
| 206 | { | 
|---|
| 207 | target_ = target; | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | void STApplyCal::setTarget(const String &name) | 
|---|
| 211 | { | 
|---|
| 212 | // always create PlainTable | 
|---|
| 213 | target_ = new Scantable(name, Table::Plain); | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | void STApplyCal::push(STCalSkyTable *table) | 
|---|
| 217 | { | 
|---|
| 218 | os_.origin(LogOrigin("STApplyCal","push",WHERE)); | 
|---|
| 219 | skytable_.push_back(table); | 
|---|
| 220 | STCalEnum::CalType caltype = STApplyTable::getCalType(table); | 
|---|
| 221 | os_ << "caltype=" <<  caltype << LogIO::POST; | 
|---|
| 222 | if (caltype_ == STCalEnum::NoType || | 
|---|
| 223 | caltype_ == STCalEnum::DefaultType || | 
|---|
| 224 | caltype_ == STCalEnum::CalTsys) { | 
|---|
| 225 | caltype_ = caltype; | 
|---|
| 226 | } | 
|---|
| 227 | os_ << "caltype_=" << caltype_ << LogIO::POST; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | void STApplyCal::push(STCalTsysTable *table) | 
|---|
| 231 | { | 
|---|
| 232 | tsystable_.push_back(table); | 
|---|
| 233 | doTsys_ = True; | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | void STApplyCal::setTimeInterpolation(STCalEnum::InterpolationType itype, Int order) | 
|---|
| 237 | { | 
|---|
| 238 | iTime_ = itype; | 
|---|
| 239 | order_ = order; | 
|---|
| 240 | } | 
|---|
| 241 |  | 
|---|
| 242 | void STApplyCal::setFrequencyInterpolation(STCalEnum::InterpolationType itype, Int order) | 
|---|
| 243 | { | 
|---|
| 244 | iFreq_ = itype; | 
|---|
| 245 | order_ = order; | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | void STApplyCal::setTsysTransfer(uInt from, Vector<uInt> to) | 
|---|
| 249 | { | 
|---|
| 250 | os_.origin(LogOrigin("STApplyCal","setTsysTransfer",WHERE)); | 
|---|
| 251 | os_ << "from=" << from << ", to=" << to << LogIO::POST; | 
|---|
| 252 | map<uInt, Vector<uInt> >::iterator i = spwmap_.find(from); | 
|---|
| 253 | if (i == spwmap_.end()) { | 
|---|
| 254 | spwmap_.insert(pair<uInt, Vector<uInt> >(from, to)); | 
|---|
| 255 | } | 
|---|
| 256 | else { | 
|---|
| 257 | Vector<uInt> toNew = i->second; | 
|---|
| 258 | spwmap_.erase(i); | 
|---|
| 259 | uInt k = toNew.nelements(); | 
|---|
| 260 | toNew.resize(k+to.nelements(), True); | 
|---|
| 261 | for (uInt i = 0; i < to.nelements(); i++) | 
|---|
| 262 | toNew[i+k] = to[i]; | 
|---|
| 263 | spwmap_.insert(pair<uInt, Vector<uInt> >(from, toNew)); | 
|---|
| 264 | } | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 | void STApplyCal::apply(Bool insitu, Bool filltsys) | 
|---|
| 268 | { | 
|---|
| 269 | os_.origin(LogOrigin("STApplyCal","apply",WHERE)); | 
|---|
| 270 |  | 
|---|
| 271 | //assert(!target_.null()); | 
|---|
| 272 | assert_<AipsError>(!target_.null(),"You have to set target scantable first."); | 
|---|
| 273 |  | 
|---|
| 274 | // calibrator | 
|---|
| 275 | if (caltype_ == STCalEnum::CalPSAlma) | 
|---|
| 276 | calibrator_ = new PSAlmaCalibrator(); | 
|---|
| 277 |  | 
|---|
| 278 | // interpolator | 
|---|
| 279 | initInterpolator(); | 
|---|
| 280 |  | 
|---|
| 281 | // select data | 
|---|
| 282 | sel_.reset(); | 
|---|
| 283 | sel_ = target_->getSelection(); | 
|---|
| 284 | if (caltype_ == STCalEnum::CalPSAlma || | 
|---|
| 285 | caltype_ == STCalEnum::CalPS) { | 
|---|
| 286 | sel_.setTypes(vector<int>(1,(int)SrcType::PSON)); | 
|---|
| 287 | } | 
|---|
| 288 | target_->setSelection(sel_); | 
|---|
| 289 |  | 
|---|
| 290 | //os_ << "sel_.print()=" << sel_.print() << LogIO::POST; | 
|---|
| 291 |  | 
|---|
| 292 | // working data | 
|---|
| 293 | if (insitu) { | 
|---|
| 294 | os_.origin(LogOrigin("STApplyCal","apply",WHERE)); | 
|---|
| 295 | os_ << "Overwrite input scantable" << LogIO::POST; | 
|---|
| 296 | work_ = target_; | 
|---|
| 297 | } | 
|---|
| 298 | else { | 
|---|
| 299 | os_.origin(LogOrigin("STApplyCal","apply",WHERE)); | 
|---|
| 300 | os_ << "Create output scantable from input" << LogIO::POST; | 
|---|
| 301 | work_ = new Scantable(*target_, false); | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | //os_ << "work_->nrow()=" << work_->nrow() << LogIO::POST; | 
|---|
| 305 |  | 
|---|
| 306 | // list of apply tables for sky calibration | 
|---|
| 307 | Vector<uInt> skycalList(skytable_.size()); | 
|---|
| 308 | uInt numSkyCal = 0; | 
|---|
| 309 |  | 
|---|
| 310 | // list of apply tables for Tsys calibration | 
|---|
| 311 | for (uInt i = 0 ; i < skytable_.size(); i++) { | 
|---|
| 312 | STCalEnum::CalType caltype = STApplyTable::getCalType(skytable_[i]); | 
|---|
| 313 | if (caltype == caltype_) { | 
|---|
| 314 | skycalList[numSkyCal] = i; | 
|---|
| 315 | numSkyCal++; | 
|---|
| 316 | } | 
|---|
| 317 | } | 
|---|
| 318 | skycalList.resize(numSkyCal, True); | 
|---|
| 319 |  | 
|---|
| 320 |  | 
|---|
| 321 | vector<string> cols( 3 ) ; | 
|---|
| 322 | cols[0] = "BEAMNO" ; | 
|---|
| 323 | cols[1] = "POLNO" ; | 
|---|
| 324 | cols[2] = "IFNO" ; | 
|---|
| 325 | CountedPtr<STIdxIter2> iter = new STIdxIter2(work_, cols) ; | 
|---|
| 326 | double start = mathutil::gettimeofday_sec(); | 
|---|
| 327 | os_ << LogIO::DEBUGGING << "start iterative doapply: " << start << LogIO::POST; | 
|---|
| 328 | while (!iter->pastEnd()) { | 
|---|
| 329 | Record ids = iter->currentValue(); | 
|---|
| 330 | Vector<uInt> rows = iter->getRows(SHARE); | 
|---|
| 331 | if (rows.nelements() > 0) | 
|---|
| 332 | doapply(ids.asuInt("BEAMNO"), ids.asuInt("IFNO"), ids.asuInt("POLNO"), rows, skycalList, filltsys); | 
|---|
| 333 | iter->next(); | 
|---|
| 334 | } | 
|---|
| 335 | double end = mathutil::gettimeofday_sec(); | 
|---|
| 336 | os_ << LogIO::DEBUGGING << "end iterative doapply: " << end << LogIO::POST; | 
|---|
| 337 | os_ << LogIO::DEBUGGING << "elapsed time for doapply: " << end - start << " sec" << LogIO::POST; | 
|---|
| 338 |  | 
|---|
| 339 | target_->unsetSelection(); | 
|---|
| 340 | } | 
|---|
| 341 |  | 
|---|
| 342 | void STApplyCal::doapply(uInt beamno, uInt ifno, uInt polno, | 
|---|
| 343 | Vector<uInt> &rows, | 
|---|
| 344 | Vector<uInt> &skylist, | 
|---|
| 345 | Bool filltsys) | 
|---|
| 346 | { | 
|---|
| 347 | os_.origin(LogOrigin("STApplyCal","doapply",WHERE)); | 
|---|
| 348 | Bool doTsys = doTsys_; | 
|---|
| 349 |  | 
|---|
| 350 | STSelector sel; | 
|---|
| 351 | vector<int> id(1); | 
|---|
| 352 | id[0] = beamno; | 
|---|
| 353 | sel.setBeams(id); | 
|---|
| 354 | id[0] = ifno; | 
|---|
| 355 | sel.setIFs(id); | 
|---|
| 356 | id[0] = polno; | 
|---|
| 357 | sel.setPolarizations(id); | 
|---|
| 358 |  | 
|---|
| 359 | // apply selection to apply tables | 
|---|
| 360 | uInt nrowSkyTotal = 0; | 
|---|
| 361 | uInt nrowTsysTotal = 0; | 
|---|
| 362 | for (uInt i = 0; i < skylist.nelements(); i++) { | 
|---|
| 363 | skytable_[skylist[i]]->setSelection(sel); | 
|---|
| 364 | nrowSkyTotal += skytable_[skylist[i]]->nrow(); | 
|---|
| 365 | os_ << "nrowSkyTotal=" << nrowSkyTotal << LogIO::POST; | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | // Skip IFNO without sky data | 
|---|
| 369 | if (nrowSkyTotal == 0) | 
|---|
| 370 | return; | 
|---|
| 371 |  | 
|---|
| 372 | uInt nchanTsys = 0; | 
|---|
| 373 | Vector<Double> ftsys; | 
|---|
| 374 | uInt tsysifno = getIFForTsys(ifno); | 
|---|
| 375 | os_ << "tsysifno=" << (Int)tsysifno << LogIO::POST; | 
|---|
| 376 | if (tsystable_.size() == 0) { | 
|---|
| 377 | os_.origin(LogOrigin("STApplyTable", "doapply", WHERE)); | 
|---|
| 378 | os_ << "No Tsys tables are given. Skip Tsys calibratoin." << LogIO::POST; | 
|---|
| 379 | doTsys = False; | 
|---|
| 380 | } | 
|---|
| 381 | else if (tsysifno == (uInt)-1) { | 
|---|
| 382 | os_.origin(LogOrigin("STApplyTable", "doapply", WHERE)); | 
|---|
| 383 | os_ << "No corresponding Tsys for IFNO " << ifno << ". Skip Tsys calibration" << LogIO::POST; | 
|---|
| 384 | doTsys = False; | 
|---|
| 385 | } | 
|---|
| 386 | else { | 
|---|
| 387 | id[0] = (int)tsysifno; | 
|---|
| 388 | sel.setIFs(id); | 
|---|
| 389 | for (uInt i = 0; i < tsystable_.size() ; i++) { | 
|---|
| 390 | tsystable_[i]->setSelection(sel); | 
|---|
| 391 | uInt nrowThisTsys = tsystable_[i]->nrow(); | 
|---|
| 392 | nrowTsysTotal += nrowThisTsys; | 
|---|
| 393 | if (nrowThisTsys > 0 && nchanTsys == 0) { | 
|---|
| 394 | nchanTsys = tsystable_[i]->nchan(tsysifno); | 
|---|
| 395 | ftsys = tsystable_[i]->getBaseFrequency(0); | 
|---|
| 396 | } | 
|---|
| 397 | } | 
|---|
| 398 | } | 
|---|
| 399 |  | 
|---|
| 400 | uInt nchanSp = skytable_[skylist[0]]->nchan(ifno); | 
|---|
| 401 | uInt nrowSky = nrowSkyTotal; | 
|---|
| 402 | Vector<Double> timeSky; | 
|---|
| 403 | Matrix<Float> spoff; | 
|---|
| 404 | Matrix<uChar> flagoff; | 
|---|
| 405 | SkyTableAccessor::GetSortedData(skytable_, skylist, | 
|---|
| 406 | nrowSkyTotal, nchanSp, | 
|---|
| 407 | timeSky, spoff, flagoff); | 
|---|
| 408 | nrowSky = timeSky.nelements(); | 
|---|
| 409 |  | 
|---|
| 410 | uInt nrowTsys = nrowTsysTotal; | 
|---|
| 411 | Vector<Double> timeTsys; | 
|---|
| 412 | Matrix<Float> tsys; | 
|---|
| 413 | Matrix<uChar> flagtsys; | 
|---|
| 414 | if (doTsys) { | 
|---|
| 415 | //os_ << "doTsys" << LogIO::POST; | 
|---|
| 416 | Vector<uInt> tsyslist(tsystable_.size()); | 
|---|
| 417 | indgen(tsyslist); | 
|---|
| 418 | TsysTableAccessor::GetSortedData(tsystable_, tsyslist, | 
|---|
| 419 | nrowTsysTotal, nchanTsys, | 
|---|
| 420 | timeTsys, tsys, flagtsys); | 
|---|
| 421 | nrowTsys = timeTsys.nelements(); | 
|---|
| 422 | } | 
|---|
| 423 |  | 
|---|
| 424 | Table tab = work_->table(); | 
|---|
| 425 | ArrayColumn<Float> spCol(tab, "SPECTRA"); | 
|---|
| 426 | ArrayColumn<uChar> flCol(tab, "FLAGTRA"); | 
|---|
| 427 | ArrayColumn<Float> tsysCol(tab, "TSYS"); | 
|---|
| 428 | ScalarColumn<Double> timeCol(tab, "TIME"); | 
|---|
| 429 |  | 
|---|
| 430 | // Array for scaling factor (aka Tsys) | 
|---|
| 431 | Vector<Float> iTsys(IPosition(1, nchanSp), new Float[nchanSp], TAKE_OVER); | 
|---|
| 432 | // Array for Tsys interpolation | 
|---|
| 433 | // This is empty array and is never referenced if doTsys == false | 
|---|
| 434 | // (i.e. nchanTsys == 0) | 
|---|
| 435 | Vector<Float> iTsysT(IPosition(1, nchanTsys), new Float[nchanTsys], TAKE_OVER); | 
|---|
| 436 |  | 
|---|
| 437 | // Array for interpolated off spectrum | 
|---|
| 438 | Vector<Float> iOff(IPosition(1, nchanSp), new Float[nchanSp], TAKE_OVER); | 
|---|
| 439 |  | 
|---|
| 440 | // working array for interpolation with flags | 
|---|
| 441 | uInt arraySize = max(max(nrowTsys, nchanTsys), nrowSky); | 
|---|
| 442 | Vector<Double> xwork(IPosition(1, arraySize), new Double[arraySize], TAKE_OVER); | 
|---|
| 443 | Vector<Float> ywork(IPosition(1, arraySize), new Float[arraySize], TAKE_OVER); | 
|---|
| 444 | Vector<uChar> fwork(IPosition(1, nchanTsys), new uChar[nchanTsys], TAKE_OVER); | 
|---|
| 445 |  | 
|---|
| 446 | for (uInt i = 0; i < rows.nelements(); i++) { | 
|---|
| 447 | //os_ << "start i = " << i << " (row = " << rows[i] << ")" << LogIO::POST; | 
|---|
| 448 | uInt irow = rows[i]; | 
|---|
| 449 |  | 
|---|
| 450 | // target spectral data | 
|---|
| 451 | Vector<Float> on = spCol(irow); | 
|---|
| 452 | Vector<uChar> flag = flCol(irow); | 
|---|
| 453 | //os_ << "on=" << on[0] << LogIO::POST; | 
|---|
| 454 | calibrator_->setSource(on); | 
|---|
| 455 |  | 
|---|
| 456 | // interpolation | 
|---|
| 457 | Double t0 = timeCol(irow); | 
|---|
| 458 | Double *xwork_p = xwork.data(); | 
|---|
| 459 | Float *ywork_p = ywork.data(); | 
|---|
| 460 | for (uInt ichan = 0; ichan < nchanSp; ichan++) { | 
|---|
| 461 | Float *tmpY = &(spoff.data()[ichan * nrowSky]); | 
|---|
| 462 | uChar *tmpF = &(flagoff.data()[ichan * nrowSky]); | 
|---|
| 463 | uInt wnrow = 0; | 
|---|
| 464 | for (uInt ir = 0; ir < nrowSky; ++ir) { | 
|---|
| 465 | if (tmpF[ir] == 0) { | 
|---|
| 466 | xwork_p[wnrow] = timeSky.data()[ir]; | 
|---|
| 467 | ywork_p[wnrow] = tmpY[ir]; | 
|---|
| 468 | wnrow++; | 
|---|
| 469 | } | 
|---|
| 470 | } | 
|---|
| 471 | if (wnrow > 0) { | 
|---|
| 472 | // any valid reference data | 
|---|
| 473 | interpolatorS_->setData(xwork_p, ywork_p, wnrow); | 
|---|
| 474 | } | 
|---|
| 475 | else { | 
|---|
| 476 | // no valid reference data | 
|---|
| 477 | // interpolate data regardless of flag | 
|---|
| 478 | interpolatorS_->setData(timeSky.data(), tmpY, nrowSky); | 
|---|
| 479 | // flag this channel for calibrated data | 
|---|
| 480 | flag[ichan] = 1 << 7; // user flag | 
|---|
| 481 | } | 
|---|
| 482 | iOff[ichan] = interpolatorS_->interpolate(t0); | 
|---|
| 483 | } | 
|---|
| 484 | //os_ << "iOff=" << iOff[0] << LogIO::POST; | 
|---|
| 485 | calibrator_->setReference(iOff); | 
|---|
| 486 |  | 
|---|
| 487 | if (doTsys) { | 
|---|
| 488 | // Tsys correction | 
|---|
| 489 | // interpolation on time axis | 
|---|
| 490 | Float *yt = iTsysT.data(); | 
|---|
| 491 | uChar *fwork_p = fwork.data(); | 
|---|
| 492 | for (uInt ichan = 0; ichan < nchanTsys; ichan++) { | 
|---|
| 493 | Float *tmpY = &(tsys.data()[ichan * nrowTsys]); | 
|---|
| 494 | uChar *tmpF = &(flagtsys.data()[ichan * nrowTsys]); | 
|---|
| 495 | uInt wnrow = 0; | 
|---|
| 496 | for (uInt ir = 0; ir < nrowTsys; ++ir) { | 
|---|
| 497 | if (tmpF[ir] == 0) { | 
|---|
| 498 | xwork_p[wnrow] = timeTsys.data()[ir]; | 
|---|
| 499 | ywork_p[wnrow] = tmpY[ir]; | 
|---|
| 500 | wnrow++; | 
|---|
| 501 | } | 
|---|
| 502 | } | 
|---|
| 503 | if (wnrow > 0) { | 
|---|
| 504 | // any valid value exists | 
|---|
| 505 | interpolatorT_->setData(xwork_p, ywork_p, wnrow); | 
|---|
| 506 | iTsysT[ichan] = interpolatorT_->interpolate(t0); | 
|---|
| 507 | fwork_p[ichan] = 0; | 
|---|
| 508 | } | 
|---|
| 509 | else { | 
|---|
| 510 | // no valid data | 
|---|
| 511 | fwork_p[ichan] = 1 << 7; // user flag | 
|---|
| 512 | } | 
|---|
| 513 | } | 
|---|
| 514 | if (nchanSp == 1) { | 
|---|
| 515 | // take average | 
|---|
| 516 | iTsys[0] = mean(iTsysT); | 
|---|
| 517 | } | 
|---|
| 518 | else { | 
|---|
| 519 | // interpolation on frequency axis | 
|---|
| 520 | Vector<Double> fsp = getBaseFrequency(rows[i]); | 
|---|
| 521 | uInt wnchan = 0; | 
|---|
| 522 | for (uInt ichan = 0; ichan < nchanTsys; ++ichan) { | 
|---|
| 523 | if (fwork_p[ichan] == 0) { | 
|---|
| 524 | xwork_p[wnchan] = ftsys.data()[ichan]; | 
|---|
| 525 | ywork_p[wnchan] = yt[ichan]; | 
|---|
| 526 | ++wnchan; | 
|---|
| 527 | } | 
|---|
| 528 | } | 
|---|
| 529 | //interpolatorF_->setY(yt, nchanTsys); | 
|---|
| 530 | interpolatorF_->setData(xwork_p, ywork_p, wnchan); | 
|---|
| 531 | for (uInt ichan = 0; ichan < nchanSp; ichan++) { | 
|---|
| 532 | iTsys[ichan] = interpolatorF_->interpolate(fsp[ichan]); | 
|---|
| 533 | } | 
|---|
| 534 | } | 
|---|
| 535 | } | 
|---|
| 536 | else { | 
|---|
| 537 | Vector<Float> tsysInRow = tsysCol(irow); | 
|---|
| 538 | if (tsysInRow.nelements() == 1) { | 
|---|
| 539 | iTsys = tsysInRow[0]; | 
|---|
| 540 | } | 
|---|
| 541 | else { | 
|---|
| 542 | for (uInt ichan = 0; ichan < tsysInRow.nelements(); ++ichan) | 
|---|
| 543 | iTsys[ichan] = tsysInRow[ichan]; | 
|---|
| 544 | } | 
|---|
| 545 | } | 
|---|
| 546 | //os_ << "iTsys=" << iTsys[0] << LogIO::POST; | 
|---|
| 547 | calibrator_->setScaler(iTsys); | 
|---|
| 548 |  | 
|---|
| 549 | // do calibration | 
|---|
| 550 | calibrator_->calibrate(); | 
|---|
| 551 |  | 
|---|
| 552 | // update table | 
|---|
| 553 | //os_ << "calibrated=" << calibrator_->getCalibrated()[0] << LogIO::POST; | 
|---|
| 554 | spCol.put(irow, calibrator_->getCalibrated()); | 
|---|
| 555 | flCol.put(irow, flag); | 
|---|
| 556 | if (filltsys) | 
|---|
| 557 | tsysCol.put(irow, iTsys); | 
|---|
| 558 | } | 
|---|
| 559 |  | 
|---|
| 560 |  | 
|---|
| 561 | // reset selection on apply tables | 
|---|
| 562 | for (uInt i = 0; i < skylist.nelements(); i++) | 
|---|
| 563 | skytable_[i]->unsetSelection(); | 
|---|
| 564 | for (uInt i = 0; i < tsystable_.size(); i++) | 
|---|
| 565 | tsystable_[i]->unsetSelection(); | 
|---|
| 566 |  | 
|---|
| 567 |  | 
|---|
| 568 | // reset interpolator | 
|---|
| 569 | interpolatorS_->reset(); | 
|---|
| 570 | interpolatorF_->reset(); | 
|---|
| 571 | interpolatorT_->reset(); | 
|---|
| 572 | } | 
|---|
| 573 |  | 
|---|
| 574 | uInt STApplyCal::getIFForTsys(uInt to) | 
|---|
| 575 | { | 
|---|
| 576 | for (map<casa::uInt, Vector<uInt> >::iterator i = spwmap_.begin(); | 
|---|
| 577 | i != spwmap_.end(); i++) { | 
|---|
| 578 | Vector<uInt> tolist = i->second; | 
|---|
| 579 | os_ << "from=" << i->first << ": tolist=" << tolist << LogIO::POST; | 
|---|
| 580 | for (uInt j = 0; j < tolist.nelements(); j++) { | 
|---|
| 581 | if (tolist[j] == to) | 
|---|
| 582 | return i->first; | 
|---|
| 583 | } | 
|---|
| 584 | } | 
|---|
| 585 | return (uInt)-1; | 
|---|
| 586 | } | 
|---|
| 587 |  | 
|---|
| 588 | void STApplyCal::save(const String &name) | 
|---|
| 589 | { | 
|---|
| 590 | //assert(!work_.null()); | 
|---|
| 591 | assert_<AipsError>(!work_.null(),"You have to execute apply method first."); | 
|---|
| 592 |  | 
|---|
| 593 | work_->setSelection(sel_); | 
|---|
| 594 | work_->makePersistent(name); | 
|---|
| 595 | work_->unsetSelection(); | 
|---|
| 596 | } | 
|---|
| 597 |  | 
|---|
| 598 | Vector<Double> STApplyCal::getBaseFrequency(uInt whichrow) | 
|---|
| 599 | { | 
|---|
| 600 | //assert(whichrow <= (uInt)work_->nrow()); | 
|---|
| 601 | assert_<AipsError>(whichrow <= (uInt)work_->nrow(),"row index out of range."); | 
|---|
| 602 | ROTableColumn col(work_->table(), "IFNO"); | 
|---|
| 603 | uInt ifno = col.asuInt(whichrow); | 
|---|
| 604 | col.attach(work_->table(), "FREQ_ID"); | 
|---|
| 605 | uInt freqid = col.asuInt(whichrow); | 
|---|
| 606 | uInt nc = work_->nchan(ifno); | 
|---|
| 607 | STFrequencies ftab = work_->frequencies(); | 
|---|
| 608 | Double rp, rf, inc; | 
|---|
| 609 | ftab.getEntry(rp, rf, inc, freqid); | 
|---|
| 610 | Vector<Double> r(nc); | 
|---|
| 611 | indgen(r, rf-rp*inc, inc); | 
|---|
| 612 | return r; | 
|---|
| 613 | } | 
|---|
| 614 |  | 
|---|
| 615 | void STApplyCal::initInterpolator() | 
|---|
| 616 | { | 
|---|
| 617 | os_.origin(LogOrigin("STApplyCal","initInterpolator",WHERE)); | 
|---|
| 618 | int order = (order_ > 0) ? order_ : 1; | 
|---|
| 619 | switch (iTime_) { | 
|---|
| 620 | case STCalEnum::NearestInterpolation: | 
|---|
| 621 | { | 
|---|
| 622 | os_ << "use NearestInterpolator in time axis" << LogIO::POST; | 
|---|
| 623 | interpolatorS_ = new NearestInterpolator1D<Double, Float>(); | 
|---|
| 624 | interpolatorT_ = new NearestInterpolator1D<Double, Float>(); | 
|---|
| 625 | break; | 
|---|
| 626 | } | 
|---|
| 627 | case STCalEnum::LinearInterpolation: | 
|---|
| 628 | { | 
|---|
| 629 | os_ << "use BufferedLinearInterpolator in time axis" << LogIO::POST; | 
|---|
| 630 | interpolatorS_ = new BufferedLinearInterpolator1D<Double, Float>(); | 
|---|
| 631 | interpolatorT_ = new BufferedLinearInterpolator1D<Double, Float>(); | 
|---|
| 632 | break; | 
|---|
| 633 | } | 
|---|
| 634 | case STCalEnum::CubicSplineInterpolation: | 
|---|
| 635 | { | 
|---|
| 636 | os_ << "use CubicSplineInterpolator in time axis" << LogIO::POST; | 
|---|
| 637 | interpolatorS_ = new CubicSplineInterpolator1D<Double, Float>(); | 
|---|
| 638 | interpolatorT_ = new CubicSplineInterpolator1D<Double, Float>(); | 
|---|
| 639 | break; | 
|---|
| 640 | } | 
|---|
| 641 | case STCalEnum::PolynomialInterpolation: | 
|---|
| 642 | { | 
|---|
| 643 | os_ << "use PolynomialInterpolator in time axis" << LogIO::POST; | 
|---|
| 644 | if (order == 0) { | 
|---|
| 645 | interpolatorS_ = new NearestInterpolator1D<Double, Float>(); | 
|---|
| 646 | interpolatorT_ = new NearestInterpolator1D<Double, Float>(); | 
|---|
| 647 | } | 
|---|
| 648 | else { | 
|---|
| 649 | interpolatorS_ = new PolynomialInterpolator1D<Double, Float>(); | 
|---|
| 650 | interpolatorT_ = new PolynomialInterpolator1D<Double, Float>(); | 
|---|
| 651 | interpolatorS_->setOrder(order); | 
|---|
| 652 | interpolatorT_->setOrder(order); | 
|---|
| 653 | } | 
|---|
| 654 | break; | 
|---|
| 655 | } | 
|---|
| 656 | default: | 
|---|
| 657 | { | 
|---|
| 658 | os_ << "use BufferedLinearInterpolator in time axis" << LogIO::POST; | 
|---|
| 659 | interpolatorS_ = new BufferedLinearInterpolator1D<Double, Float>(); | 
|---|
| 660 | interpolatorT_ = new BufferedLinearInterpolator1D<Double, Float>(); | 
|---|
| 661 | break; | 
|---|
| 662 | } | 
|---|
| 663 | } | 
|---|
| 664 |  | 
|---|
| 665 | switch (iFreq_) { | 
|---|
| 666 | case STCalEnum::NearestInterpolation: | 
|---|
| 667 | { | 
|---|
| 668 | os_ << "use NearestInterpolator in frequency axis" << LogIO::POST; | 
|---|
| 669 | interpolatorF_ = new NearestInterpolator1D<Double, Float>(); | 
|---|
| 670 | break; | 
|---|
| 671 | } | 
|---|
| 672 | case STCalEnum::LinearInterpolation: | 
|---|
| 673 | { | 
|---|
| 674 | os_ << "use BufferedLinearInterpolator in frequency axis" << LogIO::POST; | 
|---|
| 675 | interpolatorF_ = new BufferedLinearInterpolator1D<Double, Float>(); | 
|---|
| 676 | break; | 
|---|
| 677 | } | 
|---|
| 678 | case STCalEnum::CubicSplineInterpolation: | 
|---|
| 679 | { | 
|---|
| 680 | os_ << "use CubicSplineInterpolator in frequency axis" << LogIO::POST; | 
|---|
| 681 | interpolatorF_ = new CubicSplineInterpolator1D<Double, Float>(); | 
|---|
| 682 | break; | 
|---|
| 683 | } | 
|---|
| 684 | case STCalEnum::PolynomialInterpolation: | 
|---|
| 685 | { | 
|---|
| 686 | os_ << "use PolynomialInterpolator in frequency axis" << LogIO::POST; | 
|---|
| 687 | if (order == 0) { | 
|---|
| 688 | interpolatorF_ = new NearestInterpolator1D<Double, Float>(); | 
|---|
| 689 | } | 
|---|
| 690 | else { | 
|---|
| 691 | interpolatorF_ = new PolynomialInterpolator1D<Double, Float>(); | 
|---|
| 692 | interpolatorF_->setOrder(order); | 
|---|
| 693 | } | 
|---|
| 694 | break; | 
|---|
| 695 | } | 
|---|
| 696 | default: | 
|---|
| 697 | { | 
|---|
| 698 | os_ << "use LinearInterpolator in frequency axis" << LogIO::POST; | 
|---|
| 699 | interpolatorF_ = new BufferedLinearInterpolator1D<Double, Float>(); | 
|---|
| 700 | break; | 
|---|
| 701 | } | 
|---|
| 702 | } | 
|---|
| 703 | } | 
|---|
| 704 |  | 
|---|
| 705 | } | 
|---|