source: trunk/src/Scantable.cpp @ 3010

Last change on this file since 3010 was 3010, checked in by WataruKawasaki, 10 years ago

New Development: No

JIRA Issue: Yes CAS-6599

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: No

Module(s): sd

Description: modified so that sd.scantable.stats() returns None for flagged rows and rows with all channels flagged.


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