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 <sstream>
|
---|
14 |
|
---|
15 | #include <casa/iomanip.h>
|
---|
16 | #include <casa/Arrays/MaskArrLogi.h>
|
---|
17 | #include <casa/Arrays/MaskArrMath.h>
|
---|
18 | #include <casa/Arrays/ArrayLogical.h>
|
---|
19 | #include <casa/Arrays/ArrayMath.h>
|
---|
20 | #include <casa/Arrays/Slice.h>
|
---|
21 | #include <casa/Arrays/Slicer.h>
|
---|
22 | #include <casa/BasicSL/String.h>
|
---|
23 | #include <casa/Containers/Block.h>
|
---|
24 | #include <casa/Containers/RecordField.h>
|
---|
25 | #include <casa/Exceptions/Error.h>
|
---|
26 | #include <casa/Logging/LogIO.h>
|
---|
27 |
|
---|
28 | #include <coordinates/Coordinates/CoordinateSystem.h>
|
---|
29 | #include <coordinates/Coordinates/CoordinateUtil.h>
|
---|
30 | #include <coordinates/Coordinates/FrequencyAligner.h>
|
---|
31 | #include <coordinates/Coordinates/SpectralCoordinate.h>
|
---|
32 |
|
---|
33 | #include <lattices/Lattices/LatticeUtilities.h>
|
---|
34 |
|
---|
35 | #include <scimath/Functionals/Polynomial.h>
|
---|
36 | #include <scimath/Mathematics/Convolver.h>
|
---|
37 | #include <scimath/Mathematics/VectorKernel.h>
|
---|
38 |
|
---|
39 | #include <tables/Tables/ExprNode.h>
|
---|
40 | #include <tables/Tables/ReadAsciiTable.h>
|
---|
41 | #include <tables/Tables/TableCopy.h>
|
---|
42 | #include <tables/Tables/TableIter.h>
|
---|
43 | #include <tables/Tables/TableParse.h>
|
---|
44 | #include <tables/Tables/TableRecord.h>
|
---|
45 | #include <tables/Tables/TableRow.h>
|
---|
46 | #include <tables/Tables/TableVector.h>
|
---|
47 | #include <tables/Tables/TabVecMath.h>
|
---|
48 |
|
---|
49 | #include <atnf/PKSIO/SrcType.h>
|
---|
50 |
|
---|
51 | #include "RowAccumulator.h"
|
---|
52 | #include "STAttr.h"
|
---|
53 | #include "STMath.h"
|
---|
54 | #include "STSelector.h"
|
---|
55 |
|
---|
56 | using namespace casa;
|
---|
57 | using namespace asap;
|
---|
58 |
|
---|
59 | // 2012/02/17 TN
|
---|
60 | // Since STGrid is implemented, average doesn't consider direction
|
---|
61 | // when accumulating
|
---|
62 | // tolerance for direction comparison (rad)
|
---|
63 | // #define TOL_OTF 1.0e-15
|
---|
64 | // #define TOL_POINT 2.9088821e-4 // 1 arcmin
|
---|
65 |
|
---|
66 | STMath::STMath(bool insitu) :
|
---|
67 | insitu_(insitu)
|
---|
68 | {
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | STMath::~STMath()
|
---|
73 | {
|
---|
74 | }
|
---|
75 |
|
---|
76 | CountedPtr<Scantable>
|
---|
77 | STMath::average( const std::vector<CountedPtr<Scantable> >& in,
|
---|
78 | const std::vector<bool>& mask,
|
---|
79 | const std::string& weight,
|
---|
80 | const std::string& avmode)
|
---|
81 | {
|
---|
82 | LogIO os( LogOrigin( "STMath", "average()", WHERE ) ) ;
|
---|
83 | if ( avmode == "SCAN" && in.size() != 1 )
|
---|
84 | throw(AipsError("Can't perform 'SCAN' averaging on multiple tables.\n"
|
---|
85 | "Use merge first."));
|
---|
86 | WeightType wtype = stringToWeight(weight);
|
---|
87 |
|
---|
88 | // 2012/02/17 TN
|
---|
89 | // Since STGrid is implemented, average doesn't consider direction
|
---|
90 | // when accumulating
|
---|
91 | // check if OTF observation
|
---|
92 | // String obstype = in[0]->getHeader().obstype ;
|
---|
93 | // Double tol = 0.0 ;
|
---|
94 | // if ( (obstype.find( "OTF" ) != String::npos) || (obstype.find( "OBSERVE_TARGET" ) != String::npos) ) {
|
---|
95 | // tol = TOL_OTF ;
|
---|
96 | // }
|
---|
97 | // else {
|
---|
98 | // tol = TOL_POINT ;
|
---|
99 | // }
|
---|
100 |
|
---|
101 | // output
|
---|
102 | // clone as this is non insitu
|
---|
103 | bool insitu = insitu_;
|
---|
104 | setInsitu(false);
|
---|
105 | CountedPtr< Scantable > out = getScantable(in[0], true);
|
---|
106 | setInsitu(insitu);
|
---|
107 | std::vector<CountedPtr<Scantable> >::const_iterator stit = in.begin();
|
---|
108 | ++stit;
|
---|
109 | while ( stit != in.end() ) {
|
---|
110 | out->appendToHistoryTable((*stit)->history());
|
---|
111 | ++stit;
|
---|
112 | }
|
---|
113 |
|
---|
114 | Table& tout = out->table();
|
---|
115 |
|
---|
116 | /// @todo check if all scantables are conformant
|
---|
117 |
|
---|
118 | ArrayColumn<Float> specColOut(tout,"SPECTRA");
|
---|
119 | ArrayColumn<uChar> flagColOut(tout,"FLAGTRA");
|
---|
120 | ArrayColumn<Float> tsysColOut(tout,"TSYS");
|
---|
121 | ScalarColumn<Double> mjdColOut(tout,"TIME");
|
---|
122 | ScalarColumn<Double> intColOut(tout,"INTERVAL");
|
---|
123 | ScalarColumn<uInt> cycColOut(tout,"CYCLENO");
|
---|
124 | ScalarColumn<uInt> scanColOut(tout,"SCANNO");
|
---|
125 |
|
---|
126 | // set up the output table rows. These are based on the structure of the
|
---|
127 | // FIRST scantable in the vector
|
---|
128 | const Table& baset = in[0]->table();
|
---|
129 |
|
---|
130 | Block<String> cols(3);
|
---|
131 | cols[0] = String("BEAMNO");
|
---|
132 | cols[1] = String("IFNO");
|
---|
133 | cols[2] = String("POLNO");
|
---|
134 | if ( avmode == "SOURCE" ) {
|
---|
135 | cols.resize(4);
|
---|
136 | cols[3] = String("SRCNAME");
|
---|
137 | }
|
---|
138 | if ( avmode == "SCAN" && in.size() == 1) {
|
---|
139 | //cols.resize(4);
|
---|
140 | //cols[3] = String("SCANNO");
|
---|
141 | cols.resize(5);
|
---|
142 | cols[3] = String("SRCNAME");
|
---|
143 | cols[4] = String("SCANNO");
|
---|
144 | }
|
---|
145 | uInt outrowCount = 0;
|
---|
146 | TableIterator iter(baset, cols);
|
---|
147 | // int count = 0 ;
|
---|
148 | while (!iter.pastEnd()) {
|
---|
149 | Table subt = iter.table();
|
---|
150 | // copy the first row of this selection into the new table
|
---|
151 | tout.addRow();
|
---|
152 | TableCopy::copyRows(tout, subt, outrowCount, 0, 1);
|
---|
153 | // re-index to 0
|
---|
154 | if ( avmode != "SCAN" && avmode != "SOURCE" ) {
|
---|
155 | scanColOut.put(outrowCount, uInt(0));
|
---|
156 | }
|
---|
157 | ++outrowCount;
|
---|
158 | // 2012/02/17 TN
|
---|
159 | // Since STGrid is implemented, average doesn't consider direction
|
---|
160 | // when accumulating
|
---|
161 | // MDirection::ScalarColumn dircol ;
|
---|
162 | // dircol.attach( subt, "DIRECTION" ) ;
|
---|
163 | // Int length = subt.nrow() ;
|
---|
164 | // vector< Vector<Double> > dirs ;
|
---|
165 | // vector<int> indexes ;
|
---|
166 | // for ( Int i = 0 ; i < length ; i++ ) {
|
---|
167 | // Vector<Double> t = dircol(i).getAngle(Unit(String("rad"))).getValue() ;
|
---|
168 | // //os << << count++ << ": " ;
|
---|
169 | // //os << "[" << t[0] << "," << t[1] << "]" << LogIO::POST ;
|
---|
170 | // bool adddir = true ;
|
---|
171 | // for ( uInt j = 0 ; j < dirs.size() ; j++ ) {
|
---|
172 | // //if ( allTrue( t == dirs[j] ) ) {
|
---|
173 | // Double dx = t[0] - dirs[j][0] ;
|
---|
174 | // Double dy = t[1] - dirs[j][1] ;
|
---|
175 | // Double dd = sqrt( dx * dx + dy * dy ) ;
|
---|
176 | // //if ( allNearAbs( t, dirs[j], tol ) ) {
|
---|
177 | // if ( dd <= tol ) {
|
---|
178 | // adddir = false ;
|
---|
179 | // break ;
|
---|
180 | // }
|
---|
181 | // }
|
---|
182 | // if ( adddir ) {
|
---|
183 | // dirs.push_back( t ) ;
|
---|
184 | // indexes.push_back( i ) ;
|
---|
185 | // }
|
---|
186 | // }
|
---|
187 | // uInt rowNum = dirs.size() ;
|
---|
188 | // tout.addRow( rowNum ) ;
|
---|
189 | // for ( uInt i = 0 ; i < rowNum ; i++ ) {
|
---|
190 | // TableCopy::copyRows( tout, subt, outrowCount+i, indexes[i], 1 ) ;
|
---|
191 | // // re-index to 0
|
---|
192 | // if ( avmode != "SCAN" && avmode != "SOURCE" ) {
|
---|
193 | // scanColOut.put(outrowCount+i, uInt(0));
|
---|
194 | // }
|
---|
195 | // }
|
---|
196 | // outrowCount += rowNum ;
|
---|
197 | ++iter;
|
---|
198 | }
|
---|
199 | RowAccumulator acc(wtype);
|
---|
200 | Vector<Bool> cmask(mask);
|
---|
201 | acc.setUserMask(cmask);
|
---|
202 | ROTableRow row(tout);
|
---|
203 | ROArrayColumn<Float> specCol, tsysCol;
|
---|
204 | ROArrayColumn<uChar> flagCol;
|
---|
205 | ROScalarColumn<Double> mjdCol, intCol;
|
---|
206 | ROScalarColumn<Int> scanIDCol;
|
---|
207 |
|
---|
208 | Vector<uInt> rowstodelete;
|
---|
209 |
|
---|
210 | for (uInt i=0; i < tout.nrow(); ++i) {
|
---|
211 | for ( int j=0; j < int(in.size()); ++j ) {
|
---|
212 | const Table& tin = in[j]->table();
|
---|
213 | const TableRecord& rec = row.get(i);
|
---|
214 | ROScalarColumn<Double> tmp(tin, "TIME");
|
---|
215 | Double td;tmp.get(0,td);
|
---|
216 | Table basesubt = tin( tin.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
|
---|
217 | && tin.col("IFNO") == Int(rec.asuInt("IFNO"))
|
---|
218 | && tin.col("POLNO") == Int(rec.asuInt("POLNO")) );
|
---|
219 | Table subt;
|
---|
220 | if ( avmode == "SOURCE") {
|
---|
221 | subt = basesubt( basesubt.col("SRCNAME") == rec.asString("SRCNAME"));
|
---|
222 | } else if (avmode == "SCAN") {
|
---|
223 | subt = basesubt( basesubt.col("SRCNAME") == rec.asString("SRCNAME")
|
---|
224 | && basesubt.col("SCANNO") == Int(rec.asuInt("SCANNO")) );
|
---|
225 | } else {
|
---|
226 | subt = basesubt;
|
---|
227 | }
|
---|
228 |
|
---|
229 | // 2012/02/17 TN
|
---|
230 | // Since STGrid is implemented, average doesn't consider direction
|
---|
231 | // when accumulating
|
---|
232 | // vector<uInt> removeRows ;
|
---|
233 | // uInt nrsubt = subt.nrow() ;
|
---|
234 | // for ( uInt irow = 0 ; irow < nrsubt ; irow++ ) {
|
---|
235 | // //if ( !allTrue((subt.col("DIRECTION").getArrayDouble(TableExprId(irow)))==rec.asArrayDouble("DIRECTION")) ) {
|
---|
236 | // Vector<Double> x0 = (subt.col("DIRECTION").getArrayDouble(TableExprId(irow))) ;
|
---|
237 | // Vector<Double> x1 = rec.asArrayDouble("DIRECTION") ;
|
---|
238 | // double dx = x0[0] - x1[0];
|
---|
239 | // double dy = x0[1] - x1[1];
|
---|
240 | // Double dd = sqrt( dx * dx + dy * dy ) ;
|
---|
241 | // //if ( !allNearAbs((subt.col("DIRECTION").getArrayDouble(TableExprId(irow))), rec.asArrayDouble("DIRECTION"), tol ) ) {
|
---|
242 | // if ( dd > tol ) {
|
---|
243 | // removeRows.push_back( irow ) ;
|
---|
244 | // }
|
---|
245 | // }
|
---|
246 | // if ( removeRows.size() != 0 ) {
|
---|
247 | // subt.removeRow( removeRows ) ;
|
---|
248 | // }
|
---|
249 |
|
---|
250 | // if ( nrsubt == removeRows.size() )
|
---|
251 | // throw(AipsError("Averaging data is empty.")) ;
|
---|
252 |
|
---|
253 | specCol.attach(subt,"SPECTRA");
|
---|
254 | flagCol.attach(subt,"FLAGTRA");
|
---|
255 | tsysCol.attach(subt,"TSYS");
|
---|
256 | intCol.attach(subt,"INTERVAL");
|
---|
257 | mjdCol.attach(subt,"TIME");
|
---|
258 | Vector<Float> spec,tsys;
|
---|
259 | Vector<uChar> flag;
|
---|
260 | Double inter,time;
|
---|
261 | for (uInt k = 0; k < subt.nrow(); ++k ) {
|
---|
262 | flagCol.get(k, flag);
|
---|
263 | Vector<Bool> bflag(flag.shape());
|
---|
264 | convertArray(bflag, flag);
|
---|
265 | /*
|
---|
266 | if ( allEQ(bflag, True) ) {
|
---|
267 | continue;//don't accumulate
|
---|
268 | }
|
---|
269 | */
|
---|
270 | specCol.get(k, spec);
|
---|
271 | //tsysCol.get(k, tsys);
|
---|
272 | tsys.assign( tsysCol(k) );
|
---|
273 | intCol.get(k, inter);
|
---|
274 | mjdCol.get(k, time);
|
---|
275 | // spectrum has to be added last to enable weighting by the other values
|
---|
276 | acc.add(spec, !bflag, tsys, inter, time);
|
---|
277 | }
|
---|
278 |
|
---|
279 | // If there exists a channel at which all the input spectra are masked,
|
---|
280 | // spec has 'nan' values for that channel and it may affect the following
|
---|
281 | // processes. To avoid this, replacing 'nan' values in spec with
|
---|
282 | // weighted-mean of all spectra in the following line.
|
---|
283 | // (done for CAS-2776, 2011/04/07 by Wataru Kawasaki)
|
---|
284 | acc.replaceNaN();
|
---|
285 | }
|
---|
286 | const Vector<Bool>& msk = acc.getMask();
|
---|
287 | if ( allEQ(msk, False) ) {
|
---|
288 | uint n = rowstodelete.nelements();
|
---|
289 | rowstodelete.resize(n+1, True);
|
---|
290 | rowstodelete[n] = i;
|
---|
291 | continue;
|
---|
292 | }
|
---|
293 | //write out
|
---|
294 | if (acc.state()) {
|
---|
295 | Vector<uChar> flg(msk.shape());
|
---|
296 | convertArray(flg, !msk);
|
---|
297 | for (uInt k = 0; k < flg.nelements(); ++k) {
|
---|
298 | uChar userFlag = 1 << 7;
|
---|
299 | if (msk[k]==True) userFlag = 0 << 7;
|
---|
300 | flg(k) = userFlag;
|
---|
301 | }
|
---|
302 |
|
---|
303 | flagColOut.put(i, flg);
|
---|
304 | specColOut.put(i, acc.getSpectrum());
|
---|
305 | tsysColOut.put(i, acc.getTsys());
|
---|
306 | intColOut.put(i, acc.getInterval());
|
---|
307 | mjdColOut.put(i, acc.getTime());
|
---|
308 | // we should only have one cycle now -> reset it to be 0
|
---|
309 | // frequency switched data has different CYCLENO for different IFNO
|
---|
310 | // which requires resetting this value
|
---|
311 | cycColOut.put(i, uInt(0));
|
---|
312 | } else {
|
---|
313 | ostringstream oss;
|
---|
314 | oss << "For output row="<<i<<", all input rows of data are flagged. no averaging" << endl;
|
---|
315 | pushLog(String(oss));
|
---|
316 | }
|
---|
317 | acc.reset();
|
---|
318 | }
|
---|
319 |
|
---|
320 | if (rowstodelete.nelements() > 0) {
|
---|
321 | os << rowstodelete << LogIO::POST ;
|
---|
322 | tout.removeRow(rowstodelete);
|
---|
323 | if (tout.nrow() == 0) {
|
---|
324 | throw(AipsError("Can't average fully flagged data."));
|
---|
325 | }
|
---|
326 | }
|
---|
327 | return out;
|
---|
328 | }
|
---|
329 |
|
---|
330 | CountedPtr< Scantable >
|
---|
331 | STMath::averageChannel( const CountedPtr < Scantable > & in,
|
---|
332 | const std::string & mode,
|
---|
333 | const std::string& avmode )
|
---|
334 | {
|
---|
335 | (void) mode; // currently unused
|
---|
336 | // 2012/02/17 TN
|
---|
337 | // Since STGrid is implemented, average doesn't consider direction
|
---|
338 | // when accumulating
|
---|
339 | // check if OTF observation
|
---|
340 | // String obstype = in->getHeader().obstype ;
|
---|
341 | // Double tol = 0.0 ;
|
---|
342 | // if ( obstype.find( "OTF" ) != String::npos ) {
|
---|
343 | // tol = TOL_OTF ;
|
---|
344 | // }
|
---|
345 | // else {
|
---|
346 | // tol = TOL_POINT ;
|
---|
347 | // }
|
---|
348 |
|
---|
349 | // clone as this is non insitu
|
---|
350 | bool insitu = insitu_;
|
---|
351 | setInsitu(false);
|
---|
352 | CountedPtr< Scantable > out = getScantable(in, true);
|
---|
353 | setInsitu(insitu);
|
---|
354 | Table& tout = out->table();
|
---|
355 | ArrayColumn<Float> specColOut(tout,"SPECTRA");
|
---|
356 | ArrayColumn<uChar> flagColOut(tout,"FLAGTRA");
|
---|
357 | ArrayColumn<Float> tsysColOut(tout,"TSYS");
|
---|
358 | ScalarColumn<uInt> scanColOut(tout,"SCANNO");
|
---|
359 | ScalarColumn<Double> intColOut(tout, "INTERVAL");
|
---|
360 | Table tmp = in->table().sort("BEAMNO");
|
---|
361 | Block<String> cols(3);
|
---|
362 | cols[0] = String("BEAMNO");
|
---|
363 | cols[1] = String("IFNO");
|
---|
364 | cols[2] = String("POLNO");
|
---|
365 | if ( avmode == "SCAN") {
|
---|
366 | cols.resize(4);
|
---|
367 | cols[3] = String("SCANNO");
|
---|
368 | }
|
---|
369 | uInt outrowCount = 0;
|
---|
370 | uChar userflag = 1 << 7;
|
---|
371 | TableIterator iter(tmp, cols);
|
---|
372 | while (!iter.pastEnd()) {
|
---|
373 | Table subt = iter.table();
|
---|
374 | ROArrayColumn<Float> specCol, tsysCol;
|
---|
375 | ROArrayColumn<uChar> flagCol;
|
---|
376 | ROScalarColumn<Double> intCol(subt, "INTERVAL");
|
---|
377 | specCol.attach(subt,"SPECTRA");
|
---|
378 | flagCol.attach(subt,"FLAGTRA");
|
---|
379 | tsysCol.attach(subt,"TSYS");
|
---|
380 |
|
---|
381 | tout.addRow();
|
---|
382 | TableCopy::copyRows(tout, subt, outrowCount, 0, 1);
|
---|
383 | if ( avmode != "SCAN") {
|
---|
384 | scanColOut.put(outrowCount, uInt(0));
|
---|
385 | }
|
---|
386 | Vector<Float> tmp;
|
---|
387 | specCol.get(0, tmp);
|
---|
388 | uInt nchan = tmp.nelements();
|
---|
389 | // have to do channel by channel here as MaskedArrMath
|
---|
390 | // doesn't have partialMedians
|
---|
391 | Vector<uChar> flags = flagCol.getColumn(Slicer(Slice(0)));
|
---|
392 | Vector<Float> outspec(nchan);
|
---|
393 | Vector<uChar> outflag(nchan,0);
|
---|
394 | Vector<Float> outtsys(1);/// @fixme when tsys is channel based
|
---|
395 | for (uInt i=0; i<nchan; ++i) {
|
---|
396 | Vector<Float> specs = specCol.getColumn(Slicer(Slice(i)));
|
---|
397 | MaskedArray<Float> ma = maskedArray(specs,flags);
|
---|
398 | outspec[i] = median(ma);
|
---|
399 | if ( allEQ(ma.getMask(), False) )
|
---|
400 | outflag[i] = userflag;// flag data
|
---|
401 | }
|
---|
402 | outtsys[0] = median(tsysCol.getColumn());
|
---|
403 | specColOut.put(outrowCount, outspec);
|
---|
404 | flagColOut.put(outrowCount, outflag);
|
---|
405 | tsysColOut.put(outrowCount, outtsys);
|
---|
406 | Double intsum = sum(intCol.getColumn());
|
---|
407 | intColOut.put(outrowCount, intsum);
|
---|
408 | ++outrowCount;
|
---|
409 | ++iter;
|
---|
410 |
|
---|
411 | // 2012/02/17 TN
|
---|
412 | // Since STGrid is implemented, average doesn't consider direction
|
---|
413 | // when accumulating
|
---|
414 | // MDirection::ScalarColumn dircol ;
|
---|
415 | // dircol.attach( subt, "DIRECTION" ) ;
|
---|
416 | // Int length = subt.nrow() ;
|
---|
417 | // vector< Vector<Double> > dirs ;
|
---|
418 | // vector<int> indexes ;
|
---|
419 | // // Handle MX mode averaging
|
---|
420 | // if (in->nbeam() > 1 ) {
|
---|
421 | // length = 1;
|
---|
422 | // }
|
---|
423 | // for ( Int i = 0 ; i < length ; i++ ) {
|
---|
424 | // Vector<Double> t = dircol(i).getAngle(Unit(String("rad"))).getValue() ;
|
---|
425 | // bool adddir = true ;
|
---|
426 | // for ( uInt j = 0 ; j < dirs.size() ; j++ ) {
|
---|
427 | // //if ( allTrue( t == dirs[j] ) ) {
|
---|
428 | // Double dx = t[0] - dirs[j][0] ;
|
---|
429 | // Double dy = t[1] - dirs[j][1] ;
|
---|
430 | // Double dd = sqrt( dx * dx + dy * dy ) ;
|
---|
431 | // //if ( allNearAbs( t, dirs[j], tol ) ) {
|
---|
432 | // if ( dd <= tol ) {
|
---|
433 | // adddir = false ;
|
---|
434 | // break ;
|
---|
435 | // }
|
---|
436 | // }
|
---|
437 | // if ( adddir ) {
|
---|
438 | // dirs.push_back( t ) ;
|
---|
439 | // indexes.push_back( i ) ;
|
---|
440 | // }
|
---|
441 | // }
|
---|
442 | // uInt rowNum = dirs.size() ;
|
---|
443 | // tout.addRow( rowNum );
|
---|
444 | // for ( uInt i = 0 ; i < rowNum ; i++ ) {
|
---|
445 | // TableCopy::copyRows(tout, subt, outrowCount+i, indexes[i], 1) ;
|
---|
446 | // // Handle MX mode averaging
|
---|
447 | // if ( avmode != "SCAN") {
|
---|
448 | // scanColOut.put(outrowCount+i, uInt(0));
|
---|
449 | // }
|
---|
450 | // }
|
---|
451 | // MDirection::ScalarColumn dircolOut ;
|
---|
452 | // dircolOut.attach( tout, "DIRECTION" ) ;
|
---|
453 | // for ( uInt irow = 0 ; irow < rowNum ; irow++ ) {
|
---|
454 | // Vector<Double> t = \
|
---|
455 | // dircolOut(outrowCount+irow).getAngle(Unit(String("rad"))).getValue() ;
|
---|
456 | // Vector<Float> tmp;
|
---|
457 | // specCol.get(0, tmp);
|
---|
458 | // uInt nchan = tmp.nelements();
|
---|
459 | // // have to do channel by channel here as MaskedArrMath
|
---|
460 | // // doesn't have partialMedians
|
---|
461 | // Vector<uChar> flags = flagCol.getColumn(Slicer(Slice(0)));
|
---|
462 | // // mask spectra for different DIRECTION
|
---|
463 | // for ( uInt jrow = 0 ; jrow < subt.nrow() ; jrow++ ) {
|
---|
464 | // Vector<Double> direction = \
|
---|
465 | // dircol(jrow).getAngle(Unit(String("rad"))).getValue() ;
|
---|
466 | // //if ( t[0] != direction[0] || t[1] != direction[1] ) {
|
---|
467 | // Double dx = t[0] - direction[0];
|
---|
468 | // Double dy = t[1] - direction[1];
|
---|
469 | // Double dd = sqrt(dx*dx + dy*dy);
|
---|
470 | // //if ( !allNearAbs( t, direction, tol ) ) {
|
---|
471 | // if ( dd > tol && in->nbeam() < 2 ) {
|
---|
472 | // flags[jrow] = userflag ;
|
---|
473 | // }
|
---|
474 | // }
|
---|
475 | // Vector<Float> outspec(nchan);
|
---|
476 | // Vector<uChar> outflag(nchan,0);
|
---|
477 | // Vector<Float> outtsys(1);/// @fixme when tsys is channel based
|
---|
478 | // for (uInt i=0; i<nchan; ++i) {
|
---|
479 | // Vector<Float> specs = specCol.getColumn(Slicer(Slice(i)));
|
---|
480 | // MaskedArray<Float> ma = maskedArray(specs,flags);
|
---|
481 | // outspec[i] = median(ma);
|
---|
482 | // if ( allEQ(ma.getMask(), False) )
|
---|
483 | // outflag[i] = userflag;// flag data
|
---|
484 | // }
|
---|
485 | // outtsys[0] = median(tsysCol.getColumn());
|
---|
486 | // specColOut.put(outrowCount+irow, outspec);
|
---|
487 | // flagColOut.put(outrowCount+irow, outflag);
|
---|
488 | // tsysColOut.put(outrowCount+irow, outtsys);
|
---|
489 | // Vector<Double> integ = intCol.getColumn() ;
|
---|
490 | // MaskedArray<Double> mi = maskedArray( integ, flags ) ;
|
---|
491 | // Double intsum = sum(mi);
|
---|
492 | // intColOut.put(outrowCount+irow, intsum);
|
---|
493 | // }
|
---|
494 | // outrowCount += rowNum ;
|
---|
495 | // ++iter;
|
---|
496 | }
|
---|
497 | return out;
|
---|
498 | }
|
---|
499 |
|
---|
500 | CountedPtr< Scantable > STMath::getScantable(const CountedPtr< Scantable >& in,
|
---|
501 | bool droprows)
|
---|
502 | {
|
---|
503 | if (insitu_) {
|
---|
504 | return in;
|
---|
505 | }
|
---|
506 | else {
|
---|
507 | // clone
|
---|
508 | return CountedPtr<Scantable>(new Scantable(*in, Bool(droprows)));
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 | CountedPtr< Scantable > STMath::unaryOperate( const CountedPtr< Scantable >& in,
|
---|
513 | float val,
|
---|
514 | const std::string& mode,
|
---|
515 | bool tsys )
|
---|
516 | {
|
---|
517 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
518 | Table& tab = out->table();
|
---|
519 | ArrayColumn<Float> specCol(tab,"SPECTRA");
|
---|
520 | ArrayColumn<Float> tsysCol(tab,"TSYS");
|
---|
521 | if (mode=="DIV") val = 1.0/val ;
|
---|
522 | else if (mode=="SUB") val *= -1.0 ;
|
---|
523 | for (uInt i=0; i<tab.nrow(); ++i) {
|
---|
524 | Vector<Float> spec;
|
---|
525 | Vector<Float> ts;
|
---|
526 | specCol.get(i, spec);
|
---|
527 | tsysCol.get(i, ts);
|
---|
528 | if (mode == "MUL" || mode == "DIV") {
|
---|
529 | //if (mode == "DIV") val = 1.0/val;
|
---|
530 | spec *= val;
|
---|
531 | specCol.put(i, spec);
|
---|
532 | if ( tsys ) {
|
---|
533 | ts *= val;
|
---|
534 | tsysCol.put(i, ts);
|
---|
535 | }
|
---|
536 | } else if ( mode == "ADD" || mode == "SUB") {
|
---|
537 | //if (mode == "SUB") val *= -1.0;
|
---|
538 | spec += val;
|
---|
539 | specCol.put(i, spec);
|
---|
540 | if ( tsys ) {
|
---|
541 | ts += val;
|
---|
542 | tsysCol.put(i, ts);
|
---|
543 | }
|
---|
544 | }
|
---|
545 | }
|
---|
546 | return out;
|
---|
547 | }
|
---|
548 |
|
---|
549 | CountedPtr< Scantable > STMath::arrayOperate( const CountedPtr< Scantable >& in,
|
---|
550 | const std::vector<float> val,
|
---|
551 | const std::string& mode,
|
---|
552 | const std::string& opmode,
|
---|
553 | bool tsys )
|
---|
554 | {
|
---|
555 | CountedPtr< Scantable > out ;
|
---|
556 | if ( opmode == "channel" ) {
|
---|
557 | out = arrayOperateChannel( in, val, mode, tsys ) ;
|
---|
558 | }
|
---|
559 | else if ( opmode == "row" ) {
|
---|
560 | out = arrayOperateRow( in, val, mode, tsys ) ;
|
---|
561 | }
|
---|
562 | else {
|
---|
563 | throw( AipsError( "Unknown array operation mode." ) ) ;
|
---|
564 | }
|
---|
565 | return out ;
|
---|
566 | }
|
---|
567 |
|
---|
568 | CountedPtr< Scantable > STMath::arrayOperateChannel( const CountedPtr< Scantable >& in,
|
---|
569 | const std::vector<float> val,
|
---|
570 | const std::string& mode,
|
---|
571 | bool tsys )
|
---|
572 | {
|
---|
573 | if ( val.size() == 1 ){
|
---|
574 | return unaryOperate( in, val[0], mode, tsys ) ;
|
---|
575 | }
|
---|
576 |
|
---|
577 | // conformity of SPECTRA and TSYS
|
---|
578 | if ( tsys ) {
|
---|
579 | TableIterator titer(in->table(), "IFNO");
|
---|
580 | while ( !titer.pastEnd() ) {
|
---|
581 | ArrayColumn<Float> specCol( in->table(), "SPECTRA" ) ;
|
---|
582 | ArrayColumn<Float> tsysCol( in->table(), "TSYS" ) ;
|
---|
583 | Array<Float> spec = specCol.getColumn() ;
|
---|
584 | Array<Float> ts = tsysCol.getColumn() ;
|
---|
585 | if ( !spec.conform( ts ) ) {
|
---|
586 | throw( AipsError( "SPECTRA and TSYS must conform in shape if you want to apply operation on Tsys." ) ) ;
|
---|
587 | }
|
---|
588 | titer.next() ;
|
---|
589 | }
|
---|
590 | }
|
---|
591 |
|
---|
592 | // check if all spectra in the scantable have the same number of channel
|
---|
593 | vector<uInt> nchans;
|
---|
594 | vector<uInt> ifnos = in->getIFNos() ;
|
---|
595 | for ( uInt i = 0 ; i < ifnos.size() ; i++ ) {
|
---|
596 | nchans.push_back( in->nchan( ifnos[i] ) ) ;
|
---|
597 | }
|
---|
598 | Vector<uInt> mchans( nchans ) ;
|
---|
599 | if ( anyNE( mchans, mchans[0] ) ) {
|
---|
600 | throw( AipsError("All spectra in the input scantable must have the same number of channel for vector operation." ) ) ;
|
---|
601 | }
|
---|
602 |
|
---|
603 | // check if vector size is equal to nchan
|
---|
604 | Vector<Float> fact( val ) ;
|
---|
605 | if ( fact.nelements() != mchans[0] ) {
|
---|
606 | throw( AipsError("Vector size must be 1 or be same as number of channel.") ) ;
|
---|
607 | }
|
---|
608 |
|
---|
609 | // check divided by zero
|
---|
610 | if ( ( mode == "DIV" ) && anyEQ( fact, (float)0.0 ) ) {
|
---|
611 | throw( AipsError("Divided by zero is not recommended." ) ) ;
|
---|
612 | }
|
---|
613 |
|
---|
614 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
615 | Table& tab = out->table();
|
---|
616 | ArrayColumn<Float> specCol(tab,"SPECTRA");
|
---|
617 | ArrayColumn<Float> tsysCol(tab,"TSYS");
|
---|
618 | if (mode == "DIV") fact = (float)1.0 / fact;
|
---|
619 | else if (mode == "SUB") fact *= (float)-1.0 ;
|
---|
620 | for (uInt i=0; i<tab.nrow(); ++i) {
|
---|
621 | Vector<Float> spec;
|
---|
622 | Vector<Float> ts;
|
---|
623 | specCol.get(i, spec);
|
---|
624 | tsysCol.get(i, ts);
|
---|
625 | if (mode == "MUL" || mode == "DIV") {
|
---|
626 | //if (mode == "DIV") fact = (float)1.0 / fact;
|
---|
627 | spec *= fact;
|
---|
628 | specCol.put(i, spec);
|
---|
629 | if ( tsys ) {
|
---|
630 | ts *= fact;
|
---|
631 | tsysCol.put(i, ts);
|
---|
632 | }
|
---|
633 | } else if ( mode == "ADD" || mode == "SUB") {
|
---|
634 | //if (mode == "SUB") fact *= (float)-1.0 ;
|
---|
635 | spec += fact;
|
---|
636 | specCol.put(i, spec);
|
---|
637 | if ( tsys ) {
|
---|
638 | ts += fact;
|
---|
639 | tsysCol.put(i, ts);
|
---|
640 | }
|
---|
641 | }
|
---|
642 | }
|
---|
643 | return out;
|
---|
644 | }
|
---|
645 |
|
---|
646 | CountedPtr< Scantable > STMath::arrayOperateRow( const CountedPtr< Scantable >& in,
|
---|
647 | const std::vector<float> val,
|
---|
648 | const std::string& mode,
|
---|
649 | bool tsys )
|
---|
650 | {
|
---|
651 | if ( val.size() == 1 ) {
|
---|
652 | return unaryOperate( in, val[0], mode, tsys ) ;
|
---|
653 | }
|
---|
654 |
|
---|
655 | // conformity of SPECTRA and TSYS
|
---|
656 | if ( tsys ) {
|
---|
657 | TableIterator titer(in->table(), "IFNO");
|
---|
658 | while ( !titer.pastEnd() ) {
|
---|
659 | ArrayColumn<Float> specCol( in->table(), "SPECTRA" ) ;
|
---|
660 | ArrayColumn<Float> tsysCol( in->table(), "TSYS" ) ;
|
---|
661 | Array<Float> spec = specCol.getColumn() ;
|
---|
662 | Array<Float> ts = tsysCol.getColumn() ;
|
---|
663 | if ( !spec.conform( ts ) ) {
|
---|
664 | throw( AipsError( "SPECTRA and TSYS must conform in shape if you want to apply operation on Tsys." ) ) ;
|
---|
665 | }
|
---|
666 | titer.next() ;
|
---|
667 | }
|
---|
668 | }
|
---|
669 |
|
---|
670 | // check if vector size is equal to nrow
|
---|
671 | Vector<Float> fact( val ) ;
|
---|
672 | if (fact.nelements() != uInt(in->nrow())) {
|
---|
673 | throw( AipsError("Vector size must be 1 or be same as number of row.") ) ;
|
---|
674 | }
|
---|
675 |
|
---|
676 | // check divided by zero
|
---|
677 | if ( ( mode == "DIV" ) && anyEQ( fact, (float)0.0 ) ) {
|
---|
678 | throw( AipsError("Divided by zero is not recommended." ) ) ;
|
---|
679 | }
|
---|
680 |
|
---|
681 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
682 | Table& tab = out->table();
|
---|
683 | ArrayColumn<Float> specCol(tab,"SPECTRA");
|
---|
684 | ArrayColumn<Float> tsysCol(tab,"TSYS");
|
---|
685 | if (mode == "DIV") fact = (float)1.0 / fact;
|
---|
686 | if (mode == "SUB") fact *= (float)-1.0 ;
|
---|
687 | for (uInt i=0; i<tab.nrow(); ++i) {
|
---|
688 | Vector<Float> spec;
|
---|
689 | Vector<Float> ts;
|
---|
690 | specCol.get(i, spec);
|
---|
691 | tsysCol.get(i, ts);
|
---|
692 | if (mode == "MUL" || mode == "DIV") {
|
---|
693 | spec *= fact[i];
|
---|
694 | specCol.put(i, spec);
|
---|
695 | if ( tsys ) {
|
---|
696 | ts *= fact[i];
|
---|
697 | tsysCol.put(i, ts);
|
---|
698 | }
|
---|
699 | } else if ( mode == "ADD" || mode == "SUB") {
|
---|
700 | spec += fact[i];
|
---|
701 | specCol.put(i, spec);
|
---|
702 | if ( tsys ) {
|
---|
703 | ts += fact[i];
|
---|
704 | tsysCol.put(i, ts);
|
---|
705 | }
|
---|
706 | }
|
---|
707 | }
|
---|
708 | return out;
|
---|
709 | }
|
---|
710 |
|
---|
711 | CountedPtr< Scantable > STMath::array2dOperate( const CountedPtr< Scantable >& in,
|
---|
712 | const std::vector< std::vector<float> > val,
|
---|
713 | const std::string& mode,
|
---|
714 | bool tsys )
|
---|
715 | {
|
---|
716 | // conformity of SPECTRA and TSYS
|
---|
717 | if ( tsys ) {
|
---|
718 | TableIterator titer(in->table(), "IFNO");
|
---|
719 | while ( !titer.pastEnd() ) {
|
---|
720 | ArrayColumn<Float> specCol( in->table(), "SPECTRA" ) ;
|
---|
721 | ArrayColumn<Float> tsysCol( in->table(), "TSYS" ) ;
|
---|
722 | Array<Float> spec = specCol.getColumn() ;
|
---|
723 | Array<Float> ts = tsysCol.getColumn() ;
|
---|
724 | if ( !spec.conform( ts ) ) {
|
---|
725 | throw( AipsError( "SPECTRA and TSYS must conform in shape if you want to apply operation on Tsys." ) ) ;
|
---|
726 | }
|
---|
727 | titer.next() ;
|
---|
728 | }
|
---|
729 | }
|
---|
730 |
|
---|
731 | // some checks
|
---|
732 | vector<uInt> nchans;
|
---|
733 | for (Int i = 0 ; i < in->nrow() ; i++) {
|
---|
734 | nchans.push_back((in->getSpectrum(i)).size());
|
---|
735 | }
|
---|
736 | //Vector<uInt> mchans( nchans ) ;
|
---|
737 | vector< Vector<Float> > facts ;
|
---|
738 | for ( uInt i = 0 ; i < nchans.size() ; i++ ) {
|
---|
739 | Vector<Float> tmp( val[i] ) ;
|
---|
740 | // check divided by zero
|
---|
741 | if ( ( mode == "DIV" ) && anyEQ( tmp, (float)0.0 ) ) {
|
---|
742 | throw( AipsError("Divided by zero is not recommended." ) ) ;
|
---|
743 | }
|
---|
744 | // conformity check
|
---|
745 | if ( tmp.nelements() != nchans[i] ) {
|
---|
746 | stringstream ss ;
|
---|
747 | ss << "Row " << i << ": Vector size must be same as number of channel." ;
|
---|
748 | throw( AipsError( ss.str() ) ) ;
|
---|
749 | }
|
---|
750 | facts.push_back( tmp ) ;
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
755 | Table& tab = out->table();
|
---|
756 | ArrayColumn<Float> specCol(tab,"SPECTRA");
|
---|
757 | ArrayColumn<Float> tsysCol(tab,"TSYS");
|
---|
758 | for (uInt i=0; i<tab.nrow(); ++i) {
|
---|
759 | Vector<Float> fact = facts[i] ;
|
---|
760 | Vector<Float> spec;
|
---|
761 | Vector<Float> ts;
|
---|
762 | specCol.get(i, spec);
|
---|
763 | tsysCol.get(i, ts);
|
---|
764 | if (mode == "MUL" || mode == "DIV") {
|
---|
765 | if (mode == "DIV") fact = (float)1.0 / fact;
|
---|
766 | spec *= fact;
|
---|
767 | specCol.put(i, spec);
|
---|
768 | if ( tsys ) {
|
---|
769 | ts *= fact;
|
---|
770 | tsysCol.put(i, ts);
|
---|
771 | }
|
---|
772 | } else if ( mode == "ADD" || mode == "SUB") {
|
---|
773 | if (mode == "SUB") fact *= (float)-1.0 ;
|
---|
774 | spec += fact;
|
---|
775 | specCol.put(i, spec);
|
---|
776 | if ( tsys ) {
|
---|
777 | ts += fact;
|
---|
778 | tsysCol.put(i, ts);
|
---|
779 | }
|
---|
780 | }
|
---|
781 | }
|
---|
782 | return out;
|
---|
783 | }
|
---|
784 |
|
---|
785 | CountedPtr<Scantable> STMath::binaryOperate(const CountedPtr<Scantable>& left,
|
---|
786 | const CountedPtr<Scantable>& right,
|
---|
787 | const std::string& mode)
|
---|
788 | {
|
---|
789 | bool insitu = insitu_;
|
---|
790 | if ( ! left->conformant(*right) ) {
|
---|
791 | throw(AipsError("'left' and 'right' scantables are not conformant."));
|
---|
792 | }
|
---|
793 | setInsitu(false);
|
---|
794 | CountedPtr< Scantable > out = getScantable(left, false);
|
---|
795 | setInsitu(insitu);
|
---|
796 | Table& tout = out->table();
|
---|
797 | Block<String> coln(5);
|
---|
798 | coln[0] = "SCANNO"; coln[1] = "CYCLENO"; coln[2] = "BEAMNO";
|
---|
799 | coln[3] = "IFNO"; coln[4] = "POLNO";
|
---|
800 | Table tmpl = tout.sort(coln);
|
---|
801 | Table tmpr = right->table().sort(coln);
|
---|
802 | ArrayColumn<Float> lspecCol(tmpl,"SPECTRA");
|
---|
803 | ROArrayColumn<Float> rspecCol(tmpr,"SPECTRA");
|
---|
804 | ArrayColumn<uChar> lflagCol(tmpl,"FLAGTRA");
|
---|
805 | ROArrayColumn<uChar> rflagCol(tmpr,"FLAGTRA");
|
---|
806 |
|
---|
807 | for (uInt i=0; i<tout.nrow(); ++i) {
|
---|
808 | Vector<Float> lspecvec, rspecvec;
|
---|
809 | Vector<uChar> lflagvec, rflagvec;
|
---|
810 | lspecvec = lspecCol(i); rspecvec = rspecCol(i);
|
---|
811 | lflagvec = lflagCol(i); rflagvec = rflagCol(i);
|
---|
812 | MaskedArray<Float> mleft = maskedArray(lspecvec, lflagvec);
|
---|
813 | MaskedArray<Float> mright = maskedArray(rspecvec, rflagvec);
|
---|
814 | if (mode == "ADD") {
|
---|
815 | mleft += mright;
|
---|
816 | } else if ( mode == "SUB") {
|
---|
817 | mleft -= mright;
|
---|
818 | } else if ( mode == "MUL") {
|
---|
819 | mleft *= mright;
|
---|
820 | } else if ( mode == "DIV") {
|
---|
821 | mleft /= mright;
|
---|
822 | } else {
|
---|
823 | throw(AipsError("Illegal binary operator"));
|
---|
824 | }
|
---|
825 | lspecCol.put(i, mleft.getArray());
|
---|
826 | }
|
---|
827 | return out;
|
---|
828 | }
|
---|
829 |
|
---|
830 |
|
---|
831 |
|
---|
832 | MaskedArray<Float> STMath::maskedArray( const Vector<Float>& s,
|
---|
833 | const Vector<uChar>& f)
|
---|
834 | {
|
---|
835 | Vector<Bool> mask;
|
---|
836 | mask.resize(f.shape());
|
---|
837 | convertArray(mask, f);
|
---|
838 | return MaskedArray<Float>(s,!mask);
|
---|
839 | }
|
---|
840 |
|
---|
841 | MaskedArray<Double> STMath::maskedArray( const Vector<Double>& s,
|
---|
842 | const Vector<uChar>& f)
|
---|
843 | {
|
---|
844 | Vector<Bool> mask;
|
---|
845 | mask.resize(f.shape());
|
---|
846 | convertArray(mask, f);
|
---|
847 | return MaskedArray<Double>(s,!mask);
|
---|
848 | }
|
---|
849 |
|
---|
850 | Vector<uChar> STMath::flagsFromMA(const MaskedArray<Float>& ma)
|
---|
851 | {
|
---|
852 | const Vector<Bool>& m = ma.getMask();
|
---|
853 | Vector<uChar> flags(m.shape());
|
---|
854 | convertArray(flags, !m);
|
---|
855 | return flags;
|
---|
856 | }
|
---|
857 |
|
---|
858 | CountedPtr< Scantable > STMath::autoQuotient( const CountedPtr< Scantable >& in,
|
---|
859 | const std::string & mode,
|
---|
860 | bool preserve )
|
---|
861 | {
|
---|
862 | /// @todo make other modes available
|
---|
863 | /// modes should be "nearest", "pair"
|
---|
864 | // make this operation non insitu
|
---|
865 | (void) mode; //currently unused
|
---|
866 | const Table& tin = in->table();
|
---|
867 | Table ons = tin(tin.col("SRCTYPE") == Int(SrcType::PSON));
|
---|
868 | Table offs = tin(tin.col("SRCTYPE") == Int(SrcType::PSOFF));
|
---|
869 | if ( offs.nrow() == 0 )
|
---|
870 | throw(AipsError("No 'off' scans present."));
|
---|
871 | // put all "on" scans into output table
|
---|
872 |
|
---|
873 | bool insitu = insitu_;
|
---|
874 | setInsitu(false);
|
---|
875 | CountedPtr< Scantable > out = getScantable(in, true);
|
---|
876 | setInsitu(insitu);
|
---|
877 | Table& tout = out->table();
|
---|
878 |
|
---|
879 | TableCopy::copyRows(tout, ons);
|
---|
880 | TableRow row(tout);
|
---|
881 | ROScalarColumn<Double> offtimeCol(offs, "TIME");
|
---|
882 | ArrayColumn<Float> outspecCol(tout, "SPECTRA");
|
---|
883 | ROArrayColumn<Float> outtsysCol(tout, "TSYS");
|
---|
884 | ArrayColumn<uChar> outflagCol(tout, "FLAGTRA");
|
---|
885 | for (uInt i=0; i < tout.nrow(); ++i) {
|
---|
886 | const TableRecord& rec = row.get(i);
|
---|
887 | Double ontime = rec.asDouble("TIME");
|
---|
888 | Table presel = offs(offs.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
|
---|
889 | && offs.col("IFNO") == Int(rec.asuInt("IFNO"))
|
---|
890 | && offs.col("POLNO") == Int(rec.asuInt("POLNO")) );
|
---|
891 | ROScalarColumn<Double> offtimeCol(presel, "TIME");
|
---|
892 |
|
---|
893 | Double mindeltat = min(abs(offtimeCol.getColumn() - ontime));
|
---|
894 | // Timestamp may vary within a cycle ???!!!
|
---|
895 | // increase this by 0.01 sec in case of rounding errors...
|
---|
896 | // There might be a better way to do this.
|
---|
897 | // fix to this fix. TIME is MJD, so 1.0d not 1.0s
|
---|
898 | mindeltat += 0.01/24./60./60.;
|
---|
899 | Table sel = presel( abs(presel.col("TIME")-ontime) <= mindeltat);
|
---|
900 |
|
---|
901 | if ( sel.nrow() < 1 ) {
|
---|
902 | throw(AipsError("No closest in time found... This could be a rounding "
|
---|
903 | "issue. Try quotient instead."));
|
---|
904 | }
|
---|
905 | TableRow offrow(sel);
|
---|
906 | const TableRecord& offrec = offrow.get(0);//should only be one row
|
---|
907 | RORecordFieldPtr< Array<Float> > specoff(offrec, "SPECTRA");
|
---|
908 | RORecordFieldPtr< Array<Float> > tsysoff(offrec, "TSYS");
|
---|
909 | RORecordFieldPtr< Array<uChar> > flagoff(offrec, "FLAGTRA");
|
---|
910 | /// @fixme this assumes tsys is a scalar not vector
|
---|
911 | Float tsysoffscalar = (*tsysoff)(IPosition(1,0));
|
---|
912 | Vector<Float> specon, tsyson;
|
---|
913 | outtsysCol.get(i, tsyson);
|
---|
914 | outspecCol.get(i, specon);
|
---|
915 | Vector<uChar> flagon;
|
---|
916 | outflagCol.get(i, flagon);
|
---|
917 | MaskedArray<Float> mon = maskedArray(specon, flagon);
|
---|
918 | MaskedArray<Float> moff = maskedArray(*specoff, *flagoff);
|
---|
919 | MaskedArray<Float> quot = (tsysoffscalar * mon / moff);
|
---|
920 | if (preserve) {
|
---|
921 | quot -= tsysoffscalar;
|
---|
922 | } else {
|
---|
923 | quot -= tsyson[0];
|
---|
924 | }
|
---|
925 | outspecCol.put(i, quot.getArray());
|
---|
926 | outflagCol.put(i, flagsFromMA(quot));
|
---|
927 | }
|
---|
928 | // renumber scanno
|
---|
929 | TableIterator it(tout, "SCANNO");
|
---|
930 | uInt i = 0;
|
---|
931 | while ( !it.pastEnd() ) {
|
---|
932 | Table t = it.table();
|
---|
933 | TableVector<uInt> vec(t, "SCANNO");
|
---|
934 | vec = i;
|
---|
935 | ++i;
|
---|
936 | ++it;
|
---|
937 | }
|
---|
938 | return out;
|
---|
939 | }
|
---|
940 |
|
---|
941 |
|
---|
942 | CountedPtr< Scantable > STMath::quotient( const CountedPtr< Scantable > & on,
|
---|
943 | const CountedPtr< Scantable > & off,
|
---|
944 | bool preserve )
|
---|
945 | {
|
---|
946 | bool insitu = insitu_;
|
---|
947 | if ( ! on->conformant(*off) ) {
|
---|
948 | throw(AipsError("'on' and 'off' scantables are not conformant."));
|
---|
949 | }
|
---|
950 | setInsitu(false);
|
---|
951 | CountedPtr< Scantable > out = getScantable(on, false);
|
---|
952 | setInsitu(insitu);
|
---|
953 | Table& tout = out->table();
|
---|
954 | const Table& toff = off->table();
|
---|
955 | TableIterator sit(tout, "SCANNO");
|
---|
956 | TableIterator s2it(toff, "SCANNO");
|
---|
957 | while ( !sit.pastEnd() ) {
|
---|
958 | Table ton = sit.table();
|
---|
959 | TableRow row(ton);
|
---|
960 | Table t = s2it.table();
|
---|
961 | ArrayColumn<Float> outspecCol(ton, "SPECTRA");
|
---|
962 | ROArrayColumn<Float> outtsysCol(ton, "TSYS");
|
---|
963 | ArrayColumn<uChar> outflagCol(ton, "FLAGTRA");
|
---|
964 | for (uInt i=0; i < ton.nrow(); ++i) {
|
---|
965 | const TableRecord& rec = row.get(i);
|
---|
966 | Table offsel = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
|
---|
967 | && t.col("IFNO") == Int(rec.asuInt("IFNO"))
|
---|
968 | && t.col("POLNO") == Int(rec.asuInt("POLNO")) );
|
---|
969 | if ( offsel.nrow() == 0 )
|
---|
970 | throw AipsError("STMath::quotient: no matching off");
|
---|
971 | TableRow offrow(offsel);
|
---|
972 | const TableRecord& offrec = offrow.get(0);//should be ncycles - take first
|
---|
973 | RORecordFieldPtr< Array<Float> > specoff(offrec, "SPECTRA");
|
---|
974 | RORecordFieldPtr< Array<Float> > tsysoff(offrec, "TSYS");
|
---|
975 | RORecordFieldPtr< Array<uChar> > flagoff(offrec, "FLAGTRA");
|
---|
976 | Float tsysoffscalar = (*tsysoff)(IPosition(1,0));
|
---|
977 | Vector<Float> specon, tsyson;
|
---|
978 | outtsysCol.get(i, tsyson);
|
---|
979 | outspecCol.get(i, specon);
|
---|
980 | Vector<uChar> flagon;
|
---|
981 | outflagCol.get(i, flagon);
|
---|
982 | MaskedArray<Float> mon = maskedArray(specon, flagon);
|
---|
983 | MaskedArray<Float> moff = maskedArray(*specoff, *flagoff);
|
---|
984 | MaskedArray<Float> quot = (tsysoffscalar * mon / moff);
|
---|
985 | if (preserve) {
|
---|
986 | quot -= tsysoffscalar;
|
---|
987 | } else {
|
---|
988 | quot -= tsyson[0];
|
---|
989 | }
|
---|
990 | outspecCol.put(i, quot.getArray());
|
---|
991 | outflagCol.put(i, flagsFromMA(quot));
|
---|
992 | }
|
---|
993 | ++sit;
|
---|
994 | ++s2it;
|
---|
995 | // take the first off for each on scan which doesn't have a
|
---|
996 | // matching off scan
|
---|
997 | // non <= noff: matching pairs, non > noff matching pairs then first off
|
---|
998 | if ( s2it.pastEnd() ) s2it.reset();
|
---|
999 | }
|
---|
1000 | return out;
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | // dototalpower (migration of GBTIDL procedure dototalpower.pro)
|
---|
1004 | // calibrate the CAL on-off pair. It calculate Tsys and average CAL on-off subintegrations
|
---|
1005 | // do it for each cycles in a specific scan.
|
---|
1006 | CountedPtr< Scantable > STMath::dototalpower( const CountedPtr< Scantable >& calon,
|
---|
1007 | const CountedPtr< Scantable >& caloff, Float tcal )
|
---|
1008 | {
|
---|
1009 | if ( ! calon->conformant(*caloff) ) {
|
---|
1010 | throw(AipsError("'CAL on' and 'CAL off' scantables are not conformant."));
|
---|
1011 | }
|
---|
1012 | setInsitu(false);
|
---|
1013 | CountedPtr< Scantable > out = getScantable(caloff, false);
|
---|
1014 | Table& tout = out->table();
|
---|
1015 | const Table& tcon = calon->table();
|
---|
1016 | Vector<Float> tcalout;
|
---|
1017 |
|
---|
1018 | std::map<uInt,uInt> tcalIdToRecNoMap;
|
---|
1019 | const Table& calOffTcalTable = caloff->tcal().table();
|
---|
1020 | {
|
---|
1021 | ROScalarColumn<uInt> calOffTcalTable_IDcol(calOffTcalTable, "ID");
|
---|
1022 | const Vector<uInt> tcalIds(calOffTcalTable_IDcol.getColumn());
|
---|
1023 | size_t tcalIdsEnd = tcalIds.nelements();
|
---|
1024 | for (uInt i = 0; i < tcalIdsEnd; i++) {
|
---|
1025 | tcalIdToRecNoMap[tcalIds[i]] = i;
|
---|
1026 | }
|
---|
1027 | }
|
---|
1028 | ROArrayColumn<Float> calOffTcalTable_TCALcol(calOffTcalTable, "TCAL");
|
---|
1029 |
|
---|
1030 | if ( tout.nrow() != tcon.nrow() ) {
|
---|
1031 | throw(AipsError("Mismatch in number of rows to form cal on - off pair."));
|
---|
1032 | }
|
---|
1033 | // iteration by scanno or cycle no.
|
---|
1034 | TableIterator sit(tout, "SCANNO");
|
---|
1035 | TableIterator s2it(tcon, "SCANNO");
|
---|
1036 | while ( !sit.pastEnd() ) {
|
---|
1037 | Table toff = sit.table();
|
---|
1038 | TableRow row(toff);
|
---|
1039 | Table t = s2it.table();
|
---|
1040 | ScalarColumn<Double> outintCol(toff, "INTERVAL");
|
---|
1041 | ArrayColumn<Float> outspecCol(toff, "SPECTRA");
|
---|
1042 | ArrayColumn<Float> outtsysCol(toff, "TSYS");
|
---|
1043 | ArrayColumn<uChar> outflagCol(toff, "FLAGTRA");
|
---|
1044 | ROScalarColumn<uInt> outtcalIdCol(toff, "TCAL_ID");
|
---|
1045 | ROScalarColumn<uInt> outpolCol(toff, "POLNO");
|
---|
1046 | ROScalarColumn<Double> onintCol(t, "INTERVAL");
|
---|
1047 | ROArrayColumn<Float> onspecCol(t, "SPECTRA");
|
---|
1048 | ROArrayColumn<Float> ontsysCol(t, "TSYS");
|
---|
1049 | ROArrayColumn<uChar> onflagCol(t, "FLAGTRA");
|
---|
1050 | //ROScalarColumn<uInt> ontcalIdCol(t, "TCAL_ID");
|
---|
1051 |
|
---|
1052 | for (uInt i=0; i < toff.nrow(); ++i) {
|
---|
1053 | //skip these checks -> assumes the data order are the same between the cal on off pairs
|
---|
1054 | //
|
---|
1055 | Vector<Float> specCalon, specCaloff;
|
---|
1056 | // to store scalar (mean) tsys
|
---|
1057 | Vector<Float> tsysout(1);
|
---|
1058 | uInt tcalId, polno;
|
---|
1059 | Double offint, onint;
|
---|
1060 | outpolCol.get(i, polno);
|
---|
1061 | outspecCol.get(i, specCaloff);
|
---|
1062 | onspecCol.get(i, specCalon);
|
---|
1063 | Vector<uChar> flagCaloff, flagCalon;
|
---|
1064 | outflagCol.get(i, flagCaloff);
|
---|
1065 | onflagCol.get(i, flagCalon);
|
---|
1066 | outtcalIdCol.get(i, tcalId);
|
---|
1067 | outintCol.get(i, offint);
|
---|
1068 | onintCol.get(i, onint);
|
---|
1069 | // caluculate mean Tsys
|
---|
1070 | uInt nchan = specCaloff.nelements();
|
---|
1071 | // percentage of edge cut off
|
---|
1072 | uInt pc = 10;
|
---|
1073 | uInt bchan = nchan/pc;
|
---|
1074 | uInt echan = nchan-bchan;
|
---|
1075 |
|
---|
1076 | Slicer chansl(IPosition(1,bchan-1), IPosition(1,echan-1), IPosition(1,1),Slicer::endIsLast);
|
---|
1077 | Vector<Float> testsubsp = specCaloff(chansl);
|
---|
1078 | MaskedArray<Float> spoff = maskedArray( specCaloff(chansl),flagCaloff(chansl) );
|
---|
1079 | MaskedArray<Float> spon = maskedArray( specCalon(chansl),flagCalon(chansl) );
|
---|
1080 | MaskedArray<Float> spdiff = spon-spoff;
|
---|
1081 | uInt noff = spoff.nelementsValid();
|
---|
1082 | //uInt non = spon.nelementsValid();
|
---|
1083 | uInt ndiff = spdiff.nelementsValid();
|
---|
1084 | Float meantsys;
|
---|
1085 |
|
---|
1086 | /**
|
---|
1087 | Double subspec, subdiff;
|
---|
1088 | uInt usednchan;
|
---|
1089 | subspec = 0;
|
---|
1090 | subdiff = 0;
|
---|
1091 | usednchan = 0;
|
---|
1092 | for(uInt k=(bchan-1); k<echan; k++) {
|
---|
1093 | subspec += specCaloff[k];
|
---|
1094 | subdiff += static_cast<Double>(specCalon[k]-specCaloff[k]);
|
---|
1095 | ++usednchan;
|
---|
1096 | }
|
---|
1097 | **/
|
---|
1098 | // get tcal if input tcal <= 0
|
---|
1099 | Float tcalUsed;
|
---|
1100 | tcalUsed = tcal;
|
---|
1101 | if ( tcal <= 0.0 ) {
|
---|
1102 | uInt tcalRecNo = tcalIdToRecNoMap[tcalId];
|
---|
1103 | calOffTcalTable_TCALcol.get(tcalRecNo, tcalout);
|
---|
1104 | // if (polno<=3) {
|
---|
1105 | // tcalUsed = tcalout[polno];
|
---|
1106 | // }
|
---|
1107 | // else {
|
---|
1108 | // tcalUsed = tcalout[0];
|
---|
1109 | // }
|
---|
1110 | if ( tcalout.size() == 1 )
|
---|
1111 | tcalUsed = tcalout[0] ;
|
---|
1112 | else if ( tcalout.size() == nchan )
|
---|
1113 | tcalUsed = mean(tcalout) ;
|
---|
1114 | else {
|
---|
1115 | uInt ipol = polno ;
|
---|
1116 | if ( ipol > 3 ) ipol = 0 ;
|
---|
1117 | tcalUsed = tcalout[ipol] ;
|
---|
1118 | }
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | Float meanoff;
|
---|
1122 | Float meandiff;
|
---|
1123 | if (noff && ndiff) {
|
---|
1124 | //Debug
|
---|
1125 | //if(noff!=ndiff) cerr<<"noff and ndiff is not equal"<<endl;
|
---|
1126 | //LogIO os( LogOrigin( "STMath", "dototalpower()", WHERE ) ) ;
|
---|
1127 | //if(noff!=ndiff) os<<"noff and ndiff is not equal"<<LogIO::POST;
|
---|
1128 | meanoff = sum(spoff)/noff;
|
---|
1129 | meandiff = sum(spdiff)/ndiff;
|
---|
1130 | meantsys= (meanoff/meandiff )*tcalUsed + tcalUsed/2;
|
---|
1131 | }
|
---|
1132 | else {
|
---|
1133 | meantsys=1;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | tsysout[0] = Float(meantsys);
|
---|
1137 | MaskedArray<Float> mcaloff = maskedArray(specCaloff, flagCaloff);
|
---|
1138 | MaskedArray<Float> mcalon = maskedArray(specCalon, flagCalon);
|
---|
1139 | MaskedArray<Float> sig = Float(0.5) * (mcaloff + mcalon);
|
---|
1140 | //uInt ncaloff = mcaloff.nelementsValid();
|
---|
1141 | //uInt ncalon = mcalon.nelementsValid();
|
---|
1142 |
|
---|
1143 | outintCol.put(i, offint+onint);
|
---|
1144 | outspecCol.put(i, sig.getArray());
|
---|
1145 | outflagCol.put(i, flagsFromMA(sig));
|
---|
1146 | outtsysCol.put(i, tsysout);
|
---|
1147 | }
|
---|
1148 | ++sit;
|
---|
1149 | ++s2it;
|
---|
1150 | }
|
---|
1151 | return out;
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | //dosigref - migrated from GBT IDL's dosigref.pro, do calibration of position switch
|
---|
1155 | // observatiions.
|
---|
1156 | // input: sig and ref scantables, and an optional boxcar smoothing width(default width=0,
|
---|
1157 | // no smoothing).
|
---|
1158 | // output: resultant scantable [= (sig-ref/ref)*tsys]
|
---|
1159 | CountedPtr< Scantable > STMath::dosigref( const CountedPtr < Scantable >& sig,
|
---|
1160 | const CountedPtr < Scantable >& ref,
|
---|
1161 | int smoothref,
|
---|
1162 | casa::Float tsysv,
|
---|
1163 | casa::Float tau )
|
---|
1164 | {
|
---|
1165 | if ( ! ref->conformant(*sig) ) {
|
---|
1166 | throw(AipsError("'sig' and 'ref' scantables are not conformant."));
|
---|
1167 | }
|
---|
1168 | setInsitu(false);
|
---|
1169 | CountedPtr< Scantable > out = getScantable(sig, false);
|
---|
1170 | CountedPtr< Scantable > smref;
|
---|
1171 | if ( smoothref > 1 ) {
|
---|
1172 | float fsmoothref = static_cast<float>(smoothref);
|
---|
1173 | std::string inkernel = "boxcar";
|
---|
1174 | smref = smooth(ref, inkernel, fsmoothref );
|
---|
1175 | ostringstream oss;
|
---|
1176 | oss<<"Applied smoothing of "<<fsmoothref<<" on the reference."<<endl;
|
---|
1177 | pushLog(String(oss));
|
---|
1178 | }
|
---|
1179 | else {
|
---|
1180 | smref = ref;
|
---|
1181 | }
|
---|
1182 | Table& tout = out->table();
|
---|
1183 | const Table& tref = smref->table();
|
---|
1184 | if ( tout.nrow() != tref.nrow() ) {
|
---|
1185 | throw(AipsError("Mismatch in number of rows to form on-source and reference pair."));
|
---|
1186 | }
|
---|
1187 | // iteration by scanno? or cycle no.
|
---|
1188 | TableIterator sit(tout, "SCANNO");
|
---|
1189 | TableIterator s2it(tref, "SCANNO");
|
---|
1190 | while ( !sit.pastEnd() ) {
|
---|
1191 | Table ton = sit.table();
|
---|
1192 | Table t = s2it.table();
|
---|
1193 | ScalarColumn<Double> outintCol(ton, "INTERVAL");
|
---|
1194 | ArrayColumn<Float> outspecCol(ton, "SPECTRA");
|
---|
1195 | ArrayColumn<Float> outtsysCol(ton, "TSYS");
|
---|
1196 | ArrayColumn<uChar> outflagCol(ton, "FLAGTRA");
|
---|
1197 | ArrayColumn<Float> refspecCol(t, "SPECTRA");
|
---|
1198 | ROScalarColumn<Double> refintCol(t, "INTERVAL");
|
---|
1199 | ROArrayColumn<Float> reftsysCol(t, "TSYS");
|
---|
1200 | ArrayColumn<uChar> refflagCol(t, "FLAGTRA");
|
---|
1201 | ROScalarColumn<Float> refelevCol(t, "ELEVATION");
|
---|
1202 | for (uInt i=0; i < ton.nrow(); ++i) {
|
---|
1203 |
|
---|
1204 | Double onint, refint;
|
---|
1205 | Vector<Float> specon, specref;
|
---|
1206 | // to store scalar (mean) tsys
|
---|
1207 | Vector<Float> tsysref;
|
---|
1208 | outintCol.get(i, onint);
|
---|
1209 | refintCol.get(i, refint);
|
---|
1210 | outspecCol.get(i, specon);
|
---|
1211 | refspecCol.get(i, specref);
|
---|
1212 | Vector<uChar> flagref, flagon;
|
---|
1213 | outflagCol.get(i, flagon);
|
---|
1214 | refflagCol.get(i, flagref);
|
---|
1215 | reftsysCol.get(i, tsysref);
|
---|
1216 |
|
---|
1217 | Float tsysrefscalar;
|
---|
1218 | if ( tsysv > 0.0 ) {
|
---|
1219 | ostringstream oss;
|
---|
1220 | Float elev;
|
---|
1221 | refelevCol.get(i, elev);
|
---|
1222 | oss << "user specified Tsys = " << tsysv;
|
---|
1223 | // do recalc elevation if EL = 0
|
---|
1224 | if ( elev == 0 ) {
|
---|
1225 | throw(AipsError("EL=0, elevation data is missing."));
|
---|
1226 | } else {
|
---|
1227 | if ( tau <= 0.0 ) {
|
---|
1228 | throw(AipsError("Valid tau is not supplied."));
|
---|
1229 | } else {
|
---|
1230 | tsysrefscalar = tsysv * exp(tau/elev);
|
---|
1231 | }
|
---|
1232 | }
|
---|
1233 | oss << ", corrected (for El) tsys= "<<tsysrefscalar;
|
---|
1234 | pushLog(String(oss));
|
---|
1235 | }
|
---|
1236 | else {
|
---|
1237 | tsysrefscalar = tsysref[0];
|
---|
1238 | }
|
---|
1239 | //get quotient spectrum
|
---|
1240 | MaskedArray<Float> mref = maskedArray(specref, flagref);
|
---|
1241 | MaskedArray<Float> mon = maskedArray(specon, flagon);
|
---|
1242 | MaskedArray<Float> specres = tsysrefscalar*((mon - mref)/mref);
|
---|
1243 | Double resint = onint*refint*smoothref/(onint+refint*smoothref);
|
---|
1244 |
|
---|
1245 | //Debug
|
---|
1246 | //cerr<<"Tsys used="<<tsysrefscalar<<endl;
|
---|
1247 | //LogIO os( LogOrigin( "STMath", "dosigref", WHERE ) ) ;
|
---|
1248 | //os<<"Tsys used="<<tsysrefscalar<<LogIO::POST;
|
---|
1249 | // fill the result, replay signal tsys by reference tsys
|
---|
1250 | outintCol.put(i, resint);
|
---|
1251 | outspecCol.put(i, specres.getArray());
|
---|
1252 | outflagCol.put(i, flagsFromMA(specres));
|
---|
1253 | outtsysCol.put(i, tsysref);
|
---|
1254 | }
|
---|
1255 | ++sit;
|
---|
1256 | ++s2it;
|
---|
1257 | }
|
---|
1258 | return out;
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | CountedPtr< Scantable > STMath::donod(const casa::CountedPtr<Scantable>& s,
|
---|
1262 | const std::vector<int>& scans,
|
---|
1263 | int smoothref,
|
---|
1264 | casa::Float tsysv,
|
---|
1265 | casa::Float tau,
|
---|
1266 | casa::Float tcal )
|
---|
1267 |
|
---|
1268 | {
|
---|
1269 | setInsitu(false);
|
---|
1270 | STSelector sel;
|
---|
1271 | std::vector<int> scan1, scan2, beams, types;
|
---|
1272 | std::vector< vector<int> > scanpair;
|
---|
1273 | //std::vector<string> calstate;
|
---|
1274 | std::vector<int> calstate;
|
---|
1275 | String msg;
|
---|
1276 |
|
---|
1277 | CountedPtr< Scantable > s1b1on, s1b1off, s1b2on, s1b2off;
|
---|
1278 | CountedPtr< Scantable > s2b1on, s2b1off, s2b2on, s2b2off;
|
---|
1279 |
|
---|
1280 | std::vector< CountedPtr< Scantable > > sctables;
|
---|
1281 | sctables.push_back(s1b1on);
|
---|
1282 | sctables.push_back(s1b1off);
|
---|
1283 | sctables.push_back(s1b2on);
|
---|
1284 | sctables.push_back(s1b2off);
|
---|
1285 | sctables.push_back(s2b1on);
|
---|
1286 | sctables.push_back(s2b1off);
|
---|
1287 | sctables.push_back(s2b2on);
|
---|
1288 | sctables.push_back(s2b2off);
|
---|
1289 |
|
---|
1290 | //check scanlist
|
---|
1291 | int n=s->checkScanInfo(scans);
|
---|
1292 | if (n==1) {
|
---|
1293 | throw(AipsError("Incorrect scan pairs. "));
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | // Assume scans contain only a pair of consecutive scan numbers.
|
---|
1297 | // It is assumed that first beam, b1, is on target.
|
---|
1298 | // There is no check if the first beam is on or not.
|
---|
1299 | if ( scans.size()==1 ) {
|
---|
1300 | scan1.push_back(scans[0]);
|
---|
1301 | scan2.push_back(scans[0]+1);
|
---|
1302 | } else if ( scans.size()==2 ) {
|
---|
1303 | scan1.push_back(scans[0]);
|
---|
1304 | scan2.push_back(scans[1]);
|
---|
1305 | } else {
|
---|
1306 | if ( scans.size()%2 == 0 ) {
|
---|
1307 | for (uInt i=0; i<scans.size(); i++) {
|
---|
1308 | if (i%2 == 0) {
|
---|
1309 | scan1.push_back(scans[i]);
|
---|
1310 | }
|
---|
1311 | else {
|
---|
1312 | scan2.push_back(scans[i]);
|
---|
1313 | }
|
---|
1314 | }
|
---|
1315 | } else {
|
---|
1316 | throw(AipsError("Odd numbers of scans, cannot form pairs."));
|
---|
1317 | }
|
---|
1318 | }
|
---|
1319 | scanpair.push_back(scan1);
|
---|
1320 | scanpair.push_back(scan2);
|
---|
1321 | //calstate.push_back("*calon");
|
---|
1322 | //calstate.push_back("*[^calon]");
|
---|
1323 | calstate.push_back(SrcType::NODCAL);
|
---|
1324 | calstate.push_back(SrcType::NOD);
|
---|
1325 | CountedPtr< Scantable > ws = getScantable(s, false);
|
---|
1326 | uInt l=0;
|
---|
1327 | while ( l < sctables.size() ) {
|
---|
1328 | for (uInt i=0; i < 2; i++) {
|
---|
1329 | for (uInt j=0; j < 2; j++) {
|
---|
1330 | for (uInt k=0; k < 2; k++) {
|
---|
1331 | sel.reset();
|
---|
1332 | sel.setScans(scanpair[i]);
|
---|
1333 | //sel.setName(calstate[k]);
|
---|
1334 | types.clear();
|
---|
1335 | types.push_back(calstate[k]);
|
---|
1336 | sel.setTypes(types);
|
---|
1337 | beams.clear();
|
---|
1338 | beams.push_back(j);
|
---|
1339 | sel.setBeams(beams);
|
---|
1340 | ws->setSelection(sel);
|
---|
1341 | sctables[l]= getScantable(ws, false);
|
---|
1342 | l++;
|
---|
1343 | }
|
---|
1344 | }
|
---|
1345 | }
|
---|
1346 | }
|
---|
1347 |
|
---|
1348 | // replace here by splitData or getData functionality
|
---|
1349 | CountedPtr< Scantable > sig1;
|
---|
1350 | CountedPtr< Scantable > ref1;
|
---|
1351 | CountedPtr< Scantable > sig2;
|
---|
1352 | CountedPtr< Scantable > ref2;
|
---|
1353 | CountedPtr< Scantable > calb1;
|
---|
1354 | CountedPtr< Scantable > calb2;
|
---|
1355 |
|
---|
1356 | msg=String("Processing dototalpower for subset of the data");
|
---|
1357 | ostringstream oss1;
|
---|
1358 | oss1 << msg << endl;
|
---|
1359 | pushLog(String(oss1));
|
---|
1360 | // Debug for IRC CS data
|
---|
1361 | //float tcal1=7.0;
|
---|
1362 | //float tcal2=4.0;
|
---|
1363 | sig1 = dototalpower(sctables[0], sctables[1], tcal=tcal);
|
---|
1364 | ref1 = dototalpower(sctables[2], sctables[3], tcal=tcal);
|
---|
1365 | ref2 = dototalpower(sctables[4], sctables[5], tcal=tcal);
|
---|
1366 | sig2 = dototalpower(sctables[6], sctables[7], tcal=tcal);
|
---|
1367 |
|
---|
1368 | // correction of user-specified tsys for elevation here
|
---|
1369 |
|
---|
1370 | // dosigref calibration
|
---|
1371 | msg=String("Processing dosigref for subset of the data");
|
---|
1372 | ostringstream oss2;
|
---|
1373 | oss2 << msg << endl;
|
---|
1374 | pushLog(String(oss2));
|
---|
1375 | calb1=dosigref(sig1,ref2,smoothref,tsysv,tau);
|
---|
1376 | calb2=dosigref(sig2,ref1,smoothref,tsysv,tau);
|
---|
1377 |
|
---|
1378 | // iteration by scanno or cycle no.
|
---|
1379 | Table& tcalb1 = calb1->table();
|
---|
1380 | Table& tcalb2 = calb2->table();
|
---|
1381 | TableIterator sit(tcalb1, "SCANNO");
|
---|
1382 | TableIterator s2it(tcalb2, "SCANNO");
|
---|
1383 | while ( !sit.pastEnd() ) {
|
---|
1384 | Table t1 = sit.table();
|
---|
1385 | Table t2= s2it.table();
|
---|
1386 | ArrayColumn<Float> outspecCol(t1, "SPECTRA");
|
---|
1387 | ArrayColumn<Float> outtsysCol(t1, "TSYS");
|
---|
1388 | ArrayColumn<uChar> outflagCol(t1, "FLAGTRA");
|
---|
1389 | ScalarColumn<Double> outintCol(t1, "INTERVAL");
|
---|
1390 | ArrayColumn<Float> t2specCol(t2, "SPECTRA");
|
---|
1391 | ROArrayColumn<Float> t2tsysCol(t2, "TSYS");
|
---|
1392 | ArrayColumn<uChar> t2flagCol(t2, "FLAGTRA");
|
---|
1393 | ROScalarColumn<Double> t2intCol(t2, "INTERVAL");
|
---|
1394 | for (uInt i=0; i < t1.nrow(); ++i) {
|
---|
1395 | Vector<Float> spec1, spec2;
|
---|
1396 | // to store scalar (mean) tsys
|
---|
1397 | Vector<Float> tsys1, tsys2;
|
---|
1398 | Vector<uChar> flag1, flag2;
|
---|
1399 | Double tint1, tint2;
|
---|
1400 | outspecCol.get(i, spec1);
|
---|
1401 | t2specCol.get(i, spec2);
|
---|
1402 | outflagCol.get(i, flag1);
|
---|
1403 | t2flagCol.get(i, flag2);
|
---|
1404 | outtsysCol.get(i, tsys1);
|
---|
1405 | t2tsysCol.get(i, tsys2);
|
---|
1406 | outintCol.get(i, tint1);
|
---|
1407 | t2intCol.get(i, tint2);
|
---|
1408 | // average
|
---|
1409 | // assume scalar tsys for weights
|
---|
1410 | Float wt1, wt2, tsyssq1, tsyssq2;
|
---|
1411 | tsyssq1 = tsys1[0]*tsys1[0];
|
---|
1412 | tsyssq2 = tsys2[0]*tsys2[0];
|
---|
1413 | wt1 = Float(tint1)/tsyssq1;
|
---|
1414 | wt2 = Float(tint2)/tsyssq2;
|
---|
1415 | Float invsumwt=1/(wt1+wt2);
|
---|
1416 | MaskedArray<Float> mspec1 = maskedArray(spec1, flag1);
|
---|
1417 | MaskedArray<Float> mspec2 = maskedArray(spec2, flag2);
|
---|
1418 | MaskedArray<Float> avspec = invsumwt * (wt1*mspec1 + wt2*mspec2);
|
---|
1419 | //Array<Float> avtsys = Float(0.5) * (tsys1 + tsys2);
|
---|
1420 | // cerr<< "Tsys1="<<tsys1<<" Tsys2="<<tsys2<<endl;
|
---|
1421 | // LogIO os( LogOrigin( "STMath", "donod", WHERE ) ) ;
|
---|
1422 | // os<< "Tsys1="<<tsys1<<" Tsys2="<<tsys2<<LogIO::POST;
|
---|
1423 | tsys1[0] = sqrt(tsyssq1 + tsyssq2);
|
---|
1424 | Array<Float> avtsys = tsys1;
|
---|
1425 |
|
---|
1426 | outspecCol.put(i, avspec.getArray());
|
---|
1427 | outflagCol.put(i, flagsFromMA(avspec));
|
---|
1428 | outtsysCol.put(i, avtsys);
|
---|
1429 | }
|
---|
1430 | ++sit;
|
---|
1431 | ++s2it;
|
---|
1432 | }
|
---|
1433 | return calb1;
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | //GBTIDL version of frequency switched data calibration
|
---|
1437 | CountedPtr< Scantable > STMath::dofs( const CountedPtr< Scantable >& s,
|
---|
1438 | const std::vector<int>& scans,
|
---|
1439 | int smoothref,
|
---|
1440 | casa::Float tsysv,
|
---|
1441 | casa::Float tau,
|
---|
1442 | casa::Float tcal )
|
---|
1443 | {
|
---|
1444 |
|
---|
1445 |
|
---|
1446 | (void) scans; //currently unused
|
---|
1447 | STSelector sel;
|
---|
1448 | CountedPtr< Scantable > ws = getScantable(s, false);
|
---|
1449 | CountedPtr< Scantable > sig, sigwcal, ref, refwcal;
|
---|
1450 | CountedPtr< Scantable > calsig, calref, out, out1, out2;
|
---|
1451 | Bool nofold=False;
|
---|
1452 | vector<int> types ;
|
---|
1453 |
|
---|
1454 | //split the data
|
---|
1455 | //sel.setName("*_fs");
|
---|
1456 | types.push_back( SrcType::FSON ) ;
|
---|
1457 | sel.setTypes( types ) ;
|
---|
1458 | ws->setSelection(sel);
|
---|
1459 | sig = getScantable(ws,false);
|
---|
1460 | sel.reset();
|
---|
1461 | types.clear() ;
|
---|
1462 | //sel.setName("*_fs_calon");
|
---|
1463 | types.push_back( SrcType::FONCAL ) ;
|
---|
1464 | sel.setTypes( types ) ;
|
---|
1465 | ws->setSelection(sel);
|
---|
1466 | sigwcal = getScantable(ws,false);
|
---|
1467 | sel.reset();
|
---|
1468 | types.clear() ;
|
---|
1469 | //sel.setName("*_fsr");
|
---|
1470 | types.push_back( SrcType::FSOFF ) ;
|
---|
1471 | sel.setTypes( types ) ;
|
---|
1472 | ws->setSelection(sel);
|
---|
1473 | ref = getScantable(ws,false);
|
---|
1474 | sel.reset();
|
---|
1475 | types.clear() ;
|
---|
1476 | //sel.setName("*_fsr_calon");
|
---|
1477 | types.push_back( SrcType::FOFFCAL ) ;
|
---|
1478 | sel.setTypes( types ) ;
|
---|
1479 | ws->setSelection(sel);
|
---|
1480 | refwcal = getScantable(ws,false);
|
---|
1481 | sel.reset() ;
|
---|
1482 | types.clear() ;
|
---|
1483 |
|
---|
1484 | calsig = dototalpower(sigwcal, sig, tcal=tcal);
|
---|
1485 | calref = dototalpower(refwcal, ref, tcal=tcal);
|
---|
1486 |
|
---|
1487 | out1=dosigref(calsig,calref,smoothref,tsysv,tau);
|
---|
1488 | out2=dosigref(calref,calsig,smoothref,tsysv,tau);
|
---|
1489 |
|
---|
1490 | Table& tabout1=out1->table();
|
---|
1491 | Table& tabout2=out2->table();
|
---|
1492 | ROScalarColumn<uInt> freqidCol1(tabout1, "FREQ_ID");
|
---|
1493 | ScalarColumn<uInt> freqidCol2(tabout2, "FREQ_ID");
|
---|
1494 | ROArrayColumn<Float> specCol(tabout2, "SPECTRA");
|
---|
1495 | Vector<Float> spec; specCol.get(0, spec);
|
---|
1496 | uInt nchan = spec.nelements();
|
---|
1497 | uInt freqid1; freqidCol1.get(0,freqid1);
|
---|
1498 | uInt freqid2; freqidCol2.get(0,freqid2);
|
---|
1499 | Double rp1, rp2, rv1, rv2, inc1, inc2;
|
---|
1500 | out1->frequencies().getEntry(rp1, rv1, inc1, freqid1);
|
---|
1501 | out2->frequencies().getEntry(rp2, rv2, inc2, freqid2);
|
---|
1502 | //cerr << out1->frequencies().table().nrow() << " " << out2->frequencies().table().nrow() << endl ;
|
---|
1503 | //LogIO os( LogOrigin( "STMath", "dofs()", WHERE ) ) ;
|
---|
1504 | //os << out1->frequencies().table().nrow() << " " << out2->frequencies().table().nrow() << LogIO::POST ;
|
---|
1505 | if (rp1==rp2) {
|
---|
1506 | Double foffset = rv1 - rv2;
|
---|
1507 | uInt choffset = static_cast<uInt>(foffset/abs(inc2));
|
---|
1508 | if (choffset >= nchan) {
|
---|
1509 | //cerr<<"out-band frequency switching, no folding"<<endl;
|
---|
1510 | LogIO os( LogOrigin( "STMath", "dofs()", WHERE ) ) ;
|
---|
1511 | os<<"out-band frequency switching, no folding"<<LogIO::POST;
|
---|
1512 | nofold = True;
|
---|
1513 | }
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | if (nofold) {
|
---|
1517 | std::vector< CountedPtr< Scantable > > tabs;
|
---|
1518 | tabs.push_back(out1);
|
---|
1519 | tabs.push_back(out2);
|
---|
1520 | out = merge(tabs);
|
---|
1521 | }
|
---|
1522 | else {
|
---|
1523 | //out = out1;
|
---|
1524 | Double choffset = ( rv1 - rv2 ) / inc2 ;
|
---|
1525 | out = dofold( out1, out2, choffset ) ;
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 | return out;
|
---|
1529 | }
|
---|
1530 |
|
---|
1531 | CountedPtr<Scantable> STMath::dofold( const CountedPtr<Scantable> &sig,
|
---|
1532 | const CountedPtr<Scantable> &ref,
|
---|
1533 | Double choffset,
|
---|
1534 | Double choffset2 )
|
---|
1535 | {
|
---|
1536 | LogIO os( LogOrigin( "STMath", "dofold", WHERE ) ) ;
|
---|
1537 | os << "choffset=" << choffset << " choffset2=" << choffset2 << LogIO::POST ;
|
---|
1538 |
|
---|
1539 | // output scantable
|
---|
1540 | CountedPtr<Scantable> out = getScantable( sig, false ) ;
|
---|
1541 |
|
---|
1542 | // separate choffset to integer part and decimal part
|
---|
1543 | Int ioffset = (Int)choffset ;
|
---|
1544 | Double doffset = choffset - ioffset ;
|
---|
1545 | Int ioffset2 = (Int)choffset2 ;
|
---|
1546 | Double doffset2 = choffset2 - ioffset2 ;
|
---|
1547 | os << "ioffset=" << ioffset << " doffset=" << doffset << LogIO::POST ;
|
---|
1548 | os << "ioffset2=" << ioffset2 << " doffset2=" << doffset2 << LogIO::POST ;
|
---|
1549 |
|
---|
1550 | // get column
|
---|
1551 | ROArrayColumn<Float> specCol1( sig->table(), "SPECTRA" ) ;
|
---|
1552 | ROArrayColumn<Float> specCol2( ref->table(), "SPECTRA" ) ;
|
---|
1553 | ROArrayColumn<Float> tsysCol1( sig->table(), "TSYS" ) ;
|
---|
1554 | ROArrayColumn<Float> tsysCol2( ref->table(), "TSYS" ) ;
|
---|
1555 | ROArrayColumn<uChar> flagCol1( sig->table(), "FLAGTRA" ) ;
|
---|
1556 | ROArrayColumn<uChar> flagCol2( ref->table(), "FLAGTRA" ) ;
|
---|
1557 | ROScalarColumn<Double> mjdCol1( sig->table(), "TIME" ) ;
|
---|
1558 | ROScalarColumn<Double> mjdCol2( ref->table(), "TIME" ) ;
|
---|
1559 | ROScalarColumn<Double> intervalCol1( sig->table(), "INTERVAL" ) ;
|
---|
1560 | ROScalarColumn<Double> intervalCol2( ref->table(), "INTERVAL" ) ;
|
---|
1561 |
|
---|
1562 | // check
|
---|
1563 | if ( ioffset == 0 ) {
|
---|
1564 | LogIO os( LogOrigin( "STMath", "dofold()", WHERE ) ) ;
|
---|
1565 | os << "channel offset is zero, no folding" << LogIO::POST ;
|
---|
1566 | return out ;
|
---|
1567 | }
|
---|
1568 | int nchan = ref->nchan() ;
|
---|
1569 | if ( abs(ioffset) >= nchan ) {
|
---|
1570 | LogIO os( LogOrigin( "STMath", "dofold()", WHERE ) ) ;
|
---|
1571 | os << "out-band frequency switching, no folding" << LogIO::POST ;
|
---|
1572 | return out ;
|
---|
1573 | }
|
---|
1574 |
|
---|
1575 | // attach column for output scantable
|
---|
1576 | ArrayColumn<Float> specColOut( out->table(), "SPECTRA" ) ;
|
---|
1577 | ArrayColumn<uChar> flagColOut( out->table(), "FLAGTRA" ) ;
|
---|
1578 | ArrayColumn<Float> tsysColOut( out->table(), "TSYS" ) ;
|
---|
1579 | ScalarColumn<Double> mjdColOut( out->table(), "TIME" ) ;
|
---|
1580 | ScalarColumn<Double> intervalColOut( out->table(), "INTERVAL" ) ;
|
---|
1581 | ScalarColumn<uInt> fidColOut( out->table(), "FREQ_ID" ) ;
|
---|
1582 |
|
---|
1583 | // for each row
|
---|
1584 | // assume that the data order are same between sig and ref
|
---|
1585 | RowAccumulator acc( asap::W_TINTSYS ) ;
|
---|
1586 | for ( int i = 0 ; i < sig->nrow() ; i++ ) {
|
---|
1587 | // get values
|
---|
1588 | Vector<Float> spsig ;
|
---|
1589 | specCol1.get( i, spsig ) ;
|
---|
1590 | Vector<Float> spref ;
|
---|
1591 | specCol2.get( i, spref ) ;
|
---|
1592 | Vector<Float> tsyssig ;
|
---|
1593 | tsysCol1.get( i, tsyssig ) ;
|
---|
1594 | Vector<Float> tsysref ;
|
---|
1595 | tsysCol2.get( i, tsysref ) ;
|
---|
1596 | Vector<uChar> flagsig ;
|
---|
1597 | flagCol1.get( i, flagsig ) ;
|
---|
1598 | Vector<uChar> flagref ;
|
---|
1599 | flagCol2.get( i, flagref ) ;
|
---|
1600 | Double timesig ;
|
---|
1601 | mjdCol1.get( i, timesig ) ;
|
---|
1602 | Double timeref ;
|
---|
1603 | mjdCol2.get( i, timeref ) ;
|
---|
1604 | Double intsig ;
|
---|
1605 | intervalCol1.get( i, intsig ) ;
|
---|
1606 | Double intref ;
|
---|
1607 | intervalCol2.get( i, intref ) ;
|
---|
1608 |
|
---|
1609 | // shift reference spectra
|
---|
1610 | int refchan = spref.nelements() ;
|
---|
1611 | Vector<Float> sspref( spref.nelements() ) ;
|
---|
1612 | Vector<Float> stsysref( tsysref.nelements() ) ;
|
---|
1613 | Vector<uChar> sflagref( flagref.nelements() ) ;
|
---|
1614 | if ( ioffset > 0 ) {
|
---|
1615 | // SPECTRA and FLAGTRA
|
---|
1616 | for ( int j = 0 ; j < refchan-ioffset ; j++ ) {
|
---|
1617 | sspref[j] = spref[j+ioffset] ;
|
---|
1618 | sflagref[j] = flagref[j+ioffset] ;
|
---|
1619 | }
|
---|
1620 | for ( int j = refchan-ioffset ; j < refchan ; j++ ) {
|
---|
1621 | sspref[j] = spref[j-refchan+ioffset] ;
|
---|
1622 | sflagref[j] = flagref[j-refchan+ioffset] ;
|
---|
1623 | }
|
---|
1624 | spref = sspref.copy() ;
|
---|
1625 | flagref = sflagref.copy() ;
|
---|
1626 | for ( int j = 0 ; j < refchan - 1 ; j++ ) {
|
---|
1627 | sspref[j] = doffset * spref[j+1] + ( 1.0 - doffset ) * spref[j] ;
|
---|
1628 | sflagref[j] = flagref[j+1] + flagref[j] ;
|
---|
1629 | }
|
---|
1630 | sspref[refchan-1] = doffset * spref[0] + ( 1.0 - doffset ) * spref[refchan-1] ;
|
---|
1631 | sflagref[refchan-1] = flagref[0] + flagref[refchan-1] ;
|
---|
1632 |
|
---|
1633 | // TSYS
|
---|
1634 | if ( spref.nelements() == tsysref.nelements() ) {
|
---|
1635 | for ( int j = 0 ; j < refchan-ioffset ; j++ ) {
|
---|
1636 | stsysref[j] = tsysref[j+ioffset] ;
|
---|
1637 | }
|
---|
1638 | for ( int j = refchan-ioffset ; j < refchan ; j++ ) {
|
---|
1639 | stsysref[j] = tsysref[j-refchan+ioffset] ;
|
---|
1640 | }
|
---|
1641 | tsysref = stsysref.copy() ;
|
---|
1642 | for ( int j = 0 ; j < refchan - 1 ; j++ ) {
|
---|
1643 | stsysref[j] = doffset * tsysref[j+1] + ( 1.0 - doffset ) * tsysref[j] ;
|
---|
1644 | }
|
---|
1645 | stsysref[refchan-1] = doffset * tsysref[0] + ( 1.0 - doffset ) * tsysref[refchan-1] ;
|
---|
1646 | }
|
---|
1647 | }
|
---|
1648 | else {
|
---|
1649 | // SPECTRA and FLAGTRA
|
---|
1650 | for ( int j = 0 ; j < abs(ioffset) ; j++ ) {
|
---|
1651 | sspref[j] = spref[refchan+ioffset+j] ;
|
---|
1652 | sflagref[j] = flagref[refchan+ioffset+j] ;
|
---|
1653 | }
|
---|
1654 | for ( int j = abs(ioffset) ; j < refchan ; j++ ) {
|
---|
1655 | sspref[j] = spref[j+ioffset] ;
|
---|
1656 | sflagref[j] = flagref[j+ioffset] ;
|
---|
1657 | }
|
---|
1658 | spref = sspref.copy() ;
|
---|
1659 | flagref = sflagref.copy() ;
|
---|
1660 | sspref[0] = doffset * spref[refchan-1] + ( 1.0 - doffset ) * spref[0] ;
|
---|
1661 | sflagref[0] = flagref[0] + flagref[refchan-1] ;
|
---|
1662 | for ( int j = 1 ; j < refchan ; j++ ) {
|
---|
1663 | sspref[j] = doffset * spref[j-1] + ( 1.0 - doffset ) * spref[j] ;
|
---|
1664 | sflagref[j] = flagref[j-1] + flagref[j] ;
|
---|
1665 | }
|
---|
1666 | // TSYS
|
---|
1667 | if ( spref.nelements() == tsysref.nelements() ) {
|
---|
1668 | for ( int j = 0 ; j < abs(ioffset) ; j++ ) {
|
---|
1669 | stsysref[j] = tsysref[refchan+ioffset+j] ;
|
---|
1670 | }
|
---|
1671 | for ( int j = abs(ioffset) ; j < refchan ; j++ ) {
|
---|
1672 | stsysref[j] = tsysref[j+ioffset] ;
|
---|
1673 | }
|
---|
1674 | tsysref = stsysref.copy() ;
|
---|
1675 | stsysref[0] = doffset * tsysref[refchan-1] + ( 1.0 - doffset ) * tsysref[0] ;
|
---|
1676 | for ( int j = 1 ; j < refchan ; j++ ) {
|
---|
1677 | stsysref[j] = doffset * tsysref[j-1] + ( 1.0 - doffset ) * tsysref[j] ;
|
---|
1678 | }
|
---|
1679 | }
|
---|
1680 | }
|
---|
1681 |
|
---|
1682 | // shift signal spectra if necessary (only for APEX?)
|
---|
1683 | if ( choffset2 != 0.0 ) {
|
---|
1684 | int sigchan = spsig.nelements() ;
|
---|
1685 | Vector<Float> sspsig( spsig.nelements() ) ;
|
---|
1686 | Vector<Float> stsyssig( tsyssig.nelements() ) ;
|
---|
1687 | Vector<uChar> sflagsig( flagsig.nelements() ) ;
|
---|
1688 | if ( ioffset2 > 0 ) {
|
---|
1689 | // SPECTRA and FLAGTRA
|
---|
1690 | for ( int j = 0 ; j < sigchan-ioffset2 ; j++ ) {
|
---|
1691 | sspsig[j] = spsig[j+ioffset2] ;
|
---|
1692 | sflagsig[j] = flagsig[j+ioffset2] ;
|
---|
1693 | }
|
---|
1694 | for ( int j = sigchan-ioffset2 ; j < sigchan ; j++ ) {
|
---|
1695 | sspsig[j] = spsig[j-sigchan+ioffset2] ;
|
---|
1696 | sflagsig[j] = flagsig[j-sigchan+ioffset2] ;
|
---|
1697 | }
|
---|
1698 | spsig = sspsig.copy() ;
|
---|
1699 | flagsig = sflagsig.copy() ;
|
---|
1700 | for ( int j = 0 ; j < sigchan - 1 ; j++ ) {
|
---|
1701 | sspsig[j] = doffset2 * spsig[j+1] + ( 1.0 - doffset2 ) * spsig[j] ;
|
---|
1702 | sflagsig[j] = flagsig[j+1] || flagsig[j] ;
|
---|
1703 | }
|
---|
1704 | sspsig[sigchan-1] = doffset2 * spsig[0] + ( 1.0 - doffset2 ) * spsig[sigchan-1] ;
|
---|
1705 | sflagsig[sigchan-1] = flagsig[0] || flagsig[sigchan-1] ;
|
---|
1706 | // TSTS
|
---|
1707 | if ( spsig.nelements() == tsyssig.nelements() ) {
|
---|
1708 | for ( int j = 0 ; j < sigchan-ioffset2 ; j++ ) {
|
---|
1709 | stsyssig[j] = tsyssig[j+ioffset2] ;
|
---|
1710 | }
|
---|
1711 | for ( int j = sigchan-ioffset2 ; j < sigchan ; j++ ) {
|
---|
1712 | stsyssig[j] = tsyssig[j-sigchan+ioffset2] ;
|
---|
1713 | }
|
---|
1714 | tsyssig = stsyssig.copy() ;
|
---|
1715 | for ( int j = 0 ; j < sigchan - 1 ; j++ ) {
|
---|
1716 | stsyssig[j] = doffset2 * tsyssig[j+1] + ( 1.0 - doffset2 ) * tsyssig[j] ;
|
---|
1717 | }
|
---|
1718 | stsyssig[sigchan-1] = doffset2 * tsyssig[0] + ( 1.0 - doffset2 ) * tsyssig[sigchan-1] ;
|
---|
1719 | }
|
---|
1720 | }
|
---|
1721 | else {
|
---|
1722 | // SPECTRA and FLAGTRA
|
---|
1723 | for ( int j = 0 ; j < abs(ioffset2) ; j++ ) {
|
---|
1724 | sspsig[j] = spsig[sigchan+ioffset2+j] ;
|
---|
1725 | sflagsig[j] = flagsig[sigchan+ioffset2+j] ;
|
---|
1726 | }
|
---|
1727 | for ( int j = abs(ioffset2) ; j < sigchan ; j++ ) {
|
---|
1728 | sspsig[j] = spsig[j+ioffset2] ;
|
---|
1729 | sflagsig[j] = flagsig[j+ioffset2] ;
|
---|
1730 | }
|
---|
1731 | spsig = sspsig.copy() ;
|
---|
1732 | flagsig = sflagsig.copy() ;
|
---|
1733 | sspsig[0] = doffset2 * spsig[sigchan-1] + ( 1.0 - doffset2 ) * spsig[0] ;
|
---|
1734 | sflagsig[0] = flagsig[0] + flagsig[sigchan-1] ;
|
---|
1735 | for ( int j = 1 ; j < sigchan ; j++ ) {
|
---|
1736 | sspsig[j] = doffset2 * spsig[j-1] + ( 1.0 - doffset2 ) * spsig[j] ;
|
---|
1737 | sflagsig[j] = flagsig[j-1] + flagsig[j] ;
|
---|
1738 | }
|
---|
1739 | // TSYS
|
---|
1740 | if ( spsig.nelements() == tsyssig.nelements() ) {
|
---|
1741 | for ( int j = 0 ; j < abs(ioffset2) ; j++ ) {
|
---|
1742 | stsyssig[j] = tsyssig[sigchan+ioffset2+j] ;
|
---|
1743 | }
|
---|
1744 | for ( int j = abs(ioffset2) ; j < sigchan ; j++ ) {
|
---|
1745 | stsyssig[j] = tsyssig[j+ioffset2] ;
|
---|
1746 | }
|
---|
1747 | tsyssig = stsyssig.copy() ;
|
---|
1748 | stsyssig[0] = doffset2 * tsyssig[sigchan-1] + ( 1.0 - doffset2 ) * tsyssig[0] ;
|
---|
1749 | for ( int j = 1 ; j < sigchan ; j++ ) {
|
---|
1750 | stsyssig[j] = doffset2 * tsyssig[j-1] + ( 1.0 - doffset2 ) * tsyssig[j] ;
|
---|
1751 | }
|
---|
1752 | }
|
---|
1753 | }
|
---|
1754 | }
|
---|
1755 |
|
---|
1756 | // folding
|
---|
1757 | acc.add( spsig, !flagsig, tsyssig, intsig, timesig ) ;
|
---|
1758 | acc.add( sspref, !sflagref, stsysref, intref, timeref ) ;
|
---|
1759 |
|
---|
1760 | // put result
|
---|
1761 | specColOut.put( i, acc.getSpectrum() ) ;
|
---|
1762 | const Vector<Bool> &msk = acc.getMask() ;
|
---|
1763 | Vector<uChar> flg( msk.shape() ) ;
|
---|
1764 | convertArray( flg, !msk ) ;
|
---|
1765 | flagColOut.put( i, flg ) ;
|
---|
1766 | tsysColOut.put( i, acc.getTsys() ) ;
|
---|
1767 | intervalColOut.put( i, acc.getInterval() ) ;
|
---|
1768 | mjdColOut.put( i, acc.getTime() ) ;
|
---|
1769 | // change FREQ_ID to unshifted IF setting (only for APEX?)
|
---|
1770 | if ( choffset2 != 0.0 ) {
|
---|
1771 | uInt freqid = fidColOut( 0 ) ; // assume single-IF data
|
---|
1772 | double refpix, refval, increment ;
|
---|
1773 | out->frequencies().getEntry( refpix, refval, increment, freqid ) ;
|
---|
1774 | refval -= choffset * increment ;
|
---|
1775 | uInt newfreqid = out->frequencies().addEntry( refpix, refval, increment ) ;
|
---|
1776 | Vector<uInt> freqids = fidColOut.getColumn() ;
|
---|
1777 | for ( uInt j = 0 ; j < freqids.nelements() ; j++ ) {
|
---|
1778 | if ( freqids[j] == freqid )
|
---|
1779 | freqids[j] = newfreqid ;
|
---|
1780 | }
|
---|
1781 | fidColOut.putColumn( freqids ) ;
|
---|
1782 | }
|
---|
1783 |
|
---|
1784 | acc.reset() ;
|
---|
1785 | }
|
---|
1786 |
|
---|
1787 | return out ;
|
---|
1788 | }
|
---|
1789 |
|
---|
1790 |
|
---|
1791 | CountedPtr< Scantable > STMath::freqSwitch( const CountedPtr< Scantable >& in )
|
---|
1792 | {
|
---|
1793 | // make copy or reference
|
---|
1794 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
1795 | Table& tout = out->table();
|
---|
1796 | Block<String> cols(4);
|
---|
1797 | cols[0] = String("SCANNO");
|
---|
1798 | cols[1] = String("CYCLENO");
|
---|
1799 | cols[2] = String("BEAMNO");
|
---|
1800 | cols[3] = String("POLNO");
|
---|
1801 | TableIterator iter(tout, cols);
|
---|
1802 | while (!iter.pastEnd()) {
|
---|
1803 | Table subt = iter.table();
|
---|
1804 | // this should leave us with two rows for the two IFs....if not ignore
|
---|
1805 | if (subt.nrow() != 2 ) {
|
---|
1806 | continue;
|
---|
1807 | }
|
---|
1808 | ArrayColumn<Float> specCol(subt, "SPECTRA");
|
---|
1809 | ArrayColumn<Float> tsysCol(subt, "TSYS");
|
---|
1810 | ArrayColumn<uChar> flagCol(subt, "FLAGTRA");
|
---|
1811 | Vector<Float> onspec,offspec, ontsys, offtsys;
|
---|
1812 | Vector<uChar> onflag, offflag;
|
---|
1813 | tsysCol.get(0, ontsys); tsysCol.get(1, offtsys);
|
---|
1814 | specCol.get(0, onspec); specCol.get(1, offspec);
|
---|
1815 | flagCol.get(0, onflag); flagCol.get(1, offflag);
|
---|
1816 | MaskedArray<Float> on = maskedArray(onspec, onflag);
|
---|
1817 | MaskedArray<Float> off = maskedArray(offspec, offflag);
|
---|
1818 | MaskedArray<Float> oncopy = on.copy();
|
---|
1819 |
|
---|
1820 | on /= off; on -= 1.0f;
|
---|
1821 | on *= ontsys[0];
|
---|
1822 | off /= oncopy; off -= 1.0f;
|
---|
1823 | off *= offtsys[0];
|
---|
1824 | specCol.put(0, on.getArray());
|
---|
1825 | const Vector<Bool>& m0 = on.getMask();
|
---|
1826 | Vector<uChar> flags0(m0.shape());
|
---|
1827 | convertArray(flags0, !m0);
|
---|
1828 | flagCol.put(0, flags0);
|
---|
1829 |
|
---|
1830 | specCol.put(1, off.getArray());
|
---|
1831 | const Vector<Bool>& m1 = off.getMask();
|
---|
1832 | Vector<uChar> flags1(m1.shape());
|
---|
1833 | convertArray(flags1, !m1);
|
---|
1834 | flagCol.put(1, flags1);
|
---|
1835 | ++iter;
|
---|
1836 | }
|
---|
1837 |
|
---|
1838 | return out;
|
---|
1839 | }
|
---|
1840 |
|
---|
1841 | std::vector< float > STMath::statistic( const CountedPtr< Scantable > & in,
|
---|
1842 | const std::vector< bool > & mask,
|
---|
1843 | const std::string& which )
|
---|
1844 | {
|
---|
1845 |
|
---|
1846 | Vector<Bool> m(mask);
|
---|
1847 | const Table& tab = in->table();
|
---|
1848 | ROArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
1849 | ROArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
1850 | std::vector<float> out;
|
---|
1851 | for (uInt i=0; i < tab.nrow(); ++i ) {
|
---|
1852 | Vector<Float> spec; specCol.get(i, spec);
|
---|
1853 | Vector<uChar> flag; flagCol.get(i, flag);
|
---|
1854 | MaskedArray<Float> ma = maskedArray(spec, flag);
|
---|
1855 | float outstat = 0.0;
|
---|
1856 | if ( spec.nelements() == m.nelements() ) {
|
---|
1857 | outstat = mathutil::statistics(which, ma(m));
|
---|
1858 | } else {
|
---|
1859 | outstat = mathutil::statistics(which, ma);
|
---|
1860 | }
|
---|
1861 | out.push_back(outstat);
|
---|
1862 | }
|
---|
1863 | return out;
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | std::vector< float > STMath::statisticRow( const CountedPtr< Scantable > & in,
|
---|
1867 | const std::vector< bool > & mask,
|
---|
1868 | const std::string& which,
|
---|
1869 | int row )
|
---|
1870 | {
|
---|
1871 |
|
---|
1872 | Vector<Bool> m(mask);
|
---|
1873 | const Table& tab = in->table();
|
---|
1874 | ROArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
1875 | ROArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
1876 | std::vector<float> out;
|
---|
1877 |
|
---|
1878 | Vector<Float> spec; specCol.get(row, spec);
|
---|
1879 | Vector<uChar> flag; flagCol.get(row, flag);
|
---|
1880 | MaskedArray<Float> ma = maskedArray(spec, flag);
|
---|
1881 | float outstat = 0.0;
|
---|
1882 | if ( spec.nelements() == m.nelements() ) {
|
---|
1883 | outstat = mathutil::statistics(which, ma(m));
|
---|
1884 | } else {
|
---|
1885 | outstat = mathutil::statistics(which, ma);
|
---|
1886 | }
|
---|
1887 | out.push_back(outstat);
|
---|
1888 |
|
---|
1889 | return out;
|
---|
1890 | }
|
---|
1891 |
|
---|
1892 | std::vector< int > STMath::minMaxChan( const CountedPtr< Scantable > & in,
|
---|
1893 | const std::vector< bool > & mask,
|
---|
1894 | const std::string& which )
|
---|
1895 | {
|
---|
1896 |
|
---|
1897 | Vector<Bool> m(mask);
|
---|
1898 | const Table& tab = in->table();
|
---|
1899 | ROArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
1900 | ROArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
1901 | std::vector<int> out;
|
---|
1902 | for (uInt i=0; i < tab.nrow(); ++i ) {
|
---|
1903 | Vector<Float> spec; specCol.get(i, spec);
|
---|
1904 | Vector<uChar> flag; flagCol.get(i, flag);
|
---|
1905 | MaskedArray<Float> ma = maskedArray(spec, flag);
|
---|
1906 | if (ma.ndim() != 1) {
|
---|
1907 | throw (ArrayError(
|
---|
1908 | "std::vector<int> STMath::minMaxChan("
|
---|
1909 | "ContedPtr<Scantable> &in, std::vector<bool> &mask, "
|
---|
1910 | " std::string &which)"
|
---|
1911 | " - MaskedArray is not 1D"));
|
---|
1912 | }
|
---|
1913 | IPosition outpos(1,0);
|
---|
1914 | if ( spec.nelements() == m.nelements() ) {
|
---|
1915 | outpos = mathutil::minMaxPos(which, ma(m));
|
---|
1916 | } else {
|
---|
1917 | outpos = mathutil::minMaxPos(which, ma);
|
---|
1918 | }
|
---|
1919 | out.push_back(outpos[0]);
|
---|
1920 | }
|
---|
1921 | return out;
|
---|
1922 | }
|
---|
1923 |
|
---|
1924 | CountedPtr< Scantable > STMath::bin( const CountedPtr< Scantable > & in,
|
---|
1925 | int width )
|
---|
1926 | {
|
---|
1927 | if ( !in->getSelection().empty() ) throw(AipsError("Can't bin subset of the data."));
|
---|
1928 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
1929 | Table& tout = out->table();
|
---|
1930 | out->frequencies().rescale(width, "BIN");
|
---|
1931 | ArrayColumn<Float> specCol(tout, "SPECTRA");
|
---|
1932 | ArrayColumn<uChar> flagCol(tout, "FLAGTRA");
|
---|
1933 | ArrayColumn<Float> tsysCol(tout, "TSYS");
|
---|
1934 |
|
---|
1935 | for (uInt i=0; i < tout.nrow(); ++i ) {
|
---|
1936 | MaskedArray<Float> main = maskedArray(specCol(i), flagCol(i));
|
---|
1937 | MaskedArray<Float> maout;
|
---|
1938 | LatticeUtilities::bin(maout, main, 0, Int(width));
|
---|
1939 | specCol.put(i, maout.getArray());
|
---|
1940 | flagCol.put(i, flagsFromMA(maout));
|
---|
1941 | if (tsysCol(i).nelements() == specCol(i).nelements()) {
|
---|
1942 | MaskedArray<Float> matsysin = maskedArray(tsysCol(i), flagCol(i));
|
---|
1943 | MaskedArray<Float> matsysout;
|
---|
1944 | LatticeUtilities::bin(matsysout, matsysin, 0, Int(width));
|
---|
1945 | tsysCol.put(i, matsysout.getArray());
|
---|
1946 | }
|
---|
1947 | // take only the first binned spectrum's length for the deprecated
|
---|
1948 | // global header item nChan
|
---|
1949 | if (i==0) tout.rwKeywordSet().define(String("nChan"),
|
---|
1950 | Int(maout.getArray().nelements()));
|
---|
1951 | }
|
---|
1952 | return out;
|
---|
1953 | }
|
---|
1954 |
|
---|
1955 | CountedPtr< Scantable > STMath::resample( const CountedPtr< Scantable >& in,
|
---|
1956 | const std::string& method,
|
---|
1957 | float width )
|
---|
1958 | //
|
---|
1959 | // Should add the possibility of width being specified in km/s. This means
|
---|
1960 | // that for each freqID (SpectralCoordinate) we will need to convert to an
|
---|
1961 | // average channel width (say at the reference pixel). Then we would need
|
---|
1962 | // to be careful to make sure each spectrum (of different freqID)
|
---|
1963 | // is the same length.
|
---|
1964 | //
|
---|
1965 | {
|
---|
1966 | //InterpolateArray1D<Double,Float>::InterpolationMethod interp;
|
---|
1967 | Int interpMethod(stringToIMethod(method));
|
---|
1968 |
|
---|
1969 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
1970 | Table& tout = out->table();
|
---|
1971 |
|
---|
1972 | // Resample SpectralCoordinates (one per freqID)
|
---|
1973 | out->frequencies().rescale(width, "RESAMPLE");
|
---|
1974 | TableIterator iter(tout, "IFNO");
|
---|
1975 | TableRow row(tout);
|
---|
1976 | while ( !iter.pastEnd() ) {
|
---|
1977 | Table tab = iter.table();
|
---|
1978 | ArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
1979 | //ArrayColumn<Float> tsysCol(tout, "TSYS");
|
---|
1980 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
1981 | Vector<Float> spec;
|
---|
1982 | Vector<uChar> flag;
|
---|
1983 | specCol.get(0,spec); // the number of channels should be constant per IF
|
---|
1984 | uInt nChanIn = spec.nelements();
|
---|
1985 | Vector<Float> xIn(nChanIn); indgen(xIn);
|
---|
1986 | Int fac = Int(nChanIn/width);
|
---|
1987 | Vector<Float> xOut(fac+10); // 10 to be safe - resize later
|
---|
1988 | uInt k = 0;
|
---|
1989 | Float x = 0.0;
|
---|
1990 | while (x < Float(nChanIn) ) {
|
---|
1991 | xOut(k) = x;
|
---|
1992 | k++;
|
---|
1993 | x += width;
|
---|
1994 | }
|
---|
1995 | uInt nChanOut = k;
|
---|
1996 | xOut.resize(nChanOut, True);
|
---|
1997 | // process all rows for this IFNO
|
---|
1998 | Vector<Float> specOut;
|
---|
1999 | Vector<Bool> maskOut;
|
---|
2000 | Vector<uChar> flagOut;
|
---|
2001 | for (uInt i=0; i < tab.nrow(); ++i) {
|
---|
2002 | specCol.get(i, spec);
|
---|
2003 | flagCol.get(i, flag);
|
---|
2004 | Vector<Bool> mask(flag.nelements());
|
---|
2005 | convertArray(mask, flag);
|
---|
2006 |
|
---|
2007 | IPosition shapeIn(spec.shape());
|
---|
2008 | //sh.nchan = nChanOut;
|
---|
2009 | InterpolateArray1D<Float,Float>::interpolate(specOut, maskOut, xOut,
|
---|
2010 | xIn, spec, mask,
|
---|
2011 | interpMethod, True, True);
|
---|
2012 | /// @todo do the same for channel based Tsys
|
---|
2013 | flagOut.resize(maskOut.nelements());
|
---|
2014 | convertArray(flagOut, maskOut);
|
---|
2015 | specCol.put(i, specOut);
|
---|
2016 | flagCol.put(i, flagOut);
|
---|
2017 | }
|
---|
2018 | ++iter;
|
---|
2019 | }
|
---|
2020 |
|
---|
2021 | return out;
|
---|
2022 | }
|
---|
2023 |
|
---|
2024 | STMath::imethod STMath::stringToIMethod(const std::string& in)
|
---|
2025 | {
|
---|
2026 | static STMath::imap lookup;
|
---|
2027 |
|
---|
2028 | // initialize the lookup table if necessary
|
---|
2029 | if ( lookup.empty() ) {
|
---|
2030 | lookup["nearest"] = InterpolateArray1D<Double,Float>::nearestNeighbour;
|
---|
2031 | lookup["linear"] = InterpolateArray1D<Double,Float>::linear;
|
---|
2032 | lookup["cubic"] = InterpolateArray1D<Double,Float>::cubic;
|
---|
2033 | lookup["spline"] = InterpolateArray1D<Double,Float>::spline;
|
---|
2034 | }
|
---|
2035 |
|
---|
2036 | STMath::imap::const_iterator iter = lookup.find(in);
|
---|
2037 |
|
---|
2038 | if ( lookup.end() == iter ) {
|
---|
2039 | std::string message = in;
|
---|
2040 | message += " is not a valid interpolation mode";
|
---|
2041 | throw(AipsError(message));
|
---|
2042 | }
|
---|
2043 | return iter->second;
|
---|
2044 | }
|
---|
2045 |
|
---|
2046 | WeightType STMath::stringToWeight(const std::string& in)
|
---|
2047 | {
|
---|
2048 | static std::map<std::string, WeightType> lookup;
|
---|
2049 |
|
---|
2050 | // initialize the lookup table if necessary
|
---|
2051 | if ( lookup.empty() ) {
|
---|
2052 | lookup["NONE"] = asap::W_NONE;
|
---|
2053 | lookup["TINT"] = asap::W_TINT;
|
---|
2054 | lookup["TINTSYS"] = asap::W_TINTSYS;
|
---|
2055 | lookup["TSYS"] = asap::W_TSYS;
|
---|
2056 | lookup["VAR"] = asap::W_VAR;
|
---|
2057 | }
|
---|
2058 |
|
---|
2059 | std::map<std::string, WeightType>::const_iterator iter = lookup.find(in);
|
---|
2060 |
|
---|
2061 | if ( lookup.end() == iter ) {
|
---|
2062 | std::string message = in;
|
---|
2063 | message += " is not a valid weighting mode";
|
---|
2064 | throw(AipsError(message));
|
---|
2065 | }
|
---|
2066 | return iter->second;
|
---|
2067 | }
|
---|
2068 |
|
---|
2069 | CountedPtr< Scantable > STMath::gainElevation( const CountedPtr< Scantable >& in,
|
---|
2070 | const vector< float > & coeff,
|
---|
2071 | const std::string & filename,
|
---|
2072 | const std::string& method)
|
---|
2073 | {
|
---|
2074 | // Get elevation data from Scantable and convert to degrees
|
---|
2075 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
2076 | Table& tab = out->table();
|
---|
2077 | ROScalarColumn<Float> elev(tab, "ELEVATION");
|
---|
2078 | Vector<Float> x = elev.getColumn();
|
---|
2079 | x *= Float(180 / C::pi); // Degrees
|
---|
2080 |
|
---|
2081 | Vector<Float> coeffs(coeff);
|
---|
2082 | const uInt nc = coeffs.nelements();
|
---|
2083 | if ( filename.length() > 0 && nc > 0 ) {
|
---|
2084 | throw(AipsError("You must choose either polynomial coefficients or an ascii file, not both"));
|
---|
2085 | }
|
---|
2086 |
|
---|
2087 | // Correct
|
---|
2088 | if ( nc > 0 || filename.length() == 0 ) {
|
---|
2089 | // Find instrument
|
---|
2090 | Bool throwit = True;
|
---|
2091 | Instrument inst =
|
---|
2092 | STAttr::convertInstrument(tab.keywordSet().asString("AntennaName"),
|
---|
2093 | throwit);
|
---|
2094 |
|
---|
2095 | // Set polynomial
|
---|
2096 | Polynomial<Float>* ppoly = 0;
|
---|
2097 | Vector<Float> coeff;
|
---|
2098 | String msg;
|
---|
2099 | if ( nc > 0 ) {
|
---|
2100 | ppoly = new Polynomial<Float>(nc-1);
|
---|
2101 | coeff = coeffs;
|
---|
2102 | msg = String("user");
|
---|
2103 | } else {
|
---|
2104 | STAttr sdAttr;
|
---|
2105 | coeff = sdAttr.gainElevationPoly(inst);
|
---|
2106 | ppoly = new Polynomial<Float>(coeff.nelements()-1);
|
---|
2107 | msg = String("built in");
|
---|
2108 | }
|
---|
2109 |
|
---|
2110 | if ( coeff.nelements() > 0 ) {
|
---|
2111 | ppoly->setCoefficients(coeff);
|
---|
2112 | } else {
|
---|
2113 | delete ppoly;
|
---|
2114 | throw(AipsError("There is no known gain-elevation polynomial known for this instrument"));
|
---|
2115 | }
|
---|
2116 | ostringstream oss;
|
---|
2117 | oss << "Making polynomial correction with " << msg << " coefficients:" << endl;
|
---|
2118 | oss << " " << coeff;
|
---|
2119 | pushLog(String(oss));
|
---|
2120 | const uInt nrow = tab.nrow();
|
---|
2121 | Vector<Float> factor(nrow);
|
---|
2122 | for ( uInt i=0; i < nrow; ++i ) {
|
---|
2123 | factor[i] = 1.0 / (*ppoly)(x[i]);
|
---|
2124 | }
|
---|
2125 | delete ppoly;
|
---|
2126 | scaleByVector(tab, factor, true);
|
---|
2127 |
|
---|
2128 | } else {
|
---|
2129 | // Read and correct
|
---|
2130 | pushLog("Making correction from ascii Table");
|
---|
2131 | scaleFromAsciiTable(tab, filename, method, x, true);
|
---|
2132 | }
|
---|
2133 | return out;
|
---|
2134 | }
|
---|
2135 |
|
---|
2136 | void STMath::scaleFromAsciiTable(Table& in, const std::string& filename,
|
---|
2137 | const std::string& method,
|
---|
2138 | const Vector<Float>& xout, bool dotsys)
|
---|
2139 | {
|
---|
2140 |
|
---|
2141 | // Read gain-elevation ascii file data into a Table.
|
---|
2142 |
|
---|
2143 | String formatString;
|
---|
2144 | Table tbl = readAsciiTable(formatString, Table::Memory, filename, "", "", False);
|
---|
2145 | scaleFromTable(in, tbl, method, xout, dotsys);
|
---|
2146 | }
|
---|
2147 |
|
---|
2148 | void STMath::scaleFromTable(Table& in,
|
---|
2149 | const Table& table,
|
---|
2150 | const std::string& method,
|
---|
2151 | const Vector<Float>& xout, bool dotsys)
|
---|
2152 | {
|
---|
2153 |
|
---|
2154 | ROScalarColumn<Float> geElCol(table, "ELEVATION");
|
---|
2155 | ROScalarColumn<Float> geFacCol(table, "FACTOR");
|
---|
2156 | Vector<Float> xin = geElCol.getColumn();
|
---|
2157 | Vector<Float> yin = geFacCol.getColumn();
|
---|
2158 | Vector<Bool> maskin(xin.nelements(),True);
|
---|
2159 |
|
---|
2160 | // Interpolate (and extrapolate) with desired method
|
---|
2161 |
|
---|
2162 | InterpolateArray1D<Double,Float>::InterpolationMethod interp = stringToIMethod(method);
|
---|
2163 |
|
---|
2164 | Vector<Float> yout;
|
---|
2165 | Vector<Bool> maskout;
|
---|
2166 | InterpolateArray1D<Float,Float>::interpolate(yout, maskout, xout,
|
---|
2167 | xin, yin, maskin, interp,
|
---|
2168 | True, True);
|
---|
2169 |
|
---|
2170 | scaleByVector(in, Float(1.0)/yout, dotsys);
|
---|
2171 | }
|
---|
2172 |
|
---|
2173 | void STMath::scaleByVector( Table& in,
|
---|
2174 | const Vector< Float >& factor,
|
---|
2175 | bool dotsys )
|
---|
2176 | {
|
---|
2177 | uInt nrow = in.nrow();
|
---|
2178 | if ( factor.nelements() != nrow ) {
|
---|
2179 | throw(AipsError("factors.nelements() != table.nelements()"));
|
---|
2180 | }
|
---|
2181 | ArrayColumn<Float> specCol(in, "SPECTRA");
|
---|
2182 | ArrayColumn<uChar> flagCol(in, "FLAGTRA");
|
---|
2183 | ArrayColumn<Float> tsysCol(in, "TSYS");
|
---|
2184 | for (uInt i=0; i < nrow; ++i) {
|
---|
2185 | MaskedArray<Float> ma = maskedArray(specCol(i), flagCol(i));
|
---|
2186 | ma *= factor[i];
|
---|
2187 | specCol.put(i, ma.getArray());
|
---|
2188 | flagCol.put(i, flagsFromMA(ma));
|
---|
2189 | if ( dotsys ) {
|
---|
2190 | Vector<Float> tsys = tsysCol(i);
|
---|
2191 | tsys *= factor[i];
|
---|
2192 | tsysCol.put(i,tsys);
|
---|
2193 | }
|
---|
2194 | }
|
---|
2195 | }
|
---|
2196 |
|
---|
2197 | CountedPtr< Scantable > STMath::convertFlux( const CountedPtr< Scantable >& in,
|
---|
2198 | float d, float etaap,
|
---|
2199 | float jyperk )
|
---|
2200 | {
|
---|
2201 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
2202 | Table& tab = in->table();
|
---|
2203 | Table& outtab = out->table();
|
---|
2204 | Unit fluxUnit(tab.keywordSet().asString("FluxUnit"));
|
---|
2205 | Unit K(String("K"));
|
---|
2206 | Unit JY(String("Jy"));
|
---|
2207 |
|
---|
2208 | bool tokelvin = true;
|
---|
2209 | Double cfac = 1.0;
|
---|
2210 |
|
---|
2211 | if ( fluxUnit == JY ) {
|
---|
2212 | pushLog("Converting to K");
|
---|
2213 | Quantum<Double> t(1.0,fluxUnit);
|
---|
2214 | Quantum<Double> t2 = t.get(JY);
|
---|
2215 | cfac = (t2 / t).getValue(); // value to Jy
|
---|
2216 |
|
---|
2217 | tokelvin = true;
|
---|
2218 | out->setFluxUnit("K");
|
---|
2219 | } else if ( fluxUnit == K ) {
|
---|
2220 | pushLog("Converting to Jy");
|
---|
2221 | Quantum<Double> t(1.0,fluxUnit);
|
---|
2222 | Quantum<Double> t2 = t.get(K);
|
---|
2223 | cfac = (t2 / t).getValue(); // value to K
|
---|
2224 |
|
---|
2225 | tokelvin = false;
|
---|
2226 | out->setFluxUnit("Jy");
|
---|
2227 | } else {
|
---|
2228 | throw(AipsError("Unrecognized brightness units in Table - must be consistent with Jy or K"));
|
---|
2229 | }
|
---|
2230 | // Make sure input values are converted to either Jy or K first...
|
---|
2231 | Float factor = cfac;
|
---|
2232 |
|
---|
2233 | // Select method
|
---|
2234 | if (jyperk > 0.0) {
|
---|
2235 | factor *= jyperk;
|
---|
2236 | if ( tokelvin ) factor = 1.0 / jyperk;
|
---|
2237 | ostringstream oss;
|
---|
2238 | oss << "Jy/K = " << jyperk;
|
---|
2239 | pushLog(String(oss));
|
---|
2240 | Vector<Float> factors(outtab.nrow(), factor);
|
---|
2241 | scaleByVector(outtab,factors, false);
|
---|
2242 | } else if ( etaap > 0.0) {
|
---|
2243 | if (d < 0) {
|
---|
2244 | Instrument inst =
|
---|
2245 | STAttr::convertInstrument(tab.keywordSet().asString("AntennaName"),
|
---|
2246 | True);
|
---|
2247 | STAttr sda;
|
---|
2248 | d = sda.diameter(inst);
|
---|
2249 | }
|
---|
2250 | jyperk = STAttr::findJyPerK(etaap, d);
|
---|
2251 | ostringstream oss;
|
---|
2252 | oss << "Jy/K = " << jyperk;
|
---|
2253 | pushLog(String(oss));
|
---|
2254 | factor *= jyperk;
|
---|
2255 | if ( tokelvin ) {
|
---|
2256 | factor = 1.0 / factor;
|
---|
2257 | }
|
---|
2258 | Vector<Float> factors(outtab.nrow(), factor);
|
---|
2259 | scaleByVector(outtab, factors, False);
|
---|
2260 | } else {
|
---|
2261 |
|
---|
2262 | // OK now we must deal with automatic look up of values.
|
---|
2263 | // We must also deal with the fact that the factors need
|
---|
2264 | // to be computed per IF and may be different and may
|
---|
2265 | // change per integration.
|
---|
2266 |
|
---|
2267 | pushLog("Looking up conversion factors");
|
---|
2268 | convertBrightnessUnits(out, tokelvin, cfac);
|
---|
2269 | }
|
---|
2270 |
|
---|
2271 | return out;
|
---|
2272 | }
|
---|
2273 |
|
---|
2274 | void STMath::convertBrightnessUnits( CountedPtr<Scantable>& in,
|
---|
2275 | bool tokelvin, float cfac )
|
---|
2276 | {
|
---|
2277 | Table& table = in->table();
|
---|
2278 | Instrument inst =
|
---|
2279 | STAttr::convertInstrument(table.keywordSet().asString("AntennaName"), True);
|
---|
2280 | TableIterator iter(table, "FREQ_ID");
|
---|
2281 | STFrequencies stfreqs = in->frequencies();
|
---|
2282 | STAttr sdAtt;
|
---|
2283 | while (!iter.pastEnd()) {
|
---|
2284 | Table tab = iter.table();
|
---|
2285 | ArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
2286 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
2287 | ROScalarColumn<uInt> freqidCol(tab, "FREQ_ID");
|
---|
2288 | MEpoch::ROScalarColumn timeCol(tab, "TIME");
|
---|
2289 |
|
---|
2290 | uInt freqid; freqidCol.get(0, freqid);
|
---|
2291 | Vector<Float> tmpspec; specCol.get(0, tmpspec);
|
---|
2292 | // STAttr.JyPerK has a Vector interface... change sometime.
|
---|
2293 | Vector<Float> freqs(1,stfreqs.getRefFreq(freqid, tmpspec.nelements()));
|
---|
2294 | for ( uInt i=0; i<tab.nrow(); ++i) {
|
---|
2295 | Float jyperk = (sdAtt.JyPerK(inst, timeCol(i), freqs))[0];
|
---|
2296 | Float factor = cfac * jyperk;
|
---|
2297 | if ( tokelvin ) factor = Float(1.0) / factor;
|
---|
2298 | MaskedArray<Float> ma = maskedArray(specCol(i), flagCol(i));
|
---|
2299 | ma *= factor;
|
---|
2300 | specCol.put(i, ma.getArray());
|
---|
2301 | flagCol.put(i, flagsFromMA(ma));
|
---|
2302 | }
|
---|
2303 | ++iter;
|
---|
2304 | }
|
---|
2305 | }
|
---|
2306 |
|
---|
2307 | CountedPtr< Scantable > STMath::opacity( const CountedPtr< Scantable > & in,
|
---|
2308 | const std::vector<float>& tau )
|
---|
2309 | {
|
---|
2310 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
2311 |
|
---|
2312 | Table outtab = out->table();
|
---|
2313 |
|
---|
2314 | const Int ntau = uInt(tau.size());
|
---|
2315 | std::vector<float>::const_iterator tauit = tau.begin();
|
---|
2316 | AlwaysAssert((ntau == 1 || ntau == in->nif() || ntau == in->nif() * in->npol()),
|
---|
2317 | AipsError);
|
---|
2318 | TableIterator iiter(outtab, "IFNO");
|
---|
2319 | while ( !iiter.pastEnd() ) {
|
---|
2320 | Table itab = iiter.table();
|
---|
2321 | TableIterator piter(itab, "POLNO");
|
---|
2322 | while ( !piter.pastEnd() ) {
|
---|
2323 | Table tab = piter.table();
|
---|
2324 | ROScalarColumn<Float> elev(tab, "ELEVATION");
|
---|
2325 | ArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
2326 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
2327 | ArrayColumn<Float> tsysCol(tab, "TSYS");
|
---|
2328 | for ( uInt i=0; i<tab.nrow(); ++i) {
|
---|
2329 | Float zdist = Float(C::pi_2) - elev(i);
|
---|
2330 | Float factor = exp(*tauit/cos(zdist));
|
---|
2331 | MaskedArray<Float> ma = maskedArray(specCol(i), flagCol(i));
|
---|
2332 | ma *= factor;
|
---|
2333 | specCol.put(i, ma.getArray());
|
---|
2334 | flagCol.put(i, flagsFromMA(ma));
|
---|
2335 | Vector<Float> tsys;
|
---|
2336 | tsysCol.get(i, tsys);
|
---|
2337 | tsys *= factor;
|
---|
2338 | tsysCol.put(i, tsys);
|
---|
2339 | }
|
---|
2340 | if (ntau == in->nif()*in->npol() ) {
|
---|
2341 | tauit++;
|
---|
2342 | }
|
---|
2343 | piter++;
|
---|
2344 | }
|
---|
2345 | if (ntau >= in->nif() ) {
|
---|
2346 | tauit++;
|
---|
2347 | }
|
---|
2348 | iiter++;
|
---|
2349 | }
|
---|
2350 | return out;
|
---|
2351 | }
|
---|
2352 |
|
---|
2353 | CountedPtr< Scantable > STMath::smoothOther( const CountedPtr< Scantable >& in,
|
---|
2354 | const std::string& kernel,
|
---|
2355 | float width, int order)
|
---|
2356 | {
|
---|
2357 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
2358 | Table table = out->table();
|
---|
2359 |
|
---|
2360 | TableIterator iter(table, "IFNO");
|
---|
2361 | while (!iter.pastEnd()) {
|
---|
2362 | Table tab = iter.table();
|
---|
2363 | ArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
2364 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
2365 | Vector<Float> spec;
|
---|
2366 | Vector<uChar> flag;
|
---|
2367 | for (uInt i = 0; i < tab.nrow(); ++i) {
|
---|
2368 | specCol.get(i, spec);
|
---|
2369 | flagCol.get(i, flag);
|
---|
2370 | Vector<Bool> mask(flag.nelements());
|
---|
2371 | convertArray(mask, flag);
|
---|
2372 | Vector<Float> specout;
|
---|
2373 | Vector<Bool> maskout;
|
---|
2374 | if (kernel == "hanning") {
|
---|
2375 | mathutil::hanning(specout, maskout, spec, !mask);
|
---|
2376 | convertArray(flag, !maskout);
|
---|
2377 | } else if (kernel == "rmedian") {
|
---|
2378 | mathutil::runningMedian(specout, maskout, spec , mask, width);
|
---|
2379 | convertArray(flag, maskout);
|
---|
2380 | } else if (kernel == "poly") {
|
---|
2381 | mathutil::polyfit(specout, maskout, spec, !mask, width, order);
|
---|
2382 | convertArray(flag, !maskout);
|
---|
2383 | }
|
---|
2384 |
|
---|
2385 | for (uInt j = 0; j < flag.nelements(); ++j) {
|
---|
2386 | uChar userFlag = 1 << 7;
|
---|
2387 | if (maskout[j]==True) userFlag = 0 << 7;
|
---|
2388 | flag(j) = userFlag;
|
---|
2389 | }
|
---|
2390 |
|
---|
2391 | flagCol.put(i, flag);
|
---|
2392 | specCol.put(i, specout);
|
---|
2393 | }
|
---|
2394 | ++iter;
|
---|
2395 | }
|
---|
2396 | return out;
|
---|
2397 | }
|
---|
2398 |
|
---|
2399 | CountedPtr< Scantable > STMath::smooth( const CountedPtr< Scantable >& in,
|
---|
2400 | const std::string& kernel, float width,
|
---|
2401 | int order)
|
---|
2402 | {
|
---|
2403 | if (kernel == "rmedian" || kernel == "hanning" || kernel == "poly") {
|
---|
2404 | return smoothOther(in, kernel, width, order);
|
---|
2405 | }
|
---|
2406 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
2407 | Table& table = out->table();
|
---|
2408 | VectorKernel::KernelTypes type = VectorKernel::toKernelType(kernel);
|
---|
2409 | // same IFNO should have same no of channels
|
---|
2410 | // this saves overhead
|
---|
2411 | TableIterator iter(table, "IFNO");
|
---|
2412 | while (!iter.pastEnd()) {
|
---|
2413 | Table tab = iter.table();
|
---|
2414 | ArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
2415 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
2416 | Vector<Float> tmpspec; specCol.get(0, tmpspec);
|
---|
2417 | uInt nchan = tmpspec.nelements();
|
---|
2418 | Vector<Float> kvec = VectorKernel::make(type, width, nchan, True, False);
|
---|
2419 | Convolver<Float> conv(kvec, IPosition(1,nchan));
|
---|
2420 | Vector<Float> spec;
|
---|
2421 | Vector<uChar> flag;
|
---|
2422 | for ( uInt i=0; i<tab.nrow(); ++i) {
|
---|
2423 | specCol.get(i, spec);
|
---|
2424 | flagCol.get(i, flag);
|
---|
2425 | Vector<Bool> mask(flag.nelements());
|
---|
2426 | convertArray(mask, flag);
|
---|
2427 | Vector<Float> specout;
|
---|
2428 | mathutil::replaceMaskByZero(specout, mask);
|
---|
2429 | conv.linearConv(specout, spec);
|
---|
2430 | specCol.put(i, specout);
|
---|
2431 | }
|
---|
2432 | ++iter;
|
---|
2433 | }
|
---|
2434 | return out;
|
---|
2435 | }
|
---|
2436 |
|
---|
2437 | CountedPtr< Scantable >
|
---|
2438 | STMath::merge( const std::vector< CountedPtr < Scantable > >& in )
|
---|
2439 | {
|
---|
2440 | if ( in.size() < 2 ) {
|
---|
2441 | throw(AipsError("Need at least two scantables to perform a merge."));
|
---|
2442 | }
|
---|
2443 | std::vector<CountedPtr < Scantable > >::const_iterator it = in.begin();
|
---|
2444 | bool insitu = insitu_;
|
---|
2445 | setInsitu(false);
|
---|
2446 | CountedPtr< Scantable > out = getScantable(*it, false);
|
---|
2447 | setInsitu(insitu);
|
---|
2448 | Table& tout = out->table();
|
---|
2449 | ScalarColumn<uInt> freqidcol(tout,"FREQ_ID"), molidcol(tout, "MOLECULE_ID");
|
---|
2450 | ScalarColumn<uInt> scannocol(tout,"SCANNO"), focusidcol(tout,"FOCUS_ID");
|
---|
2451 | // Renumber SCANNO to be 0-based
|
---|
2452 | Vector<uInt> scannos = scannocol.getColumn();
|
---|
2453 | uInt offset = min(scannos);
|
---|
2454 | scannos -= offset;
|
---|
2455 | scannocol.putColumn(scannos);
|
---|
2456 | uInt newscanno = max(scannos)+1;
|
---|
2457 | ++it;
|
---|
2458 | while ( it != in.end() ){
|
---|
2459 | if ( ! (*it)->conformant(*out) ) {
|
---|
2460 | // non conformant.
|
---|
2461 | //pushLog(String("Warning: Can't merge scantables as header info differs."));
|
---|
2462 | LogIO os( LogOrigin( "STMath", "merge()", WHERE ) ) ;
|
---|
2463 | os << LogIO::SEVERE << "Can't merge scantables as header informations (any one of AntennaName, Equinox, and FluxUnit) differ." << LogIO::EXCEPTION ;
|
---|
2464 | }
|
---|
2465 | out->appendToHistoryTable((*it)->history());
|
---|
2466 | const Table& tab = (*it)->table();
|
---|
2467 |
|
---|
2468 | Block<String> cols(3);
|
---|
2469 | cols[0] = String("FREQ_ID");
|
---|
2470 | cols[1] = String("MOLECULE_ID");
|
---|
2471 | cols[2] = String("FOCUS_ID");
|
---|
2472 |
|
---|
2473 | TableIterator scanit(tab, "SCANNO");
|
---|
2474 | while (!scanit.pastEnd()) {
|
---|
2475 | ScalarColumn<uInt> thescannocol(scanit.table(),"SCANNO");
|
---|
2476 | Vector<uInt> thescannos(thescannocol.nrow(),newscanno);
|
---|
2477 | thescannocol.putColumn(thescannos);
|
---|
2478 | TableIterator subit(scanit.table(), cols);
|
---|
2479 | while ( !subit.pastEnd() ) {
|
---|
2480 | uInt nrow = tout.nrow();
|
---|
2481 | Table thetab = subit.table();
|
---|
2482 | ROTableRow row(thetab);
|
---|
2483 | Vector<uInt> thecolvals(thetab.nrow());
|
---|
2484 | ScalarColumn<uInt> thefreqidcol(thetab,"FREQ_ID");
|
---|
2485 | ScalarColumn<uInt> themolidcol(thetab, "MOLECULE_ID");
|
---|
2486 | ScalarColumn<uInt> thefocusidcol(thetab,"FOCUS_ID");
|
---|
2487 | // The selected subset of table should have
|
---|
2488 | // the equal FREQ_ID, MOLECULE_ID, and FOCUS_ID values.
|
---|
2489 | const TableRecord& rec = row.get(0);
|
---|
2490 | // Set the proper FREQ_ID
|
---|
2491 | Double rv,rp,inc;
|
---|
2492 | (*it)->frequencies().getEntry(rp, rv, inc, rec.asuInt("FREQ_ID"));
|
---|
2493 | uInt id;
|
---|
2494 | id = out->frequencies().addEntry(rp, rv, inc);
|
---|
2495 | thecolvals = id;
|
---|
2496 | thefreqidcol.putColumn(thecolvals);
|
---|
2497 | // Set the proper MOLECULE_ID
|
---|
2498 | Vector<String> name,fname;Vector<Double> rf;
|
---|
2499 | (*it)->molecules().getEntry(rf, name, fname, rec.asuInt("MOLECULE_ID"));
|
---|
2500 | id = out->molecules().addEntry(rf, name, fname);
|
---|
2501 | thecolvals = id;
|
---|
2502 | themolidcol.putColumn(thecolvals);
|
---|
2503 | // Set the proper FOCUS_ID
|
---|
2504 | Float fpa,frot,fax,ftan,fhand,fmount,fuser, fxy, fxyp;
|
---|
2505 | (*it)->focus().getEntry(fpa, fax, ftan, frot, fhand, fmount,fuser,
|
---|
2506 | fxy, fxyp, rec.asuInt("FOCUS_ID"));
|
---|
2507 | id = out->focus().addEntry(fpa, fax, ftan, frot, fhand, fmount,fuser,
|
---|
2508 | fxy, fxyp);
|
---|
2509 | thecolvals = id;
|
---|
2510 | thefocusidcol.putColumn(thecolvals);
|
---|
2511 |
|
---|
2512 | tout.addRow(thetab.nrow());
|
---|
2513 | TableCopy::copyRows(tout, thetab, nrow, 0, thetab.nrow());
|
---|
2514 |
|
---|
2515 | ++subit;
|
---|
2516 | }
|
---|
2517 | ++newscanno;
|
---|
2518 | ++scanit;
|
---|
2519 | }
|
---|
2520 | ++it;
|
---|
2521 | }
|
---|
2522 | return out;
|
---|
2523 | }
|
---|
2524 |
|
---|
2525 | CountedPtr< Scantable >
|
---|
2526 | STMath::invertPhase( const CountedPtr < Scantable >& in )
|
---|
2527 | {
|
---|
2528 | return applyToPol(in, &STPol::invertPhase, Float(0.0));
|
---|
2529 | }
|
---|
2530 |
|
---|
2531 | CountedPtr< Scantable >
|
---|
2532 | STMath::rotateXYPhase( const CountedPtr < Scantable >& in, float phase )
|
---|
2533 | {
|
---|
2534 | return applyToPol(in, &STPol::rotatePhase, Float(phase));
|
---|
2535 | }
|
---|
2536 |
|
---|
2537 | CountedPtr< Scantable >
|
---|
2538 | STMath::rotateLinPolPhase( const CountedPtr < Scantable >& in, float phase )
|
---|
2539 | {
|
---|
2540 | return applyToPol(in, &STPol::rotateLinPolPhase, Float(phase));
|
---|
2541 | }
|
---|
2542 |
|
---|
2543 | CountedPtr< Scantable > STMath::applyToPol( const CountedPtr<Scantable>& in,
|
---|
2544 | STPol::polOperation fptr,
|
---|
2545 | Float phase )
|
---|
2546 | {
|
---|
2547 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
2548 | Table& tout = out->table();
|
---|
2549 | Block<String> cols(4);
|
---|
2550 | cols[0] = String("SCANNO");
|
---|
2551 | cols[1] = String("BEAMNO");
|
---|
2552 | cols[2] = String("IFNO");
|
---|
2553 | cols[3] = String("CYCLENO");
|
---|
2554 | TableIterator iter(tout, cols);
|
---|
2555 | CountedPtr<STPol> stpol = STPol::getPolClass(out->factories_,
|
---|
2556 | out->getPolType() );
|
---|
2557 | while (!iter.pastEnd()) {
|
---|
2558 | Table t = iter.table();
|
---|
2559 | ArrayColumn<Float> speccol(t, "SPECTRA");
|
---|
2560 | ScalarColumn<uInt> focidcol(t, "FOCUS_ID");
|
---|
2561 | Matrix<Float> pols(speccol.getColumn());
|
---|
2562 | try {
|
---|
2563 | stpol->setSpectra(pols);
|
---|
2564 | Float fang,fhand;
|
---|
2565 | fang = in->focusTable_.getTotalAngle(focidcol(0));
|
---|
2566 | fhand = in->focusTable_.getFeedHand(focidcol(0));
|
---|
2567 | stpol->setPhaseCorrections(fang, fhand);
|
---|
2568 | // use a member function pointer in STPol. This only works on
|
---|
2569 | // the STPol pointer itself, not the Counted Pointer so
|
---|
2570 | // derefernce it.
|
---|
2571 | (&(*(stpol))->*fptr)(phase);
|
---|
2572 | speccol.putColumn(stpol->getSpectra());
|
---|
2573 | } catch (AipsError& e) {
|
---|
2574 | //delete stpol;stpol=0;
|
---|
2575 | throw(e);
|
---|
2576 | }
|
---|
2577 | ++iter;
|
---|
2578 | }
|
---|
2579 | //delete stpol;stpol=0;
|
---|
2580 | return out;
|
---|
2581 | }
|
---|
2582 |
|
---|
2583 | CountedPtr< Scantable >
|
---|
2584 | STMath::swapPolarisations( const CountedPtr< Scantable > & in )
|
---|
2585 | {
|
---|
2586 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
2587 | Table& tout = out->table();
|
---|
2588 | Table t0 = tout(tout.col("POLNO") == 0);
|
---|
2589 | Table t1 = tout(tout.col("POLNO") == 1);
|
---|
2590 | if ( t0.nrow() != t1.nrow() )
|
---|
2591 | throw(AipsError("Inconsistent number of polarisations"));
|
---|
2592 | ArrayColumn<Float> speccol0(t0, "SPECTRA");
|
---|
2593 | ArrayColumn<uChar> flagcol0(t0, "FLAGTRA");
|
---|
2594 | ArrayColumn<Float> speccol1(t1, "SPECTRA");
|
---|
2595 | ArrayColumn<uChar> flagcol1(t1, "FLAGTRA");
|
---|
2596 | Matrix<Float> s0 = speccol0.getColumn();
|
---|
2597 | Matrix<uChar> f0 = flagcol0.getColumn();
|
---|
2598 | speccol0.putColumn(speccol1.getColumn());
|
---|
2599 | flagcol0.putColumn(flagcol1.getColumn());
|
---|
2600 | speccol1.putColumn(s0);
|
---|
2601 | flagcol1.putColumn(f0);
|
---|
2602 | return out;
|
---|
2603 | }
|
---|
2604 |
|
---|
2605 | CountedPtr< Scantable >
|
---|
2606 | STMath::averagePolarisations( const CountedPtr< Scantable > & in,
|
---|
2607 | const std::vector<bool>& mask,
|
---|
2608 | const std::string& weight )
|
---|
2609 | {
|
---|
2610 | if (in->npol() < 2 )
|
---|
2611 | throw(AipsError("averagePolarisations can only be applied to two or more"
|
---|
2612 | "polarisations"));
|
---|
2613 | bool insitu = insitu_;
|
---|
2614 | setInsitu(false);
|
---|
2615 | CountedPtr< Scantable > pols = getScantable(in, true);
|
---|
2616 | setInsitu(insitu);
|
---|
2617 | Table& tout = pols->table();
|
---|
2618 | std::string taql = "SELECT FROM $1 WHERE POLNO IN [0,1]";
|
---|
2619 | Table tab = tableCommand(taql, in->table());
|
---|
2620 | if (tab.nrow() == 0 )
|
---|
2621 | throw(AipsError("Could not find any rows with POLNO==0 and POLNO==1"));
|
---|
2622 | TableCopy::copyRows(tout, tab);
|
---|
2623 | TableVector<uInt> vec(tout, "POLNO");
|
---|
2624 | vec = 0;
|
---|
2625 | pols->table_.rwKeywordSet().define("nPol", Int(1));
|
---|
2626 | pols->table_.rwKeywordSet().define("POLTYPE", String("stokes"));
|
---|
2627 | //pols->table_.rwKeywordSet().define("POLTYPE", in->getPolType());
|
---|
2628 | std::vector<CountedPtr<Scantable> > vpols;
|
---|
2629 | vpols.push_back(pols);
|
---|
2630 | CountedPtr< Scantable > out = average(vpols, mask, weight, "SCAN");
|
---|
2631 | return out;
|
---|
2632 | }
|
---|
2633 |
|
---|
2634 | CountedPtr< Scantable >
|
---|
2635 | STMath::averageBeams( const CountedPtr< Scantable > & in,
|
---|
2636 | const std::vector<bool>& mask,
|
---|
2637 | const std::string& weight )
|
---|
2638 | {
|
---|
2639 | bool insitu = insitu_;
|
---|
2640 | setInsitu(false);
|
---|
2641 | CountedPtr< Scantable > beams = getScantable(in, false);
|
---|
2642 | setInsitu(insitu);
|
---|
2643 | Table& tout = beams->table();
|
---|
2644 | // give all rows the same BEAMNO
|
---|
2645 | TableVector<uInt> vec(tout, "BEAMNO");
|
---|
2646 | vec = 0;
|
---|
2647 | beams->table_.rwKeywordSet().define("nBeam", Int(1));
|
---|
2648 | std::vector<CountedPtr<Scantable> > vbeams;
|
---|
2649 | vbeams.push_back(beams);
|
---|
2650 | CountedPtr< Scantable > out = average(vbeams, mask, weight, "SCAN");
|
---|
2651 | return out;
|
---|
2652 | }
|
---|
2653 |
|
---|
2654 |
|
---|
2655 | CountedPtr< Scantable >
|
---|
2656 | asap::STMath::frequencyAlign( const CountedPtr< Scantable > & in,
|
---|
2657 | const std::string & refTime,
|
---|
2658 | const std::string & method)
|
---|
2659 | {
|
---|
2660 | // clone as this is not working insitu
|
---|
2661 | bool insitu = insitu_;
|
---|
2662 | setInsitu(false);
|
---|
2663 | CountedPtr< Scantable > out = getScantable(in, false);
|
---|
2664 | setInsitu(insitu);
|
---|
2665 | Table& tout = out->table();
|
---|
2666 | // Get reference Epoch to time of first row or given String
|
---|
2667 | Unit DAY(String("d"));
|
---|
2668 | MEpoch::Ref epochRef(in->getTimeReference());
|
---|
2669 | MEpoch refEpoch;
|
---|
2670 | if (refTime.length()>0) {
|
---|
2671 | Quantum<Double> qt;
|
---|
2672 | if (MVTime::read(qt,refTime)) {
|
---|
2673 | MVEpoch mv(qt);
|
---|
2674 | refEpoch = MEpoch(mv, epochRef);
|
---|
2675 | } else {
|
---|
2676 | throw(AipsError("Invalid format for Epoch string"));
|
---|
2677 | }
|
---|
2678 | } else {
|
---|
2679 | refEpoch = in->timeCol_(0);
|
---|
2680 | }
|
---|
2681 | MPosition refPos = in->getAntennaPosition();
|
---|
2682 |
|
---|
2683 | InterpolateArray1D<Double,Float>::InterpolationMethod interp = stringToIMethod(method);
|
---|
2684 | /*
|
---|
2685 | // Comment from MV.
|
---|
2686 | // the following code has been commented out because different FREQ_IDs have to be aligned together even
|
---|
2687 | // if the frame doesn't change. So far, lack of this check didn't cause any problems.
|
---|
2688 | // test if user frame is different to base frame
|
---|
2689 | if ( in->frequencies().getFrameString(true)
|
---|
2690 | == in->frequencies().getFrameString(false) ) {
|
---|
2691 | throw(AipsError("Can't convert as no output frame has been set"
|
---|
2692 | " (use set_freqframe) or it is aligned already."));
|
---|
2693 | }
|
---|
2694 | */
|
---|
2695 | MFrequency::Types system = in->frequencies().getFrame();
|
---|
2696 | MVTime mvt(refEpoch.getValue());
|
---|
2697 | String epochout = mvt.string(MVTime::YMD) + String(" (") + refEpoch.getRefString() + String(")");
|
---|
2698 | ostringstream oss;
|
---|
2699 | oss << "Aligned at reference Epoch " << epochout
|
---|
2700 | << " in frame " << MFrequency::showType(system);
|
---|
2701 | pushLog(String(oss));
|
---|
2702 | // set up the iterator
|
---|
2703 | Block<String> cols(4);
|
---|
2704 | // select by constant direction
|
---|
2705 | cols[0] = String("SRCNAME");
|
---|
2706 | cols[1] = String("BEAMNO");
|
---|
2707 | // select by IF ( no of channels varies over this )
|
---|
2708 | cols[2] = String("IFNO");
|
---|
2709 | // select by restfrequency
|
---|
2710 | cols[3] = String("MOLECULE_ID");
|
---|
2711 | TableIterator iter(tout, cols);
|
---|
2712 | while ( !iter.pastEnd() ) {
|
---|
2713 | Table t = iter.table();
|
---|
2714 | MDirection::ROScalarColumn dirCol(t, "DIRECTION");
|
---|
2715 | TableIterator fiter(t, "FREQ_ID");
|
---|
2716 | // determine nchan from the first row. This should work as
|
---|
2717 | // we are iterating over BEAMNO and IFNO // we should have constant direction
|
---|
2718 |
|
---|
2719 | ROArrayColumn<Float> sCol(t, "SPECTRA");
|
---|
2720 | const MDirection direction = dirCol(0);
|
---|
2721 | const uInt nchan = sCol(0).nelements();
|
---|
2722 |
|
---|
2723 | // skip operations if there is nothing to align
|
---|
2724 | if (fiter.pastEnd()) {
|
---|
2725 | continue;
|
---|
2726 | }
|
---|
2727 |
|
---|
2728 | Table ftab = fiter.table();
|
---|
2729 | // align all frequency ids with respect to the first encountered id
|
---|
2730 | ScalarColumn<uInt> freqidCol(ftab, "FREQ_ID");
|
---|
2731 | // get the SpectralCoordinate for the freqid, which we are iterating over
|
---|
2732 | SpectralCoordinate sC = in->frequencies().getSpectralCoordinate(freqidCol(0));
|
---|
2733 | FrequencyAligner<Float> fa( sC, nchan, refEpoch,
|
---|
2734 | direction, refPos, system );
|
---|
2735 | // realign the SpectralCoordinate and put into the output Scantable
|
---|
2736 | Vector<String> units(1);
|
---|
2737 | units = String("Hz");
|
---|
2738 | Bool linear=True;
|
---|
2739 | SpectralCoordinate sc2 = fa.alignedSpectralCoordinate(linear);
|
---|
2740 | sc2.setWorldAxisUnits(units);
|
---|
2741 | const uInt id = out->frequencies().addEntry(sc2.referencePixel()[0],
|
---|
2742 | sc2.referenceValue()[0],
|
---|
2743 | sc2.increment()[0]);
|
---|
2744 | while ( !fiter.pastEnd() ) {
|
---|
2745 | ftab = fiter.table();
|
---|
2746 | // spectral coordinate for the current FREQ_ID
|
---|
2747 | ScalarColumn<uInt> freqidCol2(ftab, "FREQ_ID");
|
---|
2748 | sC = in->frequencies().getSpectralCoordinate(freqidCol2(0));
|
---|
2749 | // create the "global" abcissa for alignment with same FREQ_ID
|
---|
2750 | Vector<Double> abc(nchan);
|
---|
2751 | for (uInt i=0; i<nchan; i++) {
|
---|
2752 | Double w;
|
---|
2753 | sC.toWorld(w,Double(i));
|
---|
2754 | abc[i] = w;
|
---|
2755 | }
|
---|
2756 | TableVector<uInt> tvec(ftab, "FREQ_ID");
|
---|
2757 | // assign new frequency id to all rows
|
---|
2758 | tvec = id;
|
---|
2759 | // cache abcissa for same time stamps, so iterate over those
|
---|
2760 | TableIterator timeiter(ftab, "TIME");
|
---|
2761 | while ( !timeiter.pastEnd() ) {
|
---|
2762 | Table tab = timeiter.table();
|
---|
2763 | ArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
2764 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
2765 | MEpoch::ROScalarColumn timeCol(tab, "TIME");
|
---|
2766 | // use align abcissa cache after the first row
|
---|
2767 | // these rows should be just be POLNO
|
---|
2768 | bool first = true;
|
---|
2769 | for (int i=0; i<int(tab.nrow()); ++i) {
|
---|
2770 | // input values
|
---|
2771 | Vector<uChar> flag = flagCol(i);
|
---|
2772 | Vector<Bool> mask(flag.shape());
|
---|
2773 | Vector<Float> specOut, spec;
|
---|
2774 | spec = specCol(i);
|
---|
2775 | Vector<Bool> maskOut;Vector<uChar> flagOut;
|
---|
2776 | convertArray(mask, flag);
|
---|
2777 | // alignment
|
---|
2778 | Bool ok = fa.align(specOut, maskOut, abc, spec,
|
---|
2779 | mask, timeCol(i), !first,
|
---|
2780 | interp, False);
|
---|
2781 | (void) ok; // unused stop compiler nagging
|
---|
2782 | // back into scantable
|
---|
2783 | flagOut.resize(maskOut.nelements());
|
---|
2784 | convertArray(flagOut, maskOut);
|
---|
2785 | flagCol.put(i, flagOut);
|
---|
2786 | specCol.put(i, specOut);
|
---|
2787 | // start abcissa caching
|
---|
2788 | first = false;
|
---|
2789 | }
|
---|
2790 | // next timestamp
|
---|
2791 | ++timeiter;
|
---|
2792 | }
|
---|
2793 | // next FREQ_ID
|
---|
2794 | ++fiter;
|
---|
2795 | }
|
---|
2796 | // next aligner
|
---|
2797 | ++iter;
|
---|
2798 | }
|
---|
2799 | // set this afterwards to ensure we are doing insitu correctly.
|
---|
2800 | out->frequencies().setFrame(system, true);
|
---|
2801 | return out;
|
---|
2802 | }
|
---|
2803 |
|
---|
2804 | CountedPtr<Scantable>
|
---|
2805 | asap::STMath::convertPolarisation( const CountedPtr<Scantable>& in,
|
---|
2806 | const std::string & newtype )
|
---|
2807 | {
|
---|
2808 | if (in->npol() != 2 && in->npol() != 4)
|
---|
2809 | throw(AipsError("Can only convert two or four polarisations."));
|
---|
2810 | if ( in->getPolType() == newtype )
|
---|
2811 | throw(AipsError("No need to convert."));
|
---|
2812 | if ( ! in->selector_.empty() )
|
---|
2813 | throw(AipsError("Can only convert whole scantable. Unset the selection."));
|
---|
2814 | bool insitu = insitu_;
|
---|
2815 | setInsitu(false);
|
---|
2816 | CountedPtr< Scantable > out = getScantable(in, true);
|
---|
2817 | setInsitu(insitu);
|
---|
2818 | Table& tout = out->table();
|
---|
2819 | tout.rwKeywordSet().define("POLTYPE", String(newtype));
|
---|
2820 |
|
---|
2821 | Block<String> cols(4);
|
---|
2822 | cols[0] = "SCANNO";
|
---|
2823 | cols[1] = "CYCLENO";
|
---|
2824 | cols[2] = "BEAMNO";
|
---|
2825 | cols[3] = "IFNO";
|
---|
2826 | TableIterator it(in->originalTable_, cols);
|
---|
2827 | String basetype = in->getPolType();
|
---|
2828 | STPol* stpol = STPol::getPolClass(in->factories_, basetype);
|
---|
2829 | try {
|
---|
2830 | while ( !it.pastEnd() ) {
|
---|
2831 | Table tab = it.table();
|
---|
2832 | uInt row = tab.rowNumbers()[0];
|
---|
2833 | stpol->setSpectra(in->getPolMatrix(row));
|
---|
2834 | Float fang,fhand;
|
---|
2835 | fang = in->focusTable_.getTotalAngle(in->mfocusidCol_(row));
|
---|
2836 | fhand = in->focusTable_.getFeedHand(in->mfocusidCol_(row));
|
---|
2837 | stpol->setPhaseCorrections(fang, fhand);
|
---|
2838 | Int npolout = 0;
|
---|
2839 | for (uInt i=0; i<tab.nrow(); ++i) {
|
---|
2840 | Vector<Float> outvec = stpol->getSpectrum(i, newtype);
|
---|
2841 | if ( outvec.nelements() > 0 ) {
|
---|
2842 | tout.addRow();
|
---|
2843 | TableCopy::copyRows(tout, tab, tout.nrow()-1, 0, 1);
|
---|
2844 | ArrayColumn<Float> sCol(tout,"SPECTRA");
|
---|
2845 | ScalarColumn<uInt> pCol(tout,"POLNO");
|
---|
2846 | sCol.put(tout.nrow()-1 ,outvec);
|
---|
2847 | pCol.put(tout.nrow()-1 ,uInt(npolout));
|
---|
2848 | npolout++;
|
---|
2849 | }
|
---|
2850 | }
|
---|
2851 | tout.rwKeywordSet().define("nPol", npolout);
|
---|
2852 | ++it;
|
---|
2853 | }
|
---|
2854 | } catch (AipsError& e) {
|
---|
2855 | delete stpol;
|
---|
2856 | throw(e);
|
---|
2857 | }
|
---|
2858 | delete stpol;
|
---|
2859 | return out;
|
---|
2860 | }
|
---|
2861 |
|
---|
2862 | CountedPtr< Scantable >
|
---|
2863 | asap::STMath::mxExtract( const CountedPtr< Scantable > & in,
|
---|
2864 | const std::string & scantype )
|
---|
2865 | {
|
---|
2866 | bool insitu = insitu_;
|
---|
2867 | setInsitu(false);
|
---|
2868 | CountedPtr< Scantable > out = getScantable(in, true);
|
---|
2869 | setInsitu(insitu);
|
---|
2870 | Table& tout = out->table();
|
---|
2871 | std::string taql = "SELECT FROM $1 WHERE BEAMNO != REFBEAMNO";
|
---|
2872 | if (scantype == "on") {
|
---|
2873 | taql = "SELECT FROM $1 WHERE BEAMNO == REFBEAMNO";
|
---|
2874 | }
|
---|
2875 | Table tab = tableCommand(taql, in->table());
|
---|
2876 | TableCopy::copyRows(tout, tab);
|
---|
2877 | if (scantype == "on") {
|
---|
2878 | // re-index SCANNO to 0
|
---|
2879 | TableVector<uInt> vec(tout, "SCANNO");
|
---|
2880 | vec = 0;
|
---|
2881 | }
|
---|
2882 | return out;
|
---|
2883 | }
|
---|
2884 |
|
---|
2885 | std::vector<float>
|
---|
2886 | asap::STMath::fft( const casa::CountedPtr< Scantable > & in,
|
---|
2887 | const std::vector<int>& whichrow,
|
---|
2888 | bool getRealImag )
|
---|
2889 | {
|
---|
2890 | std::vector<float> res;
|
---|
2891 | Table tab = in->table();
|
---|
2892 | std::vector<bool> mask;
|
---|
2893 |
|
---|
2894 | if (whichrow.size() < 1) { // for all rows (by default)
|
---|
2895 | int nrow = int(tab.nrow());
|
---|
2896 | for (int i = 0; i < nrow; ++i) {
|
---|
2897 | res = in->execFFT(i, mask, getRealImag);
|
---|
2898 | }
|
---|
2899 | } else { // for specified rows
|
---|
2900 | for (uInt i = 0; i < whichrow.size(); ++i) {
|
---|
2901 | res = in->execFFT(i, mask, getRealImag);
|
---|
2902 | }
|
---|
2903 | }
|
---|
2904 |
|
---|
2905 | return res;
|
---|
2906 | }
|
---|
2907 |
|
---|
2908 |
|
---|
2909 | CountedPtr<Scantable>
|
---|
2910 | asap::STMath::lagFlag( const CountedPtr<Scantable>& in,
|
---|
2911 | double start, double end,
|
---|
2912 | const std::string& mode )
|
---|
2913 | {
|
---|
2914 | CountedPtr<Scantable> out = getScantable(in, false);
|
---|
2915 | Table& tout = out->table();
|
---|
2916 | TableIterator iter(tout, "FREQ_ID");
|
---|
2917 | FFTServer<Float,Complex> ffts;
|
---|
2918 |
|
---|
2919 | while ( !iter.pastEnd() ) {
|
---|
2920 | Table tab = iter.table();
|
---|
2921 | Double rp,rv,inc;
|
---|
2922 | ROTableRow row(tab);
|
---|
2923 | const TableRecord& rec = row.get(0);
|
---|
2924 | uInt freqid = rec.asuInt("FREQ_ID");
|
---|
2925 | out->frequencies().getEntry(rp, rv, inc, freqid);
|
---|
2926 | ArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
2927 | ArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
2928 |
|
---|
2929 | for (int i=0; i<int(tab.nrow()); ++i) {
|
---|
2930 | Vector<Float> spec = specCol(i);
|
---|
2931 | Vector<uChar> flag = flagCol(i);
|
---|
2932 | std::vector<bool> mask;
|
---|
2933 | for (uInt j = 0; j < flag.nelements(); ++j) {
|
---|
2934 | mask.push_back(!(flag[j]>0));
|
---|
2935 | }
|
---|
2936 | mathutil::doZeroOrderInterpolation(spec, mask);
|
---|
2937 |
|
---|
2938 | Vector<Complex> lags;
|
---|
2939 | ffts.fft0(lags, spec);
|
---|
2940 |
|
---|
2941 | Int lag0(start+0.5);
|
---|
2942 | Int lag1(end+0.5);
|
---|
2943 | if (mode == "frequency") {
|
---|
2944 | lag0 = Int(spec.nelements()*abs(inc)/(start)+0.5);
|
---|
2945 | lag1 = Int(spec.nelements()*abs(inc)/(end)+0.5);
|
---|
2946 | }
|
---|
2947 | Int lstart = max(0, lag0);
|
---|
2948 | Int lend = min(Int(lags.nelements()-1), lag1);
|
---|
2949 | if (lstart == lend) {
|
---|
2950 | lags[lstart] = Complex(0.0);
|
---|
2951 | } else {
|
---|
2952 | if (lstart > lend) {
|
---|
2953 | Int tmp = lend;
|
---|
2954 | lend = lstart;
|
---|
2955 | lstart = tmp;
|
---|
2956 | }
|
---|
2957 | for (int j=lstart; j <=lend ;++j) {
|
---|
2958 | lags[j] = Complex(0.0);
|
---|
2959 | }
|
---|
2960 | }
|
---|
2961 |
|
---|
2962 | ffts.fft0(spec, lags);
|
---|
2963 |
|
---|
2964 | specCol.put(i, spec);
|
---|
2965 | }
|
---|
2966 | ++iter;
|
---|
2967 | }
|
---|
2968 | return out;
|
---|
2969 | }
|
---|
2970 |
|
---|
2971 | // Averaging spectra with different channel/resolution
|
---|
2972 | CountedPtr<Scantable>
|
---|
2973 | STMath::new_average( const std::vector<CountedPtr<Scantable> >& in,
|
---|
2974 | const bool& compel,
|
---|
2975 | const std::vector<bool>& mask,
|
---|
2976 | const std::string& weight,
|
---|
2977 | const std::string& avmode )
|
---|
2978 | throw ( casa::AipsError )
|
---|
2979 | {
|
---|
2980 | LogIO os( LogOrigin( "STMath", "new_average()", WHERE ) ) ;
|
---|
2981 | if ( avmode == "SCAN" && in.size() != 1 )
|
---|
2982 | throw(AipsError("Can't perform 'SCAN' averaging on multiple tables.\n"
|
---|
2983 | "Use merge first."));
|
---|
2984 |
|
---|
2985 | // 2012/02/17 TN
|
---|
2986 | // Since STGrid is implemented, average doesn't consider direction
|
---|
2987 | // when accumulating
|
---|
2988 | // check if OTF observation
|
---|
2989 | // String obstype = in[0]->getHeader().obstype ;
|
---|
2990 | // Double tol = 0.0 ;
|
---|
2991 | // if ( obstype.find( "OTF" ) != String::npos ) {
|
---|
2992 | // tol = TOL_OTF ;
|
---|
2993 | // }
|
---|
2994 | // else {
|
---|
2995 | // tol = TOL_POINT ;
|
---|
2996 | // }
|
---|
2997 |
|
---|
2998 | CountedPtr<Scantable> out ; // processed result
|
---|
2999 | if ( compel ) {
|
---|
3000 | std::vector< CountedPtr<Scantable> > newin ; // input for average process
|
---|
3001 | uInt insize = in.size() ; // number of input scantables
|
---|
3002 |
|
---|
3003 | // TEST: do normal average in each table before IF grouping
|
---|
3004 | os << "Do preliminary averaging" << LogIO::POST ;
|
---|
3005 | vector< CountedPtr<Scantable> > tmpin( insize ) ;
|
---|
3006 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3007 | vector< CountedPtr<Scantable> > v( 1, in[itable] ) ;
|
---|
3008 | tmpin[itable] = average( v, mask, weight, avmode ) ;
|
---|
3009 | }
|
---|
3010 |
|
---|
3011 | // warning
|
---|
3012 | os << "Average spectra with different spectral resolution" << LogIO::POST ;
|
---|
3013 |
|
---|
3014 | // temporarily set coordinfo
|
---|
3015 | vector<string> oldinfo( insize ) ;
|
---|
3016 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3017 | vector<string> coordinfo = in[itable]->getCoordInfo() ;
|
---|
3018 | oldinfo[itable] = coordinfo[0] ;
|
---|
3019 | coordinfo[0] = "Hz" ;
|
---|
3020 | tmpin[itable]->setCoordInfo( coordinfo ) ;
|
---|
3021 | }
|
---|
3022 |
|
---|
3023 | // columns
|
---|
3024 | ScalarColumn<uInt> freqIDCol ;
|
---|
3025 | ScalarColumn<uInt> ifnoCol ;
|
---|
3026 | ScalarColumn<uInt> scannoCol ;
|
---|
3027 |
|
---|
3028 |
|
---|
3029 | // check IF frequency coverage
|
---|
3030 | // freqid: list of FREQ_ID, which is used, in each table
|
---|
3031 | // iffreq: list of minimum and maximum frequency for each FREQ_ID in
|
---|
3032 | // each table
|
---|
3033 | // freqid[insize][numIF]
|
---|
3034 | // freqid: [[id00, id01, ...],
|
---|
3035 | // [id10, id11, ...],
|
---|
3036 | // ...
|
---|
3037 | // [idn0, idn1, ...]]
|
---|
3038 | // iffreq[insize][numIF*2]
|
---|
3039 | // iffreq: [[min_id00, max_id00, min_id01, max_id01, ...],
|
---|
3040 | // [min_id10, max_id10, min_id11, max_id11, ...],
|
---|
3041 | // ...
|
---|
3042 | // [min_idn0, max_idn0, min_idn1, max_idn1, ...]]
|
---|
3043 | //os << "Check IF settings in each table" << LogIO::POST ;
|
---|
3044 | vector< vector<uInt> > freqid( insize );
|
---|
3045 | vector< vector<double> > iffreq( insize ) ;
|
---|
3046 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3047 | uInt rows = tmpin[itable]->nrow() ;
|
---|
3048 | uInt freqnrows = tmpin[itable]->frequencies().table().nrow() ;
|
---|
3049 | freqIDCol.attach( tmpin[itable]->table(), "FREQ_ID" ) ;
|
---|
3050 | //ifnoCol.attach( tmpin[itable]->table(), "IFNO" ) ;
|
---|
3051 | for ( uInt irow = 0 ; irow < rows ; irow++ ) {
|
---|
3052 | if ( freqid[itable].size() == freqnrows ) {
|
---|
3053 | break ;
|
---|
3054 | }
|
---|
3055 | else {
|
---|
3056 | uInt id = freqIDCol( irow ) ;
|
---|
3057 | if ( freqid[itable].size() == 0 || count( freqid[itable].begin(), freqid[itable].end(), id ) == 0 ) {
|
---|
3058 | //os << "itable = " << itable << ": IF " << id << " is included in the list" << LogIO::POST ;
|
---|
3059 | vector<double> abcissa = tmpin[itable]->getAbcissa( irow ) ;
|
---|
3060 | double lbedge, rbedge;
|
---|
3061 | freqid[itable].push_back( id ) ;
|
---|
3062 | lbedge = abcissa[0] - 0.5 * ( abcissa[1] - abcissa[0] );
|
---|
3063 | rbedge = abcissa[abcissa.size()-1] + 0.5 * ( abcissa[1] - abcissa[0] );
|
---|
3064 | iffreq[itable].push_back( min(lbedge, rbedge) ) ;
|
---|
3065 | iffreq[itable].push_back( max(lbedge, rbedge) ) ;
|
---|
3066 | }
|
---|
3067 | }
|
---|
3068 | }
|
---|
3069 | }
|
---|
3070 |
|
---|
3071 | // debug
|
---|
3072 | //os << "IF settings summary:" << endl ;
|
---|
3073 | //for ( uInt i = 0 ; i < freqid.size() ; i++ ) {
|
---|
3074 | //os << " Table" << i << endl ;
|
---|
3075 | //for ( uInt j = 0 ; j < freqid[i].size() ; j++ ) {
|
---|
3076 | //os << " id = " << freqid[i][j] << " (min,max) = (" << iffreq[i][2*j] << "," << iffreq[i][2*j+1] << ")" << endl ;
|
---|
3077 | //}
|
---|
3078 | //}
|
---|
3079 | //os << endl ;
|
---|
3080 | //os.post() ;
|
---|
3081 |
|
---|
3082 | // IF grouping based on their frequency coverage
|
---|
3083 | // ifgrp: list of table index and FREQ_ID for all members in each IF group
|
---|
3084 | // ifgfreq: list of minimum and maximum frequency in each IF group
|
---|
3085 | // ifgrp[numgrp][nummember*2]
|
---|
3086 | // ifgrp: [[table00, freqrow00, table01, freqrow01, ...],
|
---|
3087 | // [table10, freqrow10, table11, freqrow11, ...],
|
---|
3088 | // ...
|
---|
3089 | // [tablen0, freqrown0, tablen1, freqrown1, ...]]
|
---|
3090 | // ifgfreq[numgrp*2]
|
---|
3091 | // ifgfreq: [min0_grp0, max0_grp0, min1_grp1, max1_grp1, ...]
|
---|
3092 | //os << "IF grouping based on their frequency coverage" << LogIO::POST ;
|
---|
3093 | vector< vector<uInt> > ifgrp ;
|
---|
3094 | vector<double> ifgfreq ;
|
---|
3095 |
|
---|
3096 | // parameter for IF grouping
|
---|
3097 | // groupmode = OR retrieve all region
|
---|
3098 | // AND only retrieve overlaped region
|
---|
3099 | //string groupmode = "AND" ;
|
---|
3100 | string groupmode = "OR" ;
|
---|
3101 | uInt sizecr = 0 ;
|
---|
3102 | if ( groupmode == "AND" )
|
---|
3103 | sizecr = 1 ;
|
---|
3104 | else if ( groupmode == "OR" )
|
---|
3105 | sizecr = 0 ;
|
---|
3106 |
|
---|
3107 | vector<double> sortedfreq ;
|
---|
3108 | for ( uInt i = 0 ; i < iffreq.size() ; i++ ) {
|
---|
3109 | for ( uInt j = 0 ; j < iffreq[i].size() ; j++ ) {
|
---|
3110 | if ( count( sortedfreq.begin(), sortedfreq.end(), iffreq[i][j] ) == 0 )
|
---|
3111 | sortedfreq.push_back( iffreq[i][j] ) ;
|
---|
3112 | }
|
---|
3113 | }
|
---|
3114 | sort( sortedfreq.begin(), sortedfreq.end() ) ;
|
---|
3115 | for ( vector<double>::iterator i = sortedfreq.begin() ; i != sortedfreq.end()-1 ; i++ ) {
|
---|
3116 | ifgfreq.push_back( *i ) ;
|
---|
3117 | ifgfreq.push_back( *(i+1) ) ;
|
---|
3118 | }
|
---|
3119 | ifgrp.resize( ifgfreq.size()/2 ) ;
|
---|
3120 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3121 | for ( uInt iif = 0 ; iif < freqid[itable].size() ; iif++ ) {
|
---|
3122 | double range0 = iffreq[itable][2*iif] ;
|
---|
3123 | double range1 = iffreq[itable][2*iif+1] ;
|
---|
3124 | for ( uInt j = 0 ; j < ifgrp.size() ; j++ ) {
|
---|
3125 | double fmin = max( range0, ifgfreq[2*j] ) ;
|
---|
3126 | double fmax = min( range1, ifgfreq[2*j+1] ) ;
|
---|
3127 | if ( fmin < fmax ) {
|
---|
3128 | ifgrp[j].push_back( itable ) ;
|
---|
3129 | ifgrp[j].push_back( freqid[itable][iif] ) ;
|
---|
3130 | }
|
---|
3131 | }
|
---|
3132 | }
|
---|
3133 | }
|
---|
3134 | vector< vector<uInt> >::iterator fiter = ifgrp.begin() ;
|
---|
3135 | vector<double>::iterator giter = ifgfreq.begin() ;
|
---|
3136 | while( fiter != ifgrp.end() ) {
|
---|
3137 | if ( fiter->size() <= sizecr ) {
|
---|
3138 | fiter = ifgrp.erase( fiter ) ;
|
---|
3139 | giter = ifgfreq.erase( giter ) ;
|
---|
3140 | giter = ifgfreq.erase( giter ) ;
|
---|
3141 | }
|
---|
3142 | else {
|
---|
3143 | fiter++ ;
|
---|
3144 | advance( giter, 2 ) ;
|
---|
3145 | }
|
---|
3146 | }
|
---|
3147 |
|
---|
3148 | // Grouping continuous IF groups (without frequency gap)
|
---|
3149 | // freqgrp: list of IF group indexes in each frequency group
|
---|
3150 | // freqgrp[numgrp][nummember]
|
---|
3151 | // freqgrp: [[ifgrp00, ifgrp01, ifgrp02, ...],
|
---|
3152 | // [ifgrp10, ifgrp11, ifgrp12, ...],
|
---|
3153 | // ...
|
---|
3154 | // [ifgrpn0, ifgrpn1, ifgrpn2, ...]]
|
---|
3155 | vector< vector<uInt> > freqgrp ;
|
---|
3156 | double freqrange = 0.0 ;
|
---|
3157 | uInt grpnum = 0 ;
|
---|
3158 | for ( uInt i = 0 ; i < ifgrp.size() ; i++ ) {
|
---|
3159 | // Assumed that ifgfreq was sorted
|
---|
3160 | if ( grpnum != 0 && freqrange == ifgfreq[2*i] ) {
|
---|
3161 | freqgrp[grpnum-1].push_back( i ) ;
|
---|
3162 | }
|
---|
3163 | else {
|
---|
3164 | vector<uInt> grp0( 1, i ) ;
|
---|
3165 | freqgrp.push_back( grp0 ) ;
|
---|
3166 | grpnum++ ;
|
---|
3167 | }
|
---|
3168 | freqrange = ifgfreq[2*i+1] ;
|
---|
3169 | }
|
---|
3170 |
|
---|
3171 |
|
---|
3172 | // print IF groups
|
---|
3173 | ostringstream oss ;
|
---|
3174 | oss << "IF Group summary: " << endl ;
|
---|
3175 | oss << " GROUP_ID [FREQ_MIN, FREQ_MAX]: (TABLE_ID, FREQ_ID)" << endl ;
|
---|
3176 | for ( uInt i = 0 ; i < ifgrp.size() ; i++ ) {
|
---|
3177 | oss << " GROUP " << setw( 2 ) << i << " [" << ifgfreq[2*i] << "," << ifgfreq[2*i+1] << "]: " ;
|
---|
3178 | for ( uInt j = 0 ; j < ifgrp[i].size()/2 ; j++ ) {
|
---|
3179 | oss << "(" << ifgrp[i][2*j] << "," << ifgrp[i][2*j+1] << ") " ;
|
---|
3180 | }
|
---|
3181 | oss << endl ;
|
---|
3182 | }
|
---|
3183 | oss << endl ;
|
---|
3184 | os << oss.str() << LogIO::POST ;
|
---|
3185 |
|
---|
3186 | // print frequency group
|
---|
3187 | oss.str("") ;
|
---|
3188 | oss << "Frequency Group summary: " << endl ;
|
---|
3189 | oss << " GROUP_ID [FREQ_MIN, FREQ_MAX]: IF_GROUP_ID" << endl ;
|
---|
3190 | for ( uInt i = 0 ; i < freqgrp.size() ; i++ ) {
|
---|
3191 | oss << " GROUP " << setw( 2 ) << i << " [" << ifgfreq[2*freqgrp[i][0]] << "," << ifgfreq[2*freqgrp[i][freqgrp[i].size()-1]+1] << "]: " ;
|
---|
3192 | for ( uInt j = 0 ; j < freqgrp[i].size() ; j++ ) {
|
---|
3193 | oss << freqgrp[i][j] << " " ;
|
---|
3194 | }
|
---|
3195 | oss << endl ;
|
---|
3196 | }
|
---|
3197 | oss << endl ;
|
---|
3198 | os << oss.str() << LogIO::POST ;
|
---|
3199 |
|
---|
3200 | // membership check
|
---|
3201 | // groups: list of IF group indexes whose frequency range overlaps with
|
---|
3202 | // that of each table and IF
|
---|
3203 | // groups[numtable][numIF][nummembership]
|
---|
3204 | // groups: [[[grp, grp,...], [grp, grp,...],...],
|
---|
3205 | // [[grp, grp,...], [grp, grp,...],...],
|
---|
3206 | // ...
|
---|
3207 | // [[grp, grp,...], [grp, grp,...],...]]
|
---|
3208 | vector< vector< vector<uInt> > > groups( insize ) ;
|
---|
3209 | for ( uInt i = 0 ; i < insize ; i++ ) {
|
---|
3210 | groups[i].resize( freqid[i].size() ) ;
|
---|
3211 | }
|
---|
3212 | for ( uInt igrp = 0 ; igrp < ifgrp.size() ; igrp++ ) {
|
---|
3213 | for ( uInt imem = 0 ; imem < ifgrp[igrp].size()/2 ; imem++ ) {
|
---|
3214 | uInt tableid = ifgrp[igrp][2*imem] ;
|
---|
3215 | vector<uInt>::iterator iter = find( freqid[tableid].begin(), freqid[tableid].end(), ifgrp[igrp][2*imem+1] ) ;
|
---|
3216 | if ( iter != freqid[tableid].end() ) {
|
---|
3217 | uInt rowid = distance( freqid[tableid].begin(), iter ) ;
|
---|
3218 | groups[tableid][rowid].push_back( igrp ) ;
|
---|
3219 | }
|
---|
3220 | }
|
---|
3221 | }
|
---|
3222 |
|
---|
3223 | // print membership
|
---|
3224 | // oss.str("") ;
|
---|
3225 | // for ( uInt i = 0 ; i < insize ; i++ ) {
|
---|
3226 | // oss << "Table " << i << endl ;
|
---|
3227 | // for ( uInt j = 0 ; j < groups[i].size() ; j++ ) {
|
---|
3228 | // oss << " FREQ_ID " << setw( 2 ) << freqid[i][j] << ": " ;
|
---|
3229 | // for ( uInt k = 0 ; k < groups[i][j].size() ; k++ ) {
|
---|
3230 | // oss << setw( 2 ) << groups[i][j][k] << " " ;
|
---|
3231 | // }
|
---|
3232 | // oss << endl ;
|
---|
3233 | // }
|
---|
3234 | // }
|
---|
3235 | // os << oss.str() << LogIO::POST ;
|
---|
3236 |
|
---|
3237 | // set back coordinfo
|
---|
3238 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3239 | vector<string> coordinfo = tmpin[itable]->getCoordInfo() ;
|
---|
3240 | coordinfo[0] = oldinfo[itable] ;
|
---|
3241 | tmpin[itable]->setCoordInfo( coordinfo ) ;
|
---|
3242 | }
|
---|
3243 |
|
---|
3244 | // Create additional table if needed
|
---|
3245 | bool oldInsitu = insitu_ ;
|
---|
3246 | setInsitu( false ) ;
|
---|
3247 | vector< vector<uInt> > addrow( insize ) ;
|
---|
3248 | vector<uInt> addtable( insize, 0 ) ;
|
---|
3249 | vector<uInt> newtableids( insize ) ;
|
---|
3250 | vector<uInt> newifids( insize, 0 ) ;
|
---|
3251 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3252 | //os << "Table " << itable << ": " ;
|
---|
3253 | for ( uInt ifrow = 0 ; ifrow < groups[itable].size() ; ifrow++ ) {
|
---|
3254 | addrow[itable].push_back( groups[itable][ifrow].size()-1 ) ;
|
---|
3255 | //os << addrow[itable][ifrow] << " " ;
|
---|
3256 | }
|
---|
3257 | addtable[itable] = *max_element( addrow[itable].begin(), addrow[itable].end() ) ;
|
---|
3258 | //os << "(" << addtable[itable] << ")" << LogIO::POST ;
|
---|
3259 | }
|
---|
3260 | newin.resize( insize ) ;
|
---|
3261 | copy( tmpin.begin(), tmpin.end(), newin.begin() ) ;
|
---|
3262 | for ( uInt i = 0 ; i < insize ; i++ ) {
|
---|
3263 | newtableids[i] = i ;
|
---|
3264 | }
|
---|
3265 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3266 | for ( uInt iadd = 0 ; iadd < addtable[itable] ; iadd++ ) {
|
---|
3267 | CountedPtr<Scantable> add = getScantable( newin[itable], false ) ;
|
---|
3268 | vector<int> freqidlist ;
|
---|
3269 | for ( uInt i = 0 ; i < groups[itable].size() ; i++ ) {
|
---|
3270 | if ( groups[itable][i].size() > iadd + 1 ) {
|
---|
3271 | freqidlist.push_back( freqid[itable][i] ) ;
|
---|
3272 | }
|
---|
3273 | }
|
---|
3274 | stringstream taqlstream ;
|
---|
3275 | taqlstream << "SELECT FROM $1 WHERE FREQ_ID IN [" ;
|
---|
3276 | for ( uInt i = 0 ; i < freqidlist.size() ; i++ ) {
|
---|
3277 | taqlstream << freqidlist[i] ;
|
---|
3278 | if ( i < freqidlist.size() - 1 )
|
---|
3279 | taqlstream << "," ;
|
---|
3280 | else
|
---|
3281 | taqlstream << "]" ;
|
---|
3282 | }
|
---|
3283 | string taql = taqlstream.str() ;
|
---|
3284 | //os << "taql = " << taql << LogIO::POST ;
|
---|
3285 | STSelector selector = STSelector() ;
|
---|
3286 | selector.setTaQL( taql ) ;
|
---|
3287 | add->setSelection( selector ) ;
|
---|
3288 | newin.push_back( add ) ;
|
---|
3289 | newtableids.push_back( itable ) ;
|
---|
3290 | newifids.push_back( iadd + 1 ) ;
|
---|
3291 | }
|
---|
3292 | }
|
---|
3293 |
|
---|
3294 | // udpate ifgrp
|
---|
3295 | uInt offset = insize ;
|
---|
3296 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3297 | for ( uInt iadd = 0 ; iadd < addtable[itable] ; iadd++ ) {
|
---|
3298 | for ( uInt ifrow = 0 ; ifrow < groups[itable].size() ; ifrow++ ) {
|
---|
3299 | if ( groups[itable][ifrow].size() > iadd + 1 ) {
|
---|
3300 | uInt igrp = groups[itable][ifrow][iadd+1] ;
|
---|
3301 | for ( uInt imem = 0 ; imem < ifgrp[igrp].size()/2 ; imem++ ) {
|
---|
3302 | if ( ifgrp[igrp][2*imem] == newtableids[offset+iadd] && ifgrp[igrp][2*imem+1] == freqid[newtableids[offset+iadd]][ifrow] ) {
|
---|
3303 | ifgrp[igrp][2*imem] = offset + iadd ;
|
---|
3304 | }
|
---|
3305 | }
|
---|
3306 | }
|
---|
3307 | }
|
---|
3308 | }
|
---|
3309 | offset += addtable[itable] ;
|
---|
3310 | }
|
---|
3311 |
|
---|
3312 | // print IF groups again for debug
|
---|
3313 | // oss.str( "" ) ;
|
---|
3314 | // oss << "IF Group summary: " << endl ;
|
---|
3315 | // oss << " GROUP_ID [FREQ_MIN, FREQ_MAX]: (TABLE_ID, FREQ_ID)" << endl ;
|
---|
3316 | // for ( uInt i = 0 ; i < ifgrp.size() ; i++ ) {
|
---|
3317 | // oss << " GROUP " << setw( 2 ) << i << " [" << ifgfreq[2*i] << "," << ifgfreq[2*i+1] << "]: " ;
|
---|
3318 | // for ( uInt j = 0 ; j < ifgrp[i].size()/2 ; j++ ) {
|
---|
3319 | // oss << "(" << ifgrp[i][2*j] << "," << ifgrp[i][2*j+1] << ") " ;
|
---|
3320 | // }
|
---|
3321 | // oss << endl ;
|
---|
3322 | // }
|
---|
3323 | // oss << endl ;
|
---|
3324 | // os << oss.str() << LogIO::POST ;
|
---|
3325 |
|
---|
3326 | // reset SCANNO and IFNO/FREQ_ID: IF is reset by the result of sortation
|
---|
3327 | os << "All scan number is set to 0" << LogIO::POST ;
|
---|
3328 | //os << "All IF number is set to IF group index" << LogIO::POST ;
|
---|
3329 | insize = newin.size() ;
|
---|
3330 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3331 | uInt rows = newin[itable]->nrow() ;
|
---|
3332 | Table &tmpt = newin[itable]->table() ;
|
---|
3333 | freqIDCol.attach( tmpt, "FREQ_ID" ) ;
|
---|
3334 | scannoCol.attach( tmpt, "SCANNO" ) ;
|
---|
3335 | ifnoCol.attach( tmpt, "IFNO" ) ;
|
---|
3336 | for ( uInt irow=0 ; irow < rows ; irow++ ) {
|
---|
3337 | scannoCol.put( irow, 0 ) ;
|
---|
3338 | uInt freqID = freqIDCol( irow ) ;
|
---|
3339 | vector<uInt>::iterator iter = find( freqid[newtableids[itable]].begin(), freqid[newtableids[itable]].end(), freqID ) ;
|
---|
3340 | if ( iter != freqid[newtableids[itable]].end() ) {
|
---|
3341 | uInt index = distance( freqid[newtableids[itable]].begin(), iter ) ;
|
---|
3342 | ifnoCol.put( irow, groups[newtableids[itable]][index][newifids[itable]] ) ;
|
---|
3343 | }
|
---|
3344 | else {
|
---|
3345 | throw(AipsError("IF grouping was wrong in additional tables.")) ;
|
---|
3346 | }
|
---|
3347 | }
|
---|
3348 | }
|
---|
3349 | oldinfo.resize( insize ) ;
|
---|
3350 | setInsitu( oldInsitu ) ;
|
---|
3351 |
|
---|
3352 | // temporarily set coordinfo
|
---|
3353 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3354 | vector<string> coordinfo = newin[itable]->getCoordInfo() ;
|
---|
3355 | oldinfo[itable] = coordinfo[0] ;
|
---|
3356 | coordinfo[0] = "Hz" ;
|
---|
3357 | newin[itable]->setCoordInfo( coordinfo ) ;
|
---|
3358 | }
|
---|
3359 |
|
---|
3360 | // save column values in the vector
|
---|
3361 | vector< vector<uInt> > freqIdVec( insize ) ;
|
---|
3362 | vector< vector<uInt> > ifNoVec( insize ) ;
|
---|
3363 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3364 | ifnoCol.attach( newin[itable]->table(), "IFNO" ) ;
|
---|
3365 | freqIDCol.attach( newin[itable]->table(), "FREQ_ID" ) ;
|
---|
3366 | for ( uInt irow = 0 ; irow < newin[itable]->table().nrow() ; irow++ ) {
|
---|
3367 | freqIdVec[itable].push_back( freqIDCol( irow ) ) ;
|
---|
3368 | ifNoVec[itable].push_back( ifnoCol( irow ) ) ;
|
---|
3369 | }
|
---|
3370 | }
|
---|
3371 |
|
---|
3372 | // reset spectra and flagtra: pick up common part of frequency coverage
|
---|
3373 | //os << "Pick common frequency range and align resolution" << LogIO::POST ;
|
---|
3374 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3375 | uInt rows = newin[itable]->nrow() ;
|
---|
3376 | int nminchan = -1 ;
|
---|
3377 | int nmaxchan = -1 ;
|
---|
3378 | vector<uInt> freqIdUpdate ;
|
---|
3379 | for ( uInt irow = 0 ; irow < rows ; irow++ ) {
|
---|
3380 | uInt ifno = ifNoVec[itable][irow] ; // IFNO is reset by group index (IF group index)
|
---|
3381 | double minfreq = ifgfreq[2*ifno] ;
|
---|
3382 | double maxfreq = ifgfreq[2*ifno+1] ;
|
---|
3383 | //os << "frequency range: [" << minfreq << "," << maxfreq << "]" << LogIO::POST ;
|
---|
3384 | vector<double> abcissa = newin[itable]->getAbcissa( irow ) ;
|
---|
3385 | int nchan = abcissa.size() ;
|
---|
3386 | double resol = abcissa[1] - abcissa[0] ;
|
---|
3387 | //os << "abcissa range : [" << abcissa[0] << "," << abcissa[nchan-1] << "]" << LogIO::POST ;
|
---|
3388 | uInt imin, imax;
|
---|
3389 | int sigres;
|
---|
3390 | if ( resol >= 0. ) {
|
---|
3391 | imin = 0;
|
---|
3392 | imax = nchan - 1 ;
|
---|
3393 | sigres = 1 ;
|
---|
3394 | } else {
|
---|
3395 | imin = nchan - 1 ;
|
---|
3396 | imax = 0 ;
|
---|
3397 | sigres = -1 ;
|
---|
3398 | resol = abs(resol) ;
|
---|
3399 | }
|
---|
3400 | if ( minfreq <= abcissa[imin] )
|
---|
3401 | nminchan = imin ;
|
---|
3402 | else {
|
---|
3403 | //double cfreq = ( minfreq - abcissa[0] ) / resol ;
|
---|
3404 | double cfreq = ( minfreq - abcissa[imin] + 0.5 * resol ) / resol ;
|
---|
3405 | nminchan = imin + sigres * (int(cfreq) + ( ( cfreq - int(cfreq) <= 0.5 ) ? 0 : 1 )) ;
|
---|
3406 | }
|
---|
3407 | if ( maxfreq >= abcissa[imax] )
|
---|
3408 | nmaxchan = imax;
|
---|
3409 | else {
|
---|
3410 | //double cfreq = ( abcissa[abcissa.size()-1] - maxfreq ) / resol ;
|
---|
3411 | double cfreq = ( abcissa[imax] - maxfreq + 0.5 * resol ) / resol ;
|
---|
3412 | nmaxchan = imax - sigres * (int(cfreq) +( ( cfreq - int(cfreq) >= 0.5 ) ? 1 : 0 )) ;
|
---|
3413 | }
|
---|
3414 | //os << "channel range (" << irow << "): [" << nminchan << "," << nmaxchan << "]" << LogIO::POST ;
|
---|
3415 | if ( nmaxchan < nminchan ) {
|
---|
3416 | int tmp = nmaxchan ;
|
---|
3417 | nmaxchan = nminchan ;
|
---|
3418 | nminchan = tmp ;
|
---|
3419 | }
|
---|
3420 | if ( nmaxchan > nminchan ) {
|
---|
3421 | newin[itable]->reshapeSpectrum( nminchan, nmaxchan, irow ) ;
|
---|
3422 | int newchan = nmaxchan - nminchan + 1 ;
|
---|
3423 | if ( count( freqIdUpdate.begin(), freqIdUpdate.end(), freqIdVec[itable][irow] ) == 0 && newchan < nchan ) {
|
---|
3424 | freqIdUpdate.push_back( freqIdVec[itable][irow] ) ;
|
---|
3425 |
|
---|
3426 | // Update before nminchan is lost
|
---|
3427 | uInt freqId = freqIdVec[itable][irow] ;
|
---|
3428 | Double refpix ;
|
---|
3429 | Double refval ;
|
---|
3430 | Double increment ;
|
---|
3431 | newin[itable]->frequencies().getEntry( refpix, refval, increment, freqId ) ;
|
---|
3432 | //refval = refval - ( refpix - nminchan ) * increment ;
|
---|
3433 | refval = abcissa[nminchan] ;
|
---|
3434 | refpix = 0 ;
|
---|
3435 | newin[itable]->frequencies().setEntry( refpix, refval, increment, freqId ) ;
|
---|
3436 | }
|
---|
3437 | }
|
---|
3438 | else {
|
---|
3439 | throw(AipsError("Failed to pick up common part of frequency range.")) ;
|
---|
3440 | }
|
---|
3441 | }
|
---|
3442 | // for ( uInt i = 0 ; i < freqIdUpdate.size() ; i++ ) {
|
---|
3443 | // uInt freqId = freqIdUpdate[i] ;
|
---|
3444 | // Double refpix ;
|
---|
3445 | // Double refval ;
|
---|
3446 | // Double increment ;
|
---|
3447 |
|
---|
3448 | // // update row
|
---|
3449 | // newin[itable]->frequencies().getEntry( refpix, refval, increment, freqId ) ;
|
---|
3450 | // refval = refval - ( refpix - nminchan ) * increment ;
|
---|
3451 | // refpix = 0 ;
|
---|
3452 | // newin[itable]->frequencies().setEntry( refpix, refval, increment, freqId ) ;
|
---|
3453 | // }
|
---|
3454 | }
|
---|
3455 |
|
---|
3456 |
|
---|
3457 | // reset spectra and flagtra: align spectral resolution
|
---|
3458 | //os << "Align spectral resolution" << LogIO::POST ;
|
---|
3459 | // gmaxdnu: the coarsest frequency resolution in the frequency group
|
---|
3460 | // gmemid: member index that have a resolution equal to gmaxdnu
|
---|
3461 | // gmaxdnu[numfreqgrp]
|
---|
3462 | // gmaxdnu: [dnu0, dnu1, ...]
|
---|
3463 | // gmemid[numfreqgrp]
|
---|
3464 | // gmemid: [id0, id1, ...]
|
---|
3465 | vector<double> gmaxdnu( freqgrp.size(), 0.0 ) ;
|
---|
3466 | vector<uInt> gmemid( freqgrp.size(), 0 ) ;
|
---|
3467 | for ( uInt igrp = 0 ; igrp < ifgrp.size() ; igrp++ ) {
|
---|
3468 | double maxdnu = 0.0 ; // maximum (coarsest) frequency resolution
|
---|
3469 | int minchan = INT_MAX ; // minimum channel number
|
---|
3470 | Double refpixref = -1 ; // reference of 'reference pixel'
|
---|
3471 | Double refvalref = -1 ; // reference of 'reference frequency'
|
---|
3472 | Double refinc = -1 ; // reference frequency resolution
|
---|
3473 | uInt refreqid ;
|
---|
3474 | uInt reftable = INT_MAX;
|
---|
3475 | // process only if group member > 1
|
---|
3476 | if ( ifgrp[igrp].size() > 2 ) {
|
---|
3477 | // find minchan and maxdnu in each group
|
---|
3478 | for ( uInt imem = 0 ; imem < ifgrp[igrp].size()/2 ; imem++ ) {
|
---|
3479 | uInt tableid = ifgrp[igrp][2*imem] ;
|
---|
3480 | uInt rowid = ifgrp[igrp][2*imem+1] ;
|
---|
3481 | vector<uInt>::iterator iter = find( freqIdVec[tableid].begin(), freqIdVec[tableid].end(), rowid ) ;
|
---|
3482 | if ( iter != freqIdVec[tableid].end() ) {
|
---|
3483 | uInt index = distance( freqIdVec[tableid].begin(), iter ) ;
|
---|
3484 | vector<double> abcissa = newin[tableid]->getAbcissa( index ) ;
|
---|
3485 | int nchan = abcissa.size() ;
|
---|
3486 | double dnu = abcissa[1] - abcissa[0] ;
|
---|
3487 | //os << "GROUP " << igrp << " (" << tableid << "," << rowid << "): nchan = " << nchan << " (minchan = " << minchan << ")" << LogIO::POST ;
|
---|
3488 | if ( nchan < minchan ) {
|
---|
3489 | minchan = nchan ;
|
---|
3490 | maxdnu = abs(dnu) ;
|
---|
3491 | newin[tableid]->frequencies().getEntry( refpixref, refvalref, refinc, rowid ) ;
|
---|
3492 | refreqid = rowid ;
|
---|
3493 | reftable = tableid ;
|
---|
3494 | // Spectra are reversed when dnu < 0
|
---|
3495 | if (dnu < 0)
|
---|
3496 | refpixref = minchan - 1 -refpixref ;
|
---|
3497 | }
|
---|
3498 | }
|
---|
3499 | }
|
---|
3500 | // regrid spectra in each group
|
---|
3501 | os << "GROUP " << igrp << endl ;
|
---|
3502 | os << " Channel number is adjusted to " << minchan << endl ;
|
---|
3503 | os << " Corresponding frequency resolution is " << maxdnu << "Hz" << LogIO::POST ;
|
---|
3504 | for ( uInt imem = 0 ; imem < ifgrp[igrp].size()/2 ; imem++ ) {
|
---|
3505 | uInt tableid = ifgrp[igrp][2*imem] ;
|
---|
3506 | uInt rowid = ifgrp[igrp][2*imem+1] ;
|
---|
3507 | freqIDCol.attach( newin[tableid]->table(), "FREQ_ID" ) ;
|
---|
3508 | //os << "tableid = " << tableid << " rowid = " << rowid << ": " << LogIO::POST ;
|
---|
3509 | //os << " regridChannel applied to " ;
|
---|
3510 | //if ( tableid != reftable )
|
---|
3511 | refreqid = newin[tableid]->frequencies().addEntry( refpixref, refvalref, maxdnu ) ;
|
---|
3512 | for ( uInt irow = 0 ; irow < newin[tableid]->table().nrow() ; irow++ ) {
|
---|
3513 | uInt tfreqid = freqIdVec[tableid][irow] ;
|
---|
3514 | if ( tfreqid == rowid ) {
|
---|
3515 | //os << irow << " " ;
|
---|
3516 | newin[tableid]->regridChannel( minchan, maxdnu, irow ) ;
|
---|
3517 | freqIDCol.put( irow, refreqid ) ;
|
---|
3518 | freqIdVec[tableid][irow] = refreqid ;
|
---|
3519 | }
|
---|
3520 | }
|
---|
3521 | //os << LogIO::POST ;
|
---|
3522 | }
|
---|
3523 | }
|
---|
3524 | else {
|
---|
3525 | uInt tableid = ifgrp[igrp][0] ;
|
---|
3526 | uInt rowid = ifgrp[igrp][1] ;
|
---|
3527 | vector<uInt>::iterator iter = find( freqIdVec[tableid].begin(), freqIdVec[tableid].end(), rowid ) ;
|
---|
3528 | if ( iter != freqIdVec[tableid].end() ) {
|
---|
3529 | uInt index = distance( freqIdVec[tableid].begin(), iter ) ;
|
---|
3530 | vector<double> abcissa = newin[tableid]->getAbcissa( index ) ;
|
---|
3531 | minchan = abcissa.size() ;
|
---|
3532 | maxdnu = abs( abcissa[1] - abcissa[0]) ;
|
---|
3533 | }
|
---|
3534 | }
|
---|
3535 | for ( uInt i = 0 ; i < freqgrp.size() ; i++ ) {
|
---|
3536 | if ( count( freqgrp[i].begin(), freqgrp[i].end(), igrp ) > 0 ) {
|
---|
3537 | if ( maxdnu > gmaxdnu[i] ) {
|
---|
3538 | gmaxdnu[i] = maxdnu ;
|
---|
3539 | gmemid[i] = igrp ;
|
---|
3540 | }
|
---|
3541 | break ;
|
---|
3542 | }
|
---|
3543 | }
|
---|
3544 | }
|
---|
3545 |
|
---|
3546 | // set back coordinfo
|
---|
3547 | for ( uInt itable = 0 ; itable < insize ; itable++ ) {
|
---|
3548 | vector<string> coordinfo = newin[itable]->getCoordInfo() ;
|
---|
3549 | coordinfo[0] = oldinfo[itable] ;
|
---|
3550 | newin[itable]->setCoordInfo( coordinfo ) ;
|
---|
3551 | }
|
---|
3552 |
|
---|
3553 | // accumulate all rows into the first table
|
---|
3554 | // NOTE: assumed in.size() = 1
|
---|
3555 | vector< CountedPtr<Scantable> > tmp( 1 ) ;
|
---|
3556 | if ( newin.size() == 1 )
|
---|
3557 | tmp[0] = newin[0] ;
|
---|
3558 | else
|
---|
3559 | tmp[0] = merge( newin ) ;
|
---|
3560 |
|
---|
3561 | //return tmp[0] ;
|
---|
3562 |
|
---|
3563 | // average
|
---|
3564 | CountedPtr<Scantable> tmpout = average( tmp, mask, weight, avmode ) ;
|
---|
3565 |
|
---|
3566 | //return tmpout ;
|
---|
3567 |
|
---|
3568 | // combine frequency group
|
---|
3569 | os << "Combine spectra based on frequency grouping" << LogIO::POST ;
|
---|
3570 | os << "IFNO is renumbered as frequency group ID (see above)" << LogIO::POST ;
|
---|
3571 | vector<string> coordinfo = tmpout->getCoordInfo() ;
|
---|
3572 | oldinfo[0] = coordinfo[0] ;
|
---|
3573 | coordinfo[0] = "Hz" ;
|
---|
3574 | tmpout->setCoordInfo( coordinfo ) ;
|
---|
3575 | // create proformas of output table
|
---|
3576 | stringstream taqlstream ;
|
---|
3577 | taqlstream << "SELECT FROM $1 WHERE IFNO IN [" ;
|
---|
3578 | for ( uInt i = 0 ; i < gmemid.size() ; i++ ) {
|
---|
3579 | taqlstream << gmemid[i] ;
|
---|
3580 | if ( i < gmemid.size() - 1 )
|
---|
3581 | taqlstream << "," ;
|
---|
3582 | else
|
---|
3583 | taqlstream << "]" ;
|
---|
3584 | }
|
---|
3585 | string taql = taqlstream.str() ;
|
---|
3586 | //os << "taql = " << taql << LogIO::POST ;
|
---|
3587 | STSelector selector = STSelector() ;
|
---|
3588 | selector.setTaQL( taql ) ;
|
---|
3589 | oldInsitu = insitu_ ;
|
---|
3590 | setInsitu( false ) ;
|
---|
3591 | out = getScantable( tmpout, false ) ;
|
---|
3592 | setInsitu( oldInsitu ) ;
|
---|
3593 | out->setSelection( selector ) ;
|
---|
3594 | // regrid rows
|
---|
3595 | ifnoCol.attach( tmpout->table(), "IFNO" ) ;
|
---|
3596 | for ( uInt irow = 0 ; irow < tmpout->table().nrow() ; irow++ ) {
|
---|
3597 | uInt ifno = ifnoCol( irow ) ;
|
---|
3598 | for ( uInt igrp = 0 ; igrp < freqgrp.size() ; igrp++ ) {
|
---|
3599 | if ( count( freqgrp[igrp].begin(), freqgrp[igrp].end(), ifno ) > 0 ) {
|
---|
3600 | vector<double> abcissa = tmpout->getAbcissa( irow ) ;
|
---|
3601 | double bw = ( abcissa[1] - abcissa[0] ) * abcissa.size() ;
|
---|
3602 | int nchan = (int) abs( bw / gmaxdnu[igrp] ) ;
|
---|
3603 | // All spectra will have positive frequency increments
|
---|
3604 | tmpout->regridChannel( nchan, gmaxdnu[igrp], irow ) ;
|
---|
3605 | break ;
|
---|
3606 | }
|
---|
3607 | }
|
---|
3608 | }
|
---|
3609 | // combine spectra
|
---|
3610 | ArrayColumn<Float> specColOut( out->table(), "SPECTRA" ) ;
|
---|
3611 | ArrayColumn<uChar> flagColOut( out->table(), "FLAGTRA" ) ;
|
---|
3612 | ArrayColumn<Float> tsysColOut( out->table(), "TSYS" ) ;
|
---|
3613 | ScalarColumn<uInt> ifnoColOut( out->table(), "IFNO" ) ;
|
---|
3614 | ScalarColumn<uInt> polnoColOut( out->table(), "POLNO" ) ;
|
---|
3615 | ScalarColumn<uInt> freqidColOut( out->table(), "FREQ_ID" ) ;
|
---|
3616 | // MDirection::ScalarColumn dirColOut ;
|
---|
3617 | // dirColOut.attach( out->table(), "DIRECTION" ) ;
|
---|
3618 | Table &tab = tmpout->table() ;
|
---|
3619 | Block<String> cols(1);
|
---|
3620 | cols[0] = String("POLNO") ;
|
---|
3621 | TableIterator iter( tab, cols ) ;
|
---|
3622 | vector< vector<uInt> > sizes( freqgrp.size() ) ;
|
---|
3623 | vector<uInt> totalsizes( freqgrp.size(), 0 ) ;
|
---|
3624 | ArrayColumn<Float> specCols ;
|
---|
3625 | ArrayColumn<uChar> flagCols ;
|
---|
3626 | ArrayColumn<Float> tsysCols ;
|
---|
3627 | ScalarColumn<uInt> polnos ;
|
---|
3628 | vector< Vector<Float> > specout( freqgrp.size() ) ;
|
---|
3629 | vector< Vector<uChar> > flagout( freqgrp.size() ) ;
|
---|
3630 | vector< Vector<Float> > tsysout( freqgrp.size() ) ;
|
---|
3631 | while( !iter.pastEnd() ) {
|
---|
3632 | specCols.attach( iter.table(), "SPECTRA" ) ;
|
---|
3633 | flagCols.attach( iter.table(), "FLAGTRA" ) ;
|
---|
3634 | tsysCols.attach( iter.table(), "TSYS" ) ;
|
---|
3635 | ifnoCol.attach( iter.table(), "IFNO" ) ;
|
---|
3636 | polnos.attach( iter.table(), "POLNO" ) ;
|
---|
3637 | // MDirection::ScalarColumn dircol ;
|
---|
3638 | // dircol.attach( iter.table(), "DIRECTION" ) ;
|
---|
3639 | uInt polno = polnos( 0 ) ;
|
---|
3640 | //os << "POLNO iteration: " << polno << LogIO::POST ;
|
---|
3641 | // for ( uInt igrp = 0 ; igrp < freqgrp.size() ; igrp++ ) {
|
---|
3642 | // sizes[igrp].resize( freqgrp[igrp].size() ) ;
|
---|
3643 | // for ( uInt imem = 0 ; imem < freqgrp[igrp].size() ; imem++ ) {
|
---|
3644 | // for ( uInt irow = 0 ; irow < iter.table().nrow() ; irow++ ) {
|
---|
3645 | // uInt ifno = ifnoCol( irow ) ;
|
---|
3646 | // if ( ifno == freqgrp[igrp][imem] ) {
|
---|
3647 | // Vector<Float> spec = specCols( irow ) ;
|
---|
3648 | // Vector<uChar> flag = flagCols( irow ) ;
|
---|
3649 | // vector<Float> svec ;
|
---|
3650 | // spec.tovector( svec ) ;
|
---|
3651 | // vector<uChar> fvec ;
|
---|
3652 | // flag.tovector( fvec ) ;
|
---|
3653 | // //os << "spec.size() = " << svec.size() << " fvec.size() = " << fvec.size() << LogIO::POST ;
|
---|
3654 | // specout[igrp].insert( specout[igrp].end(), svec.begin(), svec.end() ) ;
|
---|
3655 | // flagout[igrp].insert( flagout[igrp].end(), fvec.begin(), fvec.end() ) ;
|
---|
3656 | // //os << "specout[" << igrp << "].size() = " << specout[igrp].size() << LogIO::POST ;
|
---|
3657 | // sizes[igrp][imem] = spec.nelements() ;
|
---|
3658 | // }
|
---|
3659 | // }
|
---|
3660 | // }
|
---|
3661 | // for ( uInt irow = 0 ; irow < out->table().nrow() ; irow++ ) {
|
---|
3662 | // uInt ifout = ifnoColOut( irow ) ;
|
---|
3663 | // uInt polout = polnoColOut( irow ) ;
|
---|
3664 | // if ( ifout == gmemid[igrp] && polout == polno ) {
|
---|
3665 | // // set SPECTRA and FRAGTRA
|
---|
3666 | // Vector<Float> newspec( specout[igrp] ) ;
|
---|
3667 | // Vector<uChar> newflag( flagout[igrp] ) ;
|
---|
3668 | // specColOut.put( irow, newspec ) ;
|
---|
3669 | // flagColOut.put( irow, newflag ) ;
|
---|
3670 | // // IFNO renumbering
|
---|
3671 | // ifnoColOut.put( irow, igrp ) ;
|
---|
3672 | // }
|
---|
3673 | // }
|
---|
3674 | // }
|
---|
3675 | // get a list of number of channels for each frequency group member
|
---|
3676 | if ( totalsizes[0] == 0 ) {
|
---|
3677 | for ( uInt igrp = 0 ; igrp < freqgrp.size() ; igrp++ ) {
|
---|
3678 | sizes[igrp].resize( freqgrp[igrp].size() ) ;
|
---|
3679 | for ( uInt imem = 0 ; imem < freqgrp[igrp].size() ; imem++ ) {
|
---|
3680 | for ( uInt irow = 0 ; irow < iter.table().nrow() ; irow++ ) {
|
---|
3681 | uInt ifno = ifnoCol( irow ) ;
|
---|
3682 | if ( ifno == freqgrp[igrp][imem] ) {
|
---|
3683 | Vector<Float> spec = specCols( irow ) ;
|
---|
3684 | sizes[igrp][imem] = spec.nelements() ;
|
---|
3685 | totalsizes[igrp] += sizes[igrp][imem] ;
|
---|
3686 | break ;
|
---|
3687 | }
|
---|
3688 | }
|
---|
3689 | }
|
---|
3690 | specout[igrp].resize( totalsizes[igrp] ) ;
|
---|
3691 | flagout[igrp].resize( totalsizes[igrp] ) ;
|
---|
3692 | tsysout[igrp].resize( totalsizes[igrp] ) ;
|
---|
3693 | }
|
---|
3694 | }
|
---|
3695 |
|
---|
3696 | // combine spectra
|
---|
3697 | for ( uInt irow = 0 ; irow < out->table().nrow() ; irow++ ) {
|
---|
3698 | uInt polout = polnoColOut( irow ) ;
|
---|
3699 | if ( polout == polno ) {
|
---|
3700 | uInt ifout = ifnoColOut( irow ) ;
|
---|
3701 | // Vector<Double> direction = dirColOut(irow).getAngle(Unit(String("rad"))).getValue() ;
|
---|
3702 | uInt igrp ;
|
---|
3703 | for ( uInt jgrp = 0 ; jgrp < freqgrp.size() ; jgrp++ ) {
|
---|
3704 | if ( ifout == gmemid[jgrp] ) {
|
---|
3705 | igrp = jgrp ;
|
---|
3706 | break ;
|
---|
3707 | }
|
---|
3708 | }
|
---|
3709 | IPosition startpos( 1, 0 ) ;
|
---|
3710 | IPosition endpos( 1, 0 ) ;
|
---|
3711 | for ( uInt imem = 0 ; imem < freqgrp[igrp].size() ; imem++ ) {
|
---|
3712 | for ( uInt jrow = 0 ; jrow < iter.table().nrow() ; jrow++ ) {
|
---|
3713 | uInt ifno = ifnoCol( jrow ) ;
|
---|
3714 | // 2012/02/17 TN
|
---|
3715 | // Since STGrid is implemented, average doesn't consider direction
|
---|
3716 | // when accumulating
|
---|
3717 | // Vector<Double> tdir = dircol(jrow).getAngle(Unit(String("rad"))).getValue() ;
|
---|
3718 | // //if ( ifno == freqgrp[igrp][imem] && allTrue( tdir == direction ) ) {
|
---|
3719 | // Double dx = tdir[0] - direction[0] ;
|
---|
3720 | // Double dy = tdir[1] - direction[1] ;
|
---|
3721 | // Double dd = sqrt( dx * dx + dy * dy ) ;
|
---|
3722 | //if ( ifno == freqgrp[igrp][imem] && allNearAbs( tdir, direction, tol ) ) {
|
---|
3723 | // if ( ifno == freqgrp[igrp][imem] && dd <= tol ) {
|
---|
3724 | if ( ifno == freqgrp[igrp][imem] ) {
|
---|
3725 | endpos[0] = startpos[0] + sizes[igrp][imem] - 1 ;
|
---|
3726 | Vector<Float> spec = specCols( jrow ) ;
|
---|
3727 | Vector<uChar> flag = flagCols( jrow ) ;
|
---|
3728 | Vector<Float> tsys = tsysCols( jrow ) ;
|
---|
3729 | //os << "spec.size() = " << svec.size() << " fvec.size() = " << fvec.size() << LogIO::POST ;
|
---|
3730 | specout[igrp]( startpos, endpos ) = spec ;
|
---|
3731 | flagout[igrp]( startpos, endpos ) = flag ;
|
---|
3732 | if ( spec.size() == tsys.size() ) {
|
---|
3733 | tsysout[igrp]( startpos, endpos ) = tsys ;
|
---|
3734 | }
|
---|
3735 | else {
|
---|
3736 | tsysout[igrp]( startpos, endpos ) = tsys[0] ;
|
---|
3737 | }
|
---|
3738 | //os << "specout[" << igrp << "].size() = " << specout[igrp].size() << LogIO::POST ;
|
---|
3739 | startpos[0] += sizes[igrp][imem] ;
|
---|
3740 | }
|
---|
3741 | }
|
---|
3742 | }
|
---|
3743 | // set SPECTRA and FRAGTRA
|
---|
3744 | specColOut.put( irow, specout[igrp] ) ;
|
---|
3745 | flagColOut.put( irow, flagout[igrp] ) ;
|
---|
3746 | tsysColOut.put( irow, tsysout[igrp] ) ;
|
---|
3747 | // IFNO renumbering (renumbered as frequency group ID)
|
---|
3748 | ifnoColOut.put( irow, igrp ) ;
|
---|
3749 | }
|
---|
3750 | }
|
---|
3751 | iter++ ;
|
---|
3752 | }
|
---|
3753 | // update FREQUENCIES subtable
|
---|
3754 | vector<bool> updated( freqgrp.size(), false ) ;
|
---|
3755 | for ( uInt igrp = 0 ; igrp < freqgrp.size() ; igrp++ ) {
|
---|
3756 | uInt index = 0 ;
|
---|
3757 | uInt pixShift = 0 ;
|
---|
3758 |
|
---|
3759 | while ( freqgrp[igrp][index] != gmemid[igrp] ) {
|
---|
3760 | pixShift += sizes[igrp][index++] ;
|
---|
3761 | }
|
---|
3762 | for ( uInt irow = 0 ; irow < out->table().nrow() ; irow++ ) {
|
---|
3763 | //if ( ifnoColOut( irow ) == gmemid[igrp] && !updated[igrp] ) {
|
---|
3764 | if ( ifnoColOut( irow ) == igrp && !updated[igrp] ) {
|
---|
3765 | uInt freqidOut = freqidColOut( irow ) ;
|
---|
3766 | //os << "freqgrp " << igrp << " freqidOut = " << freqidOut << LogIO::POST ;
|
---|
3767 | double refpix ;
|
---|
3768 | double refval ;
|
---|
3769 | double increm ;
|
---|
3770 | out->frequencies().getEntry( refpix, refval, increm, freqidOut ) ;
|
---|
3771 | if (increm < 0)
|
---|
3772 | refpix = sizes[igrp][index] - 1 - refpix ; // reversed
|
---|
3773 | refpix += pixShift ;
|
---|
3774 | out->frequencies().setEntry( refpix, refval, gmaxdnu[igrp], freqidOut ) ;
|
---|
3775 | updated[igrp] = true ;
|
---|
3776 | }
|
---|
3777 | }
|
---|
3778 | }
|
---|
3779 |
|
---|
3780 | //out = tmpout ;
|
---|
3781 |
|
---|
3782 | coordinfo = tmpout->getCoordInfo() ;
|
---|
3783 | coordinfo[0] = oldinfo[0] ;
|
---|
3784 | tmpout->setCoordInfo( coordinfo ) ;
|
---|
3785 | }
|
---|
3786 | else {
|
---|
3787 | // simple average
|
---|
3788 | out = average( in, mask, weight, avmode ) ;
|
---|
3789 | }
|
---|
3790 |
|
---|
3791 | return out;
|
---|
3792 | }
|
---|
3793 |
|
---|
3794 | CountedPtr<Scantable> STMath::cwcal( const CountedPtr<Scantable>& s,
|
---|
3795 | const String calmode,
|
---|
3796 | const String antname )
|
---|
3797 | {
|
---|
3798 | // frequency switch
|
---|
3799 | if ( calmode == "fs" ) {
|
---|
3800 | return cwcalfs( s, antname ) ;
|
---|
3801 | }
|
---|
3802 | else {
|
---|
3803 | vector<bool> masks = s->getMask( 0 ) ;
|
---|
3804 | vector<int> types ;
|
---|
3805 |
|
---|
3806 | // sky scan
|
---|
3807 | STSelector sel = STSelector() ;
|
---|
3808 | types.push_back( SrcType::SKY ) ;
|
---|
3809 | sel.setTypes( types ) ;
|
---|
3810 | s->setSelection( sel ) ;
|
---|
3811 | vector< CountedPtr<Scantable> > tmp( 1, getScantable( s, false ) ) ;
|
---|
3812 | CountedPtr<Scantable> asky = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
3813 | s->unsetSelection() ;
|
---|
3814 | sel.reset() ;
|
---|
3815 | types.clear() ;
|
---|
3816 |
|
---|
3817 | // hot scan
|
---|
3818 | types.push_back( SrcType::HOT ) ;
|
---|
3819 | sel.setTypes( types ) ;
|
---|
3820 | s->setSelection( sel ) ;
|
---|
3821 | tmp.clear() ;
|
---|
3822 | tmp.push_back( getScantable( s, false ) ) ;
|
---|
3823 | CountedPtr<Scantable> ahot = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
3824 | s->unsetSelection() ;
|
---|
3825 | sel.reset() ;
|
---|
3826 | types.clear() ;
|
---|
3827 |
|
---|
3828 | // cold scan
|
---|
3829 | CountedPtr<Scantable> acold ;
|
---|
3830 | // types.push_back( SrcType::COLD ) ;
|
---|
3831 | // sel.setTypes( types ) ;
|
---|
3832 | // s->setSelection( sel ) ;
|
---|
3833 | // tmp.clear() ;
|
---|
3834 | // tmp.push_back( getScantable( s, false ) ) ;
|
---|
3835 | // CountedPtr<Scantable> acold = average( tmp, masks, "TINT", "SCNAN" ) ;
|
---|
3836 | // s->unsetSelection() ;
|
---|
3837 | // sel.reset() ;
|
---|
3838 | // types.clear() ;
|
---|
3839 |
|
---|
3840 | // off scan
|
---|
3841 | types.push_back( SrcType::PSOFF ) ;
|
---|
3842 | sel.setTypes( types ) ;
|
---|
3843 | s->setSelection( sel ) ;
|
---|
3844 | tmp.clear() ;
|
---|
3845 | tmp.push_back( getScantable( s, false ) ) ;
|
---|
3846 | CountedPtr<Scantable> aoff = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
3847 | s->unsetSelection() ;
|
---|
3848 | sel.reset() ;
|
---|
3849 | types.clear() ;
|
---|
3850 |
|
---|
3851 | // on scan
|
---|
3852 | bool insitu = insitu_ ;
|
---|
3853 | insitu_ = false ;
|
---|
3854 | CountedPtr<Scantable> out = getScantable( s, true ) ;
|
---|
3855 | insitu_ = insitu ;
|
---|
3856 | types.push_back( SrcType::PSON ) ;
|
---|
3857 | sel.setTypes( types ) ;
|
---|
3858 | s->setSelection( sel ) ;
|
---|
3859 | TableCopy::copyRows( out->table(), s->table() ) ;
|
---|
3860 | s->unsetSelection() ;
|
---|
3861 | sel.reset() ;
|
---|
3862 | types.clear() ;
|
---|
3863 |
|
---|
3864 | // process each on scan
|
---|
3865 | ArrayColumn<Float> tsysCol ;
|
---|
3866 | tsysCol.attach( out->table(), "TSYS" ) ;
|
---|
3867 | for ( int i = 0 ; i < out->nrow() ; i++ ) {
|
---|
3868 | vector<float> sp = getCalibratedSpectra( out, aoff, asky, ahot, acold, i, antname ) ;
|
---|
3869 | out->setSpectrum( sp, i ) ;
|
---|
3870 | string reftime = out->getTime( i ) ;
|
---|
3871 | vector<int> ii( 1, out->getIF( i ) ) ;
|
---|
3872 | vector<int> ib( 1, out->getBeam( i ) ) ;
|
---|
3873 | vector<int> ip( 1, out->getPol( i ) ) ;
|
---|
3874 | sel.setIFs( ii ) ;
|
---|
3875 | sel.setBeams( ib ) ;
|
---|
3876 | sel.setPolarizations( ip ) ;
|
---|
3877 | asky->setSelection( sel ) ;
|
---|
3878 | vector<float> sptsys = getTsysFromTime( reftime, asky, "linear" ) ;
|
---|
3879 | const Vector<Float> Vtsys( sptsys ) ;
|
---|
3880 | tsysCol.put( i, Vtsys ) ;
|
---|
3881 | asky->unsetSelection() ;
|
---|
3882 | sel.reset() ;
|
---|
3883 | }
|
---|
3884 |
|
---|
3885 | // flux unit
|
---|
3886 | out->setFluxUnit( "K" ) ;
|
---|
3887 |
|
---|
3888 | return out ;
|
---|
3889 | }
|
---|
3890 | }
|
---|
3891 |
|
---|
3892 | CountedPtr<Scantable> STMath::almacal( const CountedPtr<Scantable>& s,
|
---|
3893 | const String calmode )
|
---|
3894 | {
|
---|
3895 | // frequency switch
|
---|
3896 | if ( calmode == "fs" ) {
|
---|
3897 | return almacalfs( s ) ;
|
---|
3898 | }
|
---|
3899 | else {
|
---|
3900 | vector<bool> masks = s->getMask( 0 ) ;
|
---|
3901 |
|
---|
3902 | // off scan
|
---|
3903 | STSelector sel = STSelector() ;
|
---|
3904 | vector<int> types ;
|
---|
3905 | types.push_back( SrcType::PSOFF ) ;
|
---|
3906 | sel.setTypes( types ) ;
|
---|
3907 | s->setSelection( sel ) ;
|
---|
3908 | // TODO 2010/01/08 TN
|
---|
3909 | // Grouping by time should be needed before averaging.
|
---|
3910 | // Each group must have own unique SCANNO (should be renumbered).
|
---|
3911 | // See PIPELINE/SDCalibration.py
|
---|
3912 | CountedPtr<Scantable> soff = getScantable( s, false ) ;
|
---|
3913 | Table ttab = soff->table() ;
|
---|
3914 | ROScalarColumn<Double> timeCol( ttab, "TIME" ) ;
|
---|
3915 | uInt nrow = timeCol.nrow() ;
|
---|
3916 | Vector<Double> timeSep( nrow - 1 ) ;
|
---|
3917 | for ( uInt i = 0 ; i < nrow - 1 ; i++ ) {
|
---|
3918 | timeSep[i] = timeCol(i+1) - timeCol(i) ;
|
---|
3919 | }
|
---|
3920 | ScalarColumn<Double> intervalCol( ttab, "INTERVAL" ) ;
|
---|
3921 | Vector<Double> interval = intervalCol.getColumn() ;
|
---|
3922 | interval /= 86400.0 ;
|
---|
3923 | ScalarColumn<uInt> scanCol( ttab, "SCANNO" ) ;
|
---|
3924 | vector<uInt> glist ;
|
---|
3925 | for ( uInt i = 0 ; i < nrow - 1 ; i++ ) {
|
---|
3926 | double gap = 2.0 * timeSep[i] / ( interval[i] + interval[i+1] ) ;
|
---|
3927 | //cout << "gap[" << i << "]=" << setw(5) << gap << endl ;
|
---|
3928 | if ( gap > 1.1 ) {
|
---|
3929 | glist.push_back( i ) ;
|
---|
3930 | }
|
---|
3931 | }
|
---|
3932 | Vector<uInt> gaplist( glist ) ;
|
---|
3933 | //cout << "gaplist = " << gaplist << endl ;
|
---|
3934 | uInt newid = 0 ;
|
---|
3935 | for ( uInt i = 0 ; i < nrow ; i++ ) {
|
---|
3936 | scanCol.put( i, newid ) ;
|
---|
3937 | if ( i == gaplist[newid] ) {
|
---|
3938 | newid++ ;
|
---|
3939 | }
|
---|
3940 | }
|
---|
3941 | //cout << "new scancol = " << scanCol.getColumn() << endl ;
|
---|
3942 | vector< CountedPtr<Scantable> > tmp( 1, soff ) ;
|
---|
3943 | CountedPtr<Scantable> aoff = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
3944 | //cout << "aoff.nrow = " << aoff->nrow() << endl ;
|
---|
3945 | s->unsetSelection() ;
|
---|
3946 | sel.reset() ;
|
---|
3947 | types.clear() ;
|
---|
3948 |
|
---|
3949 | // on scan
|
---|
3950 | bool insitu = insitu_ ;
|
---|
3951 | insitu_ = false ;
|
---|
3952 | CountedPtr<Scantable> out = getScantable( s, true ) ;
|
---|
3953 | insitu_ = insitu ;
|
---|
3954 | types.push_back( SrcType::PSON ) ;
|
---|
3955 | sel.setTypes( types ) ;
|
---|
3956 | s->setSelection( sel ) ;
|
---|
3957 | TableCopy::copyRows( out->table(), s->table() ) ;
|
---|
3958 | s->unsetSelection() ;
|
---|
3959 | sel.reset() ;
|
---|
3960 | types.clear() ;
|
---|
3961 |
|
---|
3962 | // process each on scan
|
---|
3963 | ArrayColumn<Float> tsysCol ;
|
---|
3964 | tsysCol.attach( out->table(), "TSYS" ) ;
|
---|
3965 | for ( int i = 0 ; i < out->nrow() ; i++ ) {
|
---|
3966 | vector<float> sp = getCalibratedSpectra( out, aoff, i ) ;
|
---|
3967 | out->setSpectrum( sp, i ) ;
|
---|
3968 | }
|
---|
3969 |
|
---|
3970 | // flux unit
|
---|
3971 | out->setFluxUnit( "K" ) ;
|
---|
3972 |
|
---|
3973 | return out ;
|
---|
3974 | }
|
---|
3975 | }
|
---|
3976 |
|
---|
3977 | CountedPtr<Scantable> STMath::cwcalfs( const CountedPtr<Scantable>& s,
|
---|
3978 | const String antname )
|
---|
3979 | {
|
---|
3980 | vector<int> types ;
|
---|
3981 |
|
---|
3982 | // APEX calibration mode
|
---|
3983 | int apexcalmode = 1 ;
|
---|
3984 |
|
---|
3985 | if ( antname.find( "APEX" ) != string::npos ) {
|
---|
3986 | // check if off scan exists or not
|
---|
3987 | STSelector sel = STSelector() ;
|
---|
3988 | //sel.setName( offstr1 ) ;
|
---|
3989 | types.push_back( SrcType::FLOOFF ) ;
|
---|
3990 | sel.setTypes( types ) ;
|
---|
3991 | try {
|
---|
3992 | s->setSelection( sel ) ;
|
---|
3993 | }
|
---|
3994 | catch ( AipsError &e ) {
|
---|
3995 | apexcalmode = 0 ;
|
---|
3996 | }
|
---|
3997 | sel.reset() ;
|
---|
3998 | }
|
---|
3999 | s->unsetSelection() ;
|
---|
4000 | types.clear() ;
|
---|
4001 |
|
---|
4002 | vector<bool> masks = s->getMask( 0 ) ;
|
---|
4003 | CountedPtr<Scantable> ssig, sref ;
|
---|
4004 | CountedPtr<Scantable> out ;
|
---|
4005 |
|
---|
4006 | if ( antname.find( "APEX" ) != string::npos ) {
|
---|
4007 | // APEX calibration
|
---|
4008 | // sky scan
|
---|
4009 | STSelector sel = STSelector() ;
|
---|
4010 | types.push_back( SrcType::FLOSKY ) ;
|
---|
4011 | sel.setTypes( types ) ;
|
---|
4012 | s->setSelection( sel ) ;
|
---|
4013 | vector< CountedPtr<Scantable> > tmp( 1, getScantable( s, false ) ) ;
|
---|
4014 | CountedPtr<Scantable> askylo = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
4015 | s->unsetSelection() ;
|
---|
4016 | sel.reset() ;
|
---|
4017 | types.clear() ;
|
---|
4018 | types.push_back( SrcType::FHISKY ) ;
|
---|
4019 | sel.setTypes( types ) ;
|
---|
4020 | s->setSelection( sel ) ;
|
---|
4021 | tmp.clear() ;
|
---|
4022 | tmp.push_back( getScantable( s, false ) ) ;
|
---|
4023 | CountedPtr<Scantable> askyhi = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
4024 | s->unsetSelection() ;
|
---|
4025 | sel.reset() ;
|
---|
4026 | types.clear() ;
|
---|
4027 |
|
---|
4028 | // hot scan
|
---|
4029 | types.push_back( SrcType::FLOHOT ) ;
|
---|
4030 | sel.setTypes( types ) ;
|
---|
4031 | s->setSelection( sel ) ;
|
---|
4032 | tmp.clear() ;
|
---|
4033 | tmp.push_back( getScantable( s, false ) ) ;
|
---|
4034 | CountedPtr<Scantable> ahotlo = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
4035 | s->unsetSelection() ;
|
---|
4036 | sel.reset() ;
|
---|
4037 | types.clear() ;
|
---|
4038 | types.push_back( SrcType::FHIHOT ) ;
|
---|
4039 | sel.setTypes( types ) ;
|
---|
4040 | s->setSelection( sel ) ;
|
---|
4041 | tmp.clear() ;
|
---|
4042 | tmp.push_back( getScantable( s, false ) ) ;
|
---|
4043 | CountedPtr<Scantable> ahothi = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
4044 | s->unsetSelection() ;
|
---|
4045 | sel.reset() ;
|
---|
4046 | types.clear() ;
|
---|
4047 |
|
---|
4048 | // cold scan
|
---|
4049 | CountedPtr<Scantable> acoldlo, acoldhi ;
|
---|
4050 | // types.push_back( SrcType::FLOCOLD ) ;
|
---|
4051 | // sel.setTypes( types ) ;
|
---|
4052 | // s->setSelection( sel ) ;
|
---|
4053 | // tmp.clear() ;
|
---|
4054 | // tmp.push_back( getScantable( s, false ) ) ;
|
---|
4055 | // CountedPtr<Scantable> acoldlo = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
4056 | // s->unsetSelection() ;
|
---|
4057 | // sel.reset() ;
|
---|
4058 | // types.clear() ;
|
---|
4059 | // types.push_back( SrcType::FHICOLD ) ;
|
---|
4060 | // sel.setTypes( types ) ;
|
---|
4061 | // s->setSelection( sel ) ;
|
---|
4062 | // tmp.clear() ;
|
---|
4063 | // tmp.push_back( getScantable( s, false ) ) ;
|
---|
4064 | // CountedPtr<Scantable> acoldhi = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
4065 | // s->unsetSelection() ;
|
---|
4066 | // sel.reset() ;
|
---|
4067 | // types.clear() ;
|
---|
4068 |
|
---|
4069 | // ref scan
|
---|
4070 | bool insitu = insitu_ ;
|
---|
4071 | insitu_ = false ;
|
---|
4072 | sref = getScantable( s, true ) ;
|
---|
4073 | insitu_ = insitu ;
|
---|
4074 | types.push_back( SrcType::FSLO ) ;
|
---|
4075 | sel.setTypes( types ) ;
|
---|
4076 | s->setSelection( sel ) ;
|
---|
4077 | TableCopy::copyRows( sref->table(), s->table() ) ;
|
---|
4078 | s->unsetSelection() ;
|
---|
4079 | sel.reset() ;
|
---|
4080 | types.clear() ;
|
---|
4081 |
|
---|
4082 | // sig scan
|
---|
4083 | insitu_ = false ;
|
---|
4084 | ssig = getScantable( s, true ) ;
|
---|
4085 | insitu_ = insitu ;
|
---|
4086 | types.push_back( SrcType::FSHI ) ;
|
---|
4087 | sel.setTypes( types ) ;
|
---|
4088 | s->setSelection( sel ) ;
|
---|
4089 | TableCopy::copyRows( ssig->table(), s->table() ) ;
|
---|
4090 | s->unsetSelection() ;
|
---|
4091 | sel.reset() ;
|
---|
4092 | types.clear() ;
|
---|
4093 |
|
---|
4094 | if ( apexcalmode == 0 ) {
|
---|
4095 | // APEX fs data without off scan
|
---|
4096 | // process each sig and ref scan
|
---|
4097 | ArrayColumn<Float> tsysCollo ;
|
---|
4098 | tsysCollo.attach( ssig->table(), "TSYS" ) ;
|
---|
4099 | ArrayColumn<Float> tsysColhi ;
|
---|
4100 | tsysColhi.attach( sref->table(), "TSYS" ) ;
|
---|
4101 | for ( int i = 0 ; i < ssig->nrow() ; i++ ) {
|
---|
4102 | vector< CountedPtr<Scantable> > sky( 2 ) ;
|
---|
4103 | sky[0] = askylo ;
|
---|
4104 | sky[1] = askyhi ;
|
---|
4105 | vector< CountedPtr<Scantable> > hot( 2 ) ;
|
---|
4106 | hot[0] = ahotlo ;
|
---|
4107 | hot[1] = ahothi ;
|
---|
4108 | vector< CountedPtr<Scantable> > cold( 2 ) ;
|
---|
4109 | //cold[0] = acoldlo ;
|
---|
4110 | //cold[1] = acoldhi ;
|
---|
4111 | vector<float> sp = getFSCalibratedSpectra( ssig, sref, sky, hot, cold, i ) ;
|
---|
4112 | ssig->setSpectrum( sp, i ) ;
|
---|
4113 | string reftime = ssig->getTime( i ) ;
|
---|
4114 | vector<int> ii( 1, ssig->getIF( i ) ) ;
|
---|
4115 | vector<int> ib( 1, ssig->getBeam( i ) ) ;
|
---|
4116 | vector<int> ip( 1, ssig->getPol( i ) ) ;
|
---|
4117 | sel.setIFs( ii ) ;
|
---|
4118 | sel.setBeams( ib ) ;
|
---|
4119 | sel.setPolarizations( ip ) ;
|
---|
4120 | askylo->setSelection( sel ) ;
|
---|
4121 | vector<float> sptsys = getTsysFromTime( reftime, askylo, "linear" ) ;
|
---|
4122 | const Vector<Float> Vtsyslo( sptsys ) ;
|
---|
4123 | tsysCollo.put( i, Vtsyslo ) ;
|
---|
4124 | askylo->unsetSelection() ;
|
---|
4125 | sel.reset() ;
|
---|
4126 | sky[0] = askyhi ;
|
---|
4127 | sky[1] = askylo ;
|
---|
4128 | hot[0] = ahothi ;
|
---|
4129 | hot[1] = ahotlo ;
|
---|
4130 | cold[0] = acoldhi ;
|
---|
4131 | cold[1] = acoldlo ;
|
---|
4132 | sp = getFSCalibratedSpectra( sref, ssig, sky, hot, cold, i ) ;
|
---|
4133 | sref->setSpectrum( sp, i ) ;
|
---|
4134 | reftime = sref->getTime( i ) ;
|
---|
4135 | ii[0] = sref->getIF( i ) ;
|
---|
4136 | ib[0] = sref->getBeam( i ) ;
|
---|
4137 | ip[0] = sref->getPol( i ) ;
|
---|
4138 | sel.setIFs( ii ) ;
|
---|
4139 | sel.setBeams( ib ) ;
|
---|
4140 | sel.setPolarizations( ip ) ;
|
---|
4141 | askyhi->setSelection( sel ) ;
|
---|
4142 | sptsys = getTsysFromTime( reftime, askyhi, "linear" ) ;
|
---|
4143 | const Vector<Float> Vtsyshi( sptsys ) ;
|
---|
4144 | tsysColhi.put( i, Vtsyshi ) ;
|
---|
4145 | askyhi->unsetSelection() ;
|
---|
4146 | sel.reset() ;
|
---|
4147 | }
|
---|
4148 | }
|
---|
4149 | else if ( apexcalmode == 1 ) {
|
---|
4150 | // APEX fs data with off scan
|
---|
4151 | // off scan
|
---|
4152 | types.push_back( SrcType::FLOOFF ) ;
|
---|
4153 | sel.setTypes( types ) ;
|
---|
4154 | s->setSelection( sel ) ;
|
---|
4155 | tmp.clear() ;
|
---|
4156 | tmp.push_back( getScantable( s, false ) ) ;
|
---|
4157 | CountedPtr<Scantable> aofflo = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
4158 | s->unsetSelection() ;
|
---|
4159 | sel.reset() ;
|
---|
4160 | types.clear() ;
|
---|
4161 | types.push_back( SrcType::FHIOFF ) ;
|
---|
4162 | sel.setTypes( types ) ;
|
---|
4163 | s->setSelection( sel ) ;
|
---|
4164 | tmp.clear() ;
|
---|
4165 | tmp.push_back( getScantable( s, false ) ) ;
|
---|
4166 | CountedPtr<Scantable> aoffhi = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
4167 | s->unsetSelection() ;
|
---|
4168 | sel.reset() ;
|
---|
4169 | types.clear() ;
|
---|
4170 |
|
---|
4171 | // process each sig and ref scan
|
---|
4172 | ArrayColumn<Float> tsysCollo ;
|
---|
4173 | tsysCollo.attach( ssig->table(), "TSYS" ) ;
|
---|
4174 | ArrayColumn<Float> tsysColhi ;
|
---|
4175 | tsysColhi.attach( sref->table(), "TSYS" ) ;
|
---|
4176 | for ( int i = 0 ; i < ssig->nrow() ; i++ ) {
|
---|
4177 | vector<float> sp = getCalibratedSpectra( ssig, aofflo, askylo, ahotlo, acoldlo, i, antname ) ;
|
---|
4178 | ssig->setSpectrum( sp, i ) ;
|
---|
4179 | sp = getCalibratedSpectra( sref, aoffhi, askyhi, ahothi, acoldhi, i, antname ) ;
|
---|
4180 | string reftime = ssig->getTime( i ) ;
|
---|
4181 | vector<int> ii( 1, ssig->getIF( i ) ) ;
|
---|
4182 | vector<int> ib( 1, ssig->getBeam( i ) ) ;
|
---|
4183 | vector<int> ip( 1, ssig->getPol( i ) ) ;
|
---|
4184 | sel.setIFs( ii ) ;
|
---|
4185 | sel.setBeams( ib ) ;
|
---|
4186 | sel.setPolarizations( ip ) ;
|
---|
4187 | askylo->setSelection( sel ) ;
|
---|
4188 | vector<float> sptsys = getTsysFromTime( reftime, askylo, "linear" ) ;
|
---|
4189 | const Vector<Float> Vtsyslo( sptsys ) ;
|
---|
4190 | tsysCollo.put( i, Vtsyslo ) ;
|
---|
4191 | askylo->unsetSelection() ;
|
---|
4192 | sel.reset() ;
|
---|
4193 | sref->setSpectrum( sp, i ) ;
|
---|
4194 | reftime = sref->getTime( i ) ;
|
---|
4195 | ii[0] = sref->getIF( i ) ;
|
---|
4196 | ib[0] = sref->getBeam( i ) ;
|
---|
4197 | ip[0] = sref->getPol( i ) ;
|
---|
4198 | sel.setIFs( ii ) ;
|
---|
4199 | sel.setBeams( ib ) ;
|
---|
4200 | sel.setPolarizations( ip ) ;
|
---|
4201 | askyhi->setSelection( sel ) ;
|
---|
4202 | sptsys = getTsysFromTime( reftime, askyhi, "linear" ) ;
|
---|
4203 | const Vector<Float> Vtsyshi( sptsys ) ;
|
---|
4204 | tsysColhi.put( i, Vtsyshi ) ;
|
---|
4205 | askyhi->unsetSelection() ;
|
---|
4206 | sel.reset() ;
|
---|
4207 | }
|
---|
4208 | }
|
---|
4209 | }
|
---|
4210 | else {
|
---|
4211 | // non-APEX fs data
|
---|
4212 | // sky scan
|
---|
4213 | STSelector sel = STSelector() ;
|
---|
4214 | types.push_back( SrcType::SKY ) ;
|
---|
4215 | sel.setTypes( types ) ;
|
---|
4216 | s->setSelection( sel ) ;
|
---|
4217 | vector< CountedPtr<Scantable> > tmp( 1, getScantable( s, false ) ) ;
|
---|
4218 | CountedPtr<Scantable> asky = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
4219 | s->unsetSelection() ;
|
---|
4220 | sel.reset() ;
|
---|
4221 | types.clear() ;
|
---|
4222 |
|
---|
4223 | // hot scan
|
---|
4224 | types.push_back( SrcType::HOT ) ;
|
---|
4225 | sel.setTypes( types ) ;
|
---|
4226 | s->setSelection( sel ) ;
|
---|
4227 | tmp.clear() ;
|
---|
4228 | tmp.push_back( getScantable( s, false ) ) ;
|
---|
4229 | CountedPtr<Scantable> ahot = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
4230 | s->unsetSelection() ;
|
---|
4231 | sel.reset() ;
|
---|
4232 | types.clear() ;
|
---|
4233 |
|
---|
4234 | // cold scan
|
---|
4235 | CountedPtr<Scantable> acold ;
|
---|
4236 | // types.push_back( SrcType::COLD ) ;
|
---|
4237 | // sel.setTypes( types ) ;
|
---|
4238 | // s->setSelection( sel ) ;
|
---|
4239 | // tmp.clear() ;
|
---|
4240 | // tmp.push_back( getScantable( s, false ) ) ;
|
---|
4241 | // CountedPtr<Scantable> acold = average( tmp, masks, "TINT", "SCAN" ) ;
|
---|
4242 | // s->unsetSelection() ;
|
---|
4243 | // sel.reset() ;
|
---|
4244 | // types.clear() ;
|
---|
4245 |
|
---|
4246 | // ref scan
|
---|
4247 | bool insitu = insitu_ ;
|
---|
4248 | insitu_ = false ;
|
---|
4249 | sref = getScantable( s, true ) ;
|
---|
4250 | insitu_ = insitu ;
|
---|
4251 | types.push_back( SrcType::FSOFF ) ;
|
---|
4252 | sel.setTypes( types ) ;
|
---|
4253 | s->setSelection( sel ) ;
|
---|
4254 | TableCopy::copyRows( sref->table(), s->table() ) ;
|
---|
4255 | s->unsetSelection() ;
|
---|
4256 | sel.reset() ;
|
---|
4257 | types.clear() ;
|
---|
4258 |
|
---|
4259 | // sig scan
|
---|
4260 | insitu_ = false ;
|
---|
4261 | ssig = getScantable( s, true ) ;
|
---|
4262 | insitu_ = insitu ;
|
---|
4263 | types.push_back( SrcType::FSON ) ;
|
---|
4264 | sel.setTypes( types ) ;
|
---|
4265 | s->setSelection( sel ) ;
|
---|
4266 | TableCopy::copyRows( ssig->table(), s->table() ) ;
|
---|
4267 | s->unsetSelection() ;
|
---|
4268 | sel.reset() ;
|
---|
4269 | types.clear() ;
|
---|
4270 |
|
---|
4271 | // process each sig and ref scan
|
---|
4272 | ArrayColumn<Float> tsysColsig ;
|
---|
4273 | tsysColsig.attach( ssig->table(), "TSYS" ) ;
|
---|
4274 | ArrayColumn<Float> tsysColref ;
|
---|
4275 | tsysColref.attach( ssig->table(), "TSYS" ) ;
|
---|
4276 | for ( int i = 0 ; i < ssig->nrow() ; i++ ) {
|
---|
4277 | vector<float> sp = getFSCalibratedSpectra( ssig, sref, asky, ahot, acold, i ) ;
|
---|
4278 | ssig->setSpectrum( sp, i ) ;
|
---|
4279 | string reftime = ssig->getTime( i ) ;
|
---|
4280 | vector<int> ii( 1, ssig->getIF( i ) ) ;
|
---|
4281 | vector<int> ib( 1, ssig->getBeam( i ) ) ;
|
---|
4282 | vector<int> ip( 1, ssig->getPol( i ) ) ;
|
---|
4283 | sel.setIFs( ii ) ;
|
---|
4284 | sel.setBeams( ib ) ;
|
---|
4285 | sel.setPolarizations( ip ) ;
|
---|
4286 | asky->setSelection( sel ) ;
|
---|
4287 | vector<float> sptsys = getTsysFromTime( reftime, asky, "linear" ) ;
|
---|
4288 | const Vector<Float> Vtsys( sptsys ) ;
|
---|
4289 | tsysColsig.put( i, Vtsys ) ;
|
---|
4290 | asky->unsetSelection() ;
|
---|
4291 | sel.reset() ;
|
---|
4292 | sp = getFSCalibratedSpectra( sref, ssig, asky, ahot, acold, i ) ;
|
---|
4293 | sref->setSpectrum( sp, i ) ;
|
---|
4294 | tsysColref.put( i, Vtsys ) ;
|
---|
4295 | }
|
---|
4296 | }
|
---|
4297 |
|
---|
4298 | // do folding if necessary
|
---|
4299 | Table sigtab = ssig->table() ;
|
---|
4300 | Table reftab = sref->table() ;
|
---|
4301 | ScalarColumn<uInt> sigifnoCol ;
|
---|
4302 | ScalarColumn<uInt> refifnoCol ;
|
---|
4303 | ScalarColumn<uInt> sigfidCol ;
|
---|
4304 | ScalarColumn<uInt> reffidCol ;
|
---|
4305 | Int nchan = (Int)ssig->nchan() ;
|
---|
4306 | sigifnoCol.attach( sigtab, "IFNO" ) ;
|
---|
4307 | refifnoCol.attach( reftab, "IFNO" ) ;
|
---|
4308 | sigfidCol.attach( sigtab, "FREQ_ID" ) ;
|
---|
4309 | reffidCol.attach( reftab, "FREQ_ID" ) ;
|
---|
4310 | Vector<uInt> sfids( sigfidCol.getColumn() ) ;
|
---|
4311 | Vector<uInt> rfids( reffidCol.getColumn() ) ;
|
---|
4312 | vector<uInt> sfids_unique ;
|
---|
4313 | vector<uInt> rfids_unique ;
|
---|
4314 | vector<uInt> sifno_unique ;
|
---|
4315 | vector<uInt> rifno_unique ;
|
---|
4316 | for ( uInt i = 0 ; i < sfids.nelements() ; i++ ) {
|
---|
4317 | if ( count( sfids_unique.begin(), sfids_unique.end(), sfids[i] ) == 0 ) {
|
---|
4318 | sfids_unique.push_back( sfids[i] ) ;
|
---|
4319 | sifno_unique.push_back( ssig->getIF( i ) ) ;
|
---|
4320 | }
|
---|
4321 | if ( count( rfids_unique.begin(), rfids_unique.end(), rfids[i] ) == 0 ) {
|
---|
4322 | rfids_unique.push_back( rfids[i] ) ;
|
---|
4323 | rifno_unique.push_back( sref->getIF( i ) ) ;
|
---|
4324 | }
|
---|
4325 | }
|
---|
4326 | double refpix_sig, refval_sig, increment_sig ;
|
---|
4327 | double refpix_ref, refval_ref, increment_ref ;
|
---|
4328 | vector< CountedPtr<Scantable> > tmp( sfids_unique.size() ) ;
|
---|
4329 | for ( uInt i = 0 ; i < sfids_unique.size() ; i++ ) {
|
---|
4330 | ssig->frequencies().getEntry( refpix_sig, refval_sig, increment_sig, sfids_unique[i] ) ;
|
---|
4331 | sref->frequencies().getEntry( refpix_ref, refval_ref, increment_ref, rfids_unique[i] ) ;
|
---|
4332 | if ( refpix_sig == refpix_ref ) {
|
---|
4333 | double foffset = refval_ref - refval_sig ;
|
---|
4334 | int choffset = static_cast<int>(foffset/increment_sig) ;
|
---|
4335 | double doffset = foffset / increment_sig ;
|
---|
4336 | if ( abs(choffset) >= nchan ) {
|
---|
4337 | LogIO os( LogOrigin( "STMath", "cwcalfs", WHERE ) ) ;
|
---|
4338 | os << "FREQ_ID=[" << sfids_unique[i] << "," << rfids_unique[i] << "]: out-band frequency switching, no folding" << LogIO::POST ;
|
---|
4339 | os << "Just return signal data" << LogIO::POST ;
|
---|
4340 | //std::vector< CountedPtr<Scantable> > tabs ;
|
---|
4341 | //tabs.push_back( ssig ) ;
|
---|
4342 | //tabs.push_back( sref ) ;
|
---|
4343 | //out = merge( tabs ) ;
|
---|
4344 | tmp[i] = ssig ;
|
---|
4345 | }
|
---|
4346 | else {
|
---|
4347 | STSelector sel = STSelector() ;
|
---|
4348 | vector<int> v( 1, sifno_unique[i] ) ;
|
---|
4349 | sel.setIFs( v ) ;
|
---|
4350 | ssig->setSelection( sel ) ;
|
---|
4351 | sel.reset() ;
|
---|
4352 | v[0] = rifno_unique[i] ;
|
---|
4353 | sel.setIFs( v ) ;
|
---|
4354 | sref->setSelection( sel ) ;
|
---|
4355 | sel.reset() ;
|
---|
4356 | if ( antname.find( "APEX" ) != string::npos ) {
|
---|
4357 | tmp[i] = dofold( ssig, sref, 0.5*doffset, -0.5*doffset ) ;
|
---|
4358 | //tmp[i] = dofold( ssig, sref, doffset ) ;
|
---|
4359 | }
|
---|
4360 | else {
|
---|
4361 | tmp[i] = dofold( ssig, sref, doffset ) ;
|
---|
4362 | }
|
---|
4363 | ssig->unsetSelection() ;
|
---|
4364 | sref->unsetSelection() ;
|
---|
4365 | }
|
---|
4366 | }
|
---|
4367 | }
|
---|
4368 |
|
---|
4369 | if ( tmp.size() > 1 ) {
|
---|
4370 | out = merge( tmp ) ;
|
---|
4371 | }
|
---|
4372 | else {
|
---|
4373 | out = tmp[0] ;
|
---|
4374 | }
|
---|
4375 |
|
---|
4376 | // flux unit
|
---|
4377 | out->setFluxUnit( "K" ) ;
|
---|
4378 |
|
---|
4379 | return out ;
|
---|
4380 | }
|
---|
4381 |
|
---|
4382 | CountedPtr<Scantable> STMath::almacalfs( const CountedPtr<Scantable>& s )
|
---|
4383 | {
|
---|
4384 | (void) s; //currently unused
|
---|
4385 | CountedPtr<Scantable> out ;
|
---|
4386 |
|
---|
4387 | return out ;
|
---|
4388 | }
|
---|
4389 |
|
---|
4390 | vector<float> STMath::getSpectrumFromTime( string reftime,
|
---|
4391 | CountedPtr<Scantable>& s,
|
---|
4392 | string mode )
|
---|
4393 | {
|
---|
4394 | LogIO os( LogOrigin( "STMath", "getSpectrumFromTime", WHERE ) ) ;
|
---|
4395 | vector<float> sp ;
|
---|
4396 |
|
---|
4397 | if ( s->nrow() == 0 ) {
|
---|
4398 | os << LogIO::SEVERE << "No spectra in the input scantable. Return empty spectrum." << LogIO::POST ;
|
---|
4399 | return sp ;
|
---|
4400 | }
|
---|
4401 | else if ( s->nrow() == 1 ) {
|
---|
4402 | //os << "use row " << 0 << " (scanno = " << s->getScan( 0 ) << ")" << LogIO::POST ;
|
---|
4403 | return s->getSpectrum( 0 ) ;
|
---|
4404 | }
|
---|
4405 | else {
|
---|
4406 | vector<int> idx = getRowIdFromTime( reftime, s ) ;
|
---|
4407 | if ( mode == "before" ) {
|
---|
4408 | int id = -1 ;
|
---|
4409 | if ( idx[0] != -1 ) {
|
---|
4410 | id = idx[0] ;
|
---|
4411 | }
|
---|
4412 | else if ( idx[1] != -1 ) {
|
---|
4413 | os << LogIO::WARN << "Failed to find a scan before reftime. return a spectrum just after the reftime." << LogIO::POST ;
|
---|
4414 | id = idx[1] ;
|
---|
4415 | }
|
---|
4416 | //os << "use row " << id << " (scanno = " << s->getScan( id ) << ")" << LogIO::POST ;
|
---|
4417 | sp = s->getSpectrum( id ) ;
|
---|
4418 | }
|
---|
4419 | else if ( mode == "after" ) {
|
---|
4420 | int id = -1 ;
|
---|
4421 | if ( idx[1] != -1 ) {
|
---|
4422 | id = idx[1] ;
|
---|
4423 | }
|
---|
4424 | else if ( idx[0] != -1 ) {
|
---|
4425 | os << LogIO::WARN << "Failed to find a scan after reftime. return a spectrum just before the reftime." << LogIO::POST ;
|
---|
4426 | id = idx[1] ;
|
---|
4427 | }
|
---|
4428 | //os << "use row " << id << " (scanno = " << s->getScan( id ) << ")" << LogIO::POST ;
|
---|
4429 | sp = s->getSpectrum( id ) ;
|
---|
4430 | }
|
---|
4431 | else if ( mode == "nearest" ) {
|
---|
4432 | int id = -1 ;
|
---|
4433 | if ( idx[0] == -1 ) {
|
---|
4434 | id = idx[1] ;
|
---|
4435 | }
|
---|
4436 | else if ( idx[1] == -1 ) {
|
---|
4437 | id = idx[0] ;
|
---|
4438 | }
|
---|
4439 | else if ( idx[0] == idx[1] ) {
|
---|
4440 | id = idx[0] ;
|
---|
4441 | }
|
---|
4442 | else {
|
---|
4443 | //double t0 = getMJD( s->getTime( idx[0] ) ) ;
|
---|
4444 | //double t1 = getMJD( s->getTime( idx[1] ) ) ;
|
---|
4445 | double t0 = s->getEpoch( idx[0] ).get( Unit( "d" ) ).getValue() ;
|
---|
4446 | double t1 = s->getEpoch( idx[1] ).get( Unit( "d" ) ).getValue() ;
|
---|
4447 | double tref = getMJD( reftime ) ;
|
---|
4448 | if ( abs( t0 - tref ) > abs( t1 - tref ) ) {
|
---|
4449 | id = idx[1] ;
|
---|
4450 | }
|
---|
4451 | else {
|
---|
4452 | id = idx[0] ;
|
---|
4453 | }
|
---|
4454 | }
|
---|
4455 | //os << "use row " << id << " (scanno = " << s->getScan( id ) << ")" << LogIO::POST ;
|
---|
4456 | sp = s->getSpectrum( id ) ;
|
---|
4457 | }
|
---|
4458 | else if ( mode == "linear" ) {
|
---|
4459 | if ( idx[0] == -1 ) {
|
---|
4460 | // use after
|
---|
4461 | os << LogIO::WARN << "Failed to interpolate. return a spectrum just after the reftime." << LogIO::POST ;
|
---|
4462 | int id = idx[1] ;
|
---|
4463 | //os << "use row " << id << " (scanno = " << s->getScan( id ) << ")" << LogIO::POST ;
|
---|
4464 | sp = s->getSpectrum( id ) ;
|
---|
4465 | }
|
---|
4466 | else if ( idx[1] == -1 ) {
|
---|
4467 | // use before
|
---|
4468 | os << LogIO::WARN << "Failed to interpolate. return a spectrum just before the reftime." << LogIO::POST ;
|
---|
4469 | int id = idx[0] ;
|
---|
4470 | //os << "use row " << id << " (scanno = " << s->getScan( id ) << ")" << LogIO::POST ;
|
---|
4471 | sp = s->getSpectrum( id ) ;
|
---|
4472 | }
|
---|
4473 | else if ( idx[0] == idx[1] ) {
|
---|
4474 | // use before
|
---|
4475 | //os << "No need to interporate." << LogIO::POST ;
|
---|
4476 | int id = idx[0] ;
|
---|
4477 | //os << "use row " << id << " (scanno = " << s->getScan( id ) << ")" << LogIO::POST ;
|
---|
4478 | sp = s->getSpectrum( id ) ;
|
---|
4479 | }
|
---|
4480 | else {
|
---|
4481 | // do interpolation
|
---|
4482 | //os << "interpolate between " << idx[0] << " and " << idx[1] << " (scanno: " << s->getScan( idx[0] ) << ", " << s->getScan( idx[1] ) << ")" << LogIO::POST ;
|
---|
4483 | //double t0 = getMJD( s->getTime( idx[0] ) ) ;
|
---|
4484 | //double t1 = getMJD( s->getTime( idx[1] ) ) ;
|
---|
4485 | double t0 = s->getEpoch( idx[0] ).get( Unit( "d" ) ).getValue() ;
|
---|
4486 | double t1 = s->getEpoch( idx[1] ).get( Unit( "d" ) ).getValue() ;
|
---|
4487 | double tref = getMJD( reftime ) ;
|
---|
4488 | vector<float> sp0 = s->getSpectrum( idx[0] ) ;
|
---|
4489 | vector<float> sp1 = s->getSpectrum( idx[1] ) ;
|
---|
4490 | for ( unsigned int i = 0 ; i < sp0.size() ; i++ ) {
|
---|
4491 | float v = ( sp1[i] - sp0[i] ) / ( t1 - t0 ) * ( tref - t0 ) + sp0[i] ;
|
---|
4492 | sp.push_back( v ) ;
|
---|
4493 | }
|
---|
4494 | }
|
---|
4495 | }
|
---|
4496 | else {
|
---|
4497 | os << LogIO::SEVERE << "Unknown mode" << LogIO::POST ;
|
---|
4498 | }
|
---|
4499 | return sp ;
|
---|
4500 | }
|
---|
4501 | }
|
---|
4502 |
|
---|
4503 | double STMath::getMJD( string strtime )
|
---|
4504 | {
|
---|
4505 | if ( strtime.find("/") == string::npos ) {
|
---|
4506 | // MJD time string
|
---|
4507 | return atof( strtime.c_str() ) ;
|
---|
4508 | }
|
---|
4509 | else {
|
---|
4510 | // string in YYYY/MM/DD/HH:MM:SS format
|
---|
4511 | uInt year = atoi( strtime.substr( 0, 4 ).c_str() ) ;
|
---|
4512 | uInt month = atoi( strtime.substr( 5, 2 ).c_str() ) ;
|
---|
4513 | uInt day = atoi( strtime.substr( 8, 2 ).c_str() ) ;
|
---|
4514 | uInt hour = atoi( strtime.substr( 11, 2 ).c_str() ) ;
|
---|
4515 | uInt minute = atoi( strtime.substr( 14, 2 ).c_str() ) ;
|
---|
4516 | uInt sec = atoi( strtime.substr( 17, 2 ).c_str() ) ;
|
---|
4517 | Time t( year, month, day, hour, minute, sec ) ;
|
---|
4518 | return t.modifiedJulianDay() ;
|
---|
4519 | }
|
---|
4520 | }
|
---|
4521 |
|
---|
4522 | vector<int> STMath::getRowIdFromTime( string reftime, CountedPtr<Scantable> &s )
|
---|
4523 | {
|
---|
4524 | double reft = getMJD( reftime ) ;
|
---|
4525 | double dtmin = 1.0e100 ;
|
---|
4526 | double dtmax = -1.0e100 ;
|
---|
4527 | vector<double> dt ;
|
---|
4528 | int just_before = -1 ;
|
---|
4529 | int just_after = -1 ;
|
---|
4530 | for ( int i = 0 ; i < s->nrow() ; i++ ) {
|
---|
4531 | dt.push_back( getMJD( s->getTime( i ) ) - reft ) ;
|
---|
4532 | }
|
---|
4533 | for ( unsigned int i = 0 ; i < dt.size() ; i++ ) {
|
---|
4534 | if ( dt[i] > 0.0 ) {
|
---|
4535 | // after reftime
|
---|
4536 | if ( dt[i] < dtmin ) {
|
---|
4537 | just_after = i ;
|
---|
4538 | dtmin = dt[i] ;
|
---|
4539 | }
|
---|
4540 | }
|
---|
4541 | else if ( dt[i] < 0.0 ) {
|
---|
4542 | // before reftime
|
---|
4543 | if ( dt[i] > dtmax ) {
|
---|
4544 | just_before = i ;
|
---|
4545 | dtmax = dt[i] ;
|
---|
4546 | }
|
---|
4547 | }
|
---|
4548 | else {
|
---|
4549 | // just a reftime
|
---|
4550 | just_before = i ;
|
---|
4551 | just_after = i ;
|
---|
4552 | dtmax = 0 ;
|
---|
4553 | dtmin = 0 ;
|
---|
4554 | break ;
|
---|
4555 | }
|
---|
4556 | }
|
---|
4557 |
|
---|
4558 | vector<int> v ;
|
---|
4559 | v.push_back( just_before ) ;
|
---|
4560 | v.push_back( just_after ) ;
|
---|
4561 |
|
---|
4562 | return v ;
|
---|
4563 | }
|
---|
4564 |
|
---|
4565 | vector<float> STMath::getTcalFromTime( string reftime,
|
---|
4566 | CountedPtr<Scantable>& s,
|
---|
4567 | string mode )
|
---|
4568 | {
|
---|
4569 | LogIO os( LogOrigin( "STMath", "getTcalFromTime", WHERE ) ) ;
|
---|
4570 | vector<float> tcal ;
|
---|
4571 | STTcal tcalTable = s->tcal() ;
|
---|
4572 | String time ;
|
---|
4573 | Vector<Float> tcalval ;
|
---|
4574 | if ( s->nrow() == 0 ) {
|
---|
4575 | os << LogIO::SEVERE << "No row in the input scantable. Return empty tcal." << LogIO::POST ;
|
---|
4576 | return tcal ;
|
---|
4577 | }
|
---|
4578 | else if ( s->nrow() == 1 ) {
|
---|
4579 | uInt tcalid = s->getTcalId( 0 ) ;
|
---|
4580 | //os << "use row " << 0 << " (tcalid = " << tcalid << ")" << LogIO::POST ;
|
---|
4581 | tcalTable.getEntry( time, tcalval, tcalid ) ;
|
---|
4582 | tcalval.tovector( tcal ) ;
|
---|
4583 | return tcal ;
|
---|
4584 | }
|
---|
4585 | else {
|
---|
4586 | vector<int> idx = getRowIdFromTime( reftime, s ) ;
|
---|
4587 | if ( mode == "before" ) {
|
---|
4588 | int id = -1 ;
|
---|
4589 | if ( idx[0] != -1 ) {
|
---|
4590 | id = idx[0] ;
|
---|
4591 | }
|
---|
4592 | else if ( idx[1] != -1 ) {
|
---|
4593 | os << LogIO::WARN << "Failed to find a scan before reftime. return a spectrum just after the reftime." << LogIO::POST ;
|
---|
4594 | id = idx[1] ;
|
---|
4595 | }
|
---|
4596 | uInt tcalid = s->getTcalId( id ) ;
|
---|
4597 | //os << "use row " << id << " (tcalid = " << tcalid << ")" << LogIO::POST ;
|
---|
4598 | tcalTable.getEntry( time, tcalval, tcalid ) ;
|
---|
4599 | tcalval.tovector( tcal ) ;
|
---|
4600 | }
|
---|
4601 | else if ( mode == "after" ) {
|
---|
4602 | int id = -1 ;
|
---|
4603 | if ( idx[1] != -1 ) {
|
---|
4604 | id = idx[1] ;
|
---|
4605 | }
|
---|
4606 | else if ( idx[0] != -1 ) {
|
---|
4607 | os << LogIO::WARN << "Failed to find a scan after reftime. return a spectrum just before the reftime." << LogIO::POST ;
|
---|
4608 | id = idx[1] ;
|
---|
4609 | }
|
---|
4610 | uInt tcalid = s->getTcalId( id ) ;
|
---|
4611 | //os << "use row " << id << " (tcalid = " << tcalid << ")" << LogIO::POST ;
|
---|
4612 | tcalTable.getEntry( time, tcalval, tcalid ) ;
|
---|
4613 | tcalval.tovector( tcal ) ;
|
---|
4614 | }
|
---|
4615 | else if ( mode == "nearest" ) {
|
---|
4616 | int id = -1 ;
|
---|
4617 | if ( idx[0] == -1 ) {
|
---|
4618 | id = idx[1] ;
|
---|
4619 | }
|
---|
4620 | else if ( idx[1] == -1 ) {
|
---|
4621 | id = idx[0] ;
|
---|
4622 | }
|
---|
4623 | else if ( idx[0] == idx[1] ) {
|
---|
4624 | id = idx[0] ;
|
---|
4625 | }
|
---|
4626 | else {
|
---|
4627 | //double t0 = getMJD( s->getTime( idx[0] ) ) ;
|
---|
4628 | //double t1 = getMJD( s->getTime( idx[1] ) ) ;
|
---|
4629 | double t0 = s->getEpoch( idx[0] ).get( Unit( "d" ) ).getValue() ;
|
---|
4630 | double t1 = s->getEpoch( idx[1] ).get( Unit( "d" ) ).getValue() ;
|
---|
4631 | double tref = getMJD( reftime ) ;
|
---|
4632 | if ( abs( t0 - tref ) > abs( t1 - tref ) ) {
|
---|
4633 | id = idx[1] ;
|
---|
4634 | }
|
---|
4635 | else {
|
---|
4636 | id = idx[0] ;
|
---|
4637 | }
|
---|
4638 | }
|
---|
4639 | uInt tcalid = s->getTcalId( id ) ;
|
---|
4640 | //os << "use row " << id << " (tcalid = " << tcalid << ")" << LogIO::POST ;
|
---|
4641 | tcalTable.getEntry( time, tcalval, tcalid ) ;
|
---|
4642 | tcalval.tovector( tcal ) ;
|
---|
4643 | }
|
---|
4644 | else if ( mode == "linear" ) {
|
---|
4645 | if ( idx[0] == -1 ) {
|
---|
4646 | // use after
|
---|
4647 | os << LogIO::WARN << "Failed to interpolate. return a spectrum just after the reftime." << LogIO::POST ;
|
---|
4648 | int id = idx[1] ;
|
---|
4649 | uInt tcalid = s->getTcalId( id ) ;
|
---|
4650 | //os << "use row " << id << " (tcalid = " << tcalid << ")" << LogIO::POST ;
|
---|
4651 | tcalTable.getEntry( time, tcalval, tcalid ) ;
|
---|
4652 | tcalval.tovector( tcal ) ;
|
---|
4653 | }
|
---|
4654 | else if ( idx[1] == -1 ) {
|
---|
4655 | // use before
|
---|
4656 | os << LogIO::WARN << "Failed to interpolate. return a spectrum just before the reftime." << LogIO::POST ;
|
---|
4657 | int id = idx[0] ;
|
---|
4658 | uInt tcalid = s->getTcalId( id ) ;
|
---|
4659 | //os << "use row " << id << " (tcalid = " << tcalid << ")" << LogIO::POST ;
|
---|
4660 | tcalTable.getEntry( time, tcalval, tcalid ) ;
|
---|
4661 | tcalval.tovector( tcal ) ;
|
---|
4662 | }
|
---|
4663 | else if ( idx[0] == idx[1] ) {
|
---|
4664 | // use before
|
---|
4665 | //os << "No need to interporate." << LogIO::POST ;
|
---|
4666 | int id = idx[0] ;
|
---|
4667 | uInt tcalid = s->getTcalId( id ) ;
|
---|
4668 | //os << "use row " << id << " (tcalid = " << tcalid << ")" << LogIO::POST ;
|
---|
4669 | tcalTable.getEntry( time, tcalval, tcalid ) ;
|
---|
4670 | tcalval.tovector( tcal ) ;
|
---|
4671 | }
|
---|
4672 | else {
|
---|
4673 | // do interpolation
|
---|
4674 | //os << "interpolate between " << idx[0] << " and " << idx[1] << " (scanno: " << s->getScan( idx[0] ) << ", " << s->getScan( idx[1] ) << ")" << LogIO::POST ;
|
---|
4675 | //double t0 = getMJD( s->getTime( idx[0] ) ) ;
|
---|
4676 | //double t1 = getMJD( s->getTime( idx[1] ) ) ;
|
---|
4677 | double t0 = s->getEpoch( idx[0] ).get( Unit( "d" ) ).getValue() ;
|
---|
4678 | double t1 = s->getEpoch( idx[1] ).get( Unit( "d" ) ).getValue() ;
|
---|
4679 | double tref = getMJD( reftime ) ;
|
---|
4680 | vector<float> tcal0 ;
|
---|
4681 | vector<float> tcal1 ;
|
---|
4682 | uInt tcalid0 = s->getTcalId( idx[0] ) ;
|
---|
4683 | uInt tcalid1 = s->getTcalId( idx[1] ) ;
|
---|
4684 | tcalTable.getEntry( time, tcalval, tcalid0 ) ;
|
---|
4685 | tcalval.tovector( tcal0 ) ;
|
---|
4686 | tcalTable.getEntry( time, tcalval, tcalid1 ) ;
|
---|
4687 | tcalval.tovector( tcal1 ) ;
|
---|
4688 | for ( unsigned int i = 0 ; i < tcal0.size() ; i++ ) {
|
---|
4689 | float v = ( tcal1[i] - tcal0[i] ) / ( t1 - t0 ) * ( tref - t0 ) + tcal0[i] ;
|
---|
4690 | tcal.push_back( v ) ;
|
---|
4691 | }
|
---|
4692 | }
|
---|
4693 | }
|
---|
4694 | else {
|
---|
4695 | os << LogIO::SEVERE << "Unknown mode" << LogIO::POST ;
|
---|
4696 | }
|
---|
4697 | return tcal ;
|
---|
4698 | }
|
---|
4699 | }
|
---|
4700 |
|
---|
4701 | vector<float> STMath::getTsysFromTime( string reftime,
|
---|
4702 | CountedPtr<Scantable>& s,
|
---|
4703 | string mode )
|
---|
4704 | {
|
---|
4705 | LogIO os( LogOrigin( "STMath", "getTsysFromTime", WHERE ) ) ;
|
---|
4706 | ArrayColumn<Float> tsysCol ;
|
---|
4707 | tsysCol.attach( s->table(), "TSYS" ) ;
|
---|
4708 | vector<float> tsys ;
|
---|
4709 | String time ;
|
---|
4710 | Vector<Float> tsysval ;
|
---|
4711 | if ( s->nrow() == 0 ) {
|
---|
4712 | os << LogIO::SEVERE << "No row in the input scantable. Return empty tsys." << LogIO::POST ;
|
---|
4713 | return tsys ;
|
---|
4714 | }
|
---|
4715 | else if ( s->nrow() == 1 ) {
|
---|
4716 | //os << "use row " << 0 << LogIO::POST ;
|
---|
4717 | tsysval = tsysCol( 0 ) ;
|
---|
4718 | tsysval.tovector( tsys ) ;
|
---|
4719 | return tsys ;
|
---|
4720 | }
|
---|
4721 | else {
|
---|
4722 | vector<int> idx = getRowIdFromTime( reftime, s ) ;
|
---|
4723 | if ( mode == "before" ) {
|
---|
4724 | int id = -1 ;
|
---|
4725 | if ( idx[0] != -1 ) {
|
---|
4726 | id = idx[0] ;
|
---|
4727 | }
|
---|
4728 | else if ( idx[1] != -1 ) {
|
---|
4729 | os << LogIO::WARN << "Failed to find a scan before reftime. return a spectrum just after the reftime." << LogIO::POST ;
|
---|
4730 | id = idx[1] ;
|
---|
4731 | }
|
---|
4732 | //os << "use row " << id << LogIO::POST ;
|
---|
4733 | tsysval = tsysCol( id ) ;
|
---|
4734 | tsysval.tovector( tsys ) ;
|
---|
4735 | }
|
---|
4736 | else if ( mode == "after" ) {
|
---|
4737 | int id = -1 ;
|
---|
4738 | if ( idx[1] != -1 ) {
|
---|
4739 | id = idx[1] ;
|
---|
4740 | }
|
---|
4741 | else if ( idx[0] != -1 ) {
|
---|
4742 | os << LogIO::WARN << "Failed to find a scan after reftime. return a spectrum just before the reftime." << LogIO::POST ;
|
---|
4743 | id = idx[1] ;
|
---|
4744 | }
|
---|
4745 | //os << "use row " << id << LogIO::POST ;
|
---|
4746 | tsysval = tsysCol( id ) ;
|
---|
4747 | tsysval.tovector( tsys ) ;
|
---|
4748 | }
|
---|
4749 | else if ( mode == "nearest" ) {
|
---|
4750 | int id = -1 ;
|
---|
4751 | if ( idx[0] == -1 ) {
|
---|
4752 | id = idx[1] ;
|
---|
4753 | }
|
---|
4754 | else if ( idx[1] == -1 ) {
|
---|
4755 | id = idx[0] ;
|
---|
4756 | }
|
---|
4757 | else if ( idx[0] == idx[1] ) {
|
---|
4758 | id = idx[0] ;
|
---|
4759 | }
|
---|
4760 | else {
|
---|
4761 | //double t0 = getMJD( s->getTime( idx[0] ) ) ;
|
---|
4762 | //double t1 = getMJD( s->getTime( idx[1] ) ) ;
|
---|
4763 | double t0 = s->getEpoch( idx[0] ).get( Unit( "d" ) ).getValue() ;
|
---|
4764 | double t1 = s->getEpoch( idx[1] ).get( Unit( "d" ) ).getValue() ;
|
---|
4765 | double tref = getMJD( reftime ) ;
|
---|
4766 | if ( abs( t0 - tref ) > abs( t1 - tref ) ) {
|
---|
4767 | id = idx[1] ;
|
---|
4768 | }
|
---|
4769 | else {
|
---|
4770 | id = idx[0] ;
|
---|
4771 | }
|
---|
4772 | }
|
---|
4773 | //os << "use row " << id << LogIO::POST ;
|
---|
4774 | tsysval = tsysCol( id ) ;
|
---|
4775 | tsysval.tovector( tsys ) ;
|
---|
4776 | }
|
---|
4777 | else if ( mode == "linear" ) {
|
---|
4778 | if ( idx[0] == -1 ) {
|
---|
4779 | // use after
|
---|
4780 | os << LogIO::WARN << "Failed to interpolate. return a spectrum just after the reftime." << LogIO::POST ;
|
---|
4781 | int id = idx[1] ;
|
---|
4782 | //os << "use row " << id << LogIO::POST ;
|
---|
4783 | tsysval = tsysCol( id ) ;
|
---|
4784 | tsysval.tovector( tsys ) ;
|
---|
4785 | }
|
---|
4786 | else if ( idx[1] == -1 ) {
|
---|
4787 | // use before
|
---|
4788 | os << LogIO::WARN << "Failed to interpolate. return a spectrum just before the reftime." << LogIO::POST ;
|
---|
4789 | int id = idx[0] ;
|
---|
4790 | //os << "use row " << id << LogIO::POST ;
|
---|
4791 | tsysval = tsysCol( id ) ;
|
---|
4792 | tsysval.tovector( tsys ) ;
|
---|
4793 | }
|
---|
4794 | else if ( idx[0] == idx[1] ) {
|
---|
4795 | // use before
|
---|
4796 | //os << "No need to interporate." << LogIO::POST ;
|
---|
4797 | int id = idx[0] ;
|
---|
4798 | //os << "use row " << id << LogIO::POST ;
|
---|
4799 | tsysval = tsysCol( id ) ;
|
---|
4800 | tsysval.tovector( tsys ) ;
|
---|
4801 | }
|
---|
4802 | else {
|
---|
4803 | // do interpolation
|
---|
4804 | //os << "interpolate between " << idx[0] << " and " << idx[1] << " (scanno: " << s->getScan( idx[0] ) << ", " << s->getScan( idx[1] ) << ")" << LogIO::POST ;
|
---|
4805 | //double t0 = getMJD( s->getTime( idx[0] ) ) ;
|
---|
4806 | //double t1 = getMJD( s->getTime( idx[1] ) ) ;
|
---|
4807 | double t0 = s->getEpoch( idx[0] ).get( Unit( "d" ) ).getValue() ;
|
---|
4808 | double t1 = s->getEpoch( idx[1] ).get( Unit( "d" ) ).getValue() ;
|
---|
4809 | double tref = getMJD( reftime ) ;
|
---|
4810 | vector<float> tsys0 ;
|
---|
4811 | vector<float> tsys1 ;
|
---|
4812 | tsysval = tsysCol( idx[0] ) ;
|
---|
4813 | tsysval.tovector( tsys0 ) ;
|
---|
4814 | tsysval = tsysCol( idx[1] ) ;
|
---|
4815 | tsysval.tovector( tsys1 ) ;
|
---|
4816 | for ( unsigned int i = 0 ; i < tsys0.size() ; i++ ) {
|
---|
4817 | float v = ( tsys1[i] - tsys0[i] ) / ( t1 - t0 ) * ( tref - t0 ) + tsys0[i] ;
|
---|
4818 | tsys.push_back( v ) ;
|
---|
4819 | }
|
---|
4820 | }
|
---|
4821 | }
|
---|
4822 | else {
|
---|
4823 | os << LogIO::SEVERE << "Unknown mode" << LogIO::POST ;
|
---|
4824 | }
|
---|
4825 | return tsys ;
|
---|
4826 | }
|
---|
4827 | }
|
---|
4828 |
|
---|
4829 | vector<float> STMath::getCalibratedSpectra( CountedPtr<Scantable>& on,
|
---|
4830 | CountedPtr<Scantable>& off,
|
---|
4831 | CountedPtr<Scantable>& sky,
|
---|
4832 | CountedPtr<Scantable>& hot,
|
---|
4833 | CountedPtr<Scantable>& cold,
|
---|
4834 | int index,
|
---|
4835 | string antname )
|
---|
4836 | {
|
---|
4837 | (void) cold; //currently unused
|
---|
4838 | string reftime = on->getTime( index ) ;
|
---|
4839 | vector<int> ii( 1, on->getIF( index ) ) ;
|
---|
4840 | vector<int> ib( 1, on->getBeam( index ) ) ;
|
---|
4841 | vector<int> ip( 1, on->getPol( index ) ) ;
|
---|
4842 | vector<int> ic( 1, on->getScan( index ) ) ;
|
---|
4843 | STSelector sel = STSelector() ;
|
---|
4844 | sel.setIFs( ii ) ;
|
---|
4845 | sel.setBeams( ib ) ;
|
---|
4846 | sel.setPolarizations( ip ) ;
|
---|
4847 | sky->setSelection( sel ) ;
|
---|
4848 | hot->setSelection( sel ) ;
|
---|
4849 | //cold->setSelection( sel ) ;
|
---|
4850 | off->setSelection( sel ) ;
|
---|
4851 | vector<float> spsky = getSpectrumFromTime( reftime, sky, "linear" ) ;
|
---|
4852 | vector<float> sphot = getSpectrumFromTime( reftime, hot, "linear" ) ;
|
---|
4853 | //vector<float> spcold = getSpectrumFromTime( reftime, cold, "linear" ) ;
|
---|
4854 | vector<float> spoff = getSpectrumFromTime( reftime, off, "linear" ) ;
|
---|
4855 | vector<float> spec = on->getSpectrum( index ) ;
|
---|
4856 | vector<float> tcal = getTcalFromTime( reftime, sky, "linear" ) ;
|
---|
4857 | vector<float> sp( tcal.size() ) ;
|
---|
4858 | if ( antname.find( "APEX" ) != string::npos ) {
|
---|
4859 | // using gain array
|
---|
4860 | for ( unsigned int j = 0 ; j < tcal.size() ; j++ ) {
|
---|
4861 | float v = ( ( spec[j] - spoff[j] ) / spoff[j] )
|
---|
4862 | * ( spsky[j] / ( sphot[j] - spsky[j] ) ) * tcal[j] ;
|
---|
4863 | sp[j] = v ;
|
---|
4864 | }
|
---|
4865 | }
|
---|
4866 | else {
|
---|
4867 | // Chopper-Wheel calibration (Ulich & Haas 1976)
|
---|
4868 | for ( unsigned int j = 0 ; j < tcal.size() ; j++ ) {
|
---|
4869 | float v = ( spec[j] - spoff[j] ) / ( sphot[j] - spsky[j] ) * tcal[j] ;
|
---|
4870 | sp[j] = v ;
|
---|
4871 | }
|
---|
4872 | }
|
---|
4873 | sel.reset() ;
|
---|
4874 | sky->unsetSelection() ;
|
---|
4875 | hot->unsetSelection() ;
|
---|
4876 | //cold->unsetSelection() ;
|
---|
4877 | off->unsetSelection() ;
|
---|
4878 |
|
---|
4879 | return sp ;
|
---|
4880 | }
|
---|
4881 |
|
---|
4882 | vector<float> STMath::getCalibratedSpectra( CountedPtr<Scantable>& on,
|
---|
4883 | CountedPtr<Scantable>& off,
|
---|
4884 | int index )
|
---|
4885 | {
|
---|
4886 | string reftime = on->getTime( index ) ;
|
---|
4887 | vector<int> ii( 1, on->getIF( index ) ) ;
|
---|
4888 | vector<int> ib( 1, on->getBeam( index ) ) ;
|
---|
4889 | vector<int> ip( 1, on->getPol( index ) ) ;
|
---|
4890 | vector<int> ic( 1, on->getScan( index ) ) ;
|
---|
4891 | STSelector sel = STSelector() ;
|
---|
4892 | sel.setIFs( ii ) ;
|
---|
4893 | sel.setBeams( ib ) ;
|
---|
4894 | sel.setPolarizations( ip ) ;
|
---|
4895 | off->setSelection( sel ) ;
|
---|
4896 | vector<float> spoff = getSpectrumFromTime( reftime, off, "linear" ) ;
|
---|
4897 | vector<float> spec = on->getSpectrum( index ) ;
|
---|
4898 | //vector<float> tcal = getTcalFromTime( reftime, sky, "linear" ) ;
|
---|
4899 | //vector<float> tsys = on->getTsysVec( index ) ;
|
---|
4900 | ArrayColumn<Float> tsysCol( on->table(), "TSYS" ) ;
|
---|
4901 | Vector<Float> tsys = tsysCol( index ) ;
|
---|
4902 | vector<float> sp( spec.size() ) ;
|
---|
4903 | // ALMA Calibration
|
---|
4904 | //
|
---|
4905 | // Ta* = Tsys * ( ON - OFF ) / OFF
|
---|
4906 | //
|
---|
4907 | // 2010/01/07 Takeshi Nakazato
|
---|
4908 | unsigned int tsyssize = tsys.nelements() ;
|
---|
4909 | unsigned int spsize = sp.size() ;
|
---|
4910 | for ( unsigned int j = 0 ; j < sp.size() ; j++ ) {
|
---|
4911 | float tscale = 0.0 ;
|
---|
4912 | if ( tsyssize == spsize )
|
---|
4913 | tscale = tsys[j] ;
|
---|
4914 | else
|
---|
4915 | tscale = tsys[0] ;
|
---|
4916 | float v = tscale * ( spec[j] - spoff[j] ) / spoff[j] ;
|
---|
4917 | sp[j] = v ;
|
---|
4918 | }
|
---|
4919 | sel.reset() ;
|
---|
4920 | off->unsetSelection() ;
|
---|
4921 |
|
---|
4922 | return sp ;
|
---|
4923 | }
|
---|
4924 |
|
---|
4925 | vector<float> STMath::getFSCalibratedSpectra( CountedPtr<Scantable>& sig,
|
---|
4926 | CountedPtr<Scantable>& ref,
|
---|
4927 | CountedPtr<Scantable>& sky,
|
---|
4928 | CountedPtr<Scantable>& hot,
|
---|
4929 | CountedPtr<Scantable>& cold,
|
---|
4930 | int index )
|
---|
4931 | {
|
---|
4932 | (void) cold; //currently unused
|
---|
4933 | string reftime = sig->getTime( index ) ;
|
---|
4934 | vector<int> ii( 1, sig->getIF( index ) ) ;
|
---|
4935 | vector<int> ib( 1, sig->getBeam( index ) ) ;
|
---|
4936 | vector<int> ip( 1, sig->getPol( index ) ) ;
|
---|
4937 | vector<int> ic( 1, sig->getScan( index ) ) ;
|
---|
4938 | STSelector sel = STSelector() ;
|
---|
4939 | sel.setIFs( ii ) ;
|
---|
4940 | sel.setBeams( ib ) ;
|
---|
4941 | sel.setPolarizations( ip ) ;
|
---|
4942 | sky->setSelection( sel ) ;
|
---|
4943 | hot->setSelection( sel ) ;
|
---|
4944 | //cold->setSelection( sel ) ;
|
---|
4945 | vector<float> spsky = getSpectrumFromTime( reftime, sky, "linear" ) ;
|
---|
4946 | vector<float> sphot = getSpectrumFromTime( reftime, hot, "linear" ) ;
|
---|
4947 | //vector<float> spcold = getSpectrumFromTime( reftime, cold, "linear" ) ;
|
---|
4948 | vector<float> spref = ref->getSpectrum( index ) ;
|
---|
4949 | vector<float> spsig = sig->getSpectrum( index ) ;
|
---|
4950 | vector<float> tcal = getTcalFromTime( reftime, sky, "linear" ) ;
|
---|
4951 | vector<float> sp( tcal.size() ) ;
|
---|
4952 | for ( unsigned int j = 0 ; j < tcal.size() ; j++ ) {
|
---|
4953 | float v = tcal[j] * spsky[j] / ( sphot[j] - spsky[j] ) * ( spsig[j] - spref[j] ) / spref[j] ;
|
---|
4954 | sp[j] = v ;
|
---|
4955 | }
|
---|
4956 | sel.reset() ;
|
---|
4957 | sky->unsetSelection() ;
|
---|
4958 | hot->unsetSelection() ;
|
---|
4959 | //cold->unsetSelection() ;
|
---|
4960 |
|
---|
4961 | return sp ;
|
---|
4962 | }
|
---|
4963 |
|
---|
4964 | vector<float> STMath::getFSCalibratedSpectra( CountedPtr<Scantable>& sig,
|
---|
4965 | CountedPtr<Scantable>& ref,
|
---|
4966 | vector< CountedPtr<Scantable> >& sky,
|
---|
4967 | vector< CountedPtr<Scantable> >& hot,
|
---|
4968 | vector< CountedPtr<Scantable> >& cold,
|
---|
4969 | int index )
|
---|
4970 | {
|
---|
4971 | (void) cold; //currently unused
|
---|
4972 | string reftime = sig->getTime( index ) ;
|
---|
4973 | vector<int> ii( 1, sig->getIF( index ) ) ;
|
---|
4974 | vector<int> ib( 1, sig->getBeam( index ) ) ;
|
---|
4975 | vector<int> ip( 1, sig->getPol( index ) ) ;
|
---|
4976 | vector<int> ic( 1, sig->getScan( index ) ) ;
|
---|
4977 | STSelector sel = STSelector() ;
|
---|
4978 | sel.setIFs( ii ) ;
|
---|
4979 | sel.setBeams( ib ) ;
|
---|
4980 | sel.setPolarizations( ip ) ;
|
---|
4981 | sky[0]->setSelection( sel ) ;
|
---|
4982 | hot[0]->setSelection( sel ) ;
|
---|
4983 | //cold[0]->setSelection( sel ) ;
|
---|
4984 | vector<float> spskys = getSpectrumFromTime( reftime, sky[0], "linear" ) ;
|
---|
4985 | vector<float> sphots = getSpectrumFromTime( reftime, hot[0], "linear" ) ;
|
---|
4986 | //vector<float> spcolds = getSpectrumFromTime( reftime, cold[0], "linear" ) ;
|
---|
4987 | vector<float> tcals = getTcalFromTime( reftime, sky[0], "linear" ) ;
|
---|
4988 | sel.reset() ;
|
---|
4989 | ii[0] = ref->getIF( index ) ;
|
---|
4990 | sel.setIFs( ii ) ;
|
---|
4991 | sel.setBeams( ib ) ;
|
---|
4992 | sel.setPolarizations( ip ) ;
|
---|
4993 | sky[1]->setSelection( sel ) ;
|
---|
4994 | hot[1]->setSelection( sel ) ;
|
---|
4995 | //cold[1]->setSelection( sel ) ;
|
---|
4996 | vector<float> spskyr = getSpectrumFromTime( reftime, sky[1], "linear" ) ;
|
---|
4997 | vector<float> sphotr = getSpectrumFromTime( reftime, hot[1], "linear" ) ;
|
---|
4998 | //vector<float> spcoldr = getSpectrumFromTime( reftime, cold[1], "linear" ) ;
|
---|
4999 | vector<float> tcalr = getTcalFromTime( reftime, sky[1], "linear" ) ;
|
---|
5000 | vector<float> spref = ref->getSpectrum( index ) ;
|
---|
5001 | vector<float> spsig = sig->getSpectrum( index ) ;
|
---|
5002 | vector<float> sp( tcals.size() ) ;
|
---|
5003 | for ( unsigned int j = 0 ; j < tcals.size() ; j++ ) {
|
---|
5004 | float v = tcals[j] * spsig[j] / ( sphots[j] - spskys[j] ) - tcalr[j] * spref[j] / ( sphotr[j] - spskyr[j] ) ;
|
---|
5005 | sp[j] = v ;
|
---|
5006 | }
|
---|
5007 | sel.reset() ;
|
---|
5008 | sky[0]->unsetSelection() ;
|
---|
5009 | hot[0]->unsetSelection() ;
|
---|
5010 | //cold[0]->unsetSelection() ;
|
---|
5011 | sky[1]->unsetSelection() ;
|
---|
5012 | hot[1]->unsetSelection() ;
|
---|
5013 | //cold[1]->unsetSelection() ;
|
---|
5014 |
|
---|
5015 | return sp ;
|
---|
5016 | }
|
---|