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