source: branches/Release2.1.2/src/Scantable.cpp@ 1376

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

Fix for ticket #61 - changing of default polarisation. Done ticket #62 as far as possible. Tid data gets automatically set as 'circular'\nAlso add circular pol class which was missing so far.

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