1 | //
|
---|
2 | // C++ Implementation: Scantable
|
---|
3 | //
|
---|
4 | // Description:
|
---|
5 | //
|
---|
6 | //
|
---|
7 | // Author: Malte Marquarding <asap@atnf.csiro.au>, (C) 2005-2013
|
---|
8 | //
|
---|
9 | // Copyright: See COPYING file that comes with this distribution
|
---|
10 | //
|
---|
11 | //
|
---|
12 | #include <map>
|
---|
13 | #include <sys/time.h>
|
---|
14 |
|
---|
15 | #include <atnf/PKSIO/SrcType.h>
|
---|
16 |
|
---|
17 | #include <casa/aips.h>
|
---|
18 | #include <casa/iomanip.h>
|
---|
19 | #include <casa/iostream.h>
|
---|
20 | #include <casa/OS/File.h>
|
---|
21 | #include <casa/OS/Path.h>
|
---|
22 | #include <casa/Logging/LogIO.h>
|
---|
23 | #include <casa/Arrays/Array.h>
|
---|
24 | #include <casa/Arrays/ArrayAccessor.h>
|
---|
25 | #include <casa/Arrays/ArrayLogical.h>
|
---|
26 | #include <casa/Arrays/ArrayMath.h>
|
---|
27 | #include <casa/Arrays/MaskArrMath.h>
|
---|
28 | #include <casa/Arrays/Slice.h>
|
---|
29 | #include <casa/Arrays/Vector.h>
|
---|
30 | #include <casa/Arrays/VectorSTLIterator.h>
|
---|
31 | #include <casa/BasicMath/Math.h>
|
---|
32 | #include <casa/BasicSL/Constants.h>
|
---|
33 | #include <casa/Containers/RecordField.h>
|
---|
34 | #include <casa/Logging/LogIO.h>
|
---|
35 | #include <casa/Quanta/MVAngle.h>
|
---|
36 | #include <casa/Quanta/MVTime.h>
|
---|
37 | #include <casa/Utilities/GenSort.h>
|
---|
38 | #include <casa/Utilities/Assert.h>
|
---|
39 |
|
---|
40 | #include <coordinates/Coordinates/CoordinateUtil.h>
|
---|
41 |
|
---|
42 | // needed to avoid error in .tcc
|
---|
43 | #include <measures/Measures/MCDirection.h>
|
---|
44 | //
|
---|
45 | #include <measures/Measures/MDirection.h>
|
---|
46 | #include <measures/Measures/MEpoch.h>
|
---|
47 | #include <measures/Measures/MFrequency.h>
|
---|
48 | #include <measures/Measures/MeasRef.h>
|
---|
49 | #include <measures/Measures/MeasTable.h>
|
---|
50 | #include <measures/TableMeasures/ScalarMeasColumn.h>
|
---|
51 | #include <measures/TableMeasures/TableMeasDesc.h>
|
---|
52 | #include <measures/TableMeasures/TableMeasRefDesc.h>
|
---|
53 | #include <measures/TableMeasures/TableMeasValueDesc.h>
|
---|
54 |
|
---|
55 | #include <tables/Tables/ArrColDesc.h>
|
---|
56 | #include <tables/Tables/ExprNode.h>
|
---|
57 | #include <tables/Tables/ScaColDesc.h>
|
---|
58 | #include <tables/Tables/SetupNewTab.h>
|
---|
59 | #include <tables/Tables/TableCopy.h>
|
---|
60 | #include <tables/Tables/TableDesc.h>
|
---|
61 | #include <tables/Tables/TableIter.h>
|
---|
62 | #include <tables/Tables/TableParse.h>
|
---|
63 | #include <tables/Tables/TableRecord.h>
|
---|
64 | #include <tables/Tables/TableRow.h>
|
---|
65 | #include <tables/Tables/TableVector.h>
|
---|
66 |
|
---|
67 | #include "MathUtils.h"
|
---|
68 | #include "STAttr.h"
|
---|
69 | #include "STBaselineTable.h"
|
---|
70 | #include "STLineFinder.h"
|
---|
71 | #include "STPolCircular.h"
|
---|
72 | #include "STPolLinear.h"
|
---|
73 | #include "STPolStokes.h"
|
---|
74 | #include "STUpgrade.h"
|
---|
75 | #include "STFitter.h"
|
---|
76 | #include "Scantable.h"
|
---|
77 |
|
---|
78 | #define debug 1
|
---|
79 |
|
---|
80 | using namespace casa;
|
---|
81 |
|
---|
82 | namespace asap {
|
---|
83 |
|
---|
84 | std::map<std::string, STPol::STPolFactory *> Scantable::factories_;
|
---|
85 |
|
---|
86 | void Scantable::initFactories() {
|
---|
87 | if ( factories_.empty() ) {
|
---|
88 | Scantable::factories_["linear"] = &STPolLinear::myFactory;
|
---|
89 | Scantable::factories_["circular"] = &STPolCircular::myFactory;
|
---|
90 | Scantable::factories_["stokes"] = &STPolStokes::myFactory;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | Scantable::Scantable(Table::TableType ttype) :
|
---|
95 | type_(ttype)
|
---|
96 | {
|
---|
97 | initFactories();
|
---|
98 | setupMainTable();
|
---|
99 | freqTable_ = STFrequencies(*this);
|
---|
100 | table_.rwKeywordSet().defineTable("FREQUENCIES", freqTable_.table());
|
---|
101 | weatherTable_ = STWeather(*this);
|
---|
102 | table_.rwKeywordSet().defineTable("WEATHER", weatherTable_.table());
|
---|
103 | focusTable_ = STFocus(*this);
|
---|
104 | table_.rwKeywordSet().defineTable("FOCUS", focusTable_.table());
|
---|
105 | tcalTable_ = STTcal(*this);
|
---|
106 | table_.rwKeywordSet().defineTable("TCAL", tcalTable_.table());
|
---|
107 | moleculeTable_ = STMolecules(*this);
|
---|
108 | table_.rwKeywordSet().defineTable("MOLECULES", moleculeTable_.table());
|
---|
109 | historyTable_ = STHistory(*this);
|
---|
110 | table_.rwKeywordSet().defineTable("HISTORY", historyTable_.table());
|
---|
111 | fitTable_ = STFit(*this);
|
---|
112 | table_.rwKeywordSet().defineTable("FIT", fitTable_.table());
|
---|
113 | table_.tableInfo().setType( "Scantable" ) ;
|
---|
114 | originalTable_ = table_;
|
---|
115 | attach();
|
---|
116 | }
|
---|
117 |
|
---|
118 | Scantable::Scantable(const std::string& name, Table::TableType ttype) :
|
---|
119 | type_(ttype)
|
---|
120 | {
|
---|
121 | initFactories();
|
---|
122 |
|
---|
123 | Table tab(name, Table::Update);
|
---|
124 | uInt version = tab.keywordSet().asuInt("VERSION");
|
---|
125 | if (version != version_) {
|
---|
126 | STUpgrade upgrader(version_);
|
---|
127 | LogIO os( LogOrigin( "Scantable" ) ) ;
|
---|
128 | os << LogIO::WARN
|
---|
129 | << name << " data format version " << version
|
---|
130 | << " is deprecated" << endl
|
---|
131 | << "Running upgrade."<< endl
|
---|
132 | << LogIO::POST ;
|
---|
133 | std::string outname = upgrader.upgrade(name);
|
---|
134 | if ( outname != name ) {
|
---|
135 | os << LogIO::WARN
|
---|
136 | << "Data will be loaded from " << outname << " instead of "
|
---|
137 | << name << LogIO::POST ;
|
---|
138 | tab = Table(outname, Table::Update ) ;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | if ( type_ == Table::Memory ) {
|
---|
142 | table_ = tab.copyToMemoryTable(generateName());
|
---|
143 | } else {
|
---|
144 | table_ = tab;
|
---|
145 | }
|
---|
146 | table_.tableInfo().setType( "Scantable" ) ;
|
---|
147 |
|
---|
148 | attachSubtables();
|
---|
149 | originalTable_ = table_;
|
---|
150 | attach();
|
---|
151 | }
|
---|
152 | /*
|
---|
153 | Scantable::Scantable(const std::string& name, Table::TableType ttype) :
|
---|
154 | type_(ttype)
|
---|
155 | {
|
---|
156 | initFactories();
|
---|
157 | Table tab(name, Table::Update);
|
---|
158 | uInt version = tab.keywordSet().asuInt("VERSION");
|
---|
159 | if (version != version_) {
|
---|
160 | throw(AipsError("Unsupported version of ASAP file."));
|
---|
161 | }
|
---|
162 | if ( type_ == Table::Memory ) {
|
---|
163 | table_ = tab.copyToMemoryTable(generateName());
|
---|
164 | } else {
|
---|
165 | table_ = tab;
|
---|
166 | }
|
---|
167 |
|
---|
168 | attachSubtables();
|
---|
169 | originalTable_ = table_;
|
---|
170 | attach();
|
---|
171 | }
|
---|
172 | */
|
---|
173 |
|
---|
174 | Scantable::Scantable( const Scantable& other, bool clear )
|
---|
175 | {
|
---|
176 | // with or without data
|
---|
177 | String newname = String(generateName());
|
---|
178 | type_ = other.table_.tableType();
|
---|
179 | if ( other.table_.tableType() == Table::Memory ) {
|
---|
180 | if ( clear ) {
|
---|
181 | table_ = TableCopy::makeEmptyMemoryTable(newname,
|
---|
182 | other.table_, True);
|
---|
183 | } else {
|
---|
184 | table_ = other.table_.copyToMemoryTable(newname);
|
---|
185 | }
|
---|
186 | } else {
|
---|
187 | other.table_.deepCopy(newname, Table::New, False,
|
---|
188 | other.table_.endianFormat(),
|
---|
189 | Bool(clear));
|
---|
190 | table_ = Table(newname, Table::Update);
|
---|
191 | table_.markForDelete();
|
---|
192 | }
|
---|
193 | table_.tableInfo().setType( "Scantable" ) ;
|
---|
194 | /// @todo reindex SCANNO, recompute nbeam, nif, npol
|
---|
195 | if ( clear ) copySubtables(other);
|
---|
196 | attachSubtables();
|
---|
197 | originalTable_ = table_;
|
---|
198 | attach();
|
---|
199 | }
|
---|
200 |
|
---|
201 | void Scantable::copySubtables(const Scantable& other) {
|
---|
202 | Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
|
---|
203 | TableCopy::copyRows(t, other.freqTable_.table());
|
---|
204 | t = table_.rwKeywordSet().asTable("FOCUS");
|
---|
205 | TableCopy::copyRows(t, other.focusTable_.table());
|
---|
206 | t = table_.rwKeywordSet().asTable("WEATHER");
|
---|
207 | TableCopy::copyRows(t, other.weatherTable_.table());
|
---|
208 | t = table_.rwKeywordSet().asTable("TCAL");
|
---|
209 | TableCopy::copyRows(t, other.tcalTable_.table());
|
---|
210 | t = table_.rwKeywordSet().asTable("MOLECULES");
|
---|
211 | TableCopy::copyRows(t, other.moleculeTable_.table());
|
---|
212 | t = table_.rwKeywordSet().asTable("HISTORY");
|
---|
213 | TableCopy::copyRows(t, other.historyTable_.table());
|
---|
214 | t = table_.rwKeywordSet().asTable("FIT");
|
---|
215 | TableCopy::copyRows(t, other.fitTable_.table());
|
---|
216 | }
|
---|
217 |
|
---|
218 | void Scantable::attachSubtables()
|
---|
219 | {
|
---|
220 | freqTable_ = STFrequencies(table_);
|
---|
221 | focusTable_ = STFocus(table_);
|
---|
222 | weatherTable_ = STWeather(table_);
|
---|
223 | tcalTable_ = STTcal(table_);
|
---|
224 | moleculeTable_ = STMolecules(table_);
|
---|
225 | historyTable_ = STHistory(table_);
|
---|
226 | fitTable_ = STFit(table_);
|
---|
227 | }
|
---|
228 |
|
---|
229 | Scantable::~Scantable()
|
---|
230 | {
|
---|
231 | }
|
---|
232 |
|
---|
233 | void Scantable::setupMainTable()
|
---|
234 | {
|
---|
235 | TableDesc td("", "1", TableDesc::Scratch);
|
---|
236 | td.comment() = "An ASAP Scantable";
|
---|
237 | td.rwKeywordSet().define("VERSION", uInt(version_));
|
---|
238 |
|
---|
239 | // n Cycles
|
---|
240 | td.addColumn(ScalarColumnDesc<uInt>("SCANNO"));
|
---|
241 | // new index every nBeam x nIF x nPol
|
---|
242 | td.addColumn(ScalarColumnDesc<uInt>("CYCLENO"));
|
---|
243 |
|
---|
244 | td.addColumn(ScalarColumnDesc<uInt>("BEAMNO"));
|
---|
245 | td.addColumn(ScalarColumnDesc<uInt>("IFNO"));
|
---|
246 | // linear, circular, stokes
|
---|
247 | td.rwKeywordSet().define("POLTYPE", String("linear"));
|
---|
248 | td.addColumn(ScalarColumnDesc<uInt>("POLNO"));
|
---|
249 |
|
---|
250 | td.addColumn(ScalarColumnDesc<uInt>("FREQ_ID"));
|
---|
251 | td.addColumn(ScalarColumnDesc<uInt>("MOLECULE_ID"));
|
---|
252 |
|
---|
253 | ScalarColumnDesc<Int> refbeamnoColumn("REFBEAMNO");
|
---|
254 | refbeamnoColumn.setDefault(Int(-1));
|
---|
255 | td.addColumn(refbeamnoColumn);
|
---|
256 |
|
---|
257 | ScalarColumnDesc<uInt> flagrowColumn("FLAGROW");
|
---|
258 | flagrowColumn.setDefault(uInt(0));
|
---|
259 | td.addColumn(flagrowColumn);
|
---|
260 |
|
---|
261 | td.addColumn(ScalarColumnDesc<Double>("TIME"));
|
---|
262 | TableMeasRefDesc measRef(MEpoch::UTC); // UTC as default
|
---|
263 | TableMeasValueDesc measVal(td, "TIME");
|
---|
264 | TableMeasDesc<MEpoch> mepochCol(measVal, measRef);
|
---|
265 | mepochCol.write(td);
|
---|
266 |
|
---|
267 | td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
|
---|
268 |
|
---|
269 | td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
|
---|
270 | // Type of source (on=0, off=1, other=-1)
|
---|
271 | ScalarColumnDesc<Int> stypeColumn("SRCTYPE");
|
---|
272 | stypeColumn.setDefault(Int(-1));
|
---|
273 | td.addColumn(stypeColumn);
|
---|
274 | td.addColumn(ScalarColumnDesc<String>("FIELDNAME"));
|
---|
275 |
|
---|
276 | //The actual Data Vectors
|
---|
277 | td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
|
---|
278 | td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
|
---|
279 | td.addColumn(ArrayColumnDesc<Float>("TSYS"));
|
---|
280 |
|
---|
281 | td.addColumn(ArrayColumnDesc<Double>("DIRECTION",
|
---|
282 | IPosition(1,2),
|
---|
283 | ColumnDesc::Direct));
|
---|
284 | TableMeasRefDesc mdirRef(MDirection::J2000); // default
|
---|
285 | TableMeasValueDesc tmvdMDir(td, "DIRECTION");
|
---|
286 | // the TableMeasDesc gives the column a type
|
---|
287 | TableMeasDesc<MDirection> mdirCol(tmvdMDir, mdirRef);
|
---|
288 | // a uder set table type e.g. GALCTIC, B1950 ...
|
---|
289 | td.rwKeywordSet().define("DIRECTIONREF", String("J2000"));
|
---|
290 | // writing create the measure column
|
---|
291 | mdirCol.write(td);
|
---|
292 | td.addColumn(ScalarColumnDesc<Float>("AZIMUTH"));
|
---|
293 | td.addColumn(ScalarColumnDesc<Float>("ELEVATION"));
|
---|
294 | td.addColumn(ScalarColumnDesc<Float>("OPACITY"));
|
---|
295 |
|
---|
296 | td.addColumn(ScalarColumnDesc<uInt>("TCAL_ID"));
|
---|
297 | ScalarColumnDesc<Int> fitColumn("FIT_ID");
|
---|
298 | fitColumn.setDefault(Int(-1));
|
---|
299 | td.addColumn(fitColumn);
|
---|
300 |
|
---|
301 | td.addColumn(ScalarColumnDesc<uInt>("FOCUS_ID"));
|
---|
302 | td.addColumn(ScalarColumnDesc<uInt>("WEATHER_ID"));
|
---|
303 |
|
---|
304 | // columns which just get dragged along, as they aren't used in asap
|
---|
305 | td.addColumn(ScalarColumnDesc<Double>("SRCVELOCITY"));
|
---|
306 | td.addColumn(ArrayColumnDesc<Double>("SRCPROPERMOTION"));
|
---|
307 | td.addColumn(ArrayColumnDesc<Double>("SRCDIRECTION"));
|
---|
308 | td.addColumn(ArrayColumnDesc<Double>("SCANRATE"));
|
---|
309 |
|
---|
310 | td.rwKeywordSet().define("OBSMODE", String(""));
|
---|
311 |
|
---|
312 | // Now create Table SetUp from the description.
|
---|
313 | SetupNewTable aNewTab(generateName(), td, Table::Scratch);
|
---|
314 | table_ = Table(aNewTab, type_, 0);
|
---|
315 | originalTable_ = table_;
|
---|
316 | }
|
---|
317 |
|
---|
318 | void Scantable::attach()
|
---|
319 | {
|
---|
320 | timeCol_.attach(table_, "TIME");
|
---|
321 | srcnCol_.attach(table_, "SRCNAME");
|
---|
322 | srctCol_.attach(table_, "SRCTYPE");
|
---|
323 | specCol_.attach(table_, "SPECTRA");
|
---|
324 | flagsCol_.attach(table_, "FLAGTRA");
|
---|
325 | tsysCol_.attach(table_, "TSYS");
|
---|
326 | cycleCol_.attach(table_,"CYCLENO");
|
---|
327 | scanCol_.attach(table_, "SCANNO");
|
---|
328 | beamCol_.attach(table_, "BEAMNO");
|
---|
329 | ifCol_.attach(table_, "IFNO");
|
---|
330 | polCol_.attach(table_, "POLNO");
|
---|
331 | integrCol_.attach(table_, "INTERVAL");
|
---|
332 | azCol_.attach(table_, "AZIMUTH");
|
---|
333 | elCol_.attach(table_, "ELEVATION");
|
---|
334 | dirCol_.attach(table_, "DIRECTION");
|
---|
335 | fldnCol_.attach(table_, "FIELDNAME");
|
---|
336 | rbeamCol_.attach(table_, "REFBEAMNO");
|
---|
337 |
|
---|
338 | mweatheridCol_.attach(table_,"WEATHER_ID");
|
---|
339 | mfitidCol_.attach(table_,"FIT_ID");
|
---|
340 | mfreqidCol_.attach(table_, "FREQ_ID");
|
---|
341 | mtcalidCol_.attach(table_, "TCAL_ID");
|
---|
342 | mfocusidCol_.attach(table_, "FOCUS_ID");
|
---|
343 | mmolidCol_.attach(table_, "MOLECULE_ID");
|
---|
344 |
|
---|
345 | //Add auxiliary column for row-based flagging (CAS-1433 Wataru Kawasaki)
|
---|
346 | attachAuxColumnDef(flagrowCol_, "FLAGROW", 0);
|
---|
347 |
|
---|
348 | }
|
---|
349 |
|
---|
350 | template<class T, class T2>
|
---|
351 | void Scantable::attachAuxColumnDef(ScalarColumn<T>& col,
|
---|
352 | const String& colName,
|
---|
353 | const T2& defValue)
|
---|
354 | {
|
---|
355 | try {
|
---|
356 | col.attach(table_, colName);
|
---|
357 | } catch (TableError& err) {
|
---|
358 | String errMesg = err.getMesg();
|
---|
359 | if (errMesg == "Table column " + colName + " is unknown") {
|
---|
360 | table_.addColumn(ScalarColumnDesc<T>(colName));
|
---|
361 | col.attach(table_, colName);
|
---|
362 | col.fillColumn(static_cast<T>(defValue));
|
---|
363 | } else {
|
---|
364 | throw;
|
---|
365 | }
|
---|
366 | } catch (...) {
|
---|
367 | throw;
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | template<class T, class T2>
|
---|
372 | void Scantable::attachAuxColumnDef(ArrayColumn<T>& col,
|
---|
373 | const String& colName,
|
---|
374 | const Array<T2>& defValue)
|
---|
375 | {
|
---|
376 | try {
|
---|
377 | col.attach(table_, colName);
|
---|
378 | } catch (TableError& err) {
|
---|
379 | String errMesg = err.getMesg();
|
---|
380 | if (errMesg == "Table column " + colName + " is unknown") {
|
---|
381 | table_.addColumn(ArrayColumnDesc<T>(colName));
|
---|
382 | col.attach(table_, colName);
|
---|
383 |
|
---|
384 | int size = 0;
|
---|
385 | ArrayIterator<T2>& it = defValue.begin();
|
---|
386 | while (it != defValue.end()) {
|
---|
387 | ++size;
|
---|
388 | ++it;
|
---|
389 | }
|
---|
390 | IPosition ip(1, size);
|
---|
391 | Array<T>& arr(ip);
|
---|
392 | for (int i = 0; i < size; ++i)
|
---|
393 | arr[i] = static_cast<T>(defValue[i]);
|
---|
394 |
|
---|
395 | col.fillColumn(arr);
|
---|
396 | } else {
|
---|
397 | throw;
|
---|
398 | }
|
---|
399 | } catch (...) {
|
---|
400 | throw;
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | void Scantable::setHeader(const STHeader& sdh)
|
---|
405 | {
|
---|
406 | table_.rwKeywordSet().define("nIF", sdh.nif);
|
---|
407 | table_.rwKeywordSet().define("nBeam", sdh.nbeam);
|
---|
408 | table_.rwKeywordSet().define("nPol", sdh.npol);
|
---|
409 | table_.rwKeywordSet().define("nChan", sdh.nchan);
|
---|
410 | table_.rwKeywordSet().define("Observer", sdh.observer);
|
---|
411 | table_.rwKeywordSet().define("Project", sdh.project);
|
---|
412 | table_.rwKeywordSet().define("Obstype", sdh.obstype);
|
---|
413 | table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
|
---|
414 | table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
|
---|
415 | table_.rwKeywordSet().define("Equinox", sdh.equinox);
|
---|
416 | table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
|
---|
417 | table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
|
---|
418 | table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
|
---|
419 | table_.rwKeywordSet().define("UTC", sdh.utc);
|
---|
420 | table_.rwKeywordSet().define("FluxUnit", sdh.fluxunit);
|
---|
421 | table_.rwKeywordSet().define("Epoch", sdh.epoch);
|
---|
422 | table_.rwKeywordSet().define("POLTYPE", sdh.poltype);
|
---|
423 | }
|
---|
424 |
|
---|
425 | STHeader Scantable::getHeader() const
|
---|
426 | {
|
---|
427 | STHeader sdh;
|
---|
428 | table_.keywordSet().get("nBeam",sdh.nbeam);
|
---|
429 | table_.keywordSet().get("nIF",sdh.nif);
|
---|
430 | table_.keywordSet().get("nPol",sdh.npol);
|
---|
431 | table_.keywordSet().get("nChan",sdh.nchan);
|
---|
432 | table_.keywordSet().get("Observer", sdh.observer);
|
---|
433 | table_.keywordSet().get("Project", sdh.project);
|
---|
434 | table_.keywordSet().get("Obstype", sdh.obstype);
|
---|
435 | table_.keywordSet().get("AntennaName", sdh.antennaname);
|
---|
436 | table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
|
---|
437 | table_.keywordSet().get("Equinox", sdh.equinox);
|
---|
438 | table_.keywordSet().get("FreqRefFrame", sdh.freqref);
|
---|
439 | table_.keywordSet().get("FreqRefVal", sdh.reffreq);
|
---|
440 | table_.keywordSet().get("Bandwidth", sdh.bandwidth);
|
---|
441 | table_.keywordSet().get("UTC", sdh.utc);
|
---|
442 | table_.keywordSet().get("FluxUnit", sdh.fluxunit);
|
---|
443 | table_.keywordSet().get("Epoch", sdh.epoch);
|
---|
444 | table_.keywordSet().get("POLTYPE", sdh.poltype);
|
---|
445 | return sdh;
|
---|
446 | }
|
---|
447 |
|
---|
448 | void Scantable::setSourceType( int stype )
|
---|
449 | {
|
---|
450 | if ( stype < 0 || stype > 1 )
|
---|
451 | throw(AipsError("Illegal sourcetype."));
|
---|
452 | TableVector<Int> tabvec(table_, "SRCTYPE");
|
---|
453 | tabvec = Int(stype);
|
---|
454 | }
|
---|
455 |
|
---|
456 | void Scantable::setSourceName( const std::string& name )
|
---|
457 | {
|
---|
458 | TableVector<String> tabvec(table_, "SRCNAME");
|
---|
459 | tabvec = name;
|
---|
460 | }
|
---|
461 |
|
---|
462 | bool Scantable::conformant( const Scantable& other )
|
---|
463 | {
|
---|
464 | return this->getHeader().conformant(other.getHeader());
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 |
|
---|
469 | std::string Scantable::formatSec(Double x) const
|
---|
470 | {
|
---|
471 | Double xcop = x;
|
---|
472 | MVTime mvt(xcop/24./3600.); // make days
|
---|
473 |
|
---|
474 | if (x < 59.95)
|
---|
475 | return String(" ") + mvt.string(MVTime::TIME_CLEAN_NO_HM, 7)+"s";
|
---|
476 | else if (x < 3599.95)
|
---|
477 | return String(" ") + mvt.string(MVTime::TIME_CLEAN_NO_H,7)+" ";
|
---|
478 | else {
|
---|
479 | ostringstream oss;
|
---|
480 | oss << setw(2) << std::right << setprecision(1) << mvt.hour();
|
---|
481 | oss << ":" << mvt.string(MVTime::TIME_CLEAN_NO_H,7) << " ";
|
---|
482 | return String(oss);
|
---|
483 | }
|
---|
484 | };
|
---|
485 |
|
---|
486 | std::string Scantable::formatDirection(const MDirection& md, Int prec) const
|
---|
487 | {
|
---|
488 | Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
|
---|
489 | if (prec<0)
|
---|
490 | prec = 7;
|
---|
491 |
|
---|
492 | String ref = md.getRefString();
|
---|
493 | MVAngle mvLon(t[0]);
|
---|
494 | String sLon = mvLon.string(MVAngle::TIME,prec);
|
---|
495 | uInt tp = md.getRef().getType();
|
---|
496 | if (tp == MDirection::GALACTIC ||
|
---|
497 | tp == MDirection::SUPERGAL ) {
|
---|
498 | sLon = mvLon(0.0).string(MVAngle::ANGLE_CLEAN,prec);
|
---|
499 | }
|
---|
500 | MVAngle mvLat(t[1]);
|
---|
501 | String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec);
|
---|
502 | return ref + String(" ") + sLon + String(" ") + sLat;
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 | std::string Scantable::getFluxUnit() const
|
---|
507 | {
|
---|
508 | return table_.keywordSet().asString("FluxUnit");
|
---|
509 | }
|
---|
510 |
|
---|
511 | void Scantable::setFluxUnit(const std::string& unit)
|
---|
512 | {
|
---|
513 | String tmp(unit);
|
---|
514 | Unit tU(tmp);
|
---|
515 | if (tU==Unit("K") || tU==Unit("Jy")) {
|
---|
516 | table_.rwKeywordSet().define(String("FluxUnit"), tmp);
|
---|
517 | } else {
|
---|
518 | throw AipsError("Illegal unit - must be compatible with Jy or K");
|
---|
519 | }
|
---|
520 | }
|
---|
521 |
|
---|
522 | void Scantable::setInstrument(const std::string& name)
|
---|
523 | {
|
---|
524 | bool throwIt = true;
|
---|
525 | // create an Instrument to see if this is valid
|
---|
526 | STAttr::convertInstrument(name, throwIt);
|
---|
527 | String nameU(name);
|
---|
528 | nameU.upcase();
|
---|
529 | table_.rwKeywordSet().define(String("AntennaName"), nameU);
|
---|
530 | }
|
---|
531 |
|
---|
532 | void Scantable::setFeedType(const std::string& feedtype)
|
---|
533 | {
|
---|
534 | if ( Scantable::factories_.find(feedtype) == Scantable::factories_.end() ) {
|
---|
535 | std::string msg = "Illegal feed type "+ feedtype;
|
---|
536 | throw(casa::AipsError(msg));
|
---|
537 | }
|
---|
538 | table_.rwKeywordSet().define(String("POLTYPE"), feedtype);
|
---|
539 | }
|
---|
540 |
|
---|
541 | MPosition Scantable::getAntennaPosition() const
|
---|
542 | {
|
---|
543 | Vector<Double> antpos;
|
---|
544 | table_.keywordSet().get("AntennaPosition", antpos);
|
---|
545 | MVPosition mvpos(antpos(0),antpos(1),antpos(2));
|
---|
546 | return MPosition(mvpos);
|
---|
547 | }
|
---|
548 |
|
---|
549 | void Scantable::makePersistent(const std::string& filename)
|
---|
550 | {
|
---|
551 | String inname(filename);
|
---|
552 | Path path(inname);
|
---|
553 | /// @todo reindex SCANNO, recompute nbeam, nif, npol
|
---|
554 | inname = path.expandedName();
|
---|
555 | // 2011/03/04 TN
|
---|
556 | // We can comment out this workaround since the essential bug is
|
---|
557 | // fixed in casacore (r20889 in google code).
|
---|
558 | table_.deepCopy(inname, Table::New);
|
---|
559 | // // WORKAROUND !!! for Table bug
|
---|
560 | // // Remove when fixed in casacore
|
---|
561 | // if ( table_.tableType() == Table::Memory && !selector_.empty() ) {
|
---|
562 | // Table tab = table_.copyToMemoryTable(generateName());
|
---|
563 | // tab.deepCopy(inname, Table::New);
|
---|
564 | // tab.markForDelete();
|
---|
565 | //
|
---|
566 | // } else {
|
---|
567 | // table_.deepCopy(inname, Table::New);
|
---|
568 | // }
|
---|
569 | }
|
---|
570 |
|
---|
571 | int Scantable::nbeam( int scanno ) const
|
---|
572 | {
|
---|
573 | if ( scanno < 0 ) {
|
---|
574 | Int n;
|
---|
575 | table_.keywordSet().get("nBeam",n);
|
---|
576 | return int(n);
|
---|
577 | } else {
|
---|
578 | // take the first POLNO,IFNO,CYCLENO as nbeam shouldn't vary with these
|
---|
579 | Table t = table_(table_.col("SCANNO") == scanno);
|
---|
580 | ROTableRow row(t);
|
---|
581 | const TableRecord& rec = row.get(0);
|
---|
582 | Table subt = t( t.col("IFNO") == Int(rec.asuInt("IFNO"))
|
---|
583 | && t.col("POLNO") == Int(rec.asuInt("POLNO"))
|
---|
584 | && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
|
---|
585 | ROTableVector<uInt> v(subt, "BEAMNO");
|
---|
586 | return int(v.nelements());
|
---|
587 | }
|
---|
588 | return 0;
|
---|
589 | }
|
---|
590 |
|
---|
591 | int Scantable::nif( int scanno ) const
|
---|
592 | {
|
---|
593 | if ( scanno < 0 ) {
|
---|
594 | Int n;
|
---|
595 | table_.keywordSet().get("nIF",n);
|
---|
596 | return int(n);
|
---|
597 | } else {
|
---|
598 | // take the first POLNO,BEAMNO,CYCLENO as nbeam shouldn't vary with these
|
---|
599 | Table t = table_(table_.col("SCANNO") == scanno);
|
---|
600 | ROTableRow row(t);
|
---|
601 | const TableRecord& rec = row.get(0);
|
---|
602 | Table subt = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
|
---|
603 | && t.col("POLNO") == Int(rec.asuInt("POLNO"))
|
---|
604 | && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
|
---|
605 | if ( subt.nrow() == 0 ) return 0;
|
---|
606 | ROTableVector<uInt> v(subt, "IFNO");
|
---|
607 | return int(v.nelements());
|
---|
608 | }
|
---|
609 | return 0;
|
---|
610 | }
|
---|
611 |
|
---|
612 | int Scantable::npol( int scanno ) const
|
---|
613 | {
|
---|
614 | if ( scanno < 0 ) {
|
---|
615 | Int n;
|
---|
616 | table_.keywordSet().get("nPol",n);
|
---|
617 | return n;
|
---|
618 | } else {
|
---|
619 | // take the first POLNO,IFNO,CYCLENO as nbeam shouldn't vary with these
|
---|
620 | Table t = table_(table_.col("SCANNO") == scanno);
|
---|
621 | ROTableRow row(t);
|
---|
622 | const TableRecord& rec = row.get(0);
|
---|
623 | Table subt = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
|
---|
624 | && t.col("IFNO") == Int(rec.asuInt("IFNO"))
|
---|
625 | && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
|
---|
626 | if ( subt.nrow() == 0 ) return 0;
|
---|
627 | ROTableVector<uInt> v(subt, "POLNO");
|
---|
628 | return int(v.nelements());
|
---|
629 | }
|
---|
630 | return 0;
|
---|
631 | }
|
---|
632 |
|
---|
633 | int Scantable::ncycle( int scanno ) const
|
---|
634 | {
|
---|
635 | if ( scanno < 0 ) {
|
---|
636 | Block<String> cols(2);
|
---|
637 | cols[0] = "SCANNO";
|
---|
638 | cols[1] = "CYCLENO";
|
---|
639 | TableIterator it(table_, cols);
|
---|
640 | int n = 0;
|
---|
641 | while ( !it.pastEnd() ) {
|
---|
642 | ++n;
|
---|
643 | ++it;
|
---|
644 | }
|
---|
645 | return n;
|
---|
646 | } else {
|
---|
647 | Table t = table_(table_.col("SCANNO") == scanno);
|
---|
648 | ROTableRow row(t);
|
---|
649 | const TableRecord& rec = row.get(0);
|
---|
650 | Table subt = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
|
---|
651 | && t.col("POLNO") == Int(rec.asuInt("POLNO"))
|
---|
652 | && t.col("IFNO") == Int(rec.asuInt("IFNO")) );
|
---|
653 | if ( subt.nrow() == 0 ) return 0;
|
---|
654 | return int(subt.nrow());
|
---|
655 | }
|
---|
656 | return 0;
|
---|
657 | }
|
---|
658 |
|
---|
659 |
|
---|
660 | int Scantable::nrow( int scanno ) const
|
---|
661 | {
|
---|
662 | return int(table_.nrow());
|
---|
663 | }
|
---|
664 |
|
---|
665 | int Scantable::nchan( int ifno ) const
|
---|
666 | {
|
---|
667 | if ( ifno < 0 ) {
|
---|
668 | Int n;
|
---|
669 | table_.keywordSet().get("nChan",n);
|
---|
670 | return int(n);
|
---|
671 | } else {
|
---|
672 | // take the first SCANNO,POLNO,BEAMNO,CYCLENO as nbeam shouldn't
|
---|
673 | // vary with these
|
---|
674 | Table t = table_(table_.col("IFNO") == ifno, 1);
|
---|
675 | if ( t.nrow() == 0 ) return 0;
|
---|
676 | ROArrayColumn<Float> v(t, "SPECTRA");
|
---|
677 | return v.shape(0)(0);
|
---|
678 | }
|
---|
679 | return 0;
|
---|
680 | }
|
---|
681 |
|
---|
682 | int Scantable::nscan() const {
|
---|
683 | Vector<uInt> scannos(scanCol_.getColumn());
|
---|
684 | uInt nout = genSort( scannos, Sort::Ascending,
|
---|
685 | Sort::QuickSort|Sort::NoDuplicates );
|
---|
686 | return int(nout);
|
---|
687 | }
|
---|
688 |
|
---|
689 | int Scantable::getChannels(int whichrow) const
|
---|
690 | {
|
---|
691 | return specCol_.shape(whichrow)(0);
|
---|
692 | }
|
---|
693 |
|
---|
694 | int Scantable::getBeam(int whichrow) const
|
---|
695 | {
|
---|
696 | return beamCol_(whichrow);
|
---|
697 | }
|
---|
698 |
|
---|
699 | std::vector<uint> Scantable::getNumbers(const ScalarColumn<uInt>& col) const
|
---|
700 | {
|
---|
701 | Vector<uInt> nos(col.getColumn());
|
---|
702 | uInt n = genSort( nos, Sort::Ascending, Sort::QuickSort|Sort::NoDuplicates );
|
---|
703 | nos.resize(n, True);
|
---|
704 | std::vector<uint> stlout;
|
---|
705 | nos.tovector(stlout);
|
---|
706 | return stlout;
|
---|
707 | }
|
---|
708 |
|
---|
709 | int Scantable::getIF(int whichrow) const
|
---|
710 | {
|
---|
711 | return ifCol_(whichrow);
|
---|
712 | }
|
---|
713 |
|
---|
714 | int Scantable::getPol(int whichrow) const
|
---|
715 | {
|
---|
716 | return polCol_(whichrow);
|
---|
717 | }
|
---|
718 |
|
---|
719 | std::string Scantable::formatTime(const MEpoch& me, bool showdate) const
|
---|
720 | {
|
---|
721 | return formatTime(me, showdate, 0);
|
---|
722 | }
|
---|
723 |
|
---|
724 | std::string Scantable::formatTime(const MEpoch& me, bool showdate, uInt prec) const
|
---|
725 | {
|
---|
726 | MVTime mvt(me.getValue());
|
---|
727 | if (showdate)
|
---|
728 | //mvt.setFormat(MVTime::YMD);
|
---|
729 | mvt.setFormat(MVTime::YMD, prec);
|
---|
730 | else
|
---|
731 | //mvt.setFormat(MVTime::TIME);
|
---|
732 | mvt.setFormat(MVTime::TIME, prec);
|
---|
733 | ostringstream oss;
|
---|
734 | oss << mvt;
|
---|
735 | return String(oss);
|
---|
736 | }
|
---|
737 |
|
---|
738 | void Scantable::calculateAZEL()
|
---|
739 | {
|
---|
740 | LogIO os( LogOrigin( "Scantable", "calculateAZEL()", WHERE ) ) ;
|
---|
741 | MPosition mp = getAntennaPosition();
|
---|
742 | MEpoch::ROScalarColumn timeCol(table_, "TIME");
|
---|
743 | ostringstream oss;
|
---|
744 | oss << mp;
|
---|
745 | os << "Computed azimuth/elevation using " << endl
|
---|
746 | << String(oss) << endl;
|
---|
747 | for (Int i=0; i<nrow(); ++i) {
|
---|
748 | MEpoch me = timeCol(i);
|
---|
749 | MDirection md = getDirection(i);
|
---|
750 | os << " Time: " << formatTime(me,False)
|
---|
751 | << " Direction: " << formatDirection(md)
|
---|
752 | << endl << " => ";
|
---|
753 | MeasFrame frame(mp, me);
|
---|
754 | Vector<Double> azel =
|
---|
755 | MDirection::Convert(md, MDirection::Ref(MDirection::AZEL,
|
---|
756 | frame)
|
---|
757 | )().getAngle("rad").getValue();
|
---|
758 | azCol_.put(i,Float(azel[0]));
|
---|
759 | elCol_.put(i,Float(azel[1]));
|
---|
760 | os << "azel: " << azel[0]/C::pi*180.0 << " "
|
---|
761 | << azel[1]/C::pi*180.0 << " (deg)" << LogIO::POST;
|
---|
762 | }
|
---|
763 | }
|
---|
764 |
|
---|
765 | void Scantable::clip(const Float uthres, const Float dthres, bool clipoutside, bool unflag)
|
---|
766 | {
|
---|
767 | Vector<uInt> flagrow = flagrowCol_.getColumn();
|
---|
768 | for (uInt i=0; i<table_.nrow(); ++i) {
|
---|
769 | // apply flag only when specified row is vaild
|
---|
770 | if (flagrow[i] == 0) {
|
---|
771 | Vector<uChar> flgs = flagsCol_(i);
|
---|
772 | srchChannelsToClip(i, uthres, dthres, clipoutside, unflag, flgs);
|
---|
773 | flagsCol_.put(i, flgs);
|
---|
774 | }
|
---|
775 | }
|
---|
776 | }
|
---|
777 |
|
---|
778 | std::vector<bool> Scantable::getClipMask(int whichrow, const Float uthres, const Float dthres, bool clipoutside, bool unflag)
|
---|
779 | {
|
---|
780 | Vector<uChar> flags;
|
---|
781 | flagsCol_.get(uInt(whichrow), flags);
|
---|
782 | srchChannelsToClip(uInt(whichrow), uthres, dthres, clipoutside, unflag, flags);
|
---|
783 | Vector<Bool> bflag(flags.shape());
|
---|
784 | convertArray(bflag, flags);
|
---|
785 | //bflag = !bflag;
|
---|
786 |
|
---|
787 | std::vector<bool> mask;
|
---|
788 | bflag.tovector(mask);
|
---|
789 | return mask;
|
---|
790 | }
|
---|
791 |
|
---|
792 | void Scantable::srchChannelsToClip(uInt whichrow, const Float uthres, const Float dthres, bool clipoutside, bool unflag,
|
---|
793 | Vector<uChar> flgs)
|
---|
794 | {
|
---|
795 | Vector<Float> spcs = specCol_(whichrow);
|
---|
796 | uInt nchannel = spcs.nelements();
|
---|
797 | if (spcs.nelements() != nchannel) {
|
---|
798 | throw(AipsError("Data has incorrect number of channels"));
|
---|
799 | }
|
---|
800 | uChar userflag = 1 << 7;
|
---|
801 | if (unflag) {
|
---|
802 | userflag = 0 << 7;
|
---|
803 | }
|
---|
804 | if (clipoutside) {
|
---|
805 | for (uInt j = 0; j < nchannel; ++j) {
|
---|
806 | Float spc = spcs(j);
|
---|
807 | if ((spc >= uthres) || (spc <= dthres)) {
|
---|
808 | flgs(j) = userflag;
|
---|
809 | }
|
---|
810 | }
|
---|
811 | } else {
|
---|
812 | for (uInt j = 0; j < nchannel; ++j) {
|
---|
813 | Float spc = spcs(j);
|
---|
814 | if ((spc < uthres) && (spc > dthres)) {
|
---|
815 | flgs(j) = userflag;
|
---|
816 | }
|
---|
817 | }
|
---|
818 | }
|
---|
819 | }
|
---|
820 |
|
---|
821 |
|
---|
822 | void Scantable::flag( int whichrow, const std::vector<bool>& msk, bool unflag ) {
|
---|
823 | std::vector<bool>::const_iterator it;
|
---|
824 | uInt ntrue = 0;
|
---|
825 | if (whichrow >= int(table_.nrow()) ) {
|
---|
826 | throw(AipsError("Invalid row number"));
|
---|
827 | }
|
---|
828 | for (it = msk.begin(); it != msk.end(); ++it) {
|
---|
829 | if ( *it ) {
|
---|
830 | ntrue++;
|
---|
831 | }
|
---|
832 | }
|
---|
833 | //if ( selector_.empty() && (msk.size() == 0 || msk.size() == ntrue) )
|
---|
834 | if ( whichrow == -1 && !unflag && selector_.empty() && (msk.size() == 0 || msk.size() == ntrue) )
|
---|
835 | throw(AipsError("Trying to flag whole scantable."));
|
---|
836 | uChar userflag = 1 << 7;
|
---|
837 | if ( unflag ) {
|
---|
838 | userflag = 0 << 7;
|
---|
839 | }
|
---|
840 | if (whichrow > -1 ) {
|
---|
841 | // apply flag only when specified row is vaild
|
---|
842 | if (flagrowCol_(whichrow) == 0) {
|
---|
843 | applyChanFlag(uInt(whichrow), msk, userflag);
|
---|
844 | }
|
---|
845 | } else {
|
---|
846 | Vector<uInt> flagrow = flagrowCol_.getColumn();
|
---|
847 | for ( uInt i=0; i<table_.nrow(); ++i) {
|
---|
848 | // apply flag only when specified row is vaild
|
---|
849 | if (flagrow[i] == 0) {
|
---|
850 | applyChanFlag(i, msk, userflag);
|
---|
851 | }
|
---|
852 | }
|
---|
853 | }
|
---|
854 | }
|
---|
855 |
|
---|
856 | void Scantable::applyChanFlag( uInt whichrow, const std::vector<bool>& msk, uChar flagval )
|
---|
857 | {
|
---|
858 | if (whichrow >= table_.nrow() ) {
|
---|
859 | throw( casa::indexError<int>( whichrow, "asap::Scantable::applyChanFlag: Invalid row number" ) );
|
---|
860 | }
|
---|
861 | Vector<uChar> flgs = flagsCol_(whichrow);
|
---|
862 | if ( msk.size() == 0 ) {
|
---|
863 | flgs = flagval;
|
---|
864 | flagsCol_.put(whichrow, flgs);
|
---|
865 | return;
|
---|
866 | }
|
---|
867 | if ( int(msk.size()) != nchan( getIF(whichrow) ) ) {
|
---|
868 | throw(AipsError("Mask has incorrect number of channels."));
|
---|
869 | }
|
---|
870 | if ( flgs.nelements() != msk.size() ) {
|
---|
871 | throw(AipsError("Mask has incorrect number of channels."
|
---|
872 | " Probably varying with IF. Please flag per IF"));
|
---|
873 | }
|
---|
874 | std::vector<bool>::const_iterator it;
|
---|
875 | uInt j = 0;
|
---|
876 | for (it = msk.begin(); it != msk.end(); ++it) {
|
---|
877 | if ( *it ) {
|
---|
878 | flgs(j) = flagval;
|
---|
879 | }
|
---|
880 | ++j;
|
---|
881 | }
|
---|
882 | flagsCol_.put(whichrow, flgs);
|
---|
883 | }
|
---|
884 |
|
---|
885 | void Scantable::flagRow(const std::vector<uInt>& rows, bool unflag)
|
---|
886 | {
|
---|
887 | if (selector_.empty() && (rows.size() == table_.nrow()) && !unflag)
|
---|
888 | throw(AipsError("Trying to flag whole scantable."));
|
---|
889 |
|
---|
890 | uInt rowflag = (unflag ? 0 : 1);
|
---|
891 | std::vector<uInt>::const_iterator it;
|
---|
892 | for (it = rows.begin(); it != rows.end(); ++it)
|
---|
893 | flagrowCol_.put(*it, rowflag);
|
---|
894 | }
|
---|
895 |
|
---|
896 | std::vector<bool> Scantable::getMask(int whichrow) const
|
---|
897 | {
|
---|
898 | Vector<uChar> flags;
|
---|
899 | flagsCol_.get(uInt(whichrow), flags);
|
---|
900 | Vector<Bool> bflag(flags.shape());
|
---|
901 | convertArray(bflag, flags);
|
---|
902 | bflag = !bflag;
|
---|
903 | std::vector<bool> mask;
|
---|
904 | bflag.tovector(mask);
|
---|
905 | return mask;
|
---|
906 | }
|
---|
907 |
|
---|
908 | std::vector<float> Scantable::getSpectrum( int whichrow,
|
---|
909 | const std::string& poltype ) const
|
---|
910 | {
|
---|
911 | LogIO os( LogOrigin( "Scantable", "getSpectrum()", WHERE ) ) ;
|
---|
912 |
|
---|
913 | String ptype = poltype;
|
---|
914 | if (poltype == "" ) ptype = getPolType();
|
---|
915 | if ( whichrow < 0 || whichrow >= nrow() )
|
---|
916 | throw(AipsError("Illegal row number."));
|
---|
917 | std::vector<float> out;
|
---|
918 | Vector<Float> arr;
|
---|
919 | uInt requestedpol = polCol_(whichrow);
|
---|
920 | String basetype = getPolType();
|
---|
921 | if ( ptype == basetype ) {
|
---|
922 | specCol_.get(whichrow, arr);
|
---|
923 | } else {
|
---|
924 | CountedPtr<STPol> stpol(STPol::getPolClass(Scantable::factories_,
|
---|
925 | basetype));
|
---|
926 | uInt row = uInt(whichrow);
|
---|
927 | stpol->setSpectra(getPolMatrix(row));
|
---|
928 | Float fang,fhand;
|
---|
929 | fang = focusTable_.getTotalAngle(mfocusidCol_(row));
|
---|
930 | fhand = focusTable_.getFeedHand(mfocusidCol_(row));
|
---|
931 | stpol->setPhaseCorrections(fang, fhand);
|
---|
932 | arr = stpol->getSpectrum(requestedpol, ptype);
|
---|
933 | }
|
---|
934 | if ( arr.nelements() == 0 )
|
---|
935 |
|
---|
936 | os << "Not enough polarisations present to do the conversion."
|
---|
937 | << LogIO::POST;
|
---|
938 | arr.tovector(out);
|
---|
939 | return out;
|
---|
940 | }
|
---|
941 |
|
---|
942 | void Scantable::setSpectrum( const std::vector<float>& spec,
|
---|
943 | int whichrow )
|
---|
944 | {
|
---|
945 | Vector<Float> spectrum(spec);
|
---|
946 | Vector<Float> arr;
|
---|
947 | specCol_.get(whichrow, arr);
|
---|
948 | if ( spectrum.nelements() != arr.nelements() )
|
---|
949 | throw AipsError("The spectrum has incorrect number of channels.");
|
---|
950 | specCol_.put(whichrow, spectrum);
|
---|
951 | }
|
---|
952 |
|
---|
953 |
|
---|
954 | String Scantable::generateName()
|
---|
955 | {
|
---|
956 | return (File::newUniqueName("./","temp")).baseName();
|
---|
957 | }
|
---|
958 |
|
---|
959 | const casa::Table& Scantable::table( ) const
|
---|
960 | {
|
---|
961 | return table_;
|
---|
962 | }
|
---|
963 |
|
---|
964 | casa::Table& Scantable::table( )
|
---|
965 | {
|
---|
966 | return table_;
|
---|
967 | }
|
---|
968 |
|
---|
969 | std::string Scantable::getPolType() const
|
---|
970 | {
|
---|
971 | return table_.keywordSet().asString("POLTYPE");
|
---|
972 | }
|
---|
973 |
|
---|
974 | void Scantable::unsetSelection()
|
---|
975 | {
|
---|
976 | table_ = originalTable_;
|
---|
977 | attach();
|
---|
978 | selector_.reset();
|
---|
979 | }
|
---|
980 |
|
---|
981 | void Scantable::setSelection( const STSelector& selection )
|
---|
982 | {
|
---|
983 | Table tab = const_cast<STSelector&>(selection).apply(originalTable_);
|
---|
984 | if ( tab.nrow() == 0 ) {
|
---|
985 | throw(AipsError("Selection contains no data. Not applying it."));
|
---|
986 | }
|
---|
987 | table_ = tab;
|
---|
988 | attach();
|
---|
989 | // tab.rwKeywordSet().define("nBeam",(Int)(getBeamNos().size())) ;
|
---|
990 | // vector<uint> selectedIFs = getIFNos() ;
|
---|
991 | // Int newnIF = selectedIFs.size() ;
|
---|
992 | // tab.rwKeywordSet().define("nIF",newnIF) ;
|
---|
993 | // if ( newnIF != 0 ) {
|
---|
994 | // Int newnChan = 0 ;
|
---|
995 | // for ( Int i = 0 ; i < newnIF ; i++ ) {
|
---|
996 | // Int nChan = nchan( selectedIFs[i] ) ;
|
---|
997 | // if ( newnChan > nChan )
|
---|
998 | // newnChan = nChan ;
|
---|
999 | // }
|
---|
1000 | // tab.rwKeywordSet().define("nChan",newnChan) ;
|
---|
1001 | // }
|
---|
1002 | // tab.rwKeywordSet().define("nPol",(Int)(getPolNos().size())) ;
|
---|
1003 | selector_ = selection;
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 |
|
---|
1007 | std::string Scantable::headerSummary()
|
---|
1008 | {
|
---|
1009 | // Format header info
|
---|
1010 | // STHeader sdh;
|
---|
1011 | // sdh = getHeader();
|
---|
1012 | // sdh.print();
|
---|
1013 | ostringstream oss;
|
---|
1014 | oss.flags(std::ios_base::left);
|
---|
1015 | String tmp;
|
---|
1016 | // Project
|
---|
1017 | table_.keywordSet().get("Project", tmp);
|
---|
1018 | oss << setw(15) << "Project:" << tmp << endl;
|
---|
1019 | // Observation date
|
---|
1020 | oss << setw(15) << "Obs Date:" << getTime(-1,true) << endl;
|
---|
1021 | // Observer
|
---|
1022 | oss << setw(15) << "Observer:"
|
---|
1023 | << table_.keywordSet().asString("Observer") << endl;
|
---|
1024 | // Antenna Name
|
---|
1025 | table_.keywordSet().get("AntennaName", tmp);
|
---|
1026 | oss << setw(15) << "Antenna Name:" << tmp << endl;
|
---|
1027 | // Obs type
|
---|
1028 | table_.keywordSet().get("Obstype", tmp);
|
---|
1029 | // Records (nrow)
|
---|
1030 | oss << setw(15) << "Data Records:" << table_.nrow() << " rows" << endl;
|
---|
1031 | oss << setw(15) << "Obs. Type:" << tmp << endl;
|
---|
1032 | // Beams, IFs, Polarizations, and Channels
|
---|
1033 | oss << setw(15) << "Beams:" << setw(4) << nbeam() << endl
|
---|
1034 | << setw(15) << "IFs:" << setw(4) << nif() << endl
|
---|
1035 | << setw(15) << "Polarisations:" << setw(4) << npol()
|
---|
1036 | << "(" << getPolType() << ")" << endl
|
---|
1037 | << setw(15) << "Channels:" << nchan() << endl;
|
---|
1038 | // Flux unit
|
---|
1039 | table_.keywordSet().get("FluxUnit", tmp);
|
---|
1040 | oss << setw(15) << "Flux Unit:" << tmp << endl;
|
---|
1041 | // Abscissa Unit
|
---|
1042 | oss << setw(15) << "Abscissa:" << getAbcissaLabel(0) << endl;
|
---|
1043 | // Selection
|
---|
1044 | oss << selector_.print() << endl;
|
---|
1045 |
|
---|
1046 | return String(oss);
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | void Scantable::summary( const std::string& filename )
|
---|
1050 | {
|
---|
1051 | ostringstream oss;
|
---|
1052 | ofstream ofs;
|
---|
1053 | LogIO ols(LogOrigin("Scantable", "summary", WHERE));
|
---|
1054 |
|
---|
1055 | if (filename != "")
|
---|
1056 | ofs.open( filename.c_str(), ios::out );
|
---|
1057 |
|
---|
1058 | oss << endl;
|
---|
1059 | oss << asap::SEPERATOR << endl;
|
---|
1060 | oss << " Scan Table Summary" << endl;
|
---|
1061 | oss << asap::SEPERATOR << endl;
|
---|
1062 |
|
---|
1063 | // Format header info
|
---|
1064 | oss << headerSummary();
|
---|
1065 | oss << endl;
|
---|
1066 |
|
---|
1067 | if (table_.nrow() <= 0){
|
---|
1068 | oss << asap::SEPERATOR << endl;
|
---|
1069 | oss << "The MAIN table is empty: there are no data!!!" << endl;
|
---|
1070 | oss << asap::SEPERATOR << endl;
|
---|
1071 |
|
---|
1072 | ols << String(oss) << LogIO::POST;
|
---|
1073 | if (ofs) {
|
---|
1074 | ofs << String(oss) << flush;
|
---|
1075 | ofs.close();
|
---|
1076 | }
|
---|
1077 | return;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 |
|
---|
1081 |
|
---|
1082 | // main table
|
---|
1083 | String dirtype = "Position ("
|
---|
1084 | + getDirectionRefString()
|
---|
1085 | + ")";
|
---|
1086 | oss.flags(std::ios_base::left);
|
---|
1087 | oss << setw(5) << "Scan"
|
---|
1088 | << setw(15) << "Source"
|
---|
1089 | << setw(35) << "Time range"
|
---|
1090 | << setw(2) << "" << setw(7) << "Int[s]"
|
---|
1091 | << setw(7) << "Record"
|
---|
1092 | << setw(8) << "SrcType"
|
---|
1093 | << setw(8) << "FreqIDs"
|
---|
1094 | << setw(7) << "MolIDs" << endl;
|
---|
1095 | oss << setw(7)<< "" << setw(6) << "Beam"
|
---|
1096 | << setw(23) << dirtype << endl;
|
---|
1097 |
|
---|
1098 | oss << asap::SEPERATOR << endl;
|
---|
1099 |
|
---|
1100 | // Flush summary and clear up the string
|
---|
1101 | ols << String(oss) << LogIO::POST;
|
---|
1102 | if (ofs) ofs << String(oss) << flush;
|
---|
1103 | oss.str("");
|
---|
1104 | oss.clear();
|
---|
1105 |
|
---|
1106 |
|
---|
1107 | // Get Freq_ID map
|
---|
1108 | ROScalarColumn<uInt> ftabIds(frequencies().table(), "ID");
|
---|
1109 | Int nfid = ftabIds.nrow();
|
---|
1110 | if (nfid <= 0){
|
---|
1111 | oss << "FREQUENCIES subtable is empty: there are no data!!!" << endl;
|
---|
1112 | oss << asap::SEPERATOR << endl;
|
---|
1113 |
|
---|
1114 | ols << String(oss) << LogIO::POST;
|
---|
1115 | if (ofs) {
|
---|
1116 | ofs << String(oss) << flush;
|
---|
1117 | ofs.close();
|
---|
1118 | }
|
---|
1119 | return;
|
---|
1120 | }
|
---|
1121 | // Storages of overall IFNO, POLNO, and nchan per FREQ_ID
|
---|
1122 | // the orders are identical to ID in FREQ subtable
|
---|
1123 | Block< Vector<uInt> > ifNos(nfid), polNos(nfid);
|
---|
1124 | Vector<Int> fIdchans(nfid,-1);
|
---|
1125 | Vector<Double> fIdfreq0(nfid,-1);
|
---|
1126 | Vector<Double> fIdfcent(nfid,-1);
|
---|
1127 | map<uInt, Int> fidMap; // (FREQ_ID, row # in FREQ subtable) pair
|
---|
1128 | for (Int i=0; i < nfid; i++){
|
---|
1129 | // fidMap[freqId] returns row number in FREQ subtable
|
---|
1130 | fidMap.insert(pair<uInt, Int>(ftabIds(i),i));
|
---|
1131 | ifNos[i] = Vector<uInt>();
|
---|
1132 | polNos[i] = Vector<uInt>();
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | TableIterator iter(table_, "SCANNO");
|
---|
1136 |
|
---|
1137 | // Vars for keeping track of time, freqids, molIds in a SCANNO
|
---|
1138 | //Vector<uInt> freqids;
|
---|
1139 | //Vector<uInt> molids;
|
---|
1140 | Vector<uInt> beamids(1,0);
|
---|
1141 | Vector<MDirection> beamDirs;
|
---|
1142 | Vector<Int> stypeids(1,0);
|
---|
1143 | Vector<String> stypestrs;
|
---|
1144 | Int nfreq(1);
|
---|
1145 | Int nmol(1);
|
---|
1146 | uInt nbeam(1);
|
---|
1147 | uInt nstype(1);
|
---|
1148 |
|
---|
1149 | Double btime(0.0), etime(0.0);
|
---|
1150 | Double meanIntTim(0.0);
|
---|
1151 |
|
---|
1152 | uInt currFreqId(0), ftabRow(0);
|
---|
1153 | Int iflen(0), pollen(0);
|
---|
1154 |
|
---|
1155 | while (!iter.pastEnd()) {
|
---|
1156 | Table subt = iter.table();
|
---|
1157 | uInt snrow = subt.nrow();
|
---|
1158 | ROTableRow row(subt);
|
---|
1159 | const TableRecord& rec = row.get(0);
|
---|
1160 |
|
---|
1161 | // relevant columns
|
---|
1162 | ROScalarColumn<Double> mjdCol(subt,"TIME");
|
---|
1163 | ROScalarColumn<Double> intervalCol(subt,"INTERVAL");
|
---|
1164 | MDirection::ROScalarColumn dirCol(subt,"DIRECTION");
|
---|
1165 |
|
---|
1166 | ScalarColumn<uInt> freqIdCol(subt,"FREQ_ID");
|
---|
1167 | ScalarColumn<uInt> molIdCol(subt,"MOLECULE_ID");
|
---|
1168 | ROScalarColumn<uInt> beamCol(subt,"BEAMNO");
|
---|
1169 | ROScalarColumn<Int> stypeCol(subt,"SRCTYPE");
|
---|
1170 |
|
---|
1171 | ROScalarColumn<uInt> ifNoCol(subt,"IFNO");
|
---|
1172 | ROScalarColumn<uInt> polNoCol(subt,"POLNO");
|
---|
1173 |
|
---|
1174 |
|
---|
1175 | // Times
|
---|
1176 | meanIntTim = sum(intervalCol.getColumn()) / (double) snrow;
|
---|
1177 | minMax(btime, etime, mjdCol.getColumn());
|
---|
1178 | double shiftInDay(0.5*meanIntTim/C::day);
|
---|
1179 | btime -= shiftInDay;
|
---|
1180 | etime += shiftInDay;
|
---|
1181 |
|
---|
1182 | // MOLECULE_ID and FREQ_ID
|
---|
1183 | Vector<uInt> molids(getNumbers(molIdCol));
|
---|
1184 | molids.shape(nmol);
|
---|
1185 |
|
---|
1186 | Vector<uInt> freqids(getNumbers(freqIdCol));
|
---|
1187 | freqids.shape(nfreq);
|
---|
1188 |
|
---|
1189 | // Add first beamid, and srcNames
|
---|
1190 | beamids.resize(1,False);
|
---|
1191 | beamDirs.resize(1,False);
|
---|
1192 | beamids(0)=beamCol(0);
|
---|
1193 | beamDirs(0)=dirCol(0);
|
---|
1194 | nbeam = 1;
|
---|
1195 |
|
---|
1196 | stypeids.resize(1,False);
|
---|
1197 | stypeids(0)=stypeCol(0);
|
---|
1198 | nstype = 1;
|
---|
1199 |
|
---|
1200 | // Global listings of nchan/IFNO/POLNO per FREQ_ID
|
---|
1201 | currFreqId=freqIdCol(0);
|
---|
1202 | ftabRow = fidMap[currFreqId];
|
---|
1203 | // Assumes an identical number of channels per FREQ_ID
|
---|
1204 | if (fIdchans(ftabRow) < 0 ) {
|
---|
1205 | RORecordFieldPtr< Array<Float> > spec(rec, "SPECTRA");
|
---|
1206 | fIdchans(ftabRow)=(*spec).shape()(0);
|
---|
1207 | }
|
---|
1208 | if (fIdfreq0(ftabRow) < 0 ) {
|
---|
1209 | SpectralCoordinate spc = frequencies().getSpectralCoordinate(ftabRow);
|
---|
1210 | Double fs, fe;
|
---|
1211 | spc.toWorld(fs, 0);
|
---|
1212 | spc.toWorld(fe, fIdchans(ftabRow)-1);
|
---|
1213 | fIdfreq0(ftabRow) = fs;
|
---|
1214 | fIdfcent(ftabRow) = 0.5 * ( fs + fe );
|
---|
1215 | }
|
---|
1216 | // Should keep ifNos and polNos form the previous SCANNO
|
---|
1217 | if ( !anyEQ(ifNos[ftabRow],ifNoCol(0)) ) {
|
---|
1218 | ifNos[ftabRow].shape(iflen);
|
---|
1219 | iflen++;
|
---|
1220 | ifNos[ftabRow].resize(iflen,True);
|
---|
1221 | ifNos[ftabRow](iflen-1) = ifNoCol(0);
|
---|
1222 | }
|
---|
1223 | if ( !anyEQ(polNos[ftabRow],polNoCol(0)) ) {
|
---|
1224 | polNos[ftabRow].shape(pollen);
|
---|
1225 | pollen++;
|
---|
1226 | polNos[ftabRow].resize(pollen,True);
|
---|
1227 | polNos[ftabRow](pollen-1) = polNoCol(0);
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | for (uInt i=1; i < snrow; i++){
|
---|
1231 | // Need to list BEAMNO and DIRECTION in the same order
|
---|
1232 | if ( !anyEQ(beamids,beamCol(i)) ) {
|
---|
1233 | nbeam++;
|
---|
1234 | beamids.resize(nbeam,True);
|
---|
1235 | beamids(nbeam-1)=beamCol(i);
|
---|
1236 | beamDirs.resize(nbeam,True);
|
---|
1237 | beamDirs(nbeam-1)=dirCol(i);
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | // SRCTYPE is Int (getNumber takes only uInt)
|
---|
1241 | if ( !anyEQ(stypeids,stypeCol(i)) ) {
|
---|
1242 | nstype++;
|
---|
1243 | stypeids.resize(nstype,True);
|
---|
1244 | stypeids(nstype-1)=stypeCol(i);
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | // Global listings of nchan/IFNO/POLNO per FREQ_ID
|
---|
1248 | currFreqId=freqIdCol(i);
|
---|
1249 | ftabRow = fidMap[currFreqId];
|
---|
1250 | if (fIdchans(ftabRow) < 0 ) {
|
---|
1251 | const TableRecord& rec = row.get(i);
|
---|
1252 | RORecordFieldPtr< Array<Float> > spec(rec, "SPECTRA");
|
---|
1253 | fIdchans(ftabRow) = (*spec).shape()(0);
|
---|
1254 | }
|
---|
1255 | if (fIdfreq0(ftabRow) < 0 ) {
|
---|
1256 | SpectralCoordinate spc = frequencies().getSpectralCoordinate(ftabRow);
|
---|
1257 | Double fs, fe;
|
---|
1258 | spc.toWorld(fs, 0);
|
---|
1259 | spc.toWorld(fe, fIdchans(ftabRow)-1);
|
---|
1260 | fIdfreq0(ftabRow) = fs;
|
---|
1261 | fIdfcent(ftabRow) = 5.e-1 * ( fs + fe );
|
---|
1262 | }
|
---|
1263 | if ( !anyEQ(ifNos[ftabRow],ifNoCol(i)) ) {
|
---|
1264 | ifNos[ftabRow].shape(iflen);
|
---|
1265 | iflen++;
|
---|
1266 | ifNos[ftabRow].resize(iflen,True);
|
---|
1267 | ifNos[ftabRow](iflen-1) = ifNoCol(i);
|
---|
1268 | }
|
---|
1269 | if ( !anyEQ(polNos[ftabRow],polNoCol(i)) ) {
|
---|
1270 | polNos[ftabRow].shape(pollen);
|
---|
1271 | pollen++;
|
---|
1272 | polNos[ftabRow].resize(pollen,True);
|
---|
1273 | polNos[ftabRow](pollen-1) = polNoCol(i);
|
---|
1274 | }
|
---|
1275 | } // end of row iteration
|
---|
1276 |
|
---|
1277 | stypestrs.resize(nstype,False);
|
---|
1278 | for (uInt j=0; j < nstype; j++)
|
---|
1279 | stypestrs(j) = SrcType::getName(stypeids(j));
|
---|
1280 |
|
---|
1281 | // Format Scan summary
|
---|
1282 | oss << setw(4) << std::right << rec.asuInt("SCANNO")
|
---|
1283 | << std::left << setw(1) << ""
|
---|
1284 | << setw(15) << rec.asString("SRCNAME")
|
---|
1285 | << setw(21) << MVTime(btime).string(MVTime::YMD,8)
|
---|
1286 | << setw(3) << " - " << MVTime(etime).string(MVTime::TIME,8)
|
---|
1287 | << setw(3) << "" << setw(6) << meanIntTim << setw(1) << ""
|
---|
1288 | << std::right << setw(5) << snrow << setw(2) << ""
|
---|
1289 | << std::left << stypestrs << setw(1) << ""
|
---|
1290 | << freqids << setw(1) << ""
|
---|
1291 | << molids << endl;
|
---|
1292 | // Format Beam summary
|
---|
1293 | for (uInt j=0; j < nbeam; j++) {
|
---|
1294 | oss << setw(7) << "" << setw(6) << beamids(j) << setw(1) << ""
|
---|
1295 | << formatDirection(beamDirs(j),9) << endl;
|
---|
1296 | }
|
---|
1297 | // Flush summary every scan and clear up the string
|
---|
1298 | ols << String(oss) << LogIO::POST;
|
---|
1299 | if (ofs) ofs << String(oss) << flush;
|
---|
1300 | oss.str("");
|
---|
1301 | oss.clear();
|
---|
1302 |
|
---|
1303 | ++iter;
|
---|
1304 | } // end of scan iteration
|
---|
1305 | oss << asap::SEPERATOR << endl;
|
---|
1306 |
|
---|
1307 | // List FRECUENCIES Table (using STFrequencies.print may be slow)
|
---|
1308 | oss << "FREQUENCIES: " << nfreq << endl;
|
---|
1309 | // oss << std::right << setw(5) << "ID" << setw(2) << ""
|
---|
1310 | // << std::left << setw(5) << "IFNO" << setw(2) << ""
|
---|
1311 | // << setw(8) << "Frame"
|
---|
1312 | // << setw(16) << "RefVal"
|
---|
1313 | // << setw(7) << "RefPix"
|
---|
1314 | // << setw(15) << "Increment"
|
---|
1315 | // << setw(9) << "Channels"
|
---|
1316 | // << setw(6) << "POLNOs" << endl;
|
---|
1317 | // Int tmplen;
|
---|
1318 | // for (Int i=0; i < nfid; i++){
|
---|
1319 | // // List row=i of FREQUENCIES subtable
|
---|
1320 | // ifNos[i].shape(tmplen);
|
---|
1321 | // if (tmplen >= 1) {
|
---|
1322 | // oss << std::right << setw(5) << ftabIds(i) << setw(2) << ""
|
---|
1323 | // << setw(3) << ifNos[i](0) << setw(1) << ""
|
---|
1324 | // << std::left << setw(46) << frequencies().print(ftabIds(i))
|
---|
1325 | // << setw(2) << ""
|
---|
1326 | // << std::right << setw(8) << fIdchans[i] << setw(2) << ""
|
---|
1327 | // << std::left << polNos[i];
|
---|
1328 | // if (tmplen > 1) {
|
---|
1329 | // oss << " (" << tmplen << " chains)";
|
---|
1330 | // }
|
---|
1331 | // oss << endl;
|
---|
1332 | // }
|
---|
1333 | oss << std::right << setw(4) << "ID" << setw(2) << ""
|
---|
1334 | << std::left << setw(9) << "IFNO(SPW)" << setw(2) << ""
|
---|
1335 | << setw(8) << "#Chans"
|
---|
1336 | << setw(8) << "Frame"
|
---|
1337 | << setw(12) << "Ch0[MHz]"
|
---|
1338 | << setw(14) << "ChanWid[kHz]"
|
---|
1339 | << setw(14) << "Center[MHz]"
|
---|
1340 | << setw(6) << "POLNOs" << endl;
|
---|
1341 | Int tmplen;
|
---|
1342 | for (Int i=0; i < nfid; i++){
|
---|
1343 | // List row=i of FREQUENCIES subtable
|
---|
1344 | ifNos[i].shape(tmplen);
|
---|
1345 | Double refpix, refval, increment ;
|
---|
1346 | if (tmplen >= 1) {
|
---|
1347 | freqTable_.getEntry( refpix, refval, increment, ftabIds(i) ) ;
|
---|
1348 | oss << std::right << setw(4) << ftabIds(i) << setw(2) << ""
|
---|
1349 | << std::left << setw(9) << ifNos[i](0) << setw(2) << ""
|
---|
1350 | << std::right << setw(6) << fIdchans[i] << setw(2) << ""
|
---|
1351 | << setw(6) << frequencies().getFrameString(true)
|
---|
1352 | << setw(2) << ""
|
---|
1353 | << setw(10) << std::setprecision(9) << (fIdfreq0[i]*1.e-6) << setw(2) << ""
|
---|
1354 | << setw(12) << (increment*1.e-3) << setw(2) << ""
|
---|
1355 | << setw(12) << (fIdfcent[i]*1.e-6) << setw(2) << ""
|
---|
1356 | << std::left << polNos[i];
|
---|
1357 | if (tmplen > 1) {
|
---|
1358 | oss << " (" << tmplen << " chains)";
|
---|
1359 | }
|
---|
1360 | oss << endl;
|
---|
1361 | }
|
---|
1362 |
|
---|
1363 | }
|
---|
1364 | oss << asap::SEPERATOR << endl;
|
---|
1365 |
|
---|
1366 | // List MOLECULES Table (currently lists all rows)
|
---|
1367 | oss << "MOLECULES: " << endl;
|
---|
1368 | if (molecules().nrow() <= 0) {
|
---|
1369 | oss << " MOLECULES subtable is empty: there are no data" << endl;
|
---|
1370 | } else {
|
---|
1371 | ROTableRow row(molecules().table());
|
---|
1372 | oss << std::right << setw(5) << "ID"
|
---|
1373 | << std::left << setw(3) << ""
|
---|
1374 | << setw(18) << "RestFreq"
|
---|
1375 | << setw(15) << "Name" << endl;
|
---|
1376 | for (Int i=0; i < molecules().nrow(); i++){
|
---|
1377 | const TableRecord& rec=row.get(i);
|
---|
1378 | oss << std::right << setw(5) << rec.asuInt("ID")
|
---|
1379 | << std::left << setw(3) << ""
|
---|
1380 | << rec.asArrayDouble("RESTFREQUENCY") << setw(1) << ""
|
---|
1381 | << rec.asArrayString("NAME") << endl;
|
---|
1382 | }
|
---|
1383 | }
|
---|
1384 | oss << asap::SEPERATOR << endl;
|
---|
1385 | ols << String(oss) << LogIO::POST;
|
---|
1386 | if (ofs) {
|
---|
1387 | ofs << String(oss) << flush;
|
---|
1388 | ofs.close();
|
---|
1389 | }
|
---|
1390 | // return String(oss);
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 |
|
---|
1394 | std::string Scantable::oldheaderSummary()
|
---|
1395 | {
|
---|
1396 | // Format header info
|
---|
1397 | // STHeader sdh;
|
---|
1398 | // sdh = getHeader();
|
---|
1399 | // sdh.print();
|
---|
1400 | ostringstream oss;
|
---|
1401 | oss.flags(std::ios_base::left);
|
---|
1402 | oss << setw(15) << "Beams:" << setw(4) << nbeam() << endl
|
---|
1403 | << setw(15) << "IFs:" << setw(4) << nif() << endl
|
---|
1404 | << setw(15) << "Polarisations:" << setw(4) << npol()
|
---|
1405 | << "(" << getPolType() << ")" << endl
|
---|
1406 | << setw(15) << "Channels:" << nchan() << endl;
|
---|
1407 | String tmp;
|
---|
1408 | oss << setw(15) << "Observer:"
|
---|
1409 | << table_.keywordSet().asString("Observer") << endl;
|
---|
1410 | oss << setw(15) << "Obs Date:" << getTime(-1,true) << endl;
|
---|
1411 | table_.keywordSet().get("Project", tmp);
|
---|
1412 | oss << setw(15) << "Project:" << tmp << endl;
|
---|
1413 | table_.keywordSet().get("Obstype", tmp);
|
---|
1414 | oss << setw(15) << "Obs. Type:" << tmp << endl;
|
---|
1415 | table_.keywordSet().get("AntennaName", tmp);
|
---|
1416 | oss << setw(15) << "Antenna Name:" << tmp << endl;
|
---|
1417 | table_.keywordSet().get("FluxUnit", tmp);
|
---|
1418 | oss << setw(15) << "Flux Unit:" << tmp << endl;
|
---|
1419 | int nid = moleculeTable_.nrow();
|
---|
1420 | Bool firstline = True;
|
---|
1421 | oss << setw(15) << "Rest Freqs:";
|
---|
1422 | for (int i=0; i<nid; i++) {
|
---|
1423 | Table t = table_(table_.col("MOLECULE_ID") == i, 1);
|
---|
1424 | if (t.nrow() > 0) {
|
---|
1425 | Vector<Double> vec(moleculeTable_.getRestFrequency(i));
|
---|
1426 | if (vec.nelements() > 0) {
|
---|
1427 | if (firstline) {
|
---|
1428 | oss << setprecision(10) << vec << " [Hz]" << endl;
|
---|
1429 | firstline=False;
|
---|
1430 | }
|
---|
1431 | else{
|
---|
1432 | oss << setw(15)<<" " << setprecision(10) << vec << " [Hz]" << endl;
|
---|
1433 | }
|
---|
1434 | } else {
|
---|
1435 | oss << "none" << endl;
|
---|
1436 | }
|
---|
1437 | }
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | oss << setw(15) << "Abcissa:" << getAbcissaLabel(0) << endl;
|
---|
1441 | oss << selector_.print() << endl;
|
---|
1442 | return String(oss);
|
---|
1443 | }
|
---|
1444 |
|
---|
1445 | //std::string Scantable::summary( const std::string& filename )
|
---|
1446 | void Scantable::oldsummary( const std::string& filename )
|
---|
1447 | {
|
---|
1448 | ostringstream oss;
|
---|
1449 | ofstream ofs;
|
---|
1450 | LogIO ols(LogOrigin("Scantable", "summary", WHERE));
|
---|
1451 |
|
---|
1452 | if (filename != "")
|
---|
1453 | ofs.open( filename.c_str(), ios::out );
|
---|
1454 |
|
---|
1455 | oss << endl;
|
---|
1456 | oss << asap::SEPERATOR << endl;
|
---|
1457 | oss << " Scan Table Summary" << endl;
|
---|
1458 | oss << asap::SEPERATOR << endl;
|
---|
1459 |
|
---|
1460 | // Format header info
|
---|
1461 | oss << oldheaderSummary();
|
---|
1462 | oss << endl;
|
---|
1463 |
|
---|
1464 | // main table
|
---|
1465 | String dirtype = "Position ("
|
---|
1466 | + getDirectionRefString()
|
---|
1467 | + ")";
|
---|
1468 | oss.flags(std::ios_base::left);
|
---|
1469 | oss << setw(5) << "Scan" << setw(15) << "Source"
|
---|
1470 | << setw(10) << "Time" << setw(18) << "Integration"
|
---|
1471 | << setw(15) << "Source Type" << endl;
|
---|
1472 | oss << setw(5) << "" << setw(5) << "Beam" << setw(3) << "" << dirtype << endl;
|
---|
1473 | oss << setw(10) << "" << setw(3) << "IF" << setw(3) << ""
|
---|
1474 | << setw(8) << "Frame" << setw(16)
|
---|
1475 | << "RefVal" << setw(10) << "RefPix" << setw(12) << "Increment"
|
---|
1476 | << setw(7) << "Channels"
|
---|
1477 | << endl;
|
---|
1478 | oss << asap::SEPERATOR << endl;
|
---|
1479 |
|
---|
1480 | // Flush summary and clear up the string
|
---|
1481 | ols << String(oss) << LogIO::POST;
|
---|
1482 | if (ofs) ofs << String(oss) << flush;
|
---|
1483 | oss.str("");
|
---|
1484 | oss.clear();
|
---|
1485 |
|
---|
1486 | TableIterator iter(table_, "SCANNO");
|
---|
1487 | while (!iter.pastEnd()) {
|
---|
1488 | Table subt = iter.table();
|
---|
1489 | ROTableRow row(subt);
|
---|
1490 | MEpoch::ROScalarColumn timeCol(subt,"TIME");
|
---|
1491 | const TableRecord& rec = row.get(0);
|
---|
1492 | oss << setw(4) << std::right << rec.asuInt("SCANNO")
|
---|
1493 | << std::left << setw(1) << ""
|
---|
1494 | << setw(15) << rec.asString("SRCNAME")
|
---|
1495 | << setw(10) << formatTime(timeCol(0), false);
|
---|
1496 | // count the cycles in the scan
|
---|
1497 | TableIterator cyciter(subt, "CYCLENO");
|
---|
1498 | int nint = 0;
|
---|
1499 | while (!cyciter.pastEnd()) {
|
---|
1500 | ++nint;
|
---|
1501 | ++cyciter;
|
---|
1502 | }
|
---|
1503 | oss << setw(3) << std::right << nint << setw(3) << " x " << std::left
|
---|
1504 | << setw(11) << formatSec(rec.asFloat("INTERVAL")) << setw(1) << ""
|
---|
1505 | << setw(15) << SrcType::getName(rec.asInt("SRCTYPE")) << endl;
|
---|
1506 |
|
---|
1507 | TableIterator biter(subt, "BEAMNO");
|
---|
1508 | while (!biter.pastEnd()) {
|
---|
1509 | Table bsubt = biter.table();
|
---|
1510 | ROTableRow brow(bsubt);
|
---|
1511 | const TableRecord& brec = brow.get(0);
|
---|
1512 | uInt row0 = bsubt.rowNumbers(table_)[0];
|
---|
1513 | oss << setw(5) << "" << setw(4) << std::right << brec.asuInt("BEAMNO")<< std::left;
|
---|
1514 | oss << setw(4) << "" << formatDirection(getDirection(row0)) << endl;
|
---|
1515 | TableIterator iiter(bsubt, "IFNO");
|
---|
1516 | while (!iiter.pastEnd()) {
|
---|
1517 | Table isubt = iiter.table();
|
---|
1518 | ROTableRow irow(isubt);
|
---|
1519 | const TableRecord& irec = irow.get(0);
|
---|
1520 | oss << setw(9) << "";
|
---|
1521 | oss << setw(3) << std::right << irec.asuInt("IFNO") << std::left
|
---|
1522 | << setw(1) << "" << frequencies().print(irec.asuInt("FREQ_ID"))
|
---|
1523 | << setw(3) << "" << nchan(irec.asuInt("IFNO"))
|
---|
1524 | << endl;
|
---|
1525 |
|
---|
1526 | ++iiter;
|
---|
1527 | }
|
---|
1528 | ++biter;
|
---|
1529 | }
|
---|
1530 | // Flush summary every scan and clear up the string
|
---|
1531 | ols << String(oss) << LogIO::POST;
|
---|
1532 | if (ofs) ofs << String(oss) << flush;
|
---|
1533 | oss.str("");
|
---|
1534 | oss.clear();
|
---|
1535 |
|
---|
1536 | ++iter;
|
---|
1537 | }
|
---|
1538 | oss << asap::SEPERATOR << endl;
|
---|
1539 | ols << String(oss) << LogIO::POST;
|
---|
1540 | if (ofs) {
|
---|
1541 | ofs << String(oss) << flush;
|
---|
1542 | ofs.close();
|
---|
1543 | }
|
---|
1544 | // return String(oss);
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | // std::string Scantable::getTime(int whichrow, bool showdate) const
|
---|
1548 | // {
|
---|
1549 | // MEpoch::ROScalarColumn timeCol(table_, "TIME");
|
---|
1550 | // MEpoch me;
|
---|
1551 | // if (whichrow > -1) {
|
---|
1552 | // me = timeCol(uInt(whichrow));
|
---|
1553 | // } else {
|
---|
1554 | // Double tm;
|
---|
1555 | // table_.keywordSet().get("UTC",tm);
|
---|
1556 | // me = MEpoch(MVEpoch(tm));
|
---|
1557 | // }
|
---|
1558 | // return formatTime(me, showdate);
|
---|
1559 | // }
|
---|
1560 |
|
---|
1561 | std::string Scantable::getTime(int whichrow, bool showdate, uInt prec) const
|
---|
1562 | {
|
---|
1563 | MEpoch me;
|
---|
1564 | me = getEpoch(whichrow);
|
---|
1565 | return formatTime(me, showdate, prec);
|
---|
1566 | }
|
---|
1567 |
|
---|
1568 | MEpoch Scantable::getEpoch(int whichrow) const
|
---|
1569 | {
|
---|
1570 | if (whichrow > -1) {
|
---|
1571 | return timeCol_(uInt(whichrow));
|
---|
1572 | } else {
|
---|
1573 | Double tm;
|
---|
1574 | table_.keywordSet().get("UTC",tm);
|
---|
1575 | return MEpoch(MVEpoch(tm));
|
---|
1576 | }
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 | std::string Scantable::getDirectionString(int whichrow) const
|
---|
1580 | {
|
---|
1581 | return formatDirection(getDirection(uInt(whichrow)));
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 |
|
---|
1585 | SpectralCoordinate Scantable::getSpectralCoordinate(int whichrow) const {
|
---|
1586 | const MPosition& mp = getAntennaPosition();
|
---|
1587 | const MDirection& md = getDirection(whichrow);
|
---|
1588 | const MEpoch& me = timeCol_(whichrow);
|
---|
1589 | //Double rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
|
---|
1590 | Vector<Double> rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
|
---|
1591 | return freqTable_.getSpectralCoordinate(md, mp, me, rf,
|
---|
1592 | mfreqidCol_(whichrow));
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 | std::vector< double > Scantable::getAbcissa( int whichrow ) const
|
---|
1596 | {
|
---|
1597 | if ( whichrow > int(table_.nrow()) ) throw(AipsError("Illegal row number"));
|
---|
1598 | std::vector<double> stlout;
|
---|
1599 | int nchan = specCol_(whichrow).nelements();
|
---|
1600 | String us = freqTable_.getUnitString();
|
---|
1601 | if ( us == "" || us == "pixel" || us == "channel" ) {
|
---|
1602 | for (int i=0; i<nchan; ++i) {
|
---|
1603 | stlout.push_back(double(i));
|
---|
1604 | }
|
---|
1605 | return stlout;
|
---|
1606 | }
|
---|
1607 | SpectralCoordinate spc = getSpectralCoordinate(whichrow);
|
---|
1608 | Vector<Double> pixel(nchan);
|
---|
1609 | Vector<Double> world;
|
---|
1610 | indgen(pixel);
|
---|
1611 | if ( Unit(us) == Unit("Hz") ) {
|
---|
1612 | for ( int i=0; i < nchan; ++i) {
|
---|
1613 | Double world;
|
---|
1614 | spc.toWorld(world, pixel[i]);
|
---|
1615 | stlout.push_back(double(world));
|
---|
1616 | }
|
---|
1617 | } else if ( Unit(us) == Unit("km/s") ) {
|
---|
1618 | Vector<Double> world;
|
---|
1619 | spc.pixelToVelocity(world, pixel);
|
---|
1620 | world.tovector(stlout);
|
---|
1621 | }
|
---|
1622 | return stlout;
|
---|
1623 | }
|
---|
1624 | void Scantable::setDirectionRefString( const std::string & refstr )
|
---|
1625 | {
|
---|
1626 | MDirection::Types mdt;
|
---|
1627 | if (refstr != "" && !MDirection::getType(mdt, refstr)) {
|
---|
1628 | throw(AipsError("Illegal Direction frame."));
|
---|
1629 | }
|
---|
1630 | if ( refstr == "" ) {
|
---|
1631 | String defaultstr = MDirection::showType(dirCol_.getMeasRef().getType());
|
---|
1632 | table_.rwKeywordSet().define("DIRECTIONREF", defaultstr);
|
---|
1633 | } else {
|
---|
1634 | table_.rwKeywordSet().define("DIRECTIONREF", String(refstr));
|
---|
1635 | }
|
---|
1636 | }
|
---|
1637 |
|
---|
1638 | std::string Scantable::getDirectionRefString( ) const
|
---|
1639 | {
|
---|
1640 | return table_.keywordSet().asString("DIRECTIONREF");
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | MDirection Scantable::getDirection(int whichrow ) const
|
---|
1644 | {
|
---|
1645 | String usertype = table_.keywordSet().asString("DIRECTIONREF");
|
---|
1646 | String type = MDirection::showType(dirCol_.getMeasRef().getType());
|
---|
1647 | if ( usertype != type ) {
|
---|
1648 | MDirection::Types mdt;
|
---|
1649 | if (!MDirection::getType(mdt, usertype)) {
|
---|
1650 | throw(AipsError("Illegal Direction frame."));
|
---|
1651 | }
|
---|
1652 | return dirCol_.convert(uInt(whichrow), mdt);
|
---|
1653 | } else {
|
---|
1654 | return dirCol_(uInt(whichrow));
|
---|
1655 | }
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | std::string Scantable::getAbcissaLabel( int whichrow ) const
|
---|
1659 | {
|
---|
1660 | if ( whichrow > int(table_.nrow()) ) throw(AipsError("Illegal ro number"));
|
---|
1661 | const MPosition& mp = getAntennaPosition();
|
---|
1662 | const MDirection& md = getDirection(whichrow);
|
---|
1663 | const MEpoch& me = timeCol_(whichrow);
|
---|
1664 | //const Double& rf = mmolidCol_(whichrow);
|
---|
1665 | const Vector<Double> rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
|
---|
1666 | SpectralCoordinate spc =
|
---|
1667 | freqTable_.getSpectralCoordinate(md, mp, me, rf, mfreqidCol_(whichrow));
|
---|
1668 |
|
---|
1669 | String s = "Channel";
|
---|
1670 | Unit u = Unit(freqTable_.getUnitString());
|
---|
1671 | if (u == Unit("km/s")) {
|
---|
1672 | s = CoordinateUtil::axisLabel(spc, 0, True,True, True);
|
---|
1673 | } else if (u == Unit("Hz")) {
|
---|
1674 | Vector<String> wau(1);wau = u.getName();
|
---|
1675 | spc.setWorldAxisUnits(wau);
|
---|
1676 | s = CoordinateUtil::axisLabel(spc, 0, True, True, False);
|
---|
1677 | }
|
---|
1678 | return s;
|
---|
1679 |
|
---|
1680 | }
|
---|
1681 |
|
---|
1682 | /**
|
---|
1683 | void asap::Scantable::setRestFrequencies( double rf, const std::string& name,
|
---|
1684 | const std::string& unit )
|
---|
1685 | **/
|
---|
1686 | void Scantable::setRestFrequencies( vector<double> rf, const vector<std::string>& name,
|
---|
1687 | const std::string& unit )
|
---|
1688 |
|
---|
1689 | {
|
---|
1690 | ///@todo lookup in line table to fill in name and formattedname
|
---|
1691 | Unit u(unit);
|
---|
1692 | //Quantum<Double> urf(rf, u);
|
---|
1693 | Quantum<Vector<Double> >urf(rf, u);
|
---|
1694 | Vector<String> formattedname(0);
|
---|
1695 | //cerr<<"Scantable::setRestFrequnecies="<<urf<<endl;
|
---|
1696 |
|
---|
1697 | //uInt id = moleculeTable_.addEntry(urf.getValue("Hz"), name, "");
|
---|
1698 | uInt id = moleculeTable_.addEntry(urf.getValue("Hz"), mathutil::toVectorString(name), formattedname);
|
---|
1699 | TableVector<uInt> tabvec(table_, "MOLECULE_ID");
|
---|
1700 | tabvec = id;
|
---|
1701 | }
|
---|
1702 |
|
---|
1703 | /**
|
---|
1704 | void asap::Scantable::setRestFrequencies( const std::string& name )
|
---|
1705 | {
|
---|
1706 | throw(AipsError("setRestFrequencies( const std::string& name ) NYI"));
|
---|
1707 | ///@todo implement
|
---|
1708 | }
|
---|
1709 | **/
|
---|
1710 |
|
---|
1711 | void Scantable::setRestFrequencies( const vector<std::string>& name )
|
---|
1712 | {
|
---|
1713 | (void) name; // suppress unused warning
|
---|
1714 | throw(AipsError("setRestFrequencies( const vector<std::string>& name ) NYI"));
|
---|
1715 | ///@todo implement
|
---|
1716 | }
|
---|
1717 |
|
---|
1718 | std::vector< unsigned int > Scantable::rownumbers( ) const
|
---|
1719 | {
|
---|
1720 | std::vector<unsigned int> stlout;
|
---|
1721 | Vector<uInt> vec = table_.rowNumbers();
|
---|
1722 | vec.tovector(stlout);
|
---|
1723 | return stlout;
|
---|
1724 | }
|
---|
1725 |
|
---|
1726 |
|
---|
1727 | Matrix<Float> Scantable::getPolMatrix( uInt whichrow ) const
|
---|
1728 | {
|
---|
1729 | ROTableRow row(table_);
|
---|
1730 | const TableRecord& rec = row.get(whichrow);
|
---|
1731 | Table t =
|
---|
1732 | originalTable_( originalTable_.col("SCANNO") == Int(rec.asuInt("SCANNO"))
|
---|
1733 | && originalTable_.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
|
---|
1734 | && originalTable_.col("IFNO") == Int(rec.asuInt("IFNO"))
|
---|
1735 | && originalTable_.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
|
---|
1736 | ROArrayColumn<Float> speccol(t, "SPECTRA");
|
---|
1737 | return speccol.getColumn();
|
---|
1738 | }
|
---|
1739 |
|
---|
1740 | std::vector< std::string > Scantable::columnNames( ) const
|
---|
1741 | {
|
---|
1742 | Vector<String> vec = table_.tableDesc().columnNames();
|
---|
1743 | return mathutil::tovectorstring(vec);
|
---|
1744 | }
|
---|
1745 |
|
---|
1746 | MEpoch::Types Scantable::getTimeReference( ) const
|
---|
1747 | {
|
---|
1748 | return MEpoch::castType(timeCol_.getMeasRef().getType());
|
---|
1749 | }
|
---|
1750 |
|
---|
1751 | void Scantable::addFit( const STFitEntry& fit, int row )
|
---|
1752 | {
|
---|
1753 | //cout << mfitidCol_(uInt(row)) << endl;
|
---|
1754 | LogIO os( LogOrigin( "Scantable", "addFit()", WHERE ) ) ;
|
---|
1755 | os << mfitidCol_(uInt(row)) << LogIO::POST ;
|
---|
1756 | uInt id = fitTable_.addEntry(fit, mfitidCol_(uInt(row)));
|
---|
1757 | mfitidCol_.put(uInt(row), id);
|
---|
1758 | }
|
---|
1759 |
|
---|
1760 | void Scantable::shift(int npix)
|
---|
1761 | {
|
---|
1762 | Vector<uInt> fids(mfreqidCol_.getColumn());
|
---|
1763 | genSort( fids, Sort::Ascending,
|
---|
1764 | Sort::QuickSort|Sort::NoDuplicates );
|
---|
1765 | for (uInt i=0; i<fids.nelements(); ++i) {
|
---|
1766 | frequencies().shiftRefPix(npix, fids[i]);
|
---|
1767 | }
|
---|
1768 | }
|
---|
1769 |
|
---|
1770 | String Scantable::getAntennaName() const
|
---|
1771 | {
|
---|
1772 | String out;
|
---|
1773 | table_.keywordSet().get("AntennaName", out);
|
---|
1774 | String::size_type pos1 = out.find("@") ;
|
---|
1775 | String::size_type pos2 = out.find("//") ;
|
---|
1776 | if ( pos2 != String::npos )
|
---|
1777 | out = out.substr(pos2+2,pos1-pos2-2) ;
|
---|
1778 | else if ( pos1 != String::npos )
|
---|
1779 | out = out.substr(0,pos1) ;
|
---|
1780 | return out;
|
---|
1781 | }
|
---|
1782 |
|
---|
1783 | int Scantable::checkScanInfo(const std::vector<int>& scanlist) const
|
---|
1784 | {
|
---|
1785 | String tbpath;
|
---|
1786 | int ret = 0;
|
---|
1787 | if ( table_.keywordSet().isDefined("GBT_GO") ) {
|
---|
1788 | table_.keywordSet().get("GBT_GO", tbpath);
|
---|
1789 | Table t(tbpath,Table::Old);
|
---|
1790 | // check each scan if other scan of the pair exist
|
---|
1791 | int nscan = scanlist.size();
|
---|
1792 | for (int i = 0; i < nscan; i++) {
|
---|
1793 | Table subt = t( t.col("SCAN") == scanlist[i] );
|
---|
1794 | if (subt.nrow()==0) {
|
---|
1795 | //cerr <<"Scan "<<scanlist[i]<<" cannot be found in the scantable."<<endl;
|
---|
1796 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1797 | os <<LogIO::WARN<<"Scan "<<scanlist[i]<<" cannot be found in the scantable."<<LogIO::POST;
|
---|
1798 | ret = 1;
|
---|
1799 | break;
|
---|
1800 | }
|
---|
1801 | ROTableRow row(subt);
|
---|
1802 | const TableRecord& rec = row.get(0);
|
---|
1803 | int scan1seqn = rec.asuInt("PROCSEQN");
|
---|
1804 | int laston1 = rec.asuInt("LASTON");
|
---|
1805 | if ( rec.asuInt("PROCSIZE")==2 ) {
|
---|
1806 | if ( i < nscan-1 ) {
|
---|
1807 | Table subt2 = t( t.col("SCAN") == scanlist[i+1] );
|
---|
1808 | if ( subt2.nrow() == 0) {
|
---|
1809 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1810 |
|
---|
1811 | //cerr<<"Scan "<<scanlist[i+1]<<" cannot be found in the scantable."<<endl;
|
---|
1812 | os<<LogIO::WARN<<"Scan "<<scanlist[i+1]<<" cannot be found in the scantable."<<LogIO::POST;
|
---|
1813 | ret = 1;
|
---|
1814 | break;
|
---|
1815 | }
|
---|
1816 | ROTableRow row2(subt2);
|
---|
1817 | const TableRecord& rec2 = row2.get(0);
|
---|
1818 | int scan2seqn = rec2.asuInt("PROCSEQN");
|
---|
1819 | int laston2 = rec2.asuInt("LASTON");
|
---|
1820 | if (scan1seqn == 1 && scan2seqn == 2) {
|
---|
1821 | if (laston1 == laston2) {
|
---|
1822 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1823 | //cerr<<"A valid scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<endl;
|
---|
1824 | os<<"A valid scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<LogIO::POST;
|
---|
1825 | i +=1;
|
---|
1826 | }
|
---|
1827 | else {
|
---|
1828 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1829 | //cerr<<"Incorrect scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<endl;
|
---|
1830 | os<<LogIO::WARN<<"Incorrect scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<LogIO::POST;
|
---|
1831 | }
|
---|
1832 | }
|
---|
1833 | else if (scan1seqn==2 && scan2seqn == 1) {
|
---|
1834 | if (laston1 == laston2) {
|
---|
1835 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1836 | //cerr<<"["<<scanlist[i]<<","<<scanlist[i+1]<<"] is a valid scan pair but in incorrect order."<<endl;
|
---|
1837 | os<<LogIO::WARN<<"["<<scanlist[i]<<","<<scanlist[i+1]<<"] is a valid scan pair but in incorrect order."<<LogIO::POST;
|
---|
1838 | ret = 1;
|
---|
1839 | break;
|
---|
1840 | }
|
---|
1841 | }
|
---|
1842 | else {
|
---|
1843 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1844 | //cerr<<"The other scan for "<<scanlist[i]<<" appears to be missing. Check the input scan numbers."<<endl;
|
---|
1845 | os<<LogIO::WARN<<"The other scan for "<<scanlist[i]<<" appears to be missing. Check the input scan numbers."<<LogIO::POST;
|
---|
1846 | ret = 1;
|
---|
1847 | break;
|
---|
1848 | }
|
---|
1849 | }
|
---|
1850 | }
|
---|
1851 | else {
|
---|
1852 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1853 | //cerr<<"The scan does not appear to be standard obsevation."<<endl;
|
---|
1854 | os<<LogIO::WARN<<"The scan does not appear to be standard obsevation."<<LogIO::POST;
|
---|
1855 | }
|
---|
1856 | //if ( i >= nscan ) break;
|
---|
1857 | }
|
---|
1858 | }
|
---|
1859 | else {
|
---|
1860 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1861 | //cerr<<"No reference to GBT_GO table."<<endl;
|
---|
1862 | os<<LogIO::WARN<<"No reference to GBT_GO table."<<LogIO::POST;
|
---|
1863 | ret = 1;
|
---|
1864 | }
|
---|
1865 | return ret;
|
---|
1866 | }
|
---|
1867 |
|
---|
1868 | std::vector<double> Scantable::getDirectionVector(int whichrow) const
|
---|
1869 | {
|
---|
1870 | Vector<Double> Dir = dirCol_(whichrow).getAngle("rad").getValue();
|
---|
1871 | std::vector<double> dir;
|
---|
1872 | Dir.tovector(dir);
|
---|
1873 | return dir;
|
---|
1874 | }
|
---|
1875 |
|
---|
1876 | void asap::Scantable::reshapeSpectrum( int nmin, int nmax )
|
---|
1877 | throw( casa::AipsError )
|
---|
1878 | {
|
---|
1879 | // assumed that all rows have same nChan
|
---|
1880 | Vector<Float> arr = specCol_( 0 ) ;
|
---|
1881 | int nChan = arr.nelements() ;
|
---|
1882 |
|
---|
1883 | // if nmin < 0 or nmax < 0, nothing to do
|
---|
1884 | if ( nmin < 0 ) {
|
---|
1885 | throw( casa::indexError<int>( nmin, "asap::Scantable::reshapeSpectrum: Invalid range. Negative index is specified." ) ) ;
|
---|
1886 | }
|
---|
1887 | if ( nmax < 0 ) {
|
---|
1888 | throw( casa::indexError<int>( nmax, "asap::Scantable::reshapeSpectrum: Invalid range. Negative index is specified." ) ) ;
|
---|
1889 | }
|
---|
1890 |
|
---|
1891 | // if nmin > nmax, exchange values
|
---|
1892 | if ( nmin > nmax ) {
|
---|
1893 | int tmp = nmax ;
|
---|
1894 | nmax = nmin ;
|
---|
1895 | nmin = tmp ;
|
---|
1896 | LogIO os( LogOrigin( "Scantable", "reshapeSpectrum()", WHERE ) ) ;
|
---|
1897 | os << "Swap values. Applied range is ["
|
---|
1898 | << nmin << ", " << nmax << "]" << LogIO::POST ;
|
---|
1899 | }
|
---|
1900 |
|
---|
1901 | // if nmin exceeds nChan, nothing to do
|
---|
1902 | if ( nmin >= nChan ) {
|
---|
1903 | throw( casa::indexError<int>( nmin, "asap::Scantable::reshapeSpectrum: Invalid range. Specified minimum exceeds nChan." ) ) ;
|
---|
1904 | }
|
---|
1905 |
|
---|
1906 | // if nmax exceeds nChan, reset nmax to nChan
|
---|
1907 | if ( nmax >= nChan-1 ) {
|
---|
1908 | if ( nmin == 0 ) {
|
---|
1909 | // nothing to do
|
---|
1910 | LogIO os( LogOrigin( "Scantable", "reshapeSpectrum()", WHERE ) ) ;
|
---|
1911 | os << "Whole range is selected. Nothing to do." << LogIO::POST ;
|
---|
1912 | return ;
|
---|
1913 | }
|
---|
1914 | else {
|
---|
1915 | LogIO os( LogOrigin( "Scantable", "reshapeSpectrum()", WHERE ) ) ;
|
---|
1916 | os << "Specified maximum exceeds nChan. Applied range is ["
|
---|
1917 | << nmin << ", " << nChan-1 << "]." << LogIO::POST ;
|
---|
1918 | nmax = nChan - 1 ;
|
---|
1919 | }
|
---|
1920 | }
|
---|
1921 |
|
---|
1922 | // reshape specCol_ and flagCol_
|
---|
1923 | for ( int irow = 0 ; irow < nrow() ; irow++ ) {
|
---|
1924 | reshapeSpectrum( nmin, nmax, irow ) ;
|
---|
1925 | }
|
---|
1926 |
|
---|
1927 | // update FREQUENCIES subtable
|
---|
1928 | Vector<uInt> freqIdArray = mfreqidCol_.getColumn();
|
---|
1929 | uInt numFreqId = GenSort<uInt>::sort(freqIdArray, Sort::Ascending,
|
---|
1930 | Sort::HeapSort | Sort::NoDuplicates);
|
---|
1931 | Double refpix ;
|
---|
1932 | Double refval ;
|
---|
1933 | Double increment ;
|
---|
1934 | for (uInt irow = 0; irow < numFreqId; irow++) {
|
---|
1935 | freqTable_.getEntry( refpix, refval, increment, freqIdArray[irow] ) ;
|
---|
1936 | /***
|
---|
1937 | * need to shift refpix to nmin
|
---|
1938 | * note that channel nmin in old index will be channel 0 in new one
|
---|
1939 | ***/
|
---|
1940 | refval = refval - ( refpix - nmin ) * increment ;
|
---|
1941 | refpix = 0 ;
|
---|
1942 | freqTable_.setEntry( refpix, refval, increment, freqIdArray[irow] ) ;
|
---|
1943 | }
|
---|
1944 |
|
---|
1945 | // update nchan
|
---|
1946 | int newsize = nmax - nmin + 1 ;
|
---|
1947 | table_.rwKeywordSet().define( "nChan", newsize ) ;
|
---|
1948 |
|
---|
1949 | // update bandwidth
|
---|
1950 | // assumed all spectra in the scantable have same bandwidth
|
---|
1951 | table_.rwKeywordSet().define( "Bandwidth", increment * newsize ) ;
|
---|
1952 |
|
---|
1953 | return ;
|
---|
1954 | }
|
---|
1955 |
|
---|
1956 | void asap::Scantable::reshapeSpectrum( int nmin, int nmax, int irow )
|
---|
1957 | {
|
---|
1958 | // reshape specCol_ and flagCol_
|
---|
1959 | Vector<Float> oldspec = specCol_( irow ) ;
|
---|
1960 | Vector<uChar> oldflag = flagsCol_( irow ) ;
|
---|
1961 | Vector<Float> oldtsys = tsysCol_( irow ) ;
|
---|
1962 | uInt newsize = nmax - nmin + 1 ;
|
---|
1963 | Slice slice( nmin, newsize, 1 ) ;
|
---|
1964 | specCol_.put( irow, oldspec( slice ) ) ;
|
---|
1965 | flagsCol_.put( irow, oldflag( slice ) ) ;
|
---|
1966 | if ( oldspec.size() == oldtsys.size() )
|
---|
1967 | tsysCol_.put( irow, oldtsys( slice ) ) ;
|
---|
1968 |
|
---|
1969 | return ;
|
---|
1970 | }
|
---|
1971 |
|
---|
1972 | void asap::Scantable::regridSpecChannel( double dnu, int nChan )
|
---|
1973 | {
|
---|
1974 | LogIO os( LogOrigin( "Scantable", "regridChannel()", WHERE ) ) ;
|
---|
1975 | os << "Regrid abcissa with spectral resoultion " << dnu << " " << freqTable_.getUnitString() << " with channel number " << ((nChan>0)? String(nChan) : "covering band width")<< LogIO::POST ;
|
---|
1976 | int freqnrow = freqTable_.table().nrow() ;
|
---|
1977 | Vector<bool> firstTime( freqnrow, true ) ;
|
---|
1978 | double oldincr, factor;
|
---|
1979 | uInt currId;
|
---|
1980 | Double refpix ;
|
---|
1981 | Double refval ;
|
---|
1982 | Double increment ;
|
---|
1983 | for ( int irow = 0 ; irow < nrow() ; irow++ ) {
|
---|
1984 | currId = mfreqidCol_(irow);
|
---|
1985 | vector<double> abcissa = getAbcissa( irow ) ;
|
---|
1986 | if (nChan < 0) {
|
---|
1987 | int oldsize = abcissa.size() ;
|
---|
1988 | double bw = (abcissa[oldsize-1]-abcissa[0]) + \
|
---|
1989 | 0.5 * (abcissa[1]-abcissa[0] + abcissa[oldsize-1]-abcissa[oldsize-2]) ;
|
---|
1990 | nChan = int( ceil( abs(bw/dnu) ) ) ;
|
---|
1991 | }
|
---|
1992 | // actual regridding
|
---|
1993 | regridChannel( nChan, dnu, irow ) ;
|
---|
1994 |
|
---|
1995 | // update FREQUENCIES subtable
|
---|
1996 | if (firstTime[currId]) {
|
---|
1997 | oldincr = abcissa[1]-abcissa[0] ;
|
---|
1998 | factor = dnu/oldincr ;
|
---|
1999 | firstTime[currId] = false ;
|
---|
2000 | freqTable_.getEntry( refpix, refval, increment, currId ) ;
|
---|
2001 |
|
---|
2002 | //refval = refval - ( refpix + 0.5 * (1 - factor) ) * increment ;
|
---|
2003 | if (factor > 0 ) {
|
---|
2004 | refpix = (refpix + 0.5)/factor - 0.5;
|
---|
2005 | } else {
|
---|
2006 | refpix = (abcissa.size() - 0.5 - refpix)/abs(factor) - 0.5;
|
---|
2007 | }
|
---|
2008 | freqTable_.setEntry( refpix, refval, increment*factor, currId ) ;
|
---|
2009 | //os << "ID" << currId << ": channel width (Orig) = " << oldincr << " [" << freqTable_.getUnitString() << "], scale factor = " << factor << LogIO::POST ;
|
---|
2010 | //os << " frequency increment (Orig) = " << increment << "-> (New) " << increment*factor << LogIO::POST ;
|
---|
2011 | }
|
---|
2012 | }
|
---|
2013 | }
|
---|
2014 |
|
---|
2015 | void asap::Scantable::regridChannel( int nChan, double dnu )
|
---|
2016 | {
|
---|
2017 | LogIO os( LogOrigin( "Scantable", "regridChannel()", WHERE ) ) ;
|
---|
2018 | os << "Regrid abcissa with channel number " << nChan << " and spectral resoultion " << dnu << "Hz." << LogIO::POST ;
|
---|
2019 | // assumed that all rows have same nChan
|
---|
2020 | Vector<Float> arr = specCol_( 0 ) ;
|
---|
2021 | int oldsize = arr.nelements() ;
|
---|
2022 |
|
---|
2023 | // if oldsize == nChan, nothing to do
|
---|
2024 | if ( oldsize == nChan ) {
|
---|
2025 | os << "Specified channel number is same as current one. Nothing to do." << LogIO::POST ;
|
---|
2026 | return ;
|
---|
2027 | }
|
---|
2028 |
|
---|
2029 | // if oldChan < nChan, unphysical operation
|
---|
2030 | if ( oldsize < nChan ) {
|
---|
2031 | os << "Unphysical operation. Nothing to do." << LogIO::POST ;
|
---|
2032 | return ;
|
---|
2033 | }
|
---|
2034 |
|
---|
2035 | // change channel number for specCol_, flagCol_, and tsysCol_ (if necessary)
|
---|
2036 | vector<string> coordinfo = getCoordInfo() ;
|
---|
2037 | string oldinfo = coordinfo[0] ;
|
---|
2038 | coordinfo[0] = "Hz" ;
|
---|
2039 | setCoordInfo( coordinfo ) ;
|
---|
2040 | for ( int irow = 0 ; irow < nrow() ; irow++ ) {
|
---|
2041 | regridChannel( nChan, dnu, irow ) ;
|
---|
2042 | }
|
---|
2043 | coordinfo[0] = oldinfo ;
|
---|
2044 | setCoordInfo( coordinfo ) ;
|
---|
2045 |
|
---|
2046 |
|
---|
2047 | // NOTE: this method does not update metadata such as
|
---|
2048 | // FREQUENCIES subtable, nChan, Bandwidth, etc.
|
---|
2049 |
|
---|
2050 | return ;
|
---|
2051 | }
|
---|
2052 |
|
---|
2053 | void asap::Scantable::regridChannel( int nChan, double dnu, int irow )
|
---|
2054 | {
|
---|
2055 | // logging
|
---|
2056 | //ofstream ofs( "average.log", std::ios::out | std::ios::app ) ;
|
---|
2057 | //ofs << "IFNO = " << getIF( irow ) << " irow = " << irow << endl ;
|
---|
2058 |
|
---|
2059 | Vector<Float> oldspec = specCol_( irow ) ;
|
---|
2060 | Vector<uChar> oldflag = flagsCol_( irow ) ;
|
---|
2061 | Vector<Float> oldtsys = tsysCol_( irow ) ;
|
---|
2062 | Vector<Float> newspec( nChan, 0 ) ;
|
---|
2063 | Vector<uChar> newflag( nChan, true ) ;
|
---|
2064 | Vector<Float> newtsys ;
|
---|
2065 | bool regridTsys = false ;
|
---|
2066 | if (oldtsys.size() == oldspec.size()) {
|
---|
2067 | regridTsys = true ;
|
---|
2068 | newtsys.resize(nChan,false) ;
|
---|
2069 | newtsys = 0 ;
|
---|
2070 | }
|
---|
2071 |
|
---|
2072 | // regrid
|
---|
2073 | vector<double> abcissa = getAbcissa( irow ) ;
|
---|
2074 | int oldsize = abcissa.size() ;
|
---|
2075 | double olddnu = abcissa[1] - abcissa[0] ;
|
---|
2076 | //int ichan = 0 ;
|
---|
2077 | double wsum = 0.0 ;
|
---|
2078 | Vector<double> zi( nChan+1 ) ;
|
---|
2079 | Vector<double> yi( oldsize + 1 ) ;
|
---|
2080 | yi[0] = abcissa[0] - 0.5 * olddnu ;
|
---|
2081 | for ( int ii = 1 ; ii < oldsize ; ii++ )
|
---|
2082 | yi[ii] = 0.5* (abcissa[ii-1] + abcissa[ii]) ;
|
---|
2083 | yi[oldsize] = abcissa[oldsize-1] \
|
---|
2084 | + 0.5 * (abcissa[oldsize-1] - abcissa[oldsize-2]) ;
|
---|
2085 | //zi[0] = abcissa[0] - 0.5 * olddnu ;
|
---|
2086 | zi[0] = ((olddnu*dnu > 0) ? yi[0] : yi[oldsize]) ;
|
---|
2087 | for ( int ii = 1 ; ii < nChan ; ii++ )
|
---|
2088 | zi[ii] = zi[0] + dnu * ii ;
|
---|
2089 | zi[nChan] = zi[nChan-1] + dnu ;
|
---|
2090 | // Access zi and yi in ascending order
|
---|
2091 | int izs = ((dnu > 0) ? 0 : nChan ) ;
|
---|
2092 | int ize = ((dnu > 0) ? nChan : 0 ) ;
|
---|
2093 | int izincr = ((dnu > 0) ? 1 : -1 ) ;
|
---|
2094 | int ichan = ((olddnu > 0) ? 0 : oldsize ) ;
|
---|
2095 | int iye = ((olddnu > 0) ? oldsize : 0 ) ;
|
---|
2096 | int iyincr = ((olddnu > 0) ? 1 : -1 ) ;
|
---|
2097 | //for ( int ii = izs ; ii != ize ; ii+=izincr ){
|
---|
2098 | int ii = izs ;
|
---|
2099 | while (ii != ize) {
|
---|
2100 | // always zl < zr
|
---|
2101 | double zl = zi[ii] ;
|
---|
2102 | double zr = zi[ii+izincr] ;
|
---|
2103 | // Need to access smaller index for the new spec, flag, and tsys.
|
---|
2104 | // Values between zi[k] and zi[k+1] should be stored in newspec[k], etc.
|
---|
2105 | int i = min(ii, ii+izincr) ;
|
---|
2106 | //for ( int jj = ichan ; jj != iye ; jj+=iyincr ) {
|
---|
2107 | int jj = ichan ;
|
---|
2108 | while (jj != iye) {
|
---|
2109 | // always yl < yr
|
---|
2110 | double yl = yi[jj] ;
|
---|
2111 | double yr = yi[jj+iyincr] ;
|
---|
2112 | // Need to access smaller index for the original spec, flag, and tsys.
|
---|
2113 | // Values between yi[k] and yi[k+1] are stored in oldspec[k], etc.
|
---|
2114 | int j = min(jj, jj+iyincr) ;
|
---|
2115 | if ( yr <= zl ) {
|
---|
2116 | jj += iyincr ;
|
---|
2117 | continue ;
|
---|
2118 | }
|
---|
2119 | else if ( yl <= zl ) {
|
---|
2120 | if ( yr < zr ) {
|
---|
2121 | if (!oldflag[j]) {
|
---|
2122 | newspec[i] += oldspec[j] * ( yr - zl ) ;
|
---|
2123 | if (regridTsys) newtsys[i] += oldtsys[j] * ( yr - zl ) ;
|
---|
2124 | wsum += ( yr - zl ) ;
|
---|
2125 | }
|
---|
2126 | newflag[i] = (newflag[i] && oldflag[j]) ? 1 << 7 : 0 ;
|
---|
2127 | }
|
---|
2128 | else {
|
---|
2129 | if (!oldflag[j]) {
|
---|
2130 | newspec[i] += oldspec[j] * abs(dnu) ;
|
---|
2131 | if (regridTsys) newtsys[i] += oldtsys[j] * abs(dnu) ;
|
---|
2132 | wsum += abs(dnu) ;
|
---|
2133 | }
|
---|
2134 | newflag[i] = (newflag[i] && oldflag[j]) ? 1 << 7 : 0 ;
|
---|
2135 | ichan = jj ;
|
---|
2136 | break ;
|
---|
2137 | }
|
---|
2138 | }
|
---|
2139 | else if ( yl < zr ) {
|
---|
2140 | if ( yr <= zr ) {
|
---|
2141 | if (!oldflag[j]) {
|
---|
2142 | newspec[i] += oldspec[j] * ( yr - yl ) ;
|
---|
2143 | if (regridTsys) newtsys[i] += oldtsys[j] * ( yr - yl ) ;
|
---|
2144 | wsum += ( yr - yl ) ;
|
---|
2145 | }
|
---|
2146 | newflag[i] = (newflag[i] && oldflag[j]) ? 1 << 7 : 0 ;
|
---|
2147 | }
|
---|
2148 | else {
|
---|
2149 | if (!oldflag[j]) {
|
---|
2150 | newspec[i] += oldspec[j] * ( zr - yl ) ;
|
---|
2151 | if (regridTsys) newtsys[i] += oldtsys[j] * ( zr - yl ) ;
|
---|
2152 | wsum += ( zr - yl ) ;
|
---|
2153 | }
|
---|
2154 | newflag[i] = (newflag[i] && oldflag[j]) ? 1 << 7 : 0 ;
|
---|
2155 | ichan = jj ;
|
---|
2156 | break ;
|
---|
2157 | }
|
---|
2158 | }
|
---|
2159 | else {
|
---|
2160 | ichan = jj - iyincr ;
|
---|
2161 | break ;
|
---|
2162 | }
|
---|
2163 | jj += iyincr ;
|
---|
2164 | }
|
---|
2165 | if ( wsum != 0.0 ) {
|
---|
2166 | newspec[i] /= wsum ;
|
---|
2167 | if (regridTsys) newtsys[i] /= wsum ;
|
---|
2168 | }
|
---|
2169 | wsum = 0.0 ;
|
---|
2170 | ii += izincr ;
|
---|
2171 | }
|
---|
2172 | // if ( dnu > 0.0 ) {
|
---|
2173 | // for ( int ii = 0 ; ii < nChan ; ii++ ) {
|
---|
2174 | // double zl = zi[ii] ;
|
---|
2175 | // double zr = zi[ii+1] ;
|
---|
2176 | // for ( int j = ichan ; j < oldsize ; j++ ) {
|
---|
2177 | // double yl = yi[j] ;
|
---|
2178 | // double yr = yi[j+1] ;
|
---|
2179 | // if ( yl <= zl ) {
|
---|
2180 | // if ( yr <= zl ) {
|
---|
2181 | // continue ;
|
---|
2182 | // }
|
---|
2183 | // else if ( yr <= zr ) {
|
---|
2184 | // if (!oldflag[j]) {
|
---|
2185 | // newspec[ii] += oldspec[j] * ( yr - zl ) ;
|
---|
2186 | // if (regridTsys) newtsys[ii] += oldtsys[j] * ( yr - zl ) ;
|
---|
2187 | // wsum += ( yr - zl ) ;
|
---|
2188 | // }
|
---|
2189 | // newflag[ii] = newflag[ii] && oldflag[j] ;
|
---|
2190 | // }
|
---|
2191 | // else {
|
---|
2192 | // if (!oldflag[j]) {
|
---|
2193 | // newspec[ii] += oldspec[j] * dnu ;
|
---|
2194 | // if (regridTsys) newtsys[ii] += oldtsys[j] * dnu ;
|
---|
2195 | // wsum += dnu ;
|
---|
2196 | // }
|
---|
2197 | // newflag[ii] = newflag[ii] && oldflag[j] ;
|
---|
2198 | // ichan = j ;
|
---|
2199 | // break ;
|
---|
2200 | // }
|
---|
2201 | // }
|
---|
2202 | // else if ( yl < zr ) {
|
---|
2203 | // if ( yr <= zr ) {
|
---|
2204 | // if (!oldflag[j]) {
|
---|
2205 | // newspec[ii] += oldspec[j] * ( yr - yl ) ;
|
---|
2206 | // if (regridTsys) newtsys[ii] += oldtsys[j] * ( yr - yl ) ;
|
---|
2207 | // wsum += ( yr - yl ) ;
|
---|
2208 | // }
|
---|
2209 | // newflag[ii] = newflag[ii] && oldflag[j] ;
|
---|
2210 | // }
|
---|
2211 | // else {
|
---|
2212 | // if (!oldflag[j]) {
|
---|
2213 | // newspec[ii] += oldspec[j] * ( zr - yl ) ;
|
---|
2214 | // if (regridTsys) newtsys[ii] += oldtsys[j] * ( zr - yl ) ;
|
---|
2215 | // wsum += ( zr - yl ) ;
|
---|
2216 | // }
|
---|
2217 | // newflag[ii] = newflag[ii] && oldflag[j] ;
|
---|
2218 | // ichan = j ;
|
---|
2219 | // break ;
|
---|
2220 | // }
|
---|
2221 | // }
|
---|
2222 | // else {
|
---|
2223 | // ichan = j - 1 ;
|
---|
2224 | // break ;
|
---|
2225 | // }
|
---|
2226 | // }
|
---|
2227 | // if ( wsum != 0.0 ) {
|
---|
2228 | // newspec[ii] /= wsum ;
|
---|
2229 | // if (regridTsys) newtsys[ii] /= wsum ;
|
---|
2230 | // }
|
---|
2231 | // wsum = 0.0 ;
|
---|
2232 | // }
|
---|
2233 | // }
|
---|
2234 | // else if ( dnu < 0.0 ) {
|
---|
2235 | // for ( int ii = 0 ; ii < nChan ; ii++ ) {
|
---|
2236 | // double zl = zi[ii] ;
|
---|
2237 | // double zr = zi[ii+1] ;
|
---|
2238 | // for ( int j = ichan ; j < oldsize ; j++ ) {
|
---|
2239 | // double yl = yi[j] ;
|
---|
2240 | // double yr = yi[j+1] ;
|
---|
2241 | // if ( yl >= zl ) {
|
---|
2242 | // if ( yr >= zl ) {
|
---|
2243 | // continue ;
|
---|
2244 | // }
|
---|
2245 | // else if ( yr >= zr ) {
|
---|
2246 | // if (!oldflag[j]) {
|
---|
2247 | // newspec[ii] += oldspec[j] * abs( yr - zl ) ;
|
---|
2248 | // if (regridTsys) newtsys[ii] += oldtsys[j] * abs( yr - zl ) ;
|
---|
2249 | // wsum += abs( yr - zl ) ;
|
---|
2250 | // }
|
---|
2251 | // newflag[ii] = newflag[ii] && oldflag[j] ;
|
---|
2252 | // }
|
---|
2253 | // else {
|
---|
2254 | // if (!oldflag[j]) {
|
---|
2255 | // newspec[ii] += oldspec[j] * abs( dnu ) ;
|
---|
2256 | // if (regridTsys) newtsys[ii] += oldtsys[j] * abs( dnu ) ;
|
---|
2257 | // wsum += abs( dnu ) ;
|
---|
2258 | // }
|
---|
2259 | // newflag[ii] = newflag[ii] && oldflag[j] ;
|
---|
2260 | // ichan = j ;
|
---|
2261 | // break ;
|
---|
2262 | // }
|
---|
2263 | // }
|
---|
2264 | // else if ( yl > zr ) {
|
---|
2265 | // if ( yr >= zr ) {
|
---|
2266 | // if (!oldflag[j]) {
|
---|
2267 | // newspec[ii] += oldspec[j] * abs( yr - yl ) ;
|
---|
2268 | // if (regridTsys) newtsys[ii] += oldtsys[j] * abs( yr - yl ) ;
|
---|
2269 | // wsum += abs( yr - yl ) ;
|
---|
2270 | // }
|
---|
2271 | // newflag[ii] = newflag[ii] && oldflag[j] ;
|
---|
2272 | // }
|
---|
2273 | // else {
|
---|
2274 | // if (!oldflag[j]) {
|
---|
2275 | // newspec[ii] += oldspec[j] * abs( zr - yl ) ;
|
---|
2276 | // if (regridTsys) newtsys[ii] += oldtsys[j] * abs( zr - yl ) ;
|
---|
2277 | // wsum += abs( zr - yl ) ;
|
---|
2278 | // }
|
---|
2279 | // newflag[ii] = newflag[ii] && oldflag[j] ;
|
---|
2280 | // ichan = j ;
|
---|
2281 | // break ;
|
---|
2282 | // }
|
---|
2283 | // }
|
---|
2284 | // else {
|
---|
2285 | // ichan = j - 1 ;
|
---|
2286 | // break ;
|
---|
2287 | // }
|
---|
2288 | // }
|
---|
2289 | // if ( wsum != 0.0 ) {
|
---|
2290 | // newspec[ii] /= wsum ;
|
---|
2291 | // if (regridTsys) newtsys[ii] /= wsum ;
|
---|
2292 | // }
|
---|
2293 | // wsum = 0.0 ;
|
---|
2294 | // }
|
---|
2295 | // }
|
---|
2296 | // // //ofs << "olddnu = " << olddnu << ", dnu = " << dnu << endl ;
|
---|
2297 | // // pile += dnu ;
|
---|
2298 | // // wedge = olddnu * ( refChan + 1 ) ;
|
---|
2299 | // // while ( wedge < pile ) {
|
---|
2300 | // // newspec[0] += olddnu * oldspec[refChan] ;
|
---|
2301 | // // newflag[0] = newflag[0] || oldflag[refChan] ;
|
---|
2302 | // // //ofs << "channel " << refChan << " is included in new channel 0" << endl ;
|
---|
2303 | // // refChan++ ;
|
---|
2304 | // // wedge += olddnu ;
|
---|
2305 | // // wsum += olddnu ;
|
---|
2306 | // // //ofs << "newspec[0] = " << newspec[0] << " wsum = " << wsum << endl ;
|
---|
2307 | // // }
|
---|
2308 | // // frac = ( wedge - pile ) / olddnu ;
|
---|
2309 | // // wsum += ( 1.0 - frac ) * olddnu ;
|
---|
2310 | // // newspec[0] += ( 1.0 - frac ) * olddnu * oldspec[refChan] ;
|
---|
2311 | // // newflag[0] = newflag[0] || oldflag[refChan] ;
|
---|
2312 | // // //ofs << "channel " << refChan << " is partly included in new channel 0" << " with fraction of " << ( 1.0 - frac ) << endl ;
|
---|
2313 | // // //ofs << "newspec[0] = " << newspec[0] << " wsum = " << wsum << endl ;
|
---|
2314 | // // newspec[0] /= wsum ;
|
---|
2315 | // // //ofs << "newspec[0] = " << newspec[0] << endl ;
|
---|
2316 | // // //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
|
---|
2317 |
|
---|
2318 | // // /***
|
---|
2319 | // // * ichan = 1 - nChan-2
|
---|
2320 | // // ***/
|
---|
2321 | // // for ( int ichan = 1 ; ichan < nChan - 1 ; ichan++ ) {
|
---|
2322 | // // pile += dnu ;
|
---|
2323 | // // newspec[ichan] += frac * olddnu * oldspec[refChan] ;
|
---|
2324 | // // newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
|
---|
2325 | // // //ofs << "channel " << refChan << " is partly included in new channel " << ichan << " with fraction of " << frac << endl ;
|
---|
2326 | // // refChan++ ;
|
---|
2327 | // // wedge += olddnu ;
|
---|
2328 | // // wsum = frac * olddnu ;
|
---|
2329 | // // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
|
---|
2330 | // // while ( wedge < pile ) {
|
---|
2331 | // // newspec[ichan] += olddnu * oldspec[refChan] ;
|
---|
2332 | // // newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
|
---|
2333 | // // //ofs << "channel " << refChan << " is included in new channel " << ichan << endl ;
|
---|
2334 | // // refChan++ ;
|
---|
2335 | // // wedge += olddnu ;
|
---|
2336 | // // wsum += olddnu ;
|
---|
2337 | // // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
|
---|
2338 | // // }
|
---|
2339 | // // frac = ( wedge - pile ) / olddnu ;
|
---|
2340 | // // wsum += ( 1.0 - frac ) * olddnu ;
|
---|
2341 | // // newspec[ichan] += ( 1.0 - frac ) * olddnu * oldspec[refChan] ;
|
---|
2342 | // // newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
|
---|
2343 | // // //ofs << "channel " << refChan << " is partly included in new channel " << ichan << " with fraction of " << ( 1.0 - frac ) << endl ;
|
---|
2344 | // // //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
|
---|
2345 | // // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
|
---|
2346 | // // newspec[ichan] /= wsum ;
|
---|
2347 | // // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << endl ;
|
---|
2348 | // // }
|
---|
2349 |
|
---|
2350 | // // /***
|
---|
2351 | // // * ichan = nChan-1
|
---|
2352 | // // ***/
|
---|
2353 | // // // NOTE: Assumed that all spectra have the same bandwidth
|
---|
2354 | // // pile += dnu ;
|
---|
2355 | // // newspec[nChan-1] += frac * olddnu * oldspec[refChan] ;
|
---|
2356 | // // newflag[nChan-1] = newflag[nChan-1] || oldflag[refChan] ;
|
---|
2357 | // // //ofs << "channel " << refChan << " is partly included in new channel " << nChan-1 << " with fraction of " << frac << endl ;
|
---|
2358 | // // refChan++ ;
|
---|
2359 | // // wedge += olddnu ;
|
---|
2360 | // // wsum = frac * olddnu ;
|
---|
2361 | // // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
|
---|
2362 | // // for ( int jchan = refChan ; jchan < oldsize ; jchan++ ) {
|
---|
2363 | // // newspec[nChan-1] += olddnu * oldspec[jchan] ;
|
---|
2364 | // // newflag[nChan-1] = newflag[nChan-1] || oldflag[jchan] ;
|
---|
2365 | // // wsum += olddnu ;
|
---|
2366 | // // //ofs << "channel " << jchan << " is included in new channel " << nChan-1 << " with fraction of " << frac << endl ;
|
---|
2367 | // // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
|
---|
2368 | // // }
|
---|
2369 | // // //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
|
---|
2370 | // // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
|
---|
2371 | // // newspec[nChan-1] /= wsum ;
|
---|
2372 | // // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << endl ;
|
---|
2373 |
|
---|
2374 | // // // ofs.close() ;
|
---|
2375 |
|
---|
2376 | specCol_.put( irow, newspec ) ;
|
---|
2377 | flagsCol_.put( irow, newflag ) ;
|
---|
2378 | if (regridTsys) tsysCol_.put( irow, newtsys );
|
---|
2379 |
|
---|
2380 | return ;
|
---|
2381 | }
|
---|
2382 |
|
---|
2383 | void Scantable::regridChannel( int nChan, double dnu, double fmin, int irow )
|
---|
2384 | {
|
---|
2385 | Vector<Float> oldspec = specCol_( irow ) ;
|
---|
2386 | Vector<uChar> oldflag = flagsCol_( irow ) ;
|
---|
2387 | Vector<Float> oldtsys = tsysCol_( irow ) ;
|
---|
2388 | Vector<Float> newspec( nChan, 0 ) ;
|
---|
2389 | Vector<uChar> newflag( nChan, true ) ;
|
---|
2390 | Vector<Float> newtsys ;
|
---|
2391 | bool regridTsys = false ;
|
---|
2392 | if (oldtsys.size() == oldspec.size()) {
|
---|
2393 | regridTsys = true ;
|
---|
2394 | newtsys.resize(nChan,false) ;
|
---|
2395 | newtsys = 0 ;
|
---|
2396 | }
|
---|
2397 |
|
---|
2398 | // regrid
|
---|
2399 | vector<double> abcissa = getAbcissa( irow ) ;
|
---|
2400 | int oldsize = abcissa.size() ;
|
---|
2401 | double olddnu = abcissa[1] - abcissa[0] ;
|
---|
2402 | //int ichan = 0 ;
|
---|
2403 | double wsum = 0.0 ;
|
---|
2404 | Vector<double> zi( nChan+1 ) ;
|
---|
2405 | Vector<double> yi( oldsize + 1 ) ;
|
---|
2406 | Block<uInt> count( nChan, 0 ) ;
|
---|
2407 | yi[0] = abcissa[0] - 0.5 * olddnu ;
|
---|
2408 | for ( int ii = 1 ; ii < oldsize ; ii++ )
|
---|
2409 | yi[ii] = 0.5* (abcissa[ii-1] + abcissa[ii]) ;
|
---|
2410 | yi[oldsize] = abcissa[oldsize-1] \
|
---|
2411 | + 0.5 * (abcissa[oldsize-1] - abcissa[oldsize-2]) ;
|
---|
2412 | // cout << "olddnu=" << olddnu << ", dnu=" << dnu << " (diff=" << olddnu-dnu << ")" << endl ;
|
---|
2413 | // cout << "yi[0]=" << yi[0] << ", fmin=" << fmin << " (diff=" << yi[0]-fmin << ")" << endl ;
|
---|
2414 | // cout << "oldsize=" << oldsize << ", nChan=" << nChan << endl ;
|
---|
2415 |
|
---|
2416 | // do not regrid if input parameters are almost same as current
|
---|
2417 | // spectral setup
|
---|
2418 | double dnuDiff = abs( ( dnu - olddnu ) / olddnu ) ;
|
---|
2419 | double oldfmin = min( yi[0], yi[oldsize] ) ;
|
---|
2420 | double fminDiff = abs( ( fmin - oldfmin ) / oldfmin ) ;
|
---|
2421 | double nChanDiff = nChan - oldsize ;
|
---|
2422 | double eps = 1.0e-8 ;
|
---|
2423 | if ( nChanDiff == 0 && dnuDiff < eps && fminDiff < eps )
|
---|
2424 | return ;
|
---|
2425 |
|
---|
2426 | //zi[0] = abcissa[0] - 0.5 * olddnu ;
|
---|
2427 | //zi[0] = ((olddnu*dnu > 0) ? yi[0] : yi[oldsize]) ;
|
---|
2428 | if ( dnu > 0 )
|
---|
2429 | zi[0] = fmin - 0.5 * dnu ;
|
---|
2430 | else
|
---|
2431 | zi[0] = fmin + nChan * abs(dnu) ;
|
---|
2432 | for ( int ii = 1 ; ii < nChan ; ii++ )
|
---|
2433 | zi[ii] = zi[0] + dnu * ii ;
|
---|
2434 | zi[nChan] = zi[nChan-1] + dnu ;
|
---|
2435 | // Access zi and yi in ascending order
|
---|
2436 | int izs = ((dnu > 0) ? 0 : nChan ) ;
|
---|
2437 | int ize = ((dnu > 0) ? nChan : 0 ) ;
|
---|
2438 | int izincr = ((dnu > 0) ? 1 : -1 ) ;
|
---|
2439 | int ichan = ((olddnu > 0) ? 0 : oldsize ) ;
|
---|
2440 | int iye = ((olddnu > 0) ? oldsize : 0 ) ;
|
---|
2441 | int iyincr = ((olddnu > 0) ? 1 : -1 ) ;
|
---|
2442 | //for ( int ii = izs ; ii != ize ; ii+=izincr ){
|
---|
2443 | int ii = izs ;
|
---|
2444 | while (ii != ize) {
|
---|
2445 | // always zl < zr
|
---|
2446 | double zl = zi[ii] ;
|
---|
2447 | double zr = zi[ii+izincr] ;
|
---|
2448 | // Need to access smaller index for the new spec, flag, and tsys.
|
---|
2449 | // Values between zi[k] and zi[k+1] should be stored in newspec[k], etc.
|
---|
2450 | int i = min(ii, ii+izincr) ;
|
---|
2451 | //for ( int jj = ichan ; jj != iye ; jj+=iyincr ) {
|
---|
2452 | int jj = ichan ;
|
---|
2453 | while (jj != iye) {
|
---|
2454 | // always yl < yr
|
---|
2455 | double yl = yi[jj] ;
|
---|
2456 | double yr = yi[jj+iyincr] ;
|
---|
2457 | // Need to access smaller index for the original spec, flag, and tsys.
|
---|
2458 | // Values between yi[k] and yi[k+1] are stored in oldspec[k], etc.
|
---|
2459 | int j = min(jj, jj+iyincr) ;
|
---|
2460 | if ( yr <= zl ) {
|
---|
2461 | jj += iyincr ;
|
---|
2462 | continue ;
|
---|
2463 | }
|
---|
2464 | else if ( yl <= zl ) {
|
---|
2465 | if ( yr < zr ) {
|
---|
2466 | if (!oldflag[j]) {
|
---|
2467 | newspec[i] += oldspec[j] * ( yr - zl ) ;
|
---|
2468 | if (regridTsys) newtsys[i] += oldtsys[j] * ( yr - zl ) ;
|
---|
2469 | wsum += ( yr - zl ) ;
|
---|
2470 | count[i]++ ;
|
---|
2471 | }
|
---|
2472 | newflag[i] = newflag[i] && oldflag[j] ;
|
---|
2473 | }
|
---|
2474 | else {
|
---|
2475 | if (!oldflag[j]) {
|
---|
2476 | newspec[i] += oldspec[j] * abs(dnu) ;
|
---|
2477 | if (regridTsys) newtsys[i] += oldtsys[j] * abs(dnu) ;
|
---|
2478 | wsum += abs(dnu) ;
|
---|
2479 | count[i]++ ;
|
---|
2480 | }
|
---|
2481 | newflag[i] = newflag[i] && oldflag[j] ;
|
---|
2482 | ichan = jj ;
|
---|
2483 | break ;
|
---|
2484 | }
|
---|
2485 | }
|
---|
2486 | else if ( yl < zr ) {
|
---|
2487 | if ( yr <= zr ) {
|
---|
2488 | if (!oldflag[j]) {
|
---|
2489 | newspec[i] += oldspec[j] * ( yr - yl ) ;
|
---|
2490 | if (regridTsys) newtsys[i] += oldtsys[j] * ( yr - yl ) ;
|
---|
2491 | wsum += ( yr - yl ) ;
|
---|
2492 | count[i]++ ;
|
---|
2493 | }
|
---|
2494 | newflag[i] = newflag[i] && oldflag[j] ;
|
---|
2495 | }
|
---|
2496 | else {
|
---|
2497 | if (!oldflag[j]) {
|
---|
2498 | newspec[i] += oldspec[j] * ( zr - yl ) ;
|
---|
2499 | if (regridTsys) newtsys[i] += oldtsys[j] * ( zr - yl ) ;
|
---|
2500 | wsum += ( zr - yl ) ;
|
---|
2501 | count[i]++ ;
|
---|
2502 | }
|
---|
2503 | newflag[i] = newflag[i] && oldflag[j] ;
|
---|
2504 | ichan = jj ;
|
---|
2505 | break ;
|
---|
2506 | }
|
---|
2507 | }
|
---|
2508 | else {
|
---|
2509 | //ichan = jj - iyincr ;
|
---|
2510 | break ;
|
---|
2511 | }
|
---|
2512 | jj += iyincr ;
|
---|
2513 | }
|
---|
2514 | if ( wsum != 0.0 ) {
|
---|
2515 | newspec[i] /= wsum ;
|
---|
2516 | if (regridTsys) newtsys[i] /= wsum ;
|
---|
2517 | }
|
---|
2518 | wsum = 0.0 ;
|
---|
2519 | ii += izincr ;
|
---|
2520 | }
|
---|
2521 |
|
---|
2522 | // flag out channels without data
|
---|
2523 | // this is tentative since there is no specific definition
|
---|
2524 | // on bit flag...
|
---|
2525 | uChar noData = 1 << 7 ;
|
---|
2526 | for ( Int i = 0 ; i < nChan ; i++ ) {
|
---|
2527 | if ( count[i] == 0 )
|
---|
2528 | newflag[i] = noData ;
|
---|
2529 | }
|
---|
2530 |
|
---|
2531 | specCol_.put( irow, newspec ) ;
|
---|
2532 | flagsCol_.put( irow, newflag ) ;
|
---|
2533 | if (regridTsys) tsysCol_.put( irow, newtsys );
|
---|
2534 |
|
---|
2535 | return ;
|
---|
2536 | }
|
---|
2537 |
|
---|
2538 | std::vector<float> Scantable::getWeather(int whichrow) const
|
---|
2539 | {
|
---|
2540 | std::vector<float> out(5);
|
---|
2541 | //Float temperature, pressure, humidity, windspeed, windaz;
|
---|
2542 | weatherTable_.getEntry(out[0], out[1], out[2], out[3], out[4],
|
---|
2543 | mweatheridCol_(uInt(whichrow)));
|
---|
2544 |
|
---|
2545 |
|
---|
2546 | return out;
|
---|
2547 | }
|
---|
2548 |
|
---|
2549 | bool Scantable::isAllChannelsFlagged(uInt whichrow)
|
---|
2550 | {
|
---|
2551 | uInt rflag;
|
---|
2552 | flagrowCol_.get(whichrow, rflag);
|
---|
2553 | if (rflag > 0)
|
---|
2554 | return true;
|
---|
2555 | bool flag;
|
---|
2556 | Vector<uChar> flags;
|
---|
2557 | flagsCol_.get(whichrow, flags);
|
---|
2558 | flag = (flags[0]>0);
|
---|
2559 | for (uInt i = 1; i < flags.size(); ++i) {
|
---|
2560 | flag &= (flags[i]>0);
|
---|
2561 | }
|
---|
2562 | // return ((flag >> 7) == 1);
|
---|
2563 | return (flag > 0);
|
---|
2564 | }
|
---|
2565 |
|
---|
2566 | std::size_t Scantable::nValidMask(const std::vector<bool>& mask)
|
---|
2567 | {
|
---|
2568 | std::size_t nvalid=0;
|
---|
2569 | // the assertion lines had better be replaced with static_assert when c++11 is supported
|
---|
2570 | AlwaysAssert(static_cast<std::size_t>(true)==1, AipsError);
|
---|
2571 | AlwaysAssert(static_cast<std::size_t>(false)==0, AipsError);
|
---|
2572 | for (uInt i = 1; i < mask.size(); ++i) {
|
---|
2573 | nvalid += static_cast<std::size_t>(mask[i]);
|
---|
2574 | }
|
---|
2575 | return nvalid;
|
---|
2576 | }
|
---|
2577 |
|
---|
2578 |
|
---|
2579 | std::vector<std::string> Scantable::applyBaselineTable(const std::string& bltable, const bool returnfitresult, const std::string& outbltable, const bool outbltableexists, const bool overwrite)
|
---|
2580 | {
|
---|
2581 | STBaselineTable btin = STBaselineTable(bltable);
|
---|
2582 |
|
---|
2583 | Vector<Bool> applyCol = btin.getApply();
|
---|
2584 | int nRowBl = applyCol.size();
|
---|
2585 | if (nRowBl != nrow()) {
|
---|
2586 | throw(AipsError("Scantable and bltable have different number of rows."));
|
---|
2587 | }
|
---|
2588 |
|
---|
2589 | std::vector<std::string> res;
|
---|
2590 | res.clear();
|
---|
2591 |
|
---|
2592 | bool outBaselineTable = ((outbltable != "") && (!outbltableexists || overwrite));
|
---|
2593 | bool bltableidentical = (bltable == outbltable);
|
---|
2594 | STBaselineTable btout = STBaselineTable(*this);
|
---|
2595 | ROScalarColumn<Double> tcol = ROScalarColumn<Double>(table_, "TIME");
|
---|
2596 | Vector<Double> timeSecCol = tcol.getColumn();
|
---|
2597 |
|
---|
2598 | for (int whichrow = 0; whichrow < nRowBl; ++whichrow) {
|
---|
2599 | if (applyCol[whichrow]) {
|
---|
2600 | std::vector<float> spec = getSpectrum(whichrow);
|
---|
2601 |
|
---|
2602 | std::vector<bool> mask = btin.getMask(whichrow); //use mask_bltable only
|
---|
2603 |
|
---|
2604 | STBaselineFunc::FuncName ftype = btin.getFunctionName(whichrow);
|
---|
2605 | std::vector<int> fpar = btin.getFuncParam(whichrow);
|
---|
2606 | std::vector<float> params;
|
---|
2607 | float rms;
|
---|
2608 | std::vector<float> resfit = doApplyBaselineTable(spec, mask, ftype, fpar, params, rms);
|
---|
2609 | setSpectrum(resfit, whichrow);
|
---|
2610 |
|
---|
2611 | if (returnfitresult) {
|
---|
2612 | res.push_back(packFittingResults(whichrow, params, rms));
|
---|
2613 | }
|
---|
2614 |
|
---|
2615 | if (outBaselineTable) {
|
---|
2616 | if (outbltableexists) {
|
---|
2617 | if (overwrite) {
|
---|
2618 | if (bltableidentical) {
|
---|
2619 | btin.setresult(uInt(whichrow), Vector<Float>(params), Float(rms));
|
---|
2620 | } else {
|
---|
2621 | btout.setresult(uInt(whichrow), Vector<Float>(params), Float(rms));
|
---|
2622 | }
|
---|
2623 | }
|
---|
2624 | } else {
|
---|
2625 | btout.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
2626 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
2627 | true, ftype, fpar, std::vector<float>(),
|
---|
2628 | getMaskListFromMask(mask), params, rms, spec.size(),
|
---|
2629 | 3.0, 0, 0.0, 0, std::vector<int>());
|
---|
2630 | }
|
---|
2631 | }
|
---|
2632 | }
|
---|
2633 | }
|
---|
2634 |
|
---|
2635 | if (outBaselineTable) {
|
---|
2636 | if (bltableidentical) {
|
---|
2637 | btin.save(outbltable);
|
---|
2638 | } else {
|
---|
2639 | btout.save(outbltable);
|
---|
2640 | }
|
---|
2641 | }
|
---|
2642 |
|
---|
2643 | return res;
|
---|
2644 | }
|
---|
2645 |
|
---|
2646 | std::vector<std::string> Scantable::subBaseline(const std::vector<std::string>& blInfoList, const bool returnfitresult, const std::string& outbltable, const bool outbltableexists, const bool overwrite)
|
---|
2647 | {
|
---|
2648 | int nRowBl = blInfoList.size();
|
---|
2649 | int nRowSt = nrow();
|
---|
2650 |
|
---|
2651 | std::vector<std::string> res;
|
---|
2652 | res.clear();
|
---|
2653 |
|
---|
2654 | bool outBaselineTable = ((outbltable != "") && (!outbltableexists || overwrite));
|
---|
2655 | if ((outbltable != "") && outbltableexists && !overwrite) {
|
---|
2656 | throw(AipsError("Cannot overwrite bltable. Set overwrite=True."));
|
---|
2657 | }
|
---|
2658 |
|
---|
2659 | STBaselineTable* btp;
|
---|
2660 | ROScalarColumn<Double> tcol = ROScalarColumn<Double>(table_, "TIME");
|
---|
2661 | Vector<Double> timeSecCol = tcol.getColumn();
|
---|
2662 |
|
---|
2663 | if (outBaselineTable) {
|
---|
2664 | if (outbltableexists) {
|
---|
2665 | btp = new STBaselineTable((String)outbltable);
|
---|
2666 | } else {
|
---|
2667 | btp = new STBaselineTable(*this);
|
---|
2668 | // for (int i = 0; i < nRowSt; ++i) {
|
---|
2669 | // btp->appendbasedata(getScan(i), getCycle(i), getBeam(i), getIF(i), getPol(i),
|
---|
2670 | // 0, timeSecCol[i]);
|
---|
2671 | // btp->setApply(i, false);
|
---|
2672 | // }
|
---|
2673 | }
|
---|
2674 | int nrow = btp->nrow();
|
---|
2675 | for (int i = nrow; i < nRowSt; ++i) {
|
---|
2676 | btp->appendbasedata(getScan(i), getCycle(i), getBeam(i), getIF(i), getPol(i),
|
---|
2677 | 0, timeSecCol[i]);
|
---|
2678 | btp->setApply(i, false);
|
---|
2679 | }
|
---|
2680 | }
|
---|
2681 |
|
---|
2682 | for (int i = 0; i < nRowBl; ++i) {
|
---|
2683 | int irow;
|
---|
2684 | STBaselineFunc::FuncName ftype;
|
---|
2685 | std::vector<bool> mask;
|
---|
2686 | std::vector<int> fpar;
|
---|
2687 | float clipth;
|
---|
2688 | int clipn;
|
---|
2689 | bool uself;
|
---|
2690 | float lfth;
|
---|
2691 | std::vector<int> lfedge;
|
---|
2692 | int lfavg;
|
---|
2693 | parseBlInfo(blInfoList[i], irow, ftype, fpar, mask, clipth, clipn, uself, lfth, lfedge, lfavg);
|
---|
2694 |
|
---|
2695 | if (irow < nRowSt) {
|
---|
2696 | std::vector<float> spec = getSpectrum(irow);
|
---|
2697 | std::vector<float> params;
|
---|
2698 | float rms;
|
---|
2699 | std::vector<bool> finalmask;
|
---|
2700 | Bool doApply = True;
|
---|
2701 |
|
---|
2702 | if (!isAllChannelsFlagged(irow)) {
|
---|
2703 | std::vector<float> resfit = doSubtractBaseline(spec, mask, ftype, fpar, params, rms, finalmask, clipth, clipn, uself, irow, lfth, lfedge, lfavg);
|
---|
2704 | setSpectrum(resfit, irow);
|
---|
2705 | }
|
---|
2706 | else {
|
---|
2707 | doApply = False;
|
---|
2708 | }
|
---|
2709 |
|
---|
2710 | if (returnfitresult) {
|
---|
2711 | res.push_back(packFittingResults(irow, params, rms));
|
---|
2712 | }
|
---|
2713 |
|
---|
2714 | if (outBaselineTable) {
|
---|
2715 | Vector<Int> fparam(fpar.size());
|
---|
2716 | for (uInt j = 0; j < fparam.size(); ++j) {
|
---|
2717 | fparam[j] = (Int)fpar[j];
|
---|
2718 | }
|
---|
2719 |
|
---|
2720 | btp->setdata(uInt(irow),
|
---|
2721 | uInt(getScan(irow)), uInt(getCycle(irow)),
|
---|
2722 | uInt(getBeam(irow)), uInt(getIF(irow)), uInt(getPol(irow)),
|
---|
2723 | uInt(0), timeSecCol[irow], doApply, ftype, fparam,
|
---|
2724 | Vector<Float>(), getMaskListFromMask(finalmask), Vector<Float>(params),
|
---|
2725 | Float(rms), uInt(spec.size()), Float(clipth), uInt(clipn),
|
---|
2726 | Float(0.0), uInt(0), Vector<uInt>());
|
---|
2727 | }
|
---|
2728 |
|
---|
2729 | }
|
---|
2730 | }
|
---|
2731 |
|
---|
2732 | if (outBaselineTable) {
|
---|
2733 | btp->save(outbltable);
|
---|
2734 | }
|
---|
2735 |
|
---|
2736 | delete btp;
|
---|
2737 | return res;
|
---|
2738 | }
|
---|
2739 |
|
---|
2740 | std::vector<float> Scantable::doApplyBaselineTable(std::vector<float>& spec,
|
---|
2741 | std::vector<bool>& mask,
|
---|
2742 | const STBaselineFunc::FuncName ftype,
|
---|
2743 | std::vector<int>& fpar,
|
---|
2744 | std::vector<float>& params,
|
---|
2745 | float&rms)
|
---|
2746 | {
|
---|
2747 | std::vector<bool> finalmask;
|
---|
2748 | std::vector<int> lfedge;
|
---|
2749 | return doSubtractBaseline(spec, mask, ftype, fpar, params, rms, finalmask, 0.0, 0, false, 0, 0.0, lfedge, 0);
|
---|
2750 | }
|
---|
2751 |
|
---|
2752 | std::vector<float> Scantable::doSubtractBaseline(std::vector<float>& spec,
|
---|
2753 | std::vector<bool>& mask,
|
---|
2754 | const STBaselineFunc::FuncName ftype,
|
---|
2755 | std::vector<int>& fpar,
|
---|
2756 | std::vector<float>& params,
|
---|
2757 | float&rms,
|
---|
2758 | std::vector<bool>& finalmask,
|
---|
2759 | float clipth,
|
---|
2760 | int clipn,
|
---|
2761 | bool uself,
|
---|
2762 | int irow,
|
---|
2763 | float lfth,
|
---|
2764 | std::vector<int>& lfedge,
|
---|
2765 | int lfavg)
|
---|
2766 | {
|
---|
2767 | if (uself) {
|
---|
2768 | STLineFinder lineFinder = STLineFinder();
|
---|
2769 | initLineFinder(lfedge, lfth, lfavg, lineFinder);
|
---|
2770 | std::vector<int> currentEdge;
|
---|
2771 | mask = getCompositeChanMask(irow, mask, lfedge, currentEdge, lineFinder);
|
---|
2772 | } else {
|
---|
2773 | mask = getCompositeChanMask(irow, mask);
|
---|
2774 | }
|
---|
2775 |
|
---|
2776 | std::vector<float> res;
|
---|
2777 | if (ftype == STBaselineFunc::Polynomial) {
|
---|
2778 | res = doPolynomialFitting(spec, mask, fpar[0], params, rms, finalmask, clipth, clipn);
|
---|
2779 | } else if (ftype == STBaselineFunc::Chebyshev) {
|
---|
2780 | res = doChebyshevFitting(spec, mask, fpar[0], params, rms, finalmask, clipth, clipn);
|
---|
2781 | } else if (ftype == STBaselineFunc::CSpline) {
|
---|
2782 | if (fpar.size() > 1) { // reading from baseline table in which pieceEdges are already calculated and stored.
|
---|
2783 | res = doCubicSplineFitting(spec, mask, fpar, params, rms, finalmask, clipth, clipn);
|
---|
2784 | } else { // usual cspline fitting by giving nPiece only. fpar will be replaced with pieceEdges.
|
---|
2785 | res = doCubicSplineFitting(spec, mask, fpar[0], fpar, params, rms, finalmask, clipth, clipn);
|
---|
2786 | }
|
---|
2787 | } else if (ftype == STBaselineFunc::Sinusoid) {
|
---|
2788 | res = doSinusoidFitting(spec, mask, fpar, params, rms, finalmask, clipth, clipn);
|
---|
2789 | }
|
---|
2790 |
|
---|
2791 | return res;
|
---|
2792 | }
|
---|
2793 |
|
---|
2794 | std::string Scantable::packFittingResults(const int irow, const std::vector<float>& params, const float rms)
|
---|
2795 | {
|
---|
2796 | // returned value: "irow:params[0],params[1],..,params[n-1]:rms"
|
---|
2797 | ostringstream os;
|
---|
2798 | os << irow << ':';
|
---|
2799 | for (uInt i = 0; i < params.size(); ++i) {
|
---|
2800 | if (i > 0) {
|
---|
2801 | os << ',';
|
---|
2802 | }
|
---|
2803 | os << params[i];
|
---|
2804 | }
|
---|
2805 | os << ':' << rms;
|
---|
2806 |
|
---|
2807 | return os.str();
|
---|
2808 | }
|
---|
2809 |
|
---|
2810 | void Scantable::parseBlInfo(const std::string& blInfo, int& irow, STBaselineFunc::FuncName& ftype, std::vector<int>& fpar, std::vector<bool>& mask, float& thresClip, int& nIterClip, bool& useLineFinder, float& thresLF, std::vector<int>& edgeLF, int& avgLF)
|
---|
2811 | {
|
---|
2812 | // The baseline info to be parsed must be column-delimited string like
|
---|
2813 | // "0:chebyshev:5:3,5,169,174,485,487" where the elements are
|
---|
2814 | // row number, funcType, funcOrder, maskList, clipThreshold, clipNIter,
|
---|
2815 | // useLineFinder, lfThreshold, lfEdge and lfChanAvgLimit.
|
---|
2816 |
|
---|
2817 | std::vector<string> res = splitToStringList(blInfo, ':');
|
---|
2818 | if (res.size() < 4) {
|
---|
2819 | throw(AipsError("baseline info has bad format")) ;
|
---|
2820 | }
|
---|
2821 |
|
---|
2822 | string ftype0, fpar0, masklist0, uself0, edge0;
|
---|
2823 | std::vector<int> masklist;
|
---|
2824 |
|
---|
2825 | stringstream ss;
|
---|
2826 | ss << res[0];
|
---|
2827 | ss >> irow;
|
---|
2828 | ss.clear(); ss.str("");
|
---|
2829 |
|
---|
2830 | ss << res[1];
|
---|
2831 | ss >> ftype0;
|
---|
2832 | if (ftype0 == "poly") {
|
---|
2833 | ftype = STBaselineFunc::Polynomial;
|
---|
2834 | } else if (ftype0 == "cspline") {
|
---|
2835 | ftype = STBaselineFunc::CSpline;
|
---|
2836 | } else if (ftype0 == "sinusoid") {
|
---|
2837 | ftype = STBaselineFunc::Sinusoid;
|
---|
2838 | } else if (ftype0 == "chebyshev") {
|
---|
2839 | ftype = STBaselineFunc::Chebyshev;
|
---|
2840 | } else {
|
---|
2841 | throw(AipsError("invalid function type."));
|
---|
2842 | }
|
---|
2843 | ss.clear(); ss.str("");
|
---|
2844 |
|
---|
2845 | ss << res[2];
|
---|
2846 | ss >> fpar0;
|
---|
2847 | fpar = splitToIntList(fpar0, ',');
|
---|
2848 | ss.clear(); ss.str("");
|
---|
2849 |
|
---|
2850 | ss << res[3];
|
---|
2851 | ss >> masklist0;
|
---|
2852 | mask = getMaskFromMaskList(nchan(getIF(irow)), splitToIntList(masklist0, ','));
|
---|
2853 | ss.clear(); ss.str("");
|
---|
2854 |
|
---|
2855 | ss << res[4];
|
---|
2856 | ss >> thresClip;
|
---|
2857 | ss.clear(); ss.str("");
|
---|
2858 |
|
---|
2859 | ss << res[5];
|
---|
2860 | ss >> nIterClip;
|
---|
2861 | ss.clear(); ss.str("");
|
---|
2862 |
|
---|
2863 | ss << res[6];
|
---|
2864 | ss >> uself0;
|
---|
2865 | if (uself0 == "true") {
|
---|
2866 | useLineFinder = true;
|
---|
2867 | } else {
|
---|
2868 | useLineFinder = false;
|
---|
2869 | }
|
---|
2870 | ss.clear(); ss.str("");
|
---|
2871 |
|
---|
2872 | if (useLineFinder) {
|
---|
2873 | ss << res[7];
|
---|
2874 | ss >> thresLF;
|
---|
2875 | ss.clear(); ss.str("");
|
---|
2876 |
|
---|
2877 | ss << res[8];
|
---|
2878 | ss >> edge0;
|
---|
2879 | edgeLF = splitToIntList(edge0, ',');
|
---|
2880 | ss.clear(); ss.str("");
|
---|
2881 |
|
---|
2882 | ss << res[9];
|
---|
2883 | ss >> avgLF;
|
---|
2884 | ss.clear(); ss.str("");
|
---|
2885 | }
|
---|
2886 |
|
---|
2887 | }
|
---|
2888 |
|
---|
2889 | std::vector<int> Scantable::splitToIntList(const std::string& s, const char delim)
|
---|
2890 | {
|
---|
2891 | istringstream iss(s);
|
---|
2892 | string tmp;
|
---|
2893 | int tmpi;
|
---|
2894 | std::vector<int> res;
|
---|
2895 | stringstream ss;
|
---|
2896 | while (getline(iss, tmp, delim)) {
|
---|
2897 | ss << tmp;
|
---|
2898 | ss >> tmpi;
|
---|
2899 | res.push_back(tmpi);
|
---|
2900 | ss.clear(); ss.str("");
|
---|
2901 | }
|
---|
2902 |
|
---|
2903 | return res;
|
---|
2904 | }
|
---|
2905 |
|
---|
2906 | std::vector<string> Scantable::splitToStringList(const std::string& s, const char delim)
|
---|
2907 | {
|
---|
2908 | istringstream iss(s);
|
---|
2909 | std::string tmp;
|
---|
2910 | std::vector<string> res;
|
---|
2911 | while (getline(iss, tmp, delim)) {
|
---|
2912 | res.push_back(tmp);
|
---|
2913 | }
|
---|
2914 |
|
---|
2915 | return res;
|
---|
2916 | }
|
---|
2917 |
|
---|
2918 | std::vector<bool> Scantable::getMaskFromMaskList(const int nchan, const std::vector<int>& masklist)
|
---|
2919 | {
|
---|
2920 | if (masklist.size() % 2 != 0) {
|
---|
2921 | throw(AipsError("masklist must have even number of elements."));
|
---|
2922 | }
|
---|
2923 |
|
---|
2924 | std::vector<bool> res(nchan);
|
---|
2925 |
|
---|
2926 | for (int i = 0; i < nchan; ++i) {
|
---|
2927 | res[i] = false;
|
---|
2928 | }
|
---|
2929 | for (uInt j = 0; j < masklist.size(); j += 2) {
|
---|
2930 | for (int i = masklist[j]; i <= min(nchan-1, masklist[j+1]); ++i) {
|
---|
2931 | res[i] = true;
|
---|
2932 | }
|
---|
2933 | }
|
---|
2934 |
|
---|
2935 | return res;
|
---|
2936 | }
|
---|
2937 |
|
---|
2938 | Vector<uInt> Scantable::getMaskListFromMask(const std::vector<bool>& mask)
|
---|
2939 | {
|
---|
2940 | std::vector<int> masklist;
|
---|
2941 | masklist.clear();
|
---|
2942 |
|
---|
2943 | for (uInt i = 0; i < mask.size(); ++i) {
|
---|
2944 | if (mask[i]) {
|
---|
2945 | if ((i == 0)||(i == mask.size()-1)) {
|
---|
2946 | masklist.push_back(i);
|
---|
2947 | } else {
|
---|
2948 | if ((mask[i])&&(!mask[i-1])) {
|
---|
2949 | masklist.push_back(i);
|
---|
2950 | }
|
---|
2951 | if ((mask[i])&&(!mask[i+1])) {
|
---|
2952 | masklist.push_back(i);
|
---|
2953 | }
|
---|
2954 | }
|
---|
2955 | }
|
---|
2956 | }
|
---|
2957 |
|
---|
2958 | Vector<uInt> res(masklist.size());
|
---|
2959 | for (uInt i = 0; i < masklist.size(); ++i) {
|
---|
2960 | res[i] = (uInt)masklist[i];
|
---|
2961 | }
|
---|
2962 |
|
---|
2963 | return res;
|
---|
2964 | }
|
---|
2965 |
|
---|
2966 | void Scantable::initialiseBaselining(const std::string& blfile,
|
---|
2967 | ofstream& ofs,
|
---|
2968 | const bool outLogger,
|
---|
2969 | bool& outTextFile,
|
---|
2970 | bool& csvFormat,
|
---|
2971 | String& coordInfo,
|
---|
2972 | bool& hasSameNchan,
|
---|
2973 | const std::string& progressInfo,
|
---|
2974 | bool& showProgress,
|
---|
2975 | int& minNRow,
|
---|
2976 | Vector<Double>& timeSecCol)
|
---|
2977 | {
|
---|
2978 | csvFormat = false;
|
---|
2979 | outTextFile = false;
|
---|
2980 |
|
---|
2981 | if (blfile != "") {
|
---|
2982 | csvFormat = (blfile.substr(0, 1) == "T");
|
---|
2983 | ofs.open(blfile.substr(1).c_str(), ios::out | ios::app);
|
---|
2984 | if (ofs) outTextFile = true;
|
---|
2985 | }
|
---|
2986 |
|
---|
2987 | coordInfo = "";
|
---|
2988 | hasSameNchan = true;
|
---|
2989 |
|
---|
2990 | if (outLogger || outTextFile) {
|
---|
2991 | coordInfo = getCoordInfo()[0];
|
---|
2992 | if (coordInfo == "") coordInfo = "channel";
|
---|
2993 | hasSameNchan = hasSameNchanOverIFs();
|
---|
2994 | }
|
---|
2995 |
|
---|
2996 | parseProgressInfo(progressInfo, showProgress, minNRow);
|
---|
2997 |
|
---|
2998 | ROScalarColumn<Double> tcol = ROScalarColumn<Double>(table_, "TIME");
|
---|
2999 | timeSecCol = tcol.getColumn();
|
---|
3000 | }
|
---|
3001 |
|
---|
3002 | void Scantable::finaliseBaselining(const bool outBaselineTable,
|
---|
3003 | STBaselineTable* pbt,
|
---|
3004 | const string& bltable,
|
---|
3005 | const bool outTextFile,
|
---|
3006 | ofstream& ofs)
|
---|
3007 | {
|
---|
3008 | if (outBaselineTable) {
|
---|
3009 | pbt->save(bltable);
|
---|
3010 | }
|
---|
3011 |
|
---|
3012 | if (outTextFile) ofs.close();
|
---|
3013 | }
|
---|
3014 |
|
---|
3015 | void Scantable::initLineFinder(const std::vector<int>& edge,
|
---|
3016 | const float threshold,
|
---|
3017 | const int chanAvgLimit,
|
---|
3018 | STLineFinder& lineFinder)
|
---|
3019 | {
|
---|
3020 | if ((edge.size() > 2) && (edge.size() < getIFNos().size()*2)) {
|
---|
3021 | throw(AipsError("Length of edge element info is less than that of IFs"));
|
---|
3022 | }
|
---|
3023 |
|
---|
3024 | lineFinder.setOptions(threshold, 3, chanAvgLimit);
|
---|
3025 | }
|
---|
3026 |
|
---|
3027 | void Scantable::polyBaseline(const std::vector<bool>& mask, int order,
|
---|
3028 | float thresClip, int nIterClip,
|
---|
3029 | bool getResidual,
|
---|
3030 | const std::string& progressInfo,
|
---|
3031 | const bool outLogger, const std::string& blfile,
|
---|
3032 | const std::string& bltable)
|
---|
3033 | {
|
---|
3034 | /****
|
---|
3035 | double TimeStart = mathutil::gettimeofday_sec();
|
---|
3036 | ****/
|
---|
3037 |
|
---|
3038 | try {
|
---|
3039 | ofstream ofs;
|
---|
3040 | String coordInfo;
|
---|
3041 | bool hasSameNchan, outTextFile, csvFormat, showProgress;
|
---|
3042 | int minNRow;
|
---|
3043 | int nRow = nrow();
|
---|
3044 | std::vector<bool> chanMask, finalChanMask;
|
---|
3045 | float rms;
|
---|
3046 | bool outBaselineTable = (bltable != "");
|
---|
3047 | STBaselineTable bt = STBaselineTable(*this);
|
---|
3048 | Vector<Double> timeSecCol;
|
---|
3049 | size_t flagged=0;
|
---|
3050 |
|
---|
3051 | initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
|
---|
3052 | coordInfo, hasSameNchan,
|
---|
3053 | progressInfo, showProgress, minNRow,
|
---|
3054 | timeSecCol);
|
---|
3055 |
|
---|
3056 | std::vector<int> nChanNos;
|
---|
3057 | std::vector<std::vector<std::vector<double> > > modelReservoir;
|
---|
3058 | modelReservoir = getPolynomialModelReservoir(order,
|
---|
3059 | &Scantable::getNormalPolynomial,
|
---|
3060 | nChanNos);
|
---|
3061 | int nModel = modelReservoir.size();
|
---|
3062 |
|
---|
3063 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
3064 | std::vector<float> sp = getSpectrum(whichrow);
|
---|
3065 | chanMask = getCompositeChanMask(whichrow, mask);
|
---|
3066 | std::vector<float> params;
|
---|
3067 |
|
---|
3068 | //if (flagrowCol_(whichrow) == 0) {
|
---|
3069 | if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
|
---|
3070 | int nClipped = 0;
|
---|
3071 | std::vector<float> res;
|
---|
3072 | res = doLeastSquareFitting(sp, chanMask,
|
---|
3073 | modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
|
---|
3074 | params, rms, finalChanMask,
|
---|
3075 | nClipped, thresClip, nIterClip, getResidual);
|
---|
3076 |
|
---|
3077 | if (outBaselineTable) {
|
---|
3078 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
3079 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
3080 | true, STBaselineFunc::Polynomial, order, std::vector<float>(),
|
---|
3081 | getMaskListFromMask(finalChanMask), params, rms, sp.size(),
|
---|
3082 | thresClip, nIterClip, 0.0, 0, std::vector<int>());
|
---|
3083 | } else {
|
---|
3084 | setSpectrum(res, whichrow);
|
---|
3085 | }
|
---|
3086 |
|
---|
3087 | outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
|
---|
3088 | coordInfo, hasSameNchan, ofs, "polyBaseline()",
|
---|
3089 | params, nClipped);
|
---|
3090 | } else {
|
---|
3091 | // no valid channels to fit (flag the row)
|
---|
3092 | flagrowCol_.put(whichrow, 1);
|
---|
3093 | ++flagged;
|
---|
3094 | if (outBaselineTable) {
|
---|
3095 | params.resize(nModel);
|
---|
3096 | for (uInt i = 0; i < params.size(); ++i) {
|
---|
3097 | params[i] = 0.0;
|
---|
3098 | }
|
---|
3099 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
3100 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
3101 | true, STBaselineFunc::Polynomial, order, std::vector<float>(),
|
---|
3102 | getMaskListFromMask(chanMask), params, 0.0, sp.size(),
|
---|
3103 | thresClip, nIterClip, 0.0, 0, std::vector<int>());
|
---|
3104 | }
|
---|
3105 | }
|
---|
3106 |
|
---|
3107 | showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
|
---|
3108 | }
|
---|
3109 |
|
---|
3110 | finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
|
---|
3111 | if (flagged > 0) {
|
---|
3112 | LogIO os( LogOrigin( "Scantable", "polyBaseline()") ) ;
|
---|
3113 | os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
|
---|
3114 | }
|
---|
3115 | } catch (...) {
|
---|
3116 | throw;
|
---|
3117 | }
|
---|
3118 |
|
---|
3119 | /****
|
---|
3120 | double TimeEnd = mathutil::gettimeofday_sec();
|
---|
3121 | double elapse1 = TimeEnd - TimeStart;
|
---|
3122 | std::cout << "poly-new : " << elapse1 << " (sec.)" << endl;
|
---|
3123 | ****/
|
---|
3124 | }
|
---|
3125 |
|
---|
3126 | void Scantable::autoPolyBaseline(const std::vector<bool>& mask, int order,
|
---|
3127 | float thresClip, int nIterClip,
|
---|
3128 | const std::vector<int>& edge,
|
---|
3129 | float threshold, int chanAvgLimit,
|
---|
3130 | bool getResidual,
|
---|
3131 | const std::string& progressInfo,
|
---|
3132 | const bool outLogger, const std::string& blfile,
|
---|
3133 | const std::string& bltable)
|
---|
3134 | {
|
---|
3135 | try {
|
---|
3136 | ofstream ofs;
|
---|
3137 | String coordInfo;
|
---|
3138 | bool hasSameNchan, outTextFile, csvFormat, showProgress;
|
---|
3139 | int minNRow;
|
---|
3140 | int nRow = nrow();
|
---|
3141 | std::vector<bool> chanMask, finalChanMask;
|
---|
3142 | float rms;
|
---|
3143 | bool outBaselineTable = (bltable != "");
|
---|
3144 | STBaselineTable bt = STBaselineTable(*this);
|
---|
3145 | Vector<Double> timeSecCol;
|
---|
3146 | STLineFinder lineFinder = STLineFinder();
|
---|
3147 | size_t flagged=0;
|
---|
3148 |
|
---|
3149 | initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
|
---|
3150 | coordInfo, hasSameNchan,
|
---|
3151 | progressInfo, showProgress, minNRow,
|
---|
3152 | timeSecCol);
|
---|
3153 |
|
---|
3154 | initLineFinder(edge, threshold, chanAvgLimit, lineFinder);
|
---|
3155 |
|
---|
3156 | std::vector<int> nChanNos;
|
---|
3157 | std::vector<std::vector<std::vector<double> > > modelReservoir;
|
---|
3158 | modelReservoir = getPolynomialModelReservoir(order,
|
---|
3159 | &Scantable::getNormalPolynomial,
|
---|
3160 | nChanNos);
|
---|
3161 | int nModel = modelReservoir.size();
|
---|
3162 |
|
---|
3163 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
3164 | std::vector<float> sp = getSpectrum(whichrow);
|
---|
3165 | std::vector<int> currentEdge;
|
---|
3166 | chanMask = getCompositeChanMask(whichrow, mask, edge, currentEdge, lineFinder);
|
---|
3167 | std::vector<float> params;
|
---|
3168 |
|
---|
3169 | //if (flagrowCol_(whichrow) == 0) {
|
---|
3170 | if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
|
---|
3171 | int nClipped = 0;
|
---|
3172 | std::vector<float> res;
|
---|
3173 | res = doLeastSquareFitting(sp, chanMask,
|
---|
3174 | modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
|
---|
3175 | params, rms, finalChanMask,
|
---|
3176 | nClipped, thresClip, nIterClip, getResidual);
|
---|
3177 |
|
---|
3178 | if (outBaselineTable) {
|
---|
3179 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
3180 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
3181 | true, STBaselineFunc::Polynomial, order, std::vector<float>(),
|
---|
3182 | getMaskListFromMask(finalChanMask), params, rms, sp.size(),
|
---|
3183 | thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
|
---|
3184 | } else {
|
---|
3185 | setSpectrum(res, whichrow);
|
---|
3186 | }
|
---|
3187 |
|
---|
3188 | outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
|
---|
3189 | coordInfo, hasSameNchan, ofs, "autoPolyBaseline()",
|
---|
3190 | params, nClipped);
|
---|
3191 | } else {
|
---|
3192 | // no valid channels to fit (flag the row)
|
---|
3193 | flagrowCol_.put(whichrow, 1);
|
---|
3194 | ++flagged;
|
---|
3195 | if (outBaselineTable) {
|
---|
3196 | params.resize(nModel);
|
---|
3197 | for (uInt i = 0; i < params.size(); ++i) {
|
---|
3198 | params[i] = 0.0;
|
---|
3199 | }
|
---|
3200 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
3201 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
3202 | true, STBaselineFunc::Polynomial, order, std::vector<float>(),
|
---|
3203 | getMaskListFromMask(chanMask), params, 0.0, sp.size(),
|
---|
3204 | thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
|
---|
3205 | }
|
---|
3206 | }
|
---|
3207 |
|
---|
3208 | showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
|
---|
3209 | }
|
---|
3210 |
|
---|
3211 | finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
|
---|
3212 | if (flagged > 0) {
|
---|
3213 | LogIO os( LogOrigin( "Scantable", "autoPolyBaseline()") ) ;
|
---|
3214 | os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
|
---|
3215 | }
|
---|
3216 | } catch (...) {
|
---|
3217 | throw;
|
---|
3218 | }
|
---|
3219 | }
|
---|
3220 |
|
---|
3221 | void Scantable::chebyshevBaseline(const std::vector<bool>& mask, int order,
|
---|
3222 | float thresClip, int nIterClip,
|
---|
3223 | bool getResidual,
|
---|
3224 | const std::string& progressInfo,
|
---|
3225 | const bool outLogger, const std::string& blfile,
|
---|
3226 | const std::string& bltable)
|
---|
3227 | {
|
---|
3228 | /*
|
---|
3229 | double TimeStart = mathutil::gettimeofday_sec();
|
---|
3230 | */
|
---|
3231 |
|
---|
3232 | try {
|
---|
3233 | ofstream ofs;
|
---|
3234 | String coordInfo;
|
---|
3235 | bool hasSameNchan, outTextFile, csvFormat, showProgress;
|
---|
3236 | int minNRow;
|
---|
3237 | int nRow = nrow();
|
---|
3238 | std::vector<bool> chanMask, finalChanMask;
|
---|
3239 | float rms;
|
---|
3240 | bool outBaselineTable = (bltable != "");
|
---|
3241 | STBaselineTable bt = STBaselineTable(*this);
|
---|
3242 | Vector<Double> timeSecCol;
|
---|
3243 | size_t flagged=0;
|
---|
3244 |
|
---|
3245 | initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
|
---|
3246 | coordInfo, hasSameNchan,
|
---|
3247 | progressInfo, showProgress, minNRow,
|
---|
3248 | timeSecCol);
|
---|
3249 |
|
---|
3250 | std::vector<int> nChanNos;
|
---|
3251 | std::vector<std::vector<std::vector<double> > > modelReservoir;
|
---|
3252 | modelReservoir = getPolynomialModelReservoir(order,
|
---|
3253 | &Scantable::getChebyshevPolynomial,
|
---|
3254 | nChanNos);
|
---|
3255 | int nModel = modelReservoir.size();
|
---|
3256 |
|
---|
3257 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
3258 | std::vector<float> sp = getSpectrum(whichrow);
|
---|
3259 | chanMask = getCompositeChanMask(whichrow, mask);
|
---|
3260 | std::vector<float> params;
|
---|
3261 |
|
---|
3262 | // if (flagrowCol_(whichrow) == 0) {
|
---|
3263 | if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
|
---|
3264 | int nClipped = 0;
|
---|
3265 | std::vector<float> res;
|
---|
3266 | res = doLeastSquareFitting(sp, chanMask,
|
---|
3267 | modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
|
---|
3268 | params, rms, finalChanMask,
|
---|
3269 | nClipped, thresClip, nIterClip, getResidual);
|
---|
3270 |
|
---|
3271 | if (outBaselineTable) {
|
---|
3272 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
3273 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
3274 | true, STBaselineFunc::Chebyshev, order, std::vector<float>(),
|
---|
3275 | getMaskListFromMask(finalChanMask), params, rms, sp.size(),
|
---|
3276 | thresClip, nIterClip, 0.0, 0, std::vector<int>());
|
---|
3277 | } else {
|
---|
3278 | setSpectrum(res, whichrow);
|
---|
3279 | }
|
---|
3280 |
|
---|
3281 | outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
|
---|
3282 | coordInfo, hasSameNchan, ofs, "chebyshevBaseline()",
|
---|
3283 | params, nClipped);
|
---|
3284 | } else {
|
---|
3285 | // no valid channels to fit (flag the row)
|
---|
3286 | flagrowCol_.put(whichrow, 1);
|
---|
3287 | ++flagged;
|
---|
3288 | if (outBaselineTable) {
|
---|
3289 | params.resize(nModel);
|
---|
3290 | for (uInt i = 0; i < params.size(); ++i) {
|
---|
3291 | params[i] = 0.0;
|
---|
3292 | }
|
---|
3293 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
3294 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
3295 | true, STBaselineFunc::Chebyshev, order, std::vector<float>(),
|
---|
3296 | getMaskListFromMask(chanMask), params, 0.0, sp.size(),
|
---|
3297 | thresClip, nIterClip, 0.0, 0, std::vector<int>());
|
---|
3298 | }
|
---|
3299 | }
|
---|
3300 |
|
---|
3301 | showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
|
---|
3302 | }
|
---|
3303 |
|
---|
3304 | finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
|
---|
3305 |
|
---|
3306 | if (flagged > 0) {
|
---|
3307 | LogIO os( LogOrigin( "Scantable", "chebyshevBaseline()") ) ;
|
---|
3308 | os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
|
---|
3309 | }
|
---|
3310 | } catch (...) {
|
---|
3311 | throw;
|
---|
3312 | }
|
---|
3313 |
|
---|
3314 | /*
|
---|
3315 | double TimeEnd = mathutil::gettimeofday_sec();
|
---|
3316 | double elapse1 = TimeEnd - TimeStart;
|
---|
3317 | std::cout << "cheby : " << elapse1 << " (sec.)" << endl;
|
---|
3318 | */
|
---|
3319 | }
|
---|
3320 |
|
---|
3321 | void Scantable::autoChebyshevBaseline(const std::vector<bool>& mask, int order,
|
---|
3322 | float thresClip, int nIterClip,
|
---|
3323 | const std::vector<int>& edge,
|
---|
3324 | float threshold, int chanAvgLimit,
|
---|
3325 | bool getResidual,
|
---|
3326 | const std::string& progressInfo,
|
---|
3327 | const bool outLogger, const std::string& blfile,
|
---|
3328 | const std::string& bltable)
|
---|
3329 | {
|
---|
3330 | try {
|
---|
3331 | ofstream ofs;
|
---|
3332 | String coordInfo;
|
---|
3333 | bool hasSameNchan, outTextFile, csvFormat, showProgress;
|
---|
3334 | int minNRow;
|
---|
3335 | int nRow = nrow();
|
---|
3336 | std::vector<bool> chanMask, finalChanMask;
|
---|
3337 | float rms;
|
---|
3338 | bool outBaselineTable = (bltable != "");
|
---|
3339 | STBaselineTable bt = STBaselineTable(*this);
|
---|
3340 | Vector<Double> timeSecCol;
|
---|
3341 | STLineFinder lineFinder = STLineFinder();
|
---|
3342 | size_t flagged=0;
|
---|
3343 |
|
---|
3344 | initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
|
---|
3345 | coordInfo, hasSameNchan,
|
---|
3346 | progressInfo, showProgress, minNRow,
|
---|
3347 | timeSecCol);
|
---|
3348 |
|
---|
3349 | initLineFinder(edge, threshold, chanAvgLimit, lineFinder);
|
---|
3350 |
|
---|
3351 | std::vector<int> nChanNos;
|
---|
3352 | std::vector<std::vector<std::vector<double> > > modelReservoir;
|
---|
3353 | modelReservoir = getPolynomialModelReservoir(order,
|
---|
3354 | &Scantable::getChebyshevPolynomial,
|
---|
3355 | nChanNos);
|
---|
3356 | int nModel = modelReservoir.size();
|
---|
3357 |
|
---|
3358 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
3359 | std::vector<float> sp = getSpectrum(whichrow);
|
---|
3360 | std::vector<int> currentEdge;
|
---|
3361 | chanMask = getCompositeChanMask(whichrow, mask, edge, currentEdge, lineFinder);
|
---|
3362 | std::vector<float> params;
|
---|
3363 |
|
---|
3364 | // if (flagrowCol_(whichrow) == 0) {
|
---|
3365 | if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
|
---|
3366 | int nClipped = 0;
|
---|
3367 | std::vector<float> res;
|
---|
3368 | res = doLeastSquareFitting(sp, chanMask,
|
---|
3369 | modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
|
---|
3370 | params, rms, finalChanMask,
|
---|
3371 | nClipped, thresClip, nIterClip, getResidual);
|
---|
3372 |
|
---|
3373 | if (outBaselineTable) {
|
---|
3374 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
3375 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
3376 | true, STBaselineFunc::Chebyshev, order, std::vector<float>(),
|
---|
3377 | getMaskListFromMask(finalChanMask), params, rms, sp.size(),
|
---|
3378 | thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
|
---|
3379 | } else {
|
---|
3380 | setSpectrum(res, whichrow);
|
---|
3381 | }
|
---|
3382 |
|
---|
3383 | outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
|
---|
3384 | coordInfo, hasSameNchan, ofs, "autoChebyshevBaseline()",
|
---|
3385 | params, nClipped);
|
---|
3386 | } else {
|
---|
3387 | // no valid channels to fit (flag the row)
|
---|
3388 | flagrowCol_.put(whichrow, 1);
|
---|
3389 | ++flagged;
|
---|
3390 | if (outBaselineTable) {
|
---|
3391 | params.resize(nModel);
|
---|
3392 | for (uInt i = 0; i < params.size(); ++i) {
|
---|
3393 | params[i] = 0.0;
|
---|
3394 | }
|
---|
3395 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
3396 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
3397 | true, STBaselineFunc::Chebyshev, order, std::vector<float>(),
|
---|
3398 | getMaskListFromMask(chanMask), params, 0.0, sp.size(),
|
---|
3399 | thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
|
---|
3400 | }
|
---|
3401 | }
|
---|
3402 |
|
---|
3403 | showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
|
---|
3404 | }
|
---|
3405 |
|
---|
3406 | finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
|
---|
3407 |
|
---|
3408 | if (flagged > 0) {
|
---|
3409 | LogIO os( LogOrigin( "Scantable", "autoChebyshevBaseline()") ) ;
|
---|
3410 | os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
|
---|
3411 | }
|
---|
3412 | } catch (...) {
|
---|
3413 | throw;
|
---|
3414 | }
|
---|
3415 | }
|
---|
3416 |
|
---|
3417 | double Scantable::calculateModelSelectionCriteria(const std::string& valname,
|
---|
3418 | const std::string& blfunc,
|
---|
3419 | int order,
|
---|
3420 | const std::vector<bool>& inMask,
|
---|
3421 | int whichrow,
|
---|
3422 | bool useLineFinder,
|
---|
3423 | const std::vector<int>& edge,
|
---|
3424 | float threshold,
|
---|
3425 | int chanAvgLimit)
|
---|
3426 | {
|
---|
3427 | std::vector<float> sp = getSpectrum(whichrow);
|
---|
3428 | std::vector<bool> chanMask;
|
---|
3429 | chanMask.clear();
|
---|
3430 |
|
---|
3431 | if (useLineFinder) {
|
---|
3432 | STLineFinder lineFinder = STLineFinder();
|
---|
3433 | initLineFinder(edge, threshold, chanAvgLimit, lineFinder);
|
---|
3434 | std::vector<int> currentEdge;
|
---|
3435 | chanMask = getCompositeChanMask(whichrow, inMask, edge, currentEdge, lineFinder);
|
---|
3436 | } else {
|
---|
3437 | chanMask = getCompositeChanMask(whichrow, inMask);
|
---|
3438 | }
|
---|
3439 |
|
---|
3440 | return doCalculateModelSelectionCriteria(valname, sp, chanMask, blfunc, order);
|
---|
3441 | }
|
---|
3442 |
|
---|
3443 | double Scantable::doCalculateModelSelectionCriteria(const std::string& valname, const std::vector<float>& spec, const std::vector<bool>& mask, const std::string& blfunc, int order)
|
---|
3444 | {
|
---|
3445 | int nparam;
|
---|
3446 | std::vector<float> params;
|
---|
3447 | std::vector<bool> finalChanMask;
|
---|
3448 | float rms;
|
---|
3449 | int nClipped = 0;
|
---|
3450 | std::vector<float> res;
|
---|
3451 | if (blfunc == "poly") {
|
---|
3452 | nparam = order + 1;
|
---|
3453 | res = doPolynomialFitting(spec, mask, order, params, rms, finalChanMask, nClipped);
|
---|
3454 | } else if (blfunc == "chebyshev") {
|
---|
3455 | nparam = order + 1;
|
---|
3456 | res = doChebyshevFitting(spec, mask, order, params, rms, finalChanMask, nClipped);
|
---|
3457 | } else if (blfunc == "cspline") {
|
---|
3458 | std::vector<int> pieceEdges;//(order+1); //order = npiece
|
---|
3459 | nparam = order + 3;
|
---|
3460 | res = doCubicSplineFitting(spec, mask, order, false, pieceEdges, params, rms, finalChanMask, nClipped);
|
---|
3461 | } else if (blfunc == "sinusoid") {
|
---|
3462 | std::vector<int> nWaves;
|
---|
3463 | nWaves.clear();
|
---|
3464 | for (int i = 0; i <= order; ++i) {
|
---|
3465 | nWaves.push_back(i);
|
---|
3466 | }
|
---|
3467 | nparam = 2*order + 1; // order = nwave
|
---|
3468 | res = doSinusoidFitting(spec, mask, nWaves, params, rms, finalChanMask, nClipped);
|
---|
3469 | } else {
|
---|
3470 | throw(AipsError("blfunc must be poly, chebyshev, cspline or sinusoid."));
|
---|
3471 | }
|
---|
3472 |
|
---|
3473 | double msq = 0.0;
|
---|
3474 | int nusedchan = 0;
|
---|
3475 | int nChan = res.size();
|
---|
3476 | for (int i = 0; i < nChan; ++i) {
|
---|
3477 | if (mask[i]) {
|
---|
3478 | msq += (double)res[i]*(double)res[i];
|
---|
3479 | nusedchan++;
|
---|
3480 | }
|
---|
3481 | }
|
---|
3482 | if (nusedchan == 0) {
|
---|
3483 | throw(AipsError("all channels masked."));
|
---|
3484 | }
|
---|
3485 | msq /= (double)nusedchan;
|
---|
3486 |
|
---|
3487 | nparam++; //add 1 for sigma of Gaussian distribution
|
---|
3488 | const double PI = 6.0 * asin(0.5); // PI (= 3.141592653...)
|
---|
3489 |
|
---|
3490 | if (valname.find("aic") == 0) {
|
---|
3491 | // Original Akaike Information Criterion (AIC)
|
---|
3492 | double aic = nusedchan * (log(2.0 * PI * msq) + 1.0) + 2.0 * nparam;
|
---|
3493 |
|
---|
3494 | // Corrected AIC by Sugiura(1978) (AICc)
|
---|
3495 | if (valname == "aicc") {
|
---|
3496 | if (nusedchan - nparam - 1 <= 0) {
|
---|
3497 | throw(AipsError("channel size is too small to calculate AICc."));
|
---|
3498 | }
|
---|
3499 | aic += 2.0*nparam*(nparam + 1)/(double)(nusedchan - nparam - 1);
|
---|
3500 | }
|
---|
3501 |
|
---|
3502 | return aic;
|
---|
3503 |
|
---|
3504 | } else if (valname == "bic") {
|
---|
3505 | // Bayesian Information Criterion (BIC)
|
---|
3506 | double bic = nusedchan * log(msq) + nparam * log((double)nusedchan);
|
---|
3507 | return bic;
|
---|
3508 |
|
---|
3509 | } else if (valname == "gcv") {
|
---|
3510 | // Generalised Cross Validation
|
---|
3511 | double x = 1.0 - (double)nparam / (double)nusedchan;
|
---|
3512 | double gcv = msq / (x * x);
|
---|
3513 | return gcv;
|
---|
3514 |
|
---|
3515 | } else {
|
---|
3516 | throw(AipsError("valname must be aic, aicc, bic or gcv."));
|
---|
3517 | }
|
---|
3518 | }
|
---|
3519 |
|
---|
3520 | double Scantable::getNormalPolynomial(int n, double x) {
|
---|
3521 | if (n == 0) {
|
---|
3522 | return 1.0;
|
---|
3523 | } else if (n > 0) {
|
---|
3524 | double res = 1.0;
|
---|
3525 | for (int i = 0; i < n; ++i) {
|
---|
3526 | res *= x;
|
---|
3527 | }
|
---|
3528 | return res;
|
---|
3529 | } else {
|
---|
3530 | if (x == 0.0) {
|
---|
3531 | throw(AipsError("infinity result: x=0 given for negative power."));
|
---|
3532 | } else {
|
---|
3533 | return pow(x, (double)n);
|
---|
3534 | }
|
---|
3535 | }
|
---|
3536 | }
|
---|
3537 |
|
---|
3538 | double Scantable::getChebyshevPolynomial(int n, double x) {
|
---|
3539 | if ((x < -1.0)||(x > 1.0)) {
|
---|
3540 | throw(AipsError("out of definition range (-1 <= x <= 1)."));
|
---|
3541 | } else if (x == 1.0) {
|
---|
3542 | return 1.0;
|
---|
3543 | } else if (x == 0.0) {
|
---|
3544 | double res;
|
---|
3545 | if (n%2 == 0) {
|
---|
3546 | if (n%4 == 0) {
|
---|
3547 | res = 1.0;
|
---|
3548 | } else {
|
---|
3549 | res = -1.0;
|
---|
3550 | }
|
---|
3551 | } else {
|
---|
3552 | res = 0.0;
|
---|
3553 | }
|
---|
3554 | return res;
|
---|
3555 | } else if (x == -1.0) {
|
---|
3556 | double res = (n%2 == 0 ? 1.0 : -1.0);
|
---|
3557 | return res;
|
---|
3558 | } else if (n < 0) {
|
---|
3559 | throw(AipsError("the order must be zero or positive."));
|
---|
3560 | } else if (n == 0) {
|
---|
3561 | return 1.0;
|
---|
3562 | } else if (n == 1) {
|
---|
3563 | return x;
|
---|
3564 | } else {
|
---|
3565 | double res[n+1];
|
---|
3566 | for (int i = 0; i < n+1; ++i) {
|
---|
3567 | double res0 = 0.0;
|
---|
3568 | if (i == 0) {
|
---|
3569 | res0 = 1.0;
|
---|
3570 | } else if (i == 1) {
|
---|
3571 | res0 = x;
|
---|
3572 | } else {
|
---|
3573 | res0 = 2.0 * x * res[i-1] - res[i-2];
|
---|
3574 | }
|
---|
3575 | res[i] = res0;
|
---|
3576 | }
|
---|
3577 | return res[n];
|
---|
3578 | }
|
---|
3579 | }
|
---|
3580 |
|
---|
3581 | std::vector<float> Scantable::doPolynomialFitting(const std::vector<float>& data,
|
---|
3582 | const std::vector<bool>& mask,
|
---|
3583 | int order,
|
---|
3584 | std::vector<float>& params,
|
---|
3585 | float& rms,
|
---|
3586 | std::vector<bool>& finalmask,
|
---|
3587 | float clipth,
|
---|
3588 | int clipn)
|
---|
3589 | {
|
---|
3590 | int nClipped = 0;
|
---|
3591 | return doPolynomialFitting(data, mask, order, params, rms, finalmask, nClipped, clipth, clipn);
|
---|
3592 | }
|
---|
3593 |
|
---|
3594 | std::vector<float> Scantable::doPolynomialFitting(const std::vector<float>& data,
|
---|
3595 | const std::vector<bool>& mask,
|
---|
3596 | int order,
|
---|
3597 | std::vector<float>& params,
|
---|
3598 | float& rms,
|
---|
3599 | std::vector<bool>& finalMask,
|
---|
3600 | int& nClipped,
|
---|
3601 | float thresClip,
|
---|
3602 | int nIterClip,
|
---|
3603 | bool getResidual)
|
---|
3604 | {
|
---|
3605 | return doLeastSquareFitting(data, mask,
|
---|
3606 | getPolynomialModel(order, data.size(), &Scantable::getNormalPolynomial),
|
---|
3607 | params, rms, finalMask,
|
---|
3608 | nClipped, thresClip, nIterClip,
|
---|
3609 | getResidual);
|
---|
3610 | }
|
---|
3611 |
|
---|
3612 | std::vector<float> Scantable::doChebyshevFitting(const std::vector<float>& data,
|
---|
3613 | const std::vector<bool>& mask,
|
---|
3614 | int order,
|
---|
3615 | std::vector<float>& params,
|
---|
3616 | float& rms,
|
---|
3617 | std::vector<bool>& finalmask,
|
---|
3618 | float clipth,
|
---|
3619 | int clipn)
|
---|
3620 | {
|
---|
3621 | int nClipped = 0;
|
---|
3622 | return doChebyshevFitting(data, mask, order, params, rms, finalmask, nClipped, clipth, clipn);
|
---|
3623 | }
|
---|
3624 |
|
---|
3625 | std::vector<float> Scantable::doChebyshevFitting(const std::vector<float>& data,
|
---|
3626 | const std::vector<bool>& mask,
|
---|
3627 | int order,
|
---|
3628 | std::vector<float>& params,
|
---|
3629 | float& rms,
|
---|
3630 | std::vector<bool>& finalMask,
|
---|
3631 | int& nClipped,
|
---|
3632 | float thresClip,
|
---|
3633 | int nIterClip,
|
---|
3634 | bool getResidual)
|
---|
3635 | {
|
---|
3636 | return doLeastSquareFitting(data, mask,
|
---|
3637 | getPolynomialModel(order, data.size(), &Scantable::getChebyshevPolynomial),
|
---|
3638 | params, rms, finalMask,
|
---|
3639 | nClipped, thresClip, nIterClip,
|
---|
3640 | getResidual);
|
---|
3641 | }
|
---|
3642 |
|
---|
3643 | std::vector<std::vector<double> > Scantable::getPolynomialModel(int order, int nchan, double (Scantable::*pfunc)(int, double))
|
---|
3644 | {
|
---|
3645 | // model : contains model values for computing the least-square matrix.
|
---|
3646 | // model.size() is nmodel and model[*].size() is nchan.
|
---|
3647 | // Each model element are as follows:
|
---|
3648 | //
|
---|
3649 | // (for normal polynomials)
|
---|
3650 | // model[0] = {1.0, 1.0, 1.0, ..., 1.0},
|
---|
3651 | // model[1] = {0.0, 1.0, 2.0, ..., (nchan-1)}
|
---|
3652 | // model[n-1] = ...,
|
---|
3653 | // model[n] = {0.0^n, 1.0^n, 2.0^n, ..., (nchan-1)^n}
|
---|
3654 | // where (0 <= n <= order)
|
---|
3655 | //
|
---|
3656 | // (for Chebyshev polynomials)
|
---|
3657 | // model[0] = {T0(-1), T0(2/(nchan-1)-1), T0(4/(nchan-1)-1), ..., T0(1)},
|
---|
3658 | // model[n-1] = ...,
|
---|
3659 | // model[n] = {Tn(-1), Tn(2/(nchan-1)-1), Tn(4/(nchan-1)-1), ..., Tn(1)}
|
---|
3660 | // where (0 <= n <= order),
|
---|
3661 |
|
---|
3662 | int nmodel = order + 1;
|
---|
3663 | std::vector<std::vector<double> > model(nmodel, std::vector<double>(nchan));
|
---|
3664 |
|
---|
3665 | double stretch, shift;
|
---|
3666 | if (pfunc == &Scantable::getChebyshevPolynomial) {
|
---|
3667 | stretch = 2.0/(double)(nchan - 1);
|
---|
3668 | shift = -1.0;
|
---|
3669 | } else {
|
---|
3670 | stretch = 1.0;
|
---|
3671 | shift = 0.0;
|
---|
3672 | }
|
---|
3673 |
|
---|
3674 | for (int i = 0; i < nmodel; ++i) {
|
---|
3675 | for (int j = 0; j < nchan; ++j) {
|
---|
3676 | model[i][j] = (this->*pfunc)(i, stretch*(double)j + shift);
|
---|
3677 | }
|
---|
3678 | }
|
---|
3679 |
|
---|
3680 | return model;
|
---|
3681 | }
|
---|
3682 |
|
---|
3683 | std::vector<std::vector<std::vector<double> > > Scantable::getPolynomialModelReservoir(int order,
|
---|
3684 | double (Scantable::*pfunc)(int, double),
|
---|
3685 | std::vector<int>& nChanNos)
|
---|
3686 | {
|
---|
3687 | std::vector<std::vector<std::vector<double> > > res;
|
---|
3688 | res.clear();
|
---|
3689 | nChanNos.clear();
|
---|
3690 |
|
---|
3691 | std::vector<uint> ifNos = getIFNos();
|
---|
3692 | for (uint i = 0; i < ifNos.size(); ++i) {
|
---|
3693 | int currNchan = nchan(ifNos[i]);
|
---|
3694 | bool hasDifferentNchan = (i == 0);
|
---|
3695 | for (uint j = 0; j < i; ++j) {
|
---|
3696 | if (currNchan != nchan(ifNos[j])) {
|
---|
3697 | hasDifferentNchan = true;
|
---|
3698 | break;
|
---|
3699 | }
|
---|
3700 | }
|
---|
3701 | if (hasDifferentNchan) {
|
---|
3702 | res.push_back(getPolynomialModel(order, currNchan, pfunc));
|
---|
3703 | nChanNos.push_back(currNchan);
|
---|
3704 | }
|
---|
3705 | }
|
---|
3706 |
|
---|
3707 | return res;
|
---|
3708 | }
|
---|
3709 |
|
---|
3710 | std::vector<float> Scantable::doLeastSquareFitting(const std::vector<float>& data,
|
---|
3711 | const std::vector<bool>& mask,
|
---|
3712 | const std::vector<std::vector<double> >& model,
|
---|
3713 | std::vector<float>& params,
|
---|
3714 | float& rms,
|
---|
3715 | std::vector<bool>& finalMask,
|
---|
3716 | int& nClipped,
|
---|
3717 | float thresClip,
|
---|
3718 | int nIterClip,
|
---|
3719 | bool getResidual)
|
---|
3720 | {
|
---|
3721 | int nDOF = model.size();
|
---|
3722 | int nChan = data.size();
|
---|
3723 |
|
---|
3724 | if (nDOF == 0) {
|
---|
3725 | throw(AipsError("no model data given"));
|
---|
3726 | }
|
---|
3727 | if (nChan < 2) {
|
---|
3728 | throw(AipsError("data size is too few"));
|
---|
3729 | }
|
---|
3730 | if (nChan != (int)mask.size()) {
|
---|
3731 | throw(AipsError("data and mask sizes are not identical"));
|
---|
3732 | }
|
---|
3733 | for (int i = 0; i < nDOF; ++i) {
|
---|
3734 | if (nChan != (int)model[i].size()) {
|
---|
3735 | throw(AipsError("data and model sizes are not identical"));
|
---|
3736 | }
|
---|
3737 | }
|
---|
3738 |
|
---|
3739 | params.clear();
|
---|
3740 | params.resize(nDOF);
|
---|
3741 |
|
---|
3742 | finalMask.clear();
|
---|
3743 | finalMask.resize(nChan);
|
---|
3744 |
|
---|
3745 | std::vector<int> maskArray(nChan);
|
---|
3746 | int j = 0;
|
---|
3747 | for (int i = 0; i < nChan; ++i) {
|
---|
3748 | maskArray[i] = mask[i] ? 1 : 0;
|
---|
3749 | if (isnan(data[i])) maskArray[i] = 0;
|
---|
3750 | if (isinf(data[i])) maskArray[i] = 0;
|
---|
3751 |
|
---|
3752 | finalMask[i] = (maskArray[i] == 1);
|
---|
3753 | if (finalMask[i]) {
|
---|
3754 | j++;
|
---|
3755 | }
|
---|
3756 |
|
---|
3757 | /*
|
---|
3758 | maskArray[i] = mask[i] ? 1 : 0;
|
---|
3759 | if (mask[i]) {
|
---|
3760 | j++;
|
---|
3761 | }
|
---|
3762 | finalMask[i] = mask[i];
|
---|
3763 | */
|
---|
3764 | }
|
---|
3765 |
|
---|
3766 | int initNData = j;
|
---|
3767 | int nData = initNData;
|
---|
3768 |
|
---|
3769 | std::vector<double> z1(nChan), r1(nChan), residual(nChan);
|
---|
3770 | for (int i = 0; i < nChan; ++i) {
|
---|
3771 | z1[i] = (double)data[i];
|
---|
3772 | r1[i] = 0.0;
|
---|
3773 | residual[i] = 0.0;
|
---|
3774 | }
|
---|
3775 |
|
---|
3776 | for (int nClip = 0; nClip < nIterClip+1; ++nClip) {
|
---|
3777 | // xMatrix : horizontal concatenation of
|
---|
3778 | // the least-sq. matrix (left) and an
|
---|
3779 | // identity matrix (right).
|
---|
3780 | // the right part is used to calculate the inverse matrix of the left part.
|
---|
3781 | double xMatrix[nDOF][2*nDOF];
|
---|
3782 | double zMatrix[nDOF];
|
---|
3783 | for (int i = 0; i < nDOF; ++i) {
|
---|
3784 | for (int j = 0; j < 2*nDOF; ++j) {
|
---|
3785 | xMatrix[i][j] = 0.0;
|
---|
3786 | }
|
---|
3787 | xMatrix[i][nDOF+i] = 1.0;
|
---|
3788 | zMatrix[i] = 0.0;
|
---|
3789 | }
|
---|
3790 |
|
---|
3791 | int nUseData = 0;
|
---|
3792 | for (int k = 0; k < nChan; ++k) {
|
---|
3793 | if (maskArray[k] == 0) continue;
|
---|
3794 |
|
---|
3795 | for (int i = 0; i < nDOF; ++i) {
|
---|
3796 | for (int j = i; j < nDOF; ++j) {
|
---|
3797 | xMatrix[i][j] += model[i][k] * model[j][k];
|
---|
3798 | }
|
---|
3799 | zMatrix[i] += z1[k] * model[i][k];
|
---|
3800 | }
|
---|
3801 |
|
---|
3802 | nUseData++;
|
---|
3803 | }
|
---|
3804 |
|
---|
3805 | if (nUseData < 1) {
|
---|
3806 | throw(AipsError("all channels clipped or masked. can't execute fitting anymore."));
|
---|
3807 | }
|
---|
3808 |
|
---|
3809 | for (int i = 0; i < nDOF; ++i) {
|
---|
3810 | for (int j = 0; j < i; ++j) {
|
---|
3811 | xMatrix[i][j] = xMatrix[j][i];
|
---|
3812 | }
|
---|
3813 | }
|
---|
3814 |
|
---|
3815 | //compute inverse matrix of the left half of xMatrix
|
---|
3816 | std::vector<double> invDiag(nDOF);
|
---|
3817 | for (int i = 0; i < nDOF; ++i) {
|
---|
3818 | invDiag[i] = 1.0 / xMatrix[i][i];
|
---|
3819 | for (int j = 0; j < nDOF; ++j) {
|
---|
3820 | xMatrix[i][j] *= invDiag[i];
|
---|
3821 | }
|
---|
3822 | }
|
---|
3823 |
|
---|
3824 | for (int k = 0; k < nDOF; ++k) {
|
---|
3825 | for (int i = 0; i < nDOF; ++i) {
|
---|
3826 | if (i != k) {
|
---|
3827 | double factor1 = xMatrix[k][k];
|
---|
3828 | double invfactor1 = 1.0 / factor1;
|
---|
3829 | double factor2 = xMatrix[i][k];
|
---|
3830 | for (int j = k; j < 2*nDOF; ++j) {
|
---|
3831 | xMatrix[i][j] *= factor1;
|
---|
3832 | xMatrix[i][j] -= xMatrix[k][j]*factor2;
|
---|
3833 | xMatrix[i][j] *= invfactor1;
|
---|
3834 | }
|
---|
3835 | }
|
---|
3836 | }
|
---|
3837 | double invXDiag = 1.0 / xMatrix[k][k];
|
---|
3838 | for (int j = k; j < 2*nDOF; ++j) {
|
---|
3839 | xMatrix[k][j] *= invXDiag;
|
---|
3840 | }
|
---|
3841 | }
|
---|
3842 |
|
---|
3843 | for (int i = 0; i < nDOF; ++i) {
|
---|
3844 | for (int j = 0; j < nDOF; ++j) {
|
---|
3845 | xMatrix[i][nDOF+j] *= invDiag[j];
|
---|
3846 | }
|
---|
3847 | }
|
---|
3848 | //compute a vector y in which coefficients of the best-fit
|
---|
3849 | //model functions are stored.
|
---|
3850 | //in case of polynomials, y consists of (a0,a1,a2,...)
|
---|
3851 | //where ai is the coefficient of the term x^i.
|
---|
3852 | //in case of sinusoids, y consists of (a0,s1,c1,s2,c2,...)
|
---|
3853 | //where a0 is constant term and s* and c* are of sine
|
---|
3854 | //and cosine functions, respectively.
|
---|
3855 | std::vector<double> y(nDOF);
|
---|
3856 | for (int i = 0; i < nDOF; ++i) {
|
---|
3857 | y[i] = 0.0;
|
---|
3858 | for (int j = 0; j < nDOF; ++j) {
|
---|
3859 | y[i] += xMatrix[i][nDOF+j]*zMatrix[j];
|
---|
3860 | }
|
---|
3861 | params[i] = (float)y[i];
|
---|
3862 | }
|
---|
3863 |
|
---|
3864 | for (int i = 0; i < nChan; ++i) {
|
---|
3865 | r1[i] = y[0];
|
---|
3866 | for (int j = 1; j < nDOF; ++j) {
|
---|
3867 | r1[i] += y[j]*model[j][i];
|
---|
3868 | }
|
---|
3869 | residual[i] = z1[i] - r1[i];
|
---|
3870 | }
|
---|
3871 |
|
---|
3872 | double stdDev = 0.0;
|
---|
3873 | for (int i = 0; i < nChan; ++i) {
|
---|
3874 | if (maskArray[i] == 0) continue;
|
---|
3875 | stdDev += residual[i]*residual[i];
|
---|
3876 | }
|
---|
3877 | stdDev = sqrt(stdDev/(double)nData);
|
---|
3878 | rms = (float)stdDev;
|
---|
3879 |
|
---|
3880 | if ((nClip == nIterClip) || (thresClip <= 0.0)) {
|
---|
3881 | break;
|
---|
3882 | } else {
|
---|
3883 |
|
---|
3884 | double thres = stdDev * thresClip;
|
---|
3885 | int newNData = 0;
|
---|
3886 | for (int i = 0; i < nChan; ++i) {
|
---|
3887 | if (abs(residual[i]) >= thres) {
|
---|
3888 | maskArray[i] = 0;
|
---|
3889 | finalMask[i] = false;
|
---|
3890 | }
|
---|
3891 | if (maskArray[i] > 0) {
|
---|
3892 | newNData++;
|
---|
3893 | }
|
---|
3894 | }
|
---|
3895 | if (newNData == nData) {
|
---|
3896 | break; //no more flag to add. stop iteration.
|
---|
3897 | } else {
|
---|
3898 | nData = newNData;
|
---|
3899 | }
|
---|
3900 |
|
---|
3901 | }
|
---|
3902 | }
|
---|
3903 |
|
---|
3904 | nClipped = initNData - nData;
|
---|
3905 |
|
---|
3906 | std::vector<float> result(nChan);
|
---|
3907 | if (getResidual) {
|
---|
3908 | for (int i = 0; i < nChan; ++i) {
|
---|
3909 | result[i] = (float)residual[i];
|
---|
3910 | }
|
---|
3911 | } else {
|
---|
3912 | for (int i = 0; i < nChan; ++i) {
|
---|
3913 | result[i] = (float)r1[i];
|
---|
3914 | }
|
---|
3915 | }
|
---|
3916 |
|
---|
3917 | return result;
|
---|
3918 | } //xMatrix
|
---|
3919 |
|
---|
3920 | void Scantable::cubicSplineBaseline(const std::vector<bool>& mask, int nPiece,
|
---|
3921 | float thresClip, int nIterClip,
|
---|
3922 | bool getResidual,
|
---|
3923 | const std::string& progressInfo,
|
---|
3924 | const bool outLogger, const std::string& blfile,
|
---|
3925 | const std::string& bltable)
|
---|
3926 | {
|
---|
3927 | /****
|
---|
3928 | double TimeStart = mathutil::gettimeofday_sec();
|
---|
3929 | ****/
|
---|
3930 |
|
---|
3931 | try {
|
---|
3932 | ofstream ofs;
|
---|
3933 | String coordInfo;
|
---|
3934 | bool hasSameNchan, outTextFile, csvFormat, showProgress;
|
---|
3935 | int minNRow;
|
---|
3936 | int nRow = nrow();
|
---|
3937 | std::vector<bool> chanMask, finalChanMask;
|
---|
3938 | float rms;
|
---|
3939 | bool outBaselineTable = (bltable != "");
|
---|
3940 | STBaselineTable bt = STBaselineTable(*this);
|
---|
3941 | Vector<Double> timeSecCol;
|
---|
3942 | size_t flagged=0;
|
---|
3943 |
|
---|
3944 | initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
|
---|
3945 | coordInfo, hasSameNchan,
|
---|
3946 | progressInfo, showProgress, minNRow,
|
---|
3947 | timeSecCol);
|
---|
3948 |
|
---|
3949 | std::vector<int> nChanNos;
|
---|
3950 | std::vector<std::vector<std::vector<double> > > modelReservoir;
|
---|
3951 | modelReservoir = getPolynomialModelReservoir(3,
|
---|
3952 | &Scantable::getNormalPolynomial,
|
---|
3953 | nChanNos);
|
---|
3954 | int nDOF = nPiece + 3;
|
---|
3955 |
|
---|
3956 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
3957 | std::vector<float> sp = getSpectrum(whichrow);
|
---|
3958 | chanMask = getCompositeChanMask(whichrow, mask);
|
---|
3959 | std::vector<int> pieceEdges;
|
---|
3960 | std::vector<float> params;
|
---|
3961 |
|
---|
3962 | //if (flagrowCol_(whichrow) == 0) {
|
---|
3963 | if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
|
---|
3964 | int nClipped = 0;
|
---|
3965 | std::vector<float> res;
|
---|
3966 | res = doCubicSplineLeastSquareFitting(sp, chanMask,
|
---|
3967 | modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
|
---|
3968 | nPiece, false, pieceEdges, params, rms, finalChanMask,
|
---|
3969 | nClipped, thresClip, nIterClip, getResidual);
|
---|
3970 |
|
---|
3971 | if (outBaselineTable) {
|
---|
3972 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
3973 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
3974 | true, STBaselineFunc::CSpline, pieceEdges, std::vector<float>(),
|
---|
3975 | getMaskListFromMask(finalChanMask), params, rms, sp.size(),
|
---|
3976 | thresClip, nIterClip, 0.0, 0, std::vector<int>());
|
---|
3977 | } else {
|
---|
3978 | setSpectrum(res, whichrow);
|
---|
3979 | }
|
---|
3980 |
|
---|
3981 | outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
|
---|
3982 | coordInfo, hasSameNchan, ofs, "cubicSplineBaseline()",
|
---|
3983 | pieceEdges, params, nClipped);
|
---|
3984 | } else {
|
---|
3985 | // no valid channels to fit (flag the row)
|
---|
3986 | flagrowCol_.put(whichrow, 1);
|
---|
3987 | ++flagged;
|
---|
3988 | if (outBaselineTable) {
|
---|
3989 | pieceEdges.resize(nPiece+1);
|
---|
3990 | for (uInt i = 0; i < pieceEdges.size(); ++i) {
|
---|
3991 | pieceEdges[i] = 0;
|
---|
3992 | }
|
---|
3993 | params.resize(nDOF);
|
---|
3994 | for (uInt i = 0; i < params.size(); ++i) {
|
---|
3995 | params[i] = 0.0;
|
---|
3996 | }
|
---|
3997 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
3998 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
3999 | true, STBaselineFunc::CSpline, pieceEdges, std::vector<float>(),
|
---|
4000 | getMaskListFromMask(chanMask), params, 0.0, sp.size(),
|
---|
4001 | thresClip, nIterClip, 0.0, 0, std::vector<int>());
|
---|
4002 | }
|
---|
4003 | }
|
---|
4004 |
|
---|
4005 | showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
|
---|
4006 | }
|
---|
4007 |
|
---|
4008 | finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
|
---|
4009 |
|
---|
4010 | if (flagged > 0) {
|
---|
4011 | LogIO os( LogOrigin( "Scantable", "cubicSplineBaseline()") ) ;
|
---|
4012 | os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
|
---|
4013 | }
|
---|
4014 | } catch (...) {
|
---|
4015 | throw;
|
---|
4016 | }
|
---|
4017 |
|
---|
4018 | /****
|
---|
4019 | double TimeEnd = mathutil::gettimeofday_sec();
|
---|
4020 | double elapse1 = TimeEnd - TimeStart;
|
---|
4021 | std::cout << "cspline-new : " << elapse1 << " (sec.)" << endl;
|
---|
4022 | ****/
|
---|
4023 | }
|
---|
4024 |
|
---|
4025 | void Scantable::autoCubicSplineBaseline(const std::vector<bool>& mask, int nPiece,
|
---|
4026 | float thresClip, int nIterClip,
|
---|
4027 | const std::vector<int>& edge,
|
---|
4028 | float threshold, int chanAvgLimit,
|
---|
4029 | bool getResidual,
|
---|
4030 | const std::string& progressInfo,
|
---|
4031 | const bool outLogger, const std::string& blfile,
|
---|
4032 | const std::string& bltable)
|
---|
4033 | {
|
---|
4034 | try {
|
---|
4035 | ofstream ofs;
|
---|
4036 | String coordInfo;
|
---|
4037 | bool hasSameNchan, outTextFile, csvFormat, showProgress;
|
---|
4038 | int minNRow;
|
---|
4039 | int nRow = nrow();
|
---|
4040 | std::vector<bool> chanMask, finalChanMask;
|
---|
4041 | float rms;
|
---|
4042 | bool outBaselineTable = (bltable != "");
|
---|
4043 | STBaselineTable bt = STBaselineTable(*this);
|
---|
4044 | Vector<Double> timeSecCol;
|
---|
4045 | STLineFinder lineFinder = STLineFinder();
|
---|
4046 | size_t flagged=0;
|
---|
4047 |
|
---|
4048 | initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
|
---|
4049 | coordInfo, hasSameNchan,
|
---|
4050 | progressInfo, showProgress, minNRow,
|
---|
4051 | timeSecCol);
|
---|
4052 |
|
---|
4053 | initLineFinder(edge, threshold, chanAvgLimit, lineFinder);
|
---|
4054 |
|
---|
4055 | std::vector<int> nChanNos;
|
---|
4056 | std::vector<std::vector<std::vector<double> > > modelReservoir;
|
---|
4057 | modelReservoir = getPolynomialModelReservoir(3,
|
---|
4058 | &Scantable::getNormalPolynomial,
|
---|
4059 | nChanNos);
|
---|
4060 | int nDOF = nPiece + 3;
|
---|
4061 |
|
---|
4062 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
4063 | std::vector<float> sp = getSpectrum(whichrow);
|
---|
4064 | std::vector<int> currentEdge;
|
---|
4065 | chanMask = getCompositeChanMask(whichrow, mask, edge, currentEdge, lineFinder);
|
---|
4066 | std::vector<int> pieceEdges;
|
---|
4067 | std::vector<float> params;
|
---|
4068 |
|
---|
4069 | //if (flagrowCol_(whichrow) == 0) {
|
---|
4070 | if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
|
---|
4071 | int nClipped = 0;
|
---|
4072 | std::vector<float> res;
|
---|
4073 | res = doCubicSplineLeastSquareFitting(sp, chanMask,
|
---|
4074 | modelReservoir[getIdxOfNchan(sp.size(), nChanNos)],
|
---|
4075 | nPiece, false, pieceEdges, params, rms, finalChanMask,
|
---|
4076 | nClipped, thresClip, nIterClip, getResidual);
|
---|
4077 |
|
---|
4078 | if (outBaselineTable) {
|
---|
4079 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
4080 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
4081 | true, STBaselineFunc::CSpline, pieceEdges, std::vector<float>(),
|
---|
4082 | getMaskListFromMask(finalChanMask), params, rms, sp.size(),
|
---|
4083 | thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
|
---|
4084 | } else {
|
---|
4085 | setSpectrum(res, whichrow);
|
---|
4086 | }
|
---|
4087 |
|
---|
4088 | outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
|
---|
4089 | coordInfo, hasSameNchan, ofs, "autoCubicSplineBaseline()",
|
---|
4090 | pieceEdges, params, nClipped);
|
---|
4091 | } else {
|
---|
4092 | // no valid channels to fit (flag the row)
|
---|
4093 | flagrowCol_.put(whichrow, 1);
|
---|
4094 | ++flagged;
|
---|
4095 | if (outBaselineTable) {
|
---|
4096 | pieceEdges.resize(nPiece+1);
|
---|
4097 | for (uInt i = 0; i < pieceEdges.size(); ++i) {
|
---|
4098 | pieceEdges[i] = 0;
|
---|
4099 | }
|
---|
4100 | params.resize(nDOF);
|
---|
4101 | for (uInt i = 0; i < params.size(); ++i) {
|
---|
4102 | params[i] = 0.0;
|
---|
4103 | }
|
---|
4104 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
4105 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
4106 | true, STBaselineFunc::CSpline, pieceEdges, std::vector<float>(),
|
---|
4107 | getMaskListFromMask(chanMask), params, 0.0, sp.size(),
|
---|
4108 | thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
|
---|
4109 | }
|
---|
4110 | }
|
---|
4111 |
|
---|
4112 | showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
|
---|
4113 | }
|
---|
4114 |
|
---|
4115 | finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
|
---|
4116 |
|
---|
4117 | if (flagged > 0) {
|
---|
4118 | LogIO os( LogOrigin( "Scantable", "autoCubicSplineBaseline()") ) ;
|
---|
4119 | os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
|
---|
4120 | }
|
---|
4121 | } catch (...) {
|
---|
4122 | throw;
|
---|
4123 | }
|
---|
4124 | }
|
---|
4125 |
|
---|
4126 | std::vector<float> Scantable::doCubicSplineFitting(const std::vector<float>& data,
|
---|
4127 | const std::vector<bool>& mask,
|
---|
4128 | std::vector<int>& idxEdge,
|
---|
4129 | std::vector<float>& params,
|
---|
4130 | float& rms,
|
---|
4131 | std::vector<bool>& finalmask,
|
---|
4132 | float clipth,
|
---|
4133 | int clipn)
|
---|
4134 | {
|
---|
4135 | int nClipped = 0;
|
---|
4136 | return doCubicSplineFitting(data, mask, idxEdge.size()-1, true, idxEdge, params, rms, finalmask, nClipped, clipth, clipn);
|
---|
4137 | }
|
---|
4138 |
|
---|
4139 | std::vector<float> Scantable::doCubicSplineFitting(const std::vector<float>& data,
|
---|
4140 | const std::vector<bool>& mask,
|
---|
4141 | int nPiece,
|
---|
4142 | std::vector<int>& idxEdge,
|
---|
4143 | std::vector<float>& params,
|
---|
4144 | float& rms,
|
---|
4145 | std::vector<bool>& finalmask,
|
---|
4146 | float clipth,
|
---|
4147 | int clipn)
|
---|
4148 | {
|
---|
4149 | int nClipped = 0;
|
---|
4150 | return doCubicSplineFitting(data, mask, nPiece, false, idxEdge, params, rms, finalmask, nClipped, clipth, clipn);
|
---|
4151 | }
|
---|
4152 |
|
---|
4153 | std::vector<float> Scantable::doCubicSplineFitting(const std::vector<float>& data,
|
---|
4154 | const std::vector<bool>& mask,
|
---|
4155 | int nPiece,
|
---|
4156 | bool useGivenPieceBoundary,
|
---|
4157 | std::vector<int>& idxEdge,
|
---|
4158 | std::vector<float>& params,
|
---|
4159 | float& rms,
|
---|
4160 | std::vector<bool>& finalMask,
|
---|
4161 | int& nClipped,
|
---|
4162 | float thresClip,
|
---|
4163 | int nIterClip,
|
---|
4164 | bool getResidual)
|
---|
4165 | {
|
---|
4166 | return doCubicSplineLeastSquareFitting(data, mask,
|
---|
4167 | getPolynomialModel(3, data.size(), &Scantable::getNormalPolynomial),
|
---|
4168 | nPiece, useGivenPieceBoundary, idxEdge,
|
---|
4169 | params, rms, finalMask,
|
---|
4170 | nClipped, thresClip, nIterClip,
|
---|
4171 | getResidual);
|
---|
4172 | }
|
---|
4173 |
|
---|
4174 | std::vector<float> Scantable::doCubicSplineLeastSquareFitting(const std::vector<float>& data,
|
---|
4175 | const std::vector<bool>& mask,
|
---|
4176 | const std::vector<std::vector<double> >& model,
|
---|
4177 | int nPiece,
|
---|
4178 | bool useGivenPieceBoundary,
|
---|
4179 | std::vector<int>& idxEdge,
|
---|
4180 | std::vector<float>& params,
|
---|
4181 | float& rms,
|
---|
4182 | std::vector<bool>& finalMask,
|
---|
4183 | int& nClipped,
|
---|
4184 | float thresClip,
|
---|
4185 | int nIterClip,
|
---|
4186 | bool getResidual)
|
---|
4187 | {
|
---|
4188 | int nDOF = nPiece + 3; //number of independent parameters to solve, namely, 4+(nPiece-1).
|
---|
4189 | int nModel = model.size();
|
---|
4190 | int nChan = data.size();
|
---|
4191 |
|
---|
4192 | if (nModel != 4) {
|
---|
4193 | throw(AipsError("model size must be 4."));
|
---|
4194 | }
|
---|
4195 | if (nPiece < 1) {
|
---|
4196 | throw(AipsError("number of the sections must be one or more"));
|
---|
4197 | }
|
---|
4198 | if (nChan < 2*nPiece) {
|
---|
4199 | throw(AipsError("data size is too few"));
|
---|
4200 | }
|
---|
4201 | if (nChan != (int)mask.size()) {
|
---|
4202 | throw(AipsError("data and mask sizes are not identical"));
|
---|
4203 | }
|
---|
4204 | for (int i = 0; i < nModel; ++i) {
|
---|
4205 | if (nChan != (int)model[i].size()) {
|
---|
4206 | throw(AipsError("data and model sizes are not identical"));
|
---|
4207 | }
|
---|
4208 | }
|
---|
4209 |
|
---|
4210 | params.clear();
|
---|
4211 | params.resize(nPiece*nModel);
|
---|
4212 |
|
---|
4213 | finalMask.clear();
|
---|
4214 | finalMask.resize(nChan);
|
---|
4215 |
|
---|
4216 | std::vector<int> maskArray(nChan);
|
---|
4217 | std::vector<int> x(nChan);
|
---|
4218 | int j = 0;
|
---|
4219 | for (int i = 0; i < nChan; ++i) {
|
---|
4220 | maskArray[i] = mask[i] ? 1 : 0;
|
---|
4221 | if (isnan(data[i])) maskArray[i] = 0;
|
---|
4222 | if (isinf(data[i])) maskArray[i] = 0;
|
---|
4223 |
|
---|
4224 | finalMask[i] = (maskArray[i] == 1);
|
---|
4225 | if (finalMask[i]) {
|
---|
4226 | x[j] = i;
|
---|
4227 | j++;
|
---|
4228 | }
|
---|
4229 |
|
---|
4230 | /*
|
---|
4231 | maskArray[i] = mask[i] ? 1 : 0;
|
---|
4232 | if (mask[i]) {
|
---|
4233 | x[j] = i;
|
---|
4234 | j++;
|
---|
4235 | }
|
---|
4236 | finalMask[i] = mask[i];
|
---|
4237 | */
|
---|
4238 | }
|
---|
4239 |
|
---|
4240 | int initNData = j;
|
---|
4241 | int nData = initNData;
|
---|
4242 |
|
---|
4243 | if (initNData < nPiece) {
|
---|
4244 | throw(AipsError("too few non-flagged channels"));
|
---|
4245 | }
|
---|
4246 |
|
---|
4247 | int nElement = (int)(floor(floor((double)(initNData/nPiece))+0.5));
|
---|
4248 | std::vector<double> invEdge(nPiece-1);
|
---|
4249 |
|
---|
4250 | if (useGivenPieceBoundary) {
|
---|
4251 | if ((int)idxEdge.size() != nPiece+1) {
|
---|
4252 | throw(AipsError("pieceEdge.size() must be equal to nPiece+1."));
|
---|
4253 | }
|
---|
4254 | } else {
|
---|
4255 | idxEdge.clear();
|
---|
4256 | idxEdge.resize(nPiece+1);
|
---|
4257 | idxEdge[0] = x[0];
|
---|
4258 | }
|
---|
4259 | for (int i = 1; i < nPiece; ++i) {
|
---|
4260 | int valX = x[nElement*i];
|
---|
4261 | if (!useGivenPieceBoundary) {
|
---|
4262 | idxEdge[i] = valX;
|
---|
4263 | }
|
---|
4264 | invEdge[i-1] = 1.0/(double)valX;
|
---|
4265 | }
|
---|
4266 | if (!useGivenPieceBoundary) {
|
---|
4267 | idxEdge[nPiece] = x[initNData-1]+1;
|
---|
4268 | }
|
---|
4269 |
|
---|
4270 | std::vector<double> z1(nChan), r1(nChan), residual(nChan);
|
---|
4271 | for (int i = 0; i < nChan; ++i) {
|
---|
4272 | z1[i] = (double)data[i];
|
---|
4273 | r1[i] = 0.0;
|
---|
4274 | residual[i] = 0.0;
|
---|
4275 | }
|
---|
4276 |
|
---|
4277 | for (int nClip = 0; nClip < nIterClip+1; ++nClip) {
|
---|
4278 | // xMatrix : horizontal concatenation of
|
---|
4279 | // the least-sq. matrix (left) and an
|
---|
4280 | // identity matrix (right).
|
---|
4281 | // the right part is used to calculate the inverse matrix of the left part.
|
---|
4282 |
|
---|
4283 | double xMatrix[nDOF][2*nDOF];
|
---|
4284 | double zMatrix[nDOF];
|
---|
4285 | for (int i = 0; i < nDOF; ++i) {
|
---|
4286 | for (int j = 0; j < 2*nDOF; ++j) {
|
---|
4287 | xMatrix[i][j] = 0.0;
|
---|
4288 | }
|
---|
4289 | xMatrix[i][nDOF+i] = 1.0;
|
---|
4290 | zMatrix[i] = 0.0;
|
---|
4291 | }
|
---|
4292 |
|
---|
4293 | for (int n = 0; n < nPiece; ++n) {
|
---|
4294 | int nUseDataInPiece = 0;
|
---|
4295 | for (int k = idxEdge[n]; k < idxEdge[n+1]; ++k) {
|
---|
4296 |
|
---|
4297 | if (maskArray[k] == 0) continue;
|
---|
4298 |
|
---|
4299 | for (int i = 0; i < nModel; ++i) {
|
---|
4300 | for (int j = i; j < nModel; ++j) {
|
---|
4301 | xMatrix[i][j] += model[i][k] * model[j][k];
|
---|
4302 | }
|
---|
4303 | zMatrix[i] += z1[k] * model[i][k];
|
---|
4304 | }
|
---|
4305 |
|
---|
4306 | for (int i = 0; i < n; ++i) {
|
---|
4307 | double q = 1.0 - model[1][k]*invEdge[i];
|
---|
4308 | q = q*q*q;
|
---|
4309 | for (int j = 0; j < nModel; ++j) {
|
---|
4310 | xMatrix[j][i+nModel] += q * model[j][k];
|
---|
4311 | }
|
---|
4312 | for (int j = 0; j < i; ++j) {
|
---|
4313 | double r = 1.0 - model[1][k]*invEdge[j];
|
---|
4314 | r = r*r*r;
|
---|
4315 | xMatrix[j+nModel][i+nModel] += r*q;
|
---|
4316 | }
|
---|
4317 | xMatrix[i+nModel][i+nModel] += q*q;
|
---|
4318 | zMatrix[i+nModel] += q*z1[k];
|
---|
4319 | }
|
---|
4320 |
|
---|
4321 | nUseDataInPiece++;
|
---|
4322 | }
|
---|
4323 |
|
---|
4324 | if (nUseDataInPiece < 1) {
|
---|
4325 | std::vector<string> suffixOfPieceNumber(4);
|
---|
4326 | suffixOfPieceNumber[0] = "th";
|
---|
4327 | suffixOfPieceNumber[1] = "st";
|
---|
4328 | suffixOfPieceNumber[2] = "nd";
|
---|
4329 | suffixOfPieceNumber[3] = "rd";
|
---|
4330 | int idxNoDataPiece = (n % 10 <= 3) ? n : 0;
|
---|
4331 | ostringstream oss;
|
---|
4332 | oss << "all channels clipped or masked in " << n << suffixOfPieceNumber[idxNoDataPiece];
|
---|
4333 | oss << " piece of the spectrum. can't execute fitting anymore.";
|
---|
4334 | throw(AipsError(String(oss)));
|
---|
4335 | }
|
---|
4336 | }
|
---|
4337 |
|
---|
4338 | for (int i = 0; i < nDOF; ++i) {
|
---|
4339 | for (int j = 0; j < i; ++j) {
|
---|
4340 | xMatrix[i][j] = xMatrix[j][i];
|
---|
4341 | }
|
---|
4342 | }
|
---|
4343 |
|
---|
4344 | std::vector<double> invDiag(nDOF);
|
---|
4345 | for (int i = 0; i < nDOF; ++i) {
|
---|
4346 | invDiag[i] = 1.0 / xMatrix[i][i];
|
---|
4347 | for (int j = 0; j < nDOF; ++j) {
|
---|
4348 | xMatrix[i][j] *= invDiag[i];
|
---|
4349 | }
|
---|
4350 | }
|
---|
4351 |
|
---|
4352 | for (int k = 0; k < nDOF; ++k) {
|
---|
4353 | for (int i = 0; i < nDOF; ++i) {
|
---|
4354 | if (i != k) {
|
---|
4355 | double factor1 = xMatrix[k][k];
|
---|
4356 | double invfactor1 = 1.0 / factor1;
|
---|
4357 | double factor2 = xMatrix[i][k];
|
---|
4358 | for (int j = k; j < 2*nDOF; ++j) {
|
---|
4359 | xMatrix[i][j] *= factor1;
|
---|
4360 | xMatrix[i][j] -= xMatrix[k][j]*factor2;
|
---|
4361 | xMatrix[i][j] *= invfactor1;
|
---|
4362 | }
|
---|
4363 | }
|
---|
4364 | }
|
---|
4365 | double invXDiag = 1.0 / xMatrix[k][k];
|
---|
4366 | for (int j = k; j < 2*nDOF; ++j) {
|
---|
4367 | xMatrix[k][j] *= invXDiag;
|
---|
4368 | }
|
---|
4369 | }
|
---|
4370 |
|
---|
4371 | for (int i = 0; i < nDOF; ++i) {
|
---|
4372 | for (int j = 0; j < nDOF; ++j) {
|
---|
4373 | xMatrix[i][nDOF+j] *= invDiag[j];
|
---|
4374 | }
|
---|
4375 | }
|
---|
4376 |
|
---|
4377 | //compute a vector y which consists of the coefficients of the best-fit spline curves
|
---|
4378 | //(a0,a1,a2,a3(,b3,c3,...)), namely, the ones for the leftmost piece and the ones of
|
---|
4379 | //cubic terms for the other pieces (in case nPiece>1).
|
---|
4380 | std::vector<double> y(nDOF);
|
---|
4381 | for (int i = 0; i < nDOF; ++i) {
|
---|
4382 | y[i] = 0.0;
|
---|
4383 | for (int j = 0; j < nDOF; ++j) {
|
---|
4384 | y[i] += xMatrix[i][nDOF+j]*zMatrix[j];
|
---|
4385 | }
|
---|
4386 | }
|
---|
4387 |
|
---|
4388 | std::vector<double> a(nModel);
|
---|
4389 | for (int i = 0; i < nModel; ++i) {
|
---|
4390 | a[i] = y[i];
|
---|
4391 | }
|
---|
4392 |
|
---|
4393 | int j = 0;
|
---|
4394 | for (int n = 0; n < nPiece; ++n) {
|
---|
4395 | for (int i = idxEdge[n]; i < idxEdge[n+1]; ++i) {
|
---|
4396 | r1[i] = 0.0;
|
---|
4397 | for (int j = 0; j < nModel; ++j) {
|
---|
4398 | r1[i] += a[j] * model[j][i];
|
---|
4399 | }
|
---|
4400 | }
|
---|
4401 | for (int i = 0; i < nModel; ++i) {
|
---|
4402 | params[j+i] = a[i];
|
---|
4403 | }
|
---|
4404 | j += nModel;
|
---|
4405 |
|
---|
4406 | if (n == nPiece-1) break;
|
---|
4407 |
|
---|
4408 | double d = y[n+nModel];
|
---|
4409 | double iE = invEdge[n];
|
---|
4410 | a[0] += d;
|
---|
4411 | a[1] -= 3.0 * d * iE;
|
---|
4412 | a[2] += 3.0 * d * iE * iE;
|
---|
4413 | a[3] -= d * iE * iE * iE;
|
---|
4414 | }
|
---|
4415 |
|
---|
4416 | //subtract constant value for masked regions at the edge of spectrum
|
---|
4417 | if (idxEdge[0] > 0) {
|
---|
4418 | int n = idxEdge[0];
|
---|
4419 | for (int i = 0; i < idxEdge[0]; ++i) {
|
---|
4420 | //--cubic extrapolate--
|
---|
4421 | //r1[i] = params[0] + params[1]*x1[i] + params[2]*x2[i] + params[3]*x3[i];
|
---|
4422 | //--linear extrapolate--
|
---|
4423 | //r1[i] = (r1[n+1] - r1[n])/(x1[n+1] - x1[n])*(x1[i] - x1[n]) + r1[n];
|
---|
4424 | //--constant--
|
---|
4425 | r1[i] = r1[n];
|
---|
4426 | }
|
---|
4427 | }
|
---|
4428 |
|
---|
4429 | if (idxEdge[nPiece] < nChan) {
|
---|
4430 | int n = idxEdge[nPiece]-1;
|
---|
4431 | for (int i = idxEdge[nPiece]; i < nChan; ++i) {
|
---|
4432 | //--cubic extrapolate--
|
---|
4433 | //int m = 4*(nPiece-1);
|
---|
4434 | //r1[i] = params[m] + params[m+1]*x1[i] + params[m+2]*x2[i] + params[m+3]*x3[i];
|
---|
4435 | //--linear extrapolate--
|
---|
4436 | //r1[i] = (r1[n-1] - r1[n])/(x1[n-1] - x1[n])*(x1[i] - x1[n]) + r1[n];
|
---|
4437 | //--constant--
|
---|
4438 | r1[i] = r1[n];
|
---|
4439 | }
|
---|
4440 | }
|
---|
4441 |
|
---|
4442 | for (int i = 0; i < nChan; ++i) {
|
---|
4443 | residual[i] = z1[i] - r1[i];
|
---|
4444 | }
|
---|
4445 |
|
---|
4446 | double stdDev = 0.0;
|
---|
4447 | for (int i = 0; i < nChan; ++i) {
|
---|
4448 | if (maskArray[i] == 0) continue;
|
---|
4449 | stdDev += residual[i]*residual[i];
|
---|
4450 | }
|
---|
4451 | stdDev = sqrt(stdDev/(double)nData);
|
---|
4452 | rms = (float)stdDev;
|
---|
4453 |
|
---|
4454 | if ((nClip == nIterClip) || (thresClip <= 0.0)) {
|
---|
4455 | break;
|
---|
4456 | } else {
|
---|
4457 |
|
---|
4458 | double thres = stdDev * thresClip;
|
---|
4459 | int newNData = 0;
|
---|
4460 | for (int i = 0; i < nChan; ++i) {
|
---|
4461 | if (abs(residual[i]) >= thres) {
|
---|
4462 | maskArray[i] = 0;
|
---|
4463 | finalMask[i] = false;
|
---|
4464 | }
|
---|
4465 | if (maskArray[i] > 0) {
|
---|
4466 | newNData++;
|
---|
4467 | }
|
---|
4468 | }
|
---|
4469 | if (newNData == nData) {
|
---|
4470 | break; //no more flag to add. iteration stops.
|
---|
4471 | } else {
|
---|
4472 | nData = newNData;
|
---|
4473 | }
|
---|
4474 |
|
---|
4475 | }
|
---|
4476 | }
|
---|
4477 |
|
---|
4478 | nClipped = initNData - nData;
|
---|
4479 |
|
---|
4480 | std::vector<float> result(nChan);
|
---|
4481 | if (getResidual) {
|
---|
4482 | for (int i = 0; i < nChan; ++i) {
|
---|
4483 | result[i] = (float)residual[i];
|
---|
4484 | }
|
---|
4485 | } else {
|
---|
4486 | for (int i = 0; i < nChan; ++i) {
|
---|
4487 | result[i] = (float)r1[i];
|
---|
4488 | }
|
---|
4489 | }
|
---|
4490 |
|
---|
4491 | return result;
|
---|
4492 | }
|
---|
4493 |
|
---|
4494 | std::vector<int> Scantable::selectWaveNumbers(const std::vector<int>& addNWaves,
|
---|
4495 | const std::vector<int>& rejectNWaves)
|
---|
4496 | {
|
---|
4497 | std::vector<bool> chanMask;
|
---|
4498 | std::string fftMethod;
|
---|
4499 | std::string fftThresh;
|
---|
4500 |
|
---|
4501 | return selectWaveNumbers(0, chanMask, false, fftMethod, fftThresh, addNWaves, rejectNWaves);
|
---|
4502 | }
|
---|
4503 |
|
---|
4504 | std::vector<int> Scantable::selectWaveNumbers(const int whichrow,
|
---|
4505 | const std::vector<bool>& chanMask,
|
---|
4506 | const bool applyFFT,
|
---|
4507 | const std::string& fftMethod,
|
---|
4508 | const std::string& fftThresh,
|
---|
4509 | const std::vector<int>& addNWaves,
|
---|
4510 | const std::vector<int>& rejectNWaves)
|
---|
4511 | {
|
---|
4512 | std::vector<int> nWaves;
|
---|
4513 | nWaves.clear();
|
---|
4514 |
|
---|
4515 | if (applyFFT) {
|
---|
4516 | string fftThAttr;
|
---|
4517 | float fftThSigma;
|
---|
4518 | int fftThTop;
|
---|
4519 | parseFFTThresholdInfo(fftThresh, fftThAttr, fftThSigma, fftThTop);
|
---|
4520 | doSelectWaveNumbers(whichrow, chanMask, fftMethod, fftThSigma, fftThTop, fftThAttr, nWaves);
|
---|
4521 | }
|
---|
4522 |
|
---|
4523 | addAuxWaveNumbers(whichrow, addNWaves, rejectNWaves, nWaves);
|
---|
4524 |
|
---|
4525 | return nWaves;
|
---|
4526 | }
|
---|
4527 |
|
---|
4528 | int Scantable::getIdxOfNchan(const int nChan, const std::vector<int>& nChanNos)
|
---|
4529 | {
|
---|
4530 | int idx = -1;
|
---|
4531 | for (uint i = 0; i < nChanNos.size(); ++i) {
|
---|
4532 | if (nChan == nChanNos[i]) {
|
---|
4533 | idx = i;
|
---|
4534 | break;
|
---|
4535 | }
|
---|
4536 | }
|
---|
4537 |
|
---|
4538 | if (idx < 0) {
|
---|
4539 | throw(AipsError("nChan not found in nChhanNos."));
|
---|
4540 | }
|
---|
4541 |
|
---|
4542 | return idx;
|
---|
4543 | }
|
---|
4544 |
|
---|
4545 | void Scantable::parseFFTInfo(const std::string& fftInfo, bool& applyFFT, std::string& fftMethod, std::string& fftThresh)
|
---|
4546 | {
|
---|
4547 | istringstream iss(fftInfo);
|
---|
4548 | std::string tmp;
|
---|
4549 | std::vector<string> res;
|
---|
4550 | while (getline(iss, tmp, ',')) {
|
---|
4551 | res.push_back(tmp);
|
---|
4552 | }
|
---|
4553 | if (res.size() < 3) {
|
---|
4554 | throw(AipsError("wrong value in 'fftinfo' parameter")) ;
|
---|
4555 | }
|
---|
4556 | applyFFT = (res[0] == "true");
|
---|
4557 | fftMethod = res[1];
|
---|
4558 | fftThresh = res[2];
|
---|
4559 | }
|
---|
4560 |
|
---|
4561 | void Scantable::parseFFTThresholdInfo(const std::string& fftThresh, std::string& fftThAttr, float& fftThSigma, int& fftThTop)
|
---|
4562 | {
|
---|
4563 | uInt idxSigma = fftThresh.find("sigma");
|
---|
4564 | uInt idxTop = fftThresh.find("top");
|
---|
4565 |
|
---|
4566 | if (idxSigma == fftThresh.size() - 5) {
|
---|
4567 | std::istringstream is(fftThresh.substr(0, fftThresh.size() - 5));
|
---|
4568 | is >> fftThSigma;
|
---|
4569 | fftThAttr = "sigma";
|
---|
4570 | } else if (idxTop == 0) {
|
---|
4571 | std::istringstream is(fftThresh.substr(3));
|
---|
4572 | is >> fftThTop;
|
---|
4573 | fftThAttr = "top";
|
---|
4574 | } else {
|
---|
4575 | bool isNumber = true;
|
---|
4576 | for (uInt i = 0; i < fftThresh.size()-1; ++i) {
|
---|
4577 | char ch = (fftThresh.substr(i, 1).c_str())[0];
|
---|
4578 | if (!(isdigit(ch) || (fftThresh.substr(i, 1) == "."))) {
|
---|
4579 | isNumber = false;
|
---|
4580 | break;
|
---|
4581 | }
|
---|
4582 | }
|
---|
4583 | if (isNumber) {
|
---|
4584 | std::istringstream is(fftThresh);
|
---|
4585 | is >> fftThSigma;
|
---|
4586 | fftThAttr = "sigma";
|
---|
4587 | } else {
|
---|
4588 | throw(AipsError("fftthresh has a wrong value"));
|
---|
4589 | }
|
---|
4590 | }
|
---|
4591 | }
|
---|
4592 |
|
---|
4593 | void Scantable::doSelectWaveNumbers(const int whichrow, const std::vector<bool>& chanMask, const std::string& fftMethod, const float fftThSigma, const int fftThTop, const std::string& fftThAttr, std::vector<int>& nWaves)
|
---|
4594 | {
|
---|
4595 | std::vector<float> fspec;
|
---|
4596 | if (fftMethod == "fft") {
|
---|
4597 | fspec = execFFT(whichrow, chanMask, false, true);
|
---|
4598 | //} else if (fftMethod == "lsp") {
|
---|
4599 | // fspec = lombScarglePeriodogram(whichrow);
|
---|
4600 | }
|
---|
4601 |
|
---|
4602 | if (fftThAttr == "sigma") {
|
---|
4603 | float mean = 0.0;
|
---|
4604 | float mean2 = 0.0;
|
---|
4605 | for (uInt i = 0; i < fspec.size(); ++i) {
|
---|
4606 | mean += fspec[i];
|
---|
4607 | mean2 += fspec[i]*fspec[i];
|
---|
4608 | }
|
---|
4609 | mean /= float(fspec.size());
|
---|
4610 | mean2 /= float(fspec.size());
|
---|
4611 | float thres = mean + fftThSigma * float(sqrt(mean2 - mean*mean));
|
---|
4612 |
|
---|
4613 | for (uInt i = 0; i < fspec.size(); ++i) {
|
---|
4614 | if (fspec[i] >= thres) {
|
---|
4615 | nWaves.push_back(i);
|
---|
4616 | }
|
---|
4617 | }
|
---|
4618 |
|
---|
4619 | } else if (fftThAttr == "top") {
|
---|
4620 | for (int i = 0; i < fftThTop; ++i) {
|
---|
4621 | float max = 0.0;
|
---|
4622 | int maxIdx = 0;
|
---|
4623 | for (uInt j = 0; j < fspec.size(); ++j) {
|
---|
4624 | if (fspec[j] > max) {
|
---|
4625 | max = fspec[j];
|
---|
4626 | maxIdx = j;
|
---|
4627 | }
|
---|
4628 | }
|
---|
4629 | nWaves.push_back(maxIdx);
|
---|
4630 | fspec[maxIdx] = 0.0;
|
---|
4631 | }
|
---|
4632 |
|
---|
4633 | }
|
---|
4634 |
|
---|
4635 | if (nWaves.size() > 1) {
|
---|
4636 | sort(nWaves.begin(), nWaves.end());
|
---|
4637 | }
|
---|
4638 | }
|
---|
4639 |
|
---|
4640 | void Scantable::addAuxWaveNumbers(const int whichrow, const std::vector<int>& addNWaves, const std::vector<int>& rejectNWaves, std::vector<int>& nWaves)
|
---|
4641 | {
|
---|
4642 | std::vector<int> tempAddNWaves, tempRejectNWaves;
|
---|
4643 | tempAddNWaves.clear();
|
---|
4644 | tempRejectNWaves.clear();
|
---|
4645 |
|
---|
4646 | for (uInt i = 0; i < addNWaves.size(); ++i) {
|
---|
4647 | tempAddNWaves.push_back(addNWaves[i]);
|
---|
4648 | }
|
---|
4649 | if ((tempAddNWaves.size() == 2) && (tempAddNWaves[1] == -999)) {
|
---|
4650 | setWaveNumberListUptoNyquistFreq(whichrow, tempAddNWaves);
|
---|
4651 | }
|
---|
4652 |
|
---|
4653 | for (uInt i = 0; i < rejectNWaves.size(); ++i) {
|
---|
4654 | tempRejectNWaves.push_back(rejectNWaves[i]);
|
---|
4655 | }
|
---|
4656 | if ((tempRejectNWaves.size() == 2) && (tempRejectNWaves[1] == -999)) {
|
---|
4657 | setWaveNumberListUptoNyquistFreq(whichrow, tempRejectNWaves);
|
---|
4658 | }
|
---|
4659 |
|
---|
4660 | for (uInt i = 0; i < tempAddNWaves.size(); ++i) {
|
---|
4661 | bool found = false;
|
---|
4662 | for (uInt j = 0; j < nWaves.size(); ++j) {
|
---|
4663 | if (nWaves[j] == tempAddNWaves[i]) {
|
---|
4664 | found = true;
|
---|
4665 | break;
|
---|
4666 | }
|
---|
4667 | }
|
---|
4668 | if (!found) nWaves.push_back(tempAddNWaves[i]);
|
---|
4669 | }
|
---|
4670 |
|
---|
4671 | for (uInt i = 0; i < tempRejectNWaves.size(); ++i) {
|
---|
4672 | for (std::vector<int>::iterator j = nWaves.begin(); j != nWaves.end(); ) {
|
---|
4673 | if (*j == tempRejectNWaves[i]) {
|
---|
4674 | j = nWaves.erase(j);
|
---|
4675 | } else {
|
---|
4676 | ++j;
|
---|
4677 | }
|
---|
4678 | }
|
---|
4679 | }
|
---|
4680 |
|
---|
4681 | if (nWaves.size() > 1) {
|
---|
4682 | sort(nWaves.begin(), nWaves.end());
|
---|
4683 | unique(nWaves.begin(), nWaves.end());
|
---|
4684 | }
|
---|
4685 | }
|
---|
4686 |
|
---|
4687 | void Scantable::setWaveNumberListUptoNyquistFreq(const int whichrow, std::vector<int>& nWaves)
|
---|
4688 | {
|
---|
4689 | int val = nWaves[0];
|
---|
4690 | int nyquistFreq = nchan(getIF(whichrow))/2+1;
|
---|
4691 | nWaves.clear();
|
---|
4692 | if (val > nyquistFreq) { // for safety, at least nWaves contains a constant; CAS-3759
|
---|
4693 | nWaves.push_back(0);
|
---|
4694 | }
|
---|
4695 | while (val <= nyquistFreq) {
|
---|
4696 | nWaves.push_back(val);
|
---|
4697 | val++;
|
---|
4698 | }
|
---|
4699 | }
|
---|
4700 |
|
---|
4701 | void Scantable::sinusoidBaseline(const std::vector<bool>& mask, const std::string& fftInfo,
|
---|
4702 | const std::vector<int>& addNWaves,
|
---|
4703 | const std::vector<int>& rejectNWaves,
|
---|
4704 | float thresClip, int nIterClip,
|
---|
4705 | bool getResidual,
|
---|
4706 | const std::string& progressInfo,
|
---|
4707 | const bool outLogger, const std::string& blfile,
|
---|
4708 | const std::string& bltable)
|
---|
4709 | {
|
---|
4710 | /****
|
---|
4711 | double TimeStart = mathutil::gettimeofday_sec();
|
---|
4712 | ****/
|
---|
4713 |
|
---|
4714 | try {
|
---|
4715 | ofstream ofs;
|
---|
4716 | String coordInfo;
|
---|
4717 | bool hasSameNchan, outTextFile, csvFormat, showProgress;
|
---|
4718 | int minNRow;
|
---|
4719 | int nRow = nrow();
|
---|
4720 | std::vector<bool> chanMask, finalChanMask;
|
---|
4721 | float rms;
|
---|
4722 | bool outBaselineTable = (bltable != "");
|
---|
4723 | STBaselineTable bt = STBaselineTable(*this);
|
---|
4724 | Vector<Double> timeSecCol;
|
---|
4725 | size_t flagged=0;
|
---|
4726 |
|
---|
4727 | initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
|
---|
4728 | coordInfo, hasSameNchan,
|
---|
4729 | progressInfo, showProgress, minNRow,
|
---|
4730 | timeSecCol);
|
---|
4731 |
|
---|
4732 | bool applyFFT;
|
---|
4733 | std::string fftMethod, fftThresh;
|
---|
4734 | parseFFTInfo(fftInfo, applyFFT, fftMethod, fftThresh);
|
---|
4735 |
|
---|
4736 | std::vector<int> nWaves;
|
---|
4737 | std::vector<int> nChanNos;
|
---|
4738 | std::vector<std::vector<std::vector<double> > > modelReservoir;
|
---|
4739 | if (!applyFFT) {
|
---|
4740 | nWaves = selectWaveNumbers(addNWaves, rejectNWaves);
|
---|
4741 | modelReservoir = getSinusoidModelReservoir(nWaves, nChanNos);
|
---|
4742 | }
|
---|
4743 |
|
---|
4744 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
4745 | std::vector<float> sp = getSpectrum(whichrow);
|
---|
4746 | chanMask = getCompositeChanMask(whichrow, mask);
|
---|
4747 | std::vector<std::vector<double> > model;
|
---|
4748 | if (applyFFT) {
|
---|
4749 | nWaves = selectWaveNumbers(whichrow, chanMask, true, fftMethod, fftThresh,
|
---|
4750 | addNWaves, rejectNWaves);
|
---|
4751 | model = getSinusoidModel(nWaves, sp.size());
|
---|
4752 | } else {
|
---|
4753 | model = modelReservoir[getIdxOfNchan(sp.size(), nChanNos)];
|
---|
4754 | }
|
---|
4755 | int nModel = modelReservoir.size();
|
---|
4756 |
|
---|
4757 | std::vector<float> params;
|
---|
4758 |
|
---|
4759 | //if (flagrowCol_(whichrow) == 0) {
|
---|
4760 | if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
|
---|
4761 | int nClipped = 0;
|
---|
4762 | std::vector<float> res;
|
---|
4763 | res = doLeastSquareFitting(sp, chanMask, model,
|
---|
4764 | params, rms, finalChanMask,
|
---|
4765 | nClipped, thresClip, nIterClip, getResidual);
|
---|
4766 |
|
---|
4767 | if (outBaselineTable) {
|
---|
4768 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
4769 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
4770 | true, STBaselineFunc::Sinusoid, nWaves, std::vector<float>(),
|
---|
4771 | getMaskListFromMask(finalChanMask), params, rms, sp.size(),
|
---|
4772 | thresClip, nIterClip, 0.0, 0, std::vector<int>());
|
---|
4773 | } else {
|
---|
4774 | setSpectrum(res, whichrow);
|
---|
4775 | }
|
---|
4776 |
|
---|
4777 | outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
|
---|
4778 | coordInfo, hasSameNchan, ofs, "sinusoidBaseline()",
|
---|
4779 | params, nClipped);
|
---|
4780 | } else {
|
---|
4781 | // no valid channels to fit (flag the row)
|
---|
4782 | flagrowCol_.put(whichrow, 1);
|
---|
4783 | ++flagged;
|
---|
4784 | if (outBaselineTable) {
|
---|
4785 | params.resize(nModel);
|
---|
4786 | for (uInt i = 0; i < params.size(); ++i) {
|
---|
4787 | params[i] = 0.0;
|
---|
4788 | }
|
---|
4789 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
4790 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
4791 | true, STBaselineFunc::Sinusoid, nWaves, std::vector<float>(),
|
---|
4792 | getMaskListFromMask(chanMask), params, 0.0, sp.size(),
|
---|
4793 | thresClip, nIterClip, 0.0, 0, std::vector<int>());
|
---|
4794 | }
|
---|
4795 | }
|
---|
4796 |
|
---|
4797 | showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
|
---|
4798 | }
|
---|
4799 |
|
---|
4800 | finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
|
---|
4801 |
|
---|
4802 | if (flagged > 0) {
|
---|
4803 | LogIO os( LogOrigin( "Scantable", "sinusoidBaseline()") ) ;
|
---|
4804 | os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
|
---|
4805 | }
|
---|
4806 | } catch (...) {
|
---|
4807 | throw;
|
---|
4808 | }
|
---|
4809 |
|
---|
4810 | /****
|
---|
4811 | double TimeEnd = mathutil::gettimeofday_sec();
|
---|
4812 | double elapse1 = TimeEnd - TimeStart;
|
---|
4813 | std::cout << "sinusoid-old : " << elapse1 << " (sec.)" << endl;
|
---|
4814 | ****/
|
---|
4815 | }
|
---|
4816 |
|
---|
4817 | void Scantable::autoSinusoidBaseline(const std::vector<bool>& mask, const std::string& fftInfo,
|
---|
4818 | const std::vector<int>& addNWaves,
|
---|
4819 | const std::vector<int>& rejectNWaves,
|
---|
4820 | float thresClip, int nIterClip,
|
---|
4821 | const std::vector<int>& edge,
|
---|
4822 | float threshold, int chanAvgLimit,
|
---|
4823 | bool getResidual,
|
---|
4824 | const std::string& progressInfo,
|
---|
4825 | const bool outLogger, const std::string& blfile,
|
---|
4826 | const std::string& bltable)
|
---|
4827 | {
|
---|
4828 | try {
|
---|
4829 | ofstream ofs;
|
---|
4830 | String coordInfo;
|
---|
4831 | bool hasSameNchan, outTextFile, csvFormat, showProgress;
|
---|
4832 | int minNRow;
|
---|
4833 | int nRow = nrow();
|
---|
4834 | std::vector<bool> chanMask, finalChanMask;
|
---|
4835 | float rms;
|
---|
4836 | bool outBaselineTable = (bltable != "");
|
---|
4837 | STBaselineTable bt = STBaselineTable(*this);
|
---|
4838 | Vector<Double> timeSecCol;
|
---|
4839 | STLineFinder lineFinder = STLineFinder();
|
---|
4840 | size_t flagged=0;
|
---|
4841 |
|
---|
4842 | initialiseBaselining(blfile, ofs, outLogger, outTextFile, csvFormat,
|
---|
4843 | coordInfo, hasSameNchan,
|
---|
4844 | progressInfo, showProgress, minNRow,
|
---|
4845 | timeSecCol);
|
---|
4846 |
|
---|
4847 | initLineFinder(edge, threshold, chanAvgLimit, lineFinder);
|
---|
4848 |
|
---|
4849 | bool applyFFT;
|
---|
4850 | string fftMethod, fftThresh;
|
---|
4851 | parseFFTInfo(fftInfo, applyFFT, fftMethod, fftThresh);
|
---|
4852 |
|
---|
4853 | std::vector<int> nWaves;
|
---|
4854 | std::vector<int> nChanNos;
|
---|
4855 | std::vector<std::vector<std::vector<double> > > modelReservoir;
|
---|
4856 | if (!applyFFT) {
|
---|
4857 | nWaves = selectWaveNumbers(addNWaves, rejectNWaves);
|
---|
4858 | modelReservoir = getSinusoidModelReservoir(nWaves, nChanNos);
|
---|
4859 | }
|
---|
4860 |
|
---|
4861 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
4862 | std::vector<float> sp = getSpectrum(whichrow);
|
---|
4863 | std::vector<int> currentEdge;
|
---|
4864 | chanMask = getCompositeChanMask(whichrow, mask, edge, currentEdge, lineFinder);
|
---|
4865 | std::vector<std::vector<double> > model;
|
---|
4866 | if (applyFFT) {
|
---|
4867 | nWaves = selectWaveNumbers(whichrow, chanMask, true, fftMethod, fftThresh,
|
---|
4868 | addNWaves, rejectNWaves);
|
---|
4869 | model = getSinusoidModel(nWaves, sp.size());
|
---|
4870 | } else {
|
---|
4871 | model = modelReservoir[getIdxOfNchan(sp.size(), nChanNos)];
|
---|
4872 | }
|
---|
4873 | int nModel = modelReservoir.size();
|
---|
4874 |
|
---|
4875 | std::vector<float> params;
|
---|
4876 |
|
---|
4877 | //if (flagrowCol_(whichrow) == 0) {
|
---|
4878 | if (flagrowCol_(whichrow)==0 && nValidMask(chanMask)>0) {
|
---|
4879 | int nClipped = 0;
|
---|
4880 | std::vector<float> res;
|
---|
4881 | res = doLeastSquareFitting(sp, chanMask, model,
|
---|
4882 | params, rms, finalChanMask,
|
---|
4883 | nClipped, thresClip, nIterClip, getResidual);
|
---|
4884 |
|
---|
4885 | if (outBaselineTable) {
|
---|
4886 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
4887 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
4888 | true, STBaselineFunc::Sinusoid, nWaves, std::vector<float>(),
|
---|
4889 | getMaskListFromMask(finalChanMask), params, rms, sp.size(),
|
---|
4890 | thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
|
---|
4891 | } else {
|
---|
4892 | setSpectrum(res, whichrow);
|
---|
4893 | }
|
---|
4894 |
|
---|
4895 | outputFittingResult(outLogger, outTextFile, csvFormat, chanMask, whichrow,
|
---|
4896 | coordInfo, hasSameNchan, ofs, "autoSinusoidBaseline()",
|
---|
4897 | params, nClipped);
|
---|
4898 | } else {
|
---|
4899 | // no valid channels to fit (flag the row)
|
---|
4900 | flagrowCol_.put(whichrow, 1);
|
---|
4901 | ++flagged;
|
---|
4902 | if (outBaselineTable) {
|
---|
4903 | params.resize(nModel);
|
---|
4904 | for (uInt i = 0; i < params.size(); ++i) {
|
---|
4905 | params[i] = 0.0;
|
---|
4906 | }
|
---|
4907 | bt.appenddata(getScan(whichrow), getCycle(whichrow), getBeam(whichrow),
|
---|
4908 | getIF(whichrow), getPol(whichrow), 0, timeSecCol[whichrow],
|
---|
4909 | true, STBaselineFunc::Sinusoid, nWaves, std::vector<float>(),
|
---|
4910 | getMaskListFromMask(chanMask), params, 0.0, sp.size(),
|
---|
4911 | thresClip, nIterClip, threshold, chanAvgLimit, currentEdge);
|
---|
4912 | }
|
---|
4913 | }
|
---|
4914 |
|
---|
4915 | showProgressOnTerminal(whichrow, nRow, showProgress, minNRow);
|
---|
4916 | }
|
---|
4917 |
|
---|
4918 | finaliseBaselining(outBaselineTable, &bt, bltable, outTextFile, ofs);
|
---|
4919 |
|
---|
4920 | if (flagged > 0) {
|
---|
4921 | LogIO os( LogOrigin( "Scantable", "autoSinusoidBaseline()") ) ;
|
---|
4922 | os << LogIO::WARN << "Baseline subtraction is skipped for " << flagged << " spectra due to too few valid channels to operate fit. The spectra will be flagged in output data." << LogIO::POST;
|
---|
4923 | }
|
---|
4924 | } catch (...) {
|
---|
4925 | throw;
|
---|
4926 | }
|
---|
4927 | }
|
---|
4928 |
|
---|
4929 | std::vector<float> Scantable::doSinusoidFitting(const std::vector<float>& data,
|
---|
4930 | const std::vector<bool>& mask,
|
---|
4931 | const std::vector<int>& waveNumbers,
|
---|
4932 | std::vector<float>& params,
|
---|
4933 | float& rms,
|
---|
4934 | std::vector<bool>& finalmask,
|
---|
4935 | float clipth,
|
---|
4936 | int clipn)
|
---|
4937 | {
|
---|
4938 | int nClipped = 0;
|
---|
4939 | return doSinusoidFitting(data, mask, waveNumbers, params, rms, finalmask, nClipped, clipth, clipn);
|
---|
4940 | }
|
---|
4941 |
|
---|
4942 | std::vector<float> Scantable::doSinusoidFitting(const std::vector<float>& data,
|
---|
4943 | const std::vector<bool>& mask,
|
---|
4944 | const std::vector<int>& waveNumbers,
|
---|
4945 | std::vector<float>& params,
|
---|
4946 | float& rms,
|
---|
4947 | std::vector<bool>& finalMask,
|
---|
4948 | int& nClipped,
|
---|
4949 | float thresClip,
|
---|
4950 | int nIterClip,
|
---|
4951 | bool getResidual)
|
---|
4952 | {
|
---|
4953 | return doLeastSquareFitting(data, mask,
|
---|
4954 | getSinusoidModel(waveNumbers, data.size()),
|
---|
4955 | params, rms, finalMask,
|
---|
4956 | nClipped, thresClip, nIterClip,
|
---|
4957 | getResidual);
|
---|
4958 | }
|
---|
4959 |
|
---|
4960 | std::vector<std::vector<std::vector<double> > > Scantable::getSinusoidModelReservoir(const std::vector<int>& waveNumbers,
|
---|
4961 | std::vector<int>& nChanNos)
|
---|
4962 | {
|
---|
4963 | std::vector<std::vector<std::vector<double> > > res;
|
---|
4964 | res.clear();
|
---|
4965 | nChanNos.clear();
|
---|
4966 |
|
---|
4967 | std::vector<uint> ifNos = getIFNos();
|
---|
4968 | for (uint i = 0; i < ifNos.size(); ++i) {
|
---|
4969 | int currNchan = nchan(ifNos[i]);
|
---|
4970 | bool hasDifferentNchan = (i == 0);
|
---|
4971 | for (uint j = 0; j < i; ++j) {
|
---|
4972 | if (currNchan != nchan(ifNos[j])) {
|
---|
4973 | hasDifferentNchan = true;
|
---|
4974 | break;
|
---|
4975 | }
|
---|
4976 | }
|
---|
4977 | if (hasDifferentNchan) {
|
---|
4978 | res.push_back(getSinusoidModel(waveNumbers, currNchan));
|
---|
4979 | nChanNos.push_back(currNchan);
|
---|
4980 | }
|
---|
4981 | }
|
---|
4982 |
|
---|
4983 | return res;
|
---|
4984 | }
|
---|
4985 |
|
---|
4986 | std::vector<std::vector<double> > Scantable::getSinusoidModel(const std::vector<int>& waveNumbers, int nchan)
|
---|
4987 | {
|
---|
4988 | // model : contains elemental values for computing the least-square matrix.
|
---|
4989 | // model.size() is nmodel and model[*].size() is nchan.
|
---|
4990 | // Each model element are as follows:
|
---|
4991 | // model[0] = {1.0, 1.0, 1.0, ..., 1.0},
|
---|
4992 | // model[2n-1] = {sin(nPI/L*x[0]), sin(nPI/L*x[1]), ..., sin(nPI/L*x[nchan])},
|
---|
4993 | // model[2n] = {cos(nPI/L*x[0]), cos(nPI/L*x[1]), ..., cos(nPI/L*x[nchan])},
|
---|
4994 | // where (1 <= n <= nMaxWavesInSW),
|
---|
4995 | // or,
|
---|
4996 | // model[2n-1] = {sin(wn[n]PI/L*x[0]), sin(wn[n]PI/L*x[1]), ..., sin(wn[n]PI/L*x[nchan])},
|
---|
4997 | // model[2n] = {cos(wn[n]PI/L*x[0]), cos(wn[n]PI/L*x[1]), ..., cos(wn[n]PI/L*x[nchan])},
|
---|
4998 | // where wn[n] denotes waveNumbers[n] (1 <= n <= waveNumbers.size()).
|
---|
4999 |
|
---|
5000 | std::vector<int> nWaves; // sorted and uniqued array of wave numbers
|
---|
5001 | nWaves.reserve(waveNumbers.size());
|
---|
5002 | copy(waveNumbers.begin(), waveNumbers.end(), back_inserter(nWaves));
|
---|
5003 | sort(nWaves.begin(), nWaves.end());
|
---|
5004 | std::vector<int>::iterator end_it = unique(nWaves.begin(), nWaves.end());
|
---|
5005 | nWaves.erase(end_it, nWaves.end());
|
---|
5006 |
|
---|
5007 | int minNWaves = nWaves[0];
|
---|
5008 | if (minNWaves < 0) {
|
---|
5009 | throw(AipsError("wave number must be positive or zero (i.e. constant)"));
|
---|
5010 | }
|
---|
5011 | bool hasConstantTerm = (minNWaves == 0);
|
---|
5012 | int nmodel = nWaves.size() * 2 - (hasConstantTerm ? 1 : 0); //number of parameters to solve.
|
---|
5013 |
|
---|
5014 | std::vector<std::vector<double> > model(nmodel, std::vector<double>(nchan));
|
---|
5015 |
|
---|
5016 | if (hasConstantTerm) {
|
---|
5017 | for (int j = 0; j < nchan; ++j) {
|
---|
5018 | model[0][j] = 1.0;
|
---|
5019 | }
|
---|
5020 | }
|
---|
5021 |
|
---|
5022 | const double PI = 6.0 * asin(0.5); // PI (= 3.141592653...)
|
---|
5023 | double stretch0 = 2.0*PI/(double)(nchan-1);
|
---|
5024 |
|
---|
5025 | for (uInt i = (hasConstantTerm ? 1 : 0); i < nWaves.size(); ++i) {
|
---|
5026 | int sidx = hasConstantTerm ? 2*i-1 : 2*i;
|
---|
5027 | int cidx = sidx + 1;
|
---|
5028 | double stretch = stretch0*(double)nWaves[i];
|
---|
5029 |
|
---|
5030 | for (int j = 0; j < nchan; ++j) {
|
---|
5031 | model[sidx][j] = sin(stretch*(double)j);
|
---|
5032 | model[cidx][j] = cos(stretch*(double)j);
|
---|
5033 | }
|
---|
5034 | }
|
---|
5035 |
|
---|
5036 | return model;
|
---|
5037 | }
|
---|
5038 |
|
---|
5039 | std::vector<bool> Scantable::getCompositeChanMask(int whichrow,
|
---|
5040 | const std::vector<bool>& inMask)
|
---|
5041 | {
|
---|
5042 | std::vector<bool> mask = getMask(whichrow);
|
---|
5043 | uInt maskSize = mask.size();
|
---|
5044 | if (inMask.size() != 0) {
|
---|
5045 | if (maskSize != inMask.size()) {
|
---|
5046 | throw(AipsError("mask sizes are not the same."));
|
---|
5047 | }
|
---|
5048 | for (uInt i = 0; i < maskSize; ++i) {
|
---|
5049 | mask[i] = mask[i] && inMask[i];
|
---|
5050 | }
|
---|
5051 | }
|
---|
5052 |
|
---|
5053 | return mask;
|
---|
5054 | }
|
---|
5055 |
|
---|
5056 | std::vector<bool> Scantable::getCompositeChanMask(int whichrow,
|
---|
5057 | const std::vector<bool>& inMask,
|
---|
5058 | const std::vector<int>& edge,
|
---|
5059 | std::vector<int>& currEdge,
|
---|
5060 | STLineFinder& lineFinder)
|
---|
5061 | {
|
---|
5062 | if (isAllChannelsFlagged(whichrow)) {//all channels flagged
|
---|
5063 | std::vector<bool> res_mask(inMask.size(),false);
|
---|
5064 | return res_mask;
|
---|
5065 | } else if (nValidMask(inMask)==0){ //no valid mask channels
|
---|
5066 | std::vector<bool> res_mask(inMask);
|
---|
5067 | return res_mask;
|
---|
5068 | }
|
---|
5069 |
|
---|
5070 | std::vector<uint> ifNos = getIFNos();
|
---|
5071 | if ((edge.size() > 2) && (edge.size() < ifNos.size()*2)) {
|
---|
5072 | throw(AipsError("Length of edge element info is less than that of IFs"));
|
---|
5073 | }
|
---|
5074 |
|
---|
5075 | uint idx = 0;
|
---|
5076 | if (edge.size() > 2) {
|
---|
5077 | int ifVal = getIF(whichrow);
|
---|
5078 | bool foundIF = false;
|
---|
5079 | for (uint i = 0; i < ifNos.size(); ++i) {
|
---|
5080 | if (ifVal == (int)ifNos[i]) {
|
---|
5081 | idx = 2*i;
|
---|
5082 | foundIF = true;
|
---|
5083 | break;
|
---|
5084 | }
|
---|
5085 | }
|
---|
5086 | if (!foundIF) {
|
---|
5087 | throw(AipsError("bad IF number"));
|
---|
5088 | }
|
---|
5089 | }
|
---|
5090 |
|
---|
5091 | currEdge.clear();
|
---|
5092 | currEdge.resize(2);
|
---|
5093 | currEdge[0] = edge[idx];
|
---|
5094 | currEdge[1] = edge[idx+1];
|
---|
5095 |
|
---|
5096 | lineFinder.setData(getSpectrum(whichrow));
|
---|
5097 | lineFinder.findLines(getCompositeChanMask(whichrow, inMask), currEdge, whichrow);
|
---|
5098 | return lineFinder.getMask();
|
---|
5099 | }
|
---|
5100 |
|
---|
5101 | /* for cspline. will be merged once cspline is available in fitter (2011/3/10 WK) */
|
---|
5102 | void Scantable::outputFittingResult(bool outLogger,
|
---|
5103 | bool outTextFile,
|
---|
5104 | bool csvFormat,
|
---|
5105 | const std::vector<bool>& chanMask,
|
---|
5106 | int whichrow,
|
---|
5107 | const casa::String& coordInfo,
|
---|
5108 | bool hasSameNchan,
|
---|
5109 | ofstream& ofs,
|
---|
5110 | const casa::String& funcName,
|
---|
5111 | const std::vector<int>& edge,
|
---|
5112 | const std::vector<float>& params,
|
---|
5113 | const int nClipped)
|
---|
5114 | {
|
---|
5115 | if (outLogger || outTextFile) {
|
---|
5116 | float rms = getRms(chanMask, whichrow);
|
---|
5117 | String masklist = getMaskRangeList(chanMask, whichrow, coordInfo, hasSameNchan);
|
---|
5118 | std::vector<bool> fixed;
|
---|
5119 | fixed.clear();
|
---|
5120 |
|
---|
5121 | if (outLogger) {
|
---|
5122 | LogIO ols(LogOrigin("Scantable", funcName, WHERE));
|
---|
5123 | ols << formatPiecewiseBaselineParams(edge, params, fixed, rms, nClipped,
|
---|
5124 | masklist, whichrow, false, csvFormat) << LogIO::POST ;
|
---|
5125 | }
|
---|
5126 | if (outTextFile) {
|
---|
5127 | ofs << formatPiecewiseBaselineParams(edge, params, fixed, rms, nClipped,
|
---|
5128 | masklist, whichrow, true, csvFormat) << flush;
|
---|
5129 | }
|
---|
5130 | }
|
---|
5131 | }
|
---|
5132 |
|
---|
5133 | /* for poly/chebyshev/sinusoid. */
|
---|
5134 | void Scantable::outputFittingResult(bool outLogger,
|
---|
5135 | bool outTextFile,
|
---|
5136 | bool csvFormat,
|
---|
5137 | const std::vector<bool>& chanMask,
|
---|
5138 | int whichrow,
|
---|
5139 | const casa::String& coordInfo,
|
---|
5140 | bool hasSameNchan,
|
---|
5141 | ofstream& ofs,
|
---|
5142 | const casa::String& funcName,
|
---|
5143 | const std::vector<float>& params,
|
---|
5144 | const int nClipped)
|
---|
5145 | {
|
---|
5146 | if (outLogger || outTextFile) {
|
---|
5147 | float rms = getRms(chanMask, whichrow);
|
---|
5148 | String masklist = getMaskRangeList(chanMask, whichrow, coordInfo, hasSameNchan);
|
---|
5149 | std::vector<bool> fixed;
|
---|
5150 | fixed.clear();
|
---|
5151 |
|
---|
5152 | if (outLogger) {
|
---|
5153 | LogIO ols(LogOrigin("Scantable", funcName, WHERE));
|
---|
5154 | ols << formatBaselineParams(params, fixed, rms, nClipped,
|
---|
5155 | masklist, whichrow, false, csvFormat) << LogIO::POST ;
|
---|
5156 | }
|
---|
5157 | if (outTextFile) {
|
---|
5158 | ofs << formatBaselineParams(params, fixed, rms, nClipped,
|
---|
5159 | masklist, whichrow, true, csvFormat) << flush;
|
---|
5160 | }
|
---|
5161 | }
|
---|
5162 | }
|
---|
5163 |
|
---|
5164 | void Scantable::parseProgressInfo(const std::string& progressInfo, bool& showProgress, int& minNRow)
|
---|
5165 | {
|
---|
5166 | int idxDelimiter = progressInfo.find(",");
|
---|
5167 | if (idxDelimiter < 0) {
|
---|
5168 | throw(AipsError("wrong value in 'showprogress' parameter")) ;
|
---|
5169 | }
|
---|
5170 | showProgress = (progressInfo.substr(0, idxDelimiter) == "true");
|
---|
5171 | std::istringstream is(progressInfo.substr(idxDelimiter+1));
|
---|
5172 | is >> minNRow;
|
---|
5173 | }
|
---|
5174 |
|
---|
5175 | void Scantable::showProgressOnTerminal(const int nProcessed, const int nTotal, const bool showProgress, const int nTotalThreshold)
|
---|
5176 | {
|
---|
5177 | if (showProgress && (nTotal >= nTotalThreshold)) {
|
---|
5178 | int nInterval = int(floor(double(nTotal)/100.0));
|
---|
5179 | if (nInterval == 0) nInterval++;
|
---|
5180 |
|
---|
5181 | if (nProcessed % nInterval == 0) {
|
---|
5182 | printf("\r"); //go to the head of line
|
---|
5183 | printf("\x1b[31m\x1b[1m"); //set red color, highlighted
|
---|
5184 | printf("[%3d%%]", (int)(100.0*(double(nProcessed+1))/(double(nTotal))) );
|
---|
5185 | printf("\x1b[39m\x1b[0m"); //set default attributes
|
---|
5186 | fflush(NULL);
|
---|
5187 | }
|
---|
5188 |
|
---|
5189 | if (nProcessed == nTotal - 1) {
|
---|
5190 | printf("\r\x1b[K"); //clear
|
---|
5191 | fflush(NULL);
|
---|
5192 | }
|
---|
5193 |
|
---|
5194 | }
|
---|
5195 | }
|
---|
5196 |
|
---|
5197 | std::vector<float> Scantable::execFFT(const int whichrow, const std::vector<bool>& inMask, bool getRealImag, bool getAmplitudeOnly)
|
---|
5198 | {
|
---|
5199 | std::vector<bool> mask = getMask(whichrow);
|
---|
5200 |
|
---|
5201 | if (inMask.size() > 0) {
|
---|
5202 | uInt maskSize = mask.size();
|
---|
5203 | if (maskSize != inMask.size()) {
|
---|
5204 | throw(AipsError("mask sizes are not the same."));
|
---|
5205 | }
|
---|
5206 | for (uInt i = 0; i < maskSize; ++i) {
|
---|
5207 | mask[i] = mask[i] && inMask[i];
|
---|
5208 | }
|
---|
5209 | }
|
---|
5210 |
|
---|
5211 | Vector<Float> spec = getSpectrum(whichrow);
|
---|
5212 | mathutil::doZeroOrderInterpolation(spec, mask);
|
---|
5213 |
|
---|
5214 | FFTServer<Float,Complex> ffts;
|
---|
5215 | Vector<Complex> fftres;
|
---|
5216 | ffts.fft0(fftres, spec);
|
---|
5217 |
|
---|
5218 | std::vector<float> res;
|
---|
5219 | float norm = float(2.0/double(spec.size()));
|
---|
5220 |
|
---|
5221 | if (getRealImag) {
|
---|
5222 | for (uInt i = 0; i < fftres.size(); ++i) {
|
---|
5223 | res.push_back(real(fftres[i])*norm);
|
---|
5224 | res.push_back(imag(fftres[i])*norm);
|
---|
5225 | }
|
---|
5226 | } else {
|
---|
5227 | for (uInt i = 0; i < fftres.size(); ++i) {
|
---|
5228 | res.push_back(abs(fftres[i])*norm);
|
---|
5229 | if (!getAmplitudeOnly) res.push_back(arg(fftres[i]));
|
---|
5230 | }
|
---|
5231 | }
|
---|
5232 |
|
---|
5233 | return res;
|
---|
5234 | }
|
---|
5235 |
|
---|
5236 |
|
---|
5237 | float Scantable::getRms(const std::vector<bool>& mask, int whichrow)
|
---|
5238 | {
|
---|
5239 | /****
|
---|
5240 | double ms1TimeStart, ms1TimeEnd;
|
---|
5241 | double elapse1 = 0.0;
|
---|
5242 | ms1TimeStart = mathutil::gettimeofday_sec();
|
---|
5243 | ****/
|
---|
5244 |
|
---|
5245 | Vector<Float> spec;
|
---|
5246 | specCol_.get(whichrow, spec);
|
---|
5247 |
|
---|
5248 | /****
|
---|
5249 | ms1TimeEnd = mathutil::gettimeofday_sec();
|
---|
5250 | elapse1 = ms1TimeEnd - ms1TimeStart;
|
---|
5251 | std::cout << "rm1 : " << elapse1 << " (sec.)" << endl;
|
---|
5252 | ****/
|
---|
5253 |
|
---|
5254 | return (float)doGetRms(mask, spec);
|
---|
5255 | }
|
---|
5256 |
|
---|
5257 | double Scantable::doGetRms(const std::vector<bool>& mask, const Vector<Float>& spec)
|
---|
5258 | {
|
---|
5259 | double mean = 0.0;
|
---|
5260 | double smean = 0.0;
|
---|
5261 | int n = 0;
|
---|
5262 | for (uInt i = 0; i < spec.nelements(); ++i) {
|
---|
5263 | if (mask[i]) {
|
---|
5264 | double val = (double)spec[i];
|
---|
5265 | mean += val;
|
---|
5266 | smean += val*val;
|
---|
5267 | n++;
|
---|
5268 | }
|
---|
5269 | }
|
---|
5270 |
|
---|
5271 | mean /= (double)n;
|
---|
5272 | smean /= (double)n;
|
---|
5273 |
|
---|
5274 | return sqrt(smean - mean*mean);
|
---|
5275 | }
|
---|
5276 |
|
---|
5277 | std::string Scantable::formatBaselineParamsHeader(int whichrow, const std::string& masklist, bool verbose, bool csvformat) const
|
---|
5278 | {
|
---|
5279 | if (verbose) {
|
---|
5280 | ostringstream oss;
|
---|
5281 |
|
---|
5282 | if (csvformat) {
|
---|
5283 | oss << getScan(whichrow) << ",";
|
---|
5284 | oss << getBeam(whichrow) << ",";
|
---|
5285 | oss << getIF(whichrow) << ",";
|
---|
5286 | oss << getPol(whichrow) << ",";
|
---|
5287 | oss << getCycle(whichrow) << ",";
|
---|
5288 | String commaReplacedMasklist = masklist;
|
---|
5289 | string::size_type pos = 0;
|
---|
5290 | while (pos = commaReplacedMasklist.find(","), pos != string::npos) {
|
---|
5291 | commaReplacedMasklist.replace(pos, 1, ";");
|
---|
5292 | pos++;
|
---|
5293 | }
|
---|
5294 | oss << commaReplacedMasklist << ",";
|
---|
5295 | } else {
|
---|
5296 | oss << " Scan[" << getScan(whichrow) << "]";
|
---|
5297 | oss << " Beam[" << getBeam(whichrow) << "]";
|
---|
5298 | oss << " IF[" << getIF(whichrow) << "]";
|
---|
5299 | oss << " Pol[" << getPol(whichrow) << "]";
|
---|
5300 | oss << " Cycle[" << getCycle(whichrow) << "]: " << endl;
|
---|
5301 | oss << "Fitter range = " << masklist << endl;
|
---|
5302 | oss << "Baseline parameters" << endl;
|
---|
5303 | }
|
---|
5304 | oss << flush;
|
---|
5305 |
|
---|
5306 | return String(oss);
|
---|
5307 | }
|
---|
5308 |
|
---|
5309 | return "";
|
---|
5310 | }
|
---|
5311 |
|
---|
5312 | std::string Scantable::formatBaselineParamsFooter(float rms, int nClipped, bool verbose, bool csvformat) const
|
---|
5313 | {
|
---|
5314 | if (verbose) {
|
---|
5315 | ostringstream oss;
|
---|
5316 |
|
---|
5317 | if (csvformat) {
|
---|
5318 | oss << rms << ",";
|
---|
5319 | if (nClipped >= 0) {
|
---|
5320 | oss << nClipped;
|
---|
5321 | }
|
---|
5322 | } else {
|
---|
5323 | oss << "Results of baseline fit" << endl;
|
---|
5324 | oss << " rms = " << setprecision(6) << rms << endl;
|
---|
5325 | if (nClipped >= 0) {
|
---|
5326 | oss << " Number of clipped channels = " << nClipped << endl;
|
---|
5327 | }
|
---|
5328 | for (int i = 0; i < 60; ++i) {
|
---|
5329 | oss << "-";
|
---|
5330 | }
|
---|
5331 | }
|
---|
5332 | oss << endl;
|
---|
5333 | oss << flush;
|
---|
5334 |
|
---|
5335 | return String(oss);
|
---|
5336 | }
|
---|
5337 |
|
---|
5338 | return "";
|
---|
5339 | }
|
---|
5340 |
|
---|
5341 | std::string Scantable::formatBaselineParams(const std::vector<float>& params,
|
---|
5342 | const std::vector<bool>& fixed,
|
---|
5343 | float rms,
|
---|
5344 | int nClipped,
|
---|
5345 | const std::string& masklist,
|
---|
5346 | int whichrow,
|
---|
5347 | bool verbose,
|
---|
5348 | bool csvformat,
|
---|
5349 | int start, int count,
|
---|
5350 | bool resetparamid) const
|
---|
5351 | {
|
---|
5352 | int nParam = (int)(params.size());
|
---|
5353 |
|
---|
5354 | if (nParam < 1) {
|
---|
5355 | return(" Not fitted");
|
---|
5356 | } else {
|
---|
5357 |
|
---|
5358 | ostringstream oss;
|
---|
5359 | oss << formatBaselineParamsHeader(whichrow, masklist, verbose, csvformat);
|
---|
5360 |
|
---|
5361 | if (start < 0) start = 0;
|
---|
5362 | if (count < 0) count = nParam;
|
---|
5363 | int end = start + count;
|
---|
5364 | if (end > nParam) end = nParam;
|
---|
5365 | int paramidoffset = (resetparamid) ? (-start) : 0;
|
---|
5366 |
|
---|
5367 | for (int i = start; i < end; ++i) {
|
---|
5368 | if (i > start) {
|
---|
5369 | oss << ",";
|
---|
5370 | }
|
---|
5371 | std::string sFix = ((fixed.size() > 0) && (fixed[i]) && verbose) ? "(fixed)" : "";
|
---|
5372 | if (csvformat) {
|
---|
5373 | oss << params[i] << sFix;
|
---|
5374 | } else {
|
---|
5375 | oss << " p" << (i+paramidoffset) << sFix << "= " << right << setw(13) << setprecision(6) << params[i];
|
---|
5376 | }
|
---|
5377 | }
|
---|
5378 |
|
---|
5379 | if (csvformat) {
|
---|
5380 | oss << ",";
|
---|
5381 | } else {
|
---|
5382 | oss << endl;
|
---|
5383 | }
|
---|
5384 | oss << formatBaselineParamsFooter(rms, nClipped, verbose, csvformat);
|
---|
5385 |
|
---|
5386 | return String(oss);
|
---|
5387 | }
|
---|
5388 |
|
---|
5389 | }
|
---|
5390 |
|
---|
5391 | std::string Scantable::formatPiecewiseBaselineParams(const std::vector<int>& ranges, const std::vector<float>& params, const std::vector<bool>& fixed, float rms, int nClipped, const std::string& masklist, int whichrow, bool verbose, bool csvformat) const
|
---|
5392 | {
|
---|
5393 | int nOutParam = (int)(params.size());
|
---|
5394 | int nPiece = (int)(ranges.size()) - 1;
|
---|
5395 |
|
---|
5396 | if (nOutParam < 1) {
|
---|
5397 | return(" Not fitted");
|
---|
5398 | } else if (nPiece < 0) {
|
---|
5399 | return formatBaselineParams(params, fixed, rms, nClipped, masklist, whichrow, verbose, csvformat);
|
---|
5400 | } else if (nPiece < 1) {
|
---|
5401 | return(" Bad count of the piece edge info");
|
---|
5402 | } else if (nOutParam % nPiece != 0) {
|
---|
5403 | return(" Bad count of the output baseline parameters");
|
---|
5404 | } else {
|
---|
5405 |
|
---|
5406 | int nParam = nOutParam / nPiece;
|
---|
5407 |
|
---|
5408 | ostringstream oss;
|
---|
5409 | oss << formatBaselineParamsHeader(whichrow, masklist, verbose, csvformat);
|
---|
5410 |
|
---|
5411 | if (csvformat) {
|
---|
5412 | for (int i = 0; i < nPiece; ++i) {
|
---|
5413 | oss << ranges[i] << "," << (ranges[i+1]-1) << ",";
|
---|
5414 | oss << formatBaselineParams(params, fixed, rms, 0, masklist, whichrow, false, csvformat, i*nParam, nParam, true);
|
---|
5415 | }
|
---|
5416 | } else {
|
---|
5417 | stringstream ss;
|
---|
5418 | ss << ranges[nPiece] << flush;
|
---|
5419 | int wRange = ss.str().size() * 2 + 5;
|
---|
5420 |
|
---|
5421 | for (int i = 0; i < nPiece; ++i) {
|
---|
5422 | ss.str("");
|
---|
5423 | ss << " [" << ranges[i] << "," << (ranges[i+1]-1) << "]";
|
---|
5424 | oss << left << setw(wRange) << ss.str();
|
---|
5425 | oss << formatBaselineParams(params, fixed, rms, 0, masklist, whichrow, false, csvformat, i*nParam, nParam, true);
|
---|
5426 | //oss << endl;
|
---|
5427 | }
|
---|
5428 | }
|
---|
5429 |
|
---|
5430 | oss << formatBaselineParamsFooter(rms, nClipped, verbose, csvformat);
|
---|
5431 |
|
---|
5432 | return String(oss);
|
---|
5433 | }
|
---|
5434 |
|
---|
5435 | }
|
---|
5436 |
|
---|
5437 | bool Scantable::hasSameNchanOverIFs()
|
---|
5438 | {
|
---|
5439 | int nIF = nif(-1);
|
---|
5440 | int nCh;
|
---|
5441 | int totalPositiveNChan = 0;
|
---|
5442 | int nPositiveNChan = 0;
|
---|
5443 |
|
---|
5444 | for (int i = 0; i < nIF; ++i) {
|
---|
5445 | nCh = nchan(i);
|
---|
5446 | if (nCh > 0) {
|
---|
5447 | totalPositiveNChan += nCh;
|
---|
5448 | nPositiveNChan++;
|
---|
5449 | }
|
---|
5450 | }
|
---|
5451 |
|
---|
5452 | return (totalPositiveNChan == (nPositiveNChan * nchan(0)));
|
---|
5453 | }
|
---|
5454 |
|
---|
5455 | std::string Scantable::getMaskRangeList(const std::vector<bool>& mask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, bool verbose)
|
---|
5456 | {
|
---|
5457 | if (mask.size() <= 0) {
|
---|
5458 | throw(AipsError("The mask elements should be > 0"));
|
---|
5459 | }
|
---|
5460 | int IF = getIF(whichrow);
|
---|
5461 | if (mask.size() != (uInt)nchan(IF)) {
|
---|
5462 | throw(AipsError("Number of channels in scantable != number of mask elements"));
|
---|
5463 | }
|
---|
5464 |
|
---|
5465 | if (verbose) {
|
---|
5466 | LogIO logOs(LogOrigin("Scantable", "getMaskRangeList()", WHERE));
|
---|
5467 | logOs << LogIO::WARN << "The current mask window unit is " << coordInfo;
|
---|
5468 | if (!hasSameNchan) {
|
---|
5469 | logOs << endl << "This mask is only valid for IF=" << IF;
|
---|
5470 | }
|
---|
5471 | logOs << LogIO::POST;
|
---|
5472 | }
|
---|
5473 |
|
---|
5474 | std::vector<double> abcissa = getAbcissa(whichrow);
|
---|
5475 | std::vector<int> edge = getMaskEdgeIndices(mask);
|
---|
5476 |
|
---|
5477 | ostringstream oss;
|
---|
5478 | oss.setf(ios::fixed);
|
---|
5479 | oss << setprecision(1) << "[";
|
---|
5480 | for (uInt i = 0; i < edge.size(); i+=2) {
|
---|
5481 | if (i > 0) oss << ",";
|
---|
5482 | oss << "[" << (float)abcissa[edge[i]] << "," << (float)abcissa[edge[i+1]] << "]";
|
---|
5483 | }
|
---|
5484 | oss << "]" << flush;
|
---|
5485 |
|
---|
5486 | return String(oss);
|
---|
5487 | }
|
---|
5488 |
|
---|
5489 | std::vector<int> Scantable::getMaskEdgeIndices(const std::vector<bool>& mask)
|
---|
5490 | {
|
---|
5491 | if (mask.size() <= 0) {
|
---|
5492 | throw(AipsError("The mask elements should be > 0"));
|
---|
5493 | }
|
---|
5494 |
|
---|
5495 | std::vector<int> out, startIndices, endIndices;
|
---|
5496 | int maskSize = mask.size();
|
---|
5497 |
|
---|
5498 | startIndices.clear();
|
---|
5499 | endIndices.clear();
|
---|
5500 |
|
---|
5501 | if (mask[0]) {
|
---|
5502 | startIndices.push_back(0);
|
---|
5503 | }
|
---|
5504 | for (int i = 1; i < maskSize; ++i) {
|
---|
5505 | if ((!mask[i-1]) && mask[i]) {
|
---|
5506 | startIndices.push_back(i);
|
---|
5507 | } else if (mask[i-1] && (!mask[i])) {
|
---|
5508 | endIndices.push_back(i-1);
|
---|
5509 | }
|
---|
5510 | }
|
---|
5511 | if (mask[maskSize-1]) {
|
---|
5512 | endIndices.push_back(maskSize-1);
|
---|
5513 | }
|
---|
5514 |
|
---|
5515 | if (startIndices.size() != endIndices.size()) {
|
---|
5516 | throw(AipsError("Inconsistent Mask Size: bad data?"));
|
---|
5517 | }
|
---|
5518 | for (uInt i = 0; i < startIndices.size(); ++i) {
|
---|
5519 | if (startIndices[i] > endIndices[i]) {
|
---|
5520 | throw(AipsError("Mask start index > mask end index"));
|
---|
5521 | }
|
---|
5522 | }
|
---|
5523 |
|
---|
5524 | out.clear();
|
---|
5525 | for (uInt i = 0; i < startIndices.size(); ++i) {
|
---|
5526 | out.push_back(startIndices[i]);
|
---|
5527 | out.push_back(endIndices[i]);
|
---|
5528 | }
|
---|
5529 |
|
---|
5530 | return out;
|
---|
5531 | }
|
---|
5532 |
|
---|
5533 | void Scantable::setTsys(const std::vector<float>& newvals, int whichrow) {
|
---|
5534 | Vector<Float> tsys(newvals);
|
---|
5535 | if (whichrow > -1) {
|
---|
5536 | if (tsysCol_.shape(whichrow) != tsys.shape())
|
---|
5537 | throw(AipsError("Given Tsys values are not of the same shape"));
|
---|
5538 | tsysCol_.put(whichrow, tsys);
|
---|
5539 | } else {
|
---|
5540 | tsysCol_.fillColumn(tsys);
|
---|
5541 | }
|
---|
5542 | }
|
---|
5543 |
|
---|
5544 | vector<float> Scantable::getTsysSpectrum( int whichrow ) const
|
---|
5545 | {
|
---|
5546 | Vector<Float> tsys( tsysCol_(whichrow) ) ;
|
---|
5547 | vector<float> stlTsys ;
|
---|
5548 | tsys.tovector( stlTsys ) ;
|
---|
5549 | return stlTsys ;
|
---|
5550 | }
|
---|
5551 |
|
---|
5552 | vector<uint> Scantable::getMoleculeIdColumnData() const
|
---|
5553 | {
|
---|
5554 | Vector<uInt> molIds(mmolidCol_.getColumn());
|
---|
5555 | vector<uint> res;
|
---|
5556 | molIds.tovector(res);
|
---|
5557 | return res;
|
---|
5558 | }
|
---|
5559 |
|
---|
5560 | void Scantable::setMoleculeIdColumnData(const std::vector<uint>& molids)
|
---|
5561 | {
|
---|
5562 | Vector<uInt> molIds(molids);
|
---|
5563 | Vector<uInt> arr(mmolidCol_.getColumn());
|
---|
5564 | if ( molIds.nelements() != arr.nelements() )
|
---|
5565 | throw AipsError("The input data size must be the number of rows.");
|
---|
5566 | mmolidCol_.putColumn(molIds);
|
---|
5567 | }
|
---|
5568 |
|
---|
5569 |
|
---|
5570 | std::vector<uint> Scantable::getRootTableRowNumbers() const
|
---|
5571 | {
|
---|
5572 | Vector<uInt> rowIds(table_.rowNumbers());
|
---|
5573 | vector<uint> res;
|
---|
5574 | rowIds.tovector(res);
|
---|
5575 | return res;
|
---|
5576 | }
|
---|
5577 |
|
---|
5578 |
|
---|
5579 | void Scantable::dropXPol()
|
---|
5580 | {
|
---|
5581 | if (npol() <= 2) {
|
---|
5582 | return;
|
---|
5583 | }
|
---|
5584 | if (!selector_.empty()) {
|
---|
5585 | throw AipsError("Can only operate with empty selection");
|
---|
5586 | }
|
---|
5587 | std::string taql = "SELECT FROM $1 WHERE POLNO IN [0,1]";
|
---|
5588 | Table tab = tableCommand(taql, table_);
|
---|
5589 | table_ = tab;
|
---|
5590 | table_.rwKeywordSet().define("nPol", Int(2));
|
---|
5591 | originalTable_ = table_;
|
---|
5592 | attach();
|
---|
5593 | }
|
---|
5594 |
|
---|
5595 | }
|
---|
5596 | //namespace asap
|
---|