source: trunk/src/Scantable.cpp @ 902

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

More work on polarisation. STPol and labelling

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