[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 <casa/Arrays/MaskArrLogi.h> |
---|
| 18 | #include <casa/Arrays/MaskArrMath.h> |
---|
| 19 | #include <casa/Arrays/ArrayLogical.h> |
---|
[81] | 20 | #include <casa/Arrays/ArrayMath.h> |
---|
[1066] | 21 | #include <casa/Arrays/Slice.h> |
---|
| 22 | #include <casa/Arrays/Slicer.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> |
---|
[1140] | 29 | #include <tables/Tables/TableParse.h> |
---|
[805] | 30 | #include <tables/Tables/ReadAsciiTable.h> |
---|
[1140] | 31 | #include <tables/Tables/TableIter.h> |
---|
| 32 | #include <tables/Tables/TableCopy.h> |
---|
[1192] | 33 | #include <scimath/Mathematics/FFTServer.h> |
---|
[2] | 34 | |
---|
[262] | 35 | #include <lattices/Lattices/LatticeUtilities.h> |
---|
| 36 | |
---|
[917] | 37 | #include <coordinates/Coordinates/SpectralCoordinate.h> |
---|
| 38 | #include <coordinates/Coordinates/CoordinateSystem.h> |
---|
| 39 | #include <coordinates/Coordinates/CoordinateUtil.h> |
---|
| 40 | #include <coordinates/Coordinates/FrequencyAligner.h> |
---|
| 41 | |
---|
[177] | 42 | #include <scimath/Mathematics/VectorKernel.h> |
---|
| 43 | #include <scimath/Mathematics/Convolver.h> |
---|
[234] | 44 | #include <scimath/Functionals/Polynomial.h> |
---|
[177] | 45 | |
---|
[38] | 46 | #include "MathUtils.h" |
---|
[805] | 47 | #include "RowAccumulator.h" |
---|
[878] | 48 | #include "STAttr.h" |
---|
[805] | 49 | #include "STMath.h" |
---|
[1391] | 50 | #include "STSelector.h" |
---|
[2] | 51 | |
---|
[805] | 52 | using namespace casa; |
---|
[2] | 53 | |
---|
[83] | 54 | using namespace asap; |
---|
[2] | 55 | |
---|
[805] | 56 | STMath::STMath(bool insitu) : |
---|
| 57 | insitu_(insitu) |
---|
[716] | 58 | { |
---|
| 59 | } |
---|
[170] | 60 | |
---|
| 61 | |
---|
[805] | 62 | STMath::~STMath() |
---|
[170] | 63 | { |
---|
| 64 | } |
---|
| 65 | |
---|
[805] | 66 | CountedPtr<Scantable> |
---|
[977] | 67 | STMath::average( const std::vector<CountedPtr<Scantable> >& in, |
---|
[858] | 68 | const std::vector<bool>& mask, |
---|
[805] | 69 | const std::string& weight, |
---|
[977] | 70 | const std::string& avmode) |
---|
[262] | 71 | { |
---|
[977] | 72 | if ( avmode == "SCAN" && in.size() != 1 ) |
---|
[1066] | 73 | throw(AipsError("Can't perform 'SCAN' averaging on multiple tables.\n" |
---|
| 74 | "Use merge first.")); |
---|
[805] | 75 | WeightType wtype = stringToWeight(weight); |
---|
[926] | 76 | |
---|
[805] | 77 | // output |
---|
| 78 | // clone as this is non insitu |
---|
| 79 | bool insitu = insitu_; |
---|
| 80 | setInsitu(false); |
---|
[977] | 81 | CountedPtr< Scantable > out = getScantable(in[0], true); |
---|
[805] | 82 | setInsitu(insitu); |
---|
[977] | 83 | std::vector<CountedPtr<Scantable> >::const_iterator stit = in.begin(); |
---|
[862] | 84 | ++stit; |
---|
[977] | 85 | while ( stit != in.end() ) { |
---|
[862] | 86 | out->appendToHistoryTable((*stit)->history()); |
---|
| 87 | ++stit; |
---|
| 88 | } |
---|
[294] | 89 | |
---|
[805] | 90 | Table& tout = out->table(); |
---|
[701] | 91 | |
---|
[805] | 92 | /// @todo check if all scantables are conformant |
---|
[294] | 93 | |
---|
[805] | 94 | ArrayColumn<Float> specColOut(tout,"SPECTRA"); |
---|
| 95 | ArrayColumn<uChar> flagColOut(tout,"FLAGTRA"); |
---|
| 96 | ArrayColumn<Float> tsysColOut(tout,"TSYS"); |
---|
| 97 | ScalarColumn<Double> mjdColOut(tout,"TIME"); |
---|
| 98 | ScalarColumn<Double> intColOut(tout,"INTERVAL"); |
---|
[1008] | 99 | ScalarColumn<uInt> cycColOut(tout,"CYCLENO"); |
---|
[1145] | 100 | ScalarColumn<uInt> scanColOut(tout,"SCANNO"); |
---|
[262] | 101 | |
---|
[805] | 102 | // set up the output table rows. These are based on the structure of the |
---|
[862] | 103 | // FIRST scantable in the vector |
---|
[977] | 104 | const Table& baset = in[0]->table(); |
---|
[262] | 105 | |
---|
[805] | 106 | Block<String> cols(3); |
---|
| 107 | cols[0] = String("BEAMNO"); |
---|
| 108 | cols[1] = String("IFNO"); |
---|
| 109 | cols[2] = String("POLNO"); |
---|
| 110 | if ( avmode == "SOURCE" ) { |
---|
| 111 | cols.resize(4); |
---|
| 112 | cols[3] = String("SRCNAME"); |
---|
[488] | 113 | } |
---|
[977] | 114 | if ( avmode == "SCAN" && in.size() == 1) { |
---|
[805] | 115 | cols.resize(4); |
---|
| 116 | cols[3] = String("SCANNO"); |
---|
[2] | 117 | } |
---|
[805] | 118 | uInt outrowCount = 0; |
---|
| 119 | TableIterator iter(baset, cols); |
---|
| 120 | while (!iter.pastEnd()) { |
---|
| 121 | Table subt = iter.table(); |
---|
| 122 | // copy the first row of this selection into the new table |
---|
| 123 | tout.addRow(); |
---|
| 124 | TableCopy::copyRows(tout, subt, outrowCount, 0, 1); |
---|
[1145] | 125 | // re-index to 0 |
---|
| 126 | if ( avmode != "SCAN" && avmode != "SOURCE" ) { |
---|
| 127 | scanColOut.put(outrowCount, uInt(0)); |
---|
| 128 | } |
---|
[805] | 129 | ++outrowCount; |
---|
| 130 | ++iter; |
---|
[144] | 131 | } |
---|
[1505] | 132 | |
---|
[805] | 133 | RowAccumulator acc(wtype); |
---|
[858] | 134 | Vector<Bool> cmask(mask); |
---|
| 135 | acc.setUserMask(cmask); |
---|
[805] | 136 | ROTableRow row(tout); |
---|
| 137 | ROArrayColumn<Float> specCol, tsysCol; |
---|
| 138 | ROArrayColumn<uChar> flagCol; |
---|
| 139 | ROScalarColumn<Double> mjdCol, intCol; |
---|
| 140 | ROScalarColumn<Int> scanIDCol; |
---|
[144] | 141 | |
---|
[1333] | 142 | Vector<uInt> rowstodelete; |
---|
| 143 | |
---|
[805] | 144 | for (uInt i=0; i < tout.nrow(); ++i) { |
---|
[996] | 145 | for ( int j=0; j < int(in.size()); ++j ) { |
---|
[977] | 146 | const Table& tin = in[j]->table(); |
---|
[805] | 147 | const TableRecord& rec = row.get(i); |
---|
| 148 | ROScalarColumn<Double> tmp(tin, "TIME"); |
---|
| 149 | Double td;tmp.get(0,td); |
---|
| 150 | Table basesubt = tin(tin.col("BEAMNO") == Int(rec.asuInt("BEAMNO")) |
---|
| 151 | && tin.col("IFNO") == Int(rec.asuInt("IFNO")) |
---|
| 152 | && tin.col("POLNO") == Int(rec.asuInt("POLNO")) ); |
---|
| 153 | Table subt; |
---|
| 154 | if ( avmode == "SOURCE") { |
---|
| 155 | subt = basesubt( basesubt.col("SRCNAME") == rec.asString("SRCNAME") ); |
---|
| 156 | } else if (avmode == "SCAN") { |
---|
| 157 | subt = basesubt( basesubt.col("SCANNO") == Int(rec.asuInt("SCANNO")) ); |
---|
| 158 | } else { |
---|
| 159 | subt = basesubt; |
---|
| 160 | } |
---|
| 161 | specCol.attach(subt,"SPECTRA"); |
---|
| 162 | flagCol.attach(subt,"FLAGTRA"); |
---|
| 163 | tsysCol.attach(subt,"TSYS"); |
---|
| 164 | intCol.attach(subt,"INTERVAL"); |
---|
| 165 | mjdCol.attach(subt,"TIME"); |
---|
| 166 | Vector<Float> spec,tsys; |
---|
| 167 | Vector<uChar> flag; |
---|
| 168 | Double inter,time; |
---|
| 169 | for (uInt k = 0; k < subt.nrow(); ++k ) { |
---|
| 170 | flagCol.get(k, flag); |
---|
| 171 | Vector<Bool> bflag(flag.shape()); |
---|
| 172 | convertArray(bflag, flag); |
---|
[1314] | 173 | /* |
---|
[805] | 174 | if ( allEQ(bflag, True) ) { |
---|
[1314] | 175 | continue;//don't accumulate |
---|
[144] | 176 | } |
---|
[1314] | 177 | */ |
---|
[805] | 178 | specCol.get(k, spec); |
---|
| 179 | tsysCol.get(k, tsys); |
---|
| 180 | intCol.get(k, inter); |
---|
| 181 | mjdCol.get(k, time); |
---|
| 182 | // spectrum has to be added last to enable weighting by the other values |
---|
| 183 | acc.add(spec, !bflag, tsys, inter, time); |
---|
| 184 | } |
---|
| 185 | } |
---|
[1333] | 186 | const Vector<Bool>& msk = acc.getMask(); |
---|
| 187 | if ( allEQ(msk, False) ) { |
---|
| 188 | uint n = rowstodelete.nelements(); |
---|
| 189 | rowstodelete.resize(n+1, True); |
---|
| 190 | rowstodelete[n] = i; |
---|
| 191 | continue; |
---|
| 192 | } |
---|
[805] | 193 | //write out |
---|
| 194 | Vector<uChar> flg(msk.shape()); |
---|
| 195 | convertArray(flg, !msk); |
---|
| 196 | flagColOut.put(i, flg); |
---|
[1333] | 197 | specColOut.put(i, acc.getSpectrum()); |
---|
[805] | 198 | tsysColOut.put(i, acc.getTsys()); |
---|
| 199 | intColOut.put(i, acc.getInterval()); |
---|
| 200 | mjdColOut.put(i, acc.getTime()); |
---|
[1008] | 201 | // we should only have one cycle now -> reset it to be 0 |
---|
| 202 | // frequency switched data has different CYCLENO for different IFNO |
---|
| 203 | // which requires resetting this value |
---|
| 204 | cycColOut.put(i, uInt(0)); |
---|
[805] | 205 | acc.reset(); |
---|
[144] | 206 | } |
---|
[1333] | 207 | if (rowstodelete.nelements() > 0) { |
---|
| 208 | tout.removeRow(rowstodelete); |
---|
| 209 | if (tout.nrow() == 0) { |
---|
| 210 | throw(AipsError("Can't average fully flagged data.")); |
---|
| 211 | } |
---|
| 212 | } |
---|
[805] | 213 | return out; |
---|
[2] | 214 | } |
---|
[9] | 215 | |
---|
[1069] | 216 | CountedPtr< Scantable > |
---|
| 217 | STMath::averageChannel( const CountedPtr < Scantable > & in, |
---|
[1078] | 218 | const std::string & mode, |
---|
| 219 | const std::string& avmode ) |
---|
[1069] | 220 | { |
---|
| 221 | // clone as this is non insitu |
---|
| 222 | bool insitu = insitu_; |
---|
| 223 | setInsitu(false); |
---|
| 224 | CountedPtr< Scantable > out = getScantable(in, true); |
---|
| 225 | setInsitu(insitu); |
---|
| 226 | Table& tout = out->table(); |
---|
| 227 | ArrayColumn<Float> specColOut(tout,"SPECTRA"); |
---|
| 228 | ArrayColumn<uChar> flagColOut(tout,"FLAGTRA"); |
---|
| 229 | ArrayColumn<Float> tsysColOut(tout,"TSYS"); |
---|
[1140] | 230 | ScalarColumn<uInt> scanColOut(tout,"SCANNO"); |
---|
[1232] | 231 | ScalarColumn<Double> intColOut(tout, "INTERVAL"); |
---|
[1140] | 232 | Table tmp = in->table().sort("BEAMNO"); |
---|
[1069] | 233 | Block<String> cols(3); |
---|
| 234 | cols[0] = String("BEAMNO"); |
---|
| 235 | cols[1] = String("IFNO"); |
---|
| 236 | cols[2] = String("POLNO"); |
---|
[1078] | 237 | if ( avmode == "SCAN") { |
---|
| 238 | cols.resize(4); |
---|
| 239 | cols[3] = String("SCANNO"); |
---|
| 240 | } |
---|
[1069] | 241 | uInt outrowCount = 0; |
---|
| 242 | uChar userflag = 1 << 7; |
---|
[1140] | 243 | TableIterator iter(tmp, cols); |
---|
[1069] | 244 | while (!iter.pastEnd()) { |
---|
| 245 | Table subt = iter.table(); |
---|
| 246 | ROArrayColumn<Float> specCol, tsysCol; |
---|
| 247 | ROArrayColumn<uChar> flagCol; |
---|
[1232] | 248 | ROScalarColumn<Double> intCol(subt, "INTERVAL"); |
---|
[1069] | 249 | specCol.attach(subt,"SPECTRA"); |
---|
| 250 | flagCol.attach(subt,"FLAGTRA"); |
---|
| 251 | tsysCol.attach(subt,"TSYS"); |
---|
| 252 | tout.addRow(); |
---|
| 253 | TableCopy::copyRows(tout, subt, outrowCount, 0, 1); |
---|
[1140] | 254 | if ( avmode != "SCAN") { |
---|
| 255 | scanColOut.put(outrowCount, uInt(0)); |
---|
| 256 | } |
---|
[1069] | 257 | Vector<Float> tmp; |
---|
| 258 | specCol.get(0, tmp); |
---|
| 259 | uInt nchan = tmp.nelements(); |
---|
[1078] | 260 | // have to do channel by channel here as MaskedArrMath |
---|
| 261 | // doesn't have partialMedians |
---|
[1069] | 262 | Vector<uChar> flags = flagCol.getColumn(Slicer(Slice(0))); |
---|
| 263 | Vector<Float> outspec(nchan); |
---|
| 264 | Vector<uChar> outflag(nchan,0); |
---|
| 265 | Vector<Float> outtsys(1);/// @fixme when tsys is channel based |
---|
| 266 | for (uInt i=0; i<nchan; ++i) { |
---|
| 267 | Vector<Float> specs = specCol.getColumn(Slicer(Slice(i))); |
---|
| 268 | MaskedArray<Float> ma = maskedArray(specs,flags); |
---|
| 269 | outspec[i] = median(ma); |
---|
| 270 | if ( allEQ(ma.getMask(), False) ) |
---|
| 271 | outflag[i] = userflag;// flag data |
---|
| 272 | } |
---|
| 273 | outtsys[0] = median(tsysCol.getColumn()); |
---|
| 274 | specColOut.put(outrowCount, outspec); |
---|
| 275 | flagColOut.put(outrowCount, outflag); |
---|
| 276 | tsysColOut.put(outrowCount, outtsys); |
---|
[1232] | 277 | Double intsum = sum(intCol.getColumn()); |
---|
| 278 | intColOut.put(outrowCount, intsum); |
---|
[1069] | 279 | ++outrowCount; |
---|
| 280 | ++iter; |
---|
| 281 | } |
---|
| 282 | return out; |
---|
| 283 | } |
---|
| 284 | |
---|
[805] | 285 | CountedPtr< Scantable > STMath::getScantable(const CountedPtr< Scantable >& in, |
---|
| 286 | bool droprows) |
---|
[185] | 287 | { |
---|
[1505] | 288 | if (insitu_) { |
---|
| 289 | return in; |
---|
| 290 | } |
---|
[805] | 291 | else { |
---|
| 292 | // clone |
---|
[1505] | 293 | return CountedPtr<Scantable>(new Scantable(*in, Bool(droprows))); |
---|
[234] | 294 | } |
---|
[805] | 295 | } |
---|
[234] | 296 | |
---|
[805] | 297 | CountedPtr< Scantable > STMath::unaryOperate( const CountedPtr< Scantable >& in, |
---|
| 298 | float val, |
---|
| 299 | const std::string& mode, |
---|
| 300 | bool tsys ) |
---|
| 301 | { |
---|
| 302 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 303 | Table& tab = out->table(); |
---|
| 304 | ArrayColumn<Float> specCol(tab,"SPECTRA"); |
---|
| 305 | ArrayColumn<Float> tsysCol(tab,"TSYS"); |
---|
| 306 | for (uInt i=0; i<tab.nrow(); ++i) { |
---|
| 307 | Vector<Float> spec; |
---|
| 308 | Vector<Float> ts; |
---|
| 309 | specCol.get(i, spec); |
---|
| 310 | tsysCol.get(i, ts); |
---|
[1308] | 311 | if (mode == "MUL" || mode == "DIV") { |
---|
| 312 | if (mode == "DIV") val = 1.0/val; |
---|
[805] | 313 | spec *= val; |
---|
| 314 | specCol.put(i, spec); |
---|
| 315 | if ( tsys ) { |
---|
| 316 | ts *= val; |
---|
| 317 | tsysCol.put(i, ts); |
---|
| 318 | } |
---|
[1308] | 319 | } else if ( mode == "ADD" || mode == "SUB") { |
---|
| 320 | if (mode == "SUB") val *= -1.0; |
---|
[805] | 321 | spec += val; |
---|
| 322 | specCol.put(i, spec); |
---|
| 323 | if ( tsys ) { |
---|
| 324 | ts += val; |
---|
| 325 | tsysCol.put(i, ts); |
---|
| 326 | } |
---|
| 327 | } |
---|
[234] | 328 | } |
---|
[805] | 329 | return out; |
---|
| 330 | } |
---|
[234] | 331 | |
---|
[1308] | 332 | CountedPtr<Scantable> STMath::binaryOperate(const CountedPtr<Scantable>& left, |
---|
| 333 | const CountedPtr<Scantable>& right, |
---|
| 334 | const std::string& mode) |
---|
| 335 | { |
---|
| 336 | bool insitu = insitu_; |
---|
| 337 | if ( ! left->conformant(*right) ) { |
---|
| 338 | throw(AipsError("'left' and 'right' scantables are not conformant.")); |
---|
| 339 | } |
---|
| 340 | setInsitu(false); |
---|
| 341 | CountedPtr< Scantable > out = getScantable(left, false); |
---|
| 342 | setInsitu(insitu); |
---|
| 343 | Table& tout = out->table(); |
---|
| 344 | Block<String> coln(5); |
---|
| 345 | coln[0] = "SCANNO"; coln[1] = "CYCLENO"; coln[2] = "BEAMNO"; |
---|
| 346 | coln[3] = "IFNO"; coln[4] = "POLNO"; |
---|
| 347 | Table tmpl = tout.sort(coln); |
---|
| 348 | Table tmpr = right->table().sort(coln); |
---|
| 349 | ArrayColumn<Float> lspecCol(tmpl,"SPECTRA"); |
---|
| 350 | ROArrayColumn<Float> rspecCol(tmpr,"SPECTRA"); |
---|
| 351 | ArrayColumn<uChar> lflagCol(tmpl,"FLAGTRA"); |
---|
| 352 | ROArrayColumn<uChar> rflagCol(tmpr,"FLAGTRA"); |
---|
| 353 | |
---|
| 354 | for (uInt i=0; i<tout.nrow(); ++i) { |
---|
| 355 | Vector<Float> lspecvec, rspecvec; |
---|
| 356 | Vector<uChar> lflagvec, rflagvec; |
---|
| 357 | lspecvec = lspecCol(i); rspecvec = rspecCol(i); |
---|
| 358 | lflagvec = lflagCol(i); rflagvec = rflagCol(i); |
---|
| 359 | MaskedArray<Float> mleft = maskedArray(lspecvec, lflagvec); |
---|
| 360 | MaskedArray<Float> mright = maskedArray(rspecvec, rflagvec); |
---|
| 361 | if (mode == "ADD") { |
---|
| 362 | mleft += mright; |
---|
| 363 | } else if ( mode == "SUB") { |
---|
| 364 | mleft -= mright; |
---|
| 365 | } else if ( mode == "MUL") { |
---|
| 366 | mleft *= mright; |
---|
| 367 | } else if ( mode == "DIV") { |
---|
| 368 | mleft /= mright; |
---|
| 369 | } else { |
---|
| 370 | throw(AipsError("Illegal binary operator")); |
---|
| 371 | } |
---|
| 372 | lspecCol.put(i, mleft.getArray()); |
---|
| 373 | } |
---|
| 374 | return out; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | |
---|
| 378 | |
---|
[805] | 379 | MaskedArray<Float> STMath::maskedArray( const Vector<Float>& s, |
---|
| 380 | const Vector<uChar>& f) |
---|
| 381 | { |
---|
| 382 | Vector<Bool> mask; |
---|
| 383 | mask.resize(f.shape()); |
---|
| 384 | convertArray(mask, f); |
---|
| 385 | return MaskedArray<Float>(s,!mask); |
---|
| 386 | } |
---|
[248] | 387 | |
---|
[805] | 388 | Vector<uChar> STMath::flagsFromMA(const MaskedArray<Float>& ma) |
---|
| 389 | { |
---|
| 390 | const Vector<Bool>& m = ma.getMask(); |
---|
| 391 | Vector<uChar> flags(m.shape()); |
---|
| 392 | convertArray(flags, !m); |
---|
| 393 | return flags; |
---|
| 394 | } |
---|
[234] | 395 | |
---|
[1066] | 396 | CountedPtr< Scantable > STMath::autoQuotient( const CountedPtr< Scantable >& in, |
---|
| 397 | const std::string & mode, |
---|
| 398 | bool preserve ) |
---|
[805] | 399 | { |
---|
| 400 | /// @todo make other modes available |
---|
| 401 | /// modes should be "nearest", "pair" |
---|
| 402 | // make this operation non insitu |
---|
| 403 | const Table& tin = in->table(); |
---|
| 404 | Table ons = tin(tin.col("SRCTYPE") == Int(0)); |
---|
| 405 | Table offs = tin(tin.col("SRCTYPE") == Int(1)); |
---|
| 406 | if ( offs.nrow() == 0 ) |
---|
| 407 | throw(AipsError("No 'off' scans present.")); |
---|
| 408 | // put all "on" scans into output table |
---|
[701] | 409 | |
---|
[805] | 410 | bool insitu = insitu_; |
---|
| 411 | setInsitu(false); |
---|
| 412 | CountedPtr< Scantable > out = getScantable(in, true); |
---|
| 413 | setInsitu(insitu); |
---|
| 414 | Table& tout = out->table(); |
---|
[248] | 415 | |
---|
[805] | 416 | TableCopy::copyRows(tout, ons); |
---|
| 417 | TableRow row(tout); |
---|
| 418 | ROScalarColumn<Double> offtimeCol(offs, "TIME"); |
---|
| 419 | ArrayColumn<Float> outspecCol(tout, "SPECTRA"); |
---|
| 420 | ROArrayColumn<Float> outtsysCol(tout, "TSYS"); |
---|
| 421 | ArrayColumn<uChar> outflagCol(tout, "FLAGTRA"); |
---|
| 422 | for (uInt i=0; i < tout.nrow(); ++i) { |
---|
| 423 | const TableRecord& rec = row.get(i); |
---|
| 424 | Double ontime = rec.asDouble("TIME"); |
---|
[1321] | 425 | Table presel = offs(offs.col("BEAMNO") == Int(rec.asuInt("BEAMNO")) |
---|
| 426 | && offs.col("IFNO") == Int(rec.asuInt("IFNO")) |
---|
| 427 | && offs.col("POLNO") == Int(rec.asuInt("POLNO")) ); |
---|
| 428 | ROScalarColumn<Double> offtimeCol(presel, "TIME"); |
---|
| 429 | |
---|
[805] | 430 | Double mindeltat = min(abs(offtimeCol.getColumn() - ontime)); |
---|
[1259] | 431 | // Timestamp may vary within a cycle ???!!! |
---|
[1321] | 432 | // increase this by 0.01 sec in case of rounding errors... |
---|
[1259] | 433 | // There might be a better way to do this. |
---|
[1321] | 434 | // fix to this fix. TIME is MJD, so 1.0d not 1.0s |
---|
| 435 | mindeltat += 0.01/24./60./60.; |
---|
| 436 | Table sel = presel( abs(presel.col("TIME")-ontime) <= mindeltat); |
---|
[780] | 437 | |
---|
[1259] | 438 | if ( sel.nrow() < 1 ) { |
---|
| 439 | throw(AipsError("No closest in time found... This could be a rounding " |
---|
| 440 | "issue. Try quotient instead.")); |
---|
| 441 | } |
---|
[805] | 442 | TableRow offrow(sel); |
---|
| 443 | const TableRecord& offrec = offrow.get(0);//should only be one row |
---|
| 444 | RORecordFieldPtr< Array<Float> > specoff(offrec, "SPECTRA"); |
---|
| 445 | RORecordFieldPtr< Array<Float> > tsysoff(offrec, "TSYS"); |
---|
| 446 | RORecordFieldPtr< Array<uChar> > flagoff(offrec, "FLAGTRA"); |
---|
| 447 | /// @fixme this assumes tsys is a scalar not vector |
---|
| 448 | Float tsysoffscalar = (*tsysoff)(IPosition(1,0)); |
---|
| 449 | Vector<Float> specon, tsyson; |
---|
| 450 | outtsysCol.get(i, tsyson); |
---|
| 451 | outspecCol.get(i, specon); |
---|
| 452 | Vector<uChar> flagon; |
---|
| 453 | outflagCol.get(i, flagon); |
---|
| 454 | MaskedArray<Float> mon = maskedArray(specon, flagon); |
---|
| 455 | MaskedArray<Float> moff = maskedArray(*specoff, *flagoff); |
---|
| 456 | MaskedArray<Float> quot = (tsysoffscalar * mon / moff); |
---|
| 457 | if (preserve) { |
---|
| 458 | quot -= tsysoffscalar; |
---|
| 459 | } else { |
---|
| 460 | quot -= tsyson[0]; |
---|
[701] | 461 | } |
---|
[805] | 462 | outspecCol.put(i, quot.getArray()); |
---|
| 463 | outflagCol.put(i, flagsFromMA(quot)); |
---|
| 464 | } |
---|
[926] | 465 | // renumber scanno |
---|
| 466 | TableIterator it(tout, "SCANNO"); |
---|
| 467 | uInt i = 0; |
---|
| 468 | while ( !it.pastEnd() ) { |
---|
| 469 | Table t = it.table(); |
---|
| 470 | TableVector<uInt> vec(t, "SCANNO"); |
---|
| 471 | vec = i; |
---|
| 472 | ++i; |
---|
| 473 | ++it; |
---|
| 474 | } |
---|
[805] | 475 | return out; |
---|
| 476 | } |
---|
[234] | 477 | |
---|
[1066] | 478 | |
---|
| 479 | CountedPtr< Scantable > STMath::quotient( const CountedPtr< Scantable > & on, |
---|
| 480 | const CountedPtr< Scantable > & off, |
---|
| 481 | bool preserve ) |
---|
| 482 | { |
---|
| 483 | bool insitu = insitu_; |
---|
[1069] | 484 | if ( ! on->conformant(*off) ) { |
---|
| 485 | throw(AipsError("'on' and 'off' scantables are not conformant.")); |
---|
| 486 | } |
---|
[1066] | 487 | setInsitu(false); |
---|
| 488 | CountedPtr< Scantable > out = getScantable(on, false); |
---|
| 489 | setInsitu(insitu); |
---|
| 490 | Table& tout = out->table(); |
---|
| 491 | const Table& toff = off->table(); |
---|
| 492 | TableIterator sit(tout, "SCANNO"); |
---|
| 493 | TableIterator s2it(toff, "SCANNO"); |
---|
| 494 | while ( !sit.pastEnd() ) { |
---|
| 495 | Table ton = sit.table(); |
---|
| 496 | TableRow row(ton); |
---|
| 497 | Table t = s2it.table(); |
---|
| 498 | ArrayColumn<Float> outspecCol(ton, "SPECTRA"); |
---|
| 499 | ROArrayColumn<Float> outtsysCol(ton, "TSYS"); |
---|
| 500 | ArrayColumn<uChar> outflagCol(ton, "FLAGTRA"); |
---|
| 501 | for (uInt i=0; i < ton.nrow(); ++i) { |
---|
| 502 | const TableRecord& rec = row.get(i); |
---|
| 503 | Table offsel = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO")) |
---|
| 504 | && t.col("IFNO") == Int(rec.asuInt("IFNO")) |
---|
| 505 | && t.col("POLNO") == Int(rec.asuInt("POLNO")) ); |
---|
[1145] | 506 | if ( offsel.nrow() == 0 ) |
---|
| 507 | throw AipsError("STMath::quotient: no matching off"); |
---|
[1066] | 508 | TableRow offrow(offsel); |
---|
| 509 | const TableRecord& offrec = offrow.get(0);//should be ncycles - take first |
---|
| 510 | RORecordFieldPtr< Array<Float> > specoff(offrec, "SPECTRA"); |
---|
| 511 | RORecordFieldPtr< Array<Float> > tsysoff(offrec, "TSYS"); |
---|
| 512 | RORecordFieldPtr< Array<uChar> > flagoff(offrec, "FLAGTRA"); |
---|
| 513 | Float tsysoffscalar = (*tsysoff)(IPosition(1,0)); |
---|
| 514 | Vector<Float> specon, tsyson; |
---|
| 515 | outtsysCol.get(i, tsyson); |
---|
| 516 | outspecCol.get(i, specon); |
---|
| 517 | Vector<uChar> flagon; |
---|
| 518 | outflagCol.get(i, flagon); |
---|
| 519 | MaskedArray<Float> mon = maskedArray(specon, flagon); |
---|
| 520 | MaskedArray<Float> moff = maskedArray(*specoff, *flagoff); |
---|
| 521 | MaskedArray<Float> quot = (tsysoffscalar * mon / moff); |
---|
| 522 | if (preserve) { |
---|
| 523 | quot -= tsysoffscalar; |
---|
| 524 | } else { |
---|
| 525 | quot -= tsyson[0]; |
---|
| 526 | } |
---|
| 527 | outspecCol.put(i, quot.getArray()); |
---|
| 528 | outflagCol.put(i, flagsFromMA(quot)); |
---|
| 529 | } |
---|
| 530 | ++sit; |
---|
| 531 | ++s2it; |
---|
| 532 | // take the first off for each on scan which doesn't have a |
---|
| 533 | // matching off scan |
---|
| 534 | // non <= noff: matching pairs, non > noff matching pairs then first off |
---|
| 535 | if ( s2it.pastEnd() ) s2it.reset(); |
---|
| 536 | } |
---|
| 537 | return out; |
---|
| 538 | } |
---|
| 539 | |
---|
[1391] | 540 | // dototalpower (migration of GBTIDL procedure dototalpower.pro) |
---|
| 541 | // calibrate the CAL on-off pair. It calculate Tsys and average CAL on-off subintegrations |
---|
| 542 | // do it for each cycles in a specific scan. |
---|
| 543 | CountedPtr< Scantable > STMath::dototalpower( const CountedPtr< Scantable >& calon, |
---|
| 544 | const CountedPtr< Scantable >& caloff, Float tcal ) |
---|
| 545 | { |
---|
| 546 | if ( ! calon->conformant(*caloff) ) { |
---|
| 547 | throw(AipsError("'CAL on' and 'CAL off' scantables are not conformant.")); |
---|
| 548 | } |
---|
| 549 | setInsitu(false); |
---|
| 550 | CountedPtr< Scantable > out = getScantable(caloff, false); |
---|
| 551 | Table& tout = out->table(); |
---|
| 552 | const Table& tcon = calon->table(); |
---|
| 553 | Vector<Float> tcalout; |
---|
| 554 | Vector<Float> tcalout2; //debug |
---|
[1066] | 555 | |
---|
[1391] | 556 | if ( tout.nrow() != tcon.nrow() ) { |
---|
| 557 | throw(AipsError("Mismatch in number of rows to form cal on - off pair.")); |
---|
| 558 | } |
---|
| 559 | // iteration by scanno or cycle no. |
---|
| 560 | TableIterator sit(tout, "SCANNO"); |
---|
| 561 | TableIterator s2it(tcon, "SCANNO"); |
---|
| 562 | while ( !sit.pastEnd() ) { |
---|
| 563 | Table toff = sit.table(); |
---|
| 564 | TableRow row(toff); |
---|
| 565 | Table t = s2it.table(); |
---|
| 566 | ScalarColumn<Double> outintCol(toff, "INTERVAL"); |
---|
| 567 | ArrayColumn<Float> outspecCol(toff, "SPECTRA"); |
---|
| 568 | ArrayColumn<Float> outtsysCol(toff, "TSYS"); |
---|
| 569 | ArrayColumn<uChar> outflagCol(toff, "FLAGTRA"); |
---|
| 570 | ROScalarColumn<uInt> outtcalIdCol(toff, "TCAL_ID"); |
---|
| 571 | ROScalarColumn<uInt> outpolCol(toff, "POLNO"); |
---|
| 572 | ROScalarColumn<Double> onintCol(t, "INTERVAL"); |
---|
| 573 | ROArrayColumn<Float> onspecCol(t, "SPECTRA"); |
---|
| 574 | ROArrayColumn<Float> ontsysCol(t, "TSYS"); |
---|
| 575 | ROArrayColumn<uChar> onflagCol(t, "FLAGTRA"); |
---|
| 576 | //ROScalarColumn<uInt> ontcalIdCol(t, "TCAL_ID"); |
---|
| 577 | |
---|
| 578 | for (uInt i=0; i < toff.nrow(); ++i) { |
---|
| 579 | //skip these checks -> assumes the data order are the same between the cal on off pairs |
---|
| 580 | // |
---|
| 581 | Vector<Float> specCalon, specCaloff; |
---|
| 582 | // to store scalar (mean) tsys |
---|
| 583 | Vector<Float> tsysout(1); |
---|
| 584 | uInt tcalId, polno; |
---|
| 585 | Double offint, onint; |
---|
| 586 | outpolCol.get(i, polno); |
---|
| 587 | outspecCol.get(i, specCaloff); |
---|
| 588 | onspecCol.get(i, specCalon); |
---|
| 589 | Vector<uChar> flagCaloff, flagCalon; |
---|
| 590 | outflagCol.get(i, flagCaloff); |
---|
| 591 | onflagCol.get(i, flagCalon); |
---|
| 592 | outtcalIdCol.get(i, tcalId); |
---|
| 593 | outintCol.get(i, offint); |
---|
| 594 | onintCol.get(i, onint); |
---|
| 595 | // caluculate mean Tsys |
---|
| 596 | uInt nchan = specCaloff.nelements(); |
---|
| 597 | // percentage of edge cut off |
---|
| 598 | uInt pc = 10; |
---|
| 599 | uInt bchan = nchan/pc; |
---|
| 600 | uInt echan = nchan-bchan; |
---|
| 601 | |
---|
| 602 | Slicer chansl(IPosition(1,bchan-1), IPosition(1,echan-1), IPosition(1,1),Slicer::endIsLast); |
---|
| 603 | Vector<Float> testsubsp = specCaloff(chansl); |
---|
| 604 | MaskedArray<Float> spoff = maskedArray( specCaloff(chansl),flagCaloff(chansl) ); |
---|
| 605 | MaskedArray<Float> spon = maskedArray( specCalon(chansl),flagCalon(chansl) ); |
---|
| 606 | MaskedArray<Float> spdiff = spon-spoff; |
---|
| 607 | uInt noff = spoff.nelementsValid(); |
---|
| 608 | //uInt non = spon.nelementsValid(); |
---|
| 609 | uInt ndiff = spdiff.nelementsValid(); |
---|
| 610 | Float meantsys; |
---|
| 611 | |
---|
| 612 | /** |
---|
| 613 | Double subspec, subdiff; |
---|
| 614 | uInt usednchan; |
---|
| 615 | subspec = 0; |
---|
| 616 | subdiff = 0; |
---|
| 617 | usednchan = 0; |
---|
| 618 | for(uInt k=(bchan-1); k<echan; k++) { |
---|
| 619 | subspec += specCaloff[k]; |
---|
| 620 | subdiff += static_cast<Double>(specCalon[k]-specCaloff[k]); |
---|
| 621 | ++usednchan; |
---|
| 622 | } |
---|
| 623 | **/ |
---|
| 624 | // get tcal if input tcal <= 0 |
---|
| 625 | String tcalt; |
---|
| 626 | Float tcalUsed; |
---|
| 627 | tcalUsed = tcal; |
---|
| 628 | if ( tcal <= 0.0 ) { |
---|
| 629 | caloff->tcal().getEntry(tcalt, tcalout, tcalId); |
---|
| 630 | if (polno<=3) { |
---|
| 631 | tcalUsed = tcalout[polno]; |
---|
| 632 | } |
---|
| 633 | else { |
---|
| 634 | tcalUsed = tcalout[0]; |
---|
| 635 | } |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | Float meanoff; |
---|
| 639 | Float meandiff; |
---|
| 640 | if (noff && ndiff) { |
---|
| 641 | //Debug |
---|
| 642 | //if(noff!=ndiff) cerr<<"noff and ndiff is not equal"<<endl; |
---|
| 643 | meanoff = sum(spoff)/noff; |
---|
| 644 | meandiff = sum(spdiff)/ndiff; |
---|
| 645 | meantsys= (meanoff/meandiff )*tcalUsed + tcalUsed/2; |
---|
| 646 | } |
---|
| 647 | else { |
---|
| 648 | meantsys=1; |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | tsysout[0] = Float(meantsys); |
---|
| 652 | MaskedArray<Float> mcaloff = maskedArray(specCaloff, flagCaloff); |
---|
| 653 | MaskedArray<Float> mcalon = maskedArray(specCalon, flagCalon); |
---|
| 654 | MaskedArray<Float> sig = Float(0.5) * (mcaloff + mcalon); |
---|
| 655 | //uInt ncaloff = mcaloff.nelementsValid(); |
---|
| 656 | //uInt ncalon = mcalon.nelementsValid(); |
---|
| 657 | |
---|
| 658 | outintCol.put(i, offint+onint); |
---|
| 659 | outspecCol.put(i, sig.getArray()); |
---|
| 660 | outflagCol.put(i, flagsFromMA(sig)); |
---|
| 661 | outtsysCol.put(i, tsysout); |
---|
| 662 | } |
---|
| 663 | ++sit; |
---|
| 664 | ++s2it; |
---|
| 665 | } |
---|
| 666 | return out; |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | //dosigref - migrated from GBT IDL's dosigref.pro, do calibration of position switch |
---|
| 670 | // observatiions. |
---|
| 671 | // input: sig and ref scantables, and an optional boxcar smoothing width(default width=0, |
---|
| 672 | // no smoothing). |
---|
| 673 | // output: resultant scantable [= (sig-ref/ref)*tsys] |
---|
| 674 | CountedPtr< Scantable > STMath::dosigref( const CountedPtr < Scantable >& sig, |
---|
| 675 | const CountedPtr < Scantable >& ref, |
---|
| 676 | int smoothref, |
---|
| 677 | casa::Float tsysv, |
---|
| 678 | casa::Float tau ) |
---|
| 679 | { |
---|
| 680 | if ( ! ref->conformant(*sig) ) { |
---|
| 681 | throw(AipsError("'sig' and 'ref' scantables are not conformant.")); |
---|
| 682 | } |
---|
| 683 | setInsitu(false); |
---|
| 684 | CountedPtr< Scantable > out = getScantable(sig, false); |
---|
| 685 | CountedPtr< Scantable > smref; |
---|
| 686 | if ( smoothref > 1 ) { |
---|
| 687 | float fsmoothref = static_cast<float>(smoothref); |
---|
| 688 | std::string inkernel = "boxcar"; |
---|
| 689 | smref = smooth(ref, inkernel, fsmoothref ); |
---|
| 690 | ostringstream oss; |
---|
| 691 | oss<<"Applied smoothing of "<<fsmoothref<<" on the reference."<<endl; |
---|
| 692 | pushLog(String(oss)); |
---|
| 693 | } |
---|
| 694 | else { |
---|
| 695 | smref = ref; |
---|
| 696 | } |
---|
| 697 | Table& tout = out->table(); |
---|
| 698 | const Table& tref = smref->table(); |
---|
| 699 | if ( tout.nrow() != tref.nrow() ) { |
---|
| 700 | throw(AipsError("Mismatch in number of rows to form on-source and reference pair.")); |
---|
| 701 | } |
---|
| 702 | // iteration by scanno? or cycle no. |
---|
| 703 | TableIterator sit(tout, "SCANNO"); |
---|
| 704 | TableIterator s2it(tref, "SCANNO"); |
---|
| 705 | while ( !sit.pastEnd() ) { |
---|
| 706 | Table ton = sit.table(); |
---|
| 707 | Table t = s2it.table(); |
---|
| 708 | ScalarColumn<Double> outintCol(ton, "INTERVAL"); |
---|
| 709 | ArrayColumn<Float> outspecCol(ton, "SPECTRA"); |
---|
| 710 | ArrayColumn<Float> outtsysCol(ton, "TSYS"); |
---|
| 711 | ArrayColumn<uChar> outflagCol(ton, "FLAGTRA"); |
---|
| 712 | ArrayColumn<Float> refspecCol(t, "SPECTRA"); |
---|
| 713 | ROScalarColumn<Double> refintCol(t, "INTERVAL"); |
---|
| 714 | ROArrayColumn<Float> reftsysCol(t, "TSYS"); |
---|
| 715 | ArrayColumn<uChar> refflagCol(t, "FLAGTRA"); |
---|
| 716 | ROScalarColumn<Float> refelevCol(t, "ELEVATION"); |
---|
| 717 | for (uInt i=0; i < ton.nrow(); ++i) { |
---|
| 718 | |
---|
| 719 | Double onint, refint; |
---|
| 720 | Vector<Float> specon, specref; |
---|
| 721 | // to store scalar (mean) tsys |
---|
| 722 | Vector<Float> tsysref; |
---|
| 723 | outintCol.get(i, onint); |
---|
| 724 | refintCol.get(i, refint); |
---|
| 725 | outspecCol.get(i, specon); |
---|
| 726 | refspecCol.get(i, specref); |
---|
| 727 | Vector<uChar> flagref, flagon; |
---|
| 728 | outflagCol.get(i, flagon); |
---|
| 729 | refflagCol.get(i, flagref); |
---|
| 730 | reftsysCol.get(i, tsysref); |
---|
| 731 | |
---|
| 732 | Float tsysrefscalar; |
---|
| 733 | if ( tsysv > 0.0 ) { |
---|
| 734 | ostringstream oss; |
---|
| 735 | Float elev; |
---|
| 736 | refelevCol.get(i, elev); |
---|
| 737 | oss << "user specified Tsys = " << tsysv; |
---|
| 738 | // do recalc elevation if EL = 0 |
---|
| 739 | if ( elev == 0 ) { |
---|
| 740 | throw(AipsError("EL=0, elevation data is missing.")); |
---|
| 741 | } else { |
---|
| 742 | if ( tau <= 0.0 ) { |
---|
| 743 | throw(AipsError("Valid tau is not supplied.")); |
---|
| 744 | } else { |
---|
| 745 | tsysrefscalar = tsysv * exp(tau/elev); |
---|
| 746 | } |
---|
| 747 | } |
---|
| 748 | oss << ", corrected (for El) tsys= "<<tsysrefscalar; |
---|
| 749 | pushLog(String(oss)); |
---|
| 750 | } |
---|
| 751 | else { |
---|
| 752 | tsysrefscalar = tsysref[0]; |
---|
| 753 | } |
---|
| 754 | //get quotient spectrum |
---|
| 755 | MaskedArray<Float> mref = maskedArray(specref, flagref); |
---|
| 756 | MaskedArray<Float> mon = maskedArray(specon, flagon); |
---|
| 757 | MaskedArray<Float> specres = tsysrefscalar*((mon - mref)/mref); |
---|
| 758 | Double resint = onint*refint*smoothref/(onint+refint*smoothref); |
---|
| 759 | |
---|
| 760 | //Debug |
---|
| 761 | //cerr<<"Tsys used="<<tsysrefscalar<<endl; |
---|
| 762 | // fill the result, replay signal tsys by reference tsys |
---|
| 763 | outintCol.put(i, resint); |
---|
| 764 | outspecCol.put(i, specres.getArray()); |
---|
| 765 | outflagCol.put(i, flagsFromMA(specres)); |
---|
| 766 | outtsysCol.put(i, tsysref); |
---|
| 767 | } |
---|
| 768 | ++sit; |
---|
| 769 | ++s2it; |
---|
| 770 | } |
---|
| 771 | return out; |
---|
| 772 | } |
---|
| 773 | |
---|
| 774 | CountedPtr< Scantable > STMath::donod(const casa::CountedPtr<Scantable>& s, |
---|
| 775 | const std::vector<int>& scans, |
---|
| 776 | int smoothref, |
---|
| 777 | casa::Float tsysv, |
---|
| 778 | casa::Float tau, |
---|
| 779 | casa::Float tcal ) |
---|
| 780 | |
---|
| 781 | { |
---|
| 782 | setInsitu(false); |
---|
| 783 | STSelector sel; |
---|
| 784 | std::vector<int> scan1, scan2, beams; |
---|
| 785 | std::vector< vector<int> > scanpair; |
---|
| 786 | std::vector<string> calstate; |
---|
| 787 | String msg; |
---|
| 788 | |
---|
| 789 | CountedPtr< Scantable > s1b1on, s1b1off, s1b2on, s1b2off; |
---|
| 790 | CountedPtr< Scantable > s2b1on, s2b1off, s2b2on, s2b2off; |
---|
| 791 | |
---|
| 792 | std::vector< CountedPtr< Scantable > > sctables; |
---|
| 793 | sctables.push_back(s1b1on); |
---|
| 794 | sctables.push_back(s1b1off); |
---|
| 795 | sctables.push_back(s1b2on); |
---|
| 796 | sctables.push_back(s1b2off); |
---|
| 797 | sctables.push_back(s2b1on); |
---|
| 798 | sctables.push_back(s2b1off); |
---|
| 799 | sctables.push_back(s2b2on); |
---|
| 800 | sctables.push_back(s2b2off); |
---|
| 801 | |
---|
| 802 | //check scanlist |
---|
| 803 | int n=s->checkScanInfo(scans); |
---|
| 804 | if (n==1) { |
---|
| 805 | throw(AipsError("Incorrect scan pairs. ")); |
---|
| 806 | } |
---|
| 807 | |
---|
| 808 | // Assume scans contain only a pair of consecutive scan numbers. |
---|
| 809 | // It is assumed that first beam, b1, is on target. |
---|
| 810 | // There is no check if the first beam is on or not. |
---|
| 811 | if ( scans.size()==1 ) { |
---|
| 812 | scan1.push_back(scans[0]); |
---|
| 813 | scan2.push_back(scans[0]+1); |
---|
| 814 | } else if ( scans.size()==2 ) { |
---|
| 815 | scan1.push_back(scans[0]); |
---|
| 816 | scan2.push_back(scans[1]); |
---|
| 817 | } else { |
---|
| 818 | if ( scans.size()%2 == 0 ) { |
---|
| 819 | for (uInt i=0; i<scans.size(); i++) { |
---|
| 820 | if (i%2 == 0) { |
---|
| 821 | scan1.push_back(scans[i]); |
---|
| 822 | } |
---|
| 823 | else { |
---|
| 824 | scan2.push_back(scans[i]); |
---|
| 825 | } |
---|
| 826 | } |
---|
| 827 | } else { |
---|
| 828 | throw(AipsError("Odd numbers of scans, cannot form pairs.")); |
---|
| 829 | } |
---|
| 830 | } |
---|
| 831 | scanpair.push_back(scan1); |
---|
| 832 | scanpair.push_back(scan2); |
---|
| 833 | calstate.push_back("*calon"); |
---|
| 834 | calstate.push_back("*[^calon]"); |
---|
| 835 | CountedPtr< Scantable > ws = getScantable(s, false); |
---|
| 836 | uInt l=0; |
---|
| 837 | while ( l < sctables.size() ) { |
---|
| 838 | for (uInt i=0; i < 2; i++) { |
---|
| 839 | for (uInt j=0; j < 2; j++) { |
---|
| 840 | for (uInt k=0; k < 2; k++) { |
---|
| 841 | sel.reset(); |
---|
| 842 | sel.setScans(scanpair[i]); |
---|
| 843 | sel.setName(calstate[k]); |
---|
| 844 | beams.clear(); |
---|
| 845 | beams.push_back(j); |
---|
| 846 | sel.setBeams(beams); |
---|
| 847 | ws->setSelection(sel); |
---|
| 848 | sctables[l]= getScantable(ws, false); |
---|
| 849 | l++; |
---|
| 850 | } |
---|
| 851 | } |
---|
| 852 | } |
---|
| 853 | } |
---|
| 854 | |
---|
| 855 | // replace here by splitData or getData functionality |
---|
| 856 | CountedPtr< Scantable > sig1; |
---|
| 857 | CountedPtr< Scantable > ref1; |
---|
| 858 | CountedPtr< Scantable > sig2; |
---|
| 859 | CountedPtr< Scantable > ref2; |
---|
| 860 | CountedPtr< Scantable > calb1; |
---|
| 861 | CountedPtr< Scantable > calb2; |
---|
| 862 | |
---|
| 863 | msg=String("Processing dototalpower for subset of the data"); |
---|
| 864 | ostringstream oss1; |
---|
| 865 | oss1 << msg << endl; |
---|
| 866 | pushLog(String(oss1)); |
---|
| 867 | // Debug for IRC CS data |
---|
| 868 | //float tcal1=7.0; |
---|
| 869 | //float tcal2=4.0; |
---|
| 870 | sig1 = dototalpower(sctables[0], sctables[1], tcal=tcal); |
---|
| 871 | ref1 = dototalpower(sctables[2], sctables[3], tcal=tcal); |
---|
| 872 | ref2 = dototalpower(sctables[4], sctables[5], tcal=tcal); |
---|
| 873 | sig2 = dototalpower(sctables[6], sctables[7], tcal=tcal); |
---|
| 874 | |
---|
| 875 | // correction of user-specified tsys for elevation here |
---|
| 876 | |
---|
| 877 | // dosigref calibration |
---|
| 878 | msg=String("Processing dosigref for subset of the data"); |
---|
| 879 | ostringstream oss2; |
---|
| 880 | oss2 << msg << endl; |
---|
| 881 | pushLog(String(oss2)); |
---|
| 882 | calb1=dosigref(sig1,ref2,smoothref,tsysv,tau); |
---|
| 883 | calb2=dosigref(sig2,ref1,smoothref,tsysv,tau); |
---|
| 884 | |
---|
| 885 | // iteration by scanno or cycle no. |
---|
| 886 | Table& tcalb1 = calb1->table(); |
---|
| 887 | Table& tcalb2 = calb2->table(); |
---|
| 888 | TableIterator sit(tcalb1, "SCANNO"); |
---|
| 889 | TableIterator s2it(tcalb2, "SCANNO"); |
---|
| 890 | while ( !sit.pastEnd() ) { |
---|
| 891 | Table t1 = sit.table(); |
---|
| 892 | Table t2= s2it.table(); |
---|
| 893 | ArrayColumn<Float> outspecCol(t1, "SPECTRA"); |
---|
| 894 | ArrayColumn<Float> outtsysCol(t1, "TSYS"); |
---|
| 895 | ArrayColumn<uChar> outflagCol(t1, "FLAGTRA"); |
---|
| 896 | ScalarColumn<Double> outintCol(t1, "INTERVAL"); |
---|
| 897 | ArrayColumn<Float> t2specCol(t2, "SPECTRA"); |
---|
| 898 | ROArrayColumn<Float> t2tsysCol(t2, "TSYS"); |
---|
| 899 | ArrayColumn<uChar> t2flagCol(t2, "FLAGTRA"); |
---|
| 900 | ROScalarColumn<Double> t2intCol(t2, "INTERVAL"); |
---|
| 901 | for (uInt i=0; i < t1.nrow(); ++i) { |
---|
| 902 | Vector<Float> spec1, spec2; |
---|
| 903 | // to store scalar (mean) tsys |
---|
| 904 | Vector<Float> tsys1, tsys2; |
---|
| 905 | Vector<uChar> flag1, flag2; |
---|
| 906 | Double tint1, tint2; |
---|
| 907 | outspecCol.get(i, spec1); |
---|
| 908 | t2specCol.get(i, spec2); |
---|
| 909 | outflagCol.get(i, flag1); |
---|
| 910 | t2flagCol.get(i, flag2); |
---|
| 911 | outtsysCol.get(i, tsys1); |
---|
| 912 | t2tsysCol.get(i, tsys2); |
---|
| 913 | outintCol.get(i, tint1); |
---|
| 914 | t2intCol.get(i, tint2); |
---|
| 915 | // average |
---|
| 916 | // assume scalar tsys for weights |
---|
| 917 | Float wt1, wt2, tsyssq1, tsyssq2; |
---|
| 918 | tsyssq1 = tsys1[0]*tsys1[0]; |
---|
| 919 | tsyssq2 = tsys2[0]*tsys2[0]; |
---|
| 920 | wt1 = Float(tint1)/tsyssq1; |
---|
| 921 | wt2 = Float(tint2)/tsyssq2; |
---|
| 922 | Float invsumwt=1/(wt1+wt2); |
---|
| 923 | MaskedArray<Float> mspec1 = maskedArray(spec1, flag1); |
---|
| 924 | MaskedArray<Float> mspec2 = maskedArray(spec2, flag2); |
---|
| 925 | MaskedArray<Float> avspec = invsumwt * (wt1*mspec1 + wt2*mspec2); |
---|
| 926 | //Array<Float> avtsys = Float(0.5) * (tsys1 + tsys2); |
---|
| 927 | // cerr<< "Tsys1="<<tsys1<<" Tsys2="<<tsys2<<endl; |
---|
| 928 | tsys1[0] = sqrt(tsyssq1 + tsyssq2); |
---|
| 929 | Array<Float> avtsys = tsys1; |
---|
| 930 | |
---|
| 931 | outspecCol.put(i, avspec.getArray()); |
---|
| 932 | outflagCol.put(i, flagsFromMA(avspec)); |
---|
| 933 | outtsysCol.put(i, avtsys); |
---|
| 934 | } |
---|
| 935 | ++sit; |
---|
| 936 | ++s2it; |
---|
| 937 | } |
---|
| 938 | return calb1; |
---|
| 939 | } |
---|
| 940 | |
---|
| 941 | //GBTIDL version of frequency switched data calibration |
---|
| 942 | CountedPtr< Scantable > STMath::dofs( const CountedPtr< Scantable >& s, |
---|
| 943 | const std::vector<int>& scans, |
---|
| 944 | int smoothref, |
---|
| 945 | casa::Float tsysv, |
---|
| 946 | casa::Float tau, |
---|
| 947 | casa::Float tcal ) |
---|
| 948 | { |
---|
| 949 | |
---|
| 950 | |
---|
| 951 | STSelector sel; |
---|
| 952 | CountedPtr< Scantable > ws = getScantable(s, false); |
---|
| 953 | CountedPtr< Scantable > sig, sigwcal, ref, refwcal; |
---|
| 954 | CountedPtr< Scantable > calsig, calref, out; |
---|
| 955 | |
---|
| 956 | //split the data |
---|
| 957 | sel.setName("*_fs"); |
---|
| 958 | ws->setSelection(sel); |
---|
| 959 | sig = getScantable(ws,false); |
---|
| 960 | sel.reset(); |
---|
| 961 | sel.setName("*_fs_calon"); |
---|
| 962 | ws->setSelection(sel); |
---|
| 963 | sigwcal = getScantable(ws,false); |
---|
| 964 | sel.reset(); |
---|
| 965 | sel.setName("*_fsr"); |
---|
| 966 | ws->setSelection(sel); |
---|
| 967 | ref = getScantable(ws,false); |
---|
| 968 | sel.reset(); |
---|
| 969 | sel.setName("*_fsr_calon"); |
---|
| 970 | ws->setSelection(sel); |
---|
| 971 | refwcal = getScantable(ws,false); |
---|
| 972 | |
---|
| 973 | calsig = dototalpower(sigwcal, sig, tcal=tcal); |
---|
| 974 | calref = dototalpower(refwcal, ref, tcal=tcal); |
---|
| 975 | |
---|
| 976 | out=dosigref(calsig,calref,smoothref,tsysv,tau); |
---|
| 977 | |
---|
| 978 | return out; |
---|
| 979 | } |
---|
| 980 | |
---|
| 981 | |
---|
[805] | 982 | CountedPtr< Scantable > STMath::freqSwitch( const CountedPtr< Scantable >& in ) |
---|
| 983 | { |
---|
| 984 | // make copy or reference |
---|
| 985 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 986 | Table& tout = out->table(); |
---|
[1008] | 987 | Block<String> cols(4); |
---|
[805] | 988 | cols[0] = String("SCANNO"); |
---|
[1008] | 989 | cols[1] = String("CYCLENO"); |
---|
| 990 | cols[2] = String("BEAMNO"); |
---|
| 991 | cols[3] = String("POLNO"); |
---|
[805] | 992 | TableIterator iter(tout, cols); |
---|
| 993 | while (!iter.pastEnd()) { |
---|
| 994 | Table subt = iter.table(); |
---|
| 995 | // this should leave us with two rows for the two IFs....if not ignore |
---|
| 996 | if (subt.nrow() != 2 ) { |
---|
| 997 | continue; |
---|
[701] | 998 | } |
---|
[1008] | 999 | ArrayColumn<Float> specCol(subt, "SPECTRA"); |
---|
| 1000 | ArrayColumn<Float> tsysCol(subt, "TSYS"); |
---|
| 1001 | ArrayColumn<uChar> flagCol(subt, "FLAGTRA"); |
---|
[805] | 1002 | Vector<Float> onspec,offspec, ontsys, offtsys; |
---|
| 1003 | Vector<uChar> onflag, offflag; |
---|
| 1004 | tsysCol.get(0, ontsys); tsysCol.get(1, offtsys); |
---|
| 1005 | specCol.get(0, onspec); specCol.get(1, offspec); |
---|
| 1006 | flagCol.get(0, onflag); flagCol.get(1, offflag); |
---|
| 1007 | MaskedArray<Float> on = maskedArray(onspec, onflag); |
---|
| 1008 | MaskedArray<Float> off = maskedArray(offspec, offflag); |
---|
| 1009 | MaskedArray<Float> oncopy = on.copy(); |
---|
[248] | 1010 | |
---|
[805] | 1011 | on /= off; on -= 1.0f; |
---|
| 1012 | on *= ontsys[0]; |
---|
| 1013 | off /= oncopy; off -= 1.0f; |
---|
| 1014 | off *= offtsys[0]; |
---|
| 1015 | specCol.put(0, on.getArray()); |
---|
| 1016 | const Vector<Bool>& m0 = on.getMask(); |
---|
| 1017 | Vector<uChar> flags0(m0.shape()); |
---|
| 1018 | convertArray(flags0, !m0); |
---|
| 1019 | flagCol.put(0, flags0); |
---|
[234] | 1020 | |
---|
[805] | 1021 | specCol.put(1, off.getArray()); |
---|
| 1022 | const Vector<Bool>& m1 = off.getMask(); |
---|
| 1023 | Vector<uChar> flags1(m1.shape()); |
---|
| 1024 | convertArray(flags1, !m1); |
---|
| 1025 | flagCol.put(1, flags1); |
---|
[867] | 1026 | ++iter; |
---|
[130] | 1027 | } |
---|
[780] | 1028 | |
---|
[805] | 1029 | return out; |
---|
[9] | 1030 | } |
---|
[48] | 1031 | |
---|
[805] | 1032 | std::vector< float > STMath::statistic( const CountedPtr< Scantable > & in, |
---|
| 1033 | const std::vector< bool > & mask, |
---|
| 1034 | const std::string& which ) |
---|
[130] | 1035 | { |
---|
| 1036 | |
---|
[805] | 1037 | Vector<Bool> m(mask); |
---|
| 1038 | const Table& tab = in->table(); |
---|
| 1039 | ROArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 1040 | ROArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 1041 | std::vector<float> out; |
---|
| 1042 | for (uInt i=0; i < tab.nrow(); ++i ) { |
---|
| 1043 | Vector<Float> spec; specCol.get(i, spec); |
---|
[867] | 1044 | Vector<uChar> flag; flagCol.get(i, flag); |
---|
| 1045 | MaskedArray<Float> ma = maskedArray(spec, flag); |
---|
| 1046 | float outstat = 0.0; |
---|
[805] | 1047 | if ( spec.nelements() == m.nelements() ) { |
---|
| 1048 | outstat = mathutil::statistics(which, ma(m)); |
---|
| 1049 | } else { |
---|
| 1050 | outstat = mathutil::statistics(which, ma); |
---|
| 1051 | } |
---|
| 1052 | out.push_back(outstat); |
---|
[234] | 1053 | } |
---|
[805] | 1054 | return out; |
---|
[130] | 1055 | } |
---|
| 1056 | |
---|
[805] | 1057 | CountedPtr< Scantable > STMath::bin( const CountedPtr< Scantable > & in, |
---|
| 1058 | int width ) |
---|
[144] | 1059 | { |
---|
[841] | 1060 | if ( !in->getSelection().empty() ) throw(AipsError("Can't bin subset of the data.")); |
---|
[805] | 1061 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 1062 | Table& tout = out->table(); |
---|
| 1063 | out->frequencies().rescale(width, "BIN"); |
---|
| 1064 | ArrayColumn<Float> specCol(tout, "SPECTRA"); |
---|
| 1065 | ArrayColumn<uChar> flagCol(tout, "FLAGTRA"); |
---|
| 1066 | for (uInt i=0; i < tout.nrow(); ++i ) { |
---|
| 1067 | MaskedArray<Float> main = maskedArray(specCol(i), flagCol(i)); |
---|
| 1068 | MaskedArray<Float> maout; |
---|
| 1069 | LatticeUtilities::bin(maout, main, 0, Int(width)); |
---|
| 1070 | /// @todo implement channel based tsys binning |
---|
| 1071 | specCol.put(i, maout.getArray()); |
---|
| 1072 | flagCol.put(i, flagsFromMA(maout)); |
---|
| 1073 | // take only the first binned spectrum's length for the deprecated |
---|
| 1074 | // global header item nChan |
---|
| 1075 | if (i==0) tout.rwKeywordSet().define(String("nChan"), |
---|
| 1076 | Int(maout.getArray().nelements())); |
---|
[169] | 1077 | } |
---|
[805] | 1078 | return out; |
---|
[146] | 1079 | } |
---|
| 1080 | |
---|
[805] | 1081 | CountedPtr< Scantable > STMath::resample( const CountedPtr< Scantable >& in, |
---|
| 1082 | const std::string& method, |
---|
| 1083 | float width ) |
---|
[299] | 1084 | // |
---|
| 1085 | // Should add the possibility of width being specified in km/s. This means |
---|
[780] | 1086 | // that for each freqID (SpectralCoordinate) we will need to convert to an |
---|
| 1087 | // average channel width (say at the reference pixel). Then we would need |
---|
| 1088 | // to be careful to make sure each spectrum (of different freqID) |
---|
[299] | 1089 | // is the same length. |
---|
| 1090 | // |
---|
| 1091 | { |
---|
[996] | 1092 | //InterpolateArray1D<Double,Float>::InterpolationMethod interp; |
---|
[805] | 1093 | Int interpMethod(stringToIMethod(method)); |
---|
[299] | 1094 | |
---|
[805] | 1095 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 1096 | Table& tout = out->table(); |
---|
[299] | 1097 | |
---|
| 1098 | // Resample SpectralCoordinates (one per freqID) |
---|
[805] | 1099 | out->frequencies().rescale(width, "RESAMPLE"); |
---|
| 1100 | TableIterator iter(tout, "IFNO"); |
---|
| 1101 | TableRow row(tout); |
---|
| 1102 | while ( !iter.pastEnd() ) { |
---|
| 1103 | Table tab = iter.table(); |
---|
| 1104 | ArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 1105 | //ArrayColumn<Float> tsysCol(tout, "TSYS"); |
---|
| 1106 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 1107 | Vector<Float> spec; |
---|
| 1108 | Vector<uChar> flag; |
---|
| 1109 | specCol.get(0,spec); // the number of channels should be constant per IF |
---|
| 1110 | uInt nChanIn = spec.nelements(); |
---|
| 1111 | Vector<Float> xIn(nChanIn); indgen(xIn); |
---|
| 1112 | Int fac = Int(nChanIn/width); |
---|
| 1113 | Vector<Float> xOut(fac+10); // 10 to be safe - resize later |
---|
| 1114 | uInt k = 0; |
---|
| 1115 | Float x = 0.0; |
---|
| 1116 | while (x < Float(nChanIn) ) { |
---|
| 1117 | xOut(k) = x; |
---|
| 1118 | k++; |
---|
| 1119 | x += width; |
---|
| 1120 | } |
---|
| 1121 | uInt nChanOut = k; |
---|
| 1122 | xOut.resize(nChanOut, True); |
---|
| 1123 | // process all rows for this IFNO |
---|
| 1124 | Vector<Float> specOut; |
---|
| 1125 | Vector<Bool> maskOut; |
---|
| 1126 | Vector<uChar> flagOut; |
---|
| 1127 | for (uInt i=0; i < tab.nrow(); ++i) { |
---|
| 1128 | specCol.get(i, spec); |
---|
| 1129 | flagCol.get(i, flag); |
---|
| 1130 | Vector<Bool> mask(flag.nelements()); |
---|
| 1131 | convertArray(mask, flag); |
---|
[299] | 1132 | |
---|
[805] | 1133 | IPosition shapeIn(spec.shape()); |
---|
| 1134 | //sh.nchan = nChanOut; |
---|
| 1135 | InterpolateArray1D<Float,Float>::interpolate(specOut, maskOut, xOut, |
---|
| 1136 | xIn, spec, mask, |
---|
| 1137 | interpMethod, True, True); |
---|
| 1138 | /// @todo do the same for channel based Tsys |
---|
| 1139 | flagOut.resize(maskOut.nelements()); |
---|
| 1140 | convertArray(flagOut, maskOut); |
---|
| 1141 | specCol.put(i, specOut); |
---|
| 1142 | flagCol.put(i, flagOut); |
---|
| 1143 | } |
---|
| 1144 | ++iter; |
---|
[299] | 1145 | } |
---|
| 1146 | |
---|
[805] | 1147 | return out; |
---|
| 1148 | } |
---|
[299] | 1149 | |
---|
[805] | 1150 | STMath::imethod STMath::stringToIMethod(const std::string& in) |
---|
| 1151 | { |
---|
| 1152 | static STMath::imap lookup; |
---|
[299] | 1153 | |
---|
[805] | 1154 | // initialize the lookup table if necessary |
---|
| 1155 | if ( lookup.empty() ) { |
---|
[926] | 1156 | lookup["nearest"] = InterpolateArray1D<Double,Float>::nearestNeighbour; |
---|
| 1157 | lookup["linear"] = InterpolateArray1D<Double,Float>::linear; |
---|
| 1158 | lookup["cubic"] = InterpolateArray1D<Double,Float>::cubic; |
---|
| 1159 | lookup["spline"] = InterpolateArray1D<Double,Float>::spline; |
---|
[299] | 1160 | } |
---|
| 1161 | |
---|
[805] | 1162 | STMath::imap::const_iterator iter = lookup.find(in); |
---|
[299] | 1163 | |
---|
[805] | 1164 | if ( lookup.end() == iter ) { |
---|
| 1165 | std::string message = in; |
---|
| 1166 | message += " is not a valid interpolation mode"; |
---|
| 1167 | throw(AipsError(message)); |
---|
[299] | 1168 | } |
---|
[805] | 1169 | return iter->second; |
---|
[299] | 1170 | } |
---|
| 1171 | |
---|
[805] | 1172 | WeightType STMath::stringToWeight(const std::string& in) |
---|
[146] | 1173 | { |
---|
[805] | 1174 | static std::map<std::string, WeightType> lookup; |
---|
[434] | 1175 | |
---|
[805] | 1176 | // initialize the lookup table if necessary |
---|
| 1177 | if ( lookup.empty() ) { |
---|
| 1178 | lookup["NONE"] = asap::NONE; |
---|
| 1179 | lookup["TINT"] = asap::TINT; |
---|
| 1180 | lookup["TINTSYS"] = asap::TINTSYS; |
---|
| 1181 | lookup["TSYS"] = asap::TSYS; |
---|
| 1182 | lookup["VAR"] = asap::VAR; |
---|
| 1183 | } |
---|
[434] | 1184 | |
---|
[805] | 1185 | std::map<std::string, WeightType>::const_iterator iter = lookup.find(in); |
---|
[294] | 1186 | |
---|
[805] | 1187 | if ( lookup.end() == iter ) { |
---|
| 1188 | std::string message = in; |
---|
| 1189 | message += " is not a valid weighting mode"; |
---|
| 1190 | throw(AipsError(message)); |
---|
| 1191 | } |
---|
| 1192 | return iter->second; |
---|
[146] | 1193 | } |
---|
| 1194 | |
---|
[805] | 1195 | CountedPtr< Scantable > STMath::gainElevation( const CountedPtr< Scantable >& in, |
---|
[867] | 1196 | const vector< float > & coeff, |
---|
[805] | 1197 | const std::string & filename, |
---|
| 1198 | const std::string& method) |
---|
[165] | 1199 | { |
---|
[805] | 1200 | // Get elevation data from Scantable and convert to degrees |
---|
| 1201 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
[926] | 1202 | Table& tab = out->table(); |
---|
[805] | 1203 | ROScalarColumn<Float> elev(tab, "ELEVATION"); |
---|
| 1204 | Vector<Float> x = elev.getColumn(); |
---|
| 1205 | x *= Float(180 / C::pi); // Degrees |
---|
[165] | 1206 | |
---|
[867] | 1207 | Vector<Float> coeffs(coeff); |
---|
[805] | 1208 | const uInt nc = coeffs.nelements(); |
---|
| 1209 | if ( filename.length() > 0 && nc > 0 ) { |
---|
| 1210 | throw(AipsError("You must choose either polynomial coefficients or an ascii file, not both")); |
---|
[315] | 1211 | } |
---|
[165] | 1212 | |
---|
[805] | 1213 | // Correct |
---|
| 1214 | if ( nc > 0 || filename.length() == 0 ) { |
---|
| 1215 | // Find instrument |
---|
| 1216 | Bool throwit = True; |
---|
| 1217 | Instrument inst = |
---|
[878] | 1218 | STAttr::convertInstrument(tab.keywordSet().asString("AntennaName"), |
---|
[805] | 1219 | throwit); |
---|
[165] | 1220 | |
---|
[805] | 1221 | // Set polynomial |
---|
| 1222 | Polynomial<Float>* ppoly = 0; |
---|
| 1223 | Vector<Float> coeff; |
---|
| 1224 | String msg; |
---|
| 1225 | if ( nc > 0 ) { |
---|
| 1226 | ppoly = new Polynomial<Float>(nc); |
---|
| 1227 | coeff = coeffs; |
---|
| 1228 | msg = String("user"); |
---|
| 1229 | } else { |
---|
[878] | 1230 | STAttr sdAttr; |
---|
[805] | 1231 | coeff = sdAttr.gainElevationPoly(inst); |
---|
| 1232 | ppoly = new Polynomial<Float>(3); |
---|
| 1233 | msg = String("built in"); |
---|
| 1234 | } |
---|
[532] | 1235 | |
---|
[805] | 1236 | if ( coeff.nelements() > 0 ) { |
---|
| 1237 | ppoly->setCoefficients(coeff); |
---|
| 1238 | } else { |
---|
| 1239 | delete ppoly; |
---|
| 1240 | throw(AipsError("There is no known gain-elevation polynomial known for this instrument")); |
---|
| 1241 | } |
---|
| 1242 | ostringstream oss; |
---|
| 1243 | oss << "Making polynomial correction with " << msg << " coefficients:" << endl; |
---|
| 1244 | oss << " " << coeff; |
---|
| 1245 | pushLog(String(oss)); |
---|
| 1246 | const uInt nrow = tab.nrow(); |
---|
| 1247 | Vector<Float> factor(nrow); |
---|
| 1248 | for ( uInt i=0; i < nrow; ++i ) { |
---|
| 1249 | factor[i] = 1.0 / (*ppoly)(x[i]); |
---|
| 1250 | } |
---|
| 1251 | delete ppoly; |
---|
| 1252 | scaleByVector(tab, factor, true); |
---|
[532] | 1253 | |
---|
[805] | 1254 | } else { |
---|
| 1255 | // Read and correct |
---|
| 1256 | pushLog("Making correction from ascii Table"); |
---|
| 1257 | scaleFromAsciiTable(tab, filename, method, x, true); |
---|
[532] | 1258 | } |
---|
[805] | 1259 | return out; |
---|
| 1260 | } |
---|
[165] | 1261 | |
---|
[805] | 1262 | void STMath::scaleFromAsciiTable(Table& in, const std::string& filename, |
---|
| 1263 | const std::string& method, |
---|
| 1264 | const Vector<Float>& xout, bool dotsys) |
---|
| 1265 | { |
---|
[165] | 1266 | |
---|
[805] | 1267 | // Read gain-elevation ascii file data into a Table. |
---|
[165] | 1268 | |
---|
[805] | 1269 | String formatString; |
---|
| 1270 | Table tbl = readAsciiTable(formatString, Table::Memory, filename, "", "", False); |
---|
| 1271 | scaleFromTable(in, tbl, method, xout, dotsys); |
---|
| 1272 | } |
---|
[165] | 1273 | |
---|
[805] | 1274 | void STMath::scaleFromTable(Table& in, |
---|
| 1275 | const Table& table, |
---|
| 1276 | const std::string& method, |
---|
| 1277 | const Vector<Float>& xout, bool dotsys) |
---|
| 1278 | { |
---|
[780] | 1279 | |
---|
[805] | 1280 | ROScalarColumn<Float> geElCol(table, "ELEVATION"); |
---|
| 1281 | ROScalarColumn<Float> geFacCol(table, "FACTOR"); |
---|
| 1282 | Vector<Float> xin = geElCol.getColumn(); |
---|
| 1283 | Vector<Float> yin = geFacCol.getColumn(); |
---|
| 1284 | Vector<Bool> maskin(xin.nelements(),True); |
---|
[165] | 1285 | |
---|
[805] | 1286 | // Interpolate (and extrapolate) with desired method |
---|
[532] | 1287 | |
---|
[996] | 1288 | InterpolateArray1D<Double,Float>::InterpolationMethod interp = stringToIMethod(method); |
---|
[165] | 1289 | |
---|
[805] | 1290 | Vector<Float> yout; |
---|
| 1291 | Vector<Bool> maskout; |
---|
| 1292 | InterpolateArray1D<Float,Float>::interpolate(yout, maskout, xout, |
---|
[996] | 1293 | xin, yin, maskin, interp, |
---|
[805] | 1294 | True, True); |
---|
[165] | 1295 | |
---|
[805] | 1296 | scaleByVector(in, Float(1.0)/yout, dotsys); |
---|
[165] | 1297 | } |
---|
[167] | 1298 | |
---|
[805] | 1299 | void STMath::scaleByVector( Table& in, |
---|
| 1300 | const Vector< Float >& factor, |
---|
| 1301 | bool dotsys ) |
---|
[177] | 1302 | { |
---|
[805] | 1303 | uInt nrow = in.nrow(); |
---|
| 1304 | if ( factor.nelements() != nrow ) { |
---|
| 1305 | throw(AipsError("factors.nelements() != table.nelements()")); |
---|
| 1306 | } |
---|
| 1307 | ArrayColumn<Float> specCol(in, "SPECTRA"); |
---|
| 1308 | ArrayColumn<uChar> flagCol(in, "FLAGTRA"); |
---|
| 1309 | ArrayColumn<Float> tsysCol(in, "TSYS"); |
---|
| 1310 | for (uInt i=0; i < nrow; ++i) { |
---|
| 1311 | MaskedArray<Float> ma = maskedArray(specCol(i), flagCol(i)); |
---|
| 1312 | ma *= factor[i]; |
---|
| 1313 | specCol.put(i, ma.getArray()); |
---|
| 1314 | flagCol.put(i, flagsFromMA(ma)); |
---|
| 1315 | if ( dotsys ) { |
---|
[926] | 1316 | Vector<Float> tsys = tsysCol(i); |
---|
[805] | 1317 | tsys *= factor[i]; |
---|
[926] | 1318 | tsysCol.put(i,tsys); |
---|
[805] | 1319 | } |
---|
| 1320 | } |
---|
[177] | 1321 | } |
---|
| 1322 | |
---|
[805] | 1323 | CountedPtr< Scantable > STMath::convertFlux( const CountedPtr< Scantable >& in, |
---|
| 1324 | float d, float etaap, |
---|
| 1325 | float jyperk ) |
---|
[221] | 1326 | { |
---|
[805] | 1327 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 1328 | Table& tab = in->table(); |
---|
| 1329 | Unit fluxUnit(tab.keywordSet().asString("FluxUnit")); |
---|
[221] | 1330 | Unit K(String("K")); |
---|
| 1331 | Unit JY(String("Jy")); |
---|
[701] | 1332 | |
---|
[805] | 1333 | bool tokelvin = true; |
---|
| 1334 | Double cfac = 1.0; |
---|
[716] | 1335 | |
---|
[805] | 1336 | if ( fluxUnit == JY ) { |
---|
[716] | 1337 | pushLog("Converting to K"); |
---|
[701] | 1338 | Quantum<Double> t(1.0,fluxUnit); |
---|
| 1339 | Quantum<Double> t2 = t.get(JY); |
---|
[805] | 1340 | cfac = (t2 / t).getValue(); // value to Jy |
---|
[780] | 1341 | |
---|
[805] | 1342 | tokelvin = true; |
---|
| 1343 | out->setFluxUnit("K"); |
---|
| 1344 | } else if ( fluxUnit == K ) { |
---|
[716] | 1345 | pushLog("Converting to Jy"); |
---|
[701] | 1346 | Quantum<Double> t(1.0,fluxUnit); |
---|
| 1347 | Quantum<Double> t2 = t.get(K); |
---|
[805] | 1348 | cfac = (t2 / t).getValue(); // value to K |
---|
[780] | 1349 | |
---|
[805] | 1350 | tokelvin = false; |
---|
| 1351 | out->setFluxUnit("Jy"); |
---|
[221] | 1352 | } else { |
---|
[701] | 1353 | throw(AipsError("Unrecognized brightness units in Table - must be consistent with Jy or K")); |
---|
[221] | 1354 | } |
---|
[701] | 1355 | // Make sure input values are converted to either Jy or K first... |
---|
[805] | 1356 | Float factor = cfac; |
---|
[221] | 1357 | |
---|
[701] | 1358 | // Select method |
---|
[805] | 1359 | if (jyperk > 0.0) { |
---|
| 1360 | factor *= jyperk; |
---|
| 1361 | if ( tokelvin ) factor = 1.0 / jyperk; |
---|
[716] | 1362 | ostringstream oss; |
---|
[805] | 1363 | oss << "Jy/K = " << jyperk; |
---|
[716] | 1364 | pushLog(String(oss)); |
---|
[805] | 1365 | Vector<Float> factors(tab.nrow(), factor); |
---|
| 1366 | scaleByVector(tab,factors, false); |
---|
| 1367 | } else if ( etaap > 0.0) { |
---|
[1319] | 1368 | if (d < 0) { |
---|
| 1369 | Instrument inst = |
---|
| 1370 | STAttr::convertInstrument(tab.keywordSet().asString("AntennaName"), |
---|
| 1371 | True); |
---|
| 1372 | STAttr sda; |
---|
| 1373 | d = sda.diameter(inst); |
---|
| 1374 | } |
---|
[996] | 1375 | jyperk = STAttr::findJyPerK(etaap, d); |
---|
[716] | 1376 | ostringstream oss; |
---|
[805] | 1377 | oss << "Jy/K = " << jyperk; |
---|
[716] | 1378 | pushLog(String(oss)); |
---|
[805] | 1379 | factor *= jyperk; |
---|
| 1380 | if ( tokelvin ) { |
---|
[701] | 1381 | factor = 1.0 / factor; |
---|
| 1382 | } |
---|
[805] | 1383 | Vector<Float> factors(tab.nrow(), factor); |
---|
| 1384 | scaleByVector(tab, factors, False); |
---|
[354] | 1385 | } else { |
---|
[780] | 1386 | |
---|
[701] | 1387 | // OK now we must deal with automatic look up of values. |
---|
| 1388 | // We must also deal with the fact that the factors need |
---|
| 1389 | // to be computed per IF and may be different and may |
---|
| 1390 | // change per integration. |
---|
[780] | 1391 | |
---|
[716] | 1392 | pushLog("Looking up conversion factors"); |
---|
[805] | 1393 | convertBrightnessUnits(out, tokelvin, cfac); |
---|
[701] | 1394 | } |
---|
[805] | 1395 | |
---|
| 1396 | return out; |
---|
[221] | 1397 | } |
---|
| 1398 | |
---|
[805] | 1399 | void STMath::convertBrightnessUnits( CountedPtr<Scantable>& in, |
---|
| 1400 | bool tokelvin, float cfac ) |
---|
[227] | 1401 | { |
---|
[805] | 1402 | Table& table = in->table(); |
---|
| 1403 | Instrument inst = |
---|
[878] | 1404 | STAttr::convertInstrument(table.keywordSet().asString("AntennaName"), True); |
---|
[805] | 1405 | TableIterator iter(table, "FREQ_ID"); |
---|
| 1406 | STFrequencies stfreqs = in->frequencies(); |
---|
[878] | 1407 | STAttr sdAtt; |
---|
[805] | 1408 | while (!iter.pastEnd()) { |
---|
| 1409 | Table tab = iter.table(); |
---|
| 1410 | ArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 1411 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 1412 | ROScalarColumn<uInt> freqidCol(tab, "FREQ_ID"); |
---|
| 1413 | MEpoch::ROScalarColumn timeCol(tab, "TIME"); |
---|
[234] | 1414 | |
---|
[805] | 1415 | uInt freqid; freqidCol.get(0, freqid); |
---|
| 1416 | Vector<Float> tmpspec; specCol.get(0, tmpspec); |
---|
[878] | 1417 | // STAttr.JyPerK has a Vector interface... change sometime. |
---|
[805] | 1418 | Vector<Float> freqs(1,stfreqs.getRefFreq(freqid, tmpspec.nelements())); |
---|
| 1419 | for ( uInt i=0; i<tab.nrow(); ++i) { |
---|
| 1420 | Float jyperk = (sdAtt.JyPerK(inst, timeCol(i), freqs))[0]; |
---|
| 1421 | Float factor = cfac * jyperk; |
---|
| 1422 | if ( tokelvin ) factor = Float(1.0) / factor; |
---|
| 1423 | MaskedArray<Float> ma = maskedArray(specCol(i), flagCol(i)); |
---|
| 1424 | ma *= factor; |
---|
| 1425 | specCol.put(i, ma.getArray()); |
---|
| 1426 | flagCol.put(i, flagsFromMA(ma)); |
---|
| 1427 | } |
---|
[867] | 1428 | ++iter; |
---|
[234] | 1429 | } |
---|
[230] | 1430 | } |
---|
[227] | 1431 | |
---|
[805] | 1432 | CountedPtr< Scantable > STMath::opacity( const CountedPtr< Scantable > & in, |
---|
| 1433 | float tau ) |
---|
[234] | 1434 | { |
---|
[805] | 1435 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
[926] | 1436 | |
---|
| 1437 | Table tab = out->table(); |
---|
[234] | 1438 | ROScalarColumn<Float> elev(tab, "ELEVATION"); |
---|
[805] | 1439 | ArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 1440 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
[1484] | 1441 | ArrayColumn<Float> tsysCol(tab, "TSYS"); |
---|
[805] | 1442 | for ( uInt i=0; i<tab.nrow(); ++i) { |
---|
| 1443 | Float zdist = Float(C::pi_2) - elev(i); |
---|
[1318] | 1444 | Float factor = exp(tau/cos(zdist)); |
---|
[926] | 1445 | MaskedArray<Float> ma = maskedArray(specCol(i), flagCol(i)); |
---|
[805] | 1446 | ma *= factor; |
---|
| 1447 | specCol.put(i, ma.getArray()); |
---|
| 1448 | flagCol.put(i, flagsFromMA(ma)); |
---|
[1484] | 1449 | Vector<Float> tsys; |
---|
| 1450 | tsysCol.get(i, tsys); |
---|
| 1451 | tsys *= factor; |
---|
| 1452 | tsysCol.put(i, tsys); |
---|
[234] | 1453 | } |
---|
[805] | 1454 | return out; |
---|
[234] | 1455 | } |
---|
| 1456 | |
---|
[1373] | 1457 | CountedPtr< Scantable > STMath::smoothOther( const CountedPtr< Scantable >& in, |
---|
| 1458 | const std::string& kernel, |
---|
| 1459 | float width ) |
---|
| 1460 | { |
---|
| 1461 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 1462 | Table& table = out->table(); |
---|
| 1463 | ArrayColumn<Float> specCol(table, "SPECTRA"); |
---|
| 1464 | ArrayColumn<uChar> flagCol(table, "FLAGTRA"); |
---|
| 1465 | Vector<Float> spec; |
---|
| 1466 | Vector<uChar> flag; |
---|
| 1467 | for ( uInt i=0; i<table.nrow(); ++i) { |
---|
| 1468 | specCol.get(i, spec); |
---|
| 1469 | flagCol.get(i, flag); |
---|
| 1470 | Vector<Bool> mask(flag.nelements()); |
---|
| 1471 | convertArray(mask, flag); |
---|
| 1472 | Vector<Float> specout; |
---|
| 1473 | Vector<Bool> maskout; |
---|
| 1474 | if ( kernel == "hanning" ) { |
---|
| 1475 | mathutil::hanning(specout, maskout, spec , !mask); |
---|
| 1476 | convertArray(flag, !maskout); |
---|
| 1477 | } else if ( kernel == "rmedian" ) { |
---|
| 1478 | mathutil::runningMedian(specout, maskout, spec , mask, width); |
---|
| 1479 | convertArray(flag, maskout); |
---|
| 1480 | } |
---|
| 1481 | flagCol.put(i, flag); |
---|
| 1482 | specCol.put(i, specout); |
---|
| 1483 | } |
---|
| 1484 | return out; |
---|
| 1485 | } |
---|
| 1486 | |
---|
[805] | 1487 | CountedPtr< Scantable > STMath::smooth( const CountedPtr< Scantable >& in, |
---|
| 1488 | const std::string& kernel, float width ) |
---|
[457] | 1489 | { |
---|
[1373] | 1490 | if (kernel == "rmedian" || kernel == "hanning") { |
---|
| 1491 | return smoothOther(in, kernel, width); |
---|
| 1492 | } |
---|
[805] | 1493 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
[1033] | 1494 | Table& table = out->table(); |
---|
[805] | 1495 | VectorKernel::KernelTypes type = VectorKernel::toKernelType(kernel); |
---|
| 1496 | // same IFNO should have same no of channels |
---|
| 1497 | // this saves overhead |
---|
| 1498 | TableIterator iter(table, "IFNO"); |
---|
| 1499 | while (!iter.pastEnd()) { |
---|
| 1500 | Table tab = iter.table(); |
---|
| 1501 | ArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 1502 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 1503 | Vector<Float> tmpspec; specCol.get(0, tmpspec); |
---|
| 1504 | uInt nchan = tmpspec.nelements(); |
---|
| 1505 | Vector<Float> kvec = VectorKernel::make(type, width, nchan, True, False); |
---|
| 1506 | Convolver<Float> conv(kvec, IPosition(1,nchan)); |
---|
| 1507 | Vector<Float> spec; |
---|
| 1508 | Vector<uChar> flag; |
---|
| 1509 | for ( uInt i=0; i<tab.nrow(); ++i) { |
---|
| 1510 | specCol.get(i, spec); |
---|
| 1511 | flagCol.get(i, flag); |
---|
| 1512 | Vector<Bool> mask(flag.nelements()); |
---|
| 1513 | convertArray(mask, flag); |
---|
| 1514 | Vector<Float> specout; |
---|
[1373] | 1515 | mathutil::replaceMaskByZero(specout, mask); |
---|
| 1516 | conv.linearConv(specout, spec); |
---|
| 1517 | specCol.put(i, specout); |
---|
[805] | 1518 | } |
---|
[867] | 1519 | ++iter; |
---|
[701] | 1520 | } |
---|
[805] | 1521 | return out; |
---|
[701] | 1522 | } |
---|
[841] | 1523 | |
---|
| 1524 | CountedPtr< Scantable > |
---|
| 1525 | STMath::merge( const std::vector< CountedPtr < Scantable > >& in ) |
---|
| 1526 | { |
---|
| 1527 | if ( in.size() < 2 ) { |
---|
[862] | 1528 | throw(AipsError("Need at least two scantables to perform a merge.")); |
---|
[841] | 1529 | } |
---|
| 1530 | std::vector<CountedPtr < Scantable > >::const_iterator it = in.begin(); |
---|
| 1531 | bool insitu = insitu_; |
---|
| 1532 | setInsitu(false); |
---|
[862] | 1533 | CountedPtr< Scantable > out = getScantable(*it, false); |
---|
[841] | 1534 | setInsitu(insitu); |
---|
| 1535 | Table& tout = out->table(); |
---|
| 1536 | ScalarColumn<uInt> freqidcol(tout,"FREQ_ID"), molidcol(tout, "MOLECULE_ID"); |
---|
[917] | 1537 | ScalarColumn<uInt> scannocol(tout,"SCANNO"), focusidcol(tout,"FOCUS_ID"); |
---|
| 1538 | // Renumber SCANNO to be 0-based |
---|
[926] | 1539 | Vector<uInt> scannos = scannocol.getColumn(); |
---|
| 1540 | uInt offset = min(scannos); |
---|
[917] | 1541 | scannos -= offset; |
---|
[926] | 1542 | scannocol.putColumn(scannos); |
---|
| 1543 | uInt newscanno = max(scannos)+1; |
---|
[862] | 1544 | ++it; |
---|
[841] | 1545 | while ( it != in.end() ){ |
---|
| 1546 | if ( ! (*it)->conformant(*out) ) { |
---|
[1439] | 1547 | // non conformant. |
---|
[1416] | 1548 | pushLog(String("Warning: Can't merge scantables as header info differs.")); |
---|
[841] | 1549 | } |
---|
[862] | 1550 | out->appendToHistoryTable((*it)->history()); |
---|
[841] | 1551 | const Table& tab = (*it)->table(); |
---|
| 1552 | TableIterator scanit(tab, "SCANNO"); |
---|
| 1553 | while (!scanit.pastEnd()) { |
---|
| 1554 | TableIterator freqit(scanit.table(), "FREQ_ID"); |
---|
| 1555 | while ( !freqit.pastEnd() ) { |
---|
| 1556 | Table thetab = freqit.table(); |
---|
| 1557 | uInt nrow = tout.nrow(); |
---|
[1505] | 1558 | tout.addRow(thetab.nrow()); |
---|
[841] | 1559 | TableCopy::copyRows(tout, thetab, nrow, 0, thetab.nrow()); |
---|
| 1560 | ROTableRow row(thetab); |
---|
| 1561 | for ( uInt i=0; i<thetab.nrow(); ++i) { |
---|
| 1562 | uInt k = nrow+i; |
---|
| 1563 | scannocol.put(k, newscanno); |
---|
| 1564 | const TableRecord& rec = row.get(i); |
---|
| 1565 | Double rv,rp,inc; |
---|
| 1566 | (*it)->frequencies().getEntry(rp, rv, inc, rec.asuInt("FREQ_ID")); |
---|
| 1567 | uInt id; |
---|
| 1568 | id = out->frequencies().addEntry(rp, rv, inc); |
---|
| 1569 | freqidcol.put(k,id); |
---|
| 1570 | String name,fname;Double rf; |
---|
| 1571 | (*it)->molecules().getEntry(rf, name, fname, rec.asuInt("MOLECULE_ID")); |
---|
| 1572 | id = out->molecules().addEntry(rf, name, fname); |
---|
| 1573 | molidcol.put(k, id); |
---|
[961] | 1574 | Float frot,fax,ftan,fhand,fmount,fuser, fxy, fxyp; |
---|
| 1575 | (*it)->focus().getEntry(fax, ftan, frot, fhand, |
---|
| 1576 | fmount,fuser, fxy, fxyp, |
---|
| 1577 | rec.asuInt("FOCUS_ID")); |
---|
| 1578 | id = out->focus().addEntry(fax, ftan, frot, fhand, |
---|
| 1579 | fmount,fuser, fxy, fxyp); |
---|
[841] | 1580 | focusidcol.put(k, id); |
---|
| 1581 | } |
---|
| 1582 | ++freqit; |
---|
| 1583 | } |
---|
| 1584 | ++newscanno; |
---|
| 1585 | ++scanit; |
---|
| 1586 | } |
---|
| 1587 | ++it; |
---|
| 1588 | } |
---|
| 1589 | return out; |
---|
| 1590 | } |
---|
[896] | 1591 | |
---|
| 1592 | CountedPtr< Scantable > |
---|
| 1593 | STMath::invertPhase( const CountedPtr < Scantable >& in ) |
---|
| 1594 | { |
---|
[996] | 1595 | return applyToPol(in, &STPol::invertPhase, Float(0.0)); |
---|
[896] | 1596 | } |
---|
| 1597 | |
---|
| 1598 | CountedPtr< Scantable > |
---|
| 1599 | STMath::rotateXYPhase( const CountedPtr < Scantable >& in, float phase ) |
---|
| 1600 | { |
---|
| 1601 | return applyToPol(in, &STPol::rotatePhase, Float(phase)); |
---|
| 1602 | } |
---|
| 1603 | |
---|
| 1604 | CountedPtr< Scantable > |
---|
| 1605 | STMath::rotateLinPolPhase( const CountedPtr < Scantable >& in, float phase ) |
---|
| 1606 | { |
---|
| 1607 | return applyToPol(in, &STPol::rotateLinPolPhase, Float(phase)); |
---|
| 1608 | } |
---|
| 1609 | |
---|
| 1610 | CountedPtr< Scantable > STMath::applyToPol( const CountedPtr<Scantable>& in, |
---|
| 1611 | STPol::polOperation fptr, |
---|
| 1612 | Float phase ) |
---|
| 1613 | { |
---|
| 1614 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 1615 | Table& tout = out->table(); |
---|
| 1616 | Block<String> cols(4); |
---|
| 1617 | cols[0] = String("SCANNO"); |
---|
| 1618 | cols[1] = String("BEAMNO"); |
---|
| 1619 | cols[2] = String("IFNO"); |
---|
| 1620 | cols[3] = String("CYCLENO"); |
---|
| 1621 | TableIterator iter(tout, cols); |
---|
[1384] | 1622 | CountedPtr<STPol> stpol = STPol::getPolClass(out->factories_, |
---|
| 1623 | out->getPolType() ); |
---|
[896] | 1624 | while (!iter.pastEnd()) { |
---|
| 1625 | Table t = iter.table(); |
---|
| 1626 | ArrayColumn<Float> speccol(t, "SPECTRA"); |
---|
[1015] | 1627 | ScalarColumn<uInt> focidcol(t, "FOCUS_ID"); |
---|
| 1628 | ScalarColumn<Float> parancol(t, "PARANGLE"); |
---|
[1384] | 1629 | Matrix<Float> pols(speccol.getColumn()); |
---|
[896] | 1630 | try { |
---|
| 1631 | stpol->setSpectra(pols); |
---|
[1015] | 1632 | Float fang,fhand,parang; |
---|
| 1633 | fang = in->focusTable_.getTotalFeedAngle(focidcol(0)); |
---|
| 1634 | fhand = in->focusTable_.getFeedHand(focidcol(0)); |
---|
| 1635 | parang = parancol(0); |
---|
| 1636 | /// @todo re-enable this |
---|
| 1637 | // disable total feed angle to support paralactifying Caswell style |
---|
| 1638 | stpol->setPhaseCorrections(parang, -parang, fhand); |
---|
[1384] | 1639 | // use a member function pointer in STPol. This only works on |
---|
| 1640 | // the STPol pointer itself, not the Counted Pointer so |
---|
| 1641 | // derefernce it. |
---|
| 1642 | (&(*(stpol))->*fptr)(phase); |
---|
[896] | 1643 | speccol.putColumn(stpol->getSpectra()); |
---|
| 1644 | } catch (AipsError& e) { |
---|
[1384] | 1645 | //delete stpol;stpol=0; |
---|
[896] | 1646 | throw(e); |
---|
| 1647 | } |
---|
| 1648 | ++iter; |
---|
| 1649 | } |
---|
[1384] | 1650 | //delete stpol;stpol=0; |
---|
[896] | 1651 | return out; |
---|
| 1652 | } |
---|
| 1653 | |
---|
| 1654 | CountedPtr< Scantable > |
---|
| 1655 | STMath::swapPolarisations( const CountedPtr< Scantable > & in ) |
---|
| 1656 | { |
---|
| 1657 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 1658 | Table& tout = out->table(); |
---|
| 1659 | Table t0 = tout(tout.col("POLNO") == 0); |
---|
| 1660 | Table t1 = tout(tout.col("POLNO") == 1); |
---|
| 1661 | if ( t0.nrow() != t1.nrow() ) |
---|
| 1662 | throw(AipsError("Inconsistent number of polarisations")); |
---|
| 1663 | ArrayColumn<Float> speccol0(t0, "SPECTRA"); |
---|
| 1664 | ArrayColumn<uChar> flagcol0(t0, "FLAGTRA"); |
---|
| 1665 | ArrayColumn<Float> speccol1(t1, "SPECTRA"); |
---|
| 1666 | ArrayColumn<uChar> flagcol1(t1, "FLAGTRA"); |
---|
| 1667 | Matrix<Float> s0 = speccol0.getColumn(); |
---|
| 1668 | Matrix<uChar> f0 = flagcol0.getColumn(); |
---|
| 1669 | speccol0.putColumn(speccol1.getColumn()); |
---|
| 1670 | flagcol0.putColumn(flagcol1.getColumn()); |
---|
| 1671 | speccol1.putColumn(s0); |
---|
| 1672 | flagcol1.putColumn(f0); |
---|
| 1673 | return out; |
---|
| 1674 | } |
---|
[917] | 1675 | |
---|
| 1676 | CountedPtr< Scantable > |
---|
[940] | 1677 | STMath::averagePolarisations( const CountedPtr< Scantable > & in, |
---|
| 1678 | const std::vector<bool>& mask, |
---|
| 1679 | const std::string& weight ) |
---|
| 1680 | { |
---|
[1232] | 1681 | if (in->npol() < 2 ) |
---|
| 1682 | throw(AipsError("averagePolarisations can only be applied to two or more" |
---|
| 1683 | "polarisations")); |
---|
[1010] | 1684 | bool insitu = insitu_; |
---|
| 1685 | setInsitu(false); |
---|
[1232] | 1686 | CountedPtr< Scantable > pols = getScantable(in, true); |
---|
[1010] | 1687 | setInsitu(insitu); |
---|
| 1688 | Table& tout = pols->table(); |
---|
[1232] | 1689 | std::string taql = "SELECT FROM $1 WHERE POLNO IN [0,1]"; |
---|
| 1690 | Table tab = tableCommand(taql, in->table()); |
---|
| 1691 | if (tab.nrow() == 0 ) |
---|
| 1692 | throw(AipsError("Could not find any rows with POLNO==0 and POLNO==1")); |
---|
| 1693 | TableCopy::copyRows(tout, tab); |
---|
[1145] | 1694 | TableVector<uInt> vec(tout, "POLNO"); |
---|
[940] | 1695 | vec = 0; |
---|
[1145] | 1696 | pols->table_.rwKeywordSet().define("nPol", Int(1)); |
---|
[1391] | 1697 | //pols->table_.rwKeywordSet().define("POLTYPE", String("stokes")); |
---|
| 1698 | pols->table_.rwKeywordSet().define("POLTYPE", in->getPolType()); |
---|
[1010] | 1699 | std::vector<CountedPtr<Scantable> > vpols; |
---|
| 1700 | vpols.push_back(pols); |
---|
[1232] | 1701 | CountedPtr< Scantable > out = average(vpols, mask, weight, "SCAN"); |
---|
[940] | 1702 | return out; |
---|
| 1703 | } |
---|
| 1704 | |
---|
[1145] | 1705 | CountedPtr< Scantable > |
---|
| 1706 | STMath::averageBeams( const CountedPtr< Scantable > & in, |
---|
| 1707 | const std::vector<bool>& mask, |
---|
| 1708 | const std::string& weight ) |
---|
| 1709 | { |
---|
| 1710 | bool insitu = insitu_; |
---|
| 1711 | setInsitu(false); |
---|
| 1712 | CountedPtr< Scantable > beams = getScantable(in, false); |
---|
| 1713 | setInsitu(insitu); |
---|
| 1714 | Table& tout = beams->table(); |
---|
| 1715 | // give all rows the same BEAMNO |
---|
| 1716 | TableVector<uInt> vec(tout, "BEAMNO"); |
---|
| 1717 | vec = 0; |
---|
| 1718 | beams->table_.rwKeywordSet().define("nBeam", Int(1)); |
---|
| 1719 | std::vector<CountedPtr<Scantable> > vbeams; |
---|
| 1720 | vbeams.push_back(beams); |
---|
[1232] | 1721 | CountedPtr< Scantable > out = average(vbeams, mask, weight, "SCAN"); |
---|
[1145] | 1722 | return out; |
---|
| 1723 | } |
---|
[940] | 1724 | |
---|
[1145] | 1725 | |
---|
[940] | 1726 | CountedPtr< Scantable > |
---|
[917] | 1727 | asap::STMath::frequencyAlign( const CountedPtr< Scantable > & in, |
---|
| 1728 | const std::string & refTime, |
---|
[926] | 1729 | const std::string & method) |
---|
[917] | 1730 | { |
---|
[940] | 1731 | // clone as this is not working insitu |
---|
| 1732 | bool insitu = insitu_; |
---|
| 1733 | setInsitu(false); |
---|
[917] | 1734 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
[940] | 1735 | setInsitu(insitu); |
---|
[917] | 1736 | Table& tout = out->table(); |
---|
| 1737 | // Get reference Epoch to time of first row or given String |
---|
| 1738 | Unit DAY(String("d")); |
---|
| 1739 | MEpoch::Ref epochRef(in->getTimeReference()); |
---|
| 1740 | MEpoch refEpoch; |
---|
| 1741 | if (refTime.length()>0) { |
---|
| 1742 | Quantum<Double> qt; |
---|
| 1743 | if (MVTime::read(qt,refTime)) { |
---|
| 1744 | MVEpoch mv(qt); |
---|
| 1745 | refEpoch = MEpoch(mv, epochRef); |
---|
| 1746 | } else { |
---|
| 1747 | throw(AipsError("Invalid format for Epoch string")); |
---|
| 1748 | } |
---|
| 1749 | } else { |
---|
| 1750 | refEpoch = in->timeCol_(0); |
---|
| 1751 | } |
---|
| 1752 | MPosition refPos = in->getAntennaPosition(); |
---|
[940] | 1753 | |
---|
[996] | 1754 | InterpolateArray1D<Double,Float>::InterpolationMethod interp = stringToIMethod(method); |
---|
[1475] | 1755 | /* |
---|
| 1756 | // Comment from MV. |
---|
| 1757 | // the following code has been commented out because different FREQ_IDs have to be aligned together even |
---|
| 1758 | // if the frame doesn't change. So far, lack of this check didn't cause any problems. |
---|
[917] | 1759 | // test if user frame is different to base frame |
---|
| 1760 | if ( in->frequencies().getFrameString(true) |
---|
| 1761 | == in->frequencies().getFrameString(false) ) { |
---|
[985] | 1762 | throw(AipsError("Can't convert as no output frame has been set" |
---|
| 1763 | " (use set_freqframe) or it is aligned already.")); |
---|
[917] | 1764 | } |
---|
[1475] | 1765 | */ |
---|
[917] | 1766 | MFrequency::Types system = in->frequencies().getFrame(); |
---|
[940] | 1767 | MVTime mvt(refEpoch.getValue()); |
---|
| 1768 | String epochout = mvt.string(MVTime::YMD) + String(" (") + refEpoch.getRefString() + String(")"); |
---|
| 1769 | ostringstream oss; |
---|
| 1770 | oss << "Aligned at reference Epoch " << epochout |
---|
| 1771 | << " in frame " << MFrequency::showType(system); |
---|
| 1772 | pushLog(String(oss)); |
---|
[917] | 1773 | // set up the iterator |
---|
[926] | 1774 | Block<String> cols(4); |
---|
| 1775 | // select by constant direction |
---|
[917] | 1776 | cols[0] = String("SRCNAME"); |
---|
| 1777 | cols[1] = String("BEAMNO"); |
---|
| 1778 | // select by IF ( no of channels varies over this ) |
---|
| 1779 | cols[2] = String("IFNO"); |
---|
[926] | 1780 | // select by restfrequency |
---|
| 1781 | cols[3] = String("MOLECULE_ID"); |
---|
[917] | 1782 | TableIterator iter(tout, cols); |
---|
[926] | 1783 | while ( !iter.pastEnd() ) { |
---|
[917] | 1784 | Table t = iter.table(); |
---|
| 1785 | MDirection::ROScalarColumn dirCol(t, "DIRECTION"); |
---|
[926] | 1786 | TableIterator fiter(t, "FREQ_ID"); |
---|
[917] | 1787 | // determine nchan from the first row. This should work as |
---|
[926] | 1788 | // we are iterating over BEAMNO and IFNO // we should have constant direction |
---|
| 1789 | |
---|
[917] | 1790 | ROArrayColumn<Float> sCol(t, "SPECTRA"); |
---|
[1475] | 1791 | const MDirection direction = dirCol(0); |
---|
| 1792 | const uInt nchan = sCol(0).nelements(); |
---|
| 1793 | |
---|
| 1794 | // skip operations if there is nothing to align |
---|
| 1795 | if (fiter.pastEnd()) { |
---|
| 1796 | continue; |
---|
| 1797 | } |
---|
| 1798 | |
---|
| 1799 | Table ftab = fiter.table(); |
---|
| 1800 | // align all frequency ids with respect to the first encountered id |
---|
| 1801 | ScalarColumn<uInt> freqidCol(ftab, "FREQ_ID"); |
---|
| 1802 | // get the SpectralCoordinate for the freqid, which we are iterating over |
---|
| 1803 | SpectralCoordinate sC = in->frequencies().getSpectralCoordinate(freqidCol(0)); |
---|
| 1804 | FrequencyAligner<Float> fa( sC, nchan, refEpoch, |
---|
| 1805 | direction, refPos, system ); |
---|
| 1806 | // realign the SpectralCoordinate and put into the output Scantable |
---|
| 1807 | Vector<String> units(1); |
---|
| 1808 | units = String("Hz"); |
---|
| 1809 | Bool linear=True; |
---|
| 1810 | SpectralCoordinate sc2 = fa.alignedSpectralCoordinate(linear); |
---|
| 1811 | sc2.setWorldAxisUnits(units); |
---|
| 1812 | const uInt id = out->frequencies().addEntry(sc2.referencePixel()[0], |
---|
| 1813 | sc2.referenceValue()[0], |
---|
| 1814 | sc2.increment()[0]); |
---|
[926] | 1815 | while ( !fiter.pastEnd() ) { |
---|
[1475] | 1816 | ftab = fiter.table(); |
---|
| 1817 | // spectral coordinate for the current FREQ_ID |
---|
| 1818 | ScalarColumn<uInt> freqidCol2(ftab, "FREQ_ID"); |
---|
| 1819 | sC = in->frequencies().getSpectralCoordinate(freqidCol2(0)); |
---|
[926] | 1820 | // create the "global" abcissa for alignment with same FREQ_ID |
---|
| 1821 | Vector<Double> abc(nchan); |
---|
[917] | 1822 | for (uInt i=0; i<nchan; i++) { |
---|
[1475] | 1823 | Double w; |
---|
| 1824 | sC.toWorld(w,Double(i)); |
---|
| 1825 | abc[i] = w; |
---|
[917] | 1826 | } |
---|
[1475] | 1827 | TableVector<uInt> tvec(ftab, "FREQ_ID"); |
---|
| 1828 | // assign new frequency id to all rows |
---|
| 1829 | tvec = id; |
---|
[926] | 1830 | // cache abcissa for same time stamps, so iterate over those |
---|
| 1831 | TableIterator timeiter(ftab, "TIME"); |
---|
| 1832 | while ( !timeiter.pastEnd() ) { |
---|
| 1833 | Table tab = timeiter.table(); |
---|
| 1834 | ArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 1835 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 1836 | MEpoch::ROScalarColumn timeCol(tab, "TIME"); |
---|
| 1837 | // use align abcissa cache after the first row |
---|
[1475] | 1838 | // these rows should be just be POLNO |
---|
[926] | 1839 | bool first = true; |
---|
[996] | 1840 | for (int i=0; i<int(tab.nrow()); ++i) { |
---|
[926] | 1841 | // input values |
---|
| 1842 | Vector<uChar> flag = flagCol(i); |
---|
| 1843 | Vector<Bool> mask(flag.shape()); |
---|
| 1844 | Vector<Float> specOut, spec; |
---|
| 1845 | spec = specCol(i); |
---|
| 1846 | Vector<Bool> maskOut;Vector<uChar> flagOut; |
---|
| 1847 | convertArray(mask, flag); |
---|
| 1848 | // alignment |
---|
| 1849 | Bool ok = fa.align(specOut, maskOut, abc, spec, |
---|
| 1850 | mask, timeCol(i), !first, |
---|
| 1851 | interp, False); |
---|
| 1852 | // back into scantable |
---|
| 1853 | flagOut.resize(maskOut.nelements()); |
---|
| 1854 | convertArray(flagOut, maskOut); |
---|
| 1855 | flagCol.put(i, flagOut); |
---|
| 1856 | specCol.put(i, specOut); |
---|
| 1857 | // start abcissa caching |
---|
| 1858 | first = false; |
---|
[917] | 1859 | } |
---|
[926] | 1860 | // next timestamp |
---|
| 1861 | ++timeiter; |
---|
[917] | 1862 | } |
---|
[940] | 1863 | // next FREQ_ID |
---|
[926] | 1864 | ++fiter; |
---|
[917] | 1865 | } |
---|
| 1866 | // next aligner |
---|
| 1867 | ++iter; |
---|
| 1868 | } |
---|
[940] | 1869 | // set this afterwards to ensure we are doing insitu correctly. |
---|
| 1870 | out->frequencies().setFrame(system, true); |
---|
[917] | 1871 | return out; |
---|
| 1872 | } |
---|
[992] | 1873 | |
---|
| 1874 | CountedPtr<Scantable> |
---|
| 1875 | asap::STMath::convertPolarisation( const CountedPtr<Scantable>& in, |
---|
| 1876 | const std::string & newtype ) |
---|
| 1877 | { |
---|
| 1878 | if (in->npol() != 2 && in->npol() != 4) |
---|
| 1879 | throw(AipsError("Can only convert two or four polarisations.")); |
---|
| 1880 | if ( in->getPolType() == newtype ) |
---|
| 1881 | throw(AipsError("No need to convert.")); |
---|
[1000] | 1882 | if ( ! in->selector_.empty() ) |
---|
| 1883 | throw(AipsError("Can only convert whole scantable. Unset the selection.")); |
---|
[992] | 1884 | bool insitu = insitu_; |
---|
| 1885 | setInsitu(false); |
---|
| 1886 | CountedPtr< Scantable > out = getScantable(in, true); |
---|
| 1887 | setInsitu(insitu); |
---|
| 1888 | Table& tout = out->table(); |
---|
| 1889 | tout.rwKeywordSet().define("POLTYPE", String(newtype)); |
---|
| 1890 | |
---|
| 1891 | Block<String> cols(4); |
---|
| 1892 | cols[0] = "SCANNO"; |
---|
| 1893 | cols[1] = "CYCLENO"; |
---|
| 1894 | cols[2] = "BEAMNO"; |
---|
| 1895 | cols[3] = "IFNO"; |
---|
| 1896 | TableIterator it(in->originalTable_, cols); |
---|
| 1897 | String basetype = in->getPolType(); |
---|
| 1898 | STPol* stpol = STPol::getPolClass(in->factories_, basetype); |
---|
| 1899 | try { |
---|
| 1900 | while ( !it.pastEnd() ) { |
---|
| 1901 | Table tab = it.table(); |
---|
| 1902 | uInt row = tab.rowNumbers()[0]; |
---|
| 1903 | stpol->setSpectra(in->getPolMatrix(row)); |
---|
| 1904 | Float fang,fhand,parang; |
---|
| 1905 | fang = in->focusTable_.getTotalFeedAngle(in->mfocusidCol_(row)); |
---|
| 1906 | fhand = in->focusTable_.getFeedHand(in->mfocusidCol_(row)); |
---|
| 1907 | parang = in->paraCol_(row); |
---|
| 1908 | /// @todo re-enable this |
---|
| 1909 | // disable total feed angle to support paralactifying Caswell style |
---|
| 1910 | stpol->setPhaseCorrections(parang, -parang, fhand); |
---|
| 1911 | Int npolout = 0; |
---|
| 1912 | for (uInt i=0; i<tab.nrow(); ++i) { |
---|
| 1913 | Vector<Float> outvec = stpol->getSpectrum(i, newtype); |
---|
| 1914 | if ( outvec.nelements() > 0 ) { |
---|
| 1915 | tout.addRow(); |
---|
| 1916 | TableCopy::copyRows(tout, tab, tout.nrow()-1, 0, 1); |
---|
| 1917 | ArrayColumn<Float> sCol(tout,"SPECTRA"); |
---|
| 1918 | ScalarColumn<uInt> pCol(tout,"POLNO"); |
---|
| 1919 | sCol.put(tout.nrow()-1 ,outvec); |
---|
| 1920 | pCol.put(tout.nrow()-1 ,uInt(npolout)); |
---|
| 1921 | npolout++; |
---|
| 1922 | } |
---|
| 1923 | } |
---|
| 1924 | tout.rwKeywordSet().define("nPol", npolout); |
---|
| 1925 | ++it; |
---|
| 1926 | } |
---|
| 1927 | } catch (AipsError& e) { |
---|
| 1928 | delete stpol; |
---|
| 1929 | throw(e); |
---|
| 1930 | } |
---|
| 1931 | delete stpol; |
---|
| 1932 | return out; |
---|
| 1933 | } |
---|
[1066] | 1934 | |
---|
[1143] | 1935 | CountedPtr< Scantable > |
---|
[1140] | 1936 | asap::STMath::mxExtract( const CountedPtr< Scantable > & in, |
---|
| 1937 | const std::string & scantype ) |
---|
| 1938 | { |
---|
| 1939 | bool insitu = insitu_; |
---|
| 1940 | setInsitu(false); |
---|
| 1941 | CountedPtr< Scantable > out = getScantable(in, true); |
---|
| 1942 | setInsitu(insitu); |
---|
| 1943 | Table& tout = out->table(); |
---|
| 1944 | std::string taql = "SELECT FROM $1 WHERE BEAMNO != REFBEAMNO"; |
---|
| 1945 | if (scantype == "on") { |
---|
| 1946 | taql = "SELECT FROM $1 WHERE BEAMNO == REFBEAMNO"; |
---|
| 1947 | } |
---|
| 1948 | Table tab = tableCommand(taql, in->table()); |
---|
| 1949 | TableCopy::copyRows(tout, tab); |
---|
| 1950 | if (scantype == "on") { |
---|
[1143] | 1951 | // re-index SCANNO to 0 |
---|
[1140] | 1952 | TableVector<uInt> vec(tout, "SCANNO"); |
---|
| 1953 | vec = 0; |
---|
| 1954 | } |
---|
| 1955 | return out; |
---|
| 1956 | } |
---|
[1192] | 1957 | |
---|
| 1958 | CountedPtr< Scantable > |
---|
| 1959 | asap::STMath::lagFlag( const CountedPtr< Scantable > & in, |
---|
[1200] | 1960 | double frequency, double width ) |
---|
[1192] | 1961 | { |
---|
| 1962 | CountedPtr< Scantable > out = getScantable(in, false); |
---|
| 1963 | Table& tout = out->table(); |
---|
| 1964 | TableIterator iter(tout, "FREQ_ID"); |
---|
| 1965 | FFTServer<Float,Complex> ffts; |
---|
| 1966 | while ( !iter.pastEnd() ) { |
---|
| 1967 | Table tab = iter.table(); |
---|
| 1968 | Double rp,rv,inc; |
---|
| 1969 | ROTableRow row(tab); |
---|
| 1970 | const TableRecord& rec = row.get(0); |
---|
| 1971 | uInt freqid = rec.asuInt("FREQ_ID"); |
---|
| 1972 | out->frequencies().getEntry(rp, rv, inc, freqid); |
---|
| 1973 | ArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 1974 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
| 1975 | for (int i=0; i<int(tab.nrow()); ++i) { |
---|
| 1976 | Vector<Float> spec = specCol(i); |
---|
| 1977 | Vector<uChar> flag = flagCol(i); |
---|
[1200] | 1978 | Int lag0 = Int(spec.nelements()*abs(inc)/(frequency+width)+0.5); |
---|
| 1979 | Int lag1 = Int(spec.nelements()*abs(inc)/(frequency-width)+0.5); |
---|
[1192] | 1980 | for (int k=0; k < flag.nelements(); ++k ) { |
---|
| 1981 | if (flag[k] > 0) { |
---|
| 1982 | spec[k] = 0.0; |
---|
| 1983 | } |
---|
| 1984 | } |
---|
| 1985 | Vector<Complex> lags; |
---|
[1203] | 1986 | ffts.fft0(lags, spec); |
---|
[1200] | 1987 | Int start = max(0, lag0); |
---|
| 1988 | Int end = min(Int(lags.nelements()-1), lag1); |
---|
[1192] | 1989 | if (start == end) { |
---|
| 1990 | lags[start] = Complex(0.0); |
---|
| 1991 | } else { |
---|
| 1992 | for (int j=start; j <=end ;++j) { |
---|
| 1993 | lags[j] = Complex(0.0); |
---|
| 1994 | } |
---|
| 1995 | } |
---|
[1203] | 1996 | ffts.fft0(spec, lags); |
---|
[1192] | 1997 | specCol.put(i, spec); |
---|
| 1998 | } |
---|
| 1999 | ++iter; |
---|
| 2000 | } |
---|
| 2001 | return out; |
---|
| 2002 | } |
---|