[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) ||
|
---|
| 376 | (nRowOn>0&&nRowOn==nRowOff);
|
---|
| 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();
|
---|
| 404 | //
|
---|
| 405 | if (nRowOff>1) {
|
---|
| 406 | delete pMOff;
|
---|
| 407 | pMOff = new MaskedArray<Float>(off->rowAsMaskedArray(i));
|
---|
| 408 | shpOff = pMOff->shape();
|
---|
| 409 | if (!shpOn.isEqual(shpOff)) {
|
---|
| 410 | throw(AipsError("on/off data are not conformant"));
|
---|
| 411 | }
|
---|
| 412 | //
|
---|
| 413 | tSysOff.get(i, tSysOffArr);
|
---|
| 414 | tSysOn.get(i, tSysOnArr);
|
---|
| 415 | if (!tSysOnArr.shape().isEqual(tSysOffArr.shape())) {
|
---|
| 416 | throw(AipsError("on/off Tsys data are not conformant"));
|
---|
| 417 | }
|
---|
| 418 | //
|
---|
| 419 | if (!shpOn.isEqual(tSysOnArr.shape())) {
|
---|
| 420 | throw(AipsError("Correlation and Tsys data are not conformant"));
|
---|
| 421 | }
|
---|
[130] | 422 | }
|
---|
| 423 |
|
---|
| 424 | // Compute quotient
|
---|
| 425 |
|
---|
[234] | 426 | MaskedArray<Float> tmp = (mOn-*pMOff);
|
---|
[130] | 427 | Array<Float> out(tmp.getArray());
|
---|
[234] | 428 | out /= *pMOff;
|
---|
| 429 | out *= tSysOffArr;
|
---|
[130] | 430 |
|
---|
[234] | 431 | // MaskedArray<Float> tmp2 = (tSysOnArr * mOn / *pMOff) - tSysOffArr;
|
---|
| 432 |
|
---|
| 433 |
|
---|
[130] | 434 | // Fill container for this row
|
---|
| 435 |
|
---|
[184] | 436 | SDContainer sc = on->getSDContainer(i);
|
---|
[163] | 437 | //
|
---|
[234] | 438 | putDataInSDC(sc, out, tmp.getMask());
|
---|
| 439 | sc.putTsys(tSysOffArr);
|
---|
[184] | 440 | sc.scanid = i;
|
---|
[130] | 441 |
|
---|
| 442 | // Put new row in output Table
|
---|
[234] | 443 |
|
---|
| 444 | pTabOut->putSDContainer(sc);
|
---|
| 445 | }
|
---|
| 446 | if (pMOff) delete pMOff;
|
---|
| 447 | //
|
---|
| 448 | return CountedPtr<SDMemTable>(pTabOut);
|
---|
| 449 | }
|
---|
[130] | 450 |
|
---|
[234] | 451 |
|
---|
| 452 | CountedPtr<SDMemTable> SDMath::simpleBinaryOperate (const CountedPtr<SDMemTable>& left,
|
---|
| 453 | const CountedPtr<SDMemTable>& right,
|
---|
| 454 | const String& op) const
|
---|
| 455 | //
|
---|
| 456 | // Simple binary Table operators. add, subtract, multiply, divide (what=0,1,2,3)
|
---|
| 457 | //
|
---|
| 458 | {
|
---|
| 459 |
|
---|
| 460 | // CHeck operator
|
---|
| 461 |
|
---|
| 462 | String op2(op);
|
---|
| 463 | op2.upcase();
|
---|
| 464 | uInt what = 0;
|
---|
| 465 | if (op2=="ADD") {
|
---|
| 466 | what = 0;
|
---|
| 467 | } else if (op2=="SUB") {
|
---|
| 468 | what = 1;
|
---|
| 469 | } else if (op2=="MUL") {
|
---|
| 470 | what = 2;
|
---|
| 471 | } else if (op2=="DIV") {
|
---|
| 472 | what = 3;
|
---|
| 473 | } else {
|
---|
| 474 | throw AipsError("Unrecognized operation");
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | // Check rows
|
---|
| 478 |
|
---|
| 479 | const uInt nRows = left->nRow();
|
---|
| 480 | if (right->nRow() != nRows) {
|
---|
| 481 | throw (AipsError("Input Scan Tables must have the same number of rows"));
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | // Input Tables and columns
|
---|
| 485 |
|
---|
| 486 | const Table& tLeft = left->table();
|
---|
| 487 | const Table& tRight = right->table();
|
---|
| 488 | //
|
---|
| 489 | ROArrayColumn<Float> tSysLeft(tLeft, "TSYS");
|
---|
| 490 | ROArrayColumn<Float> tSysRight(tRight, "TSYS");
|
---|
| 491 |
|
---|
| 492 | // Output Table cloned from input
|
---|
| 493 |
|
---|
| 494 | SDMemTable* pTabOut = new SDMemTable(*left, True);
|
---|
| 495 |
|
---|
| 496 | // Loop over rows
|
---|
| 497 |
|
---|
| 498 | for (uInt i=0; i<nRows; i++) {
|
---|
| 499 |
|
---|
| 500 | // Get data
|
---|
| 501 | MaskedArray<Float> mLeft(left->rowAsMaskedArray(i));
|
---|
| 502 | MaskedArray<Float> mRight(right->rowAsMaskedArray(i));
|
---|
| 503 | //
|
---|
| 504 | IPosition shpLeft = mLeft.shape();
|
---|
| 505 | IPosition shpRight = mRight.shape();
|
---|
| 506 | if (!shpLeft.isEqual(shpRight)) {
|
---|
| 507 | throw(AipsError("left/right Scan Tables are not conformant"));
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 | // Get TSys
|
---|
| 511 |
|
---|
| 512 | Array<Float> tSysLeftArr, tSysRightArr;
|
---|
| 513 | tSysLeft.get(i, tSysLeftArr);
|
---|
| 514 | tSysRight.get(i, tSysRightArr);
|
---|
| 515 |
|
---|
| 516 | // Make container
|
---|
| 517 |
|
---|
| 518 | SDContainer sc = left->getSDContainer(i);
|
---|
| 519 |
|
---|
| 520 | // Operate on data and TSys
|
---|
| 521 |
|
---|
| 522 | if (what==0) {
|
---|
| 523 | MaskedArray<Float> tmp = mLeft + mRight;
|
---|
| 524 | putDataInSDC(sc, tmp.getArray(), tmp.getMask());
|
---|
| 525 | sc.putTsys(tSysLeftArr+tSysRightArr);
|
---|
| 526 | } else if (what==1) {
|
---|
| 527 | MaskedArray<Float> tmp = mLeft - mRight;
|
---|
| 528 | putDataInSDC(sc, tmp.getArray(), tmp.getMask());
|
---|
| 529 | sc.putTsys(tSysLeftArr-tSysRightArr);
|
---|
| 530 | } else if (what==2) {
|
---|
| 531 | MaskedArray<Float> tmp = mLeft * mRight;
|
---|
| 532 | putDataInSDC(sc, tmp.getArray(), tmp.getMask());
|
---|
| 533 | sc.putTsys(tSysLeftArr*tSysRightArr);
|
---|
| 534 | } else if (what==3) {
|
---|
| 535 | MaskedArray<Float> tmp = mLeft / mRight;
|
---|
| 536 | putDataInSDC(sc, tmp.getArray(), tmp.getMask());
|
---|
| 537 | sc.putTsys(tSysLeftArr/tSysRightArr);
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | // Put new row in output Table
|
---|
| 541 |
|
---|
[171] | 542 | pTabOut->putSDContainer(sc);
|
---|
[130] | 543 | }
|
---|
| 544 | //
|
---|
[171] | 545 | return CountedPtr<SDMemTable>(pTabOut);
|
---|
[9] | 546 | }
|
---|
[48] | 547 |
|
---|
[146] | 548 |
|
---|
| 549 |
|
---|
[185] | 550 | std::vector<float> SDMath::statistic(const CountedPtr<SDMemTable>& in,
|
---|
[234] | 551 | const Vector<Bool>& mask,
|
---|
| 552 | const String& which, Int row) const
|
---|
[130] | 553 | //
|
---|
| 554 | // Perhaps iteration over pol/beam/if should be in here
|
---|
| 555 | // and inside the nrow iteration ?
|
---|
| 556 | //
|
---|
| 557 | {
|
---|
| 558 | const uInt nRow = in->nRow();
|
---|
| 559 |
|
---|
| 560 | // Specify cursor location
|
---|
| 561 |
|
---|
[152] | 562 | IPosition start, end;
|
---|
[185] | 563 | getCursorLocation(start, end, *in);
|
---|
[130] | 564 |
|
---|
| 565 | // Loop over rows
|
---|
| 566 |
|
---|
[234] | 567 | const uInt nEl = mask.nelements();
|
---|
| 568 | uInt iStart = 0;
|
---|
| 569 | uInt iEnd = in->nRow()-1;
|
---|
| 570 | //
|
---|
| 571 | if (row>=0) {
|
---|
| 572 | iStart = row;
|
---|
| 573 | iEnd = row;
|
---|
| 574 | }
|
---|
| 575 | //
|
---|
| 576 | std::vector<float> result(iEnd-iStart+1);
|
---|
| 577 | for (uInt ii=iStart; ii <= iEnd; ++ii) {
|
---|
[130] | 578 |
|
---|
| 579 | // Get row and deconstruct
|
---|
| 580 |
|
---|
| 581 | MaskedArray<Float> marr(in->rowAsMaskedArray(ii));
|
---|
| 582 | Array<Float> arr = marr.getArray();
|
---|
| 583 | Array<Bool> barr = marr.getMask();
|
---|
| 584 |
|
---|
| 585 | // Access desired piece of data
|
---|
| 586 |
|
---|
| 587 | Array<Float> v((arr(start,end)).nonDegenerate());
|
---|
| 588 | Array<Bool> m((barr(start,end)).nonDegenerate());
|
---|
| 589 |
|
---|
| 590 | // Apply OTF mask
|
---|
| 591 |
|
---|
| 592 | MaskedArray<Float> tmp;
|
---|
| 593 | if (m.nelements()==nEl) {
|
---|
[234] | 594 | tmp.setData(v,m&&mask);
|
---|
[130] | 595 | } else {
|
---|
| 596 | tmp.setData(v,m);
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | // Get statistic
|
---|
| 600 |
|
---|
[234] | 601 | result[ii-iStart] = mathutil::statistics(which, tmp);
|
---|
[130] | 602 | }
|
---|
| 603 | //
|
---|
| 604 | return result;
|
---|
| 605 | }
|
---|
| 606 |
|
---|
[146] | 607 |
|
---|
[234] | 608 | SDMemTable* SDMath::bin(const SDMemTable& in, Int width) const
|
---|
[144] | 609 | {
|
---|
[169] | 610 | SDHeader sh = in.getSDHeader();
|
---|
| 611 | SDMemTable* pTabOut = new SDMemTable(in, True);
|
---|
[163] | 612 |
|
---|
[169] | 613 | // Bin up SpectralCoordinates
|
---|
[163] | 614 |
|
---|
[169] | 615 | IPosition factors(1);
|
---|
| 616 | factors(0) = width;
|
---|
| 617 | for (uInt j=0; j<in.nCoordinates(); ++j) {
|
---|
| 618 | CoordinateSystem cSys;
|
---|
| 619 | cSys.addCoordinate(in.getCoordinate(j));
|
---|
| 620 | CoordinateSystem cSysBin =
|
---|
[185] | 621 | CoordinateUtil::makeBinnedCoordinateSystem(factors, cSys, False);
|
---|
[169] | 622 | //
|
---|
| 623 | SpectralCoordinate sCBin = cSysBin.spectralCoordinate(0);
|
---|
| 624 | pTabOut->setCoordinate(sCBin, j);
|
---|
| 625 | }
|
---|
[163] | 626 |
|
---|
[169] | 627 | // Use RebinLattice to find shape
|
---|
[130] | 628 |
|
---|
[169] | 629 | IPosition shapeIn(1,sh.nchan);
|
---|
[185] | 630 | IPosition shapeOut = RebinLattice<Float>::rebinShape(shapeIn, factors);
|
---|
[169] | 631 | sh.nchan = shapeOut(0);
|
---|
| 632 | pTabOut->putSDHeader(sh);
|
---|
[144] | 633 |
|
---|
| 634 |
|
---|
[169] | 635 | // Loop over rows and bin along channel axis
|
---|
| 636 |
|
---|
| 637 | for (uInt i=0; i < in.nRow(); ++i) {
|
---|
| 638 | SDContainer sc = in.getSDContainer(i);
|
---|
[144] | 639 | //
|
---|
[169] | 640 | Array<Float> tSys(sc.getTsys()); // Get it out before sc changes shape
|
---|
[144] | 641 |
|
---|
[169] | 642 | // Bin up spectrum
|
---|
[144] | 643 |
|
---|
[169] | 644 | MaskedArray<Float> marr(in.rowAsMaskedArray(i));
|
---|
| 645 | MaskedArray<Float> marrout;
|
---|
[221] | 646 | LatticeUtilities::bin(marrout, marr, asap::ChanAxis, width);
|
---|
[144] | 647 |
|
---|
[169] | 648 | // Put back the binned data and flags
|
---|
[144] | 649 |
|
---|
[169] | 650 | IPosition ip2 = marrout.shape();
|
---|
| 651 | sc.resize(ip2);
|
---|
[146] | 652 | //
|
---|
[185] | 653 | putDataInSDC(sc, marrout.getArray(), marrout.getMask());
|
---|
[146] | 654 |
|
---|
[169] | 655 | // Bin up Tsys.
|
---|
[146] | 656 |
|
---|
[169] | 657 | Array<Bool> allGood(tSys.shape(),True);
|
---|
| 658 | MaskedArray<Float> tSysIn(tSys, allGood, True);
|
---|
[146] | 659 | //
|
---|
[169] | 660 | MaskedArray<Float> tSysOut;
|
---|
[221] | 661 | LatticeUtilities::bin(tSysOut, tSysIn, asap::ChanAxis, width);
|
---|
[169] | 662 | sc.putTsys(tSysOut.getArray());
|
---|
[146] | 663 | //
|
---|
[169] | 664 | pTabOut->putSDContainer(sc);
|
---|
| 665 | }
|
---|
| 666 | return pTabOut;
|
---|
[146] | 667 | }
|
---|
| 668 |
|
---|
[185] | 669 | SDMemTable* SDMath::simpleOperate(const SDMemTable& in, Float val, Bool doAll,
|
---|
[234] | 670 | uInt what) const
|
---|
[152] | 671 | //
|
---|
| 672 | // what = 0 Multiply
|
---|
| 673 | // 1 Add
|
---|
[146] | 674 | {
|
---|
[152] | 675 | SDMemTable* pOut = new SDMemTable(in,False);
|
---|
| 676 | const Table& tOut = pOut->table();
|
---|
| 677 | ArrayColumn<Float> spec(tOut,"SPECTRA");
|
---|
[146] | 678 | //
|
---|
[152] | 679 | if (doAll) {
|
---|
| 680 | for (uInt i=0; i < tOut.nrow(); i++) {
|
---|
| 681 |
|
---|
| 682 | // Get
|
---|
| 683 |
|
---|
| 684 | MaskedArray<Float> marr(pOut->rowAsMaskedArray(i));
|
---|
| 685 |
|
---|
| 686 | // Operate
|
---|
| 687 |
|
---|
| 688 | if (what==0) {
|
---|
| 689 | marr *= val;
|
---|
| 690 | } else if (what==1) {
|
---|
| 691 | marr += val;
|
---|
| 692 | }
|
---|
| 693 |
|
---|
| 694 | // Put
|
---|
| 695 |
|
---|
| 696 | spec.put(i, marr.getArray());
|
---|
| 697 | }
|
---|
| 698 | } else {
|
---|
| 699 |
|
---|
| 700 | // Get cursor location
|
---|
| 701 |
|
---|
| 702 | IPosition start, end;
|
---|
[185] | 703 | getCursorLocation(start, end, in);
|
---|
[152] | 704 | //
|
---|
| 705 | for (uInt i=0; i < tOut.nrow(); i++) {
|
---|
| 706 |
|
---|
| 707 | // Get
|
---|
| 708 |
|
---|
| 709 | MaskedArray<Float> dataIn(pOut->rowAsMaskedArray(i));
|
---|
| 710 |
|
---|
| 711 | // Modify. More work than we would like to deal with the mask
|
---|
| 712 |
|
---|
| 713 | Array<Float>& values = dataIn.getRWArray();
|
---|
| 714 | Array<Bool> mask(dataIn.getMask());
|
---|
| 715 | //
|
---|
| 716 | Array<Float> values2 = values(start,end);
|
---|
| 717 | Array<Bool> mask2 = mask(start,end);
|
---|
| 718 | MaskedArray<Float> t(values2,mask2);
|
---|
| 719 | if (what==0) {
|
---|
| 720 | t *= val;
|
---|
| 721 | } else if (what==1) {
|
---|
| 722 | t += val;
|
---|
| 723 | }
|
---|
| 724 | values(start, end) = t.getArray(); // Write back into 'dataIn'
|
---|
| 725 |
|
---|
| 726 | // Put
|
---|
| 727 | spec.put(i, dataIn.getArray());
|
---|
| 728 | }
|
---|
| 729 | }
|
---|
| 730 | //
|
---|
[146] | 731 | return pOut;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 |
|
---|
[152] | 735 |
|
---|
[234] | 736 | SDMemTable* SDMath::averagePol(const SDMemTable& in, const Vector<Bool>& mask) const
|
---|
[152] | 737 | //
|
---|
[165] | 738 | // Average all polarizations together, weighted by variance
|
---|
| 739 | //
|
---|
| 740 | {
|
---|
| 741 | // WeightType wtType = NONE;
|
---|
[185] | 742 | // convertWeightString(wtType, weight);
|
---|
[165] | 743 |
|
---|
| 744 | const uInt nRows = in.nRow();
|
---|
[209] | 745 | const uInt polAxis = asap::PolAxis; // Polarization axis
|
---|
| 746 | const uInt chanAxis = asap::ChanAxis; // Spectrum axis
|
---|
[165] | 747 |
|
---|
| 748 | // Create output Table and reshape number of polarizations
|
---|
| 749 |
|
---|
| 750 | Bool clear=True;
|
---|
| 751 | SDMemTable* pTabOut = new SDMemTable(in, clear);
|
---|
| 752 | SDHeader header = pTabOut->getSDHeader();
|
---|
| 753 | header.npol = 1;
|
---|
| 754 | pTabOut->putSDHeader(header);
|
---|
| 755 |
|
---|
| 756 | // Shape of input and output data
|
---|
| 757 |
|
---|
| 758 | const IPosition& shapeIn = in.rowAsMaskedArray(0u, False).shape();
|
---|
| 759 | IPosition shapeOut(shapeIn);
|
---|
| 760 | shapeOut(polAxis) = 1; // Average all polarizations
|
---|
| 761 | //
|
---|
| 762 | const uInt nChan = shapeIn(chanAxis);
|
---|
| 763 | const IPosition vecShapeOut(4,1,1,1,nChan); // A multi-dim form of a Vector shape
|
---|
| 764 | IPosition start(4), end(4);
|
---|
| 765 |
|
---|
| 766 | // Output arrays
|
---|
| 767 |
|
---|
| 768 | Array<Float> outData(shapeOut, 0.0);
|
---|
| 769 | Array<Bool> outMask(shapeOut, True);
|
---|
| 770 | const IPosition axes(2, 2, 3); // pol-channel plane
|
---|
| 771 | //
|
---|
| 772 | const Bool useMask = (mask.nelements() == shapeIn(chanAxis));
|
---|
| 773 |
|
---|
| 774 | // Loop over rows
|
---|
| 775 |
|
---|
| 776 | for (uInt iRow=0; iRow<nRows; iRow++) {
|
---|
| 777 |
|
---|
| 778 | // Get data for this row
|
---|
| 779 |
|
---|
| 780 | MaskedArray<Float> marr(in.rowAsMaskedArray(iRow));
|
---|
| 781 | Array<Float>& arr = marr.getRWArray();
|
---|
| 782 | const Array<Bool>& barr = marr.getMask();
|
---|
| 783 |
|
---|
| 784 | // Make iterators to iterate by pol-channel planes
|
---|
| 785 |
|
---|
| 786 | ReadOnlyArrayIterator<Float> itDataPlane(arr, axes);
|
---|
| 787 | ReadOnlyArrayIterator<Bool> itMaskPlane(barr, axes);
|
---|
| 788 |
|
---|
| 789 | // Accumulations
|
---|
| 790 |
|
---|
| 791 | Float fac = 1.0;
|
---|
| 792 | Vector<Float> vecSum(nChan,0.0);
|
---|
| 793 |
|
---|
| 794 | // Iterate through data by pol-channel planes
|
---|
| 795 |
|
---|
| 796 | while (!itDataPlane.pastEnd()) {
|
---|
| 797 |
|
---|
| 798 | // Iterate through plane by polarization and accumulate Vectors
|
---|
| 799 |
|
---|
| 800 | Vector<Float> t1(nChan); t1 = 0.0;
|
---|
| 801 | Vector<Bool> t2(nChan); t2 = True;
|
---|
| 802 | MaskedArray<Float> vecSum(t1,t2);
|
---|
| 803 | Float varSum = 0.0;
|
---|
| 804 | {
|
---|
| 805 | ReadOnlyVectorIterator<Float> itDataVec(itDataPlane.array(), 1);
|
---|
| 806 | ReadOnlyVectorIterator<Bool> itMaskVec(itMaskPlane.array(), 1);
|
---|
| 807 | while (!itDataVec.pastEnd()) {
|
---|
| 808 |
|
---|
| 809 | // Create MA of data & mask (optionally including OTF mask) and get variance
|
---|
| 810 |
|
---|
| 811 | if (useMask) {
|
---|
| 812 | const MaskedArray<Float> spec(itDataVec.vector(),mask&&itMaskVec.vector());
|
---|
| 813 | fac = 1.0 / variance(spec);
|
---|
| 814 | } else {
|
---|
| 815 | const MaskedArray<Float> spec(itDataVec.vector(),itMaskVec.vector());
|
---|
| 816 | fac = 1.0 / variance(spec);
|
---|
| 817 | }
|
---|
| 818 |
|
---|
| 819 | // Normalize spectrum (without OTF mask) and accumulate
|
---|
| 820 |
|
---|
| 821 | const MaskedArray<Float> spec(fac*itDataVec.vector(), itMaskVec.vector());
|
---|
| 822 | vecSum += spec;
|
---|
| 823 | varSum += fac;
|
---|
| 824 |
|
---|
| 825 | // Next
|
---|
| 826 |
|
---|
| 827 | itDataVec.next();
|
---|
| 828 | itMaskVec.next();
|
---|
| 829 | }
|
---|
| 830 | }
|
---|
| 831 |
|
---|
| 832 | // Normalize summed spectrum
|
---|
| 833 |
|
---|
| 834 | vecSum /= varSum;
|
---|
| 835 |
|
---|
| 836 | // FInd position in input data array. We are iterating by pol-channel
|
---|
| 837 | // plane so all that will change is beam and IF and that's what we want.
|
---|
| 838 |
|
---|
| 839 | IPosition pos = itDataPlane.pos();
|
---|
| 840 |
|
---|
| 841 | // Write out data. This is a bit messy. We have to reform the Vector
|
---|
| 842 | // accumulator into an Array of shape (1,1,1,nChan)
|
---|
| 843 |
|
---|
| 844 | start = pos;
|
---|
| 845 | end = pos;
|
---|
| 846 | end(chanAxis) = nChan-1;
|
---|
| 847 | outData(start,end) = vecSum.getArray().reform(vecShapeOut);
|
---|
| 848 | outMask(start,end) = vecSum.getMask().reform(vecShapeOut);
|
---|
| 849 |
|
---|
| 850 | // Step to next beam/IF combination
|
---|
| 851 |
|
---|
| 852 | itDataPlane.next();
|
---|
| 853 | itMaskPlane.next();
|
---|
| 854 | }
|
---|
| 855 |
|
---|
| 856 | // Generate output container and write it to output table
|
---|
| 857 |
|
---|
| 858 | SDContainer sc = in.getSDContainer();
|
---|
| 859 | sc.resize(shapeOut);
|
---|
| 860 | //
|
---|
[185] | 861 | putDataInSDC(sc, outData, outMask);
|
---|
[165] | 862 | pTabOut->putSDContainer(sc);
|
---|
| 863 | }
|
---|
| 864 | //
|
---|
| 865 | return pTabOut;
|
---|
| 866 | }
|
---|
[167] | 867 |
|
---|
[169] | 868 |
|
---|
[185] | 869 | SDMemTable* SDMath::smooth(const SDMemTable& in,
|
---|
| 870 | const casa::String& kernelType,
|
---|
[234] | 871 | casa::Float width, Bool doAll) const
|
---|
[177] | 872 | {
|
---|
[169] | 873 |
|
---|
[177] | 874 | // Number of channels
|
---|
[169] | 875 |
|
---|
[209] | 876 | const uInt chanAxis = asap::ChanAxis; // Spectral axis
|
---|
[177] | 877 | SDHeader sh = in.getSDHeader();
|
---|
| 878 | const uInt nChan = sh.nchan;
|
---|
| 879 |
|
---|
| 880 | // Generate Kernel
|
---|
| 881 |
|
---|
[185] | 882 | VectorKernel::KernelTypes type = VectorKernel::toKernelType(kernelType);
|
---|
[177] | 883 | Vector<Float> kernel = VectorKernel::make(type, width, nChan, True, False);
|
---|
| 884 |
|
---|
| 885 | // Generate Convolver
|
---|
| 886 |
|
---|
| 887 | IPosition shape(1,nChan);
|
---|
| 888 | Convolver<Float> conv(kernel, shape);
|
---|
| 889 |
|
---|
| 890 | // New Table
|
---|
| 891 |
|
---|
| 892 | SDMemTable* pTabOut = new SDMemTable(in,True);
|
---|
| 893 |
|
---|
| 894 | // Get cursor location
|
---|
| 895 |
|
---|
| 896 | IPosition start, end;
|
---|
[185] | 897 | getCursorLocation(start, end, in);
|
---|
[177] | 898 | //
|
---|
| 899 | IPosition shapeOut(4,1);
|
---|
| 900 |
|
---|
| 901 | // Output Vectors
|
---|
| 902 |
|
---|
| 903 | Vector<Float> valuesOut(nChan);
|
---|
| 904 | Vector<Bool> maskOut(nChan);
|
---|
| 905 |
|
---|
| 906 | // Loop over rows in Table
|
---|
| 907 |
|
---|
| 908 | for (uInt ri=0; ri < in.nRow(); ++ri) {
|
---|
| 909 |
|
---|
| 910 | // Get copy of data
|
---|
| 911 |
|
---|
| 912 | const MaskedArray<Float>& dataIn(in.rowAsMaskedArray(ri));
|
---|
| 913 | AlwaysAssert(dataIn.shape()(chanAxis)==nChan, AipsError);
|
---|
| 914 | //
|
---|
| 915 | Array<Float> valuesIn = dataIn.getArray();
|
---|
| 916 | Array<Bool> maskIn = dataIn.getMask();
|
---|
| 917 |
|
---|
| 918 | // Branch depending on whether we smooth all locations or just
|
---|
| 919 | // those pointed at by the current selection cursor
|
---|
| 920 |
|
---|
| 921 | if (doAll) {
|
---|
[221] | 922 | uInt axis = asap::ChanAxis;
|
---|
[177] | 923 | VectorIterator<Float> itValues(valuesIn, axis);
|
---|
| 924 | VectorIterator<Bool> itMask(maskIn, axis);
|
---|
| 925 | while (!itValues.pastEnd()) {
|
---|
| 926 |
|
---|
| 927 | // Smooth
|
---|
| 928 | if (kernelType==VectorKernel::HANNING) {
|
---|
| 929 | mathutil::hanning(valuesOut, maskOut, itValues.vector(), itMask.vector());
|
---|
| 930 | itMask.vector() = maskOut;
|
---|
| 931 | } else {
|
---|
| 932 | mathutil::replaceMaskByZero(itValues.vector(), itMask.vector());
|
---|
| 933 | conv.linearConv(valuesOut, itValues.vector());
|
---|
| 934 | }
|
---|
| 935 | //
|
---|
| 936 | itValues.vector() = valuesOut;
|
---|
| 937 | //
|
---|
| 938 | itValues.next();
|
---|
| 939 | itMask.next();
|
---|
| 940 | }
|
---|
| 941 | } else {
|
---|
| 942 |
|
---|
| 943 | // Set multi-dim Vector shape
|
---|
| 944 |
|
---|
| 945 | shapeOut(chanAxis) = valuesIn.shape()(chanAxis);
|
---|
| 946 |
|
---|
| 947 | // Stuff about with shapes so that we don't have conformance run-time errors
|
---|
| 948 |
|
---|
| 949 | Vector<Float> valuesIn2 = valuesIn(start,end).nonDegenerate();
|
---|
| 950 | Vector<Bool> maskIn2 = maskIn(start,end).nonDegenerate();
|
---|
| 951 |
|
---|
| 952 | // Smooth
|
---|
| 953 |
|
---|
| 954 | if (kernelType==VectorKernel::HANNING) {
|
---|
| 955 | mathutil::hanning(valuesOut, maskOut, valuesIn2, maskIn2);
|
---|
| 956 | maskIn(start,end) = maskOut.reform(shapeOut);
|
---|
| 957 | } else {
|
---|
| 958 | mathutil::replaceMaskByZero(valuesIn2, maskIn2);
|
---|
| 959 | conv.linearConv(valuesOut, valuesIn2);
|
---|
| 960 | }
|
---|
| 961 | //
|
---|
| 962 | valuesIn(start,end) = valuesOut.reform(shapeOut);
|
---|
| 963 | }
|
---|
| 964 |
|
---|
| 965 | // Create and put back
|
---|
| 966 |
|
---|
| 967 | SDContainer sc = in.getSDContainer(ri);
|
---|
[185] | 968 | putDataInSDC(sc, valuesIn, maskIn);
|
---|
[177] | 969 | //
|
---|
| 970 | pTabOut->putSDContainer(sc);
|
---|
| 971 | }
|
---|
| 972 | //
|
---|
| 973 | return pTabOut;
|
---|
| 974 | }
|
---|
| 975 |
|
---|
| 976 |
|
---|
[234] | 977 | SDMemTable* SDMath::convertFlux (const SDMemTable& in, Float a, Float eta, Bool doAll) const
|
---|
[221] | 978 | //
|
---|
| 979 | // As it is, this function could be implemented with 'simpleOperate'
|
---|
| 980 | // However, I anticipate that eventually we will look the conversion
|
---|
| 981 | // values up in a Table and apply them in a frequency dependent way,
|
---|
| 982 | // so I have implemented it fully here
|
---|
| 983 | //
|
---|
| 984 | {
|
---|
| 985 | SDHeader sh = in.getSDHeader();
|
---|
| 986 | SDMemTable* pTabOut = new SDMemTable(in, True);
|
---|
[177] | 987 |
|
---|
[221] | 988 | // FInd out how to convert values into Jy and K (e.g. units might be mJy or mK)
|
---|
| 989 | // Also automatically find out what we are converting to according to the
|
---|
| 990 | // flux unit
|
---|
[177] | 991 |
|
---|
[221] | 992 | Unit fluxUnit(sh.fluxunit);
|
---|
| 993 | Unit K(String("K"));
|
---|
| 994 | Unit JY(String("Jy"));
|
---|
| 995 | //
|
---|
| 996 | Bool toKelvin = True;
|
---|
| 997 | Double inFac = 1.0;
|
---|
| 998 | if (fluxUnit==JY) {
|
---|
| 999 | cerr << "Converting to K" << endl;
|
---|
| 1000 | //
|
---|
| 1001 | Quantum<Double> t(1.0,fluxUnit);
|
---|
| 1002 | Quantum<Double> t2 = t.get(JY);
|
---|
| 1003 | inFac = (t2 / t).getValue();
|
---|
| 1004 | //
|
---|
| 1005 | toKelvin = True;
|
---|
| 1006 | sh.fluxunit = "K";
|
---|
| 1007 | } else if (fluxUnit==K) {
|
---|
| 1008 | cerr << "Converting to Jy" << endl;
|
---|
| 1009 | //
|
---|
| 1010 | Quantum<Double> t(1.0,fluxUnit);
|
---|
| 1011 | Quantum<Double> t2 = t.get(K);
|
---|
| 1012 | inFac = (t2 / t).getValue();
|
---|
| 1013 | //
|
---|
| 1014 | toKelvin = False;
|
---|
| 1015 | sh.fluxunit = "Jy";
|
---|
| 1016 | } else {
|
---|
| 1017 | throw AipsError("Unrecognized brightness units in Table - must be consistent with Jy or K");
|
---|
| 1018 | }
|
---|
| 1019 | pTabOut->putSDHeader(sh);
|
---|
[177] | 1020 |
|
---|
[221] | 1021 | // Compute conversion factor. 'a' and 'eta' are really frequency, time and
|
---|
| 1022 | // telescope dependent and should be looked// up in a table
|
---|
| 1023 |
|
---|
[234] | 1024 | Float factor = 2.0 * inFac * 1.0e-7 * 1.0e26 *
|
---|
| 1025 | QC::k.getValue(Unit(String("erg/K"))) / a / eta;
|
---|
[221] | 1026 | if (toKelvin) {
|
---|
| 1027 | factor = 1.0 / factor;
|
---|
| 1028 | }
|
---|
| 1029 | cerr << "Applying conversion factor = " << factor << endl;
|
---|
| 1030 |
|
---|
| 1031 | // For operations only on specified cursor location
|
---|
| 1032 |
|
---|
| 1033 | IPosition start, end;
|
---|
| 1034 | getCursorLocation(start, end, in);
|
---|
| 1035 |
|
---|
| 1036 | // Loop over rows and apply factor to spectra
|
---|
| 1037 |
|
---|
| 1038 | const uInt axis = asap::ChanAxis;
|
---|
| 1039 | for (uInt i=0; i < in.nRow(); ++i) {
|
---|
| 1040 |
|
---|
| 1041 | // Get data
|
---|
| 1042 |
|
---|
| 1043 | MaskedArray<Float> dataIn(in.rowAsMaskedArray(i));
|
---|
| 1044 | Array<Float>& valuesIn = dataIn.getRWArray(); // writable reference
|
---|
| 1045 | const Array<Bool>& maskIn = dataIn.getMask();
|
---|
| 1046 |
|
---|
| 1047 | // Need to apply correct conversion factor (frequency and time dependent)
|
---|
| 1048 | // which should be sourced from a Table. For now we just apply the given
|
---|
| 1049 | // factor to everything
|
---|
| 1050 |
|
---|
| 1051 | if (doAll) {
|
---|
| 1052 | VectorIterator<Float> itValues(valuesIn, asap::ChanAxis);
|
---|
| 1053 | while (!itValues.pastEnd()) {
|
---|
| 1054 | itValues.vector() *= factor; // Writes back into dataIn
|
---|
| 1055 | //
|
---|
| 1056 | itValues.next();
|
---|
| 1057 | }
|
---|
| 1058 | } else {
|
---|
| 1059 | Array<Float> valuesIn2 = valuesIn(start,end);
|
---|
| 1060 | valuesIn2 *= factor;
|
---|
| 1061 | valuesIn(start,end) = valuesIn2;
|
---|
| 1062 | }
|
---|
| 1063 |
|
---|
| 1064 | // Write out
|
---|
| 1065 |
|
---|
| 1066 | SDContainer sc = in.getSDContainer(i);
|
---|
| 1067 | putDataInSDC(sc, valuesIn, maskIn);
|
---|
| 1068 | //
|
---|
| 1069 | pTabOut->putSDContainer(sc);
|
---|
| 1070 | }
|
---|
| 1071 | return pTabOut;
|
---|
| 1072 | }
|
---|
| 1073 |
|
---|
| 1074 |
|
---|
| 1075 |
|
---|
[234] | 1076 | SDMemTable* SDMath::gainElevation (const SDMemTable& in, const Vector<Float>& coeffs,
|
---|
| 1077 | const String& fileName,
|
---|
| 1078 | const String& methodStr, Bool doAll) const
|
---|
[227] | 1079 | {
|
---|
[234] | 1080 |
|
---|
| 1081 | // Get header and clone output table
|
---|
| 1082 |
|
---|
[227] | 1083 | SDHeader sh = in.getSDHeader();
|
---|
| 1084 | SDMemTable* pTabOut = new SDMemTable(in, True);
|
---|
| 1085 |
|
---|
[234] | 1086 | // Get elevation data from SDMemTable and convert to degrees
|
---|
[227] | 1087 |
|
---|
| 1088 | const Table& tab = in.table();
|
---|
| 1089 | ROScalarColumn<Float> elev(tab, "ELEVATION");
|
---|
[234] | 1090 | Vector<Float> x = elev.getColumn();
|
---|
| 1091 | x *= Float(180 / C::pi);
|
---|
[227] | 1092 | //
|
---|
[234] | 1093 | const uInt nC = coeffs.nelements();
|
---|
| 1094 | if (fileName.length()>0 && nC>0) {
|
---|
| 1095 | throw AipsError("You must choose either polynomial coefficients or an ascii file, not both");
|
---|
| 1096 | }
|
---|
| 1097 |
|
---|
| 1098 | // Correct
|
---|
| 1099 |
|
---|
| 1100 | if (nC>0 || fileName.length()==0) {
|
---|
| 1101 |
|
---|
| 1102 | // Find instrument
|
---|
| 1103 |
|
---|
| 1104 | Bool throwIt = True;
|
---|
| 1105 | Instrument inst = SDMemTable::convertInstrument (sh.antennaname, throwIt);
|
---|
| 1106 |
|
---|
| 1107 | // Set polynomial
|
---|
| 1108 |
|
---|
| 1109 | Polynomial<Float>* pPoly = 0;
|
---|
| 1110 | Vector<Float> coeff;
|
---|
| 1111 | String msg;
|
---|
| 1112 | if (nC>0) {
|
---|
| 1113 | pPoly = new Polynomial<Float>(nC);
|
---|
| 1114 | coeff = coeffs;
|
---|
| 1115 | msg = String("user");
|
---|
| 1116 | } else {
|
---|
| 1117 | if (inst==PKSMULTIBEAM) {
|
---|
| 1118 | } else if (inst==PKSSINGLEBEAM) {
|
---|
| 1119 | } else if (inst==TIDBINBILLA) {
|
---|
| 1120 | pPoly = new Polynomial<Float>(3);
|
---|
| 1121 | coeff.resize(3);
|
---|
| 1122 | coeff(0) = 3.58788e-1;
|
---|
| 1123 | coeff(1) = 2.87243e-2;
|
---|
| 1124 | coeff(2) = -3.219093e-4;
|
---|
| 1125 | } else if (inst==MOPRA) {
|
---|
| 1126 | }
|
---|
| 1127 | msg = String("built in");
|
---|
| 1128 | }
|
---|
[227] | 1129 | //
|
---|
[234] | 1130 | if (coeff.nelements()>0) {
|
---|
| 1131 | pPoly->setCoefficients(coeff);
|
---|
| 1132 | } else {
|
---|
| 1133 | throw AipsError("There is no known gain-el polynomial known for this instrument");
|
---|
| 1134 | }
|
---|
| 1135 | //
|
---|
| 1136 | cerr << "Making polynomial correction with " << msg << " coefficients" << endl;
|
---|
| 1137 | const uInt nRow = in.nRow();
|
---|
| 1138 | Vector<Float> factor(nRow);
|
---|
| 1139 | for (uInt i=0; i<nRow; i++) {
|
---|
| 1140 | factor[i] = (*pPoly)(x[i]);
|
---|
| 1141 | }
|
---|
| 1142 | delete pPoly;
|
---|
| 1143 | //
|
---|
| 1144 | correctFromVector (pTabOut, in, doAll, factor);
|
---|
| 1145 | } else {
|
---|
| 1146 |
|
---|
| 1147 | // Indicate which columns to read from ascii file
|
---|
| 1148 |
|
---|
| 1149 | String col0("ELEVATION");
|
---|
| 1150 | String col1("FACTOR");
|
---|
| 1151 |
|
---|
| 1152 | // Read and correct
|
---|
| 1153 |
|
---|
| 1154 | cerr << "Making correction from ascii Table" << endl;
|
---|
| 1155 | correctFromAsciiTable (pTabOut, in, fileName, col0, col1,
|
---|
| 1156 | methodStr, doAll, x);
|
---|
| 1157 | }
|
---|
| 1158 | //
|
---|
| 1159 | return pTabOut;
|
---|
[230] | 1160 | }
|
---|
[227] | 1161 |
|
---|
[230] | 1162 |
|
---|
[227] | 1163 |
|
---|
[234] | 1164 | SDMemTable* SDMath::opacity (const SDMemTable& in, Float tau, Bool doAll) const
|
---|
| 1165 | {
|
---|
[227] | 1166 |
|
---|
[234] | 1167 | // Get header and clone output table
|
---|
[227] | 1168 |
|
---|
[234] | 1169 | SDHeader sh = in.getSDHeader();
|
---|
| 1170 | SDMemTable* pTabOut = new SDMemTable(in, True);
|
---|
| 1171 |
|
---|
| 1172 | // Get elevation data from SDMemTable and convert to degrees
|
---|
| 1173 |
|
---|
| 1174 | const Table& tab = in.table();
|
---|
| 1175 | ROScalarColumn<Float> elev(tab, "ELEVATION");
|
---|
| 1176 | Vector<Float> zDist = elev.getColumn();
|
---|
| 1177 | zDist = Float(C::pi_2) - zDist;
|
---|
| 1178 |
|
---|
| 1179 | // Generate correction factor
|
---|
| 1180 |
|
---|
| 1181 | const uInt nRow = in.nRow();
|
---|
| 1182 | Vector<Float> factor(nRow);
|
---|
| 1183 | Vector<Float> factor2(nRow);
|
---|
| 1184 | for (uInt i=0; i<nRow; i++) {
|
---|
| 1185 | factor[i] = exp(tau)/cos(zDist[i]);
|
---|
| 1186 | }
|
---|
| 1187 |
|
---|
| 1188 | // Correct
|
---|
| 1189 |
|
---|
| 1190 | correctFromVector (pTabOut, in, doAll, factor);
|
---|
| 1191 | //
|
---|
| 1192 | return pTabOut;
|
---|
| 1193 | }
|
---|
| 1194 |
|
---|
| 1195 |
|
---|
| 1196 |
|
---|
| 1197 |
|
---|
[169] | 1198 | // 'private' functions
|
---|
| 1199 |
|
---|
[185] | 1200 | void SDMath::fillSDC(SDContainer& sc,
|
---|
| 1201 | const Array<Bool>& mask,
|
---|
| 1202 | const Array<Float>& data,
|
---|
| 1203 | const Array<Float>& tSys,
|
---|
| 1204 | Int scanID, Double timeStamp,
|
---|
| 1205 | Double interval, const String& sourceName,
|
---|
[227] | 1206 | const Vector<uInt>& freqID) const
|
---|
[167] | 1207 | {
|
---|
[169] | 1208 | // Data and mask
|
---|
[167] | 1209 |
|
---|
[185] | 1210 | putDataInSDC(sc, data, mask);
|
---|
[167] | 1211 |
|
---|
[169] | 1212 | // TSys
|
---|
| 1213 |
|
---|
| 1214 | sc.putTsys(tSys);
|
---|
| 1215 |
|
---|
| 1216 | // Time things
|
---|
| 1217 |
|
---|
| 1218 | sc.timestamp = timeStamp;
|
---|
| 1219 | sc.interval = interval;
|
---|
| 1220 | sc.scanid = scanID;
|
---|
[167] | 1221 | //
|
---|
[169] | 1222 | sc.sourcename = sourceName;
|
---|
| 1223 | sc.putFreqMap(freqID);
|
---|
| 1224 | }
|
---|
[167] | 1225 |
|
---|
[185] | 1226 | void SDMath::normalize(MaskedArray<Float>& sum,
|
---|
[169] | 1227 | const Array<Float>& sumSq,
|
---|
| 1228 | const Array<Float>& nPts,
|
---|
| 1229 | WeightType wtType, Int axis,
|
---|
[227] | 1230 | Int nAxesSub) const
|
---|
[169] | 1231 | {
|
---|
| 1232 | IPosition pos2(nAxesSub,0);
|
---|
| 1233 | //
|
---|
| 1234 | if (wtType==NONE) {
|
---|
[167] | 1235 |
|
---|
[169] | 1236 | // We just average by the number of points accumulated.
|
---|
| 1237 | // We need to make a MA out of nPts so that no divide by
|
---|
| 1238 | // zeros occur
|
---|
[167] | 1239 |
|
---|
[169] | 1240 | MaskedArray<Float> t(nPts, (nPts>Float(0.0)));
|
---|
| 1241 | sum /= t;
|
---|
| 1242 | } else if (wtType==VAR) {
|
---|
[167] | 1243 |
|
---|
[169] | 1244 | // Normalize each spectrum by sum(1/var) where the variance
|
---|
| 1245 | // is worked out for each spectrum
|
---|
| 1246 |
|
---|
| 1247 | Array<Float>& data = sum.getRWArray();
|
---|
| 1248 | VectorIterator<Float> itData(data, axis);
|
---|
| 1249 | while (!itData.pastEnd()) {
|
---|
| 1250 | pos2 = itData.pos().getFirst(nAxesSub);
|
---|
| 1251 | itData.vector() /= sumSq(pos2);
|
---|
| 1252 | itData.next();
|
---|
| 1253 | }
|
---|
| 1254 | } else if (wtType==TSYS) {
|
---|
| 1255 | }
|
---|
| 1256 | }
|
---|
| 1257 |
|
---|
| 1258 |
|
---|
[185] | 1259 | void SDMath::accumulate(Double& timeSum, Double& intSum, Int& nAccum,
|
---|
| 1260 | MaskedArray<Float>& sum, Array<Float>& sumSq,
|
---|
| 1261 | Array<Float>& nPts, Array<Float>& tSysSum,
|
---|
| 1262 | const Array<Float>& tSys, const Array<Float>& nInc,
|
---|
| 1263 | const Vector<Bool>& mask, Double time, Double interval,
|
---|
| 1264 | const Block<CountedPtr<SDMemTable> >& in,
|
---|
| 1265 | uInt iTab, uInt iRow, uInt axis,
|
---|
| 1266 | uInt nAxesSub, Bool useMask,
|
---|
[227] | 1267 | WeightType wtType) const
|
---|
[169] | 1268 | {
|
---|
| 1269 |
|
---|
| 1270 | // Get data
|
---|
| 1271 |
|
---|
| 1272 | MaskedArray<Float> dataIn(in[iTab]->rowAsMaskedArray(iRow));
|
---|
| 1273 | Array<Float>& valuesIn = dataIn.getRWArray(); // writable reference
|
---|
| 1274 | const Array<Bool>& maskIn = dataIn.getMask(); // RO reference
|
---|
[167] | 1275 | //
|
---|
[169] | 1276 | if (wtType==NONE) {
|
---|
| 1277 | const MaskedArray<Float> n(nInc,dataIn.getMask());
|
---|
| 1278 | nPts += n; // Only accumulates where mask==T
|
---|
| 1279 | } else if (wtType==VAR) {
|
---|
[167] | 1280 |
|
---|
[169] | 1281 | // We are going to average the data, weighted by the noise for each pol, beam and IF.
|
---|
| 1282 | // So therefore we need to iterate through by spectrum (axis 3)
|
---|
[167] | 1283 |
|
---|
[169] | 1284 | VectorIterator<Float> itData(valuesIn, axis);
|
---|
| 1285 | ReadOnlyVectorIterator<Bool> itMask(maskIn, axis);
|
---|
| 1286 | Float fac = 1.0;
|
---|
| 1287 | IPosition pos(nAxesSub,0);
|
---|
| 1288 | //
|
---|
| 1289 | while (!itData.pastEnd()) {
|
---|
[167] | 1290 |
|
---|
[169] | 1291 | // Make MaskedArray of Vector, optionally apply OTF mask, and find scaling factor
|
---|
[167] | 1292 |
|
---|
[169] | 1293 | if (useMask) {
|
---|
| 1294 | MaskedArray<Float> tmp(itData.vector(),mask&&itMask.vector());
|
---|
| 1295 | fac = 1.0/variance(tmp);
|
---|
| 1296 | } else {
|
---|
| 1297 | MaskedArray<Float> tmp(itData.vector(),itMask.vector());
|
---|
| 1298 | fac = 1.0/variance(tmp);
|
---|
| 1299 | }
|
---|
| 1300 |
|
---|
| 1301 | // Scale data
|
---|
| 1302 |
|
---|
| 1303 | itData.vector() *= fac; // Writes back into 'dataIn'
|
---|
[167] | 1304 | //
|
---|
[169] | 1305 | // Accumulate variance per if/pol/beam averaged over spectrum
|
---|
| 1306 | // This method to get pos2 from itData.pos() is only valid
|
---|
| 1307 | // because the spectral axis is the last one (so we can just
|
---|
| 1308 | // copy the first nAXesSub positions out)
|
---|
[167] | 1309 |
|
---|
[169] | 1310 | pos = itData.pos().getFirst(nAxesSub);
|
---|
| 1311 | sumSq(pos) += fac;
|
---|
| 1312 | //
|
---|
| 1313 | itData.next();
|
---|
| 1314 | itMask.next();
|
---|
| 1315 | }
|
---|
| 1316 | } else if (wtType==TSYS) {
|
---|
| 1317 | }
|
---|
[167] | 1318 |
|
---|
[169] | 1319 | // Accumulate sum of (possibly scaled) data
|
---|
| 1320 |
|
---|
| 1321 | sum += dataIn;
|
---|
| 1322 |
|
---|
| 1323 | // Accumulate Tsys, time, and interval
|
---|
| 1324 |
|
---|
| 1325 | tSysSum += tSys;
|
---|
| 1326 | timeSum += time;
|
---|
| 1327 | intSum += interval;
|
---|
| 1328 | nAccum += 1;
|
---|
| 1329 | }
|
---|
| 1330 |
|
---|
| 1331 |
|
---|
| 1332 |
|
---|
| 1333 |
|
---|
[185] | 1334 | void SDMath::getCursorLocation(IPosition& start, IPosition& end,
|
---|
[227] | 1335 | const SDMemTable& in) const
|
---|
[169] | 1336 | {
|
---|
| 1337 | const uInt nDim = 4;
|
---|
| 1338 | const uInt i = in.getBeam();
|
---|
| 1339 | const uInt j = in.getIF();
|
---|
| 1340 | const uInt k = in.getPol();
|
---|
| 1341 | const uInt n = in.nChan();
|
---|
[167] | 1342 | //
|
---|
[169] | 1343 | start.resize(nDim);
|
---|
| 1344 | start(0) = i;
|
---|
| 1345 | start(1) = j;
|
---|
| 1346 | start(2) = k;
|
---|
| 1347 | start(3) = 0;
|
---|
[167] | 1348 | //
|
---|
[169] | 1349 | end.resize(nDim);
|
---|
| 1350 | end(0) = i;
|
---|
| 1351 | end(1) = j;
|
---|
| 1352 | end(2) = k;
|
---|
| 1353 | end(3) = n-1;
|
---|
| 1354 | }
|
---|
| 1355 |
|
---|
| 1356 |
|
---|
[227] | 1357 | void SDMath::convertWeightString(WeightType& wtType, const String& weightStr) const
|
---|
[169] | 1358 | {
|
---|
| 1359 | String tStr(weightStr);
|
---|
| 1360 | tStr.upcase();
|
---|
| 1361 | if (tStr.contains(String("NONE"))) {
|
---|
| 1362 | wtType = NONE;
|
---|
| 1363 | } else if (tStr.contains(String("VAR"))) {
|
---|
| 1364 | wtType = VAR;
|
---|
| 1365 | } else if (tStr.contains(String("TSYS"))) {
|
---|
| 1366 | wtType = TSYS;
|
---|
[185] | 1367 | throw(AipsError("T_sys weighting not yet implemented"));
|
---|
[169] | 1368 | } else {
|
---|
[185] | 1369 | throw(AipsError("Unrecognized weighting type"));
|
---|
[167] | 1370 | }
|
---|
| 1371 | }
|
---|
| 1372 |
|
---|
[227] | 1373 | void SDMath::convertInterpString(Int& type, const String& interp) const
|
---|
| 1374 | {
|
---|
| 1375 | String tStr(interp);
|
---|
| 1376 | tStr.upcase();
|
---|
| 1377 | if (tStr.contains(String("NEAR"))) {
|
---|
| 1378 | type = InterpolateArray1D<Float,Float>::nearestNeighbour;
|
---|
| 1379 | } else if (tStr.contains(String("LIN"))) {
|
---|
| 1380 | type = InterpolateArray1D<Float,Float>::linear;
|
---|
| 1381 | } else if (tStr.contains(String("CUB"))) {
|
---|
| 1382 | type = InterpolateArray1D<Float,Float>::cubic;
|
---|
| 1383 | } else if (tStr.contains(String("SPL"))) {
|
---|
| 1384 | type = InterpolateArray1D<Float,Float>::spline;
|
---|
| 1385 | } else {
|
---|
| 1386 | throw(AipsError("Unrecognized interpolation type"));
|
---|
| 1387 | }
|
---|
| 1388 | }
|
---|
| 1389 |
|
---|
[185] | 1390 | void SDMath::putDataInSDC(SDContainer& sc, const Array<Float>& data,
|
---|
[227] | 1391 | const Array<Bool>& mask) const
|
---|
[169] | 1392 | {
|
---|
| 1393 | sc.putSpectrum(data);
|
---|
| 1394 | //
|
---|
| 1395 | Array<uChar> outflags(data.shape());
|
---|
| 1396 | convertArray(outflags,!mask);
|
---|
| 1397 | sc.putFlags(outflags);
|
---|
| 1398 | }
|
---|
[227] | 1399 |
|
---|
| 1400 | Table SDMath::readAsciiFile (const String& fileName) const
|
---|
| 1401 | {
|
---|
[230] | 1402 | String formatString;
|
---|
| 1403 | Table tbl = readAsciiTable (formatString, Table::Memory, fileName, "", "", False);
|
---|
[227] | 1404 | return tbl;
|
---|
| 1405 | }
|
---|
[230] | 1406 |
|
---|
| 1407 |
|
---|
[234] | 1408 |
|
---|
| 1409 | void SDMath::correctFromAsciiTable(SDMemTable* pTabOut,
|
---|
| 1410 | const SDMemTable& in, const String& fileName,
|
---|
| 1411 | const String& col0, const String& col1,
|
---|
| 1412 | const String& methodStr, Bool doAll,
|
---|
| 1413 | const Vector<Float>& xOut) const
|
---|
[230] | 1414 | {
|
---|
| 1415 |
|
---|
| 1416 | // Read gain-elevation ascii file data into a Table.
|
---|
| 1417 |
|
---|
[234] | 1418 | Table geTable = readAsciiFile (fileName);
|
---|
[230] | 1419 | //
|
---|
[234] | 1420 | correctFromTable (pTabOut, in, geTable, col0, col1, methodStr, doAll, xOut);
|
---|
[230] | 1421 | }
|
---|
| 1422 |
|
---|
[234] | 1423 | void SDMath::correctFromTable(SDMemTable* pTabOut, const SDMemTable& in,
|
---|
| 1424 | const Table& tTable, const String& col0,
|
---|
| 1425 | const String& col1,
|
---|
| 1426 | const String& methodStr, Bool doAll,
|
---|
| 1427 | const Vector<Float>& xOut) const
|
---|
[230] | 1428 | {
|
---|
| 1429 |
|
---|
| 1430 | // Get data from Table
|
---|
| 1431 |
|
---|
| 1432 | ROScalarColumn<Float> geElCol(tTable, col0);
|
---|
| 1433 | ROScalarColumn<Float> geFacCol(tTable, col1);
|
---|
| 1434 | Vector<Float> xIn = geElCol.getColumn();
|
---|
| 1435 | Vector<Float> yIn = geFacCol.getColumn();
|
---|
| 1436 | Vector<Bool> maskIn(xIn.nelements(),True);
|
---|
| 1437 |
|
---|
| 1438 | // Interpolate (and extrapolate) with desired method
|
---|
| 1439 |
|
---|
| 1440 | Int method = 0;
|
---|
| 1441 | convertInterpString(method, methodStr);
|
---|
| 1442 | //
|
---|
| 1443 | Vector<Float> yOut;
|
---|
| 1444 | Vector<Bool> maskOut;
|
---|
| 1445 | InterpolateArray1D<Float,Float>::interpolate(yOut, maskOut, xOut,
|
---|
| 1446 | xIn, yIn, maskIn, method,
|
---|
| 1447 | True, True);
|
---|
[234] | 1448 | // Apply
|
---|
[230] | 1449 |
|
---|
[234] | 1450 | correctFromVector (pTabOut, in, doAll, yOut);
|
---|
| 1451 | }
|
---|
| 1452 |
|
---|
| 1453 |
|
---|
| 1454 | void SDMath::correctFromVector (SDMemTable* pTabOut, const SDMemTable& in,
|
---|
| 1455 | Bool doAll, const Vector<Float>& factor) const
|
---|
| 1456 | {
|
---|
[230] | 1457 | // For operations only on specified cursor location
|
---|
| 1458 |
|
---|
| 1459 | IPosition start, end;
|
---|
| 1460 | getCursorLocation(start, end, in);
|
---|
| 1461 |
|
---|
| 1462 | // Loop over rows and interpolate correction factor
|
---|
| 1463 |
|
---|
| 1464 | const uInt axis = asap::ChanAxis;
|
---|
| 1465 | for (uInt i=0; i < in.nRow(); ++i) {
|
---|
| 1466 |
|
---|
| 1467 | // Get data
|
---|
| 1468 |
|
---|
| 1469 | MaskedArray<Float> dataIn(in.rowAsMaskedArray(i));
|
---|
[234] | 1470 | Array<Float>& valuesIn = dataIn.getRWArray();
|
---|
[230] | 1471 | const Array<Bool>& maskIn = dataIn.getMask();
|
---|
| 1472 |
|
---|
| 1473 | // Apply factor
|
---|
| 1474 |
|
---|
| 1475 | if (doAll) {
|
---|
| 1476 | VectorIterator<Float> itValues(valuesIn, asap::ChanAxis);
|
---|
| 1477 | while (!itValues.pastEnd()) {
|
---|
[234] | 1478 | itValues.vector() *= factor(i);
|
---|
[230] | 1479 | itValues.next();
|
---|
| 1480 | }
|
---|
| 1481 | } else {
|
---|
| 1482 | Array<Float> valuesIn2 = valuesIn(start,end);
|
---|
[234] | 1483 | valuesIn2 *= factor(i);
|
---|
[230] | 1484 | valuesIn(start,end) = valuesIn2;
|
---|
| 1485 | }
|
---|
| 1486 |
|
---|
| 1487 | // Write out
|
---|
| 1488 |
|
---|
| 1489 | SDContainer sc = in.getSDContainer(i);
|
---|
| 1490 | putDataInSDC(sc, valuesIn, maskIn);
|
---|
| 1491 | //
|
---|
| 1492 | pTabOut->putSDContainer(sc);
|
---|
| 1493 | }
|
---|
| 1494 | }
|
---|
| 1495 |
|
---|
[234] | 1496 |
|
---|