source: trunk/src/Scantable.cpp@ 2651

Last change on this file since 2651 was 2645, checked in by WataruKawasaki, 12 years ago

New Development: Yes

JIRA Issue: Yes CAS-4145

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: Yes

Module(s): scantable

Description: added scantable methods [auto_]chebyshev_baseline() to subtract baseline using Chebyshev polynomials.


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