source: trunk/src/STMath.cpp@ 3135

Last change on this file since 3135 was 3106, checked in by Takeshi Nakazato, 8 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes/No

Interface Changes: Yes/No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...


Check-in asap modifications from Jim regarding casacore namespace conversion.

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