source: trunk/src/SDMath.cc@ 474

Last change on this file since 474 was 457, checked in by kil064, 20 years ago

move SDMemTable::rotateXYPhase to SDMath

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