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