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