source: trunk/src/Scantable.cpp @ 987

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

added direction reference conversions as in Ticket #13

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.5 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>
[902]30#include <casa/Utilities/GenSort.h>
[2]31
[80]32#include <tables/Tables/TableParse.h>
33#include <tables/Tables/TableDesc.h>
[488]34#include <tables/Tables/TableCopy.h>
[80]35#include <tables/Tables/SetupNewTab.h>
36#include <tables/Tables/ScaColDesc.h>
37#include <tables/Tables/ArrColDesc.h>
[805]38#include <tables/Tables/TableRow.h>
39#include <tables/Tables/TableVector.h>
40#include <tables/Tables/TableIter.h>
[2]41
[80]42#include <tables/Tables/ExprNode.h>
43#include <tables/Tables/TableRecord.h>
44#include <measures/Measures/MFrequency.h>
[805]45#include <measures/Measures/MEpoch.h>
[80]46#include <measures/Measures/MeasTable.h>
[805]47#include <measures/Measures/MeasRef.h>
48#include <measures/TableMeasures/TableMeasRefDesc.h>
49#include <measures/TableMeasures/TableMeasValueDesc.h>
50#include <measures/TableMeasures/TableMeasDesc.h>
51#include <measures/TableMeasures/ScalarMeasColumn.h>
[105]52#include <coordinates/Coordinates/CoordinateUtil.h>
[80]53#include <casa/Quanta/MVTime.h>
[281]54#include <casa/Quanta/MVAngle.h>
[2]55
[805]56#include "Scantable.h"
[896]57#include "STPolLinear.h"
[913]58#include "STPolStokes.h"
[878]59#include "STAttr.h"
[902]60#include "MathUtils.h"
[2]61
[125]62using namespace casa;
[2]63
[805]64namespace asap {
65
[896]66std::map<std::string, STPol::STPolFactory *> Scantable::factories_;
67
68void Scantable::initFactories() {
69  if ( factories_.empty() ) {
70    Scantable::factories_["linear"] = &STPolLinear::myFactory;
[913]71    Scantable::factories_["stokes"] = &STPolStokes::myFactory;
[896]72  }
73}
74
[805]75Scantable::Scantable(Table::TableType ttype) :
[852]76  type_(ttype)
[206]77{
[896]78  initFactories();
[805]79  setupMainTable();
[852]80  freqTable_ = STFrequencies(*this);
[805]81  table_.rwKeywordSet().defineTable("FREQUENCIES", freqTable_.table());
[852]82  weatherTable_ = STWeather(*this);
[805]83  table_.rwKeywordSet().defineTable("WEATHER", weatherTable_.table());
[852]84  focusTable_ = STFocus(*this);
[805]85  table_.rwKeywordSet().defineTable("FOCUS", focusTable_.table());
[852]86  tcalTable_ = STTcal(*this);
[805]87  table_.rwKeywordSet().defineTable("TCAL", tcalTable_.table());
[852]88  moleculeTable_ = STMolecules(*this);
[805]89  table_.rwKeywordSet().defineTable("MOLECULES", moleculeTable_.table());
[860]90  historyTable_ = STHistory(*this);
91  table_.rwKeywordSet().defineTable("HISTORY", historyTable_.table());
[959]92  fitTable_ = STFit(*this);
93  table_.rwKeywordSet().defineTable("FIT", fitTable_.table());
[805]94  originalTable_ = table_;
[322]95  attach();
[18]96}
[206]97
[805]98Scantable::Scantable(const std::string& name, Table::TableType ttype) :
[852]99  type_(ttype)
[206]100{
[896]101  initFactories();
[865]102  Table tab(name, Table::Update);
[499]103  Int version;
[483]104  tab.keywordSet().get("VERSION", version);
105  if (version != version_) {
106    throw(AipsError("Unsupported version of ASAP file."));
107  }
[805]108  if ( type_ == Table::Memory )
[852]109    table_ = tab.copyToMemoryTable(generateName());
[805]110  else
111    table_ = tab;
[859]112  attachSubtables();
[805]113  originalTable_ = table_;
[329]114  attach();
[2]115}
116
[805]117Scantable::Scantable( const Scantable& other, bool clear )
[206]118{
[805]119  // with or without data
[859]120  String newname = String(generateName());
[865]121  type_ = other.table_.tableType();
[859]122  if ( other.table_.tableType() == Table::Memory ) {
123      if ( clear ) {
124        table_ = TableCopy::makeEmptyMemoryTable(newname,
125                                                 other.table_, True);
126      } else
127        table_ = other.table_.copyToMemoryTable(newname);
[16]128  } else {
[915]129      other.table_.deepCopy(newname, Table::New, False,
130                            other.table_.endianFormat(),
[865]131                            Bool(clear));
132      table_ = Table(newname, Table::Update);
133      table_.markForDelete();
134  }
[859]135
[915]136  if ( clear ) copySubtables(other);
[859]137  attachSubtables();
[805]138  originalTable_ = table_;
[322]139  attach();
[2]140}
141
[865]142void Scantable::copySubtables(const Scantable& other) {
143  Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
144  TableCopy::copyRows(t, other.freqTable_.table());
145  t = table_.rwKeywordSet().asTable("FOCUS");
146  TableCopy::copyRows(t, other.focusTable_.table());
147  t = table_.rwKeywordSet().asTable("WEATHER");
148  TableCopy::copyRows(t, other.weatherTable_.table());
149  t = table_.rwKeywordSet().asTable("TCAL");
150  TableCopy::copyRows(t, other.tcalTable_.table());
151  t = table_.rwKeywordSet().asTable("MOLECULES");
152  TableCopy::copyRows(t, other.moleculeTable_.table());
153  t = table_.rwKeywordSet().asTable("HISTORY");
154  TableCopy::copyRows(t, other.historyTable_.table());
[972]155  t = table_.rwKeywordSet().asTable("FIT");
156  TableCopy::copyRows(t, other.fitTable_.table());
[865]157}
158
[859]159void Scantable::attachSubtables()
160{
161  freqTable_ = STFrequencies(table_);
162  focusTable_ = STFocus(table_);
163  weatherTable_ = STWeather(table_);
164  tcalTable_ = STTcal(table_);
165  moleculeTable_ = STMolecules(table_);
[860]166  historyTable_ = STHistory(table_);
[972]167  fitTable_ = STFit(table_);
[859]168}
169
[805]170Scantable::~Scantable()
[206]171{
[941]172  //cout << "~Scantable() " << this << endl;
[2]173}
174
[805]175void Scantable::setupMainTable()
[206]176{
[805]177  TableDesc td("", "1", TableDesc::Scratch);
178  td.comment() = "An ASAP Scantable";
179  td.rwKeywordSet().define("VERSION", Int(version_));
[2]180
[805]181  // n Cycles
182  td.addColumn(ScalarColumnDesc<uInt>("SCANNO"));
183  // new index every nBeam x nIF x nPol
184  td.addColumn(ScalarColumnDesc<uInt>("CYCLENO"));
[2]185
[805]186  td.addColumn(ScalarColumnDesc<uInt>("BEAMNO"));
187  td.addColumn(ScalarColumnDesc<uInt>("IFNO"));
[972]188  // linear, circular, stokes
[805]189  td.rwKeywordSet().define("POLTYPE", String("linear"));
190  td.addColumn(ScalarColumnDesc<uInt>("POLNO"));
[138]191
[805]192  td.addColumn(ScalarColumnDesc<uInt>("FREQ_ID"));
193  td.addColumn(ScalarColumnDesc<uInt>("MOLECULE_ID"));
194  td.addColumn(ScalarColumnDesc<Int>("REFBEAMNO"));
[80]195
[805]196  td.addColumn(ScalarColumnDesc<Double>("TIME"));
197  TableMeasRefDesc measRef(MEpoch::UTC); // UTC as default
198  TableMeasValueDesc measVal(td, "TIME");
199  TableMeasDesc<MEpoch> mepochCol(measVal, measRef);
200  mepochCol.write(td);
[483]201
[805]202  td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
203
[2]204  td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
[805]205  // Type of source (on=0, off=1, other=-1)
206  td.addColumn(ScalarColumnDesc<Int>("SRCTYPE", Int(-1)));
207  td.addColumn(ScalarColumnDesc<String>("FIELDNAME"));
208
209  //The actual Data Vectors
[2]210  td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
211  td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
[89]212  td.addColumn(ArrayColumnDesc<Float>("TSYS"));
[805]213
214  td.addColumn(ArrayColumnDesc<Double>("DIRECTION",
215                                       IPosition(1,2),
216                                       ColumnDesc::Direct));
217  TableMeasRefDesc mdirRef(MDirection::J2000); // default
218  TableMeasValueDesc tmvdMDir(td, "DIRECTION");
219  // the TableMeasDesc gives the column a type
220  TableMeasDesc<MDirection> mdirCol(tmvdMDir, mdirRef);
[987]221  // a uder set table type e.g. GALCTIC, B1950 ...
222  td.rwKeywordSet().define("DIRECTIONREF", String("J2000"));
[805]223  // writing create the measure column
224  mdirCol.write(td);
[923]225  td.addColumn(ScalarColumnDesc<Float>("AZIMUTH"));
226  td.addColumn(ScalarColumnDesc<Float>("ELEVATION"));
[105]227  td.addColumn(ScalarColumnDesc<Float>("PARANGLE"));
228
[805]229  td.addColumn(ScalarColumnDesc<uInt>("TCAL_ID"));
[972]230  ScalarColumnDesc<Int> fitColumn("FIT_ID");
[973]231  fitColumn.setDefault(Int(-1));
[972]232  td.addColumn(fitColumn);
[805]233
234  td.addColumn(ScalarColumnDesc<uInt>("FOCUS_ID"));
235  td.addColumn(ScalarColumnDesc<uInt>("WEATHER_ID"));
236
237  td.rwKeywordSet().define("OBSMODE", String(""));
238
[418]239  // Now create Table SetUp from the description.
[859]240  SetupNewTable aNewTab(generateName(), td, Table::Scratch);
[852]241  table_ = Table(aNewTab, type_, 0);
[805]242  originalTable_ = table_;
243}
[418]244
[455]245
[805]246void Scantable::attach()
[455]247{
[805]248  timeCol_.attach(table_, "TIME");
249  srcnCol_.attach(table_, "SRCNAME");
250  specCol_.attach(table_, "SPECTRA");
251  flagsCol_.attach(table_, "FLAGTRA");
[865]252  tsysCol_.attach(table_, "TSYS");
[805]253  cycleCol_.attach(table_,"CYCLENO");
254  scanCol_.attach(table_, "SCANNO");
255  beamCol_.attach(table_, "BEAMNO");
[847]256  ifCol_.attach(table_, "IFNO");
257  polCol_.attach(table_, "POLNO");
[805]258  integrCol_.attach(table_, "INTERVAL");
259  azCol_.attach(table_, "AZIMUTH");
260  elCol_.attach(table_, "ELEVATION");
261  dirCol_.attach(table_, "DIRECTION");
262  paraCol_.attach(table_, "PARANGLE");
263  fldnCol_.attach(table_, "FIELDNAME");
264  rbeamCol_.attach(table_, "REFBEAMNO");
[455]265
[805]266  mfitidCol_.attach(table_,"FIT_ID");
267  mfreqidCol_.attach(table_, "FREQ_ID");
268  mtcalidCol_.attach(table_, "TCAL_ID");
269  mfocusidCol_.attach(table_, "FOCUS_ID");
270  mmolidCol_.attach(table_, "MOLECULE_ID");
[455]271}
272
[901]273void Scantable::setHeader(const STHeader& sdh)
[206]274{
[18]275  table_.rwKeywordSet().define("nIF", sdh.nif);
276  table_.rwKeywordSet().define("nBeam", sdh.nbeam);
277  table_.rwKeywordSet().define("nPol", sdh.npol);
278  table_.rwKeywordSet().define("nChan", sdh.nchan);
279  table_.rwKeywordSet().define("Observer", sdh.observer);
280  table_.rwKeywordSet().define("Project", sdh.project);
281  table_.rwKeywordSet().define("Obstype", sdh.obstype);
282  table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
283  table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
284  table_.rwKeywordSet().define("Equinox", sdh.equinox);
285  table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
286  table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
287  table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
288  table_.rwKeywordSet().define("UTC", sdh.utc);
[206]289  table_.rwKeywordSet().define("FluxUnit", sdh.fluxunit);
290  table_.rwKeywordSet().define("Epoch", sdh.epoch);
[905]291  table_.rwKeywordSet().define("POLTYPE", sdh.poltype);
[50]292}
[21]293
[901]294STHeader Scantable::getHeader() const
[206]295{
[901]296  STHeader sdh;
[21]297  table_.keywordSet().get("nBeam",sdh.nbeam);
298  table_.keywordSet().get("nIF",sdh.nif);
299  table_.keywordSet().get("nPol",sdh.npol);
300  table_.keywordSet().get("nChan",sdh.nchan);
301  table_.keywordSet().get("Observer", sdh.observer);
302  table_.keywordSet().get("Project", sdh.project);
303  table_.keywordSet().get("Obstype", sdh.obstype);
304  table_.keywordSet().get("AntennaName", sdh.antennaname);
305  table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
306  table_.keywordSet().get("Equinox", sdh.equinox);
307  table_.keywordSet().get("FreqRefFrame", sdh.freqref);
308  table_.keywordSet().get("FreqRefVal", sdh.reffreq);
309  table_.keywordSet().get("Bandwidth", sdh.bandwidth);
310  table_.keywordSet().get("UTC", sdh.utc);
[206]311  table_.keywordSet().get("FluxUnit", sdh.fluxunit);
312  table_.keywordSet().get("Epoch", sdh.epoch);
[905]313  table_.keywordSet().get("POLTYPE", sdh.poltype);
[21]314  return sdh;
[18]315}
[805]316
[845]317bool Scantable::conformant( const Scantable& other )
318{
319  return this->getHeader().conformant(other.getHeader());
320}
321
322
[837]323int Scantable::nscan() const {
[805]324  int n = 0;
[902]325  Int previous = -1; Int current = 0;
326  Vector<uInt> scannos(scanCol_.getColumn());
327  uInt nout = GenSort<uInt>::sort( scannos, Sort::Ascending,
328                       Sort::QuickSort|Sort::NoDuplicates );
329  return int(nout);
[50]330}
331
[805]332std::string Scantable::formatSec(Double x) const
[206]333{
[105]334  Double xcop = x;
335  MVTime mvt(xcop/24./3600.);  // make days
[365]336
[105]337  if (x < 59.95)
[281]338    return  String("      ") + mvt.string(MVTime::TIME_CLEAN_NO_HM, 7)+"s";
[745]339  else if (x < 3599.95)
[281]340    return String("   ") + mvt.string(MVTime::TIME_CLEAN_NO_H,7)+" ";
341  else {
342    ostringstream oss;
343    oss << setw(2) << std::right << setprecision(1) << mvt.hour();
344    oss << ":" << mvt.string(MVTime::TIME_CLEAN_NO_H,7) << " ";
345    return String(oss);
[745]346  }
[105]347};
348
[805]349std::string Scantable::formatDirection(const MDirection& md) const
[281]350{
351  Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
352  Int prec = 7;
353
354  MVAngle mvLon(t[0]);
355  String sLon = mvLon.string(MVAngle::TIME,prec);
[987]356  uInt tp = md.getRef().getType();
357  if (tp == MDirection::GALACTIC ||
358      tp == MDirection::SUPERGAL ) {
359    sLon = mvLon(0.0).string(MVAngle::ANGLE_CLEAN,prec);
360  }
[281]361  MVAngle mvLat(t[1]);
362  String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec);
[380]363  return sLon + String(" ") + sLat;
[281]364}
365
366
[805]367std::string Scantable::getFluxUnit() const
[206]368{
[847]369  return table_.keywordSet().asString("FluxUnit");
[206]370}
371
[805]372void Scantable::setFluxUnit(const std::string& unit)
[218]373{
374  String tmp(unit);
375  Unit tU(tmp);
376  if (tU==Unit("K") || tU==Unit("Jy")) {
377     table_.rwKeywordSet().define(String("FluxUnit"), tmp);
378  } else {
379     throw AipsError("Illegal unit - must be compatible with Jy or K");
380  }
381}
382
[805]383void Scantable::setInstrument(const std::string& name)
[236]384{
[805]385  bool throwIt = true;
[878]386  Instrument ins = STAttr::convertInstrument(name, throwIt);
[236]387  String nameU(name);
388  nameU.upcase();
389  table_.rwKeywordSet().define(String("AntennaName"), nameU);
390}
391
[805]392MPosition Scantable::getAntennaPosition () const
393{
394  Vector<Double> antpos;
395  table_.keywordSet().get("AntennaPosition", antpos);
396  MVPosition mvpos(antpos(0),antpos(1),antpos(2));
397  return MPosition(mvpos);
398}
[281]399
[805]400void Scantable::makePersistent(const std::string& filename)
401{
402  String inname(filename);
403  Path path(inname);
404  inname = path.expandedName();
405  table_.deepCopy(inname, Table::New);
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
[888]416    Table t = table_(table_.col("SCANNO") == scanno);
417    ROTableRow row(t);
418    const TableRecord& rec = row.get(0);
419    Table subt = t( t.col("IFNO") == Int(rec.asuInt("IFNO"))
420                    && t.col("POLNO") == Int(rec.asuInt("POLNO"))
421                    && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
422    ROTableVector<uInt> v(subt, "BEAMNO");
[805]423    return int(v.nelements());
[105]424  }
[805]425  return 0;
426}
[455]427
[837]428int Scantable::nif( int scanno ) const
[805]429{
430  if ( scanno < 0 ) {
431    Int n;
432    table_.keywordSet().get("nIF",n);
433    return int(n);
434  } else {
435    // take the first POLNO,BEAMNO,CYCLENO as nbeam shouldn't vary with these
[888]436    Table t = table_(table_.col("SCANNO") == scanno);
437    ROTableRow row(t);
438    const TableRecord& rec = row.get(0);
439    Table subt = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
440                    && t.col("POLNO") == Int(rec.asuInt("POLNO"))
441                    && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
442    if ( subt.nrow() == 0 ) return 0;
443    ROTableVector<uInt> v(subt, "IFNO");
[805]444    return int(v.nelements());
[2]445  }
[805]446  return 0;
447}
[321]448
[837]449int Scantable::npol( int scanno ) const
[805]450{
451  if ( scanno < 0 ) {
452    Int n;
453    table_.keywordSet().get("nPol",n);
454    return n;
455  } else {
456    // take the first POLNO,IFNO,CYCLENO as nbeam shouldn't vary with these
[888]457    Table t = table_(table_.col("SCANNO") == scanno);
458    ROTableRow row(t);
459    const TableRecord& rec = row.get(0);
460    Table subt = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
461                    && t.col("IFNO") == Int(rec.asuInt("IFNO"))
462                    && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
463    if ( subt.nrow() == 0 ) return 0;
464    ROTableVector<uInt> v(subt, "POLNO");
[805]465    return int(v.nelements());
[321]466  }
[805]467  return 0;
[2]468}
[805]469
[845]470int Scantable::ncycle( int scanno ) const
[206]471{
[805]472  if ( scanno < 0 ) {
[837]473    Block<String> cols(2);
474    cols[0] = "SCANNO";
475    cols[1] = "CYCLENO";
476    TableIterator it(table_, cols);
477    int n = 0;
478    while ( !it.pastEnd() ) {
479      ++n;
[902]480      ++it;
[837]481    }
482    return n;
[805]483  } else {
[888]484    Table t = table_(table_.col("SCANNO") == scanno);
485    ROTableRow row(t);
486    const TableRecord& rec = row.get(0);
487    Table subt = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
488                    && t.col("POLNO") == Int(rec.asuInt("POLNO"))
489                    && t.col("IFNO") == Int(rec.asuInt("IFNO")) );
490    if ( subt.nrow() == 0 ) return 0;
491    return int(subt.nrow());
[805]492  }
493  return 0;
[18]494}
[455]495
496
[845]497int Scantable::nrow( int scanno ) const
[805]498{
[845]499  return int(table_.nrow());
500}
501
502int Scantable::nchan( int ifno ) const
503{
504  if ( ifno < 0 ) {
[805]505    Int n;
506    table_.keywordSet().get("nChan",n);
507    return int(n);
508  } else {
[845]509    // take the first SCANNO,POLNO,BEAMNO,CYCLENO as nbeam shouldn't vary with these
[888]510    Table t = table_(table_.col("IFNO") == ifno);
511    if ( t.nrow() == 0 ) return 0;
512    ROArrayColumn<Float> v(t, "SPECTRA");
[923]513    return v.shape(0)(0);
[805]514  }
515  return 0;
[18]516}
[455]517
[923]518int Scantable::getChannels(int whichrow) const
519{
520  return specCol_.shape(whichrow)(0);
521}
[847]522
523int Scantable::getBeam(int whichrow) const
524{
525  return beamCol_(whichrow);
526}
527
528int Scantable::getIF(int whichrow) const
529{
530  return ifCol_(whichrow);
531}
532
533int Scantable::getPol(int whichrow) const
534{
535  return polCol_(whichrow);
536}
537
[805]538std::string Scantable::formatTime(const MEpoch& me, bool showdate) const
539{
540  MVTime mvt(me.getValue());
541  if (showdate)
542    mvt.setFormat(MVTime::YMD);
543  else
544    mvt.setFormat(MVTime::TIME);
545  ostringstream oss;
546  oss << mvt;
547  return String(oss);
548}
[488]549
[805]550void Scantable::calculateAZEL()
551{
552  MPosition mp = getAntennaPosition();
553  MEpoch::ROScalarColumn timeCol(table_, "TIME");
554  ostringstream oss;
555  oss << "Computed azimuth/elevation using " << endl
556      << mp << endl;
557  for (uInt i=0; i<nrow(); ++i) {
558    MEpoch me = timeCol(i);
[987]559    MDirection md = getDirection(i);
[805]560    oss  << " Time: " << formatTime(me,False) << " Direction: " << formatDirection(md)
561         << endl << "     => ";
562    MeasFrame frame(mp, me);
563    Vector<Double> azel =
564        MDirection::Convert(md, MDirection::Ref(MDirection::AZEL,
565                                                frame)
566                            )().getAngle("rad").getValue();
[923]567    azCol_.put(i,Float(azel[0]));
568    elCol_.put(i,Float(azel[1]));
[805]569    oss << "azel: " << azel[0]/C::pi*180.0 << " "
570        << azel[1]/C::pi*180.0 << " (deg)" << endl;
[16]571  }
[805]572  pushLog(String(oss));
573}
[89]574
[865]575void Scantable::flag()
576{
577  if ( selector_.empty() )
578    throw(AipsError("Trying to flag whole scantable. Aborted."));
[896]579  TableVector<uChar> tvec(table_, "FLAGTRA");
580  uChar userflag = 1 << 7;
581  tvec = userflag;
[865]582}
583
[805]584std::vector<bool> Scantable::getMask(int whichrow) const
585{
586  Vector<uChar> flags;
587  flagsCol_.get(uInt(whichrow), flags);
588  Vector<Bool> bflag(flags.shape());
589  convertArray(bflag, flags);
590  bflag = !bflag;
591  std::vector<bool> mask;
592  bflag.tovector(mask);
593  return mask;
594}
[89]595
[896]596std::vector<float> Scantable::getSpectrum( int whichrow,
[902]597                                           const std::string& poltype ) const
[805]598{
[905]599  String ptype = poltype;
600  if (poltype == "" ) ptype = getPolType();
[902]601  if ( whichrow  < 0 || whichrow >= nrow() )
602    throw(AipsError("Illegal row number."));
[896]603  std::vector<float> out;
[805]604  Vector<Float> arr;
[896]605  uInt requestedpol = polCol_(whichrow);
606  String basetype = getPolType();
[905]607  if ( ptype == basetype ) {
[896]608    specCol_.get(whichrow, arr);
609  } else {
610    STPol* stpol = 0;
611    stpol =STPol::getPolClass(Scantable::factories_, basetype);
612    try {
613      uInt row = uInt(whichrow);
614      stpol->setSpectra(getPolMatrix(row));
[959]615      Float fang,fhand,parang;
616      fang = focusTable_.getTotalFeedAngle(mfocusidCol_(row));
617      fhand = focusTable_.getFeedHand(mfocusidCol_(row));
618      parang = paraCol_(row);
619      /// @todo re-enable this
620      // disable total feed angle to support paralactifying Caswell style
621      stpol->setPhaseCorrections(parang, -parang, fhand);
[905]622      arr = stpol->getSpectrum(requestedpol, ptype);
[896]623      delete stpol;
624    } catch (AipsError& e) {
625      delete stpol;
626      throw(e);
627    }
628  }
[902]629  if ( arr.nelements() == 0 )
630    pushLog("Not enough polarisations present to do the conversion.");
[805]631  arr.tovector(out);
632  return out;
[89]633}
[212]634
[884]635void asap::Scantable::setSpectrum( const std::vector<float>& spec,
636                                   int whichrow )
637{
638  Vector<Float> spectrum(spec);
639  Vector<Float> arr;
640  specCol_.get(whichrow, arr);
641  if ( spectrum.nelements() != arr.nelements() )
[896]642    throw AipsError("The spectrum has incorrect number of channels.");
[884]643  specCol_.put(whichrow, spectrum);
644}
645
646
[805]647String Scantable::generateName()
[745]648{
[805]649  return (File::newUniqueName("./","temp")).baseName();
[212]650}
651
[805]652const casa::Table& Scantable::table( ) const
[212]653{
[805]654  return table_;
[212]655}
656
[805]657casa::Table& Scantable::table( )
[386]658{
[805]659  return table_;
[386]660}
661
[896]662std::string Scantable::getPolType() const
663{
664  return table_.keywordSet().asString("POLTYPE");
665}
666
[805]667void Scantable::unsetSelection()
[380]668{
[805]669  table_ = originalTable_;
[847]670  attach();
[805]671  selector_.reset();
[380]672}
[386]673
[805]674void Scantable::setSelection( const STSelector& selection )
[430]675{
[805]676  Table tab = const_cast<STSelector&>(selection).apply(originalTable_);
677  if ( tab.nrow() == 0 ) {
678    throw(AipsError("Selection contains no data. Not applying it."));
679  }
680  table_ = tab;
[847]681  attach();
[805]682  selector_ = selection;
[430]683}
684
[837]685std::string Scantable::summary( bool verbose )
[447]686{
[805]687  // Format header info
688  ostringstream oss;
689  oss << endl;
690  oss << asap::SEPERATOR << endl;
691  oss << " Scan Table Summary" << endl;
692  oss << asap::SEPERATOR << endl;
693  oss.flags(std::ios_base::left);
694  oss << setw(15) << "Beams:" << setw(4) << nbeam() << endl
695      << setw(15) << "IFs:" << setw(4) << nif() << endl
[896]696      << setw(15) << "Polarisations:" << setw(4) << npol()
697      << "(" << getPolType() << ")" << endl
[805]698      << setw(15) << "Channels:"  << setw(4) << nchan() << endl;
699  oss << endl;
700  String tmp;
[860]701  oss << setw(15) << "Observer:"
702      << table_.keywordSet().asString("Observer") << endl;
[805]703  oss << setw(15) << "Obs Date:" << getTime(-1,true) << endl;
704  table_.keywordSet().get("Project", tmp);
705  oss << setw(15) << "Project:" << tmp << endl;
706  table_.keywordSet().get("Obstype", tmp);
707  oss << setw(15) << "Obs. Type:" << tmp << endl;
708  table_.keywordSet().get("AntennaName", tmp);
709  oss << setw(15) << "Antenna Name:" << tmp << endl;
710  table_.keywordSet().get("FluxUnit", tmp);
711  oss << setw(15) << "Flux Unit:" << tmp << endl;
[941]712  Vector<Double> vec(moleculeTable_.getRestFrequencies());
[805]713  oss << setw(15) << "Rest Freqs:";
714  if (vec.nelements() > 0) {
715      oss << setprecision(10) << vec << " [Hz]" << endl;
716  } else {
717      oss << "none" << endl;
718  }
[941]719
720  oss << setw(15) << "Abcissa:" << getAbcissaLabel(0) << endl;
[805]721  oss << selector_.print() << endl;
722  oss << endl;
723  // main table
724  String dirtype = "Position ("
[987]725                  + getDirectionRefString()
[805]726                  + ")";
[941]727  oss << setw(5) << "Scan" << setw(15) << "Source"
728      << setw(10) << "Time" << setw(18) << "Integration" << endl;
729  oss << setw(5) << "" << setw(5) << "Beam" << setw(3) << "" << dirtype << endl;
730  oss << setw(10) << "" << setw(3) << "IF" << setw(6) << ""
[805]731      << setw(8) << "Frame" << setw(16)
732      << "RefVal" << setw(10) << "RefPix" << setw(12) << "Increment" <<endl;
733  oss << asap::SEPERATOR << endl;
734  TableIterator iter(table_, "SCANNO");
735  while (!iter.pastEnd()) {
736    Table subt = iter.table();
737    ROTableRow row(subt);
738    MEpoch::ROScalarColumn timeCol(subt,"TIME");
739    const TableRecord& rec = row.get(0);
740    oss << setw(4) << std::right << rec.asuInt("SCANNO")
741        << std::left << setw(1) << ""
742        << setw(15) << rec.asString("SRCNAME")
743        << setw(10) << formatTime(timeCol(0), false);
744    // count the cycles in the scan
745    TableIterator cyciter(subt, "CYCLENO");
746    int nint = 0;
747    while (!cyciter.pastEnd()) {
748      ++nint;
749      ++cyciter;
750    }
751    oss << setw(3) << std::right << nint  << setw(3) << " x " << std::left
752        << setw(6) <<  formatSec(rec.asFloat("INTERVAL")) << endl;
[447]753
[805]754    TableIterator biter(subt, "BEAMNO");
755    while (!biter.pastEnd()) {
756      Table bsubt = biter.table();
757      ROTableRow brow(bsubt);
758      const TableRecord& brec = brow.get(0);
[987]759      uInt row0 = bsubt.rowNumbers()[0];
[941]760      oss << setw(5) << "" <<  setw(4) << std::right << brec.asuInt("BEAMNO")<< std::left;
[987]761      oss  << setw(4) << ""  << formatDirection(getDirection(row0)) << endl;
[805]762      TableIterator iiter(bsubt, "IFNO");
763      while (!iiter.pastEnd()) {
764        Table isubt = iiter.table();
765        ROTableRow irow(isubt);
766        const TableRecord& irec = irow.get(0);
[941]767        oss << setw(10) << "";
768        oss << setw(3) << std::right << irec.asuInt("IFNO") << std::left
769            << setw(2) << "" << frequencies().print(irec.asuInt("FREQ_ID"));
[447]770
[805]771        ++iiter;
772      }
773      ++biter;
774    }
775    ++iter;
[447]776  }
[805]777  /// @todo implement verbose mode
778  return String(oss);
[447]779}
780
[805]781std::string Scantable::getTime(int whichrow, bool showdate) const
[777]782{
[805]783  MEpoch::ROScalarColumn timeCol(table_, "TIME");
784  MEpoch me;
785  if (whichrow > -1) {
786    me = timeCol(uInt(whichrow));
787  } else {
788    Double tm;
789    table_.keywordSet().get("UTC",tm);
790    me = MEpoch(MVEpoch(tm));
[777]791  }
[805]792  return formatTime(me, showdate);
[777]793}
[805]794
[865]795std::vector< double > asap::Scantable::getAbcissa( int whichrow ) const
796{
797  if ( whichrow > table_.nrow() ) throw(AipsError("Illegal ro number"));
798  std::vector<double> stlout;
799  int nchan = specCol_(whichrow).nelements();
800  String us = freqTable_.getUnitString();
801  if ( us == "" || us == "pixel" || us == "channel" ) {
802    for (int i=0; i<nchan; ++i) {
803      stlout.push_back(double(i));
804    }
805    return stlout;
806  }
807
808  const MPosition& mp = getAntennaPosition();
[987]809  const MDirection& md = getDirection(whichrow);
[865]810  const MEpoch& me = timeCol_(whichrow);
811  Double rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
812  SpectralCoordinate spc =
813    freqTable_.getSpectralCoordinate(md, mp, me, rf, mfreqidCol_(whichrow));
814  Vector<Double> pixel(nchan);
815  Vector<Double> world;
816  indgen(pixel);
817  if ( Unit(us) == Unit("Hz") ) {
818    for ( int i=0; i < nchan; ++i) {
819      Double world;
820      spc.toWorld(world, pixel[i]);
821      stlout.push_back(double(world));
822    }
823  } else if ( Unit(us) == Unit("km/s") ) {
824    Vector<Double> world;
825    spc.pixelToVelocity(world, pixel);
826    world.tovector(stlout);
827  }
828  return stlout;
829}
[987]830void asap::Scantable::setDirectionRefString( const std::string & refstr )
831{
832  MDirection::Types mdt;
833  if (refstr != "" && !MDirection::getType(mdt, refstr)) {
834    throw(AipsError("Illegal Direction frame."));
835  }
836  if ( refstr == "" ) {
837    String defaultstr = MDirection::showType(dirCol_.getMeasRef().getType());
838    table_.rwKeywordSet().define("DIRECTIONREF", defaultstr);
839  } else {
840    table_.rwKeywordSet().define("DIRECTIONREF", String(refstr));
841  }
842}
[865]843
[987]844std::string asap::Scantable::getDirectionRefString( ) const
845{
846  return table_.keywordSet().asString("DIRECTIONREF");
847}
848
849MDirection Scantable::getDirection(int whichrow ) const
850{
851  String usertype = table_.keywordSet().asString("DIRECTIONREF");
852  String type = MDirection::showType(dirCol_.getMeasRef().getType());
853  if ( usertype != type ) {
854    MDirection::Types mdt;
855    if (!MDirection::getType(mdt, usertype)) {
856      throw(AipsError("Illegal Direction frame."));
857    }
858    return dirCol_.convert(uInt(whichrow), mdt);
859  } else {
860    return dirCol_(uInt(whichrow));
861  }
862}
863
[847]864std::string Scantable::getAbcissaLabel( int whichrow ) const
865{
866  if ( whichrow > table_.nrow() ) throw(AipsError("Illegal ro number"));
867  const MPosition& mp = getAntennaPosition();
[987]868  const MDirection& md = getDirection(whichrow);
[847]869  const MEpoch& me = timeCol_(whichrow);
870  const Double& rf = mmolidCol_(whichrow);
871  SpectralCoordinate spc =
872    freqTable_.getSpectralCoordinate(md, mp, me, rf, mfreqidCol_(whichrow));
873
874  String s = "Channel";
875  Unit u = Unit(freqTable_.getUnitString());
876  if (u == Unit("km/s")) {
877    s = CoordinateUtil::axisLabel(spc,0,True,True,True);
878  } else if (u == Unit("Hz")) {
879    Vector<String> wau(1);wau = u.getName();
880    spc.setWorldAxisUnits(wau);
881    s = CoordinateUtil::axisLabel(spc,0,True,True,False);
882  }
883  return s;
884
885}
886
887void asap::Scantable::setRestFrequencies( double rf, const std::string& unit )
888{
[923]889  ///@todo lookup in line table to fill in name and formattedname
[847]890  Unit u(unit);
891  Quantum<Double> urf(rf, u);
892  uInt id = moleculeTable_.addEntry(urf.getValue("Hz"), "", "");
893  TableVector<uInt> tabvec(table_, "MOLECULE_ID");
894  tabvec = id;
895}
896
897void asap::Scantable::setRestFrequencies( const std::string& name )
898{
899  throw(AipsError("setRestFrequencies( const std::string& name ) NYI"));
900  ///@todo implement
901}
902
[852]903std::vector< unsigned int > asap::Scantable::rownumbers( ) const
904{
905  std::vector<unsigned int> stlout;
906  Vector<uInt> vec = table_.rowNumbers();
907  vec.tovector(stlout);
908  return stlout;
909}
910
[865]911
[896]912Matrix<Float> asap::Scantable::getPolMatrix( uInt whichrow ) const
913{
914  ROTableRow row(table_);
915  const TableRecord& rec = row.get(whichrow);
916  Table t =
917    originalTable_( originalTable_.col("SCANNO") == Int(rec.asuInt("SCANNO"))
918                    && originalTable_.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
919                    && originalTable_.col("IFNO") == Int(rec.asuInt("IFNO"))
920                    && originalTable_.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
921  ROArrayColumn<Float> speccol(t, "SPECTRA");
922  return speccol.getColumn();
923}
[865]924
[902]925std::vector< std::string > asap::Scantable::columnNames( ) const
926{
927  Vector<String> vec = table_.tableDesc().columnNames();
928  return mathutil::tovectorstring(vec);
929}
[896]930
[915]931casa::MEpoch::Types asap::Scantable::getTimeReference( ) const
932{
933  return MEpoch::castType(timeCol_.getMeasRef().getType());
[972]934}
[915]935
[972]936void asap::Scantable::addFit( const STFitEntry & fit, int row )
937{
938  cout << mfitidCol_(uInt(row)) << endl;
939  uInt id = fitTable_.addEntry(fit, mfitidCol_(uInt(row)));
940  mfitidCol_.put(uInt(row), id);
941}
[915]942
[987]943
944}
945 //namespace asap
Note: See TracBrowser for help on using the repository browser.