source: trunk/src/Scantable.cpp@ 3117

Last change on this file since 3117 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: 172.1 KB
Line 
1//
2// C++ Implementation: Scantable
3//
4// Description:
5//
6//
7// Author: Malte Marquarding <asap@atnf.csiro.au>, (C) 2005-2013
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12
13// ASAP
14// STLineFinder must be included first to avoid compiler warnings related with _XOPEN_SOURCE
15#include "STLineFinder.h"
16#include "MathUtils.h"
17#include "STAttr.h"
18#include "STBaselineTable.h"
19#include "STPolCircular.h"
20#include "STPolLinear.h"
21#include "STPolStokes.h"
22#include "STUpgrade.h"
23#include "STFitter.h"
24#include "Scantable.h"
25
26#include <map>
27#include <sys/time.h>
28#include <iostream>
29
30#include <atnf/PKSIO/SrcType.h>
31
32#include <casa/aips.h>
33#include <casa/iomanip.h>
34#include <casa/iostream.h>
35#include <casa/OS/File.h>
36#include <casa/OS/Path.h>
37#include <casa/Logging/LogIO.h>
38#include <casa/Arrays/Array.h>
39#include <casa/Arrays/ArrayAccessor.h>
40#include <casa/Arrays/ArrayLogical.h>
41#include <casa/Arrays/ArrayMath.h>
42#include <casa/Arrays/MaskArrMath.h>
43#include <casa/Arrays/Slice.h>
44#include <casa/Arrays/Vector.h>
45#include <casa/Arrays/VectorSTLIterator.h>
46#include <casa/BasicMath/Math.h>
47#include <casa/BasicSL/Constants.h>
48#include <casa/Containers/RecordField.h>
49#include <casa/Logging/LogIO.h>
50#include <casa/Quanta/MVAngle.h>
51#include <casa/Quanta/MVTime.h>
52#include <casa/Utilities/GenSort.h>
53#include <casa/Utilities/Assert.h>
54
55#include <coordinates/Coordinates/CoordinateUtil.h>
56
57// needed to avoid error in .tcc
58#include <measures/Measures/MCDirection.h>
59//
60#include <measures/Measures/MDirection.h>
61#include <measures/Measures/MEpoch.h>
62#include <measures/Measures/MFrequency.h>
63#include <measures/Measures/MeasRef.h>
64#include <measures/Measures/MeasTable.h>
65#include <measures/TableMeasures/ScalarMeasColumn.h>
66#include <measures/TableMeasures/TableMeasDesc.h>
67#include <measures/TableMeasures/TableMeasRefDesc.h>
68#include <measures/TableMeasures/TableMeasValueDesc.h>
69
70#include <tables/Tables/ArrColDesc.h>
71#include <tables/TaQL/ExprNode.h>
72#include <tables/Tables/ScaColDesc.h>
73#include <tables/Tables/SetupNewTab.h>
74#include <tables/Tables/TableCopy.h>
75#include <tables/Tables/TableDesc.h>
76#include <tables/Tables/TableIter.h>
77#include <tables/TaQL/TableParse.h>
78#include <tables/Tables/TableRecord.h>
79#include <tables/Tables/TableRow.h>
80#include <tables/Tables/TableVector.h>
81
82#define debug 1
83
84using namespace casacore;
85
86namespace asap {
87
88std::map<std::string, STPol::STPolFactory *> Scantable::factories_;
89
90void Scantable::initFactories() {
91 if ( factories_.empty() ) {
92 Scantable::factories_["linear"] = &STPolLinear::myFactory;
93 Scantable::factories_["circular"] = &STPolCircular::myFactory;
94 Scantable::factories_["stokes"] = &STPolStokes::myFactory;
95 }
96}
97
98Scantable::Scantable(Table::TableType ttype) :
99 type_(ttype),
100 cubicSplineModelPool_()
101{
102 initFactories();
103 setupMainTable();
104 freqTable_ = STFrequencies(*this);
105 table_.rwKeywordSet().defineTable("FREQUENCIES", freqTable_.table());
106 weatherTable_ = STWeather(*this);
107 table_.rwKeywordSet().defineTable("WEATHER", weatherTable_.table());
108 focusTable_ = STFocus(*this);
109 table_.rwKeywordSet().defineTable("FOCUS", focusTable_.table());
110 tcalTable_ = STTcal(*this);
111 table_.rwKeywordSet().defineTable("TCAL", tcalTable_.table());
112 moleculeTable_ = STMolecules(*this);
113 table_.rwKeywordSet().defineTable("MOLECULES", moleculeTable_.table());
114 historyTable_ = STHistory(*this);
115 table_.rwKeywordSet().defineTable("HISTORY", historyTable_.table());
116 fitTable_ = STFit(*this);
117 table_.rwKeywordSet().defineTable("FIT", fitTable_.table());
118 table_.tableInfo().setType( "Scantable" ) ;
119 originalTable_ = table_;
120 attach();
121}
122
123Scantable::Scantable(const std::string& name, Table::TableType ttype) :
124 type_(ttype),
125 cubicSplineModelPool_()
126{
127 initFactories();
128
129 Table tab(name, Table::Update);
130 uInt version = tab.keywordSet().asuInt("VERSION");
131 if (version != version_) {
132 STUpgrade upgrader(version_);
133 LogIO os( LogOrigin( "Scantable" ) ) ;
134 os << LogIO::WARN
135 << name << " data format version " << version
136 << " is deprecated" << endl
137 << "Running upgrade."<< endl
138 << LogIO::POST ;
139 std::string outname = upgrader.upgrade(name);
140 if ( outname != name ) {
141 os << LogIO::WARN
142 << "Data will be loaded from " << outname << " instead of "
143 << name << LogIO::POST ;
144 tab = Table(outname, Table::Update ) ;
145 }
146 }
147 if ( type_ == Table::Memory ) {
148 table_ = tab.copyToMemoryTable(generateName());
149 } else {
150 table_ = tab;
151 }
152 table_.tableInfo().setType( "Scantable" ) ;
153
154 attachSubtables();
155 originalTable_ = table_;
156 attach();
157}
158/*
159Scantable::Scantable(const std::string& name, Table::TableType ttype) :
160 type_(ttype)
161{
162 initFactories();
163 Table tab(name, Table::Update);
164 uInt version = tab.keywordSet().asuInt("VERSION");
165 if (version != version_) {
166 throw(AipsError("Unsupported version of ASAP file."));
167 }
168 if ( type_ == Table::Memory ) {
169 table_ = tab.copyToMemoryTable(generateName());
170 } else {
171 table_ = tab;
172 }
173
174 attachSubtables();
175 originalTable_ = table_;
176 attach();
177}
178*/
179
180Scantable::Scantable( const Scantable& other, bool clear )
181{
182 // with or without data
183 String newname = String(generateName());
184 type_ = other.table_.tableType();
185 if ( other.table_.tableType() == Table::Memory ) {
186 if ( clear ) {
187 table_ = TableCopy::makeEmptyMemoryTable(newname,
188 other.table_, True);
189 } else {
190 table_ = other.table_.copyToMemoryTable(newname);
191 }
192 } else {
193 other.table_.deepCopy(newname, Table::New, False,
194 other.table_.endianFormat(),
195 Bool(clear));
196 table_ = Table(newname, Table::Update);
197 table_.markForDelete();
198 }
199 table_.tableInfo().setType( "Scantable" ) ;
200 /// @todo reindex SCANNO, recompute nbeam, nif, npol
201 if ( clear ) copySubtables(other);
202 attachSubtables();
203 originalTable_ = table_;
204 attach();
205}
206
207void Scantable::copySubtables(const Scantable& other) {
208 Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
209 TableCopy::copyRows(t, other.freqTable_.table());
210 t = table_.rwKeywordSet().asTable("FOCUS");
211 TableCopy::copyRows(t, other.focusTable_.table());
212 t = table_.rwKeywordSet().asTable("WEATHER");
213 TableCopy::copyRows(t, other.weatherTable_.table());
214 t = table_.rwKeywordSet().asTable("TCAL");
215 TableCopy::copyRows(t, other.tcalTable_.table());
216 t = table_.rwKeywordSet().asTable("MOLECULES");
217 TableCopy::copyRows(t, other.moleculeTable_.table());
218 t = table_.rwKeywordSet().asTable("HISTORY");
219 TableCopy::copyRows(t, other.historyTable_.table());
220 t = table_.rwKeywordSet().asTable("FIT");
221 TableCopy::copyRows(t, other.fitTable_.table());
222}
223
224void Scantable::attachSubtables()
225{
226 freqTable_ = STFrequencies(table_);
227 focusTable_ = STFocus(table_);
228 weatherTable_ = STWeather(table_);
229 tcalTable_ = STTcal(table_);
230 moleculeTable_ = STMolecules(table_);
231 historyTable_ = STHistory(table_);
232 fitTable_ = STFit(table_);
233}
234
235Scantable::~Scantable()
236{
237}
238
239void Scantable::setupMainTable()
240{
241 TableDesc td("", "1", TableDesc::Scratch);
242 td.comment() = "An ASAP Scantable";
243 td.rwKeywordSet().define("VERSION", uInt(version_));
244
245 // n Cycles
246 td.addColumn(ScalarColumnDesc<uInt>("SCANNO"));
247 // new index every nBeam x nIF x nPol
248 td.addColumn(ScalarColumnDesc<uInt>("CYCLENO"));
249
250 td.addColumn(ScalarColumnDesc<uInt>("BEAMNO"));
251 td.addColumn(ScalarColumnDesc<uInt>("IFNO"));
252 // linear, circular, stokes
253 td.rwKeywordSet().define("POLTYPE", String("linear"));
254 td.addColumn(ScalarColumnDesc<uInt>("POLNO"));
255
256 td.addColumn(ScalarColumnDesc<uInt>("FREQ_ID"));
257 td.addColumn(ScalarColumnDesc<uInt>("MOLECULE_ID"));
258
259 ScalarColumnDesc<Int> refbeamnoColumn("REFBEAMNO");
260 refbeamnoColumn.setDefault(Int(-1));
261 td.addColumn(refbeamnoColumn);
262
263 ScalarColumnDesc<uInt> flagrowColumn("FLAGROW");
264 flagrowColumn.setDefault(uInt(0));
265 td.addColumn(flagrowColumn);
266
267 td.addColumn(ScalarColumnDesc<Double>("TIME"));
268 TableMeasRefDesc measRef(MEpoch::UTC); // UTC as default
269 TableMeasValueDesc measVal(td, "TIME");
270 TableMeasDesc<MEpoch> mepochCol(measVal, measRef);
271 mepochCol.write(td);
272
273 td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
274
275 td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
276 // Type of source (on=0, off=1, other=-1)
277 ScalarColumnDesc<Int> stypeColumn("SRCTYPE");
278 stypeColumn.setDefault(Int(-1));
279 td.addColumn(stypeColumn);
280 td.addColumn(ScalarColumnDesc<String>("FIELDNAME"));
281
282 //The actual Data Vectors
283 td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
284 td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
285 td.addColumn(ArrayColumnDesc<Float>("TSYS"));
286
287 td.addColumn(ArrayColumnDesc<Double>("DIRECTION",
288 IPosition(1,2),
289 ColumnDesc::Direct));
290 TableMeasRefDesc mdirRef(MDirection::J2000); // default
291 TableMeasValueDesc tmvdMDir(td, "DIRECTION");
292 // the TableMeasDesc gives the column a type
293 TableMeasDesc<MDirection> mdirCol(tmvdMDir, mdirRef);
294 // a uder set table type e.g. GALCTIC, B1950 ...
295 td.rwKeywordSet().define("DIRECTIONREF", String("J2000"));
296 // writing create the measure column
297 mdirCol.write(td);
298 td.addColumn(ScalarColumnDesc<Float>("AZIMUTH"));
299 td.addColumn(ScalarColumnDesc<Float>("ELEVATION"));
300 td.addColumn(ScalarColumnDesc<Float>("OPACITY"));
301
302 td.addColumn(ScalarColumnDesc<uInt>("TCAL_ID"));
303 ScalarColumnDesc<Int> fitColumn("FIT_ID");
304 fitColumn.setDefault(Int(-1));
305 td.addColumn(fitColumn);
306
307 td.addColumn(ScalarColumnDesc<uInt>("FOCUS_ID"));
308 td.addColumn(ScalarColumnDesc<uInt>("WEATHER_ID"));
309
310 // columns which just get dragged along, as they aren't used in asap
311 td.addColumn(ScalarColumnDesc<Double>("SRCVELOCITY"));
312 td.addColumn(ArrayColumnDesc<Double>("SRCPROPERMOTION"));
313 td.addColumn(ArrayColumnDesc<Double>("SRCDIRECTION"));
314 td.addColumn(ArrayColumnDesc<Double>("SCANRATE"));
315
316 td.rwKeywordSet().define("OBSMODE", String(""));
317
318 // Now create Table SetUp from the description.
319 SetupNewTable aNewTab(generateName(), td, Table::Scratch);
320 table_ = Table(aNewTab, type_, 0);
321 originalTable_ = table_;
322}
323
324void Scantable::attach()
325{
326 timeCol_.attach(table_, "TIME");
327 srcnCol_.attach(table_, "SRCNAME");
328 srctCol_.attach(table_, "SRCTYPE");
329 specCol_.attach(table_, "SPECTRA");
330 flagsCol_.attach(table_, "FLAGTRA");
331 tsysCol_.attach(table_, "TSYS");
332 cycleCol_.attach(table_,"CYCLENO");
333 scanCol_.attach(table_, "SCANNO");
334 beamCol_.attach(table_, "BEAMNO");
335 ifCol_.attach(table_, "IFNO");
336 polCol_.attach(table_, "POLNO");
337 integrCol_.attach(table_, "INTERVAL");
338 azCol_.attach(table_, "AZIMUTH");
339 elCol_.attach(table_, "ELEVATION");
340 dirCol_.attach(table_, "DIRECTION");
341 fldnCol_.attach(table_, "FIELDNAME");
342 rbeamCol_.attach(table_, "REFBEAMNO");
343
344 mweatheridCol_.attach(table_,"WEATHER_ID");
345 mfitidCol_.attach(table_,"FIT_ID");
346 mfreqidCol_.attach(table_, "FREQ_ID");
347 mtcalidCol_.attach(table_, "TCAL_ID");
348 mfocusidCol_.attach(table_, "FOCUS_ID");
349 mmolidCol_.attach(table_, "MOLECULE_ID");
350
351 //Add auxiliary column for row-based flagging (CAS-1433 Wataru Kawasaki)
352 attachAuxColumnDef(flagrowCol_, "FLAGROW", 0);
353
354}
355
356template<class T, class T2>
357void Scantable::attachAuxColumnDef(ScalarColumn<T>& col,
358 const String& colName,
359 const T2& defValue)
360{
361 try {
362 col.attach(table_, colName);
363 } catch (TableError& err) {
364 String errMesg = err.getMesg();
365 if (errMesg == "Table column " + colName + " is unknown") {
366 table_.addColumn(ScalarColumnDesc<T>(colName));
367 col.attach(table_, colName);
368 col.fillColumn(static_cast<T>(defValue));
369 } else {
370 throw;
371 }
372 } catch (...) {
373 throw;
374 }
375}
376
377template<class T, class T2>
378void Scantable::attachAuxColumnDef(ArrayColumn<T>& col,
379 const String& colName,
380 const Array<T2>& defValue)
381{
382 try {
383 col.attach(table_, colName);
384 } catch (TableError& err) {
385 String errMesg = err.getMesg();
386 if (errMesg == "Table column " + colName + " is unknown") {
387 table_.addColumn(ArrayColumnDesc<T>(colName));
388 col.attach(table_, colName);
389
390 int size = 0;
391 ArrayIterator<T2>& it = defValue.begin();
392 while (it != defValue.end()) {
393 ++size;
394 ++it;
395 }
396 IPosition ip(1, size);
397 Array<T>& arr(ip);
398 for (int i = 0; i < size; ++i)
399 arr[i] = static_cast<T>(defValue[i]);
400
401 col.fillColumn(arr);
402 } else {
403 throw;
404 }
405 } catch (...) {
406 throw;
407 }
408}
409
410void Scantable::setHeader(const STHeader& sdh)
411{
412 table_.rwKeywordSet().define("nIF", sdh.nif);
413 table_.rwKeywordSet().define("nBeam", sdh.nbeam);
414 table_.rwKeywordSet().define("nPol", sdh.npol);
415 table_.rwKeywordSet().define("nChan", sdh.nchan);
416 table_.rwKeywordSet().define("Observer", sdh.observer);
417 table_.rwKeywordSet().define("Project", sdh.project);
418 table_.rwKeywordSet().define("Obstype", sdh.obstype);
419 table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
420 table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
421 table_.rwKeywordSet().define("Equinox", sdh.equinox);
422 table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
423 table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
424 table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
425 table_.rwKeywordSet().define("UTC", sdh.utc);
426 table_.rwKeywordSet().define("FluxUnit", sdh.fluxunit);
427 table_.rwKeywordSet().define("Epoch", sdh.epoch);
428 table_.rwKeywordSet().define("POLTYPE", sdh.poltype);
429}
430
431STHeader Scantable::getHeader() const
432{
433 STHeader sdh;
434 table_.keywordSet().get("nBeam",sdh.nbeam);
435 table_.keywordSet().get("nIF",sdh.nif);
436 table_.keywordSet().get("nPol",sdh.npol);
437 table_.keywordSet().get("nChan",sdh.nchan);
438 table_.keywordSet().get("Observer", sdh.observer);
439 table_.keywordSet().get("Project", sdh.project);
440 table_.keywordSet().get("Obstype", sdh.obstype);
441 table_.keywordSet().get("AntennaName", sdh.antennaname);
442 table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
443 table_.keywordSet().get("Equinox", sdh.equinox);
444 table_.keywordSet().get("FreqRefFrame", sdh.freqref);
445 table_.keywordSet().get("FreqRefVal", sdh.reffreq);
446 table_.keywordSet().get("Bandwidth", sdh.bandwidth);
447 table_.keywordSet().get("UTC", sdh.utc);
448 table_.keywordSet().get("FluxUnit", sdh.fluxunit);
449 table_.keywordSet().get("Epoch", sdh.epoch);
450 table_.keywordSet().get("POLTYPE", sdh.poltype);
451 return sdh;
452}
453
454void Scantable::setSourceType( int stype )
455{
456 if ( stype < 0 || stype > 1 )
457 throw(AipsError("Illegal sourcetype."));
458 TableVector<Int> tabvec(table_, "SRCTYPE");
459 tabvec = Int(stype);
460}
461
462void Scantable::setSourceName( const std::string& name )
463{
464 TableVector<String> tabvec(table_, "SRCNAME");
465 tabvec = name;
466}
467
468bool Scantable::conformant( const Scantable& other )
469{
470 return this->getHeader().conformant(other.getHeader());
471}
472
473
474
475std::string Scantable::formatSec(Double x) const
476{
477 Double xcop = x;
478 MVTime mvt(xcop/24./3600.); // make days
479
480 if (x < 59.95)
481 return String(" ") + mvt.string(MVTime::TIME_CLEAN_NO_HM, 7)+"s";
482 else if (x < 3599.95)
483 return String(" ") + mvt.string(MVTime::TIME_CLEAN_NO_H,7)+" ";
484 else {
485 ostringstream oss;
486 oss << setw(2) << std::right << setprecision(1) << mvt.hour();
487 oss << ":" << mvt.string(MVTime::TIME_CLEAN_NO_H,7) << " ";
488 return String(oss);
489 }
490};
491
492 std::string Scantable::formatDirection(const MDirection& md, Int prec) const
493{
494 Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
495 if (prec<0)
496 prec = 7;
497
498 String ref = md.getRefString();
499 MVAngle mvLon(t[0]);
500 String sLon = mvLon.string(MVAngle::TIME,prec);
501 uInt tp = md.getRef().getType();
502 if (tp == MDirection::GALACTIC ||
503 tp == MDirection::SUPERGAL ) {
504 sLon = mvLon(0.0).string(MVAngle::ANGLE_CLEAN,prec);
505 }
506 MVAngle mvLat(t[1]);
507 String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec);
508 return ref + String(" ") + sLon + String(" ") + sLat;
509}
510
511
512std::string Scantable::getFluxUnit() const
513{
514 return table_.keywordSet().asString("FluxUnit");
515}
516
517void Scantable::setFluxUnit(const std::string& unit)
518{
519 String tmp(unit);
520 Unit tU(tmp);
521 if (tU==Unit("K") || tU==Unit("Jy")) {
522 table_.rwKeywordSet().define(String("FluxUnit"), tmp);
523 } else {
524 throw AipsError("Illegal unit - must be compatible with Jy or K");
525 }
526}
527
528void Scantable::setInstrument(const std::string& name)
529{
530 bool throwIt = true;
531 // create an Instrument to see if this is valid
532 STAttr::convertInstrument(name, throwIt);
533 String nameU(name);
534 nameU.upcase();
535 table_.rwKeywordSet().define(String("AntennaName"), nameU);
536}
537
538void Scantable::setFeedType(const std::string& feedtype)
539{
540 if ( Scantable::factories_.find(feedtype) == Scantable::factories_.end() ) {
541 std::string msg = "Illegal feed type "+ feedtype;
542 throw(casacore::AipsError(msg));
543 }
544 table_.rwKeywordSet().define(String("POLTYPE"), feedtype);
545}
546
547MPosition Scantable::getAntennaPosition() const
548{
549 Vector<Double> antpos;
550 table_.keywordSet().get("AntennaPosition", antpos);
551 MVPosition mvpos(antpos(0),antpos(1),antpos(2));
552 return MPosition(mvpos);
553}
554
555void Scantable::makePersistent(const std::string& filename)
556{
557 String inname(filename);
558 Path path(inname);
559 /// @todo reindex SCANNO, recompute nbeam, nif, npol
560 inname = path.expandedName();
561 // 2011/03/04 TN
562 // We can comment out this workaround since the essential bug is
563 // fixed in casacore (r20889 in google code).
564 table_.deepCopy(inname, Table::New);
565// // WORKAROUND !!! for Table bug
566// // Remove when fixed in casacore
567// if ( table_.tableType() == Table::Memory && !selector_.empty() ) {
568// Table tab = table_.copyToMemoryTable(generateName());
569// tab.deepCopy(inname, Table::New);
570// tab.markForDelete();
571//
572// } else {
573// table_.deepCopy(inname, Table::New);
574// }
575}
576
577int Scantable::nbeam( int scanno ) const
578{
579 if ( scanno < 0 ) {
580 Int n;
581 table_.keywordSet().get("nBeam",n);
582 return int(n);
583 } else {
584 // take the first POLNO,IFNO,CYCLENO as nbeam shouldn't vary with these
585 Table t = table_(table_.col("SCANNO") == scanno);
586 ROTableRow row(t);
587 const TableRecord& rec = row.get(0);
588 Table subt = t( t.col("IFNO") == Int(rec.asuInt("IFNO"))
589 && t.col("POLNO") == Int(rec.asuInt("POLNO"))
590 && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
591 ROTableVector<uInt> v(subt, "BEAMNO");
592 return int(v.nelements());
593 }
594 return 0;
595}
596
597int Scantable::nif( int scanno ) const
598{
599 if ( scanno < 0 ) {
600 Int n;
601 table_.keywordSet().get("nIF",n);
602 return int(n);
603 } else {
604 // take the first POLNO,BEAMNO,CYCLENO as nbeam shouldn't vary with these
605 Table t = table_(table_.col("SCANNO") == scanno);
606 ROTableRow row(t);
607 const TableRecord& rec = row.get(0);
608 Table subt = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
609 && t.col("POLNO") == Int(rec.asuInt("POLNO"))
610 && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
611 if ( subt.nrow() == 0 ) return 0;
612 ROTableVector<uInt> v(subt, "IFNO");
613 return int(v.nelements());
614 }
615 return 0;
616}
617
618int Scantable::npol( int scanno ) const
619{
620 if ( scanno < 0 ) {
621 Int n;
622 table_.keywordSet().get("nPol",n);
623 return n;
624 } else {
625 // take the first POLNO,IFNO,CYCLENO as nbeam shouldn't vary with these
626 Table t = table_(table_.col("SCANNO") == scanno);
627 ROTableRow row(t);
628 const TableRecord& rec = row.get(0);
629 Table subt = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
630 && t.col("IFNO") == Int(rec.asuInt("IFNO"))
631 && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
632 if ( subt.nrow() == 0 ) return 0;
633 ROTableVector<uInt> v(subt, "POLNO");
634 return int(v.nelements());
635 }
636 return 0;
637}
638
639int Scantable::ncycle( int scanno ) const
640{
641 if ( scanno < 0 ) {
642 Block<String> cols(2);
643 cols[0] = "SCANNO";
644 cols[1] = "CYCLENO";
645 TableIterator it(table_, cols);
646 int n = 0;
647 while ( !it.pastEnd() ) {
648 ++n;
649 ++it;
650 }
651 return n;
652 } else {
653 Table t = table_(table_.col("SCANNO") == scanno);
654 ROTableRow row(t);
655 const TableRecord& rec = row.get(0);
656 Table subt = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
657 && t.col("POLNO") == Int(rec.asuInt("POLNO"))
658 && t.col("IFNO") == Int(rec.asuInt("IFNO")) );
659 if ( subt.nrow() == 0 ) return 0;
660 return int(subt.nrow());
661 }
662 return 0;
663}
664
665
666int Scantable::nrow( int /*scanno*/ ) const
667{
668 return int(table_.nrow());
669}
670
671int Scantable::nchan( int ifno ) const
672{
673 if ( ifno < 0 ) {
674 Int n;
675 table_.keywordSet().get("nChan",n);
676 return int(n);
677 } else {
678 // take the first SCANNO,POLNO,BEAMNO,CYCLENO as nbeam shouldn't
679 // vary with these
680 Table t = table_(table_.col("IFNO") == ifno, 1);
681 if ( t.nrow() == 0 ) return 0;
682 ROArrayColumn<Float> v(t, "SPECTRA");
683 return v.shape(0)(0);
684 }
685 return 0;
686}
687
688int Scantable::nscan() const {
689 Vector<uInt> scannos(scanCol_.getColumn());
690 uInt nout = genSort( scannos, Sort::Ascending,
691 Sort::QuickSort|Sort::NoDuplicates );
692 return int(nout);
693}
694
695int Scantable::getChannels(int whichrow) const
696{
697 return specCol_.shape(whichrow)(0);
698}
699
700int Scantable::getBeam(int whichrow) const
701{
702 return beamCol_(whichrow);
703}
704
705std::vector<uint> Scantable::getNumbers(const ScalarColumn<uInt>& col) const
706{
707 Vector<uInt> nos(col.getColumn());
708 uInt n = genSort( nos, Sort::Ascending, Sort::QuickSort|Sort::NoDuplicates );
709 nos.resize(n, True);
710 std::vector<uint> stlout;
711 nos.tovector(stlout);
712 return stlout;
713}
714
715int Scantable::getIF(int whichrow) const
716{
717 return ifCol_(whichrow);
718}
719
720int Scantable::getPol(int whichrow) const
721{
722 return polCol_(whichrow);
723}
724
725std::string Scantable::formatTime(const MEpoch& me, bool showdate) const
726{
727 return formatTime(me, showdate, 0);
728}
729
730std::string Scantable::formatTime(const MEpoch& me, bool showdate, uInt prec) const
731{
732 MVTime mvt(me.getValue());
733 if (showdate)
734 //mvt.setFormat(MVTime::YMD);
735 mvt.setFormat(MVTime::YMD, prec);
736 else
737 //mvt.setFormat(MVTime::TIME);
738 mvt.setFormat(MVTime::TIME, prec);
739 ostringstream oss;
740 oss << mvt;
741 return String(oss);
742}
743
744void Scantable::calculateAZEL()
745{
746 LogIO os( LogOrigin( "Scantable", "calculateAZEL()", WHERE ) ) ;
747 MPosition mp = getAntennaPosition();
748 MEpoch::ROScalarColumn timeCol(table_, "TIME");
749 ostringstream oss;
750 oss << mp;
751 os << "Computed azimuth/elevation using " << endl
752 << String(oss) << endl;
753 for (Int i=0; i<nrow(); ++i) {
754 MEpoch me = timeCol(i);
755 MDirection md = getDirection(i);
756 os << " Time: " << formatTime(me,False)
757 << " Direction: " << formatDirection(md)
758 << endl << " => ";
759 MeasFrame frame(mp, me);
760 Vector<Double> azel =
761 MDirection::Convert(md, MDirection::Ref(MDirection::AZEL,
762 frame)
763 )().getAngle("rad").getValue();
764 azCol_.put(i,Float(azel[0]));
765 elCol_.put(i,Float(azel[1]));
766 os << "azel: " << azel[0]/C::pi*180.0 << " "
767 << azel[1]/C::pi*180.0 << " (deg)" << LogIO::POST;
768 }
769}
770
771void Scantable::clip(const Float uthres, const Float dthres, bool clipoutside, bool unflag)
772{
773 Vector<uInt> flagrow = flagrowCol_.getColumn();
774 for (uInt i=0; i<table_.nrow(); ++i) {
775 // apply flag only when specified row is vaild
776 if (flagrow[i] == 0) {
777 Vector<uChar> flgs = flagsCol_(i);
778 srchChannelsToClip(i, uthres, dthres, clipoutside, unflag, flgs);
779 flagsCol_.put(i, flgs);
780 }
781 }
782}
783
784std::vector<bool> Scantable::getClipMask(int whichrow, const Float uthres, const Float dthres, bool clipoutside, bool unflag)
785{
786 Vector<uChar> flags;
787 flagsCol_.get(uInt(whichrow), flags);
788 srchChannelsToClip(uInt(whichrow), uthres, dthres, clipoutside, unflag, flags);
789 Vector<Bool> bflag(flags.shape());
790 convertArray(bflag, flags);
791 //bflag = !bflag;
792
793 std::vector<bool> mask;
794 bflag.tovector(mask);
795 return mask;
796}
797
798void Scantable::srchChannelsToClip(uInt whichrow, const Float uthres, const Float dthres, bool clipoutside, bool unflag,
799 Vector<uChar> flgs)
800{
801 Vector<Float> spcs = specCol_(whichrow);
802 uInt nchannel = spcs.nelements();
803 if (spcs.nelements() != nchannel) {
804 throw(AipsError("Data has incorrect number of channels"));
805 }
806 uChar userflag = 1 << 7;
807 if (unflag) {
808 userflag = 0 << 7;
809 }
810 if (clipoutside) {
811 for (uInt j = 0; j < nchannel; ++j) {
812 Float spc = spcs(j);
813 if ((spc >= uthres) || (spc <= dthres)) {
814 flgs(j) = userflag;
815 }
816 }
817 } else {
818 for (uInt j = 0; j < nchannel; ++j) {
819 Float spc = spcs(j);
820 if ((spc < uthres) && (spc > dthres)) {
821 flgs(j) = userflag;
822 }
823 }
824 }
825}
826
827
828void Scantable::flag( int whichrow, const std::vector<bool>& msk, bool unflag ) {
829 std::vector<bool>::const_iterator it;
830 uInt ntrue = 0;
831 if (whichrow >= int(table_.nrow()) ) {
832 throw(AipsError("Invalid row number"));
833 }
834 for (it = msk.begin(); it != msk.end(); ++it) {
835 if ( *it ) {
836 ntrue++;
837 }
838 }
839 //if ( selector_.empty() && (msk.size() == 0 || msk.size() == ntrue) )
840 if ( whichrow == -1 && !unflag && selector_.empty() && (msk.size() == 0 || msk.size() == ntrue) )
841 throw(AipsError("Trying to flag whole scantable."));
842 uChar userflag = 1 << 7;
843 if ( unflag ) {
844 userflag = 0 << 7;
845 }
846 if (whichrow > -1 ) {
847 // apply flag only when specified row is vaild
848 if (flagrowCol_(whichrow) == 0) {
849 applyChanFlag(uInt(whichrow), msk, userflag);
850 }
851 } else {
852 Vector<uInt> flagrow = flagrowCol_.getColumn();
853 for ( uInt i=0; i<table_.nrow(); ++i) {
854 // apply flag only when specified row is vaild
855 if (flagrow[i] == 0) {
856 applyChanFlag(i, msk, userflag);
857 }
858 }
859 }
860}
861
862void Scantable::applyChanFlag( uInt whichrow, const std::vector<bool>& msk, uChar flagval )
863{
864 if (whichrow >= table_.nrow() ) {
865 throw( casacore::indexError<int>( whichrow, "asap::Scantable::applyChanFlag: Invalid row number" ) );
866 }
867 Vector<uChar> flgs = flagsCol_(whichrow);
868 if ( msk.size() == 0 ) {
869 flgs = flagval;
870 flagsCol_.put(whichrow, flgs);
871 return;
872 }
873 if ( int(msk.size()) != nchan( getIF(whichrow) ) ) {
874 throw(AipsError("Mask has incorrect number of channels."));
875 }
876 if ( flgs.nelements() != msk.size() ) {
877 throw(AipsError("Mask has incorrect number of channels."
878 " Probably varying with IF. Please flag per IF"));
879 }
880 std::vector<bool>::const_iterator it;
881 uInt j = 0;
882 for (it = msk.begin(); it != msk.end(); ++it) {
883 if ( *it ) {
884 flgs(j) = flagval;
885 }
886 ++j;
887 }
888 flagsCol_.put(whichrow, flgs);
889}
890
891void Scantable::flagRow(const std::vector<uInt>& rows, bool unflag)
892{
893 if (selector_.empty() && (rows.size() == table_.nrow()) && !unflag)
894 throw(AipsError("Trying to flag whole scantable."));
895
896 uInt rowflag = (unflag ? 0 : 1);
897 std::vector<uInt>::const_iterator it;
898 for (it = rows.begin(); it != rows.end(); ++it)
899 flagrowCol_.put(*it, rowflag);
900}
901
902std::vector<bool> Scantable::getMask(int whichrow) const
903{
904 Vector<uChar> flags;
905 flagsCol_.get(uInt(whichrow), flags);
906 Vector<Bool> bflag(flags.shape());
907 convertArray(bflag, flags);
908 bflag = !bflag;
909 std::vector<bool> mask;
910 bflag.tovector(mask);
911 return mask;
912}
913
914std::vector<float> Scantable::getSpectrum( int whichrow,
915 const std::string& poltype ) const
916{
917 LogIO os( LogOrigin( "Scantable", "getSpectrum()", WHERE ) ) ;
918
919 String ptype = poltype;
920 if (poltype == "" ) ptype = getPolType();
921 if ( whichrow < 0 || whichrow >= nrow() )
922 throw(AipsError("Illegal row number."));
923 std::vector<float> out;
924 Vector<Float> arr;
925 uInt requestedpol = polCol_(whichrow);
926 String basetype = getPolType();
927 if ( ptype == basetype ) {
928 specCol_.get(whichrow, arr);
929 } else {
930 CountedPtr<STPol> stpol(STPol::getPolClass(Scantable::factories_,
931 basetype));
932 uInt row = uInt(whichrow);
933 stpol->setSpectra(getPolMatrix(row));
934 Float fang,fhand;
935 fang = focusTable_.getTotalAngle(mfocusidCol_(row));
936 fhand = focusTable_.getFeedHand(mfocusidCol_(row));
937 stpol->setPhaseCorrections(fang, fhand);
938 arr = stpol->getSpectrum(requestedpol, ptype);
939 }
940 if ( arr.nelements() == 0 )
941
942 os << "Not enough polarisations present to do the conversion."
943 << LogIO::POST;
944 arr.tovector(out);
945 return out;
946}
947
948void Scantable::setSpectrum( const std::vector<float>& spec,
949 int whichrow )
950{
951 Vector<Float> spectrum(spec);
952 Vector<Float> arr;
953 specCol_.get(whichrow, arr);
954 if ( spectrum.nelements() != arr.nelements() )
955 throw AipsError("The spectrum has incorrect number of channels.");
956 specCol_.put(whichrow, spectrum);
957}
958
959
960String Scantable::generateName()
961{
962 return (File::newUniqueName("./","temp")).baseName();
963}
964
965const casacore::Table& Scantable::table( ) const
966{
967 return table_;
968}
969
970casacore::Table& Scantable::table( )
971{
972 return table_;
973}
974
975std::string Scantable::getPolType() const
976{
977 return table_.keywordSet().asString("POLTYPE");
978}
979
980void Scantable::unsetSelection()
981{
982 table_ = originalTable_;
983 attach();
984 selector_.reset();
985}
986
987void Scantable::setSelection( const STSelector& selection )
988{
989 Table tab = const_cast<STSelector&>(selection).apply(originalTable_);
990 if ( tab.nrow() == 0 ) {
991 throw(AipsError("Selection contains no data. Not applying it."));
992 }
993 table_ = tab;
994 attach();
995// tab.rwKeywordSet().define("nBeam",(Int)(getBeamNos().size())) ;
996// vector<uint> selectedIFs = getIFNos() ;
997// Int newnIF = selectedIFs.size() ;
998// tab.rwKeywordSet().define("nIF",newnIF) ;
999// if ( newnIF != 0 ) {
1000// Int newnChan = 0 ;
1001// for ( Int i = 0 ; i < newnIF ; i++ ) {
1002// Int nChan = nchan( selectedIFs[i] ) ;
1003// if ( newnChan > nChan )
1004// newnChan = nChan ;
1005// }
1006// tab.rwKeywordSet().define("nChan",newnChan) ;
1007// }
1008// tab.rwKeywordSet().define("nPol",(Int)(getPolNos().size())) ;
1009 selector_ = selection;
1010}
1011
1012
1013std::string Scantable::headerSummary()
1014{
1015 // Format header info
1016// STHeader sdh;
1017// sdh = getHeader();
1018// sdh.print();
1019 ostringstream oss;
1020 oss.flags(std::ios_base::left);
1021 String tmp;
1022 // Project
1023 table_.keywordSet().get("Project", tmp);
1024 oss << setw(15) << "Project:" << tmp << endl;
1025 // Observation date
1026 oss << setw(15) << "Obs Date:" << getTime(-1,true) << endl;
1027 // Observer
1028 oss << setw(15) << "Observer:"
1029 << table_.keywordSet().asString("Observer") << endl;
1030 // Antenna Name
1031 table_.keywordSet().get("AntennaName", tmp);
1032 oss << setw(15) << "Antenna Name:" << tmp << endl;
1033 // Obs type
1034 table_.keywordSet().get("Obstype", tmp);
1035 // Records (nrow)
1036 oss << setw(15) << "Data Records:" << table_.nrow() << " rows" << endl;
1037 oss << setw(15) << "Obs. Type:" << tmp << endl;
1038 // Beams, IFs, Polarizations, and Channels
1039 oss << setw(15) << "Beams:" << setw(4) << nbeam() << endl
1040 << setw(15) << "IFs:" << setw(4) << nif() << endl
1041 << setw(15) << "Polarisations:" << setw(4) << npol()
1042 << "(" << getPolType() << ")" << endl
1043 << setw(15) << "Channels:" << nchan() << endl;
1044 // Flux unit
1045 table_.keywordSet().get("FluxUnit", tmp);
1046 oss << setw(15) << "Flux Unit:" << tmp << endl;
1047 // Abscissa Unit
1048 oss << setw(15) << "Abscissa:" << getAbcissaLabel(0) << endl;
1049 // Selection
1050 oss << selector_.print() << endl;
1051
1052 return String(oss);
1053}
1054
1055void Scantable::summary( const std::string& filename )
1056{
1057 ostringstream oss;
1058 ofstream ofs;
1059 LogIO ols(LogOrigin("Scantable", "summary", WHERE));
1060
1061 if (filename != "")
1062 ofs.open( filename.c_str(), ios::out );
1063
1064 oss << endl;
1065 oss << asap::SEPERATOR << endl;
1066 oss << " Scan Table Summary" << endl;
1067 oss << asap::SEPERATOR << endl;
1068
1069 // Format header info
1070 oss << headerSummary();
1071 oss << endl;
1072
1073 if (table_.nrow() <= 0){
1074 oss << asap::SEPERATOR << endl;
1075 oss << "The MAIN table is empty: there are no data!!!" << endl;
1076 oss << asap::SEPERATOR << endl;
1077
1078 ols << String(oss) << LogIO::POST;
1079 if (ofs) {
1080 ofs << String(oss) << flush;
1081 ofs.close();
1082 }
1083 return;
1084 }
1085
1086
1087
1088 // main table
1089 String dirtype = "Position ("
1090 + getDirectionRefString()
1091 + ")";
1092 oss.flags(std::ios_base::left);
1093 oss << setw(5) << "Scan"
1094 << setw(15) << "Source"
1095 << setw(35) << "Time range"
1096 << setw(2) << "" << setw(7) << "Int[s]"
1097 << setw(7) << "Record"
1098 << setw(8) << "SrcType"
1099 << setw(8) << "FreqIDs"
1100 << setw(7) << "MolIDs" << endl;
1101 oss << setw(7)<< "" << setw(6) << "Beam"
1102 << setw(23) << dirtype << endl;
1103
1104 oss << asap::SEPERATOR << endl;
1105
1106 // Flush summary and clear up the string
1107 ols << String(oss) << LogIO::POST;
1108 if (ofs) ofs << String(oss) << flush;
1109 oss.str("");
1110 oss.clear();
1111
1112
1113 // Get Freq_ID map
1114 ROScalarColumn<uInt> ftabIds(frequencies().table(), "ID");
1115 Int nfid = ftabIds.nrow();
1116 if (nfid <= 0){
1117 oss << "FREQUENCIES subtable is empty: there are no data!!!" << endl;
1118 oss << asap::SEPERATOR << endl;
1119
1120 ols << String(oss) << LogIO::POST;
1121 if (ofs) {
1122 ofs << String(oss) << flush;
1123 ofs.close();
1124 }
1125 return;
1126 }
1127 // Storages of overall IFNO, POLNO, and nchan per FREQ_ID
1128 // the orders are identical to ID in FREQ subtable
1129 Block< Vector<uInt> > ifNos(nfid), polNos(nfid);
1130 Vector<Int> fIdchans(nfid,-1);
1131 Vector<Double> fIdfreq0(nfid,-1);
1132 Vector<Double> fIdfcent(nfid,-1);
1133 map<uInt, Int> fidMap; // (FREQ_ID, row # in FREQ subtable) pair
1134 for (Int i=0; i < nfid; i++){
1135 // fidMap[freqId] returns row number in FREQ subtable
1136 fidMap.insert(pair<uInt, Int>(ftabIds(i),i));
1137 ifNos[i] = Vector<uInt>();
1138 polNos[i] = Vector<uInt>();
1139 }
1140
1141 TableIterator iter(table_, "SCANNO");
1142
1143 // Vars for keeping track of time, freqids, molIds in a SCANNO
1144 //Vector<uInt> freqids;
1145 //Vector<uInt> molids;
1146 Vector<uInt> beamids(1,0);
1147 Vector<MDirection> beamDirs;
1148 Vector<Int> stypeids(1,0);
1149 Vector<String> stypestrs;
1150 Int nfreq(1);
1151 Int nmol(1);
1152 uInt nbeam(1);
1153 uInt nstype(1);
1154
1155 Double btime(0.0), etime(0.0);
1156 Double meanIntTim(0.0);
1157
1158 uInt currFreqId(0), ftabRow(0);
1159 Int iflen(0), pollen(0);
1160
1161 while (!iter.pastEnd()) {
1162 Table subt = iter.table();
1163 uInt snrow = subt.nrow();
1164 ROTableRow row(subt);
1165 const TableRecord& rec = row.get(0);
1166
1167 // relevant columns
1168 ROScalarColumn<Double> mjdCol(subt,"TIME");
1169 ROScalarColumn<Double> intervalCol(subt,"INTERVAL");
1170 MDirection::ROScalarColumn dirCol(subt,"DIRECTION");
1171
1172 ScalarColumn<uInt> freqIdCol(subt,"FREQ_ID");
1173 ScalarColumn<uInt> molIdCol(subt,"MOLECULE_ID");
1174 ROScalarColumn<uInt> beamCol(subt,"BEAMNO");
1175 ROScalarColumn<Int> stypeCol(subt,"SRCTYPE");
1176
1177 ROScalarColumn<uInt> ifNoCol(subt,"IFNO");
1178 ROScalarColumn<uInt> polNoCol(subt,"POLNO");
1179
1180
1181 // Times
1182 meanIntTim = sum(intervalCol.getColumn()) / (double) snrow;
1183 minMax(btime, etime, mjdCol.getColumn());
1184 double shiftInDay(0.5*meanIntTim/C::day);
1185 btime -= shiftInDay;
1186 etime += shiftInDay;
1187
1188 // MOLECULE_ID and FREQ_ID
1189 Vector<uInt> molids(getNumbers(molIdCol));
1190 molids.shape(nmol);
1191
1192 Vector<uInt> freqids(getNumbers(freqIdCol));
1193 freqids.shape(nfreq);
1194
1195 // Add first beamid, and srcNames
1196 beamids.resize(1,False);
1197 beamDirs.resize(1,False);
1198 beamids(0)=beamCol(0);
1199 beamDirs(0)=dirCol(0);
1200 nbeam = 1;
1201
1202 stypeids.resize(1,False);
1203 stypeids(0)=stypeCol(0);
1204 nstype = 1;
1205
1206 // Global listings of nchan/IFNO/POLNO per FREQ_ID
1207 currFreqId=freqIdCol(0);
1208 ftabRow = fidMap[currFreqId];
1209 // Assumes an identical number of channels per FREQ_ID
1210 if (fIdchans(ftabRow) < 0 ) {
1211 RORecordFieldPtr< Array<Float> > spec(rec, "SPECTRA");
1212 fIdchans(ftabRow)=(*spec).shape()(0);
1213 }
1214 if (fIdfreq0(ftabRow) < 0 ) {
1215 SpectralCoordinate spc = frequencies().getSpectralCoordinate(ftabRow);
1216 Double fs, fe;
1217 spc.toWorld(fs, 0);
1218 spc.toWorld(fe, fIdchans(ftabRow)-1);
1219 fIdfreq0(ftabRow) = fs;
1220 fIdfcent(ftabRow) = 0.5 * ( fs + fe );
1221 }
1222 // Should keep ifNos and polNos form the previous SCANNO
1223 if ( !anyEQ(ifNos[ftabRow],ifNoCol(0)) ) {
1224 ifNos[ftabRow].shape(iflen);
1225 iflen++;
1226 ifNos[ftabRow].resize(iflen,True);
1227 ifNos[ftabRow](iflen-1) = ifNoCol(0);
1228 }
1229 if ( !anyEQ(polNos[ftabRow],polNoCol(0)) ) {
1230 polNos[ftabRow].shape(pollen);
1231 pollen++;
1232 polNos[ftabRow].resize(pollen,True);
1233 polNos[ftabRow](pollen-1) = polNoCol(0);
1234 }
1235
1236 for (uInt i=1; i < snrow; i++){
1237 // Need to list BEAMNO and DIRECTION in the same order
1238 if ( !anyEQ(beamids,beamCol(i)) ) {
1239 nbeam++;
1240 beamids.resize(nbeam,True);
1241 beamids(nbeam-1)=beamCol(i);
1242 beamDirs.resize(nbeam,True);
1243 beamDirs(nbeam-1)=dirCol(i);
1244 }
1245
1246 // SRCTYPE is Int (getNumber takes only uInt)
1247 if ( !anyEQ(stypeids,stypeCol(i)) ) {
1248 nstype++;
1249 stypeids.resize(nstype,True);
1250 stypeids(nstype-1)=stypeCol(i);
1251 }
1252
1253 // Global listings of nchan/IFNO/POLNO per FREQ_ID
1254 currFreqId=freqIdCol(i);
1255 ftabRow = fidMap[currFreqId];
1256 if (fIdchans(ftabRow) < 0 ) {
1257 const TableRecord& rec = row.get(i);
1258 RORecordFieldPtr< Array<Float> > spec(rec, "SPECTRA");
1259 fIdchans(ftabRow) = (*spec).shape()(0);
1260 }
1261 if (fIdfreq0(ftabRow) < 0 ) {
1262 SpectralCoordinate spc = frequencies().getSpectralCoordinate(ftabRow);
1263 Double fs, fe;
1264 spc.toWorld(fs, 0);
1265 spc.toWorld(fe, fIdchans(ftabRow)-1);
1266 fIdfreq0(ftabRow) = fs;
1267 fIdfcent(ftabRow) = 5.e-1 * ( fs + fe );
1268 }
1269 if ( !anyEQ(ifNos[ftabRow],ifNoCol(i)) ) {
1270 ifNos[ftabRow].shape(iflen);
1271 iflen++;
1272 ifNos[ftabRow].resize(iflen,True);
1273 ifNos[ftabRow](iflen-1) = ifNoCol(i);
1274 }
1275 if ( !anyEQ(polNos[ftabRow],polNoCol(i)) ) {
1276 polNos[ftabRow].shape(pollen);
1277 pollen++;
1278 polNos[ftabRow].resize(pollen,True);
1279 polNos[ftabRow](pollen-1) = polNoCol(i);
1280 }
1281 } // end of row iteration
1282
1283 stypestrs.resize(nstype,False);
1284 for (uInt j=0; j < nstype; j++)
1285 stypestrs(j) = SrcType::getName(stypeids(j));
1286
1287 // Format Scan summary
1288 oss << setw(4) << std::right << rec.asuInt("SCANNO")
1289 << std::left << setw(1) << ""
1290 << setw(15) << rec.asString("SRCNAME")
1291 << setw(21) << MVTime(btime).string(MVTime::YMD,8)
1292 << setw(3) << " - " << MVTime(etime).string(MVTime::TIME,8)
1293 << setw(3) << "" << setw(6) << meanIntTim << setw(1) << ""
1294 << std::right << setw(5) << snrow << setw(2) << ""
1295 << std::left << stypestrs << setw(1) << ""
1296 << freqids << setw(1) << ""
1297 << molids << endl;
1298 // Format Beam summary
1299 for (uInt j=0; j < nbeam; j++) {
1300 oss << setw(7) << "" << setw(6) << beamids(j) << setw(1) << ""
1301 << formatDirection(beamDirs(j),9) << endl;
1302 }
1303 // Flush summary every scan and clear up the string
1304 ols << String(oss) << LogIO::POST;
1305 if (ofs) ofs << String(oss) << flush;
1306 oss.str("");
1307 oss.clear();
1308
1309 ++iter;
1310 } // end of scan iteration
1311 oss << asap::SEPERATOR << endl;
1312
1313 // List FRECUENCIES Table (using STFrequencies.print may be slow)
1314 oss << "FREQUENCIES: " << nfreq << endl;
1315// oss << std::right << setw(5) << "ID" << setw(2) << ""
1316// << std::left << setw(5) << "IFNO" << setw(2) << ""
1317// << setw(8) << "Frame"
1318// << setw(16) << "RefVal"
1319// << setw(7) << "RefPix"
1320// << setw(15) << "Increment"
1321// << setw(9) << "Channels"
1322// << setw(6) << "POLNOs" << endl;
1323// Int tmplen;
1324// for (Int i=0; i < nfid; i++){
1325// // List row=i of FREQUENCIES subtable
1326// ifNos[i].shape(tmplen);
1327// if (tmplen >= 1) {
1328// oss << std::right << setw(5) << ftabIds(i) << setw(2) << ""
1329// << setw(3) << ifNos[i](0) << setw(1) << ""
1330// << std::left << setw(46) << frequencies().print(ftabIds(i))
1331// << setw(2) << ""
1332// << std::right << setw(8) << fIdchans[i] << setw(2) << ""
1333// << std::left << polNos[i];
1334// if (tmplen > 1) {
1335// oss << " (" << tmplen << " chains)";
1336// }
1337// oss << endl;
1338// }
1339 oss << std::right << setw(4) << "ID" << setw(2) << ""
1340 << std::left << setw(9) << "IFNO(SPW)" << setw(2) << ""
1341 << setw(8) << "#Chans"
1342 << setw(8) << "Frame"
1343 << setw(12) << "Ch0[MHz]"
1344 << setw(14) << "ChanWid[kHz]"
1345 << setw(14) << "Center[MHz]"
1346 << setw(6) << "POLNOs" << endl;
1347 Int tmplen;
1348 for (Int i=0; i < nfid; i++){
1349 // List row=i of FREQUENCIES subtable
1350 ifNos[i].shape(tmplen);
1351 Double refpix, refval, increment ;
1352 if (tmplen >= 1) {
1353 freqTable_.getEntry( refpix, refval, increment, ftabIds(i) ) ;
1354 oss << std::right << setw(4) << ftabIds(i) << setw(2) << ""
1355 << std::left << setw(9) << ifNos[i](0) << setw(2) << ""
1356 << std::right << setw(6) << fIdchans[i] << setw(2) << ""
1357 << setw(6) << frequencies().getFrameString(true)
1358 << setw(2) << ""
1359 << setw(10) << std::setprecision(9) << (fIdfreq0[i]*1.e-6) << setw(2) << ""
1360 << setw(12) << (increment*1.e-3) << setw(2) << ""
1361 << setw(12) << (fIdfcent[i]*1.e-6) << setw(2) << ""
1362 << std::left << polNos[i];
1363 if (tmplen > 1) {
1364 oss << " (" << tmplen << " chains)";
1365 }
1366 oss << endl;
1367 }
1368
1369 }
1370 oss << asap::SEPERATOR << endl;
1371
1372 // List MOLECULES Table (currently lists all rows)
1373 oss << "MOLECULES: " << endl;
1374 if (molecules().nrow() <= 0) {
1375 oss << " MOLECULES subtable is empty: there are no data" << endl;
1376 } else {
1377 ROTableRow row(molecules().table());
1378 oss << std::right << setw(5) << "ID"
1379 << std::left << setw(3) << ""
1380 << setw(18) << "RestFreq"
1381 << setw(15) << "Name" << endl;
1382 for (Int i=0; i < molecules().nrow(); i++){
1383 const TableRecord& rec=row.get(i);
1384 oss << std::right << setw(5) << rec.asuInt("ID")
1385 << std::left << setw(3) << ""
1386 << rec.asArrayDouble("RESTFREQUENCY") << setw(1) << ""
1387 << rec.asArrayString("NAME") << endl;
1388 }
1389 }
1390 oss << asap::SEPERATOR << endl;
1391 ols << String(oss) << LogIO::POST;
1392 if (ofs) {
1393 ofs << String(oss) << flush;
1394 ofs.close();
1395 }
1396 // return String(oss);
1397}
1398
1399
1400std::string Scantable::oldheaderSummary()
1401{
1402 // Format header info
1403// STHeader sdh;
1404// sdh = getHeader();
1405// sdh.print();
1406 ostringstream oss;
1407 oss.flags(std::ios_base::left);
1408 oss << setw(15) << "Beams:" << setw(4) << nbeam() << endl
1409 << setw(15) << "IFs:" << setw(4) << nif() << endl
1410 << setw(15) << "Polarisations:" << setw(4) << npol()
1411 << "(" << getPolType() << ")" << endl
1412 << setw(15) << "Channels:" << nchan() << endl;
1413 String tmp;
1414 oss << setw(15) << "Observer:"
1415 << table_.keywordSet().asString("Observer") << endl;
1416 oss << setw(15) << "Obs Date:" << getTime(-1,true) << endl;
1417 table_.keywordSet().get("Project", tmp);
1418 oss << setw(15) << "Project:" << tmp << endl;
1419 table_.keywordSet().get("Obstype", tmp);
1420 oss << setw(15) << "Obs. Type:" << tmp << endl;
1421 table_.keywordSet().get("AntennaName", tmp);
1422 oss << setw(15) << "Antenna Name:" << tmp << endl;
1423 table_.keywordSet().get("FluxUnit", tmp);
1424 oss << setw(15) << "Flux Unit:" << tmp << endl;
1425 int nid = moleculeTable_.nrow();
1426 Bool firstline = True;
1427 oss << setw(15) << "Rest Freqs:";
1428 for (int i=0; i<nid; i++) {
1429 Table t = table_(table_.col("MOLECULE_ID") == i, 1);
1430 if (t.nrow() > 0) {
1431 Vector<Double> vec(moleculeTable_.getRestFrequency(i));
1432 if (vec.nelements() > 0) {
1433 if (firstline) {
1434 oss << setprecision(10) << vec << " [Hz]" << endl;
1435 firstline=False;
1436 }
1437 else{
1438 oss << setw(15)<<" " << setprecision(10) << vec << " [Hz]" << endl;
1439 }
1440 } else {
1441 oss << "none" << endl;
1442 }
1443 }
1444 }
1445
1446 oss << setw(15) << "Abcissa:" << getAbcissaLabel(0) << endl;
1447 oss << selector_.print() << endl;
1448 return String(oss);
1449}
1450
1451 //std::string Scantable::summary( const std::string& filename )
1452void Scantable::oldsummary( const std::string& filename )
1453{
1454 ostringstream oss;
1455 ofstream ofs;
1456 LogIO ols(LogOrigin("Scantable", "summary", WHERE));
1457
1458 if (filename != "")
1459 ofs.open( filename.c_str(), ios::out );
1460
1461 oss << endl;
1462 oss << asap::SEPERATOR << endl;
1463 oss << " Scan Table Summary" << endl;
1464 oss << asap::SEPERATOR << endl;
1465
1466 // Format header info
1467 oss << oldheaderSummary();
1468 oss << endl;
1469
1470 // main table
1471 String dirtype = "Position ("
1472 + getDirectionRefString()
1473 + ")";
1474 oss.flags(std::ios_base::left);
1475 oss << setw(5) << "Scan" << setw(15) << "Source"
1476 << setw(10) << "Time" << setw(18) << "Integration"
1477 << setw(15) << "Source Type" << endl;
1478 oss << setw(5) << "" << setw(5) << "Beam" << setw(3) << "" << dirtype << endl;
1479 oss << setw(10) << "" << setw(3) << "IF" << setw(3) << ""
1480 << setw(8) << "Frame" << setw(16)
1481 << "RefVal" << setw(10) << "RefPix" << setw(12) << "Increment"
1482 << setw(7) << "Channels"
1483 << endl;
1484 oss << asap::SEPERATOR << endl;
1485
1486 // Flush summary and clear up the string
1487 ols << String(oss) << LogIO::POST;
1488 if (ofs) ofs << String(oss) << flush;
1489 oss.str("");
1490 oss.clear();
1491
1492 TableIterator iter(table_, "SCANNO");
1493 while (!iter.pastEnd()) {
1494 Table subt = iter.table();
1495 ROTableRow row(subt);
1496 MEpoch::ROScalarColumn timeCol(subt,"TIME");
1497 const TableRecord& rec = row.get(0);
1498 oss << setw(4) << std::right << rec.asuInt("SCANNO")
1499 << std::left << setw(1) << ""
1500 << setw(15) << rec.asString("SRCNAME")
1501 << setw(10) << formatTime(timeCol(0), false);
1502 // count the cycles in the scan
1503 TableIterator cyciter(subt, "CYCLENO");
1504 int nint = 0;
1505 while (!cyciter.pastEnd()) {
1506 ++nint;
1507 ++cyciter;
1508 }
1509 oss << setw(3) << std::right << nint << setw(3) << " x " << std::left
1510 << setw(11) << formatSec(rec.asFloat("INTERVAL")) << setw(1) << ""
1511 << setw(15) << SrcType::getName(rec.asInt("SRCTYPE")) << endl;
1512
1513 TableIterator biter(subt, "BEAMNO");
1514 while (!biter.pastEnd()) {
1515 Table bsubt = biter.table();
1516 ROTableRow brow(bsubt);
1517 const TableRecord& brec = brow.get(0);
1518 uInt row0 = bsubt.rowNumbers(table_)[0];
1519 oss << setw(5) << "" << setw(4) << std::right << brec.asuInt("BEAMNO")<< std::left;
1520 oss << setw(4) << "" << formatDirection(getDirection(row0)) << endl;
1521 TableIterator iiter(bsubt, "IFNO");
1522 while (!iiter.pastEnd()) {
1523 Table isubt = iiter.table();
1524 ROTableRow irow(isubt);
1525 const TableRecord& irec = irow.get(0);
1526 oss << setw(9) << "";
1527 oss << setw(3) << std::right << irec.asuInt("IFNO") << std::left
1528 << setw(1) << "" << frequencies().print(irec.asuInt("FREQ_ID"))
1529 << setw(3) << "" << nchan(irec.asuInt("IFNO"))
1530 << endl;
1531
1532 ++iiter;
1533 }
1534 ++biter;
1535 }
1536 // Flush summary every scan and clear up the string
1537 ols << String(oss) << LogIO::POST;
1538 if (ofs) ofs << String(oss) << flush;
1539 oss.str("");
1540 oss.clear();
1541
1542 ++iter;
1543 }
1544 oss << asap::SEPERATOR << endl;
1545 ols << String(oss) << LogIO::POST;
1546 if (ofs) {
1547 ofs << String(oss) << flush;
1548 ofs.close();
1549 }
1550 // return String(oss);
1551}
1552
1553// std::string Scantable::getTime(int whichrow, bool showdate) const
1554// {
1555// MEpoch::ROScalarColumn timeCol(table_, "TIME");
1556// MEpoch me;
1557// if (whichrow > -1) {
1558// me = timeCol(uInt(whichrow));
1559// } else {
1560// Double tm;
1561// table_.keywordSet().get("UTC",tm);
1562// me = MEpoch(MVEpoch(tm));
1563// }
1564// return formatTime(me, showdate);
1565// }
1566
1567std::string Scantable::getTime(int whichrow, bool showdate, uInt prec) const
1568{
1569 MEpoch me;
1570 me = getEpoch(whichrow);
1571 return formatTime(me, showdate, prec);
1572}
1573
1574MEpoch Scantable::getEpoch(int whichrow) const
1575{
1576 if (whichrow > -1) {
1577 return timeCol_(uInt(whichrow));
1578 } else {
1579 Double tm;
1580 table_.keywordSet().get("UTC",tm);
1581 return MEpoch(MVEpoch(tm));
1582 }
1583}
1584
1585std::string Scantable::getDirectionString(int whichrow) const
1586{
1587 return formatDirection(getDirection(uInt(whichrow)));
1588}
1589
1590
1591SpectralCoordinate Scantable::getSpectralCoordinate(int whichrow) const {
1592 const MPosition& mp = getAntennaPosition();
1593 const MDirection& md = getDirection(whichrow);
1594 const MEpoch& me = timeCol_(whichrow);
1595 //Double rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
1596 Vector<Double> rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
1597 return freqTable_.getSpectralCoordinate(md, mp, me, rf,
1598 mfreqidCol_(whichrow));
1599}
1600
1601std::vector< double > Scantable::getAbcissa( int whichrow ) const
1602{
1603 if ( whichrow > int(table_.nrow()) ) throw(AipsError("Illegal row number"));
1604 std::vector<double> stlout;
1605 int nchan = specCol_(whichrow).nelements();
1606 String us = freqTable_.getUnitString();
1607 if ( us == "" || us == "pixel" || us == "channel" ) {
1608 for (int i=0; i<nchan; ++i) {
1609 stlout.push_back(double(i));
1610 }
1611 return stlout;
1612 }
1613 SpectralCoordinate spc = getSpectralCoordinate(whichrow);
1614 Vector<Double> pixel(nchan);
1615 Vector<Double> world;
1616 indgen(pixel);
1617 if ( Unit(us) == Unit("Hz") ) {
1618 for ( int i=0; i < nchan; ++i) {
1619 Double world;
1620 spc.toWorld(world, pixel[i]);
1621 stlout.push_back(double(world));
1622 }
1623 } else if ( Unit(us) == Unit("km/s") ) {
1624 Vector<Double> world;
1625 spc.pixelToVelocity(world, pixel);
1626 world.tovector(stlout);
1627 }
1628 return stlout;
1629}
1630void Scantable::setDirectionRefString( const std::string & refstr )
1631{
1632 MDirection::Types mdt;
1633 if (refstr != "" && !MDirection::getType(mdt, refstr)) {
1634 throw(AipsError("Illegal Direction frame."));
1635 }
1636 if ( refstr == "" ) {
1637 String defaultstr = MDirection::showType(dirCol_.getMeasRef().getType());
1638 table_.rwKeywordSet().define("DIRECTIONREF", defaultstr);
1639 } else {
1640 table_.rwKeywordSet().define("DIRECTIONREF", String(refstr));
1641 }
1642}
1643
1644std::string Scantable::getDirectionRefString( ) const
1645{
1646 return table_.keywordSet().asString("DIRECTIONREF");
1647}
1648
1649MDirection Scantable::getDirection(int whichrow ) const
1650{
1651 String usertype = table_.keywordSet().asString("DIRECTIONREF");
1652 String type = MDirection::showType(dirCol_.getMeasRef().getType());
1653 if ( usertype != type ) {
1654 MDirection::Types mdt;
1655 if (!MDirection::getType(mdt, usertype)) {
1656 throw(AipsError("Illegal Direction frame."));
1657 }
1658 return dirCol_.convert(uInt(whichrow), mdt);
1659 } else {
1660 return dirCol_(uInt(whichrow));
1661 }
1662}
1663
1664std::string Scantable::getAbcissaLabel( int whichrow ) const
1665{
1666 if ( whichrow > int(table_.nrow()) ) throw(AipsError("Illegal ro number"));
1667 const MPosition& mp = getAntennaPosition();
1668 const MDirection& md = getDirection(whichrow);
1669 const MEpoch& me = timeCol_(whichrow);
1670 //const Double& rf = mmolidCol_(whichrow);
1671 const Vector<Double> rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
1672 SpectralCoordinate spc =
1673 freqTable_.getSpectralCoordinate(md, mp, me, rf, mfreqidCol_(whichrow));
1674
1675 String s = "Channel";
1676 Unit u = Unit(freqTable_.getUnitString());
1677 if (u == Unit("km/s")) {
1678 s = CoordinateUtil::axisLabel(spc, 0, True,True, True);
1679 } else if (u == Unit("Hz")) {
1680 Vector<String> wau(1);wau = u.getName();
1681 spc.setWorldAxisUnits(wau);
1682 s = CoordinateUtil::axisLabel(spc, 0, True, True, False);
1683 }
1684 return s;
1685
1686}
1687
1688/**
1689void asap::Scantable::setRestFrequencies( double rf, const std::string& name,
1690 const std::string& unit )
1691**/
1692void Scantable::setRestFrequencies( vector<double> rf, const vector<std::string>& name,
1693 const std::string& unit )
1694
1695{
1696 ///@todo lookup in line table to fill in name and formattedname
1697 Unit u(unit);
1698 //Quantum<Double> urf(rf, u);
1699 Quantum<Vector<Double> >urf(rf, u);
1700 Vector<String> formattedname(0);
1701 //cerr<<"Scantable::setRestFrequnecies="<<urf<<endl;
1702
1703 //uInt id = moleculeTable_.addEntry(urf.getValue("Hz"), name, "");
1704 uInt id = moleculeTable_.addEntry(urf.getValue("Hz"), mathutil::toVectorString(name), formattedname);
1705 TableVector<uInt> tabvec(table_, "MOLECULE_ID");
1706 tabvec = id;
1707}
1708
1709/**
1710void asap::Scantable::setRestFrequencies( const std::string& name )
1711{
1712 throw(AipsError("setRestFrequencies( const std::string& name ) NYI"));
1713 ///@todo implement
1714}
1715**/
1716
1717void Scantable::setRestFrequencies( const vector<std::string>& name )
1718{
1719 (void) name; // suppress unused warning
1720 throw(AipsError("setRestFrequencies( const vector<std::string>& name ) NYI"));
1721 ///@todo implement
1722}
1723
1724std::vector< unsigned int > Scantable::rownumbers( ) const
1725{
1726 std::vector<unsigned int> stlout;
1727 Vector<uInt> vec = table_.rowNumbers();
1728 vec.tovector(stlout);
1729 return stlout;
1730}
1731
1732
1733Matrix<Float> Scantable::getPolMatrix( uInt whichrow ) const
1734{
1735 ROTableRow row(table_);
1736 const TableRecord& rec = row.get(whichrow);
1737 Table t =
1738 originalTable_( originalTable_.col("SCANNO") == Int(rec.asuInt("SCANNO"))
1739 && originalTable_.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
1740 && originalTable_.col("IFNO") == Int(rec.asuInt("IFNO"))
1741 && originalTable_.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
1742 ROArrayColumn<Float> speccol(t, "SPECTRA");
1743 return speccol.getColumn();
1744}
1745
1746std::vector< std::string > Scantable::columnNames( ) const
1747{
1748 Vector<String> vec = table_.tableDesc().columnNames();
1749 return mathutil::tovectorstring(vec);
1750}
1751
1752MEpoch::Types Scantable::getTimeReference( ) const
1753{
1754 return MEpoch::castType(timeCol_.getMeasRef().getType());
1755}
1756
1757void Scantable::addFit( const STFitEntry& fit, int row )
1758{
1759 //cout << mfitidCol_(uInt(row)) << endl;
1760 LogIO os( LogOrigin( "Scantable", "addFit()", WHERE ) ) ;
1761 os << mfitidCol_(uInt(row)) << LogIO::POST ;
1762 uInt id = fitTable_.addEntry(fit, mfitidCol_(uInt(row)));
1763 mfitidCol_.put(uInt(row), id);
1764}
1765
1766void Scantable::shift(int npix)
1767{
1768 Vector<uInt> fids(mfreqidCol_.getColumn());
1769 genSort( fids, Sort::Ascending,
1770 Sort::QuickSort|Sort::NoDuplicates );
1771 for (uInt i=0; i<fids.nelements(); ++i) {
1772 frequencies().shiftRefPix(npix, fids[i]);
1773 }
1774}
1775
1776String Scantable::getAntennaName() const
1777{
1778 String out;
1779 table_.keywordSet().get("AntennaName", out);
1780 String::size_type pos1 = out.find("@") ;
1781 String::size_type pos2 = out.find("//") ;
1782 if ( pos2 != String::npos )
1783 out = out.substr(pos2+2,pos1-pos2-2) ;
1784 else if ( pos1 != String::npos )
1785 out = out.substr(0,pos1) ;
1786 return out;
1787}
1788
1789int Scantable::checkScanInfo(const std::vector<int>& scanlist) const
1790{
1791 String tbpath;
1792 int ret = 0;
1793 if ( table_.keywordSet().isDefined("GBT_GO") ) {
1794 table_.keywordSet().get("GBT_GO", tbpath);
1795 Table t(tbpath,Table::Old);
1796 // check each scan if other scan of the pair exist
1797 int nscan = scanlist.size();
1798 for (int i = 0; i < nscan; i++) {
1799 Table subt = t( t.col("SCAN") == scanlist[i] );
1800 if (subt.nrow()==0) {
1801 //cerr <<"Scan "<<scanlist[i]<<" cannot be found in the scantable."<<endl;
1802 LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
1803 os <<LogIO::WARN<<"Scan "<<scanlist[i]<<" cannot be found in the scantable."<<LogIO::POST;
1804 ret = 1;
1805 break;
1806 }
1807 ROTableRow row(subt);
1808 const TableRecord& rec = row.get(0);
1809 int scan1seqn = rec.asuInt("PROCSEQN");
1810 int laston1 = rec.asuInt("LASTON");
1811 if ( rec.asuInt("PROCSIZE")==2 ) {
1812 if ( i < nscan-1 ) {
1813 Table subt2 = t( t.col("SCAN") == scanlist[i+1] );
1814 if ( subt2.nrow() == 0) {
1815 LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
1816
1817 //cerr<<"Scan "<<scanlist[i+1]<<" cannot be found in the scantable."<<endl;
1818 os<<LogIO::WARN<<"Scan "<<scanlist[i+1]<<" cannot be found in the scantable."<<LogIO::POST;
1819 ret = 1;
1820 break;
1821 }
1822 ROTableRow row2(subt2);
1823 const TableRecord& rec2 = row2.get(0);
1824 int scan2seqn = rec2.asuInt("PROCSEQN");
1825 int laston2 = rec2.asuInt("LASTON");
1826 if (scan1seqn == 1 && scan2seqn == 2) {
1827 if (laston1 == laston2) {
1828 LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
1829 //cerr<<"A valid scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<endl;
1830 os<<"A valid scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<LogIO::POST;
1831 i +=1;
1832 }
1833 else {
1834 LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
1835 //cerr<<"Incorrect scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<endl;
1836 os<<LogIO::WARN<<"Incorrect scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<LogIO::POST;
1837 }
1838 }
1839 else if (scan1seqn==2 && scan2seqn == 1) {
1840 if (laston1 == laston2) {
1841 LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
1842 //cerr<<"["<<scanlist[i]<<","<<scanlist[i+1]<<"] is a valid scan pair but in incorrect order."<<endl;
1843 os<<LogIO::WARN<<"["<<scanlist[i]<<","<<scanlist[i+1]<<"] is a valid scan pair but in incorrect order."<<LogIO::POST;
1844 ret = 1;
1845 break;
1846 }
1847 }
1848 else {
1849 LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
1850 //cerr<<"The other scan for "<<scanlist[i]<<" appears to be missing. Check the input scan numbers."<<endl;
1851 os<<LogIO::WARN<<"The other scan for "<<scanlist[i]<<" appears to be missing. Check the input scan numbers."<<LogIO::POST;
1852 ret = 1;
1853 break;
1854 }
1855 }
1856 }
1857 else {
1858 LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
1859 //cerr<<"The scan does not appear to be standard obsevation."<<endl;
1860 os<<LogIO::WARN<<"The scan does not appear to be standard obsevation."<<LogIO::POST;
1861 }
1862 //if ( i >= nscan ) break;
1863 }
1864 }
1865 else {
1866 LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
1867 //cerr<<"No reference to GBT_GO table."<<endl;
1868 os<<LogIO::WARN<<"No reference to GBT_GO table."<<LogIO::POST;
1869 ret = 1;
1870 }
1871 return ret;
1872}
1873
1874std::vector<double> Scantable::getDirectionVector(int whichrow) const
1875{
1876 Vector<Double> Dir = dirCol_(whichrow).getAngle("rad").getValue();
1877 std::vector<double> dir;
1878 Dir.tovector(dir);
1879 return dir;
1880}
1881
1882void asap::Scantable::reshapeSpectrum( int nmin, int nmax )
1883 throw( casacore::AipsError )
1884{
1885 // assumed that all rows have same nChan
1886 Vector<Float> arr = specCol_( 0 ) ;
1887 int nChan = arr.nelements() ;
1888
1889 // if nmin < 0 or nmax < 0, nothing to do
1890 if ( nmin < 0 ) {
1891 throw( casacore::indexError<int>( nmin, "asap::Scantable::reshapeSpectrum: Invalid range. Negative index is specified." ) ) ;
1892 }
1893 if ( nmax < 0 ) {
1894 throw( casacore::indexError<int>( nmax, "asap::Scantable::reshapeSpectrum: Invalid range. Negative index is specified." ) ) ;
1895 }
1896
1897 // if nmin > nmax, exchange values
1898 if ( nmin > nmax ) {
1899 int tmp = nmax ;
1900 nmax = nmin ;
1901 nmin = tmp ;
1902 LogIO os( LogOrigin( "Scantable", "reshapeSpectrum()", WHERE ) ) ;
1903 os << "Swap values. Applied range is ["
1904 << nmin << ", " << nmax << "]" << LogIO::POST ;
1905 }
1906
1907 // if nmin exceeds nChan, nothing to do
1908 if ( nmin >= nChan ) {
1909 throw( casacore::indexError<int>( nmin, "asap::Scantable::reshapeSpectrum: Invalid range. Specified minimum exceeds nChan." ) ) ;
1910 }
1911
1912 // if nmax exceeds nChan, reset nmax to nChan
1913 if ( nmax >= nChan-1 ) {
1914 if ( nmin == 0 ) {
1915 // nothing to do
1916 LogIO os( LogOrigin( "Scantable", "reshapeSpectrum()", WHERE ) ) ;
1917 os << "Whole range is selected. Nothing to do." << LogIO::POST ;
1918 return ;
1919 }
1920 else {
1921 LogIO os( LogOrigin( "Scantable", "reshapeSpectrum()", WHERE ) ) ;
1922 os << "Specified maximum exceeds nChan. Applied range is ["
1923 << nmin << ", " << nChan-1 << "]." << LogIO::POST ;
1924 nmax = nChan - 1 ;
1925 }
1926 }
1927
1928 // reshape specCol_ and flagCol_
1929 for ( int irow = 0 ; irow < nrow() ; irow++ ) {
1930 reshapeSpectrum( nmin, nmax, irow ) ;
1931 }
1932
1933 // update FREQUENCIES subtable
1934 Vector<uInt> freqIdArray = mfreqidCol_.getColumn();
1935 uInt numFreqId = GenSort<uInt>::sort(freqIdArray, Sort::Ascending,
1936 Sort::HeapSort | Sort::NoDuplicates);
1937 Double refpix ;
1938 Double refval ;
1939 Double increment ;
1940 for (uInt irow = 0; irow < numFreqId; irow++) {
1941 freqTable_.getEntry( refpix, refval, increment, freqIdArray[irow] ) ;
1942 /***
1943 * need to shift refpix to nmin
1944 * note that channel nmin in old index will be channel 0 in new one
1945 ***/
1946 refval = refval - ( refpix - nmin ) * increment ;
1947 refpix = 0 ;
1948 freqTable_.setEntry( refpix, refval, increment, freqIdArray[irow] ) ;
1949 }
1950
1951 // update nchan
1952 int newsize = nmax - nmin + 1 ;
1953 table_.rwKeywordSet().define( "nChan", newsize ) ;
1954
1955 // update bandwidth
1956 // assumed all spectra in the scantable have same bandwidth
1957 table_.rwKeywordSet().define( "Bandwidth", increment * newsize ) ;
1958
1959 return ;
1960}
1961
1962void asap::Scantable::reshapeSpectrum( int nmin, int nmax, int irow )
1963{
1964 // reshape specCol_ and flagCol_
1965 Vector<Float> oldspec = specCol_( irow ) ;
1966 Vector<uChar> oldflag = flagsCol_( irow ) ;
1967 Vector<Float> oldtsys = tsysCol_( irow ) ;
1968 uInt newsize = nmax - nmin + 1 ;
1969 Slice slice( nmin, newsize, 1 ) ;
1970 specCol_.put( irow, oldspec( slice ) ) ;
1971 flagsCol_.put( irow, oldflag( slice ) ) ;
1972 if ( oldspec.size() == oldtsys.size() )
1973 tsysCol_.put( irow, oldtsys( slice ) ) ;
1974
1975 return ;
1976}
1977
1978void asap::Scantable::regridSpecChannel( double dnu, int nChan )
1979{
1980 LogIO os( LogOrigin( "Scantable", "regridChannel()", WHERE ) ) ;
1981 os << "Regrid abcissa with spectral resoultion " << dnu << " " << freqTable_.getUnitString() << " with channel number " << ((nChan>0)? String(nChan) : "covering band width")<< LogIO::POST ;
1982 int freqnrow = freqTable_.table().nrow() ;
1983 Vector<bool> firstTime( freqnrow, true ) ;
1984 double oldincr, factor;
1985 uInt currId;
1986 Double refpix ;
1987 Double refval ;
1988 Double increment ;
1989 for ( int irow = 0 ; irow < nrow() ; irow++ ) {
1990 currId = mfreqidCol_(irow);
1991 vector<double> abcissa = getAbcissa( irow ) ;
1992 if (nChan < 0) {
1993 int oldsize = abcissa.size() ;
1994 double bw = (abcissa[oldsize-1]-abcissa[0]) + \
1995 0.5 * (abcissa[1]-abcissa[0] + abcissa[oldsize-1]-abcissa[oldsize-2]) ;
1996 nChan = int( ceil( abs(bw/dnu) ) ) ;
1997 }
1998 // actual regridding
1999 regridChannel( nChan, dnu, irow ) ;
2000
2001 // update FREQUENCIES subtable
2002 if (firstTime[currId]) {
2003 oldincr = abcissa[1]-abcissa[0] ;
2004 factor = dnu/oldincr ;
2005 firstTime[currId] = false ;
2006 freqTable_.getEntry( refpix, refval, increment, currId ) ;
2007
2008 //refval = refval - ( refpix + 0.5 * (1 - factor) ) * increment ;
2009 if (factor > 0 ) {
2010 refpix = (refpix + 0.5)/factor - 0.5;
2011 } else {
2012 refpix = (abcissa.size() - 0.5 - refpix)/abs(factor) - 0.5;
2013 }
2014 freqTable_.setEntry( refpix, refval, increment*factor, currId ) ;
2015 //os << "ID" << currId << ": channel width (Orig) = " << oldincr << " [" << freqTable_.getUnitString() << "], scale factor = " << factor << LogIO::POST ;
2016 //os << " frequency increment (Orig) = " << increment << "-> (New) " << increment*factor << LogIO::POST ;
2017 }
2018 }
2019}
2020
2021void asap::Scantable::regridChannel( int nChan, double dnu )
2022{
2023 LogIO os( LogOrigin( "Scantable", "regridChannel()", WHERE ) ) ;
2024 os << "Regrid abcissa with channel number " << nChan << " and spectral resoultion " << dnu << "Hz." << LogIO::POST ;
2025 // assumed that all rows have same nChan
2026 Vector<Float> arr = specCol_( 0 ) ;
2027 int oldsize = arr.nelements() ;
2028
2029 // if oldsize == nChan, nothing to do
2030 if ( oldsize == nChan ) {
2031 os << "Specified channel number is same as current one. Nothing to do." << LogIO::POST ;
2032 return ;
2033 }
2034
2035 // if oldChan < nChan, unphysical operation
2036 if ( oldsize < nChan ) {
2037 os << "Unphysical operation. Nothing to do." << LogIO::POST ;
2038 return ;
2039 }
2040
2041 // change channel number for specCol_, flagCol_, and tsysCol_ (if necessary)
2042 vector<string> coordinfo = getCoordInfo() ;
2043 string oldinfo = coordinfo[0] ;
2044 coordinfo[0] = "Hz" ;
2045 setCoordInfo( coordinfo ) ;
2046 for ( int irow = 0 ; irow < nrow() ; irow++ ) {
2047 regridChannel( nChan, dnu, irow ) ;
2048 }
2049 coordinfo[0] = oldinfo ;
2050 setCoordInfo( coordinfo ) ;
2051
2052
2053 // NOTE: this method does not update metadata such as
2054 // FREQUENCIES subtable, nChan, Bandwidth, etc.
2055
2056 return ;
2057}
2058
2059void asap::Scantable::regridChannel( int nChan, double dnu, int irow )
2060{
2061 // logging
2062 //ofstream ofs( "average.log", std::ios::out | std::ios::app ) ;
2063 //ofs << "IFNO = " << getIF( irow ) << " irow = " << irow << endl ;
2064
2065 Vector<Float> oldspec = specCol_( irow ) ;
2066 Vector<uChar> oldflag = flagsCol_( irow ) ;
2067 Vector<Float> oldtsys = tsysCol_( irow ) ;
2068 Vector<Float> newspec( nChan, 0 ) ;
2069 Vector<uChar> newflag( nChan, true ) ;
2070 Vector<Float> newtsys ;
2071 bool regridTsys = false ;
2072 if (oldtsys.size() == oldspec.size()) {
2073 regridTsys = true ;
2074 newtsys.resize(nChan,false) ;
2075 newtsys = 0 ;
2076 }
2077
2078 // regrid
2079 vector<double> abcissa = getAbcissa( irow ) ;
2080 int oldsize = abcissa.size() ;
2081 double olddnu = abcissa[1] - abcissa[0] ;
2082 //int ichan = 0 ;
2083 double wsum = 0.0 ;
2084 Vector<double> zi( nChan+1 ) ;
2085 Vector<double> yi( oldsize + 1 ) ;
2086 yi[0] = abcissa[0] - 0.5 * olddnu ;
2087 for ( int ii = 1 ; ii < oldsize ; ii++ )
2088 yi[ii] = 0.5* (abcissa[ii-1] + abcissa[ii]) ;
2089 yi[oldsize] = abcissa[oldsize-1] \
2090 + 0.5 * (abcissa[oldsize-1] - abcissa[oldsize-2]) ;
2091 //zi[0] = abcissa[0] - 0.5 * olddnu ;
2092 zi[0] = ((olddnu*dnu > 0) ? yi[0] : yi[oldsize]) ;
2093 for ( int ii = 1 ; ii < nChan ; ii++ )
2094 zi[ii] = zi[0] + dnu * ii ;
2095 zi[nChan] = zi[nChan-1] + dnu ;
2096 // Access zi and yi in ascending order
2097 int izs = ((dnu > 0) ? 0 : nChan ) ;
2098 int ize = ((dnu > 0) ? nChan : 0 ) ;
2099 int izincr = ((dnu > 0) ? 1 : -1 ) ;
2100 int ichan = ((olddnu > 0) ? 0 : oldsize ) ;
2101 int iye = ((olddnu > 0) ? oldsize : 0 ) ;
2102 int iyincr = ((olddnu > 0) ? 1 : -1 ) ;
2103 //for ( int ii = izs ; ii != ize ; ii+=izincr ){
2104 int ii = izs ;
2105 while (ii != ize) {
2106 // always zl < zr
2107 double zl = zi[ii] ;
2108 double zr = zi[ii+izincr] ;
2109 // Need to access smaller index for the new spec, flag, and tsys.
2110 // Values between zi[k] and zi[k+1] should be stored in newspec[k], etc.
2111 int i = min(ii, ii+izincr) ;
2112 //for ( int jj = ichan ; jj != iye ; jj+=iyincr ) {
2113 int jj = ichan ;
2114 while (jj != iye) {
2115 // always yl < yr
2116 double yl = yi[jj] ;
2117 double yr = yi[jj+iyincr] ;
2118 // Need to access smaller index for the original spec, flag, and tsys.
2119 // Values between yi[k] and yi[k+1] are stored in oldspec[k], etc.
2120 int j = min(jj, jj+iyincr) ;
2121 if ( yr <= zl ) {
2122 jj += iyincr ;
2123 continue ;
2124 }
2125 else if ( yl <= zl ) {
2126 if ( yr < zr ) {
2127 if (!oldflag[j]) {
2128 newspec[i] += oldspec[j] * ( yr - zl ) ;
2129 if (regridTsys) newtsys[i] += oldtsys[j] * ( yr - zl ) ;
2130 wsum += ( yr - zl ) ;
2131 }
2132 newflag[i] = (newflag[i] && oldflag[j]) ? 1 << 7 : 0 ;
2133 }
2134 else {
2135 if (!oldflag[j]) {
2136 newspec[i] += oldspec[j] * abs(dnu) ;
2137 if (regridTsys) newtsys[i] += oldtsys[j] * abs(dnu) ;
2138 wsum += abs(dnu) ;
2139 }
2140 newflag[i] = (newflag[i] && oldflag[j]) ? 1 << 7 : 0 ;
2141 ichan = jj ;
2142 break ;
2143 }
2144 }
2145 else if ( yl < zr ) {
2146 if ( yr <= zr ) {
2147 if (!oldflag[j]) {
2148 newspec[i] += oldspec[j] * ( yr - yl ) ;
2149 if (regridTsys) newtsys[i] += oldtsys[j] * ( yr - yl ) ;
2150 wsum += ( yr - yl ) ;
2151 }
2152 newflag[i] = (newflag[i] && oldflag[j]) ? 1 << 7 : 0 ;
2153 }
2154 else {
2155 if (!oldflag[j]) {
2156 newspec[i] += oldspec[j] * ( zr - yl ) ;
2157 if (regridTsys) newtsys[i] += oldtsys[j] * ( zr - yl ) ;
2158 wsum += ( zr - yl ) ;
2159 }
2160 newflag[i] = (newflag[i] && oldflag[j]) ? 1 << 7 : 0 ;
2161 ichan = jj ;
2162 break ;
2163 }
2164 }
2165 else {
2166 ichan = jj - iyincr ;
2167 break ;
2168 }
2169 jj += iyincr ;
2170 }
2171 if ( wsum != 0.0 ) {
2172 newspec[i] /= wsum ;
2173 if (regridTsys) newtsys[i] /= wsum ;
2174 }
2175 wsum = 0.0 ;
2176 ii += izincr ;
2177 }
2178// if ( dnu > 0.0 ) {
2179// for ( int ii = 0 ; ii < nChan ; ii++ ) {
2180// double zl = zi[ii] ;
2181// double zr = zi[ii+1] ;
2182// for ( int j = ichan ; j < oldsize ; j++ ) {
2183// double yl = yi[j] ;
2184// double yr = yi[j+1] ;
2185// if ( yl <= zl ) {
2186// if ( yr <= zl ) {
2187// continue ;
2188// }
2189// else if ( yr <= zr ) {
2190// if (!oldflag[j]) {
2191// newspec[ii] += oldspec[j] * ( yr - zl ) ;
2192// if (regridTsys) newtsys[ii] += oldtsys[j] * ( yr - zl ) ;
2193// wsum += ( yr - zl ) ;
2194// }
2195// newflag[ii] = newflag[ii] && oldflag[j] ;
2196// }
2197// else {
2198// if (!oldflag[j]) {
2199// newspec[ii] += oldspec[j] * dnu ;
2200// if (regridTsys) newtsys[ii] += oldtsys[j] * dnu ;
2201// wsum += dnu ;
2202// }
2203// newflag[ii] = newflag[ii] && oldflag[j] ;
2204// ichan = j ;
2205// break ;
2206// }
2207// }
2208// else if ( yl < zr ) {
2209// if ( yr <= zr ) {
2210// if (!oldflag[j]) {
2211// newspec[ii] += oldspec[j] * ( yr - yl ) ;
2212// if (regridTsys) newtsys[ii] += oldtsys[j] * ( yr - yl ) ;
2213// wsum += ( yr - yl ) ;
2214// }
2215// newflag[ii] = newflag[ii] && oldflag[j] ;
2216// }
2217// else {
2218// if (!oldflag[j]) {
2219// newspec[ii] += oldspec[j] * ( zr - yl ) ;
2220// if (regridTsys) newtsys[ii] += oldtsys[j] * ( zr - yl ) ;
2221// wsum += ( zr - yl ) ;
2222// }
2223// newflag[ii] = newflag[ii] && oldflag[j] ;
2224// ichan = j ;
2225// break ;
2226// }
2227// }
2228// else {
2229// ichan = j - 1 ;
2230// break ;
2231// }
2232// }
2233// if ( wsum != 0.0 ) {
2234// newspec[ii] /= wsum ;
2235// if (regridTsys) newtsys[ii] /= wsum ;
2236// }
2237// wsum = 0.0 ;
2238// }
2239// }
2240// else if ( dnu < 0.0 ) {
2241// for ( int ii = 0 ; ii < nChan ; ii++ ) {
2242// double zl = zi[ii] ;
2243// double zr = zi[ii+1] ;
2244// for ( int j = ichan ; j < oldsize ; j++ ) {
2245// double yl = yi[j] ;
2246// double yr = yi[j+1] ;
2247// if ( yl >= zl ) {
2248// if ( yr >= zl ) {
2249// continue ;
2250// }
2251// else if ( yr >= zr ) {
2252// if (!oldflag[j]) {
2253// newspec[ii] += oldspec[j] * abs( yr - zl ) ;
2254// if (regridTsys) newtsys[ii] += oldtsys[j] * abs( yr - zl ) ;
2255// wsum += abs( yr - zl ) ;
2256// }
2257// newflag[ii] = newflag[ii] && oldflag[j] ;
2258// }
2259// else {
2260// if (!oldflag[j]) {
2261// newspec[ii] += oldspec[j] * abs( dnu ) ;
2262// if (regridTsys) newtsys[ii] += oldtsys[j] * abs( dnu ) ;
2263// wsum += abs( dnu ) ;
2264// }
2265// newflag[ii] = newflag[ii] && oldflag[j] ;
2266// ichan = j ;
2267// break ;
2268// }
2269// }
2270// else if ( yl > zr ) {
2271// if ( yr >= zr ) {
2272// if (!oldflag[j]) {
2273// newspec[ii] += oldspec[j] * abs( yr - yl ) ;
2274// if (regridTsys) newtsys[ii] += oldtsys[j] * abs( yr - yl ) ;
2275// wsum += abs( yr - yl ) ;
2276// }
2277// newflag[ii] = newflag[ii] && oldflag[j] ;
2278// }
2279// else {
2280// if (!oldflag[j]) {
2281// newspec[ii] += oldspec[j] * abs( zr - yl ) ;
2282// if (regridTsys) newtsys[ii] += oldtsys[j] * abs( zr - yl ) ;
2283// wsum += abs( zr - yl ) ;
2284// }
2285// newflag[ii] = newflag[ii] && oldflag[j] ;
2286// ichan = j ;
2287// break ;
2288// }
2289// }
2290// else {
2291// ichan = j - 1 ;
2292// break ;
2293// }
2294// }
2295// if ( wsum != 0.0 ) {
2296// newspec[ii] /= wsum ;
2297// if (regridTsys) newtsys[ii] /= wsum ;
2298// }
2299// wsum = 0.0 ;
2300// }
2301// }
2302// // //ofs << "olddnu = " << olddnu << ", dnu = " << dnu << endl ;
2303// // pile += dnu ;
2304// // wedge = olddnu * ( refChan + 1 ) ;
2305// // while ( wedge < pile ) {
2306// // newspec[0] += olddnu * oldspec[refChan] ;
2307// // newflag[0] = newflag[0] || oldflag[refChan] ;
2308// // //ofs << "channel " << refChan << " is included in new channel 0" << endl ;
2309// // refChan++ ;
2310// // wedge += olddnu ;
2311// // wsum += olddnu ;
2312// // //ofs << "newspec[0] = " << newspec[0] << " wsum = " << wsum << endl ;
2313// // }
2314// // frac = ( wedge - pile ) / olddnu ;
2315// // wsum += ( 1.0 - frac ) * olddnu ;
2316// // newspec[0] += ( 1.0 - frac ) * olddnu * oldspec[refChan] ;
2317// // newflag[0] = newflag[0] || oldflag[refChan] ;
2318// // //ofs << "channel " << refChan << " is partly included in new channel 0" << " with fraction of " << ( 1.0 - frac ) << endl ;
2319// // //ofs << "newspec[0] = " << newspec[0] << " wsum = " << wsum << endl ;
2320// // newspec[0] /= wsum ;
2321// // //ofs << "newspec[0] = " << newspec[0] << endl ;
2322// // //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
2323
2324// // /***
2325// // * ichan = 1 - nChan-2
2326// // ***/
2327// // for ( int ichan = 1 ; ichan < nChan - 1 ; ichan++ ) {
2328// // pile += dnu ;
2329// // newspec[ichan] += frac * olddnu * oldspec[refChan] ;
2330// // newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
2331// // //ofs << "channel " << refChan << " is partly included in new channel " << ichan << " with fraction of " << frac << endl ;
2332// // refChan++ ;
2333// // wedge += olddnu ;
2334// // wsum = frac * olddnu ;
2335// // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
2336// // while ( wedge < pile ) {
2337// // newspec[ichan] += olddnu * oldspec[refChan] ;
2338// // newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
2339// // //ofs << "channel " << refChan << " is included in new channel " << ichan << endl ;
2340// // refChan++ ;
2341// // wedge += olddnu ;
2342// // wsum += olddnu ;
2343// // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
2344// // }
2345// // frac = ( wedge - pile ) / olddnu ;
2346// // wsum += ( 1.0 - frac ) * olddnu ;
2347// // newspec[ichan] += ( 1.0 - frac ) * olddnu * oldspec[refChan] ;
2348// // newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
2349// // //ofs << "channel " << refChan << " is partly included in new channel " << ichan << " with fraction of " << ( 1.0 - frac ) << endl ;
2350// // //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
2351// // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
2352// // newspec[ichan] /= wsum ;
2353// // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << endl ;
2354// // }
2355
2356// // /***
2357// // * ichan = nChan-1
2358// // ***/
2359// // // NOTE: Assumed that all spectra have the same bandwidth
2360// // pile += dnu ;
2361// // newspec[nChan-1] += frac * olddnu * oldspec[refChan] ;
2362// // newflag[nChan-1] = newflag[nChan-1] || oldflag[refChan] ;
2363// // //ofs << "channel " << refChan << " is partly included in new channel " << nChan-1 << " with fraction of " << frac << endl ;
2364// // refChan++ ;
2365// // wedge += olddnu ;
2366// // wsum = frac * olddnu ;
2367// // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
2368// // for ( int jchan = refChan ; jchan < oldsize ; jchan++ ) {
2369// // newspec[nChan-1] += olddnu * oldspec[jchan] ;
2370// // newflag[nChan-1] = newflag[nChan-1] || oldflag[jchan] ;
2371// // wsum += olddnu ;
2372// // //ofs << "channel " << jchan << " is included in new channel " << nChan-1 << " with fraction of " << frac << endl ;
2373// // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
2374// // }
2375// // //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
2376// // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
2377// // newspec[nChan-1] /= wsum ;
2378// // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << endl ;
2379
2380// // // ofs.close() ;
2381
2382 specCol_.put( irow, newspec ) ;
2383 flagsCol_.put( irow, newflag ) ;
2384 if (regridTsys) tsysCol_.put( irow, newtsys );
2385
2386 return ;
2387}
2388
2389void Scantable::regridChannel( int nChan, double dnu, double fmin, int irow )
2390{
2391 Vector<Float> oldspec = specCol_( irow ) ;
2392 Vector<uChar> oldflag = flagsCol_( irow ) ;
2393 Vector<Float> oldtsys = tsysCol_( irow ) ;
2394 Vector<Float> newspec( nChan, 0 ) ;
2395 Vector<uChar> newflag( nChan, true ) ;
2396 Vector<Float> newtsys ;
2397 bool regridTsys = false ;
2398 if (oldtsys.size() == oldspec.size()) {
2399 regridTsys = true ;
2400 newtsys.resize(nChan,false) ;
2401 newtsys = 0 ;
2402 }
2403
2404 // regrid
2405 vector<double> abcissa = getAbcissa( irow ) ;
2406 int oldsize = abcissa.size() ;
2407 double olddnu = abcissa[1] - abcissa[0] ;
2408 //int ichan = 0 ;
2409 double wsum = 0.0 ;
2410 Vector<double> zi( nChan+1 ) ;
2411 Vector<double> yi( oldsize + 1 ) ;
2412 Block<uInt> count( nChan, 0 ) ;
2413 yi[0] = abcissa[0] - 0.5 * olddnu ;
2414 for ( int ii = 1 ; ii < oldsize ; ii++ )
2415 yi[ii] = 0.5* (abcissa[ii-1] + abcissa[ii]) ;
2416 yi[oldsize] = abcissa[oldsize-1] \
2417 + 0.5 * (abcissa[oldsize-1] - abcissa[oldsize-2]) ;
2418// cout << "olddnu=" << olddnu << ", dnu=" << dnu << " (diff=" << olddnu-dnu << ")" << endl ;
2419// cout << "yi[0]=" << yi[0] << ", fmin=" << fmin << " (diff=" << yi[0]-fmin << ")" << endl ;
2420// cout << "oldsize=" << oldsize << ", nChan=" << nChan << endl ;
2421
2422 // do not regrid if input parameters are almost same as current
2423 // spectral setup
2424 double dnuDiff = abs( ( dnu - olddnu ) / olddnu ) ;
2425 double oldfmin = min( yi[0], yi[oldsize] ) ;
2426 double fminDiff = abs( ( fmin - oldfmin ) / oldfmin ) ;
2427 double nChanDiff = nChan - oldsize ;
2428 double eps = 1.0e-8 ;
2429 if ( nChanDiff == 0 && dnuDiff < eps && fminDiff < eps )
2430 return ;
2431
2432 //zi[0] = abcissa[0] - 0.5 * olddnu ;
2433 //zi[0] = ((olddnu*dnu > 0) ? yi[0] : yi[oldsize]) ;
2434 if ( dnu > 0 )
2435 zi[0] = fmin - 0.5 * dnu ;
2436 else
2437 zi[0] = fmin + nChan * abs(dnu) ;
2438 for ( int ii = 1 ; ii < nChan ; ii++ )
2439 zi[ii] = zi[0] + dnu * ii ;
2440 zi[nChan] = zi[nChan-1] + dnu ;
2441 // Access zi and yi in ascending order
2442 int izs = ((dnu > 0) ? 0 : nChan ) ;
2443 int ize = ((dnu > 0) ? nChan : 0 ) ;
2444 int izincr = ((dnu > 0) ? 1 : -1 ) ;
2445 int ichan = ((olddnu > 0) ? 0 : oldsize ) ;
2446 int iye = ((olddnu > 0) ? oldsize : 0 ) ;
2447 int iyincr = ((olddnu > 0) ? 1 : -1 ) ;
2448 //for ( int ii = izs ; ii != ize ; ii+=izincr ){
2449 int ii = izs ;
2450 while (ii != ize) {
2451 // always zl < zr
2452 double zl = zi[ii] ;
2453 double zr = zi[ii+izincr] ;
2454 // Need to access smaller index for the new spec, flag, and tsys.
2455 // Values between zi[k] and zi[k+1] should be stored in newspec[k], etc.
2456 int i = min(ii, ii+izincr) ;
2457 //for ( int jj = ichan ; jj != iye ; jj+=iyincr ) {
2458 int jj = ichan ;
2459 while (jj != iye) {
2460 // always yl < yr
2461 double yl = yi[jj] ;
2462 double yr = yi[jj+iyincr] ;
2463 // Need to access smaller index for the original spec, flag, and tsys.
2464 // Values between yi[k] and yi[k+1] are stored in oldspec[k], etc.
2465 int j = min(jj, jj+iyincr) ;
2466 if ( yr <= zl ) {
2467 jj += iyincr ;
2468 continue ;
2469 }
2470 else if ( yl <= zl ) {
2471 if ( yr < zr ) {
2472 if (!oldflag[j]) {
2473 newspec[i] += oldspec[j] * ( yr - zl ) ;
2474 if (regridTsys) newtsys[i] += oldtsys[j] * ( yr - zl ) ;
2475 wsum += ( yr - zl ) ;
2476 count[i]++ ;
2477 }
2478 newflag[i] = newflag[i] && oldflag[j] ;
2479 }
2480 else {
2481 if (!oldflag[j]) {
2482 newspec[i] += oldspec[j] * abs(dnu) ;
2483 if (regridTsys) newtsys[i] += oldtsys[j] * abs(dnu) ;
2484 wsum += abs(dnu) ;
2485 count[i]++ ;
2486 }
2487 newflag[i] = newflag[i] && oldflag[j] ;
2488 ichan = jj ;
2489 break ;
2490 }
2491 }
2492 else if ( yl < zr ) {
2493 if ( yr <= zr ) {
2494 if (!oldflag[j]) {
2495 newspec[i] += oldspec[j] * ( yr - yl ) ;
2496 if (regridTsys) newtsys[i] += oldtsys[j] * ( yr - yl ) ;
2497 wsum += ( yr - yl ) ;
2498 count[i]++ ;
2499 }
2500 newflag[i] = newflag[i] && oldflag[j] ;
2501 }
2502 else {
2503 if (!oldflag[j]) {
2504 newspec[i] += oldspec[j] * ( zr - yl ) ;
2505 if (regridTsys) newtsys[i] += oldtsys[j] * ( zr - yl ) ;
2506 wsum += ( zr - yl ) ;
2507 count[i]++ ;
2508 }
2509 newflag[i] = newflag[i] && oldflag[j] ;
2510 ichan = jj ;
2511 break ;
2512 }
2513 }
2514 else {
2515 //ichan = jj - iyincr ;
2516 break ;
2517 }
2518 jj += iyincr ;
2519 }
2520 if ( wsum != 0.0 ) {
2521 newspec[i] /= wsum ;
2522 if (regridTsys) newtsys[i] /= wsum ;
2523 }
2524 wsum = 0.0 ;
2525 ii += izincr ;
2526 }
2527
2528 // flag out channels without data
2529 // this is tentative since there is no specific definition
2530 // on bit flag...
2531 uChar noData = 1 << 7 ;
2532 for ( Int i = 0 ; i < nChan ; i++ ) {
2533 if ( count[i] == 0 )
2534 newflag[i] = noData ;
2535 }
2536
2537 specCol_.put( irow, newspec ) ;
2538 flagsCol_.put( irow, newflag ) ;
2539 if (regridTsys) tsysCol_.put( irow, newtsys );
2540
2541 return ;
2542}
2543
2544std::vector<float> Scantable::getWeather(int whichrow) const
2545{
2546 std::vector<float> out(5);
2547 //Float temperature, pressure, humidity, windspeed, windaz;
2548 weatherTable_.getEntry(out[0], out[1], out[2], out[3], out[4],
2549 mweatheridCol_(uInt(whichrow)));
2550
2551
2552 return out;
2553}
2554
2555bool Scantable::isAllChannelsFlagged(uInt whichrow)
2556{
2557 uInt rflag;
2558 flagrowCol_.get(whichrow, rflag);
2559 if (rflag > 0)
2560 return true;
2561 bool flag;
2562 Vector<uChar> flags;
2563 flagsCol_.get(whichrow, flags);
2564 flag = (flags[0]>0);
2565 for (uInt i = 1; i < flags.size(); ++i) {
2566 flag &= (flags[i]>0);
2567 }
2568 // return ((flag >> 7) == 1);
2569 return (flag > 0);
2570}
2571
2572std::size_t Scantable::nValidMask(const std::vector<bool>& mask)
2573{
2574 std::size_t nvalid=0;
2575 // the assertion lines had better be replaced with static_assert when c++11 is supported
2576 AlwaysAssert(static_cast<std::size_t>(true)==1, AipsError);
2577 AlwaysAssert(static_cast<std::size_t>(false)==0, AipsError);
2578 for (uInt i = 1; i < mask.size(); ++i) {
2579 nvalid += static_cast<std::size_t>(mask[i]);
2580 }
2581 return nvalid;
2582}
2583
2584
2585std::vector<std::string> Scantable::applyBaselineTable(const std::string& bltable, const bool returnfitresult, const std::string& outbltable, const bool outbltableexists, const bool overwrite)
2586{
2587 STBaselineTable btin = STBaselineTable(bltable);
2588
2589 Vector<Bool> applyCol = btin.getApply();
2590 int nRowBl = applyCol.size();
2591 if (nRowBl != nrow()) {
2592 throw(AipsError("Scantable and bltable have different number of rows."));
2593 }
2594
2595 std::vector<std::string> res;
2596 res.clear();
2597
2598 bool outBaselineTable = ((outbltable != "") && (!outbltableexists || overwrite));
2599 bool bltableidentical = (bltable == outbltable);
2600 STBaselineTable btout = STBaselineTable(*this);
2601 ROScalarColumn<Double> tcol = ROScalarColumn<Double>(table_, "TIME");
2602 Vector<Double> timeSecCol = tcol.getColumn();
2603
2604 for (int whichrow = 0; whichrow < nRowBl; ++whichrow) {
2605 if (applyCol[whichrow]) {
2606 std::vector<float> spec = getSpectrum(whichrow);
2607
2608 std::vector<bool> mask = btin.getMask(whichrow); //use mask_bltable only
2609
2610 STBaselineFunc::FuncName ftype = btin.getFunctionName(whichrow);
2611 std::vector<int> fpar = btin.getFuncParam(whichrow);
2612 std::vector<float> params;
2613 float rms;
2614 std::vector<float> resfit = doApplyBaselineTable(spec, mask, ftype, fpar, params, rms, whichrow);
2615 setSpectrum(resfit, whichrow);
2616
2617 if (returnfitresult) {
2618 res.push_back(packFittingResults(whichrow, params, rms));
2619 }
2620
2621 if (outBaselineTable) {
2622 if (outbltableexists) {
2623 if (overwrite) {
2624 if (bltableidentical) {
2625 btin.setresult(uInt(whichrow), Vector<Float>(params), Float(rms));
2626 } else {
2627 btout.setresult(uInt(whichrow), Vector<Float>(params), Float(rms));
2628 }
2629 }
2630 } else {
2631 btout.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
2632 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
2633 true, ftype, fpar, std::vector<float>(),
2634 getMaskListFromMask(mask), params, rms, spec.size(),
2635 3.0, 0, 0.0, 0, std::vector<int>());
2636 }
2637 }
2638 }
2639 }
2640
2641 if (outBaselineTable) {
2642 if (bltableidentical) {
2643 btin.save(outbltable);
2644 } else {
2645 btout.save(outbltable);
2646 }
2647 }
2648
2649 return res;
2650}
2651
2652std::vector<std::string> Scantable::subBaseline(const std::vector<std::string>& blInfoList, const bool returnfitresult, const std::string& outbltable, const bool outbltableexists, const bool overwrite)
2653{
2654 int nRowBl = blInfoList.size();
2655 int nRowSt = nrow();
2656
2657 std::vector<std::string> res;
2658 res.clear();
2659
2660 bool outBaselineTable = ((outbltable != "") && (!outbltableexists || overwrite));
2661 if ((outbltable != "") && outbltableexists && !overwrite) {
2662 throw(AipsError("Cannot overwrite bltable. Set overwrite=True."));
2663 }
2664
2665 STBaselineTable* btp = NULL;
2666 ROScalarColumn<Double> tcol = ROScalarColumn<Double>(table_, "TIME");
2667 Vector<Double> timeSecCol = tcol.getColumn();
2668
2669 if (outBaselineTable) {
2670 if (outbltableexists) {
2671 btp = new STBaselineTable((String)outbltable);
2672 } else {
2673 btp = new STBaselineTable(*this);
2674 // for (int i = 0; i < nRowSt; ++i) {
2675 // btp->appendbasedata(getScan(i), getCycle(i), getBeam(i), getIF(i), getPol(i),
2676 // 0, timeSecCol[i]);
2677 // btp->setApply(i, false);
2678 // }
2679 }
2680 int nrow = btp->nrow();
2681 for (int i = nrow; i < nRowSt; ++i) {
2682 btp->appendbasedata(getScan(i), getCycle(i), getBeam(i), getIF(i), getPol(i),
2683 0, timeSecCol[i]);
2684 btp->setApply(i, false);
2685 }
2686 }
2687
2688 for (int i = 0; i < nRowBl; ++i) {
2689 int irow;
2690 STBaselineFunc::FuncName ftype;
2691 std::vector<bool> mask;
2692 std::vector<int> fpar;
2693 float clipth;
2694 int clipn;
2695 bool uself;
2696 float lfth;
2697 std::vector<int> lfedge;
2698 int lfavg;
2699 parseBlInfo(blInfoList[i], irow, ftype, fpar, mask, clipth, clipn, uself, lfth, lfedge, lfavg);
2700
2701 if (irow < nRowSt) {
2702 std::vector<float> spec = getSpectrum(irow);
2703 std::vector<float> params;
2704 float rms;
2705 std::vector<bool> finalmask;
2706 Bool doApply = True;
2707
2708 if (!isAllChannelsFlagged(irow)) {
2709 std::vector<float> resfit = doSubtractBaseline(spec, mask, ftype, fpar, params, rms, finalmask, clipth, clipn, uself, irow, lfth, lfedge, lfavg);
2710 setSpectrum(resfit, irow);
2711 }
2712 else {
2713 doApply = False;
2714 }
2715
2716 if (returnfitresult) {
2717 res.push_back(packFittingResults(irow, params, rms));
2718 }
2719
2720 if (outBaselineTable) {
2721 Vector<Int> fparam(fpar.size());
2722 for (uInt j = 0; j < fparam.size(); ++j) {
2723 fparam[j] = (Int)fpar[j];
2724 }
2725
2726 btp->setdata(uInt(irow),
2727 uInt(getScan(irow)), uInt(getCycle(irow)),
2728 uInt(getBeam(irow)), uInt(getIF(irow)), uInt(getPol(irow)),
2729 uInt(0), timeSecCol[irow], doApply, ftype, fparam,
2730 Vector<Float>(), getMaskListFromMask(finalmask), Vector<Float>(params),
2731 Float(rms), uInt(spec.size()), Float(clipth), uInt(clipn),
2732 Float(0.0), uInt(0), Vector<uInt>());
2733 }
2734
2735 }
2736 }
2737
2738 if (outBaselineTable) {
2739 btp->save(outbltable);
2740 }
2741
2742 if (btp != NULL) {
2743 delete btp;
2744 }
2745
2746 return res;
2747}
2748
2749std::vector<float> Scantable::doApplyBaselineTable(std::vector<float>& spec,
2750 std::vector<bool>& mask,
2751 const STBaselineFunc::FuncName ftype,
2752 std::vector<int>& fpar,
2753 std::vector<float>& params,
2754 float&rms,
2755 int irow)
2756{
2757 std::vector<bool> finalmask;
2758 std::vector<int> lfedge;
2759 return doSubtractBaseline(spec, mask, ftype, fpar, params, rms, finalmask, 0.0, 0, false, irow, 0.0, lfedge, 0);
2760}
2761
2762std::vector<float> Scantable::doSubtractBaseline(std::vector<float>& spec,
2763 std::vector<bool>& mask,
2764 const STBaselineFunc::FuncName ftype,
2765 std::vector<int>& fpar,
2766 std::vector<float>& params,
2767 float&rms,
2768 std::vector<bool>& finalmask,
2769 float clipth,
2770 int clipn,
2771 bool uself,
2772 int irow,
2773 float lfth,
2774 std::vector<int>& lfedge,
2775 int lfavg)
2776{
2777 if (uself) {
2778 STLineFinder lineFinder = STLineFinder();
2779 initLineFinder(lfedge, lfth, lfavg, lineFinder);
2780 std::vector<int> currentEdge;
2781 mask = getCompositeChanMask(irow, mask, lfedge, currentEdge, lineFinder);
2782 } else {
2783 mask = getCompositeChanMask(irow, mask);
2784 }
2785
2786 std::vector<float> res;
2787 if (ftype == STBaselineFunc::Polynomial) {
2788 res = doPolynomialFitting(spec, mask, fpar[0], params, rms, finalmask, clipth, clipn);
2789 } else if (ftype == STBaselineFunc::Chebyshev) {
2790 res = doChebyshevFitting(spec, mask, fpar[0], params, rms, finalmask, clipth, clipn);
2791 } else if (ftype == STBaselineFunc::CSpline) {
2792 int nclip = 0;
2793 size_t numChan = spec.size();
2794 if (cubicSplineModelPool_.find(numChan) == cubicSplineModelPool_.end()) {
2795 cubicSplineModelPool_[numChan] = getPolynomialModel(3, numChan, &Scantable::getNormalPolynomial);
2796 }
2797 if (fpar.size() > 1) { // reading from baseline table in which pieceEdges are already calculated and stored.
2798 //res = doCubicSplineFitting(spec, mask, fpar, params, rms, finalmask, clipth, clipn);
2799 res = doCubicSplineLeastSquareFitting(spec, mask,
2800 cubicSplineModelPool_[numChan],
2801 fpar.size()-1, true, fpar, params,
2802 rms, finalmask, nclip, clipth,
2803 clipn);
2804 } else { // usual cspline fitting by giving nPiece only. fpar will be replaced with pieceEdges.
2805 //res = doCubicSplineFitting(spec, mask, fpar[0], fpar, params, rms, finalmask, clipth, clipn);
2806 res = doCubicSplineLeastSquareFitting(spec, mask,
2807 cubicSplineModelPool_[numChan],
2808 fpar[0], false, fpar, params,
2809 rms, finalmask, nclip, clipth,
2810 clipn);
2811 }
2812 } else if (ftype == STBaselineFunc::Sinusoid) {
2813 res = doSinusoidFitting(spec, mask, fpar, params, rms, finalmask, clipth, clipn);
2814 }
2815
2816 return res;
2817}
2818
2819std::string Scantable::packFittingResults(const int irow, const std::vector<float>& params, const float rms)
2820{
2821 // returned value: "irow:params[0],params[1],..,params[n-1]:rms"
2822 ostringstream os;
2823 os << irow << ':';
2824 for (uInt i = 0; i < params.size(); ++i) {
2825 if (i > 0) {
2826 os << ',';
2827 }
2828 os << params[i];
2829 }
2830 os << ':' << rms;
2831
2832 return os.str();
2833}
2834
2835void Scantable::parseBlInfo(const std::string& blInfo, int& irow, STBaselineFunc::FuncName& ftype, std::vector<int>& fpar, std::vector<bool>& mask, float& thresClip, int& nIterClip, bool& useLineFinder, float& thresLF, std::vector<int>& edgeLF, int& avgLF)
2836{
2837 // The baseline info to be parsed must be column-delimited string like
2838 // "0:chebyshev:5:3,5,169,174,485,487" where the elements are
2839 // row number, funcType, funcOrder, maskList, clipThreshold, clipNIter,
2840 // useLineFinder, lfThreshold, lfEdge and lfChanAvgLimit.
2841
2842 std::vector<string> res = splitToStringList(blInfo, ':');
2843 if (res.size() < 4) {
2844 throw(AipsError("baseline info has bad format")) ;
2845 }
2846
2847 string ftype0, fpar0, masklist0, uself0, edge0;
2848 std::vector<int> masklist;
2849
2850 stringstream ss;
2851 ss << res[0];
2852 ss >> irow;
2853 ss.clear(); ss.str("");
2854
2855 ss << res[1];
2856 ss >> ftype0;
2857 if (ftype0 == "poly") {
2858 ftype = STBaselineFunc::Polynomial;
2859 } else if (ftype0 == "cspline") {
2860 ftype = STBaselineFunc::CSpline;
2861 } else if (ftype0 == "sinusoid") {
2862 ftype = STBaselineFunc::Sinusoid;
2863 } else if (ftype0 == "chebyshev") {
2864 ftype = STBaselineFunc::Chebyshev;
2865 } else {
2866 throw(AipsError("invalid function type."));
2867 }
2868 ss.clear(); ss.str("");
2869
2870 ss << res[2];
2871 ss >> fpar0;
2872 fpar = splitToIntList(fpar0, ',');
2873 ss.clear(); ss.str("");
2874
2875 ss << res[3];
2876 ss >> masklist0;
2877 mask = getMaskFromMaskList(nchan(getIF(irow)), splitToIntList(masklist0, ','));
2878 ss.clear(); ss.str("");
2879
2880 ss << res[4];
2881 ss >> thresClip;
2882 ss.clear(); ss.str("");
2883
2884 ss << res[5];
2885 ss >> nIterClip;
2886 ss.clear(); ss.str("");
2887
2888 ss << res[6];
2889 ss >> uself0;
2890 if (uself0 == "true") {
2891 useLineFinder = true;
2892 } else {
2893 useLineFinder = false;
2894 }
2895 ss.clear(); ss.str("");
2896
2897 if (useLineFinder) {
2898 ss << res[7];
2899 ss >> thresLF;
2900 ss.clear(); ss.str("");
2901
2902 ss << res[8];
2903 ss >> edge0;
2904 edgeLF = splitToIntList(edge0, ',');
2905 ss.clear(); ss.str("");
2906
2907 ss << res[9];
2908 ss >> avgLF;
2909 ss.clear(); ss.str("");
2910 }
2911
2912}
2913
2914std::vector<int> Scantable::splitToIntList(const std::string& s, const char delim)
2915{
2916 istringstream iss(s);
2917 string tmp;
2918 int tmpi;
2919 std::vector<int> res;
2920 stringstream ss;
2921 while (getline(iss, tmp, delim)) {
2922 ss << tmp;
2923 ss >> tmpi;
2924 res.push_back(tmpi);
2925 ss.clear(); ss.str("");
2926 }
2927
2928 return res;
2929}
2930
2931std::vector<string> Scantable::splitToStringList(const std::string& s, const char delim)
2932{
2933 istringstream iss(s);
2934 std::string tmp;
2935 std::vector<string> res;
2936 while (getline(iss, tmp, delim)) {
2937 res.push_back(tmp);
2938 }
2939
2940 return res;
2941}
2942
2943std::vector<bool> Scantable::getMaskFromMaskList(const int nchan, const std::vector<int>& masklist)
2944{
2945 if (masklist.size() % 2 != 0) {
2946 throw(AipsError("masklist must have even number of elements."));
2947 }
2948
2949 std::vector<bool> res(nchan);
2950
2951 for (int i = 0; i < nchan; ++i) {
2952 res[i] = false;
2953 }
2954 for (uInt j = 0; j < masklist.size(); j += 2) {
2955 for (int i = masklist[j]; i <= min(nchan-1, masklist[j+1]); ++i) {
2956 res[i] = true;
2957 }
2958 }
2959
2960 return res;
2961}
2962
2963Vector<uInt> Scantable::getMaskListFromMask(const std::vector<bool>& mask)
2964{
2965 std::vector<int> masklist;
2966 masklist.clear();
2967
2968 for (uInt i = 0; i < mask.size(); ++i) {
2969 if (mask[i]) {
2970 if ((i == 0)||(i == mask.size()-1)) {
2971 masklist.push_back(i);
2972 } else {
2973 if ((mask[i])&&(!mask[i-1])) {
2974 masklist.push_back(i);
2975 }
2976 if ((mask[i])&&(!mask[i+1])) {
2977 masklist.push_back(i);
2978 }
2979 }
2980 }
2981 }
2982
2983 Vector<uInt> res(masklist.size());
2984 for (uInt i = 0; i < masklist.size(); ++i) {
2985 res[i] = (uInt)masklist[i];
2986 }
2987
2988 return res;
2989}
2990
2991void Scantable::initialiseBaselining(const std::string& blfile,
2992 ofstream& ofs,
2993 const bool outLogger,
2994 bool& outTextFile,
2995 bool& csvFormat,
2996 String& coordInfo,
2997 bool& hasSameNchan,
2998 const std::string& progressInfo,
2999 bool& showProgress,
3000 int& minNRow,
3001 Vector<Double>& timeSecCol)
3002{
3003 csvFormat = false;
3004 outTextFile = false;
3005
3006 if (blfile != "") {
3007 csvFormat = (blfile.substr(0, 1) == "T");
3008 ofs.open(blfile.substr(1).c_str(), ios::out | ios::app);
3009 if (ofs) outTextFile = true;
3010 }
3011
3012 coordInfo = "";
3013 hasSameNchan = true;
3014
3015 if (outLogger || outTextFile) {
3016 coordInfo = getCoordInfo()[0];
3017 if (coordInfo == "") coordInfo = "channel";
3018 hasSameNchan = hasSameNchanOverIFs();
3019 }
3020
3021 parseProgressInfo(progressInfo, showProgress, minNRow);
3022
3023 ROScalarColumn<Double> tcol = ROScalarColumn<Double>(table_, "TIME");
3024 timeSecCol = tcol.getColumn();
3025}
3026
3027void Scantable::finaliseBaselining(const bool outBaselineTable,
3028 STBaselineTable* pbt,
3029 const string& bltable,
3030 const bool outTextFile,
3031 ofstream& ofs)
3032{
3033 if (outBaselineTable) {
3034 pbt->save(bltable);
3035 }
3036
3037 if (outTextFile) ofs.close();
3038}
3039
3040void Scantable::initLineFinder(const std::vector<int>& edge,
3041 const float threshold,
3042 const int chanAvgLimit,
3043 STLineFinder& lineFinder)
3044{
3045 if ((edge.size() > 2) && (edge.size() < getIFNos().size()*2)) {
3046 throw(AipsError("Length of edge element info is less than that of IFs"));
3047 }
3048
3049 lineFinder.setOptions(threshold, 3, chanAvgLimit);
3050}
3051
3052void Scantable::polyBaseline(const std::vector<bool>& mask, int order,
3053 float thresClip, int nIterClip,
3054 bool getResidual,
3055 const std::string& progressInfo,
3056 const bool outLogger, const std::string& blfile,
3057 const std::string& bltable)
3058{
3059 /****
3060 double TimeStart = mathutil::gettimeofday_sec();
3061 ****/
3062
3063 try {
3064 ofstream ofs;
3065 String coordInfo;
3066 bool hasSameNchan, outTextFile, csvFormat, showProgress;
3067 int minNRow;
3068 int nRow = nrow();
3069 std::vector<bool> chanMask, finalChanMask;
3070 float rms;
3071 bool outBaselineTable = (bltable != "");
3072 STBaselineTable bt = STBaselineTable(*this);
3073 Vector<Double> timeSecCol;
3074 size_t flagged=0;
3075
3076 initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
3077 coordInfo, hasSameNchan,
3078 progressInfo, showProgress, minNRow,
3079 timeSecCol);
3080
3081 std::vector<int> nChanNos;
3082 std::vector<std::vector<std::vector<double> > > modelReservoir;
3083 modelReservoir = getPolynomialModelReservoir(order,
3084 &Scantable::getNormalPolynomial,
3085 nChanNos);
3086 int nModel = modelReservoir.size();
3087
3088 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
3089 std::vector<float> sp = getSpectrum(whichrow);
3090 chanMask = getCompositeChanMask(whichrow, mask);
3091 std::vector<float> params;
3092
3093 //if (flagrowCol_(whichrow) == 0) {
3094 if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
3095 int nClipped = 0;
3096 std::vector<float> res;
3097 res = doLeastSquareFitting(sp, chanMask,
3098 modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
3099 params, rms, finalChanMask,
3100 nClipped, thresClip, nIterClip, getResidual);
3101
3102 if (outBaselineTable) {
3103 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
3104 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
3105 true, STBaselineFunc::Polynomial, order, std::vector<float>(),
3106 getMaskListFromMask(finalChanMask), params, rms, sp.size(),
3107 thresClip, nIterClip, 0.0, 0, std::vector<int>());
3108 } else {
3109 setSpectrum(res, whichrow);
3110 }
3111
3112 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
3113 coordInfo, hasSameNchan, ofs, "polyBaseline()",
3114 params, nClipped);
3115 } else {
3116 // no valid channels to fit (flag the row)
3117 flagrowCol_.put(whichrow, 1);
3118 ++flagged;
3119 if (outBaselineTable) {
3120 params.resize(nModel);
3121 for (uInt i = 0; i < params.size(); ++i) {
3122 params[i] = 0.0;
3123 }
3124 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
3125 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
3126 true, STBaselineFunc::Polynomial, order, std::vector<float>(),
3127 getMaskListFromMask(chanMask), params, 0.0, sp.size(),
3128 thresClip, nIterClip, 0.0, 0, std::vector<int>());
3129 }
3130 }
3131
3132 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
3133 }
3134
3135 finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
3136 if (flagged > 0) {
3137 LogIO os( LogOrigin( "Scantable", "polyBaseline()") ) ;
3138 os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
3139 }
3140 } catch (...) {
3141 throw;
3142 }
3143
3144 /****
3145 double TimeEnd = mathutil::gettimeofday_sec();
3146 double elapse1 = TimeEnd - TimeStart;
3147 std::cout << "poly-new : " << elapse1 << " (sec.)" << endl;
3148 ****/
3149}
3150
3151void Scantable::autoPolyBaseline(const std::vector<bool>& mask, int order,
3152 float thresClip, int nIterClip,
3153 const std::vector<int>& edge,
3154 float threshold, int chanAvgLimit,
3155 bool getResidual,
3156 const std::string& progressInfo,
3157 const bool outLogger, const std::string& blfile,
3158 const std::string& bltable)
3159{
3160 try {
3161 ofstream ofs;
3162 String coordInfo;
3163 bool hasSameNchan, outTextFile, csvFormat, showProgress;
3164 int minNRow;
3165 int nRow = nrow();
3166 std::vector<bool> chanMask, finalChanMask;
3167 float rms;
3168 bool outBaselineTable = (bltable != "");
3169 STBaselineTable bt = STBaselineTable(*this);
3170 Vector<Double> timeSecCol;
3171 STLineFinder lineFinder = STLineFinder();
3172 size_t flagged=0;
3173
3174 initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
3175 coordInfo, hasSameNchan,
3176 progressInfo, showProgress, minNRow,
3177 timeSecCol);
3178
3179 initLineFinder(edge, threshold, chanAvgLimit, lineFinder);
3180
3181 std::vector<int> nChanNos;
3182 std::vector<std::vector<std::vector<double> > > modelReservoir;
3183 modelReservoir = getPolynomialModelReservoir(order,
3184 &Scantable::getNormalPolynomial,
3185 nChanNos);
3186 int nModel = modelReservoir.size();
3187
3188 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
3189 std::vector<float> sp = getSpectrum(whichrow);
3190 std::vector<int> currentEdge;
3191 chanMask = getCompositeChanMask(whichrow, mask, edge, currentEdge, lineFinder);
3192 std::vector<float> params;
3193
3194 //if (flagrowCol_(whichrow) == 0) {
3195 if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
3196 int nClipped = 0;
3197 std::vector<float> res;
3198 res = doLeastSquareFitting(sp, chanMask,
3199 modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
3200 params, rms, finalChanMask,
3201 nClipped, thresClip, nIterClip, getResidual);
3202
3203 if (outBaselineTable) {
3204 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
3205 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
3206 true, STBaselineFunc::Polynomial, order, std::vector<float>(),
3207 getMaskListFromMask(finalChanMask), params, rms, sp.size(),
3208 thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
3209 } else {
3210 setSpectrum(res, whichrow);
3211 }
3212
3213 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
3214 coordInfo, hasSameNchan, ofs, "autoPolyBaseline()",
3215 params, nClipped);
3216 } else {
3217 // no valid channels to fit (flag the row)
3218 flagrowCol_.put(whichrow, 1);
3219 ++flagged;
3220 if (outBaselineTable) {
3221 params.resize(nModel);
3222 for (uInt i = 0; i < params.size(); ++i) {
3223 params[i] = 0.0;
3224 }
3225 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
3226 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
3227 true, STBaselineFunc::Polynomial, order, std::vector<float>(),
3228 getMaskListFromMask(chanMask), params, 0.0, sp.size(),
3229 thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
3230 }
3231 }
3232
3233 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
3234 }
3235
3236 finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
3237 if (flagged > 0) {
3238 LogIO os( LogOrigin( "Scantable", "autoPolyBaseline()") ) ;
3239 os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
3240 }
3241 } catch (...) {
3242 throw;
3243 }
3244}
3245
3246void Scantable::chebyshevBaseline(const std::vector<bool>& mask, int order,
3247 float thresClip, int nIterClip,
3248 bool getResidual,
3249 const std::string& progressInfo,
3250 const bool outLogger, const std::string& blfile,
3251 const std::string& bltable)
3252{
3253 /*
3254 double TimeStart = mathutil::gettimeofday_sec();
3255 */
3256
3257 try {
3258 ofstream ofs;
3259 String coordInfo;
3260 bool hasSameNchan, outTextFile, csvFormat, showProgress;
3261 int minNRow;
3262 int nRow = nrow();
3263 std::vector<bool> chanMask, finalChanMask;
3264 float rms;
3265 bool outBaselineTable = (bltable != "");
3266 STBaselineTable bt = STBaselineTable(*this);
3267 Vector<Double> timeSecCol;
3268 size_t flagged=0;
3269
3270 initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
3271 coordInfo, hasSameNchan,
3272 progressInfo, showProgress, minNRow,
3273 timeSecCol);
3274
3275 std::vector<int> nChanNos;
3276 std::vector<std::vector<std::vector<double> > > modelReservoir;
3277 modelReservoir = getPolynomialModelReservoir(order,
3278 &Scantable::getChebyshevPolynomial,
3279 nChanNos);
3280 int nModel = modelReservoir.size();
3281
3282 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
3283 std::vector<float> sp = getSpectrum(whichrow);
3284 chanMask = getCompositeChanMask(whichrow, mask);
3285 std::vector<float> params;
3286
3287 // if (flagrowCol_(whichrow) == 0) {
3288 if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
3289 int nClipped = 0;
3290 std::vector<float> res;
3291 res = doLeastSquareFitting(sp, chanMask,
3292 modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
3293 params, rms, finalChanMask,
3294 nClipped, thresClip, nIterClip, getResidual);
3295
3296 if (outBaselineTable) {
3297 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
3298 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
3299 true, STBaselineFunc::Chebyshev, order, std::vector<float>(),
3300 getMaskListFromMask(finalChanMask), params, rms, sp.size(),
3301 thresClip, nIterClip, 0.0, 0, std::vector<int>());
3302 } else {
3303 setSpectrum(res, whichrow);
3304 }
3305
3306 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
3307 coordInfo, hasSameNchan, ofs, "chebyshevBaseline()",
3308 params, nClipped);
3309 } else {
3310 // no valid channels to fit (flag the row)
3311 flagrowCol_.put(whichrow, 1);
3312 ++flagged;
3313 if (outBaselineTable) {
3314 params.resize(nModel);
3315 for (uInt i = 0; i < params.size(); ++i) {
3316 params[i] = 0.0;
3317 }
3318 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
3319 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
3320 true, STBaselineFunc::Chebyshev, order, std::vector<float>(),
3321 getMaskListFromMask(chanMask), params, 0.0, sp.size(),
3322 thresClip, nIterClip, 0.0, 0, std::vector<int>());
3323 }
3324 }
3325
3326 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
3327 }
3328
3329 finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
3330
3331 if (flagged > 0) {
3332 LogIO os( LogOrigin( "Scantable", "chebyshevBaseline()") ) ;
3333 os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
3334 }
3335 } catch (...) {
3336 throw;
3337 }
3338
3339 /*
3340 double TimeEnd = mathutil::gettimeofday_sec();
3341 double elapse1 = TimeEnd - TimeStart;
3342 std::cout << "cheby : " << elapse1 << " (sec.)" << endl;
3343 */
3344}
3345
3346void Scantable::autoChebyshevBaseline(const std::vector<bool>& mask, int order,
3347 float thresClip, int nIterClip,
3348 const std::vector<int>& edge,
3349 float threshold, int chanAvgLimit,
3350 bool getResidual,
3351 const std::string& progressInfo,
3352 const bool outLogger, const std::string& blfile,
3353 const std::string& bltable)
3354{
3355 try {
3356 ofstream ofs;
3357 String coordInfo;
3358 bool hasSameNchan, outTextFile, csvFormat, showProgress;
3359 int minNRow;
3360 int nRow = nrow();
3361 std::vector<bool> chanMask, finalChanMask;
3362 float rms;
3363 bool outBaselineTable = (bltable != "");
3364 STBaselineTable bt = STBaselineTable(*this);
3365 Vector<Double> timeSecCol;
3366 STLineFinder lineFinder = STLineFinder();
3367 size_t flagged=0;
3368
3369 initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
3370 coordInfo, hasSameNchan,
3371 progressInfo, showProgress, minNRow,
3372 timeSecCol);
3373
3374 initLineFinder(edge, threshold, chanAvgLimit, lineFinder);
3375
3376 std::vector<int> nChanNos;
3377 std::vector<std::vector<std::vector<double> > > modelReservoir;
3378 modelReservoir = getPolynomialModelReservoir(order,
3379 &Scantable::getChebyshevPolynomial,
3380 nChanNos);
3381 int nModel = modelReservoir.size();
3382
3383 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
3384 std::vector<float> sp = getSpectrum(whichrow);
3385 std::vector<int> currentEdge;
3386 chanMask = getCompositeChanMask(whichrow, mask, edge, currentEdge, lineFinder);
3387 std::vector<float> params;
3388
3389 // if (flagrowCol_(whichrow) == 0) {
3390 if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
3391 int nClipped = 0;
3392 std::vector<float> res;
3393 res = doLeastSquareFitting(sp, chanMask,
3394 modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
3395 params, rms, finalChanMask,
3396 nClipped, thresClip, nIterClip, getResidual);
3397
3398 if (outBaselineTable) {
3399 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
3400 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
3401 true, STBaselineFunc::Chebyshev, order, std::vector<float>(),
3402 getMaskListFromMask(finalChanMask), params, rms, sp.size(),
3403 thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
3404 } else {
3405 setSpectrum(res, whichrow);
3406 }
3407
3408 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
3409 coordInfo, hasSameNchan, ofs, "autoChebyshevBaseline()",
3410 params, nClipped);
3411 } else {
3412 // no valid channels to fit (flag the row)
3413 flagrowCol_.put(whichrow, 1);
3414 ++flagged;
3415 if (outBaselineTable) {
3416 params.resize(nModel);
3417 for (uInt i = 0; i < params.size(); ++i) {
3418 params[i] = 0.0;
3419 }
3420 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
3421 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
3422 true, STBaselineFunc::Chebyshev, order, std::vector<float>(),
3423 getMaskListFromMask(chanMask), params, 0.0, sp.size(),
3424 thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
3425 }
3426 }
3427
3428 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
3429 }
3430
3431 finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
3432
3433 if (flagged > 0) {
3434 LogIO os( LogOrigin( "Scantable", "autoChebyshevBaseline()") ) ;
3435 os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
3436 }
3437 } catch (...) {
3438 throw;
3439 }
3440}
3441
3442double Scantable::calculateModelSelectionCriteria(const std::string& valname,
3443 const std::string& blfunc,
3444 int order,
3445 const std::vector<bool>& inMask,
3446 int whichrow,
3447 bool useLineFinder,
3448 const std::vector<int>& edge,
3449 float threshold,
3450 int chanAvgLimit)
3451{
3452 std::vector<float> sp = getSpectrum(whichrow);
3453 std::vector<bool> chanMask;
3454 chanMask.clear();
3455
3456 if (useLineFinder) {
3457 STLineFinder lineFinder = STLineFinder();
3458 initLineFinder(edge, threshold, chanAvgLimit, lineFinder);
3459 std::vector<int> currentEdge;
3460 chanMask = getCompositeChanMask(whichrow, inMask, edge, currentEdge, lineFinder);
3461 } else {
3462 chanMask = getCompositeChanMask(whichrow, inMask);
3463 }
3464
3465 return doCalculateModelSelectionCriteria(valname, sp, chanMask, blfunc, order);
3466}
3467
3468double Scantable::doCalculateModelSelectionCriteria(const std::string& valname, const std::vector<float>& spec, const std::vector<bool>& mask, const std::string& blfunc, int order)
3469{
3470 int nparam;
3471 std::vector<float> params;
3472 std::vector<bool> finalChanMask;
3473 float rms;
3474 int nClipped = 0;
3475 std::vector<float> res;
3476 if (blfunc == "poly") {
3477 nparam = order + 1;
3478 res = doPolynomialFitting(spec, mask, order, params, rms, finalChanMask, nClipped);
3479 } else if (blfunc == "chebyshev") {
3480 nparam = order + 1;
3481 res = doChebyshevFitting(spec, mask, order, params, rms, finalChanMask, nClipped);
3482 } else if (blfunc == "cspline") {
3483 std::vector<int> pieceEdges;//(order+1); //order = npiece
3484 nparam = order + 3;
3485 res = doCubicSplineFitting(spec, mask, order, false, pieceEdges, params, rms, finalChanMask, nClipped);
3486 } else if (blfunc == "sinusoid") {
3487 std::vector<int> nWaves;
3488 nWaves.clear();
3489 for (int i = 0; i <= order; ++i) {
3490 nWaves.push_back(i);
3491 }
3492 nparam = 2*order + 1; // order = nwave
3493 res = doSinusoidFitting(spec, mask, nWaves, params, rms, finalChanMask, nClipped);
3494 } else {
3495 throw(AipsError("blfunc must be poly, chebyshev, cspline or sinusoid."));
3496 }
3497
3498 double msq = 0.0;
3499 int nusedchan = 0;
3500 int nChan = res.size();
3501 for (int i = 0; i < nChan; ++i) {
3502 if (mask[i]) {
3503 msq += (double)res[i]*(double)res[i];
3504 nusedchan++;
3505 }
3506 }
3507 if (nusedchan == 0) {
3508 throw(AipsError("all channels masked."));
3509 }
3510 msq /= (double)nusedchan;
3511
3512 nparam++; //add 1 for sigma of Gaussian distribution
3513 const double PI = 6.0 * asin(0.5); // PI (= 3.141592653...)
3514
3515 if (valname.find("aic") == 0) {
3516 // Original Akaike Information Criterion (AIC)
3517 double aic = nusedchan * (log(2.0 * PI * msq) + 1.0) + 2.0 * nparam;
3518
3519 // Corrected AIC by Sugiura(1978) (AICc)
3520 if (valname == "aicc") {
3521 if (nusedchan - nparam - 1 <= 0) {
3522 throw(AipsError("channel size is too small to calculate AICc."));
3523 }
3524 aic += 2.0*nparam*(nparam + 1)/(double)(nusedchan - nparam - 1);
3525 }
3526
3527 return aic;
3528
3529 } else if (valname == "bic") {
3530 // Bayesian Information Criterion (BIC)
3531 double bic = nusedchan * log(msq) + nparam * log((double)nusedchan);
3532 return bic;
3533
3534 } else if (valname == "gcv") {
3535 // Generalised Cross Validation
3536 double x = 1.0 - (double)nparam / (double)nusedchan;
3537 double gcv = msq / (x * x);
3538 return gcv;
3539
3540 } else {
3541 throw(AipsError("valname must be aic, aicc, bic or gcv."));
3542 }
3543}
3544
3545double Scantable::getNormalPolynomial(int n, double x) {
3546 if (n == 0) {
3547 return 1.0;
3548 } else if (n > 0) {
3549 double res = 1.0;
3550 for (int i = 0; i < n; ++i) {
3551 res *= x;
3552 }
3553 return res;
3554 } else {
3555 if (x == 0.0) {
3556 throw(AipsError("infinity result: x=0 given for negative power."));
3557 } else {
3558 return pow(x, (double)n);
3559 }
3560 }
3561}
3562
3563double Scantable::getChebyshevPolynomial(int n, double x) {
3564 if ((x < -1.0)||(x > 1.0)) {
3565 throw(AipsError("out of definition range (-1 <= x <= 1)."));
3566 } else if (x == 1.0) {
3567 return 1.0;
3568 } else if (x == 0.0) {
3569 double res;
3570 if (n%2 == 0) {
3571 if (n%4 == 0) {
3572 res = 1.0;
3573 } else {
3574 res = -1.0;
3575 }
3576 } else {
3577 res = 0.0;
3578 }
3579 return res;
3580 } else if (x == -1.0) {
3581 double res = (n%2 == 0 ? 1.0 : -1.0);
3582 return res;
3583 } else if (n < 0) {
3584 throw(AipsError("the order must be zero or positive."));
3585 } else if (n == 0) {
3586 return 1.0;
3587 } else if (n == 1) {
3588 return x;
3589 } else {
3590 double res[n+1];
3591 for (int i = 0; i < n+1; ++i) {
3592 double res0 = 0.0;
3593 if (i == 0) {
3594 res0 = 1.0;
3595 } else if (i == 1) {
3596 res0 = x;
3597 } else {
3598 res0 = 2.0 * x * res[i-1] - res[i-2];
3599 }
3600 res[i] = res0;
3601 }
3602 return res[n];
3603 }
3604}
3605
3606std::vector<float> Scantable::doPolynomialFitting(const std::vector<float>& data,
3607 const std::vector<bool>& mask,
3608 int order,
3609 std::vector<float>& params,
3610 float& rms,
3611 std::vector<bool>& finalmask,
3612 float clipth,
3613 int clipn)
3614{
3615 int nClipped = 0;
3616 return doPolynomialFitting(data, mask, order, params, rms, finalmask, nClipped, clipth, clipn);
3617}
3618
3619std::vector<float> Scantable::doPolynomialFitting(const std::vector<float>& data,
3620 const std::vector<bool>& mask,
3621 int order,
3622 std::vector<float>& params,
3623 float& rms,
3624 std::vector<bool>& finalMask,
3625 int& nClipped,
3626 float thresClip,
3627 int nIterClip,
3628 bool getResidual)
3629{
3630 return doLeastSquareFitting(data, mask,
3631 getPolynomialModel(order, data.size(), &Scantable::getNormalPolynomial),
3632 params, rms, finalMask,
3633 nClipped, thresClip, nIterClip,
3634 getResidual);
3635}
3636
3637std::vector<float> Scantable::doChebyshevFitting(const std::vector<float>& data,
3638 const std::vector<bool>& mask,
3639 int order,
3640 std::vector<float>& params,
3641 float& rms,
3642 std::vector<bool>& finalmask,
3643 float clipth,
3644 int clipn)
3645{
3646 int nClipped = 0;
3647 return doChebyshevFitting(data, mask, order, params, rms, finalmask, nClipped, clipth, clipn);
3648}
3649
3650std::vector<float> Scantable::doChebyshevFitting(const std::vector<float>& data,
3651 const std::vector<bool>& mask,
3652 int order,
3653 std::vector<float>& params,
3654 float& rms,
3655 std::vector<bool>& finalMask,
3656 int& nClipped,
3657 float thresClip,
3658 int nIterClip,
3659 bool getResidual)
3660{
3661 return doLeastSquareFitting(data, mask,
3662 getPolynomialModel(order, data.size(), &Scantable::getChebyshevPolynomial),
3663 params, rms, finalMask,
3664 nClipped, thresClip, nIterClip,
3665 getResidual);
3666}
3667
3668std::vector<std::vector<double> > Scantable::getPolynomialModel(int order, int nchan, double (Scantable::*pfunc)(int, double))
3669{
3670 // model : contains model values for computing the least-square matrix.
3671 // model.size() is nmodel and model[*].size() is nchan.
3672 // Each model element are as follows:
3673 //
3674 // (for normal polynomials)
3675 // model[0] = {1.0, 1.0, 1.0, ..., 1.0},
3676 // model[1] = {0.0, 1.0, 2.0, ..., (nchan-1)}
3677 // model[n-1] = ...,
3678 // model[n] = {0.0^n, 1.0^n, 2.0^n, ..., (nchan-1)^n}
3679 // where (0 <= n <= order)
3680 //
3681 // (for Chebyshev polynomials)
3682 // model[0] = {T0(-1), T0(2/(nchan-1)-1), T0(4/(nchan-1)-1), ..., T0(1)},
3683 // model[n-1] = ...,
3684 // model[n] = {Tn(-1), Tn(2/(nchan-1)-1), Tn(4/(nchan-1)-1), ..., Tn(1)}
3685 // where (0 <= n <= order),
3686
3687 int nmodel = order + 1;
3688 std::vector<std::vector<double> > model(nmodel, std::vector<double>(nchan));
3689
3690 double stretch, shift;
3691 if (pfunc == &Scantable::getChebyshevPolynomial) {
3692 stretch = 2.0/(double)(nchan - 1);
3693 shift = -1.0;
3694 } else {
3695 stretch = 1.0;
3696 shift = 0.0;
3697 }
3698
3699 for (int i = 0; i < nmodel; ++i) {
3700 for (int j = 0; j < nchan; ++j) {
3701 model[i][j] = (this->*pfunc)(i, stretch*(double)j + shift);
3702 }
3703 }
3704
3705 return model;
3706}
3707
3708std::vector<std::vector<std::vector<double> > > Scantable::getPolynomialModelReservoir(int order,
3709 double (Scantable::*pfunc)(int, double),
3710 std::vector<int>& nChanNos)
3711{
3712 std::vector<std::vector<std::vector<double> > > res;
3713 res.clear();
3714 nChanNos.clear();
3715
3716 std::vector<uint> ifNos = getIFNos();
3717 for (uint i = 0; i < ifNos.size(); ++i) {
3718 int currNchan = nchan(ifNos[i]);
3719 bool hasDifferentNchan = (i == 0);
3720 for (uint j = 0; j < i; ++j) {
3721 if (currNchan != nchan(ifNos[j])) {
3722 hasDifferentNchan = true;
3723 break;
3724 }
3725 }
3726 if (hasDifferentNchan) {
3727 res.push_back(getPolynomialModel(order, currNchan, pfunc));
3728 nChanNos.push_back(currNchan);
3729 }
3730 }
3731
3732 return res;
3733}
3734
3735std::vector<float> Scantable::doLeastSquareFitting(const std::vector<float>& data,
3736 const std::vector<bool>& mask,
3737 const std::vector<std::vector<double> >& model,
3738 std::vector<float>& params,
3739 float& rms,
3740 std::vector<bool>& finalMask,
3741 int& nClipped,
3742 float thresClip,
3743 int nIterClip,
3744 bool getResidual)
3745{
3746 int nDOF = model.size();
3747 int nChan = data.size();
3748
3749 if (nDOF == 0) {
3750 throw(AipsError("no model data given"));
3751 }
3752 if (nChan < 2) {
3753 throw(AipsError("data size is too few"));
3754 }
3755 if (nChan != (int)mask.size()) {
3756 throw(AipsError("data and mask sizes are not identical"));
3757 }
3758 for (int i = 0; i < nDOF; ++i) {
3759 if (nChan != (int)model[i].size()) {
3760 throw(AipsError("data and model sizes are not identical"));
3761 }
3762 }
3763
3764 params.clear();
3765 params.resize(nDOF);
3766
3767 finalMask.clear();
3768 finalMask.resize(nChan);
3769
3770 std::vector<int> maskArray(nChan);
3771 int j = 0;
3772 for (int i = 0; i < nChan; ++i) {
3773 maskArray[i] = mask[i] ? 1 : 0;
3774 if (isnan(data[i])) maskArray[i] = 0;
3775 if (isinf(data[i])) maskArray[i] = 0;
3776
3777 finalMask[i] = (maskArray[i] == 1);
3778 if (finalMask[i]) {
3779 j++;
3780 }
3781
3782 /*
3783 maskArray[i] = mask[i] ? 1 : 0;
3784 if (mask[i]) {
3785 j++;
3786 }
3787 finalMask[i] = mask[i];
3788 */
3789 }
3790
3791 int initNData = j;
3792 int nData = initNData;
3793
3794 std::vector<double> z1(nChan), r1(nChan), residual(nChan);
3795 for (int i = 0; i < nChan; ++i) {
3796 z1[i] = (double)data[i];
3797 r1[i] = 0.0;
3798 residual[i] = 0.0;
3799 }
3800
3801 for (int nClip = 0; nClip < nIterClip+1; ++nClip) {
3802 // xMatrix : horizontal concatenation of
3803 // the least-sq. matrix (left) and an
3804 // identity matrix (right).
3805 // the right part is used to calculate the inverse matrix of the left part.
3806 double xMatrix[nDOF][2*nDOF];
3807 double zMatrix[nDOF];
3808 for (int i = 0; i < nDOF; ++i) {
3809 for (int j = 0; j < 2*nDOF; ++j) {
3810 xMatrix[i][j] = 0.0;
3811 }
3812 xMatrix[i][nDOF+i] = 1.0;
3813 zMatrix[i] = 0.0;
3814 }
3815
3816 int nUseData = 0;
3817 for (int k = 0; k < nChan; ++k) {
3818 if (maskArray[k] == 0) continue;
3819
3820 for (int i = 0; i < nDOF; ++i) {
3821 for (int j = i; j < nDOF; ++j) {
3822 xMatrix[i][j] += model[i][k] * model[j][k];
3823 }
3824 zMatrix[i] += z1[k] * model[i][k];
3825 }
3826
3827 nUseData++;
3828 }
3829
3830 if (nUseData < 1) {
3831 throw(AipsError("all channels clipped or masked. can't execute fitting anymore."));
3832 }
3833
3834 for (int i = 0; i < nDOF; ++i) {
3835 for (int j = 0; j < i; ++j) {
3836 xMatrix[i][j] = xMatrix[j][i];
3837 }
3838 }
3839
3840 //compute inverse matrix of the left half of xMatrix
3841 std::vector<double> invDiag(nDOF);
3842 for (int i = 0; i < nDOF; ++i) {
3843 invDiag[i] = 1.0 / xMatrix[i][i];
3844 for (int j = 0; j < nDOF; ++j) {
3845 xMatrix[i][j] *= invDiag[i];
3846 }
3847 }
3848
3849 for (int k = 0; k < nDOF; ++k) {
3850 for (int i = 0; i < nDOF; ++i) {
3851 if (i != k) {
3852 double factor1 = xMatrix[k][k];
3853 double invfactor1 = 1.0 / factor1;
3854 double factor2 = xMatrix[i][k];
3855 for (int j = k; j < 2*nDOF; ++j) {
3856 xMatrix[i][j] *= factor1;
3857 xMatrix[i][j] -= xMatrix[k][j]*factor2;
3858 xMatrix[i][j] *= invfactor1;
3859 }
3860 }
3861 }
3862 double invXDiag = 1.0 / xMatrix[k][k];
3863 for (int j = k; j < 2*nDOF; ++j) {
3864 xMatrix[k][j] *= invXDiag;
3865 }
3866 }
3867
3868 for (int i = 0; i < nDOF; ++i) {
3869 for (int j = 0; j < nDOF; ++j) {
3870 xMatrix[i][nDOF+j] *= invDiag[j];
3871 }
3872 }
3873 //compute a vector y in which coefficients of the best-fit
3874 //model functions are stored.
3875 //in case of polynomials, y consists of (a0,a1,a2,...)
3876 //where ai is the coefficient of the term x^i.
3877 //in case of sinusoids, y consists of (a0,s1,c1,s2,c2,...)
3878 //where a0 is constant term and s* and c* are of sine
3879 //and cosine functions, respectively.
3880 std::vector<double> y(nDOF);
3881 for (int i = 0; i < nDOF; ++i) {
3882 y[i] = 0.0;
3883 for (int j = 0; j < nDOF; ++j) {
3884 y[i] += xMatrix[i][nDOF+j]*zMatrix[j];
3885 }
3886 params[i] = (float)y[i];
3887 }
3888
3889 for (int i = 0; i < nChan; ++i) {
3890 r1[i] = y[0];
3891 for (int j = 1; j < nDOF; ++j) {
3892 r1[i] += y[j]*model[j][i];
3893 }
3894 residual[i] = z1[i] - r1[i];
3895 }
3896
3897 double mean = 0.0;
3898 double mean2 = 0.0;
3899 for (int i = 0; i < nChan; ++i) {
3900 if (maskArray[i] == 0) continue;
3901 mean += residual[i];
3902 mean2 += residual[i]*residual[i];
3903 }
3904 mean /= (double)nData;
3905 mean2 /= (double)nData;
3906 double rmsd = sqrt(mean2 - mean*mean);
3907 rms = (float)rmsd;
3908
3909 if ((nClip == nIterClip) || (thresClip <= 0.0)) {
3910 break;
3911 } else {
3912
3913 double thres = rmsd * thresClip;
3914 int newNData = 0;
3915 for (int i = 0; i < nChan; ++i) {
3916 if (abs(residual[i]) >= thres) {
3917 maskArray[i] = 0;
3918 finalMask[i] = false;
3919 }
3920 if (maskArray[i] > 0) {
3921 newNData++;
3922 }
3923 }
3924 if (newNData == nData) {
3925 break; //no more flag to add. stop iteration.
3926 } else {
3927 nData = newNData;
3928 }
3929
3930 }
3931 }
3932
3933 nClipped = initNData - nData;
3934
3935 std::vector<float> result(nChan);
3936 if (getResidual) {
3937 for (int i = 0; i < nChan; ++i) {
3938 result[i] = (float)residual[i];
3939 }
3940 } else {
3941 for (int i = 0; i < nChan; ++i) {
3942 result[i] = (float)r1[i];
3943 }
3944 }
3945
3946 return result;
3947} //xMatrix
3948
3949void Scantable::cubicSplineBaseline(const std::vector<bool>& mask, int nPiece,
3950 float thresClip, int nIterClip,
3951 bool getResidual,
3952 const std::string& progressInfo,
3953 const bool outLogger, const std::string& blfile,
3954 const std::string& bltable)
3955{
3956 /****
3957 double TimeStart = mathutil::gettimeofday_sec();
3958 ****/
3959
3960 try {
3961 ofstream ofs;
3962 String coordInfo;
3963 bool hasSameNchan, outTextFile, csvFormat, showProgress;
3964 int minNRow;
3965 int nRow = nrow();
3966 std::vector<bool> chanMask, finalChanMask;
3967 float rms;
3968 bool outBaselineTable = (bltable != "");
3969 STBaselineTable bt = STBaselineTable(*this);
3970 Vector<Double> timeSecCol;
3971 size_t flagged=0;
3972
3973 initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
3974 coordInfo, hasSameNchan,
3975 progressInfo, showProgress, minNRow,
3976 timeSecCol);
3977
3978 std::vector<int> nChanNos;
3979 std::vector<std::vector<std::vector<double> > > modelReservoir;
3980 modelReservoir = getPolynomialModelReservoir(3,
3981 &Scantable::getNormalPolynomial,
3982 nChanNos);
3983 int nDOF = nPiece + 3;
3984
3985 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
3986 std::vector<float> sp = getSpectrum(whichrow);
3987 chanMask = getCompositeChanMask(whichrow, mask);
3988 std::vector<int> pieceEdges;
3989 std::vector<float> params;
3990
3991 //if (flagrowCol_(whichrow) == 0) {
3992 if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
3993 int nClipped = 0;
3994 std::vector<float> res;
3995 res = doCubicSplineLeastSquareFitting(sp, chanMask,
3996 modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
3997 nPiece, false, pieceEdges, params, rms, finalChanMask,
3998 nClipped, thresClip, nIterClip, getResidual);
3999
4000 if (outBaselineTable) {
4001 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
4002 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
4003 true, STBaselineFunc::CSpline, pieceEdges, std::vector<float>(),
4004 getMaskListFromMask(finalChanMask), params, rms, sp.size(),
4005 thresClip, nIterClip, 0.0, 0, std::vector<int>());
4006 } else {
4007 setSpectrum(res, whichrow);
4008 }
4009
4010 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
4011 coordInfo, hasSameNchan, ofs, "cubicSplineBaseline()",
4012 pieceEdges, params, nClipped);
4013 } else {
4014 // no valid channels to fit (flag the row)
4015 flagrowCol_.put(whichrow, 1);
4016 ++flagged;
4017 if (outBaselineTable) {
4018 pieceEdges.resize(nPiece+1);
4019 for (uInt i = 0; i < pieceEdges.size(); ++i) {
4020 pieceEdges[i] = 0;
4021 }
4022 params.resize(nDOF);
4023 for (uInt i = 0; i < params.size(); ++i) {
4024 params[i] = 0.0;
4025 }
4026 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
4027 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
4028 true, STBaselineFunc::CSpline, pieceEdges, std::vector<float>(),
4029 getMaskListFromMask(chanMask), params, 0.0, sp.size(),
4030 thresClip, nIterClip, 0.0, 0, std::vector<int>());
4031 }
4032 }
4033
4034 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
4035 }
4036
4037 finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
4038
4039 if (flagged > 0) {
4040 LogIO os( LogOrigin( "Scantable", "cubicSplineBaseline()") ) ;
4041 os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
4042 }
4043 } catch (...) {
4044 throw;
4045 }
4046
4047 /****
4048 double TimeEnd = mathutil::gettimeofday_sec();
4049 double elapse1 = TimeEnd - TimeStart;
4050 std::cout << "cspline-new : " << elapse1 << " (sec.)" << endl;
4051 ****/
4052}
4053
4054void Scantable::autoCubicSplineBaseline(const std::vector<bool>& mask, int nPiece,
4055 float thresClip, int nIterClip,
4056 const std::vector<int>& edge,
4057 float threshold, int chanAvgLimit,
4058 bool getResidual,
4059 const std::string& progressInfo,
4060 const bool outLogger, const std::string& blfile,
4061 const std::string& bltable)
4062{
4063 try {
4064 ofstream ofs;
4065 String coordInfo;
4066 bool hasSameNchan, outTextFile, csvFormat, showProgress;
4067 int minNRow;
4068 int nRow = nrow();
4069 std::vector<bool> chanMask, finalChanMask;
4070 float rms;
4071 bool outBaselineTable = (bltable != "");
4072 STBaselineTable bt = STBaselineTable(*this);
4073 Vector<Double> timeSecCol;
4074 STLineFinder lineFinder = STLineFinder();
4075 size_t flagged=0;
4076
4077 initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
4078 coordInfo, hasSameNchan,
4079 progressInfo, showProgress, minNRow,
4080 timeSecCol);
4081
4082 initLineFinder(edge, threshold, chanAvgLimit, lineFinder);
4083
4084 std::vector<int> nChanNos;
4085 std::vector<std::vector<std::vector<double> > > modelReservoir;
4086 modelReservoir = getPolynomialModelReservoir(3,
4087 &Scantable::getNormalPolynomial,
4088 nChanNos);
4089 int nDOF = nPiece + 3;
4090
4091 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
4092 std::vector<float> sp = getSpectrum(whichrow);
4093 std::vector<int> currentEdge;
4094 chanMask = getCompositeChanMask(whichrow, mask, edge, currentEdge, lineFinder);
4095 std::vector<int> pieceEdges;
4096 std::vector<float> params;
4097
4098 //if (flagrowCol_(whichrow) == 0) {
4099 if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
4100 int nClipped = 0;
4101 std::vector<float> res;
4102 res = doCubicSplineLeastSquareFitting(sp, chanMask,
4103 modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
4104 nPiece, false, pieceEdges, params, rms, finalChanMask,
4105 nClipped, thresClip, nIterClip, getResidual);
4106
4107 if (outBaselineTable) {
4108 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
4109 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
4110 true, STBaselineFunc::CSpline, pieceEdges, std::vector<float>(),
4111 getMaskListFromMask(finalChanMask), params, rms, sp.size(),
4112 thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
4113 } else {
4114 setSpectrum(res, whichrow);
4115 }
4116
4117 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
4118 coordInfo, hasSameNchan, ofs, "autoCubicSplineBaseline()",
4119 pieceEdges, params, nClipped);
4120 } else {
4121 // no valid channels to fit (flag the row)
4122 flagrowCol_.put(whichrow, 1);
4123 ++flagged;
4124 if (outBaselineTable) {
4125 pieceEdges.resize(nPiece+1);
4126 for (uInt i = 0; i < pieceEdges.size(); ++i) {
4127 pieceEdges[i] = 0;
4128 }
4129 params.resize(nDOF);
4130 for (uInt i = 0; i < params.size(); ++i) {
4131 params[i] = 0.0;
4132 }
4133 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
4134 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
4135 true, STBaselineFunc::CSpline, pieceEdges, std::vector<float>(),
4136 getMaskListFromMask(chanMask), params, 0.0, sp.size(),
4137 thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
4138 }
4139 }
4140
4141 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
4142 }
4143
4144 finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
4145
4146 if (flagged > 0) {
4147 LogIO os( LogOrigin( "Scantable", "autoCubicSplineBaseline()") ) ;
4148 os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
4149 }
4150 } catch (...) {
4151 throw;
4152 }
4153}
4154
4155std::vector<float> Scantable::doCubicSplineFitting(const std::vector<float>& data,
4156 const std::vector<bool>& mask,
4157 std::vector<int>& idxEdge,
4158 std::vector<float>& params,
4159 float& rms,
4160 std::vector<bool>& finalmask,
4161 float clipth,
4162 int clipn)
4163{
4164 int nClipped = 0;
4165 return doCubicSplineFitting(data, mask, idxEdge.size()-1, true, idxEdge, params, rms, finalmask, nClipped, clipth, clipn);
4166}
4167
4168std::vector<float> Scantable::doCubicSplineFitting(const std::vector<float>& data,
4169 const std::vector<bool>& mask,
4170 int nPiece,
4171 std::vector<int>& idxEdge,
4172 std::vector<float>& params,
4173 float& rms,
4174 std::vector<bool>& finalmask,
4175 float clipth,
4176 int clipn)
4177{
4178 int nClipped = 0;
4179 return doCubicSplineFitting(data, mask, nPiece, false, idxEdge, params, rms, finalmask, nClipped, clipth, clipn);
4180}
4181
4182std::vector<float> Scantable::doCubicSplineFitting(const std::vector<float>& data,
4183 const std::vector<bool>& mask,
4184 int nPiece,
4185 bool useGivenPieceBoundary,
4186 std::vector<int>& idxEdge,
4187 std::vector<float>& params,
4188 float& rms,
4189 std::vector<bool>& finalMask,
4190 int& nClipped,
4191 float thresClip,
4192 int nIterClip,
4193 bool getResidual)
4194{
4195 return doCubicSplineLeastSquareFitting(data, mask,
4196 getPolynomialModel(3, data.size(), &Scantable::getNormalPolynomial),
4197 nPiece, useGivenPieceBoundary, idxEdge,
4198 params, rms, finalMask,
4199 nClipped, thresClip, nIterClip,
4200 getResidual);
4201}
4202
4203std::vector<float> Scantable::doCubicSplineLeastSquareFitting(const std::vector<float>& data,
4204 const std::vector<bool>& mask,
4205 const std::vector<std::vector<double> >& model,
4206 int nPiece,
4207 bool useGivenPieceBoundary,
4208 std::vector<int>& idxEdge,
4209 std::vector<float>& params,
4210 float& rms,
4211 std::vector<bool>& finalMask,
4212 int& nClipped,
4213 float thresClip,
4214 int nIterClip,
4215 bool getResidual)
4216{
4217 int nDOF = nPiece + 3; //number of independent parameters to solve, namely, 4+(nPiece-1).
4218 int nModel = model.size();
4219 int nChan = data.size();
4220
4221 if (nModel != 4) {
4222 throw(AipsError("model size must be 4."));
4223 }
4224 if (nPiece < 1) {
4225 throw(AipsError("number of the sections must be one or more"));
4226 }
4227 if (nChan < 2*nPiece) {
4228 throw(AipsError("data size is too few"));
4229 }
4230 if (nChan != (int)mask.size()) {
4231 throw(AipsError("data and mask sizes are not identical"));
4232 }
4233 for (int i = 0; i < nModel; ++i) {
4234 if (nChan != (int)model[i].size()) {
4235 throw(AipsError("data and model sizes are not identical"));
4236 }
4237 }
4238
4239 params.clear();
4240 params.resize(nPiece*nModel);
4241
4242 finalMask.clear();
4243 finalMask.resize(nChan);
4244
4245 std::vector<int> maskArray(nChan);
4246 std::vector<int> x(nChan);
4247 int j = 0;
4248 for (int i = 0; i < nChan; ++i) {
4249 maskArray[i] = mask[i] ? 1 : 0;
4250 if (isnan(data[i])) maskArray[i] = 0;
4251 if (isinf(data[i])) maskArray[i] = 0;
4252
4253 finalMask[i] = (maskArray[i] == 1);
4254 if (finalMask[i]) {
4255 x[j] = i;
4256 j++;
4257 }
4258
4259 /*
4260 maskArray[i] = mask[i] ? 1 : 0;
4261 if (mask[i]) {
4262 x[j] = i;
4263 j++;
4264 }
4265 finalMask[i] = mask[i];
4266 */
4267 }
4268
4269 int initNData = j;
4270 int nData = initNData;
4271
4272 if (initNData < nPiece) {
4273 throw(AipsError("too few non-flagged channels"));
4274 }
4275
4276 int nElement = (int)(floor(floor((double)(initNData/nPiece))+0.5));
4277 std::vector<double> invEdge(nPiece-1);
4278
4279 if (useGivenPieceBoundary) {
4280 if ((int)idxEdge.size() != nPiece+1) {
4281 throw(AipsError("pieceEdge.size() must be equal to nPiece+1."));
4282 }
4283 } else {
4284 idxEdge.clear();
4285 idxEdge.resize(nPiece+1);
4286 idxEdge[0] = x[0];
4287 }
4288 for (int i = 1; i < nPiece; ++i) {
4289 int valX = x[nElement*i];
4290 if (!useGivenPieceBoundary) {
4291 idxEdge[i] = valX;
4292 }
4293 invEdge[i-1] = 1.0/(double)valX;
4294 }
4295 if (!useGivenPieceBoundary) {
4296 idxEdge[nPiece] = x[initNData-1]+1;
4297 }
4298
4299 std::vector<double> z1(nChan), r1(nChan), residual(nChan);
4300 for (int i = 0; i < nChan; ++i) {
4301 z1[i] = (double)data[i];
4302 r1[i] = 0.0;
4303 residual[i] = 0.0;
4304 }
4305
4306 for (int nClip = 0; nClip < nIterClip+1; ++nClip) {
4307 // xMatrix : horizontal concatenation of
4308 // the least-sq. matrix (left) and an
4309 // identity matrix (right).
4310 // the right part is used to calculate the inverse matrix of the left part.
4311
4312 double xMatrix[nDOF][2*nDOF];
4313 double zMatrix[nDOF];
4314 for (int i = 0; i < nDOF; ++i) {
4315 for (int j = 0; j < 2*nDOF; ++j) {
4316 xMatrix[i][j] = 0.0;
4317 }
4318 xMatrix[i][nDOF+i] = 1.0;
4319 zMatrix[i] = 0.0;
4320 }
4321
4322 for (int n = 0; n < nPiece; ++n) {
4323 int nUseDataInPiece = 0;
4324 for (int k = idxEdge[n]; k < idxEdge[n+1]; ++k) {
4325
4326 if (maskArray[k] == 0) continue;
4327
4328 for (int i = 0; i < nModel; ++i) {
4329 for (int j = i; j < nModel; ++j) {
4330 xMatrix[i][j] += model[i][k] * model[j][k];
4331 }
4332 zMatrix[i] += z1[k] * model[i][k];
4333 }
4334
4335 for (int i = 0; i < n; ++i) {
4336 double q = 1.0 - model[1][k]*invEdge[i];
4337 q = q*q*q;
4338 for (int j = 0; j < nModel; ++j) {
4339 xMatrix[j][i+nModel] += q * model[j][k];
4340 }
4341 for (int j = 0; j < i; ++j) {
4342 double r = 1.0 - model[1][k]*invEdge[j];
4343 r = r*r*r;
4344 xMatrix[j+nModel][i+nModel] += r*q;
4345 }
4346 xMatrix[i+nModel][i+nModel] += q*q;
4347 zMatrix[i+nModel] += q*z1[k];
4348 }
4349
4350 nUseDataInPiece++;
4351 }
4352
4353 if (nUseDataInPiece < 1) {
4354 std::vector<string> suffixOfPieceNumber(4);
4355 suffixOfPieceNumber[0] = "th";
4356 suffixOfPieceNumber[1] = "st";
4357 suffixOfPieceNumber[2] = "nd";
4358 suffixOfPieceNumber[3] = "rd";
4359 int idxNoDataPiece = (n % 10 <= 3) ? n : 0;
4360 ostringstream oss;
4361 oss << "all channels clipped or masked in " << n << suffixOfPieceNumber[idxNoDataPiece];
4362 oss << " piece of the spectrum. can't execute fitting anymore.";
4363 throw(AipsError(String(oss)));
4364 }
4365 }
4366
4367 for (int i = 0; i < nDOF; ++i) {
4368 for (int j = 0; j < i; ++j) {
4369 xMatrix[i][j] = xMatrix[j][i];
4370 }
4371 }
4372
4373 std::vector<double> invDiag(nDOF);
4374 for (int i = 0; i < nDOF; ++i) {
4375 invDiag[i] = 1.0 / xMatrix[i][i];
4376 for (int j = 0; j < nDOF; ++j) {
4377 xMatrix[i][j] *= invDiag[i];
4378 }
4379 }
4380
4381 for (int k = 0; k < nDOF; ++k) {
4382 for (int i = 0; i < nDOF; ++i) {
4383 if (i != k) {
4384 double factor1 = xMatrix[k][k];
4385 double invfactor1 = 1.0 / factor1;
4386 double factor2 = xMatrix[i][k];
4387 for (int j = k; j < 2*nDOF; ++j) {
4388 xMatrix[i][j] *= factor1;
4389 xMatrix[i][j] -= xMatrix[k][j]*factor2;
4390 xMatrix[i][j] *= invfactor1;
4391 }
4392 }
4393 }
4394 double invXDiag = 1.0 / xMatrix[k][k];
4395 for (int j = k; j < 2*nDOF; ++j) {
4396 xMatrix[k][j] *= invXDiag;
4397 }
4398 }
4399
4400 for (int i = 0; i < nDOF; ++i) {
4401 for (int j = 0; j < nDOF; ++j) {
4402 xMatrix[i][nDOF+j] *= invDiag[j];
4403 }
4404 }
4405
4406 //compute a vector y which consists of the coefficients of the best-fit spline curves
4407 //(a0,a1,a2,a3(,b3,c3,...)), namely, the ones for the leftmost piece and the ones of
4408 //cubic terms for the other pieces (in case nPiece>1).
4409 std::vector<double> y(nDOF);
4410 for (int i = 0; i < nDOF; ++i) {
4411 y[i] = 0.0;
4412 for (int j = 0; j < nDOF; ++j) {
4413 y[i] += xMatrix[i][nDOF+j]*zMatrix[j];
4414 }
4415 }
4416
4417 std::vector<double> a(nModel);
4418 for (int i = 0; i < nModel; ++i) {
4419 a[i] = y[i];
4420 }
4421
4422 int j = 0;
4423 for (int n = 0; n < nPiece; ++n) {
4424 for (int i = idxEdge[n]; i < idxEdge[n+1]; ++i) {
4425 r1[i] = 0.0;
4426 for (int j = 0; j < nModel; ++j) {
4427 r1[i] += a[j] * model[j][i];
4428 }
4429 }
4430 for (int i = 0; i < nModel; ++i) {
4431 params[j+i] = a[i];
4432 }
4433 j += nModel;
4434
4435 if (n == nPiece-1) break;
4436
4437 double d = y[n+nModel];
4438 double iE = invEdge[n];
4439 a[0] += d;
4440 a[1] -= 3.0 * d * iE;
4441 a[2] += 3.0 * d * iE * iE;
4442 a[3] -= d * iE * iE * iE;
4443 }
4444
4445 //subtract constant value for masked regions at the edge of spectrum
4446 if (idxEdge[0] > 0) {
4447 int n = idxEdge[0];
4448 for (int i = 0; i < idxEdge[0]; ++i) {
4449 //--cubic extrapolate--
4450 //r1[i] = params[0] + params[1]*x1[i] + params[2]*x2[i] + params[3]*x3[i];
4451 //--linear extrapolate--
4452 //r1[i] = (r1[n+1] - r1[n])/(x1[n+1] - x1[n])*(x1[i] - x1[n]) + r1[n];
4453 //--constant--
4454 r1[i] = r1[n];
4455 }
4456 }
4457
4458 if (idxEdge[nPiece] < nChan) {
4459 int n = idxEdge[nPiece]-1;
4460 for (int i = idxEdge[nPiece]; i < nChan; ++i) {
4461 //--cubic extrapolate--
4462 //int m = 4*(nPiece-1);
4463 //r1[i] = params[m] + params[m+1]*x1[i] + params[m+2]*x2[i] + params[m+3]*x3[i];
4464 //--linear extrapolate--
4465 //r1[i] = (r1[n-1] - r1[n])/(x1[n-1] - x1[n])*(x1[i] - x1[n]) + r1[n];
4466 //--constant--
4467 r1[i] = r1[n];
4468 }
4469 }
4470
4471 for (int i = 0; i < nChan; ++i) {
4472 residual[i] = z1[i] - r1[i];
4473 }
4474
4475 double mean = 0.0;
4476 double mean2 = 0.0;
4477 for (int i = 0; i < nChan; ++i) {
4478 if (maskArray[i] == 0) continue;
4479 mean += residual[i];
4480 mean2 += residual[i]*residual[i];
4481 }
4482 mean /= (double)nData;
4483 mean2 /= (double)nData;
4484 double rmsd = sqrt(mean2 - mean*mean);
4485 rms = (float)rmsd;
4486
4487 if ((nClip == nIterClip) || (thresClip <= 0.0)) {
4488 break;
4489 } else {
4490
4491 double thres = rmsd * thresClip;
4492 int newNData = 0;
4493 for (int i = 0; i < nChan; ++i) {
4494 if (abs(residual[i]) >= thres) {
4495 maskArray[i] = 0;
4496 finalMask[i] = false;
4497 }
4498 if (maskArray[i] > 0) {
4499 newNData++;
4500 }
4501 }
4502 if (newNData == nData) {
4503 break; //no more flag to add. iteration stops.
4504 } else {
4505 nData = newNData;
4506 }
4507
4508 }
4509 }
4510
4511 nClipped = initNData - nData;
4512
4513 std::vector<float> result(nChan);
4514 if (getResidual) {
4515 for (int i = 0; i < nChan; ++i) {
4516 result[i] = (float)residual[i];
4517 }
4518 } else {
4519 for (int i = 0; i < nChan; ++i) {
4520 result[i] = (float)r1[i];
4521 }
4522 }
4523
4524 return result;
4525}
4526
4527std::vector<int> Scantable::selectWaveNumbers(const std::vector<int>& addNWaves,
4528 const std::vector<int>& rejectNWaves)
4529{
4530 std::vector<bool> chanMask;
4531 std::string fftMethod;
4532 std::string fftThresh;
4533
4534 return selectWaveNumbers(0, chanMask, false, fftMethod, fftThresh, addNWaves, rejectNWaves);
4535}
4536
4537std::vector<int> Scantable::selectWaveNumbers(const int whichrow,
4538 const std::vector<bool>& chanMask,
4539 const bool applyFFT,
4540 const std::string& fftMethod,
4541 const std::string& fftThresh,
4542 const std::vector<int>& addNWaves,
4543 const std::vector<int>& rejectNWaves)
4544{
4545 std::vector<int> nWaves;
4546 nWaves.clear();
4547
4548 if (applyFFT) {
4549 string fftThAttr;
4550 float fftThSigma;
4551 int fftThTop;
4552 parseFFTThresholdInfo(fftThresh, fftThAttr, fftThSigma, fftThTop);
4553 doSelectWaveNumbers(whichrow, chanMask, fftMethod, fftThSigma, fftThTop, fftThAttr, nWaves);
4554 }
4555
4556 addAuxWaveNumbers(whichrow, addNWaves, rejectNWaves, nWaves);
4557
4558 return nWaves;
4559}
4560
4561int Scantable::getIdxOfNchan(const int nChan, const std::vector<int>& nChanNos)
4562{
4563 int idx = -1;
4564 for (uint i = 0; i < nChanNos.size(); ++i) {
4565 if (nChan == nChanNos[i]) {
4566 idx = i;
4567 break;
4568 }
4569 }
4570
4571 if (idx < 0) {
4572 throw(AipsError("nChan not found in nChhanNos."));
4573 }
4574
4575 return idx;
4576}
4577
4578void Scantable::parseFFTInfo(const std::string& fftInfo, bool& applyFFT, std::string& fftMethod, std::string& fftThresh)
4579{
4580 istringstream iss(fftInfo);
4581 std::string tmp;
4582 std::vector<string> res;
4583 while (getline(iss, tmp, ',')) {
4584 res.push_back(tmp);
4585 }
4586 if (res.size() < 3) {
4587 throw(AipsError("wrong value in 'fftinfo' parameter")) ;
4588 }
4589 applyFFT = (res[0] == "true");
4590 fftMethod = res[1];
4591 fftThresh = res[2];
4592}
4593
4594void Scantable::parseFFTThresholdInfo(const std::string& fftThresh, std::string& fftThAttr, float& fftThSigma, int& fftThTop)
4595{
4596 uInt idxSigma = fftThresh.find("sigma");
4597 uInt idxTop = fftThresh.find("top");
4598
4599 if (idxSigma == fftThresh.size() - 5) {
4600 std::istringstream is(fftThresh.substr(0, fftThresh.size() - 5));
4601 is >> fftThSigma;
4602 fftThAttr = "sigma";
4603 } else if (idxTop == 0) {
4604 std::istringstream is(fftThresh.substr(3));
4605 is >> fftThTop;
4606 fftThAttr = "top";
4607 } else {
4608 bool isNumber = true;
4609 for (uInt i = 0; i < fftThresh.size()-1; ++i) {
4610 char ch = (fftThresh.substr(i, 1).c_str())[0];
4611 if (!(isdigit(ch) || (fftThresh.substr(i, 1) == "."))) {
4612 isNumber = false;
4613 break;
4614 }
4615 }
4616 if (isNumber) {
4617 std::istringstream is(fftThresh);
4618 is >> fftThSigma;
4619 fftThAttr = "sigma";
4620 } else {
4621 throw(AipsError("fftthresh has a wrong value"));
4622 }
4623 }
4624}
4625
4626void Scantable::doSelectWaveNumbers(const int whichrow, const std::vector<bool>& chanMask, const std::string& fftMethod, const float fftThSigma, const int fftThTop, const std::string& fftThAttr, std::vector<int>& nWaves)
4627{
4628 std::vector<float> fspec;
4629 if (fftMethod == "fft") {
4630 fspec = execFFT(whichrow, chanMask, false, true);
4631 //} else if (fftMethod == "lsp") {
4632 // fspec = lombScarglePeriodogram(whichrow);
4633 }
4634
4635 if (fftThAttr == "sigma") {
4636 float mean = 0.0;
4637 float mean2 = 0.0;
4638 for (uInt i = 0; i < fspec.size(); ++i) {
4639 mean += fspec[i];
4640 mean2 += fspec[i]*fspec[i];
4641 }
4642 mean /= float(fspec.size());
4643 mean2 /= float(fspec.size());
4644 float thres = mean + fftThSigma * float(sqrt(mean2 - mean*mean));
4645
4646 for (uInt i = 0; i < fspec.size(); ++i) {
4647 if (fspec[i] >= thres) {
4648 nWaves.push_back(i);
4649 }
4650 }
4651
4652 } else if (fftThAttr == "top") {
4653 for (int i = 0; i < fftThTop; ++i) {
4654 float max = 0.0;
4655 int maxIdx = 0;
4656 for (uInt j = 0; j < fspec.size(); ++j) {
4657 if (fspec[j] > max) {
4658 max = fspec[j];
4659 maxIdx = j;
4660 }
4661 }
4662 nWaves.push_back(maxIdx);
4663 fspec[maxIdx] = 0.0;
4664 }
4665
4666 }
4667
4668 if (nWaves.size() > 1) {
4669 sort(nWaves.begin(), nWaves.end());
4670 }
4671}
4672
4673void Scantable::addAuxWaveNumbers(const int whichrow, const std::vector<int>& addNWaves, const std::vector<int>& rejectNWaves, std::vector<int>& nWaves)
4674{
4675 std::vector<int> tempAddNWaves, tempRejectNWaves;
4676 tempAddNWaves.clear();
4677 tempRejectNWaves.clear();
4678
4679 for (uInt i = 0; i < addNWaves.size(); ++i) {
4680 tempAddNWaves.push_back(addNWaves[i]);
4681 }
4682 if ((tempAddNWaves.size() == 2) && (tempAddNWaves[1] == -999)) {
4683 setWaveNumberListUptoNyquistFreq(whichrow, tempAddNWaves);
4684 }
4685
4686 for (uInt i = 0; i < rejectNWaves.size(); ++i) {
4687 tempRejectNWaves.push_back(rejectNWaves[i]);
4688 }
4689 if ((tempRejectNWaves.size() == 2) && (tempRejectNWaves[1] == -999)) {
4690 setWaveNumberListUptoNyquistFreq(whichrow, tempRejectNWaves);
4691 }
4692
4693 for (uInt i = 0; i < tempAddNWaves.size(); ++i) {
4694 bool found = false;
4695 for (uInt j = 0; j < nWaves.size(); ++j) {
4696 if (nWaves[j] == tempAddNWaves[i]) {
4697 found = true;
4698 break;
4699 }
4700 }
4701 if (!found) nWaves.push_back(tempAddNWaves[i]);
4702 }
4703
4704 for (uInt i = 0; i < tempRejectNWaves.size(); ++i) {
4705 for (std::vector<int>::iterator j = nWaves.begin(); j != nWaves.end(); ) {
4706 if (*j == tempRejectNWaves[i]) {
4707 j = nWaves.erase(j);
4708 } else {
4709 ++j;
4710 }
4711 }
4712 }
4713
4714 if (nWaves.size() > 1) {
4715 sort(nWaves.begin(), nWaves.end());
4716 unique(nWaves.begin(), nWaves.end());
4717 }
4718}
4719
4720void Scantable::setWaveNumberListUptoNyquistFreq(const int whichrow, std::vector<int>& nWaves)
4721{
4722 int val = nWaves[0];
4723 int nyquistFreq = nchan(getIF(whichrow))/2+1;
4724 nWaves.clear();
4725 if (val > nyquistFreq) { // for safety, at least nWaves contains a constant; CAS-3759
4726 nWaves.push_back(0);
4727 }
4728 while (val <= nyquistFreq) {
4729 nWaves.push_back(val);
4730 val++;
4731 }
4732}
4733
4734void Scantable::sinusoidBaseline(const std::vector<bool>& mask, const std::string& fftInfo,
4735 const std::vector<int>& addNWaves,
4736 const std::vector<int>& rejectNWaves,
4737 float thresClip, int nIterClip,
4738 bool getResidual,
4739 const std::string& progressInfo,
4740 const bool outLogger, const std::string& blfile,
4741 const std::string& bltable)
4742{
4743 /****
4744 double TimeStart = mathutil::gettimeofday_sec();
4745 ****/
4746
4747 try {
4748 ofstream ofs;
4749 String coordInfo;
4750 bool hasSameNchan, outTextFile, csvFormat, showProgress;
4751 int minNRow;
4752 int nRow = nrow();
4753 std::vector<bool> chanMask, finalChanMask;
4754 float rms;
4755 bool outBaselineTable = (bltable != "");
4756 STBaselineTable bt = STBaselineTable(*this);
4757 Vector<Double> timeSecCol;
4758 size_t flagged=0;
4759
4760 initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
4761 coordInfo, hasSameNchan,
4762 progressInfo, showProgress, minNRow,
4763 timeSecCol);
4764
4765 bool applyFFT;
4766 std::string fftMethod, fftThresh;
4767 parseFFTInfo(fftInfo, applyFFT, fftMethod, fftThresh);
4768
4769 std::vector<int> nWaves;
4770 std::vector<int> nChanNos;
4771 std::vector<std::vector<std::vector<double> > > modelReservoir;
4772 if (!applyFFT) {
4773 nWaves = selectWaveNumbers(addNWaves, rejectNWaves);
4774 if (nWaves.size()==0) //no wave numbers to fit
4775 throw(AipsError("No valid wave numbers to fit"));
4776 modelReservoir = getSinusoidModelReservoir(nWaves, nChanNos);
4777 }
4778
4779 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
4780 std::vector<float> sp = getSpectrum(whichrow);
4781 chanMask = getCompositeChanMask(whichrow, mask);
4782 std::vector<std::vector<double> > model;
4783 bool canfit = true;
4784 if (applyFFT) {
4785 nWaves = selectWaveNumbers(whichrow, chanMask, true, fftMethod, fftThresh,
4786 addNWaves, rejectNWaves);
4787 if (nWaves.size()==0) {// no wave numbers to fit.
4788 canfit = false;
4789 break;
4790 }
4791 model = getSinusoidModel(nWaves, sp.size());
4792 } else {
4793 model = modelReservoir[getIdxOfNchan(sp.size(), nChanNos)];
4794 }
4795 int nModel = modelReservoir.size();
4796
4797 std::vector<float> params;
4798
4799 //if (flagrowCol_(whichrow) == 0) {
4800 if (canfit && flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
4801 int nClipped = 0;
4802 std::vector<float> res;
4803 res = doLeastSquareFitting(sp, chanMask, model,
4804 params, rms, finalChanMask,
4805 nClipped, thresClip, nIterClip, getResidual);
4806
4807 if (outBaselineTable) {
4808 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
4809 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
4810 true, STBaselineFunc::Sinusoid, nWaves, std::vector<float>(),
4811 getMaskListFromMask(finalChanMask), params, rms, sp.size(),
4812 thresClip, nIterClip, 0.0, 0, std::vector<int>());
4813 } else {
4814 setSpectrum(res, whichrow);
4815 }
4816
4817 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
4818 coordInfo, hasSameNchan, ofs, "sinusoidBaseline()",
4819 params, nClipped);
4820 } else {
4821 // no valid channels to fit (flag the row)
4822 flagrowCol_.put(whichrow, 1);
4823 ++flagged;
4824 if (outBaselineTable) {
4825 params.resize(nModel);
4826 for (uInt i = 0; i < params.size(); ++i) {
4827 params[i] = 0.0;
4828 }
4829 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
4830 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
4831 true, STBaselineFunc::Sinusoid, nWaves, std::vector<float>(),
4832 getMaskListFromMask(chanMask), params, 0.0, sp.size(),
4833 thresClip, nIterClip, 0.0, 0, std::vector<int>());
4834 }
4835 }
4836
4837 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
4838 }
4839
4840 finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
4841
4842 if (flagged > 0) {
4843 LogIO os( LogOrigin( "Scantable", "sinusoidBaseline()") ) ;
4844 os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
4845 }
4846 } catch (...) {
4847 throw;
4848 }
4849
4850 /****
4851 double TimeEnd = mathutil::gettimeofday_sec();
4852 double elapse1 = TimeEnd - TimeStart;
4853 std::cout << "sinusoid-old : " << elapse1 << " (sec.)" << endl;
4854 ****/
4855}
4856
4857void Scantable::autoSinusoidBaseline(const std::vector<bool>& mask, const std::string& fftInfo,
4858 const std::vector<int>& addNWaves,
4859 const std::vector<int>& rejectNWaves,
4860 float thresClip, int nIterClip,
4861 const std::vector<int>& edge,
4862 float threshold, int chanAvgLimit,
4863 bool getResidual,
4864 const std::string& progressInfo,
4865 const bool outLogger, const std::string& blfile,
4866 const std::string& bltable)
4867{
4868 try {
4869 ofstream ofs;
4870 String coordInfo;
4871 bool hasSameNchan, outTextFile, csvFormat, showProgress;
4872 int minNRow;
4873 int nRow = nrow();
4874 std::vector<bool> chanMask, finalChanMask;
4875 float rms;
4876 bool outBaselineTable = (bltable != "");
4877 STBaselineTable bt = STBaselineTable(*this);
4878 Vector<Double> timeSecCol;
4879 STLineFinder lineFinder = STLineFinder();
4880 size_t flagged=0;
4881
4882 initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
4883 coordInfo, hasSameNchan,
4884 progressInfo, showProgress, minNRow,
4885 timeSecCol);
4886
4887 initLineFinder(edge, threshold, chanAvgLimit, lineFinder);
4888
4889 bool applyFFT;
4890 string fftMethod, fftThresh;
4891 parseFFTInfo(fftInfo, applyFFT, fftMethod, fftThresh);
4892
4893 std::vector<int> nWaves;
4894 std::vector<int> nChanNos;
4895 std::vector<std::vector<std::vector<double> > > modelReservoir;
4896 if (!applyFFT) {
4897 nWaves = selectWaveNumbers(addNWaves, rejectNWaves);
4898 if (nWaves.size()==0) //no wave numbers to fit
4899 throw(AipsError("No valid wave numbers to fit"));
4900 modelReservoir = getSinusoidModelReservoir(nWaves, nChanNos);
4901 }
4902
4903 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
4904 std::vector<float> sp = getSpectrum(whichrow);
4905 std::vector<int> currentEdge;
4906 chanMask = getCompositeChanMask(whichrow, mask, edge, currentEdge, lineFinder);
4907 std::vector<std::vector<double> > model;
4908 bool canfit=true;
4909 if (applyFFT) {
4910 nWaves = selectWaveNumbers(whichrow, chanMask, true, fftMethod, fftThresh,
4911 addNWaves, rejectNWaves);
4912 if (nWaves.size()==0) { // no wave numbers to fit.
4913 canfit = false;
4914 break;
4915 }
4916 model = getSinusoidModel(nWaves, sp.size());
4917 } else {
4918 model = modelReservoir[getIdxOfNchan(sp.size(), nChanNos)];
4919 }
4920 int nModel = modelReservoir.size();
4921
4922 std::vector<float> params;
4923
4924 //if (flagrowCol_(whichrow) == 0) {
4925 if (canfit && flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
4926 int nClipped = 0;
4927 std::vector<float> res;
4928 res = doLeastSquareFitting(sp, chanMask, model,
4929 params, rms, finalChanMask,
4930 nClipped, thresClip, nIterClip, getResidual);
4931
4932 if (outBaselineTable) {
4933 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
4934 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
4935 true, STBaselineFunc::Sinusoid, nWaves, std::vector<float>(),
4936 getMaskListFromMask(finalChanMask), params, rms, sp.size(),
4937 thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
4938 } else {
4939 setSpectrum(res, whichrow);
4940 }
4941
4942 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
4943 coordInfo, hasSameNchan, ofs, "autoSinusoidBaseline()",
4944 params, nClipped);
4945 } else {
4946 // no valid channels to fit (flag the row)
4947 flagrowCol_.put(whichrow, 1);
4948 ++flagged;
4949 if (outBaselineTable) {
4950 params.resize(nModel);
4951 for (uInt i = 0; i < params.size(); ++i) {
4952 params[i] = 0.0;
4953 }
4954 bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
4955 getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
4956 true, STBaselineFunc::Sinusoid, nWaves, std::vector<float>(),
4957 getMaskListFromMask(chanMask), params, 0.0, sp.size(),
4958 thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
4959 }
4960 }
4961
4962 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
4963 }
4964
4965 finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
4966
4967 if (flagged > 0) {
4968 LogIO os( LogOrigin( "Scantable", "autoSinusoidBaseline()") ) ;
4969 os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
4970 }
4971 } catch (...) {
4972 throw;
4973 }
4974}
4975
4976std::vector<float> Scantable::doSinusoidFitting(const std::vector<float>& data,
4977 const std::vector<bool>& mask,
4978 const std::vector<int>& waveNumbers,
4979 std::vector<float>& params,
4980 float& rms,
4981 std::vector<bool>& finalmask,
4982 float clipth,
4983 int clipn)
4984{
4985 int nClipped = 0;
4986 return doSinusoidFitting(data, mask, waveNumbers, params, rms, finalmask, nClipped, clipth, clipn);
4987}
4988
4989std::vector<float> Scantable::doSinusoidFitting(const std::vector<float>& data,
4990 const std::vector<bool>& mask,
4991 const std::vector<int>& waveNumbers,
4992 std::vector<float>& params,
4993 float& rms,
4994 std::vector<bool>& finalMask,
4995 int& nClipped,
4996 float thresClip,
4997 int nIterClip,
4998 bool getResidual)
4999{
5000 return doLeastSquareFitting(data, mask,
5001 getSinusoidModel(waveNumbers, data.size()),
5002 params, rms, finalMask,
5003 nClipped, thresClip, nIterClip,
5004 getResidual);
5005}
5006
5007std::vector<std::vector<std::vector<double> > > Scantable::getSinusoidModelReservoir(const std::vector<int>& waveNumbers,
5008 std::vector<int>& nChanNos)
5009{
5010 std::vector<std::vector<std::vector<double> > > res;
5011 res.clear();
5012 nChanNos.clear();
5013
5014 std::vector<uint> ifNos = getIFNos();
5015 for (uint i = 0; i < ifNos.size(); ++i) {
5016 int currNchan = nchan(ifNos[i]);
5017 bool hasDifferentNchan = (i == 0);
5018 for (uint j = 0; j < i; ++j) {
5019 if (currNchan != nchan(ifNos[j])) {
5020 hasDifferentNchan = true;
5021 break;
5022 }
5023 }
5024 if (hasDifferentNchan) {
5025 res.push_back(getSinusoidModel(waveNumbers, currNchan));
5026 nChanNos.push_back(currNchan);
5027 }
5028 }
5029
5030 return res;
5031}
5032
5033std::vector<std::vector<double> > Scantable::getSinusoidModel(const std::vector<int>& waveNumbers, int nchan)
5034{
5035 // model : contains elemental values for computing the least-square matrix.
5036 // model.size() is nmodel and model[*].size() is nchan.
5037 // Each model element are as follows:
5038 // model[0] = {1.0, 1.0, 1.0, ..., 1.0},
5039 // model[2n-1] = {sin(nPI/L*x[0]), sin(nPI/L*x[1]), ..., sin(nPI/L*x[nchan])},
5040 // model[2n] = {cos(nPI/L*x[0]), cos(nPI/L*x[1]), ..., cos(nPI/L*x[nchan])},
5041 // where (1 <= n <= nMaxWavesInSW),
5042 // or,
5043 // model[2n-1] = {sin(wn[n]PI/L*x[0]), sin(wn[n]PI/L*x[1]), ..., sin(wn[n]PI/L*x[nchan])},
5044 // model[2n] = {cos(wn[n]PI/L*x[0]), cos(wn[n]PI/L*x[1]), ..., cos(wn[n]PI/L*x[nchan])},
5045 // where wn[n] denotes waveNumbers[n] (1 <= n <= waveNumbers.size()).
5046
5047 std::vector<int> nWaves; // sorted and uniqued array of wave numbers
5048 nWaves.reserve(waveNumbers.size());
5049 copy(waveNumbers.begin(), waveNumbers.end(), back_inserter(nWaves));
5050 sort(nWaves.begin(), nWaves.end());
5051 std::vector<int>::iterator end_it = unique(nWaves.begin(), nWaves.end());
5052 nWaves.erase(end_it, nWaves.end());
5053
5054 if (nWaves.size()==0)
5055 throw(AipsError("No valid wavenumbers to fit."));
5056
5057 int minNWaves = nWaves[0];
5058 if (minNWaves < 0) {
5059 throw(AipsError("wave number must be positive or zero (i.e. constant)"));
5060 }
5061 bool hasConstantTerm = (minNWaves == 0);
5062 int nmodel = nWaves.size() * 2 - (hasConstantTerm ? 1 : 0); //number of parameters to solve.
5063
5064 std::vector<std::vector<double> > model(nmodel, std::vector<double>(nchan));
5065
5066 if (hasConstantTerm) {
5067 for (int j = 0; j < nchan; ++j) {
5068 model[0][j] = 1.0;
5069 }
5070 }
5071
5072 const double PI = 6.0 * asin(0.5); // PI (= 3.141592653...)
5073 double stretch0 = 2.0*PI/(double)(nchan-1);
5074
5075 for (uInt i = (hasConstantTerm ? 1 : 0); i < nWaves.size(); ++i) {
5076 int sidx = hasConstantTerm ? 2*i-1 : 2*i;
5077 int cidx = sidx + 1;
5078 double stretch = stretch0*(double)nWaves[i];
5079
5080 for (int j = 0; j < nchan; ++j) {
5081 model[sidx][j] = sin(stretch*(double)j);
5082 model[cidx][j] = cos(stretch*(double)j);
5083 }
5084 }
5085
5086 return model;
5087}
5088
5089std::vector<bool> Scantable::getCompositeChanMask(int whichrow,
5090 const std::vector<bool>& inMask)
5091{
5092 std::vector<bool> mask = getMask(whichrow);
5093 uInt maskSize = mask.size();
5094 if (inMask.size() != 0) {
5095 if (maskSize != inMask.size()) {
5096 throw(AipsError("mask sizes are not the same."));
5097 }
5098 for (uInt i = 0; i < maskSize; ++i) {
5099 mask[i] = mask[i] && inMask[i];
5100 }
5101 }
5102
5103 return mask;
5104}
5105
5106std::vector<bool> Scantable::getCompositeChanMask(int whichrow,
5107 const std::vector<bool>& inMask,
5108 const std::vector<int>& edge,
5109 std::vector<int>& currEdge,
5110 STLineFinder& lineFinder)
5111{
5112 if (isAllChannelsFlagged(whichrow)) {//all channels flagged
5113 std::vector<bool> res_mask(nchan(getIF(whichrow)),false);
5114 return res_mask;
5115 } else if (inMask.size() != 0 && nValidMask(inMask)==0){ //no valid mask channels
5116 std::vector<bool> res_mask(inMask);
5117 return res_mask;
5118 }
5119
5120 std::vector<uint> ifNos = getIFNos();
5121 if ((edge.size() > 2) && (edge.size() < ifNos.size()*2)) {
5122 throw(AipsError("Length of edge element info is less than that of IFs"));
5123 }
5124
5125 uint idx = 0;
5126 if (edge.size() > 2) {
5127 int ifVal = getIF(whichrow);
5128 bool foundIF = false;
5129 for (uint i = 0; i < ifNos.size(); ++i) {
5130 if (ifVal == (int)ifNos[i]) {
5131 idx = 2*i;
5132 foundIF = true;
5133 break;
5134 }
5135 }
5136 if (!foundIF) {
5137 throw(AipsError("bad IF number"));
5138 }
5139 }
5140
5141 currEdge.clear();
5142 currEdge.resize(2);
5143 currEdge[0] = edge[idx];
5144 currEdge[1] = edge[idx+1];
5145
5146 lineFinder.setData(getSpectrum(whichrow));
5147 lineFinder.findLines(getCompositeChanMask(whichrow, inMask), currEdge, whichrow);
5148 return lineFinder.getMask();
5149}
5150
5151/* for cspline. will be merged once cspline is available in fitter (2011/3/10 WK) */
5152void Scantable::outputFittingResult(bool outLogger,
5153 bool outTextFile,
5154 bool csvFormat,
5155 const std::vector<bool>& chanMask,
5156 int whichrow,
5157 const casacore::String& coordInfo,
5158 bool hasSameNchan,
5159 ofstream& ofs,
5160 const casacore::String& funcName,
5161 const std::vector<int>& edge,
5162 const std::vector<float>& params,
5163 const int nClipped)
5164{
5165 if (outLogger || outTextFile) {
5166 float rms = getRms(chanMask, whichrow);
5167 String masklist = getMaskRangeList(chanMask, whichrow, coordInfo, hasSameNchan);
5168 std::vector<bool> fixed;
5169 fixed.clear();
5170
5171 if (outLogger) {
5172 LogIO ols(LogOrigin("Scantable", funcName, WHERE));
5173 ols << formatPiecewiseBaselineParams(edge, params, fixed, rms, nClipped,
5174 masklist, whichrow, false, csvFormat) << LogIO::POST ;
5175 }
5176 if (outTextFile) {
5177 ofs << formatPiecewiseBaselineParams(edge, params, fixed, rms, nClipped,
5178 masklist, whichrow, true, csvFormat) << flush;
5179 }
5180 }
5181}
5182
5183/* for poly/chebyshev/sinusoid. */
5184void Scantable::outputFittingResult(bool outLogger,
5185 bool outTextFile,
5186 bool csvFormat,
5187 const std::vector<bool>& chanMask,
5188 int whichrow,
5189 const casacore::String& coordInfo,
5190 bool hasSameNchan,
5191 ofstream& ofs,
5192 const casacore::String& funcName,
5193 const std::vector<float>& params,
5194 const int nClipped)
5195{
5196 if (outLogger || outTextFile) {
5197 float rms = getRms(chanMask, whichrow);
5198 String masklist = getMaskRangeList(chanMask, whichrow, coordInfo, hasSameNchan);
5199 std::vector<bool> fixed;
5200 fixed.clear();
5201
5202 if (outLogger) {
5203 LogIO ols(LogOrigin("Scantable", funcName, WHERE));
5204 ols << formatBaselineParams(params, fixed, rms, nClipped,
5205 masklist, whichrow, false, csvFormat) << LogIO::POST ;
5206 }
5207 if (outTextFile) {
5208 ofs << formatBaselineParams(params, fixed, rms, nClipped,
5209 masklist, whichrow, true, csvFormat) << flush;
5210 }
5211 }
5212}
5213
5214void Scantable::parseProgressInfo(const std::string& progressInfo, bool& showProgress, int& minNRow)
5215{
5216 int idxDelimiter = progressInfo.find(",");
5217 if (idxDelimiter < 0) {
5218 throw(AipsError("wrong value in 'showprogress' parameter")) ;
5219 }
5220 showProgress = (progressInfo.substr(0, idxDelimiter) == "true");
5221 std::istringstream is(progressInfo.substr(idxDelimiter+1));
5222 is >> minNRow;
5223}
5224
5225void Scantable::showProgressOnTerminal(const int nProcessed, const int nTotal, const bool showProgress, const int nTotalThreshold)
5226{
5227 if (showProgress && (nTotal >= nTotalThreshold)) {
5228 int nInterval = int(floor(double(nTotal)/100.0));
5229 if (nInterval == 0) nInterval++;
5230
5231 if (nProcessed % nInterval == 0) {
5232 printf("\r"); //go to the head of line
5233 printf("\x1b[31m\x1b[1m"); //set red color, highlighted
5234 printf("[%3d%%]", (int)(100.0*(double(nProcessed+1))/(double(nTotal))) );
5235 printf("\x1b[39m\x1b[0m"); //set default attributes
5236 fflush(NULL);
5237 }
5238
5239 if (nProcessed == nTotal - 1) {
5240 printf("\r\x1b[K"); //clear
5241 fflush(NULL);
5242 }
5243
5244 }
5245}
5246
5247std::vector<float> Scantable::execFFT(const int whichrow, const std::vector<bool>& inMask, bool getRealImag, bool getAmplitudeOnly)
5248{
5249 std::vector<bool> mask = getMask(whichrow);
5250
5251 if (inMask.size() > 0) {
5252 uInt maskSize = mask.size();
5253 if (maskSize != inMask.size()) {
5254 throw(AipsError("mask sizes are not the same."));
5255 }
5256 for (uInt i = 0; i < maskSize; ++i) {
5257 mask[i] = mask[i] && inMask[i];
5258 }
5259 }
5260
5261 Vector<Float> spec = getSpectrum(whichrow);
5262 mathutil::doZeroOrderInterpolation(spec, mask);
5263
5264 FFTServer<Float,Complex> ffts;
5265 Vector<Complex> fftres;
5266 ffts.fft0(fftres, spec);
5267
5268 std::vector<float> res;
5269 float norm = float(2.0/double(spec.size()));
5270
5271 if (getRealImag) {
5272 for (uInt i = 0; i < fftres.size(); ++i) {
5273 res.push_back(real(fftres[i])*norm);
5274 res.push_back(imag(fftres[i])*norm);
5275 }
5276 } else {
5277 for (uInt i = 0; i < fftres.size(); ++i) {
5278 res.push_back(abs(fftres[i])*norm);
5279 if (!getAmplitudeOnly) res.push_back(arg(fftres[i]));
5280 }
5281 }
5282
5283 return res;
5284}
5285
5286
5287float Scantable::getRms(const std::vector<bool>& mask, int whichrow)
5288{
5289 /****
5290 double ms1TimeStart, ms1TimeEnd;
5291 double elapse1 = 0.0;
5292 ms1TimeStart = mathutil::gettimeofday_sec();
5293 ****/
5294
5295 Vector<Float> spec;
5296 specCol_.get(whichrow, spec);
5297
5298 /****
5299 ms1TimeEnd = mathutil::gettimeofday_sec();
5300 elapse1 = ms1TimeEnd - ms1TimeStart;
5301 std::cout << "rm1 : " << elapse1 << " (sec.)" << endl;
5302 ****/
5303
5304 return (float)doGetRms(mask, spec);
5305}
5306
5307double Scantable::doGetRms(const std::vector<bool>& mask, const Vector<Float>& spec)
5308{
5309 double mean = 0.0;
5310 double smean = 0.0;
5311 int n = 0;
5312 for (uInt i = 0; i < spec.nelements(); ++i) {
5313 if (mask[i]) {
5314 double val = (double)spec[i];
5315 mean += val;
5316 smean += val*val;
5317 n++;
5318 }
5319 }
5320
5321 mean /= (double)n;
5322 smean /= (double)n;
5323
5324 return sqrt(smean - mean*mean);
5325}
5326
5327std::string Scantable::formatBaselineParamsHeader(int whichrow, const std::string& masklist, bool verbose, bool csvformat) const
5328{
5329 if (verbose) {
5330 ostringstream oss;
5331
5332 if (csvformat) {
5333 oss << getScan(whichrow) << ",";
5334 oss << getBeam(whichrow) << ",";
5335 oss << getIF(whichrow) << ",";
5336 oss << getPol(whichrow) << ",";
5337 oss << getCycle(whichrow) << ",";
5338 String commaReplacedMasklist = masklist;
5339 string::size_type pos = 0;
5340 while (pos = commaReplacedMasklist.find(","), pos != string::npos) {
5341 commaReplacedMasklist.replace(pos, 1, ";");
5342 pos++;
5343 }
5344 oss << commaReplacedMasklist << ",";
5345 } else {
5346 oss << " Scan[" << getScan(whichrow) << "]";
5347 oss << " Beam[" << getBeam(whichrow) << "]";
5348 oss << " IF[" << getIF(whichrow) << "]";
5349 oss << " Pol[" << getPol(whichrow) << "]";
5350 oss << " Cycle[" << getCycle(whichrow) << "]: " << endl;
5351 oss << "Fitter range = " << masklist << endl;
5352 oss << "Baseline parameters" << endl;
5353 }
5354 oss << flush;
5355
5356 return String(oss);
5357 }
5358
5359 return "";
5360}
5361
5362std::string Scantable::formatBaselineParamsFooter(float rms, int nClipped, bool verbose, bool csvformat) const
5363{
5364 if (verbose) {
5365 ostringstream oss;
5366
5367 if (csvformat) {
5368 oss << rms << ",";
5369 if (nClipped >= 0) {
5370 oss << nClipped;
5371 }
5372 } else {
5373 oss << "Results of baseline fit" << endl;
5374 oss << " rms = " << setprecision(6) << rms << endl;
5375 if (nClipped >= 0) {
5376 oss << " Number of clipped channels = " << nClipped << endl;
5377 }
5378 for (int i = 0; i < 60; ++i) {
5379 oss << "-";
5380 }
5381 }
5382 oss << endl;
5383 oss << flush;
5384
5385 return String(oss);
5386 }
5387
5388 return "";
5389}
5390
5391std::string Scantable::formatBaselineParams(const std::vector<float>& params,
5392 const std::vector<bool>& fixed,
5393 float rms,
5394 int nClipped,
5395 const std::string& masklist,
5396 int whichrow,
5397 bool verbose,
5398 bool csvformat,
5399 int start, int count,
5400 bool resetparamid) const
5401{
5402 int nParam = (int)(params.size());
5403
5404 if (nParam < 1) {
5405 return(" Not fitted");
5406 } else {
5407
5408 ostringstream oss;
5409 oss << formatBaselineParamsHeader(whichrow, masklist, verbose, csvformat);
5410
5411 if (start < 0) start = 0;
5412 if (count < 0) count = nParam;
5413 int end = start + count;
5414 if (end > nParam) end = nParam;
5415 int paramidoffset = (resetparamid) ? (-start) : 0;
5416
5417 for (int i = start; i < end; ++i) {
5418 if (i > start) {
5419 oss << ",";
5420 }
5421 std::string sFix = ((fixed.size() > 0) && (fixed[i]) && verbose) ? "(fixed)" : "";
5422 if (csvformat) {
5423 oss << params[i] << sFix;
5424 } else {
5425 oss << " p" << (i+paramidoffset) << sFix << "= " << right << setw(13) << setprecision(6) << params[i];
5426 }
5427 }
5428
5429 if (csvformat) {
5430 oss << ",";
5431 } else {
5432 oss << endl;
5433 }
5434 oss << formatBaselineParamsFooter(rms, nClipped, verbose, csvformat);
5435
5436 return String(oss);
5437 }
5438
5439}
5440
5441std::string Scantable::formatPiecewiseBaselineParams(const std::vector<int>& ranges, const std::vector<float>& params, const std::vector<bool>& fixed, float rms, int nClipped, const std::string& masklist, int whichrow, bool verbose, bool csvformat) const
5442{
5443 int nOutParam = (int)(params.size());
5444 int nPiece = (int)(ranges.size()) - 1;
5445
5446 if (nOutParam < 1) {
5447 return(" Not fitted");
5448 } else if (nPiece < 0) {
5449 return formatBaselineParams(params, fixed, rms, nClipped, masklist, whichrow, verbose, csvformat);
5450 } else if (nPiece < 1) {
5451 return(" Bad count of the piece edge info");
5452 } else if (nOutParam % nPiece != 0) {
5453 return(" Bad count of the output baseline parameters");
5454 } else {
5455
5456 int nParam = nOutParam / nPiece;
5457
5458 ostringstream oss;
5459 oss << formatBaselineParamsHeader(whichrow, masklist, verbose, csvformat);
5460
5461 if (csvformat) {
5462 for (int i = 0; i < nPiece; ++i) {
5463 oss << ranges[i] << "," << (ranges[i+1]-1) << ",";
5464 oss << formatBaselineParams(params, fixed, rms, 0, masklist, whichrow, false, csvformat, i*nParam, nParam, true);
5465 }
5466 } else {
5467 stringstream ss;
5468 ss << ranges[nPiece] << flush;
5469 int wRange = ss.str().size() * 2 + 5;
5470
5471 for (int i = 0; i < nPiece; ++i) {
5472 ss.str("");
5473 ss << " [" << ranges[i] << "," << (ranges[i+1]-1) << "]";
5474 oss << left << setw(wRange) << ss.str();
5475 oss << formatBaselineParams(params, fixed, rms, 0, masklist, whichrow, false, csvformat, i*nParam, nParam, true);
5476 //oss << endl;
5477 }
5478 }
5479
5480 oss << formatBaselineParamsFooter(rms, nClipped, verbose, csvformat);
5481
5482 return String(oss);
5483 }
5484
5485}
5486
5487bool Scantable::hasSameNchanOverIFs()
5488{
5489 int nIF = nif(-1);
5490 int nCh;
5491 int totalPositiveNChan = 0;
5492 int nPositiveNChan = 0;
5493
5494 for (int i = 0; i < nIF; ++i) {
5495 nCh = nchan(i);
5496 if (nCh > 0) {
5497 totalPositiveNChan += nCh;
5498 nPositiveNChan++;
5499 }
5500 }
5501
5502 return (totalPositiveNChan == (nPositiveNChan * nchan(0)));
5503}
5504
5505std::string Scantable::getMaskRangeList(const std::vector<bool>& mask, int whichrow, const casacore::String& coordInfo, bool hasSameNchan, bool verbose)
5506{
5507 if (mask.size() <= 0) {
5508 throw(AipsError("The mask elements should be > 0"));
5509 }
5510 int IF = getIF(whichrow);
5511 if (mask.size() != (uInt)nchan(IF)) {
5512 throw(AipsError("Number of channels in scantable != number of mask elements"));
5513 }
5514
5515 if (verbose) {
5516 LogIO logOs(LogOrigin("Scantable", "getMaskRangeList()", WHERE));
5517 logOs << LogIO::WARN << "The current mask window unit is " << coordInfo;
5518 if (!hasSameNchan) {
5519 logOs << endl << "This mask is only valid for IF=" << IF;
5520 }
5521 logOs << LogIO::POST;
5522 }
5523
5524 std::vector<double> abcissa = getAbcissa(whichrow);
5525 std::vector<int> edge = getMaskEdgeIndices(mask);
5526
5527 ostringstream oss;
5528 oss.setf(ios::fixed);
5529 oss << setprecision(1) << "[";
5530 for (uInt i = 0; i < edge.size(); i+=2) {
5531 if (i > 0) oss << ",";
5532 oss << "[" << (float)abcissa[edge[i]] << "," << (float)abcissa[edge[i+1]] << "]";
5533 }
5534 oss << "]" << flush;
5535
5536 return String(oss);
5537}
5538
5539std::vector<int> Scantable::getMaskEdgeIndices(const std::vector<bool>& mask)
5540{
5541 if (mask.size() <= 0) {
5542 throw(AipsError("The mask elements should be > 0"));
5543 }
5544
5545 std::vector<int> out, startIndices, endIndices;
5546 int maskSize = mask.size();
5547
5548 startIndices.clear();
5549 endIndices.clear();
5550
5551 if (mask[0]) {
5552 startIndices.push_back(0);
5553 }
5554 for (int i = 1; i < maskSize; ++i) {
5555 if ((!mask[i-1]) && mask[i]) {
5556 startIndices.push_back(i);
5557 } else if (mask[i-1] && (!mask[i])) {
5558 endIndices.push_back(i-1);
5559 }
5560 }
5561 if (mask[maskSize-1]) {
5562 endIndices.push_back(maskSize-1);
5563 }
5564
5565 if (startIndices.size() != endIndices.size()) {
5566 throw(AipsError("Inconsistent Mask Size: bad data?"));
5567 }
5568 for (uInt i = 0; i < startIndices.size(); ++i) {
5569 if (startIndices[i] > endIndices[i]) {
5570 throw(AipsError("Mask start index > mask end index"));
5571 }
5572 }
5573
5574 out.clear();
5575 for (uInt i = 0; i < startIndices.size(); ++i) {
5576 out.push_back(startIndices[i]);
5577 out.push_back(endIndices[i]);
5578 }
5579
5580 return out;
5581}
5582
5583void Scantable::setTsys(const std::vector<float>& newvals, int whichrow) {
5584 Vector<Float> tsys(newvals);
5585 if (whichrow > -1) {
5586 if (tsysCol_.shape(whichrow) != tsys.shape())
5587 throw(AipsError("Given Tsys values are not of the same shape"));
5588 tsysCol_.put(whichrow, tsys);
5589 } else {
5590 tsysCol_.fillColumn(tsys);
5591 }
5592}
5593
5594vector<float> Scantable::getTsysSpectrum( int whichrow ) const
5595{
5596 Vector<Float> tsys( tsysCol_(whichrow) ) ;
5597 vector<float> stlTsys ;
5598 tsys.tovector( stlTsys ) ;
5599 return stlTsys ;
5600}
5601
5602vector<uint> Scantable::getMoleculeIdColumnData() const
5603{
5604 Vector<uInt> molIds(mmolidCol_.getColumn());
5605 vector<uint> res;
5606 molIds.tovector(res);
5607 return res;
5608}
5609
5610void Scantable::setMoleculeIdColumnData(const std::vector<uint>& molids)
5611{
5612 Vector<uInt> molIds(molids);
5613 Vector<uInt> arr(mmolidCol_.getColumn());
5614 if ( molIds.nelements() != arr.nelements() )
5615 throw AipsError("The input data size must be the number of rows.");
5616 mmolidCol_.putColumn(molIds);
5617}
5618
5619
5620std::vector<uint> Scantable::getRootTableRowNumbers() const
5621{
5622 Vector<uInt> rowIds(table_.rowNumbers());
5623 vector<uint> res;
5624 rowIds.tovector(res);
5625 return res;
5626}
5627
5628
5629void Scantable::dropXPol()
5630{
5631 if (npol() <= 2) {
5632 return;
5633 }
5634 if (!selector_.empty()) {
5635 throw AipsError("Can only operate with empty selection");
5636 }
5637 std::string taql = "SELECT FROM $1 WHERE POLNO IN [0,1]";
5638 Table tab = tableCommand(taql, table_);
5639 table_ = tab;
5640 table_.rwKeywordSet().define("nPol", Int(2));
5641 originalTable_ = table_;
5642 attach();
5643}
5644
5645}
5646//namespace asap
Note: See TracBrowser for help on using the repository browser.