source: trunk/src/SDMemTable.cc@ 768

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

Bug fix: SDFreqTable did not copy user settings for unit and frame

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 52.4 KB
Line 
1//#---------------------------------------------------------------------------
2//# SDMemTable.cc: A MemoryTable container for single dish integrations
3//#---------------------------------------------------------------------------
4//# Copyright (C) 2004
5//# ATNF
6//#
7//# This program is free software; you can redistribute it and/or modify it
8//# under the terms of the GNU General Public License as published by the Free
9//# Software Foundation; either version 2 of the License, or (at your option)
10//# any later version.
11//#
12//# This program is distributed in the hope that it will be useful, but
13//# WITHOUT ANY WARRANTY; without even the implied warranty of
14//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15//# Public License for more details.
16//#
17//# You should have received a copy of the GNU General Public License along
18//# with this program; if not, write to the Free Software Foundation, Inc.,
19//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20//#
21//# Correspondence concerning this software should be addressed as follows:
22//# Internet email: Malte.Marquarding@csiro.au
23//# Postal address: Malte Marquarding,
24//# Australia Telescope National Facility,
25//# P.O. Box 76,
26//# Epping, NSW, 2121,
27//# AUSTRALIA
28//#
29//# $Id:
30//#---------------------------------------------------------------------------
31
32#include <map>
33
34#include <casa/aips.h>
35#include <casa/iostream.h>
36#include <casa/iomanip.h>
37#include <casa/Arrays/Array.h>
38#include <casa/Arrays/ArrayMath.h>
39#include <casa/Arrays/MaskArrMath.h>
40#include <casa/Arrays/ArrayLogical.h>
41#include <casa/Arrays/ArrayAccessor.h>
42#include <casa/Arrays/VectorSTLIterator.h>
43#include <casa/Arrays/Vector.h>
44#include <casa/BasicMath/Math.h>
45#include <casa/BasicSL/Constants.h>
46#include <casa/Quanta/MVAngle.h>
47
48#include <tables/Tables/TableParse.h>
49#include <tables/Tables/TableDesc.h>
50#include <tables/Tables/TableCopy.h>
51#include <tables/Tables/SetupNewTab.h>
52#include <tables/Tables/ScaColDesc.h>
53#include <tables/Tables/ArrColDesc.h>
54
55#include <tables/Tables/ExprNode.h>
56#include <tables/Tables/TableRecord.h>
57#include <measures/Measures/MFrequency.h>
58#include <measures/Measures/MeasTable.h>
59#include <coordinates/Coordinates/CoordinateUtil.h>
60#include <casa/Quanta/MVTime.h>
61#include <casa/Quanta/MVAngle.h>
62
63#include "SDDefs.h"
64#include "SDContainer.h"
65#include "MathUtils.h"
66#include "SDPol.h"
67#include "SDAttr.h"
68
69#include "SDMemTable.h"
70
71using namespace casa;
72using namespace asap;
73
74SDMemTable::SDMemTable() :
75 IFSel_(0),
76 beamSel_(0),
77 polSel_(0)
78{
79 setup();
80 attach();
81}
82
83SDMemTable::SDMemTable(const std::string& name) :
84 IFSel_(0),
85 beamSel_(0),
86 polSel_(0)
87{
88 Table tab(name);
89 Int version;
90 tab.keywordSet().get("VERSION", version);
91 if (version != version_) {
92 throw(AipsError("Unsupported version of ASAP file."));
93 }
94 table_ = tab.copyToMemoryTable("dummy");
95 attach();
96}
97
98SDMemTable::SDMemTable(const SDMemTable& other, Bool clear)
99{
100 table_ = other.table_.copyToMemoryTable(String("dummy"));
101 // clear all rows()
102 if (clear) {
103 table_.removeRow(this->table_.rowNumbers());
104 IFSel_= 0;
105 beamSel_= 0;
106 polSel_= 0;
107 } 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 Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
536
537 Vector<Double> rstf;
538 t.keywordSet().get("RESTFREQS",rstf);
539
540 Bool canDo = True;
541 Unit u1("km/s");Unit u2("Hz");
542 if (Unit(un) == u1) {
543 Vector<Double> rstf;
544 t.keywordSet().get("RESTFREQS",rstf);
545 if (rstf.nelements() == 0) {
546 throw(AipsError("Can't set unit to km/s if no restfrequencies are specified"));
547 }
548 } else if (Unit(un) != u2 && un != "") {
549 throw(AipsError("Unit not conformant with Spectral Coordinates"));
550 }
551 t.rwKeywordSet().define("UNIT", un);
552
553 MFrequency::Types mdr;
554 if (!MFrequency::getType(mdr, rfrm)) {
555
556 Int a,b;const uInt* c;
557 const String* valid = MFrequency::allMyTypes(a, b, c);
558 String pfix = "Please specify a legal frame type. Types are\n";
559 throw(AipsError(pfix+(*valid)));
560 } else {
561 t.rwKeywordSet().define("REFFRAME",rfrm);
562 }
563
564 MDoppler::Types dtype;
565 dpl.upcase();
566 if (!MDoppler::getType(dtype, dpl)) {
567 throw(AipsError("Doppler type unknown"));
568 } else {
569 t.rwKeywordSet().define("DOPPLER",dpl);
570 }
571
572 if (!MFrequency::getType(mdr, brfrm)) {
573 Int a,b;const uInt* c;
574 const String* valid = MFrequency::allMyTypes(a, b, c);
575 String pfix = "Please specify a legal frame type. Types are\n";
576 throw(AipsError(pfix+(*valid)));
577 } else {
578 t.rwKeywordSet().define("BASEREFFRAME",brfrm);
579 }
580}
581
582
583std::vector<double> SDMemTable::getAbcissa(Int whichRow) const
584{
585 std::vector<double> abc(nChan());
586
587 // Get header units keyword
588 Table t = table_.keywordSet().asTable("FREQUENCIES");
589 String sunit;
590 t.keywordSet().get("UNIT",sunit);
591 if (sunit == "") sunit = "pixel";
592 Unit u(sunit);
593
594 // Easy if just wanting pixels
595 if (sunit==String("pixel")) {
596 // assume channels/pixels
597 std::vector<double>::iterator it;
598 uInt i=0;
599 for (it = abc.begin(); it != abc.end(); ++it) {
600 (*it) = Double(i++);
601 }
602 return abc;
603 }
604
605 // Continue with km/s or Hz. Get FreqIDs
606 Vector<uInt> freqIDs;
607 freqidCol_.get(whichRow, freqIDs);
608 uInt freqID = freqIDs(IFSel_);
609 restfreqidCol_.get(whichRow, freqIDs);
610 uInt restFreqID = freqIDs(IFSel_);
611
612 // Get SpectralCoordinate, set reference frame conversion,
613 // velocity conversion, and rest freq state
614
615 SpectralCoordinate spc = getSpectralCoordinate(freqID, restFreqID, whichRow);
616 Vector<Double> pixel(nChan());
617 indgen(pixel);
618
619 if (u == Unit("km/s")) {
620 Vector<Double> world;
621 spc.pixelToVelocity(world,pixel);
622 std::vector<double>::iterator it;
623 uInt i = 0;
624 for (it = abc.begin(); it != abc.end(); ++it) {
625 (*it) = world[i];
626 i++;
627 }
628 } else if (u == Unit("Hz")) {
629
630 // Set world axis units
631 Vector<String> wau(1); wau = u.getName();
632 spc.setWorldAxisUnits(wau);
633
634 std::vector<double>::iterator it;
635 Double tmp;
636 uInt i = 0;
637 for (it = abc.begin(); it != abc.end(); ++it) {
638 spc.toWorld(tmp,pixel[i]);
639 (*it) = tmp;
640 i++;
641 }
642 }
643 return abc;
644}
645
646std::string SDMemTable::getAbcissaString(Int whichRow) const
647{
648 Table t = table_.keywordSet().asTable("FREQUENCIES");
649
650 String sunit;
651 t.keywordSet().get("UNIT",sunit);
652 if (sunit == "") sunit = "pixel";
653 Unit u(sunit);
654
655 Vector<uInt> freqIDs;
656 freqidCol_.get(whichRow, freqIDs);
657 uInt freqID = freqIDs(IFSel_);
658 restfreqidCol_.get(whichRow, freqIDs);
659 uInt restFreqID = freqIDs(IFSel_);
660
661 // Get SpectralCoordinate, with frame, velocity, rest freq state set
662 SpectralCoordinate spc = getSpectralCoordinate(freqID, restFreqID, whichRow);
663
664 String s = "Channel";
665 if (u == Unit("km/s")) {
666 s = CoordinateUtil::axisLabel(spc,0,True,True,True);
667 } else if (u == Unit("Hz")) {
668 Vector<String> wau(1);wau = u.getName();
669 spc.setWorldAxisUnits(wau);
670
671 s = CoordinateUtil::axisLabel(spc,0,True,True,False);
672 }
673 return s;
674}
675
676void SDMemTable::setSpectrum(std::vector<float> spectrum, int whichRow)
677{
678 Array<Float> arr;
679 specCol_.get(whichRow, arr);
680 if (spectrum.size() != arr.shape()(asap::ChanAxis)) {
681 throw(AipsError("Attempting to set spectrum with incorrect length."));
682 }
683
684 // Setup accessors
685 ArrayAccessor<Float, Axis<asap::BeamAxis> > aa0(arr);
686 aa0.reset(aa0.begin(uInt(beamSel_))); // Beam selection
687 ArrayAccessor<Float, Axis<asap::IFAxis> > aa1(aa0);
688 aa1.reset(aa1.begin(uInt(IFSel_))); // IF selection
689 ArrayAccessor<Float, Axis<asap::PolAxis> > aa2(aa1);
690 aa2.reset(aa2.begin(uInt(polSel_))); // Pol selection
691
692 // Iterate
693 std::vector<float>::iterator it = spectrum.begin();
694 for (ArrayAccessor<Float, Axis<asap::ChanAxis> > i(aa2); i != i.end(); ++i) {
695 (*i) = Float(*it);
696 it++;
697 }
698 specCol_.put(whichRow, arr);
699}
700
701void SDMemTable::getSpectrum(Vector<Float>& spectrum, Int whichRow) const
702{
703 Array<Float> arr;
704 specCol_.get(whichRow, arr);
705
706 // Iterate and extract
707 spectrum.resize(arr.shape()(3));
708 ArrayAccessor<Float, Axis<asap::BeamAxis> > aa0(arr);
709 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
710 ArrayAccessor<Float, Axis<asap::IFAxis> > aa1(aa0);
711 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
712 ArrayAccessor<Float, Axis<asap::PolAxis> > aa2(aa1);
713 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
714
715 ArrayAccessor<Float, Axis<asap::BeamAxis> > va(spectrum);
716 for (ArrayAccessor<Float, Axis<asap::ChanAxis> > i(aa2); i != i.end(); ++i) {
717 (*va) = (*i);
718 va++;
719 }
720}
721
722
723/*
724void SDMemTable::getMask(Vector<Bool>& mask, Int whichRow) const {
725 Array<uChar> arr;
726 flagsCol_.get(whichRow, arr);
727 mask.resize(arr.shape()(3));
728
729 ArrayAccessor<uChar, Axis<asap::BeamAxis> > aa0(arr);
730 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
731 ArrayAccessor<uChar, Axis<asap::IFAxis> > aa1(aa0);
732 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
733 ArrayAccessor<uChar, Axis<asap::PolAxis> > aa2(aa1);
734 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
735
736 Bool useUserMask = ( chanMask_.size() == arr.shape()(3) );
737
738 ArrayAccessor<Bool, Axis<asap::BeamAxis> > va(mask);
739 std::vector<bool> tmp;
740 tmp = chanMask_; // WHY the fxxx do I have to make a copy here. The
741 // iterator should work on chanMask_??
742 std::vector<bool>::iterator miter;
743 miter = tmp.begin();
744
745 for (ArrayAccessor<uChar, Axis<asap::ChanAxis> > i(aa2); i != i.end(); ++i) {
746 bool out =!static_cast<bool>(*i);
747 if (useUserMask) {
748 out = out && (*miter);
749 miter++;
750 }
751 (*va) = out;
752 va++;
753 }
754}
755*/
756
757MaskedArray<Float> SDMemTable::rowAsMaskedArray(uInt whichRow,
758 Bool toStokes) const
759{
760 // Get flags
761 Array<uChar> farr;
762 flagsCol_.get(whichRow, farr);
763
764 // Get data and convert mask to Bool
765 Array<Float> arr;
766 Array<Bool> mask;
767 if (toStokes) {
768 stokesCol_.get(whichRow, arr);
769
770 Array<Bool> tMask(farr.shape());
771 convertArray(tMask, farr);
772 mask = SDPolUtil::stokesData (tMask, True);
773 } else {
774 specCol_.get(whichRow, arr);
775 mask.resize(farr.shape());
776 convertArray(mask, farr);
777 }
778
779 return MaskedArray<Float>(arr,!mask);
780}
781
782Float SDMemTable::getTsys(Int whichRow) const
783{
784 Array<Float> arr;
785 tsCol_.get(whichRow, arr);
786 Float out;
787
788 IPosition ip(arr.shape());
789 ip(asap::BeamAxis) = beamSel_;
790 ip(asap::IFAxis) = IFSel_;
791 ip(asap::PolAxis) = polSel_;
792 ip(asap::ChanAxis)=0; // First channel only
793
794 out = arr(ip);
795 return out;
796}
797
798MDirection SDMemTable::getDirection(Int whichRow, Bool refBeam) const
799{
800 MDirection::Types mdr = getDirectionReference();
801 Array<Double> posit;
802 dirCol_.get(whichRow,posit);
803 Vector<Double> wpos(2);
804 Int rb;
805 rbeamCol_.get(whichRow,rb);
806 wpos[0] = posit(IPosition(2,beamSel_,0));
807 wpos[1] = posit(IPosition(2,beamSel_,1));
808 if (refBeam && rb > -1) { // use refBeam instead if it exists
809 wpos[0] = posit(IPosition(2,rb,0));
810 wpos[1] = posit(IPosition(2,rb,1));
811 }
812
813 Quantum<Double> lon(wpos[0],Unit(String("rad")));
814 Quantum<Double> lat(wpos[1],Unit(String("rad")));
815 return MDirection(lon, lat, mdr);
816}
817
818MEpoch SDMemTable::getEpoch(Int whichRow) const
819{
820 MEpoch::Types met = getTimeReference();
821
822 Double obstime;
823 timeCol_.get(whichRow,obstime);
824 MVEpoch tm2(Quantum<Double>(obstime, Unit(String("d"))));
825 return MEpoch(tm2, met);
826}
827
828MPosition SDMemTable::getAntennaPosition () const
829{
830 Vector<Double> antpos;
831 table_.keywordSet().get("AntennaPosition", antpos);
832 MVPosition mvpos(antpos(0),antpos(1),antpos(2));
833 return MPosition(mvpos);
834}
835
836
837SpectralCoordinate SDMemTable::getSpectralCoordinate(uInt freqID) const
838{
839
840 Table t = table_.keywordSet().asTable("FREQUENCIES");
841 if (freqID> t.nrow() ) {
842 throw(AipsError("SDMemTable::getSpectralCoordinate - freqID out of range"));
843 }
844
845 Double rp,rv,inc;
846 String rf;
847 ROScalarColumn<Double> rpc(t, "REFPIX");
848 ROScalarColumn<Double> rvc(t, "REFVAL");
849 ROScalarColumn<Double> incc(t, "INCREMENT");
850 t.keywordSet().get("BASEREFFRAME",rf);
851
852 // Create SpectralCoordinate (units Hz)
853 MFrequency::Types mft;
854 if (!MFrequency::getType(mft, rf)) {
855 ostringstream oss;
856 pushLog("WARNING: Frequency type unknown assuming TOPO");
857 mft = MFrequency::TOPO;
858 }
859 rpc.get(freqID, rp);
860 rvc.get(freqID, rv);
861 incc.get(freqID, inc);
862
863 SpectralCoordinate spec(mft,rv,inc,rp);
864 return spec;
865}
866
867
868SpectralCoordinate SDMemTable::getSpectralCoordinate(uInt freqID,
869 uInt restFreqID,
870 uInt whichRow) const
871{
872
873 // Create basic SC
874 SpectralCoordinate spec = getSpectralCoordinate (freqID);
875
876 Table t = table_.keywordSet().asTable("FREQUENCIES");
877
878 // Set rest frequency
879 Vector<Double> restFreqIDs;
880 t.keywordSet().get("RESTFREQS",restFreqIDs);
881 if (restFreqID < restFreqIDs.nelements()) { // Should always be true
882 spec.setRestFrequency(restFreqIDs(restFreqID),True);
883 }
884
885 // Set up frame conversion layer
886 String frm;
887 t.keywordSet().get("REFFRAME",frm);
888 if (frm == "") frm = "TOPO";
889 MFrequency::Types mtype;
890 if (!MFrequency::getType(mtype, frm)) {
891 // Should never happen
892 pushLog("WARNING: Frequency type unknown assuming TOPO");
893 mtype = MFrequency::TOPO;
894 }
895
896 // Set reference frame conversion (requires row)
897 MDirection dir = getDirection(whichRow);
898 MEpoch epoch = getEpoch(whichRow);
899 MPosition pos = getAntennaPosition();
900
901 if (!spec.setReferenceConversion(mtype,epoch,pos,dir)) {
902 throw(AipsError("Couldn't convert frequency frame."));
903 }
904
905 // Now velocity conversion if appropriate
906 String unitStr;
907 t.keywordSet().get("UNIT",unitStr);
908
909 String dpl;
910 t.keywordSet().get("DOPPLER",dpl);
911 MDoppler::Types dtype;
912 MDoppler::getType(dtype, dpl);
913
914 // Only set velocity unit if non-blank and non-Hz
915 if (!unitStr.empty()) {
916 Unit unitU(unitStr);
917 if (unitU==Unit("Hz")) {
918 } else {
919 spec.setVelocity(unitStr, dtype);
920 }
921 }
922
923 return spec;
924}
925
926
927Bool SDMemTable::setCoordinate(const SpectralCoordinate& speccord,
928 uInt freqID) {
929 Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
930 if (freqID > t.nrow() ) {
931 throw(AipsError("SDMemTable::setCoordinate - coord no out of range"));
932 }
933 ScalarColumn<Double> rpc(t, "REFPIX");
934 ScalarColumn<Double> rvc(t, "REFVAL");
935 ScalarColumn<Double> incc(t, "INCREMENT");
936
937 rpc.put(freqID, speccord.referencePixel()[0]);
938 rvc.put(freqID, speccord.referenceValue()[0]);
939 incc.put(freqID, speccord.increment()[0]);
940
941 return True;
942}
943
944Int SDMemTable::nCoordinates() const
945{
946 return table_.keywordSet().asTable("FREQUENCIES").nrow();
947}
948
949
950std::vector<double> SDMemTable::getRestFreqs() const
951{
952 Table t = table_.keywordSet().asTable("FREQUENCIES");
953 Vector<Double> tvec;
954 t.keywordSet().get("RESTFREQS",tvec);
955 std::vector<double> stlout;
956 tvec.tovector(stlout);
957 return stlout;
958}
959
960bool SDMemTable::putSDFreqTable(const SDFrequencyTable& sdft)
961{
962 TableDesc td("", "1", TableDesc::Scratch);
963 td.addColumn(ScalarColumnDesc<Double>("REFPIX"));
964 td.addColumn(ScalarColumnDesc<Double>("REFVAL"));
965 td.addColumn(ScalarColumnDesc<Double>("INCREMENT"));
966 SetupNewTable aNewTab("freqs", td, Table::New);
967 Table aTable (aNewTab, Table::Memory, sdft.length());
968 ScalarColumn<Double> sc0(aTable, "REFPIX");
969 ScalarColumn<Double> sc1(aTable, "REFVAL");
970 ScalarColumn<Double> sc2(aTable, "INCREMENT");
971 for (uInt i=0; i < sdft.length(); ++i) {
972 sc0.put(i,sdft.referencePixel(i));
973 sc1.put(i,sdft.referenceValue(i));
974 sc2.put(i,sdft.increment(i));
975 }
976 String rf = sdft.refFrame();
977 if (rf.contains("TOPO")) rf = "TOPO";
978 String brf = sdft.baseRefFrame();
979 if (brf.contains("TOPO")) brf = "TOPO";
980
981 aTable.rwKeywordSet().define("BASEREFFRAME", brf);
982 aTable.rwKeywordSet().define("REFFRAME", rf);
983 aTable.rwKeywordSet().define("EQUINOX", sdft.equinox());
984 aTable.rwKeywordSet().define("UNIT", sdft.unit());
985 aTable.rwKeywordSet().define("DOPPLER", String("RADIO"));
986 Vector<Double> rfvec;
987 String rfunit;
988 sdft.restFrequencies(rfvec,rfunit);
989 Quantum<Vector<Double> > q(rfvec, rfunit);
990 rfvec.resize();
991 rfvec = q.getValue("Hz");
992 aTable.rwKeywordSet().define("RESTFREQS", rfvec);
993 table_.rwKeywordSet().defineTable("FREQUENCIES", aTable);
994 return true;
995}
996
997bool SDMemTable::putSDFitTable(const SDFitTable& sdft)
998{
999 TableDesc td("", "1", TableDesc::Scratch);
1000 td.addColumn(ArrayColumnDesc<String>("FUNCTIONS"));
1001 td.addColumn(ArrayColumnDesc<Int>("COMPONENTS"));
1002 td.addColumn(ArrayColumnDesc<Double>("PARAMETERS"));
1003 td.addColumn(ArrayColumnDesc<Bool>("PARMASK"));
1004 td.addColumn(ArrayColumnDesc<String>("FRAMEINFO"));
1005 SetupNewTable aNewTab("fits", td, Table::New);
1006 Table aTable(aNewTab, Table::Memory);
1007 ArrayColumn<String> sc0(aTable, "FUNCTIONS");
1008 ArrayColumn<Int> sc1(aTable, "COMPONENTS");
1009 ArrayColumn<Double> sc2(aTable, "PARAMETERS");
1010 ArrayColumn<Bool> sc3(aTable, "PARMASK");
1011 ArrayColumn<String> sc4(aTable, "FRAMEINFO");
1012 for (uInt i; i<sdft.length(); ++i) {
1013 const Vector<Double>& parms = sdft.getParameters(i);
1014 const Vector<Bool>& parmask = sdft.getParameterMask(i);
1015 const Vector<String>& funcs = sdft.getFunctions(i);
1016 const Vector<Int>& comps = sdft.getComponents(i);
1017 const Vector<String>& finfo = sdft.getFrameInfo(i);
1018 sc0.put(i,funcs);
1019 sc1.put(i,comps);
1020 sc3.put(i,parmask);
1021 sc2.put(i,parms);
1022 sc4.put(i,finfo);
1023 }
1024 table_.rwKeywordSet().defineTable("FITS", aTable);
1025 return true;
1026}
1027
1028SDFitTable SDMemTable::getSDFitTable() const
1029{
1030 const Table& t = table_.keywordSet().asTable("FITS");
1031 Vector<Double> parms;
1032 Vector<Bool> parmask;
1033 Vector<String> funcs;
1034 Vector<String> finfo;
1035 Vector<Int> comps;
1036 ROArrayColumn<Double> parmsCol(t, "PARAMETERS");
1037 ROArrayColumn<Bool> parmaskCol(t, "PARMASK");
1038 ROArrayColumn<Int> compsCol(t, "COMPONENTS");
1039 ROArrayColumn<String> funcsCol(t, "FUNCTIONS");
1040 ROArrayColumn<String> finfoCol(t, "FRAMEINFO");
1041 uInt n = t.nrow();
1042 SDFitTable sdft;
1043 for (uInt i=0; i<n; ++i) {
1044 parmaskCol.get(i, parmask);
1045 parmsCol.get(i, parms);
1046 funcsCol.get(i, funcs);
1047 compsCol.get(i, comps);
1048 finfoCol.get(i, finfo);
1049 sdft.addFit(parms, parmask, funcs, comps, finfo);
1050 }
1051 return sdft;
1052}
1053
1054SDFitTable SDMemTable::getSDFitTable(uInt whichRow) const {
1055 const Table& t = table_.keywordSet().asTable("FITS");
1056 if (t.nrow() == 0 || whichRow >= t.nrow()) return SDFitTable();
1057 Array<Int> fitid;
1058 fitCol_.get(whichRow, fitid);
1059 if (fitid.nelements() == 0) return SDFitTable();
1060
1061 IPosition shp = fitid.shape();
1062 IPosition start(4, beamSel_, IFSel_, polSel_,0);
1063 IPosition end(4, beamSel_, IFSel_, polSel_, shp[3]-1);
1064
1065 // reform the output array slice to be of dim=1
1066 Vector<Int> tmp = (fitid(start, end)).reform(IPosition(1,shp[3]));
1067
1068 Vector<Double> parms;
1069 Vector<Bool> parmask;
1070 Vector<String> funcs;
1071 Vector<String> finfo;
1072 Vector<Int> comps;
1073 ROArrayColumn<Double> parmsCol(t, "PARAMETERS");
1074 ROArrayColumn<Bool> parmaskCol(t, "PARMASK");
1075 ROArrayColumn<Int> compsCol(t, "COMPONENTS");
1076 ROArrayColumn<String> funcsCol(t, "FUNCTIONS");
1077 ROArrayColumn<String> finfoCol(t, "FRAMEINFO");
1078 SDFitTable sdft;
1079 Int k=-1;
1080 for (uInt i=0; i< tmp.nelements(); ++i) {
1081 k = tmp[i];
1082 if ( k > -1 && k < t.nrow() ) {
1083 parms.resize();
1084 parmsCol.get(k, parms);
1085 parmask.resize();
1086 parmaskCol.get(k, parmask);
1087 funcs.resize();
1088 funcsCol.get(k, funcs);
1089 comps.resize();
1090 compsCol.get(k, comps);
1091 finfo.resize();
1092 finfoCol.get(k, finfo);
1093 sdft.addFit(parms, parmask, funcs, comps, finfo);
1094 }
1095 }
1096 return sdft;
1097}
1098
1099void SDMemTable::addFit(uInt whichRow,
1100 const Vector<Double>& p, const Vector<Bool>& m,
1101 const Vector<String>& f, const Vector<Int>& c)
1102{
1103 if (whichRow >= nRow()) {
1104 throw(AipsError("Specified row out of range"));
1105 }
1106 Table t = table_.keywordSet().asTable("FITS");
1107 uInt nrow = t.nrow();
1108 t.addRow();
1109 ArrayColumn<Double> parmsCol(t, "PARAMETERS");
1110 ArrayColumn<Bool> parmaskCol(t, "PARMASK");
1111 ArrayColumn<Int> compsCol(t, "COMPONENTS");
1112 ArrayColumn<String> funcsCol(t, "FUNCTIONS");
1113 ArrayColumn<String> finfoCol(t, "FRAMEINFO");
1114 parmsCol.put(nrow, p);
1115 parmaskCol.put(nrow, m);
1116 compsCol.put(nrow, c);
1117 funcsCol.put(nrow, f);
1118 Vector<String> fi = mathutil::toVectorString(getCoordInfo());
1119 finfoCol.put(nrow, fi);
1120
1121 Array<Int> fitarr;
1122 fitCol_.get(whichRow, fitarr);
1123
1124 Array<Int> newarr; // The new Array containing the fitid
1125 Int pos =-1; // The fitid position in the array
1126 if ( fitarr.nelements() == 0 ) { // no fits at all in this row
1127 Array<Int> arr(IPosition(4,nBeam(),nIF(),nPol(),1));
1128 arr = -1;
1129 newarr.reference(arr);
1130 pos = 0;
1131 } else {
1132 IPosition shp = fitarr.shape();
1133 IPosition start(4, beamSel_, IFSel_, polSel_,0);
1134 IPosition end(4, beamSel_, IFSel_, polSel_, shp[3]-1);
1135 // reform the output array slice to be of dim=1
1136 Array<Int> tmp = (fitarr(start, end)).reform(IPosition(1,shp[3]));
1137 const Vector<Int>& fits = tmp;
1138 VectorSTLIterator<Int> it(fits);
1139 Int i = 0;
1140 while (it != fits.end()) {
1141 if (*it == -1) {
1142 pos = i;
1143 break;
1144 }
1145 ++i;
1146 ++it;
1147 };
1148 }
1149 if (pos == -1) {
1150 mathutil::extendLastArrayAxis(newarr,fitarr, -1);
1151 pos = fitarr.shape()[3]; // the new element position
1152 } else {
1153 if (fitarr.nelements() > 0)
1154 newarr = fitarr;
1155 }
1156 newarr(IPosition(4, beamSel_, IFSel_, polSel_, pos)) = Int(nrow);
1157 fitCol_.put(whichRow, newarr);
1158
1159}
1160
1161SDFrequencyTable SDMemTable::getSDFreqTable() const
1162{
1163 const Table& t = table_.keywordSet().asTable("FREQUENCIES");
1164 SDFrequencyTable sdft;
1165
1166 // Add refpix/refval/incr. What are the units ? Hz I suppose
1167 // but it's nowhere described...
1168 Vector<Double> refPix, refVal, incr;
1169 ScalarColumn<Double> refPixCol(t, "REFPIX");
1170 ScalarColumn<Double> refValCol(t, "REFVAL");
1171 ScalarColumn<Double> incrCol(t, "INCREMENT");
1172 refPix = refPixCol.getColumn();
1173 refVal = refValCol.getColumn();
1174 incr = incrCol.getColumn();
1175
1176 uInt n = refPix.nelements();
1177 for (uInt i=0; i<n; i++) {
1178 sdft.addFrequency(refPix[i], refVal[i], incr[i]);
1179 }
1180
1181 // Frequency reference frame. I don't know if this
1182 // is the correct frame. It might be 'REFFRAME'
1183 // rather than 'BASEREFFRAME' ?
1184 String frame;
1185 t.keywordSet().get("REFFRAME",frame);
1186 sdft.setRefFrame(frame);
1187 t.keywordSet().get("BASEREFFRAME",frame);
1188 sdft.setBaseRefFrame(frame);
1189
1190 // Equinox
1191 Float equinox;
1192 t.keywordSet().get("EQUINOX", equinox);
1193 sdft.setEquinox(equinox);
1194
1195 String unit;
1196 t.keywordSet().get("UNIT", unit);
1197 sdft.setUnit(unit);
1198
1199 // Rest Frequency
1200 Vector<Double> restFreqs;
1201 t.keywordSet().get("RESTFREQS", restFreqs);
1202 for (uInt i=0; i<restFreqs.nelements(); i++) {
1203 sdft.addRestFrequency(restFreqs[i]);
1204 }
1205 sdft.setRestFrequencyUnit(String("Hz"));
1206
1207 return sdft;
1208}
1209
1210bool SDMemTable::putSDContainer(const SDContainer& sdc)
1211{
1212 uInt rno = table_.nrow();
1213 table_.addRow();
1214
1215 timeCol_.put(rno, sdc.timestamp);
1216 srcnCol_.put(rno, sdc.sourcename);
1217 fldnCol_.put(rno, sdc.fieldname);
1218 specCol_.put(rno, sdc.getSpectrum());
1219 flagsCol_.put(rno, sdc.getFlags());
1220 tsCol_.put(rno, sdc.getTsys());
1221 scanCol_.put(rno, sdc.scanid);
1222 integrCol_.put(rno, sdc.interval);
1223 freqidCol_.put(rno, sdc.getFreqMap());
1224 restfreqidCol_.put(rno, sdc.getRestFreqMap());
1225 dirCol_.put(rno, sdc.getDirection());
1226 rbeamCol_.put(rno, sdc.refbeam);
1227 tcalCol_.put(rno, sdc.tcal);
1228 tcaltCol_.put(rno, sdc.tcaltime);
1229 azCol_.put(rno, sdc.azimuth);
1230 elCol_.put(rno, sdc.elevation);
1231 paraCol_.put(rno, sdc.parangle);
1232 fitCol_.put(rno, sdc.getFitMap());
1233 return true;
1234}
1235
1236SDContainer SDMemTable::getSDContainer(uInt whichRow) const
1237{
1238 SDContainer sdc(nBeam(),nIF(),nPol(),nChan());
1239 timeCol_.get(whichRow, sdc.timestamp);
1240 srcnCol_.get(whichRow, sdc.sourcename);
1241 integrCol_.get(whichRow, sdc.interval);
1242 scanCol_.get(whichRow, sdc.scanid);
1243 fldnCol_.get(whichRow, sdc.fieldname);
1244 rbeamCol_.get(whichRow, sdc.refbeam);
1245 azCol_.get(whichRow, sdc.azimuth);
1246 elCol_.get(whichRow, sdc.elevation);
1247 paraCol_.get(whichRow, sdc.parangle);
1248 Vector<Float> tc;
1249 tcalCol_.get(whichRow, tc);
1250 sdc.tcal[0] = tc[0];sdc.tcal[1] = tc[1];
1251 tcaltCol_.get(whichRow, sdc.tcaltime);
1252
1253 Array<Float> spectrum;
1254 Array<Float> tsys;
1255 Array<uChar> flagtrum;
1256 Vector<uInt> fmap;
1257 Array<Double> direction;
1258 Array<Int> fits;
1259
1260 specCol_.get(whichRow, spectrum);
1261 sdc.putSpectrum(spectrum);
1262 flagsCol_.get(whichRow, flagtrum);
1263 sdc.putFlags(flagtrum);
1264 tsCol_.get(whichRow, tsys);
1265 sdc.putTsys(tsys);
1266 freqidCol_.get(whichRow, fmap);
1267 sdc.putFreqMap(fmap);
1268 restfreqidCol_.get(whichRow, fmap);
1269 sdc.putRestFreqMap(fmap);
1270 dirCol_.get(whichRow, direction);
1271 sdc.putDirection(direction);
1272 fitCol_.get(whichRow, fits);
1273 sdc.putFitMap(fits);
1274 return sdc;
1275}
1276
1277bool SDMemTable::putSDHeader(const SDHeader& sdh)
1278{
1279 table_.rwKeywordSet().define("nIF", sdh.nif);
1280 table_.rwKeywordSet().define("nBeam", sdh.nbeam);
1281 table_.rwKeywordSet().define("nPol", sdh.npol);
1282 table_.rwKeywordSet().define("nChan", sdh.nchan);
1283 table_.rwKeywordSet().define("Observer", sdh.observer);
1284 table_.rwKeywordSet().define("Project", sdh.project);
1285 table_.rwKeywordSet().define("Obstype", sdh.obstype);
1286 table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
1287 table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
1288 table_.rwKeywordSet().define("Equinox", sdh.equinox);
1289 table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
1290 table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
1291 table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
1292 table_.rwKeywordSet().define("UTC", sdh.utc);
1293 table_.rwKeywordSet().define("FluxUnit", sdh.fluxunit);
1294 table_.rwKeywordSet().define("Epoch", sdh.epoch);
1295 return true;
1296}
1297
1298SDHeader SDMemTable::getSDHeader() const
1299{
1300 SDHeader sdh;
1301 table_.keywordSet().get("nBeam",sdh.nbeam);
1302 table_.keywordSet().get("nIF",sdh.nif);
1303 table_.keywordSet().get("nPol",sdh.npol);
1304 table_.keywordSet().get("nChan",sdh.nchan);
1305 table_.keywordSet().get("Observer", sdh.observer);
1306 table_.keywordSet().get("Project", sdh.project);
1307 table_.keywordSet().get("Obstype", sdh.obstype);
1308 table_.keywordSet().get("AntennaName", sdh.antennaname);
1309 table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
1310 table_.keywordSet().get("Equinox", sdh.equinox);
1311 table_.keywordSet().get("FreqRefFrame", sdh.freqref);
1312 table_.keywordSet().get("FreqRefVal", sdh.reffreq);
1313 table_.keywordSet().get("Bandwidth", sdh.bandwidth);
1314 table_.keywordSet().get("UTC", sdh.utc);
1315 table_.keywordSet().get("FluxUnit", sdh.fluxunit);
1316 table_.keywordSet().get("Epoch", sdh.epoch);
1317 return sdh;
1318}
1319void SDMemTable::makePersistent(const std::string& filename)
1320{
1321 table_.deepCopy(filename,Table::New);
1322
1323}
1324
1325Int SDMemTable::nScan() const {
1326 Int n = 0;
1327 Int previous = -1;Int current=0;
1328 for (uInt i=0; i< scanCol_.nrow();i++) {
1329 scanCol_.getScalar(i,current);
1330 if (previous != current) {
1331 previous = current;
1332 n++;
1333 }
1334 }
1335 return n;
1336}
1337
1338String SDMemTable::formatSec(Double x) const
1339{
1340 Double xcop = x;
1341 MVTime mvt(xcop/24./3600.); // make days
1342
1343 if (x < 59.95)
1344 return String(" ") + mvt.string(MVTime::TIME_CLEAN_NO_HM, 7)+"s";
1345 else if (x < 3599.95)
1346 return String(" ") + mvt.string(MVTime::TIME_CLEAN_NO_H,7)+" ";
1347 else {
1348 ostringstream oss;
1349 oss << setw(2) << std::right << setprecision(1) << mvt.hour();
1350 oss << ":" << mvt.string(MVTime::TIME_CLEAN_NO_H,7) << " ";
1351 return String(oss);
1352 }
1353};
1354
1355String SDMemTable::formatDirection(const MDirection& md) const
1356{
1357 Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
1358 Int prec = 7;
1359
1360 MVAngle mvLon(t[0]);
1361 String sLon = mvLon.string(MVAngle::TIME,prec);
1362 MVAngle mvLat(t[1]);
1363 String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec);
1364 return sLon + String(" ") + sLat;
1365}
1366
1367
1368std::string SDMemTable::getFluxUnit() const
1369{
1370 String tmp;
1371 table_.keywordSet().get("FluxUnit", tmp);
1372 return tmp;
1373}
1374
1375void SDMemTable::setFluxUnit(const std::string& unit)
1376{
1377 String tmp(unit);
1378 Unit tU(tmp);
1379 if (tU==Unit("K") || tU==Unit("Jy")) {
1380 table_.rwKeywordSet().define(String("FluxUnit"), tmp);
1381 } else {
1382 throw AipsError("Illegal unit - must be compatible with Jy or K");
1383 }
1384}
1385
1386
1387void SDMemTable::setInstrument(const std::string& name)
1388{
1389 Bool throwIt = True;
1390 Instrument ins = SDAttr::convertInstrument(name, throwIt);
1391 String nameU(name);
1392 nameU.upcase();
1393 table_.rwKeywordSet().define(String("AntennaName"), nameU);
1394}
1395
1396std::string SDMemTable::summary(bool verbose) const {
1397
1398 // Format header info
1399 ostringstream oss;
1400 oss << endl;
1401 oss << "--------------------------------------------------------------------------------" << endl;
1402 oss << " Scan Table Summary" << endl;
1403 oss << "--------------------------------------------------------------------------------" << endl;
1404 oss.flags(std::ios_base::left);
1405 oss << setw(15) << "Beams:" << setw(4) << nBeam() << endl
1406 << setw(15) << "IFs:" << setw(4) << nIF() << endl
1407 << setw(15) << "Polarisations:" << setw(4) << nPol() << endl
1408 << setw(15) << "Channels:" << setw(4) << nChan() << endl;
1409 oss << endl;
1410 String tmp;
1411 table_.keywordSet().get("Observer", tmp);
1412 oss << setw(15) << "Observer:" << tmp << endl;
1413 oss << setw(15) << "Obs Date:" << getTime(-1,True) << endl;
1414 table_.keywordSet().get("Project", tmp);
1415 oss << setw(15) << "Project:" << tmp << endl;
1416 table_.keywordSet().get("Obstype", tmp);
1417 oss << setw(15) << "Obs. Type:" << tmp << endl;
1418 table_.keywordSet().get("AntennaName", tmp);
1419 oss << setw(15) << "Antenna Name:" << tmp << endl;
1420 table_.keywordSet().get("FluxUnit", tmp);
1421 oss << setw(15) << "Flux Unit:" << tmp << endl;
1422 Table t = table_.keywordSet().asTable("FREQUENCIES");
1423 Vector<Double> vec;
1424 t.keywordSet().get("RESTFREQS",vec);
1425 oss << setw(15) << "Rest Freqs:";
1426 if (vec.nelements() > 0) {
1427 oss << setprecision(10) << vec << " [Hz]" << endl;
1428 } else {
1429 oss << "None set" << endl;
1430 }
1431 oss << setw(15) << "Abcissa:" << getAbcissaString() << endl;
1432 oss << setw(15) << "Cursor:" << "Beam[" << getBeam() << "] "
1433 << "IF[" << getIF() << "] " << "Pol[" << getPol() << "]" << endl;
1434 oss << endl;
1435
1436 String dirtype ="Position ("+ MDirection::showType(getDirectionReference()) + ")";
1437 oss << setw(5) << "Scan"
1438 << setw(15) << "Source"
1439 << setw(24) << dirtype
1440 << setw(10) << "Time"
1441 << setw(18) << "Integration"
1442 << setw(7) << "FreqIDs" << endl;
1443 oss << "--------------------------------------------------------------------------------" << endl;
1444
1445 // Generate list of scan start and end integrations
1446 Vector<Int> scanIDs = scanCol_.getColumn();
1447 Vector<uInt> startInt, endInt;
1448 mathutil::scanBoundaries(startInt, endInt, scanIDs);
1449
1450 const uInt nScans = startInt.nelements();
1451 String name;
1452 Vector<uInt> freqIDs, listFQ;
1453 Vector<uInt> restFreqIDs, listRestFQ;
1454 uInt nInt;
1455
1456 for (uInt i=0; i<nScans; i++) {
1457 // Get things from first integration of scan
1458 String time = getTime(startInt(i),False);
1459 String tInt = formatSec(Double(getInterval(startInt(i))));
1460 String posit = formatDirection(getDirection(startInt(i),True));
1461 srcnCol_.getScalar(startInt(i),name);
1462
1463 // Find all the FreqIDs in this scan
1464 listFQ.resize(0);
1465 listRestFQ.resize(0);
1466 for (uInt j=startInt(i); j<endInt(i)+1; j++) {
1467 freqidCol_.get(j, freqIDs);
1468 for (uInt k=0; k<freqIDs.nelements(); k++) {
1469 mathutil::addEntry(listFQ, freqIDs(k));
1470 }
1471//
1472 restfreqidCol_.get(j, restFreqIDs);
1473 for (uInt k=0; k<restFreqIDs.nelements(); k++) {
1474 mathutil::addEntry(listRestFQ, restFreqIDs(k));
1475 }
1476 }
1477
1478 nInt = endInt(i) - startInt(i) + 1;
1479 oss << setw(3) << std::right << i << std::left << setw(2) << " "
1480 << setw(15) << name
1481 << setw(24) << posit
1482 << setw(10) << time
1483 << setw(3) << std::right << nInt << setw(3) << " x " << std::left
1484 << setw(6) << tInt
1485 << " " << listFQ << " " << listRestFQ << endl;
1486 }
1487 oss << endl;
1488 oss << "Table contains " << table_.nrow() << " integration(s) in "
1489 << nScans << " scan(s)." << endl;
1490
1491 // Frequency Table
1492 if (verbose) {
1493 std::vector<string> info = getCoordInfo();
1494 SDFrequencyTable sdft = getSDFreqTable();
1495 oss << endl << endl;
1496 oss << "FreqID Frame RefFreq(Hz) RefPix Increment(Hz)" << endl;
1497 oss << "--------------------------------------------------------------------------------" << endl;
1498 for (uInt i=0; i<sdft.length(); i++) {
1499 oss << setw(8) << i << setw(8)
1500 << info[3] << setw(16) << setprecision(8)
1501 << sdft.referenceValue(i) << setw(10)
1502 << sdft.referencePixel(i) << setw(12)
1503 << sdft.increment(i) << endl;
1504 }
1505 oss << "--------------------------------------------------------------------------------" << endl;
1506 }
1507 return String(oss);
1508}
1509/*
1510std::string SDMemTable::scanSummary(const std::vector<int>& whichScans) {
1511 ostringstream oss;
1512 Vector<Int> scanIDs = scanCol_.getColumn();
1513 Vector<uInt> startInt, endInt;
1514 mathutil::scanBoundaries(startInt, endInt, scanIDs);
1515 const uInt nScans = startInt.nelements();
1516 std::vector<int>::const_iterator it(whichScans);
1517 return String(oss);
1518}
1519*/
1520Int SDMemTable::nBeam() const
1521{
1522 Int n;
1523 table_.keywordSet().get("nBeam",n);
1524 return n;
1525}
1526
1527Int SDMemTable::nIF() const {
1528 Int n;
1529 table_.keywordSet().get("nIF",n);
1530 return n;
1531}
1532
1533Int SDMemTable::nPol() const {
1534 Int n;
1535 table_.keywordSet().get("nPol",n);
1536 return n;
1537}
1538
1539Int SDMemTable::nChan() const {
1540 Int n;
1541 table_.keywordSet().get("nChan",n);
1542 return n;
1543}
1544
1545Table SDMemTable::getHistoryTable() const
1546{
1547 return table_.keywordSet().asTable("HISTORY");
1548}
1549
1550void SDMemTable::appendToHistoryTable(const Table& otherHist)
1551{
1552 Table t = table_.rwKeywordSet().asTable("HISTORY");
1553 const String sep = "--------------------------------------------------------------------------------";
1554 addHistory(sep);
1555 TableCopy::copyRows(t, otherHist, t.nrow(), 0, otherHist.nrow());
1556 addHistory(sep);
1557}
1558
1559void SDMemTable::addHistory(const std::string& hist)
1560{
1561 Table t = table_.rwKeywordSet().asTable("HISTORY");
1562 uInt nrow = t.nrow();
1563 t.addRow();
1564 ScalarColumn<String> itemCol(t, "ITEM");
1565 itemCol.put(nrow, hist);
1566}
1567
1568std::vector<std::string> SDMemTable::getHistory() const
1569{
1570 Vector<String> history;
1571 const Table& t = table_.keywordSet().asTable("HISTORY");
1572 uInt nrow = t.nrow();
1573 ROScalarColumn<String> itemCol(t, "ITEM");
1574 std::vector<std::string> stlout;
1575 String hist;
1576 for (uInt i=0; i<nrow; ++i) {
1577 itemCol.get(i, hist);
1578 stlout.push_back(hist);
1579 }
1580 return stlout;
1581}
1582
1583
1584/*
1585 void SDMemTable::maskChannels(const std::vector<Int>& whichChans ) {
1586
1587 std::vector<int>::iterator it;
1588 ArrayAccessor<uChar, Axis<asap::PolAxis> > j(flags_);
1589 for (it = whichChans.begin(); it != whichChans.end(); it++) {
1590 j.reset(j.begin(uInt(*it)));
1591 for (ArrayAccessor<uChar, Axis<asap::BeamAxis> > i(j); i != i.end(); ++i) {
1592 for (ArrayAccessor<uChar, Axis<asap::IFAxis> > ii(i); ii != ii.end(); ++ii) {
1593 for (ArrayAccessor<uChar, Axis<asap::ChanAxis> > iii(ii);
1594 iii != iii.end(); ++iii) {
1595 (*iii) =
1596 }
1597 }
1598 }
1599 }
1600
1601}
1602*/
1603void SDMemTable::flag(int whichRow)
1604 {
1605 Array<uChar> arr;
1606 flagsCol_.get(whichRow, arr);
1607
1608 ArrayAccessor<uChar, Axis<asap::BeamAxis> > aa0(arr);
1609 aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
1610 ArrayAccessor<uChar, Axis<asap::IFAxis> > aa1(aa0);
1611 aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
1612 ArrayAccessor<uChar, Axis<asap::PolAxis> > aa2(aa1);
1613 aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
1614
1615 for (ArrayAccessor<uChar, Axis<asap::ChanAxis> > i(aa2); i != i.end(); ++i) {
1616 (*i) = uChar(True);
1617 }
1618
1619 flagsCol_.put(whichRow, arr);
1620}
1621
1622MDirection::Types SDMemTable::getDirectionReference() const
1623{
1624 Float eq;
1625 table_.keywordSet().get("Equinox",eq);
1626 std::map<float,string> mp;
1627 mp[2000.0] = "J2000";
1628 mp[1950.0] = "B1950";
1629 MDirection::Types mdr;
1630 if (!MDirection::getType(mdr, mp[eq])) {
1631 mdr = MDirection::J2000;
1632 pushLog("WARNING: Unknown equinox using J2000");
1633
1634 }
1635
1636 return mdr;
1637}
1638
1639MEpoch::Types SDMemTable::getTimeReference() const
1640{
1641 MEpoch::Types met;
1642 String ep;
1643 table_.keywordSet().get("Epoch",ep);
1644 if (!MEpoch::getType(met, ep)) {
1645 pushLog("WARNING: Epoch type unknown - using UTC");
1646 met = MEpoch::UTC;
1647 }
1648
1649 return met;
1650}
1651
1652
1653Bool SDMemTable::setRestFreqs(const Vector<Double>& restFreqsIn,
1654 const String& sUnit,
1655 const vector<string>& lines,
1656 const String& source,
1657 Int whichIF)
1658{
1659 const Int nIFs = nIF();
1660 if (whichIF>=nIFs) {
1661 throw(AipsError("Illegal IF index"));
1662 }
1663
1664 // Find vector of restfrequencies
1665 // Double takes precedence over String
1666 Unit unit;
1667 Vector<Double> restFreqs;
1668 if (restFreqsIn.nelements()>0) {
1669 restFreqs.resize(restFreqsIn.nelements());
1670 restFreqs = restFreqsIn;
1671 unit = Unit(sUnit);
1672 } else if (lines.size()>0) {
1673 const uInt nLines = lines.size();
1674 unit = Unit("Hz");
1675 restFreqs.resize(nLines);
1676 MFrequency lineFreq;
1677 for (uInt i=0; i<nLines; i++) {
1678 String tS(lines[i]);
1679 tS.upcase();
1680 if (MeasTable::Line(lineFreq, tS)) {
1681 restFreqs[i] = lineFreq.getValue().getValue(); // Hz
1682 } else {
1683 String s = String(lines[i]) +
1684 String(" is an unrecognized spectral line");
1685 throw(AipsError(s));
1686 }
1687 }
1688 } else {
1689 throw(AipsError("You have not specified any rest frequencies or lines"));
1690 }
1691
1692 // If multiple restfreqs, must be length nIF. In this
1693 // case we will just replace the rest frequencies
1694 // We can't disinguish scalar and vector of length 1
1695 const uInt nRestFreqs = restFreqs.nelements();
1696 Int idx = -1;
1697 SDFrequencyTable sdft = getSDFreqTable();
1698
1699 ostringstream oss;
1700 if (nRestFreqs>1) {
1701 // Replace restFreqs, one per IF
1702 if (nRestFreqs != nIFs) {
1703 throw (AipsError("Number of rest frequencies must be equal to the number of IFs"));
1704 }
1705 ostringstream oss;
1706 oss << "Replacing rest frequencies, one per IF, with given list : " << restFreqs;
1707 sdft.deleteRestFrequencies();
1708 for (uInt i=0; i<nRestFreqs; i++) {
1709 Quantum<Double> rf(restFreqs[i], unit);
1710 sdft.addRestFrequency(rf.getValue("Hz"));
1711 }
1712 } else {
1713
1714 // Add new rest freq
1715 Quantum<Double> rf(restFreqs[0], unit);
1716 idx = sdft.addRestFrequency(rf.getValue("Hz"));
1717 if (whichIF>=0) {
1718 oss << "Selecting given rest frequency (" << restFreqs[0] << ") for IF " << whichIF << endl;
1719 } else {
1720 oss << "Selecting given rest frequency (" << restFreqs[0] << ") for all IFs" << endl;
1721 }
1722 }
1723 pushLog(String(oss));
1724 // Replace
1725 Bool empty = source.empty();
1726 Bool ok = False;
1727 if (putSDFreqTable(sdft)) {
1728 const uInt nRow = table_.nrow();
1729 String srcName;
1730 Vector<uInt> restFreqIDs;
1731 for (uInt i=0; i<nRow; i++) {
1732 srcnCol_.get(i, srcName);
1733 restfreqidCol_.get(i,restFreqIDs);
1734 if (idx==-1) {
1735 // Replace vector of restFreqs; one per IF.
1736 // No selection possible
1737 for (uInt i=0; i<nIFs; i++) restFreqIDs[i] = i;
1738 } else {
1739 // Set RestFreqID for selected data
1740 if (empty || source==srcName) {
1741 if (whichIF<0) {
1742 restFreqIDs = idx;
1743 } else {
1744 restFreqIDs[whichIF] = idx;
1745 }
1746 }
1747 }
1748 restfreqidCol_.put(i,restFreqIDs);
1749 }
1750 ok = True;
1751 } else {
1752 ok = False;
1753 }
1754
1755 return ok;
1756}
1757
1758std::string SDMemTable::spectralLines() const
1759{
1760 Vector<String> lines = MeasTable::Lines();
1761 MFrequency lineFreq;
1762 Double freq;
1763 ostringstream oss;
1764
1765 oss.flags(std::ios_base::left);
1766 oss << "Line Frequency (Hz)" << endl;
1767 oss << "-----------------------" << endl;
1768 for (uInt i=0; i<lines.nelements(); i++) {
1769 MeasTable::Line(lineFreq, lines[i]);
1770 freq = lineFreq.getValue().getValue(); // Hz
1771 oss << setw(11) << lines[i] << setprecision(10) << freq << endl;
1772 }
1773 return String(oss);
1774}
1775
1776void SDMemTable::renumber()
1777{
1778 uInt nRow = scanCol_.nrow();
1779 Int newscanid = 0;
1780 Int cIdx;// the current scanid
1781 // get the first scanid
1782 scanCol_.getScalar(0,cIdx);
1783 Int pIdx = cIdx;// the scanid of the previous row
1784 for (uInt i=0; i<nRow;++i) {
1785 scanCol_.getScalar(i,cIdx);
1786 if (pIdx == cIdx) {
1787 // renumber
1788 scanCol_.put(i,newscanid);
1789 } else {
1790 ++newscanid;
1791 pIdx = cIdx; // store scanid
1792 --i; // don't increment next loop
1793 }
1794 }
1795}
1796
1797
1798void SDMemTable::getCursorSlice(IPosition& start, IPosition& end,
1799 const IPosition& shape) const
1800{
1801 const uInt nDim = shape.nelements();
1802 start.resize(nDim);
1803 end.resize(nDim);
1804
1805 start(asap::BeamAxis) = beamSel_;
1806 end(asap::BeamAxis) = beamSel_;
1807 start(asap::IFAxis) = IFSel_;
1808 end(asap::IFAxis) = IFSel_;
1809
1810 start(asap::PolAxis) = polSel_;
1811 end(asap::PolAxis) = polSel_;
1812
1813 start(asap::ChanAxis) = 0;
1814 end(asap::ChanAxis) = shape(asap::ChanAxis) - 1;
1815}
1816
1817
1818std::vector<float> SDMemTable::getFloatSpectrum(const Array<Float>& arr) const
1819 // Get spectrum at cursor location
1820{
1821
1822 // Setup accessors
1823 ArrayAccessor<Float, Axis<asap::BeamAxis> > aa0(arr);
1824 aa0.reset(aa0.begin(uInt(beamSel_))); // Beam selection
1825
1826 ArrayAccessor<Float, Axis<asap::IFAxis> > aa1(aa0);
1827 aa1.reset(aa1.begin(uInt(IFSel_))); // IF selection
1828
1829 ArrayAccessor<Float, Axis<asap::PolAxis> > aa2(aa1);
1830 aa2.reset(aa2.begin(uInt(polSel_))); // Pol selection
1831
1832 std::vector<float> spectrum;
1833 for (ArrayAccessor<Float, Axis<asap::ChanAxis> > i(aa2); i != i.end(); ++i) {
1834 spectrum.push_back(*i);
1835 }
1836 return spectrum;
1837}
1838
Note: See TracBrowser for help on using the repository browser.