source: trunk/src/Scantable.cpp@ 981

Last change on this file since 981 was 973, checked in by mar637, 19 years ago

removed debug info

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