[805] | 1 | // |
---|
| 2 | // C++ Implementation: STMath |
---|
| 3 | // |
---|
| 4 | // Description: |
---|
| 5 | // |
---|
| 6 | // |
---|
| 7 | // Author: Malte Marquarding <asap@atnf.csiro.au>, (C) 2006 |
---|
| 8 | // |
---|
| 9 | // Copyright: See COPYING file that comes with this distribution |
---|
| 10 | // |
---|
| 11 | // |
---|
[38] | 12 | |
---|
[330] | 13 | #include <casa/iomanip.h> |
---|
[805] | 14 | #include <casa/Exceptions/Error.h> |
---|
| 15 | #include <casa/Containers/Block.h> |
---|
[81] | 16 | #include <casa/BasicSL/String.h> |
---|
[805] | 17 | #include <tables/Tables/TableIter.h> |
---|
| 18 | #include <tables/Tables/TableCopy.h> |
---|
| 19 | #include <casa/Arrays/MaskArrLogi.h> |
---|
| 20 | #include <casa/Arrays/MaskArrMath.h> |
---|
| 21 | #include <casa/Arrays/ArrayLogical.h> |
---|
[81] | 22 | #include <casa/Arrays/ArrayMath.h> |
---|
[805] | 23 | #include <casa/Containers/RecordField.h> |
---|
| 24 | #include <tables/Tables/TableRow.h> |
---|
| 25 | #include <tables/Tables/TableVector.h> |
---|
[917] | 26 | #include <tables/Tables/TabVecMath.h> |
---|
[805] | 27 | #include <tables/Tables/ExprNode.h> |
---|
| 28 | #include <tables/Tables/TableRecord.h> |
---|
| 29 | #include <tables/Tables/ReadAsciiTable.h> |
---|
[2] | 30 | |
---|
[262] | 31 | #include <lattices/Lattices/LatticeUtilities.h> |
---|
| 32 | |
---|
[917] | 33 | #include <coordinates/Coordinates/SpectralCoordinate.h> |
---|
| 34 | #include <coordinates/Coordinates/CoordinateSystem.h> |
---|
| 35 | #include <coordinates/Coordinates/CoordinateUtil.h> |
---|
| 36 | #include <coordinates/Coordinates/FrequencyAligner.h> |
---|
| 37 | |
---|
[177] | 38 | #include <scimath/Mathematics/VectorKernel.h> |
---|
| 39 | #include <scimath/Mathematics/Convolver.h> |
---|
[234] | 40 | #include <scimath/Functionals/Polynomial.h> |
---|
[177] | 41 | |
---|
[38] | 42 | #include "MathUtils.h" |
---|
[805] | 43 | #include "RowAccumulator.h" |
---|
[878] | 44 | #include "STAttr.h" |
---|
[805] | 45 | #include "STMath.h" |
---|
[2] | 46 | |
---|
[805] | 47 | using namespace casa; |
---|
[2] | 48 | |
---|
[83] | 49 | using namespace asap; |
---|
[2] | 50 | |
---|
[805] | 51 | STMath::STMath(bool insitu) : |
---|
| 52 | insitu_(insitu) |
---|
[716] | 53 | { |
---|
| 54 | } |
---|
[170] | 55 | |
---|
| 56 | |
---|
[805] | 57 | STMath::~STMath() |
---|
[170] | 58 | { |
---|
| 59 | } |
---|
| 60 | |
---|
[805] | 61 | CountedPtr<Scantable> |
---|
[977] | 62 | STMath::average( const std::vector<CountedPtr<Scantable> >& in, |
---|
[858] | 63 | const std::vector<bool>& mask, |
---|
[805] | 64 | const std::string& weight, |
---|
[977] | 65 | const std::string& avmode) |
---|
[262] | 66 | { |
---|
[977] | 67 | if ( avmode == "SCAN" && in.size() != 1 ) |
---|
[805] | 68 | throw(AipsError("Can't perform 'SCAN' averaging on multiple tables")); |
---|
| 69 | WeightType wtype = stringToWeight(weight); |
---|
[926] | 70 | |
---|
[805] | 71 | // output |
---|
| 72 | // clone as this is non insitu |
---|
| 73 | bool insitu = insitu_; |
---|
| 74 | setInsitu(false); |
---|
[977] | 75 | CountedPtr< Scantable > out = getScantable(in[0], true); |
---|
[805] | 76 | setInsitu(insitu); |
---|
[977] | 77 | std::vector<CountedPtr<Scantable> >::const_iterator stit = in.begin(); |
---|
[862] | 78 | ++stit; |
---|
[977] | 79 | while ( stit != in.end() ) { |
---|
[862] | 80 | out->appendToHistoryTable((*stit)->history()); |
---|
| 81 | ++stit; |
---|
| 82 | } |
---|
[294] | 83 | |
---|
[805] | 84 | Table& tout = out->table(); |
---|
[701] | 85 | |
---|
[805] | 86 | /// @todo check if all scantables are conformant |
---|
[294] | 87 | |
---|
[805] | 88 | ArrayColumn<Float> specColOut(tout,"SPECTRA"); |
---|
| 89 | ArrayColumn<uChar> flagColOut(tout,"FLAGTRA"); |
---|
| 90 | ArrayColumn<Float> tsysColOut(tout,"TSYS"); |
---|
| 91 | ScalarColumn<Double> mjdColOut(tout,"TIME"); |
---|
| 92 | ScalarColumn<Double> intColOut(tout,"INTERVAL"); |
---|
[1008] | 93 | ScalarColumn<uInt> cycColOut(tout,"CYCLENO"); |
---|
[262] | 94 | |
---|
[805] | 95 | // set up the output table rows. These are based on the structure of the |
---|
[862] | 96 | // FIRST scantable in the vector |
---|
[977] | 97 | const Table& baset = in[0]->table(); |
---|
[262] | 98 | |
---|
[805] | 99 | Block<String> cols(3); |
---|
| 100 | cols[0] = String("BEAMNO"); |
---|
| 101 | cols[1] = String("IFNO"); |
---|
| 102 | cols[2] = String("POLNO"); |
---|
| 103 | if ( avmode == "SOURCE" ) { |
---|
| 104 | cols.resize(4); |
---|
| 105 | cols[3] = String("SRCNAME"); |
---|
[488] | 106 | } |
---|
[977] | 107 | if ( avmode == "SCAN" && in.size() == 1) { |
---|
[805] | 108 | cols.resize(4); |
---|
| 109 | cols[3] = String("SCANNO"); |
---|
[2] | 110 | } |
---|
[805] | 111 | uInt outrowCount = 0; |
---|
| 112 | TableIterator iter(baset, cols); |
---|
| 113 | while (!iter.pastEnd()) { |
---|
| 114 | Table subt = iter.table(); |
---|
| 115 | // copy the first row of this selection into the new table |
---|
| 116 | tout.addRow(); |
---|
| 117 | TableCopy::copyRows(tout, subt, outrowCount, 0, 1); |
---|
| 118 | ++outrowCount; |
---|
| 119 | ++iter; |
---|
[144] | 120 | } |
---|
[805] | 121 | RowAccumulator acc(wtype); |
---|
[858] | 122 | Vector<Bool> cmask(mask); |
---|
| 123 | acc.setUserMask(cmask); |
---|
[805] | 124 | ROTableRow row(tout); |
---|
| 125 | ROArrayColumn<Float> specCol, tsysCol; |
---|
| 126 | ROArrayColumn<uChar> flagCol; |
---|
| 127 | ROScalarColumn<Double> mjdCol, intCol; |
---|
| 128 | ROScalarColumn<Int> scanIDCol; |
---|
[144] | 129 | |
---|
[805] | 130 | for (uInt i=0; i < tout.nrow(); ++i) { |
---|
[996] | 131 | for ( int j=0; j < int(in.size()); ++j ) { |
---|
[977] | 132 | const Table& tin = in[j]->table(); |
---|
[805] | 133 | const TableRecord& rec = row.get(i); |
---|
| 134 | ROScalarColumn<Double> tmp(tin, "TIME"); |
---|
| 135 | Double td;tmp.get(0,td); |
---|
| 136 | Table basesubt = tin(tin.col("BEAMNO") == Int(rec.asuInt("BEAMNO")) |
---|
| 137 | && tin.col("IFNO") == Int(rec.asuInt("IFNO")) |
---|
| 138 | && tin.col("POLNO") == Int(rec.asuInt("POLNO")) ); |
---|
| 139 | Table subt; |
---|
| 140 | if ( avmode == "SOURCE") { |
---|
| 141 | subt = basesubt( basesubt.col("SRCNAME") == rec.asString("SRCNAME") ); |
---|
| 142 | } else if (avmode == "SCAN") { |
---|
| 143 | subt = basesubt( basesubt.col("SCANNO") == Int(rec.asuInt("SCANNO")) ); |
---|
| 144 | } else { |
---|
| 145 | subt = basesubt; |
---|
| 146 | } |
---|
| 147 | specCol.attach(subt,"SPECTRA"); |
---|
| 148 | flagCol.attach(subt,"FLAGTRA"); |
---|
| 149 | tsysCol.attach(subt,"TSYS"); |
---|
| 150 | intCol.attach(subt,"INTERVAL"); |
---|
| 151 | mjdCol.attach(subt,"TIME"); |
---|
| 152 | Vector<Float> spec,tsys; |
---|
| 153 | Vector<uChar> flag; |
---|
| 154 | Double inter,time; |
---|
| 155 | for (uInt k = 0; k < subt.nrow(); ++k ) { |
---|
| 156 | flagCol.get(k, flag); |
---|
| 157 | Vector<Bool> bflag(flag.shape()); |
---|
| 158 | convertArray(bflag, flag); |
---|
| 159 | if ( allEQ(bflag, True) ) { |
---|
| 160 | continue;//don't accumulate |
---|
[144] | 161 | } |
---|
[805] | 162 | specCol.get(k, spec); |
---|
| 163 | tsysCol.get(k, tsys); |
---|
| 164 | intCol.get(k, inter); |
---|
| 165 | mjdCol.get(k, time); |
---|
| 166 | // spectrum has to be added last to enable weighting by the other values |
---|
| 167 | acc.add(spec, !bflag, tsys, inter, time); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | //write out |
---|
| 171 | specColOut.put(i, acc.getSpectrum()); |
---|
| 172 | const Vector<Bool>& msk = acc.getMask(); |
---|
| 173 | Vector<uChar> flg(msk.shape()); |
---|
| 174 | convertArray(flg, !msk); |
---|
| 175 | flagColOut.put(i, flg); |
---|
| 176 | tsysColOut.put(i, acc.getTsys()); |
---|
| 177 | intColOut.put(i, acc.getInterval()); |
---|
| 178 | mjdColOut.put(i, acc.getTime()); |
---|
[1008] | 179 | // we should only have one cycle now -> reset it to be 0 |
---|
| 180 | // frequency switched data has different CYCLENO for different IFNO |
---|
| 181 | // which requires resetting this value |
---|
| 182 | cycColOut.put(i, uInt(0)); |
---|
[805] | 183 | acc.reset(); |
---|
[144] | 184 | } |
---|
[805] | 185 | return out; |
---|
[2] | 186 | } |
---|
[9] | 187 | |
---|
[805] | 188 | CountedPtr< Scantable > STMath::getScantable(const CountedPtr< Scantable >& in, |
---|
| 189 | bool droprows) |
---|
[185] | 190 | { |
---|
[805] | 191 | if (insitu_) return in; |
---|
| 192 | else { |
---|
| 193 | // clone |
---|
| 194 | Scantable* tabp = new Scantable(*in, Bool(droprows)); |
---|
| 195 | return CountedPtr<Scantable>(tabp); |
---|
[234] | 196 | } |
---|
[805] | 197 | } |
---|
[234] | 198 | |
---|
[805] | 199 | CountedPtr< Scantable > STMath::unaryOperate( const CountedPtr< Scantable >& in, |
---|
| 200 | float val, |
---|
| 201 | const std::string& mode, |
---|
| 202 | bool tsys ) |
---|
| 203 | { |
---|
| 204 | // modes are "ADD" and "MUL" |
---|
| 205 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 206 | Table& tab = out->table(); |
---|
| 207 | ArrayColumn<Float> specCol(tab,"SPECTRA"); |
---|
| 208 | ArrayColumn<Float> tsysCol(tab,"TSYS"); |
---|
| 209 | for (uInt i=0; i<tab.nrow(); ++i) { |
---|
| 210 | Vector<Float> spec; |
---|
| 211 | Vector<Float> ts; |
---|
| 212 | specCol.get(i, spec); |
---|
| 213 | tsysCol.get(i, ts); |
---|
| 214 | if (mode == "MUL") { |
---|
| 215 | spec *= val; |
---|
| 216 | specCol.put(i, spec); |
---|
| 217 | if ( tsys ) { |
---|
| 218 | ts *= val; |
---|
| 219 | tsysCol.put(i, ts); |
---|
| 220 | } |
---|
| 221 | } else if ( mode == "ADD" ) { |
---|
| 222 | spec += val; |
---|
| 223 | specCol.put(i, spec); |
---|
| 224 | if ( tsys ) { |
---|
| 225 | ts += val; |
---|
| 226 | tsysCol.put(i, ts); |
---|
| 227 | } |
---|
| 228 | } |
---|
[234] | 229 | } |
---|
[805] | 230 | return out; |
---|
| 231 | } |
---|
[234] | 232 | |
---|
[805] | 233 | MaskedArray<Float> STMath::maskedArray( const Vector<Float>& s, |
---|
| 234 | const Vector<uChar>& f) |
---|
| 235 | { |
---|
| 236 | Vector<Bool> mask; |
---|
| 237 | mask.resize(f.shape()); |
---|
| 238 | convertArray(mask, f); |
---|
| 239 | return MaskedArray<Float>(s,!mask); |
---|
| 240 | } |
---|
[248] | 241 | |
---|
[805] | 242 | Vector<uChar> STMath::flagsFromMA(const MaskedArray<Float>& ma) |
---|
| 243 | { |
---|
| 244 | const Vector<Bool>& m = ma.getMask(); |
---|
| 245 | Vector<uChar> flags(m.shape()); |
---|
| 246 | convertArray(flags, !m); |
---|
| 247 | return flags; |
---|
| 248 | } |
---|
[234] | 249 | |
---|
[805] | 250 | CountedPtr< Scantable > STMath::quotient( const CountedPtr< Scantable >& in, |
---|
| 251 | const std::string & mode, |
---|
| 252 | bool preserve ) |
---|
| 253 | { |
---|
| 254 | /// @todo make other modes available |
---|
| 255 | /// modes should be "nearest", "pair" |
---|
| 256 | // make this operation non insitu |
---|
| 257 | const Table& tin = in->table(); |
---|
| 258 | Table ons = tin(tin.col("SRCTYPE") == Int(0)); |
---|
| 259 | Table offs = tin(tin.col("SRCTYPE") == Int(1)); |
---|
| 260 | if ( offs.nrow() == 0 ) |
---|
| 261 | throw(AipsError("No 'off' scans present.")); |
---|
| 262 | // put all "on" scans into output table |
---|
[701] | 263 | |
---|
[805] | 264 | bool insitu = insitu_; |
---|
| 265 | setInsitu(false); |
---|
| 266 | CountedPtr< Scantable > out = getScantable(in, true); |
---|
| 267 | setInsitu(insitu); |
---|
| 268 | Table& tout = out->table(); |
---|
[248] | 269 | |
---|
[805] | 270 | TableCopy::copyRows(tout, ons); |
---|
| 271 | TableRow row(tout); |
---|
| 272 | ROScalarColumn<Double> offtimeCol(offs, "TIME"); |
---|
[234] | 273 | |
---|
[805] | 274 | ArrayColumn<Float> outspecCol(tout, "SPECTRA"); |
---|
| 275 | ROArrayColumn<Float> outtsysCol(tout, "TSYS"); |
---|
| 276 | ArrayColumn<uChar> outflagCol(tout, "FLAGTRA"); |
---|
| 277 | for (uInt i=0; i < tout.nrow(); ++i) { |
---|
| 278 | const TableRecord& rec = row.get(i); |
---|
| 279 | Double ontime = rec.asDouble("TIME"); |
---|
| 280 | ROScalarColumn<Double> offtimeCol(offs, "TIME"); |
---|
| 281 | Double mindeltat = min(abs(offtimeCol.getColumn() - ontime)); |
---|
| 282 | Table sel = offs( abs(offs.col("TIME")-ontime) <= mindeltat |
---|
| 283 | && offs.col("BEAMNO") == Int(rec.asuInt("BEAMNO")) |
---|
| 284 | && offs.col("IFNO") == Int(rec.asuInt("IFNO")) |
---|
| 285 | && offs.col("POLNO") == Int(rec.asuInt("POLNO")) ); |
---|
[780] | 286 | |
---|
[805] | 287 | TableRow offrow(sel); |
---|
| 288 | const TableRecord& offrec = offrow.get(0);//should only be one row |
---|
| 289 | RORecordFieldPtr< Array<Float> > specoff(offrec, "SPECTRA"); |
---|
| 290 | RORecordFieldPtr< Array<Float> > tsysoff(offrec, "TSYS"); |
---|
| 291 | RORecordFieldPtr< Array<uChar> > flagoff(offrec, "FLAGTRA"); |
---|
| 292 | /// @fixme this assumes tsys is a scalar not vector |
---|
| 293 | Float tsysoffscalar = (*tsysoff)(IPosition(1,0)); |
---|
| 294 | Vector<Float> specon, tsyson; |
---|
| 295 | outtsysCol.get(i, tsyson); |
---|
| 296 | outspecCol.get(i, specon); |
---|
| 297 | Vector<uChar> flagon; |
---|
| 298 | outflagCol.get(i, flagon); |
---|
| 299 | MaskedArray<Float> mon = maskedArray(specon, flagon); |
---|
| 300 | MaskedArray<Float> moff = maskedArray(*specoff, *flagoff); |
---|
| 301 | MaskedArray<Float> quot = (tsysoffscalar * mon / moff); |
---|
| 302 | if (preserve) { |
---|
| 303 | quot -= tsysoffscalar; |
---|
| 304 | } else { |
---|
| 305 | quot -= tsyson[0]; |
---|
[701] | 306 | } |
---|
[805] | 307 | outspecCol.put(i, quot.getArray()); |
---|
| 308 | outflagCol.put(i, flagsFromMA(quot)); |
---|
| 309 | } |
---|
[926] | 310 | // renumber scanno |
---|
| 311 | TableIterator it(tout, "SCANNO"); |
---|
| 312 | uInt i = 0; |
---|
| 313 | while ( !it.pastEnd() ) { |
---|
| 314 | Table t = it.table(); |
---|
| 315 | TableVector<uInt> vec(t, "SCANNO"); |
---|
| 316 | vec = i; |
---|
| 317 | ++i; |
---|
| 318 | ++it; |
---|
| 319 | } |
---|
[805] | 320 | return out; |
---|
| 321 | } |
---|
[234] | 322 | |
---|
[805] | 323 | CountedPtr< Scantable > STMath::freqSwitch( const CountedPtr< Scantable >& in ) |
---|
| 324 | { |
---|
| 325 | // make copy or reference |
---|
| 326 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 327 | Table& tout = out->table(); |
---|
[1008] | 328 | Block<String> cols(4); |
---|
[805] | 329 | cols[0] = String("SCANNO"); |
---|
[1008] | 330 | cols[1] = String("CYCLENO"); |
---|
| 331 | cols[2] = String("BEAMNO"); |
---|
| 332 | cols[3] = String("POLNO"); |
---|
[805] | 333 | TableIterator iter(tout, cols); |
---|
| 334 | while (!iter.pastEnd()) { |
---|
| 335 | Table subt = iter.table(); |
---|
| 336 | // this should leave us with two rows for the two IFs....if not ignore |
---|
| 337 | if (subt.nrow() != 2 ) { |
---|
| 338 | continue; |
---|
[701] | 339 | } |
---|
[1008] | 340 | ArrayColumn<Float> specCol(subt, "SPECTRA"); |
---|
| 341 | ArrayColumn<Float> tsysCol(subt, "TSYS"); |
---|
| 342 | ArrayColumn<uChar> flagCol(subt, "FLAGTRA"); |
---|
[805] | 343 | Vector<Float> onspec,offspec, ontsys, offtsys; |
---|
| 344 | Vector<uChar> onflag, offflag; |
---|
| 345 | tsysCol.get(0, ontsys); tsysCol.get(1, offtsys); |
---|
| 346 | specCol.get(0, onspec); specCol.get(1, offspec); |
---|
| 347 | flagCol.get(0, onflag); flagCol.get(1, offflag); |
---|
| 348 | MaskedArray<Float> on = maskedArray(onspec, onflag); |
---|
| 349 | MaskedArray<Float> off = maskedArray(offspec, offflag); |
---|
| 350 | MaskedArray<Float> oncopy = on.copy(); |
---|
[248] | 351 | |
---|
[805] | 352 | on /= off; on -= 1.0f; |
---|
| 353 | on *= ontsys[0]; |
---|
| 354 | off /= oncopy; off -= 1.0f; |
---|
| 355 | off *= offtsys[0]; |
---|
| 356 | specCol.put(0, on.getArray()); |
---|
| 357 | const Vector<Bool>& m0 = on.getMask(); |
---|
| 358 | Vector<uChar> flags0(m0.shape()); |
---|
| 359 | convertArray(flags0, !m0); |
---|
| 360 | flagCol.put(0, flags0); |
---|
[234] | 361 | |
---|
[805] | 362 | specCol.put(1, off.getArray()); |
---|
| 363 | const Vector<Bool>& m1 = off.getMask(); |
---|
| 364 | Vector<uChar> flags1(m1.shape()); |
---|
| 365 | convertArray(flags1, !m1); |
---|
| 366 | flagCol.put(1, flags1); |
---|
[867] | 367 | ++iter; |
---|
[130] | 368 | } |
---|
[780] | 369 | |
---|
[805] | 370 | return out; |
---|
[9] | 371 | } |
---|
[48] | 372 | |
---|
[805] | 373 | std::vector< float > STMath::statistic( const CountedPtr< Scantable > & in, |
---|
| 374 | const std::vector< bool > & mask, |
---|
| 375 | const std::string& which ) |
---|
[130] | 376 | { |
---|
| 377 | |
---|
[805] | 378 | Vector<Bool> m(mask); |
---|
| 379 | const Table& tab = in->table(); |
---|
| 380 | ROArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 381 | ROArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 382 | std::vector<float> out; |
---|
| 383 | for (uInt i=0; i < tab.nrow(); ++i ) { |
---|
| 384 | Vector<Float> spec; specCol.get(i, spec); |
---|
[867] | 385 | Vector<uChar> flag; flagCol.get(i, flag); |
---|
| 386 | MaskedArray<Float> ma = maskedArray(spec, flag); |
---|
| 387 | float outstat = 0.0; |
---|
[805] | 388 | if ( spec.nelements() == m.nelements() ) { |
---|
| 389 | outstat = mathutil::statistics(which, ma(m)); |
---|
| 390 | } else { |
---|
| 391 | outstat = mathutil::statistics(which, ma); |
---|
| 392 | } |
---|
| 393 | out.push_back(outstat); |
---|
[234] | 394 | } |
---|
[805] | 395 | return out; |
---|
[130] | 396 | } |
---|
| 397 | |
---|
[805] | 398 | CountedPtr< Scantable > STMath::bin( const CountedPtr< Scantable > & in, |
---|
| 399 | int width ) |
---|
[144] | 400 | { |
---|
[841] | 401 | if ( !in->getSelection().empty() ) throw(AipsError("Can't bin subset of the data.")); |
---|
[805] | 402 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 403 | Table& tout = out->table(); |
---|
| 404 | out->frequencies().rescale(width, "BIN"); |
---|
| 405 | ArrayColumn<Float> specCol(tout, "SPECTRA"); |
---|
| 406 | ArrayColumn<uChar> flagCol(tout, "FLAGTRA"); |
---|
| 407 | for (uInt i=0; i < tout.nrow(); ++i ) { |
---|
| 408 | MaskedArray<Float> main = maskedArray(specCol(i), flagCol(i)); |
---|
| 409 | MaskedArray<Float> maout; |
---|
| 410 | LatticeUtilities::bin(maout, main, 0, Int(width)); |
---|
| 411 | /// @todo implement channel based tsys binning |
---|
| 412 | specCol.put(i, maout.getArray()); |
---|
| 413 | flagCol.put(i, flagsFromMA(maout)); |
---|
| 414 | // take only the first binned spectrum's length for the deprecated |
---|
| 415 | // global header item nChan |
---|
| 416 | if (i==0) tout.rwKeywordSet().define(String("nChan"), |
---|
| 417 | Int(maout.getArray().nelements())); |
---|
[169] | 418 | } |
---|
[805] | 419 | return out; |
---|
[146] | 420 | } |
---|
| 421 | |
---|
[805] | 422 | CountedPtr< Scantable > STMath::resample( const CountedPtr< Scantable >& in, |
---|
| 423 | const std::string& method, |
---|
| 424 | float width ) |
---|
[299] | 425 | // |
---|
| 426 | // Should add the possibility of width being specified in km/s. This means |
---|
[780] | 427 | // that for each freqID (SpectralCoordinate) we will need to convert to an |
---|
| 428 | // average channel width (say at the reference pixel). Then we would need |
---|
| 429 | // to be careful to make sure each spectrum (of different freqID) |
---|
[299] | 430 | // is the same length. |
---|
| 431 | // |
---|
| 432 | { |
---|
[996] | 433 | //InterpolateArray1D<Double,Float>::InterpolationMethod interp; |
---|
[805] | 434 | Int interpMethod(stringToIMethod(method)); |
---|
[299] | 435 | |
---|
[805] | 436 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 437 | Table& tout = out->table(); |
---|
[299] | 438 | |
---|
| 439 | // Resample SpectralCoordinates (one per freqID) |
---|
[805] | 440 | out->frequencies().rescale(width, "RESAMPLE"); |
---|
| 441 | TableIterator iter(tout, "IFNO"); |
---|
| 442 | TableRow row(tout); |
---|
| 443 | while ( !iter.pastEnd() ) { |
---|
| 444 | Table tab = iter.table(); |
---|
| 445 | ArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 446 | //ArrayColumn<Float> tsysCol(tout, "TSYS"); |
---|
| 447 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 448 | Vector<Float> spec; |
---|
| 449 | Vector<uChar> flag; |
---|
| 450 | specCol.get(0,spec); // the number of channels should be constant per IF |
---|
| 451 | uInt nChanIn = spec.nelements(); |
---|
| 452 | Vector<Float> xIn(nChanIn); indgen(xIn); |
---|
| 453 | Int fac = Int(nChanIn/width); |
---|
| 454 | Vector<Float> xOut(fac+10); // 10 to be safe - resize later |
---|
| 455 | uInt k = 0; |
---|
| 456 | Float x = 0.0; |
---|
| 457 | while (x < Float(nChanIn) ) { |
---|
| 458 | xOut(k) = x; |
---|
| 459 | k++; |
---|
| 460 | x += width; |
---|
| 461 | } |
---|
| 462 | uInt nChanOut = k; |
---|
| 463 | xOut.resize(nChanOut, True); |
---|
| 464 | // process all rows for this IFNO |
---|
| 465 | Vector<Float> specOut; |
---|
| 466 | Vector<Bool> maskOut; |
---|
| 467 | Vector<uChar> flagOut; |
---|
| 468 | for (uInt i=0; i < tab.nrow(); ++i) { |
---|
| 469 | specCol.get(i, spec); |
---|
| 470 | flagCol.get(i, flag); |
---|
| 471 | Vector<Bool> mask(flag.nelements()); |
---|
| 472 | convertArray(mask, flag); |
---|
[299] | 473 | |
---|
[805] | 474 | IPosition shapeIn(spec.shape()); |
---|
| 475 | //sh.nchan = nChanOut; |
---|
| 476 | InterpolateArray1D<Float,Float>::interpolate(specOut, maskOut, xOut, |
---|
| 477 | xIn, spec, mask, |
---|
| 478 | interpMethod, True, True); |
---|
| 479 | /// @todo do the same for channel based Tsys |
---|
| 480 | flagOut.resize(maskOut.nelements()); |
---|
| 481 | convertArray(flagOut, maskOut); |
---|
| 482 | specCol.put(i, specOut); |
---|
| 483 | flagCol.put(i, flagOut); |
---|
| 484 | } |
---|
| 485 | ++iter; |
---|
[299] | 486 | } |
---|
| 487 | |
---|
[805] | 488 | return out; |
---|
| 489 | } |
---|
[299] | 490 | |
---|
[805] | 491 | STMath::imethod STMath::stringToIMethod(const std::string& in) |
---|
| 492 | { |
---|
| 493 | static STMath::imap lookup; |
---|
[299] | 494 | |
---|
[805] | 495 | // initialize the lookup table if necessary |
---|
| 496 | if ( lookup.empty() ) { |
---|
[926] | 497 | lookup["nearest"] = InterpolateArray1D<Double,Float>::nearestNeighbour; |
---|
| 498 | lookup["linear"] = InterpolateArray1D<Double,Float>::linear; |
---|
| 499 | lookup["cubic"] = InterpolateArray1D<Double,Float>::cubic; |
---|
| 500 | lookup["spline"] = InterpolateArray1D<Double,Float>::spline; |
---|
[299] | 501 | } |
---|
| 502 | |
---|
[805] | 503 | STMath::imap::const_iterator iter = lookup.find(in); |
---|
[299] | 504 | |
---|
[805] | 505 | if ( lookup.end() == iter ) { |
---|
| 506 | std::string message = in; |
---|
| 507 | message += " is not a valid interpolation mode"; |
---|
| 508 | throw(AipsError(message)); |
---|
[299] | 509 | } |
---|
[805] | 510 | return iter->second; |
---|
[299] | 511 | } |
---|
| 512 | |
---|
[805] | 513 | WeightType STMath::stringToWeight(const std::string& in) |
---|
[146] | 514 | { |
---|
[805] | 515 | static std::map<std::string, WeightType> lookup; |
---|
[434] | 516 | |
---|
[805] | 517 | // initialize the lookup table if necessary |
---|
| 518 | if ( lookup.empty() ) { |
---|
| 519 | lookup["NONE"] = asap::NONE; |
---|
| 520 | lookup["TINT"] = asap::TINT; |
---|
| 521 | lookup["TINTSYS"] = asap::TINTSYS; |
---|
| 522 | lookup["TSYS"] = asap::TSYS; |
---|
| 523 | lookup["VAR"] = asap::VAR; |
---|
| 524 | } |
---|
[434] | 525 | |
---|
[805] | 526 | std::map<std::string, WeightType>::const_iterator iter = lookup.find(in); |
---|
[294] | 527 | |
---|
[805] | 528 | if ( lookup.end() == iter ) { |
---|
| 529 | std::string message = in; |
---|
| 530 | message += " is not a valid weighting mode"; |
---|
| 531 | throw(AipsError(message)); |
---|
| 532 | } |
---|
| 533 | return iter->second; |
---|
[146] | 534 | } |
---|
| 535 | |
---|
[805] | 536 | CountedPtr< Scantable > STMath::gainElevation( const CountedPtr< Scantable >& in, |
---|
[867] | 537 | const vector< float > & coeff, |
---|
[805] | 538 | const std::string & filename, |
---|
| 539 | const std::string& method) |
---|
[165] | 540 | { |
---|
[805] | 541 | // Get elevation data from Scantable and convert to degrees |
---|
| 542 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
[926] | 543 | Table& tab = out->table(); |
---|
[805] | 544 | ROScalarColumn<Float> elev(tab, "ELEVATION"); |
---|
| 545 | Vector<Float> x = elev.getColumn(); |
---|
| 546 | x *= Float(180 / C::pi); // Degrees |
---|
[165] | 547 | |
---|
[867] | 548 | Vector<Float> coeffs(coeff); |
---|
[805] | 549 | const uInt nc = coeffs.nelements(); |
---|
| 550 | if ( filename.length() > 0 && nc > 0 ) { |
---|
| 551 | throw(AipsError("You must choose either polynomial coefficients or an ascii file, not both")); |
---|
[315] | 552 | } |
---|
[165] | 553 | |
---|
[805] | 554 | // Correct |
---|
| 555 | if ( nc > 0 || filename.length() == 0 ) { |
---|
| 556 | // Find instrument |
---|
| 557 | Bool throwit = True; |
---|
| 558 | Instrument inst = |
---|
[878] | 559 | STAttr::convertInstrument(tab.keywordSet().asString("AntennaName"), |
---|
[805] | 560 | throwit); |
---|
[165] | 561 | |
---|
[805] | 562 | // Set polynomial |
---|
| 563 | Polynomial<Float>* ppoly = 0; |
---|
| 564 | Vector<Float> coeff; |
---|
| 565 | String msg; |
---|
| 566 | if ( nc > 0 ) { |
---|
| 567 | ppoly = new Polynomial<Float>(nc); |
---|
| 568 | coeff = coeffs; |
---|
| 569 | msg = String("user"); |
---|
| 570 | } else { |
---|
[878] | 571 | STAttr sdAttr; |
---|
[805] | 572 | coeff = sdAttr.gainElevationPoly(inst); |
---|
| 573 | ppoly = new Polynomial<Float>(3); |
---|
| 574 | msg = String("built in"); |
---|
| 575 | } |
---|
[532] | 576 | |
---|
[805] | 577 | if ( coeff.nelements() > 0 ) { |
---|
| 578 | ppoly->setCoefficients(coeff); |
---|
| 579 | } else { |
---|
| 580 | delete ppoly; |
---|
| 581 | throw(AipsError("There is no known gain-elevation polynomial known for this instrument")); |
---|
| 582 | } |
---|
| 583 | ostringstream oss; |
---|
| 584 | oss << "Making polynomial correction with " << msg << " coefficients:" << endl; |
---|
| 585 | oss << " " << coeff; |
---|
| 586 | pushLog(String(oss)); |
---|
| 587 | const uInt nrow = tab.nrow(); |
---|
| 588 | Vector<Float> factor(nrow); |
---|
| 589 | for ( uInt i=0; i < nrow; ++i ) { |
---|
| 590 | factor[i] = 1.0 / (*ppoly)(x[i]); |
---|
| 591 | } |
---|
| 592 | delete ppoly; |
---|
| 593 | scaleByVector(tab, factor, true); |
---|
[532] | 594 | |
---|
[805] | 595 | } else { |
---|
| 596 | // Read and correct |
---|
| 597 | pushLog("Making correction from ascii Table"); |
---|
| 598 | scaleFromAsciiTable(tab, filename, method, x, true); |
---|
[532] | 599 | } |
---|
[805] | 600 | return out; |
---|
| 601 | } |
---|
[165] | 602 | |
---|
[805] | 603 | void STMath::scaleFromAsciiTable(Table& in, const std::string& filename, |
---|
| 604 | const std::string& method, |
---|
| 605 | const Vector<Float>& xout, bool dotsys) |
---|
| 606 | { |
---|
[165] | 607 | |
---|
[805] | 608 | // Read gain-elevation ascii file data into a Table. |
---|
[165] | 609 | |
---|
[805] | 610 | String formatString; |
---|
| 611 | Table tbl = readAsciiTable(formatString, Table::Memory, filename, "", "", False); |
---|
| 612 | scaleFromTable(in, tbl, method, xout, dotsys); |
---|
| 613 | } |
---|
[165] | 614 | |
---|
[805] | 615 | void STMath::scaleFromTable(Table& in, |
---|
| 616 | const Table& table, |
---|
| 617 | const std::string& method, |
---|
| 618 | const Vector<Float>& xout, bool dotsys) |
---|
| 619 | { |
---|
[780] | 620 | |
---|
[805] | 621 | ROScalarColumn<Float> geElCol(table, "ELEVATION"); |
---|
| 622 | ROScalarColumn<Float> geFacCol(table, "FACTOR"); |
---|
| 623 | Vector<Float> xin = geElCol.getColumn(); |
---|
| 624 | Vector<Float> yin = geFacCol.getColumn(); |
---|
| 625 | Vector<Bool> maskin(xin.nelements(),True); |
---|
[165] | 626 | |
---|
[805] | 627 | // Interpolate (and extrapolate) with desired method |
---|
[532] | 628 | |
---|
[996] | 629 | InterpolateArray1D<Double,Float>::InterpolationMethod interp = stringToIMethod(method); |
---|
[165] | 630 | |
---|
[805] | 631 | Vector<Float> yout; |
---|
| 632 | Vector<Bool> maskout; |
---|
| 633 | InterpolateArray1D<Float,Float>::interpolate(yout, maskout, xout, |
---|
[996] | 634 | xin, yin, maskin, interp, |
---|
[805] | 635 | True, True); |
---|
[165] | 636 | |
---|
[805] | 637 | scaleByVector(in, Float(1.0)/yout, dotsys); |
---|
[165] | 638 | } |
---|
[167] | 639 | |
---|
[805] | 640 | void STMath::scaleByVector( Table& in, |
---|
| 641 | const Vector< Float >& factor, |
---|
| 642 | bool dotsys ) |
---|
[177] | 643 | { |
---|
[805] | 644 | uInt nrow = in.nrow(); |
---|
| 645 | if ( factor.nelements() != nrow ) { |
---|
| 646 | throw(AipsError("factors.nelements() != table.nelements()")); |
---|
| 647 | } |
---|
| 648 | ArrayColumn<Float> specCol(in, "SPECTRA"); |
---|
| 649 | ArrayColumn<uChar> flagCol(in, "FLAGTRA"); |
---|
| 650 | ArrayColumn<Float> tsysCol(in, "TSYS"); |
---|
| 651 | for (uInt i=0; i < nrow; ++i) { |
---|
| 652 | MaskedArray<Float> ma = maskedArray(specCol(i), flagCol(i)); |
---|
| 653 | ma *= factor[i]; |
---|
| 654 | specCol.put(i, ma.getArray()); |
---|
| 655 | flagCol.put(i, flagsFromMA(ma)); |
---|
| 656 | if ( dotsys ) { |
---|
[926] | 657 | Vector<Float> tsys = tsysCol(i); |
---|
[805] | 658 | tsys *= factor[i]; |
---|
[926] | 659 | tsysCol.put(i,tsys); |
---|
[805] | 660 | } |
---|
| 661 | } |
---|
[177] | 662 | } |
---|
| 663 | |
---|
[805] | 664 | CountedPtr< Scantable > STMath::convertFlux( const CountedPtr< Scantable >& in, |
---|
| 665 | float d, float etaap, |
---|
| 666 | float jyperk ) |
---|
[221] | 667 | { |
---|
[805] | 668 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 669 | Table& tab = in->table(); |
---|
| 670 | Unit fluxUnit(tab.keywordSet().asString("FluxUnit")); |
---|
[221] | 671 | Unit K(String("K")); |
---|
| 672 | Unit JY(String("Jy")); |
---|
[701] | 673 | |
---|
[805] | 674 | bool tokelvin = true; |
---|
| 675 | Double cfac = 1.0; |
---|
[716] | 676 | |
---|
[805] | 677 | if ( fluxUnit == JY ) { |
---|
[716] | 678 | pushLog("Converting to K"); |
---|
[701] | 679 | Quantum<Double> t(1.0,fluxUnit); |
---|
| 680 | Quantum<Double> t2 = t.get(JY); |
---|
[805] | 681 | cfac = (t2 / t).getValue(); // value to Jy |
---|
[780] | 682 | |
---|
[805] | 683 | tokelvin = true; |
---|
| 684 | out->setFluxUnit("K"); |
---|
| 685 | } else if ( fluxUnit == K ) { |
---|
[716] | 686 | pushLog("Converting to Jy"); |
---|
[701] | 687 | Quantum<Double> t(1.0,fluxUnit); |
---|
| 688 | Quantum<Double> t2 = t.get(K); |
---|
[805] | 689 | cfac = (t2 / t).getValue(); // value to K |
---|
[780] | 690 | |
---|
[805] | 691 | tokelvin = false; |
---|
| 692 | out->setFluxUnit("Jy"); |
---|
[221] | 693 | } else { |
---|
[701] | 694 | throw(AipsError("Unrecognized brightness units in Table - must be consistent with Jy or K")); |
---|
[221] | 695 | } |
---|
[701] | 696 | // Make sure input values are converted to either Jy or K first... |
---|
[805] | 697 | Float factor = cfac; |
---|
[221] | 698 | |
---|
[701] | 699 | // Select method |
---|
[805] | 700 | if (jyperk > 0.0) { |
---|
| 701 | factor *= jyperk; |
---|
| 702 | if ( tokelvin ) factor = 1.0 / jyperk; |
---|
[716] | 703 | ostringstream oss; |
---|
[805] | 704 | oss << "Jy/K = " << jyperk; |
---|
[716] | 705 | pushLog(String(oss)); |
---|
[805] | 706 | Vector<Float> factors(tab.nrow(), factor); |
---|
| 707 | scaleByVector(tab,factors, false); |
---|
| 708 | } else if ( etaap > 0.0) { |
---|
| 709 | Instrument inst = |
---|
[878] | 710 | STAttr::convertInstrument(tab.keywordSet().asString("AntennaName"), True); |
---|
| 711 | STAttr sda; |
---|
[805] | 712 | if (d < 0) d = sda.diameter(inst); |
---|
[996] | 713 | jyperk = STAttr::findJyPerK(etaap, d); |
---|
[716] | 714 | ostringstream oss; |
---|
[805] | 715 | oss << "Jy/K = " << jyperk; |
---|
[716] | 716 | pushLog(String(oss)); |
---|
[805] | 717 | factor *= jyperk; |
---|
| 718 | if ( tokelvin ) { |
---|
[701] | 719 | factor = 1.0 / factor; |
---|
| 720 | } |
---|
[805] | 721 | Vector<Float> factors(tab.nrow(), factor); |
---|
| 722 | scaleByVector(tab, factors, False); |
---|
[354] | 723 | } else { |
---|
[780] | 724 | |
---|
[701] | 725 | // OK now we must deal with automatic look up of values. |
---|
| 726 | // We must also deal with the fact that the factors need |
---|
| 727 | // to be computed per IF and may be different and may |
---|
| 728 | // change per integration. |
---|
[780] | 729 | |
---|
[716] | 730 | pushLog("Looking up conversion factors"); |
---|
[805] | 731 | convertBrightnessUnits(out, tokelvin, cfac); |
---|
[701] | 732 | } |
---|
[805] | 733 | |
---|
| 734 | return out; |
---|
[221] | 735 | } |
---|
| 736 | |
---|
[805] | 737 | void STMath::convertBrightnessUnits( CountedPtr<Scantable>& in, |
---|
| 738 | bool tokelvin, float cfac ) |
---|
[227] | 739 | { |
---|
[805] | 740 | Table& table = in->table(); |
---|
| 741 | Instrument inst = |
---|
[878] | 742 | STAttr::convertInstrument(table.keywordSet().asString("AntennaName"), True); |
---|
[805] | 743 | TableIterator iter(table, "FREQ_ID"); |
---|
| 744 | STFrequencies stfreqs = in->frequencies(); |
---|
[878] | 745 | STAttr sdAtt; |
---|
[805] | 746 | while (!iter.pastEnd()) { |
---|
| 747 | Table tab = iter.table(); |
---|
| 748 | ArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 749 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 750 | ROScalarColumn<uInt> freqidCol(tab, "FREQ_ID"); |
---|
| 751 | MEpoch::ROScalarColumn timeCol(tab, "TIME"); |
---|
[234] | 752 | |
---|
[805] | 753 | uInt freqid; freqidCol.get(0, freqid); |
---|
| 754 | Vector<Float> tmpspec; specCol.get(0, tmpspec); |
---|
[878] | 755 | // STAttr.JyPerK has a Vector interface... change sometime. |
---|
[805] | 756 | Vector<Float> freqs(1,stfreqs.getRefFreq(freqid, tmpspec.nelements())); |
---|
| 757 | for ( uInt i=0; i<tab.nrow(); ++i) { |
---|
| 758 | Float jyperk = (sdAtt.JyPerK(inst, timeCol(i), freqs))[0]; |
---|
| 759 | Float factor = cfac * jyperk; |
---|
| 760 | if ( tokelvin ) factor = Float(1.0) / factor; |
---|
| 761 | MaskedArray<Float> ma = maskedArray(specCol(i), flagCol(i)); |
---|
| 762 | ma *= factor; |
---|
| 763 | specCol.put(i, ma.getArray()); |
---|
| 764 | flagCol.put(i, flagsFromMA(ma)); |
---|
| 765 | } |
---|
[867] | 766 | ++iter; |
---|
[234] | 767 | } |
---|
[230] | 768 | } |
---|
[227] | 769 | |
---|
[805] | 770 | CountedPtr< Scantable > STMath::opacity( const CountedPtr< Scantable > & in, |
---|
| 771 | float tau ) |
---|
[234] | 772 | { |
---|
[805] | 773 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
[926] | 774 | |
---|
| 775 | Table tab = out->table(); |
---|
[234] | 776 | ROScalarColumn<Float> elev(tab, "ELEVATION"); |
---|
[805] | 777 | ArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 778 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 779 | for ( uInt i=0; i<tab.nrow(); ++i) { |
---|
| 780 | Float zdist = Float(C::pi_2) - elev(i); |
---|
| 781 | Float factor = exp(tau)/cos(zdist); |
---|
[926] | 782 | MaskedArray<Float> ma = maskedArray(specCol(i), flagCol(i)); |
---|
[805] | 783 | ma *= factor; |
---|
| 784 | specCol.put(i, ma.getArray()); |
---|
| 785 | flagCol.put(i, flagsFromMA(ma)); |
---|
[234] | 786 | } |
---|
[805] | 787 | return out; |
---|
[234] | 788 | } |
---|
| 789 | |
---|
[805] | 790 | CountedPtr< Scantable > STMath::smooth( const CountedPtr< Scantable >& in, |
---|
| 791 | const std::string& kernel, float width ) |
---|
[457] | 792 | { |
---|
[805] | 793 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 794 | Table& table = in->table(); |
---|
| 795 | VectorKernel::KernelTypes type = VectorKernel::toKernelType(kernel); |
---|
| 796 | // same IFNO should have same no of channels |
---|
| 797 | // this saves overhead |
---|
| 798 | TableIterator iter(table, "IFNO"); |
---|
| 799 | while (!iter.pastEnd()) { |
---|
| 800 | Table tab = iter.table(); |
---|
| 801 | ArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 802 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 803 | Vector<Float> tmpspec; specCol.get(0, tmpspec); |
---|
| 804 | uInt nchan = tmpspec.nelements(); |
---|
| 805 | Vector<Float> kvec = VectorKernel::make(type, width, nchan, True, False); |
---|
| 806 | Convolver<Float> conv(kvec, IPosition(1,nchan)); |
---|
| 807 | Vector<Float> spec; |
---|
| 808 | Vector<uChar> flag; |
---|
| 809 | for ( uInt i=0; i<tab.nrow(); ++i) { |
---|
| 810 | specCol.get(i, spec); |
---|
| 811 | flagCol.get(i, flag); |
---|
| 812 | Vector<Bool> mask(flag.nelements()); |
---|
| 813 | convertArray(mask, flag); |
---|
| 814 | Vector<Float> specout; |
---|
| 815 | if ( type == VectorKernel::HANNING ) { |
---|
| 816 | Vector<Bool> maskout; |
---|
| 817 | mathutil::hanning(specout, maskout, spec , mask); |
---|
| 818 | convertArray(flag, maskout); |
---|
| 819 | flagCol.put(i, flag); |
---|
| 820 | } else { |
---|
| 821 | mathutil::replaceMaskByZero(specout, mask); |
---|
| 822 | conv.linearConv(specout, spec); |
---|
[354] | 823 | } |
---|
[805] | 824 | specCol.put(i, specout); |
---|
| 825 | } |
---|
[867] | 826 | ++iter; |
---|
[701] | 827 | } |
---|
[805] | 828 | return out; |
---|
[701] | 829 | } |
---|
[841] | 830 | |
---|
| 831 | CountedPtr< Scantable > |
---|
| 832 | STMath::merge( const std::vector< CountedPtr < Scantable > >& in ) |
---|
| 833 | { |
---|
| 834 | if ( in.size() < 2 ) { |
---|
[862] | 835 | throw(AipsError("Need at least two scantables to perform a merge.")); |
---|
[841] | 836 | } |
---|
| 837 | std::vector<CountedPtr < Scantable > >::const_iterator it = in.begin(); |
---|
| 838 | bool insitu = insitu_; |
---|
| 839 | setInsitu(false); |
---|
[862] | 840 | CountedPtr< Scantable > out = getScantable(*it, false); |
---|
[841] | 841 | setInsitu(insitu); |
---|
| 842 | Table& tout = out->table(); |
---|
| 843 | ScalarColumn<uInt> freqidcol(tout,"FREQ_ID"), molidcol(tout, "MOLECULE_ID"); |
---|
[917] | 844 | ScalarColumn<uInt> scannocol(tout,"SCANNO"), focusidcol(tout,"FOCUS_ID"); |
---|
| 845 | // Renumber SCANNO to be 0-based |
---|
[926] | 846 | Vector<uInt> scannos = scannocol.getColumn(); |
---|
| 847 | uInt offset = min(scannos); |
---|
[917] | 848 | scannos -= offset; |
---|
[926] | 849 | scannocol.putColumn(scannos); |
---|
| 850 | uInt newscanno = max(scannos)+1; |
---|
[862] | 851 | ++it; |
---|
[841] | 852 | while ( it != in.end() ){ |
---|
| 853 | if ( ! (*it)->conformant(*out) ) { |
---|
| 854 | // log message: "ignoring scantable i, as it isn't |
---|
| 855 | // conformant with the other(s)" |
---|
| 856 | cerr << "oh oh" << endl; |
---|
| 857 | ++it; |
---|
| 858 | continue; |
---|
| 859 | } |
---|
[862] | 860 | out->appendToHistoryTable((*it)->history()); |
---|
[841] | 861 | const Table& tab = (*it)->table(); |
---|
| 862 | TableIterator scanit(tab, "SCANNO"); |
---|
| 863 | while (!scanit.pastEnd()) { |
---|
| 864 | TableIterator freqit(scanit.table(), "FREQ_ID"); |
---|
| 865 | while ( !freqit.pastEnd() ) { |
---|
| 866 | Table thetab = freqit.table(); |
---|
| 867 | uInt nrow = tout.nrow(); |
---|
| 868 | //tout.addRow(thetab.nrow()); |
---|
| 869 | TableCopy::copyRows(tout, thetab, nrow, 0, thetab.nrow()); |
---|
| 870 | ROTableRow row(thetab); |
---|
| 871 | for ( uInt i=0; i<thetab.nrow(); ++i) { |
---|
| 872 | uInt k = nrow+i; |
---|
| 873 | scannocol.put(k, newscanno); |
---|
| 874 | const TableRecord& rec = row.get(i); |
---|
| 875 | Double rv,rp,inc; |
---|
| 876 | (*it)->frequencies().getEntry(rp, rv, inc, rec.asuInt("FREQ_ID")); |
---|
| 877 | uInt id; |
---|
| 878 | id = out->frequencies().addEntry(rp, rv, inc); |
---|
| 879 | freqidcol.put(k,id); |
---|
| 880 | String name,fname;Double rf; |
---|
| 881 | (*it)->molecules().getEntry(rf, name, fname, rec.asuInt("MOLECULE_ID")); |
---|
| 882 | id = out->molecules().addEntry(rf, name, fname); |
---|
| 883 | molidcol.put(k, id); |
---|
[961] | 884 | Float frot,fax,ftan,fhand,fmount,fuser, fxy, fxyp; |
---|
| 885 | (*it)->focus().getEntry(fax, ftan, frot, fhand, |
---|
| 886 | fmount,fuser, fxy, fxyp, |
---|
| 887 | rec.asuInt("FOCUS_ID")); |
---|
| 888 | id = out->focus().addEntry(fax, ftan, frot, fhand, |
---|
| 889 | fmount,fuser, fxy, fxyp); |
---|
[841] | 890 | focusidcol.put(k, id); |
---|
| 891 | } |
---|
| 892 | ++freqit; |
---|
| 893 | } |
---|
| 894 | ++newscanno; |
---|
| 895 | ++scanit; |
---|
| 896 | } |
---|
| 897 | ++it; |
---|
| 898 | } |
---|
| 899 | return out; |
---|
| 900 | } |
---|
[896] | 901 | |
---|
| 902 | CountedPtr< Scantable > |
---|
| 903 | STMath::invertPhase( const CountedPtr < Scantable >& in ) |
---|
| 904 | { |
---|
[996] | 905 | return applyToPol(in, &STPol::invertPhase, Float(0.0)); |
---|
[896] | 906 | } |
---|
| 907 | |
---|
| 908 | CountedPtr< Scantable > |
---|
| 909 | STMath::rotateXYPhase( const CountedPtr < Scantable >& in, float phase ) |
---|
| 910 | { |
---|
| 911 | return applyToPol(in, &STPol::rotatePhase, Float(phase)); |
---|
| 912 | } |
---|
| 913 | |
---|
| 914 | CountedPtr< Scantable > |
---|
| 915 | STMath::rotateLinPolPhase( const CountedPtr < Scantable >& in, float phase ) |
---|
| 916 | { |
---|
| 917 | return applyToPol(in, &STPol::rotateLinPolPhase, Float(phase)); |
---|
| 918 | } |
---|
| 919 | |
---|
| 920 | CountedPtr< Scantable > STMath::applyToPol( const CountedPtr<Scantable>& in, |
---|
| 921 | STPol::polOperation fptr, |
---|
| 922 | Float phase ) |
---|
| 923 | { |
---|
| 924 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 925 | Table& tout = out->table(); |
---|
| 926 | Block<String> cols(4); |
---|
| 927 | cols[0] = String("SCANNO"); |
---|
| 928 | cols[1] = String("BEAMNO"); |
---|
| 929 | cols[2] = String("IFNO"); |
---|
| 930 | cols[3] = String("CYCLENO"); |
---|
| 931 | TableIterator iter(tout, cols); |
---|
[1015] | 932 | STPol* stpol = STPol::getPolClass(out->factories_, out->getPolType() ); |
---|
[896] | 933 | while (!iter.pastEnd()) { |
---|
| 934 | Table t = iter.table(); |
---|
| 935 | ArrayColumn<Float> speccol(t, "SPECTRA"); |
---|
[1015] | 936 | ScalarColumn<uInt> focidcol(t, "FOCUS_ID"); |
---|
| 937 | ScalarColumn<Float> parancol(t, "PARANGLE"); |
---|
[896] | 938 | Matrix<Float> pols = speccol.getColumn(); |
---|
| 939 | try { |
---|
| 940 | stpol->setSpectra(pols); |
---|
[1015] | 941 | Float fang,fhand,parang; |
---|
| 942 | fang = in->focusTable_.getTotalFeedAngle(focidcol(0)); |
---|
| 943 | fhand = in->focusTable_.getFeedHand(focidcol(0)); |
---|
| 944 | parang = parancol(0); |
---|
| 945 | /// @todo re-enable this |
---|
| 946 | // disable total feed angle to support paralactifying Caswell style |
---|
| 947 | stpol->setPhaseCorrections(parang, -parang, fhand); |
---|
[896] | 948 | (stpol->*fptr)(phase); |
---|
| 949 | speccol.putColumn(stpol->getSpectra()); |
---|
[1015] | 950 | Matrix<Float> tmp = stpol->getSpectra(); |
---|
[896] | 951 | } catch (AipsError& e) { |
---|
| 952 | delete stpol;stpol=0; |
---|
| 953 | throw(e); |
---|
| 954 | } |
---|
| 955 | ++iter; |
---|
| 956 | } |
---|
[934] | 957 | delete stpol;stpol=0; |
---|
[896] | 958 | return out; |
---|
| 959 | } |
---|
| 960 | |
---|
| 961 | CountedPtr< Scantable > |
---|
| 962 | STMath::swapPolarisations( const CountedPtr< Scantable > & in ) |
---|
| 963 | { |
---|
| 964 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 965 | Table& tout = out->table(); |
---|
| 966 | Table t0 = tout(tout.col("POLNO") == 0); |
---|
| 967 | Table t1 = tout(tout.col("POLNO") == 1); |
---|
| 968 | if ( t0.nrow() != t1.nrow() ) |
---|
| 969 | throw(AipsError("Inconsistent number of polarisations")); |
---|
| 970 | ArrayColumn<Float> speccol0(t0, "SPECTRA"); |
---|
| 971 | ArrayColumn<uChar> flagcol0(t0, "FLAGTRA"); |
---|
| 972 | ArrayColumn<Float> speccol1(t1, "SPECTRA"); |
---|
| 973 | ArrayColumn<uChar> flagcol1(t1, "FLAGTRA"); |
---|
| 974 | Matrix<Float> s0 = speccol0.getColumn(); |
---|
| 975 | Matrix<uChar> f0 = flagcol0.getColumn(); |
---|
| 976 | speccol0.putColumn(speccol1.getColumn()); |
---|
| 977 | flagcol0.putColumn(flagcol1.getColumn()); |
---|
| 978 | speccol1.putColumn(s0); |
---|
| 979 | flagcol1.putColumn(f0); |
---|
| 980 | return out; |
---|
| 981 | } |
---|
[917] | 982 | |
---|
| 983 | CountedPtr< Scantable > |
---|
[940] | 984 | STMath::averagePolarisations( const CountedPtr< Scantable > & in, |
---|
| 985 | const std::vector<bool>& mask, |
---|
| 986 | const std::string& weight ) |
---|
| 987 | { |
---|
| 988 | if (in->getPolType() != "linear" || in->npol() != 2 ) |
---|
| 989 | throw(AipsError("averagePolarisations can only be applied to two linear polarisations.")); |
---|
[1010] | 990 | bool insitu = insitu_; |
---|
| 991 | setInsitu(false); |
---|
| 992 | CountedPtr< Scantable > pols = getScantable(in, false); |
---|
| 993 | setInsitu(insitu); |
---|
| 994 | Table& tout = pols->table(); |
---|
| 995 | // give all rows the same POLNO |
---|
| 996 | TableVector<uInt> vec(tout,"POLNO"); |
---|
[940] | 997 | vec = 0; |
---|
[1010] | 998 | pols->table_.rwKeywordSet().define("nPol",Int(1)); |
---|
| 999 | std::vector<CountedPtr<Scantable> > vpols; |
---|
| 1000 | vpols.push_back(pols); |
---|
| 1001 | CountedPtr< Scantable > out = average(vpols, mask, weight, "NONE"); |
---|
[940] | 1002 | return out; |
---|
| 1003 | } |
---|
| 1004 | |
---|
| 1005 | |
---|
| 1006 | CountedPtr< Scantable > |
---|
[917] | 1007 | asap::STMath::frequencyAlign( const CountedPtr< Scantable > & in, |
---|
| 1008 | const std::string & refTime, |
---|
[926] | 1009 | const std::string & method) |
---|
[917] | 1010 | { |
---|
[940] | 1011 | // clone as this is not working insitu |
---|
| 1012 | bool insitu = insitu_; |
---|
| 1013 | setInsitu(false); |
---|
[917] | 1014 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
[940] | 1015 | setInsitu(insitu); |
---|
[917] | 1016 | Table& tout = out->table(); |
---|
| 1017 | // Get reference Epoch to time of first row or given String |
---|
| 1018 | Unit DAY(String("d")); |
---|
| 1019 | MEpoch::Ref epochRef(in->getTimeReference()); |
---|
| 1020 | MEpoch refEpoch; |
---|
| 1021 | if (refTime.length()>0) { |
---|
| 1022 | Quantum<Double> qt; |
---|
| 1023 | if (MVTime::read(qt,refTime)) { |
---|
| 1024 | MVEpoch mv(qt); |
---|
| 1025 | refEpoch = MEpoch(mv, epochRef); |
---|
| 1026 | } else { |
---|
| 1027 | throw(AipsError("Invalid format for Epoch string")); |
---|
| 1028 | } |
---|
| 1029 | } else { |
---|
| 1030 | refEpoch = in->timeCol_(0); |
---|
| 1031 | } |
---|
| 1032 | MPosition refPos = in->getAntennaPosition(); |
---|
[940] | 1033 | |
---|
[996] | 1034 | InterpolateArray1D<Double,Float>::InterpolationMethod interp = stringToIMethod(method); |
---|
[917] | 1035 | // test if user frame is different to base frame |
---|
| 1036 | if ( in->frequencies().getFrameString(true) |
---|
| 1037 | == in->frequencies().getFrameString(false) ) { |
---|
[985] | 1038 | throw(AipsError("Can't convert as no output frame has been set" |
---|
| 1039 | " (use set_freqframe) or it is aligned already.")); |
---|
[917] | 1040 | } |
---|
| 1041 | MFrequency::Types system = in->frequencies().getFrame(); |
---|
[940] | 1042 | MVTime mvt(refEpoch.getValue()); |
---|
| 1043 | String epochout = mvt.string(MVTime::YMD) + String(" (") + refEpoch.getRefString() + String(")"); |
---|
| 1044 | ostringstream oss; |
---|
| 1045 | oss << "Aligned at reference Epoch " << epochout |
---|
| 1046 | << " in frame " << MFrequency::showType(system); |
---|
| 1047 | pushLog(String(oss)); |
---|
[917] | 1048 | // set up the iterator |
---|
[926] | 1049 | Block<String> cols(4); |
---|
| 1050 | // select by constant direction |
---|
[917] | 1051 | cols[0] = String("SRCNAME"); |
---|
| 1052 | cols[1] = String("BEAMNO"); |
---|
| 1053 | // select by IF ( no of channels varies over this ) |
---|
| 1054 | cols[2] = String("IFNO"); |
---|
[926] | 1055 | // select by restfrequency |
---|
| 1056 | cols[3] = String("MOLECULE_ID"); |
---|
[917] | 1057 | TableIterator iter(tout, cols); |
---|
[926] | 1058 | while ( !iter.pastEnd() ) { |
---|
[917] | 1059 | Table t = iter.table(); |
---|
| 1060 | MDirection::ROScalarColumn dirCol(t, "DIRECTION"); |
---|
[926] | 1061 | TableIterator fiter(t, "FREQ_ID"); |
---|
[917] | 1062 | // determine nchan from the first row. This should work as |
---|
[926] | 1063 | // we are iterating over BEAMNO and IFNO // we should have constant direction |
---|
| 1064 | |
---|
[917] | 1065 | ROArrayColumn<Float> sCol(t, "SPECTRA"); |
---|
[926] | 1066 | MDirection direction = dirCol(0); |
---|
[917] | 1067 | uInt nchan = sCol(0).nelements(); |
---|
[926] | 1068 | while ( !fiter.pastEnd() ) { |
---|
| 1069 | Table ftab = fiter.table(); |
---|
| 1070 | ScalarColumn<uInt> freqidCol(ftab, "FREQ_ID"); |
---|
| 1071 | // get the SpectralCoordinate for the freqid, which we are iterating over |
---|
| 1072 | SpectralCoordinate sC = in->frequencies().getSpectralCoordinate(freqidCol(0)); |
---|
| 1073 | FrequencyAligner<Float> fa( sC, nchan, refEpoch, |
---|
| 1074 | direction, refPos, system ); |
---|
| 1075 | // realign the SpectralCoordinate and put into the output Scantable |
---|
| 1076 | Vector<String> units(1); |
---|
| 1077 | units = String("Hz"); |
---|
| 1078 | Bool linear=True; |
---|
| 1079 | SpectralCoordinate sc2 = fa.alignedSpectralCoordinate(linear); |
---|
| 1080 | sc2.setWorldAxisUnits(units); |
---|
[934] | 1081 | uInt id = out->frequencies().addEntry(sc2.referencePixel()[0], |
---|
| 1082 | sc2.referenceValue()[0], |
---|
| 1083 | sc2.increment()[0]); |
---|
| 1084 | TableVector<uInt> tvec(ftab, "FREQ_ID"); |
---|
| 1085 | tvec = id; |
---|
[926] | 1086 | // create the "global" abcissa for alignment with same FREQ_ID |
---|
| 1087 | Vector<Double> abc(nchan); |
---|
[917] | 1088 | Double w; |
---|
| 1089 | for (uInt i=0; i<nchan; i++) { |
---|
| 1090 | sC.toWorld(w,Double(i)); |
---|
| 1091 | abc[i] = w; |
---|
| 1092 | } |
---|
[926] | 1093 | // cache abcissa for same time stamps, so iterate over those |
---|
| 1094 | TableIterator timeiter(ftab, "TIME"); |
---|
| 1095 | while ( !timeiter.pastEnd() ) { |
---|
| 1096 | Table tab = timeiter.table(); |
---|
| 1097 | ArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 1098 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 1099 | MEpoch::ROScalarColumn timeCol(tab, "TIME"); |
---|
| 1100 | // use align abcissa cache after the first row |
---|
| 1101 | bool first = true; |
---|
| 1102 | // these rows should be just be POLNO |
---|
[996] | 1103 | for (int i=0; i<int(tab.nrow()); ++i) { |
---|
[926] | 1104 | // input values |
---|
| 1105 | Vector<uChar> flag = flagCol(i); |
---|
| 1106 | Vector<Bool> mask(flag.shape()); |
---|
| 1107 | Vector<Float> specOut, spec; |
---|
| 1108 | spec = specCol(i); |
---|
| 1109 | Vector<Bool> maskOut;Vector<uChar> flagOut; |
---|
| 1110 | convertArray(mask, flag); |
---|
| 1111 | // alignment |
---|
| 1112 | Bool ok = fa.align(specOut, maskOut, abc, spec, |
---|
| 1113 | mask, timeCol(i), !first, |
---|
| 1114 | interp, False); |
---|
| 1115 | // back into scantable |
---|
| 1116 | flagOut.resize(maskOut.nelements()); |
---|
| 1117 | convertArray(flagOut, maskOut); |
---|
| 1118 | flagCol.put(i, flagOut); |
---|
| 1119 | specCol.put(i, specOut); |
---|
| 1120 | // start abcissa caching |
---|
| 1121 | first = false; |
---|
[917] | 1122 | } |
---|
[926] | 1123 | // next timestamp |
---|
| 1124 | ++timeiter; |
---|
[917] | 1125 | } |
---|
[940] | 1126 | // next FREQ_ID |
---|
[926] | 1127 | ++fiter; |
---|
[917] | 1128 | } |
---|
| 1129 | // next aligner |
---|
| 1130 | ++iter; |
---|
| 1131 | } |
---|
[940] | 1132 | // set this afterwards to ensure we are doing insitu correctly. |
---|
| 1133 | out->frequencies().setFrame(system, true); |
---|
[917] | 1134 | return out; |
---|
| 1135 | } |
---|
[992] | 1136 | |
---|
| 1137 | CountedPtr<Scantable> |
---|
| 1138 | asap::STMath::convertPolarisation( const CountedPtr<Scantable>& in, |
---|
| 1139 | const std::string & newtype ) |
---|
| 1140 | { |
---|
| 1141 | if (in->npol() != 2 && in->npol() != 4) |
---|
| 1142 | throw(AipsError("Can only convert two or four polarisations.")); |
---|
| 1143 | if ( in->getPolType() == newtype ) |
---|
| 1144 | throw(AipsError("No need to convert.")); |
---|
[1000] | 1145 | if ( ! in->selector_.empty() ) |
---|
| 1146 | throw(AipsError("Can only convert whole scantable. Unset the selection.")); |
---|
[992] | 1147 | bool insitu = insitu_; |
---|
| 1148 | setInsitu(false); |
---|
| 1149 | CountedPtr< Scantable > out = getScantable(in, true); |
---|
| 1150 | setInsitu(insitu); |
---|
| 1151 | Table& tout = out->table(); |
---|
| 1152 | tout.rwKeywordSet().define("POLTYPE", String(newtype)); |
---|
| 1153 | |
---|
| 1154 | Block<String> cols(4); |
---|
| 1155 | cols[0] = "SCANNO"; |
---|
| 1156 | cols[1] = "CYCLENO"; |
---|
| 1157 | cols[2] = "BEAMNO"; |
---|
| 1158 | cols[3] = "IFNO"; |
---|
| 1159 | TableIterator it(in->originalTable_, cols); |
---|
| 1160 | String basetype = in->getPolType(); |
---|
| 1161 | STPol* stpol = STPol::getPolClass(in->factories_, basetype); |
---|
| 1162 | try { |
---|
| 1163 | while ( !it.pastEnd() ) { |
---|
| 1164 | Table tab = it.table(); |
---|
| 1165 | uInt row = tab.rowNumbers()[0]; |
---|
| 1166 | stpol->setSpectra(in->getPolMatrix(row)); |
---|
| 1167 | Float fang,fhand,parang; |
---|
| 1168 | fang = in->focusTable_.getTotalFeedAngle(in->mfocusidCol_(row)); |
---|
| 1169 | fhand = in->focusTable_.getFeedHand(in->mfocusidCol_(row)); |
---|
| 1170 | parang = in->paraCol_(row); |
---|
| 1171 | /// @todo re-enable this |
---|
| 1172 | // disable total feed angle to support paralactifying Caswell style |
---|
| 1173 | stpol->setPhaseCorrections(parang, -parang, fhand); |
---|
| 1174 | Int npolout = 0; |
---|
| 1175 | for (uInt i=0; i<tab.nrow(); ++i) { |
---|
| 1176 | Vector<Float> outvec = stpol->getSpectrum(i, newtype); |
---|
| 1177 | if ( outvec.nelements() > 0 ) { |
---|
| 1178 | tout.addRow(); |
---|
| 1179 | TableCopy::copyRows(tout, tab, tout.nrow()-1, 0, 1); |
---|
| 1180 | ArrayColumn<Float> sCol(tout,"SPECTRA"); |
---|
| 1181 | ScalarColumn<uInt> pCol(tout,"POLNO"); |
---|
| 1182 | sCol.put(tout.nrow()-1 ,outvec); |
---|
| 1183 | pCol.put(tout.nrow()-1 ,uInt(npolout)); |
---|
| 1184 | npolout++; |
---|
| 1185 | } |
---|
| 1186 | } |
---|
| 1187 | tout.rwKeywordSet().define("nPol", npolout); |
---|
| 1188 | ++it; |
---|
| 1189 | } |
---|
| 1190 | } catch (AipsError& e) { |
---|
| 1191 | delete stpol; |
---|
| 1192 | throw(e); |
---|
| 1193 | } |
---|
| 1194 | delete stpol; |
---|
| 1195 | return out; |
---|
| 1196 | } |
---|