source: trunk/src/Scantable.cpp@ 3043

Last change on this file since 3043 was 3043, checked in by Takeshi Nakazato, 10 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: Yes/No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Optimize cubic spline fitting in Scantable::subBaseline.
I expect that the performance will improve about 30%.


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