source: trunk/src/Scantable.cpp@ 2730

Last change on this file since 2730 was 2713, checked in by WataruKawasaki, 12 years ago

New Development: Yes

JIRA Issue: Yes CAS-3618

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: No

Module(s): sd

Description: new method for scantable to calculate AIC, AICc, BIC and GCV.


  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 141.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()) && !unflag)
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
2658double Scantable::calculateModelSelectionCriteria(const std::string& valname, const std::string& blfunc, int order, const std::vector<bool>& inMask, int whichrow, bool useLineFinder, const std::vector<int>& edge, float threshold, int chanAvgLimit)
2659{
2660 if (useLineFinder) {
2661 int minEdgeSize = getIFNos().size()*2;
2662
2663
2664 int edgeSize = edge.size();
2665 std::vector<int> currentEdge;
2666 if (edgeSize >= 2) {
2667 int idx = 0;
2668 if (edgeSize > 2) {
2669 if (edgeSize < minEdgeSize) {
2670 throw(AipsError("Length of edge element info is less than that of IFs"));
2671 }
2672 idx = 2 * getIF(whichrow);
2673 }
2674 currentEdge.push_back(edge[idx]);
2675 currentEdge.push_back(edge[idx+1]);
2676 } else {
2677 throw(AipsError("Wrong length of edge element"));
2678 }
2679
2680 STLineFinder lineFinder = STLineFinder();
2681 std::vector<float> sp = getSpectrum(whichrow);
2682 lineFinder.setData(sp);
2683 lineFinder.setOptions(threshold, 3, chanAvgLimit);
2684 lineFinder.findLines(getCompositeChanMask(whichrow, inMask), currentEdge, whichrow);
2685 std::vector<bool> chanMask = lineFinder.getMask();
2686
2687 return doCalculateModelSelectionCriteria(valname, sp, chanMask, blfunc, order);
2688
2689 } else {
2690 return doCalculateModelSelectionCriteria(valname, getSpectrum(whichrow), getCompositeChanMask(whichrow, inMask), blfunc, order);
2691 }
2692
2693}
2694
2695double Scantable::doCalculateModelSelectionCriteria(const std::string& valname, const std::vector<float>& spec, const std::vector<bool>& mask, const std::string& blfunc, int order)
2696{
2697 int nparam;
2698 std::vector<float> params;
2699 int nClipped = 0;
2700 float thresClip = 0.0;
2701 int nIterClip = 0;
2702 std::vector<float> res;
2703 if (blfunc == "chebyshev") {
2704 nparam = order + 1;
2705 res = doChebyshevFitting(spec, mask, order, params, nClipped, thresClip, nIterClip, true);
2706 } else if (blfunc == "sinusoid") {
2707 std::vector<int> nWaves;
2708 nWaves.clear();
2709 for (int i = 0; i <= order; ++i) {
2710 nWaves.push_back(i);
2711 }
2712 nparam = 2*order + 1; // order = nwave
2713 res = doSinusoidFitting(spec, mask, nWaves, params, nClipped, thresClip, nIterClip, true);
2714 } else if (blfunc == "cspline") {
2715 std::vector<int> pieceEdges(order+1); //order = npiece
2716 nparam = order + 3;
2717 params.resize(4*order);
2718 res = doCubicSplineFitting(spec, mask, order, pieceEdges, params, nClipped, thresClip, nIterClip, true);
2719 } else {
2720 throw(AipsError("blfunc must be chebyshev, cspline or sinusoid."));
2721 }
2722
2723 double msq = 0.0;
2724 int nusedchan = 0;
2725 int nChan = res.size();
2726 for (int i = 0; i < nChan; ++i) {
2727 if (mask[i]) {
2728 msq += (double)res[i]*(double)res[i];
2729 nusedchan++;
2730 }
2731 }
2732 if (nusedchan == 0) {
2733 throw(AipsError("all channels masked."));
2734 }
2735 msq /= (double)nusedchan;
2736
2737 nparam++; //add 1 for sigma of Gaussian distribution
2738 const double PI = 6.0 * asin(0.5); // PI (= 3.141592653...)
2739
2740 if (valname.find("aic") == 0) {
2741 // Original Akaike Information Criterion (AIC)
2742 double aic = nusedchan * (log(2.0 * PI * msq) + 1.0) + 2.0 * nparam;
2743
2744 // Corrected AIC by Sugiura(1978)
2745 if (valname == "aicc") {
2746 if (nusedchan - nparam - 1 <= 0) {
2747 throw(AipsError("channel size is too small to calculate AICc."));
2748 }
2749 aic += 2.0*nparam*(nparam + 1)/(double)(nusedchan - nparam - 1);
2750 }
2751
2752 return aic;
2753
2754 } else if (valname == "bic") {
2755 // Bayesian Information Criterion (BIC)
2756 double bic = nusedchan * log(msq) + nparam * log((double)nusedchan);
2757 return bic;
2758
2759 } else if (valname == "gcv") {
2760 // Generalised Cross Validation
2761 double x = 1.0 - (double)nparam / (double)nusedchan;
2762 double gcv = msq / (x * x);
2763 return gcv;
2764
2765 } else {
2766 throw(AipsError("valname must be aic, aicc, bic or gcv."));
2767 }
2768}
2769
2770void 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)
2771{
2772 try {
2773 ofstream ofs;
2774 String coordInfo = "";
2775 bool hasSameNchan = true;
2776 bool outTextFile = false;
2777 bool csvFormat = false;
2778
2779 if (blfile != "") {
2780 csvFormat = (blfile.substr(0, 1) == "T");
2781 ofs.open(blfile.substr(1).c_str(), ios::out | ios::app);
2782 if (ofs) outTextFile = true;
2783 }
2784
2785 if (outLogger || outTextFile) {
2786 coordInfo = getCoordInfo()[0];
2787 if (coordInfo == "") coordInfo = "channel";
2788 hasSameNchan = hasSameNchanOverIFs();
2789 }
2790
2791 int nRow = nrow();
2792 std::vector<bool> chanMask;
2793 int minEdgeSize = getIFNos().size()*2;
2794 STLineFinder lineFinder = STLineFinder();
2795 lineFinder.setOptions(threshold, 3, chanAvgLimit);
2796
2797 bool showProgress;
2798 int minNRow;
2799 parseProgressInfo(progressInfo, showProgress, minNRow);
2800
2801 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
2802 std::vector<float> sp = getSpectrum(whichrow);
2803
2804 //-------------------------------------------------------
2805 //chanMask = getCompositeChanMask(whichrow, mask, edge, minEdgeSize, lineFinder);
2806 //-------------------------------------------------------
2807 int edgeSize = edge.size();
2808 std::vector<int> currentEdge;
2809 if (edgeSize >= 2) {
2810 int idx = 0;
2811 if (edgeSize > 2) {
2812 if (edgeSize < minEdgeSize) {
2813 throw(AipsError("Length of edge element info is less than that of IFs"));
2814 }
2815 idx = 2 * getIF(whichrow);
2816 }
2817 currentEdge.push_back(edge[idx]);
2818 currentEdge.push_back(edge[idx+1]);
2819 } else {
2820 throw(AipsError("Wrong length of edge element"));
2821 }
2822 //lineFinder.setData(getSpectrum(whichrow));
2823 lineFinder.setData(sp);
2824 lineFinder.findLines(getCompositeChanMask(whichrow, mask), currentEdge, whichrow);
2825 chanMask = lineFinder.getMask();
2826 //-------------------------------------------------------
2827
2828
2829 //fitBaseline(chanMask, whichrow, fitter);
2830 //setSpectrum((getResidual ? fitter.getResidual() : fitter.getFit()), whichrow);
2831 std::vector<float> params(order+1);
2832 int nClipped = 0;
2833 std::vector<float> res = doChebyshevFitting(sp, chanMask, order, params, nClipped, thresClip, nIterClip, getResidual);
2834 setSpectrum(res, whichrow);
2835
2836 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow, coordInfo, hasSameNchan, ofs, "autoChebyshevBaseline()", params, nClipped);
2837 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
2838 }
2839
2840 if (outTextFile) ofs.close();
2841
2842 } catch (...) {
2843 throw;
2844 }
2845}
2846
2847 /*
2848double Scantable::getChebyshevPolynomial(int n, double x) {
2849 if ((x < -1.0)||(x > 1.0)) {
2850 throw(AipsError("out of definition range (-1 <= x <= 1)."));
2851 } else if (n < 0) {
2852 throw(AipsError("the order must be zero or positive."));
2853 } else if (n == 0) {
2854 return 1.0;
2855 } else if (n == 1) {
2856 return x;
2857 } else {
2858 return 2.0*x*getChebyshevPolynomial(n-1, x) - getChebyshevPolynomial(n-2, x);
2859 }
2860}
2861 */
2862double Scantable::getChebyshevPolynomial(int n, double x) {
2863 if ((x < -1.0)||(x > 1.0)) {
2864 throw(AipsError("out of definition range (-1 <= x <= 1)."));
2865 } else if (x == 1.0) {
2866 return 1.0;
2867 } else if (x == 0.0) {
2868 double res;
2869 if (n%2 == 0) {
2870 if (n%4 == 0) {
2871 res = 1.0;
2872 } else {
2873 res = -1.0;
2874 }
2875 } else {
2876 res = 0.0;
2877 }
2878 return res;
2879 } else if (x == -1.0) {
2880 double res = (n%2 == 0 ? 1.0 : -1.0);
2881 return res;
2882 } else if (n < 0) {
2883 throw(AipsError("the order must be zero or positive."));
2884 } else if (n == 0) {
2885 return 1.0;
2886 } else if (n == 1) {
2887 return x;
2888 } else {
2889 double res = 0.0;
2890 for (int m = 0; m <= n/2; ++m) {
2891 double c = 1.0;
2892 if (m > 0) {
2893 for (int i = 1; i <= m; ++i) {
2894 c *= (double)(n-2*m+i)/(double)i;
2895 }
2896 }
2897 res += (m%2 == 0 ? 1.0 : -1.0)*(double)n/(double)(n-m)*pow(2.0*x, (double)(n-2*m))/2.0*c;
2898 }
2899 return res;
2900 }
2901}
2902
2903std::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)
2904{
2905 if (data.size() != mask.size()) {
2906 throw(AipsError("data and mask sizes are not identical"));
2907 }
2908 if (order < 0) {
2909 throw(AipsError("maximum order of Chebyshev polynomial must not be negative."));
2910 }
2911
2912 int nChan = data.size();
2913 std::vector<int> maskArray;
2914 std::vector<int> x;
2915 for (int i = 0; i < nChan; ++i) {
2916 maskArray.push_back(mask[i] ? 1 : 0);
2917 if (mask[i]) {
2918 x.push_back(i);
2919 }
2920 }
2921
2922 int initNData = x.size();
2923
2924 int nData = initNData;
2925 int nDOF = order + 1; //number of parameters to solve.
2926
2927 // xArray : contains elemental values for computing the least-square matrix.
2928 // xArray.size() is nDOF and xArray[*].size() is nChan.
2929 // Each xArray element are as follows:
2930 // xArray[0] = {T0(-1), T0(2/(nChan-1)-1), T0(4/(nChan-1)-1), ..., T0(1)},
2931 // xArray[n-1] = ...,
2932 // xArray[n] = {Tn(-1), Tn(2/(nChan-1)-1), Tn(4/(nChan-1)-1), ..., Tn(1)}
2933 // where (0 <= n <= order),
2934 std::vector<std::vector<double> > xArray;
2935 for (int i = 0; i < nDOF; ++i) {
2936 double xFactor = 2.0/(double)(nChan - 1);
2937 std::vector<double> xs;
2938 xs.clear();
2939 for (int j = 0; j < nChan; ++j) {
2940 xs.push_back(getChebyshevPolynomial(i, xFactor*(double)j-1.0));
2941 }
2942 xArray.push_back(xs);
2943 }
2944
2945 std::vector<double> z1, r1, residual;
2946 for (int i = 0; i < nChan; ++i) {
2947 z1.push_back((double)data[i]);
2948 r1.push_back(0.0);
2949 residual.push_back(0.0);
2950 }
2951
2952 for (int nClip = 0; nClip < nIterClip+1; ++nClip) {
2953 // xMatrix : horizontal concatenation of
2954 // the least-sq. matrix (left) and an
2955 // identity matrix (right).
2956 // the right part is used to calculate the inverse matrix of the left part.
2957 double xMatrix[nDOF][2*nDOF];
2958 double zMatrix[nDOF];
2959 for (int i = 0; i < nDOF; ++i) {
2960 for (int j = 0; j < 2*nDOF; ++j) {
2961 xMatrix[i][j] = 0.0;
2962 }
2963 xMatrix[i][nDOF+i] = 1.0;
2964 zMatrix[i] = 0.0;
2965 }
2966
2967 int nUseData = 0;
2968 for (int k = 0; k < nChan; ++k) {
2969 if (maskArray[k] == 0) continue;
2970
2971 for (int i = 0; i < nDOF; ++i) {
2972 for (int j = i; j < nDOF; ++j) {
2973 xMatrix[i][j] += xArray[i][k] * xArray[j][k];
2974 }
2975 zMatrix[i] += z1[k] * xArray[i][k];
2976 }
2977
2978 nUseData++;
2979 }
2980
2981 if (nUseData < 1) {
2982 throw(AipsError("all channels clipped or masked. can't execute fitting anymore."));
2983 }
2984
2985 for (int i = 0; i < nDOF; ++i) {
2986 for (int j = 0; j < i; ++j) {
2987 xMatrix[i][j] = xMatrix[j][i];
2988 }
2989 }
2990
2991 std::vector<double> invDiag;
2992 for (int i = 0; i < nDOF; ++i) {
2993 invDiag.push_back(1.0/xMatrix[i][i]);
2994 for (int j = 0; j < nDOF; ++j) {
2995 xMatrix[i][j] *= invDiag[i];
2996 }
2997 }
2998
2999 for (int k = 0; k < nDOF; ++k) {
3000 for (int i = 0; i < nDOF; ++i) {
3001 if (i != k) {
3002 double factor1 = xMatrix[k][k];
3003 double factor2 = xMatrix[i][k];
3004 for (int j = k; j < 2*nDOF; ++j) {
3005 xMatrix[i][j] *= factor1;
3006 xMatrix[i][j] -= xMatrix[k][j]*factor2;
3007 xMatrix[i][j] /= factor1;
3008 }
3009 }
3010 }
3011 double xDiag = xMatrix[k][k];
3012 for (int j = k; j < 2*nDOF; ++j) {
3013 xMatrix[k][j] /= xDiag;
3014 }
3015 }
3016
3017 for (int i = 0; i < nDOF; ++i) {
3018 for (int j = 0; j < nDOF; ++j) {
3019 xMatrix[i][nDOF+j] *= invDiag[j];
3020 }
3021 }
3022 //compute a vector y which consists of the coefficients of the sinusoids forming the
3023 //best-fit curves (a0,s1,c1,s2,c2,...), where a0 is constant and s* and c* are of sine
3024 //and cosine functions, respectively.
3025 std::vector<double> y;
3026 params.clear();
3027 for (int i = 0; i < nDOF; ++i) {
3028 y.push_back(0.0);
3029 for (int j = 0; j < nDOF; ++j) {
3030 y[i] += xMatrix[i][nDOF+j]*zMatrix[j];
3031 }
3032 params.push_back(y[i]);
3033 }
3034
3035 for (int i = 0; i < nChan; ++i) {
3036 r1[i] = y[0];
3037 for (int j = 1; j < nDOF; ++j) {
3038 r1[i] += y[j]*xArray[j][i];
3039 }
3040 residual[i] = z1[i] - r1[i];
3041 }
3042
3043 if ((nClip == nIterClip) || (thresClip <= 0.0)) {
3044 break;
3045 } else {
3046 double stdDev = 0.0;
3047 for (int i = 0; i < nChan; ++i) {
3048 stdDev += residual[i]*residual[i]*(double)maskArray[i];
3049 }
3050 stdDev = sqrt(stdDev/(double)nData);
3051
3052 double thres = stdDev * thresClip;
3053 int newNData = 0;
3054 for (int i = 0; i < nChan; ++i) {
3055 if (abs(residual[i]) >= thres) {
3056 maskArray[i] = 0;
3057 }
3058 if (maskArray[i] > 0) {
3059 newNData++;
3060 }
3061 }
3062 if (newNData == nData) {
3063 break; //no more flag to add. iteration stops.
3064 } else {
3065 nData = newNData;
3066 }
3067 }
3068 }
3069
3070 nClipped = initNData - nData;
3071
3072 std::vector<float> result;
3073 if (getResidual) {
3074 for (int i = 0; i < nChan; ++i) {
3075 result.push_back((float)residual[i]);
3076 }
3077 } else {
3078 for (int i = 0; i < nChan; ++i) {
3079 result.push_back((float)r1[i]);
3080 }
3081 }
3082
3083 return result;
3084}
3085
3086void 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)
3087{
3088 /*************
3089 double totTimeStart, totTimeEnd, blTimeStart, blTimeEnd, ioTimeStart, ioTimeEnd, msTimeStart, msTimeEnd, seTimeStart, seTimeEnd, otTimeStart, otTimeEnd, prTimeStart, prTimeEnd;
3090 double elapseMs = 0.0;
3091 double elapseSe = 0.0;
3092 double elapseOt = 0.0;
3093 double elapsePr = 0.0;
3094 double elapseBl = 0.0;
3095 double elapseIo = 0.0;
3096 totTimeStart = mathutil::gettimeofday_sec();
3097 *************/
3098
3099 try {
3100 ofstream ofs;
3101 String coordInfo = "";
3102 bool hasSameNchan = true;
3103 bool outTextFile = false;
3104 bool csvFormat = false;
3105
3106 if (blfile != "") {
3107 csvFormat = (blfile.substr(0, 1) == "T");
3108 ofs.open(blfile.substr(1).c_str(), ios::out | ios::app);
3109 if (ofs) outTextFile = true;
3110 }
3111
3112 if (outLogger || outTextFile) {
3113 coordInfo = getCoordInfo()[0];
3114 if (coordInfo == "") coordInfo = "channel";
3115 hasSameNchan = hasSameNchanOverIFs();
3116 }
3117
3118 //Fitter fitter = Fitter();
3119 //fitter.setExpression("cspline", nPiece);
3120 //fitter.setIterClipping(thresClip, nIterClip);
3121
3122 bool showProgress;
3123 int minNRow;
3124 parseProgressInfo(progressInfo, showProgress, minNRow);
3125
3126 int nRow = nrow();
3127 std::vector<bool> chanMask;
3128
3129 //--------------------------------
3130 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
3131 /******************
3132 ioTimeStart = mathutil::gettimeofday_sec();
3133 **/
3134 std::vector<float> sp = getSpectrum(whichrow);
3135 /**
3136 ioTimeEnd = mathutil::gettimeofday_sec();
3137 elapseIo += (double)(ioTimeEnd - ioTimeStart);
3138 msTimeStart = mathutil::gettimeofday_sec();
3139 ******************/
3140
3141 chanMask = getCompositeChanMask(whichrow, mask);
3142
3143 /**
3144 msTimeEnd = mathutil::gettimeofday_sec();
3145 elapseMs += (double)(msTimeEnd - msTimeStart);
3146 blTimeStart = mathutil::gettimeofday_sec();
3147 **/
3148
3149 //fitBaseline(chanMask, whichrow, fitter);
3150 //setSpectrum((getResidual ? fitter.getResidual() : fitter.getFit()), whichrow);
3151 std::vector<int> pieceEdges(nPiece+1);
3152 std::vector<float> params(nPiece*4);
3153 int nClipped = 0;
3154 std::vector<float> res = doCubicSplineFitting(sp, chanMask, nPiece, pieceEdges, params, nClipped, thresClip, nIterClip, getResidual);
3155
3156 /**
3157 blTimeEnd = mathutil::gettimeofday_sec();
3158 elapseBl += (double)(blTimeEnd - blTimeStart);
3159 seTimeStart = mathutil::gettimeofday_sec();
3160 **/
3161
3162
3163 setSpectrum(res, whichrow);
3164 //
3165
3166 /**
3167 seTimeEnd = mathutil::gettimeofday_sec();
3168 elapseSe += (double)(seTimeEnd - seTimeStart);
3169 otTimeStart = mathutil::gettimeofday_sec();
3170 **/
3171
3172 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow, coordInfo, hasSameNchan, ofs, "cubicSplineBaseline()", pieceEdges, params, nClipped);
3173
3174 /**
3175 otTimeEnd = mathutil::gettimeofday_sec();
3176 elapseOt += (double)(otTimeEnd - otTimeStart);
3177 prTimeStart = mathutil::gettimeofday_sec();
3178 **/
3179
3180 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
3181
3182 /******************
3183 prTimeEnd = mathutil::gettimeofday_sec();
3184 elapsePr += (double)(prTimeEnd - prTimeStart);
3185 ******************/
3186 }
3187 //--------------------------------
3188
3189 if (outTextFile) ofs.close();
3190
3191 } catch (...) {
3192 throw;
3193 }
3194 /***************
3195 totTimeEnd = mathutil::gettimeofday_sec();
3196 std::cout << "io : " << elapseIo << " (sec.)" << endl;
3197 std::cout << "ms : " << elapseMs << " (sec.)" << endl;
3198 std::cout << "bl : " << elapseBl << " (sec.)" << endl;
3199 std::cout << "se : " << elapseSe << " (sec.)" << endl;
3200 std::cout << "ot : " << elapseOt << " (sec.)" << endl;
3201 std::cout << "pr : " << elapsePr << " (sec.)" << endl;
3202 std::cout << "total : " << (double)(totTimeEnd - totTimeStart) << " (sec.)" << endl;
3203 ***************/
3204}
3205
3206void 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)
3207{
3208 try {
3209 ofstream ofs;
3210 String coordInfo = "";
3211 bool hasSameNchan = true;
3212 bool outTextFile = false;
3213 bool csvFormat = false;
3214
3215 if (blfile != "") {
3216 csvFormat = (blfile.substr(0, 1) == "T");
3217 ofs.open(blfile.substr(1).c_str(), ios::out | ios::app);
3218 if (ofs) outTextFile = true;
3219 }
3220
3221 if (outLogger || outTextFile) {
3222 coordInfo = getCoordInfo()[0];
3223 if (coordInfo == "") coordInfo = "channel";
3224 hasSameNchan = hasSameNchanOverIFs();
3225 }
3226
3227 //Fitter fitter = Fitter();
3228 //fitter.setExpression("cspline", nPiece);
3229 //fitter.setIterClipping(thresClip, nIterClip);
3230
3231 int nRow = nrow();
3232 std::vector<bool> chanMask;
3233 int minEdgeSize = getIFNos().size()*2;
3234 STLineFinder lineFinder = STLineFinder();
3235 lineFinder.setOptions(threshold, 3, chanAvgLimit);
3236
3237 bool showProgress;
3238 int minNRow;
3239 parseProgressInfo(progressInfo, showProgress, minNRow);
3240
3241 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
3242 std::vector<float> sp = getSpectrum(whichrow);
3243
3244 //-------------------------------------------------------
3245 //chanMask = getCompositeChanMask(whichrow, mask, edge, minEdgeSize, lineFinder);
3246 //-------------------------------------------------------
3247 int edgeSize = edge.size();
3248 std::vector<int> currentEdge;
3249 if (edgeSize >= 2) {
3250 int idx = 0;
3251 if (edgeSize > 2) {
3252 if (edgeSize < minEdgeSize) {
3253 throw(AipsError("Length of edge element info is less than that of IFs"));
3254 }
3255 idx = 2 * getIF(whichrow);
3256 }
3257 currentEdge.push_back(edge[idx]);
3258 currentEdge.push_back(edge[idx+1]);
3259 } else {
3260 throw(AipsError("Wrong length of edge element"));
3261 }
3262 //lineFinder.setData(getSpectrum(whichrow));
3263 lineFinder.setData(sp);
3264 lineFinder.findLines(getCompositeChanMask(whichrow, mask), currentEdge, whichrow);
3265 chanMask = lineFinder.getMask();
3266 //-------------------------------------------------------
3267
3268
3269 //fitBaseline(chanMask, whichrow, fitter);
3270 //setSpectrum((getResidual ? fitter.getResidual() : fitter.getFit()), whichrow);
3271 std::vector<int> pieceEdges(nPiece+1);
3272 std::vector<float> params(nPiece*4);
3273 int nClipped = 0;
3274 std::vector<float> res = doCubicSplineFitting(sp, chanMask, nPiece, pieceEdges, params, nClipped, thresClip, nIterClip, getResidual);
3275 setSpectrum(res, whichrow);
3276 //
3277
3278 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow, coordInfo, hasSameNchan, ofs, "autoCubicSplineBaseline()", pieceEdges, params, nClipped);
3279 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
3280 }
3281
3282 if (outTextFile) ofs.close();
3283
3284 } catch (...) {
3285 throw;
3286 }
3287}
3288
3289std::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)
3290{
3291 if (data.size() != mask.size()) {
3292 throw(AipsError("data and mask sizes are not identical"));
3293 }
3294 if (nPiece < 1) {
3295 throw(AipsError("number of the sections must be one or more"));
3296 }
3297
3298 int nChan = data.size();
3299 std::vector<int> maskArray(nChan);
3300 std::vector<int> x(nChan);
3301 int j = 0;
3302 for (int i = 0; i < nChan; ++i) {
3303 maskArray[i] = mask[i] ? 1 : 0;
3304 if (mask[i]) {
3305 x[j] = i;
3306 j++;
3307 }
3308 }
3309 int initNData = j;
3310
3311 if (initNData < nPiece) {
3312 throw(AipsError("too few non-flagged channels"));
3313 }
3314
3315 int nElement = (int)(floor(floor((double)(initNData/nPiece))+0.5));
3316 std::vector<double> invEdge(nPiece-1);
3317 idxEdge[0] = x[0];
3318 for (int i = 1; i < nPiece; ++i) {
3319 int valX = x[nElement*i];
3320 idxEdge[i] = valX;
3321 invEdge[i-1] = 1.0/(double)valX;
3322 }
3323 idxEdge[nPiece] = x[initNData-1]+1;
3324
3325 int nData = initNData;
3326 int nDOF = nPiece + 3; //number of parameters to solve, namely, 4+(nPiece-1).
3327
3328 std::vector<double> x1(nChan), x2(nChan), x3(nChan);
3329 std::vector<double> z1(nChan), x1z1(nChan), x2z1(nChan), x3z1(nChan);
3330 std::vector<double> r1(nChan), residual(nChan);
3331 for (int i = 0; i < nChan; ++i) {
3332 double di = (double)i;
3333 double dD = (double)data[i];
3334 x1[i] = di;
3335 x2[i] = di*di;
3336 x3[i] = di*di*di;
3337 z1[i] = dD;
3338 x1z1[i] = dD*di;
3339 x2z1[i] = dD*di*di;
3340 x3z1[i] = dD*di*di*di;
3341 r1[i] = 0.0;
3342 residual[i] = 0.0;
3343 }
3344
3345 for (int nClip = 0; nClip < nIterClip+1; ++nClip) {
3346 // xMatrix : horizontal concatenation of
3347 // the least-sq. matrix (left) and an
3348 // identity matrix (right).
3349 // the right part is used to calculate the inverse matrix of the left part.
3350 double xMatrix[nDOF][2*nDOF];
3351 double zMatrix[nDOF];
3352 for (int i = 0; i < nDOF; ++i) {
3353 for (int j = 0; j < 2*nDOF; ++j) {
3354 xMatrix[i][j] = 0.0;
3355 }
3356 xMatrix[i][nDOF+i] = 1.0;
3357 zMatrix[i] = 0.0;
3358 }
3359
3360 for (int n = 0; n < nPiece; ++n) {
3361 int nUseDataInPiece = 0;
3362 for (int i = idxEdge[n]; i < idxEdge[n+1]; ++i) {
3363
3364 if (maskArray[i] == 0) continue;
3365
3366 xMatrix[0][0] += 1.0;
3367 xMatrix[0][1] += x1[i];
3368 xMatrix[0][2] += x2[i];
3369 xMatrix[0][3] += x3[i];
3370 xMatrix[1][1] += x2[i];
3371 xMatrix[1][2] += x3[i];
3372 xMatrix[1][3] += x2[i]*x2[i];
3373 xMatrix[2][2] += x2[i]*x2[i];
3374 xMatrix[2][3] += x3[i]*x2[i];
3375 xMatrix[3][3] += x3[i]*x3[i];
3376 zMatrix[0] += z1[i];
3377 zMatrix[1] += x1z1[i];
3378 zMatrix[2] += x2z1[i];
3379 zMatrix[3] += x3z1[i];
3380
3381 for (int j = 0; j < n; ++j) {
3382 double q = 1.0 - x1[i]*invEdge[j];
3383 q = q*q*q;
3384 xMatrix[0][j+4] += q;
3385 xMatrix[1][j+4] += q*x1[i];
3386 xMatrix[2][j+4] += q*x2[i];
3387 xMatrix[3][j+4] += q*x3[i];
3388 for (int k = 0; k < j; ++k) {
3389 double r = 1.0 - x1[i]*invEdge[k];
3390 r = r*r*r;
3391 xMatrix[k+4][j+4] += r*q;
3392 }
3393 xMatrix[j+4][j+4] += q*q;
3394 zMatrix[j+4] += q*z1[i];
3395 }
3396
3397 nUseDataInPiece++;
3398 }
3399
3400 if (nUseDataInPiece < 1) {
3401 std::vector<string> suffixOfPieceNumber(4);
3402 suffixOfPieceNumber[0] = "th";
3403 suffixOfPieceNumber[1] = "st";
3404 suffixOfPieceNumber[2] = "nd";
3405 suffixOfPieceNumber[3] = "rd";
3406 int idxNoDataPiece = (n % 10 <= 3) ? n : 0;
3407 ostringstream oss;
3408 oss << "all channels clipped or masked in " << n << suffixOfPieceNumber[idxNoDataPiece];
3409 oss << " piece of the spectrum. can't execute fitting anymore.";
3410 throw(AipsError(String(oss)));
3411 }
3412 }
3413
3414 for (int i = 0; i < nDOF; ++i) {
3415 for (int j = 0; j < i; ++j) {
3416 xMatrix[i][j] = xMatrix[j][i];
3417 }
3418 }
3419
3420 std::vector<double> invDiag(nDOF);
3421 for (int i = 0; i < nDOF; ++i) {
3422 invDiag[i] = 1.0/xMatrix[i][i];
3423 for (int j = 0; j < nDOF; ++j) {
3424 xMatrix[i][j] *= invDiag[i];
3425 }
3426 }
3427
3428 for (int k = 0; k < nDOF; ++k) {
3429 for (int i = 0; i < nDOF; ++i) {
3430 if (i != k) {
3431 double factor1 = xMatrix[k][k];
3432 double factor2 = xMatrix[i][k];
3433 for (int j = k; j < 2*nDOF; ++j) {
3434 xMatrix[i][j] *= factor1;
3435 xMatrix[i][j] -= xMatrix[k][j]*factor2;
3436 xMatrix[i][j] /= factor1;
3437 }
3438 }
3439 }
3440 double xDiag = xMatrix[k][k];
3441 for (int j = k; j < 2*nDOF; ++j) {
3442 xMatrix[k][j] /= xDiag;
3443 }
3444 }
3445
3446 for (int i = 0; i < nDOF; ++i) {
3447 for (int j = 0; j < nDOF; ++j) {
3448 xMatrix[i][nDOF+j] *= invDiag[j];
3449 }
3450 }
3451 //compute a vector y which consists of the coefficients of the best-fit spline curves
3452 //(a0,a1,a2,a3(,b3,c3,...)), namely, the ones for the leftmost piece and the ones of
3453 //cubic terms for the other pieces (in case nPiece>1).
3454 std::vector<double> y(nDOF);
3455 for (int i = 0; i < nDOF; ++i) {
3456 y[i] = 0.0;
3457 for (int j = 0; j < nDOF; ++j) {
3458 y[i] += xMatrix[i][nDOF+j]*zMatrix[j];
3459 }
3460 }
3461
3462 double a0 = y[0];
3463 double a1 = y[1];
3464 double a2 = y[2];
3465 double a3 = y[3];
3466
3467 int j = 0;
3468 for (int n = 0; n < nPiece; ++n) {
3469 for (int i = idxEdge[n]; i < idxEdge[n+1]; ++i) {
3470 r1[i] = a0 + a1*x1[i] + a2*x2[i] + a3*x3[i];
3471 }
3472 params[j] = a0;
3473 params[j+1] = a1;
3474 params[j+2] = a2;
3475 params[j+3] = a3;
3476 j += 4;
3477
3478 if (n == nPiece-1) break;
3479
3480 double d = y[4+n];
3481 double iE = invEdge[n];
3482 a0 += d;
3483 a1 -= 3.0*d*iE;
3484 a2 += 3.0*d*iE*iE;
3485 a3 -= d*iE*iE*iE;
3486 }
3487
3488 //subtract constant value for masked regions at the edge of spectrum
3489 if (idxEdge[0] > 0) {
3490 int n = idxEdge[0];
3491 for (int i = 0; i < idxEdge[0]; ++i) {
3492 //--cubic extrapolate--
3493 //r1[i] = params[0] + params[1]*x1[i] + params[2]*x2[i] + params[3]*x3[i];
3494 //--linear extrapolate--
3495 //r1[i] = (r1[n+1] - r1[n])/(x1[n+1] - x1[n])*(x1[i] - x1[n]) + r1[n];
3496 //--constant--
3497 r1[i] = r1[n];
3498 }
3499 }
3500 if (idxEdge[nPiece] < nChan) {
3501 int n = idxEdge[nPiece]-1;
3502 for (int i = idxEdge[nPiece]; i < nChan; ++i) {
3503 //--cubic extrapolate--
3504 //int m = 4*(nPiece-1);
3505 //r1[i] = params[m] + params[m+1]*x1[i] + params[m+2]*x2[i] + params[m+3]*x3[i];
3506 //--linear extrapolate--
3507 //r1[i] = (r1[n-1] - r1[n])/(x1[n-1] - x1[n])*(x1[i] - x1[n]) + r1[n];
3508 //--constant--
3509 r1[i] = r1[n];
3510 }
3511 }
3512
3513 for (int i = 0; i < nChan; ++i) {
3514 residual[i] = z1[i] - r1[i];
3515 }
3516
3517 if ((nClip == nIterClip) || (thresClip <= 0.0)) {
3518 break;
3519 } else {
3520 double stdDev = 0.0;
3521 for (int i = 0; i < nChan; ++i) {
3522 stdDev += residual[i]*residual[i]*(double)maskArray[i];
3523 }
3524 stdDev = sqrt(stdDev/(double)nData);
3525
3526 double thres = stdDev * thresClip;
3527 int newNData = 0;
3528 for (int i = 0; i < nChan; ++i) {
3529 if (abs(residual[i]) >= thres) {
3530 maskArray[i] = 0;
3531 }
3532 if (maskArray[i] > 0) {
3533 newNData++;
3534 }
3535 }
3536 if (newNData == nData) {
3537 break; //no more flag to add. iteration stops.
3538 } else {
3539 nData = newNData;
3540 }
3541 }
3542 }
3543
3544 nClipped = initNData - nData;
3545
3546 std::vector<float> result(nChan);
3547 if (getResidual) {
3548 for (int i = 0; i < nChan; ++i) {
3549 result[i] = (float)residual[i];
3550 }
3551 } else {
3552 for (int i = 0; i < nChan; ++i) {
3553 result[i] = (float)r1[i];
3554 }
3555 }
3556
3557 return result;
3558}
3559
3560void 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)
3561{
3562 nWaves.clear();
3563
3564 if (applyFFT) {
3565 string fftThAttr;
3566 float fftThSigma;
3567 int fftThTop;
3568 parseThresholdExpression(fftThresh, fftThAttr, fftThSigma, fftThTop);
3569 doSelectWaveNumbers(whichrow, chanMask, fftMethod, fftThSigma, fftThTop, fftThAttr, nWaves);
3570 }
3571
3572 addAuxWaveNumbers(whichrow, addNWaves, rejectNWaves, nWaves);
3573}
3574
3575void Scantable::parseThresholdExpression(const std::string& fftThresh, std::string& fftThAttr, float& fftThSigma, int& fftThTop)
3576{
3577 uInt idxSigma = fftThresh.find("sigma");
3578 uInt idxTop = fftThresh.find("top");
3579
3580 if (idxSigma == fftThresh.size() - 5) {
3581 std::istringstream is(fftThresh.substr(0, fftThresh.size() - 5));
3582 is >> fftThSigma;
3583 fftThAttr = "sigma";
3584 } else if (idxTop == 0) {
3585 std::istringstream is(fftThresh.substr(3));
3586 is >> fftThTop;
3587 fftThAttr = "top";
3588 } else {
3589 bool isNumber = true;
3590 for (uInt i = 0; i < fftThresh.size()-1; ++i) {
3591 char ch = (fftThresh.substr(i, 1).c_str())[0];
3592 if (!(isdigit(ch) || (fftThresh.substr(i, 1) == "."))) {
3593 isNumber = false;
3594 break;
3595 }
3596 }
3597 if (isNumber) {
3598 std::istringstream is(fftThresh);
3599 is >> fftThSigma;
3600 fftThAttr = "sigma";
3601 } else {
3602 throw(AipsError("fftthresh has a wrong value"));
3603 }
3604 }
3605}
3606
3607void 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)
3608{
3609 std::vector<float> fspec;
3610 if (fftMethod == "fft") {
3611 fspec = execFFT(whichrow, chanMask, false, true);
3612 //} else if (fftMethod == "lsp") {
3613 // fspec = lombScarglePeriodogram(whichrow);
3614 }
3615
3616 if (fftThAttr == "sigma") {
3617 float mean = 0.0;
3618 float mean2 = 0.0;
3619 for (uInt i = 0; i < fspec.size(); ++i) {
3620 mean += fspec[i];
3621 mean2 += fspec[i]*fspec[i];
3622 }
3623 mean /= float(fspec.size());
3624 mean2 /= float(fspec.size());
3625 float thres = mean + fftThSigma * float(sqrt(mean2 - mean*mean));
3626
3627 for (uInt i = 0; i < fspec.size(); ++i) {
3628 if (fspec[i] >= thres) {
3629 nWaves.push_back(i);
3630 }
3631 }
3632
3633 } else if (fftThAttr == "top") {
3634 for (int i = 0; i < fftThTop; ++i) {
3635 float max = 0.0;
3636 int maxIdx = 0;
3637 for (uInt j = 0; j < fspec.size(); ++j) {
3638 if (fspec[j] > max) {
3639 max = fspec[j];
3640 maxIdx = j;
3641 }
3642 }
3643 nWaves.push_back(maxIdx);
3644 fspec[maxIdx] = 0.0;
3645 }
3646
3647 }
3648
3649 if (nWaves.size() > 1) {
3650 sort(nWaves.begin(), nWaves.end());
3651 }
3652}
3653
3654void Scantable::addAuxWaveNumbers(const int whichrow, const std::vector<int>& addNWaves, const std::vector<int>& rejectNWaves, std::vector<int>& nWaves)
3655{
3656 std::vector<int> tempAddNWaves, tempRejectNWaves;
3657 for (uInt i = 0; i < addNWaves.size(); ++i) {
3658 tempAddNWaves.push_back(addNWaves[i]);
3659 }
3660 if ((tempAddNWaves.size() == 2) && (tempAddNWaves[1] == -999)) {
3661 setWaveNumberListUptoNyquistFreq(whichrow, tempAddNWaves);
3662 }
3663
3664 for (uInt i = 0; i < rejectNWaves.size(); ++i) {
3665 tempRejectNWaves.push_back(rejectNWaves[i]);
3666 }
3667 if ((tempRejectNWaves.size() == 2) && (tempRejectNWaves[1] == -999)) {
3668 setWaveNumberListUptoNyquistFreq(whichrow, tempRejectNWaves);
3669 }
3670
3671 for (uInt i = 0; i < tempAddNWaves.size(); ++i) {
3672 bool found = false;
3673 for (uInt j = 0; j < nWaves.size(); ++j) {
3674 if (nWaves[j] == tempAddNWaves[i]) {
3675 found = true;
3676 break;
3677 }
3678 }
3679 if (!found) nWaves.push_back(tempAddNWaves[i]);
3680 }
3681
3682 for (uInt i = 0; i < tempRejectNWaves.size(); ++i) {
3683 for (std::vector<int>::iterator j = nWaves.begin(); j != nWaves.end(); ) {
3684 if (*j == tempRejectNWaves[i]) {
3685 j = nWaves.erase(j);
3686 } else {
3687 ++j;
3688 }
3689 }
3690 }
3691
3692 if (nWaves.size() > 1) {
3693 sort(nWaves.begin(), nWaves.end());
3694 unique(nWaves.begin(), nWaves.end());
3695 }
3696}
3697
3698void Scantable::setWaveNumberListUptoNyquistFreq(const int whichrow, std::vector<int>& nWaves)
3699{
3700 if ((nWaves.size() == 2)&&(nWaves[1] == -999)) {
3701 int val = nWaves[0];
3702 int nyquistFreq = nchan(getIF(whichrow))/2+1;
3703 nWaves.clear();
3704 if (val > nyquistFreq) { // for safety, at least nWaves contains a constant; CAS-3759
3705 nWaves.push_back(0);
3706 }
3707 while (val <= nyquistFreq) {
3708 nWaves.push_back(val);
3709 val++;
3710 }
3711 }
3712}
3713
3714void 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)
3715{
3716 try {
3717 ofstream ofs;
3718 String coordInfo = "";
3719 bool hasSameNchan = true;
3720 bool outTextFile = false;
3721 bool csvFormat = false;
3722
3723 if (blfile != "") {
3724 csvFormat = (blfile.substr(0, 1) == "T");
3725 ofs.open(blfile.substr(1).c_str(), ios::out | ios::app);
3726 if (ofs) outTextFile = true;
3727 }
3728
3729 if (outLogger || outTextFile) {
3730 coordInfo = getCoordInfo()[0];
3731 if (coordInfo == "") coordInfo = "channel";
3732 hasSameNchan = hasSameNchanOverIFs();
3733 }
3734
3735 //Fitter fitter = Fitter();
3736 //fitter.setExpression("sinusoid", nWaves);
3737 //fitter.setIterClipping(thresClip, nIterClip);
3738
3739 int nRow = nrow();
3740 std::vector<bool> chanMask;
3741 std::vector<int> nWaves;
3742
3743 bool showProgress;
3744 int minNRow;
3745 parseProgressInfo(progressInfo, showProgress, minNRow);
3746
3747 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
3748 chanMask = getCompositeChanMask(whichrow, mask);
3749 selectWaveNumbers(whichrow, chanMask, applyFFT, fftMethod, fftThresh, addNWaves, rejectNWaves, nWaves);
3750
3751 //FOR DEBUGGING------------
3752 /*
3753 if (whichrow < 0) {// == nRow -1) {
3754 cout << "+++ i=" << setw(3) << whichrow << ", IF=" << setw(2) << getIF(whichrow);
3755 if (applyFFT) {
3756 cout << "[ ";
3757 for (uInt j = 0; j < nWaves.size(); ++j) {
3758 cout << nWaves[j] << ", ";
3759 }
3760 cout << " ] " << endl;
3761 }
3762 cout << flush;
3763 }
3764 */
3765 //-------------------------
3766
3767 //fitBaseline(chanMask, whichrow, fitter);
3768 //setSpectrum((getResidual ? fitter.getResidual() : fitter.getFit()), whichrow);
3769 std::vector<float> params;
3770 int nClipped = 0;
3771 std::vector<float> res = doSinusoidFitting(getSpectrum(whichrow), chanMask, nWaves, params, nClipped, thresClip, nIterClip, getResidual);
3772 setSpectrum(res, whichrow);
3773 //
3774
3775 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow, coordInfo, hasSameNchan, ofs, "sinusoidBaseline()", params, nClipped);
3776 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
3777 }
3778
3779 if (outTextFile) ofs.close();
3780
3781 } catch (...) {
3782 throw;
3783 }
3784}
3785
3786void 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)
3787{
3788 try {
3789 ofstream ofs;
3790 String coordInfo = "";
3791 bool hasSameNchan = true;
3792 bool outTextFile = false;
3793 bool csvFormat = false;
3794
3795 if (blfile != "") {
3796 csvFormat = (blfile.substr(0, 1) == "T");
3797 ofs.open(blfile.substr(1).c_str(), ios::out | ios::app);
3798 if (ofs) outTextFile = true;
3799 }
3800
3801 if (outLogger || outTextFile) {
3802 coordInfo = getCoordInfo()[0];
3803 if (coordInfo == "") coordInfo = "channel";
3804 hasSameNchan = hasSameNchanOverIFs();
3805 }
3806
3807 //Fitter fitter = Fitter();
3808 //fitter.setExpression("sinusoid", nWaves);
3809 //fitter.setIterClipping(thresClip, nIterClip);
3810
3811 int nRow = nrow();
3812 std::vector<bool> chanMask;
3813 std::vector<int> nWaves;
3814
3815 int minEdgeSize = getIFNos().size()*2;
3816 STLineFinder lineFinder = STLineFinder();
3817 lineFinder.setOptions(threshold, 3, chanAvgLimit);
3818
3819 bool showProgress;
3820 int minNRow;
3821 parseProgressInfo(progressInfo, showProgress, minNRow);
3822
3823 for (int whichrow = 0; whichrow < nRow; ++whichrow) {
3824
3825 //-------------------------------------------------------
3826 //chanMask = getCompositeChanMask(whichrow, mask, edge, minEdgeSize, lineFinder);
3827 //-------------------------------------------------------
3828 int edgeSize = edge.size();
3829 std::vector<int> currentEdge;
3830 if (edgeSize >= 2) {
3831 int idx = 0;
3832 if (edgeSize > 2) {
3833 if (edgeSize < minEdgeSize) {
3834 throw(AipsError("Length of edge element info is less than that of IFs"));
3835 }
3836 idx = 2 * getIF(whichrow);
3837 }
3838 currentEdge.push_back(edge[idx]);
3839 currentEdge.push_back(edge[idx+1]);
3840 } else {
3841 throw(AipsError("Wrong length of edge element"));
3842 }
3843 lineFinder.setData(getSpectrum(whichrow));
3844 lineFinder.findLines(getCompositeChanMask(whichrow, mask), currentEdge, whichrow);
3845 chanMask = lineFinder.getMask();
3846 //-------------------------------------------------------
3847
3848 selectWaveNumbers(whichrow, chanMask, applyFFT, fftMethod, fftThresh, addNWaves, rejectNWaves, nWaves);
3849
3850 //fitBaseline(chanMask, whichrow, fitter);
3851 //setSpectrum((getResidual ? fitter.getResidual() : fitter.getFit()), whichrow);
3852 std::vector<float> params;
3853 int nClipped = 0;
3854 std::vector<float> res = doSinusoidFitting(getSpectrum(whichrow), chanMask, nWaves, params, nClipped, thresClip, nIterClip, getResidual);
3855 setSpectrum(res, whichrow);
3856 //
3857
3858 outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow, coordInfo, hasSameNchan, ofs, "autoSinusoidBaseline()", params, nClipped);
3859 showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
3860 }
3861
3862 if (outTextFile) ofs.close();
3863
3864 } catch (...) {
3865 throw;
3866 }
3867}
3868
3869std::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)
3870{
3871 if (data.size() != mask.size()) {
3872 throw(AipsError("data and mask sizes are not identical"));
3873 }
3874 if (data.size() < 2) {
3875 throw(AipsError("data size is too short"));
3876 }
3877 if (waveNumbers.size() == 0) {
3878 throw(AipsError("no wave numbers given"));
3879 }
3880 std::vector<int> nWaves; // sorted and uniqued array of wave numbers
3881 nWaves.reserve(waveNumbers.size());
3882 copy(waveNumbers.begin(), waveNumbers.end(), back_inserter(nWaves));
3883 sort(nWaves.begin(), nWaves.end());
3884 std::vector<int>::iterator end_it = unique(nWaves.begin(), nWaves.end());
3885 nWaves.erase(end_it, nWaves.end());
3886
3887 int minNWaves = nWaves[0];
3888 if (minNWaves < 0) {
3889 throw(AipsError("wave number must be positive or zero (i.e. constant)"));
3890 }
3891 bool hasConstantTerm = (minNWaves == 0);
3892
3893 int nChan = data.size();
3894 std::vector<int> maskArray;
3895 std::vector<int> x;
3896 for (int i = 0; i < nChan; ++i) {
3897 maskArray.push_back(mask[i] ? 1 : 0);
3898 if (mask[i]) {
3899 x.push_back(i);
3900 }
3901 }
3902
3903 int initNData = x.size();
3904
3905 int nData = initNData;
3906 int nDOF = nWaves.size() * 2 - (hasConstantTerm ? 1 : 0); //number of parameters to solve.
3907
3908 const double PI = 6.0 * asin(0.5); // PI (= 3.141592653...)
3909 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)
3910
3911 // xArray : contains elemental values for computing the least-square matrix.
3912 // xArray.size() is nDOF and xArray[*].size() is nChan.
3913 // Each xArray element are as follows:
3914 // xArray[0] = {1.0, 1.0, 1.0, ..., 1.0},
3915 // xArray[2n-1] = {sin(nPI/L*x[0]), sin(nPI/L*x[1]), ..., sin(nPI/L*x[nChan])},
3916 // xArray[2n] = {cos(nPI/L*x[0]), cos(nPI/L*x[1]), ..., cos(nPI/L*x[nChan])},
3917 // where (1 <= n <= nMaxWavesInSW),
3918 // or,
3919 // xArray[2n-1] = {sin(wn[n]PI/L*x[0]), sin(wn[n]PI/L*x[1]), ..., sin(wn[n]PI/L*x[nChan])},
3920 // xArray[2n] = {cos(wn[n]PI/L*x[0]), cos(wn[n]PI/L*x[1]), ..., cos(wn[n]PI/L*x[nChan])},
3921 // where wn[n] denotes waveNumbers[n] (1 <= n <= waveNumbers.size()).
3922 std::vector<std::vector<double> > xArray;
3923 if (hasConstantTerm) {
3924 std::vector<double> xu;
3925 for (int j = 0; j < nChan; ++j) {
3926 xu.push_back(1.0);
3927 }
3928 xArray.push_back(xu);
3929 }
3930 for (uInt i = (hasConstantTerm ? 1 : 0); i < nWaves.size(); ++i) {
3931 double xFactor = baseXFactor*(double)nWaves[i];
3932 std::vector<double> xs, xc;
3933 xs.clear();
3934 xc.clear();
3935 for (int j = 0; j < nChan; ++j) {
3936 xs.push_back(sin(xFactor*(double)j));
3937 xc.push_back(cos(xFactor*(double)j));
3938 }
3939 xArray.push_back(xs);
3940 xArray.push_back(xc);
3941 }
3942
3943 std::vector<double> z1, r1, residual;
3944 for (int i = 0; i < nChan; ++i) {
3945 z1.push_back((double)data[i]);
3946 r1.push_back(0.0);
3947 residual.push_back(0.0);
3948 }
3949
3950 for (int nClip = 0; nClip < nIterClip+1; ++nClip) {
3951 // xMatrix : horizontal concatenation of
3952 // the least-sq. matrix (left) and an
3953 // identity matrix (right).
3954 // the right part is used to calculate the inverse matrix of the left part.
3955 double xMatrix[nDOF][2*nDOF];
3956 double zMatrix[nDOF];
3957 for (int i = 0; i < nDOF; ++i) {
3958 for (int j = 0; j < 2*nDOF; ++j) {
3959 xMatrix[i][j] = 0.0;
3960 }
3961 xMatrix[i][nDOF+i] = 1.0;
3962 zMatrix[i] = 0.0;
3963 }
3964
3965 int nUseData = 0;
3966 for (int k = 0; k < nChan; ++k) {
3967 if (maskArray[k] == 0) continue;
3968
3969 for (int i = 0; i < nDOF; ++i) {
3970 for (int j = i; j < nDOF; ++j) {
3971 xMatrix[i][j] += xArray[i][k] * xArray[j][k];
3972 }
3973 zMatrix[i] += z1[k] * xArray[i][k];
3974 }
3975
3976 nUseData++;
3977 }
3978
3979 if (nUseData < 1) {
3980 throw(AipsError("all channels clipped or masked. can't execute fitting anymore."));
3981 }
3982
3983 for (int i = 0; i < nDOF; ++i) {
3984 for (int j = 0; j < i; ++j) {
3985 xMatrix[i][j] = xMatrix[j][i];
3986 }
3987 }
3988
3989 std::vector<double> invDiag;
3990 for (int i = 0; i < nDOF; ++i) {
3991 invDiag.push_back(1.0/xMatrix[i][i]);
3992 for (int j = 0; j < nDOF; ++j) {
3993 xMatrix[i][j] *= invDiag[i];
3994 }
3995 }
3996
3997 for (int k = 0; k < nDOF; ++k) {
3998 for (int i = 0; i < nDOF; ++i) {
3999 if (i != k) {
4000 double factor1 = xMatrix[k][k];
4001 double factor2 = xMatrix[i][k];
4002 for (int j = k; j < 2*nDOF; ++j) {
4003 xMatrix[i][j] *= factor1;
4004 xMatrix[i][j] -= xMatrix[k][j]*factor2;
4005 xMatrix[i][j] /= factor1;
4006 }
4007 }
4008 }
4009 double xDiag = xMatrix[k][k];
4010 for (int j = k; j < 2*nDOF; ++j) {
4011 xMatrix[k][j] /= xDiag;
4012 }
4013 }
4014
4015 for (int i = 0; i < nDOF; ++i) {
4016 for (int j = 0; j < nDOF; ++j) {
4017 xMatrix[i][nDOF+j] *= invDiag[j];
4018 }
4019 }
4020 //compute a vector y which consists of the coefficients of the sinusoids forming the
4021 //best-fit curves (a0,s1,c1,s2,c2,...), where a0 is constant and s* and c* are of sine
4022 //and cosine functions, respectively.
4023 std::vector<double> y;
4024 params.clear();
4025 for (int i = 0; i < nDOF; ++i) {
4026 y.push_back(0.0);
4027 for (int j = 0; j < nDOF; ++j) {
4028 y[i] += xMatrix[i][nDOF+j]*zMatrix[j];
4029 }
4030 params.push_back(y[i]);
4031 }
4032
4033 for (int i = 0; i < nChan; ++i) {
4034 r1[i] = y[0];
4035 for (int j = 1; j < nDOF; ++j) {
4036 r1[i] += y[j]*xArray[j][i];
4037 }
4038 residual[i] = z1[i] - r1[i];
4039 }
4040
4041 if ((nClip == nIterClip) || (thresClip <= 0.0)) {
4042 break;
4043 } else {
4044 double stdDev = 0.0;
4045 for (int i = 0; i < nChan; ++i) {
4046 stdDev += residual[i]*residual[i]*(double)maskArray[i];
4047 }
4048 stdDev = sqrt(stdDev/(double)nData);
4049
4050 double thres = stdDev * thresClip;
4051 int newNData = 0;
4052 for (int i = 0; i < nChan; ++i) {
4053 if (abs(residual[i]) >= thres) {
4054 maskArray[i] = 0;
4055 }
4056 if (maskArray[i] > 0) {
4057 newNData++;
4058 }
4059 }
4060 if (newNData == nData) {
4061 break; //no more flag to add. iteration stops.
4062 } else {
4063 nData = newNData;
4064 }
4065 }
4066 }
4067
4068 nClipped = initNData - nData;
4069
4070 std::vector<float> result;
4071 if (getResidual) {
4072 for (int i = 0; i < nChan; ++i) {
4073 result.push_back((float)residual[i]);
4074 }
4075 } else {
4076 for (int i = 0; i < nChan; ++i) {
4077 result.push_back((float)r1[i]);
4078 }
4079 }
4080
4081 return result;
4082}
4083
4084void Scantable::fitBaseline(const std::vector<bool>& mask, int whichrow, Fitter& fitter)
4085{
4086 std::vector<double> dAbcissa = getAbcissa(whichrow);
4087 std::vector<float> abcissa;
4088 for (uInt i = 0; i < dAbcissa.size(); ++i) {
4089 abcissa.push_back((float)dAbcissa[i]);
4090 }
4091 std::vector<float> spec = getSpectrum(whichrow);
4092
4093 fitter.setData(abcissa, spec, mask);
4094 fitter.lfit();
4095}
4096
4097std::vector<bool> Scantable::getCompositeChanMask(int whichrow, const std::vector<bool>& inMask)
4098{
4099 /****
4100 double ms1TimeStart, ms1TimeEnd, ms2TimeStart, ms2TimeEnd;
4101 double elapse1 = 0.0;
4102 double elapse2 = 0.0;
4103
4104 ms1TimeStart = mathutil::gettimeofday_sec();
4105 ****/
4106
4107 std::vector<bool> mask = getMask(whichrow);
4108
4109 /****
4110 ms1TimeEnd = mathutil::gettimeofday_sec();
4111 elapse1 = ms1TimeEnd - ms1TimeStart;
4112 std::cout << "ms1 : " << elapse1 << " (sec.)" << endl;
4113 ms2TimeStart = mathutil::gettimeofday_sec();
4114 ****/
4115
4116 uInt maskSize = mask.size();
4117 if (inMask.size() != 0) {
4118 if (maskSize != inMask.size()) {
4119 throw(AipsError("mask sizes are not the same."));
4120 }
4121 for (uInt i = 0; i < maskSize; ++i) {
4122 mask[i] = mask[i] && inMask[i];
4123 }
4124 }
4125
4126 /****
4127 ms2TimeEnd = mathutil::gettimeofday_sec();
4128 elapse2 = ms2TimeEnd - ms2TimeStart;
4129 std::cout << "ms2 : " << elapse2 << " (sec.)" << endl;
4130 ****/
4131
4132 return mask;
4133}
4134
4135/*
4136std::vector<bool> Scantable::getCompositeChanMask(int whichrow, const std::vector<bool>& inMask, const std::vector<int>& edge, const int minEdgeSize, STLineFinder& lineFinder)
4137{
4138 int edgeSize = edge.size();
4139 std::vector<int> currentEdge;
4140 if (edgeSize >= 2) {
4141 int idx = 0;
4142 if (edgeSize > 2) {
4143 if (edgeSize < minEdgeSize) {
4144 throw(AipsError("Length of edge element info is less than that of IFs"));
4145 }
4146 idx = 2 * getIF(whichrow);
4147 }
4148 currentEdge.push_back(edge[idx]);
4149 currentEdge.push_back(edge[idx+1]);
4150 } else {
4151 throw(AipsError("Wrong length of edge element"));
4152 }
4153
4154 lineFinder.setData(getSpectrum(whichrow));
4155 lineFinder.findLines(getCompositeChanMask(whichrow, inMask), currentEdge, whichrow);
4156
4157 return lineFinder.getMask();
4158}
4159*/
4160
4161/* for poly. the variations of outputFittingResult() should be merged into one eventually (2011/3/10 WK) */
4162void 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)
4163{
4164 if (outLogger || outTextFile) {
4165 std::vector<float> params = fitter.getParameters();
4166 std::vector<bool> fixed = fitter.getFixedParameters();
4167 float rms = getRms(chanMask, whichrow);
4168 String masklist = getMaskRangeList(chanMask, whichrow, coordInfo, hasSameNchan);
4169
4170 if (outLogger) {
4171 LogIO ols(LogOrigin("Scantable", funcName, WHERE));
4172 ols << formatBaselineParams(params, fixed, rms, -1, masklist, whichrow, false, csvFormat) << LogIO::POST ;
4173 }
4174 if (outTextFile) {
4175 ofs << formatBaselineParams(params, fixed, rms, -1, masklist, whichrow, true, csvFormat) << flush;
4176 }
4177 }
4178}
4179
4180/* for cspline. will be merged once cspline is available in fitter (2011/3/10 WK) */
4181void 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)
4182{
4183 if (outLogger || outTextFile) {
4184 /****
4185 double ms1TimeStart, ms1TimeEnd, ms2TimeStart, ms2TimeEnd;
4186 double elapse1 = 0.0;
4187 double elapse2 = 0.0;
4188
4189 ms1TimeStart = mathutil::gettimeofday_sec();
4190 ****/
4191
4192 float rms = getRms(chanMask, whichrow);
4193
4194 /****
4195 ms1TimeEnd = mathutil::gettimeofday_sec();
4196 elapse1 = ms1TimeEnd - ms1TimeStart;
4197 std::cout << "ot1 : " << elapse1 << " (sec.)" << endl;
4198 ms2TimeStart = mathutil::gettimeofday_sec();
4199 ****/
4200
4201 String masklist = getMaskRangeList(chanMask, whichrow, coordInfo, hasSameNchan);
4202 std::vector<bool> fixed;
4203 fixed.clear();
4204
4205 if (outLogger) {
4206 LogIO ols(LogOrigin("Scantable", funcName, WHERE));
4207 ols << formatPiecewiseBaselineParams(edge, params, fixed, rms, nClipped, masklist, whichrow, false, csvFormat) << LogIO::POST ;
4208 }
4209 if (outTextFile) {
4210 ofs << formatPiecewiseBaselineParams(edge, params, fixed, rms, nClipped, masklist, whichrow, true, csvFormat) << flush;
4211 }
4212
4213 /****
4214 ms2TimeEnd = mathutil::gettimeofday_sec();
4215 elapse2 = ms2TimeEnd - ms2TimeStart;
4216 std::cout << "ot2 : " << elapse2 << " (sec.)" << endl;
4217 ****/
4218
4219 }
4220}
4221
4222/* for chebyshev/sinusoid. will be merged once chebyshev/sinusoid is available in fitter (2011/3/10 WK) */
4223void 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)
4224{
4225 if (outLogger || outTextFile) {
4226 float rms = getRms(chanMask, whichrow);
4227 String masklist = getMaskRangeList(chanMask, whichrow, coordInfo, hasSameNchan);
4228 std::vector<bool> fixed;
4229 fixed.clear();
4230
4231 if (outLogger) {
4232 LogIO ols(LogOrigin("Scantable", funcName, WHERE));
4233 ols << formatBaselineParams(params, fixed, rms, nClipped, masklist, whichrow, false, csvFormat) << LogIO::POST ;
4234 }
4235 if (outTextFile) {
4236 ofs << formatBaselineParams(params, fixed, rms, nClipped, masklist, whichrow, true, csvFormat) << flush;
4237 }
4238 }
4239}
4240
4241void Scantable::parseProgressInfo(const std::string& progressInfo, bool& showProgress, int& minNRow)
4242{
4243 int idxDelimiter = progressInfo.find(",");
4244 if (idxDelimiter < 0) {
4245 throw(AipsError("wrong value in 'showprogress' parameter")) ;
4246 }
4247 showProgress = (progressInfo.substr(0, idxDelimiter) == "true");
4248 std::istringstream is(progressInfo.substr(idxDelimiter+1));
4249 is >> minNRow;
4250}
4251
4252void Scantable::showProgressOnTerminal(const int nProcessed, const int nTotal, const bool showProgress, const int nTotalThreshold)
4253{
4254 if (showProgress && (nTotal >= nTotalThreshold)) {
4255 int nInterval = int(floor(double(nTotal)/100.0));
4256 if (nInterval == 0) nInterval++;
4257
4258 if (nProcessed % nInterval == 0) {
4259 printf("\r"); //go to the head of line
4260 printf("\x1b[31m\x1b[1m"); //set red color, highlighted
4261 printf("[%3d%%]", (int)(100.0*(double(nProcessed+1))/(double(nTotal))) );
4262 printf("\x1b[39m\x1b[0m"); //set default attributes
4263 fflush(NULL);
4264 }
4265
4266 if (nProcessed == nTotal - 1) {
4267 printf("\r\x1b[K"); //clear
4268 fflush(NULL);
4269 }
4270
4271 }
4272}
4273
4274std::vector<float> Scantable::execFFT(const int whichrow, const std::vector<bool>& inMask, bool getRealImag, bool getAmplitudeOnly)
4275{
4276 std::vector<bool> mask = getMask(whichrow);
4277
4278 if (inMask.size() > 0) {
4279 uInt maskSize = mask.size();
4280 if (maskSize != inMask.size()) {
4281 throw(AipsError("mask sizes are not the same."));
4282 }
4283 for (uInt i = 0; i < maskSize; ++i) {
4284 mask[i] = mask[i] && inMask[i];
4285 }
4286 }
4287
4288 Vector<Float> spec = getSpectrum(whichrow);
4289 mathutil::doZeroOrderInterpolation(spec, mask);
4290
4291 FFTServer<Float,Complex> ffts;
4292 Vector<Complex> fftres;
4293 ffts.fft0(fftres, spec);
4294
4295 std::vector<float> res;
4296 float norm = float(2.0/double(spec.size()));
4297
4298 if (getRealImag) {
4299 for (uInt i = 0; i < fftres.size(); ++i) {
4300 res.push_back(real(fftres[i])*norm);
4301 res.push_back(imag(fftres[i])*norm);
4302 }
4303 } else {
4304 for (uInt i = 0; i < fftres.size(); ++i) {
4305 res.push_back(abs(fftres[i])*norm);
4306 if (!getAmplitudeOnly) res.push_back(arg(fftres[i]));
4307 }
4308 }
4309
4310 return res;
4311}
4312
4313
4314float Scantable::getRms(const std::vector<bool>& mask, int whichrow)
4315{
4316 /****
4317 double ms1TimeStart, ms1TimeEnd, ms2TimeStart, ms2TimeEnd;
4318 double elapse1 = 0.0;
4319 double elapse2 = 0.0;
4320
4321 ms1TimeStart = mathutil::gettimeofday_sec();
4322 ****/
4323
4324 Vector<Float> spec;
4325
4326 specCol_.get(whichrow, spec);
4327
4328 /****
4329 ms1TimeEnd = mathutil::gettimeofday_sec();
4330 elapse1 = ms1TimeEnd - ms1TimeStart;
4331 std::cout << "rm1 : " << elapse1 << " (sec.)" << endl;
4332 ms2TimeStart = mathutil::gettimeofday_sec();
4333 ****/
4334
4335 float mean = 0.0;
4336 float smean = 0.0;
4337 int n = 0;
4338 for (uInt i = 0; i < spec.nelements(); ++i) {
4339 if (mask[i]) {
4340 mean += spec[i];
4341 smean += spec[i]*spec[i];
4342 n++;
4343 }
4344 }
4345
4346 mean /= (float)n;
4347 smean /= (float)n;
4348
4349 /****
4350 ms2TimeEnd = mathutil::gettimeofday_sec();
4351 elapse2 = ms2TimeEnd - ms2TimeStart;
4352 std::cout << "rm2 : " << elapse2 << " (sec.)" << endl;
4353 ****/
4354
4355 return sqrt(smean - mean*mean);
4356}
4357
4358
4359std::string Scantable::formatBaselineParamsHeader(int whichrow, const std::string& masklist, bool verbose, bool csvformat) const
4360{
4361 if (verbose) {
4362 ostringstream oss;
4363
4364 if (csvformat) {
4365 oss << getScan(whichrow) << ",";
4366 oss << getBeam(whichrow) << ",";
4367 oss << getIF(whichrow) << ",";
4368 oss << getPol(whichrow) << ",";
4369 oss << getCycle(whichrow) << ",";
4370 String commaReplacedMasklist = masklist;
4371 string::size_type pos = 0;
4372 while (pos = commaReplacedMasklist.find(","), pos != string::npos) {
4373 commaReplacedMasklist.replace(pos, 1, ";");
4374 pos++;
4375 }
4376 oss << commaReplacedMasklist << ",";
4377 } else {
4378 oss << " Scan[" << getScan(whichrow) << "]";
4379 oss << " Beam[" << getBeam(whichrow) << "]";
4380 oss << " IF[" << getIF(whichrow) << "]";
4381 oss << " Pol[" << getPol(whichrow) << "]";
4382 oss << " Cycle[" << getCycle(whichrow) << "]: " << endl;
4383 oss << "Fitter range = " << masklist << endl;
4384 oss << "Baseline parameters" << endl;
4385 }
4386 oss << flush;
4387
4388 return String(oss);
4389 }
4390
4391 return "";
4392}
4393
4394std::string Scantable::formatBaselineParamsFooter(float rms, int nClipped, bool verbose, bool csvformat) const
4395{
4396 if (verbose) {
4397 ostringstream oss;
4398
4399 if (csvformat) {
4400 oss << rms << ",";
4401 if (nClipped >= 0) {
4402 oss << nClipped;
4403 }
4404 } else {
4405 oss << "Results of baseline fit" << endl;
4406 oss << " rms = " << setprecision(6) << rms << endl;
4407 if (nClipped >= 0) {
4408 oss << " Number of clipped channels = " << nClipped << endl;
4409 }
4410 for (int i = 0; i < 60; ++i) {
4411 oss << "-";
4412 }
4413 }
4414 oss << endl;
4415 oss << flush;
4416
4417 return String(oss);
4418 }
4419
4420 return "";
4421}
4422
4423std::string Scantable::formatBaselineParams(const std::vector<float>& params,
4424 const std::vector<bool>& fixed,
4425 float rms,
4426 int nClipped,
4427 const std::string& masklist,
4428 int whichrow,
4429 bool verbose,
4430 bool csvformat,
4431 int start, int count,
4432 bool resetparamid) const
4433{
4434 int nParam = (int)(params.size());
4435
4436 if (nParam < 1) {
4437 return(" Not fitted");
4438 } else {
4439
4440 ostringstream oss;
4441 oss << formatBaselineParamsHeader(whichrow, masklist, verbose, csvformat);
4442
4443 if (start < 0) start = 0;
4444 if (count < 0) count = nParam;
4445 int end = start + count;
4446 if (end > nParam) end = nParam;
4447 int paramidoffset = (resetparamid) ? (-start) : 0;
4448
4449 for (int i = start; i < end; ++i) {
4450 if (i > start) {
4451 oss << ",";
4452 }
4453 std::string sFix = ((fixed.size() > 0) && (fixed[i]) && verbose) ? "(fixed)" : "";
4454 if (csvformat) {
4455 oss << params[i] << sFix;
4456 } else {
4457 oss << " p" << (i+paramidoffset) << sFix << "= " << right << setw(13) << setprecision(6) << params[i];
4458 }
4459 }
4460
4461 if (csvformat) {
4462 oss << ",";
4463 } else {
4464 oss << endl;
4465 }
4466 oss << formatBaselineParamsFooter(rms, nClipped, verbose, csvformat);
4467
4468 return String(oss);
4469 }
4470
4471}
4472
4473std::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
4474{
4475 int nOutParam = (int)(params.size());
4476 int nPiece = (int)(ranges.size()) - 1;
4477
4478 if (nOutParam < 1) {
4479 return(" Not fitted");
4480 } else if (nPiece < 0) {
4481 return formatBaselineParams(params, fixed, rms, nClipped, masklist, whichrow, verbose, csvformat);
4482 } else if (nPiece < 1) {
4483 return(" Bad count of the piece edge info");
4484 } else if (nOutParam % nPiece != 0) {
4485 return(" Bad count of the output baseline parameters");
4486 } else {
4487
4488 int nParam = nOutParam / nPiece;
4489
4490 ostringstream oss;
4491 oss << formatBaselineParamsHeader(whichrow, masklist, verbose, csvformat);
4492
4493 if (csvformat) {
4494 for (int i = 0; i < nPiece; ++i) {
4495 oss << ranges[i] << "," << (ranges[i+1]-1) << ",";
4496 oss << formatBaselineParams(params, fixed, rms, 0, masklist, whichrow, false, csvformat, i*nParam, nParam, true);
4497 }
4498 } else {
4499 stringstream ss;
4500 ss << ranges[nPiece] << flush;
4501 int wRange = ss.str().size() * 2 + 5;
4502
4503 for (int i = 0; i < nPiece; ++i) {
4504 ss.str("");
4505 ss << " [" << ranges[i] << "," << (ranges[i+1]-1) << "]";
4506 oss << left << setw(wRange) << ss.str();
4507 oss << formatBaselineParams(params, fixed, rms, 0, masklist, whichrow, false, csvformat, i*nParam, nParam, true);
4508 //oss << endl;
4509 }
4510 }
4511
4512 oss << formatBaselineParamsFooter(rms, nClipped, verbose, csvformat);
4513
4514 return String(oss);
4515 }
4516
4517}
4518
4519bool Scantable::hasSameNchanOverIFs()
4520{
4521 int nIF = nif(-1);
4522 int nCh;
4523 int totalPositiveNChan = 0;
4524 int nPositiveNChan = 0;
4525
4526 for (int i = 0; i < nIF; ++i) {
4527 nCh = nchan(i);
4528 if (nCh > 0) {
4529 totalPositiveNChan += nCh;
4530 nPositiveNChan++;
4531 }
4532 }
4533
4534 return (totalPositiveNChan == (nPositiveNChan * nchan(0)));
4535}
4536
4537std::string Scantable::getMaskRangeList(const std::vector<bool>& mask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, bool verbose)
4538{
4539 if (mask.size() <= 0) {
4540 throw(AipsError("The mask elements should be > 0"));
4541 }
4542 int IF = getIF(whichrow);
4543 if (mask.size() != (uInt)nchan(IF)) {
4544 throw(AipsError("Number of channels in scantable != number of mask elements"));
4545 }
4546
4547 if (verbose) {
4548 LogIO logOs(LogOrigin("Scantable", "getMaskRangeList()", WHERE));
4549 logOs << LogIO::WARN << "The current mask window unit is " << coordInfo;
4550 if (!hasSameNchan) {
4551 logOs << endl << "This mask is only valid for IF=" << IF;
4552 }
4553 logOs << LogIO::POST;
4554 }
4555
4556 std::vector<double> abcissa = getAbcissa(whichrow);
4557 std::vector<int> edge = getMaskEdgeIndices(mask);
4558
4559 ostringstream oss;
4560 oss.setf(ios::fixed);
4561 oss << setprecision(1) << "[";
4562 for (uInt i = 0; i < edge.size(); i+=2) {
4563 if (i > 0) oss << ",";
4564 oss << "[" << (float)abcissa[edge[i]] << "," << (float)abcissa[edge[i+1]] << "]";
4565 }
4566 oss << "]" << flush;
4567
4568 return String(oss);
4569}
4570
4571std::vector<int> Scantable::getMaskEdgeIndices(const std::vector<bool>& mask)
4572{
4573 if (mask.size() <= 0) {
4574 throw(AipsError("The mask elements should be > 0"));
4575 }
4576
4577 std::vector<int> out, startIndices, endIndices;
4578 int maskSize = mask.size();
4579
4580 startIndices.clear();
4581 endIndices.clear();
4582
4583 if (mask[0]) {
4584 startIndices.push_back(0);
4585 }
4586 for (int i = 1; i < maskSize; ++i) {
4587 if ((!mask[i-1]) && mask[i]) {
4588 startIndices.push_back(i);
4589 } else if (mask[i-1] && (!mask[i])) {
4590 endIndices.push_back(i-1);
4591 }
4592 }
4593 if (mask[maskSize-1]) {
4594 endIndices.push_back(maskSize-1);
4595 }
4596
4597 if (startIndices.size() != endIndices.size()) {
4598 throw(AipsError("Inconsistent Mask Size: bad data?"));
4599 }
4600 for (uInt i = 0; i < startIndices.size(); ++i) {
4601 if (startIndices[i] > endIndices[i]) {
4602 throw(AipsError("Mask start index > mask end index"));
4603 }
4604 }
4605
4606 out.clear();
4607 for (uInt i = 0; i < startIndices.size(); ++i) {
4608 out.push_back(startIndices[i]);
4609 out.push_back(endIndices[i]);
4610 }
4611
4612 return out;
4613}
4614
4615vector<float> Scantable::getTsysSpectrum( int whichrow ) const
4616{
4617 Vector<Float> tsys( tsysCol_(whichrow) ) ;
4618 vector<float> stlTsys ;
4619 tsys.tovector( stlTsys ) ;
4620 return stlTsys ;
4621}
4622
4623vector<uint> Scantable::getMoleculeIdColumnData() const
4624{
4625 Vector<uInt> molIds(mmolidCol_.getColumn());
4626 vector<uint> res;
4627 molIds.tovector(res);
4628 return res;
4629}
4630
4631void Scantable::setMoleculeIdColumnData(const std::vector<uint>& molids)
4632{
4633 Vector<uInt> molIds(molids);
4634 Vector<uInt> arr(mmolidCol_.getColumn());
4635 if ( molIds.nelements() != arr.nelements() )
4636 throw AipsError("The input data size must be the number of rows.");
4637 mmolidCol_.putColumn(molIds);
4638}
4639
4640
4641}
4642//namespace asap
Note: See TracBrowser for help on using the repository browser.