source: trunk/src/SDMemTable.cc @ 527

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

ficed bug in getSDFitTable

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 52.4 KB
RevLine 
[2]1//#---------------------------------------------------------------------------
2//# SDMemTable.cc: A MemoryTable container for single dish integrations
3//#---------------------------------------------------------------------------
4//# Copyright (C) 2004
[125]5//# ATNF
[2]6//#
7//# This program is free software; you can redistribute it and/or modify it
8//# under the terms of the GNU General Public License as published by the Free
9//# Software Foundation; either version 2 of the License, or (at your option)
10//# any later version.
11//#
12//# This program is distributed in the hope that it will be useful, but
13//# WITHOUT ANY WARRANTY; without even the implied warranty of
14//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15//# Public License for more details.
16//#
17//# You should have received a copy of the GNU General Public License along
18//# with this program; if not, write to the Free Software Foundation, Inc.,
19//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20//#
21//# Correspondence concerning this software should be addressed as follows:
22//#        Internet email: Malte.Marquarding@csiro.au
23//#        Postal address: Malte Marquarding,
24//#                        Australia Telescope National Facility,
25//#                        P.O. Box 76,
26//#                        Epping, NSW, 2121,
27//#                        AUSTRALIA
28//#
29//# $Id:
30//#---------------------------------------------------------------------------
31
[206]32#include <map>
33
[125]34#include <casa/aips.h>
[80]35#include <casa/iostream.h>
36#include <casa/iomanip.h>
37#include <casa/Arrays/Array.h>
38#include <casa/Arrays/ArrayMath.h>
39#include <casa/Arrays/MaskArrMath.h>
40#include <casa/Arrays/ArrayLogical.h>
41#include <casa/Arrays/ArrayAccessor.h>
[455]42#include <casa/Arrays/VectorSTLIterator.h>
[206]43#include <casa/Arrays/Vector.h>
[418]44#include <casa/BasicMath/Math.h>
[504]45#include <casa/BasicSL/Constants.h>
[286]46#include <casa/Quanta/MVAngle.h>
[2]47
[80]48#include <tables/Tables/TableParse.h>
49#include <tables/Tables/TableDesc.h>
[488]50#include <tables/Tables/TableCopy.h>
[80]51#include <tables/Tables/SetupNewTab.h>
52#include <tables/Tables/ScaColDesc.h>
53#include <tables/Tables/ArrColDesc.h>
[2]54
[80]55#include <tables/Tables/ExprNode.h>
56#include <tables/Tables/TableRecord.h>
57#include <measures/Measures/MFrequency.h>
58#include <measures/Measures/MeasTable.h>
[105]59#include <coordinates/Coordinates/CoordinateUtil.h>
[80]60#include <casa/Quanta/MVTime.h>
[281]61#include <casa/Quanta/MVAngle.h>
[2]62
[215]63#include "SDDefs.h"
[2]64#include "SDContainer.h"
[365]65#include "MathUtils.h"
[418]66#include "SDPol.h"
[476]67#include "SDAttr.h"
[2]68
[465]69#include "SDMemTable.h"
[206]70
[125]71using namespace casa;
[83]72using namespace asap;
[2]73
[18]74SDMemTable::SDMemTable() :
75  IFSel_(0),
76  beamSel_(0),
[206]77  polSel_(0)
78{
[18]79  setup();
[322]80  attach();
[18]81}
[206]82
[2]83SDMemTable::SDMemTable(const std::string& name) :
84  IFSel_(0),
85  beamSel_(0),
[206]86  polSel_(0)
87{
[50]88  Table tab(name);
[499]89  Int version;
[483]90  tab.keywordSet().get("VERSION", version);
91  if (version != version_) {
92    throw(AipsError("Unsupported version of ASAP file."));
93  }
[22]94  table_ = tab.copyToMemoryTable("dummy");
[329]95  attach();
[2]96}
97
[206]98SDMemTable::SDMemTable(const SDMemTable& other, Bool clear)
99{
[148]100  table_ = other.table_.copyToMemoryTable(String("dummy"));
[2]101  // clear all rows()
[16]102  if (clear) {
[148]103    table_.removeRow(this->table_.rowNumbers());
[483]104    IFSel_= 0;
105    beamSel_= 0;
106    polSel_= 0;
107    chanMask_.resize(0);
[16]108  } else {
[483]109    IFSel_= other.IFSel_;
110    beamSel_= other.beamSel_;
111    polSel_= other.polSel_;
112    chanMask_.resize(0);
113    chanMask_ = other.chanMask_;
[16]114  }
[483]115
[322]116  attach();
[2]117}
118
[80]119SDMemTable::SDMemTable(const Table& tab, const std::string& exprs) :
[2]120  IFSel_(0),
121  beamSel_(0),
[206]122  polSel_(0)
123{
[2]124  Table t = tableCommand(exprs,tab);
[89]125  if (t.nrow() == 0)
126      throw(AipsError("Query unsuccessful."));
[22]127  table_ = t.copyToMemoryTable("dummy");
[329]128  attach();
[380]129  renumber();
[2]130}
131
[206]132SDMemTable::~SDMemTable()
133{
[105]134  //cerr << "goodbye from SDMemTable @ " << this << endl;
[2]135}
136
[206]137SDMemTable SDMemTable::getScan(Int scanID) const
138{
[80]139  String cond("SELECT * from $1 WHERE SCANID == ");
140  cond += String::toString(scanID);
141  return SDMemTable(table_, cond);
[2]142}
143
[206]144SDMemTable &SDMemTable::operator=(const SDMemTable& other)
145{
[148]146  if (this != &other) {
147     IFSel_= other.IFSel_;
148     beamSel_= other.beamSel_;
149     polSel_= other.polSel_;
150     chanMask_.resize(0);
151     chanMask_ = other.chanMask_;
152     table_ = other.table_.copyToMemoryTable(String("dummy"));
[322]153     attach();
[206]154  }
[138]155  return *this;
156}
157
[206]158SDMemTable SDMemTable::getSource(const std::string& source) const
159{
[80]160  String cond("SELECT * from $1 WHERE SRCNAME == ");
161  cond += source;
162  return SDMemTable(table_, cond);
163}
164
[206]165void SDMemTable::setup()
166{
[2]167  TableDesc td("", "1", TableDesc::Scratch);
168  td.comment() = "A SDMemTable";
[483]169  td.rwKeywordSet().define("VERSION", Int(version_));
170
[2]171  td.addColumn(ScalarColumnDesc<Double>("TIME"));
172  td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
173  td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
174  td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
[89]175  td.addColumn(ArrayColumnDesc<Float>("TSYS"));
[418]176  td.addColumn(ArrayColumnDesc<Float>("STOKES"));
[89]177  td.addColumn(ScalarColumnDesc<Int>("SCANID"));
178  td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
[39]179  td.addColumn(ArrayColumnDesc<uInt>("FREQID"));
[386]180  td.addColumn(ArrayColumnDesc<uInt>("RESTFREQID"));
[78]181  td.addColumn(ArrayColumnDesc<Double>("DIRECTION"));
[105]182  td.addColumn(ScalarColumnDesc<String>("FIELDNAME"));
183  td.addColumn(ScalarColumnDesc<String>("TCALTIME"));
184  td.addColumn(ArrayColumnDesc<Float>("TCAL"));
185  td.addColumn(ScalarColumnDesc<Float>("AZIMUTH"));
186  td.addColumn(ScalarColumnDesc<Float>("ELEVATION"));
187  td.addColumn(ScalarColumnDesc<Float>("PARANGLE"));
188  td.addColumn(ScalarColumnDesc<Int>("REFBEAM"));
[455]189  td.addColumn(ArrayColumnDesc<Int>("FITID"));
[105]190
[418]191  // Now create Table SetUp from the description.
[22]192  SetupNewTable aNewTab("dummy", td, Table::New);
[418]193
[455]194  // Bind the Stokes Virtual machine to the STOKES column Because we
195  // don't know how many polarizations will be in the data at this
196  // point, we must bind the Virtual Engine regardless.  The STOKES
197  // column won't be accessed if not appropriate (nPol=4)
[483]198  SDStokesEngine::registerClass();
199  SDStokesEngine stokesEngine(String("STOKES"), String("SPECTRA"));
200  aNewTab.bindColumn("STOKES", stokesEngine);
201 
202  // Create Table
203  table_ = Table(aNewTab, Table::Memory, 0);
[455]204  // add subtable
205  TableDesc tdf("", "1", TableDesc::Scratch);
206  tdf.addColumn(ArrayColumnDesc<String>("FUNCTIONS"));
207  tdf.addColumn(ArrayColumnDesc<Int>("COMPONENTS"));
208  tdf.addColumn(ArrayColumnDesc<Double>("PARAMETERS"));
209  tdf.addColumn(ArrayColumnDesc<Bool>("PARMASK"));
[465]210  tdf.addColumn(ArrayColumnDesc<String>("FRAMEINFO"));
[455]211  SetupNewTable fittab("fits", tdf, Table::New);
212  Table fitTable(fittab, Table::Memory);
213  table_.rwKeywordSet().defineTable("FITS", fitTable);
[418]214
[483]215  TableDesc tdh("", "1", TableDesc::Scratch);
216  tdh.addColumn(ScalarColumnDesc<String>("ITEM"));
217  SetupNewTable histtab("hist", tdh, Table::New);
218  Table histTable(histtab, Table::Memory);
219  table_.rwKeywordSet().defineTable("HISTORY", histTable);
[2]220}
221
[380]222void SDMemTable::attach()
[322]223{
224  timeCol_.attach(table_, "TIME");
225  srcnCol_.attach(table_, "SRCNAME");
226  specCol_.attach(table_, "SPECTRA");
227  flagsCol_.attach(table_, "FLAGTRA");
228  tsCol_.attach(table_, "TSYS");
[418]229  stokesCol_.attach(table_, "STOKES");
[322]230  scanCol_.attach(table_, "SCANID");
231  integrCol_.attach(table_, "INTERVAL");
232  freqidCol_.attach(table_, "FREQID");
[386]233  restfreqidCol_.attach(table_, "RESTFREQID");
[322]234  dirCol_.attach(table_, "DIRECTION");
235  fldnCol_.attach(table_, "FIELDNAME");
236  tcaltCol_.attach(table_, "TCALTIME");
237  tcalCol_.attach(table_, "TCAL");
238  azCol_.attach(table_, "AZIMUTH");
239  elCol_.attach(table_, "ELEVATION");
240  paraCol_.attach(table_, "PARANGLE");
241  rbeamCol_.attach(table_, "REFBEAM");
[455]242  fitCol_.attach(table_,"FITID");
[322]243}
244
245
[206]246std::string SDMemTable::getSourceName(Int whichRow) const
247{
[2]248  String name;
[322]249  srcnCol_.get(whichRow, name);
[2]250  return name;
251}
252
[281]253std::string SDMemTable::getTime(Int whichRow, Bool showDate) const
[206]254{
[2]255  Double tm;
[281]256  if (whichRow > -1) {
[322]257    timeCol_.get(whichRow, tm);
[281]258  } else {
259    table_.keywordSet().get("UTC",tm);
260  }
[50]261  MVTime mvt(tm);
[281]262  if (showDate)
263    mvt.setFormat(MVTime::YMD);
264  else
265    mvt.setFormat(MVTime::TIME);
[50]266  ostringstream oss;
267  oss << mvt;
[281]268  return String(oss);
[2]269}
[281]270
[206]271double SDMemTable::getInterval(Int whichRow) const
272{
[50]273  Double intval;
[322]274  integrCol_.get(whichRow, intval);
[50]275  return intval;
276}
[2]277
[206]278bool SDMemTable::setIF(Int whichIF)
279{
[50]280  if ( whichIF >= 0 && whichIF < nIF()) {
[2]281    IFSel_ = whichIF;
282    return true;
[50]283  }
284  return false;
[2]285}
[50]286
[206]287bool SDMemTable::setBeam(Int whichBeam)
288{
[50]289  if ( whichBeam >= 0 && whichBeam < nBeam()) {
[2]290    beamSel_ = whichBeam;
291    return true;
[50]292  }
293  return false;
294}
[2]295
[206]296bool SDMemTable::setPol(Int whichPol)
297{
[50]298  if ( whichPol >= 0 && whichPol < nPol()) {
[2]299    polSel_ = whichPol;
300    return true;
[50]301  }
302  return false;
[2]303}
304
[380]305void SDMemTable::resetCursor()
[303]306{
307   polSel_ = 0;
308   IFSel_ = 0;
309   beamSel_ = 0;
310}
311
[206]312bool SDMemTable::setMask(std::vector<int> whichChans)
313{
[16]314  std::vector<int>::iterator it;
[322]315  uInt n = flagsCol_.shape(0)(3);
[105]316  if (whichChans.empty()) {
317    chanMask_ = std::vector<bool>(n,true);
318    return true;     
319  }
[16]320  chanMask_.resize(n,true);
[39]321  for (it = whichChans.begin(); it != whichChans.end(); ++it) {
[105]322    if (*it < n) {
[16]323      chanMask_[*it] = false;
[105]324    }
[16]325  }
[2]326  return true;
327}
328
[447]329std::vector<bool> SDMemTable::getMask(Int whichRow) const
330{
331
[16]332  std::vector<bool> mask;
[455]333
[16]334  Array<uChar> arr;
[322]335  flagsCol_.get(whichRow, arr);
[455]336
[206]337  ArrayAccessor<uChar, Axis<asap::BeamAxis> > aa0(arr);
[16]338  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
[206]339  ArrayAccessor<uChar, Axis<asap::IFAxis> > aa1(aa0);
[16]340  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
[206]341  ArrayAccessor<uChar, Axis<asap::PolAxis> > aa2(aa1);
[16]342  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
343
344  Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
345
346  std::vector<bool> tmp;
347  tmp = chanMask_; // WHY the fxxx do I have to make a copy here
348  std::vector<bool>::iterator miter;
349  miter = tmp.begin();
350
[206]351  for (ArrayAccessor<uChar, Axis<asap::ChanAxis> > i(aa2); i != i.end(); ++i) {
[16]352    bool out =!static_cast<bool>(*i);
353    if (useUserMask) {
354      out = out && (*miter);
355      miter++;
356    }
357    mask.push_back(out);
[89]358  }
[16]359  return mask;
[2]360}
[418]361
362
363
[206]364std::vector<float> SDMemTable::getSpectrum(Int whichRow) const
365{
[2]366  Array<Float> arr;
[322]367  specCol_.get(whichRow, arr);
[455]368  return getFloatSpectrum(arr);
[418]369}
370
[490]371
[493]372int SDMemTable::nStokes() const
[490]373{
374   return stokesCol_.shape(0).nelements();        // All rows same shape
375}
376
377
[527]378std::vector<float> SDMemTable::getStokesSpectrum(Int whichRow,
379                                                 Bool doPol) const
380  //
381  // Gets one STokes parameter depending on cursor polSel location
382  //  doPol=False  : I,Q,U,V
383  //  doPol=True   : I,P,PA,V   ; P = sqrt(Q**2+U**2), PA = 0.5*atan2(Q,U)
384  //
[418]385{
[430]386  AlwaysAssert(asap::nAxes==4,AipsError);
[428]387  if (nPol()!=1 && nPol()!=2 && nPol()!=4) {
[527]388    throw (AipsError("You must have 1,2 or 4 polarizations to get the Stokes parameters"));
[418]389  }
[527]390 
391  // For full conversion we are only supporting linears at the moment
392 
[521]393  if (nPol() > 2) {
[527]394    String antName;
395    table_.keywordSet().get("AntennaName", antName);
396    Instrument inst = SDAttr::convertInstrument (antName, True);
397    SDAttr sdAtt;
398    if (sdAtt.feedPolType(inst) != LINEAR) {
399      throw(AipsError("Only linear polarizations are supported"));
400    }
[521]401  }
[527]402
[418]403  Array<Float> arr;
404  stokesCol_.get(whichRow, arr);
[527]405 
[430]406  if (doPol && (polSel_==1 || polSel_==2)) {       //   Q,U --> P, P.A.
[527]407   
408    // Set current cursor location
409   
410    const IPosition& shape = arr.shape();
411    IPosition start, end;
412    getCursorSlice(start, end, shape);
413   
414    // Get Q and U slices
415   
416    Array<Float> Q = SDPolUtil::getStokesSlice(arr,start,end,"Q");
417    Array<Float> U = SDPolUtil::getStokesSlice(arr,start,end,"U");
418   
419    // Compute output
420   
421    Array<Float> out;
422    if (polSel_==1) {                                        // P
423      out = SDPolUtil::polarizedIntensity(Q,U);
424    } else if (polSel_==2) {                                 // P.A.
425      out = SDPolUtil::positionAngle(Q,U);
426    }
427   
428    // Copy to output
429   
430    IPosition vecShape(1,shape(asap::ChanAxis));
431    Vector<Float> outV = out.reform(vecShape);
432    std::vector<float> stlout;
433    outV.tovector(stlout);
434    return stlout;
435   
436  } else {   
[455]437    // Selects at the cursor location
438    return getFloatSpectrum(arr);
[418]439  }
440}
441
[493]442std::string SDMemTable::getPolarizationLabel (Bool linear, Bool stokes, Bool linPol, Int polIdx) const
[490]443{
[493]444   uInt idx = polSel_;   
445   if (polIdx >=0) idx = polIdx;
[490]446//
[493]447   return SDPolUtil::polarizationLabel (idx, linear, stokes, linPol);
[490]448}
449
450
451
[493]452std::vector<float> SDMemTable::stokesToPolSpectrum (Int whichRow, Bool toLinear, uInt polIdx) const
453//
454// polIdx
455//   0:3 -> RR,LL,Real(RL),Imag(RL)
456//          XX,YY,Real(XY),Image(XY)
457//
458// Gets only
459//  RR = I + V
460//  LL = I - V
461// at the moment
462//
[430]463{
464  AlwaysAssert(asap::nAxes==4,AipsError);
[493]465  if (nStokes()!=4) {
466     throw (AipsError("You must have 4 Stokes to convert to linear or circular"));
[430]467  }
[493]468//
469  Array<Float> arr, out;
[430]470  stokesCol_.get(whichRow, arr);
471
[493]472// Set current cursor location
[430]473
474  const IPosition& shape = arr.shape();
475  IPosition start, end;
[483]476  getCursorSlice(start, end, shape);
[430]477
[493]478// Get the slice
[430]479
[493]480  if (toLinear) {
481     throw(AipsError("Conversion to linears not yet supported"));
482  } else {
483     Bool doRR = (polIdx==0);
484     if(polIdx>1) {
485        throw(AipsError("Only conversion to RR & LL is currently supported"));
486     }
[430]487
[493]488// Get I and V slices
[430]489
[493]490     Array<Float> I = SDPolUtil::getStokesSlice(arr,start,end,"I");
491     Array<Float> V = SDPolUtil::getStokesSlice(arr,start,end,"V");
[430]492
[493]493// Compute output
[430]494
[493]495     out = SDPolUtil::circularPolarizationFromStokes(I, V, doRR);
496   }
[455]497
[493]498// Copy to output
[430]499
[493]500   IPosition vecShape(1,shape(asap::ChanAxis));
501   Vector<Float> outV = out.reform(vecShape);
502   std::vector<float> stlout;
503   outV.tovector(stlout);
[490]504//
[493]505   return stlout;
[490]506}
507
508
509
510
[472]511Array<Float> SDMemTable::getStokesSpectrum(Int whichRow, Int iBeam, Int iIF) const
512{
[418]513
[472]514// Get data
515
516  Array<Float> arr;
517  stokesCol_.get(whichRow, arr);
518
519// Set current cursor location and overwrite polarization axis
520
521  const IPosition& shape = arr.shape();
522  IPosition start(shape.nelements(),0); 
523  IPosition end(shape-1);
524  if (iBeam!=-1) {
525     start(asap::BeamAxis) = iBeam;
526     end(asap::BeamAxis) = iBeam;
527  }
528  if (iIF!=-1) {
529     start(asap::IFAxis) = iIF;
530     end(asap::IFAxis) = iIF;
531  }
532
533// Get slice
534
535  return arr(start,end);
536}
537
538
[206]539std::vector<string> SDMemTable::getCoordInfo() const
540{
[105]541  String un;
542  Table t = table_.keywordSet().asTable("FREQUENCIES");
543  String sunit;
544  t.keywordSet().get("UNIT",sunit);
545  String dpl;
546  t.keywordSet().get("DOPPLER",dpl);
547  if (dpl == "") dpl = "RADIO";
548  String rfrm;
549  t.keywordSet().get("REFFRAME",rfrm);
550  std::vector<string> inf;
551  inf.push_back(sunit);
552  inf.push_back(rfrm);
553  inf.push_back(dpl);
[263]554  t.keywordSet().get("BASEREFFRAME",rfrm);
555  inf.push_back(rfrm);
[105]556  return inf;
557}
[39]558
[206]559void SDMemTable::setCoordInfo(std::vector<string> theinfo)
560{
[105]561  std::vector<string>::iterator it;
[306]562  String un,rfrm, brfrm,dpl;
563  un = theinfo[0];              // Abcissa unit
564  rfrm = theinfo[1];            // Active (or conversion) frame
565  dpl = theinfo[2];             // Doppler
566  brfrm = theinfo[3];           // Base frame
[455]567
[105]568  Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
[455]569
[105]570  Vector<Double> rstf;
571  t.keywordSet().get("RESTFREQS",rstf);
[455]572
[105]573  Bool canDo = True;
574  Unit u1("km/s");Unit u2("Hz");
575  if (Unit(un) == u1) {
576    Vector<Double> rstf;
577    t.keywordSet().get("RESTFREQS",rstf);
578    if (rstf.nelements() == 0) {
579        throw(AipsError("Can't set unit to km/s if no restfrequencies are specified"));
580    }
581  } else if (Unit(un) != u2 && un != "") {
582        throw(AipsError("Unit not conformant with Spectral Coordinates"));
583  }
584  t.rwKeywordSet().define("UNIT", un);
[455]585
[105]586  MFrequency::Types mdr;
587  if (!MFrequency::getType(mdr, rfrm)) {
588   
589    Int a,b;const uInt* c;
590    const String* valid = MFrequency::allMyTypes(a, b, c);
591    String pfix = "Please specify a legal frame type. Types are\n";
592    throw(AipsError(pfix+(*valid)));
593  } else {
594    t.rwKeywordSet().define("REFFRAME",rfrm);
595  }
[455]596
[275]597  MDoppler::Types dtype;
598  dpl.upcase();
599  if (!MDoppler::getType(dtype, dpl)) {
600    throw(AipsError("Doppler type unknown"));
601  } else {
602    t.rwKeywordSet().define("DOPPLER",dpl);
603  }
[455]604
[306]605  if (!MFrequency::getType(mdr, brfrm)) {
606     Int a,b;const uInt* c;
607     const String* valid = MFrequency::allMyTypes(a, b, c);
608     String pfix = "Please specify a legal frame type. Types are\n";
609     throw(AipsError(pfix+(*valid)));
610   } else {
611    t.rwKeywordSet().define("BASEREFFRAME",brfrm);
612   }
[105]613}
614
[286]615
[206]616std::vector<double> SDMemTable::getAbcissa(Int whichRow) const
617{
[286]618  std::vector<double> abc(nChan());
[206]619
[455]620  // Get header units keyword
[286]621  Table t = table_.keywordSet().asTable("FREQUENCIES");
[105]622  String sunit;
623  t.keywordSet().get("UNIT",sunit);
624  if (sunit == "") sunit = "pixel";
625  Unit u(sunit);
[286]626
[455]627  // Easy if just wanting pixels
[286]628  if (sunit==String("pixel")) {
629    // assume channels/pixels
630    std::vector<double>::iterator it;
631    uInt i=0;
632    for (it = abc.begin(); it != abc.end(); ++it) {
633      (*it) = Double(i++);
634    }
635    return abc;
[78]636  }
637
[455]638  // Continue with km/s or Hz.  Get FreqIDs
[347]639  Vector<uInt> freqIDs;
640  freqidCol_.get(whichRow, freqIDs);
641  uInt freqID = freqIDs(IFSel_);
[386]642  restfreqidCol_.get(whichRow, freqIDs);
643  uInt restFreqID = freqIDs(IFSel_);
[286]644
[455]645  // Get SpectralCoordinate, set reference frame conversion,
646  // velocity conversion, and rest freq state
[286]647
[386]648  SpectralCoordinate spc = getSpectralCoordinate(freqID, restFreqID, whichRow);
[286]649  Vector<Double> pixel(nChan());
650  indgen(pixel);
[455]651
[286]652  if (u == Unit("km/s")) {
653     Vector<Double> world;
654     spc.pixelToVelocity(world,pixel);
655     std::vector<double>::iterator it;
656     uInt i = 0;
657     for (it = abc.begin(); it != abc.end(); ++it) {
658       (*it) = world[i];
659       i++;
660     }
[39]661  } else if (u == Unit("Hz")) {
[286]662
[455]663    // Set world axis units   
[39]664    Vector<String> wau(1); wau = u.getName();
665    spc.setWorldAxisUnits(wau);
[455]666
[39]667    std::vector<double>::iterator it;
668    Double tmp;
669    uInt i = 0;
[286]670    for (it = abc.begin(); it != abc.end(); ++it) {
671      spc.toWorld(tmp,pixel[i]);
[39]672      (*it) = tmp;
673      i++;
674    }
675  }
[286]676  return abc;
[39]677}
678
[164]679std::string SDMemTable::getAbcissaString(Int whichRow) const
[105]680{
681  Table t = table_.keywordSet().asTable("FREQUENCIES");
[455]682
[105]683  String sunit;
684  t.keywordSet().get("UNIT",sunit);
685  if (sunit == "") sunit = "pixel";
686  Unit u(sunit);
[455]687
[347]688  Vector<uInt> freqIDs;
689  freqidCol_.get(whichRow, freqIDs);
[386]690  uInt freqID = freqIDs(IFSel_); 
691  restfreqidCol_.get(whichRow, freqIDs);
692  uInt restFreqID = freqIDs(IFSel_);
[286]693
[455]694  // Get SpectralCoordinate, with frame, velocity, rest freq state set
695  SpectralCoordinate spc = getSpectralCoordinate(freqID, restFreqID, whichRow);
[286]696
[105]697  String s = "Channel";
698  if (u == Unit("km/s")) {
699    s = CoordinateUtil::axisLabel(spc,0,True,True,True);
700  } else if (u == Unit("Hz")) {
701    Vector<String> wau(1);wau = u.getName();
702    spc.setWorldAxisUnits(wau);
[455]703
[286]704    s = CoordinateUtil::axisLabel(spc,0,True,True,False);
[105]705  }
706  return s;
707}
708
[206]709void SDMemTable::setSpectrum(std::vector<float> spectrum, int whichRow)
710{
[89]711  Array<Float> arr;
[322]712  specCol_.get(whichRow, arr);
[455]713  if (spectrum.size() != arr.shape()(asap::ChanAxis)) {
[89]714    throw(AipsError("Attempting to set spectrum with incorrect length."));
715  }
716
[455]717  // Setup accessors
[206]718  ArrayAccessor<Float, Axis<asap::BeamAxis> > aa0(arr);
[447]719  aa0.reset(aa0.begin(uInt(beamSel_)));                   // Beam selection
[206]720  ArrayAccessor<Float, Axis<asap::IFAxis> > aa1(aa0);
[447]721  aa1.reset(aa1.begin(uInt(IFSel_)));                     // IF selection
[206]722  ArrayAccessor<Float, Axis<asap::PolAxis> > aa2(aa1);
[447]723  aa2.reset(aa2.begin(uInt(polSel_)));                    // Pol selection
[89]724
[455]725  // Iterate
[89]726  std::vector<float>::iterator it = spectrum.begin();
[206]727  for (ArrayAccessor<Float, Axis<asap::ChanAxis> > i(aa2); i != i.end(); ++i) {
[89]728    (*i) = Float(*it);
729    it++;
730  }
[322]731  specCol_.put(whichRow, arr);
[89]732}
733
[206]734void SDMemTable::getSpectrum(Vector<Float>& spectrum, Int whichRow) const
735{
[21]736  Array<Float> arr;
[322]737  specCol_.get(whichRow, arr);
[418]738
[455]739  // Iterate and extract
[21]740  spectrum.resize(arr.shape()(3));
[206]741  ArrayAccessor<Float, Axis<asap::BeamAxis> > aa0(arr);
[21]742  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
[206]743  ArrayAccessor<Float, Axis<asap::IFAxis> > aa1(aa0);
[21]744  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
[206]745  ArrayAccessor<Float, Axis<asap::PolAxis> > aa2(aa1);
[21]746  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
[455]747
[206]748  ArrayAccessor<Float, Axis<asap::BeamAxis> > va(spectrum);
749  for (ArrayAccessor<Float, Axis<asap::ChanAxis> > i(aa2); i != i.end(); ++i) {
[21]750    (*va) = (*i);
751    va++;
752  }
753}
[418]754
755
[89]756/*
[68]757void SDMemTable::getMask(Vector<Bool>& mask, Int whichRow) const {
[21]758  Array<uChar> arr;
[322]759  flagsCol_.get(whichRow, arr);
[21]760  mask.resize(arr.shape()(3));
761
[206]762  ArrayAccessor<uChar, Axis<asap::BeamAxis> > aa0(arr);
[21]763  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
[206]764  ArrayAccessor<uChar, Axis<asap::IFAxis> > aa1(aa0);
[21]765  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
[206]766  ArrayAccessor<uChar, Axis<asap::PolAxis> > aa2(aa1);
[21]767  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
768
769  Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
[89]770
[206]771  ArrayAccessor<Bool, Axis<asap::BeamAxis> > va(mask);
[21]772  std::vector<bool> tmp;
773  tmp = chanMask_; // WHY the fxxx do I have to make a copy here. The
[89]774                   // iterator should work on chanMask_??
[21]775  std::vector<bool>::iterator miter;
776  miter = tmp.begin();
777
[206]778  for (ArrayAccessor<uChar, Axis<asap::ChanAxis> > i(aa2); i != i.end(); ++i) {
[21]779    bool out =!static_cast<bool>(*i);
780    if (useUserMask) {
781      out = out && (*miter);
782      miter++;
783    }
784    (*va) = out;
785    va++;
[89]786  }
[21]787}
[89]788*/
[447]789
[455]790MaskedArray<Float> SDMemTable::rowAsMaskedArray(uInt whichRow,
791                                                Bool toStokes) const
[164]792{
[455]793  // Get flags
[441]794  Array<uChar> farr;
795  flagsCol_.get(whichRow, farr);
796
[455]797  // Get data and convert mask to Bool
[2]798  Array<Float> arr;
[441]799  Array<Bool> mask;
[439]800  if (toStokes) {
801     stokesCol_.get(whichRow, arr);
[455]802
[441]803     Array<Bool> tMask(farr.shape());
804     convertArray(tMask, farr);
[462]805     mask = SDPolUtil::stokesData (tMask, True);
[439]806  } else {
807     specCol_.get(whichRow, arr);
[441]808     mask.resize(farr.shape());
809     convertArray(mask, farr);
[439]810  }
[455]811
[447]812  return MaskedArray<Float>(arr,!mask);
[2]813}
814
[206]815Float SDMemTable::getTsys(Int whichRow) const
816{
[2]817  Array<Float> arr;
[322]818  tsCol_.get(whichRow, arr);
[2]819  Float out;
[455]820
[2]821  IPosition ip(arr.shape());
[418]822  ip(asap::BeamAxis) = beamSel_;
823  ip(asap::IFAxis) = IFSel_;
824  ip(asap::PolAxis) = polSel_;
825  ip(asap::ChanAxis)=0;               // First channel only
[455]826
[2]827  out = arr(ip);
828  return out;
829}
830
[281]831MDirection SDMemTable::getDirection(Int whichRow, Bool refBeam) const
[206]832{
[212]833  MDirection::Types mdr = getDirectionReference();
[206]834  Array<Double> posit;
[322]835  dirCol_.get(whichRow,posit);
[206]836  Vector<Double> wpos(2);
[380]837  Int rb;
838  rbeamCol_.get(whichRow,rb);
[206]839  wpos[0] = posit(IPosition(2,beamSel_,0));
840  wpos[1] = posit(IPosition(2,beamSel_,1));
[380]841  if (refBeam && rb > -1) {  // use refBeam instead if it exists
842    wpos[0] = posit(IPosition(2,rb,0));
843    wpos[1] = posit(IPosition(2,rb,1));
844  }
845
[206]846  Quantum<Double> lon(wpos[0],Unit(String("rad")));
847  Quantum<Double> lat(wpos[1],Unit(String("rad")));
[286]848  return MDirection(lon, lat, mdr);
[206]849}
[39]850
[380]851MEpoch SDMemTable::getEpoch(Int whichRow) const
[206]852{
[286]853  MEpoch::Types met = getTimeReference();
[455]854
[286]855  Double obstime;
[322]856  timeCol_.get(whichRow,obstime);
[286]857  MVEpoch tm2(Quantum<Double>(obstime, Unit(String("d"))));
858  return MEpoch(tm2, met);
859}
860
861MPosition SDMemTable::getAntennaPosition () const
862{
863  Vector<Double> antpos;
864  table_.keywordSet().get("AntennaPosition", antpos);
865  MVPosition mvpos(antpos(0),antpos(1),antpos(2));
866  return MPosition(mvpos);
867}
868
869
[347]870SpectralCoordinate SDMemTable::getSpectralCoordinate(uInt freqID) const
[286]871{
[206]872 
[39]873  Table t = table_.keywordSet().asTable("FREQUENCIES");
[347]874  if (freqID> t.nrow() ) {
875    throw(AipsError("SDMemTable::getSpectralCoordinate - freqID out of range"));
[39]876  }
[89]877
[39]878  Double rp,rv,inc;
879  String rf;
880  ROScalarColumn<Double> rpc(t, "REFPIX");
881  ROScalarColumn<Double> rvc(t, "REFVAL");
882  ROScalarColumn<Double> incc(t, "INCREMENT");
[105]883  t.keywordSet().get("BASEREFFRAME",rf);
[89]884
[455]885  // Create SpectralCoordinate (units Hz)
[39]886  MFrequency::Types mft;
887  if (!MFrequency::getType(mft, rf)) {
888    cerr << "Frequency type unknown assuming TOPO" << endl;
[89]889    mft = MFrequency::TOPO;
890  }
[347]891  rpc.get(freqID, rp);
892  rvc.get(freqID, rv);
893  incc.get(freqID, inc);
[455]894
[39]895  SpectralCoordinate spec(mft,rv,inc,rp);
[286]896  return spec;
897}
898
899
[455]900SpectralCoordinate SDMemTable::getSpectralCoordinate(uInt freqID,
901                                                     uInt restFreqID,
[386]902                                                     uInt whichRow) const
[286]903{
[455]904 
905  // Create basic SC
906  SpectralCoordinate spec = getSpectralCoordinate (freqID);
[385]907
[286]908  Table t = table_.keywordSet().asTable("FREQUENCIES");
909
[455]910  // Set rest frequency
[386]911  Vector<Double> restFreqIDs;
912  t.keywordSet().get("RESTFREQS",restFreqIDs);
[455]913  if (restFreqID < restFreqIDs.nelements()) {    // Should always be true
[386]914    spec.setRestFrequency(restFreqIDs(restFreqID),True);
[286]915  }
916
[455]917  // Set up frame conversion layer
[286]918  String frm;
919  t.keywordSet().get("REFFRAME",frm);
920  if (frm == "") frm = "TOPO";
921  MFrequency::Types mtype;
922  if (!MFrequency::getType(mtype, frm)) {
[455]923    // Should never happen
924    cerr << "Frequency type unknown assuming TOPO" << endl;
[286]925    mtype = MFrequency::TOPO;
926  }
927
[455]928  // Set reference frame conversion  (requires row)
[401]929  MDirection dir = getDirection(whichRow);
[286]930  MEpoch epoch = getEpoch(whichRow);
931  MPosition pos = getAntennaPosition();
[455]932
[401]933  if (!spec.setReferenceConversion(mtype,epoch,pos,dir)) {
[286]934    throw(AipsError("Couldn't convert frequency frame."));
935  }
936
[455]937  // Now velocity conversion if appropriate
[286]938  String unitStr;
939  t.keywordSet().get("UNIT",unitStr);
[455]940
[286]941  String dpl;
942  t.keywordSet().get("DOPPLER",dpl);
943  MDoppler::Types dtype;
944  MDoppler::getType(dtype, dpl);
945
[455]946  // Only set velocity unit if non-blank and non-Hz
[286]947  if (!unitStr.empty()) {
948     Unit unitU(unitStr);
949     if (unitU==Unit("Hz")) {
950     } else {
951        spec.setVelocity(unitStr, dtype);
952     }
953  }
[455]954
[39]955  return spec;
956}
957
[286]958
[89]959Bool SDMemTable::setCoordinate(const SpectralCoordinate& speccord,
[347]960                               uInt freqID) {
[50]961  Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
[347]962  if (freqID > t.nrow() ) {
[89]963    throw(AipsError("SDMemTable::setCoordinate - coord no out of range"));
[50]964  }
965  ScalarColumn<Double> rpc(t, "REFPIX");
966  ScalarColumn<Double> rvc(t, "REFVAL");
967  ScalarColumn<Double> incc(t, "INCREMENT");
[89]968
[347]969  rpc.put(freqID, speccord.referencePixel()[0]);
970  rvc.put(freqID, speccord.referenceValue()[0]);
971  incc.put(freqID, speccord.increment()[0]);
[50]972
973  return True;
974}
975
[89]976Int SDMemTable::nCoordinates() const
977{
978  return table_.keywordSet().asTable("FREQUENCIES").nrow();
979}
[50]980
[89]981
[251]982std::vector<double> SDMemTable::getRestFreqs() const
983{
984  Table t = table_.keywordSet().asTable("FREQUENCIES");
985  Vector<Double> tvec;
986  t.keywordSet().get("RESTFREQS",tvec);
987  std::vector<double> stlout;
988  tvec.tovector(stlout);
989  return stlout; 
990}
991
[206]992bool SDMemTable::putSDFreqTable(const SDFrequencyTable& sdft)
993{
[39]994  TableDesc td("", "1", TableDesc::Scratch);
995  td.addColumn(ScalarColumnDesc<Double>("REFPIX"));
996  td.addColumn(ScalarColumnDesc<Double>("REFVAL"));
997  td.addColumn(ScalarColumnDesc<Double>("INCREMENT"));
998  SetupNewTable aNewTab("freqs", td, Table::New);
999  Table aTable (aNewTab, Table::Memory, sdft.length());
1000  ScalarColumn<Double> sc0(aTable, "REFPIX");
1001  ScalarColumn<Double> sc1(aTable, "REFVAL");
1002  ScalarColumn<Double> sc2(aTable, "INCREMENT");
1003  for (uInt i=0; i < sdft.length(); ++i) {
1004    sc0.put(i,sdft.referencePixel(i));
1005    sc1.put(i,sdft.referenceValue(i));
1006    sc2.put(i,sdft.increment(i));
1007  }
[105]1008  String rf = sdft.refFrame();
1009  if (rf.contains("TOPO")) rf = "TOPO";
1010
1011  aTable.rwKeywordSet().define("BASEREFFRAME", rf);
1012  aTable.rwKeywordSet().define("REFFRAME", rf);
[39]1013  aTable.rwKeywordSet().define("EQUINOX", sdft.equinox());
[105]1014  aTable.rwKeywordSet().define("UNIT", String(""));
1015  aTable.rwKeywordSet().define("DOPPLER", String("RADIO"));
[89]1016  Vector<Double> rfvec;
[206]1017  String rfunit;
1018  sdft.restFrequencies(rfvec,rfunit);
1019  Quantum<Vector<Double> > q(rfvec, rfunit);
1020  rfvec.resize();
1021  rfvec = q.getValue("Hz");
[89]1022  aTable.rwKeywordSet().define("RESTFREQS", rfvec);
[455]1023  table_.rwKeywordSet().defineTable("FREQUENCIES", aTable);
1024  return true;
[39]1025}
1026
[455]1027bool SDMemTable::putSDFitTable(const SDFitTable& sdft)
1028{
1029  TableDesc td("", "1", TableDesc::Scratch);
1030  td.addColumn(ArrayColumnDesc<String>("FUNCTIONS"));
1031  td.addColumn(ArrayColumnDesc<Int>("COMPONENTS"));
1032  td.addColumn(ArrayColumnDesc<Double>("PARAMETERS"));
1033  td.addColumn(ArrayColumnDesc<Bool>("PARMASK"));
[465]1034  td.addColumn(ArrayColumnDesc<String>("FRAMEINFO"));
[455]1035  SetupNewTable aNewTab("fits", td, Table::New);
1036  Table aTable(aNewTab, Table::Memory);
1037  ArrayColumn<String> sc0(aTable, "FUNCTIONS");
1038  ArrayColumn<Int> sc1(aTable, "COMPONENTS");
1039  ArrayColumn<Double> sc2(aTable, "PARAMETERS");
1040  ArrayColumn<Bool> sc3(aTable, "PARMASK");
[465]1041  ArrayColumn<String> sc4(aTable, "FRAMEINFO");
[455]1042  for (uInt i; i<sdft.length(); ++i) {
[465]1043    const Vector<Double>& parms = sdft.getParameters(i);
1044    const Vector<Bool>& parmask = sdft.getParameterMask(i);
1045    const Vector<String>& funcs = sdft.getFunctions(i);
1046    const Vector<Int>& comps = sdft.getComponents(i);
1047    const Vector<String>& finfo = sdft.getFrameInfo(i);
[455]1048    sc0.put(i,funcs);
1049    sc1.put(i,comps);
1050    sc3.put(i,parmask);
1051    sc2.put(i,parms);
[465]1052    sc4.put(i,finfo);
[455]1053  }
1054  table_.rwKeywordSet().defineTable("FITS", aTable);
1055  return true;
1056}
1057
1058SDFitTable SDMemTable::getSDFitTable() const
1059{
1060  const Table& t = table_.keywordSet().asTable("FITS");
1061  Vector<Double> parms;
1062  Vector<Bool> parmask;
1063  Vector<String> funcs;
[465]1064  Vector<String> finfo;
[455]1065  Vector<Int> comps; 
1066  ROArrayColumn<Double> parmsCol(t, "PARAMETERS");
1067  ROArrayColumn<Bool> parmaskCol(t, "PARMASK");
1068  ROArrayColumn<Int> compsCol(t, "COMPONENTS");
1069  ROArrayColumn<String> funcsCol(t, "FUNCTIONS");
[465]1070  ROArrayColumn<String> finfoCol(t, "FRAMEINFO");
[455]1071  uInt n = t.nrow();
[465]1072  SDFitTable sdft;
[455]1073  for (uInt i=0; i<n; ++i) {
1074    parmaskCol.get(i, parmask);
1075    parmsCol.get(i, parms);
1076    funcsCol.get(i, funcs);
1077    compsCol.get(i, comps);
[465]1078    finfoCol.get(i, finfo);
1079    sdft.addFit(parms, parmask, funcs, comps, finfo);
[455]1080  }
1081  return sdft;
1082}
1083
[465]1084SDFitTable SDMemTable::getSDFitTable(uInt whichRow) const {
[527]1085  const Table& t = table_.keywordSet().asTable("FITS");
1086  if (t.nrow() == 0 || whichRow >= t.nrow()) return SDFitTable();
[465]1087  Array<Int> fitid;
1088  fitCol_.get(whichRow, fitid);
1089  if (fitid.nelements() == 0) return SDFitTable();
[455]1090
[465]1091  IPosition shp = fitid.shape();
1092  IPosition start(4, beamSel_, IFSel_, polSel_,0);
1093  IPosition end(4, beamSel_, IFSel_, polSel_, shp[3]-1);
1094
1095  // reform the output array slice to be of dim=1
1096  Vector<Int> tmp = (fitid(start, end)).reform(IPosition(1,shp[3]));
1097
1098  Vector<Double> parms;
1099  Vector<Bool> parmask;
1100  Vector<String> funcs;
1101  Vector<String> finfo;
1102  Vector<Int> comps;   
1103  ROArrayColumn<Double> parmsCol(t, "PARAMETERS");
1104  ROArrayColumn<Bool> parmaskCol(t, "PARMASK");
1105  ROArrayColumn<Int> compsCol(t, "COMPONENTS");
1106  ROArrayColumn<String> funcsCol(t, "FUNCTIONS");
1107  ROArrayColumn<String> finfoCol(t, "FRAMEINFO");
1108  SDFitTable sdft;
1109  Int k=-1;
1110  for (uInt i=0; i< tmp.nelements(); ++i) {
1111    k = tmp[i];
1112    if ( k > -1 && k < t.nrow() ) {
1113      parms.resize();
1114      parmsCol.get(k, parms);
1115      parmask.resize();
1116      parmaskCol.get(k, parmask);
1117      funcs.resize();
1118      funcsCol.get(k, funcs);
1119      comps.resize();
1120      compsCol.get(k, comps);
1121      finfo.resize();
1122      finfoCol.get(k, finfo);
1123      sdft.addFit(parms, parmask, funcs, comps, finfo);
1124    }
1125  }
1126  return sdft;
1127}
1128
[455]1129void SDMemTable::addFit(uInt whichRow,
1130                        const Vector<Double>& p, const Vector<Bool>& m,
1131                        const Vector<String>& f, const Vector<Int>& c)
1132{
1133  if (whichRow >= nRow()) {
1134    throw(AipsError("Specified row out of range"));
1135  }
1136  Table t = table_.keywordSet().asTable("FITS");
1137  uInt nrow = t.nrow(); 
1138  t.addRow();
1139  ArrayColumn<Double> parmsCol(t, "PARAMETERS");
1140  ArrayColumn<Bool> parmaskCol(t, "PARMASK");
1141  ArrayColumn<Int> compsCol(t, "COMPONENTS");
1142  ArrayColumn<String> funcsCol(t, "FUNCTIONS");
[465]1143  ArrayColumn<String> finfoCol(t, "FRAMEINFO");
[455]1144  parmsCol.put(nrow, p);
1145  parmaskCol.put(nrow, m);
1146  compsCol.put(nrow, c);
1147  funcsCol.put(nrow, f);
[465]1148  Vector<String> fi = mathutil::toVectorString(getCoordInfo());
1149  finfoCol.put(nrow, fi);
[455]1150
1151  Array<Int> fitarr;
1152  fitCol_.get(whichRow, fitarr);
1153
1154  Array<Int> newarr;               // The new Array containing the fitid
1155  Int pos =-1;                     // The fitid position in the array
1156  if ( fitarr.nelements() == 0 ) { // no fits at all in this row
1157    Array<Int> arr(IPosition(4,nBeam(),nIF(),nPol(),1));
1158    arr = -1;
1159    newarr.reference(arr);
1160    pos = 0;     
1161  } else {
1162    IPosition shp = fitarr.shape();
1163    IPosition start(4, beamSel_, IFSel_, polSel_,0);
1164    IPosition end(4, beamSel_, IFSel_, polSel_, shp[3]-1);
1165    // reform the output array slice to be of dim=1
1166    Array<Int> tmp = (fitarr(start, end)).reform(IPosition(1,shp[3]));
1167    const Vector<Int>& fits = tmp;
1168    VectorSTLIterator<Int> it(fits);
1169    Int i = 0;
1170    while (it != fits.end()) {
1171      if (*it == -1) {
1172        pos = i;
1173        break;
1174      }
1175      ++i;
1176      ++it;
1177    };
1178  }
1179  if (pos == -1) {
1180      mathutil::extendLastArrayAxis(newarr,fitarr, -1);
1181      pos = fitarr.shape()[3];     // the new element position
1182  } else {
1183    if (fitarr.nelements() > 0)
1184      newarr = fitarr;
1185  }
1186  newarr(IPosition(4, beamSel_, IFSel_, polSel_, pos)) = Int(nrow);
1187  fitCol_.put(whichRow, newarr);
1188
1189}
1190
[206]1191SDFrequencyTable SDMemTable::getSDFreqTable() const
1192{
[251]1193  const Table& t = table_.keywordSet().asTable("FREQUENCIES");
[39]1194  SDFrequencyTable sdft;
[306]1195
[455]1196  // Add refpix/refval/incr.  What are the units ? Hz I suppose
1197  // but it's nowhere described...
[306]1198  Vector<Double> refPix, refVal, incr;
1199  ScalarColumn<Double> refPixCol(t, "REFPIX");
1200  ScalarColumn<Double> refValCol(t, "REFVAL");
1201  ScalarColumn<Double> incrCol(t, "INCREMENT");
1202  refPix = refPixCol.getColumn();
1203  refVal = refValCol.getColumn();
1204  incr = incrCol.getColumn();
[455]1205
[306]1206  uInt n = refPix.nelements();
1207  for (uInt i=0; i<n; i++) {
1208     sdft.addFrequency(refPix[i], refVal[i], incr[i]);
1209  }
1210
[455]1211  // Frequency reference frame.  I don't know if this
1212  // is the correct frame.  It might be 'REFFRAME'
1213  // rather than 'BASEREFFRAME' ?
[306]1214  String baseFrame;
1215  t.keywordSet().get("BASEREFFRAME",baseFrame);
1216  sdft.setRefFrame(baseFrame);
1217
[455]1218  // Equinox 
[306]1219  Float equinox;
1220  t.keywordSet().get("EQUINOX", equinox);
1221  sdft.setEquinox(equinox);
1222
[455]1223  // Rest Frequency
[306]1224  Vector<Double> restFreqs;
1225  t.keywordSet().get("RESTFREQS", restFreqs);
1226  for (uInt i=0; i<restFreqs.nelements(); i++) {
1227     sdft.addRestFrequency(restFreqs[i]);
1228  }
1229  sdft.setRestFrequencyUnit(String("Hz"));
[455]1230
[39]1231  return sdft;
1232}
1233
[206]1234bool SDMemTable::putSDContainer(const SDContainer& sdc)
1235{
[2]1236  uInt rno = table_.nrow();
1237  table_.addRow();
[455]1238
[322]1239  timeCol_.put(rno, sdc.timestamp);
1240  srcnCol_.put(rno, sdc.sourcename);
1241  fldnCol_.put(rno, sdc.fieldname);
1242  specCol_.put(rno, sdc.getSpectrum());
1243  flagsCol_.put(rno, sdc.getFlags());
1244  tsCol_.put(rno, sdc.getTsys());
1245  scanCol_.put(rno, sdc.scanid);
1246  integrCol_.put(rno, sdc.interval);
1247  freqidCol_.put(rno, sdc.getFreqMap());
[386]1248  restfreqidCol_.put(rno, sdc.getRestFreqMap());
[322]1249  dirCol_.put(rno, sdc.getDirection());
1250  rbeamCol_.put(rno, sdc.refbeam);
1251  tcalCol_.put(rno, sdc.tcal);
1252  tcaltCol_.put(rno, sdc.tcaltime);
1253  azCol_.put(rno, sdc.azimuth);
1254  elCol_.put(rno, sdc.elevation);
1255  paraCol_.put(rno, sdc.parangle);
[455]1256  fitCol_.put(rno, sdc.getFitMap());
[2]1257  return true;
1258}
[18]1259
[206]1260SDContainer SDMemTable::getSDContainer(uInt whichRow) const
1261{
[21]1262  SDContainer sdc(nBeam(),nIF(),nPol(),nChan());
[322]1263  timeCol_.get(whichRow, sdc.timestamp);
1264  srcnCol_.get(whichRow, sdc.sourcename);
1265  integrCol_.get(whichRow, sdc.interval);
1266  scanCol_.get(whichRow, sdc.scanid);
1267  fldnCol_.get(whichRow, sdc.fieldname);
1268  rbeamCol_.get(whichRow, sdc.refbeam);
1269  azCol_.get(whichRow, sdc.azimuth);
1270  elCol_.get(whichRow, sdc.elevation);
1271  paraCol_.get(whichRow, sdc.parangle);
[105]1272  Vector<Float> tc;
[322]1273  tcalCol_.get(whichRow, tc);
[105]1274  sdc.tcal[0] = tc[0];sdc.tcal[1] = tc[1];
[322]1275  tcaltCol_.get(whichRow, sdc.tcaltime);
[455]1276
[21]1277  Array<Float> spectrum;
1278  Array<Float> tsys;
1279  Array<uChar> flagtrum;
[39]1280  Vector<uInt> fmap;
[78]1281  Array<Double> direction;
[455]1282  Array<Int> fits;
1283 
[322]1284  specCol_.get(whichRow, spectrum);
[21]1285  sdc.putSpectrum(spectrum);
[322]1286  flagsCol_.get(whichRow, flagtrum);
[21]1287  sdc.putFlags(flagtrum);
[322]1288  tsCol_.get(whichRow, tsys);
[21]1289  sdc.putTsys(tsys);
[322]1290  freqidCol_.get(whichRow, fmap);
[39]1291  sdc.putFreqMap(fmap);
[386]1292  restfreqidCol_.get(whichRow, fmap);
1293  sdc.putRestFreqMap(fmap);
[322]1294  dirCol_.get(whichRow, direction);
[78]1295  sdc.putDirection(direction);
[455]1296  fitCol_.get(whichRow, fits);
1297  sdc.putFitMap(fits);
[21]1298  return sdc;
1299}
[78]1300
[206]1301bool SDMemTable::putSDHeader(const SDHeader& sdh)
1302{
[18]1303  table_.rwKeywordSet().define("nIF", sdh.nif);
1304  table_.rwKeywordSet().define("nBeam", sdh.nbeam);
1305  table_.rwKeywordSet().define("nPol", sdh.npol);
1306  table_.rwKeywordSet().define("nChan", sdh.nchan);
1307  table_.rwKeywordSet().define("Observer", sdh.observer);
1308  table_.rwKeywordSet().define("Project", sdh.project);
1309  table_.rwKeywordSet().define("Obstype", sdh.obstype);
1310  table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
1311  table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
1312  table_.rwKeywordSet().define("Equinox", sdh.equinox);
1313  table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
1314  table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
1315  table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
1316  table_.rwKeywordSet().define("UTC", sdh.utc);
[206]1317  table_.rwKeywordSet().define("FluxUnit", sdh.fluxunit);
1318  table_.rwKeywordSet().define("Epoch", sdh.epoch);
[18]1319  return true;
[50]1320}
[21]1321
[206]1322SDHeader SDMemTable::getSDHeader() const
1323{
[21]1324  SDHeader sdh;
1325  table_.keywordSet().get("nBeam",sdh.nbeam);
1326  table_.keywordSet().get("nIF",sdh.nif);
1327  table_.keywordSet().get("nPol",sdh.npol);
1328  table_.keywordSet().get("nChan",sdh.nchan);
1329  table_.keywordSet().get("Observer", sdh.observer);
1330  table_.keywordSet().get("Project", sdh.project);
1331  table_.keywordSet().get("Obstype", sdh.obstype);
1332  table_.keywordSet().get("AntennaName", sdh.antennaname);
1333  table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
1334  table_.keywordSet().get("Equinox", sdh.equinox);
1335  table_.keywordSet().get("FreqRefFrame", sdh.freqref);
1336  table_.keywordSet().get("FreqRefVal", sdh.reffreq);
1337  table_.keywordSet().get("Bandwidth", sdh.bandwidth);
1338  table_.keywordSet().get("UTC", sdh.utc);
[206]1339  table_.keywordSet().get("FluxUnit", sdh.fluxunit);
1340  table_.keywordSet().get("Epoch", sdh.epoch);
[21]1341  return sdh;
[18]1342}
[206]1343void SDMemTable::makePersistent(const std::string& filename)
[483]1344
[2]1345  table_.deepCopy(filename,Table::New);
[483]1346
[2]1347}
1348
[89]1349Int SDMemTable::nScan() const {
[50]1350  Int n = 0;
1351  Int previous = -1;Int current=0;
[322]1352  for (uInt i=0; i< scanCol_.nrow();i++) {
1353    scanCol_.getScalar(i,current);
[50]1354    if (previous != current) {
[89]1355      previous = current;
[50]1356      n++;
1357    }
1358  }
1359  return n;
1360}
1361
[260]1362String SDMemTable::formatSec(Double x) const
[206]1363{
[105]1364  Double xcop = x;
1365  MVTime mvt(xcop/24./3600.);  // make days
[365]1366
[105]1367  if (x < 59.95)
[281]1368    return  String("      ") + mvt.string(MVTime::TIME_CLEAN_NO_HM, 7)+"s";
1369  else if (x < 3599.95)
1370    return String("   ") + mvt.string(MVTime::TIME_CLEAN_NO_H,7)+" ";
1371  else {
1372    ostringstream oss;
1373    oss << setw(2) << std::right << setprecision(1) << mvt.hour();
1374    oss << ":" << mvt.string(MVTime::TIME_CLEAN_NO_H,7) << " ";
1375    return String(oss);
1376  }   
[105]1377};
1378
[281]1379String SDMemTable::formatDirection(const MDirection& md) const
1380{
1381  Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
1382  Int prec = 7;
1383
1384  MVAngle mvLon(t[0]);
1385  String sLon = mvLon.string(MVAngle::TIME,prec);
1386  MVAngle mvLat(t[1]);
1387  String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec);
[380]1388  return sLon + String(" ") + sLat;
[281]1389}
1390
1391
[206]1392std::string SDMemTable::getFluxUnit() const
1393{
1394  String tmp;
1395  table_.keywordSet().get("FluxUnit", tmp);
1396  return tmp;
1397}
1398
[218]1399void SDMemTable::setFluxUnit(const std::string& unit)
1400{
1401  String tmp(unit);
1402  Unit tU(tmp);
1403  if (tU==Unit("K") || tU==Unit("Jy")) {
1404     table_.rwKeywordSet().define(String("FluxUnit"), tmp);
1405  } else {
1406     throw AipsError("Illegal unit - must be compatible with Jy or K");
1407  }
1408}
1409
[275]1410
[236]1411void SDMemTable::setInstrument(const std::string& name)
1412{
1413  Bool throwIt = True;
[476]1414  Instrument ins = SDAttr::convertInstrument(name, throwIt);
[236]1415  String nameU(name);
1416  nameU.upcase();
1417  table_.rwKeywordSet().define(String("AntennaName"), nameU);
1418}
1419
[380]1420std::string SDMemTable::summary(bool verbose) const  {
[281]1421
[455]1422  // Format header info
[89]1423  ostringstream oss;
1424  oss << endl;
[380]1425  oss << "--------------------------------------------------------------------------------" << endl;
[89]1426  oss << " Scan Table Summary" << endl;
[380]1427  oss << "--------------------------------------------------------------------------------" << endl;
[89]1428  oss.flags(std::ios_base::left);
1429  oss << setw(15) << "Beams:" << setw(4) << nBeam() << endl
1430      << setw(15) << "IFs:" << setw(4) << nIF() << endl
1431      << setw(15) << "Polarisations:" << setw(4) << nPol() << endl
1432      << setw(15) << "Channels:"  << setw(4) << nChan() << endl;
1433  oss << endl;
1434  String tmp;
1435  table_.keywordSet().get("Observer", tmp);
1436  oss << setw(15) << "Observer:" << tmp << endl;
[281]1437  oss << setw(15) << "Obs Date:" << getTime(-1,True) << endl;
[89]1438  table_.keywordSet().get("Project", tmp);
1439  oss << setw(15) << "Project:" << tmp << endl;
1440  table_.keywordSet().get("Obstype", tmp);
1441  oss << setw(15) << "Obs. Type:" << tmp << endl;
1442  table_.keywordSet().get("AntennaName", tmp);
1443  oss << setw(15) << "Antenna Name:" << tmp << endl;
[206]1444  table_.keywordSet().get("FluxUnit", tmp);
1445  oss << setw(15) << "Flux Unit:" << tmp << endl;
[260]1446  Table t = table_.keywordSet().asTable("FREQUENCIES");
[105]1447  Vector<Double> vec;
1448  t.keywordSet().get("RESTFREQS",vec);
1449  oss << setw(15) << "Rest Freqs:";
1450  if (vec.nelements() > 0) {
1451      oss << setprecision(0) << vec << " [Hz]" << endl;
1452  } else {
1453      oss << "None set" << endl;
1454  }
[158]1455  oss << setw(15) << "Abcissa:" << getAbcissaString() << endl;
[105]1456  oss << setw(15) << "Cursor:" << "Beam[" << getBeam() << "] "
1457      << "IF[" << getIF() << "] " << "Pol[" << getPol() << "]" << endl;
[89]1458  oss << endl;
[455]1459
[385]1460  String dirtype ="Position ("+ MDirection::showType(getDirectionReference()) + ")";
[380]1461  oss << setw(5) << "Scan"
[281]1462      << setw(15) << "Source"
[380]1463      << setw(24) << dirtype
[281]1464      << setw(10) << "Time"
[365]1465      << setw(18) << "Integration"
[380]1466      << setw(7) << "FreqIDs" << endl;
1467  oss << "--------------------------------------------------------------------------------" << endl;
[281]1468 
[455]1469  // Generate list of scan start and end integrations
[385]1470  Vector<Int> scanIDs = scanCol_.getColumn();
1471  Vector<uInt> startInt, endInt;
1472  mathutil::scanBoundaries(startInt, endInt, scanIDs);
[455]1473
[385]1474  const uInt nScans = startInt.nelements();
[365]1475  String name;
1476  Vector<uInt> freqIDs, listFQ;
[521]1477  Vector<uInt> restFreqIDs, listRestFQ;
[385]1478  uInt nInt;
[455]1479
[385]1480  for (uInt i=0; i<nScans; i++) {
[455]1481    // Get things from first integration of scan
1482    String time = getTime(startInt(i),False);
1483    String tInt = formatSec(Double(getInterval(startInt(i))));
1484    String posit = formatDirection(getDirection(startInt(i),True));
1485    srcnCol_.getScalar(startInt(i),name);
[367]1486
[455]1487    // Find all the FreqIDs in this scan
1488    listFQ.resize(0);     
[521]1489    listRestFQ.resize(0);     
[455]1490    for (uInt j=startInt(i); j<endInt(i)+1; j++) {
1491      freqidCol_.get(j, freqIDs);
1492      for (uInt k=0; k<freqIDs.nelements(); k++) {
1493        mathutil::addEntry(listFQ, freqIDs(k));
1494      }
[521]1495//
1496      restfreqidCol_.get(j, restFreqIDs);
1497      for (uInt k=0; k<restFreqIDs.nelements(); k++) {
1498        mathutil::addEntry(listRestFQ, restFreqIDs(k));
1499      }
[455]1500    }
[367]1501
[455]1502    nInt = endInt(i) - startInt(i) + 1;
1503    oss << setw(3) << std::right << i << std::left << setw(2) << "  "
1504        << setw(15) << name
1505        << setw(24) << posit
1506        << setw(10) << time
1507        << setw(3) << std::right << nInt  << setw(3) << " x " << std::left
1508        << setw(6) <<  tInt
[521]1509        << " " << listFQ << " " << listRestFQ << endl;
[2]1510  }
[89]1511  oss << endl;
[455]1512  oss << "Table contains " << table_.nrow() << " integration(s) in "
1513      << nScans << " scan(s)." << endl;
[321]1514
[455]1515  // Frequency Table
[380]1516  if (verbose) {
1517    std::vector<string> info = getCoordInfo();
1518    SDFrequencyTable sdft = getSDFreqTable();
1519    oss << endl << endl;
1520    oss << "FreqID  Frame   RefFreq(Hz)     RefPix   Increment(Hz)" << endl;
1521    oss << "--------------------------------------------------------------------------------" << endl;
1522    for (uInt i=0; i<sdft.length(); i++) {
1523      oss << setw(8) << i << setw(8)
1524          << info[3] << setw(16) << setprecision(8)
1525          << sdft.referenceValue(i) << setw(10)
1526          << sdft.referencePixel(i) << setw(12)
1527          << sdft.increment(i) << endl;
1528    }
1529    oss << "--------------------------------------------------------------------------------" << endl;
[321]1530  }
[89]1531  return String(oss);
[2]1532}
[483]1533/*
1534std::string SDMemTable::scanSummary(const std::vector<int>& whichScans) {
1535  ostringstream oss;
1536  Vector<Int> scanIDs = scanCol_.getColumn();
1537  Vector<uInt> startInt, endInt;
1538  mathutil::scanBoundaries(startInt, endInt, scanIDs);
1539  const uInt nScans = startInt.nelements();
1540  std::vector<int>::const_iterator it(whichScans);
1541  return String(oss);
1542}
1543*/
[206]1544Int SDMemTable::nBeam() const
1545{
[18]1546  Int n;
1547  table_.keywordSet().get("nBeam",n);
1548  return n;
1549}
[455]1550
[18]1551Int SDMemTable::nIF() const {
1552  Int n;
1553  table_.keywordSet().get("nIF",n);
1554  return n;
1555}
[455]1556
[18]1557Int SDMemTable::nPol() const {
1558  Int n;
1559  table_.keywordSet().get("nPol",n);
1560  return n;
1561}
[455]1562
[18]1563Int SDMemTable::nChan() const {
1564  Int n;
1565  table_.keywordSet().get("nChan",n);
1566  return n;
1567}
[455]1568
[488]1569Table SDMemTable::getHistoryTable() const
1570{
1571  return table_.keywordSet().asTable("HISTORY");
1572}
1573
1574void SDMemTable::appendToHistoryTable(const Table& otherHist)
1575{
1576  Table t = table_.rwKeywordSet().asTable("HISTORY");
1577  const String sep = "--------------------------------------------------------------------------------";
1578  addHistory(sep);
1579  TableCopy::copyRows(t, otherHist, t.nrow(), 0, otherHist.nrow());
1580  addHistory(sep);
1581}
1582
[483]1583void SDMemTable::addHistory(const std::string& hist)
[206]1584{
[483]1585  Table t = table_.rwKeywordSet().asTable("HISTORY");
1586  uInt nrow = t.nrow(); 
1587  t.addRow();
1588  ScalarColumn<String> itemCol(t, "ITEM");
1589  itemCol.put(nrow, hist);
[206]1590}
1591
[483]1592std::vector<std::string> SDMemTable::getHistory() const
[206]1593{
1594  Vector<String> history;
[483]1595  const Table& t = table_.keywordSet().asTable("HISTORY");
1596  uInt nrow = t.nrow(); 
1597  ROScalarColumn<String> itemCol(t, "ITEM");
1598  std::vector<std::string> stlout;
1599  String hist;
1600  for (uInt i=0; i<nrow; ++i) {
1601    itemCol.get(i, hist);
1602    stlout.push_back(hist);
1603  }
[206]1604  return stlout;
1605}
[488]1606
1607
[16]1608/*
[483]1609  void SDMemTable::maskChannels(const std::vector<Int>& whichChans ) {
[89]1610
[16]1611  std::vector<int>::iterator it;
[206]1612  ArrayAccessor<uChar, Axis<asap::PolAxis> > j(flags_);
[16]1613  for (it = whichChans.begin(); it != whichChans.end(); it++) {
1614    j.reset(j.begin(uInt(*it)));
[206]1615    for (ArrayAccessor<uChar, Axis<asap::BeamAxis> > i(j); i != i.end(); ++i) {
1616      for (ArrayAccessor<uChar, Axis<asap::IFAxis> > ii(i); ii != ii.end(); ++ii) {
1617        for (ArrayAccessor<uChar, Axis<asap::ChanAxis> > iii(ii);
[89]1618             iii != iii.end(); ++iii) {
1619          (*iii) =
1620        }
[16]1621      }
1622    }
1623  }
[89]1624
[16]1625}
1626*/
[206]1627void SDMemTable::flag(int whichRow)
[483]1628  {
[89]1629  Array<uChar> arr;
[322]1630  flagsCol_.get(whichRow, arr);
[89]1631
[206]1632  ArrayAccessor<uChar, Axis<asap::BeamAxis> > aa0(arr);
[89]1633  aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
[206]1634  ArrayAccessor<uChar, Axis<asap::IFAxis> > aa1(aa0);
[89]1635  aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
[206]1636  ArrayAccessor<uChar, Axis<asap::PolAxis> > aa2(aa1);
[89]1637  aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
1638
[206]1639  for (ArrayAccessor<uChar, Axis<asap::ChanAxis> > i(aa2); i != i.end(); ++i) {
[89]1640    (*i) = uChar(True);
1641  }
1642
[322]1643  flagsCol_.put(whichRow, arr);
[89]1644}
[212]1645
[281]1646MDirection::Types SDMemTable::getDirectionReference() const
[212]1647
1648  Float eq;
1649  table_.keywordSet().get("Equinox",eq);
1650  std::map<float,string> mp;
1651  mp[2000.0] = "J2000";
1652  mp[1950.0] = "B1950";
1653  MDirection::Types mdr;
1654  if (!MDirection::getType(mdr, mp[eq])) {   
1655    mdr = MDirection::J2000;
1656    cerr  << "Unknown equinox using J2000" << endl;
1657  }
[455]1658
[212]1659  return mdr;
1660}
1661
[380]1662MEpoch::Types SDMemTable::getTimeReference() const
[212]1663{
1664  MEpoch::Types met;
1665  String ep;
1666  table_.keywordSet().get("Epoch",ep);
1667  if (!MEpoch::getType(met, ep)) {
[387]1668    cerr << "Epoch type unknown - using UTC" << endl;
[212]1669    met = MEpoch::UTC;
1670  }
[455]1671
[212]1672  return met;
1673}
1674
[236]1675
[455]1676Bool SDMemTable::setRestFreqs(const Vector<Double>& restFreqsIn,
1677                              const String& sUnit,
1678                              const vector<string>& lines,
1679                              const String& source,
1680                              Int whichIF)
[386]1681{
1682   const Int nIFs = nIF();
1683   if (whichIF>=nIFs) {
1684      throw(AipsError("Illegal IF index"));
1685   }
1686
[455]1687   // Find vector of restfrequencies
1688   // Double takes precedence over String
[401]1689   Unit unit;
1690   Vector<Double> restFreqs;
1691   if (restFreqsIn.nelements()>0) {
1692      restFreqs.resize(restFreqsIn.nelements());
1693      restFreqs = restFreqsIn;
1694      unit = Unit(sUnit);
1695   } else if (lines.size()>0) {
1696      const uInt nLines = lines.size();
1697      unit = Unit("Hz");
1698      restFreqs.resize(nLines);
1699      MFrequency lineFreq;
1700      for (uInt i=0; i<nLines; i++) {
1701         String tS(lines[i]);
1702         tS.upcase();
1703         if (MeasTable::Line(lineFreq, tS)) {
1704            restFreqs[i] = lineFreq.getValue().getValue();          // Hz
1705         } else {
[455]1706            String s = String(lines[i]) +
1707              String(" is an unrecognized spectral line");
[401]1708            throw(AipsError(s));
1709         }
1710      }
1711   } else {
1712      throw(AipsError("You have not specified any rest frequencies or lines"));
1713   }
1714
[455]1715   // If multiple restfreqs, must be length nIF. In this
1716   // case we will just replace the rest frequencies
[521]1717   // We can't disinguish scalar and vector of length 1
[392]1718   const uInt nRestFreqs = restFreqs.nelements();
1719   Int idx = -1;
[386]1720   SDFrequencyTable sdft = getSDFreqTable();
1721
[392]1722   if (nRestFreqs>1) {
[455]1723     // Replace restFreqs, one per IF
[392]1724      if (nRestFreqs != nIFs) {
1725         throw (AipsError("Number of rest frequencies must be equal to the number of IFs"));
1726      }
[521]1727      cout << "Replacing rest frequencies, one per IF, with given list : " << restFreqs << endl;
[392]1728      sdft.deleteRestFrequencies();
1729      for (uInt i=0; i<nRestFreqs; i++) {
1730         Quantum<Double> rf(restFreqs[i], unit);
1731         sdft.addRestFrequency(rf.getValue("Hz"));
1732      }
1733   } else {
1734
[455]1735     // Add new rest freq
[392]1736      Quantum<Double> rf(restFreqs[0], unit);
1737      idx = sdft.addRestFrequency(rf.getValue("Hz"));
[521]1738      if (whichIF>=0) {
1739         cout << "Selecting given rest frequency (" << restFreqs[0] << ") for IF " << whichIF << endl;
1740      } else {
1741         cout << "Selecting given rest frequency (" << restFreqs[0] << ") for all IFs" << endl;
1742      }
[392]1743   }
[455]1744   
1745   // Replace
[386]1746   Bool empty = source.empty();
1747   Bool ok = False;
1748   if (putSDFreqTable(sdft)) {
1749      const uInt nRow = table_.nrow();
1750      String srcName;
1751      Vector<uInt> restFreqIDs;
1752      for (uInt i=0; i<nRow; i++) {
1753         srcnCol_.get(i, srcName);
1754         restfreqidCol_.get(i,restFreqIDs);       
[392]1755         if (idx==-1) {
[455]1756           // Replace vector of restFreqs; one per IF.
1757           // No selection possible
[392]1758            for (uInt i=0; i<nIFs; i++) restFreqIDs[i] = i;
1759         } else {
[455]1760           // Set RestFreqID for selected data
[392]1761            if (empty || source==srcName) {
1762               if (whichIF<0) {
1763                  restFreqIDs = idx;
1764               } else {             
1765                  restFreqIDs[whichIF] = idx;
1766               }
[386]1767            }
1768         }
1769         restfreqidCol_.put(i,restFreqIDs);       
1770      }
1771      ok = True;
1772   } else {
[455]1773     ok = False;
[386]1774   }
[455]1775
[386]1776   return ok;
1777}
1778
[414]1779void SDMemTable::spectralLines() const
[401]1780{
1781   Vector<String> lines = MeasTable::Lines();
1782   MFrequency lineFreq;
1783   Double freq;
[455]1784
[414]1785   cout.flags(std::ios_base::left);
1786   cout << "Line      Frequency (Hz)" << endl;
1787   cout << "-----------------------" << endl;
[401]1788   for (uInt i=0; i<lines.nelements(); i++) {
1789     MeasTable::Line(lineFreq, lines[i]);
1790     freq = lineFreq.getValue().getValue();          // Hz
[414]1791     cout << setw(11) << lines[i] << setprecision(10) << freq << endl;
[401]1792   }
1793}
[386]1794
[380]1795void SDMemTable::renumber()
1796{
1797  uInt nRow = scanCol_.nrow();
1798  Int newscanid = 0;
1799  Int cIdx;// the current scanid
1800  // get the first scanid
1801  scanCol_.getScalar(0,cIdx);
1802  Int pIdx = cIdx;// the scanid of the previous row
1803  for (uInt i=0; i<nRow;++i) {
1804    scanCol_.getScalar(i,cIdx);
1805    if (pIdx == cIdx) {
1806      // renumber
1807      scanCol_.put(i,newscanid);
1808    } else {
1809      ++newscanid;
1810      pIdx = cIdx;   // store scanid
1811      --i;           // don't increment next loop
1812    }
1813  }
1814}
[386]1815
[418]1816
[483]1817void SDMemTable::getCursorSlice(IPosition& start, IPosition& end,
[455]1818                                const IPosition& shape) const
[430]1819{
[455]1820  const uInt nDim = shape.nelements();
1821  start.resize(nDim);
1822  end.resize(nDim);
1823 
1824  start(asap::BeamAxis) = beamSel_;
1825  end(asap::BeamAxis) = beamSel_;
1826  start(asap::IFAxis) = IFSel_;
1827  end(asap::IFAxis) = IFSel_;
[430]1828
[455]1829  start(asap::PolAxis) = polSel_;
1830  end(asap::PolAxis) = polSel_;
[430]1831
[455]1832  start(asap::ChanAxis) = 0;
1833  end(asap::ChanAxis) = shape(asap::ChanAxis) - 1;
[430]1834}
1835
[447]1836
[455]1837std::vector<float> SDMemTable::getFloatSpectrum(const Array<Float>& arr) const
1838  // Get spectrum at cursor location
[447]1839{
1840
[455]1841  // Setup accessors
[447]1842  ArrayAccessor<Float, Axis<asap::BeamAxis> > aa0(arr);
1843  aa0.reset(aa0.begin(uInt(beamSel_)));                    // Beam selection
1844
1845  ArrayAccessor<Float, Axis<asap::IFAxis> > aa1(aa0);
1846  aa1.reset(aa1.begin(uInt(IFSel_)));                      // IF selection
1847
1848  ArrayAccessor<Float, Axis<asap::PolAxis> > aa2(aa1);
1849  aa2.reset(aa2.begin(uInt(polSel_)));                     // Pol selection
[455]1850 
[447]1851  std::vector<float> spectrum;
1852  for (ArrayAccessor<Float, Axis<asap::ChanAxis> > i(aa2); i != i.end(); ++i) {
1853    spectrum.push_back(*i);
1854  }
1855  return spectrum;
1856}
1857
Note: See TracBrowser for help on using the repository browser.