source: trunk/src/Scantable.cpp @ 2437

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

New Development: No

JIRA Issue: Yes

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs: unit test of sdsmooth

Put in Release Notes: No

Module(s):

Description:

Changed the equation to calculate regridded spectral coordinate in
scantable.regrid_channel() so that it would have the same values as
the result of scantable.bin().
scantable.bin() now rebins Tsys arrays if necessary.


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