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