source: trunk/src/SDMemTable.cc @ 209

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