source: trunk/src/SDMemTable.cc @ 488

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