| 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 asap {
|
|---|
| 48 |
|
|---|
| 49 | STApplyCal::STApplyCal()
|
|---|
| 50 | {
|
|---|
| 51 | init();
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | STApplyCal::STApplyCal(CountedPtr<Scantable> target)
|
|---|
| 55 | : target_(target)
|
|---|
| 56 | {
|
|---|
| 57 | init();
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | STApplyCal::~STApplyCal()
|
|---|
| 61 | {
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | void STApplyCal::init()
|
|---|
| 65 | {
|
|---|
| 66 | caltype_ = STCalEnum::NoType;
|
|---|
| 67 | doTsys_ = False;
|
|---|
| 68 | iTime_ = STCalEnum::DefaultInterpolation;
|
|---|
| 69 | iFreq_ = STCalEnum::DefaultInterpolation;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | void STApplyCal::reset()
|
|---|
| 73 | {
|
|---|
| 74 | // call init
|
|---|
| 75 | init();
|
|---|
| 76 |
|
|---|
| 77 | // clear apply tables
|
|---|
| 78 | // do not delete object here
|
|---|
| 79 | skytable_.resize(0);
|
|---|
| 80 | tsystable_.resize(0);
|
|---|
| 81 |
|
|---|
| 82 | // clear mapping for Tsys transfer
|
|---|
| 83 | spwmap_.clear();
|
|---|
| 84 |
|
|---|
| 85 | // reset selector
|
|---|
| 86 | sel_.reset();
|
|---|
| 87 |
|
|---|
| 88 | // delete interpolators
|
|---|
| 89 | interpolatorT_ = 0;
|
|---|
| 90 | interpolatorS_ = 0;
|
|---|
| 91 | interpolatorF_ = 0;
|
|---|
| 92 |
|
|---|
| 93 | // clear working scantable
|
|---|
| 94 | work_ = 0;
|
|---|
| 95 |
|
|---|
| 96 | // clear calibrator
|
|---|
| 97 | calibrator_ = 0;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | void STApplyCal::completeReset()
|
|---|
| 101 | {
|
|---|
| 102 | reset();
|
|---|
| 103 | target_ = 0;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void STApplyCal::setTarget(CountedPtr<Scantable> target)
|
|---|
| 107 | {
|
|---|
| 108 | target_ = target;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | void STApplyCal::setTarget(const String &name)
|
|---|
| 112 | {
|
|---|
| 113 | // always create PlainTable
|
|---|
| 114 | target_ = new Scantable(name, Table::Plain);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | void STApplyCal::push(STCalSkyTable *table)
|
|---|
| 118 | {
|
|---|
| 119 | os_.origin(LogOrigin("STApplyCal","push",WHERE));
|
|---|
| 120 | skytable_.push_back(table);
|
|---|
| 121 | STCalEnum::CalType caltype = STApplyTable::getCalType(table);
|
|---|
| 122 | os_ << "caltype=" << caltype << LogIO::POST;
|
|---|
| 123 | if (caltype_ == STCalEnum::NoType ||
|
|---|
| 124 | caltype_ == STCalEnum::DefaultType ||
|
|---|
| 125 | caltype_ == STCalEnum::CalTsys) {
|
|---|
| 126 | caltype_ = caltype;
|
|---|
| 127 | }
|
|---|
| 128 | os_ << "caltype_=" << caltype_ << LogIO::POST;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | void STApplyCal::push(STCalTsysTable *table)
|
|---|
| 132 | {
|
|---|
| 133 | tsystable_.push_back(table);
|
|---|
| 134 | doTsys_ = True;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | void STApplyCal::setTimeInterpolation(STCalEnum::InterpolationType itype, Int order)
|
|---|
| 138 | {
|
|---|
| 139 | iTime_ = itype;
|
|---|
| 140 | order_ = order;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | void STApplyCal::setFrequencyInterpolation(STCalEnum::InterpolationType itype, Int order)
|
|---|
| 144 | {
|
|---|
| 145 | iFreq_ = itype;
|
|---|
| 146 | order_ = order;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | void STApplyCal::setTsysTransfer(uInt from, Vector<uInt> to)
|
|---|
| 150 | {
|
|---|
| 151 | os_.origin(LogOrigin("STApplyCal","setTsysTransfer",WHERE));
|
|---|
| 152 | os_ << "from=" << from << ", to=" << to << LogIO::POST;
|
|---|
| 153 | map<uInt, Vector<uInt> >::iterator i = spwmap_.find(from);
|
|---|
| 154 | if (i == spwmap_.end()) {
|
|---|
| 155 | spwmap_.insert(pair<uInt, Vector<uInt> >(from, to));
|
|---|
| 156 | }
|
|---|
| 157 | else {
|
|---|
| 158 | Vector<uInt> toNew = i->second;
|
|---|
| 159 | spwmap_.erase(i);
|
|---|
| 160 | uInt k = toNew.nelements();
|
|---|
| 161 | toNew.resize(k+to.nelements(), True);
|
|---|
| 162 | for (uInt i = 0; i < to.nelements(); i++)
|
|---|
| 163 | toNew[i+k] = to[i];
|
|---|
| 164 | spwmap_.insert(pair<uInt, Vector<uInt> >(from, toNew));
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | void STApplyCal::apply(Bool insitu, Bool filltsys)
|
|---|
| 169 | {
|
|---|
| 170 | os_.origin(LogOrigin("STApplyCal","apply",WHERE));
|
|---|
| 171 |
|
|---|
| 172 | //assert(!target_.null());
|
|---|
| 173 | assert_<AipsError>(!target_.null(),"You have to set target scantable first.");
|
|---|
| 174 |
|
|---|
| 175 | // calibrator
|
|---|
| 176 | if (caltype_ == STCalEnum::CalPSAlma)
|
|---|
| 177 | calibrator_ = new PSAlmaCalibrator();
|
|---|
| 178 |
|
|---|
| 179 | // interpolator
|
|---|
| 180 | initInterpolator();
|
|---|
| 181 |
|
|---|
| 182 | // select data
|
|---|
| 183 | sel_.reset();
|
|---|
| 184 | sel_ = target_->getSelection();
|
|---|
| 185 | if (caltype_ == STCalEnum::CalPSAlma ||
|
|---|
| 186 | caltype_ == STCalEnum::CalPS) {
|
|---|
| 187 | sel_.setTypes(vector<int>(1,(int)SrcType::PSON));
|
|---|
| 188 | }
|
|---|
| 189 | target_->setSelection(sel_);
|
|---|
| 190 |
|
|---|
| 191 | //os_ << "sel_.print()=" << sel_.print() << LogIO::POST;
|
|---|
| 192 |
|
|---|
| 193 | // working data
|
|---|
| 194 | if (insitu) {
|
|---|
| 195 | os_.origin(LogOrigin("STApplyCal","apply",WHERE));
|
|---|
| 196 | os_ << "Overwrite input scantable" << LogIO::POST;
|
|---|
| 197 | work_ = target_;
|
|---|
| 198 | }
|
|---|
| 199 | else {
|
|---|
| 200 | os_.origin(LogOrigin("STApplyCal","apply",WHERE));
|
|---|
| 201 | os_ << "Create output scantable from input" << LogIO::POST;
|
|---|
| 202 | work_ = new Scantable(*target_, false);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | //os_ << "work_->nrow()=" << work_->nrow() << LogIO::POST;
|
|---|
| 206 |
|
|---|
| 207 | // list of apply tables for sky calibration
|
|---|
| 208 | Vector<uInt> skycalList(skytable_.size());
|
|---|
| 209 | uInt numSkyCal = 0;
|
|---|
| 210 |
|
|---|
| 211 | // list of apply tables for Tsys calibration
|
|---|
| 212 | for (uInt i = 0 ; i < skytable_.size(); i++) {
|
|---|
| 213 | STCalEnum::CalType caltype = STApplyTable::getCalType(skytable_[i]);
|
|---|
| 214 | if (caltype == caltype_) {
|
|---|
| 215 | skycalList[numSkyCal] = i;
|
|---|
| 216 | numSkyCal++;
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 | skycalList.resize(numSkyCal, True);
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 | vector<string> cols( 3 ) ;
|
|---|
| 223 | cols[0] = "BEAMNO" ;
|
|---|
| 224 | cols[1] = "POLNO" ;
|
|---|
| 225 | cols[2] = "IFNO" ;
|
|---|
| 226 | CountedPtr<STIdxIter2> iter = new STIdxIter2(work_, cols) ;
|
|---|
| 227 | double start = mathutil::gettimeofday_sec();
|
|---|
| 228 | os_ << LogIO::DEBUGGING << "start iterative doapply: " << start << LogIO::POST;
|
|---|
| 229 | while (!iter->pastEnd()) {
|
|---|
| 230 | Record ids = iter->currentValue();
|
|---|
| 231 | Vector<uInt> rows = iter->getRows(SHARE);
|
|---|
| 232 | if (rows.nelements() > 0)
|
|---|
| 233 | doapply(ids.asuInt("BEAMNO"), ids.asuInt("IFNO"), ids.asuInt("POLNO"), rows, skycalList, filltsys);
|
|---|
| 234 | iter->next();
|
|---|
| 235 | }
|
|---|
| 236 | double end = mathutil::gettimeofday_sec();
|
|---|
| 237 | os_ << LogIO::DEBUGGING << "end iterative doapply: " << end << LogIO::POST;
|
|---|
| 238 | os_ << LogIO::DEBUGGING << "elapsed time for doapply: " << end - start << " sec" << LogIO::POST;
|
|---|
| 239 |
|
|---|
| 240 | target_->unsetSelection();
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | void STApplyCal::doapply(uInt beamno, uInt ifno, uInt polno,
|
|---|
| 244 | Vector<uInt> &rows,
|
|---|
| 245 | Vector<uInt> &skylist,
|
|---|
| 246 | Bool filltsys)
|
|---|
| 247 | {
|
|---|
| 248 | os_.origin(LogOrigin("STApplyCal","doapply",WHERE));
|
|---|
| 249 | Bool doTsys = doTsys_;
|
|---|
| 250 |
|
|---|
| 251 | STSelector sel;
|
|---|
| 252 | vector<int> id(1);
|
|---|
| 253 | id[0] = beamno;
|
|---|
| 254 | sel.setBeams(id);
|
|---|
| 255 | id[0] = ifno;
|
|---|
| 256 | sel.setIFs(id);
|
|---|
| 257 | id[0] = polno;
|
|---|
| 258 | sel.setPolarizations(id);
|
|---|
| 259 |
|
|---|
| 260 | // apply selection to apply tables
|
|---|
| 261 | uInt nrowSky = 0;
|
|---|
| 262 | uInt nrowTsys = 0;
|
|---|
| 263 | for (uInt i = 0; i < skylist.nelements(); i++) {
|
|---|
| 264 | skytable_[skylist[i]]->setSelection(sel);
|
|---|
| 265 | nrowSky += skytable_[skylist[i]]->nrow();
|
|---|
| 266 | os_ << "nrowSky=" << nrowSky << LogIO::POST;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | // Skip IFNO without sky data
|
|---|
| 270 | if (nrowSky == 0)
|
|---|
| 271 | return;
|
|---|
| 272 |
|
|---|
| 273 | uInt nchanTsys = 0;
|
|---|
| 274 | Vector<Double> ftsys;
|
|---|
| 275 | uInt tsysifno = getIFForTsys(ifno);
|
|---|
| 276 | os_ << "tsysifno=" << (Int)tsysifno << LogIO::POST;
|
|---|
| 277 | if (tsystable_.size() == 0) {
|
|---|
| 278 | os_.origin(LogOrigin("STApplyTable", "doapply", WHERE));
|
|---|
| 279 | os_ << "No Tsys tables are given. Skip Tsys calibratoin." << LogIO::POST;
|
|---|
| 280 | doTsys = False;
|
|---|
| 281 | }
|
|---|
| 282 | else if (tsysifno == (uInt)-1) {
|
|---|
| 283 | os_.origin(LogOrigin("STApplyTable", "doapply", WHERE));
|
|---|
| 284 | os_ << "No corresponding Tsys for IFNO " << ifno << ". Skip Tsys calibration" << LogIO::POST;
|
|---|
| 285 | doTsys = False;
|
|---|
| 286 | }
|
|---|
| 287 | else {
|
|---|
| 288 | id[0] = (int)tsysifno;
|
|---|
| 289 | sel.setIFs(id);
|
|---|
| 290 | for (uInt i = 0; i < tsystable_.size() ; i++) {
|
|---|
| 291 | tsystable_[i]->setSelection(sel);
|
|---|
| 292 | uInt nrowThisTsys = tsystable_[i]->nrow();
|
|---|
| 293 | nrowTsys += nrowThisTsys;
|
|---|
| 294 | if (nrowThisTsys > 0 and nchanTsys == 0) {
|
|---|
| 295 | nchanTsys = tsystable_[i]->nchan(tsysifno);
|
|---|
| 296 | ftsys = tsystable_[i]->getBaseFrequency(0);
|
|---|
| 297 | }
|
|---|
| 298 | }
|
|---|
| 299 | interpolatorF_->setX(ftsys.data(), nchanTsys);
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | uInt nchanSp = skytable_[skylist[0]]->nchan(ifno);
|
|---|
| 303 | uInt nrowSkySorted = nrowSky;
|
|---|
| 304 | Vector<Double> timeSkySorted;
|
|---|
| 305 | Matrix<Float> spoffSorted;
|
|---|
| 306 | Matrix<uChar> flagoffSorted;
|
|---|
| 307 | {
|
|---|
| 308 | Vector<Double> timeSky(nrowSky);
|
|---|
| 309 | Matrix<Float> spoff(nrowSky, nchanSp);
|
|---|
| 310 | Matrix<uChar> flagoff(nrowSky, nchanSp);
|
|---|
| 311 | nrowSky = 0;
|
|---|
| 312 | for (uInt i = 0 ; i < skylist.nelements(); i++) {
|
|---|
| 313 | STCalSkyTable *p = skytable_[skylist[i]];
|
|---|
| 314 | Vector<Double> t = p->getTime();
|
|---|
| 315 | Matrix<Float> sp = p->getSpectra();
|
|---|
| 316 | Matrix<uChar> fl = p->getFlagtra();
|
|---|
| 317 | for (uInt j = 0; j < t.nelements(); j++) {
|
|---|
| 318 | timeSky[nrowSky] = t[j];
|
|---|
| 319 | spoff.row(nrowSky) = sp.column(j);
|
|---|
| 320 | flagoff.row(nrowSky) = fl.column(j);
|
|---|
| 321 | nrowSky++;
|
|---|
| 322 | }
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | Vector<uInt> skyIdx = timeSort(timeSky);
|
|---|
| 326 | nrowSkySorted = skyIdx.nelements();
|
|---|
| 327 |
|
|---|
| 328 | timeSkySorted.takeStorage(IPosition(1, nrowSkySorted),
|
|---|
| 329 | new Double[nrowSkySorted],
|
|---|
| 330 | TAKE_OVER);
|
|---|
| 331 | for (uInt i = 0 ; i < nrowSkySorted; i++) {
|
|---|
| 332 | timeSkySorted[i] = timeSky[skyIdx[i]];
|
|---|
| 333 | }
|
|---|
| 334 | interpolatorS_->setX(timeSkySorted.data(), nrowSkySorted);
|
|---|
| 335 |
|
|---|
| 336 | spoffSorted.takeStorage(IPosition(2, nrowSky, nchanSp),
|
|---|
| 337 | new Float[nrowSky * nchanSp],
|
|---|
| 338 | TAKE_OVER);
|
|---|
| 339 | flagoffSorted.takeStorage(IPosition(2, nrowSkySorted, nchanSp),
|
|---|
| 340 | new uChar[nrowSkySorted * nchanSp],
|
|---|
| 341 | TAKE_OVER);
|
|---|
| 342 | for (uInt i = 0 ; i < nrowSky; i++) {
|
|---|
| 343 | spoffSorted.row(i) = spoff.row(skyIdx[i]);
|
|---|
| 344 | flagoffSorted.row(i) = flagoff.row(skyIdx[i]);
|
|---|
| 345 | }
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | uInt nrowTsysSorted = nrowTsys;
|
|---|
| 349 | Matrix<Float> tsysSorted;
|
|---|
| 350 | Matrix<uChar> flagtsysSorted;
|
|---|
| 351 | Vector<Double> timeTsysSorted;
|
|---|
| 352 | if (doTsys) {
|
|---|
| 353 | //os_ << "doTsys" << LogIO::POST;
|
|---|
| 354 | Vector<Double> timeTsys(nrowTsys);
|
|---|
| 355 | Matrix<Float> tsys(nrowTsys, nchanTsys);
|
|---|
| 356 | Matrix<uChar> flagtsys(nrowTsys, nchanTsys);
|
|---|
| 357 | tsysSorted.takeStorage(IPosition(2, nrowTsys, nchanTsys),
|
|---|
| 358 | new Float[nrowTsys * nchanTsys],
|
|---|
| 359 | TAKE_OVER);
|
|---|
| 360 | nrowTsys = 0;
|
|---|
| 361 | for (uInt i = 0 ; i < tsystable_.size(); i++) {
|
|---|
| 362 | STCalTsysTable *p = tsystable_[i];
|
|---|
| 363 | Vector<Double> t = p->getTime();
|
|---|
| 364 | Matrix<Float> ts = p->getTsys();
|
|---|
| 365 | Matrix<uChar> fl = p->getFlagtra();
|
|---|
| 366 | for (uInt j = 0; j < t.nelements(); j++) {
|
|---|
| 367 | timeTsys[nrowTsys] = t[j];
|
|---|
| 368 | tsys.row(nrowTsys) = ts.column(j);
|
|---|
| 369 | flagtsys.row(nrowTsys) = fl.column(j);
|
|---|
| 370 | nrowTsys++;
|
|---|
| 371 | }
|
|---|
| 372 | }
|
|---|
| 373 | Vector<uInt> tsysIdx = timeSort(timeTsys);
|
|---|
| 374 | nrowTsysSorted = tsysIdx.nelements();
|
|---|
| 375 |
|
|---|
| 376 | timeTsysSorted.takeStorage(IPosition(1, nrowTsysSorted),
|
|---|
| 377 | new Double[nrowTsysSorted],
|
|---|
| 378 | TAKE_OVER);
|
|---|
| 379 | flagtsysSorted.takeStorage(IPosition(2, nrowTsysSorted, nchanTsys),
|
|---|
| 380 | new uChar[nrowTsysSorted * nchanTsys],
|
|---|
| 381 | TAKE_OVER);
|
|---|
| 382 | for (uInt i = 0 ; i < nrowTsysSorted; i++) {
|
|---|
| 383 | timeTsysSorted[i] = timeTsys[tsysIdx[i]];
|
|---|
| 384 | }
|
|---|
| 385 | interpolatorT_->setX(timeTsysSorted.data(), nrowTsysSorted);
|
|---|
| 386 |
|
|---|
| 387 | for (uInt i = 0; i < nrowTsys; ++i) {
|
|---|
| 388 | tsysSorted.row(i) = tsys.row(tsysIdx[i]);
|
|---|
| 389 | flagtsysSorted.row(i) = flagtsys.row(tsysIdx[i]);
|
|---|
| 390 | }
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | Table tab = work_->table();
|
|---|
| 394 | ArrayColumn<Float> spCol(tab, "SPECTRA");
|
|---|
| 395 | ArrayColumn<uChar> flCol(tab, "FLAGTRA");
|
|---|
| 396 | ArrayColumn<Float> tsysCol(tab, "TSYS");
|
|---|
| 397 | ScalarColumn<Double> timeCol(tab, "TIME");
|
|---|
| 398 | //Vector<Float> on;
|
|---|
| 399 |
|
|---|
| 400 | // Array for scaling factor (aka Tsys)
|
|---|
| 401 | Vector<Float> iTsys(IPosition(1, nchanSp), new Float[nchanSp], TAKE_OVER);
|
|---|
| 402 | // Array for Tsys interpolation
|
|---|
| 403 | // This is empty array and is never referenced if doTsys == false
|
|---|
| 404 | // (i.e. nchanTsys == 0)
|
|---|
| 405 | Vector<Float> iTsysT(IPosition(1, nchanTsys), new Float[nchanTsys], TAKE_OVER);
|
|---|
| 406 |
|
|---|
| 407 | // Array for interpolated off spectrum
|
|---|
| 408 | Vector<Float> iOff(IPosition(1, nchanSp), new Float[nchanSp], TAKE_OVER);
|
|---|
| 409 |
|
|---|
| 410 | for (uInt i = 0; i < rows.nelements(); i++) {
|
|---|
| 411 | //os_ << "start i = " << i << " (row = " << rows[i] << ")" << LogIO::POST;
|
|---|
| 412 | uInt irow = rows[i];
|
|---|
| 413 |
|
|---|
| 414 | // target spectral data
|
|---|
| 415 | Vector<Float> on = spCol(irow);
|
|---|
| 416 | Vector<uChar> flag = flCol(irow);
|
|---|
| 417 | //os_ << "on=" << on[0] << LogIO::POST;
|
|---|
| 418 | calibrator_->setSource(on);
|
|---|
| 419 |
|
|---|
| 420 | // interpolation
|
|---|
| 421 | Double t0 = timeCol(irow);
|
|---|
| 422 | for (uInt ichan = 0; ichan < nchanSp; ichan++) {
|
|---|
| 423 | Float *tmpY = &(spoffSorted.data()[ichan * nrowSkySorted]);
|
|---|
| 424 | if (allNE(flagoffSorted.column(ichan), (uChar)0)) {
|
|---|
| 425 | flag[ichan] = 1 << 7; // user flag
|
|---|
| 426 | }
|
|---|
| 427 | interpolatorS_->setY(tmpY, nrowSkySorted);
|
|---|
| 428 | iOff[ichan] = interpolatorS_->interpolate(t0);
|
|---|
| 429 | }
|
|---|
| 430 | //os_ << "iOff=" << iOff[0] << LogIO::POST;
|
|---|
| 431 | calibrator_->setReference(iOff);
|
|---|
| 432 |
|
|---|
| 433 | if (doTsys) {
|
|---|
| 434 | // Tsys correction
|
|---|
| 435 | Float *yt = iTsysT.data();
|
|---|
| 436 | for (uInt ichan = 0; ichan < nchanTsys; ichan++) {
|
|---|
| 437 | Float *tmpY = &(tsysSorted.data()[ichan * nrowTsysSorted]);
|
|---|
| 438 | interpolatorT_->setY(tmpY, nrowTsysSorted);
|
|---|
| 439 | iTsysT[ichan] = interpolatorT_->interpolate(t0);
|
|---|
| 440 | }
|
|---|
| 441 | if (nchanSp == 1) {
|
|---|
| 442 | // take average
|
|---|
| 443 | iTsys[0] = mean(iTsysT);
|
|---|
| 444 | }
|
|---|
| 445 | else {
|
|---|
| 446 | // interpolation on frequency axis
|
|---|
| 447 | Vector<Double> fsp = getBaseFrequency(rows[i]);
|
|---|
| 448 | interpolatorF_->setY(yt, nchanTsys);
|
|---|
| 449 | for (uInt ichan = 0; ichan < nchanSp; ichan++) {
|
|---|
| 450 | iTsys[ichan] = interpolatorF_->interpolate(fsp[ichan]);
|
|---|
| 451 | }
|
|---|
| 452 | }
|
|---|
| 453 | }
|
|---|
| 454 | else {
|
|---|
| 455 | Vector<Float> tsysInRow = tsysCol(irow);
|
|---|
| 456 | if (tsysInRow.nelements() == 1) {
|
|---|
| 457 | iTsys = tsysInRow[0];
|
|---|
| 458 | }
|
|---|
| 459 | else {
|
|---|
| 460 | for (uInt ichan = 0; ichan < tsysInRow.nelements(); ++ichan)
|
|---|
| 461 | iTsys[ichan] = tsysInRow[ichan];
|
|---|
| 462 | }
|
|---|
| 463 | }
|
|---|
| 464 | //os_ << "iTsys=" << iTsys[0] << LogIO::POST;
|
|---|
| 465 | calibrator_->setScaler(iTsys);
|
|---|
| 466 |
|
|---|
| 467 | // do calibration
|
|---|
| 468 | calibrator_->calibrate();
|
|---|
| 469 |
|
|---|
| 470 | // update table
|
|---|
| 471 | //os_ << "calibrated=" << calibrator_->getCalibrated()[0] << LogIO::POST;
|
|---|
| 472 | spCol.put(irow, calibrator_->getCalibrated());
|
|---|
| 473 | flCol.put(irow, flag);
|
|---|
| 474 | if (filltsys)
|
|---|
| 475 | tsysCol.put(irow, iTsys);
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 | // reset selection on apply tables
|
|---|
| 480 | for (uInt i = 0; i < skylist.nelements(); i++)
|
|---|
| 481 | skytable_[i]->unsetSelection();
|
|---|
| 482 | for (uInt i = 0; i < tsystable_.size(); i++)
|
|---|
| 483 | tsystable_[i]->unsetSelection();
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 | // reset interpolator
|
|---|
| 487 | interpolatorS_->reset();
|
|---|
| 488 | interpolatorF_->reset();
|
|---|
| 489 | interpolatorT_->reset();
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | Vector<uInt> STApplyCal::timeSort(Vector<Double> &t)
|
|---|
| 493 | {
|
|---|
| 494 | Sort sort;
|
|---|
| 495 | sort.sortKey(&t[0], TpDouble, 0, Sort::Ascending);
|
|---|
| 496 | Vector<uInt> idx;
|
|---|
| 497 | sort.sort(idx, t.nelements(), Sort::QuickSort|Sort::NoDuplicates);
|
|---|
| 498 | return idx;
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | uInt STApplyCal::getIFForTsys(uInt to)
|
|---|
| 502 | {
|
|---|
| 503 | for (map<casa::uInt, Vector<uInt> >::iterator i = spwmap_.begin();
|
|---|
| 504 | i != spwmap_.end(); i++) {
|
|---|
| 505 | Vector<uInt> tolist = i->second;
|
|---|
| 506 | os_ << "from=" << i->first << ": tolist=" << tolist << LogIO::POST;
|
|---|
| 507 | for (uInt j = 0; j < tolist.nelements(); j++) {
|
|---|
| 508 | if (tolist[j] == to)
|
|---|
| 509 | return i->first;
|
|---|
| 510 | }
|
|---|
| 511 | }
|
|---|
| 512 | return (uInt)-1;
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | void STApplyCal::save(const String &name)
|
|---|
| 516 | {
|
|---|
| 517 | //assert(!work_.null());
|
|---|
| 518 | assert_<AipsError>(!work_.null(),"You have to execute apply method first.");
|
|---|
| 519 |
|
|---|
| 520 | work_->setSelection(sel_);
|
|---|
| 521 | work_->makePersistent(name);
|
|---|
| 522 | work_->unsetSelection();
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | Vector<Double> STApplyCal::getBaseFrequency(uInt whichrow)
|
|---|
| 526 | {
|
|---|
| 527 | //assert(whichrow <= (uInt)work_->nrow());
|
|---|
| 528 | assert_<AipsError>(whichrow <= (uInt)work_->nrow(),"row index out of range.");
|
|---|
| 529 | ROTableColumn col(work_->table(), "IFNO");
|
|---|
| 530 | uInt ifno = col.asuInt(whichrow);
|
|---|
| 531 | col.attach(work_->table(), "FREQ_ID");
|
|---|
| 532 | uInt freqid = col.asuInt(whichrow);
|
|---|
| 533 | uInt nc = work_->nchan(ifno);
|
|---|
| 534 | STFrequencies ftab = work_->frequencies();
|
|---|
| 535 | Double rp, rf, inc;
|
|---|
| 536 | ftab.getEntry(rp, rf, inc, freqid);
|
|---|
| 537 | Vector<Double> r(nc);
|
|---|
| 538 | indgen(r, rf-rp*inc, inc);
|
|---|
| 539 | return r;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | void STApplyCal::initInterpolator()
|
|---|
| 543 | {
|
|---|
| 544 | os_.origin(LogOrigin("STApplyCal","initInterpolator",WHERE));
|
|---|
| 545 | int order = (order_ > 0) ? order_ : 1;
|
|---|
| 546 | switch (iTime_) {
|
|---|
| 547 | case STCalEnum::NearestInterpolation:
|
|---|
| 548 | {
|
|---|
| 549 | os_ << "use NearestInterpolator in time axis" << LogIO::POST;
|
|---|
| 550 | interpolatorS_ = new NearestInterpolator1D<Double, Float>();
|
|---|
| 551 | interpolatorT_ = new NearestInterpolator1D<Double, Float>();
|
|---|
| 552 | break;
|
|---|
| 553 | }
|
|---|
| 554 | case STCalEnum::LinearInterpolation:
|
|---|
| 555 | {
|
|---|
| 556 | os_ << "use BufferedLinearInterpolator in time axis" << LogIO::POST;
|
|---|
| 557 | interpolatorS_ = new BufferedLinearInterpolator1D<Double, Float>();
|
|---|
| 558 | interpolatorT_ = new BufferedLinearInterpolator1D<Double, Float>();
|
|---|
| 559 | break;
|
|---|
| 560 | }
|
|---|
| 561 | case STCalEnum::CubicSplineInterpolation:
|
|---|
| 562 | {
|
|---|
| 563 | os_ << "use CubicSplineInterpolator in time axis" << LogIO::POST;
|
|---|
| 564 | interpolatorS_ = new CubicSplineInterpolator1D<Double, Float>();
|
|---|
| 565 | interpolatorT_ = new CubicSplineInterpolator1D<Double, Float>();
|
|---|
| 566 | break;
|
|---|
| 567 | }
|
|---|
| 568 | case STCalEnum::PolynomialInterpolation:
|
|---|
| 569 | {
|
|---|
| 570 | os_ << "use PolynomialInterpolator in time axis" << LogIO::POST;
|
|---|
| 571 | if (order == 0) {
|
|---|
| 572 | interpolatorS_ = new NearestInterpolator1D<Double, Float>();
|
|---|
| 573 | interpolatorT_ = new NearestInterpolator1D<Double, Float>();
|
|---|
| 574 | }
|
|---|
| 575 | else {
|
|---|
| 576 | interpolatorS_ = new PolynomialInterpolator1D<Double, Float>();
|
|---|
| 577 | interpolatorT_ = new PolynomialInterpolator1D<Double, Float>();
|
|---|
| 578 | interpolatorS_->setOrder(order);
|
|---|
| 579 | interpolatorT_->setOrder(order);
|
|---|
| 580 | }
|
|---|
| 581 | break;
|
|---|
| 582 | }
|
|---|
| 583 | default:
|
|---|
| 584 | {
|
|---|
| 585 | os_ << "use BufferedLinearInterpolator in time axis" << LogIO::POST;
|
|---|
| 586 | interpolatorS_ = new BufferedLinearInterpolator1D<Double, Float>();
|
|---|
| 587 | interpolatorT_ = new BufferedLinearInterpolator1D<Double, Float>();
|
|---|
| 588 | break;
|
|---|
| 589 | }
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | switch (iFreq_) {
|
|---|
| 593 | case STCalEnum::NearestInterpolation:
|
|---|
| 594 | {
|
|---|
| 595 | os_ << "use NearestInterpolator in frequency axis" << LogIO::POST;
|
|---|
| 596 | interpolatorF_ = new NearestInterpolator1D<Double, Float>();
|
|---|
| 597 | break;
|
|---|
| 598 | }
|
|---|
| 599 | case STCalEnum::LinearInterpolation:
|
|---|
| 600 | {
|
|---|
| 601 | os_ << "use BufferedLinearInterpolator in frequency axis" << LogIO::POST;
|
|---|
| 602 | interpolatorF_ = new BufferedLinearInterpolator1D<Double, Float>();
|
|---|
| 603 | break;
|
|---|
| 604 | }
|
|---|
| 605 | case STCalEnum::CubicSplineInterpolation:
|
|---|
| 606 | {
|
|---|
| 607 | os_ << "use CubicSplineInterpolator in frequency axis" << LogIO::POST;
|
|---|
| 608 | interpolatorF_ = new CubicSplineInterpolator1D<Double, Float>();
|
|---|
| 609 | break;
|
|---|
| 610 | }
|
|---|
| 611 | case STCalEnum::PolynomialInterpolation:
|
|---|
| 612 | {
|
|---|
| 613 | os_ << "use PolynomialInterpolator in frequency axis" << LogIO::POST;
|
|---|
| 614 | if (order == 0) {
|
|---|
| 615 | interpolatorF_ = new NearestInterpolator1D<Double, Float>();
|
|---|
| 616 | }
|
|---|
| 617 | else {
|
|---|
| 618 | interpolatorF_ = new PolynomialInterpolator1D<Double, Float>();
|
|---|
| 619 | interpolatorF_->setOrder(order);
|
|---|
| 620 | }
|
|---|
| 621 | break;
|
|---|
| 622 | }
|
|---|
| 623 | default:
|
|---|
| 624 | {
|
|---|
| 625 | os_ << "use LinearInterpolator in frequency axis" << LogIO::POST;
|
|---|
| 626 | interpolatorF_ = new BufferedLinearInterpolator1D<Double, Float>();
|
|---|
| 627 | break;
|
|---|
| 628 | }
|
|---|
| 629 | }
|
|---|
| 630 | }
|
|---|
| 631 | }
|
|---|