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