source: trunk/src/SDMemTable.cc @ 21

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

Added retrieval functions for SDHeader and SDContainer.

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