source: trunk/src/STApplyCal.cpp@ 2849

Last change on this file since 2849 was 2848, checked in by Takeshi Nakazato, 11 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: No

Module(s): sd

Description: Describe your changes here...

Fixed a bug that frequency label for Tsys is not properly set.


File size: 16.3 KB
Line 
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
44using namespace casa;
45using namespace std;
46
47namespace asap {
48
49STApplyCal::STApplyCal()
50{
51 init();
52}
53
54STApplyCal::STApplyCal(CountedPtr<Scantable> target)
55 : target_(target)
56{
57 init();
58}
59
60STApplyCal::~STApplyCal()
61{
62}
63
64void STApplyCal::init()
65{
66 caltype_ = STCalEnum::NoType;
67 doTsys_ = False;
68 iTime_ = STCalEnum::DefaultInterpolation;
69 iFreq_ = STCalEnum::DefaultInterpolation;
70}
71
72void 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
100void STApplyCal::completeReset()
101{
102 reset();
103 target_ = 0;
104}
105
106void STApplyCal::setTarget(CountedPtr<Scantable> target)
107{
108 target_ = target;
109}
110
111void STApplyCal::setTarget(const String &name)
112{
113 // always create PlainTable
114 target_ = new Scantable(name, Table::Plain);
115}
116
117void 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
131void STApplyCal::push(STCalTsysTable *table)
132{
133 tsystable_.push_back(table);
134 doTsys_ = True;
135}
136
137void STApplyCal::setTimeInterpolation(STCalEnum::InterpolationType itype, Int order)
138{
139 iTime_ = itype;
140 order_ = order;
141}
142
143void STApplyCal::setFrequencyInterpolation(STCalEnum::InterpolationType itype, Int order)
144{
145 iFreq_ = itype;
146 order_ = order;
147}
148
149void 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
168void 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;
209 uInt numSkyCal = 0;
210 uInt nrowSky = 0;
211
212 // list of apply tables for Tsys calibration
213 for (uInt i = 0 ; i < skytable_.size(); i++) {
214 STCalEnum::CalType caltype = STApplyTable::getCalType(skytable_[i]);
215 if (caltype == caltype_) {
216 skycalList.resize(numSkyCal+1, True);
217 skycalList[numSkyCal] = i;
218 numSkyCal++;
219 nrowSky += skytable_[i]->nrow();
220 }
221 }
222
223
224 vector<string> cols( 3 ) ;
225 cols[0] = "BEAMNO" ;
226 cols[1] = "POLNO" ;
227 cols[2] = "IFNO" ;
228 CountedPtr<STIdxIter> iter = new STIdxIterAcc(work_, cols) ;
229 while (!iter->pastEnd()) {
230 Vector<uInt> ids = iter->current();
231 Vector<uInt> rows = iter->getRows(SHARE);
232 if (rows.nelements() > 0)
233 doapply(ids[0], ids[2], ids[1], rows, skycalList, filltsys);
234 iter->next();
235 }
236
237 target_->unsetSelection();
238}
239
240void STApplyCal::doapply(uInt beamno, uInt ifno, uInt polno,
241 Vector<uInt> &rows,
242 Vector<uInt> &skylist,
243 Bool filltsys)
244{
245 os_.origin(LogOrigin("STApplyCal","doapply",WHERE));
246 Bool doTsys = doTsys_;
247
248 STSelector sel;
249 vector<int> id(1);
250 id[0] = beamno;
251 sel.setBeams(id);
252 id[0] = ifno;
253 sel.setIFs(id);
254 id[0] = polno;
255 sel.setPolarizations(id);
256
257 // apply selection to apply tables
258 uInt nrowSky = 0;
259 uInt nrowTsys = 0;
260 for (uInt i = 0; i < skylist.nelements(); i++) {
261 skytable_[skylist[i]]->setSelection(sel);
262 nrowSky += skytable_[skylist[i]]->nrow();
263 os_ << "nrowSky=" << nrowSky << LogIO::POST;
264 }
265
266 // Skip IFNO without sky data
267 if (nrowSky == 0)
268 return;
269
270 uInt nchanTsys = 0;
271 Vector<Double> ftsys;
272 uInt tsysifno = getIFForTsys(ifno);
273 os_ << "tsysifno=" << (Int)tsysifno << LogIO::POST;
274 if (tsystable_.size() == 0) {
275 os_.origin(LogOrigin("STApplyTable", "doapply", WHERE));
276 os_ << "No Tsys tables are given. Skip Tsys calibratoin." << LogIO::POST;
277 doTsys = False;
278 }
279 else if (tsysifno == (uInt)-1) {
280 os_.origin(LogOrigin("STApplyTable", "doapply", WHERE));
281 os_ << "No corresponding Tsys for IFNO " << ifno << ". Skip Tsys calibration" << LogIO::POST;
282 doTsys = False;
283 }
284 else {
285 id[0] = (int)tsysifno;
286 sel.setIFs(id);
287 for (uInt i = 0; i < tsystable_.size() ; i++) {
288 tsystable_[i]->setSelection(sel);
289 uInt nrowThisTsys = tsystable_[i]->nrow();
290 nrowTsys += nrowThisTsys;
291 if (nrowThisTsys > 0 and nchanTsys == 0) {
292 nchanTsys = tsystable_[i]->nchan(tsysifno);
293 ftsys = tsystable_[i]->getBaseFrequency(0);
294 }
295 }
296 interpolatorF_->setX(ftsys.data(), nchanTsys);
297 }
298
299 uInt nchanSp = skytable_[skylist[0]]->nchan(ifno);
300 Vector<Double> timeSky(nrowSky);
301 Matrix<Float> spoff(nchanSp, nrowSky);
302 Vector<Float> iOff(nchanSp);
303 nrowSky = 0;
304 for (uInt i = 0 ; i < skylist.nelements(); i++) {
305 STCalSkyTable *p = skytable_[skylist[i]];
306 Vector<Double> t = p->getTime();
307 Matrix<Float> sp = p->getSpectra();
308 for (uInt j = 0; j < t.nelements(); j++) {
309 timeSky[nrowSky] = t[j];
310 spoff.column(nrowSky) = sp.column(j);
311 nrowSky++;
312 }
313 }
314
315 Vector<uInt> skyIdx = timeSort(timeSky);
316
317 Double *xa = new Double[skyIdx.nelements()];
318 Float *ya = new Float[skyIdx.nelements()];
319 IPosition ipos(1, skyIdx.nelements());
320 Vector<Double> timeSkySorted(ipos, xa, TAKE_OVER);
321 Vector<Float> tmpOff(ipos, ya, TAKE_OVER);
322 for (uInt i = 0 ; i < skyIdx.nelements(); i++) {
323 timeSkySorted[i] = timeSky[skyIdx[i]];
324 }
325
326 interpolatorS_->setX(xa, skyIdx.nelements());
327
328 Vector<uInt> tsysIdx;
329 Vector<Double> timeTsys(nrowTsys);
330 Matrix<Float> tsys;
331 Vector<Double> timeTsysSorted;
332 Vector<Float> tmpTsys;
333 if (doTsys) {
334 //os_ << "doTsys" << LogIO::POST;
335 timeTsys.resize(nrowTsys);
336 tsys.resize(nchanTsys, nrowTsys);
337 nrowTsys = 0;
338 for (uInt i = 0 ; i < tsystable_.size(); i++) {
339 STCalTsysTable *p = tsystable_[i];
340 Vector<Double> t = p->getTime();
341 Matrix<Float> ts = p->getTsys();
342 for (uInt j = 0; j < t.nelements(); j++) {
343 timeTsys[nrowTsys] = t[j];
344 tsys.column(nrowTsys) = ts.column(j);
345 nrowTsys++;
346 }
347 }
348 tsysIdx = timeSort(timeTsys);
349
350 Double *xb = new Double[tsysIdx.nelements()];
351 Float *yb = new Float[tsysIdx.nelements()];
352 IPosition ipos(1, tsysIdx.nelements());
353 timeTsysSorted.takeStorage(ipos, xb, TAKE_OVER);
354 tmpTsys.takeStorage(ipos, yb, TAKE_OVER);
355 for (uInt i = 0 ; i < tsysIdx.nelements(); i++) {
356 timeTsysSorted[i] = timeTsys[tsysIdx[i]];
357 }
358 interpolatorT_->setX(xb, tsysIdx.nelements());
359 }
360
361 Table tab = work_->table();
362 ArrayColumn<Float> spCol(tab, "SPECTRA");
363 ArrayColumn<Float> tsysCol(tab, "TSYS");
364 ScalarColumn<Double> timeCol(tab, "TIME");
365 Vector<Float> on;
366 for (uInt i = 0; i < rows.nelements(); i++) {
367 //os_ << "start i = " << i << " (row = " << rows[i] << ")" << LogIO::POST;
368 uInt irow = rows[i];
369
370 // target spectral data
371 on = spCol(irow);
372 calibrator_->setSource(on);
373
374 // interpolation
375 Double t0 = timeCol(irow);
376 for (uInt ichan = 0; ichan < nchanSp; ichan++) {
377 Vector<Float> spOffSlice = spoff.row(ichan);
378 //os_ << "spOffSlice = " << spOffSlice << LogIO::POST;
379 for (uInt j = 0; j < skyIdx.nelements(); j++) {
380 tmpOff[j] = spOffSlice[skyIdx[j]];
381 }
382 interpolatorS_->setY(ya, skyIdx.nelements());
383 iOff[ichan] = interpolatorS_->interpolate(t0);
384 }
385 //os_ << "iOff=" << iOff << LogIO::POST;
386 calibrator_->setReference(iOff);
387
388 Float *Y = new Float[nchanSp];
389 Vector<Float> iTsys(IPosition(1,nchanSp), Y, TAKE_OVER);
390 if (doTsys) {
391 // Tsys correction
392 Float *yt = new Float[nchanTsys];
393 Vector<Float> iTsysT(IPosition(1,nchanTsys), yt, TAKE_OVER);
394 Float *yb = tmpTsys.data();
395 for (uInt ichan = 0; ichan < nchanTsys; ichan++) {
396 Vector<Float> tsysSlice = tsys.row(ichan);
397 for (uInt j = 0; j < tsysIdx.nelements(); j++) {
398 tmpTsys[j] = tsysSlice[tsysIdx[j]];
399 }
400 interpolatorT_->setY(yb, tsysIdx.nelements());
401 iTsysT[ichan] = interpolatorT_->interpolate(t0);
402 }
403 if (nchanSp == 1) {
404 // take average
405 iTsys[0] = mean(iTsysT);
406 }
407 else {
408 // interpolation on frequency axis
409 Vector<Double> fsp = getBaseFrequency(rows[i]);
410 interpolatorF_->setY(yt, nchanTsys);
411 for (uInt ichan = 0; ichan < nchanSp; ichan++) {
412 iTsys[ichan] = interpolatorF_->interpolate(fsp[ichan]);
413 }
414 }
415 }
416 else {
417 Vector<Float> tsysInRow = tsysCol(irow);
418 if (tsysInRow.nelements() == 1) {
419 iTsys = tsysInRow[0];
420 }
421 else {
422 for (uInt ichan = 0; ichan < nchanTsys; ++ichan)
423 iTsys[ichan] = tsysInRow[ichan];
424 }
425 }
426 //os_ << "iTsys=" << iTsys << LogIO::POST;
427 calibrator_->setScaler(iTsys);
428
429 // do calibration
430 calibrator_->calibrate();
431
432 // update table
433 //os_ << "calibrated=" << calibrator_->getCalibrated() << LogIO::POST;
434 spCol.put(irow, calibrator_->getCalibrated());
435 if (filltsys)
436 tsysCol.put(irow, iTsys);
437 }
438
439
440 // reset selection on apply tables
441 for (uInt i = 0; i < skylist.nelements(); i++)
442 skytable_[i]->unsetSelection();
443 for (uInt i = 0; i < tsystable_.size(); i++)
444 tsystable_[i]->unsetSelection();
445
446
447 // reset interpolator
448 interpolatorS_->reset();
449 interpolatorF_->reset();
450 interpolatorT_->reset();
451}
452
453Vector<uInt> STApplyCal::timeSort(Vector<Double> &t)
454{
455 Sort sort;
456 sort.sortKey(&t[0], TpDouble, 0, Sort::Ascending);
457 Vector<uInt> idx;
458 sort.sort(idx, t.nelements(), Sort::QuickSort|Sort::NoDuplicates);
459 return idx;
460}
461
462uInt STApplyCal::getIFForTsys(uInt to)
463{
464 for (map<casa::uInt, Vector<uInt> >::iterator i = spwmap_.begin();
465 i != spwmap_.end(); i++) {
466 Vector<uInt> tolist = i->second;
467 os_ << "from=" << i->first << ": tolist=" << tolist << LogIO::POST;
468 for (uInt j = 0; j < tolist.nelements(); j++) {
469 if (tolist[j] == to)
470 return i->first;
471 }
472 }
473 return (uInt)-1;
474}
475
476void STApplyCal::save(const String &name)
477{
478 //assert(!work_.null());
479 assert_<AipsError>(!work_.null(),"You have to execute apply method first.");
480
481 work_->setSelection(sel_);
482 work_->makePersistent(name);
483 work_->unsetSelection();
484}
485
486Vector<Double> STApplyCal::getBaseFrequency(uInt whichrow)
487{
488 //assert(whichrow <= (uInt)work_->nrow());
489 assert_<AipsError>(whichrow <= (uInt)work_->nrow(),"row index out of range.");
490 ROTableColumn col(work_->table(), "IFNO");
491 uInt ifno = col.asuInt(whichrow);
492 col.attach(work_->table(), "FREQ_ID");
493 uInt freqid = col.asuInt(whichrow);
494 uInt nc = work_->nchan(ifno);
495 STFrequencies ftab = work_->frequencies();
496 Double rp, rf, inc;
497 ftab.getEntry(rp, rf, inc, freqid);
498 Vector<Double> r(nc);
499 indgen(r, rf-rp*inc, inc);
500 return r;
501}
502
503void STApplyCal::initInterpolator()
504{
505 os_.origin(LogOrigin("STApplyCal","initInterpolator",WHERE));
506 int order = (order_ > 0) ? order_ : 1;
507 switch (iTime_) {
508 case STCalEnum::NearestInterpolation:
509 {
510 os_ << "use NearestInterpolator in time axis" << LogIO::POST;
511 interpolatorS_ = new NearestInterpolator1D<Double, Float>();
512 interpolatorT_ = new NearestInterpolator1D<Double, Float>();
513 break;
514 }
515 case STCalEnum::LinearInterpolation:
516 {
517 os_ << "use BufferedLinearInterpolator in time axis" << LogIO::POST;
518 interpolatorS_ = new BufferedLinearInterpolator1D<Double, Float>();
519 interpolatorT_ = new BufferedLinearInterpolator1D<Double, Float>();
520 break;
521 }
522 case STCalEnum::CubicSplineInterpolation:
523 {
524 os_ << "use CubicSplineInterpolator in time axis" << LogIO::POST;
525 interpolatorS_ = new CubicSplineInterpolator1D<Double, Float>();
526 interpolatorT_ = new CubicSplineInterpolator1D<Double, Float>();
527 break;
528 }
529 case STCalEnum::PolynomialInterpolation:
530 {
531 os_ << "use PolynomialInterpolator in time axis" << LogIO::POST;
532 if (order == 0) {
533 interpolatorS_ = new NearestInterpolator1D<Double, Float>();
534 interpolatorT_ = new NearestInterpolator1D<Double, Float>();
535 }
536 else {
537 interpolatorS_ = new PolynomialInterpolator1D<Double, Float>();
538 interpolatorT_ = new PolynomialInterpolator1D<Double, Float>();
539 interpolatorS_->setOrder(order);
540 interpolatorT_->setOrder(order);
541 }
542 break;
543 }
544 default:
545 {
546 os_ << "use BufferedLinearInterpolator in time axis" << LogIO::POST;
547 interpolatorS_ = new BufferedLinearInterpolator1D<Double, Float>();
548 interpolatorT_ = new BufferedLinearInterpolator1D<Double, Float>();
549 break;
550 }
551 }
552
553 switch (iFreq_) {
554 case STCalEnum::NearestInterpolation:
555 {
556 os_ << "use NearestInterpolator in frequency axis" << LogIO::POST;
557 interpolatorF_ = new NearestInterpolator1D<Double, Float>();
558 break;
559 }
560 case STCalEnum::LinearInterpolation:
561 {
562 os_ << "use BufferedLinearInterpolator in frequency axis" << LogIO::POST;
563 interpolatorF_ = new BufferedLinearInterpolator1D<Double, Float>();
564 break;
565 }
566 case STCalEnum::CubicSplineInterpolation:
567 {
568 os_ << "use CubicSplineInterpolator in frequency axis" << LogIO::POST;
569 interpolatorF_ = new CubicSplineInterpolator1D<Double, Float>();
570 break;
571 }
572 case STCalEnum::PolynomialInterpolation:
573 {
574 os_ << "use PolynomialInterpolator in frequency axis" << LogIO::POST;
575 if (order == 0) {
576 interpolatorF_ = new NearestInterpolator1D<Double, Float>();
577 }
578 else {
579 interpolatorF_ = new PolynomialInterpolator1D<Double, Float>();
580 interpolatorF_->setOrder(order);
581 }
582 break;
583 }
584 default:
585 {
586 os_ << "use LinearInterpolator in frequency axis" << LogIO::POST;
587 interpolatorF_ = new BufferedLinearInterpolator1D<Double, Float>();
588 break;
589 }
590 }
591}
592}
Note: See TracBrowser for help on using the repository browser.