source: trunk/src/SDMemTable.cc@ 489

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