source: trunk/src/SDMemTable.cc @ 125

Last change on this file since 125 was 125, checked in by mar637, 19 years ago

Moved to casa namespace.
Adjusted the copyright to be ATNF.

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