source: trunk/src/Scantable.cpp @ 3042

Last change on this file since 3042 was 3042, checked in by Takeshi Nakazato, 9 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: Yes/No?

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No?

Module(s): Module Names change impacts.

Description: Describe your changes here...


Fixed wrong memory deallocation. Deleted only when memory is allocated.

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