source: trunk/src/SDMemTable.cc@ 537

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

ficed bug in getSDFitTable

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