source: trunk/src/SDMemTable.cc @ 105

Last change on this file since 105 was 105, checked in by mar637, 20 years ago

added set/getCoordInfo, reworked summary, added columns for azimuth,elevation,parangle,refbeam,fieldname,tcal,tcaltime
modified getabscissa, added getAbscissaString
added doppler, frequency frame an unit conversion via columns

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