source: trunk/src/SDMemTable.cc @ 122

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

added restfrequency selector

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.0 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      if (rstf.nelements() >= nIF())
371        spc.selectRestFrequency(uInt(IFSel_));
372      spc.setVelocity(u.getName());
373      Vector<Double> wrld;
374      spc.pixelToVelocity(wrld,absc1);
375      std::vector<double>::iterator it;
376      uInt i = 0;
377      for (it = absc.begin(); it != absc.end(); ++it) {
378        (*it) = wrld[i];
379        i++;
380      }
381    }
382  } else if (u == Unit("Hz")) {
383    Vector<String> wau(1); wau = u.getName();
384    spc.setWorldAxisUnits(wau);
385    std::vector<double>::iterator it;
386    Double tmp;
387    uInt i = 0;
388    for (it = absc.begin(); it != absc.end(); ++it) {
389      spc.toWorld(tmp,absc1[i]);
390      (*it) = tmp;
391      i++;
392    }
393
394  } else {
395    // assume channels/pixels
396    std::vector<double>::iterator it;
397    uInt i=0;
398    for (it = absc.begin(); it != absc.end(); ++it) {
399      (*it) = Double(i++);
400    }
401  }
402  return absc;
403}
404
405std::string SDMemTable::getAbscissaString(Int whichRow)
406{
407  ROArrayColumn<uInt> fid(table_, "FREQID");
408  Table t = table_.keywordSet().asTable("FREQUENCIES");
409  String sunit;
410  t.keywordSet().get("UNIT",sunit);
411  if (sunit == "") sunit = "pixel";
412  Unit u(sunit);
413  Vector<uInt> v;
414  fid.get(whichRow, v);
415  uInt specidx = v(IFSel_);
416  SpectralCoordinate spc = getCoordinate(specidx);
417  String frm;
418  t.keywordSet().get("REFFRAME",frm);
419  MFrequency::Types mtype;
420  if (!MFrequency::getType(mtype, frm)) {
421    cout << "Frequency type unknown assuming TOPO" << endl;
422    mtype = MFrequency::TOPO;
423  }
424  spc.setFrequencySystem(mtype);
425  String s = "Channel";
426  if (u == Unit("km/s")) {
427    spc.setVelocity(u.getName());
428    s = CoordinateUtil::axisLabel(spc,0,True,True,True);
429  } else if (u == Unit("Hz")) {
430    Vector<String> wau(1);wau = u.getName();
431    spc.setWorldAxisUnits(wau);
432    s = CoordinateUtil::axisLabel(spc);
433  }
434  return s;
435}
436
437void SDMemTable::setSpectrum(std::vector<float> spectrum, int whichRow) {
438  ArrayColumn<Float> spec(table_, "SPECTRA");
439  Array<Float> arr;
440  spec.get(whichRow, arr);
441  if (spectrum.size() != arr.shape()(3)) {
442    throw(AipsError("Attempting to set spectrum with incorrect length."));
443  }
444
445  ArrayAccessor<Float, Axis<0> > aa0(arr);
446  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
447  ArrayAccessor<Float, Axis<1> > aa1(aa0);
448  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
449  ArrayAccessor<Float, Axis<2> > aa2(aa1);
450  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
451
452  std::vector<float>::iterator it = spectrum.begin();
453  for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
454    (*i) = Float(*it);
455    it++;
456  }
457  spec.put(whichRow, arr);
458}
459
460void SDMemTable::getSpectrum(Vector<Float>& spectrum, Int whichRow) {
461  ROArrayColumn<Float> spec(table_, "SPECTRA");
462  Array<Float> arr;
463  spec.get(whichRow, arr);
464  spectrum.resize(arr.shape()(3));
465  ArrayAccessor<Float, Axis<0> > aa0(arr);
466  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
467  ArrayAccessor<Float, Axis<1> > aa1(aa0);
468  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
469  ArrayAccessor<Float, Axis<2> > aa2(aa1);
470  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
471
472  ArrayAccessor<Float, Axis<0> > va(spectrum);
473  for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
474    (*va) = (*i);
475    va++;
476  }
477}
478/*
479void SDMemTable::getMask(Vector<Bool>& mask, Int whichRow) const {
480  ROArrayColumn<uChar> spec(table_, "FLAGTRA");
481  Array<uChar> arr;
482  spec.get(whichRow, arr);
483  mask.resize(arr.shape()(3));
484
485  ArrayAccessor<uChar, Axis<0> > aa0(arr);
486  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
487  ArrayAccessor<uChar, Axis<1> > aa1(aa0);
488  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
489  ArrayAccessor<uChar, Axis<2> > aa2(aa1);
490  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
491
492  Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
493
494  ArrayAccessor<Bool, Axis<0> > va(mask);
495  std::vector<bool> tmp;
496  tmp = chanMask_; // WHY the fxxx do I have to make a copy here. The
497                   // iterator should work on chanMask_??
498  std::vector<bool>::iterator miter;
499  miter = tmp.begin();
500
501  for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
502    bool out =!static_cast<bool>(*i);
503    if (useUserMask) {
504      out = out && (*miter);
505      miter++;
506    }
507    (*va) = out;
508    va++;
509  }
510}
511*/
512MaskedArray<Float> SDMemTable::rowAsMaskedArray(uInt whichRow,
513                                                Bool useSelection) {
514  ROArrayColumn<Float> spec(table_, "SPECTRA");
515  Array<Float> arr;
516  ROArrayColumn<uChar> flag(table_, "FLAGTRA");
517  Array<uChar> farr;
518  spec.get(whichRow, arr);
519  flag.get(whichRow, farr);
520  Array<Bool> barr(farr.shape());convertArray(barr, farr);
521  MaskedArray<Float> marr;
522  if (useSelection) {
523    ArrayAccessor<Float, Axis<0> > aa0(arr);
524    aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
525    ArrayAccessor<Float, Axis<1> > aa1(aa0);
526    aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
527    ArrayAccessor<Float, Axis<2> > aa2(aa1);
528    aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
529
530    ArrayAccessor<Bool, Axis<0> > baa0(barr);
531    baa0.reset(baa0.begin(uInt(beamSel_)));//go to beam
532    ArrayAccessor<Bool, Axis<1> > baa1(baa0);
533    baa1.reset(baa1.begin(uInt(IFSel_)));// go to IF
534    ArrayAccessor<Bool, Axis<2> > baa2(baa1);
535    baa2.reset(baa2.begin(uInt(polSel_)));// go to pol
536
537    Vector<Float> a(arr.shape()(3));
538    Vector<Bool> b(barr.shape()(3));
539    ArrayAccessor<Float, Axis<0> > a0(a);
540    ArrayAccessor<Bool, Axis<0> > b0(b);
541
542    ArrayAccessor<Bool, Axis<3> > j(baa2);
543    for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) {
544      (*a0) = (*i);
545      (*b0) = !(*j);
546      j++;
547      a0++;
548      b0++;
549    }
550    marr.setData(a,b);
551  } else {
552    marr.setData(arr,!barr);
553  }
554  return marr;
555}
556
557Float SDMemTable::getTsys(Int whichRow) const {
558  ROArrayColumn<Float> ts(table_, "TSYS");
559  Array<Float> arr;
560  ts.get(whichRow, arr);
561  Float out;
562  IPosition ip(arr.shape());
563  ip(0) = beamSel_;ip(1) = IFSel_;ip(2) = polSel_;ip(3)=0;
564  out = arr(ip);
565  return out;
566}
567
568SpectralCoordinate SDMemTable::getCoordinate(uInt whichIdx)  const {
569
570  Table t = table_.keywordSet().asTable("FREQUENCIES");
571  if (whichIdx > t.nrow() ) {
572    cerr << "SDMemTable::getCoordinate - whichIdx out of range" << endl;
573    return SpectralCoordinate();
574  }
575
576  Double rp,rv,inc;
577  String rf;
578  Vector<Double> vec;
579  ROScalarColumn<Double> rpc(t, "REFPIX");
580  ROScalarColumn<Double> rvc(t, "REFVAL");
581  ROScalarColumn<Double> incc(t, "INCREMENT");
582  t.keywordSet().get("RESTFREQS",vec);
583  t.keywordSet().get("BASEREFFRAME",rf);
584
585  MFrequency::Types mft;
586  if (!MFrequency::getType(mft, rf)) {
587    cerr << "Frequency type unknown assuming TOPO" << endl;
588    mft = MFrequency::TOPO;
589  }
590  rpc.get(whichIdx, rp);
591  rvc.get(whichIdx, rv);
592  incc.get(whichIdx, inc);
593  SpectralCoordinate spec(mft,rv,inc,rp);
594  if (vec.nelements() > 0)
595    spec.setRestFrequencies(vec);
596  return spec;
597}
598
599Bool SDMemTable::setCoordinate(const SpectralCoordinate& speccord,
600                               uInt whichIdx) {
601  Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
602  if (whichIdx > t.nrow() ) {
603    throw(AipsError("SDMemTable::setCoordinate - coord no out of range"));
604  }
605  ScalarColumn<Double> rpc(t, "REFPIX");
606  ScalarColumn<Double> rvc(t, "REFVAL");
607  ScalarColumn<Double> incc(t, "INCREMENT");
608
609  rpc.put(whichIdx, speccord.referencePixel()[0]);
610  rvc.put(whichIdx, speccord.referenceValue()[0]);
611  incc.put(whichIdx, speccord.increment()[0]);
612
613  return True;
614}
615
616Int SDMemTable::nCoordinates() const
617{
618  return table_.keywordSet().asTable("FREQUENCIES").nrow();
619}
620
621void SDMemTable::setRestFreqs(std::vector<double> freqs, const std::string& theunit)
622{
623  Vector<Double> tvec(freqs);
624  Quantum<Vector<Double> > q(tvec, String(theunit));
625  tvec.resize();
626  tvec = q.getValue("Hz");
627  Table t = table_.keywordSet().asTable("FREQUENCIES");
628  t.rwKeywordSet().define("RESTFREQS",tvec);
629}
630
631bool SDMemTable::putSDFreqTable(const SDFrequencyTable& sdft) {
632  TableDesc td("", "1", TableDesc::Scratch);
633  td.addColumn(ScalarColumnDesc<Double>("REFPIX"));
634  td.addColumn(ScalarColumnDesc<Double>("REFVAL"));
635  td.addColumn(ScalarColumnDesc<Double>("INCREMENT"));
636  SetupNewTable aNewTab("freqs", td, Table::New);
637  Table aTable (aNewTab, Table::Memory, sdft.length());
638  ScalarColumn<Double> sc0(aTable, "REFPIX");
639  ScalarColumn<Double> sc1(aTable, "REFVAL");
640  ScalarColumn<Double> sc2(aTable, "INCREMENT");
641  for (uInt i=0; i < sdft.length(); ++i) {
642    sc0.put(i,sdft.referencePixel(i));
643    sc1.put(i,sdft.referenceValue(i));
644    sc2.put(i,sdft.increment(i));
645  }
646  String rf = sdft.refFrame();
647  if (rf.contains("TOPO")) rf = "TOPO";
648
649  aTable.rwKeywordSet().define("BASEREFFRAME", rf);
650  aTable.rwKeywordSet().define("REFFRAME", rf);
651  aTable.rwKeywordSet().define("EQUINOX", sdft.equinox());
652  aTable.rwKeywordSet().define("UNIT", String(""));
653  aTable.rwKeywordSet().define("DOPPLER", String("RADIO"));
654  Vector<Double> rfvec;
655  aTable.rwKeywordSet().define("RESTFREQS", rfvec);
656  table_.rwKeywordSet().defineTable ("FREQUENCIES", aTable);
657  return True;
658}
659
660SDFrequencyTable SDMemTable::getSDFreqTable() const  {
661  SDFrequencyTable sdft;
662
663  return sdft;
664}
665
666bool SDMemTable::putSDContainer(const SDContainer& sdc) {
667  ScalarColumn<Double> mjd(table_, "TIME");
668  ScalarColumn<String> srcn(table_, "SRCNAME");
669  ScalarColumn<String> fldn(table_, "FIELDNAME");
670  ArrayColumn<Float> spec(table_, "SPECTRA");
671  ArrayColumn<uChar> flags(table_, "FLAGTRA");
672  ArrayColumn<Float> ts(table_, "TSYS");
673  ScalarColumn<Int> scan(table_, "SCANID");
674  ScalarColumn<Double> integr(table_, "INTERVAL");
675  ArrayColumn<uInt> freqid(table_, "FREQID");
676  ArrayColumn<Double> dir(table_, "DIRECTION");
677  ScalarColumn<Int> rbeam(table_, "REFBEAM");
678  ArrayColumn<Float> tcal(table_, "TCAL");
679  ScalarColumn<String> tcalt(table_, "TCALTIME");
680  ScalarColumn<Float> az(table_, "AZIMUTH");
681  ScalarColumn<Float> el(table_, "ELEVATION");
682  ScalarColumn<Float> para(table_, "PARANGLE");
683
684  uInt rno = table_.nrow();
685  table_.addRow();
686
687  mjd.put(rno, sdc.timestamp);
688  srcn.put(rno, sdc.sourcename);
689  fldn.put(rno, sdc.fieldname);
690  spec.put(rno, sdc.getSpectrum());
691  flags.put(rno, sdc.getFlags());
692  ts.put(rno, sdc.getTsys());
693  scan.put(rno, sdc.scanid);
694  integr.put(rno, sdc.interval);
695  freqid.put(rno, sdc.getFreqMap());
696  dir.put(rno, sdc.getDirection());
697  rbeam.put(rno, sdc.refbeam);
698  tcal.put(rno, sdc.tcal);
699  tcalt.put(rno, sdc.tcaltime);
700  az.put(rno, sdc.azimuth);
701  el.put(rno, sdc.elevation);
702  para.put(rno, sdc.parangle);
703
704  return true;
705}
706
707SDContainer SDMemTable::getSDContainer(uInt whichRow) const {
708  ROScalarColumn<Double> mjd(table_, "TIME");
709  ROScalarColumn<String> srcn(table_, "SRCNAME");
710  ROScalarColumn<String> fldn(table_, "FIELDNAME");
711  ROArrayColumn<Float> spec(table_, "SPECTRA");
712  ROArrayColumn<uChar> flags(table_, "FLAGTRA");
713  ROArrayColumn<Float> ts(table_, "TSYS");
714  ROScalarColumn<Int> scan(table_, "SCANID");
715  ROScalarColumn<Double> integr(table_, "INTERVAL");
716  ROArrayColumn<uInt> freqid(table_, "FREQID");
717  ROArrayColumn<Double> dir(table_, "DIRECTION");
718  ROScalarColumn<Int> rbeam(table_, "REFBEAM");
719  ROArrayColumn<Float> tcal(table_, "TCAL");
720  ROScalarColumn<String> tcalt(table_, "TCALTIME");
721  ROScalarColumn<Float> az(table_, "AZIMUTH");
722  ROScalarColumn<Float> el(table_, "ELEVATION");
723  ROScalarColumn<Float> para(table_, "PARANGLE");
724
725  SDContainer sdc(nBeam(),nIF(),nPol(),nChan());
726  mjd.get(whichRow, sdc.timestamp);
727  srcn.get(whichRow, sdc.sourcename);
728  integr.get(whichRow, sdc.interval);
729  scan.get(whichRow, sdc.scanid);
730  fldn.get(whichRow, sdc.fieldname);
731  rbeam.get(whichRow, sdc.refbeam);
732  az.get(whichRow, sdc.azimuth);
733  el.get(whichRow, sdc.elevation);
734  para.get(whichRow, sdc.parangle);
735  Vector<Float> tc;
736  tcal.get(whichRow, tc);
737  sdc.tcal[0] = tc[0];sdc.tcal[1] = tc[1];
738  tcalt.get(whichRow, sdc.tcaltime);
739  Array<Float> spectrum;
740  Array<Float> tsys;
741  Array<uChar> flagtrum;
742  Vector<uInt> fmap;
743  Array<Double> direction;
744  spec.get(whichRow, spectrum);
745  sdc.putSpectrum(spectrum);
746  flags.get(whichRow, flagtrum);
747  sdc.putFlags(flagtrum);
748  ts.get(whichRow, tsys);
749  sdc.putTsys(tsys);
750  freqid.get(whichRow, fmap);
751  sdc.putFreqMap(fmap);
752  dir.get(whichRow, direction);
753  sdc.putDirection(direction);
754  return sdc;
755}
756
757bool SDMemTable::putSDHeader(const SDHeader& sdh) {
758  table_.lock();
759  table_.rwKeywordSet().define("nIF", sdh.nif);
760  table_.rwKeywordSet().define("nBeam", sdh.nbeam);
761  table_.rwKeywordSet().define("nPol", sdh.npol);
762  table_.rwKeywordSet().define("nChan", sdh.nchan);
763  table_.rwKeywordSet().define("Observer", sdh.observer);
764  table_.rwKeywordSet().define("Project", sdh.project);
765  table_.rwKeywordSet().define("Obstype", sdh.obstype);
766  table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
767  table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
768  table_.rwKeywordSet().define("Equinox", sdh.equinox);
769  table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
770  table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
771  table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
772  table_.rwKeywordSet().define("UTC", sdh.utc);
773  table_.unlock();
774  return true;
775}
776
777SDHeader SDMemTable::getSDHeader() const {
778  SDHeader sdh;
779  table_.keywordSet().get("nBeam",sdh.nbeam);
780  table_.keywordSet().get("nIF",sdh.nif);
781  table_.keywordSet().get("nPol",sdh.npol);
782  table_.keywordSet().get("nChan",sdh.nchan);
783  table_.keywordSet().get("Observer", sdh.observer);
784  table_.keywordSet().get("Project", sdh.project);
785  table_.keywordSet().get("Obstype", sdh.obstype);
786  table_.keywordSet().get("AntennaName", sdh.antennaname);
787  table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
788  table_.keywordSet().get("Equinox", sdh.equinox);
789  table_.keywordSet().get("FreqRefFrame", sdh.freqref);
790  table_.keywordSet().get("FreqRefVal", sdh.reffreq);
791  table_.keywordSet().get("Bandwidth", sdh.bandwidth);
792  table_.keywordSet().get("UTC", sdh.utc);
793  return sdh;
794}
795void SDMemTable::makePersistent(const std::string& filename) {
796  table_.deepCopy(filename,Table::New);
797}
798
799Int SDMemTable::nScan() const {
800  Int n = 0;
801  ROScalarColumn<Int> scans(table_, "SCANID");
802  Int previous = -1;Int current=0;
803  for (uInt i=0; i< scans.nrow();i++) {
804    scans.getScalar(i,current);
805    if (previous != current) {
806      previous = current;
807      n++;
808    }
809  }
810  return n;
811}
812
813String SDMemTable::formatSec(Double x) {
814  Double xcop = x;
815  MVTime mvt(xcop/24./3600.);  // make days
816  if (x < 59.95)
817    return  String("   ") + mvt.string(MVTime::TIME_CLEAN_NO_HM, 7)+"s";
818  return mvt.string(MVTime::TIME_CLEAN_NO_H, 7)+" ";
819};
820
821std::string SDMemTable::summary()  {
822  ROScalarColumn<Int> scans(table_, "SCANID");
823  ROScalarColumn<String> srcs(table_, "SRCNAME");
824  ostringstream oss;
825  oss << endl;
826  oss << "--------------------------------------------------" << endl;
827  oss << " Scan Table Summary" << endl;
828  oss << "--------------------------------------------------" << endl;
829  oss.flags(std::ios_base::left);
830  oss << setw(15) << "Beams:" << setw(4) << nBeam() << endl
831      << setw(15) << "IFs:" << setw(4) << nIF() << endl
832      << setw(15) << "Polarisations:" << setw(4) << nPol() << endl
833      << setw(15) << "Channels:"  << setw(4) << nChan() << endl;
834  oss << endl;
835  String tmp;
836  table_.keywordSet().get("Observer", tmp);
837  oss << setw(15) << "Observer:" << tmp << endl;
838  table_.keywordSet().get("Project", tmp);
839  oss << setw(15) << "Project:" << tmp << endl;
840  table_.keywordSet().get("Obstype", tmp);
841  oss << setw(15) << "Obs. Type:" << tmp << endl;
842  table_.keywordSet().get("AntennaName", tmp);
843  oss << setw(15) << "Antenna Name:" << tmp << endl;
844  Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
845  Vector<Double> vec;
846  t.keywordSet().get("RESTFREQS",vec);
847  oss << setw(15) << "Rest Freqs:";
848  if (vec.nelements() > 0) {
849      oss << setprecision(0) << vec << " [Hz]" << endl;
850  } else {
851      oss << "None set" << endl;
852  }
853  oss << setw(15) << "Abscissa:" << getAbscissaString() << endl;
854  oss << setw(15) << "Cursor:" << "Beam[" << getBeam() << "] "
855      << "IF[" << getIF() << "] " << "Pol[" << getPol() << "]" << endl;
856  oss << endl;
857  uInt count = 0;
858  String name;
859  Int previous = -1;Int current=0;
860  Int integ = 0;
861  oss << setw(6) << "Scan"
862      << setw(12) << "Source"
863      << setw(21) << "Time"
864      << setw(11) << "Integration" << endl;
865  oss << "--------------------------------------------------" << endl;
866  for (uInt i=0; i< scans.nrow();i++) {
867    scans.getScalar(i,current);
868    if (previous != current) {
869      srcs.getScalar(i,name);
870      previous = current;
871      String t = formatSec(Double(getInterval(i)));
872      oss << setw(6) << count << setw(12) << name << setw(21) << getTime(i)
873          << setw(2) << setprecision(1)
874          << t << endl;
875      count++;
876    } else {
877      integ++;
878    }
879  }
880  oss << endl;
881  oss << "Table contains " << table_.nrow() << " integration(s)." << endl;
882  oss << "in " << count << " scan(s)." << endl;
883  oss << "--------------------------------------------------";
884  return String(oss);
885}
886
887Int SDMemTable::nBeam() const {
888  Int n;
889  table_.keywordSet().get("nBeam",n);
890  return n;
891}
892Int SDMemTable::nIF() const {
893  Int n;
894  table_.keywordSet().get("nIF",n);
895  return n;
896}
897Int SDMemTable::nPol() const {
898  Int n;
899  table_.keywordSet().get("nPol",n);
900  return n;
901}
902Int SDMemTable::nChan() const {
903  Int n;
904  table_.keywordSet().get("nChan",n);
905  return n;
906}
907/*
908void SDMemTable::maskChannels(const std::vector<Int>& whichChans ) {
909
910  std::vector<int>::iterator it;
911  ArrayAccessor<uChar, Axis<2> > j(flags_);
912  for (it = whichChans.begin(); it != whichChans.end(); it++) {
913    j.reset(j.begin(uInt(*it)));
914    for (ArrayAccessor<uChar, Axis<0> > i(j); i != i.end(); ++i) {
915      for (ArrayAccessor<uChar, Axis<1> > ii(i); ii != ii.end(); ++ii) {
916        for (ArrayAccessor<uChar, Axis<3> > iii(ii);
917             iii != iii.end(); ++iii) {
918          (*iii) =
919        }
920      }
921    }
922  }
923
924}
925*/
926void SDMemTable::flag(int whichRow) {
927  ArrayColumn<uChar> spec(table_, "FLAGTRA");
928  Array<uChar> arr;
929  spec.get(whichRow, arr);
930
931  ArrayAccessor<uChar, Axis<0> > aa0(arr);
932  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
933  ArrayAccessor<uChar, Axis<1> > aa1(aa0);
934  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
935  ArrayAccessor<uChar, Axis<2> > aa2(aa1);
936  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
937
938  for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) {
939    (*i) = uChar(True);
940  }
941
942  spec.put(whichRow, arr);
943}
Note: See TracBrowser for help on using the repository browser.