source: tags/asap2beta/src/Scantable.cpp@ 1141

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

fix to version number data type

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