source: trunk/src/SDMemTable.cc@ 464

Last change on this file since 464 was 464, checked in by kil064, 20 years ago

make sure i register the StokesVirtualMachine class

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