source: trunk/src/Scantable.cpp@ 2677

Last change on this file since 2677 was 2672, checked in by Malte Marquarding, 12 years ago

introduce 'reshape' method; this includes a fix to c++ for the upper boundary assertion

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