source: branches/hpc34/src/Scantable.cpp@ 2849

Last change on this file since 2849 was 2640, checked in by WataruKawasaki, 12 years ago

added a new parameter 'csvformat' to sd.scantable.*baseline() and the relevant functions in the C++ side. (2012/08/10 WK)

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