source: trunk/src/Scantable.cpp@ 2947

Last change on this file since 2947 was 2947, checked in by Takeshi Nakazato, 12 years ago

New Development: No

JIRA Issue: Yes CAS-6588

Ready for Test: Yes

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...

flag and clip is updated so that channel flagging operation is not
applied if target row is flagged.

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