source: trunk/src/SDMemTable.cc@ 141

Last change on this file since 141 was 138, checked in by mar637, 20 years ago

Added assignment operator.
Changed const casa::Table& table() to be const
added SDMemTable* getPtr()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.2 KB
RevLine 
[2]1//#---------------------------------------------------------------------------
2//# SDMemTable.cc: A MemoryTable container for single dish integrations
3//#---------------------------------------------------------------------------
4//# Copyright (C) 2004
[125]5//# ATNF
[2]6//#
7//# This program is free software; you can redistribute it and/or modify it
8//# under the terms of the GNU General Public License as published by the Free
9//# Software Foundation; either version 2 of the License, or (at your option)
10//# any later version.
11//#
12//# This program is distributed in the hope that it will be useful, but
13//# WITHOUT ANY WARRANTY; without even the implied warranty of
14//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15//# Public License for more details.
16//#
17//# You should have received a copy of the GNU General Public License along
18//# with this program; if not, write to the Free Software Foundation, Inc.,
19//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20//#
21//# Correspondence concerning this software should be addressed as follows:
22//# Internet email: Malte.Marquarding@csiro.au
23//# Postal address: Malte Marquarding,
24//# Australia Telescope National Facility,
25//# P.O. Box 76,
26//# Epping, NSW, 2121,
27//# AUSTRALIA
28//#
29//# $Id:
30//#---------------------------------------------------------------------------
31
[125]32#include <casa/aips.h>
[80]33#include <casa/iostream.h>
34#include <casa/iomanip.h>
35#include <casa/Arrays/Array.h>
36#include <casa/Arrays/ArrayMath.h>
37#include <casa/Arrays/MaskArrMath.h>
38#include <casa/Arrays/ArrayLogical.h>
39#include <casa/Arrays/ArrayAccessor.h>
[2]40
[80]41#include <tables/Tables/TableParse.h>
42#include <tables/Tables/TableDesc.h>
43#include <tables/Tables/SetupNewTab.h>
44#include <tables/Tables/ScaColDesc.h>
45#include <tables/Tables/ArrColDesc.h>
[2]46
[80]47#include <tables/Tables/ExprNode.h>
48#include <tables/Tables/ScalarColumn.h>
49#include <tables/Tables/ArrayColumn.h>
50#include <tables/Tables/TableRecord.h>
51#include <measures/Measures/MFrequency.h>
52#include <measures/Measures/MeasTable.h>
[105]53#include <coordinates/Coordinates/CoordinateUtil.h>
[80]54#include <casa/Quanta/MVTime.h>
[2]55
56#include "SDMemTable.h"
57#include "SDContainer.h"
58
[125]59using namespace casa;
[83]60using namespace asap;
[2]61
[18]62SDMemTable::SDMemTable() :
63 IFSel_(0),
64 beamSel_(0),
65 polSel_(0) {
66 setup();
67}
[2]68SDMemTable::SDMemTable(const std::string& name) :
69 IFSel_(0),
70 beamSel_(0),
71 polSel_(0) {
[50]72 Table tab(name);
[22]73 table_ = tab.copyToMemoryTable("dummy");
[2]74}
75
[16]76SDMemTable::SDMemTable(const SDMemTable& other, Bool clear) {
[2]77 this->IFSel_= other.IFSel_;
78 this->beamSel_= other.beamSel_;
79 this->polSel_= other.polSel_;
80 this->chanMask_ = other.chanMask_;
81 this->table_ = other.table_.copyToMemoryTable(String("dummy"));
82 // clear all rows()
[16]83 if (clear) {
84 this->table_.removeRow(this->table_.rowNumbers());
85 } else {
86 this->IFSel_ = other.IFSel_;
87 this->beamSel_ = other.beamSel_;
88 this->polSel_ = other.polSel_;
89 }
[2]90}
91
[80]92SDMemTable::SDMemTable(const Table& tab, const std::string& exprs) :
[2]93 IFSel_(0),
94 beamSel_(0),
95 polSel_(0) {
96 Table t = tableCommand(exprs,tab);
[89]97 if (t.nrow() == 0)
98 throw(AipsError("Query unsuccessful."));
[22]99 table_ = t.copyToMemoryTable("dummy");
[2]100}
101
102SDMemTable::~SDMemTable(){
[105]103 //cerr << "goodbye from SDMemTable @ " << this << endl;
[2]104}
105
106SDMemTable SDMemTable::getScan(Int scanID) {
[80]107 String cond("SELECT * from $1 WHERE SCANID == ");
108 cond += String::toString(scanID);
109 return SDMemTable(table_, cond);
[2]110}
111
[138]112SDMemTable &SDMemTable::operator=(const SDMemTable& other) {
113 // reset "cursor"
114 IFSel_ = 0;
115 beamSel_ = 0;
116 polSel_ = 0;
117 table_ = other.table().copyToMemoryTable("dummy");
118 return *this;
119}
120
[80]121SDMemTable SDMemTable::getSource(const std::string& source) {
122 String cond("SELECT * from $1 WHERE SRCNAME == ");
123 cond += source;
124 return SDMemTable(table_, cond);
125}
126
[2]127void SDMemTable::setup() {
128 TableDesc td("", "1", TableDesc::Scratch);
129 td.comment() = "A SDMemTable";
130 td.addColumn(ScalarColumnDesc<Double>("TIME"));
131 td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
132 td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
133 td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
[89]134 td.addColumn(ArrayColumnDesc<Float>("TSYS"));
135 td.addColumn(ScalarColumnDesc<Int>("SCANID"));
136 td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
[39]137 td.addColumn(ArrayColumnDesc<uInt>("FREQID"));
[78]138 td.addColumn(ArrayColumnDesc<Double>("DIRECTION"));
[105]139 td.addColumn(ScalarColumnDesc<String>("FIELDNAME"));
140 td.addColumn(ScalarColumnDesc<String>("TCALTIME"));
141 td.addColumn(ArrayColumnDesc<Float>("TCAL"));
142 td.addColumn(ScalarColumnDesc<Float>("AZIMUTH"));
143 td.addColumn(ScalarColumnDesc<Float>("ELEVATION"));
144 td.addColumn(ScalarColumnDesc<Float>("PARANGLE"));
145 td.addColumn(ScalarColumnDesc<Int>("REFBEAM"));
146
[2]147 // Now create a new table from the description.
[18]148
[22]149 SetupNewTable aNewTab("dummy", td, Table::New);
[89]150 table_ = Table(aNewTab, Table::Memory, 0);
[2]151}
152
153std::string SDMemTable::getSourceName(Int whichRow) const {
154 ROScalarColumn<String> src(table_, "SRCNAME");
155 String name;
156 src.get(whichRow, name);
157 return name;
158}
159
[50]160std::string SDMemTable::getTime(Int whichRow) const {
[2]161 ROScalarColumn<Double> src(table_, "TIME");
162 Double tm;
163 src.get(whichRow, tm);
[50]164 MVTime mvt(tm);
[105]165 mvt.setFormat(MVTime::TIME);
[50]166 ostringstream oss;
167 oss << mvt;
168 String str(oss);
169 return str;
[2]170}
[50]171double SDMemTable::getInterval(Int whichRow) const {
172 ROScalarColumn<Double> src(table_, "INTERVAL");
173 Double intval;
174 src.get(whichRow, intval);
175 return intval;
176}
[2]177
178bool SDMemTable::setIF(Int whichIF) {
[50]179 if ( whichIF >= 0 && whichIF < nIF()) {
[2]180 IFSel_ = whichIF;
181 return true;
[50]182 }
183 return false;
[2]184}
[50]185
[2]186bool SDMemTable::setBeam(Int whichBeam) {
[50]187 if ( whichBeam >= 0 && whichBeam < nBeam()) {
[2]188 beamSel_ = whichBeam;
189 return true;
[50]190 }
191 return false;
192}
[2]193
194bool SDMemTable::setPol(Int whichPol) {
[50]195 if ( whichPol >= 0 && whichPol < nPol()) {
[2]196 polSel_ = whichPol;
197 return true;
[50]198 }
199 return false;
[2]200}
201
[68]202bool SDMemTable::setMask(std::vector<int> whichChans) {
[16]203 ROArrayColumn<uChar> spec(table_, "FLAGTRA");
204 std::vector<int>::iterator it;
205 uInt n = spec.shape(0)(3);
[105]206 if (whichChans.empty()) {
207 chanMask_ = std::vector<bool>(n,true);
208 return true;
209 }
[16]210 chanMask_.resize(n,true);
[39]211 for (it = whichChans.begin(); it != whichChans.end(); ++it) {
[105]212 if (*it < n) {
[16]213 chanMask_[*it] = false;
[105]214 }
[16]215 }
[2]216 return true;
217}
218
[16]219std::vector<bool> SDMemTable::getMask(Int whichRow) const {
220 std::vector<bool> mask;
221 ROArrayColumn<uChar> spec(table_, "FLAGTRA");
222 Array<uChar> arr;
223 spec.get(whichRow, arr);
224 ArrayAccessor<uChar, Axis<0> > aa0(arr);
225 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
226 ArrayAccessor<uChar, Axis<1> > aa1(aa0);
227 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
228 ArrayAccessor<uChar, Axis<2> > aa2(aa1);
229 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
230
231 Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
232
233 std::vector<bool> tmp;
234 tmp = chanMask_; // WHY the fxxx do I have to make a copy here
235 std::vector<bool>::iterator miter;
236 miter = tmp.begin();
237
238 for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
239 bool out =!static_cast<bool>(*i);
240 if (useUserMask) {
241 out = out && (*miter);
242 miter++;
243 }
244 mask.push_back(out);
[89]245 }
[16]246 return mask;
[2]247}
[50]248std::vector<float> SDMemTable::getSpectrum(Int whichRow) const {
[2]249
250 std::vector<float> spectrum;
251 ROArrayColumn<Float> spec(table_, "SPECTRA");
252 Array<Float> arr;
253 spec.get(whichRow, arr);
254 ArrayAccessor<Float, Axis<0> > aa0(arr);
255 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
256 ArrayAccessor<Float, Axis<1> > aa1(aa0);
257 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
[16]258 ArrayAccessor<Float, Axis<2> > aa2(aa1);
259 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
260 for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
261 spectrum.push_back(*i);
[2]262 }
263 return spectrum;
264}
[105]265std::vector<string> SDMemTable::getCoordInfo() const {
266 String un;
267 Table t = table_.keywordSet().asTable("FREQUENCIES");
268 String sunit;
269 t.keywordSet().get("UNIT",sunit);
270 String dpl;
271 t.keywordSet().get("DOPPLER",dpl);
272 if (dpl == "") dpl = "RADIO";
273 String rfrm;
274 t.keywordSet().get("REFFRAME",rfrm);
275 std::vector<string> inf;
276 inf.push_back(sunit);
277 inf.push_back(rfrm);
278 inf.push_back(dpl);
279 return inf;
280}
[39]281
[105]282void SDMemTable::setCoordInfo(std::vector<string> theinfo) {
283
284 std::vector<string>::iterator it;
285 String un,rfrm,dpl;
286 un = theinfo[0];
287 rfrm = theinfo[1];
288 dpl = theinfo[2];
289
290 //String un(theunit);
291 Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
292 Vector<Double> rstf;
293 t.keywordSet().get("RESTFREQS",rstf);
294 Bool canDo = True;
295 Unit u1("km/s");Unit u2("Hz");
296 if (Unit(un) == u1) {
297 Vector<Double> rstf;
298 t.keywordSet().get("RESTFREQS",rstf);
299 if (rstf.nelements() == 0) {
300 throw(AipsError("Can't set unit to km/s if no restfrequencies are specified"));
301 }
302 } else if (Unit(un) != u2 && un != "") {
303 throw(AipsError("Unit not conformant with Spectral Coordinates"));
304 }
305 t.rwKeywordSet().define("UNIT", un);
306
307 MFrequency::Types mdr;
308 if (!MFrequency::getType(mdr, rfrm)) {
309
310 Int a,b;const uInt* c;
311 const String* valid = MFrequency::allMyTypes(a, b, c);
312 String pfix = "Please specify a legal frame type. Types are\n";
313 throw(AipsError(pfix+(*valid)));
314 } else {
315 t.rwKeywordSet().define("REFFRAME",rfrm);
316 }
317
318}
319
320std::vector<double> SDMemTable::getAbscissa(Int whichRow) {
[39]321 std::vector<double> absc(nChan());
[89]322 Vector<Double> absc1(nChan());
[39]323 indgen(absc1);
324 ROArrayColumn<uInt> fid(table_, "FREQID");
325 Vector<uInt> v;
326 fid.get(whichRow, v);
327 uInt specidx = v(IFSel_);
328 SpectralCoordinate spc = getCoordinate(specidx);
[78]329 Table t = table_.keywordSet().asTable("FREQUENCIES");
330 String rf;
[105]331 //t.keywordSet().get("EQUINOX",rf);
[78]332 MDirection::Types mdr;
[105]333 //if (!MDirection::getType(mdr, rf)) {
334 mdr = MDirection::J2000;
335 //cout << "Unknown equinox using J2000" << endl;
336 //}
[78]337 ROArrayColumn<Double> dir(table_, "DIRECTION");
338 Array<Double> posit;
339 dir.get(whichRow,posit);
340 Vector<Double> wpos(2);
341 wpos[0] = posit(IPosition(2,beamSel_,0));
342 wpos[1] = posit(IPosition(2,beamSel_,1));
343 Quantum<Double> lon(wpos[0],Unit(String("rad")));
344 Quantum<Double> lat(wpos[1],Unit(String("rad")));
345 MDirection direct(lon, lat, mdr);
346 ROScalarColumn<Double> tme(table_, "TIME");
347 Double obstime;
348 tme.get(whichRow,obstime);
[89]349 MVEpoch tm2(Quantum<Double>(obstime, Unit(String("d"))));
[78]350 MEpoch epoch(tm2);
351
352 Vector<Double> antpos;
353 table_.keywordSet().get("AntennaPosition", antpos);
354 MVPosition mvpos(antpos(0),antpos(1),antpos(2));
355 MPosition pos(mvpos);
[105]356 String sunit;
357 t.keywordSet().get("UNIT",sunit);
358 if (sunit == "") sunit = "pixel";
359 Unit u(sunit);
360 String frm;
361 t.keywordSet().get("REFFRAME",frm);
362 if (frm == "") frm = "TOPO";
363 String dpl;
364 t.keywordSet().get("DOPPLER",dpl);
365 if (dpl == "") dpl = "RADIO";
[78]366 MFrequency::Types mtype;
[105]367 if (!MFrequency::getType(mtype, frm)) {
[89]368 cout << "Frequency type unknown assuming TOPO" << endl;
[78]369 mtype = MFrequency::TOPO;
370 }
[105]371
372 if (!spc.setReferenceConversion(mtype,epoch,pos,direct)) {
373 throw(AipsError("Couldn't convert frequency frame."));
374 }
[78]375
[39]376 if ( u == Unit("km/s") ) {
[89]377 Vector<Double> rstf;
378 t.keywordSet().get("RESTFREQS",rstf);
379 if (rstf.nelements() > 0) {
[121]380 if (rstf.nelements() >= nIF())
[125]381 spc.selectRestFrequency(uInt(IFSel_));
[89]382 spc.setVelocity(u.getName());
[39]383 Vector<Double> wrld;
384 spc.pixelToVelocity(wrld,absc1);
385 std::vector<double>::iterator it;
386 uInt i = 0;
387 for (it = absc.begin(); it != absc.end(); ++it) {
[89]388 (*it) = wrld[i];
389 i++;
[39]390 }
391 }
392 } else if (u == Unit("Hz")) {
393 Vector<String> wau(1); wau = u.getName();
394 spc.setWorldAxisUnits(wau);
395 std::vector<double>::iterator it;
396 Double tmp;
397 uInt i = 0;
[89]398 for (it = absc.begin(); it != absc.end(); ++it) {
[39]399 spc.toWorld(tmp,absc1[i]);
400 (*it) = tmp;
401 i++;
402 }
[105]403
404 } else {
405 // assume channels/pixels
406 std::vector<double>::iterator it;
407 uInt i=0;
408 for (it = absc.begin(); it != absc.end(); ++it) {
409 (*it) = Double(i++);
410 }
[39]411 }
412 return absc;
413}
414
[105]415std::string SDMemTable::getAbscissaString(Int whichRow)
416{
417 ROArrayColumn<uInt> fid(table_, "FREQID");
418 Table t = table_.keywordSet().asTable("FREQUENCIES");
419 String sunit;
420 t.keywordSet().get("UNIT",sunit);
421 if (sunit == "") sunit = "pixel";
422 Unit u(sunit);
423 Vector<uInt> v;
424 fid.get(whichRow, v);
425 uInt specidx = v(IFSel_);
426 SpectralCoordinate spc = getCoordinate(specidx);
427 String frm;
428 t.keywordSet().get("REFFRAME",frm);
429 MFrequency::Types mtype;
430 if (!MFrequency::getType(mtype, frm)) {
431 cout << "Frequency type unknown assuming TOPO" << endl;
432 mtype = MFrequency::TOPO;
433 }
434 spc.setFrequencySystem(mtype);
435 String s = "Channel";
436 if (u == Unit("km/s")) {
437 spc.setVelocity(u.getName());
438 s = CoordinateUtil::axisLabel(spc,0,True,True,True);
439 } else if (u == Unit("Hz")) {
440 Vector<String> wau(1);wau = u.getName();
441 spc.setWorldAxisUnits(wau);
442 s = CoordinateUtil::axisLabel(spc);
443 }
444 return s;
445}
446
[89]447void SDMemTable::setSpectrum(std::vector<float> spectrum, int whichRow) {
448 ArrayColumn<Float> spec(table_, "SPECTRA");
449 Array<Float> arr;
450 spec.get(whichRow, arr);
451 if (spectrum.size() != arr.shape()(3)) {
452 throw(AipsError("Attempting to set spectrum with incorrect length."));
453 }
454
455 ArrayAccessor<Float, Axis<0> > aa0(arr);
456 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
457 ArrayAccessor<Float, Axis<1> > aa1(aa0);
458 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
459 ArrayAccessor<Float, Axis<2> > aa2(aa1);
460 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
461
462 std::vector<float>::iterator it = spectrum.begin();
463 for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
464 (*i) = Float(*it);
465 it++;
466 }
467 spec.put(whichRow, arr);
468}
469
[68]470void SDMemTable::getSpectrum(Vector<Float>& spectrum, Int whichRow) {
[21]471 ROArrayColumn<Float> spec(table_, "SPECTRA");
472 Array<Float> arr;
473 spec.get(whichRow, arr);
474 spectrum.resize(arr.shape()(3));
475 ArrayAccessor<Float, Axis<0> > aa0(arr);
476 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
477 ArrayAccessor<Float, Axis<1> > aa1(aa0);
478 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
479 ArrayAccessor<Float, Axis<2> > aa2(aa1);
480 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
[2]481
[21]482 ArrayAccessor<Float, Axis<0> > va(spectrum);
483 for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
484 (*va) = (*i);
485 va++;
486 }
487}
[89]488/*
[68]489void SDMemTable::getMask(Vector<Bool>& mask, Int whichRow) const {
[21]490 ROArrayColumn<uChar> spec(table_, "FLAGTRA");
491 Array<uChar> arr;
492 spec.get(whichRow, arr);
493 mask.resize(arr.shape()(3));
494
495 ArrayAccessor<uChar, Axis<0> > aa0(arr);
496 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
497 ArrayAccessor<uChar, Axis<1> > aa1(aa0);
498 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
499 ArrayAccessor<uChar, Axis<2> > aa2(aa1);
500 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
501
502 Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
[89]503
[21]504 ArrayAccessor<Bool, Axis<0> > va(mask);
505 std::vector<bool> tmp;
506 tmp = chanMask_; // WHY the fxxx do I have to make a copy here. The
[89]507 // iterator should work on chanMask_??
[21]508 std::vector<bool>::iterator miter;
509 miter = tmp.begin();
510
511 for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
512 bool out =!static_cast<bool>(*i);
513 if (useUserMask) {
514 out = out && (*miter);
515 miter++;
516 }
517 (*va) = out;
518 va++;
[89]519 }
[21]520}
[89]521*/
[16]522MaskedArray<Float> SDMemTable::rowAsMaskedArray(uInt whichRow,
[89]523 Bool useSelection) {
[2]524 ROArrayColumn<Float> spec(table_, "SPECTRA");
525 Array<Float> arr;
526 ROArrayColumn<uChar> flag(table_, "FLAGTRA");
527 Array<uChar> farr;
528 spec.get(whichRow, arr);
529 flag.get(whichRow, farr);
530 Array<Bool> barr(farr.shape());convertArray(barr, farr);
531 MaskedArray<Float> marr;
[16]532 if (useSelection) {
533 ArrayAccessor<Float, Axis<0> > aa0(arr);
534 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
535 ArrayAccessor<Float, Axis<1> > aa1(aa0);
536 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
537 ArrayAccessor<Float, Axis<2> > aa2(aa1);
538 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
[89]539
[16]540 ArrayAccessor<Bool, Axis<0> > baa0(barr);
541 baa0.reset(baa0.begin(uInt(beamSel_)));//go to beam
542 ArrayAccessor<Bool, Axis<1> > baa1(baa0);
543 baa1.reset(baa1.begin(uInt(IFSel_)));// go to IF
544 ArrayAccessor<Bool, Axis<2> > baa2(baa1);
545 baa2.reset(baa2.begin(uInt(polSel_)));// go to pol
546
547 Vector<Float> a(arr.shape()(3));
548 Vector<Bool> b(barr.shape()(3));
549 ArrayAccessor<Float, Axis<0> > a0(a);
550 ArrayAccessor<Bool, Axis<0> > b0(b);
551
552 ArrayAccessor<Bool, Axis<3> > j(baa2);
553 for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
554 (*a0) = (*i);
555 (*b0) = !(*j);
556 j++;
557 a0++;
558 b0++;
559 }
560 marr.setData(a,b);
561 } else {
562 marr.setData(arr,!barr);
563 }
[2]564 return marr;
565}
566
567Float SDMemTable::getTsys(Int whichRow) const {
568 ROArrayColumn<Float> ts(table_, "TSYS");
569 Array<Float> arr;
570 ts.get(whichRow, arr);
571 Float out;
572 IPosition ip(arr.shape());
[16]573 ip(0) = beamSel_;ip(1) = IFSel_;ip(2) = polSel_;ip(3)=0;
[2]574 out = arr(ip);
575 return out;
576}
577
[39]578SpectralCoordinate SDMemTable::getCoordinate(uInt whichIdx) const {
579
580 Table t = table_.keywordSet().asTable("FREQUENCIES");
581 if (whichIdx > t.nrow() ) {
582 cerr << "SDMemTable::getCoordinate - whichIdx out of range" << endl;
[68]583 return SpectralCoordinate();
[39]584 }
[89]585
[39]586 Double rp,rv,inc;
587 String rf;
[89]588 Vector<Double> vec;
[39]589 ROScalarColumn<Double> rpc(t, "REFPIX");
590 ROScalarColumn<Double> rvc(t, "REFVAL");
591 ROScalarColumn<Double> incc(t, "INCREMENT");
[89]592 t.keywordSet().get("RESTFREQS",vec);
[105]593 t.keywordSet().get("BASEREFFRAME",rf);
[89]594
[39]595 MFrequency::Types mft;
596 if (!MFrequency::getType(mft, rf)) {
597 cerr << "Frequency type unknown assuming TOPO" << endl;
[89]598 mft = MFrequency::TOPO;
599 }
[39]600 rpc.get(whichIdx, rp);
601 rvc.get(whichIdx, rv);
602 incc.get(whichIdx, inc);
603 SpectralCoordinate spec(mft,rv,inc,rp);
[89]604 if (vec.nelements() > 0)
605 spec.setRestFrequencies(vec);
[39]606 return spec;
607}
608
[89]609Bool SDMemTable::setCoordinate(const SpectralCoordinate& speccord,
610 uInt whichIdx) {
[50]611 Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
612 if (whichIdx > t.nrow() ) {
[89]613 throw(AipsError("SDMemTable::setCoordinate - coord no out of range"));
[50]614 }
615 ScalarColumn<Double> rpc(t, "REFPIX");
616 ScalarColumn<Double> rvc(t, "REFVAL");
617 ScalarColumn<Double> incc(t, "INCREMENT");
[89]618
[50]619 rpc.put(whichIdx, speccord.referencePixel()[0]);
620 rvc.put(whichIdx, speccord.referenceValue()[0]);
621 incc.put(whichIdx, speccord.increment()[0]);
622
623 return True;
624}
625
[89]626Int SDMemTable::nCoordinates() const
627{
628 return table_.keywordSet().asTable("FREQUENCIES").nrow();
629}
[50]630
[89]631void SDMemTable::setRestFreqs(std::vector<double> freqs, const std::string& theunit)
632{
633 Vector<Double> tvec(freqs);
634 Quantum<Vector<Double> > q(tvec, String(theunit));
635 tvec.resize();
636 tvec = q.getValue("Hz");
637 Table t = table_.keywordSet().asTable("FREQUENCIES");
638 t.rwKeywordSet().define("RESTFREQS",tvec);
639}
640
[39]641bool SDMemTable::putSDFreqTable(const SDFrequencyTable& sdft) {
642 TableDesc td("", "1", TableDesc::Scratch);
643 td.addColumn(ScalarColumnDesc<Double>("REFPIX"));
644 td.addColumn(ScalarColumnDesc<Double>("REFVAL"));
645 td.addColumn(ScalarColumnDesc<Double>("INCREMENT"));
646 SetupNewTable aNewTab("freqs", td, Table::New);
647 Table aTable (aNewTab, Table::Memory, sdft.length());
648 ScalarColumn<Double> sc0(aTable, "REFPIX");
649 ScalarColumn<Double> sc1(aTable, "REFVAL");
650 ScalarColumn<Double> sc2(aTable, "INCREMENT");
651 for (uInt i=0; i < sdft.length(); ++i) {
652 sc0.put(i,sdft.referencePixel(i));
653 sc1.put(i,sdft.referenceValue(i));
654 sc2.put(i,sdft.increment(i));
655 }
[105]656 String rf = sdft.refFrame();
657 if (rf.contains("TOPO")) rf = "TOPO";
658
659 aTable.rwKeywordSet().define("BASEREFFRAME", rf);
660 aTable.rwKeywordSet().define("REFFRAME", rf);
[39]661 aTable.rwKeywordSet().define("EQUINOX", sdft.equinox());
[105]662 aTable.rwKeywordSet().define("UNIT", String(""));
663 aTable.rwKeywordSet().define("DOPPLER", String("RADIO"));
[89]664 Vector<Double> rfvec;
665 aTable.rwKeywordSet().define("RESTFREQS", rfvec);
[39]666 table_.rwKeywordSet().defineTable ("FREQUENCIES", aTable);
667 return True;
668}
669
670SDFrequencyTable SDMemTable::getSDFreqTable() const {
671 SDFrequencyTable sdft;
[89]672
[39]673 return sdft;
674}
675
[2]676bool SDMemTable::putSDContainer(const SDContainer& sdc) {
677 ScalarColumn<Double> mjd(table_, "TIME");
678 ScalarColumn<String> srcn(table_, "SRCNAME");
[105]679 ScalarColumn<String> fldn(table_, "FIELDNAME");
[2]680 ArrayColumn<Float> spec(table_, "SPECTRA");
681 ArrayColumn<uChar> flags(table_, "FLAGTRA");
682 ArrayColumn<Float> ts(table_, "TSYS");
683 ScalarColumn<Int> scan(table_, "SCANID");
[16]684 ScalarColumn<Double> integr(table_, "INTERVAL");
[39]685 ArrayColumn<uInt> freqid(table_, "FREQID");
[78]686 ArrayColumn<Double> dir(table_, "DIRECTION");
[105]687 ScalarColumn<Int> rbeam(table_, "REFBEAM");
688 ArrayColumn<Float> tcal(table_, "TCAL");
689 ScalarColumn<String> tcalt(table_, "TCALTIME");
690 ScalarColumn<Float> az(table_, "AZIMUTH");
691 ScalarColumn<Float> el(table_, "ELEVATION");
692 ScalarColumn<Float> para(table_, "PARANGLE");
[2]693
694 uInt rno = table_.nrow();
695 table_.addRow();
[89]696
[2]697 mjd.put(rno, sdc.timestamp);
698 srcn.put(rno, sdc.sourcename);
[105]699 fldn.put(rno, sdc.fieldname);
[2]700 spec.put(rno, sdc.getSpectrum());
701 flags.put(rno, sdc.getFlags());
702 ts.put(rno, sdc.getTsys());
703 scan.put(rno, sdc.scanid);
[16]704 integr.put(rno, sdc.interval);
[39]705 freqid.put(rno, sdc.getFreqMap());
[78]706 dir.put(rno, sdc.getDirection());
[105]707 rbeam.put(rno, sdc.refbeam);
708 tcal.put(rno, sdc.tcal);
709 tcalt.put(rno, sdc.tcaltime);
710 az.put(rno, sdc.azimuth);
711 el.put(rno, sdc.elevation);
712 para.put(rno, sdc.parangle);
[89]713
[2]714 return true;
715}
[18]716
[21]717SDContainer SDMemTable::getSDContainer(uInt whichRow) const {
718 ROScalarColumn<Double> mjd(table_, "TIME");
719 ROScalarColumn<String> srcn(table_, "SRCNAME");
[105]720 ROScalarColumn<String> fldn(table_, "FIELDNAME");
[21]721 ROArrayColumn<Float> spec(table_, "SPECTRA");
722 ROArrayColumn<uChar> flags(table_, "FLAGTRA");
723 ROArrayColumn<Float> ts(table_, "TSYS");
724 ROScalarColumn<Int> scan(table_, "SCANID");
725 ROScalarColumn<Double> integr(table_, "INTERVAL");
[39]726 ROArrayColumn<uInt> freqid(table_, "FREQID");
[78]727 ROArrayColumn<Double> dir(table_, "DIRECTION");
[105]728 ROScalarColumn<Int> rbeam(table_, "REFBEAM");
729 ROArrayColumn<Float> tcal(table_, "TCAL");
730 ROScalarColumn<String> tcalt(table_, "TCALTIME");
731 ROScalarColumn<Float> az(table_, "AZIMUTH");
732 ROScalarColumn<Float> el(table_, "ELEVATION");
733 ROScalarColumn<Float> para(table_, "PARANGLE");
[21]734
735 SDContainer sdc(nBeam(),nIF(),nPol(),nChan());
736 mjd.get(whichRow, sdc.timestamp);
737 srcn.get(whichRow, sdc.sourcename);
738 integr.get(whichRow, sdc.interval);
739 scan.get(whichRow, sdc.scanid);
[105]740 fldn.get(whichRow, sdc.fieldname);
741 rbeam.get(whichRow, sdc.refbeam);
742 az.get(whichRow, sdc.azimuth);
743 el.get(whichRow, sdc.elevation);
744 para.get(whichRow, sdc.parangle);
745 Vector<Float> tc;
746 tcal.get(whichRow, tc);
747 sdc.tcal[0] = tc[0];sdc.tcal[1] = tc[1];
748 tcalt.get(whichRow, sdc.tcaltime);
[21]749 Array<Float> spectrum;
750 Array<Float> tsys;
751 Array<uChar> flagtrum;
[39]752 Vector<uInt> fmap;
[78]753 Array<Double> direction;
[21]754 spec.get(whichRow, spectrum);
755 sdc.putSpectrum(spectrum);
756 flags.get(whichRow, flagtrum);
757 sdc.putFlags(flagtrum);
758 ts.get(whichRow, tsys);
759 sdc.putTsys(tsys);
[39]760 freqid.get(whichRow, fmap);
761 sdc.putFreqMap(fmap);
[78]762 dir.get(whichRow, direction);
763 sdc.putDirection(direction);
[21]764 return sdc;
765}
[78]766
[18]767bool SDMemTable::putSDHeader(const SDHeader& sdh) {
768 table_.lock();
769 table_.rwKeywordSet().define("nIF", sdh.nif);
770 table_.rwKeywordSet().define("nBeam", sdh.nbeam);
771 table_.rwKeywordSet().define("nPol", sdh.npol);
772 table_.rwKeywordSet().define("nChan", sdh.nchan);
773 table_.rwKeywordSet().define("Observer", sdh.observer);
774 table_.rwKeywordSet().define("Project", sdh.project);
775 table_.rwKeywordSet().define("Obstype", sdh.obstype);
776 table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
777 table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
778 table_.rwKeywordSet().define("Equinox", sdh.equinox);
779 table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
780 table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
781 table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
782 table_.rwKeywordSet().define("UTC", sdh.utc);
783 table_.unlock();
784 return true;
[50]785}
[21]786
787SDHeader SDMemTable::getSDHeader() const {
788 SDHeader sdh;
789 table_.keywordSet().get("nBeam",sdh.nbeam);
790 table_.keywordSet().get("nIF",sdh.nif);
791 table_.keywordSet().get("nPol",sdh.npol);
792 table_.keywordSet().get("nChan",sdh.nchan);
793 table_.keywordSet().get("Observer", sdh.observer);
794 table_.keywordSet().get("Project", sdh.project);
795 table_.keywordSet().get("Obstype", sdh.obstype);
796 table_.keywordSet().get("AntennaName", sdh.antennaname);
797 table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
798 table_.keywordSet().get("Equinox", sdh.equinox);
799 table_.keywordSet().get("FreqRefFrame", sdh.freqref);
800 table_.keywordSet().get("FreqRefVal", sdh.reffreq);
801 table_.keywordSet().get("Bandwidth", sdh.bandwidth);
802 table_.keywordSet().get("UTC", sdh.utc);
803 return sdh;
[18]804}
[2]805void SDMemTable::makePersistent(const std::string& filename) {
806 table_.deepCopy(filename,Table::New);
807}
808
[89]809Int SDMemTable::nScan() const {
[50]810 Int n = 0;
811 ROScalarColumn<Int> scans(table_, "SCANID");
812 Int previous = -1;Int current=0;
813 for (uInt i=0; i< scans.nrow();i++) {
814 scans.getScalar(i,current);
815 if (previous != current) {
[89]816 previous = current;
[50]817 n++;
818 }
819 }
820 return n;
821}
822
[105]823String SDMemTable::formatSec(Double x) {
824 Double xcop = x;
825 MVTime mvt(xcop/24./3600.); // make days
826 if (x < 59.95)
827 return String(" ") + mvt.string(MVTime::TIME_CLEAN_NO_HM, 7)+"s";
828 return mvt.string(MVTime::TIME_CLEAN_NO_H, 7)+" ";
829};
830
831std::string SDMemTable::summary() {
[2]832 ROScalarColumn<Int> scans(table_, "SCANID");
833 ROScalarColumn<String> srcs(table_, "SRCNAME");
[89]834 ostringstream oss;
835 oss << endl;
836 oss << "--------------------------------------------------" << endl;
837 oss << " Scan Table Summary" << endl;
838 oss << "--------------------------------------------------" << endl;
839 oss.flags(std::ios_base::left);
840 oss << setw(15) << "Beams:" << setw(4) << nBeam() << endl
841 << setw(15) << "IFs:" << setw(4) << nIF() << endl
842 << setw(15) << "Polarisations:" << setw(4) << nPol() << endl
843 << setw(15) << "Channels:" << setw(4) << nChan() << endl;
844 oss << endl;
845 String tmp;
846 table_.keywordSet().get("Observer", tmp);
847 oss << setw(15) << "Observer:" << tmp << endl;
848 table_.keywordSet().get("Project", tmp);
849 oss << setw(15) << "Project:" << tmp << endl;
850 table_.keywordSet().get("Obstype", tmp);
851 oss << setw(15) << "Obs. Type:" << tmp << endl;
852 table_.keywordSet().get("AntennaName", tmp);
853 oss << setw(15) << "Antenna Name:" << tmp << endl;
[105]854 Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
855 Vector<Double> vec;
856 t.keywordSet().get("RESTFREQS",vec);
857 oss << setw(15) << "Rest Freqs:";
858 if (vec.nelements() > 0) {
859 oss << setprecision(0) << vec << " [Hz]" << endl;
860 } else {
861 oss << "None set" << endl;
862 }
863 oss << setw(15) << "Abscissa:" << getAbscissaString() << endl;
864 oss << setw(15) << "Cursor:" << "Beam[" << getBeam() << "] "
865 << "IF[" << getIF() << "] " << "Pol[" << getPol() << "]" << endl;
[89]866 oss << endl;
[2]867 uInt count = 0;
868 String name;
869 Int previous = -1;Int current=0;
[89]870 Int integ = 0;
871 oss << setw(6) << "Scan"
872 << setw(12) << "Source"
873 << setw(21) << "Time"
874 << setw(11) << "Integration" << endl;
[105]875 oss << "--------------------------------------------------" << endl;
[2]876 for (uInt i=0; i< scans.nrow();i++) {
877 scans.getScalar(i,current);
878 if (previous != current) {
879 srcs.getScalar(i,name);
[50]880 previous = current;
[105]881 String t = formatSec(Double(getInterval(i)));
[89]882 oss << setw(6) << count << setw(12) << name << setw(21) << getTime(i)
[105]883 << setw(2) << setprecision(1)
884 << t << endl;
[50]885 count++;
[89]886 } else {
887 integ++;
[2]888 }
889 }
[89]890 oss << endl;
891 oss << "Table contains " << table_.nrow() << " integration(s)." << endl;
892 oss << "in " << count << " scan(s)." << endl;
893 oss << "--------------------------------------------------";
894 return String(oss);
[2]895}
[18]896
897Int SDMemTable::nBeam() const {
898 Int n;
899 table_.keywordSet().get("nBeam",n);
900 return n;
901}
902Int SDMemTable::nIF() const {
903 Int n;
904 table_.keywordSet().get("nIF",n);
905 return n;
906}
907Int SDMemTable::nPol() const {
908 Int n;
909 table_.keywordSet().get("nPol",n);
910 return n;
911}
912Int SDMemTable::nChan() const {
913 Int n;
914 table_.keywordSet().get("nChan",n);
915 return n;
916}
[16]917/*
[18]918void SDMemTable::maskChannels(const std::vector<Int>& whichChans ) {
[89]919
[16]920 std::vector<int>::iterator it;
921 ArrayAccessor<uChar, Axis<2> > j(flags_);
922 for (it = whichChans.begin(); it != whichChans.end(); it++) {
923 j.reset(j.begin(uInt(*it)));
924 for (ArrayAccessor<uChar, Axis<0> > i(j); i != i.end(); ++i) {
925 for (ArrayAccessor<uChar, Axis<1> > ii(i); ii != ii.end(); ++ii) {
[89]926 for (ArrayAccessor<uChar, Axis<3> > iii(ii);
927 iii != iii.end(); ++iii) {
928 (*iii) =
929 }
[16]930 }
931 }
932 }
[89]933
[16]934}
935*/
[89]936void SDMemTable::flag(int whichRow) {
937 ArrayColumn<uChar> spec(table_, "FLAGTRA");
938 Array<uChar> arr;
939 spec.get(whichRow, arr);
940
941 ArrayAccessor<uChar, Axis<0> > aa0(arr);
942 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
943 ArrayAccessor<uChar, Axis<1> > aa1(aa0);
944 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
945 ArrayAccessor<uChar, Axis<2> > aa2(aa1);
946 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
947
948 for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
949 (*i) = uChar(True);
950 }
951
952 spec.put(whichRow, arr);
953}
Note: See TracBrowser for help on using the repository browser.