source: branches/Release12/src/SDMemTable.cc @ 773

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

Request: added recalc_azel

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