source: trunk/src/Scantable.cpp@ 969

Last change on this file since 969 was 959, checked in by mar637, 19 years ago

c++ side of Ticket #7; update as requested in Ticket #8 and last revision

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