source: trunk/src/SDMemTable.cc @ 138

Last change on this file since 138 was 138, checked in by mar637, 19 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
Line 
1//#---------------------------------------------------------------------------
2//# SDMemTable.cc: A MemoryTable container for single dish integrations
3//#---------------------------------------------------------------------------
4//# Copyright (C) 2004
5//# 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#include <casa/aips.h>
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 <coordinates/Coordinates/CoordinateUtil.h>
54#include <casa/Quanta/MVTime.h>
55
56#include "SDMemTable.h"
57#include "SDContainer.h"
58
59using namespace casa;
60using namespace asap;
61
62SDMemTable::SDMemTable() :
63  IFSel_(0),
64  beamSel_(0),
65  polSel_(0) {
66  setup();
67}
68SDMemTable::SDMemTable(const std::string& name) :
69  IFSel_(0),
70  beamSel_(0),
71  polSel_(0) {
72  Table tab(name);
73  table_ = tab.copyToMemoryTable("dummy");
74}
75
76SDMemTable::SDMemTable(const SDMemTable& other, Bool clear) {
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()
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  }
90}
91
92SDMemTable::SDMemTable(const Table& tab, const std::string& exprs) :
93  IFSel_(0),
94  beamSel_(0),
95  polSel_(0) {
96  Table t = tableCommand(exprs,tab);
97  if (t.nrow() == 0)
98      throw(AipsError("Query unsuccessful."));
99  table_ = t.copyToMemoryTable("dummy");
100}
101
102SDMemTable::~SDMemTable(){
103  //cerr << "goodbye from SDMemTable @ " << this << endl;
104}
105
106SDMemTable SDMemTable::getScan(Int scanID) {
107  String cond("SELECT * from $1 WHERE SCANID == ");
108  cond += String::toString(scanID);
109  return SDMemTable(table_, cond);
110}
111
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
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
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"));
134  td.addColumn(ArrayColumnDesc<Float>("TSYS"));
135  td.addColumn(ScalarColumnDesc<Int>("SCANID"));
136  td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
137  td.addColumn(ArrayColumnDesc<uInt>("FREQID"));
138  td.addColumn(ArrayColumnDesc<Double>("DIRECTION"));
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
147  // Now create a new table from the description.
148
149  SetupNewTable aNewTab("dummy", td, Table::New);
150  table_ = Table(aNewTab, Table::Memory, 0);
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
160std::string SDMemTable::getTime(Int whichRow) const {
161  ROScalarColumn<Double> src(table_, "TIME");
162  Double tm;
163  src.get(whichRow, tm);
164  MVTime mvt(tm);
165  mvt.setFormat(MVTime::TIME);
166  ostringstream oss;
167  oss << mvt;
168  String str(oss);
169  return str;
170}
171double SDMemTable::getInterval(Int whichRow) const {
172  ROScalarColumn<Double> src(table_, "INTERVAL");
173  Double intval;
174  src.get(whichRow, intval);
175  return intval;
176}
177
178bool SDMemTable::setIF(Int whichIF) {
179  if ( whichIF >= 0 && whichIF < nIF()) {
180    IFSel_ = whichIF;
181    return true;
182  }
183  return false;
184}
185
186bool SDMemTable::setBeam(Int whichBeam) {
187  if ( whichBeam >= 0 && whichBeam < nBeam()) {
188    beamSel_ = whichBeam;
189    return true;
190  }
191  return false;
192}
193
194bool SDMemTable::setPol(Int whichPol) {
195  if ( whichPol >= 0 && whichPol < nPol()) {
196    polSel_ = whichPol;
197    return true;
198  }
199  return false;
200}
201
202bool SDMemTable::setMask(std::vector<int> whichChans) {
203  ROArrayColumn<uChar> spec(table_, "FLAGTRA");
204  std::vector<int>::iterator it;
205  uInt n = spec.shape(0)(3);
206  if (whichChans.empty()) {
207    chanMask_ = std::vector<bool>(n,true);
208    return true;     
209  }
210  chanMask_.resize(n,true);
211  for (it = whichChans.begin(); it != whichChans.end(); ++it) {
212    if (*it < n) {
213      chanMask_[*it] = false;
214    }
215  }
216  return true;
217}
218
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);
245  }
246  return mask;
247}
248std::vector<float> SDMemTable::getSpectrum(Int whichRow) const {
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
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);
262  }
263  return spectrum;
264}
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}
281
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) {
321  std::vector<double> absc(nChan());
322  Vector<Double> absc1(nChan());
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);
329  Table t = table_.keywordSet().asTable("FREQUENCIES");
330  String rf;
331  //t.keywordSet().get("EQUINOX",rf);
332  MDirection::Types mdr;
333  //if (!MDirection::getType(mdr, rf)) {
334  mdr = MDirection::J2000;
335  //cout << "Unknown equinox using J2000" << endl;
336  //}
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);
349  MVEpoch tm2(Quantum<Double>(obstime, Unit(String("d"))));
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);
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";
366  MFrequency::Types mtype;
367  if (!MFrequency::getType(mtype, frm)) {
368    cout << "Frequency type unknown assuming TOPO" << endl;
369    mtype = MFrequency::TOPO;
370  }
371 
372  if (!spc.setReferenceConversion(mtype,epoch,pos,direct)) {
373    throw(AipsError("Couldn't convert frequency frame."));
374  }
375
376  if ( u == Unit("km/s") ) {
377    Vector<Double> rstf;
378    t.keywordSet().get("RESTFREQS",rstf);
379    if (rstf.nelements() > 0) {
380      if (rstf.nelements() >= nIF())
381        spc.selectRestFrequency(uInt(IFSel_));
382      spc.setVelocity(u.getName());
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) {
388        (*it) = wrld[i];
389        i++;
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;
398    for (it = absc.begin(); it != absc.end(); ++it) {
399      spc.toWorld(tmp,absc1[i]);
400      (*it) = tmp;
401      i++;
402    }
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    }
411  }
412  return absc;
413}
414
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
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
470void SDMemTable::getSpectrum(Vector<Float>& spectrum, Int whichRow) {
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
481
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}
488/*
489void SDMemTable::getMask(Vector<Bool>& mask, Int whichRow) const {
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) );
503
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
507                   // iterator should work on chanMask_??
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++;
519  }
520}
521*/
522MaskedArray<Float> SDMemTable::rowAsMaskedArray(uInt whichRow,
523                                                Bool useSelection) {
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;
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
539
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  }
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());
573  ip(0) = beamSel_;ip(1) = IFSel_;ip(2) = polSel_;ip(3)=0;
574  out = arr(ip);
575  return out;
576}
577
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;
583    return SpectralCoordinate();
584  }
585
586  Double rp,rv,inc;
587  String rf;
588  Vector<Double> vec;
589  ROScalarColumn<Double> rpc(t, "REFPIX");
590  ROScalarColumn<Double> rvc(t, "REFVAL");
591  ROScalarColumn<Double> incc(t, "INCREMENT");
592  t.keywordSet().get("RESTFREQS",vec);
593  t.keywordSet().get("BASEREFFRAME",rf);
594
595  MFrequency::Types mft;
596  if (!MFrequency::getType(mft, rf)) {
597    cerr << "Frequency type unknown assuming TOPO" << endl;
598    mft = MFrequency::TOPO;
599  }
600  rpc.get(whichIdx, rp);
601  rvc.get(whichIdx, rv);
602  incc.get(whichIdx, inc);
603  SpectralCoordinate spec(mft,rv,inc,rp);
604  if (vec.nelements() > 0)
605    spec.setRestFrequencies(vec);
606  return spec;
607}
608
609Bool SDMemTable::setCoordinate(const SpectralCoordinate& speccord,
610                               uInt whichIdx) {
611  Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
612  if (whichIdx > t.nrow() ) {
613    throw(AipsError("SDMemTable::setCoordinate - coord no out of range"));
614  }
615  ScalarColumn<Double> rpc(t, "REFPIX");
616  ScalarColumn<Double> rvc(t, "REFVAL");
617  ScalarColumn<Double> incc(t, "INCREMENT");
618
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
626Int SDMemTable::nCoordinates() const
627{
628  return table_.keywordSet().asTable("FREQUENCIES").nrow();
629}
630
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
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  }
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);
661  aTable.rwKeywordSet().define("EQUINOX", sdft.equinox());
662  aTable.rwKeywordSet().define("UNIT", String(""));
663  aTable.rwKeywordSet().define("DOPPLER", String("RADIO"));
664  Vector<Double> rfvec;
665  aTable.rwKeywordSet().define("RESTFREQS", rfvec);
666  table_.rwKeywordSet().defineTable ("FREQUENCIES", aTable);
667  return True;
668}
669
670SDFrequencyTable SDMemTable::getSDFreqTable() const  {
671  SDFrequencyTable sdft;
672
673  return sdft;
674}
675
676bool SDMemTable::putSDContainer(const SDContainer& sdc) {
677  ScalarColumn<Double> mjd(table_, "TIME");
678  ScalarColumn<String> srcn(table_, "SRCNAME");
679  ScalarColumn<String> fldn(table_, "FIELDNAME");
680  ArrayColumn<Float> spec(table_, "SPECTRA");
681  ArrayColumn<uChar> flags(table_, "FLAGTRA");
682  ArrayColumn<Float> ts(table_, "TSYS");
683  ScalarColumn<Int> scan(table_, "SCANID");
684  ScalarColumn<Double> integr(table_, "INTERVAL");
685  ArrayColumn<uInt> freqid(table_, "FREQID");
686  ArrayColumn<Double> dir(table_, "DIRECTION");
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");
693
694  uInt rno = table_.nrow();
695  table_.addRow();
696
697  mjd.put(rno, sdc.timestamp);
698  srcn.put(rno, sdc.sourcename);
699  fldn.put(rno, sdc.fieldname);
700  spec.put(rno, sdc.getSpectrum());
701  flags.put(rno, sdc.getFlags());
702  ts.put(rno, sdc.getTsys());
703  scan.put(rno, sdc.scanid);
704  integr.put(rno, sdc.interval);
705  freqid.put(rno, sdc.getFreqMap());
706  dir.put(rno, sdc.getDirection());
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);
713
714  return true;
715}
716
717SDContainer SDMemTable::getSDContainer(uInt whichRow) const {
718  ROScalarColumn<Double> mjd(table_, "TIME");
719  ROScalarColumn<String> srcn(table_, "SRCNAME");
720  ROScalarColumn<String> fldn(table_, "FIELDNAME");
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");
726  ROArrayColumn<uInt> freqid(table_, "FREQID");
727  ROArrayColumn<Double> dir(table_, "DIRECTION");
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");
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);
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);
749  Array<Float> spectrum;
750  Array<Float> tsys;
751  Array<uChar> flagtrum;
752  Vector<uInt> fmap;
753  Array<Double> direction;
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);
760  freqid.get(whichRow, fmap);
761  sdc.putFreqMap(fmap);
762  dir.get(whichRow, direction);
763  sdc.putDirection(direction);
764  return sdc;
765}
766
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;
785}
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;
804}
805void SDMemTable::makePersistent(const std::string& filename) {
806  table_.deepCopy(filename,Table::New);
807}
808
809Int SDMemTable::nScan() const {
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) {
816      previous = current;
817      n++;
818    }
819  }
820  return n;
821}
822
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()  {
832  ROScalarColumn<Int> scans(table_, "SCANID");
833  ROScalarColumn<String> srcs(table_, "SRCNAME");
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;
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;
866  oss << endl;
867  uInt count = 0;
868  String name;
869  Int previous = -1;Int current=0;
870  Int integ = 0;
871  oss << setw(6) << "Scan"
872      << setw(12) << "Source"
873      << setw(21) << "Time"
874      << setw(11) << "Integration" << endl;
875  oss << "--------------------------------------------------" << endl;
876  for (uInt i=0; i< scans.nrow();i++) {
877    scans.getScalar(i,current);
878    if (previous != current) {
879      srcs.getScalar(i,name);
880      previous = current;
881      String t = formatSec(Double(getInterval(i)));
882      oss << setw(6) << count << setw(12) << name << setw(21) << getTime(i)
883          << setw(2) << setprecision(1)
884          << t << endl;
885      count++;
886    } else {
887      integ++;
888    }
889  }
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);
895}
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}
917/*
918void SDMemTable::maskChannels(const std::vector<Int>& whichChans ) {
919
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) {
926        for (ArrayAccessor<uChar, Axis<3> > iii(ii);
927             iii != iii.end(); ++iii) {
928          (*iii) =
929        }
930      }
931    }
932  }
933
934}
935*/
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.