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