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