source: trunk/src/Scantable.cpp@ 903

Last change on this file since 903 was 902, checked in by mar637, 19 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
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"
[878]58#include "STAttr.h"
[902]59#include "MathUtils.h"
[2]60
[125]61using namespace casa;
[2]62
[805]63namespace asap {
64
[896]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
[805]73Scantable::Scantable(Table::TableType ttype) :
[852]74 type_(ttype)
[206]75{
[896]76 initFactories();
[805]77 setupMainTable();
[852]78 freqTable_ = STFrequencies(*this);
[805]79 table_.rwKeywordSet().defineTable("FREQUENCIES", freqTable_.table());
[852]80 weatherTable_ = STWeather(*this);
[805]81 table_.rwKeywordSet().defineTable("WEATHER", weatherTable_.table());
[852]82 focusTable_ = STFocus(*this);
[805]83 table_.rwKeywordSet().defineTable("FOCUS", focusTable_.table());
[852]84 tcalTable_ = STTcal(*this);
[805]85 table_.rwKeywordSet().defineTable("TCAL", tcalTable_.table());
[852]86 moleculeTable_ = STMolecules(*this);
[805]87 table_.rwKeywordSet().defineTable("MOLECULES", moleculeTable_.table());
[860]88 historyTable_ = STHistory(*this);
89 table_.rwKeywordSet().defineTable("HISTORY", historyTable_.table());
[805]90 setupFitTable();
[852]91 fitTable_ = table_.keywordSet().asTable("FITS");
[805]92 originalTable_ = table_;
[322]93 attach();
[18]94}
[206]95
[805]96Scantable::Scantable(const std::string& name, Table::TableType ttype) :
[852]97 type_(ttype)
[206]98{
[896]99 initFactories();
[865]100 Table tab(name, Table::Update);
[499]101 Int version;
[483]102 tab.keywordSet().get("VERSION", version);
103 if (version != version_) {
104 throw(AipsError("Unsupported version of ASAP file."));
105 }
[805]106 if ( type_ == Table::Memory )
[852]107 table_ = tab.copyToMemoryTable(generateName());
[805]108 else
109 table_ = tab;
[859]110 attachSubtables();
[805]111 originalTable_ = table_;
[329]112 attach();
[2]113}
114
[805]115Scantable::Scantable( const Scantable& other, bool clear )
[206]116{
[805]117 // with or without data
[859]118 String newname = String(generateName());
[865]119 type_ = other.table_.tableType();
[859]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);
[16]126 } else {
[865]127 other.table_.deepCopy(newname, Table::New, False, Table::AipsrcEndian,
128 Bool(clear));
129 table_ = Table(newname, Table::Update);
[902]130 if ( clear ) copySubtables(other);
[865]131 table_.markForDelete();
132 }
[859]133
134 attachSubtables();
[805]135 originalTable_ = table_;
[322]136 attach();
[2]137}
138
[865]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
[859]154void Scantable::attachSubtables()
155{
156 freqTable_ = STFrequencies(table_);
157 focusTable_ = STFocus(table_);
158 weatherTable_ = STWeather(table_);
159 tcalTable_ = STTcal(table_);
160 moleculeTable_ = STMolecules(table_);
[860]161 historyTable_ = STHistory(table_);
[859]162}
163
[805]164Scantable::~Scantable()
[206]165{
[805]166 cout << "~Scantable() " << this << endl;
[2]167}
168
[805]169void Scantable::setupMainTable()
[206]170{
[805]171 TableDesc td("", "1", TableDesc::Scratch);
172 td.comment() = "An ASAP Scantable";
173 td.rwKeywordSet().define("VERSION", Int(version_));
[2]174
[805]175 // n Cycles
176 td.addColumn(ScalarColumnDesc<uInt>("SCANNO"));
177 // new index every nBeam x nIF x nPol
178 td.addColumn(ScalarColumnDesc<uInt>("CYCLENO"));
[2]179
[805]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"));
[138]184
[805]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"));
[80]189
[805]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);
[483]195
[805]196 td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
197
[2]198 td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
[805]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
[2]204 td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
205 td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
[89]206 td.addColumn(ArrayColumnDesc<Float>("TSYS"));
[805]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"));
[105]219 td.addColumn(ScalarColumnDesc<Float>("PARANGLE"));
220
[805]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
[418]229 // Now create Table SetUp from the description.
[859]230 SetupNewTable aNewTab(generateName(), td, Table::Scratch);
[852]231 table_ = Table(aNewTab, type_, 0);
[805]232 originalTable_ = table_;
[745]233
[805]234}
[418]235
[805]236void Scantable::setupFitTable()
[322]237{
[39]238 TableDesc td("", "1", TableDesc::Scratch);
[805]239 td.addColumn(ScalarColumnDesc<uInt>("FIT_ID"));
[455]240 td.addColumn(ArrayColumnDesc<String>("FUNCTIONS"));
241 td.addColumn(ArrayColumnDesc<Int>("COMPONENTS"));
242 td.addColumn(ArrayColumnDesc<Double>("PARAMETERS"));
243 td.addColumn(ArrayColumnDesc<Bool>("PARMASK"));
[465]244 td.addColumn(ArrayColumnDesc<String>("FRAMEINFO"));
[859]245 SetupNewTable aNewTab("fits", td, Table::Scratch);
[455]246 Table aTable(aNewTab, Table::Memory);
247 table_.rwKeywordSet().defineTable("FITS", aTable);
248}
249
[805]250void Scantable::attach()
[455]251{
[805]252 timeCol_.attach(table_, "TIME");
253 srcnCol_.attach(table_, "SRCNAME");
254 specCol_.attach(table_, "SPECTRA");
255 flagsCol_.attach(table_, "FLAGTRA");
[865]256 tsysCol_.attach(table_, "TSYS");
[805]257 cycleCol_.attach(table_,"CYCLENO");
258 scanCol_.attach(table_, "SCANNO");
259 beamCol_.attach(table_, "BEAMNO");
[847]260 ifCol_.attach(table_, "IFNO");
261 polCol_.attach(table_, "POLNO");
[805]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");
[455]269
[805]270 mfitidCol_.attach(table_,"FIT_ID");
[859]271 //fitidCol_.attach(fitTable_,"FIT_ID");
[465]272
[805]273 mfreqidCol_.attach(table_, "FREQ_ID");
[465]274
[805]275 mtcalidCol_.attach(table_, "TCAL_ID");
[465]276
[805]277 mfocusidCol_.attach(table_, "FOCUS_ID");
[455]278
[805]279 mmolidCol_.attach(table_, "MOLECULE_ID");
[455]280}
281
[901]282void Scantable::setHeader(const STHeader& sdh)
[206]283{
[18]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);
[206]298 table_.rwKeywordSet().define("FluxUnit", sdh.fluxunit);
299 table_.rwKeywordSet().define("Epoch", sdh.epoch);
[50]300}
[21]301
[901]302STHeader Scantable::getHeader() const
[206]303{
[901]304 STHeader sdh;
[21]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);
[206]319 table_.keywordSet().get("FluxUnit", sdh.fluxunit);
320 table_.keywordSet().get("Epoch", sdh.epoch);
[21]321 return sdh;
[18]322}
[805]323
[845]324bool Scantable::conformant( const Scantable& other )
325{
326 return this->getHeader().conformant(other.getHeader());
327}
328
329
[837]330int Scantable::nscan() const {
[805]331 int n = 0;
[902]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);
[50]337}
338
[805]339std::string Scantable::formatSec(Double x) const
[206]340{
[105]341 Double xcop = x;
342 MVTime mvt(xcop/24./3600.); // make days
[365]343
[105]344 if (x < 59.95)
[281]345 return String(" ") + mvt.string(MVTime::TIME_CLEAN_NO_HM, 7)+"s";
[745]346 else if (x < 3599.95)
[281]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);
[745]353 }
[105]354};
355
[805]356std::string Scantable::formatDirection(const MDirection& md) const
[281]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);
[380]365 return sLon + String(" ") + sLat;
[281]366}
367
368
[805]369std::string Scantable::getFluxUnit() const
[206]370{
[847]371 return table_.keywordSet().asString("FluxUnit");
[206]372}
373
[805]374void Scantable::setFluxUnit(const std::string& unit)
[218]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
[805]385void Scantable::setInstrument(const std::string& name)
[236]386{
[805]387 bool throwIt = true;
[878]388 Instrument ins = STAttr::convertInstrument(name, throwIt);
[236]389 String nameU(name);
390 nameU.upcase();
391 table_.rwKeywordSet().define(String("AntennaName"), nameU);
392}
393
[805]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}
[281]401
[805]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
[837]410int Scantable::nbeam( int scanno ) const
[805]411{
412 if ( scanno < 0 ) {
413 Int n;
414 table_.keywordSet().get("nBeam",n);
415 return int(n);
[105]416 } else {
[805]417 // take the first POLNO,IFNO,CYCLENO as nbeam shouldn't vary with these
[888]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");
[805]425 return int(v.nelements());
[105]426 }
[805]427 return 0;
428}
[455]429
[837]430int Scantable::nif( int scanno ) const
[805]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
[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("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");
[805]446 return int(v.nelements());
[2]447 }
[805]448 return 0;
449}
[321]450
[837]451int Scantable::npol( int scanno ) const
[805]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
[888]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");
[805]467 return int(v.nelements());
[321]468 }
[805]469 return 0;
[2]470}
[805]471
[845]472int Scantable::ncycle( int scanno ) const
[206]473{
[805]474 if ( scanno < 0 ) {
[837]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;
[902]482 ++it;
[837]483 }
484 return n;
[805]485 } else {
[888]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());
[805]494 }
495 return 0;
[18]496}
[455]497
498
[845]499int Scantable::nrow( int scanno ) const
[805]500{
[845]501 return int(table_.nrow());
502}
503
504int Scantable::nchan( int ifno ) const
505{
506 if ( ifno < 0 ) {
[805]507 Int n;
508 table_.keywordSet().get("nChan",n);
509 return int(n);
510 } else {
[845]511 // take the first SCANNO,POLNO,BEAMNO,CYCLENO as nbeam shouldn't vary with these
[888]512 Table t = table_(table_.col("IFNO") == ifno);
513 if ( t.nrow() == 0 ) return 0;
514 ROArrayColumn<Float> v(t, "SPECTRA");
[837]515 return v(0).nelements();
[805]516 }
517 return 0;
[18]518}
[455]519
[847]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
[805]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}
[488]547
[805]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;
[16]570 }
[805]571 pushLog(String(oss));
572}
[89]573
[865]574void Scantable::flag()
575{
576 if ( selector_.empty() )
577 throw(AipsError("Trying to flag whole scantable. Aborted."));
[896]578 TableVector<uChar> tvec(table_, "FLAGTRA");
579 uChar userflag = 1 << 7;
580 tvec = userflag;
[865]581}
582
[805]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}
[89]594
[896]595std::vector<float> Scantable::getSpectrum( int whichrow,
[902]596 const std::string& poltype ) const
[805]597{
[902]598 if ( whichrow < 0 || whichrow >= nrow() )
599 throw(AipsError("Illegal row number."));
[896]600 std::vector<float> out;
[805]601 Vector<Float> arr;
[896]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;
[902]613 focusTable_.getEntry(frot, fang, ftan, mfocusidCol_(row));
[896]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 }
[902]622 if ( arr.nelements() == 0 )
623 pushLog("Not enough polarisations present to do the conversion.");
[805]624 arr.tovector(out);
625 return out;
[89]626}
[212]627
[884]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() )
[896]635 throw AipsError("The spectrum has incorrect number of channels.");
[884]636 specCol_.put(whichrow, spectrum);
637}
638
639
[805]640String Scantable::generateName()
[745]641{
[805]642 return (File::newUniqueName("./","temp")).baseName();
[212]643}
644
[805]645const casa::Table& Scantable::table( ) const
[212]646{
[805]647 return table_;
[212]648}
649
[805]650casa::Table& Scantable::table( )
[386]651{
[805]652 return table_;
[386]653}
654
[896]655std::string Scantable::getPolType() const
656{
657 return table_.keywordSet().asString("POLTYPE");
658}
659
[805]660void Scantable::unsetSelection()
[380]661{
[805]662 table_ = originalTable_;
[847]663 attach();
[805]664 selector_.reset();
[380]665}
[386]666
[805]667void Scantable::setSelection( const STSelector& selection )
[430]668{
[805]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;
[847]674 attach();
[805]675 selector_ = selection;
[430]676}
677
[837]678std::string Scantable::summary( bool verbose )
[447]679{
[805]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
[896]689 << setw(15) << "Polarisations:" << setw(4) << npol()
690 << "(" << getPolType() << ")" << endl
[805]691 << setw(15) << "Channels:" << setw(4) << nchan() << endl;
692 oss << endl;
693 String tmp;
[860]694 oss << setw(15) << "Observer:"
695 << table_.keywordSet().asString("Observer") << endl;
[805]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;
[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);
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"));
[447]764
[805]765 ++iiter;
766 }
767 ++biter;
768 }
769 ++iter;
[447]770 }
[805]771 /// @todo implement verbose mode
772 return String(oss);
[447]773}
774
[805]775std::string Scantable::getTime(int whichrow, bool showdate) const
[777]776{
[805]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));
[777]785 }
[805]786 return formatTime(me, showdate);
[777]787}
[805]788
[865]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
[847]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
[852]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
[865]873
[896]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}
[865]886
[902]887std::vector< std::string > asap::Scantable::columnNames( ) const
888{
889 Vector<String> vec = table_.tableDesc().columnNames();
890 return mathutil::tovectorstring(vec);
891}
[896]892
[902]893} //namespace asap
Note: See TracBrowser for help on using the repository browser.