source: trunk/src/SDMemTable.cc@ 33

Last change on this file since 33 was 22, checked in by mmarquar, 20 years ago

Removed irrelevantr member function "name". Added function to return number of rows in table.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 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 <aips/iostream.h>
34#include <aips/Arrays/Array.h>
35#include <aips/Arrays/ArrayMath.h>
36#include <aips/Arrays/MaskArrMath.h>
37#include <aips/Arrays/ArrayLogical.h>
38#include <aips/Arrays/ArrayAccessor.h>
39
40#include <aips/Tables/TableParse.h>
41#include <aips/Tables/TableDesc.h>
42#include <aips/Tables/SetupNewTab.h>
43#include <aips/Tables/ScaColDesc.h>
44#include <aips/Tables/ArrColDesc.h>
45
46#include <aips/Tables/ExprNode.h>
47#include <aips/Tables/ScalarColumn.h>
48#include <aips/Tables/ArrayColumn.h>
49#include <aips/Tables/TableRecord.h>
50
51
52#include "SDMemTable.h"
53#include "SDContainer.h"
54
55using namespace atnf_sd;
56
57SDMemTable::SDMemTable() :
58 IFSel_(0),
59 beamSel_(0),
60 polSel_(0) {
61 setup();
62}
63SDMemTable::SDMemTable(const std::string& name) :
64 IFSel_(0),
65 beamSel_(0),
66 polSel_(0) {
67 Table tab("dummy");
68 table_ = tab.copyToMemoryTable("dummy");
69}
70
71SDMemTable::SDMemTable(const SDMemTable& other, Bool clear) {
72 this->IFSel_= other.IFSel_;
73 this->beamSel_= other.beamSel_;
74 this->polSel_= other.polSel_;
75 this->chanMask_ = other.chanMask_;
76 this->table_ = other.table_.copyToMemoryTable(String("dummy"));
77 // clear all rows()
78 if (clear) {
79 this->table_.removeRow(this->table_.rowNumbers());
80 } else {
81 this->IFSel_ = other.IFSel_;
82 this->beamSel_ = other.beamSel_;
83 this->polSel_ = other.polSel_;
84 }
85}
86
87SDMemTable::SDMemTable(const Table& tab, Int scanID) :
88 IFSel_(0),
89 beamSel_(0),
90 polSel_(0) {
91 String exprs = String("select * from $1 where SCANID == ")
92 +String::toString(scanID);
93 cerr << exprs << endl;
94 Table t = tableCommand(exprs,tab);
95 table_ = t.copyToMemoryTable("dummy");
96}
97
98SDMemTable::~SDMemTable(){
99 cerr << "goodbye from SDMemTable @ " << this << endl;
100}
101
102SDMemTable SDMemTable::getScan(Int scanID) {
103 return SDMemTable(table_, scanID);
104}
105
106void SDMemTable::setup() {
107 TableDesc td("", "1", TableDesc::Scratch);
108 td.comment() = "A SDMemTable";
109 td.addColumn(ScalarColumnDesc<Double>("TIME"));
110 td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
111 td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
112 td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
113 td.addColumn(ArrayColumnDesc<Float>("TSYS"));
114 td.addColumn(ScalarColumnDesc<Int>("SCANID"));
115 td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
116 // Now create a new table from the description.
117
118 SetupNewTable aNewTab("dummy", td, Table::New);
119 table_ = Table(aNewTab, Table::Memory, 0);
120}
121
122std::string SDMemTable::getSourceName(Int whichRow) const {
123 ROScalarColumn<String> src(table_, "SRCNAME");
124 String name;
125 src.get(whichRow, name);
126 return name;
127}
128
129Double SDMemTable::getTime(Int whichRow) const {
130 ROScalarColumn<Double> src(table_, "TIME");
131 Double tm;
132 src.get(whichRow, tm);
133 return tm;
134}
135
136bool SDMemTable::setIF(Int whichIF) {
137 //if ( whichIF >= 0 && whichIF < nIF_) {
138 IFSel_ = whichIF;
139 return true;
140 //}
141 //return false;
142}
143bool SDMemTable::setBeam(Int whichBeam) {
144 //if ( whichBeam >= 0 && whichBeam < nBeam_) {
145 beamSel_ = whichBeam;
146 return true;
147 //}
148 //return false;
149
150}
151bool SDMemTable::setPol(Int whichPol) {
152 //if ( whichPol >= 0 && whichPol < nPol_) {
153 polSel_ = whichPol;
154 return true;
155 //}
156 //return false;
157}
158
159bool SDMemTable::setMask(const std::vector<int>& whichChans) {
160 ROArrayColumn<uChar> spec(table_, "FLAGTRA");
161
162 std::vector<int>::iterator it;
163 uInt n = spec.shape(0)(3);
164 chanMask_.resize(n,true);
165 for (it = whichChans.begin(); it != whichChans.end(); it++) {
166 if (*it < n)
167 chanMask_[*it] = false;
168 }
169 return true;
170}
171
172std::vector<bool> SDMemTable::getMask(Int whichRow) const {
173 std::vector<bool> mask;
174 ROArrayColumn<uChar> spec(table_, "FLAGTRA");
175 Array<uChar> arr;
176 spec.get(whichRow, arr);
177 ArrayAccessor<uChar, Axis<0> > aa0(arr);
178 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
179 ArrayAccessor<uChar, Axis<1> > aa1(aa0);
180 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
181 ArrayAccessor<uChar, Axis<2> > aa2(aa1);
182 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
183
184 Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
185
186 std::vector<bool> tmp;
187 tmp = chanMask_; // WHY the fxxx do I have to make a copy here
188 std::vector<bool>::iterator miter;
189 miter = tmp.begin();
190
191 for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
192 bool out =!static_cast<bool>(*i);
193 if (useUserMask) {
194 out = out && (*miter);
195 miter++;
196 }
197 mask.push_back(out);
198 }
199 return mask;
200}
201std::vector<float> SDMemTable::getSpectrum(Int whichRow) {
202
203 std::vector<float> spectrum;
204 ROArrayColumn<Float> spec(table_, "SPECTRA");
205 Array<Float> arr;
206 spec.get(whichRow, arr);
207 ArrayAccessor<Float, Axis<0> > aa0(arr);
208 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
209 ArrayAccessor<Float, Axis<1> > aa1(aa0);
210 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
211 ArrayAccessor<Float, Axis<2> > aa2(aa1);
212 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
213 for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
214 spectrum.push_back(*i);
215 }
216 return spectrum;
217}
218void SDMemTable::getSpectrum(Vector<Float>& spectrum, Int whichRow=0) {
219 ROArrayColumn<Float> spec(table_, "SPECTRA");
220 Array<Float> arr;
221 spec.get(whichRow, arr);
222 spectrum.resize(arr.shape()(3));
223 ArrayAccessor<Float, Axis<0> > aa0(arr);
224 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
225 ArrayAccessor<Float, Axis<1> > aa1(aa0);
226 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
227 ArrayAccessor<Float, Axis<2> > aa2(aa1);
228 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
229
230 ArrayAccessor<Float, Axis<0> > va(spectrum);
231 for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
232 (*va) = (*i);
233 va++;
234 }
235}
236
237void SDMemTable::getMask(Vector<Bool>& mask, Int whichRow=0) const {
238 ROArrayColumn<uChar> spec(table_, "FLAGTRA");
239 Array<uChar> arr;
240 spec.get(whichRow, arr);
241 mask.resize(arr.shape()(3));
242
243 ArrayAccessor<uChar, Axis<0> > aa0(arr);
244 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
245 ArrayAccessor<uChar, Axis<1> > aa1(aa0);
246 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
247 ArrayAccessor<uChar, Axis<2> > aa2(aa1);
248 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
249
250 Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
251
252 ArrayAccessor<Bool, Axis<0> > va(mask);
253 std::vector<bool> tmp;
254 tmp = chanMask_; // WHY the fxxx do I have to make a copy here. The
255 // iterator should work on chanMask_??
256 std::vector<bool>::iterator miter;
257 miter = tmp.begin();
258
259 for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
260 bool out =!static_cast<bool>(*i);
261 if (useUserMask) {
262 out = out && (*miter);
263 miter++;
264 }
265 (*va) = out;
266 va++;
267 }
268}
269
270MaskedArray<Float> SDMemTable::rowAsMaskedArray(uInt whichRow,
271 Bool useSelection) {
272 ROArrayColumn<Float> spec(table_, "SPECTRA");
273 Array<Float> arr;
274 ROArrayColumn<uChar> flag(table_, "FLAGTRA");
275 Array<uChar> farr;
276 spec.get(whichRow, arr);
277 flag.get(whichRow, farr);
278 Array<Bool> barr(farr.shape());convertArray(barr, farr);
279 MaskedArray<Float> marr;
280 if (useSelection) {
281 ArrayAccessor<Float, Axis<0> > aa0(arr);
282 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
283 ArrayAccessor<Float, Axis<1> > aa1(aa0);
284 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
285 ArrayAccessor<Float, Axis<2> > aa2(aa1);
286 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
287
288 ArrayAccessor<Bool, Axis<0> > baa0(barr);
289 baa0.reset(baa0.begin(uInt(beamSel_)));//go to beam
290 ArrayAccessor<Bool, Axis<1> > baa1(baa0);
291 baa1.reset(baa1.begin(uInt(IFSel_)));// go to IF
292 ArrayAccessor<Bool, Axis<2> > baa2(baa1);
293 baa2.reset(baa2.begin(uInt(polSel_)));// go to pol
294
295 Vector<Float> a(arr.shape()(3));
296 Vector<Bool> b(barr.shape()(3));
297 ArrayAccessor<Float, Axis<0> > a0(a);
298 ArrayAccessor<Bool, Axis<0> > b0(b);
299
300 ArrayAccessor<Bool, Axis<3> > j(baa2);
301 for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
302 (*a0) = (*i);
303 (*b0) = !(*j);
304 j++;
305 a0++;
306 b0++;
307 }
308 marr.setData(a,b);
309 } else {
310 marr.setData(arr,!barr);
311 }
312 return marr;
313}
314
315Float SDMemTable::getTsys(Int whichRow) const {
316 ROArrayColumn<Float> ts(table_, "TSYS");
317 Array<Float> arr;
318 ts.get(whichRow, arr);
319 Float out;
320 IPosition ip(arr.shape());
321 ip(0) = beamSel_;ip(1) = IFSel_;ip(2) = polSel_;ip(3)=0;
322 out = arr(ip);
323 return out;
324}
325
326bool SDMemTable::putSDContainer(const SDContainer& sdc) {
327 ScalarColumn<Double> mjd(table_, "TIME");
328 ScalarColumn<String> srcn(table_, "SRCNAME");
329 ArrayColumn<Float> spec(table_, "SPECTRA");
330 ArrayColumn<uChar> flags(table_, "FLAGTRA");
331 ArrayColumn<Float> ts(table_, "TSYS");
332 ScalarColumn<Int> scan(table_, "SCANID");
333 ScalarColumn<Double> integr(table_, "INTERVAL");
334
335 uInt rno = table_.nrow();
336 table_.addRow();
337
338 mjd.put(rno, sdc.timestamp);
339 srcn.put(rno, sdc.sourcename);
340 spec.put(rno, sdc.getSpectrum());
341 flags.put(rno, sdc.getFlags());
342 ts.put(rno, sdc.getTsys());
343 scan.put(rno, sdc.scanid);
344 integr.put(rno, sdc.interval);
345
346 return true;
347}
348
349SDContainer SDMemTable::getSDContainer(uInt whichRow) const {
350 ROScalarColumn<Double> mjd(table_, "TIME");
351 ROScalarColumn<String> srcn(table_, "SRCNAME");
352 ROArrayColumn<Float> spec(table_, "SPECTRA");
353 ROArrayColumn<uChar> flags(table_, "FLAGTRA");
354 ROArrayColumn<Float> ts(table_, "TSYS");
355 ROScalarColumn<Int> scan(table_, "SCANID");
356 ROScalarColumn<Double> integr(table_, "INTERVAL");
357
358 SDContainer sdc(nBeam(),nIF(),nPol(),nChan());
359 mjd.get(whichRow, sdc.timestamp);
360 srcn.get(whichRow, sdc.sourcename);
361 integr.get(whichRow, sdc.interval);
362 scan.get(whichRow, sdc.scanid);
363 Array<Float> spectrum;
364 Array<Float> tsys;
365 Array<uChar> flagtrum;
366 spec.get(whichRow, spectrum);
367 sdc.putSpectrum(spectrum);
368 flags.get(whichRow, flagtrum);
369 sdc.putFlags(flagtrum);
370 ts.get(whichRow, tsys);
371 sdc.putTsys(tsys);
372 return sdc;
373}
374bool SDMemTable::putSDHeader(const SDHeader& sdh) {
375 table_.lock();
376 table_.rwKeywordSet().define("nIF", sdh.nif);
377 table_.rwKeywordSet().define("nBeam", sdh.nbeam);
378 table_.rwKeywordSet().define("nPol", sdh.npol);
379 table_.rwKeywordSet().define("nChan", sdh.nchan);
380 table_.rwKeywordSet().define("Observer", sdh.observer);
381 table_.rwKeywordSet().define("Project", sdh.project);
382 table_.rwKeywordSet().define("Obstype", sdh.obstype);
383 table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
384 table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
385 table_.rwKeywordSet().define("Equinox", sdh.equinox);
386 table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
387 table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
388 table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
389 table_.rwKeywordSet().define("UTC", sdh.utc);
390 table_.unlock();
391 cerr << "Table Header set" << endl;
392 return true;
393}\
394
395SDHeader SDMemTable::getSDHeader() const {
396 SDHeader sdh;
397 table_.keywordSet().get("nBeam",sdh.nbeam);
398 table_.keywordSet().get("nIF",sdh.nif);
399 table_.keywordSet().get("nPol",sdh.npol);
400 table_.keywordSet().get("nChan",sdh.nchan);
401 table_.keywordSet().get("Observer", sdh.observer);
402 table_.keywordSet().get("Project", sdh.project);
403 table_.keywordSet().get("Obstype", sdh.obstype);
404 table_.keywordSet().get("AntennaName", sdh.antennaname);
405 table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
406 table_.keywordSet().get("Equinox", sdh.equinox);
407 table_.keywordSet().get("FreqRefFrame", sdh.freqref);
408 table_.keywordSet().get("FreqRefVal", sdh.reffreq);
409 table_.keywordSet().get("Bandwidth", sdh.bandwidth);
410 table_.keywordSet().get("UTC", sdh.utc);
411 return sdh;
412}
413void SDMemTable::makePersistent(const std::string& filename) {
414 table_.deepCopy(filename,Table::New);
415}
416
417void SDMemTable::summary() const {
418 ROScalarColumn<Int> scans(table_, "SCANID");
419 ROScalarColumn<String> srcs(table_, "SRCNAME");
420 cout << "*************** Header ***************" << endl;
421 cout << "nBeam = " << nBeam() << "\t"
422 << "nIF = " << nIF() << endl
423 << "nPol = " << nPol() << "\t"
424 << "nChan = " << nChan() << "\t" << endl;
425 cout << "*************** Header ***************" << endl;
426 uInt count = 0;
427 String name;
428 Int previous = -1;Int current=0;
429 cout << "Scan\tSource" << endl;
430 for (uInt i=0; i< scans.nrow();i++) {
431 scans.getScalar(i,current);
432 if (previous != current) {
433 srcs.getScalar(i,name);
434 previous = current;
435 count++;
436 cout << count << "\t"
437 << name
438 << endl;
439 }
440 }
441 cout << "Table contains " << table_.nrow() << " integration(s)." << endl;
442 cout << "in " << count << " scan(s)." << endl;
443}
444
445Int SDMemTable::nBeam() const {
446 Int n;
447 table_.keywordSet().get("nBeam",n);
448 return n;
449}
450Int SDMemTable::nIF() const {
451 Int n;
452 table_.keywordSet().get("nIF",n);
453 return n;
454}
455Int SDMemTable::nPol() const {
456 Int n;
457 table_.keywordSet().get("nPol",n);
458 return n;
459}
460Int SDMemTable::nChan() const {
461 Int n;
462 table_.keywordSet().get("nChan",n);
463 return n;
464}
465/*
466void SDMemTable::maskChannels(const std::vector<Int>& whichChans ) {
467
468 std::vector<int>::iterator it;
469 ArrayAccessor<uChar, Axis<2> > j(flags_);
470 for (it = whichChans.begin(); it != whichChans.end(); it++) {
471 j.reset(j.begin(uInt(*it)));
472 for (ArrayAccessor<uChar, Axis<0> > i(j); i != i.end(); ++i) {
473 for (ArrayAccessor<uChar, Axis<1> > ii(i); ii != ii.end(); ++ii) {
474 for (ArrayAccessor<uChar, Axis<3> > iii(ii);
475 iii != iii.end(); ++iii) {
476 (*iii) =
477 }
478 }
479 }
480 }
481
482}
483*/
Note: See TracBrowser for help on using the repository browser.