source: trunk/src/SDMemTable.cc@ 749

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

removed setmask functionality

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