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

Last change on this file since 2595 was 2575, checked in by Kana Sugimoto, 12 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

import asap
scan = asap.scantable("SOME_NAME")
scan.get_direction(0)
# retuns a string of reference frame in addition to RA and Dec.

Put in Release Notes: No

Module(s): scantable.get_direction, scantable._getdirection

Description:

Modified Scantable::formatDirection so that it returns a string of direction
with the reference frame.


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