Line data Source code
1 : //# BaselineTable.cc: this code defines baseline table
2 : //# Copyright (C) 2015
3 : //# National Astronomical Observatory of Japan
4 : //#
5 : //# This library is free software; you can redistribute it and/or modify it
6 : //# under the terms of the GNU Library General Public License as published by
7 : //# the Free Software Foundation; either version 2 of the License, or (at your
8 : //# option) any later version.
9 : //#
10 : //# This library is distributed in the hope that it will be useful, but WITHOUT
11 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 : //# License for more details.
14 : //#
15 : //# You should have received a copy of the GNU Library General Public License
16 : //# along with this library; if not, write to the Free Software Foundation,
17 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 : //#
19 : //# Correspondence concerning AIPS++ should be addressed as follows:
20 : //# Internet email: casa-feedback@nrao.edu.
21 : //# Postal address: AIPS++ Project Office
22 : //# National Radio Astronomy Observatory
23 : //# 520 Edgemont Road
24 : //# Charlottesville, VA 22903-2475 USA
25 : //#
26 : //# $Id$
27 : #include <assert.h>
28 :
29 : #include <casacore/casa/Containers/ValueHolder.h>
30 : #include <casacore/casa/Exceptions/Error.h>
31 : #include <casacore/casa/OS/Path.h>
32 : #include <casacore/measures/TableMeasures/TableMeasDesc.h>
33 : #include <casacore/measures/TableMeasures/TableMeasRefDesc.h>
34 : #include <casacore/measures/TableMeasures/TableMeasValueDesc.h>
35 : #include <singledish/SingleDish/BaselineTable.h>
36 : #include <stdcasa/StdCasa/CasacSupport.h>
37 : #include <casacore/tables/Tables/TableDesc.h>
38 : #include <casacore/tables/Tables/SetupNewTab.h>
39 : #include <casacore/tables/Tables/ArrColDesc.h>
40 : #include <casacore/tables/Tables/ScaColDesc.h>
41 : #include <casacore/tables/Tables/TableRecord.h>
42 : #include <casacore/tables/Tables/TableProxy.h>
43 :
44 : using namespace casacore;
45 : using namespace casacore;
46 : using namespace casacore;
47 : using namespace casacore;
48 : using namespace casacore;
49 : using namespace casacore;
50 : using namespace casacore;
51 : namespace casa {
52 :
53 : const String BaselineTable::name_ = "APPLY_BASELINE";
54 :
55 81 : BaselineTable::BaselineTable(const MeasurementSet& parent)
56 : {
57 162 : TableDesc td("", "1", TableDesc::Scratch);
58 81 : td.addColumn(ScalarColumnDesc<uInt>("SCANNO"));
59 81 : td.addColumn(ScalarColumnDesc<uInt>("BEAMNO"));
60 81 : td.addColumn(ScalarColumnDesc<uInt>("ANTNO"));
61 81 : td.addColumn(ScalarColumnDesc<uInt>("IFNO"));
62 81 : td.addColumn(ScalarColumnDesc<uInt>("FREQ_ID"));
63 81 : td.addColumn(ScalarColumnDesc<Double>("TIME"));
64 81 : TableMeasRefDesc measRef(MEpoch::UTC); // UTC as default
65 81 : TableMeasValueDesc measVal(td, "TIME");
66 81 : TableMeasDesc<MEpoch> mepochCol(measVal, measRef);
67 81 : mepochCol.write(td);
68 81 : String tabname = parent.tableName()+"/"+name_;
69 81 : SetupNewTable aNewTab(tabname, td, Table::Scratch);
70 81 : table_ = Table(aNewTab, Table::Memory);
71 81 : attachBaseColumns();
72 :
73 81 : table_.rwKeywordSet().define("VERSION", 1);
74 81 : table_.rwKeywordSet().define("MSName", parent.tableName());
75 81 : table_.rwKeywordSet().define("ApplyType", "NONE");
76 81 : table_.rwKeywordSet().defineTable("FREQUENCIES", parent.spectralWindow());
77 81 : table_.tableInfo().setType("ApplyTable");
78 81 : originaltable_ = table_;
79 :
80 81 : setup();
81 81 : }
82 :
83 13 : BaselineTable::BaselineTable(const String &name)
84 : {
85 13 : table_ = Table(name, Table::Update);
86 13 : attachBaseColumns();
87 13 : originaltable_ = table_;
88 13 : attachOptionalColumns();
89 13 : }
90 :
91 188 : BaselineTable::~BaselineTable()
92 : {
93 188 : }
94 :
95 0 : void BaselineTable::attach()
96 : {
97 0 : attachBaseColumns();
98 0 : attachOptionalColumns();
99 0 : }
100 :
101 94 : void BaselineTable::attachBaseColumns()
102 : {
103 94 : scanCol_.attach(table_, "SCANNO");
104 94 : beamCol_.attach(table_, "BEAMNO");
105 94 : antCol_.attach(table_, "ANTNO");
106 94 : ifCol_.attach(table_, "IFNO");
107 94 : timeCol_.attach(table_, "TIME");
108 94 : timeMeasCol_.attach(table_, "TIME");
109 94 : freqidCol_.attach(table_, "FREQ_ID");
110 94 : }
111 81 : void BaselineTable::setup()
112 : {
113 81 : table_.addColumn(ArrayColumnDesc<Bool>("APPLY"));
114 81 : table_.addColumn(ArrayColumnDesc<uInt>("FUNC_TYPE"));
115 81 : table_.addColumn(ArrayColumnDesc<Int>("FUNC_PARAM"));
116 81 : table_.addColumn(ArrayColumnDesc<Float>("FUNC_FPARAM"));
117 81 : table_.addColumn(ArrayColumnDesc<uInt>("MASKLIST"));
118 81 : table_.addColumn(ArrayColumnDesc<Float>("RESULT"));
119 81 : table_.addColumn(ArrayColumnDesc<Float>("RMS"));
120 81 : table_.addColumn(ScalarColumnDesc<uInt>("NCHAN"));
121 81 : table_.addColumn(ArrayColumnDesc<Float>("CLIP_THRESHOLD"));
122 81 : table_.addColumn(ArrayColumnDesc<uInt>("CLIP_ITERATION"));
123 81 : table_.addColumn(ArrayColumnDesc<Bool>("USE_LF"));
124 81 : table_.addColumn(ArrayColumnDesc<Float>("LF_THRESHOLD"));
125 81 : table_.addColumn(ArrayColumnDesc<uInt>("LF_AVERAGE"));
126 81 : table_.addColumn(ArrayColumnDesc<uInt>("LF_EDGE"));
127 :
128 81 : table_.rwKeywordSet().define("ApplyType", "BASELINE");
129 :
130 81 : attachOptionalColumns();
131 81 : }
132 :
133 94 : void BaselineTable::attachOptionalColumns()
134 : {
135 94 : applyCol_.attach(table_, "APPLY");
136 94 : ftypeCol_.attach(table_, "FUNC_TYPE");
137 94 : fparCol_.attach(table_, "FUNC_PARAM");
138 94 : ffparCol_.attach(table_, "FUNC_FPARAM");
139 94 : maskCol_.attach(table_, "MASKLIST");
140 94 : resCol_.attach(table_, "RESULT");
141 94 : rmsCol_.attach(table_, "RMS");
142 94 : nchanCol_.attach(table_, "NCHAN");
143 94 : cthresCol_.attach(table_, "CLIP_THRESHOLD");
144 94 : citerCol_.attach(table_, "CLIP_ITERATION");
145 94 : uselfCol_.attach(table_, "USE_LF");
146 94 : lfthresCol_.attach(table_, "LF_THRESHOLD");
147 94 : lfavgCol_.attach(table_, "LF_AVERAGE");
148 94 : lfedgeCol_.attach(table_, "LF_EDGE");
149 :
150 94 : ftypeCol_.rwKeywordSet().define("Polynomial", BaselineType_kPolynomial);
151 94 : ftypeCol_.rwKeywordSet().define("Chebyshev", BaselineType_kChebyshev);
152 94 : ftypeCol_.rwKeywordSet().define("Cubic Spline", BaselineType_kCubicSpline);
153 94 : ftypeCol_.rwKeywordSet().define("Sinusoid", BaselineType_kSinusoid);
154 94 : }
155 :
156 81 : void BaselineTable::save(const std::string &filename)
157 : {
158 81 : String inname(filename);
159 81 : Path path(inname);
160 81 : inname = path.expandedName();
161 81 : table_.deepCopy(inname, Table::New);
162 81 : }
163 :
164 148 : bool BaselineTable::getApply(uInt irow, uInt ipol) const
165 : {
166 148 : Vector<Bool> apply = applyCol_.get(irow);
167 296 : return static_cast<bool>(apply[ipol]);
168 148 : }
169 :
170 73 : std::vector<bool> BaselineTable::getMask(uInt irow, uInt ipol)
171 : {
172 73 : uInt nchan = nchanCol_.get(irow);
173 73 : Matrix<uInt> masklist = maskCol_.get(irow);
174 219 : return getMaskFromMaskList(nchan, masklist.row(ipol));
175 73 : }
176 :
177 146 : uint BaselineTable::getBaselineType(uInt irow, uInt ipol) const
178 : {
179 146 : Vector<uInt> ftype = ftypeCol_.get(irow);
180 292 : return static_cast<uint>(ftype[ipol]);
181 146 : }
182 :
183 73 : int BaselineTable::getFPar(uInt irow, uInt ipol, uInt bltype) const
184 : {
185 73 : auto fpar = fparCol_.get(irow); // Get the stored data
186 :
187 73 : assert(fpar.ndim() == 1 || fpar.ndim() == 2);
188 73 : if (fpar.ndim() == 2) { // If it is a matrix
189 73 : Matrix<Int> fpar_data = fpar;
190 :
191 73 : if (bltype == BaselineType_kSinusoid)
192 6 : return static_cast<int>(max(fpar_data.row(ipol)));
193 :
194 67 : auto res = static_cast<int>(fpar_data.row(ipol)[0]);
195 67 : return res;
196 73 : }
197 : else { // If it is a vector
198 0 : Vector<Int> fpar_data = fpar;
199 0 : return static_cast<int>(fpar_data[ipol]);
200 0 : }
201 73 : }
202 :
203 6 : std::vector<size_t> BaselineTable::getFParSinusoid(uInt irow, uInt ipol) const
204 : {
205 : // To retrieve number of waves array
206 6 : Matrix<Int> fparsin = fparCol_.get(irow);
207 6 : Vector<Int> vector_nwave = fparsin.row(ipol);
208 12 : std::vector<size_t>preprocess(vector_nwave.begin(), vector_nwave.end());
209 : // Due to the use of Matrix in bltable creation, the shape must be rectangular
210 : // By the default initialized by 0s, therefore the following situation can occur
211 : // 0,0 ->[0,1,3,5,12] 0,1 ->[0,1,3,0,0]
212 : // Check all numbers except the first element which can be 0
213 6 : auto it = std::find(preprocess.begin() + 1, preprocess.end(), 0);
214 :
215 : // No 0s found return vector as it is
216 6 : if (it == preprocess.end()) return preprocess;
217 :
218 : // Return the subvector up to (but not including) the first 0
219 2 : return std::vector<size_t>(preprocess.begin(), it);
220 6 : }
221 :
222 269 : void BaselineTable::setbasedata(uInt irow, uInt scanno, uInt beamno, uInt antno,
223 : uInt ifno, uInt freqid, Double time)
224 : {
225 269 : scanCol_.put(irow, scanno);
226 269 : beamCol_.put(irow, beamno);
227 269 : antCol_.put(irow, antno);
228 269 : ifCol_.put(irow, ifno);
229 269 : timeCol_.put(irow, time);
230 269 : freqidCol_.put(irow, freqid);
231 269 : }
232 :
233 269 : void BaselineTable::setdata(uInt irow, uInt scanno,
234 : uInt beamno, uInt antno, uInt ifno,
235 : uInt freqid, Double time,
236 : Array<Bool> apply,
237 : Array<uInt> ftype,
238 : Array<Int> fpar,
239 : Array<Float> ffpar,
240 : Array<uInt> mask,
241 : Array<Float> res,
242 : Array<Float> rms,
243 : uInt nchan,
244 : Array<Float> cthres,
245 : Array<uInt> citer,
246 : Array<Bool> uself,
247 : Array<Float> lfthres,
248 : Array<uInt> lfavg,
249 : Array<uInt> lfedge)
250 : {
251 269 : if (irow >= (uInt)nrow()) {
252 0 : stringstream ss;
253 0 : ss << "row index out of range[irow=" << irow << "][nrow=" << nrow() << "]";
254 0 : throw AipsError(ss.str());
255 0 : }
256 :
257 269 : setbasedata(irow, scanno, beamno, antno, ifno, freqid, time);
258 269 : applyCol_.put(irow, apply);
259 269 : ftypeCol_.put(irow, ftype);
260 269 : fparCol_.put(irow, fpar);
261 269 : ffparCol_.put(irow, ffpar);
262 269 : maskCol_.put(irow, mask);
263 269 : resCol_.put(irow, res);
264 269 : rmsCol_.put(irow, rms);
265 269 : nchanCol_.put(irow, nchan);
266 269 : cthresCol_.put(irow, cthres);
267 269 : citerCol_.put(irow, citer);
268 269 : uselfCol_.put(irow, uself);
269 269 : lfthresCol_.put(irow, lfthres);
270 269 : lfavgCol_.put(irow, lfavg);
271 269 : lfedgeCol_.put(irow, lfedge);
272 269 : }
273 :
274 269 : void BaselineTable::appenddata(uInt scanno, uInt beamno,
275 : uInt antno, uInt ifno,
276 : uInt freqid, Double time,
277 : Array<Bool> apply,
278 : Array<uInt> ftype,
279 : Array<Int> fpar,
280 : Array<Float> ffpar,
281 : Array<uInt> mask,
282 : Array<Float> res,
283 : Array<Float> rms,
284 : uInt nchan,
285 : Array<Float> cthres,
286 : Array<uInt> citer,
287 : Array<Bool> uself,
288 : Array<Float> lfthres,
289 : Array<uInt> lfavg,
290 : Array<uInt> lfedge)
291 : {
292 269 : uInt irow = nrow();
293 269 : table_.addRow(1, true);
294 269 : setdata(irow, scanno, beamno, antno, ifno, freqid, time,
295 : apply, ftype, fpar, ffpar, mask, res, rms,
296 : nchan, cthres, citer, uself, lfthres, lfavg, lfedge);
297 269 : }
298 :
299 0 : void BaselineTable::appendbasedata(int scanno, int beamno,
300 : int antno, int ifno,
301 : int freqid, Double time)
302 : {
303 0 : uInt irow = nrow();
304 0 : table_.addRow(1, true);
305 0 : setbasedata(irow, uInt(scanno), uInt(beamno), uInt(antno),
306 : uInt(ifno), uInt(freqid), time);
307 0 : }
308 :
309 0 : void BaselineTable::setresult(uInt irow,
310 : Vector<Float> res,
311 : Array<Float> rms)
312 : {
313 0 : resCol_.put(irow, res);
314 0 : rmsCol_.put(irow, rms);
315 0 : }
316 :
317 0 : uInt BaselineTable::getNChan(int irow)
318 : {
319 0 : return nchanCol_.get(irow);
320 : }
321 :
322 0 : uInt BaselineTable::nchan(uInt ifno)
323 : {
324 0 : uInt tmp = ifno;
325 0 : return tmp;
326 : }
327 :
328 73 : std::vector<bool> BaselineTable::getMaskFromMaskList(uInt const nchan, Vector<uInt> const& masklist)
329 : {
330 73 : if (masklist.size() % 2 != 0) {
331 0 : throw(AipsError("masklist must have even number of elements."));
332 : }
333 :
334 73 : std::vector<bool> res((int)nchan, false);
335 :
336 164 : for (uInt j = 0; j < masklist.size(); j += 2) {
337 438043 : for (int i = masklist[j]; i <= min((Int)nchan-1, (Int)masklist[j+1]); ++i) {
338 437919 : res[i] = true;
339 : }
340 124 : if (masklist[j+1] == nchan-1) {
341 33 : break;
342 : }
343 : }
344 :
345 73 : return res;
346 0 : }
347 : }
|