1 | //#---------------------------------------------------------------------------
|
---|
2 | //# SDMath.cc: A collection of single dish mathematical operations
|
---|
3 | //#---------------------------------------------------------------------------
|
---|
4 | //# Copyright (C) 2004
|
---|
5 | //# ATNF
|
---|
6 | //#
|
---|
7 | //# This program is free software; you can redistribute it and/or modify it
|
---|
8 | //# under the terms of the GNU General Public License as published by the Free
|
---|
9 | //# Software Foundation; either version 2 of the License, or (at your option)
|
---|
10 | //# any later version.
|
---|
11 | //#
|
---|
12 | //# This program is distributed in the hope that it will be useful, but
|
---|
13 | //# WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
---|
15 | //# Public License for more details.
|
---|
16 | //#
|
---|
17 | //# You should have received a copy of the GNU General Public License along
|
---|
18 | //# with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
19 | //# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
|
---|
20 | //#
|
---|
21 | //# Correspondence concerning this software should be addressed as follows:
|
---|
22 | //# Internet email: Malte.Marquarding@csiro.au
|
---|
23 | //# Postal address: Malte Marquarding,
|
---|
24 | //# Australia Telescope National Facility,
|
---|
25 | //# P.O. Box 76,
|
---|
26 | //# Epping, NSW, 2121,
|
---|
27 | //# AUSTRALIA
|
---|
28 | //#
|
---|
29 | //# $Id:
|
---|
30 | //#---------------------------------------------------------------------------
|
---|
31 | #include <vector>
|
---|
32 |
|
---|
33 | #include <casa/aips.h>
|
---|
34 | #include <casa/BasicSL/String.h>
|
---|
35 | #include <casa/Arrays/IPosition.h>
|
---|
36 | #include <casa/Arrays/Array.h>
|
---|
37 | #include <casa/Arrays/ArrayIter.h>
|
---|
38 | #include <casa/Arrays/VectorIter.h>
|
---|
39 | #include <casa/Arrays/ArrayMath.h>
|
---|
40 | #include <casa/Arrays/ArrayLogical.h>
|
---|
41 | #include <casa/Arrays/MaskedArray.h>
|
---|
42 | #include <casa/Arrays/MaskArrMath.h>
|
---|
43 | #include <casa/Arrays/MaskArrLogi.h>
|
---|
44 | #include <casa/BasicMath/Math.h>
|
---|
45 | #include <casa/Containers/Block.h>
|
---|
46 | #include <casa/Exceptions.h>
|
---|
47 | #include <casa/Quanta/Quantum.h>
|
---|
48 | #include <casa/Quanta/Unit.h>
|
---|
49 | #include <casa/Quanta/MVEpoch.h>
|
---|
50 | #include <casa/Quanta/QC.h>
|
---|
51 | #include <casa/Quanta/MVTime.h>
|
---|
52 | #include <casa/Utilities/Assert.h>
|
---|
53 |
|
---|
54 | #include <coordinates/Coordinates/SpectralCoordinate.h>
|
---|
55 | #include <coordinates/Coordinates/CoordinateSystem.h>
|
---|
56 | #include <coordinates/Coordinates/CoordinateUtil.h>
|
---|
57 | #include <coordinates/Coordinates/FrequencyAligner.h>
|
---|
58 |
|
---|
59 | #include <lattices/Lattices/LatticeUtilities.h>
|
---|
60 | #include <lattices/Lattices/RebinLattice.h>
|
---|
61 |
|
---|
62 | #include <measures/Measures/MEpoch.h>
|
---|
63 | #include <measures/Measures/MDirection.h>
|
---|
64 | #include <measures/Measures/MPosition.h>
|
---|
65 |
|
---|
66 | #include <scimath/Mathematics/VectorKernel.h>
|
---|
67 | #include <scimath/Mathematics/Convolver.h>
|
---|
68 | #include <scimath/Mathematics/InterpolateArray1D.h>
|
---|
69 | #include <scimath/Functionals/Polynomial.h>
|
---|
70 |
|
---|
71 | #include <tables/Tables/Table.h>
|
---|
72 | #include <tables/Tables/ScalarColumn.h>
|
---|
73 | #include <tables/Tables/ArrayColumn.h>
|
---|
74 | #include <tables/Tables/ReadAsciiTable.h>
|
---|
75 |
|
---|
76 | #include "MathUtils.h"
|
---|
77 | #include "SDDefs.h"
|
---|
78 | #include "SDContainer.h"
|
---|
79 | #include "SDMemTable.h"
|
---|
80 |
|
---|
81 | #include "SDMath.h"
|
---|
82 |
|
---|
83 | using namespace casa;
|
---|
84 | using namespace asap;
|
---|
85 |
|
---|
86 |
|
---|
87 | SDMath::SDMath()
|
---|
88 | {;}
|
---|
89 |
|
---|
90 | SDMath::SDMath(const SDMath& other)
|
---|
91 | {
|
---|
92 |
|
---|
93 | // No state
|
---|
94 |
|
---|
95 | }
|
---|
96 |
|
---|
97 | SDMath& SDMath::operator=(const SDMath& other)
|
---|
98 | {
|
---|
99 | if (this != &other) {
|
---|
100 | // No state
|
---|
101 | }
|
---|
102 | return *this;
|
---|
103 | }
|
---|
104 |
|
---|
105 | SDMath::~SDMath()
|
---|
106 | {;}
|
---|
107 |
|
---|
108 |
|
---|
109 |
|
---|
110 | SDMemTable* SDMath::frequencyAlignment (const SDMemTable& in, const String& refTime, const String& method) const
|
---|
111 | {
|
---|
112 |
|
---|
113 | // Get frame info from Table
|
---|
114 |
|
---|
115 | std::vector<std::string> info = in.getCoordInfo();
|
---|
116 |
|
---|
117 | // Parse frequency system
|
---|
118 |
|
---|
119 | String systemStr(info[1]);
|
---|
120 | String baseSystemStr(info[3]);
|
---|
121 | if (baseSystemStr==systemStr) {
|
---|
122 | throw(AipsError("You have not set a frequency frame different from the initial - use function set_freqframe"));
|
---|
123 | }
|
---|
124 | //
|
---|
125 | MFrequency::Types freqSystem;
|
---|
126 | MFrequency::getType(freqSystem, systemStr);
|
---|
127 |
|
---|
128 | // Do it
|
---|
129 |
|
---|
130 | return frequencyAlign (in, freqSystem, refTime, method);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 |
|
---|
135 | CountedPtr<SDMemTable> SDMath::average(const Block<CountedPtr<SDMemTable> >& in,
|
---|
136 | const Vector<Bool>& mask, Bool scanAv,
|
---|
137 | const String& weightStr, Bool alignFreq) const
|
---|
138 | //
|
---|
139 | // Weighted averaging of spectra from one or more Tables.
|
---|
140 | //
|
---|
141 | {
|
---|
142 |
|
---|
143 | // Convert weight type
|
---|
144 |
|
---|
145 | WeightType wtType = NONE;
|
---|
146 | convertWeightString(wtType, weightStr);
|
---|
147 |
|
---|
148 | // Create output Table by cloning from the first table
|
---|
149 |
|
---|
150 | SDMemTable* pTabOut = new SDMemTable(*in[0],True);
|
---|
151 |
|
---|
152 | // Setup
|
---|
153 |
|
---|
154 | IPosition shp = in[0]->rowAsMaskedArray(0).shape(); // Must not change
|
---|
155 | Array<Float> arr(shp);
|
---|
156 | Array<Bool> barr(shp);
|
---|
157 | const Bool useMask = (mask.nelements() == shp(asap::ChanAxis));
|
---|
158 |
|
---|
159 | // Columns from Tables
|
---|
160 |
|
---|
161 | ROArrayColumn<Float> tSysCol;
|
---|
162 | ROScalarColumn<Double> mjdCol;
|
---|
163 | ROScalarColumn<String> srcNameCol;
|
---|
164 | ROScalarColumn<Double> intCol;
|
---|
165 | ROArrayColumn<uInt> fqIDCol;
|
---|
166 |
|
---|
167 | // Create accumulation MaskedArray. We accumulate for each channel,if,pol,beam
|
---|
168 | // Note that the mask of the accumulation array will ALWAYS remain ALL True.
|
---|
169 | // The MA is only used so that when data which is masked Bad is added to it,
|
---|
170 | // that data does not contribute.
|
---|
171 |
|
---|
172 | Array<Float> zero(shp);
|
---|
173 | zero=0.0;
|
---|
174 | Array<Bool> good(shp);
|
---|
175 | good = True;
|
---|
176 | MaskedArray<Float> sum(zero,good);
|
---|
177 |
|
---|
178 | // Counter arrays
|
---|
179 |
|
---|
180 | Array<Float> nPts(shp); // Number of points
|
---|
181 | nPts = 0.0;
|
---|
182 | Array<Float> nInc(shp); // Increment
|
---|
183 | nInc = 1.0;
|
---|
184 |
|
---|
185 | // Create accumulation Array for variance. We accumulate for
|
---|
186 | // each if,pol,beam, but average over channel. So we need
|
---|
187 | // a shape with one less axis dropping channels.
|
---|
188 |
|
---|
189 | const uInt nAxesSub = shp.nelements() - 1;
|
---|
190 | IPosition shp2(nAxesSub);
|
---|
191 | for (uInt i=0,j=0; i<(nAxesSub+1); i++) {
|
---|
192 | if (i!=asap::ChanAxis) {
|
---|
193 | shp2(j) = shp(i);
|
---|
194 | j++;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | Array<Float> sumSq(shp2);
|
---|
198 | sumSq = 0.0;
|
---|
199 | IPosition pos2(nAxesSub,0); // For indexing
|
---|
200 |
|
---|
201 | // Time-related accumulators
|
---|
202 |
|
---|
203 | Double time;
|
---|
204 | Double timeSum = 0.0;
|
---|
205 | Double intSum = 0.0;
|
---|
206 | Double interval = 0.0;
|
---|
207 |
|
---|
208 | // To get the right shape for the Tsys accumulator we need to
|
---|
209 | // access a column from the first table. The shape of this
|
---|
210 | // array must not change
|
---|
211 |
|
---|
212 | Array<Float> tSysSum;
|
---|
213 | {
|
---|
214 | const Table& tabIn = in[0]->table();
|
---|
215 | tSysCol.attach(tabIn,"TSYS");
|
---|
216 | tSysSum.resize(tSysCol.shape(0));
|
---|
217 | }
|
---|
218 | tSysSum =0.0;
|
---|
219 | Array<Float> tSys;
|
---|
220 |
|
---|
221 | // Scan and row tracking
|
---|
222 |
|
---|
223 | Int oldScanID = 0;
|
---|
224 | Int outScanID = 0;
|
---|
225 | Int scanID = 0;
|
---|
226 | Int rowStart = 0;
|
---|
227 | Int nAccum = 0;
|
---|
228 | Int tableStart = 0;
|
---|
229 |
|
---|
230 | // Source and FreqID
|
---|
231 |
|
---|
232 | String sourceName, oldSourceName, sourceNameStart;
|
---|
233 | Vector<uInt> freqID, freqIDStart, oldFreqID;
|
---|
234 |
|
---|
235 | // Loop over tables
|
---|
236 |
|
---|
237 | Float fac = 1.0;
|
---|
238 | const uInt nTables = in.nelements();
|
---|
239 | for (uInt iTab=0; iTab<nTables; iTab++) {
|
---|
240 |
|
---|
241 | // Should check that the frequency tables don't change if doing FreqAlignment
|
---|
242 |
|
---|
243 | // Attach columns to Table
|
---|
244 |
|
---|
245 | const Table& tabIn = in[iTab]->table();
|
---|
246 | tSysCol.attach(tabIn, "TSYS");
|
---|
247 | mjdCol.attach(tabIn, "TIME");
|
---|
248 | srcNameCol.attach(tabIn, "SRCNAME");
|
---|
249 | intCol.attach(tabIn, "INTERVAL");
|
---|
250 | fqIDCol.attach(tabIn, "FREQID");
|
---|
251 |
|
---|
252 | // Loop over rows in Table
|
---|
253 |
|
---|
254 | const uInt nRows = in[iTab]->nRow();
|
---|
255 | for (uInt iRow=0; iRow<nRows; iRow++) {
|
---|
256 |
|
---|
257 | // Check conformance
|
---|
258 |
|
---|
259 | IPosition shp2 = in[iTab]->rowAsMaskedArray(iRow).shape();
|
---|
260 | if (!shp.isEqual(shp2)) {
|
---|
261 | throw (AipsError("Shapes for all rows must be the same"));
|
---|
262 | }
|
---|
263 |
|
---|
264 | // If we are not doing scan averages, make checks for source and
|
---|
265 | // frequency setup and warn if averaging across them
|
---|
266 |
|
---|
267 | // Get copy of Scan Container for this row
|
---|
268 |
|
---|
269 | SDContainer sc = in[iTab]->getSDContainer(iRow);
|
---|
270 | scanID = sc.scanid;
|
---|
271 |
|
---|
272 | // Get quantities from columns
|
---|
273 |
|
---|
274 | srcNameCol.getScalar(iRow, sourceName);
|
---|
275 | mjdCol.get(iRow, time);
|
---|
276 | tSysCol.get(iRow, tSys);
|
---|
277 | intCol.get(iRow, interval);
|
---|
278 | fqIDCol.get(iRow, freqID);
|
---|
279 |
|
---|
280 | // Initialize first source and freqID
|
---|
281 |
|
---|
282 | if (iRow==0 && iTab==0) {
|
---|
283 | sourceNameStart = sourceName;
|
---|
284 | freqIDStart = freqID;
|
---|
285 | }
|
---|
286 |
|
---|
287 | // If we are doing scan averages, see if we are at the end of an
|
---|
288 | // accumulation period (scan). We must check soutce names too,
|
---|
289 | // since we might have two tables with one scan each but different
|
---|
290 | // source names; we shouldn't average different sources together
|
---|
291 |
|
---|
292 | if (scanAv && ( (scanID != oldScanID) ||
|
---|
293 | (iRow==0 && iTab>0 && sourceName!=oldSourceName))) {
|
---|
294 |
|
---|
295 | // Normalize data in 'sum' accumulation array according to weighting scheme
|
---|
296 |
|
---|
297 | normalize(sum, sumSq, nPts, wtType, asap::ChanAxis, nAxesSub);
|
---|
298 |
|
---|
299 | // Fill scan container. The source and freqID come from the
|
---|
300 | // first row of the first table that went into this average (
|
---|
301 | // should be the same for all rows in the scan average)
|
---|
302 |
|
---|
303 | Float nR(nAccum);
|
---|
304 | fillSDC(sc, sum.getMask(), sum.getArray(), tSysSum/nR, outScanID,
|
---|
305 | timeSum/nR, intSum, sourceNameStart, freqIDStart);
|
---|
306 |
|
---|
307 | // Write container out to Table
|
---|
308 |
|
---|
309 | pTabOut->putSDContainer(sc);
|
---|
310 |
|
---|
311 | // Reset accumulators
|
---|
312 |
|
---|
313 | sum = 0.0;
|
---|
314 | sumSq = 0.0;
|
---|
315 | nAccum = 0;
|
---|
316 | //
|
---|
317 | tSysSum =0.0;
|
---|
318 | timeSum = 0.0;
|
---|
319 | intSum = 0.0;
|
---|
320 | nPts = 0.0;
|
---|
321 |
|
---|
322 | // Increment
|
---|
323 |
|
---|
324 | rowStart = iRow; // First row for next accumulation
|
---|
325 | tableStart = iTab; // First table for next accumulation
|
---|
326 | sourceNameStart = sourceName; // First source name for next accumulation
|
---|
327 | freqIDStart = freqID; // First FreqID for next accumulation
|
---|
328 | //
|
---|
329 | oldScanID = scanID;
|
---|
330 | outScanID += 1; // Scan ID for next accumulation period
|
---|
331 | }
|
---|
332 |
|
---|
333 | // Accumulate
|
---|
334 |
|
---|
335 | accumulate(timeSum, intSum, nAccum, sum, sumSq, nPts, tSysSum,
|
---|
336 | tSys, nInc, mask, time, interval, in, iTab, iRow, asap::ChanAxis,
|
---|
337 | nAxesSub, useMask, wtType);
|
---|
338 | //
|
---|
339 | oldSourceName = sourceName;
|
---|
340 | oldFreqID = freqID;
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | // OK at this point we have accumulation data which is either
|
---|
345 | // - accumulated from all tables into one row
|
---|
346 | // or
|
---|
347 | // - accumulated from the last scan average
|
---|
348 | //
|
---|
349 | // Normalize data in 'sum' accumulation array according to weighting scheme
|
---|
350 | normalize(sum, sumSq, nPts, wtType, asap::ChanAxis, nAxesSub);
|
---|
351 |
|
---|
352 | // Create and fill container. The container we clone will be from
|
---|
353 | // the last Table and the first row that went into the current
|
---|
354 | // accumulation. It probably doesn't matter that much really...
|
---|
355 |
|
---|
356 | Float nR(nAccum);
|
---|
357 | SDContainer sc = in[tableStart]->getSDContainer(rowStart);
|
---|
358 | fillSDC(sc, sum.getMask(), sum.getArray(), tSysSum/nR, outScanID,
|
---|
359 | timeSum/nR, intSum, sourceNameStart, freqIDStart);
|
---|
360 | pTabOut->putSDContainer(sc);
|
---|
361 | pTabOut->resetCursor();
|
---|
362 | //
|
---|
363 | return CountedPtr<SDMemTable>(pTabOut);
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 |
|
---|
368 | CountedPtr<SDMemTable> SDMath::binaryOperate (const CountedPtr<SDMemTable>& left,
|
---|
369 | const CountedPtr<SDMemTable>& right,
|
---|
370 | const String& op, Bool preserve,
|
---|
371 | Bool doTSys) const
|
---|
372 | {
|
---|
373 |
|
---|
374 | // Check operator
|
---|
375 |
|
---|
376 | String op2(op);
|
---|
377 | op2.upcase();
|
---|
378 | uInt what = 0;
|
---|
379 | if (op2=="ADD") {
|
---|
380 | what = 0;
|
---|
381 | } else if (op2=="SUB") {
|
---|
382 | what = 1;
|
---|
383 | } else if (op2=="MUL") {
|
---|
384 | what = 2;
|
---|
385 | } else if (op2=="DIV") {
|
---|
386 | what = 3;
|
---|
387 | } else if (op2=="QUOTIENT") {
|
---|
388 | what = 4;
|
---|
389 | doTSys = True;
|
---|
390 | } else {
|
---|
391 | throw( AipsError("Unrecognized operation"));
|
---|
392 | }
|
---|
393 |
|
---|
394 | // Check rows
|
---|
395 |
|
---|
396 | const uInt nRowLeft = left->nRow();
|
---|
397 | const uInt nRowRight = right->nRow();
|
---|
398 | Bool ok = (nRowRight==1&&nRowLeft>0) ||
|
---|
399 | (nRowRight>=1&&nRowLeft==nRowRight);
|
---|
400 | if (!ok) {
|
---|
401 | throw (AipsError("The right Scan Table can have one row or the same number of rows as the left Scan Table"));
|
---|
402 | }
|
---|
403 |
|
---|
404 | // Input Tables
|
---|
405 |
|
---|
406 | const Table& tLeft = left->table();
|
---|
407 | const Table& tRight = right->table();
|
---|
408 |
|
---|
409 | // TSys columns
|
---|
410 |
|
---|
411 | ROArrayColumn<Float> tSysLeftCol, tSysRightCol;
|
---|
412 | if (doTSys) {
|
---|
413 | tSysLeftCol.attach(tLeft, "TSYS");
|
---|
414 | tSysRightCol.attach(tRight, "TSYS");
|
---|
415 | }
|
---|
416 |
|
---|
417 | // First row for right
|
---|
418 |
|
---|
419 | Array<Float> tSysLeftArr, tSysRightArr;
|
---|
420 | if (doTSys) tSysRightCol.get(0, tSysRightArr);
|
---|
421 | MaskedArray<Float>* pMRight = new MaskedArray<Float>(right->rowAsMaskedArray(0));
|
---|
422 | IPosition shpRight = pMRight->shape();
|
---|
423 |
|
---|
424 | // Output Table cloned from left
|
---|
425 |
|
---|
426 | SDMemTable* pTabOut = new SDMemTable(*left, True);
|
---|
427 |
|
---|
428 | // Loop over rows
|
---|
429 |
|
---|
430 | for (uInt i=0; i<nRowLeft; i++) {
|
---|
431 |
|
---|
432 | // Get data
|
---|
433 |
|
---|
434 | MaskedArray<Float> mLeft(left->rowAsMaskedArray(i));
|
---|
435 | IPosition shpLeft = mLeft.shape();
|
---|
436 | if (doTSys) tSysLeftCol.get(i, tSysLeftArr);
|
---|
437 | //
|
---|
438 | if (nRowRight>1) {
|
---|
439 | delete pMRight;
|
---|
440 | pMRight = new MaskedArray<Float>(right->rowAsMaskedArray(i));
|
---|
441 | shpRight = pMRight->shape();
|
---|
442 | if (doTSys) tSysRightCol.get(i, tSysRightArr);
|
---|
443 | }
|
---|
444 | //
|
---|
445 | if (!shpRight.isEqual(shpLeft)) {
|
---|
446 | throw(AipsError("left and right scan tables are not conformant"));
|
---|
447 | }
|
---|
448 | if (doTSys) {
|
---|
449 | if (!tSysRightArr.shape().isEqual(tSysRightArr.shape())) {
|
---|
450 | throw(AipsError("left and right Tsys data are not conformant"));
|
---|
451 | }
|
---|
452 | if (!shpRight.isEqual(tSysRightArr.shape())) {
|
---|
453 | throw(AipsError("left and right scan tables are not conformant"));
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 | // Make container
|
---|
458 |
|
---|
459 | SDContainer sc = left->getSDContainer(i);
|
---|
460 |
|
---|
461 | // Operate on data and TSys
|
---|
462 |
|
---|
463 | if (what==0) {
|
---|
464 | MaskedArray<Float> tmp = mLeft + *pMRight;
|
---|
465 | putDataInSDC(sc, tmp.getArray(), tmp.getMask());
|
---|
466 | if (doTSys) sc.putTsys(tSysLeftArr+tSysRightArr);
|
---|
467 | } else if (what==1) {
|
---|
468 | MaskedArray<Float> tmp = mLeft - *pMRight;
|
---|
469 | putDataInSDC(sc, tmp.getArray(), tmp.getMask());
|
---|
470 | if (doTSys) sc.putTsys(tSysLeftArr-tSysRightArr);
|
---|
471 | } else if (what==2) {
|
---|
472 | MaskedArray<Float> tmp = mLeft * *pMRight;
|
---|
473 | putDataInSDC(sc, tmp.getArray(), tmp.getMask());
|
---|
474 | if (doTSys) sc.putTsys(tSysLeftArr*tSysRightArr);
|
---|
475 | } else if (what==3) {
|
---|
476 | MaskedArray<Float> tmp = mLeft / *pMRight;
|
---|
477 | putDataInSDC(sc, tmp.getArray(), tmp.getMask());
|
---|
478 | if (doTSys) sc.putTsys(tSysLeftArr/tSysRightArr);
|
---|
479 | } else if (what==4) {
|
---|
480 | if (preserve) {
|
---|
481 | MaskedArray<Float> tmp = (tSysRightArr * mLeft / *pMRight) - tSysRightArr;
|
---|
482 | putDataInSDC(sc, tmp.getArray(), tmp.getMask());
|
---|
483 | } else {
|
---|
484 | MaskedArray<Float> tmp = (tSysRightArr * mLeft / *pMRight) - tSysLeftArr;
|
---|
485 | putDataInSDC(sc, tmp.getArray(), tmp.getMask());
|
---|
486 | }
|
---|
487 | sc.putTsys(tSysRightArr);
|
---|
488 | }
|
---|
489 |
|
---|
490 | // Put new row in output Table
|
---|
491 |
|
---|
492 | pTabOut->putSDContainer(sc);
|
---|
493 | }
|
---|
494 | if (pMRight) delete pMRight;
|
---|
495 | pTabOut->resetCursor();
|
---|
496 | //
|
---|
497 | return CountedPtr<SDMemTable>(pTabOut);
|
---|
498 | }
|
---|
499 |
|
---|
500 |
|
---|
501 |
|
---|
502 | std::vector<float> SDMath::statistic(const CountedPtr<SDMemTable>& in,
|
---|
503 | const Vector<Bool>& mask,
|
---|
504 | const String& which, Int row) const
|
---|
505 | //
|
---|
506 | // Perhaps iteration over pol/beam/if should be in here
|
---|
507 | // and inside the nrow iteration ?
|
---|
508 | //
|
---|
509 | {
|
---|
510 | const uInt nRow = in->nRow();
|
---|
511 |
|
---|
512 | // Specify cursor location
|
---|
513 |
|
---|
514 | IPosition start, end;
|
---|
515 | getCursorLocation(start, end, *in);
|
---|
516 |
|
---|
517 | // Loop over rows
|
---|
518 |
|
---|
519 | const uInt nEl = mask.nelements();
|
---|
520 | uInt iStart = 0;
|
---|
521 | uInt iEnd = in->nRow()-1;
|
---|
522 | //
|
---|
523 | if (row>=0) {
|
---|
524 | iStart = row;
|
---|
525 | iEnd = row;
|
---|
526 | }
|
---|
527 | //
|
---|
528 | std::vector<float> result(iEnd-iStart+1);
|
---|
529 | for (uInt ii=iStart; ii <= iEnd; ++ii) {
|
---|
530 |
|
---|
531 | // Get row and deconstruct
|
---|
532 |
|
---|
533 | MaskedArray<Float> marr(in->rowAsMaskedArray(ii));
|
---|
534 | Array<Float> arr = marr.getArray();
|
---|
535 | Array<Bool> barr = marr.getMask();
|
---|
536 |
|
---|
537 | // Access desired piece of data
|
---|
538 |
|
---|
539 | Array<Float> v((arr(start,end)).nonDegenerate());
|
---|
540 | Array<Bool> m((barr(start,end)).nonDegenerate());
|
---|
541 |
|
---|
542 | // Apply OTF mask
|
---|
543 |
|
---|
544 | MaskedArray<Float> tmp;
|
---|
545 | if (m.nelements()==nEl) {
|
---|
546 | tmp.setData(v,m&&mask);
|
---|
547 | } else {
|
---|
548 | tmp.setData(v,m);
|
---|
549 | }
|
---|
550 |
|
---|
551 | // Get statistic
|
---|
552 |
|
---|
553 | result[ii-iStart] = mathutil::statistics(which, tmp);
|
---|
554 | }
|
---|
555 | //
|
---|
556 | return result;
|
---|
557 | }
|
---|
558 |
|
---|
559 |
|
---|
560 | SDMemTable* SDMath::bin(const SDMemTable& in, Int width) const
|
---|
561 | {
|
---|
562 | SDHeader sh = in.getSDHeader();
|
---|
563 | SDMemTable* pTabOut = new SDMemTable(in, True);
|
---|
564 |
|
---|
565 | // Bin up SpectralCoordinates
|
---|
566 |
|
---|
567 | IPosition factors(1);
|
---|
568 | factors(0) = width;
|
---|
569 | for (uInt j=0; j<in.nCoordinates(); ++j) {
|
---|
570 | CoordinateSystem cSys;
|
---|
571 | cSys.addCoordinate(in.getSpectralCoordinate(j));
|
---|
572 | CoordinateSystem cSysBin =
|
---|
573 | CoordinateUtil::makeBinnedCoordinateSystem(factors, cSys, False);
|
---|
574 | //
|
---|
575 | SpectralCoordinate sCBin = cSysBin.spectralCoordinate(0);
|
---|
576 | pTabOut->setCoordinate(sCBin, j);
|
---|
577 | }
|
---|
578 |
|
---|
579 | // Use RebinLattice to find shape
|
---|
580 |
|
---|
581 | IPosition shapeIn(1,sh.nchan);
|
---|
582 | IPosition shapeOut = RebinLattice<Float>::rebinShape(shapeIn, factors);
|
---|
583 | sh.nchan = shapeOut(0);
|
---|
584 | pTabOut->putSDHeader(sh);
|
---|
585 |
|
---|
586 | // Loop over rows and bin along channel axis
|
---|
587 |
|
---|
588 | for (uInt i=0; i < in.nRow(); ++i) {
|
---|
589 | SDContainer sc = in.getSDContainer(i);
|
---|
590 | //
|
---|
591 | Array<Float> tSys(sc.getTsys()); // Get it out before sc changes shape
|
---|
592 |
|
---|
593 | // Bin up spectrum
|
---|
594 |
|
---|
595 | MaskedArray<Float> marr(in.rowAsMaskedArray(i));
|
---|
596 | MaskedArray<Float> marrout;
|
---|
597 | LatticeUtilities::bin(marrout, marr, asap::ChanAxis, width);
|
---|
598 |
|
---|
599 | // Put back the binned data and flags
|
---|
600 |
|
---|
601 | IPosition ip2 = marrout.shape();
|
---|
602 | sc.resize(ip2);
|
---|
603 | //
|
---|
604 | putDataInSDC(sc, marrout.getArray(), marrout.getMask());
|
---|
605 |
|
---|
606 | // Bin up Tsys.
|
---|
607 |
|
---|
608 | Array<Bool> allGood(tSys.shape(),True);
|
---|
609 | MaskedArray<Float> tSysIn(tSys, allGood, True);
|
---|
610 | //
|
---|
611 | MaskedArray<Float> tSysOut;
|
---|
612 | LatticeUtilities::bin(tSysOut, tSysIn, asap::ChanAxis, width);
|
---|
613 | sc.putTsys(tSysOut.getArray());
|
---|
614 | //
|
---|
615 | pTabOut->putSDContainer(sc);
|
---|
616 | }
|
---|
617 | return pTabOut;
|
---|
618 | }
|
---|
619 |
|
---|
620 | SDMemTable* SDMath::resample (const SDMemTable& in, const String& methodStr,
|
---|
621 | Float width) const
|
---|
622 | //
|
---|
623 | // Should add the possibility of width being specified in km/s. This means
|
---|
624 | // that for each freqID (SpectralCoordinate) we will need to convert to an
|
---|
625 | // average channel width (say at the reference pixel). Then we would need
|
---|
626 | // to be careful to make sure each spectrum (of different freqID)
|
---|
627 | // is the same length.
|
---|
628 | //
|
---|
629 | {
|
---|
630 | Bool doVel = False;
|
---|
631 | if (doVel) {
|
---|
632 | for (uInt j=0; j<in.nCoordinates(); ++j) {
|
---|
633 | SpectralCoordinate sC = in.getSpectralCoordinate(j);
|
---|
634 | }
|
---|
635 | }
|
---|
636 |
|
---|
637 | // Interpolation method
|
---|
638 |
|
---|
639 | InterpolateArray1D<Double,Float>::InterpolationMethod interp;
|
---|
640 | convertInterpString(interp, methodStr);
|
---|
641 | Int interpMethod(interp);
|
---|
642 |
|
---|
643 | // Make output table
|
---|
644 |
|
---|
645 | SDMemTable* pTabOut = new SDMemTable(in, True);
|
---|
646 |
|
---|
647 | // Resample SpectralCoordinates (one per freqID)
|
---|
648 |
|
---|
649 | const uInt nCoord = in.nCoordinates();
|
---|
650 | Vector<Float> offset(1,0.0);
|
---|
651 | Vector<Float> factors(1,1.0/width);
|
---|
652 | Vector<Int> newShape;
|
---|
653 | for (uInt j=0; j<in.nCoordinates(); ++j) {
|
---|
654 | CoordinateSystem cSys;
|
---|
655 | cSys.addCoordinate(in.getSpectralCoordinate(j));
|
---|
656 | CoordinateSystem cSys2 = cSys.subImage(offset, factors, newShape);
|
---|
657 | SpectralCoordinate sC = cSys2.spectralCoordinate(0);
|
---|
658 | //
|
---|
659 | pTabOut->setCoordinate(sC, j);
|
---|
660 | }
|
---|
661 |
|
---|
662 | // Get header
|
---|
663 |
|
---|
664 | SDHeader sh = in.getSDHeader();
|
---|
665 |
|
---|
666 | // Generate resampling vectors
|
---|
667 |
|
---|
668 | const uInt nChanIn = sh.nchan;
|
---|
669 | Vector<Float> xIn(nChanIn);
|
---|
670 | indgen(xIn);
|
---|
671 | //
|
---|
672 | Int fac = Int(nChanIn/width);
|
---|
673 | Vector<Float> xOut(fac+10); // 10 to be safe - resize later
|
---|
674 | uInt i = 0;
|
---|
675 | Float x = 0.0;
|
---|
676 | Bool more = True;
|
---|
677 | while (more) {
|
---|
678 | xOut(i) = x;
|
---|
679 | //
|
---|
680 | i++;
|
---|
681 | x += width;
|
---|
682 | if (x>nChanIn-1) more = False;
|
---|
683 | }
|
---|
684 | const uInt nChanOut = i;
|
---|
685 | xOut.resize(nChanOut,True);
|
---|
686 | //
|
---|
687 | IPosition shapeIn(in.rowAsMaskedArray(0).shape());
|
---|
688 | sh.nchan = nChanOut;
|
---|
689 | pTabOut->putSDHeader(sh);
|
---|
690 |
|
---|
691 | // Loop over rows and resample along channel axis
|
---|
692 |
|
---|
693 | Array<Float> valuesOut;
|
---|
694 | Array<Bool> maskOut;
|
---|
695 | Array<Float> tSysOut;
|
---|
696 | Array<Bool> tSysMaskIn(shapeIn,True);
|
---|
697 | Array<Bool> tSysMaskOut;
|
---|
698 | for (uInt i=0; i < in.nRow(); ++i) {
|
---|
699 |
|
---|
700 | // Get container
|
---|
701 |
|
---|
702 | SDContainer sc = in.getSDContainer(i);
|
---|
703 |
|
---|
704 | // Get data and Tsys
|
---|
705 |
|
---|
706 | const Array<Float>& tSysIn = sc.getTsys();
|
---|
707 | const MaskedArray<Float>& dataIn(in.rowAsMaskedArray(i));
|
---|
708 | Array<Float> valuesIn = dataIn.getArray();
|
---|
709 | Array<Bool> maskIn = dataIn.getMask();
|
---|
710 |
|
---|
711 | // Interpolate data
|
---|
712 |
|
---|
713 | InterpolateArray1D<Float,Float>::interpolate(valuesOut, maskOut, xOut,
|
---|
714 | xIn, valuesIn, maskIn,
|
---|
715 | interpMethod, True, True);
|
---|
716 | sc.resize(valuesOut.shape());
|
---|
717 | putDataInSDC(sc, valuesOut, maskOut);
|
---|
718 |
|
---|
719 | // Interpolate TSys
|
---|
720 |
|
---|
721 | InterpolateArray1D<Float,Float>::interpolate(tSysOut, tSysMaskOut, xOut,
|
---|
722 | xIn, tSysIn, tSysMaskIn,
|
---|
723 | interpMethod, True, True);
|
---|
724 | sc.putTsys(tSysOut);
|
---|
725 |
|
---|
726 | // Put container in output
|
---|
727 |
|
---|
728 | pTabOut->putSDContainer(sc);
|
---|
729 | }
|
---|
730 | //
|
---|
731 | return pTabOut;
|
---|
732 | }
|
---|
733 |
|
---|
734 | SDMemTable* SDMath::unaryOperate(const SDMemTable& in, Float val, Bool doAll,
|
---|
735 | uInt what, Bool doTSys) const
|
---|
736 | //
|
---|
737 | // what = 0 Multiply
|
---|
738 | // 1 Add
|
---|
739 | {
|
---|
740 | SDMemTable* pOut = new SDMemTable(in,False);
|
---|
741 | const Table& tOut = pOut->table();
|
---|
742 | ArrayColumn<Float> specCol(tOut,"SPECTRA");
|
---|
743 | ArrayColumn<Float> tSysCol(tOut,"TSYS");
|
---|
744 | Array<Float> tSysArr;
|
---|
745 | //
|
---|
746 | if (doAll) {
|
---|
747 | for (uInt i=0; i < tOut.nrow(); i++) {
|
---|
748 |
|
---|
749 | // Modify data
|
---|
750 |
|
---|
751 | MaskedArray<Float> dataIn(pOut->rowAsMaskedArray(i));
|
---|
752 | if (what==0) {
|
---|
753 | dataIn *= val;
|
---|
754 | } else if (what==1) {
|
---|
755 | dataIn += val;
|
---|
756 | }
|
---|
757 | specCol.put(i, dataIn.getArray());
|
---|
758 |
|
---|
759 | // Modify Tsys
|
---|
760 |
|
---|
761 | if (doTSys) {
|
---|
762 | tSysCol.get(i, tSysArr);
|
---|
763 | if (what==0) {
|
---|
764 | tSysArr *= val;
|
---|
765 | } else if (what==1) {
|
---|
766 | tSysArr += val;
|
---|
767 | }
|
---|
768 | tSysCol.put(i, tSysArr);
|
---|
769 | }
|
---|
770 | }
|
---|
771 | } else {
|
---|
772 |
|
---|
773 | // Get cursor location
|
---|
774 |
|
---|
775 | IPosition start, end;
|
---|
776 | getCursorLocation(start, end, in);
|
---|
777 | //
|
---|
778 | for (uInt i=0; i < tOut.nrow(); i++) {
|
---|
779 |
|
---|
780 | // Modify data
|
---|
781 |
|
---|
782 | MaskedArray<Float> dataIn(pOut->rowAsMaskedArray(i));
|
---|
783 | MaskedArray<Float> dataIn2 = dataIn(start,end); // Reference
|
---|
784 | if (what==0) {
|
---|
785 | dataIn2 *= val;
|
---|
786 | } else if (what==1) {
|
---|
787 | dataIn2 += val;
|
---|
788 | }
|
---|
789 | specCol.put(i, dataIn.getArray());
|
---|
790 |
|
---|
791 | // Modify Tsys
|
---|
792 |
|
---|
793 | if (doTSys) {
|
---|
794 | tSysCol.get(i, tSysArr);
|
---|
795 | Array<Float> tSysArr2 = tSysArr(start,end); // Reference
|
---|
796 | if (what==0) {
|
---|
797 | tSysArr2 *= val;
|
---|
798 | } else if (what==1) {
|
---|
799 | tSysArr2 += val;
|
---|
800 | }
|
---|
801 | tSysCol.put(i, tSysArr);
|
---|
802 | }
|
---|
803 | }
|
---|
804 | }
|
---|
805 | //
|
---|
806 | return pOut;
|
---|
807 | }
|
---|
808 |
|
---|
809 |
|
---|
810 |
|
---|
811 | SDMemTable* SDMath::averagePol(const SDMemTable& in, const Vector<Bool>& mask,
|
---|
812 | const String& weightStr) const
|
---|
813 | //
|
---|
814 | // Average all polarizations together, weighted by variance
|
---|
815 | //
|
---|
816 | {
|
---|
817 | WeightType wtType = NONE;
|
---|
818 | convertWeightString(wtType, weightStr);
|
---|
819 |
|
---|
820 | const uInt nRows = in.nRow();
|
---|
821 |
|
---|
822 | // Create output Table and reshape number of polarizations
|
---|
823 |
|
---|
824 | Bool clear=True;
|
---|
825 | SDMemTable* pTabOut = new SDMemTable(in, clear);
|
---|
826 | SDHeader header = pTabOut->getSDHeader();
|
---|
827 | header.npol = 1;
|
---|
828 | pTabOut->putSDHeader(header);
|
---|
829 |
|
---|
830 | // Shape of input and output data
|
---|
831 |
|
---|
832 | const IPosition& shapeIn = in.rowAsMaskedArray(0u, False).shape();
|
---|
833 | IPosition shapeOut(shapeIn);
|
---|
834 | shapeOut(asap::PolAxis) = 1; // Average all polarizations
|
---|
835 | if (shapeIn(asap::PolAxis)==1) {
|
---|
836 | throw(AipsError("The input has only one polarisation"));
|
---|
837 | }
|
---|
838 | //
|
---|
839 | const uInt nChan = shapeIn(asap::ChanAxis);
|
---|
840 | const IPosition vecShapeOut(4,1,1,1,nChan); // A multi-dim form of a Vector shape
|
---|
841 | IPosition start(4), end(4);
|
---|
842 |
|
---|
843 | // Output arrays
|
---|
844 |
|
---|
845 | Array<Float> outData(shapeOut, 0.0);
|
---|
846 | Array<Bool> outMask(shapeOut, True);
|
---|
847 | const IPosition axes(2, asap::PolAxis, asap::ChanAxis); // pol-channel plane
|
---|
848 | //
|
---|
849 | const Bool useMask = (mask.nelements() == shapeIn(asap::ChanAxis));
|
---|
850 |
|
---|
851 | // Loop over rows
|
---|
852 |
|
---|
853 | for (uInt iRow=0; iRow<nRows; iRow++) {
|
---|
854 |
|
---|
855 | // Get data for this row
|
---|
856 |
|
---|
857 | MaskedArray<Float> marr(in.rowAsMaskedArray(iRow));
|
---|
858 | Array<Float>& arr = marr.getRWArray();
|
---|
859 | const Array<Bool>& barr = marr.getMask();
|
---|
860 |
|
---|
861 | // Make iterators to iterate by pol-channel planes
|
---|
862 |
|
---|
863 | ReadOnlyArrayIterator<Float> itDataPlane(arr, axes);
|
---|
864 | ReadOnlyArrayIterator<Bool> itMaskPlane(barr, axes);
|
---|
865 |
|
---|
866 | // Accumulations
|
---|
867 |
|
---|
868 | Float fac = 1.0;
|
---|
869 | Vector<Float> vecSum(nChan,0.0);
|
---|
870 |
|
---|
871 | // Iterate through data by pol-channel planes
|
---|
872 |
|
---|
873 | while (!itDataPlane.pastEnd()) {
|
---|
874 |
|
---|
875 | // Iterate through plane by polarization and accumulate Vectors
|
---|
876 |
|
---|
877 | Vector<Float> t1(nChan); t1 = 0.0;
|
---|
878 | Vector<Bool> t2(nChan); t2 = True;
|
---|
879 | MaskedArray<Float> vecSum(t1,t2);
|
---|
880 | Float norm = 0.0;
|
---|
881 | {
|
---|
882 | ReadOnlyVectorIterator<Float> itDataVec(itDataPlane.array(), 1);
|
---|
883 | ReadOnlyVectorIterator<Bool> itMaskVec(itMaskPlane.array(), 1);
|
---|
884 | while (!itDataVec.pastEnd()) {
|
---|
885 |
|
---|
886 | // Create MA of data & mask (optionally including OTF mask) and get variance for this spectrum
|
---|
887 |
|
---|
888 | if (useMask) {
|
---|
889 | const MaskedArray<Float> spec(itDataVec.vector(),mask&&itMaskVec.vector());
|
---|
890 | if (wtType==VAR) fac = 1.0 / variance(spec);
|
---|
891 | } else {
|
---|
892 | const MaskedArray<Float> spec(itDataVec.vector(),itMaskVec.vector());
|
---|
893 | if (wtType==VAR) fac = 1.0 / variance(spec);
|
---|
894 | }
|
---|
895 |
|
---|
896 | // Normalize spectrum (without OTF mask) and accumulate
|
---|
897 |
|
---|
898 | const MaskedArray<Float> spec(fac*itDataVec.vector(), itMaskVec.vector());
|
---|
899 | vecSum += spec;
|
---|
900 | norm += fac;
|
---|
901 |
|
---|
902 | // Next
|
---|
903 |
|
---|
904 | itDataVec.next();
|
---|
905 | itMaskVec.next();
|
---|
906 | }
|
---|
907 | }
|
---|
908 |
|
---|
909 | // Normalize summed spectrum
|
---|
910 |
|
---|
911 | vecSum /= norm;
|
---|
912 |
|
---|
913 | // FInd position in input data array. We are iterating by pol-channel
|
---|
914 | // plane so all that will change is beam and IF and that's what we want.
|
---|
915 |
|
---|
916 | IPosition pos = itDataPlane.pos();
|
---|
917 |
|
---|
918 | // Write out data. This is a bit messy. We have to reform the Vector
|
---|
919 | // accumulator into an Array of shape (1,1,1,nChan)
|
---|
920 |
|
---|
921 | start = pos;
|
---|
922 | end = pos;
|
---|
923 | end(asap::ChanAxis) = nChan-1;
|
---|
924 | outData(start,end) = vecSum.getArray().reform(vecShapeOut);
|
---|
925 | outMask(start,end) = vecSum.getMask().reform(vecShapeOut);
|
---|
926 |
|
---|
927 | // Step to next beam/IF combination
|
---|
928 |
|
---|
929 | itDataPlane.next();
|
---|
930 | itMaskPlane.next();
|
---|
931 | }
|
---|
932 |
|
---|
933 | // Generate output container and write it to output table
|
---|
934 |
|
---|
935 | SDContainer sc = in.getSDContainer();
|
---|
936 | sc.resize(shapeOut);
|
---|
937 | //
|
---|
938 | putDataInSDC(sc, outData, outMask);
|
---|
939 | pTabOut->putSDContainer(sc);
|
---|
940 | }
|
---|
941 |
|
---|
942 | // Set polarization cursor to 0
|
---|
943 |
|
---|
944 | pTabOut->setPol(0);
|
---|
945 | //
|
---|
946 | return pTabOut;
|
---|
947 | }
|
---|
948 |
|
---|
949 |
|
---|
950 | SDMemTable* SDMath::smooth(const SDMemTable& in,
|
---|
951 | const casa::String& kernelType,
|
---|
952 | casa::Float width, Bool doAll) const
|
---|
953 | //
|
---|
954 | // Should smooth TSys as well
|
---|
955 | //
|
---|
956 | {
|
---|
957 |
|
---|
958 | // Number of channels
|
---|
959 |
|
---|
960 | SDHeader sh = in.getSDHeader();
|
---|
961 | const uInt nChan = sh.nchan;
|
---|
962 |
|
---|
963 | // Generate Kernel
|
---|
964 |
|
---|
965 | VectorKernel::KernelTypes type = VectorKernel::toKernelType(kernelType);
|
---|
966 | Vector<Float> kernel = VectorKernel::make(type, width, nChan, True, False);
|
---|
967 |
|
---|
968 | // Generate Convolver
|
---|
969 |
|
---|
970 | IPosition shape(1,nChan);
|
---|
971 | Convolver<Float> conv(kernel, shape);
|
---|
972 |
|
---|
973 | // New Table
|
---|
974 |
|
---|
975 | SDMemTable* pTabOut = new SDMemTable(in,True);
|
---|
976 |
|
---|
977 | // Get cursor location
|
---|
978 |
|
---|
979 | IPosition start, end;
|
---|
980 | getCursorLocation(start, end, in);
|
---|
981 | //
|
---|
982 | IPosition shapeOut(4,1);
|
---|
983 |
|
---|
984 | // Output Vectors
|
---|
985 |
|
---|
986 | Vector<Float> valuesOut(nChan);
|
---|
987 | Vector<Bool> maskOut(nChan);
|
---|
988 |
|
---|
989 | // Loop over rows in Table
|
---|
990 |
|
---|
991 | for (uInt ri=0; ri < in.nRow(); ++ri) {
|
---|
992 |
|
---|
993 | // Get copy of data
|
---|
994 |
|
---|
995 | const MaskedArray<Float>& dataIn(in.rowAsMaskedArray(ri));
|
---|
996 | AlwaysAssert(dataIn.shape()(asap::ChanAxis)==nChan, AipsError);
|
---|
997 | //
|
---|
998 | Array<Float> valuesIn = dataIn.getArray();
|
---|
999 | Array<Bool> maskIn = dataIn.getMask();
|
---|
1000 |
|
---|
1001 | // Branch depending on whether we smooth all locations or just
|
---|
1002 | // those pointed at by the current selection cursor
|
---|
1003 |
|
---|
1004 | if (doAll) {
|
---|
1005 | VectorIterator<Float> itValues(valuesIn, asap::ChanAxis);
|
---|
1006 | VectorIterator<Bool> itMask(maskIn, asap::ChanAxis);
|
---|
1007 | while (!itValues.pastEnd()) {
|
---|
1008 |
|
---|
1009 | // Smooth
|
---|
1010 | if (kernelType==VectorKernel::HANNING) {
|
---|
1011 | mathutil::hanning(valuesOut, maskOut, itValues.vector(), itMask.vector());
|
---|
1012 | itMask.vector() = maskOut;
|
---|
1013 | } else {
|
---|
1014 | mathutil::replaceMaskByZero(itValues.vector(), itMask.vector());
|
---|
1015 | conv.linearConv(valuesOut, itValues.vector());
|
---|
1016 | }
|
---|
1017 | //
|
---|
1018 | itValues.vector() = valuesOut;
|
---|
1019 | //
|
---|
1020 | itValues.next();
|
---|
1021 | itMask.next();
|
---|
1022 | }
|
---|
1023 | } else {
|
---|
1024 |
|
---|
1025 | // Set multi-dim Vector shape
|
---|
1026 |
|
---|
1027 | shapeOut(asap::ChanAxis) = valuesIn.shape()(asap::ChanAxis);
|
---|
1028 |
|
---|
1029 | // Stuff about with shapes so that we don't have conformance run-time errors
|
---|
1030 |
|
---|
1031 | Vector<Float> valuesIn2 = valuesIn(start,end).nonDegenerate();
|
---|
1032 | Vector<Bool> maskIn2 = maskIn(start,end).nonDegenerate();
|
---|
1033 |
|
---|
1034 | // Smooth
|
---|
1035 |
|
---|
1036 | if (kernelType==VectorKernel::HANNING) {
|
---|
1037 | mathutil::hanning(valuesOut, maskOut, valuesIn2, maskIn2);
|
---|
1038 | maskIn(start,end) = maskOut.reform(shapeOut);
|
---|
1039 | } else {
|
---|
1040 | mathutil::replaceMaskByZero(valuesIn2, maskIn2);
|
---|
1041 | conv.linearConv(valuesOut, valuesIn2);
|
---|
1042 | }
|
---|
1043 | //
|
---|
1044 | valuesIn(start,end) = valuesOut.reform(shapeOut);
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | // Create and put back
|
---|
1048 |
|
---|
1049 | SDContainer sc = in.getSDContainer(ri);
|
---|
1050 | putDataInSDC(sc, valuesIn, maskIn);
|
---|
1051 | //
|
---|
1052 | pTabOut->putSDContainer(sc);
|
---|
1053 | }
|
---|
1054 | //
|
---|
1055 | return pTabOut;
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 |
|
---|
1059 |
|
---|
1060 | SDMemTable* SDMath::convertFlux (const SDMemTable& in, Float a, Float eta, Bool doAll) const
|
---|
1061 | //
|
---|
1062 | // As it is, this function could be implemented with 'simpleOperate'
|
---|
1063 | // However, I anticipate that eventually we will look the conversion
|
---|
1064 | // values up in a Table and apply them in a frequency dependent way,
|
---|
1065 | // so I have implemented it fully here
|
---|
1066 | //
|
---|
1067 | {
|
---|
1068 | SDHeader sh = in.getSDHeader();
|
---|
1069 | SDMemTable* pTabOut = new SDMemTable(in, True);
|
---|
1070 |
|
---|
1071 | // FInd out how to convert values into Jy and K (e.g. units might be mJy or mK)
|
---|
1072 | // Also automatically find out what we are converting to according to the
|
---|
1073 | // flux unit
|
---|
1074 |
|
---|
1075 | Unit fluxUnit(sh.fluxunit);
|
---|
1076 | Unit K(String("K"));
|
---|
1077 | Unit JY(String("Jy"));
|
---|
1078 | //
|
---|
1079 | Bool toKelvin = True;
|
---|
1080 | Double inFac = 1.0;
|
---|
1081 | if (fluxUnit==JY) {
|
---|
1082 | cerr << "Converting to K" << endl;
|
---|
1083 | //
|
---|
1084 | Quantum<Double> t(1.0,fluxUnit);
|
---|
1085 | Quantum<Double> t2 = t.get(JY);
|
---|
1086 | inFac = (t2 / t).getValue();
|
---|
1087 | //
|
---|
1088 | toKelvin = True;
|
---|
1089 | sh.fluxunit = "K";
|
---|
1090 | } else if (fluxUnit==K) {
|
---|
1091 | cerr << "Converting to Jy" << endl;
|
---|
1092 | //
|
---|
1093 | Quantum<Double> t(1.0,fluxUnit);
|
---|
1094 | Quantum<Double> t2 = t.get(K);
|
---|
1095 | inFac = (t2 / t).getValue();
|
---|
1096 | //
|
---|
1097 | toKelvin = False;
|
---|
1098 | sh.fluxunit = "Jy";
|
---|
1099 | } else {
|
---|
1100 | throw(AipsError("Unrecognized brightness units in Table - must be consistent with Jy or K"));
|
---|
1101 | }
|
---|
1102 | pTabOut->putSDHeader(sh);
|
---|
1103 |
|
---|
1104 | // Compute conversion factor. 'a' and 'eta' are really frequency, time and
|
---|
1105 | // telescope dependent and should be looked// up in a table
|
---|
1106 |
|
---|
1107 | Float factor = 2.0 * inFac * 1.0e-7 * 1.0e26 *
|
---|
1108 | QC::k.getValue(Unit(String("erg/K"))) / a / eta;
|
---|
1109 | if (toKelvin) {
|
---|
1110 | factor = 1.0 / factor;
|
---|
1111 | }
|
---|
1112 | cerr << "Applying conversion factor = " << factor << endl;
|
---|
1113 |
|
---|
1114 | // Generate correction vector. Apply same factor regardless
|
---|
1115 | // of beam/pol/IF. This will need to change somewhen.
|
---|
1116 |
|
---|
1117 | Vector<Float> factors(in.nRow(), factor);
|
---|
1118 |
|
---|
1119 | // Correct
|
---|
1120 |
|
---|
1121 | correctFromVector (pTabOut, in, doAll, factors);
|
---|
1122 | //
|
---|
1123 | return pTabOut;
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 |
|
---|
1127 | SDMemTable* SDMath::gainElevation (const SDMemTable& in, const Vector<Float>& coeffs,
|
---|
1128 | const String& fileName,
|
---|
1129 | const String& methodStr, Bool doAll) const
|
---|
1130 | {
|
---|
1131 |
|
---|
1132 | // Get header and clone output table
|
---|
1133 |
|
---|
1134 | SDHeader sh = in.getSDHeader();
|
---|
1135 | SDMemTable* pTabOut = new SDMemTable(in, True);
|
---|
1136 |
|
---|
1137 | // Get elevation data from SDMemTable and convert to degrees
|
---|
1138 |
|
---|
1139 | const Table& tab = in.table();
|
---|
1140 | ROScalarColumn<Float> elev(tab, "ELEVATION");
|
---|
1141 | Vector<Float> x = elev.getColumn();
|
---|
1142 | x *= Float(180 / C::pi);
|
---|
1143 | //
|
---|
1144 | const uInt nC = coeffs.nelements();
|
---|
1145 | if (fileName.length()>0 && nC>0) {
|
---|
1146 | throw(AipsError("You must choose either polynomial coefficients or an ascii file, not both"));
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 | // Correct
|
---|
1150 |
|
---|
1151 | if (nC>0 || fileName.length()==0) {
|
---|
1152 |
|
---|
1153 | // Find instrument
|
---|
1154 |
|
---|
1155 | Bool throwIt = True;
|
---|
1156 | Instrument inst = SDMemTable::convertInstrument (sh.antennaname, throwIt);
|
---|
1157 |
|
---|
1158 | // Set polynomial
|
---|
1159 |
|
---|
1160 | Polynomial<Float>* pPoly = 0;
|
---|
1161 | Vector<Float> coeff;
|
---|
1162 | String msg;
|
---|
1163 | if (nC>0) {
|
---|
1164 | pPoly = new Polynomial<Float>(nC);
|
---|
1165 | coeff = coeffs;
|
---|
1166 | msg = String("user");
|
---|
1167 | } else {
|
---|
1168 | if (inst==ATPKSMB) {
|
---|
1169 | } else if (inst==ATPKSHOH) {
|
---|
1170 | } else if (inst==TIDBINBILLA) {
|
---|
1171 | pPoly = new Polynomial<Float>(3);
|
---|
1172 | coeff.resize(3);
|
---|
1173 | coeff(0) = 3.58788e-1;
|
---|
1174 | coeff(1) = 2.87243e-2;
|
---|
1175 | coeff(2) = -3.219093e-4;
|
---|
1176 | } else if (inst==ATMOPRA) {
|
---|
1177 | } else {
|
---|
1178 | }
|
---|
1179 | msg = String("built in");
|
---|
1180 | }
|
---|
1181 | //
|
---|
1182 | if (coeff.nelements()>0) {
|
---|
1183 | pPoly->setCoefficients(coeff);
|
---|
1184 | } else {
|
---|
1185 | throw(AipsError("There is no known gain-el polynomial known for this instrument"));
|
---|
1186 | }
|
---|
1187 | //
|
---|
1188 | cerr << "Making polynomial correction with " << msg << " coefficients" << endl;
|
---|
1189 | const uInt nRow = in.nRow();
|
---|
1190 | Vector<Float> factor(nRow);
|
---|
1191 | for (uInt i=0; i<nRow; i++) {
|
---|
1192 | factor[i] = (*pPoly)(x[i]);
|
---|
1193 | }
|
---|
1194 | delete pPoly;
|
---|
1195 | //
|
---|
1196 | correctFromVector (pTabOut, in, doAll, factor);
|
---|
1197 | } else {
|
---|
1198 |
|
---|
1199 | // Indicate which columns to read from ascii file
|
---|
1200 |
|
---|
1201 | String col0("ELEVATION");
|
---|
1202 | String col1("FACTOR");
|
---|
1203 |
|
---|
1204 | // Read and correct
|
---|
1205 |
|
---|
1206 | cerr << "Making correction from ascii Table" << endl;
|
---|
1207 | correctFromAsciiTable (pTabOut, in, fileName, col0, col1,
|
---|
1208 | methodStr, doAll, x);
|
---|
1209 | }
|
---|
1210 | //
|
---|
1211 | return pTabOut;
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 |
|
---|
1215 |
|
---|
1216 | SDMemTable* SDMath::opacity (const SDMemTable& in, Float tau, Bool doAll) const
|
---|
1217 | {
|
---|
1218 |
|
---|
1219 | // Get header and clone output table
|
---|
1220 |
|
---|
1221 | SDHeader sh = in.getSDHeader();
|
---|
1222 | SDMemTable* pTabOut = new SDMemTable(in, True);
|
---|
1223 |
|
---|
1224 | // Get elevation data from SDMemTable and convert to degrees
|
---|
1225 |
|
---|
1226 | const Table& tab = in.table();
|
---|
1227 | ROScalarColumn<Float> elev(tab, "ELEVATION");
|
---|
1228 | Vector<Float> zDist = elev.getColumn();
|
---|
1229 | zDist = Float(C::pi_2) - zDist;
|
---|
1230 |
|
---|
1231 | // Generate correction factor
|
---|
1232 |
|
---|
1233 | const uInt nRow = in.nRow();
|
---|
1234 | Vector<Float> factor(nRow);
|
---|
1235 | Vector<Float> factor2(nRow);
|
---|
1236 | for (uInt i=0; i<nRow; i++) {
|
---|
1237 | factor[i] = exp(tau)/cos(zDist[i]);
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | // Correct
|
---|
1241 |
|
---|
1242 | correctFromVector (pTabOut, in, doAll, factor);
|
---|
1243 | //
|
---|
1244 | return pTabOut;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 |
|
---|
1248 |
|
---|
1249 |
|
---|
1250 | // 'private' functions
|
---|
1251 |
|
---|
1252 |
|
---|
1253 | SDMemTable* SDMath::frequencyAlign (const SDMemTable& in,
|
---|
1254 | MFrequency::Types freqSystem,
|
---|
1255 | const String& refTime, const String& methodStr) const
|
---|
1256 | {
|
---|
1257 | // Get Header
|
---|
1258 |
|
---|
1259 | SDHeader sh = in.getSDHeader();
|
---|
1260 | const uInt nChan = sh.nchan;
|
---|
1261 | const uInt nRows = in.nRow();
|
---|
1262 |
|
---|
1263 | // Get Table reference
|
---|
1264 |
|
---|
1265 | const Table& tabIn = in.table();
|
---|
1266 |
|
---|
1267 | // Get Columns from Table
|
---|
1268 |
|
---|
1269 | ROScalarColumn<Double> mjdCol(tabIn, "TIME");
|
---|
1270 | ROScalarColumn<String> srcCol(tabIn, "SRCNAME");
|
---|
1271 | ROArrayColumn<uInt> fqIDCol(tabIn, "FREQID");
|
---|
1272 | //
|
---|
1273 | Vector<Double> times = mjdCol.getColumn();
|
---|
1274 | Vector<String> srcNames = srcCol.getColumn();
|
---|
1275 | Vector<uInt> freqID;
|
---|
1276 |
|
---|
1277 | // Generate Source table
|
---|
1278 |
|
---|
1279 | Vector<String> srcTab;
|
---|
1280 | Vector<uInt> srcIdx, firstRow;
|
---|
1281 | generateSourceTable (srcTab, srcIdx, firstRow, srcNames);
|
---|
1282 | const uInt nSrcTab = srcTab.nelements();
|
---|
1283 | cerr << "Found " << srcTab.nelements() << " sources to align " << endl;
|
---|
1284 |
|
---|
1285 | // Get reference Epoch to time of first row or given String
|
---|
1286 |
|
---|
1287 | Unit DAY(String("d"));
|
---|
1288 | MEpoch::Ref epochRef(in.getTimeReference());
|
---|
1289 | MEpoch refEpoch;
|
---|
1290 | if (refTime.length()>0) {
|
---|
1291 | refEpoch = epochFromString(refTime, in.getTimeReference());
|
---|
1292 | } else {
|
---|
1293 | refEpoch = in.getEpoch(0);
|
---|
1294 | }
|
---|
1295 | cerr << "Aligning at reference Epoch " << formatEpoch(refEpoch) <<
|
---|
1296 | " in frame " << MFrequency::showType(freqSystem) << endl;
|
---|
1297 |
|
---|
1298 | // Get Reference Position
|
---|
1299 |
|
---|
1300 | MPosition refPos = in.getAntennaPosition();
|
---|
1301 |
|
---|
1302 | // Get Frequency Table
|
---|
1303 |
|
---|
1304 | SDFrequencyTable fTab = in.getSDFreqTable();
|
---|
1305 | const uInt nFreqIDs = fTab.length();
|
---|
1306 |
|
---|
1307 | // Create FrequencyAligner Block. One VA for each possible
|
---|
1308 | // source/freqID combination
|
---|
1309 |
|
---|
1310 | PtrBlock<FrequencyAligner<Float>* > a(nFreqIDs*nSrcTab);
|
---|
1311 | generateFrequencyAligners (a, in, nChan, nFreqIDs, nSrcTab, firstRow,
|
---|
1312 | freqSystem, refPos, refEpoch);
|
---|
1313 |
|
---|
1314 | // Interpolation method
|
---|
1315 |
|
---|
1316 | InterpolateArray1D<Double,Float>::InterpolationMethod interp;
|
---|
1317 | convertInterpString(interp, methodStr);
|
---|
1318 |
|
---|
1319 | // New output Table
|
---|
1320 |
|
---|
1321 | SDMemTable* pTabOut = new SDMemTable(in,True);
|
---|
1322 |
|
---|
1323 | // Loop over rows in Table
|
---|
1324 |
|
---|
1325 | const IPosition polChanAxes(2, asap::PolAxis, asap::ChanAxis);
|
---|
1326 |
|
---|
1327 | Bool extrapolate=False;
|
---|
1328 | Bool useCachedAbcissa = False;
|
---|
1329 | Bool first = True;
|
---|
1330 | Bool ok;
|
---|
1331 | Vector<Float> yOut;
|
---|
1332 | Vector<Bool> maskOut;
|
---|
1333 | uInt ifIdx, faIdx;
|
---|
1334 | //
|
---|
1335 | for (uInt iRow=0; iRow<nRows; ++iRow) {
|
---|
1336 | if (iRow%10==0) {
|
---|
1337 | cerr << "Processing row " << iRow << endl;
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | // Get EPoch
|
---|
1341 |
|
---|
1342 | Quantum<Double> tQ2(times[iRow],DAY);
|
---|
1343 | MVEpoch mv2(tQ2);
|
---|
1344 | MEpoch epoch(mv2, epochRef);
|
---|
1345 |
|
---|
1346 | // Get FreqID vector. One freqID per IF
|
---|
1347 |
|
---|
1348 | fqIDCol.get(iRow, freqID);
|
---|
1349 |
|
---|
1350 | // Get copy of data
|
---|
1351 |
|
---|
1352 | const MaskedArray<Float>& mArrIn(in.rowAsMaskedArray(iRow));
|
---|
1353 | Array<Float> values = mArrIn.getArray();
|
---|
1354 | Array<Bool> mask = mArrIn.getMask();
|
---|
1355 |
|
---|
1356 | // cerr << "values in = " << values(IPosition(4,0,0,0,0),IPosition(4,0,0,0,9)) << endl;
|
---|
1357 |
|
---|
1358 | // For each row, the Frequency abcissa will be the same regardless
|
---|
1359 | // of polarization. For all other axes (IF and BEAM) the abcissa
|
---|
1360 | // will change. So we iterate through the data by pol-chan planes
|
---|
1361 | // to mimimize the work. At this point, I think the Direction
|
---|
1362 | // is stored as the same for each beam. DOn't know where the
|
---|
1363 | // offsets are or what to do about them right now. For now
|
---|
1364 | // all beams get same position and velocoity abcissa.
|
---|
1365 |
|
---|
1366 | ArrayIterator<Float> itValuesPlane(values, polChanAxes);
|
---|
1367 | ArrayIterator<Bool> itMaskPlane(mask, polChanAxes);
|
---|
1368 | while (!itValuesPlane.pastEnd()) {
|
---|
1369 |
|
---|
1370 | // Find the IF index and then the FA PtrBlock index
|
---|
1371 |
|
---|
1372 | const IPosition& pos = itValuesPlane.pos();
|
---|
1373 | ifIdx = pos(asap::IFAxis);
|
---|
1374 | faIdx = (srcIdx[iRow]*nFreqIDs) + freqID[ifIdx];
|
---|
1375 | //
|
---|
1376 | VectorIterator<Float> itValuesVec(itValuesPlane.array(), 1);
|
---|
1377 | VectorIterator<Bool> itMaskVec(itMaskPlane.array(), 1);
|
---|
1378 | //
|
---|
1379 | first = True;
|
---|
1380 | useCachedAbcissa=False;
|
---|
1381 | while (!itValuesVec.pastEnd()) {
|
---|
1382 | ok = a[faIdx]->align (yOut, maskOut, itValuesVec.vector(),
|
---|
1383 | itMaskVec.vector(), epoch, useCachedAbcissa,
|
---|
1384 | interp, extrapolate);
|
---|
1385 | itValuesVec.vector() = yOut;
|
---|
1386 | itMaskVec.vector() = maskOut;
|
---|
1387 | //
|
---|
1388 | itValuesVec.next();
|
---|
1389 | itMaskVec.next();
|
---|
1390 | //
|
---|
1391 | if (first) {
|
---|
1392 | useCachedAbcissa = True;
|
---|
1393 | first = False;
|
---|
1394 | }
|
---|
1395 | }
|
---|
1396 | //
|
---|
1397 | itValuesPlane.next();
|
---|
1398 | itMaskPlane.next();
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | // cerr << "values out = " << values(IPosition(4,0,0,0,0),IPosition(4,0,0,0,9)) << endl;
|
---|
1402 |
|
---|
1403 | // Create and put back
|
---|
1404 |
|
---|
1405 | SDContainer sc = in.getSDContainer(iRow);
|
---|
1406 | putDataInSDC(sc, values, mask);
|
---|
1407 | //
|
---|
1408 | pTabOut->putSDContainer(sc);
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 |
|
---|
1412 | // Write out correct frequency table information for output.
|
---|
1413 | // CLone from input and overwrite
|
---|
1414 |
|
---|
1415 | SDFrequencyTable freqTab = in.getSDFreqTable();
|
---|
1416 | freqTab.setLength(0);
|
---|
1417 |
|
---|
1418 | // Note that we don't have anyway to hold frequency table information
|
---|
1419 | // in the SDMemTable which is source dependent. Each FrequencyAligner
|
---|
1420 | // is generated for an freqID/Source combination. Don't know what to
|
---|
1421 | // do about this apart from to pick the aligned SC from the first source
|
---|
1422 | // at the moment. ASAP really needs to handle data description id
|
---|
1423 | // like in aips++ which is a combination of source and freqid. Otherwise
|
---|
1424 | // all else I can do in this function is disallow multiple sources
|
---|
1425 |
|
---|
1426 | Bool linear=True;
|
---|
1427 | Vector<String> units(1);
|
---|
1428 | units = String("Hz");
|
---|
1429 | for (uInt fqID=0; fqID<nFreqIDs; fqID++) {
|
---|
1430 | for (uInt iSrc=0; iSrc<nSrcTab; iSrc++) {
|
---|
1431 | uInt idx = (iSrc*nFreqIDs) + fqID;
|
---|
1432 |
|
---|
1433 | // Get Aligned SC in Hz
|
---|
1434 |
|
---|
1435 | if (iSrc==0) {
|
---|
1436 | SpectralCoordinate sC = a[idx]->alignedSpectralCoordinate(linear);
|
---|
1437 | sC.setWorldAxisUnits(units);
|
---|
1438 |
|
---|
1439 | // Write out frequency table information to SDMemTable
|
---|
1440 |
|
---|
1441 | Int idx = freqTab.addFrequency(sC.referencePixel()[0],
|
---|
1442 | sC.referenceValue()[0],
|
---|
1443 | sC.increment()[0]);
|
---|
1444 | }
|
---|
1445 | }
|
---|
1446 | }
|
---|
1447 | pTabOut->putSDFreqTable(freqTab);
|
---|
1448 |
|
---|
1449 | // Now we must set the base and extra frames to the
|
---|
1450 | // input frame
|
---|
1451 |
|
---|
1452 | std::vector<string> info = pTabOut->getCoordInfo();
|
---|
1453 | info[1] = MFrequency::showType(freqSystem); // Conversion frame
|
---|
1454 | info[3] = info[1]; // Base frame
|
---|
1455 | pTabOut->setCoordInfo(info);
|
---|
1456 |
|
---|
1457 | // Clean up PointerBlock
|
---|
1458 |
|
---|
1459 | for (uInt i=0; i<a.nelements(); i++) delete a[i];
|
---|
1460 | //
|
---|
1461 | return pTabOut;
|
---|
1462 | }
|
---|
1463 |
|
---|
1464 |
|
---|
1465 | void SDMath::fillSDC(SDContainer& sc,
|
---|
1466 | const Array<Bool>& mask,
|
---|
1467 | const Array<Float>& data,
|
---|
1468 | const Array<Float>& tSys,
|
---|
1469 | Int scanID, Double timeStamp,
|
---|
1470 | Double interval, const String& sourceName,
|
---|
1471 | const Vector<uInt>& freqID) const
|
---|
1472 | {
|
---|
1473 | // Data and mask
|
---|
1474 |
|
---|
1475 | putDataInSDC(sc, data, mask);
|
---|
1476 |
|
---|
1477 | // TSys
|
---|
1478 |
|
---|
1479 | sc.putTsys(tSys);
|
---|
1480 |
|
---|
1481 | // Time things
|
---|
1482 |
|
---|
1483 | sc.timestamp = timeStamp;
|
---|
1484 | sc.interval = interval;
|
---|
1485 | sc.scanid = scanID;
|
---|
1486 | //
|
---|
1487 | sc.sourcename = sourceName;
|
---|
1488 | sc.putFreqMap(freqID);
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | void SDMath::normalize(MaskedArray<Float>& sum,
|
---|
1492 | const Array<Float>& sumSq,
|
---|
1493 | const Array<Float>& nPts,
|
---|
1494 | WeightType wtType, Int axis,
|
---|
1495 | Int nAxesSub) const
|
---|
1496 | {
|
---|
1497 | IPosition pos2(nAxesSub,0);
|
---|
1498 | //
|
---|
1499 | if (wtType==NONE) {
|
---|
1500 |
|
---|
1501 | // We just average by the number of points accumulated.
|
---|
1502 | // We need to make a MA out of nPts so that no divide by
|
---|
1503 | // zeros occur
|
---|
1504 |
|
---|
1505 | MaskedArray<Float> t(nPts, (nPts>Float(0.0)));
|
---|
1506 | sum /= t;
|
---|
1507 | } else if (wtType==VAR) {
|
---|
1508 |
|
---|
1509 | // Normalize each spectrum by sum(1/var) where the variance
|
---|
1510 | // is worked out for each spectrum
|
---|
1511 |
|
---|
1512 | Array<Float>& data = sum.getRWArray();
|
---|
1513 | VectorIterator<Float> itData(data, axis);
|
---|
1514 | while (!itData.pastEnd()) {
|
---|
1515 | pos2 = itData.pos().getFirst(nAxesSub);
|
---|
1516 | itData.vector() /= sumSq(pos2);
|
---|
1517 | itData.next();
|
---|
1518 | }
|
---|
1519 | } else if (wtType==TSYS) {
|
---|
1520 | }
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 |
|
---|
1524 | void SDMath::accumulate(Double& timeSum, Double& intSum, Int& nAccum,
|
---|
1525 | MaskedArray<Float>& sum, Array<Float>& sumSq,
|
---|
1526 | Array<Float>& nPts, Array<Float>& tSysSum,
|
---|
1527 | const Array<Float>& tSys, const Array<Float>& nInc,
|
---|
1528 | const Vector<Bool>& mask, Double time, Double interval,
|
---|
1529 | const Block<CountedPtr<SDMemTable> >& in,
|
---|
1530 | uInt iTab, uInt iRow, uInt axis,
|
---|
1531 | uInt nAxesSub, Bool useMask,
|
---|
1532 | WeightType wtType) const
|
---|
1533 | {
|
---|
1534 |
|
---|
1535 | // Get data
|
---|
1536 |
|
---|
1537 | MaskedArray<Float> dataIn(in[iTab]->rowAsMaskedArray(iRow));
|
---|
1538 | Array<Float>& valuesIn = dataIn.getRWArray(); // writable reference
|
---|
1539 | const Array<Bool>& maskIn = dataIn.getMask(); // RO reference
|
---|
1540 | //
|
---|
1541 | if (wtType==NONE) {
|
---|
1542 | const MaskedArray<Float> n(nInc,dataIn.getMask());
|
---|
1543 | nPts += n; // Only accumulates where mask==T
|
---|
1544 | } else if (wtType==VAR) {
|
---|
1545 |
|
---|
1546 | // We are going to average the data, weighted by the noise for each pol, beam and IF.
|
---|
1547 | // So therefore we need to iterate through by spectrum (axis 3)
|
---|
1548 |
|
---|
1549 | VectorIterator<Float> itData(valuesIn, axis);
|
---|
1550 | ReadOnlyVectorIterator<Bool> itMask(maskIn, axis);
|
---|
1551 | Float fac = 1.0;
|
---|
1552 | IPosition pos(nAxesSub,0);
|
---|
1553 | //
|
---|
1554 | while (!itData.pastEnd()) {
|
---|
1555 |
|
---|
1556 | // Make MaskedArray of Vector, optionally apply OTF mask, and find scaling factor
|
---|
1557 |
|
---|
1558 | if (useMask) {
|
---|
1559 | MaskedArray<Float> tmp(itData.vector(),mask&&itMask.vector());
|
---|
1560 | fac = 1.0/variance(tmp);
|
---|
1561 | } else {
|
---|
1562 | MaskedArray<Float> tmp(itData.vector(),itMask.vector());
|
---|
1563 | fac = 1.0/variance(tmp);
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | // Scale data
|
---|
1567 |
|
---|
1568 | itData.vector() *= fac; // Writes back into 'dataIn'
|
---|
1569 | //
|
---|
1570 | // Accumulate variance per if/pol/beam averaged over spectrum
|
---|
1571 | // This method to get pos2 from itData.pos() is only valid
|
---|
1572 | // because the spectral axis is the last one (so we can just
|
---|
1573 | // copy the first nAXesSub positions out)
|
---|
1574 |
|
---|
1575 | pos = itData.pos().getFirst(nAxesSub);
|
---|
1576 | sumSq(pos) += fac;
|
---|
1577 | //
|
---|
1578 | itData.next();
|
---|
1579 | itMask.next();
|
---|
1580 | }
|
---|
1581 | } else if (wtType==TSYS) {
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | // Accumulate sum of (possibly scaled) data
|
---|
1585 |
|
---|
1586 | sum += dataIn;
|
---|
1587 |
|
---|
1588 | // Accumulate Tsys, time, and interval
|
---|
1589 |
|
---|
1590 | tSysSum += tSys;
|
---|
1591 | timeSum += time;
|
---|
1592 | intSum += interval;
|
---|
1593 | nAccum += 1;
|
---|
1594 | }
|
---|
1595 |
|
---|
1596 |
|
---|
1597 |
|
---|
1598 |
|
---|
1599 | void SDMath::getCursorLocation(IPosition& start, IPosition& end,
|
---|
1600 | const SDMemTable& in) const
|
---|
1601 | {
|
---|
1602 | const uInt nDim = 4;
|
---|
1603 | const uInt i = in.getBeam();
|
---|
1604 | const uInt j = in.getIF();
|
---|
1605 | const uInt k = in.getPol();
|
---|
1606 | const uInt n = in.nChan();
|
---|
1607 | //
|
---|
1608 | start.resize(nDim);
|
---|
1609 | start(0) = i;
|
---|
1610 | start(1) = j;
|
---|
1611 | start(2) = k;
|
---|
1612 | start(3) = 0;
|
---|
1613 | //
|
---|
1614 | end.resize(nDim);
|
---|
1615 | end(0) = i;
|
---|
1616 | end(1) = j;
|
---|
1617 | end(2) = k;
|
---|
1618 | end(3) = n-1;
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 |
|
---|
1622 | void SDMath::convertWeightString(WeightType& wtType, const String& weightStr) const
|
---|
1623 | {
|
---|
1624 | String tStr(weightStr);
|
---|
1625 | tStr.upcase();
|
---|
1626 | if (tStr.contains(String("NONE"))) {
|
---|
1627 | wtType = NONE;
|
---|
1628 | } else if (tStr.contains(String("VAR"))) {
|
---|
1629 | wtType = VAR;
|
---|
1630 | } else if (tStr.contains(String("TSYS"))) {
|
---|
1631 | wtType = TSYS;
|
---|
1632 | throw(AipsError("T_sys weighting not yet implemented"));
|
---|
1633 | } else {
|
---|
1634 | throw(AipsError("Unrecognized weighting type"));
|
---|
1635 | }
|
---|
1636 | }
|
---|
1637 |
|
---|
1638 |
|
---|
1639 | void SDMath::convertInterpString(casa::InterpolateArray1D<Double,Float>::InterpolationMethod& type,
|
---|
1640 | const casa::String& interp) const
|
---|
1641 | {
|
---|
1642 | String tStr(interp);
|
---|
1643 | tStr.upcase();
|
---|
1644 | if (tStr.contains(String("NEAR"))) {
|
---|
1645 | type = InterpolateArray1D<Double,Float>::nearestNeighbour;
|
---|
1646 | } else if (tStr.contains(String("LIN"))) {
|
---|
1647 | type = InterpolateArray1D<Double,Float>::linear;
|
---|
1648 | } else if (tStr.contains(String("CUB"))) {
|
---|
1649 | type = InterpolateArray1D<Double,Float>::cubic;
|
---|
1650 | } else if (tStr.contains(String("SPL"))) {
|
---|
1651 | type = InterpolateArray1D<Double,Float>::spline;
|
---|
1652 | } else {
|
---|
1653 | throw(AipsError("Unrecognized interpolation type"));
|
---|
1654 | }
|
---|
1655 | }
|
---|
1656 |
|
---|
1657 | void SDMath::putDataInSDC(SDContainer& sc, const Array<Float>& data,
|
---|
1658 | const Array<Bool>& mask) const
|
---|
1659 | {
|
---|
1660 | sc.putSpectrum(data);
|
---|
1661 | //
|
---|
1662 | Array<uChar> outflags(data.shape());
|
---|
1663 | convertArray(outflags,!mask);
|
---|
1664 | sc.putFlags(outflags);
|
---|
1665 | }
|
---|
1666 |
|
---|
1667 | Table SDMath::readAsciiFile (const String& fileName) const
|
---|
1668 | {
|
---|
1669 | String formatString;
|
---|
1670 | Table tbl = readAsciiTable (formatString, Table::Memory, fileName, "", "", False);
|
---|
1671 | return tbl;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 |
|
---|
1675 |
|
---|
1676 | void SDMath::correctFromAsciiTable(SDMemTable* pTabOut,
|
---|
1677 | const SDMemTable& in, const String& fileName,
|
---|
1678 | const String& col0, const String& col1,
|
---|
1679 | const String& methodStr, Bool doAll,
|
---|
1680 | const Vector<Float>& xOut) const
|
---|
1681 | {
|
---|
1682 |
|
---|
1683 | // Read gain-elevation ascii file data into a Table.
|
---|
1684 |
|
---|
1685 | Table geTable = readAsciiFile (fileName);
|
---|
1686 | //
|
---|
1687 | correctFromTable (pTabOut, in, geTable, col0, col1, methodStr, doAll, xOut);
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | void SDMath::correctFromTable(SDMemTable* pTabOut, const SDMemTable& in,
|
---|
1691 | const Table& tTable, const String& col0,
|
---|
1692 | const String& col1,
|
---|
1693 | const String& methodStr, Bool doAll,
|
---|
1694 | const Vector<Float>& xOut) const
|
---|
1695 | {
|
---|
1696 |
|
---|
1697 | // Get data from Table
|
---|
1698 |
|
---|
1699 | ROScalarColumn<Float> geElCol(tTable, col0);
|
---|
1700 | ROScalarColumn<Float> geFacCol(tTable, col1);
|
---|
1701 | Vector<Float> xIn = geElCol.getColumn();
|
---|
1702 | Vector<Float> yIn = geFacCol.getColumn();
|
---|
1703 | Vector<Bool> maskIn(xIn.nelements(),True);
|
---|
1704 |
|
---|
1705 | // Interpolate (and extrapolate) with desired method
|
---|
1706 |
|
---|
1707 | InterpolateArray1D<Double,Float>::InterpolationMethod method;
|
---|
1708 | convertInterpString(method, methodStr);
|
---|
1709 | Int intMethod(method);
|
---|
1710 | //
|
---|
1711 | Vector<Float> yOut;
|
---|
1712 | Vector<Bool> maskOut;
|
---|
1713 | InterpolateArray1D<Float,Float>::interpolate(yOut, maskOut, xOut,
|
---|
1714 | xIn, yIn, maskIn, intMethod,
|
---|
1715 | True, True);
|
---|
1716 | // Apply
|
---|
1717 |
|
---|
1718 | correctFromVector (pTabOut, in, doAll, yOut);
|
---|
1719 | }
|
---|
1720 |
|
---|
1721 |
|
---|
1722 | void SDMath::correctFromVector (SDMemTable* pTabOut, const SDMemTable& in,
|
---|
1723 | Bool doAll, const Vector<Float>& factor) const
|
---|
1724 | {
|
---|
1725 |
|
---|
1726 | // For operations only on specified cursor location
|
---|
1727 |
|
---|
1728 | IPosition start, end;
|
---|
1729 | getCursorLocation(start, end, in);
|
---|
1730 |
|
---|
1731 | // Loop over rows and apply correction factor
|
---|
1732 |
|
---|
1733 | const uInt axis = asap::ChanAxis;
|
---|
1734 | for (uInt i=0; i < in.nRow(); ++i) {
|
---|
1735 |
|
---|
1736 | // Get data
|
---|
1737 |
|
---|
1738 | MaskedArray<Float> dataIn(in.rowAsMaskedArray(i));
|
---|
1739 |
|
---|
1740 | // Apply factor
|
---|
1741 |
|
---|
1742 | if (doAll) {
|
---|
1743 | dataIn *= factor[i];
|
---|
1744 | } else {
|
---|
1745 | MaskedArray<Float> dataIn2 = dataIn(start,end); // reference
|
---|
1746 | dataIn2 *= factor[i];
|
---|
1747 | }
|
---|
1748 |
|
---|
1749 | // Write out
|
---|
1750 |
|
---|
1751 | SDContainer sc = in.getSDContainer(i);
|
---|
1752 | putDataInSDC(sc, dataIn.getArray(), dataIn.getMask());
|
---|
1753 | //
|
---|
1754 | pTabOut->putSDContainer(sc);
|
---|
1755 | }
|
---|
1756 | }
|
---|
1757 |
|
---|
1758 |
|
---|
1759 | void SDMath::generateSourceTable (Vector<String>& srcTab,
|
---|
1760 | Vector<uInt>& srcIdx,
|
---|
1761 | Vector<uInt>& firstRow,
|
---|
1762 | const Vector<String>& srcNames) const
|
---|
1763 | //
|
---|
1764 | // This algorithm assumes that if there are multiple beams
|
---|
1765 | // that the source names are diffent. Oterwise we would need
|
---|
1766 | // to look atthe direction for each beam...
|
---|
1767 | //
|
---|
1768 | {
|
---|
1769 | const uInt nRow = srcNames.nelements();
|
---|
1770 | srcTab.resize(0);
|
---|
1771 | srcIdx.resize(nRow);
|
---|
1772 | firstRow.resize(0);
|
---|
1773 | //
|
---|
1774 | uInt nSrc = 0;
|
---|
1775 | for (uInt i=0; i<nRow; i++) {
|
---|
1776 | String srcName = srcNames[i];
|
---|
1777 |
|
---|
1778 | // Do we have this source already ?
|
---|
1779 |
|
---|
1780 | Int idx = -1;
|
---|
1781 | if (nSrc>0) {
|
---|
1782 | for (uInt j=0; j<nSrc; j++) {
|
---|
1783 | if (srcName==srcTab[j]) {
|
---|
1784 | idx = j;
|
---|
1785 | break;
|
---|
1786 | }
|
---|
1787 | }
|
---|
1788 | }
|
---|
1789 |
|
---|
1790 | // Add new entry if not found
|
---|
1791 |
|
---|
1792 | if (idx==-1) {
|
---|
1793 | nSrc++;
|
---|
1794 | srcTab.resize(nSrc,True);
|
---|
1795 | srcTab(nSrc-1) = srcName;
|
---|
1796 | idx = nSrc-1;
|
---|
1797 | //
|
---|
1798 | firstRow.resize(nSrc,True);
|
---|
1799 | firstRow(nSrc-1) = i; // First row for which this source occurs
|
---|
1800 | }
|
---|
1801 |
|
---|
1802 | // Set index for this row
|
---|
1803 |
|
---|
1804 | srcIdx[i] = idx;
|
---|
1805 | }
|
---|
1806 | }
|
---|
1807 |
|
---|
1808 | MEpoch SDMath::epochFromString (const String& str, MEpoch::Types timeRef) const
|
---|
1809 | {
|
---|
1810 | Quantum<Double> qt;
|
---|
1811 | if (MVTime::read(qt,str)) {
|
---|
1812 | MVEpoch mv(qt);
|
---|
1813 | MEpoch me(mv, timeRef);
|
---|
1814 | return me;
|
---|
1815 | } else {
|
---|
1816 | throw(AipsError("Invalid format for Epoch string"));
|
---|
1817 | }
|
---|
1818 | }
|
---|
1819 |
|
---|
1820 |
|
---|
1821 | String SDMath::formatEpoch(const MEpoch& epoch) const
|
---|
1822 | {
|
---|
1823 | MVTime mvt(epoch.getValue());
|
---|
1824 | return mvt.string(MVTime::YMD) + String(" (") + epoch.getRefString() + String(")");
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 |
|
---|
1828 |
|
---|
1829 | void SDMath::generateFrequencyAligners (PtrBlock<FrequencyAligner<Float>* >& a,
|
---|
1830 | const SDMemTable& in, uInt nChan,
|
---|
1831 | uInt nFreqIDs, uInt nSrcTab,
|
---|
1832 | const Vector<uInt>& firstRow,
|
---|
1833 | MFrequency::Types system,
|
---|
1834 | const MPosition& refPos,
|
---|
1835 | const MEpoch& refEpoch) const
|
---|
1836 | {
|
---|
1837 | for (uInt fqID=0; fqID<nFreqIDs; fqID++) {
|
---|
1838 | SpectralCoordinate sC = in.getSpectralCoordinate(fqID);
|
---|
1839 | for (uInt iSrc=0; iSrc<nSrcTab; iSrc++) {
|
---|
1840 | MDirection refDir = in.getDirection(firstRow[iSrc]);
|
---|
1841 | uInt idx = (iSrc*nFreqIDs) + fqID;
|
---|
1842 | a[idx] = new FrequencyAligner<Float>(sC, nChan, refEpoch, refDir, refPos, system);
|
---|
1843 | }
|
---|
1844 | }
|
---|
1845 | }
|
---|
1846 |
|
---|
1847 |
|
---|