1 | //#---------------------------------------------------------------------------
|
---|
2 | //# SDMemTable.cc: A MemoryTable container for single dish integrations
|
---|
3 | //#---------------------------------------------------------------------------
|
---|
4 | //# Copyright (C) 2004
|
---|
5 | //# Malte Marquarding, 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 <aips/iostream.h>
|
---|
33 | #include <aips/Arrays/Array.h>
|
---|
34 | #include <aips/Arrays/ArrayMath.h>
|
---|
35 | #include <aips/Arrays/MaskArrMath.h>
|
---|
36 | #include <aips/Arrays/ArrayLogical.h>
|
---|
37 | #include <aips/Arrays/ArrayAccessor.h>
|
---|
38 |
|
---|
39 | #include <aips/Tables/TableParse.h>
|
---|
40 | #include <aips/Tables/TableDesc.h>
|
---|
41 | #include <aips/Tables/SetupNewTab.h>
|
---|
42 | #include <aips/Tables/ScaColDesc.h>
|
---|
43 | #include <aips/Tables/ArrColDesc.h>
|
---|
44 |
|
---|
45 | #include <aips/Tables/ExprNode.h>
|
---|
46 | #include <aips/Tables/ScalarColumn.h>
|
---|
47 | #include <aips/Tables/ArrayColumn.h>
|
---|
48 |
|
---|
49 | #include "SDMemTable.h"
|
---|
50 | #include "SDContainer.h"
|
---|
51 |
|
---|
52 | using namespace atnf_sd;
|
---|
53 |
|
---|
54 | SDMemTable::SDMemTable(const std::string& name) :
|
---|
55 | IFSel_(0),
|
---|
56 | beamSel_(0),
|
---|
57 | polSel_(0) {
|
---|
58 | name_ = String(name);
|
---|
59 | setup();
|
---|
60 | }
|
---|
61 |
|
---|
62 | SDMemTable::SDMemTable(const SDMemTable& other) {
|
---|
63 | this->IFSel_= other.IFSel_;
|
---|
64 | this->beamSel_= other.beamSel_;
|
---|
65 | this->polSel_= other.polSel_;
|
---|
66 | this->chanMask_ = other.chanMask_;
|
---|
67 | this->name_ = String("dummy");
|
---|
68 | this->table_ = other.table_.copyToMemoryTable(String("dummy"));
|
---|
69 | // clear all rows()
|
---|
70 | this->table_.removeRow(this->table_.rowNumbers());
|
---|
71 | }
|
---|
72 |
|
---|
73 | SDMemTable::SDMemTable(const Table& tab, Int scanID) :
|
---|
74 | IFSel_(0),
|
---|
75 | beamSel_(0),
|
---|
76 | polSel_(0) {
|
---|
77 | name_ = String("SDMemTable");
|
---|
78 | String exprs = String("select * from $1 where SCANID == ")
|
---|
79 | +String::toString(scanID);
|
---|
80 | cerr << exprs << endl;
|
---|
81 | Table t = tableCommand(exprs,tab);
|
---|
82 | table_ = t.copyToMemoryTable(name_);
|
---|
83 | }
|
---|
84 |
|
---|
85 | SDMemTable::~SDMemTable(){
|
---|
86 | cerr << "goodbye from SDMemTable @ " << this << endl;
|
---|
87 | }
|
---|
88 |
|
---|
89 | SDMemTable SDMemTable::getScan(Int scanID) {
|
---|
90 | return SDMemTable(table_, scanID);
|
---|
91 | }
|
---|
92 |
|
---|
93 | void SDMemTable::setup() {
|
---|
94 | TableDesc td("", "1", TableDesc::Scratch);
|
---|
95 | td.comment() = "A SDMemTable";
|
---|
96 | //td.rwKeywordSet().define("VERSION",Float(0.1));
|
---|
97 | td.addColumn(ScalarColumnDesc<Double>("TIME"));
|
---|
98 | td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
|
---|
99 | td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
|
---|
100 | td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
|
---|
101 | td.addColumn(ArrayColumnDesc<Float>("TSYS"));
|
---|
102 | td.addColumn(ScalarColumnDesc<Int>("SCANID"));
|
---|
103 | // Now create a new table from the description.
|
---|
104 | SetupNewTable aNewTab(name_, td, Table::New);
|
---|
105 | table_ = Table(aNewTab, Table::Memory, 0);
|
---|
106 | }
|
---|
107 |
|
---|
108 | std::string SDMemTable::name() const {
|
---|
109 | return name_;
|
---|
110 | }
|
---|
111 |
|
---|
112 | std::string SDMemTable::getSourceName(Int whichRow) const {
|
---|
113 | ROScalarColumn<String> src(table_, "SRCNAME");
|
---|
114 | String name;
|
---|
115 | src.get(whichRow, name);
|
---|
116 | return name;
|
---|
117 | }
|
---|
118 |
|
---|
119 | Double SDMemTable::getTime(Int whichRow) const {
|
---|
120 | ROScalarColumn<Double> src(table_, "TIME");
|
---|
121 | Double tm;
|
---|
122 | src.get(whichRow, tm);
|
---|
123 | return tm;
|
---|
124 | }
|
---|
125 |
|
---|
126 | bool SDMemTable::setIF(Int whichIF) {
|
---|
127 | //if ( whichIF >= 0 && whichIF < nIF_) {
|
---|
128 | IFSel_ = whichIF;
|
---|
129 | return true;
|
---|
130 | //}
|
---|
131 | //return false;
|
---|
132 | }
|
---|
133 | bool SDMemTable::setBeam(Int whichBeam) {
|
---|
134 | //if ( whichBeam >= 0 && whichBeam < nBeam_) {
|
---|
135 | beamSel_ = whichBeam;
|
---|
136 | return true;
|
---|
137 | //}
|
---|
138 | //return false;
|
---|
139 |
|
---|
140 | }
|
---|
141 | bool SDMemTable::setPol(Int whichPol) {
|
---|
142 | //if ( whichPol >= 0 && whichPol < nPol_) {
|
---|
143 | polSel_ = whichPol;
|
---|
144 | return true;
|
---|
145 | //}
|
---|
146 | //return false;
|
---|
147 | }
|
---|
148 |
|
---|
149 | bool SDMemTable::setChannels(const std::vector<int>& whichChans) {
|
---|
150 | //std::vector<bool>::iterator it;
|
---|
151 | //std::fill(chanMask_.begin(), chanMask_.end(), false);
|
---|
152 | //for (it = whichChans.begin(); it != whichChans.end(); it++) {
|
---|
153 | //chanMask_[*it] = true;
|
---|
154 | //}
|
---|
155 | cout << "SDMemTable::setChannels() disabled" << endl;
|
---|
156 | return true;
|
---|
157 | }
|
---|
158 |
|
---|
159 | std::vector<bool> SDMemTable::getMask() const {
|
---|
160 | return chanMask_;
|
---|
161 | }
|
---|
162 | std::vector<float> SDMemTable::getSpectrum(Int whichRow) const {
|
---|
163 |
|
---|
164 | std::vector<float> spectrum;
|
---|
165 | ROArrayColumn<Float> spec(table_, "SPECTRA");
|
---|
166 | Array<Float> arr;
|
---|
167 | spec.get(whichRow, arr);
|
---|
168 | ArrayAccessor<Float, Axis<0> > aa0(arr);
|
---|
169 | aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam
|
---|
170 | ArrayAccessor<Float, Axis<1> > aa1(aa0);
|
---|
171 | aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF
|
---|
172 | for (ArrayAccessor<Float, Axis<2> > i(aa1); i != i.end(); ++i) {
|
---|
173 | ArrayAccessor<Float, Axis<3> > aa2(i);
|
---|
174 | aa2.reset(aa2.begin(uInt(polSel_)));// go to pol
|
---|
175 | spectrum.push_back(*aa2);
|
---|
176 | }
|
---|
177 | return spectrum;
|
---|
178 | }
|
---|
179 |
|
---|
180 | MaskedArray<Float> SDMemTable::rowAsMaskedArray(uInt whichRow) {
|
---|
181 | ROArrayColumn<Float> spec(table_, "SPECTRA");
|
---|
182 | Array<Float> arr;
|
---|
183 | ROArrayColumn<uChar> flag(table_, "FLAGTRA");
|
---|
184 | Array<uChar> farr;
|
---|
185 | spec.get(whichRow, arr);
|
---|
186 | flag.get(whichRow, farr);
|
---|
187 | Array<Bool> barr(farr.shape());convertArray(barr, farr);
|
---|
188 | MaskedArray<Float> marr;
|
---|
189 | marr.setData(arr,!barr);
|
---|
190 | return marr;
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | Float SDMemTable::getTsys(Int whichRow) const {
|
---|
195 | ROArrayColumn<Float> ts(table_, "TSYS");
|
---|
196 | Array<Float> arr;
|
---|
197 | ts.get(whichRow, arr);
|
---|
198 | Float out;
|
---|
199 | IPosition ip(arr.shape());
|
---|
200 | ip(0) = beamSel_;ip(1) = IFSel_;ip(2) = polSel_;
|
---|
201 | out = arr(ip);
|
---|
202 | return out;
|
---|
203 | }
|
---|
204 |
|
---|
205 | bool SDMemTable::putSDContainer(const SDContainer& sdc) {
|
---|
206 | ScalarColumn<Double> mjd(table_, "TIME");
|
---|
207 | ScalarColumn<String> srcn(table_, "SRCNAME");
|
---|
208 | ArrayColumn<Float> spec(table_, "SPECTRA");
|
---|
209 | ArrayColumn<uChar> flags(table_, "FLAGTRA");
|
---|
210 | ArrayColumn<Float> ts(table_, "TSYS");
|
---|
211 | ScalarColumn<Int> scan(table_, "SCANID");
|
---|
212 |
|
---|
213 | uInt rno = table_.nrow();
|
---|
214 | table_.addRow();
|
---|
215 |
|
---|
216 | mjd.put(rno, sdc.timestamp);
|
---|
217 | srcn.put(rno, sdc.sourcename);
|
---|
218 | spec.put(rno, sdc.getSpectrum());
|
---|
219 | flags.put(rno, sdc.getFlags());
|
---|
220 | ts.put(rno, sdc.getTsys());
|
---|
221 | scan.put(rno, sdc.scanid);
|
---|
222 | return true;
|
---|
223 | }
|
---|
224 | void SDMemTable::makePersistent(const std::string& filename) {
|
---|
225 | table_.deepCopy(filename,Table::New);
|
---|
226 | }
|
---|
227 |
|
---|
228 | void SDMemTable::summary() const {
|
---|
229 | cerr << "SDMemTable::summary()" << endl;
|
---|
230 | ROScalarColumn<Int> scans(table_, "SCANID");
|
---|
231 | ROScalarColumn<String> srcs(table_, "SRCNAME");
|
---|
232 | uInt count = 0;
|
---|
233 | String name;
|
---|
234 | Int previous = -1;Int current=0;
|
---|
235 | cout << "Scan\tSource" << endl;
|
---|
236 | for (uInt i=0; i< scans.nrow();i++) {
|
---|
237 | scans.getScalar(i,current);
|
---|
238 | if (previous != current) {
|
---|
239 | srcs.getScalar(i,name);
|
---|
240 | previous = current;
|
---|
241 | count++;
|
---|
242 | cout << count << "\t" << name << endl;
|
---|
243 | }
|
---|
244 | }
|
---|
245 | cout << "Table contains " << table_.nrow() << "integrations." << endl;
|
---|
246 | cout << "in " << count << "scans." << endl;
|
---|
247 | }
|
---|
248 |
|
---|