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