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 | // 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 |
|
---|
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;
|
---|
86 | iTime_ = STCalEnum::DefaultInterpolation;
|
---|
87 | iFreq_ = STCalEnum::DefaultInterpolation;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void STApplyCal::reset()
|
---|
91 | {
|
---|
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;
|
---|
116 | }
|
---|
117 |
|
---|
118 | void STApplyCal::completeReset()
|
---|
119 | {
|
---|
120 | reset();
|
---|
121 | target_ = 0;
|
---|
122 | }
|
---|
123 |
|
---|
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 | {
|
---|
137 | os_.origin(LogOrigin("STApplyCal","push",WHERE));
|
---|
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 |
|
---|
155 | void STApplyCal::setTimeInterpolation(STCalEnum::InterpolationType itype, Int order)
|
---|
156 | {
|
---|
157 | iTime_ = itype;
|
---|
158 | order_ = order;
|
---|
159 | }
|
---|
160 |
|
---|
161 | void STApplyCal::setFrequencyInterpolation(STCalEnum::InterpolationType itype, Int order)
|
---|
162 | {
|
---|
163 | iFreq_ = itype;
|
---|
164 | order_ = order;
|
---|
165 | }
|
---|
166 |
|
---|
167 | void STApplyCal::setTsysTransfer(uInt from, Vector<uInt> to)
|
---|
168 | {
|
---|
169 | os_.origin(LogOrigin("STApplyCal","setTsysTransfer",WHERE));
|
---|
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 |
|
---|
186 | void STApplyCal::apply(Bool insitu, Bool filltsys)
|
---|
187 | {
|
---|
188 | os_.origin(LogOrigin("STApplyCal","apply",WHERE));
|
---|
189 |
|
---|
190 | //assert(!target_.null());
|
---|
191 | assert_<AipsError>(!target_.null(),"You have to set target scantable first.");
|
---|
192 |
|
---|
193 | // calibrator
|
---|
194 | if (caltype_ == STCalEnum::CalPSAlma)
|
---|
195 | calibrator_ = new PSAlmaCalibrator();
|
---|
196 |
|
---|
197 | // interpolator
|
---|
198 | initInterpolator();
|
---|
199 |
|
---|
200 | // select data
|
---|
201 | sel_.reset();
|
---|
202 | sel_ = target_->getSelection();
|
---|
203 | if (caltype_ == STCalEnum::CalPSAlma ||
|
---|
204 | caltype_ == STCalEnum::CalPS) {
|
---|
205 | sel_.setTypes(vector<int>(1,(int)SrcType::PSON));
|
---|
206 | }
|
---|
207 | target_->setSelection(sel_);
|
---|
208 |
|
---|
209 | //os_ << "sel_.print()=" << sel_.print() << LogIO::POST;
|
---|
210 |
|
---|
211 | // working data
|
---|
212 | if (insitu) {
|
---|
213 | os_.origin(LogOrigin("STApplyCal","apply",WHERE));
|
---|
214 | os_ << "Overwrite input scantable" << LogIO::POST;
|
---|
215 | work_ = target_;
|
---|
216 | }
|
---|
217 | else {
|
---|
218 | os_.origin(LogOrigin("STApplyCal","apply",WHERE));
|
---|
219 | os_ << "Create output scantable from input" << LogIO::POST;
|
---|
220 | work_ = new Scantable(*target_, false);
|
---|
221 | }
|
---|
222 |
|
---|
223 | //os_ << "work_->nrow()=" << work_->nrow() << LogIO::POST;
|
---|
224 |
|
---|
225 | // list of apply tables for sky calibration
|
---|
226 | Vector<uInt> skycalList(skytable_.size());
|
---|
227 | uInt numSkyCal = 0;
|
---|
228 |
|
---|
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 | }
|
---|
237 | skycalList.resize(numSkyCal, True);
|
---|
238 |
|
---|
239 |
|
---|
240 | vector<string> cols( 3 ) ;
|
---|
241 | cols[0] = "BEAMNO" ;
|
---|
242 | cols[1] = "POLNO" ;
|
---|
243 | cols[2] = "IFNO" ;
|
---|
244 | CountedPtr<STIdxIter2> iter = new STIdxIter2(work_, cols) ;
|
---|
245 | double start = mathutil::gettimeofday_sec();
|
---|
246 | os_ << LogIO::DEBUGGING << "start iterative doapply: " << start << LogIO::POST;
|
---|
247 | while (!iter->pastEnd()) {
|
---|
248 | Record ids = iter->currentValue();
|
---|
249 | Vector<uInt> rows = iter->getRows(SHARE);
|
---|
250 | if (rows.nelements() > 0)
|
---|
251 | doapply(ids.asuInt("BEAMNO"), ids.asuInt("IFNO"), ids.asuInt("POLNO"), rows, skycalList, filltsys);
|
---|
252 | iter->next();
|
---|
253 | }
|
---|
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;
|
---|
257 |
|
---|
258 | target_->unsetSelection();
|
---|
259 | }
|
---|
260 |
|
---|
261 | void STApplyCal::doapply(uInt beamno, uInt ifno, uInt polno,
|
---|
262 | Vector<uInt> &rows,
|
---|
263 | Vector<uInt> &skylist,
|
---|
264 | Bool filltsys)
|
---|
265 | {
|
---|
266 | os_.origin(LogOrigin("STApplyCal","doapply",WHERE));
|
---|
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 | }
|
---|
286 |
|
---|
287 | // Skip IFNO without sky data
|
---|
288 | if (nrowSky == 0)
|
---|
289 | return;
|
---|
290 |
|
---|
291 | uInt nchanTsys = 0;
|
---|
292 | Vector<Double> ftsys;
|
---|
293 | uInt tsysifno = getIFForTsys(ifno);
|
---|
294 | os_ << "tsysifno=" << (Int)tsysifno << LogIO::POST;
|
---|
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);
|
---|
310 | uInt nrowThisTsys = tsystable_[i]->nrow();
|
---|
311 | nrowTsys += nrowThisTsys;
|
---|
312 | if (nrowThisTsys > 0 && nchanTsys == 0) {
|
---|
313 | nchanTsys = tsystable_[i]->nchan(tsysifno);
|
---|
314 | ftsys = tsystable_[i]->getBaseFrequency(0);
|
---|
315 | }
|
---|
316 | }
|
---|
317 | interpolatorF_->setX(ftsys.data(), nchanTsys);
|
---|
318 | }
|
---|
319 |
|
---|
320 | uInt nchanSp = skytable_[skylist[0]]->nchan(ifno);
|
---|
321 | uInt nrowSkySorted = nrowSky;
|
---|
322 | Vector<Double> timeSkySorted;
|
---|
323 | Matrix<Float> spoffSorted;
|
---|
324 | Matrix<uChar> flagoffSorted;
|
---|
325 | {
|
---|
326 | Vector<Double> timeSky(nrowSky);
|
---|
327 | Matrix<Float> spoff(nrowSky, nchanSp);
|
---|
328 | Matrix<uChar> flagoff(nrowSky, nchanSp);
|
---|
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();
|
---|
334 | Matrix<uChar> fl = p->getFlagtra();
|
---|
335 | for (uInt j = 0; j < t.nelements(); j++) {
|
---|
336 | timeSky[nrowSky] = t[j];
|
---|
337 | spoff.row(nrowSky) = sp.column(j);
|
---|
338 | flagoff.row(nrowSky) = fl.column(j);
|
---|
339 | nrowSky++;
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | Vector<uInt> skyIdx = indexSort(timeSky);
|
---|
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);
|
---|
357 | flagoffSorted.takeStorage(IPosition(2, nrowSkySorted, nchanSp),
|
---|
358 | new uChar[nrowSkySorted * nchanSp],
|
---|
359 | TAKE_OVER);
|
---|
360 | for (uInt i = 0 ; i < nrowSky; i++) {
|
---|
361 | spoffSorted.row(i) = spoff.row(skyIdx[i]);
|
---|
362 | flagoffSorted.row(i) = flagoff.row(skyIdx[i]);
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | uInt nrowTsysSorted = nrowTsys;
|
---|
367 | Matrix<Float> tsysSorted;
|
---|
368 | Matrix<uChar> flagtsysSorted;
|
---|
369 | Vector<Double> timeTsysSorted;
|
---|
370 | if (doTsys) {
|
---|
371 | //os_ << "doTsys" << LogIO::POST;
|
---|
372 | Vector<Double> timeTsys(nrowTsys);
|
---|
373 | Matrix<Float> tsys(nrowTsys, nchanTsys);
|
---|
374 | Matrix<uChar> flagtsys(nrowTsys, nchanTsys);
|
---|
375 | tsysSorted.takeStorage(IPosition(2, nrowTsys, nchanTsys),
|
---|
376 | new Float[nrowTsys * nchanTsys],
|
---|
377 | TAKE_OVER);
|
---|
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();
|
---|
383 | Matrix<uChar> fl = p->getFlagtra();
|
---|
384 | for (uInt j = 0; j < t.nelements(); j++) {
|
---|
385 | timeTsys[nrowTsys] = t[j];
|
---|
386 | tsys.row(nrowTsys) = ts.column(j);
|
---|
387 | flagtsys.row(nrowTsys) = fl.column(j);
|
---|
388 | nrowTsys++;
|
---|
389 | }
|
---|
390 | }
|
---|
391 | Vector<uInt> tsysIdx = indexSort(timeTsys);
|
---|
392 | nrowTsysSorted = tsysIdx.nelements();
|
---|
393 |
|
---|
394 | timeTsysSorted.takeStorage(IPosition(1, nrowTsysSorted),
|
---|
395 | new Double[nrowTsysSorted],
|
---|
396 | TAKE_OVER);
|
---|
397 | flagtsysSorted.takeStorage(IPosition(2, nrowTsysSorted, nchanTsys),
|
---|
398 | new uChar[nrowTsysSorted * nchanTsys],
|
---|
399 | TAKE_OVER);
|
---|
400 | for (uInt i = 0 ; i < nrowTsysSorted; i++) {
|
---|
401 | timeTsysSorted[i] = timeTsys[tsysIdx[i]];
|
---|
402 | }
|
---|
403 | interpolatorT_->setX(timeTsysSorted.data(), nrowTsysSorted);
|
---|
404 |
|
---|
405 | for (uInt i = 0; i < nrowTsys; ++i) {
|
---|
406 | tsysSorted.row(i) = tsys.row(tsysIdx[i]);
|
---|
407 | flagtsysSorted.row(i) = flagtsys.row(tsysIdx[i]);
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | Table tab = work_->table();
|
---|
412 | ArrayColumn<Float> spCol(tab, "SPECTRA");
|
---|
413 | ArrayColumn<uChar> flCol(tab, "FLAGTRA");
|
---|
414 | ArrayColumn<Float> tsysCol(tab, "TSYS");
|
---|
415 | ScalarColumn<Double> timeCol(tab, "TIME");
|
---|
416 | //Vector<Float> on;
|
---|
417 |
|
---|
418 | // Array for scaling factor (aka Tsys)
|
---|
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);
|
---|
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);
|
---|
433 |
|
---|
434 | for (uInt i = 0; i < rows.nelements(); i++) {
|
---|
435 | //os_ << "start i = " << i << " (row = " << rows[i] << ")" << LogIO::POST;
|
---|
436 | uInt irow = rows[i];
|
---|
437 |
|
---|
438 | // target spectral data
|
---|
439 | Vector<Float> on = spCol(irow);
|
---|
440 | Vector<uChar> flag = flCol(irow);
|
---|
441 | //os_ << "on=" << on[0] << LogIO::POST;
|
---|
442 | calibrator_->setSource(on);
|
---|
443 |
|
---|
444 | // interpolation
|
---|
445 | Double t0 = timeCol(irow);
|
---|
446 | Double *xwork_p = xwork.data();
|
---|
447 | Float *ywork_p = ywork.data();
|
---|
448 | for (uInt ichan = 0; ichan < nchanSp; ichan++) {
|
---|
449 | Float *tmpY = &(spoffSorted.data()[ichan * nrowSkySorted]);
|
---|
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
|
---|
469 | flag[ichan] = 1 << 7; // user flag
|
---|
470 | }
|
---|
471 | //interpolatorS_->setY(tmpY, nrowSkySorted);
|
---|
472 | iOff[ichan] = interpolatorS_->interpolate(t0);
|
---|
473 | }
|
---|
474 | //os_ << "iOff=" << iOff[0] << LogIO::POST;
|
---|
475 | calibrator_->setReference(iOff);
|
---|
476 |
|
---|
477 | if (doTsys) {
|
---|
478 | // Tsys correction
|
---|
479 | // interpolation on time axis
|
---|
480 | Float *yt = iTsysT.data();
|
---|
481 | uChar *fwork_p = fwork.data();
|
---|
482 | for (uInt ichan = 0; ichan < nchanTsys; ichan++) {
|
---|
483 | Float *tmpY = &(tsysSorted.data()[ichan * nrowTsysSorted]);
|
---|
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 | }
|
---|
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]);
|
---|
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);
|
---|
522 | for (uInt ichan = 0; ichan < nchanSp; ichan++) {
|
---|
523 | iTsys[ichan] = interpolatorF_->interpolate(fsp[ichan]);
|
---|
524 | }
|
---|
525 | }
|
---|
526 | }
|
---|
527 | else {
|
---|
528 | Vector<Float> tsysInRow = tsysCol(irow);
|
---|
529 | if (tsysInRow.nelements() == 1) {
|
---|
530 | iTsys = tsysInRow[0];
|
---|
531 | }
|
---|
532 | else {
|
---|
533 | for (uInt ichan = 0; ichan < tsysInRow.nelements(); ++ichan)
|
---|
534 | iTsys[ichan] = tsysInRow[ichan];
|
---|
535 | }
|
---|
536 | }
|
---|
537 | //os_ << "iTsys=" << iTsys[0] << LogIO::POST;
|
---|
538 | calibrator_->setScaler(iTsys);
|
---|
539 |
|
---|
540 | // do calibration
|
---|
541 | calibrator_->calibrate();
|
---|
542 |
|
---|
543 | // update table
|
---|
544 | //os_ << "calibrated=" << calibrator_->getCalibrated()[0] << LogIO::POST;
|
---|
545 | spCol.put(irow, calibrator_->getCalibrated());
|
---|
546 | flCol.put(irow, flag);
|
---|
547 | if (filltsys)
|
---|
548 | tsysCol.put(irow, iTsys);
|
---|
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;
|
---|
570 | os_ << "from=" << i->first << ": tolist=" << tolist << LogIO::POST;
|
---|
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 | {
|
---|
581 | //assert(!work_.null());
|
---|
582 | assert_<AipsError>(!work_.null(),"You have to execute apply method first.");
|
---|
583 |
|
---|
584 | work_->setSelection(sel_);
|
---|
585 | work_->makePersistent(name);
|
---|
586 | work_->unsetSelection();
|
---|
587 | }
|
---|
588 |
|
---|
589 | Vector<Double> STApplyCal::getBaseFrequency(uInt whichrow)
|
---|
590 | {
|
---|
591 | //assert(whichrow <= (uInt)work_->nrow());
|
---|
592 | assert_<AipsError>(whichrow <= (uInt)work_->nrow(),"row index out of range.");
|
---|
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 |
|
---|
606 | void STApplyCal::initInterpolator()
|
---|
607 | {
|
---|
608 | os_.origin(LogOrigin("STApplyCal","initInterpolator",WHERE));
|
---|
609 | int order = (order_ > 0) ? order_ : 1;
|
---|
610 | switch (iTime_) {
|
---|
611 | case STCalEnum::NearestInterpolation:
|
---|
612 | {
|
---|
613 | os_ << "use NearestInterpolator in time axis" << LogIO::POST;
|
---|
614 | interpolatorS_ = new NearestInterpolator1D<Double, Float>();
|
---|
615 | interpolatorT_ = new NearestInterpolator1D<Double, Float>();
|
---|
616 | break;
|
---|
617 | }
|
---|
618 | case STCalEnum::LinearInterpolation:
|
---|
619 | {
|
---|
620 | os_ << "use BufferedLinearInterpolator in time axis" << LogIO::POST;
|
---|
621 | interpolatorS_ = new BufferedLinearInterpolator1D<Double, Float>();
|
---|
622 | interpolatorT_ = new BufferedLinearInterpolator1D<Double, Float>();
|
---|
623 | break;
|
---|
624 | }
|
---|
625 | case STCalEnum::CubicSplineInterpolation:
|
---|
626 | {
|
---|
627 | os_ << "use CubicSplineInterpolator in time axis" << LogIO::POST;
|
---|
628 | interpolatorS_ = new CubicSplineInterpolator1D<Double, Float>();
|
---|
629 | interpolatorT_ = new CubicSplineInterpolator1D<Double, Float>();
|
---|
630 | break;
|
---|
631 | }
|
---|
632 | case STCalEnum::PolynomialInterpolation:
|
---|
633 | {
|
---|
634 | os_ << "use PolynomialInterpolator in time axis" << LogIO::POST;
|
---|
635 | if (order == 0) {
|
---|
636 | interpolatorS_ = new NearestInterpolator1D<Double, Float>();
|
---|
637 | interpolatorT_ = new NearestInterpolator1D<Double, Float>();
|
---|
638 | }
|
---|
639 | else {
|
---|
640 | interpolatorS_ = new PolynomialInterpolator1D<Double, Float>();
|
---|
641 | interpolatorT_ = new PolynomialInterpolator1D<Double, Float>();
|
---|
642 | interpolatorS_->setOrder(order);
|
---|
643 | interpolatorT_->setOrder(order);
|
---|
644 | }
|
---|
645 | break;
|
---|
646 | }
|
---|
647 | default:
|
---|
648 | {
|
---|
649 | os_ << "use BufferedLinearInterpolator in time axis" << LogIO::POST;
|
---|
650 | interpolatorS_ = new BufferedLinearInterpolator1D<Double, Float>();
|
---|
651 | interpolatorT_ = new BufferedLinearInterpolator1D<Double, Float>();
|
---|
652 | break;
|
---|
653 | }
|
---|
654 | }
|
---|
655 |
|
---|
656 | switch (iFreq_) {
|
---|
657 | case STCalEnum::NearestInterpolation:
|
---|
658 | {
|
---|
659 | os_ << "use NearestInterpolator in frequency axis" << LogIO::POST;
|
---|
660 | interpolatorF_ = new NearestInterpolator1D<Double, Float>();
|
---|
661 | break;
|
---|
662 | }
|
---|
663 | case STCalEnum::LinearInterpolation:
|
---|
664 | {
|
---|
665 | os_ << "use BufferedLinearInterpolator in frequency axis" << LogIO::POST;
|
---|
666 | interpolatorF_ = new BufferedLinearInterpolator1D<Double, Float>();
|
---|
667 | break;
|
---|
668 | }
|
---|
669 | case STCalEnum::CubicSplineInterpolation:
|
---|
670 | {
|
---|
671 | os_ << "use CubicSplineInterpolator in frequency axis" << LogIO::POST;
|
---|
672 | interpolatorF_ = new CubicSplineInterpolator1D<Double, Float>();
|
---|
673 | break;
|
---|
674 | }
|
---|
675 | case STCalEnum::PolynomialInterpolation:
|
---|
676 | {
|
---|
677 | os_ << "use PolynomialInterpolator in frequency axis" << LogIO::POST;
|
---|
678 | if (order == 0) {
|
---|
679 | interpolatorF_ = new NearestInterpolator1D<Double, Float>();
|
---|
680 | }
|
---|
681 | else {
|
---|
682 | interpolatorF_ = new PolynomialInterpolator1D<Double, Float>();
|
---|
683 | interpolatorF_->setOrder(order);
|
---|
684 | }
|
---|
685 | break;
|
---|
686 | }
|
---|
687 | default:
|
---|
688 | {
|
---|
689 | os_ << "use LinearInterpolator in frequency axis" << LogIO::POST;
|
---|
690 | interpolatorF_ = new BufferedLinearInterpolator1D<Double, Float>();
|
---|
691 | break;
|
---|
692 | }
|
---|
693 | }
|
---|
694 | }
|
---|
695 |
|
---|
696 | }
|
---|