source: trunk/src/SDMemTable.cc @ 164

Last change on this file since 164 was 164, checked in by kil064, 19 years ago

make a few more functions 'const'

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.3 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  IFSel_= other.IFSel_;
78  beamSel_= other.beamSel_;
79  polSel_= other.polSel_;
80  chanMask_ = other.chanMask_;
81  table_ = other.table_.copyToMemoryTable(String("dummy"));
82  // clear all rows()
83  if (clear) {
84    table_.removeRow(this->table_.rowNumbers());
85  } else {
86    IFSel_ = other.IFSel_;
87    beamSel_ = other.beamSel_;
88    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) const {
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  if (this != &other) {
114     IFSel_= other.IFSel_;
115     beamSel_= other.beamSel_;
116     polSel_= other.polSel_;
117     chanMask_.resize(0);
118     chanMask_ = other.chanMask_;
119     table_ = other.table_.copyToMemoryTable(String("dummy"));
120   }
121  return *this;
122}
123
124SDMemTable SDMemTable::getSource(const std::string& source) const {
125  String cond("SELECT * from $1 WHERE SRCNAME == ");
126  cond += source;
127  return SDMemTable(table_, cond);
128}
129
130void SDMemTable::setup() {
131  TableDesc td("", "1", TableDesc::Scratch);
132  td.comment() = "A SDMemTable";
133  td.addColumn(ScalarColumnDesc<Double>("TIME"));
134  td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
135  td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
136  td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
137  td.addColumn(ArrayColumnDesc<Float>("TSYS"));
138  td.addColumn(ScalarColumnDesc<Int>("SCANID"));
139  td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
140  td.addColumn(ArrayColumnDesc<uInt>("FREQID"));
141  td.addColumn(ArrayColumnDesc<Double>("DIRECTION"));
142  td.addColumn(ScalarColumnDesc<String>("FIELDNAME"));
143  td.addColumn(ScalarColumnDesc<String>("TCALTIME"));
144  td.addColumn(ArrayColumnDesc<Float>("TCAL"));
145  td.addColumn(ScalarColumnDesc<Float>("AZIMUTH"));
146  td.addColumn(ScalarColumnDesc<Float>("ELEVATION"));
147  td.addColumn(ScalarColumnDesc<Float>("PARANGLE"));
148  td.addColumn(ScalarColumnDesc<Int>("REFBEAM"));
149
150  // Now create a new table from the description.
151
152  SetupNewTable aNewTab("dummy", td, Table::New);
153  table_ = Table(aNewTab, Table::Memory, 0);
154}
155
156std::string SDMemTable::getSourceName(Int whichRow) const {
157  ROScalarColumn<String> src(table_, "SRCNAME");
158  String name;
159  src.get(whichRow, name);
160  return name;
161}
162
163std::string SDMemTable::getTime(Int whichRow) const {
164  ROScalarColumn<Double> src(table_, "TIME");
165  Double tm;
166  src.get(whichRow, tm);
167  MVTime mvt(tm);
168  mvt.setFormat(MVTime::TIME);
169  ostringstream oss;
170  oss << mvt;
171  String str(oss);
172  return str;
173}
174double SDMemTable::getInterval(Int whichRow) const {
175  ROScalarColumn<Double> src(table_, "INTERVAL");
176  Double intval;
177  src.get(whichRow, intval);
178  return intval;
179}
180
181bool SDMemTable::setIF(Int whichIF) {
182  if ( whichIF >= 0 && whichIF < nIF()) {
183    IFSel_ = whichIF;
184    return true;
185  }
186  return false;
187}
188
189bool SDMemTable::setBeam(Int whichBeam) {
190  if ( whichBeam >= 0 && whichBeam < nBeam()) {
191    beamSel_ = whichBeam;
192    return true;
193  }
194  return false;
195}
196
197bool SDMemTable::setPol(Int whichPol) {
198  if ( whichPol >= 0 && whichPol < nPol()) {
199    polSel_ = whichPol;
200    return true;
201  }
202  return false;
203}
204
205bool SDMemTable::setMask(std::vector<int> whichChans) {
206  ROArrayColumn<uChar> spec(table_, "FLAGTRA");
207  std::vector<int>::iterator it;
208  uInt n = spec.shape(0)(3);
209  if (whichChans.empty()) {
210    chanMask_ = std::vector<bool>(n,true);
211    return true;     
212  }
213  chanMask_.resize(n,true);
214  for (it = whichChans.begin(); it != whichChans.end(); ++it) {
215    if (*it < n) {
216      chanMask_[*it] = false;
217    }
218  }
219  return true;
220}
221
222std::vector<bool> SDMemTable::getMask(Int whichRow) const {
223  std::vector<bool> mask;
224  ROArrayColumn<uChar> spec(table_, "FLAGTRA");
225  Array<uChar> arr;
226  spec.get(whichRow, arr);
227  ArrayAccessor<uChar, Axis<0> > aa0(arr);
228  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
229  ArrayAccessor<uChar, Axis<1> > aa1(aa0);
230  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
231  ArrayAccessor<uChar, Axis<2> > aa2(aa1);
232  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
233
234  Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
235
236  std::vector<bool> tmp;
237  tmp = chanMask_; // WHY the fxxx do I have to make a copy here
238  std::vector<bool>::iterator miter;
239  miter = tmp.begin();
240
241  for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
242    bool out =!static_cast<bool>(*i);
243    if (useUserMask) {
244      out = out && (*miter);
245      miter++;
246    }
247    mask.push_back(out);
248  }
249  return mask;
250}
251std::vector<float> SDMemTable::getSpectrum(Int whichRow) const {
252
253  std::vector<float> spectrum;
254  ROArrayColumn<Float> spec(table_, "SPECTRA");
255  Array<Float> arr;
256  spec.get(whichRow, arr);
257  ArrayAccessor<Float, Axis<0> > aa0(arr);
258  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
259  ArrayAccessor<Float, Axis<1> > aa1(aa0);
260  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
261  ArrayAccessor<Float, Axis<2> > aa2(aa1);
262  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
263  for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
264    spectrum.push_back(*i);
265  }
266  return spectrum;
267}
268std::vector<string> SDMemTable::getCoordInfo() const {
269  String un;
270  Table t = table_.keywordSet().asTable("FREQUENCIES");
271  String sunit;
272  t.keywordSet().get("UNIT",sunit);
273  String dpl;
274  t.keywordSet().get("DOPPLER",dpl);
275  if (dpl == "") dpl = "RADIO";
276  String rfrm;
277  t.keywordSet().get("REFFRAME",rfrm);
278  std::vector<string> inf;
279  inf.push_back(sunit);
280  inf.push_back(rfrm);
281  inf.push_back(dpl);
282  return inf;
283}
284
285void SDMemTable::setCoordInfo(std::vector<string> theinfo) {
286
287  std::vector<string>::iterator it;
288  String un,rfrm,dpl;
289  un = theinfo[0];
290  rfrm = theinfo[1];
291  dpl = theinfo[2];
292
293  //String un(theunit);
294  Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
295  Vector<Double> rstf;
296  t.keywordSet().get("RESTFREQS",rstf);
297  Bool canDo = True;
298  Unit u1("km/s");Unit u2("Hz");
299  if (Unit(un) == u1) {
300    Vector<Double> rstf;
301    t.keywordSet().get("RESTFREQS",rstf);
302    if (rstf.nelements() == 0) {
303        throw(AipsError("Can't set unit to km/s if no restfrequencies are specified"));
304    }
305  } else if (Unit(un) != u2 && un != "") {
306        throw(AipsError("Unit not conformant with Spectral Coordinates"));
307  }
308  t.rwKeywordSet().define("UNIT", un);
309
310  MFrequency::Types mdr;
311  if (!MFrequency::getType(mdr, rfrm)) {
312   
313    Int a,b;const uInt* c;
314    const String* valid = MFrequency::allMyTypes(a, b, c);
315    String pfix = "Please specify a legal frame type. Types are\n";
316    throw(AipsError(pfix+(*valid)));
317  } else {
318    t.rwKeywordSet().define("REFFRAME",rfrm);
319  }
320
321}
322
323std::vector<double> SDMemTable::getAbcissa(Int whichRow) const {
324  std::vector<double> absc(nChan());
325  Vector<Double> absc1(nChan());
326  indgen(absc1);
327  ROArrayColumn<uInt> fid(table_, "FREQID");
328  Vector<uInt> v;
329  fid.get(whichRow, v);
330  uInt specidx = v(IFSel_);
331  SpectralCoordinate spc = getCoordinate(specidx);
332  Table t = table_.keywordSet().asTable("FREQUENCIES");
333  String rf;
334  //t.keywordSet().get("EQUINOX",rf);
335  MDirection::Types mdr;
336  //if (!MDirection::getType(mdr, rf)) {
337  mdr = MDirection::J2000;
338  //cout << "Unknown equinox using J2000" << endl;
339  //}
340  ROArrayColumn<Double> dir(table_, "DIRECTION");
341  Array<Double> posit;
342  dir.get(whichRow,posit);
343  Vector<Double> wpos(2);
344  wpos[0] = posit(IPosition(2,beamSel_,0));
345  wpos[1] = posit(IPosition(2,beamSel_,1));
346  Quantum<Double> lon(wpos[0],Unit(String("rad")));
347  Quantum<Double> lat(wpos[1],Unit(String("rad")));
348  MDirection direct(lon, lat, mdr);
349  ROScalarColumn<Double> tme(table_, "TIME");
350  Double obstime;
351  tme.get(whichRow,obstime);
352  MVEpoch tm2(Quantum<Double>(obstime, Unit(String("d"))));
353  MEpoch epoch(tm2);
354
355  Vector<Double> antpos;
356  table_.keywordSet().get("AntennaPosition", antpos);
357  MVPosition mvpos(antpos(0),antpos(1),antpos(2));
358  MPosition pos(mvpos);
359  String sunit;
360  t.keywordSet().get("UNIT",sunit);
361  if (sunit == "") sunit = "pixel";
362  Unit u(sunit);
363  String frm;
364  t.keywordSet().get("REFFRAME",frm);
365  if (frm == "") frm = "TOPO";
366  String dpl;
367  t.keywordSet().get("DOPPLER",dpl);
368  if (dpl == "") dpl = "RADIO";
369  MFrequency::Types mtype;
370  if (!MFrequency::getType(mtype, frm)) {
371    cout << "Frequency type unknown assuming TOPO" << endl;
372    mtype = MFrequency::TOPO;
373  }
374 
375  if (!spc.setReferenceConversion(mtype,epoch,pos,direct)) {
376    throw(AipsError("Couldn't convert frequency frame."));
377  }
378
379  if ( u == Unit("km/s") ) {
380    Vector<Double> rstf;
381    t.keywordSet().get("RESTFREQS",rstf);
382    if (rstf.nelements() > 0) {
383      if (rstf.nelements() >= nIF())
384        spc.selectRestFrequency(uInt(IFSel_));
385      spc.setVelocity(u.getName());
386      Vector<Double> wrld;
387      spc.pixelToVelocity(wrld,absc1);
388      std::vector<double>::iterator it;
389      uInt i = 0;
390      for (it = absc.begin(); it != absc.end(); ++it) {
391        (*it) = wrld[i];
392        i++;
393      }
394    }
395  } else if (u == Unit("Hz")) {
396    Vector<String> wau(1); wau = u.getName();
397    spc.setWorldAxisUnits(wau);
398    std::vector<double>::iterator it;
399    Double tmp;
400    uInt i = 0;
401    for (it = absc.begin(); it != absc.end(); ++it) {
402      spc.toWorld(tmp,absc1[i]);
403      (*it) = tmp;
404      i++;
405    }
406
407  } else {
408    // assume channels/pixels
409    std::vector<double>::iterator it;
410    uInt i=0;
411    for (it = absc.begin(); it != absc.end(); ++it) {
412      (*it) = Double(i++);
413    }
414  }
415  return absc;
416}
417
418std::string SDMemTable::getAbcissaString(Int whichRow) const
419{
420  ROArrayColumn<uInt> fid(table_, "FREQID");
421  Table t = table_.keywordSet().asTable("FREQUENCIES");
422  String sunit;
423  t.keywordSet().get("UNIT",sunit);
424  if (sunit == "") sunit = "pixel";
425  Unit u(sunit);
426  Vector<uInt> v;
427  fid.get(whichRow, v);
428  uInt specidx = v(IFSel_);
429  SpectralCoordinate spc = getCoordinate(specidx);
430  String frm;
431  t.keywordSet().get("REFFRAME",frm);
432  MFrequency::Types mtype;
433  if (!MFrequency::getType(mtype, frm)) {
434    cout << "Frequency type unknown assuming TOPO" << endl;
435    mtype = MFrequency::TOPO;
436  }
437  spc.setFrequencySystem(mtype);
438  String s = "Channel";
439  if (u == Unit("km/s")) {
440    spc.setVelocity(u.getName());
441    s = CoordinateUtil::axisLabel(spc,0,True,True,True);
442  } else if (u == Unit("Hz")) {
443    Vector<String> wau(1);wau = u.getName();
444    spc.setWorldAxisUnits(wau);
445    s = CoordinateUtil::axisLabel(spc);
446  }
447  return s;
448}
449
450void SDMemTable::setSpectrum(std::vector<float> spectrum, int whichRow) {
451  ArrayColumn<Float> spec(table_, "SPECTRA");
452  Array<Float> arr;
453  spec.get(whichRow, arr);
454  if (spectrum.size() != arr.shape()(3)) {
455    throw(AipsError("Attempting to set spectrum with incorrect length."));
456  }
457
458  ArrayAccessor<Float, Axis<0> > aa0(arr);
459  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
460  ArrayAccessor<Float, Axis<1> > aa1(aa0);
461  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
462  ArrayAccessor<Float, Axis<2> > aa2(aa1);
463  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
464
465  std::vector<float>::iterator it = spectrum.begin();
466  for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
467    (*i) = Float(*it);
468    it++;
469  }
470  spec.put(whichRow, arr);
471}
472
473void SDMemTable::getSpectrum(Vector<Float>& spectrum, Int whichRow) const {
474  ROArrayColumn<Float> spec(table_, "SPECTRA");
475  Array<Float> arr;
476  spec.get(whichRow, arr);
477  spectrum.resize(arr.shape()(3));
478  ArrayAccessor<Float, Axis<0> > aa0(arr);
479  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
480  ArrayAccessor<Float, Axis<1> > aa1(aa0);
481  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
482  ArrayAccessor<Float, Axis<2> > aa2(aa1);
483  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
484
485  ArrayAccessor<Float, Axis<0> > va(spectrum);
486  for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
487    (*va) = (*i);
488    va++;
489  }
490}
491/*
492void SDMemTable::getMask(Vector<Bool>& mask, Int whichRow) const {
493  ROArrayColumn<uChar> spec(table_, "FLAGTRA");
494  Array<uChar> arr;
495  spec.get(whichRow, arr);
496  mask.resize(arr.shape()(3));
497
498  ArrayAccessor<uChar, Axis<0> > aa0(arr);
499  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
500  ArrayAccessor<uChar, Axis<1> > aa1(aa0);
501  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
502  ArrayAccessor<uChar, Axis<2> > aa2(aa1);
503  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
504
505  Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
506
507  ArrayAccessor<Bool, Axis<0> > va(mask);
508  std::vector<bool> tmp;
509  tmp = chanMask_; // WHY the fxxx do I have to make a copy here. The
510                   // iterator should work on chanMask_??
511  std::vector<bool>::iterator miter;
512  miter = tmp.begin();
513
514  for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
515    bool out =!static_cast<bool>(*i);
516    if (useUserMask) {
517      out = out && (*miter);
518      miter++;
519    }
520    (*va) = out;
521    va++;
522  }
523}
524*/
525MaskedArray<Float> SDMemTable::rowAsMaskedArray(uInt whichRow,
526                                                Bool useSelection) const
527{
528  ROArrayColumn<Float> spec(table_, "SPECTRA");
529  Array<Float> arr;
530  ROArrayColumn<uChar> flag(table_, "FLAGTRA");
531  Array<uChar> farr;
532  spec.get(whichRow, arr);
533  flag.get(whichRow, farr);
534  Array<Bool> barr(farr.shape());convertArray(barr, farr);
535  MaskedArray<Float> marr;
536  if (useSelection) {
537    ArrayAccessor<Float, Axis<0> > aa0(arr);
538    aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
539    ArrayAccessor<Float, Axis<1> > aa1(aa0);
540    aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
541    ArrayAccessor<Float, Axis<2> > aa2(aa1);
542    aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
543
544    ArrayAccessor<Bool, Axis<0> > baa0(barr);
545    baa0.reset(baa0.begin(uInt(beamSel_)));//go to beam
546    ArrayAccessor<Bool, Axis<1> > baa1(baa0);
547    baa1.reset(baa1.begin(uInt(IFSel_)));// go to IF
548    ArrayAccessor<Bool, Axis<2> > baa2(baa1);
549    baa2.reset(baa2.begin(uInt(polSel_)));// go to pol
550
551    Vector<Float> a(arr.shape()(3));
552    Vector<Bool> b(barr.shape()(3));
553    ArrayAccessor<Float, Axis<0> > a0(a);
554    ArrayAccessor<Bool, Axis<0> > b0(b);
555
556    ArrayAccessor<Bool, Axis<3> > j(baa2);
557    for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
558      (*a0) = (*i);
559      (*b0) = !(*j);
560      j++;
561      a0++;
562      b0++;
563    }
564    marr.setData(a,b);
565  } else {
566    marr.setData(arr,!barr);
567  }
568  return marr;
569}
570
571Float SDMemTable::getTsys(Int whichRow) const {
572  ROArrayColumn<Float> ts(table_, "TSYS");
573  Array<Float> arr;
574  ts.get(whichRow, arr);
575  Float out;
576  IPosition ip(arr.shape());
577  ip(0) = beamSel_;ip(1) = IFSel_;ip(2) = polSel_;ip(3)=0;
578  out = arr(ip);
579  return out;
580}
581
582SpectralCoordinate SDMemTable::getCoordinate(uInt whichIdx)  const {
583
584  Table t = table_.keywordSet().asTable("FREQUENCIES");
585  if (whichIdx > t.nrow() ) {
586    cerr << "SDMemTable::getCoordinate - whichIdx out of range" << endl;
587    return SpectralCoordinate();
588  }
589
590  Double rp,rv,inc;
591  String rf;
592  Vector<Double> vec;
593  ROScalarColumn<Double> rpc(t, "REFPIX");
594  ROScalarColumn<Double> rvc(t, "REFVAL");
595  ROScalarColumn<Double> incc(t, "INCREMENT");
596  t.keywordSet().get("RESTFREQS",vec);
597  t.keywordSet().get("BASEREFFRAME",rf);
598
599  MFrequency::Types mft;
600  if (!MFrequency::getType(mft, rf)) {
601    cerr << "Frequency type unknown assuming TOPO" << endl;
602    mft = MFrequency::TOPO;
603  }
604  rpc.get(whichIdx, rp);
605  rvc.get(whichIdx, rv);
606  incc.get(whichIdx, inc);
607  SpectralCoordinate spec(mft,rv,inc,rp);
608  if (vec.nelements() > 0)
609    spec.setRestFrequencies(vec);
610  return spec;
611}
612
613Bool SDMemTable::setCoordinate(const SpectralCoordinate& speccord,
614                               uInt whichIdx) {
615  Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
616  if (whichIdx > t.nrow() ) {
617    throw(AipsError("SDMemTable::setCoordinate - coord no out of range"));
618  }
619  ScalarColumn<Double> rpc(t, "REFPIX");
620  ScalarColumn<Double> rvc(t, "REFVAL");
621  ScalarColumn<Double> incc(t, "INCREMENT");
622
623  rpc.put(whichIdx, speccord.referencePixel()[0]);
624  rvc.put(whichIdx, speccord.referenceValue()[0]);
625  incc.put(whichIdx, speccord.increment()[0]);
626
627  return True;
628}
629
630Int SDMemTable::nCoordinates() const
631{
632  return table_.keywordSet().asTable("FREQUENCIES").nrow();
633}
634
635void SDMemTable::setRestFreqs(std::vector<double> freqs, const std::string& theunit)
636{
637  Vector<Double> tvec(freqs);
638  Quantum<Vector<Double> > q(tvec, String(theunit));
639  tvec.resize();
640  tvec = q.getValue("Hz");
641  Table t = table_.keywordSet().asTable("FREQUENCIES");
642  t.rwKeywordSet().define("RESTFREQS",tvec);
643}
644
645bool SDMemTable::putSDFreqTable(const SDFrequencyTable& sdft) {
646  TableDesc td("", "1", TableDesc::Scratch);
647  td.addColumn(ScalarColumnDesc<Double>("REFPIX"));
648  td.addColumn(ScalarColumnDesc<Double>("REFVAL"));
649  td.addColumn(ScalarColumnDesc<Double>("INCREMENT"));
650  SetupNewTable aNewTab("freqs", td, Table::New);
651  Table aTable (aNewTab, Table::Memory, sdft.length());
652  ScalarColumn<Double> sc0(aTable, "REFPIX");
653  ScalarColumn<Double> sc1(aTable, "REFVAL");
654  ScalarColumn<Double> sc2(aTable, "INCREMENT");
655  for (uInt i=0; i < sdft.length(); ++i) {
656    sc0.put(i,sdft.referencePixel(i));
657    sc1.put(i,sdft.referenceValue(i));
658    sc2.put(i,sdft.increment(i));
659  }
660  String rf = sdft.refFrame();
661  if (rf.contains("TOPO")) rf = "TOPO";
662
663  aTable.rwKeywordSet().define("BASEREFFRAME", rf);
664  aTable.rwKeywordSet().define("REFFRAME", rf);
665  aTable.rwKeywordSet().define("EQUINOX", sdft.equinox());
666  aTable.rwKeywordSet().define("UNIT", String(""));
667  aTable.rwKeywordSet().define("DOPPLER", String("RADIO"));
668  Vector<Double> rfvec;
669  aTable.rwKeywordSet().define("RESTFREQS", rfvec);
670  table_.rwKeywordSet().defineTable ("FREQUENCIES", aTable);
671  return True;
672}
673
674SDFrequencyTable SDMemTable::getSDFreqTable() const  {
675  SDFrequencyTable sdft;
676
677  return sdft;
678}
679
680bool SDMemTable::putSDContainer(const SDContainer& sdc) {
681  ScalarColumn<Double> mjd(table_, "TIME");
682  ScalarColumn<String> srcn(table_, "SRCNAME");
683  ScalarColumn<String> fldn(table_, "FIELDNAME");
684  ArrayColumn<Float> spec(table_, "SPECTRA");
685  ArrayColumn<uChar> flags(table_, "FLAGTRA");
686  ArrayColumn<Float> ts(table_, "TSYS");
687  ScalarColumn<Int> scan(table_, "SCANID");
688  ScalarColumn<Double> integr(table_, "INTERVAL");
689  ArrayColumn<uInt> freqid(table_, "FREQID");
690  ArrayColumn<Double> dir(table_, "DIRECTION");
691  ScalarColumn<Int> rbeam(table_, "REFBEAM");
692  ArrayColumn<Float> tcal(table_, "TCAL");
693  ScalarColumn<String> tcalt(table_, "TCALTIME");
694  ScalarColumn<Float> az(table_, "AZIMUTH");
695  ScalarColumn<Float> el(table_, "ELEVATION");
696  ScalarColumn<Float> para(table_, "PARANGLE");
697
698  uInt rno = table_.nrow();
699  table_.addRow();
700
701  mjd.put(rno, sdc.timestamp);
702  srcn.put(rno, sdc.sourcename);
703  fldn.put(rno, sdc.fieldname);
704  spec.put(rno, sdc.getSpectrum());
705  flags.put(rno, sdc.getFlags());
706  ts.put(rno, sdc.getTsys());
707  scan.put(rno, sdc.scanid);
708  integr.put(rno, sdc.interval);
709  freqid.put(rno, sdc.getFreqMap());
710  dir.put(rno, sdc.getDirection());
711  rbeam.put(rno, sdc.refbeam);
712  tcal.put(rno, sdc.tcal);
713  tcalt.put(rno, sdc.tcaltime);
714  az.put(rno, sdc.azimuth);
715  el.put(rno, sdc.elevation);
716  para.put(rno, sdc.parangle);
717
718  return true;
719}
720
721SDContainer SDMemTable::getSDContainer(uInt whichRow) const {
722  ROScalarColumn<Double> mjd(table_, "TIME");
723  ROScalarColumn<String> srcn(table_, "SRCNAME");
724  ROScalarColumn<String> fldn(table_, "FIELDNAME");
725  ROArrayColumn<Float> spec(table_, "SPECTRA");
726  ROArrayColumn<uChar> flags(table_, "FLAGTRA");
727  ROArrayColumn<Float> ts(table_, "TSYS");
728  ROScalarColumn<Int> scan(table_, "SCANID");
729  ROScalarColumn<Double> integr(table_, "INTERVAL");
730  ROArrayColumn<uInt> freqid(table_, "FREQID");
731  ROArrayColumn<Double> dir(table_, "DIRECTION");
732  ROScalarColumn<Int> rbeam(table_, "REFBEAM");
733  ROArrayColumn<Float> tcal(table_, "TCAL");
734  ROScalarColumn<String> tcalt(table_, "TCALTIME");
735  ROScalarColumn<Float> az(table_, "AZIMUTH");
736  ROScalarColumn<Float> el(table_, "ELEVATION");
737  ROScalarColumn<Float> para(table_, "PARANGLE");
738
739  SDContainer sdc(nBeam(),nIF(),nPol(),nChan());
740  mjd.get(whichRow, sdc.timestamp);
741  srcn.get(whichRow, sdc.sourcename);
742  integr.get(whichRow, sdc.interval);
743  scan.get(whichRow, sdc.scanid);
744  fldn.get(whichRow, sdc.fieldname);
745  rbeam.get(whichRow, sdc.refbeam);
746  az.get(whichRow, sdc.azimuth);
747  el.get(whichRow, sdc.elevation);
748  para.get(whichRow, sdc.parangle);
749  Vector<Float> tc;
750  tcal.get(whichRow, tc);
751  sdc.tcal[0] = tc[0];sdc.tcal[1] = tc[1];
752  tcalt.get(whichRow, sdc.tcaltime);
753  Array<Float> spectrum;
754  Array<Float> tsys;
755  Array<uChar> flagtrum;
756  Vector<uInt> fmap;
757  Array<Double> direction;
758  spec.get(whichRow, spectrum);
759  sdc.putSpectrum(spectrum);
760  flags.get(whichRow, flagtrum);
761  sdc.putFlags(flagtrum);
762  ts.get(whichRow, tsys);
763  sdc.putTsys(tsys);
764  freqid.get(whichRow, fmap);
765  sdc.putFreqMap(fmap);
766  dir.get(whichRow, direction);
767  sdc.putDirection(direction);
768  return sdc;
769}
770
771bool SDMemTable::putSDHeader(const SDHeader& sdh) {
772  table_.lock();
773  table_.rwKeywordSet().define("nIF", sdh.nif);
774  table_.rwKeywordSet().define("nBeam", sdh.nbeam);
775  table_.rwKeywordSet().define("nPol", sdh.npol);
776  table_.rwKeywordSet().define("nChan", sdh.nchan);
777  table_.rwKeywordSet().define("Observer", sdh.observer);
778  table_.rwKeywordSet().define("Project", sdh.project);
779  table_.rwKeywordSet().define("Obstype", sdh.obstype);
780  table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
781  table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
782  table_.rwKeywordSet().define("Equinox", sdh.equinox);
783  table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
784  table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
785  table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
786  table_.rwKeywordSet().define("UTC", sdh.utc);
787  table_.unlock();
788  return true;
789}
790
791SDHeader SDMemTable::getSDHeader() const {
792  SDHeader sdh;
793  table_.keywordSet().get("nBeam",sdh.nbeam);
794  table_.keywordSet().get("nIF",sdh.nif);
795  table_.keywordSet().get("nPol",sdh.npol);
796  table_.keywordSet().get("nChan",sdh.nchan);
797  table_.keywordSet().get("Observer", sdh.observer);
798  table_.keywordSet().get("Project", sdh.project);
799  table_.keywordSet().get("Obstype", sdh.obstype);
800  table_.keywordSet().get("AntennaName", sdh.antennaname);
801  table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
802  table_.keywordSet().get("Equinox", sdh.equinox);
803  table_.keywordSet().get("FreqRefFrame", sdh.freqref);
804  table_.keywordSet().get("FreqRefVal", sdh.reffreq);
805  table_.keywordSet().get("Bandwidth", sdh.bandwidth);
806  table_.keywordSet().get("UTC", sdh.utc);
807  return sdh;
808}
809void SDMemTable::makePersistent(const std::string& filename) {
810  table_.deepCopy(filename,Table::New);
811}
812
813Int SDMemTable::nScan() const {
814  Int n = 0;
815  ROScalarColumn<Int> scans(table_, "SCANID");
816  Int previous = -1;Int current=0;
817  for (uInt i=0; i< scans.nrow();i++) {
818    scans.getScalar(i,current);
819    if (previous != current) {
820      previous = current;
821      n++;
822    }
823  }
824  return n;
825}
826
827String SDMemTable::formatSec(Double x) {
828  Double xcop = x;
829  MVTime mvt(xcop/24./3600.);  // make days
830  if (x < 59.95)
831    return  String("   ") + mvt.string(MVTime::TIME_CLEAN_NO_HM, 7)+"s";
832  return mvt.string(MVTime::TIME_CLEAN_NO_H, 7)+" ";
833};
834
835std::string SDMemTable::summary()   {
836  ROScalarColumn<Int> scans(table_, "SCANID");
837  ROScalarColumn<String> srcs(table_, "SRCNAME");
838  ostringstream oss;
839  oss << endl;
840  oss << "--------------------------------------------------" << endl;
841  oss << " Scan Table Summary" << endl;
842  oss << "--------------------------------------------------" << endl;
843  oss.flags(std::ios_base::left);
844  oss << setw(15) << "Beams:" << setw(4) << nBeam() << endl
845      << setw(15) << "IFs:" << setw(4) << nIF() << endl
846      << setw(15) << "Polarisations:" << setw(4) << nPol() << endl
847      << setw(15) << "Channels:"  << setw(4) << nChan() << endl;
848  oss << endl;
849  String tmp;
850  table_.keywordSet().get("Observer", tmp);
851  oss << setw(15) << "Observer:" << tmp << endl;
852  table_.keywordSet().get("Project", tmp);
853  oss << setw(15) << "Project:" << tmp << endl;
854  table_.keywordSet().get("Obstype", tmp);
855  oss << setw(15) << "Obs. Type:" << tmp << endl;
856  table_.keywordSet().get("AntennaName", tmp);
857  oss << setw(15) << "Antenna Name:" << tmp << endl;
858  Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
859  Vector<Double> vec;
860  t.keywordSet().get("RESTFREQS",vec);
861  oss << setw(15) << "Rest Freqs:";
862  if (vec.nelements() > 0) {
863      oss << setprecision(0) << vec << " [Hz]" << endl;
864  } else {
865      oss << "None set" << endl;
866  }
867  oss << setw(15) << "Abcissa:" << getAbcissaString() << endl;
868  oss << setw(15) << "Cursor:" << "Beam[" << getBeam() << "] "
869      << "IF[" << getIF() << "] " << "Pol[" << getPol() << "]" << endl;
870  oss << endl;
871  uInt count = 0;
872  String name;
873  Int previous = -1;Int current=0;
874  Int integ = 0;
875  oss << setw(6) << "Scan"
876      << setw(12) << "Source"
877      << setw(21) << "Time"
878      << setw(11) << "Integration" << endl;
879  oss << "--------------------------------------------------" << endl;
880  for (uInt i=0; i< scans.nrow();i++) {
881    scans.getScalar(i,current);
882    if (previous != current) {
883      srcs.getScalar(i,name);
884      previous = current;
885      String t = formatSec(Double(getInterval(i)));
886      oss << setw(6) << count << setw(12) << name << setw(21) << getTime(i)
887          << setw(2) << setprecision(1)
888          << t << endl;
889      count++;
890    } else {
891      integ++;
892    }
893  }
894  oss << endl;
895  oss << "Table contains " << table_.nrow() << " integration(s)." << endl;
896  oss << "in " << count << " scan(s)." << endl;
897  oss << "--------------------------------------------------";
898  return String(oss);
899}
900
901Int SDMemTable::nBeam() const {
902  Int n;
903  table_.keywordSet().get("nBeam",n);
904  return n;
905}
906Int SDMemTable::nIF() const {
907  Int n;
908  table_.keywordSet().get("nIF",n);
909  return n;
910}
911Int SDMemTable::nPol() const {
912  Int n;
913  table_.keywordSet().get("nPol",n);
914  return n;
915}
916Int SDMemTable::nChan() const {
917  Int n;
918  table_.keywordSet().get("nChan",n);
919  return n;
920}
921/*
922void SDMemTable::maskChannels(const std::vector<Int>& whichChans ) {
923
924  std::vector<int>::iterator it;
925  ArrayAccessor<uChar, Axis<2> > j(flags_);
926  for (it = whichChans.begin(); it != whichChans.end(); it++) {
927    j.reset(j.begin(uInt(*it)));
928    for (ArrayAccessor<uChar, Axis<0> > i(j); i != i.end(); ++i) {
929      for (ArrayAccessor<uChar, Axis<1> > ii(i); ii != ii.end(); ++ii) {
930        for (ArrayAccessor<uChar, Axis<3> > iii(ii);
931             iii != iii.end(); ++iii) {
932          (*iii) =
933        }
934      }
935    }
936  }
937
938}
939*/
940void SDMemTable::flag(int whichRow) {
941  ArrayColumn<uChar> spec(table_, "FLAGTRA");
942  Array<uChar> arr;
943  spec.get(whichRow, arr);
944
945  ArrayAccessor<uChar, Axis<0> > aa0(arr);
946  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
947  ArrayAccessor<uChar, Axis<1> > aa1(aa0);
948  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
949  ArrayAccessor<uChar, Axis<2> > aa2(aa1);
950  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
951
952  for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
953    (*i) = uChar(True);
954  }
955
956  spec.put(whichRow, arr);
957}
Note: See TracBrowser for help on using the repository browser.