source: trunk/src/Scantable.cpp @ 859

Last change on this file since 859 was 859, checked in by mar637, 18 years ago

reworked subtable handling
fixed bug in casa::Table construction, where Table::New was used instead of Table::Scratch

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.0 KB
RevLine 
[805]1//
2// C++ Implementation: Scantable
3//
4// Description:
5//
6//
7// Author: Malte Marquarding <asap@atnf.csiro.au>, (C) 2005
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
[206]12#include <map>
13
[125]14#include <casa/aips.h>
[80]15#include <casa/iostream.h>
16#include <casa/iomanip.h>
[805]17#include <casa/OS/Path.h>
18#include <casa/OS/File.h>
[80]19#include <casa/Arrays/Array.h>
20#include <casa/Arrays/ArrayMath.h>
21#include <casa/Arrays/MaskArrMath.h>
22#include <casa/Arrays/ArrayLogical.h>
23#include <casa/Arrays/ArrayAccessor.h>
[455]24#include <casa/Arrays/VectorSTLIterator.h>
[206]25#include <casa/Arrays/Vector.h>
[418]26#include <casa/BasicMath/Math.h>
[504]27#include <casa/BasicSL/Constants.h>
[286]28#include <casa/Quanta/MVAngle.h>
[805]29#include <casa/Containers/RecordField.h>
[2]30
[80]31#include <tables/Tables/TableParse.h>
32#include <tables/Tables/TableDesc.h>
[488]33#include <tables/Tables/TableCopy.h>
[80]34#include <tables/Tables/SetupNewTab.h>
35#include <tables/Tables/ScaColDesc.h>
36#include <tables/Tables/ArrColDesc.h>
[805]37#include <tables/Tables/TableRow.h>
38#include <tables/Tables/TableVector.h>
39#include <tables/Tables/TableIter.h>
[2]40
[80]41#include <tables/Tables/ExprNode.h>
42#include <tables/Tables/TableRecord.h>
43#include <measures/Measures/MFrequency.h>
[805]44#include <measures/Measures/MEpoch.h>
[80]45#include <measures/Measures/MeasTable.h>
[805]46#include <measures/Measures/MeasRef.h>
47#include <measures/TableMeasures/TableMeasRefDesc.h>
48#include <measures/TableMeasures/TableMeasValueDesc.h>
49#include <measures/TableMeasures/TableMeasDesc.h>
50#include <measures/TableMeasures/ScalarMeasColumn.h>
[105]51#include <coordinates/Coordinates/CoordinateUtil.h>
[80]52#include <casa/Quanta/MVTime.h>
[281]53#include <casa/Quanta/MVAngle.h>
[2]54
[805]55#include "Scantable.h"
[476]56#include "SDAttr.h"
[2]57
[125]58using namespace casa;
[2]59
[805]60namespace asap {
61
62Scantable::Scantable(Table::TableType ttype) :
[852]63  type_(ttype)
[206]64{
[805]65  setupMainTable();
[852]66  freqTable_ = STFrequencies(*this);
[805]67  table_.rwKeywordSet().defineTable("FREQUENCIES", freqTable_.table());
[852]68  weatherTable_ = STWeather(*this);
[805]69  table_.rwKeywordSet().defineTable("WEATHER", weatherTable_.table());
[852]70  focusTable_ = STFocus(*this);
[805]71  table_.rwKeywordSet().defineTable("FOCUS", focusTable_.table());
[852]72  tcalTable_ = STTcal(*this);
[805]73  table_.rwKeywordSet().defineTable("TCAL", tcalTable_.table());
[852]74  moleculeTable_ = STMolecules(*this);
[805]75  table_.rwKeywordSet().defineTable("MOLECULES", moleculeTable_.table());
76  setupHistoryTable();
77  setupFitTable();
[852]78  historyTable_ = table_.keywordSet().asTable("HISTORY");
79  fitTable_ = table_.keywordSet().asTable("FITS");
[805]80  originalTable_ = table_;
[322]81  attach();
[18]82}
[206]83
[805]84Scantable::Scantable(const std::string& name, Table::TableType ttype) :
[852]85  type_(ttype)
[206]86{
[50]87  Table tab(name);
[499]88  Int version;
[483]89  tab.keywordSet().get("VERSION", version);
90  if (version != version_) {
91    throw(AipsError("Unsupported version of ASAP file."));
92  }
[805]93  if ( type_ == Table::Memory )
[852]94    table_ = tab.copyToMemoryTable(generateName());
[805]95  else
96    table_ = tab;
[859]97  attachSubtables();
[805]98  originalTable_ = table_;
[329]99  attach();
[2]100}
101
[805]102
103Scantable::Scantable( const Scantable& other, bool clear )
[206]104{
[805]105  // with or without data
[859]106  String newname = String(generateName());
107  if ( other.table_.tableType() == Table::Memory ) {
108      if ( clear ) {
109        cout << "copy ctor memory clear" << endl;
110        table_ = TableCopy::makeEmptyMemoryTable(newname,
111                                                 other.table_, True);
112      } else
113        table_ = other.table_.copyToMemoryTable(newname);
[16]114  } else {
[859]115    if ( clear ) {
116      cout << "copy ctor clear" << endl;
117      other.table_.deepCopy(newname, Table::New);
118      cout << "reading table" << endl;
119      table_ = Table(newname, Table::Scratch);
120      cout << "removing rows" << endl;
121      table_.removeRow(table_.rowNumbers());
122
123    } else {
124      cout << "copy ctor no clear" << endl;
125      other.table_.deepCopy(newname, Table::Scratch);
126      table_ = Table(newname, Table::Scratch);
127    }
[16]128  }
[859]129  //table_.rwKeywordSet().renameTables(newname, table_.tableName());
130  //cout << table_.keywordSet().asTable("TCAL").tableName() << endl;
131  attachSubtables();
[805]132  originalTable_ = table_;
[322]133  attach();
[2]134}
135
[859]136void Scantable::attachSubtables()
137{
138  freqTable_ = STFrequencies(table_);
139  focusTable_ = STFocus(table_);
140  weatherTable_ = STWeather(table_);
141  tcalTable_ = STTcal(table_);
142  moleculeTable_ = STMolecules(table_);
143}
144
[805]145Scantable::~Scantable()
[206]146{
[805]147  cout << "~Scantable() " << this << endl;
[2]148}
149
[805]150void Scantable::setupMainTable()
[206]151{
[805]152  TableDesc td("", "1", TableDesc::Scratch);
153  td.comment() = "An ASAP Scantable";
154  td.rwKeywordSet().define("VERSION", Int(version_));
[2]155
[805]156  // n Cycles
157  td.addColumn(ScalarColumnDesc<uInt>("SCANNO"));
158  // new index every nBeam x nIF x nPol
159  td.addColumn(ScalarColumnDesc<uInt>("CYCLENO"));
[2]160
[805]161  td.addColumn(ScalarColumnDesc<uInt>("BEAMNO"));
162  td.addColumn(ScalarColumnDesc<uInt>("IFNO"));
163  td.rwKeywordSet().define("POLTYPE", String("linear"));
164  td.addColumn(ScalarColumnDesc<uInt>("POLNO"));
[138]165
[805]166  td.addColumn(ScalarColumnDesc<uInt>("FREQ_ID"));
167  td.addColumn(ScalarColumnDesc<uInt>("MOLECULE_ID"));
168  // linear, circular, stokes [I Q U V], stokes1 [I Plinear Pangle V]
169  td.addColumn(ScalarColumnDesc<Int>("REFBEAMNO"));
[80]170
[805]171  td.addColumn(ScalarColumnDesc<Double>("TIME"));
172  TableMeasRefDesc measRef(MEpoch::UTC); // UTC as default
173  TableMeasValueDesc measVal(td, "TIME");
174  TableMeasDesc<MEpoch> mepochCol(measVal, measRef);
175  mepochCol.write(td);
[483]176
[805]177  td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
178
[2]179  td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
[805]180  // Type of source (on=0, off=1, other=-1)
181  td.addColumn(ScalarColumnDesc<Int>("SRCTYPE", Int(-1)));
182  td.addColumn(ScalarColumnDesc<String>("FIELDNAME"));
183
184  //The actual Data Vectors
[2]185  td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
186  td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
[89]187  td.addColumn(ArrayColumnDesc<Float>("TSYS"));
[805]188
189  td.addColumn(ArrayColumnDesc<Double>("DIRECTION",
190                                       IPosition(1,2),
191                                       ColumnDesc::Direct));
192  TableMeasRefDesc mdirRef(MDirection::J2000); // default
193  TableMeasValueDesc tmvdMDir(td, "DIRECTION");
194  // the TableMeasDesc gives the column a type
195  TableMeasDesc<MDirection> mdirCol(tmvdMDir, mdirRef);
196  // writing create the measure column
197  mdirCol.write(td);
198  td.addColumn(ScalarColumnDesc<Double>("AZIMUTH"));
199  td.addColumn(ScalarColumnDesc<Double>("ELEVATION"));
[105]200  td.addColumn(ScalarColumnDesc<Float>("PARANGLE"));
201
[805]202  td.addColumn(ScalarColumnDesc<uInt>("TCAL_ID"));
203  td.addColumn(ScalarColumnDesc<uInt>("FIT_ID"));
204
205  td.addColumn(ScalarColumnDesc<uInt>("FOCUS_ID"));
206  td.addColumn(ScalarColumnDesc<uInt>("WEATHER_ID"));
207
208  td.rwKeywordSet().define("OBSMODE", String(""));
209
[418]210  // Now create Table SetUp from the description.
[859]211  SetupNewTable aNewTab(generateName(), td, Table::Scratch);
[852]212  table_ = Table(aNewTab, type_, 0);
[805]213  originalTable_ = table_;
[745]214
[805]215}
[418]216
[837]217void Scantable::setupHistoryTable( )
[805]218{
[483]219  TableDesc tdh("", "1", TableDesc::Scratch);
220  tdh.addColumn(ScalarColumnDesc<String>("ITEM"));
[859]221  SetupNewTable histtab("history", tdh, Table::Scratch);
[483]222  Table histTable(histtab, Table::Memory);
223  table_.rwKeywordSet().defineTable("HISTORY", histTable);
[2]224}
225
[805]226void Scantable::setupFitTable()
[322]227{
[39]228  TableDesc td("", "1", TableDesc::Scratch);
[805]229  td.addColumn(ScalarColumnDesc<uInt>("FIT_ID"));
[455]230  td.addColumn(ArrayColumnDesc<String>("FUNCTIONS"));
231  td.addColumn(ArrayColumnDesc<Int>("COMPONENTS"));
232  td.addColumn(ArrayColumnDesc<Double>("PARAMETERS"));
233  td.addColumn(ArrayColumnDesc<Bool>("PARMASK"));
[465]234  td.addColumn(ArrayColumnDesc<String>("FRAMEINFO"));
[859]235  SetupNewTable aNewTab("fits", td, Table::Scratch);
[455]236  Table aTable(aNewTab, Table::Memory);
237  table_.rwKeywordSet().defineTable("FITS", aTable);
238}
239
[805]240void Scantable::attach()
[455]241{
[805]242  timeCol_.attach(table_, "TIME");
243  srcnCol_.attach(table_, "SRCNAME");
244  specCol_.attach(table_, "SPECTRA");
245  flagsCol_.attach(table_, "FLAGTRA");
246  tsCol_.attach(table_, "TSYS");
247  cycleCol_.attach(table_,"CYCLENO");
248  scanCol_.attach(table_, "SCANNO");
249  beamCol_.attach(table_, "BEAMNO");
[847]250  ifCol_.attach(table_, "IFNO");
251  polCol_.attach(table_, "POLNO");
[805]252  integrCol_.attach(table_, "INTERVAL");
253  azCol_.attach(table_, "AZIMUTH");
254  elCol_.attach(table_, "ELEVATION");
255  dirCol_.attach(table_, "DIRECTION");
256  paraCol_.attach(table_, "PARANGLE");
257  fldnCol_.attach(table_, "FIELDNAME");
258  rbeamCol_.attach(table_, "REFBEAMNO");
[455]259
[805]260  mfitidCol_.attach(table_,"FIT_ID");
[859]261  //fitidCol_.attach(fitTable_,"FIT_ID");
[465]262
[805]263  mfreqidCol_.attach(table_, "FREQ_ID");
[465]264
[805]265  mtcalidCol_.attach(table_, "TCAL_ID");
[465]266
[805]267  mfocusidCol_.attach(table_, "FOCUS_ID");
[455]268
[805]269  mmolidCol_.attach(table_, "MOLECULE_ID");
[455]270}
271
[845]272void Scantable::setHeader(const SDHeader& sdh)
[206]273{
[18]274  table_.rwKeywordSet().define("nIF", sdh.nif);
275  table_.rwKeywordSet().define("nBeam", sdh.nbeam);
276  table_.rwKeywordSet().define("nPol", sdh.npol);
277  table_.rwKeywordSet().define("nChan", sdh.nchan);
278  table_.rwKeywordSet().define("Observer", sdh.observer);
279  table_.rwKeywordSet().define("Project", sdh.project);
280  table_.rwKeywordSet().define("Obstype", sdh.obstype);
281  table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
282  table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
283  table_.rwKeywordSet().define("Equinox", sdh.equinox);
284  table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
285  table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
286  table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
287  table_.rwKeywordSet().define("UTC", sdh.utc);
[206]288  table_.rwKeywordSet().define("FluxUnit", sdh.fluxunit);
289  table_.rwKeywordSet().define("Epoch", sdh.epoch);
[50]290}
[21]291
[845]292SDHeader Scantable::getHeader() const
[206]293{
[21]294  SDHeader sdh;
295  table_.keywordSet().get("nBeam",sdh.nbeam);
296  table_.keywordSet().get("nIF",sdh.nif);
297  table_.keywordSet().get("nPol",sdh.npol);
298  table_.keywordSet().get("nChan",sdh.nchan);
299  table_.keywordSet().get("Observer", sdh.observer);
300  table_.keywordSet().get("Project", sdh.project);
301  table_.keywordSet().get("Obstype", sdh.obstype);
302  table_.keywordSet().get("AntennaName", sdh.antennaname);
303  table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
304  table_.keywordSet().get("Equinox", sdh.equinox);
305  table_.keywordSet().get("FreqRefFrame", sdh.freqref);
306  table_.keywordSet().get("FreqRefVal", sdh.reffreq);
307  table_.keywordSet().get("Bandwidth", sdh.bandwidth);
308  table_.keywordSet().get("UTC", sdh.utc);
[206]309  table_.keywordSet().get("FluxUnit", sdh.fluxunit);
310  table_.keywordSet().get("Epoch", sdh.epoch);
[21]311  return sdh;
[18]312}
[805]313
[845]314bool Scantable::conformant( const Scantable& other )
315{
316  return this->getHeader().conformant(other.getHeader());
317}
318
319
[837]320int Scantable::nscan() const {
[805]321  int n = 0;
322  int previous = -1; int current = 0;
[322]323  for (uInt i=0; i< scanCol_.nrow();i++) {
324    scanCol_.getScalar(i,current);
[50]325    if (previous != current) {
[89]326      previous = current;
[50]327      n++;
328    }
329  }
330  return n;
331}
332
[805]333std::string Scantable::formatSec(Double x) const
[206]334{
[105]335  Double xcop = x;
336  MVTime mvt(xcop/24./3600.);  // make days
[365]337
[105]338  if (x < 59.95)
[281]339    return  String("      ") + mvt.string(MVTime::TIME_CLEAN_NO_HM, 7)+"s";
[745]340  else if (x < 3599.95)
[281]341    return String("   ") + mvt.string(MVTime::TIME_CLEAN_NO_H,7)+" ";
342  else {
343    ostringstream oss;
344    oss << setw(2) << std::right << setprecision(1) << mvt.hour();
345    oss << ":" << mvt.string(MVTime::TIME_CLEAN_NO_H,7) << " ";
346    return String(oss);
[745]347  }
[105]348};
349
[805]350std::string Scantable::formatDirection(const MDirection& md) const
[281]351{
352  Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
353  Int prec = 7;
354
355  MVAngle mvLon(t[0]);
356  String sLon = mvLon.string(MVAngle::TIME,prec);
357  MVAngle mvLat(t[1]);
358  String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec);
[380]359  return sLon + String(" ") + sLat;
[281]360}
361
362
[805]363std::string Scantable::getFluxUnit() const
[206]364{
[847]365  return table_.keywordSet().asString("FluxUnit");
[206]366}
367
[805]368void Scantable::setFluxUnit(const std::string& unit)
[218]369{
370  String tmp(unit);
371  Unit tU(tmp);
372  if (tU==Unit("K") || tU==Unit("Jy")) {
373     table_.rwKeywordSet().define(String("FluxUnit"), tmp);
374  } else {
375     throw AipsError("Illegal unit - must be compatible with Jy or K");
376  }
377}
378
[805]379void Scantable::setInstrument(const std::string& name)
[236]380{
[805]381  bool throwIt = true;
[476]382  Instrument ins = SDAttr::convertInstrument(name, throwIt);
[236]383  String nameU(name);
384  nameU.upcase();
385  table_.rwKeywordSet().define(String("AntennaName"), nameU);
386}
387
[805]388MPosition Scantable::getAntennaPosition () const
389{
390  Vector<Double> antpos;
391  table_.keywordSet().get("AntennaPosition", antpos);
392  MVPosition mvpos(antpos(0),antpos(1),antpos(2));
393  return MPosition(mvpos);
394}
[281]395
[805]396void Scantable::makePersistent(const std::string& filename)
397{
398  String inname(filename);
399  Path path(inname);
400  inname = path.expandedName();
[859]401  //cout << table_.tableName() << endl;
402  //cout << freqTable_.table().tableName() << endl;
[805]403  table_.deepCopy(inname, Table::New);
[859]404  //Table t = Table(inname, Table::Update);
405  //cout << t.keywordSet().asTable("FREQUENCIES").tableName() << endl;
[805]406}
407
[837]408int Scantable::nbeam( int scanno ) const
[805]409{
410  if ( scanno < 0 ) {
411    Int n;
412    table_.keywordSet().get("nBeam",n);
413    return int(n);
[105]414  } else {
[805]415    // take the first POLNO,IFNO,CYCLENO as nbeam shouldn't vary with these
416    Table tab = table_(table_.col("SCANNO") == scanno
417                       && table_.col("POLNO") == 0
418                       && table_.col("IFNO") == 0
419                       && table_.col("CYCLENO") == 0 );
420    ROTableVector<uInt> v(tab, "BEAMNO");
421    return int(v.nelements());
[105]422  }
[805]423  return 0;
424}
[455]425
[837]426int Scantable::nif( int scanno ) const
[805]427{
428  if ( scanno < 0 ) {
429    Int n;
430    table_.keywordSet().get("nIF",n);
431    return int(n);
432  } else {
433    // take the first POLNO,BEAMNO,CYCLENO as nbeam shouldn't vary with these
434    Table tab = table_(table_.col("SCANNO") == scanno
435                       && table_.col("POLNO") == 0
436                       && table_.col("BEAMNO") == 0
437                       && table_.col("CYCLENO") == 0 );
438    ROTableVector<uInt> v(tab, "IFNO");
439    return int(v.nelements());
[2]440  }
[805]441  return 0;
442}
[321]443
[837]444int Scantable::npol( int scanno ) const
[805]445{
446  if ( scanno < 0 ) {
447    Int n;
448    table_.keywordSet().get("nPol",n);
449    return n;
450  } else {
451    // take the first POLNO,IFNO,CYCLENO as nbeam shouldn't vary with these
452    Table tab = table_(table_.col("SCANNO") == scanno
453                       && table_.col("IFNO") == 0
454                       && table_.col("BEAMNO") == 0
455                       && table_.col("CYCLENO") == 0 );
456    ROTableVector<uInt> v(tab, "POLNO");
457    return int(v.nelements());
[321]458  }
[805]459  return 0;
[2]460}
[805]461
[845]462int Scantable::ncycle( int scanno ) const
[206]463{
[805]464  if ( scanno < 0 ) {
[837]465    Block<String> cols(2);
466    cols[0] = "SCANNO";
467    cols[1] = "CYCLENO";
468    TableIterator it(table_, cols);
469    int n = 0;
470    while ( !it.pastEnd() ) {
471      ++n;
472    }
473    return n;
[805]474  } else {
475    // take the first POLNO,IFNO,CYCLENO as nbeam shouldn't vary with these
476    Table tab = table_(table_.col("SCANNO") == scanno
477                       && table_.col("BEAMNO") == 0
478                       && table_.col("IFNO") == 0
479                       && table_.col("POLNO") == 0 );
480    return int(tab.nrow());
481  }
482  return 0;
[18]483}
[455]484
485
[845]486int Scantable::nrow( int scanno ) const
[805]487{
[845]488  return int(table_.nrow());
489}
490
491int Scantable::nchan( int ifno ) const
492{
493  if ( ifno < 0 ) {
[805]494    Int n;
495    table_.keywordSet().get("nChan",n);
496    return int(n);
497  } else {
[845]498    // take the first SCANNO,POLNO,BEAMNO,CYCLENO as nbeam shouldn't vary with these
499    Table tab = table_(table_.col("SCANNO") == 0
[805]500                       && table_.col("IFNO") == ifno
501                       && table_.col("BEAMNO") == 0
502                       && table_.col("POLNO") == 0
503                       && table_.col("CYCLENO") == 0 );
504    ROArrayColumn<Float> v(tab, "SPECTRA");
[837]505    return v(0).nelements();
[805]506  }
507  return 0;
[18]508}
[455]509
[847]510
511int Scantable::getBeam(int whichrow) const
512{
513  return beamCol_(whichrow);
514}
515
516int Scantable::getIF(int whichrow) const
517{
518  return ifCol_(whichrow);
519}
520
521int Scantable::getPol(int whichrow) const
522{
523  return polCol_(whichrow);
524}
525
[805]526Table Scantable::getHistoryTable() const
[488]527{
528  return table_.keywordSet().asTable("HISTORY");
529}
530
[805]531void Scantable::appendToHistoryTable(const Table& otherHist)
[488]532{
533  Table t = table_.rwKeywordSet().asTable("HISTORY");
[805]534
535  addHistory(asap::SEPERATOR);
[488]536  TableCopy::copyRows(t, otherHist, t.nrow(), 0, otherHist.nrow());
[805]537  addHistory(asap::SEPERATOR);
[488]538}
539
[805]540void Scantable::addHistory(const std::string& hist)
[206]541{
[483]542  Table t = table_.rwKeywordSet().asTable("HISTORY");
[745]543  uInt nrow = t.nrow();
[483]544  t.addRow();
545  ScalarColumn<String> itemCol(t, "ITEM");
546  itemCol.put(nrow, hist);
[206]547}
548
[805]549std::vector<std::string> Scantable::getHistory() const
[206]550{
551  Vector<String> history;
[483]552  const Table& t = table_.keywordSet().asTable("HISTORY");
[745]553  uInt nrow = t.nrow();
[483]554  ROScalarColumn<String> itemCol(t, "ITEM");
555  std::vector<std::string> stlout;
556  String hist;
557  for (uInt i=0; i<nrow; ++i) {
558    itemCol.get(i, hist);
559    stlout.push_back(hist);
560  }
[206]561  return stlout;
562}
[488]563
[805]564std::string Scantable::formatTime(const MEpoch& me, bool showdate) const
565{
566  MVTime mvt(me.getValue());
567  if (showdate)
568    mvt.setFormat(MVTime::YMD);
569  else
570    mvt.setFormat(MVTime::TIME);
571  ostringstream oss;
572  oss << mvt;
573  return String(oss);
574}
[488]575
[805]576void Scantable::calculateAZEL()
577{
578  MPosition mp = getAntennaPosition();
579  MEpoch::ROScalarColumn timeCol(table_, "TIME");
580  ostringstream oss;
581  oss << "Computed azimuth/elevation using " << endl
582      << mp << endl;
583  for (uInt i=0; i<nrow(); ++i) {
584    MEpoch me = timeCol(i);
585    MDirection md = dirCol_(i);
586    dirCol_.get(i,md);
587    oss  << " Time: " << formatTime(me,False) << " Direction: " << formatDirection(md)
588         << endl << "     => ";
589    MeasFrame frame(mp, me);
590    Vector<Double> azel =
591        MDirection::Convert(md, MDirection::Ref(MDirection::AZEL,
592                                                frame)
593                            )().getAngle("rad").getValue();
594    azCol_.put(i,azel[0]);
595    elCol_.put(i,azel[1]);
596    oss << "azel: " << azel[0]/C::pi*180.0 << " "
597        << azel[1]/C::pi*180.0 << " (deg)" << endl;
[16]598  }
[805]599  pushLog(String(oss));
600}
[89]601
[805]602std::vector<bool> Scantable::getMask(int whichrow) const
603{
604  Vector<uChar> flags;
605  flagsCol_.get(uInt(whichrow), flags);
606  Vector<Bool> bflag(flags.shape());
607  convertArray(bflag, flags);
608  bflag = !bflag;
609  std::vector<bool> mask;
610  bflag.tovector(mask);
611  return mask;
612}
[89]613
[805]614std::vector<float> Scantable::getSpectrum(int whichrow) const
615{
616  Vector<Float> arr;
617  specCol_.get(whichrow, arr);
618  std::vector<float> out;
619  arr.tovector(out);
620  return out;
[89]621}
[212]622
[805]623String Scantable::generateName()
[745]624{
[805]625  return (File::newUniqueName("./","temp")).baseName();
[212]626}
627
[805]628const casa::Table& Scantable::table( ) const
[212]629{
[805]630  return table_;
[212]631}
632
[805]633casa::Table& Scantable::table( )
[386]634{
[805]635  return table_;
[386]636}
637
[805]638std::string Scantable::getPolarizationLabel(bool linear, bool stokes,
639                                            bool linpol, int polidx) const
[401]640{
[805]641  uInt idx = 0;
642  if (polidx >=0) idx = polidx;
643  return "";
644  //return SDPolUtil::polarizationLabel(idx, linear, stokes, linpol);
[401]645}
[386]646
[805]647void Scantable::unsetSelection()
[380]648{
[805]649  table_ = originalTable_;
[847]650  attach();
[805]651  selector_.reset();
[380]652}
[386]653
[805]654void Scantable::setSelection( const STSelector& selection )
[430]655{
[805]656  Table tab = const_cast<STSelector&>(selection).apply(originalTable_);
657  if ( tab.nrow() == 0 ) {
658    throw(AipsError("Selection contains no data. Not applying it."));
659  }
660  table_ = tab;
[847]661  attach();
[805]662  selector_ = selection;
[430]663}
664
[837]665std::string Scantable::summary( bool verbose )
[447]666{
[805]667  // Format header info
668  ostringstream oss;
669  oss << endl;
670  oss << asap::SEPERATOR << endl;
671  oss << " Scan Table Summary" << endl;
672  oss << asap::SEPERATOR << endl;
673  oss.flags(std::ios_base::left);
674  oss << setw(15) << "Beams:" << setw(4) << nbeam() << endl
675      << setw(15) << "IFs:" << setw(4) << nif() << endl
676      << setw(15) << "Polarisations:" << setw(4) << npol() << endl
677      << setw(15) << "Channels:"  << setw(4) << nchan() << endl;
678  oss << endl;
679  String tmp;
680  //table_.keywordSet().get("Observer", tmp);
681  oss << setw(15) << "Observer:" << table_.keywordSet().asString("Observer") << endl;
682  oss << setw(15) << "Obs Date:" << getTime(-1,true) << endl;
683  table_.keywordSet().get("Project", tmp);
684  oss << setw(15) << "Project:" << tmp << endl;
685  table_.keywordSet().get("Obstype", tmp);
686  oss << setw(15) << "Obs. Type:" << tmp << endl;
687  table_.keywordSet().get("AntennaName", tmp);
688  oss << setw(15) << "Antenna Name:" << tmp << endl;
689  table_.keywordSet().get("FluxUnit", tmp);
690  oss << setw(15) << "Flux Unit:" << tmp << endl;
691  Vector<Float> vec;
692  oss << setw(15) << "Rest Freqs:";
693  if (vec.nelements() > 0) {
694      oss << setprecision(10) << vec << " [Hz]" << endl;
695  } else {
696      oss << "none" << endl;
697  }
698  oss << setw(15) << "Abcissa:" << "channel" << endl;
699  oss << selector_.print() << endl;
700  oss << endl;
701  // main table
702  String dirtype = "Position ("
703                  + MDirection::showType(dirCol_.getMeasRef().getType())
704                  + ")";
705  oss << setw(5) << "Scan"
706      << setw(15) << "Source"
707//      << setw(24) << dirtype
708      << setw(10) << "Time"
709      << setw(18) << "Integration" << endl
710      << setw(5) << "" << setw(10) << "Beam" << dirtype << endl
711      << setw(15) << "" << setw(5) << "IF"
712      << setw(8) << "Frame" << setw(16)
713      << "RefVal" << setw(10) << "RefPix" << setw(12) << "Increment" <<endl;
714  oss << asap::SEPERATOR << endl;
715  TableIterator iter(table_, "SCANNO");
716  while (!iter.pastEnd()) {
717    Table subt = iter.table();
718    ROTableRow row(subt);
719    MEpoch::ROScalarColumn timeCol(subt,"TIME");
720    const TableRecord& rec = row.get(0);
721    oss << setw(4) << std::right << rec.asuInt("SCANNO")
722        << std::left << setw(1) << ""
723        << setw(15) << rec.asString("SRCNAME")
724        << setw(10) << formatTime(timeCol(0), false);
725    // count the cycles in the scan
726    TableIterator cyciter(subt, "CYCLENO");
727    int nint = 0;
728    while (!cyciter.pastEnd()) {
729      ++nint;
730      ++cyciter;
731    }
732    oss << setw(3) << std::right << nint  << setw(3) << " x " << std::left
733        << setw(6) <<  formatSec(rec.asFloat("INTERVAL")) << endl;
[447]734
[805]735    TableIterator biter(subt, "BEAMNO");
736    while (!biter.pastEnd()) {
737      Table bsubt = biter.table();
738      ROTableRow brow(bsubt);
739      MDirection::ROScalarColumn bdirCol(bsubt,"DIRECTION");
740      const TableRecord& brec = brow.get(0);
741      oss << setw(6) << "" <<  setw(10) << brec.asuInt("BEAMNO");
742      oss  << setw(24) << formatDirection(bdirCol(0)) << endl;
743      TableIterator iiter(bsubt, "IFNO");
744      while (!iiter.pastEnd()) {
745        Table isubt = iiter.table();
746        ROTableRow irow(isubt);
747        const TableRecord& irec = irow.get(0);
748        oss << std::right <<setw(8) << "" << std::left << irec.asuInt("IFNO");
749        oss << frequencies().print(irec.asuInt("FREQ_ID"));
[447]750
[805]751        ++iiter;
752      }
753      ++biter;
754    }
755    ++iter;
[447]756  }
[805]757  /// @todo implement verbose mode
758  return String(oss);
[447]759}
760
[805]761std::string Scantable::getTime(int whichrow, bool showdate) const
[777]762{
[805]763  MEpoch::ROScalarColumn timeCol(table_, "TIME");
764  MEpoch me;
765  if (whichrow > -1) {
766    me = timeCol(uInt(whichrow));
767  } else {
768    Double tm;
769    table_.keywordSet().get("UTC",tm);
770    me = MEpoch(MVEpoch(tm));
[777]771  }
[805]772  return formatTime(me, showdate);
[777]773}
[805]774
[847]775std::string Scantable::getAbcissaLabel( int whichrow ) const
776{
777  if ( whichrow > table_.nrow() ) throw(AipsError("Illegal ro number"));
778  const MPosition& mp = getAntennaPosition();
779  const MDirection& md = dirCol_(whichrow);
780  const MEpoch& me = timeCol_(whichrow);
781  const Double& rf = mmolidCol_(whichrow);
782  SpectralCoordinate spc =
783    freqTable_.getSpectralCoordinate(md, mp, me, rf, mfreqidCol_(whichrow));
784
785  String s = "Channel";
786  Unit u = Unit(freqTable_.getUnitString());
787  if (u == Unit("km/s")) {
788    s = CoordinateUtil::axisLabel(spc,0,True,True,True);
789  } else if (u == Unit("Hz")) {
790    Vector<String> wau(1);wau = u.getName();
791    spc.setWorldAxisUnits(wau);
792
793    s = CoordinateUtil::axisLabel(spc,0,True,True,False);
794  }
795  return s;
796
797}
798
799void asap::Scantable::setRestFrequencies( double rf, const std::string& unit )
800{
801  ///@todo lookup in line table
802  Unit u(unit);
803  Quantum<Double> urf(rf, u);
804  uInt id = moleculeTable_.addEntry(urf.getValue("Hz"), "", "");
805  TableVector<uInt> tabvec(table_, "MOLECULE_ID");
806  tabvec = id;
807}
808
809void asap::Scantable::setRestFrequencies( const std::string& name )
810{
811  throw(AipsError("setRestFrequencies( const std::string& name ) NYI"));
812  ///@todo implement
813}
814
[852]815std::vector< unsigned int > asap::Scantable::rownumbers( ) const
816{
817  std::vector<unsigned int> stlout;
818  Vector<uInt> vec = table_.rowNumbers();
819  vec.tovector(stlout);
820  return stlout;
821}
822
[805]823}//namespace asap
Note: See TracBrowser for help on using the repository browser.