source: trunk/src/Scantable.cpp @ 3085

Last change on this file since 3085 was 3085, checked in by Takeshi Nakazato, 8 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...


Reorder include files to suppress compiler warning related with _XOPEN_SOURCE redefinition.

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