source: trunk/src/Scantable.cpp @ 1323

Last change on this file since 1323 was 1323, checked in by mar637, 17 years ago

Fix for Ticket #102; cut-and-paster error in initFactories.

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