source: trunk/src/SDMemTable.cc@ 474

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

add a new getStokesSPectrum function (for the writer) which gets all
Stokes or a beam/IF slice

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