source: trunk/src/SDMemTable.cc@ 91

Last change on this file since 91 was 89, checked in by mar637, 20 years ago

changes summary to return std::string
comment out unused getMask function
rename a few functions to make it consistent nXXX()
added nCoordinates
added use and setting of internal restfrequencies
added setSpecturm to mdify a spectrum
gave the summary function a major overhaul
added flag function, to hard flag full spectra

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.2 KB
Line 
1//#---------------------------------------------------------------------------
2//# SDMemTable.cc: A MemoryTable container for single dish integrations
3//#---------------------------------------------------------------------------
4//# Copyright (C) 2004
5//# Malte Marquarding, ATNF
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
32
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>
40
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>
46
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>
53#include <casa/Quanta/MVTime.h>
54
55#include "SDMemTable.h"
56#include "SDContainer.h"
57
58using namespace asap;
59
60SDMemTable::SDMemTable() :
61 IFSel_(0),
62 beamSel_(0),
63 polSel_(0) {
64 setup();
65}
66SDMemTable::SDMemTable(const std::string& name) :
67 IFSel_(0),
68 beamSel_(0),
69 polSel_(0) {
70 Table tab(name);
71 table_ = tab.copyToMemoryTable("dummy");
72}
73
74SDMemTable::SDMemTable(const SDMemTable& other, Bool clear) {
75 this->IFSel_= other.IFSel_;
76 this->beamSel_= other.beamSel_;
77 this->polSel_= other.polSel_;
78 this->chanMask_ = other.chanMask_;
79 this->table_ = other.table_.copyToMemoryTable(String("dummy"));
80 // clear all rows()
81 if (clear) {
82 this->table_.removeRow(this->table_.rowNumbers());
83 } else {
84 this->IFSel_ = other.IFSel_;
85 this->beamSel_ = other.beamSel_;
86 this->polSel_ = other.polSel_;
87 }
88}
89
90SDMemTable::SDMemTable(const Table& tab, const std::string& exprs) :
91 IFSel_(0),
92 beamSel_(0),
93 polSel_(0) {
94 //cerr << exprs << endl;
95 Table t = tableCommand(exprs,tab);
96 if (t.nrow() == 0)
97 throw(AipsError("Query unsuccessful."));
98 table_ = t.copyToMemoryTable("dummy");
99}
100
101SDMemTable::~SDMemTable(){
102 cerr << "goodbye from SDMemTable @ " << this << endl;
103}
104
105SDMemTable SDMemTable::getScan(Int scanID) {
106 String cond("SELECT * from $1 WHERE SCANID == ");
107 cond += String::toString(scanID);
108 return SDMemTable(table_, cond);
109}
110
111SDMemTable SDMemTable::getSource(const std::string& source) {
112 String cond("SELECT * from $1 WHERE SRCNAME == ");
113 cond += source;
114 return SDMemTable(table_, cond);
115}
116
117void SDMemTable::setup() {
118 TableDesc td("", "1", TableDesc::Scratch);
119 td.comment() = "A SDMemTable";
120 td.addColumn(ScalarColumnDesc<Double>("TIME"));
121 td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
122 td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
123 td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
124 td.addColumn(ArrayColumnDesc<Float>("TSYS"));
125 td.addColumn(ScalarColumnDesc<Int>("SCANID"));
126 td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
127 td.addColumn(ArrayColumnDesc<uInt>("FREQID"));
128 td.addColumn(ArrayColumnDesc<Double>("DIRECTION"));
129 // Now create a new table from the description.
130
131 SetupNewTable aNewTab("dummy", td, Table::New);
132 table_ = Table(aNewTab, Table::Memory, 0);
133}
134
135std::string SDMemTable::getSourceName(Int whichRow) const {
136 ROScalarColumn<String> src(table_, "SRCNAME");
137 String name;
138 src.get(whichRow, name);
139 return name;
140}
141
142std::string SDMemTable::getTime(Int whichRow) const {
143 ROScalarColumn<Double> src(table_, "TIME");
144 Double tm;
145 src.get(whichRow, tm);
146 MVTime mvt(tm);
147 mvt.setFormat(MVTime::YMD);
148 ostringstream oss;
149 oss << mvt;
150 String str(oss);
151 return str;
152}
153double SDMemTable::getInterval(Int whichRow) const {
154 ROScalarColumn<Double> src(table_, "INTERVAL");
155 Double intval;
156 src.get(whichRow, intval);
157 return intval;
158}
159
160bool SDMemTable::setIF(Int whichIF) {
161 if ( whichIF >= 0 && whichIF < nIF()) {
162 IFSel_ = whichIF;
163 return true;
164 }
165 return false;
166}
167
168bool SDMemTable::setBeam(Int whichBeam) {
169 if ( whichBeam >= 0 && whichBeam < nBeam()) {
170 beamSel_ = whichBeam;
171 return true;
172 }
173 return false;
174}
175
176bool SDMemTable::setPol(Int whichPol) {
177 if ( whichPol >= 0 && whichPol < nPol()) {
178 polSel_ = whichPol;
179 return true;
180 }
181 return false;
182}
183
184bool SDMemTable::setMask(std::vector<int> whichChans) {
185 ROArrayColumn<uChar> spec(table_, "FLAGTRA");
186 std::vector<int>::iterator it;
187 uInt n = spec.shape(0)(3);
188 chanMask_.resize(n,true);
189 for (it = whichChans.begin(); it != whichChans.end(); ++it) {
190 if (*it < n)
191 chanMask_[*it] = false;
192 }
193 return true;
194}
195
196std::vector<bool> SDMemTable::getMask(Int whichRow) const {
197 std::vector<bool> mask;
198 ROArrayColumn<uChar> spec(table_, "FLAGTRA");
199 Array<uChar> arr;
200 spec.get(whichRow, arr);
201 ArrayAccessor<uChar, Axis<0> > aa0(arr);
202 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
203 ArrayAccessor<uChar, Axis<1> > aa1(aa0);
204 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
205 ArrayAccessor<uChar, Axis<2> > aa2(aa1);
206 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
207
208 Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
209
210 std::vector<bool> tmp;
211 tmp = chanMask_; // WHY the fxxx do I have to make a copy here
212 std::vector<bool>::iterator miter;
213 miter = tmp.begin();
214
215 for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
216 bool out =!static_cast<bool>(*i);
217 if (useUserMask) {
218 out = out && (*miter);
219 miter++;
220 }
221 mask.push_back(out);
222 }
223 return mask;
224}
225std::vector<float> SDMemTable::getSpectrum(Int whichRow) const {
226
227 std::vector<float> spectrum;
228 ROArrayColumn<Float> spec(table_, "SPECTRA");
229 Array<Float> arr;
230 spec.get(whichRow, arr);
231 ArrayAccessor<Float, Axis<0> > aa0(arr);
232 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
233 ArrayAccessor<Float, Axis<1> > aa1(aa0);
234 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
235 ArrayAccessor<Float, Axis<2> > aa2(aa1);
236 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
237 for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
238 spectrum.push_back(*i);
239 }
240 return spectrum;
241}
242
243std::vector<double> SDMemTable::getAbscissa(Int whichRow,
244 const std::string& whichUnit,
245 const std::string& whichFrame,
246 double restfreq) {
247 std::vector<double> absc(nChan());
248 Vector<Double> absc1(nChan());
249 indgen(absc1);
250 ROArrayColumn<uInt> fid(table_, "FREQID");
251 Vector<uInt> v;
252 fid.get(whichRow, v);
253 uInt specidx = v(IFSel_);
254 Unit u;
255 if (whichUnit == "") {
256 // get unit from table
257 } else {
258 u = String(whichUnit);
259 }
260 SpectralCoordinate spc = getCoordinate(specidx);
261 Table t = table_.keywordSet().asTable("FREQUENCIES");
262 String rf;
263 t.keywordSet().get("REFFRAME",rf);
264 MDirection::Types mdr;
265 MDirection::getType(mdr, rf);
266 ROArrayColumn<Double> dir(table_, "DIRECTION");
267 Array<Double> posit;
268 dir.get(whichRow,posit);
269 Vector<Double> wpos(2);
270 wpos[0] = posit(IPosition(2,beamSel_,0));
271 wpos[1] = posit(IPosition(2,beamSel_,1));
272 Quantum<Double> lon(wpos[0],Unit(String("rad")));
273 Quantum<Double> lat(wpos[1],Unit(String("rad")));
274 MDirection direct(lon, lat, mdr);
275 ROScalarColumn<Double> tme(table_, "TIME");
276 Double obstime;
277 tme.get(whichRow,obstime);
278 //Quantum<Double> tm(obstime, Unit(String("d")));
279 MVEpoch tm2(Quantum<Double>(obstime, Unit(String("d"))));
280 MEpoch epoch(tm2);
281
282 Vector<Double> antpos;
283 table_.keywordSet().get("AntennaPosition", antpos);
284 //MPosition pos;
285 MVPosition mvpos(antpos(0),antpos(1),antpos(2));
286 MPosition pos(mvpos);
287 //MeasTable::Observatory(pos, String("ATCA"));
288
289 MFrequency::Types mtype;
290 if (!MFrequency::getType(mtype, whichFrame)) {
291 cout << "Frequency type unknown assuming TOPO" << endl;
292 mtype = MFrequency::TOPO;
293 }
294 spc.setReferenceConversion(mtype,epoch,pos,direct);
295
296 if ( u == Unit("km/s") ) {
297 Vector<Double> rstf;
298 t.keywordSet().get("RESTFREQS",rstf);
299 if (rstf.nelements() > 0) {
300 cout << "converting to velocities"<< endl;
301 //spc.setRestFrequency(Double(restfreq));
302 spc.setVelocity(u.getName());
303 Vector<Double> wrld;
304 spc.pixelToVelocity(wrld,absc1);
305 std::vector<double>::iterator it;
306 uInt i = 0;
307 for (it = absc.begin(); it != absc.end(); ++it) {
308 (*it) = wrld[i];
309 i++;
310 }
311 }
312 } else if (u == Unit("Hz")) {
313 Vector<String> wau(1); wau = u.getName();
314 spc.setWorldAxisUnits(wau);
315 cout << " converting in frequency" << endl;
316 std::vector<double>::iterator it;
317 Double tmp;
318 uInt i = 0;
319 for (it = absc.begin(); it != absc.end(); ++it) {
320 spc.toWorld(tmp,absc1[i]);
321 (*it) = tmp;
322 i++;
323 }
324 }
325 return absc;
326}
327
328void SDMemTable::setSpectrum(std::vector<float> spectrum, int whichRow) {
329 ArrayColumn<Float> spec(table_, "SPECTRA");
330 Array<Float> arr;
331 spec.get(whichRow, arr);
332 if (spectrum.size() != arr.shape()(3)) {
333 throw(AipsError("Attempting to set spectrum with incorrect length."));
334 }
335
336 ArrayAccessor<Float, Axis<0> > aa0(arr);
337 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
338 ArrayAccessor<Float, Axis<1> > aa1(aa0);
339 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
340 ArrayAccessor<Float, Axis<2> > aa2(aa1);
341 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
342
343 std::vector<float>::iterator it = spectrum.begin();
344 for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
345 (*i) = Float(*it);
346 it++;
347 }
348 spec.put(whichRow, arr);
349}
350
351void SDMemTable::getSpectrum(Vector<Float>& spectrum, Int whichRow) {
352 ROArrayColumn<Float> spec(table_, "SPECTRA");
353 Array<Float> arr;
354 spec.get(whichRow, arr);
355 spectrum.resize(arr.shape()(3));
356 ArrayAccessor<Float, Axis<0> > aa0(arr);
357 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
358 ArrayAccessor<Float, Axis<1> > aa1(aa0);
359 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
360 ArrayAccessor<Float, Axis<2> > aa2(aa1);
361 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
362
363 ArrayAccessor<Float, Axis<0> > va(spectrum);
364 for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
365 (*va) = (*i);
366 va++;
367 }
368}
369/*
370void SDMemTable::getMask(Vector<Bool>& mask, Int whichRow) const {
371 ROArrayColumn<uChar> spec(table_, "FLAGTRA");
372 Array<uChar> arr;
373 spec.get(whichRow, arr);
374 mask.resize(arr.shape()(3));
375
376 ArrayAccessor<uChar, Axis<0> > aa0(arr);
377 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
378 ArrayAccessor<uChar, Axis<1> > aa1(aa0);
379 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
380 ArrayAccessor<uChar, Axis<2> > aa2(aa1);
381 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
382
383 Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
384
385 ArrayAccessor<Bool, Axis<0> > va(mask);
386 std::vector<bool> tmp;
387 tmp = chanMask_; // WHY the fxxx do I have to make a copy here. The
388 // iterator should work on chanMask_??
389 std::vector<bool>::iterator miter;
390 miter = tmp.begin();
391
392 for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
393 bool out =!static_cast<bool>(*i);
394 if (useUserMask) {
395 out = out && (*miter);
396 miter++;
397 }
398 (*va) = out;
399 va++;
400 }
401}
402*/
403MaskedArray<Float> SDMemTable::rowAsMaskedArray(uInt whichRow,
404 Bool useSelection) {
405 ROArrayColumn<Float> spec(table_, "SPECTRA");
406 Array<Float> arr;
407 ROArrayColumn<uChar> flag(table_, "FLAGTRA");
408 Array<uChar> farr;
409 spec.get(whichRow, arr);
410 flag.get(whichRow, farr);
411 Array<Bool> barr(farr.shape());convertArray(barr, farr);
412 MaskedArray<Float> marr;
413 if (useSelection) {
414 ArrayAccessor<Float, Axis<0> > aa0(arr);
415 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
416 ArrayAccessor<Float, Axis<1> > aa1(aa0);
417 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
418 ArrayAccessor<Float, Axis<2> > aa2(aa1);
419 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
420
421 ArrayAccessor<Bool, Axis<0> > baa0(barr);
422 baa0.reset(baa0.begin(uInt(beamSel_)));//go to beam
423 ArrayAccessor<Bool, Axis<1> > baa1(baa0);
424 baa1.reset(baa1.begin(uInt(IFSel_)));// go to IF
425 ArrayAccessor<Bool, Axis<2> > baa2(baa1);
426 baa2.reset(baa2.begin(uInt(polSel_)));// go to pol
427
428 Vector<Float> a(arr.shape()(3));
429 Vector<Bool> b(barr.shape()(3));
430 ArrayAccessor<Float, Axis<0> > a0(a);
431 ArrayAccessor<Bool, Axis<0> > b0(b);
432
433 ArrayAccessor<Bool, Axis<3> > j(baa2);
434 for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
435 (*a0) = (*i);
436 (*b0) = !(*j);
437 j++;
438 a0++;
439 b0++;
440 }
441 marr.setData(a,b);
442 } else {
443 marr.setData(arr,!barr);
444 }
445 return marr;
446}
447
448Float SDMemTable::getTsys(Int whichRow) const {
449 ROArrayColumn<Float> ts(table_, "TSYS");
450 Array<Float> arr;
451 ts.get(whichRow, arr);
452 Float out;
453 IPosition ip(arr.shape());
454 ip(0) = beamSel_;ip(1) = IFSel_;ip(2) = polSel_;ip(3)=0;
455 out = arr(ip);
456 return out;
457}
458
459SpectralCoordinate SDMemTable::getCoordinate(uInt whichIdx) const {
460
461 Table t = table_.keywordSet().asTable("FREQUENCIES");
462 if (whichIdx > t.nrow() ) {
463 cerr << "SDMemTable::getCoordinate - whichIdx out of range" << endl;
464 return SpectralCoordinate();
465 }
466
467 Double rp,rv,inc;
468 String rf;
469 Vector<Double> vec;
470 ROScalarColumn<Double> rpc(t, "REFPIX");
471 ROScalarColumn<Double> rvc(t, "REFVAL");
472 ROScalarColumn<Double> incc(t, "INCREMENT");
473 t.keywordSet().get("RESTFREQS",vec);
474 t.keywordSet().get("REFFRAME",rf);
475
476 MFrequency::Types mft;
477 if (!MFrequency::getType(mft, rf)) {
478 cerr << "Frequency type unknown assuming TOPO" << endl;
479 mft = MFrequency::TOPO;
480 }
481 rpc.get(whichIdx, rp);
482 rvc.get(whichIdx, rv);
483 incc.get(whichIdx, inc);
484 //cerr << "creating speccord from " << whichIdx << ": "
485 // << rp <<", " << rv << ", " << inc << ", " << mft <<endl;
486 SpectralCoordinate spec(mft,rv,inc,rp);
487 if (vec.nelements() > 0)
488 spec.setRestFrequencies(vec);
489 return spec;
490}
491
492Bool SDMemTable::setCoordinate(const SpectralCoordinate& speccord,
493 uInt whichIdx) {
494 Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
495 if (whichIdx > t.nrow() ) {
496 throw(AipsError("SDMemTable::setCoordinate - coord no out of range"));
497 }
498 ScalarColumn<Double> rpc(t, "REFPIX");
499 ScalarColumn<Double> rvc(t, "REFVAL");
500 ScalarColumn<Double> incc(t, "INCREMENT");
501
502 rpc.put(whichIdx, speccord.referencePixel()[0]);
503 rvc.put(whichIdx, speccord.referenceValue()[0]);
504 incc.put(whichIdx, speccord.increment()[0]);
505
506 return True;
507}
508
509Int SDMemTable::nCoordinates() const
510{
511 return table_.keywordSet().asTable("FREQUENCIES").nrow();
512}
513
514void SDMemTable::setRestFreqs(std::vector<double> freqs, const std::string& theunit)
515{
516 Vector<Double> tvec(freqs);
517 Quantum<Vector<Double> > q(tvec, String(theunit));
518 tvec.resize();
519 tvec = q.getValue("Hz");
520 Table t = table_.keywordSet().asTable("FREQUENCIES");
521 t.rwKeywordSet().define("RESTFREQS",tvec);
522
523}
524
525bool SDMemTable::putSDFreqTable(const SDFrequencyTable& sdft) {
526 TableDesc td("", "1", TableDesc::Scratch);
527 td.addColumn(ScalarColumnDesc<Double>("REFPIX"));
528 td.addColumn(ScalarColumnDesc<Double>("REFVAL"));
529 td.addColumn(ScalarColumnDesc<Double>("INCREMENT"));
530 SetupNewTable aNewTab("freqs", td, Table::New);
531 Table aTable (aNewTab, Table::Memory, sdft.length());
532 ScalarColumn<Double> sc0(aTable, "REFPIX");
533 ScalarColumn<Double> sc1(aTable, "REFVAL");
534 ScalarColumn<Double> sc2(aTable, "INCREMENT");
535 for (uInt i=0; i < sdft.length(); ++i) {
536 sc0.put(i,sdft.referencePixel(i));
537 sc1.put(i,sdft.referenceValue(i));
538 sc2.put(i,sdft.increment(i));
539 }
540 aTable.rwKeywordSet().define("REFFRAME", sdft.refFrame());
541 aTable.rwKeywordSet().define("EQUINOX", sdft.equinox());
542 aTable.rwKeywordSet().define("Unit", String("kms-1"));
543 Vector<Double> rfvec;
544 aTable.rwKeywordSet().define("RESTFREQS", rfvec);
545 table_.rwKeywordSet().defineTable ("FREQUENCIES", aTable);
546 return True;
547}
548
549SDFrequencyTable SDMemTable::getSDFreqTable() const {
550 SDFrequencyTable sdft;
551
552 return sdft;
553}
554
555bool SDMemTable::putSDContainer(const SDContainer& sdc) {
556 ScalarColumn<Double> mjd(table_, "TIME");
557 ScalarColumn<String> srcn(table_, "SRCNAME");
558 ArrayColumn<Float> spec(table_, "SPECTRA");
559 ArrayColumn<uChar> flags(table_, "FLAGTRA");
560 ArrayColumn<Float> ts(table_, "TSYS");
561 ScalarColumn<Int> scan(table_, "SCANID");
562 ScalarColumn<Double> integr(table_, "INTERVAL");
563 ArrayColumn<uInt> freqid(table_, "FREQID");
564 ArrayColumn<Double> dir(table_, "DIRECTION");
565
566 uInt rno = table_.nrow();
567 table_.addRow();
568
569 mjd.put(rno, sdc.timestamp);
570 srcn.put(rno, sdc.sourcename);
571 spec.put(rno, sdc.getSpectrum());
572 flags.put(rno, sdc.getFlags());
573 ts.put(rno, sdc.getTsys());
574 scan.put(rno, sdc.scanid);
575 integr.put(rno, sdc.interval);
576 freqid.put(rno, sdc.getFreqMap());
577 dir.put(rno, sdc.getDirection());
578
579 return true;
580}
581
582SDContainer SDMemTable::getSDContainer(uInt whichRow) const {
583 ROScalarColumn<Double> mjd(table_, "TIME");
584 ROScalarColumn<String> srcn(table_, "SRCNAME");
585 ROArrayColumn<Float> spec(table_, "SPECTRA");
586 ROArrayColumn<uChar> flags(table_, "FLAGTRA");
587 ROArrayColumn<Float> ts(table_, "TSYS");
588 ROScalarColumn<Int> scan(table_, "SCANID");
589 ROScalarColumn<Double> integr(table_, "INTERVAL");
590 ROArrayColumn<uInt> freqid(table_, "FREQID");
591 ROArrayColumn<Double> dir(table_, "DIRECTION");
592
593 SDContainer sdc(nBeam(),nIF(),nPol(),nChan());
594 mjd.get(whichRow, sdc.timestamp);
595 srcn.get(whichRow, sdc.sourcename);
596 integr.get(whichRow, sdc.interval);
597 scan.get(whichRow, sdc.scanid);
598 Array<Float> spectrum;
599 Array<Float> tsys;
600 Array<uChar> flagtrum;
601 Vector<uInt> fmap;
602 Array<Double> direction;
603 spec.get(whichRow, spectrum);
604 sdc.putSpectrum(spectrum);
605 flags.get(whichRow, flagtrum);
606 sdc.putFlags(flagtrum);
607 ts.get(whichRow, tsys);
608 sdc.putTsys(tsys);
609 freqid.get(whichRow, fmap);
610 sdc.putFreqMap(fmap);
611 dir.get(whichRow, direction);
612 sdc.putDirection(direction);
613 return sdc;
614}
615
616bool SDMemTable::putSDHeader(const SDHeader& sdh) {
617 table_.lock();
618 table_.rwKeywordSet().define("nIF", sdh.nif);
619 table_.rwKeywordSet().define("nBeam", sdh.nbeam);
620 table_.rwKeywordSet().define("nPol", sdh.npol);
621 table_.rwKeywordSet().define("nChan", sdh.nchan);
622 table_.rwKeywordSet().define("Observer", sdh.observer);
623 table_.rwKeywordSet().define("Project", sdh.project);
624 table_.rwKeywordSet().define("Obstype", sdh.obstype);
625 table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
626 table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
627 table_.rwKeywordSet().define("Equinox", sdh.equinox);
628 table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
629 table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
630 table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
631 table_.rwKeywordSet().define("UTC", sdh.utc);
632 table_.unlock();
633 return true;
634}
635
636SDHeader SDMemTable::getSDHeader() const {
637 SDHeader sdh;
638 table_.keywordSet().get("nBeam",sdh.nbeam);
639 table_.keywordSet().get("nIF",sdh.nif);
640 table_.keywordSet().get("nPol",sdh.npol);
641 table_.keywordSet().get("nChan",sdh.nchan);
642 table_.keywordSet().get("Observer", sdh.observer);
643 table_.keywordSet().get("Project", sdh.project);
644 table_.keywordSet().get("Obstype", sdh.obstype);
645 table_.keywordSet().get("AntennaName", sdh.antennaname);
646 table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
647 table_.keywordSet().get("Equinox", sdh.equinox);
648 table_.keywordSet().get("FreqRefFrame", sdh.freqref);
649 table_.keywordSet().get("FreqRefVal", sdh.reffreq);
650 table_.keywordSet().get("Bandwidth", sdh.bandwidth);
651 table_.keywordSet().get("UTC", sdh.utc);
652 return sdh;
653}
654void SDMemTable::makePersistent(const std::string& filename) {
655 table_.deepCopy(filename,Table::New);
656}
657
658Int SDMemTable::nScan() const {
659 Int n = 0;
660 ROScalarColumn<Int> scans(table_, "SCANID");
661 Int previous = -1;Int current=0;
662 for (uInt i=0; i< scans.nrow();i++) {
663 scans.getScalar(i,current);
664 if (previous != current) {
665 previous = current;
666 n++;
667 }
668 }
669 return n;
670}
671
672std::string SDMemTable::summary() const {
673 ROScalarColumn<Int> scans(table_, "SCANID");
674 ROScalarColumn<String> srcs(table_, "SRCNAME");
675 ostringstream oss;
676 oss << endl;
677 oss << "--------------------------------------------------" << endl;
678 oss << " Scan Table Summary" << endl;
679 oss << "--------------------------------------------------" << endl;
680 oss.flags(std::ios_base::left);
681 //oss.width(15);
682 oss << setw(15) << "Beams:" << setw(4) << nBeam() << endl
683 << setw(15) << "IFs:" << setw(4) << nIF() << endl
684 << setw(15) << "Polarisations:" << setw(4) << nPol() << endl
685 << setw(15) << "Channels:" << setw(4) << nChan() << endl;
686 oss << endl;
687 String tmp;
688 table_.keywordSet().get("Observer", tmp);
689 oss << setw(15) << "Observer:" << tmp << endl;
690 table_.keywordSet().get("Project", tmp);
691 oss << setw(15) << "Project:" << tmp << endl;
692 table_.keywordSet().get("Obstype", tmp);
693 oss << setw(15) << "Obs. Type:" << tmp << endl;
694 table_.keywordSet().get("AntennaName", tmp);
695 oss << setw(15) << "Antenna Name:" << tmp << endl;
696 oss << endl;
697 uInt count = 0;
698 String name;
699 Int previous = -1;Int current=0;
700 Int integ = 0;
701 oss << setw(6) << "Scan"
702 << setw(12) << "Source"
703 << setw(21) << "Time"
704 << setw(11) << "Integration" << endl;
705 for (uInt i=0; i< scans.nrow();i++) {
706 scans.getScalar(i,current);
707 if (previous != current) {
708 srcs.getScalar(i,name);
709 previous = current;
710 Double t = getInterval(i);
711 String unit("s");
712 if (t/3600.0 > 1.0) {
713 t/=3600.0;unit = "h";
714 } else if (t/60.0 > 1.0) {
715 t/=60.0;unit = "m";
716 }
717 oss << setw(6) << count << setw(12) << name << setw(21) << getTime(i)
718 << setw(2) << setprecision(1) << setiosflags(std::ios_base::fixed)
719 << t << " " << unit << endl;
720 count++;
721 } else {
722 integ++;
723 }
724 }
725 oss << endl;
726 oss << "Table contains " << table_.nrow() << " integration(s)." << endl;
727 oss << "in " << count << " scan(s)." << endl;
728 oss << "--------------------------------------------------";
729 return String(oss);
730}
731
732Int SDMemTable::nBeam() const {
733 Int n;
734 table_.keywordSet().get("nBeam",n);
735 return n;
736}
737Int SDMemTable::nIF() const {
738 Int n;
739 table_.keywordSet().get("nIF",n);
740 return n;
741}
742Int SDMemTable::nPol() const {
743 Int n;
744 table_.keywordSet().get("nPol",n);
745 return n;
746}
747Int SDMemTable::nChan() const {
748 Int n;
749 table_.keywordSet().get("nChan",n);
750 return n;
751}
752/*
753void SDMemTable::maskChannels(const std::vector<Int>& whichChans ) {
754
755 std::vector<int>::iterator it;
756 ArrayAccessor<uChar, Axis<2> > j(flags_);
757 for (it = whichChans.begin(); it != whichChans.end(); it++) {
758 j.reset(j.begin(uInt(*it)));
759 for (ArrayAccessor<uChar, Axis<0> > i(j); i != i.end(); ++i) {
760 for (ArrayAccessor<uChar, Axis<1> > ii(i); ii != ii.end(); ++ii) {
761 for (ArrayAccessor<uChar, Axis<3> > iii(ii);
762 iii != iii.end(); ++iii) {
763 (*iii) =
764 }
765 }
766 }
767 }
768
769}
770*/
771void SDMemTable::flag(int whichRow) {
772 ArrayColumn<uChar> spec(table_, "FLAGTRA");
773 Array<uChar> arr;
774 spec.get(whichRow, arr);
775
776 ArrayAccessor<uChar, Axis<0> > aa0(arr);
777 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
778 ArrayAccessor<uChar, Axis<1> > aa1(aa0);
779 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
780 ArrayAccessor<uChar, Axis<2> > aa2(aa1);
781 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
782
783 for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
784 (*i) = uChar(True);
785 }
786
787 spec.put(whichRow, arr);
788}
Note: See TracBrowser for help on using the repository browser.