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 <fstream>
|
---|
14 |
|
---|
15 | #include <casa/aips.h>
|
---|
16 | #include <casa/iostream.h>
|
---|
17 | #include <casa/iomanip.h>
|
---|
18 | #include <casa/OS/Path.h>
|
---|
19 | #include <casa/OS/File.h>
|
---|
20 | #include <casa/Arrays/Array.h>
|
---|
21 | #include <casa/Arrays/ArrayMath.h>
|
---|
22 | #include <casa/Arrays/MaskArrMath.h>
|
---|
23 | #include <casa/Arrays/ArrayLogical.h>
|
---|
24 | #include <casa/Arrays/ArrayAccessor.h>
|
---|
25 | #include <casa/Arrays/Vector.h>
|
---|
26 | #include <casa/Arrays/VectorSTLIterator.h>
|
---|
27 | #include <casa/Arrays/Slice.h>
|
---|
28 | #include <casa/BasicMath/Math.h>
|
---|
29 | #include <casa/BasicSL/Constants.h>
|
---|
30 | #include <casa/Quanta/MVAngle.h>
|
---|
31 | #include <casa/Containers/RecordField.h>
|
---|
32 | #include <casa/Utilities/GenSort.h>
|
---|
33 | #include <casa/Logging/LogIO.h>
|
---|
34 |
|
---|
35 | #include <tables/Tables/TableParse.h>
|
---|
36 | #include <tables/Tables/TableDesc.h>
|
---|
37 | #include <tables/Tables/TableCopy.h>
|
---|
38 | #include <tables/Tables/SetupNewTab.h>
|
---|
39 | #include <tables/Tables/ScaColDesc.h>
|
---|
40 | #include <tables/Tables/ArrColDesc.h>
|
---|
41 | #include <tables/Tables/TableRow.h>
|
---|
42 | #include <tables/Tables/TableVector.h>
|
---|
43 | #include <tables/Tables/TableIter.h>
|
---|
44 |
|
---|
45 | #include <tables/Tables/ExprNode.h>
|
---|
46 | #include <tables/Tables/TableRecord.h>
|
---|
47 | #include <casa/Quanta/MVTime.h>
|
---|
48 | #include <casa/Quanta/MVAngle.h>
|
---|
49 | #include <measures/Measures/MeasRef.h>
|
---|
50 | #include <measures/Measures/MeasTable.h>
|
---|
51 | // needed to avoid error in .tcc
|
---|
52 | #include <measures/Measures/MCDirection.h>
|
---|
53 | //
|
---|
54 | #include <measures/Measures/MDirection.h>
|
---|
55 | #include <measures/Measures/MFrequency.h>
|
---|
56 | #include <measures/Measures/MEpoch.h>
|
---|
57 | #include <measures/TableMeasures/TableMeasRefDesc.h>
|
---|
58 | #include <measures/TableMeasures/TableMeasValueDesc.h>
|
---|
59 | #include <measures/TableMeasures/TableMeasDesc.h>
|
---|
60 | #include <measures/TableMeasures/ScalarMeasColumn.h>
|
---|
61 | #include <coordinates/Coordinates/CoordinateUtil.h>
|
---|
62 |
|
---|
63 | #include <atnf/PKSIO/SrcType.h>
|
---|
64 | #include "Scantable.h"
|
---|
65 | #include "STPolLinear.h"
|
---|
66 | #include "STPolCircular.h"
|
---|
67 | #include "STPolStokes.h"
|
---|
68 | #include "STAttr.h"
|
---|
69 | #include "STLineFinder.h"
|
---|
70 | #include "MathUtils.h"
|
---|
71 |
|
---|
72 | using namespace casa;
|
---|
73 |
|
---|
74 | namespace asap {
|
---|
75 |
|
---|
76 | std::map<std::string, STPol::STPolFactory *> Scantable::factories_;
|
---|
77 |
|
---|
78 | void Scantable::initFactories() {
|
---|
79 | if ( factories_.empty() ) {
|
---|
80 | Scantable::factories_["linear"] = &STPolLinear::myFactory;
|
---|
81 | Scantable::factories_["circular"] = &STPolCircular::myFactory;
|
---|
82 | Scantable::factories_["stokes"] = &STPolStokes::myFactory;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | Scantable::Scantable(Table::TableType ttype) :
|
---|
87 | type_(ttype)
|
---|
88 | {
|
---|
89 | initFactories();
|
---|
90 | setupMainTable();
|
---|
91 | freqTable_ = STFrequencies(*this);
|
---|
92 | table_.rwKeywordSet().defineTable("FREQUENCIES", freqTable_.table());
|
---|
93 | weatherTable_ = STWeather(*this);
|
---|
94 | table_.rwKeywordSet().defineTable("WEATHER", weatherTable_.table());
|
---|
95 | focusTable_ = STFocus(*this);
|
---|
96 | table_.rwKeywordSet().defineTable("FOCUS", focusTable_.table());
|
---|
97 | tcalTable_ = STTcal(*this);
|
---|
98 | table_.rwKeywordSet().defineTable("TCAL", tcalTable_.table());
|
---|
99 | moleculeTable_ = STMolecules(*this);
|
---|
100 | table_.rwKeywordSet().defineTable("MOLECULES", moleculeTable_.table());
|
---|
101 | historyTable_ = STHistory(*this);
|
---|
102 | table_.rwKeywordSet().defineTable("HISTORY", historyTable_.table());
|
---|
103 | fitTable_ = STFit(*this);
|
---|
104 | table_.rwKeywordSet().defineTable("FIT", fitTable_.table());
|
---|
105 | table_.tableInfo().setType( "Scantable" ) ;
|
---|
106 | originalTable_ = table_;
|
---|
107 | attach();
|
---|
108 | }
|
---|
109 |
|
---|
110 | Scantable::Scantable(const std::string& name, Table::TableType ttype) :
|
---|
111 | type_(ttype)
|
---|
112 | {
|
---|
113 | initFactories();
|
---|
114 |
|
---|
115 | Table tab(name, Table::Update);
|
---|
116 | uInt version = tab.keywordSet().asuInt("VERSION");
|
---|
117 | if (version != version_) {
|
---|
118 | throw(AipsError("Unsupported version of ASAP file."));
|
---|
119 | }
|
---|
120 | if ( type_ == Table::Memory ) {
|
---|
121 | table_ = tab.copyToMemoryTable(generateName());
|
---|
122 | } else {
|
---|
123 | table_ = tab;
|
---|
124 | }
|
---|
125 | table_.tableInfo().setType( "Scantable" ) ;
|
---|
126 |
|
---|
127 | attachSubtables();
|
---|
128 | originalTable_ = table_;
|
---|
129 | attach();
|
---|
130 | }
|
---|
131 | /*
|
---|
132 | Scantable::Scantable(const std::string& name, Table::TableType ttype) :
|
---|
133 | type_(ttype)
|
---|
134 | {
|
---|
135 | initFactories();
|
---|
136 | Table tab(name, Table::Update);
|
---|
137 | uInt version = tab.keywordSet().asuInt("VERSION");
|
---|
138 | if (version != version_) {
|
---|
139 | throw(AipsError("Unsupported version of ASAP file."));
|
---|
140 | }
|
---|
141 | if ( type_ == Table::Memory ) {
|
---|
142 | table_ = tab.copyToMemoryTable(generateName());
|
---|
143 | } else {
|
---|
144 | table_ = tab;
|
---|
145 | }
|
---|
146 |
|
---|
147 | attachSubtables();
|
---|
148 | originalTable_ = table_;
|
---|
149 | attach();
|
---|
150 | }
|
---|
151 | */
|
---|
152 |
|
---|
153 | Scantable::Scantable( const Scantable& other, bool clear )
|
---|
154 | {
|
---|
155 | // with or without data
|
---|
156 | String newname = String(generateName());
|
---|
157 | type_ = other.table_.tableType();
|
---|
158 | if ( other.table_.tableType() == Table::Memory ) {
|
---|
159 | if ( clear ) {
|
---|
160 | table_ = TableCopy::makeEmptyMemoryTable(newname,
|
---|
161 | other.table_, True);
|
---|
162 | } else
|
---|
163 | table_ = other.table_.copyToMemoryTable(newname);
|
---|
164 | } else {
|
---|
165 | other.table_.deepCopy(newname, Table::New, False,
|
---|
166 | other.table_.endianFormat(),
|
---|
167 | Bool(clear));
|
---|
168 | table_ = Table(newname, Table::Update);
|
---|
169 | table_.markForDelete();
|
---|
170 | }
|
---|
171 | table_.tableInfo().setType( "Scantable" ) ;
|
---|
172 | /// @todo reindex SCANNO, recompute nbeam, nif, npol
|
---|
173 | if ( clear ) copySubtables(other);
|
---|
174 | attachSubtables();
|
---|
175 | originalTable_ = table_;
|
---|
176 | attach();
|
---|
177 | }
|
---|
178 |
|
---|
179 | void Scantable::copySubtables(const Scantable& other) {
|
---|
180 | Table t = table_.rwKeywordSet().asTable("FREQUENCIES");
|
---|
181 | TableCopy::copyRows(t, other.freqTable_.table());
|
---|
182 | t = table_.rwKeywordSet().asTable("FOCUS");
|
---|
183 | TableCopy::copyRows(t, other.focusTable_.table());
|
---|
184 | t = table_.rwKeywordSet().asTable("WEATHER");
|
---|
185 | TableCopy::copyRows(t, other.weatherTable_.table());
|
---|
186 | t = table_.rwKeywordSet().asTable("TCAL");
|
---|
187 | TableCopy::copyRows(t, other.tcalTable_.table());
|
---|
188 | t = table_.rwKeywordSet().asTable("MOLECULES");
|
---|
189 | TableCopy::copyRows(t, other.moleculeTable_.table());
|
---|
190 | t = table_.rwKeywordSet().asTable("HISTORY");
|
---|
191 | TableCopy::copyRows(t, other.historyTable_.table());
|
---|
192 | t = table_.rwKeywordSet().asTable("FIT");
|
---|
193 | TableCopy::copyRows(t, other.fitTable_.table());
|
---|
194 | }
|
---|
195 |
|
---|
196 | void Scantable::attachSubtables()
|
---|
197 | {
|
---|
198 | freqTable_ = STFrequencies(table_);
|
---|
199 | focusTable_ = STFocus(table_);
|
---|
200 | weatherTable_ = STWeather(table_);
|
---|
201 | tcalTable_ = STTcal(table_);
|
---|
202 | moleculeTable_ = STMolecules(table_);
|
---|
203 | historyTable_ = STHistory(table_);
|
---|
204 | fitTable_ = STFit(table_);
|
---|
205 | }
|
---|
206 |
|
---|
207 | Scantable::~Scantable()
|
---|
208 | {
|
---|
209 | //cout << "~Scantable() " << this << endl;
|
---|
210 | }
|
---|
211 |
|
---|
212 | void Scantable::setupMainTable()
|
---|
213 | {
|
---|
214 | TableDesc td("", "1", TableDesc::Scratch);
|
---|
215 | td.comment() = "An ASAP Scantable";
|
---|
216 | td.rwKeywordSet().define("VERSION", uInt(version_));
|
---|
217 |
|
---|
218 | // n Cycles
|
---|
219 | td.addColumn(ScalarColumnDesc<uInt>("SCANNO"));
|
---|
220 | // new index every nBeam x nIF x nPol
|
---|
221 | td.addColumn(ScalarColumnDesc<uInt>("CYCLENO"));
|
---|
222 |
|
---|
223 | td.addColumn(ScalarColumnDesc<uInt>("BEAMNO"));
|
---|
224 | td.addColumn(ScalarColumnDesc<uInt>("IFNO"));
|
---|
225 | // linear, circular, stokes
|
---|
226 | td.rwKeywordSet().define("POLTYPE", String("linear"));
|
---|
227 | td.addColumn(ScalarColumnDesc<uInt>("POLNO"));
|
---|
228 |
|
---|
229 | td.addColumn(ScalarColumnDesc<uInt>("FREQ_ID"));
|
---|
230 | td.addColumn(ScalarColumnDesc<uInt>("MOLECULE_ID"));
|
---|
231 |
|
---|
232 | ScalarColumnDesc<Int> refbeamnoColumn("REFBEAMNO");
|
---|
233 | refbeamnoColumn.setDefault(Int(-1));
|
---|
234 | td.addColumn(refbeamnoColumn);
|
---|
235 |
|
---|
236 | ScalarColumnDesc<uInt> flagrowColumn("FLAGROW");
|
---|
237 | flagrowColumn.setDefault(uInt(0));
|
---|
238 | td.addColumn(flagrowColumn);
|
---|
239 |
|
---|
240 | td.addColumn(ScalarColumnDesc<Double>("TIME"));
|
---|
241 | TableMeasRefDesc measRef(MEpoch::UTC); // UTC as default
|
---|
242 | TableMeasValueDesc measVal(td, "TIME");
|
---|
243 | TableMeasDesc<MEpoch> mepochCol(measVal, measRef);
|
---|
244 | mepochCol.write(td);
|
---|
245 |
|
---|
246 | td.addColumn(ScalarColumnDesc<Double>("INTERVAL"));
|
---|
247 |
|
---|
248 | td.addColumn(ScalarColumnDesc<String>("SRCNAME"));
|
---|
249 | // Type of source (on=0, off=1, other=-1)
|
---|
250 | ScalarColumnDesc<Int> stypeColumn("SRCTYPE");
|
---|
251 | stypeColumn.setDefault(Int(-1));
|
---|
252 | td.addColumn(stypeColumn);
|
---|
253 | td.addColumn(ScalarColumnDesc<String>("FIELDNAME"));
|
---|
254 |
|
---|
255 | //The actual Data Vectors
|
---|
256 | td.addColumn(ArrayColumnDesc<Float>("SPECTRA"));
|
---|
257 | td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA"));
|
---|
258 | td.addColumn(ArrayColumnDesc<Float>("TSYS"));
|
---|
259 |
|
---|
260 | td.addColumn(ArrayColumnDesc<Double>("DIRECTION",
|
---|
261 | IPosition(1,2),
|
---|
262 | ColumnDesc::Direct));
|
---|
263 | TableMeasRefDesc mdirRef(MDirection::J2000); // default
|
---|
264 | TableMeasValueDesc tmvdMDir(td, "DIRECTION");
|
---|
265 | // the TableMeasDesc gives the column a type
|
---|
266 | TableMeasDesc<MDirection> mdirCol(tmvdMDir, mdirRef);
|
---|
267 | // a uder set table type e.g. GALCTIC, B1950 ...
|
---|
268 | td.rwKeywordSet().define("DIRECTIONREF", String("J2000"));
|
---|
269 | // writing create the measure column
|
---|
270 | mdirCol.write(td);
|
---|
271 | td.addColumn(ScalarColumnDesc<Float>("AZIMUTH"));
|
---|
272 | td.addColumn(ScalarColumnDesc<Float>("ELEVATION"));
|
---|
273 | td.addColumn(ScalarColumnDesc<Float>("OPACITY"));
|
---|
274 |
|
---|
275 | td.addColumn(ScalarColumnDesc<uInt>("TCAL_ID"));
|
---|
276 | ScalarColumnDesc<Int> fitColumn("FIT_ID");
|
---|
277 | fitColumn.setDefault(Int(-1));
|
---|
278 | td.addColumn(fitColumn);
|
---|
279 |
|
---|
280 | td.addColumn(ScalarColumnDesc<uInt>("FOCUS_ID"));
|
---|
281 | td.addColumn(ScalarColumnDesc<uInt>("WEATHER_ID"));
|
---|
282 |
|
---|
283 | // columns which just get dragged along, as they aren't used in asap
|
---|
284 | td.addColumn(ScalarColumnDesc<Double>("SRCVELOCITY"));
|
---|
285 | td.addColumn(ArrayColumnDesc<Double>("SRCPROPERMOTION"));
|
---|
286 | td.addColumn(ArrayColumnDesc<Double>("SRCDIRECTION"));
|
---|
287 | td.addColumn(ArrayColumnDesc<Double>("SCANRATE"));
|
---|
288 |
|
---|
289 | td.rwKeywordSet().define("OBSMODE", String(""));
|
---|
290 |
|
---|
291 | // Now create Table SetUp from the description.
|
---|
292 | SetupNewTable aNewTab(generateName(), td, Table::Scratch);
|
---|
293 | table_ = Table(aNewTab, type_, 0);
|
---|
294 | originalTable_ = table_;
|
---|
295 | }
|
---|
296 |
|
---|
297 | void Scantable::attach()
|
---|
298 | {
|
---|
299 | timeCol_.attach(table_, "TIME");
|
---|
300 | srcnCol_.attach(table_, "SRCNAME");
|
---|
301 | srctCol_.attach(table_, "SRCTYPE");
|
---|
302 | specCol_.attach(table_, "SPECTRA");
|
---|
303 | flagsCol_.attach(table_, "FLAGTRA");
|
---|
304 | tsysCol_.attach(table_, "TSYS");
|
---|
305 | cycleCol_.attach(table_,"CYCLENO");
|
---|
306 | scanCol_.attach(table_, "SCANNO");
|
---|
307 | beamCol_.attach(table_, "BEAMNO");
|
---|
308 | ifCol_.attach(table_, "IFNO");
|
---|
309 | polCol_.attach(table_, "POLNO");
|
---|
310 | integrCol_.attach(table_, "INTERVAL");
|
---|
311 | azCol_.attach(table_, "AZIMUTH");
|
---|
312 | elCol_.attach(table_, "ELEVATION");
|
---|
313 | dirCol_.attach(table_, "DIRECTION");
|
---|
314 | fldnCol_.attach(table_, "FIELDNAME");
|
---|
315 | rbeamCol_.attach(table_, "REFBEAMNO");
|
---|
316 |
|
---|
317 | mweatheridCol_.attach(table_,"WEATHER_ID");
|
---|
318 | mfitidCol_.attach(table_,"FIT_ID");
|
---|
319 | mfreqidCol_.attach(table_, "FREQ_ID");
|
---|
320 | mtcalidCol_.attach(table_, "TCAL_ID");
|
---|
321 | mfocusidCol_.attach(table_, "FOCUS_ID");
|
---|
322 | mmolidCol_.attach(table_, "MOLECULE_ID");
|
---|
323 |
|
---|
324 | //Add auxiliary column for row-based flagging (CAS-1433 Wataru Kawasaki)
|
---|
325 | attachAuxColumnDef(flagrowCol_, "FLAGROW", 0);
|
---|
326 |
|
---|
327 | }
|
---|
328 |
|
---|
329 | template<class T, class T2>
|
---|
330 | void Scantable::attachAuxColumnDef(ScalarColumn<T>& col,
|
---|
331 | const String& colName,
|
---|
332 | const T2& defValue)
|
---|
333 | {
|
---|
334 | try {
|
---|
335 | col.attach(table_, colName);
|
---|
336 | } catch (TableError& err) {
|
---|
337 | String errMesg = err.getMesg();
|
---|
338 | if (errMesg == "Table column " + colName + " is unknown") {
|
---|
339 | table_.addColumn(ScalarColumnDesc<T>(colName));
|
---|
340 | col.attach(table_, colName);
|
---|
341 | col.fillColumn(static_cast<T>(defValue));
|
---|
342 | } else {
|
---|
343 | throw;
|
---|
344 | }
|
---|
345 | } catch (...) {
|
---|
346 | throw;
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | template<class T, class T2>
|
---|
351 | void Scantable::attachAuxColumnDef(ArrayColumn<T>& col,
|
---|
352 | const String& colName,
|
---|
353 | const Array<T2>& defValue)
|
---|
354 | {
|
---|
355 | try {
|
---|
356 | col.attach(table_, colName);
|
---|
357 | } catch (TableError& err) {
|
---|
358 | String errMesg = err.getMesg();
|
---|
359 | if (errMesg == "Table column " + colName + " is unknown") {
|
---|
360 | table_.addColumn(ArrayColumnDesc<T>(colName));
|
---|
361 | col.attach(table_, colName);
|
---|
362 |
|
---|
363 | int size = 0;
|
---|
364 | ArrayIterator<T2>& it = defValue.begin();
|
---|
365 | while (it != defValue.end()) {
|
---|
366 | ++size;
|
---|
367 | ++it;
|
---|
368 | }
|
---|
369 | IPosition ip(1, size);
|
---|
370 | Array<T>& arr(ip);
|
---|
371 | for (int i = 0; i < size; ++i)
|
---|
372 | arr[i] = static_cast<T>(defValue[i]);
|
---|
373 |
|
---|
374 | col.fillColumn(arr);
|
---|
375 | } else {
|
---|
376 | throw;
|
---|
377 | }
|
---|
378 | } catch (...) {
|
---|
379 | throw;
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | void Scantable::setHeader(const STHeader& sdh)
|
---|
384 | {
|
---|
385 | table_.rwKeywordSet().define("nIF", sdh.nif);
|
---|
386 | table_.rwKeywordSet().define("nBeam", sdh.nbeam);
|
---|
387 | table_.rwKeywordSet().define("nPol", sdh.npol);
|
---|
388 | table_.rwKeywordSet().define("nChan", sdh.nchan);
|
---|
389 | table_.rwKeywordSet().define("Observer", sdh.observer);
|
---|
390 | table_.rwKeywordSet().define("Project", sdh.project);
|
---|
391 | table_.rwKeywordSet().define("Obstype", sdh.obstype);
|
---|
392 | table_.rwKeywordSet().define("AntennaName", sdh.antennaname);
|
---|
393 | table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition);
|
---|
394 | table_.rwKeywordSet().define("Equinox", sdh.equinox);
|
---|
395 | table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref);
|
---|
396 | table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq);
|
---|
397 | table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth);
|
---|
398 | table_.rwKeywordSet().define("UTC", sdh.utc);
|
---|
399 | table_.rwKeywordSet().define("FluxUnit", sdh.fluxunit);
|
---|
400 | table_.rwKeywordSet().define("Epoch", sdh.epoch);
|
---|
401 | table_.rwKeywordSet().define("POLTYPE", sdh.poltype);
|
---|
402 | }
|
---|
403 |
|
---|
404 | STHeader Scantable::getHeader() const
|
---|
405 | {
|
---|
406 | STHeader sdh;
|
---|
407 | table_.keywordSet().get("nBeam",sdh.nbeam);
|
---|
408 | table_.keywordSet().get("nIF",sdh.nif);
|
---|
409 | table_.keywordSet().get("nPol",sdh.npol);
|
---|
410 | table_.keywordSet().get("nChan",sdh.nchan);
|
---|
411 | table_.keywordSet().get("Observer", sdh.observer);
|
---|
412 | table_.keywordSet().get("Project", sdh.project);
|
---|
413 | table_.keywordSet().get("Obstype", sdh.obstype);
|
---|
414 | table_.keywordSet().get("AntennaName", sdh.antennaname);
|
---|
415 | table_.keywordSet().get("AntennaPosition", sdh.antennaposition);
|
---|
416 | table_.keywordSet().get("Equinox", sdh.equinox);
|
---|
417 | table_.keywordSet().get("FreqRefFrame", sdh.freqref);
|
---|
418 | table_.keywordSet().get("FreqRefVal", sdh.reffreq);
|
---|
419 | table_.keywordSet().get("Bandwidth", sdh.bandwidth);
|
---|
420 | table_.keywordSet().get("UTC", sdh.utc);
|
---|
421 | table_.keywordSet().get("FluxUnit", sdh.fluxunit);
|
---|
422 | table_.keywordSet().get("Epoch", sdh.epoch);
|
---|
423 | table_.keywordSet().get("POLTYPE", sdh.poltype);
|
---|
424 | return sdh;
|
---|
425 | }
|
---|
426 |
|
---|
427 | void Scantable::setSourceType( int stype )
|
---|
428 | {
|
---|
429 | if ( stype < 0 || stype > 1 )
|
---|
430 | throw(AipsError("Illegal sourcetype."));
|
---|
431 | TableVector<Int> tabvec(table_, "SRCTYPE");
|
---|
432 | tabvec = Int(stype);
|
---|
433 | }
|
---|
434 |
|
---|
435 | bool Scantable::conformant( const Scantable& other )
|
---|
436 | {
|
---|
437 | return this->getHeader().conformant(other.getHeader());
|
---|
438 | }
|
---|
439 |
|
---|
440 |
|
---|
441 |
|
---|
442 | std::string Scantable::formatSec(Double x) const
|
---|
443 | {
|
---|
444 | Double xcop = x;
|
---|
445 | MVTime mvt(xcop/24./3600.); // make days
|
---|
446 |
|
---|
447 | if (x < 59.95)
|
---|
448 | return String(" ") + mvt.string(MVTime::TIME_CLEAN_NO_HM, 7)+"s";
|
---|
449 | else if (x < 3599.95)
|
---|
450 | return String(" ") + mvt.string(MVTime::TIME_CLEAN_NO_H,7)+" ";
|
---|
451 | else {
|
---|
452 | ostringstream oss;
|
---|
453 | oss << setw(2) << std::right << setprecision(1) << mvt.hour();
|
---|
454 | oss << ":" << mvt.string(MVTime::TIME_CLEAN_NO_H,7) << " ";
|
---|
455 | return String(oss);
|
---|
456 | }
|
---|
457 | };
|
---|
458 |
|
---|
459 | std::string Scantable::formatDirection(const MDirection& md) const
|
---|
460 | {
|
---|
461 | Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
|
---|
462 | Int prec = 7;
|
---|
463 |
|
---|
464 | MVAngle mvLon(t[0]);
|
---|
465 | String sLon = mvLon.string(MVAngle::TIME,prec);
|
---|
466 | uInt tp = md.getRef().getType();
|
---|
467 | if (tp == MDirection::GALACTIC ||
|
---|
468 | tp == MDirection::SUPERGAL ) {
|
---|
469 | sLon = mvLon(0.0).string(MVAngle::ANGLE_CLEAN,prec);
|
---|
470 | }
|
---|
471 | MVAngle mvLat(t[1]);
|
---|
472 | String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec);
|
---|
473 | return sLon + String(" ") + sLat;
|
---|
474 | }
|
---|
475 |
|
---|
476 |
|
---|
477 | std::string Scantable::getFluxUnit() const
|
---|
478 | {
|
---|
479 | return table_.keywordSet().asString("FluxUnit");
|
---|
480 | }
|
---|
481 |
|
---|
482 | void Scantable::setFluxUnit(const std::string& unit)
|
---|
483 | {
|
---|
484 | String tmp(unit);
|
---|
485 | Unit tU(tmp);
|
---|
486 | if (tU==Unit("K") || tU==Unit("Jy")) {
|
---|
487 | table_.rwKeywordSet().define(String("FluxUnit"), tmp);
|
---|
488 | } else {
|
---|
489 | throw AipsError("Illegal unit - must be compatible with Jy or K");
|
---|
490 | }
|
---|
491 | }
|
---|
492 |
|
---|
493 | void Scantable::setInstrument(const std::string& name)
|
---|
494 | {
|
---|
495 | bool throwIt = true;
|
---|
496 | // create an Instrument to see if this is valid
|
---|
497 | STAttr::convertInstrument(name, throwIt);
|
---|
498 | String nameU(name);
|
---|
499 | nameU.upcase();
|
---|
500 | table_.rwKeywordSet().define(String("AntennaName"), nameU);
|
---|
501 | }
|
---|
502 |
|
---|
503 | void Scantable::setFeedType(const std::string& feedtype)
|
---|
504 | {
|
---|
505 | if ( Scantable::factories_.find(feedtype) == Scantable::factories_.end() ) {
|
---|
506 | std::string msg = "Illegal feed type "+ feedtype;
|
---|
507 | throw(casa::AipsError(msg));
|
---|
508 | }
|
---|
509 | table_.rwKeywordSet().define(String("POLTYPE"), feedtype);
|
---|
510 | }
|
---|
511 |
|
---|
512 | MPosition Scantable::getAntennaPosition() const
|
---|
513 | {
|
---|
514 | Vector<Double> antpos;
|
---|
515 | table_.keywordSet().get("AntennaPosition", antpos);
|
---|
516 | MVPosition mvpos(antpos(0),antpos(1),antpos(2));
|
---|
517 | return MPosition(mvpos);
|
---|
518 | }
|
---|
519 |
|
---|
520 | void Scantable::makePersistent(const std::string& filename)
|
---|
521 | {
|
---|
522 | String inname(filename);
|
---|
523 | Path path(inname);
|
---|
524 | /// @todo reindex SCANNO, recompute nbeam, nif, npol
|
---|
525 | inname = path.expandedName();
|
---|
526 | // 2011/03/04 TN
|
---|
527 | // We can comment out this workaround since the essential bug is
|
---|
528 | // fixed in casacore (r20889 in google code).
|
---|
529 | table_.deepCopy(inname, Table::New);
|
---|
530 | // // WORKAROUND !!! for Table bug
|
---|
531 | // // Remove when fixed in casacore
|
---|
532 | // if ( table_.tableType() == Table::Memory && !selector_.empty() ) {
|
---|
533 | // Table tab = table_.copyToMemoryTable(generateName());
|
---|
534 | // tab.deepCopy(inname, Table::New);
|
---|
535 | // tab.markForDelete();
|
---|
536 | //
|
---|
537 | // } else {
|
---|
538 | // table_.deepCopy(inname, Table::New);
|
---|
539 | // }
|
---|
540 | }
|
---|
541 |
|
---|
542 | int Scantable::nbeam( int scanno ) const
|
---|
543 | {
|
---|
544 | if ( scanno < 0 ) {
|
---|
545 | Int n;
|
---|
546 | table_.keywordSet().get("nBeam",n);
|
---|
547 | return int(n);
|
---|
548 | } else {
|
---|
549 | // take the first POLNO,IFNO,CYCLENO as nbeam shouldn't vary with these
|
---|
550 | Table t = table_(table_.col("SCANNO") == scanno);
|
---|
551 | ROTableRow row(t);
|
---|
552 | const TableRecord& rec = row.get(0);
|
---|
553 | Table subt = t( t.col("IFNO") == Int(rec.asuInt("IFNO"))
|
---|
554 | && t.col("POLNO") == Int(rec.asuInt("POLNO"))
|
---|
555 | && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
|
---|
556 | ROTableVector<uInt> v(subt, "BEAMNO");
|
---|
557 | return int(v.nelements());
|
---|
558 | }
|
---|
559 | return 0;
|
---|
560 | }
|
---|
561 |
|
---|
562 | int Scantable::nif( int scanno ) const
|
---|
563 | {
|
---|
564 | if ( scanno < 0 ) {
|
---|
565 | Int n;
|
---|
566 | table_.keywordSet().get("nIF",n);
|
---|
567 | return int(n);
|
---|
568 | } else {
|
---|
569 | // take the first POLNO,BEAMNO,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("BEAMNO") == Int(rec.asuInt("BEAMNO"))
|
---|
574 | && t.col("POLNO") == Int(rec.asuInt("POLNO"))
|
---|
575 | && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
|
---|
576 | if ( subt.nrow() == 0 ) return 0;
|
---|
577 | ROTableVector<uInt> v(subt, "IFNO");
|
---|
578 | return int(v.nelements());
|
---|
579 | }
|
---|
580 | return 0;
|
---|
581 | }
|
---|
582 |
|
---|
583 | int Scantable::npol( int scanno ) const
|
---|
584 | {
|
---|
585 | if ( scanno < 0 ) {
|
---|
586 | Int n;
|
---|
587 | table_.keywordSet().get("nPol",n);
|
---|
588 | return n;
|
---|
589 | } else {
|
---|
590 | // take the first POLNO,IFNO,CYCLENO as nbeam shouldn't vary with these
|
---|
591 | Table t = table_(table_.col("SCANNO") == scanno);
|
---|
592 | ROTableRow row(t);
|
---|
593 | const TableRecord& rec = row.get(0);
|
---|
594 | Table subt = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
|
---|
595 | && t.col("IFNO") == Int(rec.asuInt("IFNO"))
|
---|
596 | && t.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
|
---|
597 | if ( subt.nrow() == 0 ) return 0;
|
---|
598 | ROTableVector<uInt> v(subt, "POLNO");
|
---|
599 | return int(v.nelements());
|
---|
600 | }
|
---|
601 | return 0;
|
---|
602 | }
|
---|
603 |
|
---|
604 | int Scantable::ncycle( int scanno ) const
|
---|
605 | {
|
---|
606 | if ( scanno < 0 ) {
|
---|
607 | Block<String> cols(2);
|
---|
608 | cols[0] = "SCANNO";
|
---|
609 | cols[1] = "CYCLENO";
|
---|
610 | TableIterator it(table_, cols);
|
---|
611 | int n = 0;
|
---|
612 | while ( !it.pastEnd() ) {
|
---|
613 | ++n;
|
---|
614 | ++it;
|
---|
615 | }
|
---|
616 | return n;
|
---|
617 | } else {
|
---|
618 | Table t = table_(table_.col("SCANNO") == scanno);
|
---|
619 | ROTableRow row(t);
|
---|
620 | const TableRecord& rec = row.get(0);
|
---|
621 | Table subt = t( t.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
|
---|
622 | && t.col("POLNO") == Int(rec.asuInt("POLNO"))
|
---|
623 | && t.col("IFNO") == Int(rec.asuInt("IFNO")) );
|
---|
624 | if ( subt.nrow() == 0 ) return 0;
|
---|
625 | return int(subt.nrow());
|
---|
626 | }
|
---|
627 | return 0;
|
---|
628 | }
|
---|
629 |
|
---|
630 |
|
---|
631 | int Scantable::nrow( int scanno ) const
|
---|
632 | {
|
---|
633 | return int(table_.nrow());
|
---|
634 | }
|
---|
635 |
|
---|
636 | int Scantable::nchan( int ifno ) const
|
---|
637 | {
|
---|
638 | if ( ifno < 0 ) {
|
---|
639 | Int n;
|
---|
640 | table_.keywordSet().get("nChan",n);
|
---|
641 | return int(n);
|
---|
642 | } else {
|
---|
643 | // take the first SCANNO,POLNO,BEAMNO,CYCLENO as nbeam shouldn't
|
---|
644 | // vary with these
|
---|
645 | Table t = table_(table_.col("IFNO") == ifno);
|
---|
646 | if ( t.nrow() == 0 ) return 0;
|
---|
647 | ROArrayColumn<Float> v(t, "SPECTRA");
|
---|
648 | return v.shape(0)(0);
|
---|
649 | }
|
---|
650 | return 0;
|
---|
651 | }
|
---|
652 |
|
---|
653 | int Scantable::nscan() const {
|
---|
654 | Vector<uInt> scannos(scanCol_.getColumn());
|
---|
655 | uInt nout = genSort( scannos, Sort::Ascending,
|
---|
656 | Sort::QuickSort|Sort::NoDuplicates );
|
---|
657 | return int(nout);
|
---|
658 | }
|
---|
659 |
|
---|
660 | int Scantable::getChannels(int whichrow) const
|
---|
661 | {
|
---|
662 | return specCol_.shape(whichrow)(0);
|
---|
663 | }
|
---|
664 |
|
---|
665 | int Scantable::getBeam(int whichrow) const
|
---|
666 | {
|
---|
667 | return beamCol_(whichrow);
|
---|
668 | }
|
---|
669 |
|
---|
670 | std::vector<uint> Scantable::getNumbers(const ScalarColumn<uInt>& col) const
|
---|
671 | {
|
---|
672 | Vector<uInt> nos(col.getColumn());
|
---|
673 | uInt n = genSort( nos, Sort::Ascending, Sort::QuickSort|Sort::NoDuplicates );
|
---|
674 | nos.resize(n, True);
|
---|
675 | std::vector<uint> stlout;
|
---|
676 | nos.tovector(stlout);
|
---|
677 | return stlout;
|
---|
678 | }
|
---|
679 |
|
---|
680 | int Scantable::getIF(int whichrow) const
|
---|
681 | {
|
---|
682 | return ifCol_(whichrow);
|
---|
683 | }
|
---|
684 |
|
---|
685 | int Scantable::getPol(int whichrow) const
|
---|
686 | {
|
---|
687 | return polCol_(whichrow);
|
---|
688 | }
|
---|
689 |
|
---|
690 | std::string Scantable::formatTime(const MEpoch& me, bool showdate) const
|
---|
691 | {
|
---|
692 | return formatTime(me, showdate, 0);
|
---|
693 | }
|
---|
694 |
|
---|
695 | std::string Scantable::formatTime(const MEpoch& me, bool showdate, uInt prec) const
|
---|
696 | {
|
---|
697 | MVTime mvt(me.getValue());
|
---|
698 | if (showdate)
|
---|
699 | //mvt.setFormat(MVTime::YMD);
|
---|
700 | mvt.setFormat(MVTime::YMD, prec);
|
---|
701 | else
|
---|
702 | //mvt.setFormat(MVTime::TIME);
|
---|
703 | mvt.setFormat(MVTime::TIME, prec);
|
---|
704 | ostringstream oss;
|
---|
705 | oss << mvt;
|
---|
706 | return String(oss);
|
---|
707 | }
|
---|
708 |
|
---|
709 | void Scantable::calculateAZEL()
|
---|
710 | {
|
---|
711 | MPosition mp = getAntennaPosition();
|
---|
712 | MEpoch::ROScalarColumn timeCol(table_, "TIME");
|
---|
713 | ostringstream oss;
|
---|
714 | oss << "Computed azimuth/elevation using " << endl
|
---|
715 | << mp << endl;
|
---|
716 | for (Int i=0; i<nrow(); ++i) {
|
---|
717 | MEpoch me = timeCol(i);
|
---|
718 | MDirection md = getDirection(i);
|
---|
719 | oss << " Time: " << formatTime(me,False) << " Direction: " << formatDirection(md)
|
---|
720 | << endl << " => ";
|
---|
721 | MeasFrame frame(mp, me);
|
---|
722 | Vector<Double> azel =
|
---|
723 | MDirection::Convert(md, MDirection::Ref(MDirection::AZEL,
|
---|
724 | frame)
|
---|
725 | )().getAngle("rad").getValue();
|
---|
726 | azCol_.put(i,Float(azel[0]));
|
---|
727 | elCol_.put(i,Float(azel[1]));
|
---|
728 | oss << "azel: " << azel[0]/C::pi*180.0 << " "
|
---|
729 | << azel[1]/C::pi*180.0 << " (deg)" << endl;
|
---|
730 | }
|
---|
731 | pushLog(String(oss));
|
---|
732 | }
|
---|
733 |
|
---|
734 | void Scantable::clip(const Float uthres, const Float dthres, bool clipoutside, bool unflag)
|
---|
735 | {
|
---|
736 | for (uInt i=0; i<table_.nrow(); ++i) {
|
---|
737 | Vector<uChar> flgs = flagsCol_(i);
|
---|
738 | srchChannelsToClip(i, uthres, dthres, clipoutside, unflag, flgs);
|
---|
739 | flagsCol_.put(i, flgs);
|
---|
740 | }
|
---|
741 | }
|
---|
742 |
|
---|
743 | std::vector<bool> Scantable::getClipMask(int whichrow, const Float uthres, const Float dthres, bool clipoutside, bool unflag)
|
---|
744 | {
|
---|
745 | Vector<uChar> flags;
|
---|
746 | flagsCol_.get(uInt(whichrow), flags);
|
---|
747 | srchChannelsToClip(uInt(whichrow), uthres, dthres, clipoutside, unflag, flags);
|
---|
748 | Vector<Bool> bflag(flags.shape());
|
---|
749 | convertArray(bflag, flags);
|
---|
750 | //bflag = !bflag;
|
---|
751 |
|
---|
752 | std::vector<bool> mask;
|
---|
753 | bflag.tovector(mask);
|
---|
754 | return mask;
|
---|
755 | }
|
---|
756 |
|
---|
757 | void Scantable::srchChannelsToClip(uInt whichrow, const Float uthres, const Float dthres, bool clipoutside, bool unflag,
|
---|
758 | Vector<uChar> flgs)
|
---|
759 | {
|
---|
760 | Vector<Float> spcs = specCol_(whichrow);
|
---|
761 | uInt nchannel = nchan();
|
---|
762 | if (spcs.nelements() != nchannel) {
|
---|
763 | throw(AipsError("Data has incorrect number of channels"));
|
---|
764 | }
|
---|
765 | uChar userflag = 1 << 7;
|
---|
766 | if (unflag) {
|
---|
767 | userflag = 0 << 7;
|
---|
768 | }
|
---|
769 | if (clipoutside) {
|
---|
770 | for (uInt j = 0; j < nchannel; ++j) {
|
---|
771 | Float spc = spcs(j);
|
---|
772 | if ((spc >= uthres) || (spc <= dthres)) {
|
---|
773 | flgs(j) = userflag;
|
---|
774 | }
|
---|
775 | }
|
---|
776 | } else {
|
---|
777 | for (uInt j = 0; j < nchannel; ++j) {
|
---|
778 | Float spc = spcs(j);
|
---|
779 | if ((spc < uthres) && (spc > dthres)) {
|
---|
780 | flgs(j) = userflag;
|
---|
781 | }
|
---|
782 | }
|
---|
783 | }
|
---|
784 | }
|
---|
785 |
|
---|
786 |
|
---|
787 | void Scantable::flag( int whichrow, const std::vector<bool>& msk, bool unflag ) {
|
---|
788 | std::vector<bool>::const_iterator it;
|
---|
789 | uInt ntrue = 0;
|
---|
790 | if (whichrow >= int(table_.nrow()) ) {
|
---|
791 | throw(AipsError("Invalid row number"));
|
---|
792 | }
|
---|
793 | for (it = msk.begin(); it != msk.end(); ++it) {
|
---|
794 | if ( *it ) {
|
---|
795 | ntrue++;
|
---|
796 | }
|
---|
797 | }
|
---|
798 | //if ( selector_.empty() && (msk.size() == 0 || msk.size() == ntrue) )
|
---|
799 | if ( whichrow == -1 && !unflag && selector_.empty() && (msk.size() == 0 || msk.size() == ntrue) )
|
---|
800 | throw(AipsError("Trying to flag whole scantable."));
|
---|
801 | uChar userflag = 1 << 7;
|
---|
802 | if ( unflag ) {
|
---|
803 | userflag = 0 << 7;
|
---|
804 | }
|
---|
805 | if (whichrow > -1 ) {
|
---|
806 | applyChanFlag(uInt(whichrow), msk, userflag);
|
---|
807 | } else {
|
---|
808 | for ( uInt i=0; i<table_.nrow(); ++i) {
|
---|
809 | applyChanFlag(i, msk, userflag);
|
---|
810 | }
|
---|
811 | }
|
---|
812 | }
|
---|
813 |
|
---|
814 | void Scantable::applyChanFlag( uInt whichrow, const std::vector<bool>& msk, uChar flagval )
|
---|
815 | {
|
---|
816 | if (whichrow >= table_.nrow() ) {
|
---|
817 | throw( casa::indexError<int>( whichrow, "asap::Scantable::applyChanFlag: Invalid row number" ) );
|
---|
818 | }
|
---|
819 | Vector<uChar> flgs = flagsCol_(whichrow);
|
---|
820 | if ( msk.size() == 0 ) {
|
---|
821 | flgs = flagval;
|
---|
822 | flagsCol_.put(whichrow, flgs);
|
---|
823 | return;
|
---|
824 | }
|
---|
825 | if ( int(msk.size()) != nchan() ) {
|
---|
826 | throw(AipsError("Mask has incorrect number of channels."));
|
---|
827 | }
|
---|
828 | if ( flgs.nelements() != msk.size() ) {
|
---|
829 | throw(AipsError("Mask has incorrect number of channels."
|
---|
830 | " Probably varying with IF. Please flag per IF"));
|
---|
831 | }
|
---|
832 | std::vector<bool>::const_iterator it;
|
---|
833 | uInt j = 0;
|
---|
834 | for (it = msk.begin(); it != msk.end(); ++it) {
|
---|
835 | if ( *it ) {
|
---|
836 | flgs(j) = flagval;
|
---|
837 | }
|
---|
838 | ++j;
|
---|
839 | }
|
---|
840 | flagsCol_.put(whichrow, flgs);
|
---|
841 | }
|
---|
842 |
|
---|
843 | void Scantable::flagRow(const std::vector<uInt>& rows, bool unflag)
|
---|
844 | {
|
---|
845 | if ( selector_.empty() && (rows.size() == table_.nrow()) )
|
---|
846 | throw(AipsError("Trying to flag whole scantable."));
|
---|
847 |
|
---|
848 | uInt rowflag = (unflag ? 0 : 1);
|
---|
849 | std::vector<uInt>::const_iterator it;
|
---|
850 | for (it = rows.begin(); it != rows.end(); ++it)
|
---|
851 | flagrowCol_.put(*it, rowflag);
|
---|
852 | }
|
---|
853 |
|
---|
854 | std::vector<bool> Scantable::getMask(int whichrow) const
|
---|
855 | {
|
---|
856 | Vector<uChar> flags;
|
---|
857 | flagsCol_.get(uInt(whichrow), flags);
|
---|
858 | Vector<Bool> bflag(flags.shape());
|
---|
859 | convertArray(bflag, flags);
|
---|
860 | bflag = !bflag;
|
---|
861 | std::vector<bool> mask;
|
---|
862 | bflag.tovector(mask);
|
---|
863 | return mask;
|
---|
864 | }
|
---|
865 |
|
---|
866 | std::vector<float> Scantable::getSpectrum( int whichrow,
|
---|
867 | const std::string& poltype ) const
|
---|
868 | {
|
---|
869 | String ptype = poltype;
|
---|
870 | if (poltype == "" ) ptype = getPolType();
|
---|
871 | if ( whichrow < 0 || whichrow >= nrow() )
|
---|
872 | throw(AipsError("Illegal row number."));
|
---|
873 | std::vector<float> out;
|
---|
874 | Vector<Float> arr;
|
---|
875 | uInt requestedpol = polCol_(whichrow);
|
---|
876 | String basetype = getPolType();
|
---|
877 | if ( ptype == basetype ) {
|
---|
878 | specCol_.get(whichrow, arr);
|
---|
879 | } else {
|
---|
880 | CountedPtr<STPol> stpol(STPol::getPolClass(Scantable::factories_,
|
---|
881 | basetype));
|
---|
882 | uInt row = uInt(whichrow);
|
---|
883 | stpol->setSpectra(getPolMatrix(row));
|
---|
884 | Float fang,fhand;
|
---|
885 | fang = focusTable_.getTotalAngle(mfocusidCol_(row));
|
---|
886 | fhand = focusTable_.getFeedHand(mfocusidCol_(row));
|
---|
887 | stpol->setPhaseCorrections(fang, fhand);
|
---|
888 | arr = stpol->getSpectrum(requestedpol, ptype);
|
---|
889 | }
|
---|
890 | if ( arr.nelements() == 0 )
|
---|
891 | pushLog("Not enough polarisations present to do the conversion.");
|
---|
892 | arr.tovector(out);
|
---|
893 | return out;
|
---|
894 | }
|
---|
895 |
|
---|
896 | void Scantable::setSpectrum( const std::vector<float>& spec,
|
---|
897 | int whichrow )
|
---|
898 | {
|
---|
899 | Vector<Float> spectrum(spec);
|
---|
900 | Vector<Float> arr;
|
---|
901 | specCol_.get(whichrow, arr);
|
---|
902 | if ( spectrum.nelements() != arr.nelements() )
|
---|
903 | throw AipsError("The spectrum has incorrect number of channels.");
|
---|
904 | specCol_.put(whichrow, spectrum);
|
---|
905 | }
|
---|
906 |
|
---|
907 |
|
---|
908 | String Scantable::generateName()
|
---|
909 | {
|
---|
910 | return (File::newUniqueName("./","temp")).baseName();
|
---|
911 | }
|
---|
912 |
|
---|
913 | const casa::Table& Scantable::table( ) const
|
---|
914 | {
|
---|
915 | return table_;
|
---|
916 | }
|
---|
917 |
|
---|
918 | casa::Table& Scantable::table( )
|
---|
919 | {
|
---|
920 | return table_;
|
---|
921 | }
|
---|
922 |
|
---|
923 | std::string Scantable::getPolType() const
|
---|
924 | {
|
---|
925 | return table_.keywordSet().asString("POLTYPE");
|
---|
926 | }
|
---|
927 |
|
---|
928 | void Scantable::unsetSelection()
|
---|
929 | {
|
---|
930 | table_ = originalTable_;
|
---|
931 | attach();
|
---|
932 | selector_.reset();
|
---|
933 | }
|
---|
934 |
|
---|
935 | void Scantable::setSelection( const STSelector& selection )
|
---|
936 | {
|
---|
937 | Table tab = const_cast<STSelector&>(selection).apply(originalTable_);
|
---|
938 | if ( tab.nrow() == 0 ) {
|
---|
939 | throw(AipsError("Selection contains no data. Not applying it."));
|
---|
940 | }
|
---|
941 | table_ = tab;
|
---|
942 | attach();
|
---|
943 | // tab.rwKeywordSet().define("nBeam",(Int)(getBeamNos().size())) ;
|
---|
944 | // vector<uint> selectedIFs = getIFNos() ;
|
---|
945 | // Int newnIF = selectedIFs.size() ;
|
---|
946 | // tab.rwKeywordSet().define("nIF",newnIF) ;
|
---|
947 | // if ( newnIF != 0 ) {
|
---|
948 | // Int newnChan = 0 ;
|
---|
949 | // for ( Int i = 0 ; i < newnIF ; i++ ) {
|
---|
950 | // Int nChan = nchan( selectedIFs[i] ) ;
|
---|
951 | // if ( newnChan > nChan )
|
---|
952 | // newnChan = nChan ;
|
---|
953 | // }
|
---|
954 | // tab.rwKeywordSet().define("nChan",newnChan) ;
|
---|
955 | // }
|
---|
956 | // tab.rwKeywordSet().define("nPol",(Int)(getPolNos().size())) ;
|
---|
957 | selector_ = selection;
|
---|
958 | }
|
---|
959 |
|
---|
960 |
|
---|
961 | std::string Scantable::headerSummary( bool verbose )
|
---|
962 | {
|
---|
963 | // Format header info
|
---|
964 | // STHeader sdh;
|
---|
965 | // sdh = getHeader();
|
---|
966 | // sdh.print();
|
---|
967 | ostringstream oss;
|
---|
968 | oss.flags(std::ios_base::left);
|
---|
969 | oss << setw(15) << "Beams:" << setw(4) << nbeam() << endl
|
---|
970 | << setw(15) << "IFs:" << setw(4) << nif() << endl
|
---|
971 | << setw(15) << "Polarisations:" << setw(4) << npol()
|
---|
972 | << "(" << getPolType() << ")" << endl
|
---|
973 | << setw(15) << "Channels:" << nchan() << endl;
|
---|
974 | String tmp;
|
---|
975 | oss << setw(15) << "Observer:"
|
---|
976 | << table_.keywordSet().asString("Observer") << endl;
|
---|
977 | oss << setw(15) << "Obs Date:" << getTime(-1,true) << endl;
|
---|
978 | table_.keywordSet().get("Project", tmp);
|
---|
979 | oss << setw(15) << "Project:" << tmp << endl;
|
---|
980 | table_.keywordSet().get("Obstype", tmp);
|
---|
981 | oss << setw(15) << "Obs. Type:" << tmp << endl;
|
---|
982 | table_.keywordSet().get("AntennaName", tmp);
|
---|
983 | oss << setw(15) << "Antenna Name:" << tmp << endl;
|
---|
984 | table_.keywordSet().get("FluxUnit", tmp);
|
---|
985 | oss << setw(15) << "Flux Unit:" << tmp << endl;
|
---|
986 | //Vector<Double> vec(moleculeTable_.getRestFrequencies());
|
---|
987 | int nid = moleculeTable_.nrow();
|
---|
988 | Bool firstline = True;
|
---|
989 | oss << setw(15) << "Rest Freqs:";
|
---|
990 | for (int i=0; i<nid; i++) {
|
---|
991 | Table t = table_(table_.col("MOLECULE_ID") == i);
|
---|
992 | if (t.nrow() > 0) {
|
---|
993 | Vector<Double> vec(moleculeTable_.getRestFrequency(i));
|
---|
994 | if (vec.nelements() > 0) {
|
---|
995 | if (firstline) {
|
---|
996 | oss << setprecision(10) << vec << " [Hz]" << endl;
|
---|
997 | firstline=False;
|
---|
998 | }
|
---|
999 | else{
|
---|
1000 | oss << setw(15)<<" " << setprecision(10) << vec << " [Hz]" << endl;
|
---|
1001 | }
|
---|
1002 | } else {
|
---|
1003 | oss << "none" << endl;
|
---|
1004 | }
|
---|
1005 | }
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | oss << setw(15) << "Abcissa:" << getAbcissaLabel(0) << endl;
|
---|
1009 | oss << selector_.print() << endl;
|
---|
1010 | /// @todo implement verbose mode
|
---|
1011 | return String(oss);
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | std::string Scantable::summary( bool verbose )
|
---|
1015 | {
|
---|
1016 | ostringstream oss;
|
---|
1017 | oss << endl;
|
---|
1018 | oss << asap::SEPERATOR << endl;
|
---|
1019 | oss << " Scan Table Summary" << endl;
|
---|
1020 | oss << asap::SEPERATOR << endl;
|
---|
1021 |
|
---|
1022 | // Format header info
|
---|
1023 | oss << headerSummary(verbose);
|
---|
1024 | oss << endl;
|
---|
1025 |
|
---|
1026 | // main table
|
---|
1027 | String dirtype = "Position ("
|
---|
1028 | + getDirectionRefString()
|
---|
1029 | + ")";
|
---|
1030 | oss.flags(std::ios_base::left);
|
---|
1031 | oss << setw(5) << "Scan" << setw(15) << "Source"
|
---|
1032 | << setw(10) << "Time" << setw(18) << "Integration"
|
---|
1033 | << setw(15) << "Source Type" << endl;
|
---|
1034 | oss << setw(5) << "" << setw(5) << "Beam" << setw(3) << "" << dirtype << endl;
|
---|
1035 | oss << setw(10) << "" << setw(3) << "IF" << setw(3) << ""
|
---|
1036 | << setw(8) << "Frame" << setw(16)
|
---|
1037 | << "RefVal" << setw(10) << "RefPix" << setw(12) << "Increment"
|
---|
1038 | << setw(7) << "Channels"
|
---|
1039 | << endl;
|
---|
1040 | oss << asap::SEPERATOR << endl;
|
---|
1041 | TableIterator iter(table_, "SCANNO");
|
---|
1042 | while (!iter.pastEnd()) {
|
---|
1043 | Table subt = iter.table();
|
---|
1044 | ROTableRow row(subt);
|
---|
1045 | MEpoch::ROScalarColumn timeCol(subt,"TIME");
|
---|
1046 | const TableRecord& rec = row.get(0);
|
---|
1047 | oss << setw(4) << std::right << rec.asuInt("SCANNO")
|
---|
1048 | << std::left << setw(1) << ""
|
---|
1049 | << setw(15) << rec.asString("SRCNAME")
|
---|
1050 | << setw(10) << formatTime(timeCol(0), false);
|
---|
1051 | // count the cycles in the scan
|
---|
1052 | TableIterator cyciter(subt, "CYCLENO");
|
---|
1053 | int nint = 0;
|
---|
1054 | while (!cyciter.pastEnd()) {
|
---|
1055 | ++nint;
|
---|
1056 | ++cyciter;
|
---|
1057 | }
|
---|
1058 | oss << setw(3) << std::right << nint << setw(3) << " x " << std::left
|
---|
1059 | << setw(11) << formatSec(rec.asFloat("INTERVAL")) << setw(1) << ""
|
---|
1060 | << setw(15) << SrcType::getName(rec.asInt("SRCTYPE")) << endl;
|
---|
1061 |
|
---|
1062 | TableIterator biter(subt, "BEAMNO");
|
---|
1063 | while (!biter.pastEnd()) {
|
---|
1064 | Table bsubt = biter.table();
|
---|
1065 | ROTableRow brow(bsubt);
|
---|
1066 | const TableRecord& brec = brow.get(0);
|
---|
1067 | uInt row0 = bsubt.rowNumbers(table_)[0];
|
---|
1068 | oss << setw(5) << "" << setw(4) << std::right << brec.asuInt("BEAMNO")<< std::left;
|
---|
1069 | oss << setw(4) << "" << formatDirection(getDirection(row0)) << endl;
|
---|
1070 | TableIterator iiter(bsubt, "IFNO");
|
---|
1071 | while (!iiter.pastEnd()) {
|
---|
1072 | Table isubt = iiter.table();
|
---|
1073 | ROTableRow irow(isubt);
|
---|
1074 | const TableRecord& irec = irow.get(0);
|
---|
1075 | oss << setw(9) << "";
|
---|
1076 | oss << setw(3) << std::right << irec.asuInt("IFNO") << std::left
|
---|
1077 | << setw(1) << "" << frequencies().print(irec.asuInt("FREQ_ID"))
|
---|
1078 | << setw(3) << "" << nchan(irec.asuInt("IFNO"))
|
---|
1079 | << endl;
|
---|
1080 |
|
---|
1081 | ++iiter;
|
---|
1082 | }
|
---|
1083 | ++biter;
|
---|
1084 | }
|
---|
1085 | ++iter;
|
---|
1086 | }
|
---|
1087 | /// @todo implement verbose mode
|
---|
1088 | return String(oss);
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | // std::string Scantable::getTime(int whichrow, bool showdate) const
|
---|
1092 | // {
|
---|
1093 | // MEpoch::ROScalarColumn timeCol(table_, "TIME");
|
---|
1094 | // MEpoch me;
|
---|
1095 | // if (whichrow > -1) {
|
---|
1096 | // me = timeCol(uInt(whichrow));
|
---|
1097 | // } else {
|
---|
1098 | // Double tm;
|
---|
1099 | // table_.keywordSet().get("UTC",tm);
|
---|
1100 | // me = MEpoch(MVEpoch(tm));
|
---|
1101 | // }
|
---|
1102 | // return formatTime(me, showdate);
|
---|
1103 | // }
|
---|
1104 |
|
---|
1105 | std::string Scantable::getTime(int whichrow, bool showdate, uInt prec) const
|
---|
1106 | {
|
---|
1107 | MEpoch me;
|
---|
1108 | me = getEpoch(whichrow);
|
---|
1109 | return formatTime(me, showdate, prec);
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | MEpoch Scantable::getEpoch(int whichrow) const
|
---|
1113 | {
|
---|
1114 | if (whichrow > -1) {
|
---|
1115 | return timeCol_(uInt(whichrow));
|
---|
1116 | } else {
|
---|
1117 | Double tm;
|
---|
1118 | table_.keywordSet().get("UTC",tm);
|
---|
1119 | return MEpoch(MVEpoch(tm));
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | std::string Scantable::getDirectionString(int whichrow) const
|
---|
1124 | {
|
---|
1125 | return formatDirection(getDirection(uInt(whichrow)));
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 |
|
---|
1129 | SpectralCoordinate Scantable::getSpectralCoordinate(int whichrow) const {
|
---|
1130 | const MPosition& mp = getAntennaPosition();
|
---|
1131 | const MDirection& md = getDirection(whichrow);
|
---|
1132 | const MEpoch& me = timeCol_(whichrow);
|
---|
1133 | //Double rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
|
---|
1134 | Vector<Double> rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
|
---|
1135 | return freqTable_.getSpectralCoordinate(md, mp, me, rf,
|
---|
1136 | mfreqidCol_(whichrow));
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | std::vector< double > Scantable::getAbcissa( int whichrow ) const
|
---|
1140 | {
|
---|
1141 | if ( whichrow > int(table_.nrow()) ) throw(AipsError("Illegal row number"));
|
---|
1142 | std::vector<double> stlout;
|
---|
1143 | int nchan = specCol_(whichrow).nelements();
|
---|
1144 | String us = freqTable_.getUnitString();
|
---|
1145 | if ( us == "" || us == "pixel" || us == "channel" ) {
|
---|
1146 | for (int i=0; i<nchan; ++i) {
|
---|
1147 | stlout.push_back(double(i));
|
---|
1148 | }
|
---|
1149 | return stlout;
|
---|
1150 | }
|
---|
1151 | SpectralCoordinate spc = getSpectralCoordinate(whichrow);
|
---|
1152 | Vector<Double> pixel(nchan);
|
---|
1153 | Vector<Double> world;
|
---|
1154 | indgen(pixel);
|
---|
1155 | if ( Unit(us) == Unit("Hz") ) {
|
---|
1156 | for ( int i=0; i < nchan; ++i) {
|
---|
1157 | Double world;
|
---|
1158 | spc.toWorld(world, pixel[i]);
|
---|
1159 | stlout.push_back(double(world));
|
---|
1160 | }
|
---|
1161 | } else if ( Unit(us) == Unit("km/s") ) {
|
---|
1162 | Vector<Double> world;
|
---|
1163 | spc.pixelToVelocity(world, pixel);
|
---|
1164 | world.tovector(stlout);
|
---|
1165 | }
|
---|
1166 | return stlout;
|
---|
1167 | }
|
---|
1168 | void Scantable::setDirectionRefString( const std::string & refstr )
|
---|
1169 | {
|
---|
1170 | MDirection::Types mdt;
|
---|
1171 | if (refstr != "" && !MDirection::getType(mdt, refstr)) {
|
---|
1172 | throw(AipsError("Illegal Direction frame."));
|
---|
1173 | }
|
---|
1174 | if ( refstr == "" ) {
|
---|
1175 | String defaultstr = MDirection::showType(dirCol_.getMeasRef().getType());
|
---|
1176 | table_.rwKeywordSet().define("DIRECTIONREF", defaultstr);
|
---|
1177 | } else {
|
---|
1178 | table_.rwKeywordSet().define("DIRECTIONREF", String(refstr));
|
---|
1179 | }
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | std::string Scantable::getDirectionRefString( ) const
|
---|
1183 | {
|
---|
1184 | return table_.keywordSet().asString("DIRECTIONREF");
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | MDirection Scantable::getDirection(int whichrow ) const
|
---|
1188 | {
|
---|
1189 | String usertype = table_.keywordSet().asString("DIRECTIONREF");
|
---|
1190 | String type = MDirection::showType(dirCol_.getMeasRef().getType());
|
---|
1191 | if ( usertype != type ) {
|
---|
1192 | MDirection::Types mdt;
|
---|
1193 | if (!MDirection::getType(mdt, usertype)) {
|
---|
1194 | throw(AipsError("Illegal Direction frame."));
|
---|
1195 | }
|
---|
1196 | return dirCol_.convert(uInt(whichrow), mdt);
|
---|
1197 | } else {
|
---|
1198 | return dirCol_(uInt(whichrow));
|
---|
1199 | }
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | std::string Scantable::getAbcissaLabel( int whichrow ) const
|
---|
1203 | {
|
---|
1204 | if ( whichrow > int(table_.nrow()) ) throw(AipsError("Illegal ro number"));
|
---|
1205 | const MPosition& mp = getAntennaPosition();
|
---|
1206 | const MDirection& md = getDirection(whichrow);
|
---|
1207 | const MEpoch& me = timeCol_(whichrow);
|
---|
1208 | //const Double& rf = mmolidCol_(whichrow);
|
---|
1209 | const Vector<Double> rf = moleculeTable_.getRestFrequency(mmolidCol_(whichrow));
|
---|
1210 | SpectralCoordinate spc =
|
---|
1211 | freqTable_.getSpectralCoordinate(md, mp, me, rf, mfreqidCol_(whichrow));
|
---|
1212 |
|
---|
1213 | String s = "Channel";
|
---|
1214 | Unit u = Unit(freqTable_.getUnitString());
|
---|
1215 | if (u == Unit("km/s")) {
|
---|
1216 | s = CoordinateUtil::axisLabel(spc, 0, True,True, True);
|
---|
1217 | } else if (u == Unit("Hz")) {
|
---|
1218 | Vector<String> wau(1);wau = u.getName();
|
---|
1219 | spc.setWorldAxisUnits(wau);
|
---|
1220 | s = CoordinateUtil::axisLabel(spc, 0, True, True, False);
|
---|
1221 | }
|
---|
1222 | return s;
|
---|
1223 |
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | /**
|
---|
1227 | void asap::Scantable::setRestFrequencies( double rf, const std::string& name,
|
---|
1228 | const std::string& unit )
|
---|
1229 | **/
|
---|
1230 | void Scantable::setRestFrequencies( vector<double> rf, const vector<std::string>& name,
|
---|
1231 | const std::string& unit )
|
---|
1232 |
|
---|
1233 | {
|
---|
1234 | ///@todo lookup in line table to fill in name and formattedname
|
---|
1235 | Unit u(unit);
|
---|
1236 | //Quantum<Double> urf(rf, u);
|
---|
1237 | Quantum<Vector<Double> >urf(rf, u);
|
---|
1238 | Vector<String> formattedname(0);
|
---|
1239 | //cerr<<"Scantable::setRestFrequnecies="<<urf<<endl;
|
---|
1240 |
|
---|
1241 | //uInt id = moleculeTable_.addEntry(urf.getValue("Hz"), name, "");
|
---|
1242 | uInt id = moleculeTable_.addEntry(urf.getValue("Hz"), mathutil::toVectorString(name), formattedname);
|
---|
1243 | TableVector<uInt> tabvec(table_, "MOLECULE_ID");
|
---|
1244 | tabvec = id;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | /**
|
---|
1248 | void asap::Scantable::setRestFrequencies( const std::string& name )
|
---|
1249 | {
|
---|
1250 | throw(AipsError("setRestFrequencies( const std::string& name ) NYI"));
|
---|
1251 | ///@todo implement
|
---|
1252 | }
|
---|
1253 | **/
|
---|
1254 |
|
---|
1255 | void Scantable::setRestFrequencies( const vector<std::string>& name )
|
---|
1256 | {
|
---|
1257 | throw(AipsError("setRestFrequencies( const vector<std::string>& name ) NYI"));
|
---|
1258 | ///@todo implement
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | std::vector< unsigned int > Scantable::rownumbers( ) const
|
---|
1262 | {
|
---|
1263 | std::vector<unsigned int> stlout;
|
---|
1264 | Vector<uInt> vec = table_.rowNumbers();
|
---|
1265 | vec.tovector(stlout);
|
---|
1266 | return stlout;
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 |
|
---|
1270 | Matrix<Float> Scantable::getPolMatrix( uInt whichrow ) const
|
---|
1271 | {
|
---|
1272 | ROTableRow row(table_);
|
---|
1273 | const TableRecord& rec = row.get(whichrow);
|
---|
1274 | Table t =
|
---|
1275 | originalTable_( originalTable_.col("SCANNO") == Int(rec.asuInt("SCANNO"))
|
---|
1276 | && originalTable_.col("BEAMNO") == Int(rec.asuInt("BEAMNO"))
|
---|
1277 | && originalTable_.col("IFNO") == Int(rec.asuInt("IFNO"))
|
---|
1278 | && originalTable_.col("CYCLENO") == Int(rec.asuInt("CYCLENO")) );
|
---|
1279 | ROArrayColumn<Float> speccol(t, "SPECTRA");
|
---|
1280 | return speccol.getColumn();
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | std::vector< std::string > Scantable::columnNames( ) const
|
---|
1284 | {
|
---|
1285 | Vector<String> vec = table_.tableDesc().columnNames();
|
---|
1286 | return mathutil::tovectorstring(vec);
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | MEpoch::Types Scantable::getTimeReference( ) const
|
---|
1290 | {
|
---|
1291 | return MEpoch::castType(timeCol_.getMeasRef().getType());
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | void Scantable::addFit( const STFitEntry& fit, int row )
|
---|
1295 | {
|
---|
1296 | //cout << mfitidCol_(uInt(row)) << endl;
|
---|
1297 | LogIO os( LogOrigin( "Scantable", "addFit()", WHERE ) ) ;
|
---|
1298 | os << mfitidCol_(uInt(row)) << LogIO::POST ;
|
---|
1299 | uInt id = fitTable_.addEntry(fit, mfitidCol_(uInt(row)));
|
---|
1300 | mfitidCol_.put(uInt(row), id);
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | void Scantable::shift(int npix)
|
---|
1304 | {
|
---|
1305 | Vector<uInt> fids(mfreqidCol_.getColumn());
|
---|
1306 | genSort( fids, Sort::Ascending,
|
---|
1307 | Sort::QuickSort|Sort::NoDuplicates );
|
---|
1308 | for (uInt i=0; i<fids.nelements(); ++i) {
|
---|
1309 | frequencies().shiftRefPix(npix, fids[i]);
|
---|
1310 | }
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 | String Scantable::getAntennaName() const
|
---|
1314 | {
|
---|
1315 | String out;
|
---|
1316 | table_.keywordSet().get("AntennaName", out);
|
---|
1317 | String::size_type pos1 = out.find("@") ;
|
---|
1318 | String::size_type pos2 = out.find("//") ;
|
---|
1319 | if ( pos2 != String::npos )
|
---|
1320 | out = out.substr(pos2+2,pos1-pos2-2) ;
|
---|
1321 | else if ( pos1 != String::npos )
|
---|
1322 | out = out.substr(0,pos1) ;
|
---|
1323 | return out;
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 | int Scantable::checkScanInfo(const std::vector<int>& scanlist) const
|
---|
1327 | {
|
---|
1328 | String tbpath;
|
---|
1329 | int ret = 0;
|
---|
1330 | if ( table_.keywordSet().isDefined("GBT_GO") ) {
|
---|
1331 | table_.keywordSet().get("GBT_GO", tbpath);
|
---|
1332 | Table t(tbpath,Table::Old);
|
---|
1333 | // check each scan if other scan of the pair exist
|
---|
1334 | int nscan = scanlist.size();
|
---|
1335 | for (int i = 0; i < nscan; i++) {
|
---|
1336 | Table subt = t( t.col("SCAN") == scanlist[i]+1 );
|
---|
1337 | if (subt.nrow()==0) {
|
---|
1338 | //cerr <<"Scan "<<scanlist[i]<<" cannot be found in the scantable."<<endl;
|
---|
1339 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1340 | os <<LogIO::WARN<<"Scan "<<scanlist[i]<<" cannot be found in the scantable."<<LogIO::POST;
|
---|
1341 | ret = 1;
|
---|
1342 | break;
|
---|
1343 | }
|
---|
1344 | ROTableRow row(subt);
|
---|
1345 | const TableRecord& rec = row.get(0);
|
---|
1346 | int scan1seqn = rec.asuInt("PROCSEQN");
|
---|
1347 | int laston1 = rec.asuInt("LASTON");
|
---|
1348 | if ( rec.asuInt("PROCSIZE")==2 ) {
|
---|
1349 | if ( i < nscan-1 ) {
|
---|
1350 | Table subt2 = t( t.col("SCAN") == scanlist[i+1]+1 );
|
---|
1351 | if ( subt2.nrow() == 0) {
|
---|
1352 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1353 |
|
---|
1354 | //cerr<<"Scan "<<scanlist[i+1]<<" cannot be found in the scantable."<<endl;
|
---|
1355 | os<<LogIO::WARN<<"Scan "<<scanlist[i+1]<<" cannot be found in the scantable."<<LogIO::POST;
|
---|
1356 | ret = 1;
|
---|
1357 | break;
|
---|
1358 | }
|
---|
1359 | ROTableRow row2(subt2);
|
---|
1360 | const TableRecord& rec2 = row2.get(0);
|
---|
1361 | int scan2seqn = rec2.asuInt("PROCSEQN");
|
---|
1362 | int laston2 = rec2.asuInt("LASTON");
|
---|
1363 | if (scan1seqn == 1 && scan2seqn == 2) {
|
---|
1364 | if (laston1 == laston2) {
|
---|
1365 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1366 | //cerr<<"A valid scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<endl;
|
---|
1367 | os<<"A valid scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<LogIO::POST;
|
---|
1368 | i +=1;
|
---|
1369 | }
|
---|
1370 | else {
|
---|
1371 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1372 | //cerr<<"Incorrect scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<endl;
|
---|
1373 | os<<LogIO::WARN<<"Incorrect scan pair ["<<scanlist[i]<<","<<scanlist[i+1]<<"]"<<LogIO::POST;
|
---|
1374 | }
|
---|
1375 | }
|
---|
1376 | else if (scan1seqn==2 && scan2seqn == 1) {
|
---|
1377 | if (laston1 == laston2) {
|
---|
1378 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1379 | //cerr<<"["<<scanlist[i]<<","<<scanlist[i+1]<<"] is a valid scan pair but in incorrect order."<<endl;
|
---|
1380 | os<<LogIO::WARN<<"["<<scanlist[i]<<","<<scanlist[i+1]<<"] is a valid scan pair but in incorrect order."<<LogIO::POST;
|
---|
1381 | ret = 1;
|
---|
1382 | break;
|
---|
1383 | }
|
---|
1384 | }
|
---|
1385 | else {
|
---|
1386 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1387 | //cerr<<"The other scan for "<<scanlist[i]<<" appears to be missing. Check the input scan numbers."<<endl;
|
---|
1388 | os<<LogIO::WARN<<"The other scan for "<<scanlist[i]<<" appears to be missing. Check the input scan numbers."<<LogIO::POST;
|
---|
1389 | ret = 1;
|
---|
1390 | break;
|
---|
1391 | }
|
---|
1392 | }
|
---|
1393 | }
|
---|
1394 | else {
|
---|
1395 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1396 | //cerr<<"The scan does not appear to be standard obsevation."<<endl;
|
---|
1397 | os<<LogIO::WARN<<"The scan does not appear to be standard obsevation."<<LogIO::POST;
|
---|
1398 | }
|
---|
1399 | //if ( i >= nscan ) break;
|
---|
1400 | }
|
---|
1401 | }
|
---|
1402 | else {
|
---|
1403 | LogIO os( LogOrigin( "Scantable", "checkScanInfo()", WHERE ) ) ;
|
---|
1404 | //cerr<<"No reference to GBT_GO table."<<endl;
|
---|
1405 | os<<LogIO::WARN<<"No reference to GBT_GO table."<<LogIO::POST;
|
---|
1406 | ret = 1;
|
---|
1407 | }
|
---|
1408 | return ret;
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | std::vector<double> Scantable::getDirectionVector(int whichrow) const
|
---|
1412 | {
|
---|
1413 | Vector<Double> Dir = dirCol_(whichrow).getAngle("rad").getValue();
|
---|
1414 | std::vector<double> dir;
|
---|
1415 | Dir.tovector(dir);
|
---|
1416 | return dir;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | void asap::Scantable::reshapeSpectrum( int nmin, int nmax )
|
---|
1420 | throw( casa::AipsError )
|
---|
1421 | {
|
---|
1422 | // assumed that all rows have same nChan
|
---|
1423 | Vector<Float> arr = specCol_( 0 ) ;
|
---|
1424 | int nChan = arr.nelements() ;
|
---|
1425 |
|
---|
1426 | // if nmin < 0 or nmax < 0, nothing to do
|
---|
1427 | if ( nmin < 0 ) {
|
---|
1428 | throw( casa::indexError<int>( nmin, "asap::Scantable::reshapeSpectrum: Invalid range. Negative index is specified." ) ) ;
|
---|
1429 | }
|
---|
1430 | if ( nmax < 0 ) {
|
---|
1431 | throw( casa::indexError<int>( nmax, "asap::Scantable::reshapeSpectrum: Invalid range. Negative index is specified." ) ) ;
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | // if nmin > nmax, exchange values
|
---|
1435 | if ( nmin > nmax ) {
|
---|
1436 | int tmp = nmax ;
|
---|
1437 | nmax = nmin ;
|
---|
1438 | nmin = tmp ;
|
---|
1439 | LogIO os( LogOrigin( "Scantable", "reshapeSpectrum()", WHERE ) ) ;
|
---|
1440 | os << "Swap values. Applied range is ["
|
---|
1441 | << nmin << ", " << nmax << "]" << LogIO::POST ;
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | // if nmin exceeds nChan, nothing to do
|
---|
1445 | if ( nmin >= nChan ) {
|
---|
1446 | throw( casa::indexError<int>( nmin, "asap::Scantable::reshapeSpectrum: Invalid range. Specified minimum exceeds nChan." ) ) ;
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 | // if nmax exceeds nChan, reset nmax to nChan
|
---|
1450 | if ( nmax >= nChan ) {
|
---|
1451 | if ( nmin == 0 ) {
|
---|
1452 | // nothing to do
|
---|
1453 | LogIO os( LogOrigin( "Scantable", "reshapeSpectrum()", WHERE ) ) ;
|
---|
1454 | os << "Whole range is selected. Nothing to do." << LogIO::POST ;
|
---|
1455 | return ;
|
---|
1456 | }
|
---|
1457 | else {
|
---|
1458 | LogIO os( LogOrigin( "Scantable", "reshapeSpectrum()", WHERE ) ) ;
|
---|
1459 | os << "Specified maximum exceeds nChan. Applied range is ["
|
---|
1460 | << nmin << ", " << nChan-1 << "]." << LogIO::POST ;
|
---|
1461 | nmax = nChan - 1 ;
|
---|
1462 | }
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 | // reshape specCol_ and flagCol_
|
---|
1466 | for ( int irow = 0 ; irow < nrow() ; irow++ ) {
|
---|
1467 | reshapeSpectrum( nmin, nmax, irow ) ;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | // update FREQUENCIES subtable
|
---|
1471 | Double refpix ;
|
---|
1472 | Double refval ;
|
---|
1473 | Double increment ;
|
---|
1474 | int freqnrow = freqTable_.table().nrow() ;
|
---|
1475 | Vector<uInt> oldId( freqnrow ) ;
|
---|
1476 | Vector<uInt> newId( freqnrow ) ;
|
---|
1477 | for ( int irow = 0 ; irow < freqnrow ; irow++ ) {
|
---|
1478 | freqTable_.getEntry( refpix, refval, increment, irow ) ;
|
---|
1479 | /***
|
---|
1480 | * need to shift refpix to nmin
|
---|
1481 | * note that channel nmin in old index will be channel 0 in new one
|
---|
1482 | ***/
|
---|
1483 | refval = refval - ( refpix - nmin ) * increment ;
|
---|
1484 | refpix = 0 ;
|
---|
1485 | freqTable_.setEntry( refpix, refval, increment, irow ) ;
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | // update nchan
|
---|
1489 | int newsize = nmax - nmin + 1 ;
|
---|
1490 | table_.rwKeywordSet().define( "nChan", newsize ) ;
|
---|
1491 |
|
---|
1492 | // update bandwidth
|
---|
1493 | // assumed all spectra in the scantable have same bandwidth
|
---|
1494 | table_.rwKeywordSet().define( "Bandwidth", increment * newsize ) ;
|
---|
1495 |
|
---|
1496 | return ;
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | void asap::Scantable::reshapeSpectrum( int nmin, int nmax, int irow )
|
---|
1500 | {
|
---|
1501 | // reshape specCol_ and flagCol_
|
---|
1502 | Vector<Float> oldspec = specCol_( irow ) ;
|
---|
1503 | Vector<uChar> oldflag = flagsCol_( irow ) ;
|
---|
1504 | uInt newsize = nmax - nmin + 1 ;
|
---|
1505 | specCol_.put( irow, oldspec( Slice( nmin, newsize, 1 ) ) ) ;
|
---|
1506 | flagsCol_.put( irow, oldflag( Slice( nmin, newsize, 1 ) ) ) ;
|
---|
1507 |
|
---|
1508 | return ;
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | void asap::Scantable::regridChannel( int nChan, double dnu )
|
---|
1512 | {
|
---|
1513 | LogIO os( LogOrigin( "Scantable", "regridChannel()", WHERE ) ) ;
|
---|
1514 | os << "Regrid abcissa with channel number " << nChan << " and spectral resoultion " << dnu << "Hz." << LogIO::POST ;
|
---|
1515 | // assumed that all rows have same nChan
|
---|
1516 | Vector<Float> arr = specCol_( 0 ) ;
|
---|
1517 | int oldsize = arr.nelements() ;
|
---|
1518 |
|
---|
1519 | // if oldsize == nChan, nothing to do
|
---|
1520 | if ( oldsize == nChan ) {
|
---|
1521 | os << "Specified channel number is same as current one. Nothing to do." << LogIO::POST ;
|
---|
1522 | return ;
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | // if oldChan < nChan, unphysical operation
|
---|
1526 | if ( oldsize < nChan ) {
|
---|
1527 | os << "Unphysical operation. Nothing to do." << LogIO::POST ;
|
---|
1528 | return ;
|
---|
1529 | }
|
---|
1530 |
|
---|
1531 | // change channel number for specCol_ and flagCol_
|
---|
1532 | Vector<Float> newspec( nChan, 0 ) ;
|
---|
1533 | Vector<uChar> newflag( nChan, false ) ;
|
---|
1534 | vector<string> coordinfo = getCoordInfo() ;
|
---|
1535 | string oldinfo = coordinfo[0] ;
|
---|
1536 | coordinfo[0] = "Hz" ;
|
---|
1537 | setCoordInfo( coordinfo ) ;
|
---|
1538 | for ( int irow = 0 ; irow < nrow() ; irow++ ) {
|
---|
1539 | regridChannel( nChan, dnu, irow ) ;
|
---|
1540 | }
|
---|
1541 | coordinfo[0] = oldinfo ;
|
---|
1542 | setCoordInfo( coordinfo ) ;
|
---|
1543 |
|
---|
1544 |
|
---|
1545 | // NOTE: this method does not update metadata such as
|
---|
1546 | // FREQUENCIES subtable, nChan, Bandwidth, etc.
|
---|
1547 |
|
---|
1548 | return ;
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | void asap::Scantable::regridChannel( int nChan, double dnu, int irow )
|
---|
1552 | {
|
---|
1553 | // logging
|
---|
1554 | //ofstream ofs( "average.log", std::ios::out | std::ios::app ) ;
|
---|
1555 | //ofs << "IFNO = " << getIF( irow ) << " irow = " << irow << endl ;
|
---|
1556 |
|
---|
1557 | Vector<Float> oldspec = specCol_( irow ) ;
|
---|
1558 | Vector<uChar> oldflag = flagsCol_( irow ) ;
|
---|
1559 | Vector<Float> newspec( nChan, 0 ) ;
|
---|
1560 | Vector<uChar> newflag( nChan, false ) ;
|
---|
1561 |
|
---|
1562 | // regrid
|
---|
1563 | vector<double> abcissa = getAbcissa( irow ) ;
|
---|
1564 | int oldsize = abcissa.size() ;
|
---|
1565 | double olddnu = abcissa[1] - abcissa[0] ;
|
---|
1566 | //int refChan = 0 ;
|
---|
1567 | //double frac = 0.0 ;
|
---|
1568 | //double wedge = 0.0 ;
|
---|
1569 | //double pile = 0.0 ;
|
---|
1570 | int ichan = 0 ;
|
---|
1571 | double wsum = 0.0 ;
|
---|
1572 | Vector<Float> zi( nChan+1 ) ;
|
---|
1573 | Vector<Float> yi( oldsize + 1 ) ;
|
---|
1574 | zi[0] = abcissa[0] - 0.5 * olddnu ;
|
---|
1575 | zi[1] = zi[1] + dnu ;
|
---|
1576 | for ( int ii = 2 ; ii < nChan ; ii++ )
|
---|
1577 | zi[ii] = zi[0] + dnu * ii ;
|
---|
1578 | zi[nChan] = zi[nChan-1] + dnu ;
|
---|
1579 | yi[0] = abcissa[0] - 0.5 * olddnu ;
|
---|
1580 | yi[1] = abcissa[1] + 0.5 * olddnu ;
|
---|
1581 | for ( int ii = 2 ; ii < oldsize ; ii++ )
|
---|
1582 | yi[ii] = abcissa[ii-1] + olddnu ;
|
---|
1583 | yi[oldsize] = abcissa[oldsize-1] + 0.5 * olddnu ;
|
---|
1584 | if ( dnu > 0.0 ) {
|
---|
1585 | for ( int ii = 0 ; ii < nChan ; ii++ ) {
|
---|
1586 | double zl = zi[ii] ;
|
---|
1587 | double zr = zi[ii+1] ;
|
---|
1588 | for ( int j = ichan ; j < oldsize ; j++ ) {
|
---|
1589 | double yl = yi[j] ;
|
---|
1590 | double yr = yi[j+1] ;
|
---|
1591 | if ( yl <= zl ) {
|
---|
1592 | if ( yr <= zl ) {
|
---|
1593 | continue ;
|
---|
1594 | }
|
---|
1595 | else if ( yr <= zr ) {
|
---|
1596 | newspec[ii] += oldspec[j] * ( yr - zl ) ;
|
---|
1597 | newflag[ii] = newflag[ii] || oldflag[j] ;
|
---|
1598 | wsum += ( yr - zl ) ;
|
---|
1599 | }
|
---|
1600 | else {
|
---|
1601 | newspec[ii] += oldspec[j] * dnu ;
|
---|
1602 | newflag[ii] = newflag[ii] || oldflag[j] ;
|
---|
1603 | wsum += dnu ;
|
---|
1604 | ichan = j ;
|
---|
1605 | break ;
|
---|
1606 | }
|
---|
1607 | }
|
---|
1608 | else if ( yl < zr ) {
|
---|
1609 | if ( yr <= zr ) {
|
---|
1610 | newspec[ii] += oldspec[j] * ( yr - yl ) ;
|
---|
1611 | newflag[ii] = newflag[ii] || oldflag[j] ;
|
---|
1612 | wsum += ( yr - yl ) ;
|
---|
1613 | }
|
---|
1614 | else {
|
---|
1615 | newspec[ii] += oldspec[j] * ( zr - yl ) ;
|
---|
1616 | newflag[ii] = newflag[ii] || oldflag[j] ;
|
---|
1617 | wsum += ( zr - yl ) ;
|
---|
1618 | ichan = j ;
|
---|
1619 | break ;
|
---|
1620 | }
|
---|
1621 | }
|
---|
1622 | else {
|
---|
1623 | ichan = j - 1 ;
|
---|
1624 | break ;
|
---|
1625 | }
|
---|
1626 | }
|
---|
1627 | if ( wsum != 0.0 )
|
---|
1628 | newspec[ii] /= wsum ;
|
---|
1629 | wsum = 0.0 ;
|
---|
1630 | }
|
---|
1631 | }
|
---|
1632 | else if ( dnu < 0.0 ) {
|
---|
1633 | for ( int ii = 0 ; ii < nChan ; ii++ ) {
|
---|
1634 | double zl = zi[ii] ;
|
---|
1635 | double zr = zi[ii+1] ;
|
---|
1636 | for ( int j = ichan ; j < oldsize ; j++ ) {
|
---|
1637 | double yl = yi[j] ;
|
---|
1638 | double yr = yi[j+1] ;
|
---|
1639 | if ( yl >= zl ) {
|
---|
1640 | if ( yr >= zl ) {
|
---|
1641 | continue ;
|
---|
1642 | }
|
---|
1643 | else if ( yr >= zr ) {
|
---|
1644 | newspec[ii] += oldspec[j] * abs( yr - zl ) ;
|
---|
1645 | newflag[ii] = newflag[ii] || oldflag[j] ;
|
---|
1646 | wsum += abs( yr - zl ) ;
|
---|
1647 | }
|
---|
1648 | else {
|
---|
1649 | newspec[ii] += oldspec[j] * abs( dnu ) ;
|
---|
1650 | newflag[ii] = newflag[ii] || oldflag[j] ;
|
---|
1651 | wsum += abs( dnu ) ;
|
---|
1652 | ichan = j ;
|
---|
1653 | break ;
|
---|
1654 | }
|
---|
1655 | }
|
---|
1656 | else if ( yl > zr ) {
|
---|
1657 | if ( yr >= zr ) {
|
---|
1658 | newspec[ii] += oldspec[j] * abs( yr - yl ) ;
|
---|
1659 | newflag[ii] = newflag[ii] || oldflag[j] ;
|
---|
1660 | wsum += abs( yr - yl ) ;
|
---|
1661 | }
|
---|
1662 | else {
|
---|
1663 | newspec[ii] += oldspec[j] * abs( zr - yl ) ;
|
---|
1664 | newflag[ii] = newflag[ii] || oldflag[j] ;
|
---|
1665 | wsum += abs( zr - yl ) ;
|
---|
1666 | ichan = j ;
|
---|
1667 | break ;
|
---|
1668 | }
|
---|
1669 | }
|
---|
1670 | else {
|
---|
1671 | ichan = j - 1 ;
|
---|
1672 | break ;
|
---|
1673 | }
|
---|
1674 | }
|
---|
1675 | if ( wsum != 0.0 )
|
---|
1676 | newspec[ii] /= wsum ;
|
---|
1677 | wsum = 0.0 ;
|
---|
1678 | }
|
---|
1679 | }
|
---|
1680 | // * ichan = 0
|
---|
1681 | // ***/
|
---|
1682 | // //ofs << "olddnu = " << olddnu << ", dnu = " << dnu << endl ;
|
---|
1683 | // pile += dnu ;
|
---|
1684 | // wedge = olddnu * ( refChan + 1 ) ;
|
---|
1685 | // while ( wedge < pile ) {
|
---|
1686 | // newspec[0] += olddnu * oldspec[refChan] ;
|
---|
1687 | // newflag[0] = newflag[0] || oldflag[refChan] ;
|
---|
1688 | // //ofs << "channel " << refChan << " is included in new channel 0" << endl ;
|
---|
1689 | // refChan++ ;
|
---|
1690 | // wedge += olddnu ;
|
---|
1691 | // wsum += olddnu ;
|
---|
1692 | // //ofs << "newspec[0] = " << newspec[0] << " wsum = " << wsum << endl ;
|
---|
1693 | // }
|
---|
1694 | // frac = ( wedge - pile ) / olddnu ;
|
---|
1695 | // wsum += ( 1.0 - frac ) * olddnu ;
|
---|
1696 | // newspec[0] += ( 1.0 - frac ) * olddnu * oldspec[refChan] ;
|
---|
1697 | // newflag[0] = newflag[0] || oldflag[refChan] ;
|
---|
1698 | // //ofs << "channel " << refChan << " is partly included in new channel 0" << " with fraction of " << ( 1.0 - frac ) << endl ;
|
---|
1699 | // //ofs << "newspec[0] = " << newspec[0] << " wsum = " << wsum << endl ;
|
---|
1700 | // newspec[0] /= wsum ;
|
---|
1701 | // //ofs << "newspec[0] = " << newspec[0] << endl ;
|
---|
1702 | // //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
|
---|
1703 |
|
---|
1704 | // /***
|
---|
1705 | // * ichan = 1 - nChan-2
|
---|
1706 | // ***/
|
---|
1707 | // for ( int ichan = 1 ; ichan < nChan - 1 ; ichan++ ) {
|
---|
1708 | // pile += dnu ;
|
---|
1709 | // newspec[ichan] += frac * olddnu * oldspec[refChan] ;
|
---|
1710 | // newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
|
---|
1711 | // //ofs << "channel " << refChan << " is partly included in new channel " << ichan << " with fraction of " << frac << endl ;
|
---|
1712 | // refChan++ ;
|
---|
1713 | // wedge += olddnu ;
|
---|
1714 | // wsum = frac * olddnu ;
|
---|
1715 | // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
|
---|
1716 | // while ( wedge < pile ) {
|
---|
1717 | // newspec[ichan] += olddnu * oldspec[refChan] ;
|
---|
1718 | // newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
|
---|
1719 | // //ofs << "channel " << refChan << " is included in new channel " << ichan << endl ;
|
---|
1720 | // refChan++ ;
|
---|
1721 | // wedge += olddnu ;
|
---|
1722 | // wsum += olddnu ;
|
---|
1723 | // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
|
---|
1724 | // }
|
---|
1725 | // frac = ( wedge - pile ) / olddnu ;
|
---|
1726 | // wsum += ( 1.0 - frac ) * olddnu ;
|
---|
1727 | // newspec[ichan] += ( 1.0 - frac ) * olddnu * oldspec[refChan] ;
|
---|
1728 | // newflag[ichan] = newflag[ichan] || oldflag[refChan] ;
|
---|
1729 | // //ofs << "channel " << refChan << " is partly included in new channel " << ichan << " with fraction of " << ( 1.0 - frac ) << endl ;
|
---|
1730 | // //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
|
---|
1731 | // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << " wsum = " << wsum << endl ;
|
---|
1732 | // newspec[ichan] /= wsum ;
|
---|
1733 | // //ofs << "newspec[" << ichan << "] = " << newspec[ichan] << endl ;
|
---|
1734 | // }
|
---|
1735 |
|
---|
1736 | // /***
|
---|
1737 | // * ichan = nChan-1
|
---|
1738 | // ***/
|
---|
1739 | // // NOTE: Assumed that all spectra have the same bandwidth
|
---|
1740 | // pile += dnu ;
|
---|
1741 | // newspec[nChan-1] += frac * olddnu * oldspec[refChan] ;
|
---|
1742 | // newflag[nChan-1] = newflag[nChan-1] || oldflag[refChan] ;
|
---|
1743 | // //ofs << "channel " << refChan << " is partly included in new channel " << nChan-1 << " with fraction of " << frac << endl ;
|
---|
1744 | // refChan++ ;
|
---|
1745 | // wedge += olddnu ;
|
---|
1746 | // wsum = frac * olddnu ;
|
---|
1747 | // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
|
---|
1748 | // for ( int jchan = refChan ; jchan < oldsize ; jchan++ ) {
|
---|
1749 | // newspec[nChan-1] += olddnu * oldspec[jchan] ;
|
---|
1750 | // newflag[nChan-1] = newflag[nChan-1] || oldflag[jchan] ;
|
---|
1751 | // wsum += olddnu ;
|
---|
1752 | // //ofs << "channel " << jchan << " is included in new channel " << nChan-1 << " with fraction of " << frac << endl ;
|
---|
1753 | // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
|
---|
1754 | // }
|
---|
1755 | // //ofs << "wedge = " << wedge << ", pile = " << pile << endl ;
|
---|
1756 | // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << " wsum = " << wsum << endl ;
|
---|
1757 | // newspec[nChan-1] /= wsum ;
|
---|
1758 | // //ofs << "newspec[" << nChan - 1 << "] = " << newspec[nChan-1] << endl ;
|
---|
1759 |
|
---|
1760 | // // ofs.close() ;
|
---|
1761 |
|
---|
1762 | specCol_.put( irow, newspec ) ;
|
---|
1763 | flagsCol_.put( irow, newflag ) ;
|
---|
1764 |
|
---|
1765 | return ;
|
---|
1766 | }
|
---|
1767 |
|
---|
1768 | std::vector<float> Scantable::getWeather(int whichrow) const
|
---|
1769 | {
|
---|
1770 | std::vector<float> out(5);
|
---|
1771 | //Float temperature, pressure, humidity, windspeed, windaz;
|
---|
1772 | weatherTable_.getEntry(out[0], out[1], out[2], out[3], out[4],
|
---|
1773 | mweatheridCol_(uInt(whichrow)));
|
---|
1774 |
|
---|
1775 |
|
---|
1776 | return out;
|
---|
1777 | }
|
---|
1778 |
|
---|
1779 | bool Scantable::getFlagtraFast(uInt whichrow)
|
---|
1780 | {
|
---|
1781 | uChar flag;
|
---|
1782 | Vector<uChar> flags;
|
---|
1783 | flagsCol_.get(whichrow, flags);
|
---|
1784 | flag = flags[0];
|
---|
1785 | for (uInt i = 1; i < flags.size(); ++i) {
|
---|
1786 | flag &= flags[i];
|
---|
1787 | }
|
---|
1788 | return ((flag >> 7) == 1);
|
---|
1789 | }
|
---|
1790 |
|
---|
1791 | void Scantable::polyBaseline(const std::vector<bool>& mask, int order, bool getResidual, bool outLogger, const std::string& blfile)
|
---|
1792 | {
|
---|
1793 | ofstream ofs;
|
---|
1794 | String coordInfo = "";
|
---|
1795 | bool hasSameNchan = true;
|
---|
1796 | bool outTextFile = false;
|
---|
1797 |
|
---|
1798 | if (blfile != "") {
|
---|
1799 | ofs.open(blfile.c_str(), ios::out | ios::app);
|
---|
1800 | if (ofs) outTextFile = true;
|
---|
1801 | }
|
---|
1802 |
|
---|
1803 | if (outLogger || outTextFile) {
|
---|
1804 | coordInfo = getCoordInfo()[0];
|
---|
1805 | if (coordInfo == "") coordInfo = "channel";
|
---|
1806 | hasSameNchan = hasSameNchanOverIFs();
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 | Fitter fitter = Fitter();
|
---|
1810 | fitter.setExpression("poly", order);
|
---|
1811 | //fitter.setIterClipping(thresClip, nIterClip);
|
---|
1812 |
|
---|
1813 | int nRow = nrow();
|
---|
1814 | std::vector<bool> chanMask;
|
---|
1815 |
|
---|
1816 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
1817 | chanMask = getCompositeChanMask(whichrow, mask);
|
---|
1818 | fitBaseline(chanMask, whichrow, fitter);
|
---|
1819 | setSpectrum((getResidual ? fitter.getResidual() : fitter.getFit()), whichrow);
|
---|
1820 | outputFittingResult(outLogger, outTextFile, chanMask, whichrow, coordInfo, hasSameNchan, ofs, "polyBaseline()", fitter);
|
---|
1821 | }
|
---|
1822 |
|
---|
1823 | if (outTextFile) ofs.close();
|
---|
1824 | }
|
---|
1825 |
|
---|
1826 | void Scantable::autoPolyBaseline(const std::vector<bool>& mask, int order, const std::vector<int>& edge, float threshold, int chanAvgLimit, bool getResidual, bool outLogger, const std::string& blfile)
|
---|
1827 | {
|
---|
1828 | ofstream ofs;
|
---|
1829 | String coordInfo = "";
|
---|
1830 | bool hasSameNchan = true;
|
---|
1831 | bool outTextFile = false;
|
---|
1832 |
|
---|
1833 | if (blfile != "") {
|
---|
1834 | ofs.open(blfile.c_str(), ios::out | ios::app);
|
---|
1835 | if (ofs) outTextFile = true;
|
---|
1836 | }
|
---|
1837 |
|
---|
1838 | if (outLogger || outTextFile) {
|
---|
1839 | coordInfo = getCoordInfo()[0];
|
---|
1840 | if (coordInfo == "") coordInfo = "channel";
|
---|
1841 | hasSameNchan = hasSameNchanOverIFs();
|
---|
1842 | }
|
---|
1843 |
|
---|
1844 | Fitter fitter = Fitter();
|
---|
1845 | fitter.setExpression("poly", order);
|
---|
1846 | //fitter.setIterClipping(thresClip, nIterClip);
|
---|
1847 |
|
---|
1848 | int nRow = nrow();
|
---|
1849 | std::vector<bool> chanMask;
|
---|
1850 | int minEdgeSize = getIFNos().size()*2;
|
---|
1851 | STLineFinder lineFinder = STLineFinder();
|
---|
1852 | lineFinder.setOptions(threshold, 3, chanAvgLimit);
|
---|
1853 |
|
---|
1854 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
1855 |
|
---|
1856 | //-------------------------------------------------------
|
---|
1857 | //chanMask = getCompositeChanMask(whichrow, mask, edge, minEdgeSize, lineFinder);
|
---|
1858 | //-------------------------------------------------------
|
---|
1859 | int edgeSize = edge.size();
|
---|
1860 | std::vector<int> currentEdge;
|
---|
1861 | if (edgeSize >= 2) {
|
---|
1862 | int idx = 0;
|
---|
1863 | if (edgeSize > 2) {
|
---|
1864 | if (edgeSize < minEdgeSize) {
|
---|
1865 | throw(AipsError("Length of edge element info is less than that of IFs"));
|
---|
1866 | }
|
---|
1867 | idx = 2 * getIF(whichrow);
|
---|
1868 | }
|
---|
1869 | currentEdge.push_back(edge[idx]);
|
---|
1870 | currentEdge.push_back(edge[idx+1]);
|
---|
1871 | } else {
|
---|
1872 | throw(AipsError("Wrong length of edge element"));
|
---|
1873 | }
|
---|
1874 | lineFinder.setData(getSpectrum(whichrow));
|
---|
1875 | lineFinder.findLines(getCompositeChanMask(whichrow, mask), currentEdge, whichrow);
|
---|
1876 | chanMask = lineFinder.getMask();
|
---|
1877 | //-------------------------------------------------------
|
---|
1878 |
|
---|
1879 | fitBaseline(chanMask, whichrow, fitter);
|
---|
1880 | setSpectrum((getResidual ? fitter.getResidual() : fitter.getFit()), whichrow);
|
---|
1881 |
|
---|
1882 | outputFittingResult(outLogger, outTextFile, chanMask, whichrow, coordInfo, hasSameNchan, ofs, "autoPolyBaseline()", fitter);
|
---|
1883 | }
|
---|
1884 |
|
---|
1885 | if (outTextFile) ofs.close();
|
---|
1886 | }
|
---|
1887 |
|
---|
1888 | void Scantable::cubicSplineBaseline(const std::vector<bool>& mask, int nPiece, float thresClip, int nIterClip, bool getResidual, bool outLogger, const std::string& blfile)
|
---|
1889 | {
|
---|
1890 | ofstream ofs;
|
---|
1891 | String coordInfo = "";
|
---|
1892 | bool hasSameNchan = true;
|
---|
1893 | bool outTextFile = false;
|
---|
1894 |
|
---|
1895 | if (blfile != "") {
|
---|
1896 | ofs.open(blfile.c_str(), ios::out | ios::app);
|
---|
1897 | if (ofs) outTextFile = true;
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 | if (outLogger || outTextFile) {
|
---|
1901 | coordInfo = getCoordInfo()[0];
|
---|
1902 | if (coordInfo == "") coordInfo = "channel";
|
---|
1903 | hasSameNchan = hasSameNchanOverIFs();
|
---|
1904 | }
|
---|
1905 |
|
---|
1906 | //Fitter fitter = Fitter();
|
---|
1907 | //fitter.setExpression("cspline", nPiece);
|
---|
1908 | //fitter.setIterClipping(thresClip, nIterClip);
|
---|
1909 |
|
---|
1910 | int nRow = nrow();
|
---|
1911 | std::vector<bool> chanMask;
|
---|
1912 |
|
---|
1913 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
1914 | chanMask = getCompositeChanMask(whichrow, mask);
|
---|
1915 | //fitBaseline(chanMask, whichrow, fitter);
|
---|
1916 | //setSpectrum((getResidual ? fitter.getResidual() : fitter.getFit()), whichrow);
|
---|
1917 | std::vector<int> pieceEdges;
|
---|
1918 | std::vector<float> params;
|
---|
1919 | std::vector<float> res = doCubicSplineFitting(getSpectrum(whichrow), chanMask, nPiece, pieceEdges, params, thresClip, nIterClip, getResidual);
|
---|
1920 | setSpectrum(res, whichrow);
|
---|
1921 | //
|
---|
1922 |
|
---|
1923 | outputFittingResult(outLogger, outTextFile, chanMask, whichrow, coordInfo, hasSameNchan, ofs, "cubicSplineBaseline()", pieceEdges, params);
|
---|
1924 | }
|
---|
1925 |
|
---|
1926 | if (outTextFile) ofs.close();
|
---|
1927 | }
|
---|
1928 |
|
---|
1929 | 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, bool outLogger, const std::string& blfile)
|
---|
1930 | {
|
---|
1931 | ofstream ofs;
|
---|
1932 | String coordInfo = "";
|
---|
1933 | bool hasSameNchan = true;
|
---|
1934 | bool outTextFile = false;
|
---|
1935 |
|
---|
1936 | if (blfile != "") {
|
---|
1937 | ofs.open(blfile.c_str(), ios::out | ios::app);
|
---|
1938 | if (ofs) outTextFile = true;
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 | if (outLogger || outTextFile) {
|
---|
1942 | coordInfo = getCoordInfo()[0];
|
---|
1943 | if (coordInfo == "") coordInfo = "channel";
|
---|
1944 | hasSameNchan = hasSameNchanOverIFs();
|
---|
1945 | }
|
---|
1946 |
|
---|
1947 | //Fitter fitter = Fitter();
|
---|
1948 | //fitter.setExpression("cspline", nPiece);
|
---|
1949 | //fitter.setIterClipping(thresClip, nIterClip);
|
---|
1950 |
|
---|
1951 | int nRow = nrow();
|
---|
1952 | std::vector<bool> chanMask;
|
---|
1953 | int minEdgeSize = getIFNos().size()*2;
|
---|
1954 | STLineFinder lineFinder = STLineFinder();
|
---|
1955 | lineFinder.setOptions(threshold, 3, chanAvgLimit);
|
---|
1956 |
|
---|
1957 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
1958 |
|
---|
1959 | //-------------------------------------------------------
|
---|
1960 | //chanMask = getCompositeChanMask(whichrow, mask, edge, minEdgeSize, lineFinder);
|
---|
1961 | //-------------------------------------------------------
|
---|
1962 | int edgeSize = edge.size();
|
---|
1963 | std::vector<int> currentEdge;
|
---|
1964 | if (edgeSize >= 2) {
|
---|
1965 | int idx = 0;
|
---|
1966 | if (edgeSize > 2) {
|
---|
1967 | if (edgeSize < minEdgeSize) {
|
---|
1968 | throw(AipsError("Length of edge element info is less than that of IFs"));
|
---|
1969 | }
|
---|
1970 | idx = 2 * getIF(whichrow);
|
---|
1971 | }
|
---|
1972 | currentEdge.push_back(edge[idx]);
|
---|
1973 | currentEdge.push_back(edge[idx+1]);
|
---|
1974 | } else {
|
---|
1975 | throw(AipsError("Wrong length of edge element"));
|
---|
1976 | }
|
---|
1977 | lineFinder.setData(getSpectrum(whichrow));
|
---|
1978 | lineFinder.findLines(getCompositeChanMask(whichrow, mask), currentEdge, whichrow);
|
---|
1979 | chanMask = lineFinder.getMask();
|
---|
1980 | //-------------------------------------------------------
|
---|
1981 |
|
---|
1982 |
|
---|
1983 | //fitBaseline(chanMask, whichrow, fitter);
|
---|
1984 | //setSpectrum((getResidual ? fitter.getResidual() : fitter.getFit()), whichrow);
|
---|
1985 | std::vector<int> pieceEdges;
|
---|
1986 | std::vector<float> params;
|
---|
1987 | std::vector<float> res = doCubicSplineFitting(getSpectrum(whichrow), chanMask, nPiece, pieceEdges, params, thresClip, nIterClip, getResidual);
|
---|
1988 | setSpectrum(res, whichrow);
|
---|
1989 | //
|
---|
1990 |
|
---|
1991 | outputFittingResult(outLogger, outTextFile, chanMask, whichrow, coordInfo, hasSameNchan, ofs, "autoCubicSplineBaseline()", pieceEdges, params);
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | if (outTextFile) ofs.close();
|
---|
1995 | }
|
---|
1996 |
|
---|
1997 | 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 thresClip, int nIterClip, bool getResidual)
|
---|
1998 | {
|
---|
1999 | if (data.size() != mask.size()) {
|
---|
2000 | throw(AipsError("data and mask sizes are not identical"));
|
---|
2001 | }
|
---|
2002 | if (nPiece < 1) {
|
---|
2003 | throw(AipsError("number of the sections must be one or more"));
|
---|
2004 | }
|
---|
2005 |
|
---|
2006 | int nChan = data.size();
|
---|
2007 | std::vector<int> maskArray;
|
---|
2008 | std::vector<int> x;
|
---|
2009 | for (int i = 0; i < nChan; ++i) {
|
---|
2010 | maskArray.push_back(mask[i] ? 1 : 0);
|
---|
2011 | if (mask[i]) {
|
---|
2012 | x.push_back(i);
|
---|
2013 | }
|
---|
2014 | }
|
---|
2015 |
|
---|
2016 | int initNData = x.size();
|
---|
2017 |
|
---|
2018 | int nElement = (int)(floor(floor((double)(initNData/nPiece))+0.5));
|
---|
2019 | std::vector<double> invEdge;
|
---|
2020 | idxEdge.clear();
|
---|
2021 | idxEdge.push_back(x[0]);
|
---|
2022 | for (int i = 1; i < nPiece; ++i) {
|
---|
2023 | int valX = x[nElement*i];
|
---|
2024 | idxEdge.push_back(valX);
|
---|
2025 | invEdge.push_back(1.0/(double)valX);
|
---|
2026 | }
|
---|
2027 | idxEdge.push_back(x[x.size()-1]+1);
|
---|
2028 |
|
---|
2029 | int nData = initNData;
|
---|
2030 | int nDOF = nPiece + 3; //number of parameters to solve, namely, 4+(nPiece-1).
|
---|
2031 |
|
---|
2032 | std::vector<double> x1, x2, x3, z1, x1z1, x2z1, x3z1, r1, residual;
|
---|
2033 | for (int i = 0; i < nChan; ++i) {
|
---|
2034 | double di = (double)i;
|
---|
2035 | double dD = (double)data[i];
|
---|
2036 | x1.push_back(di);
|
---|
2037 | x2.push_back(di*di);
|
---|
2038 | x3.push_back(di*di*di);
|
---|
2039 | z1.push_back(dD);
|
---|
2040 | x1z1.push_back(dD*di);
|
---|
2041 | x2z1.push_back(dD*di*di);
|
---|
2042 | x3z1.push_back(dD*di*di*di);
|
---|
2043 | r1.push_back(0.0);
|
---|
2044 | residual.push_back(0.0);
|
---|
2045 | }
|
---|
2046 |
|
---|
2047 | for (int nClip = 0; nClip < nIterClip+1; ++nClip) {
|
---|
2048 | // xMatrix : horizontal concatenation of
|
---|
2049 | // the least-sq. matrix (left) and an
|
---|
2050 | // identity matrix (right).
|
---|
2051 | // the right part is used to calculate the inverse matrix of the left part.
|
---|
2052 | double xMatrix[nDOF][2*nDOF];
|
---|
2053 | double zMatrix[nDOF];
|
---|
2054 | for (int i = 0; i < nDOF; ++i) {
|
---|
2055 | for (int j = 0; j < 2*nDOF; ++j) {
|
---|
2056 | xMatrix[i][j] = 0.0;
|
---|
2057 | }
|
---|
2058 | xMatrix[i][nDOF+i] = 1.0;
|
---|
2059 | zMatrix[i] = 0.0;
|
---|
2060 | }
|
---|
2061 |
|
---|
2062 | for (int n = 0; n < nPiece; ++n) {
|
---|
2063 | for (int i = idxEdge[n]; i < idxEdge[n+1]; ++i) {
|
---|
2064 |
|
---|
2065 | if (maskArray[i] == 0) continue;
|
---|
2066 |
|
---|
2067 | xMatrix[0][0] += 1.0;
|
---|
2068 | xMatrix[0][1] += x1[i];
|
---|
2069 | xMatrix[0][2] += x2[i];
|
---|
2070 | xMatrix[0][3] += x3[i];
|
---|
2071 | xMatrix[1][1] += x2[i];
|
---|
2072 | xMatrix[1][2] += x3[i];
|
---|
2073 | xMatrix[1][3] += x2[i]*x2[i];
|
---|
2074 | xMatrix[2][2] += x2[i]*x2[i];
|
---|
2075 | xMatrix[2][3] += x3[i]*x2[i];
|
---|
2076 | xMatrix[3][3] += x3[i]*x3[i];
|
---|
2077 | zMatrix[0] += z1[i];
|
---|
2078 | zMatrix[1] += x1z1[i];
|
---|
2079 | zMatrix[2] += x2z1[i];
|
---|
2080 | zMatrix[3] += x3z1[i];
|
---|
2081 |
|
---|
2082 | for (int j = 0; j < n; ++j) {
|
---|
2083 | double q = 1.0 - x1[i]*invEdge[j];
|
---|
2084 | q = q*q*q;
|
---|
2085 | xMatrix[0][j+4] += q;
|
---|
2086 | xMatrix[1][j+4] += q*x1[i];
|
---|
2087 | xMatrix[2][j+4] += q*x2[i];
|
---|
2088 | xMatrix[3][j+4] += q*x3[i];
|
---|
2089 | for (int k = 0; k < j; ++k) {
|
---|
2090 | double r = 1.0 - x1[i]*invEdge[k];
|
---|
2091 | r = r*r*r;
|
---|
2092 | xMatrix[k+4][j+4] += r*q;
|
---|
2093 | }
|
---|
2094 | xMatrix[j+4][j+4] += q*q;
|
---|
2095 | zMatrix[j+4] += q*z1[i];
|
---|
2096 | }
|
---|
2097 |
|
---|
2098 | }
|
---|
2099 | }
|
---|
2100 |
|
---|
2101 | for (int i = 0; i < nDOF; ++i) {
|
---|
2102 | for (int j = 0; j < i; ++j) {
|
---|
2103 | xMatrix[i][j] = xMatrix[j][i];
|
---|
2104 | }
|
---|
2105 | }
|
---|
2106 |
|
---|
2107 | std::vector<double> invDiag;
|
---|
2108 | for (int i = 0; i < nDOF; ++i) {
|
---|
2109 | invDiag.push_back(1.0/xMatrix[i][i]);
|
---|
2110 | for (int j = 0; j < nDOF; ++j) {
|
---|
2111 | xMatrix[i][j] *= invDiag[i];
|
---|
2112 | }
|
---|
2113 | }
|
---|
2114 |
|
---|
2115 | for (int k = 0; k < nDOF; ++k) {
|
---|
2116 | for (int i = 0; i < nDOF; ++i) {
|
---|
2117 | if (i != k) {
|
---|
2118 | double factor1 = xMatrix[k][k];
|
---|
2119 | double factor2 = xMatrix[i][k];
|
---|
2120 | for (int j = k; j < 2*nDOF; ++j) {
|
---|
2121 | xMatrix[i][j] *= factor1;
|
---|
2122 | xMatrix[i][j] -= xMatrix[k][j]*factor2;
|
---|
2123 | xMatrix[i][j] /= factor1;
|
---|
2124 | }
|
---|
2125 | }
|
---|
2126 | }
|
---|
2127 | double xDiag = xMatrix[k][k];
|
---|
2128 | for (int j = k; j < 2*nDOF; ++j) {
|
---|
2129 | xMatrix[k][j] /= xDiag;
|
---|
2130 | }
|
---|
2131 | }
|
---|
2132 |
|
---|
2133 | for (int i = 0; i < nDOF; ++i) {
|
---|
2134 | for (int j = 0; j < nDOF; ++j) {
|
---|
2135 | xMatrix[i][nDOF+j] *= invDiag[j];
|
---|
2136 | }
|
---|
2137 | }
|
---|
2138 | //compute a vector y which consists of the coefficients of the best-fit spline curves
|
---|
2139 | //(a0,a1,a2,a3(,b3,c3,...)), namely, the ones for the leftmost piece and the ones of
|
---|
2140 | //cubic terms for the other pieces (in case nPiece>1).
|
---|
2141 | std::vector<double> y;
|
---|
2142 | y.clear();
|
---|
2143 | for (int i = 0; i < nDOF; ++i) {
|
---|
2144 | y.push_back(0.0);
|
---|
2145 | for (int j = 0; j < nDOF; ++j) {
|
---|
2146 | y[i] += xMatrix[i][nDOF+j]*zMatrix[j];
|
---|
2147 | }
|
---|
2148 | }
|
---|
2149 |
|
---|
2150 | double a0 = y[0];
|
---|
2151 | double a1 = y[1];
|
---|
2152 | double a2 = y[2];
|
---|
2153 | double a3 = y[3];
|
---|
2154 | params.clear();
|
---|
2155 |
|
---|
2156 | for (int n = 0; n < nPiece; ++n) {
|
---|
2157 | for (int i = idxEdge[n]; i < idxEdge[n+1]; ++i) {
|
---|
2158 | r1[i] = a0 + a1*x1[i] + a2*x2[i] + a3*x3[i];
|
---|
2159 | residual[i] = z1[i] - r1[i];
|
---|
2160 | }
|
---|
2161 | params.push_back(a0);
|
---|
2162 | params.push_back(a1);
|
---|
2163 | params.push_back(a2);
|
---|
2164 | params.push_back(a3);
|
---|
2165 |
|
---|
2166 | if (n == nPiece-1) break;
|
---|
2167 |
|
---|
2168 | double d = y[4+n];
|
---|
2169 | double iE = invEdge[n];
|
---|
2170 | a0 += d;
|
---|
2171 | a1 -= 3.0*d*iE;
|
---|
2172 | a2 += 3.0*d*iE*iE;
|
---|
2173 | a3 -= d*iE*iE*iE;
|
---|
2174 | }
|
---|
2175 |
|
---|
2176 | if ((nClip == nIterClip) || (thresClip <= 0.0)) {
|
---|
2177 | break;
|
---|
2178 | } else {
|
---|
2179 | double stdDev = 0.0;
|
---|
2180 | for (int i = 0; i < nChan; ++i) {
|
---|
2181 | stdDev += residual[i]*residual[i]*(double)maskArray[i];
|
---|
2182 | }
|
---|
2183 | stdDev = sqrt(stdDev/(double)nData);
|
---|
2184 |
|
---|
2185 | double thres = stdDev * thresClip;
|
---|
2186 | int newNData = 0;
|
---|
2187 | for (int i = 0; i < nChan; ++i) {
|
---|
2188 | if (abs(residual[i]) >= thres) {
|
---|
2189 | maskArray[i] = 0;
|
---|
2190 | }
|
---|
2191 | if (maskArray[i] > 0) {
|
---|
2192 | newNData++;
|
---|
2193 | }
|
---|
2194 | }
|
---|
2195 | if (newNData == nData) {
|
---|
2196 | break; //no more flag to add. iteration stops.
|
---|
2197 | } else {
|
---|
2198 | nData = newNData;
|
---|
2199 | }
|
---|
2200 | }
|
---|
2201 | }
|
---|
2202 |
|
---|
2203 | std::vector<float> result;
|
---|
2204 | if (getResidual) {
|
---|
2205 | for (int i = 0; i < nChan; ++i) {
|
---|
2206 | result.push_back((float)residual[i]);
|
---|
2207 | }
|
---|
2208 | } else {
|
---|
2209 | for (int i = 0; i < nChan; ++i) {
|
---|
2210 | result.push_back((float)r1[i]);
|
---|
2211 | }
|
---|
2212 | }
|
---|
2213 |
|
---|
2214 | return result;
|
---|
2215 | }
|
---|
2216 |
|
---|
2217 | void Scantable::sinusoidBaseline(const std::vector<bool>& mask, const std::vector<int>& nWaves, float maxWaveLength, float thresClip, int nIterClip, bool getResidual, bool outLogger, const std::string& blfile)
|
---|
2218 | {
|
---|
2219 | ofstream ofs;
|
---|
2220 | String coordInfo = "";
|
---|
2221 | bool hasSameNchan = true;
|
---|
2222 | bool outTextFile = false;
|
---|
2223 |
|
---|
2224 | if (blfile != "") {
|
---|
2225 | ofs.open(blfile.c_str(), ios::out | ios::app);
|
---|
2226 | if (ofs) outTextFile = true;
|
---|
2227 | }
|
---|
2228 |
|
---|
2229 | if (outLogger || outTextFile) {
|
---|
2230 | coordInfo = getCoordInfo()[0];
|
---|
2231 | if (coordInfo == "") coordInfo = "channel";
|
---|
2232 | hasSameNchan = hasSameNchanOverIFs();
|
---|
2233 | }
|
---|
2234 |
|
---|
2235 | //Fitter fitter = Fitter();
|
---|
2236 | //fitter.setExpression("sinusoid", nWaves);
|
---|
2237 | //fitter.setIterClipping(thresClip, nIterClip);
|
---|
2238 |
|
---|
2239 | int nRow = nrow();
|
---|
2240 | std::vector<bool> chanMask;
|
---|
2241 |
|
---|
2242 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
2243 | chanMask = getCompositeChanMask(whichrow, mask);
|
---|
2244 | //fitBaseline(chanMask, whichrow, fitter);
|
---|
2245 | //setSpectrum((getResidual ? fitter.getResidual() : fitter.getFit()), whichrow);
|
---|
2246 | std::vector<float> params;
|
---|
2247 | std::vector<float> res = doSinusoidFitting(getSpectrum(whichrow), chanMask, nWaves, maxWaveLength, params, thresClip, nIterClip, getResidual);
|
---|
2248 | setSpectrum(res, whichrow);
|
---|
2249 | //
|
---|
2250 |
|
---|
2251 | outputFittingResult(outLogger, outTextFile, chanMask, whichrow, coordInfo, hasSameNchan, ofs, "sinusoidBaseline()", params);
|
---|
2252 | }
|
---|
2253 |
|
---|
2254 | if (outTextFile) ofs.close();
|
---|
2255 | }
|
---|
2256 |
|
---|
2257 | void Scantable::autoSinusoidBaseline(const std::vector<bool>& mask, const std::vector<int>& nWaves, float maxWaveLength, float thresClip, int nIterClip, const std::vector<int>& edge, float threshold, int chanAvgLimit, bool getResidual, bool outLogger, const std::string& blfile)
|
---|
2258 | {
|
---|
2259 | ofstream ofs;
|
---|
2260 | String coordInfo = "";
|
---|
2261 | bool hasSameNchan = true;
|
---|
2262 | bool outTextFile = false;
|
---|
2263 |
|
---|
2264 | if (blfile != "") {
|
---|
2265 | ofs.open(blfile.c_str(), ios::out | ios::app);
|
---|
2266 | if (ofs) outTextFile = true;
|
---|
2267 | }
|
---|
2268 |
|
---|
2269 | if (outLogger || outTextFile) {
|
---|
2270 | coordInfo = getCoordInfo()[0];
|
---|
2271 | if (coordInfo == "") coordInfo = "channel";
|
---|
2272 | hasSameNchan = hasSameNchanOverIFs();
|
---|
2273 | }
|
---|
2274 |
|
---|
2275 | //Fitter fitter = Fitter();
|
---|
2276 | //fitter.setExpression("sinusoid", nWaves);
|
---|
2277 | //fitter.setIterClipping(thresClip, nIterClip);
|
---|
2278 |
|
---|
2279 | int nRow = nrow();
|
---|
2280 | std::vector<bool> chanMask;
|
---|
2281 | int minEdgeSize = getIFNos().size()*2;
|
---|
2282 | STLineFinder lineFinder = STLineFinder();
|
---|
2283 | lineFinder.setOptions(threshold, 3, chanAvgLimit);
|
---|
2284 |
|
---|
2285 | for (int whichrow = 0; whichrow < nRow; ++whichrow) {
|
---|
2286 |
|
---|
2287 | //-------------------------------------------------------
|
---|
2288 | //chanMask = getCompositeChanMask(whichrow, mask, edge, minEdgeSize, lineFinder);
|
---|
2289 | //-------------------------------------------------------
|
---|
2290 | int edgeSize = edge.size();
|
---|
2291 | std::vector<int> currentEdge;
|
---|
2292 | if (edgeSize >= 2) {
|
---|
2293 | int idx = 0;
|
---|
2294 | if (edgeSize > 2) {
|
---|
2295 | if (edgeSize < minEdgeSize) {
|
---|
2296 | throw(AipsError("Length of edge element info is less than that of IFs"));
|
---|
2297 | }
|
---|
2298 | idx = 2 * getIF(whichrow);
|
---|
2299 | }
|
---|
2300 | currentEdge.push_back(edge[idx]);
|
---|
2301 | currentEdge.push_back(edge[idx+1]);
|
---|
2302 | } else {
|
---|
2303 | throw(AipsError("Wrong length of edge element"));
|
---|
2304 | }
|
---|
2305 | lineFinder.setData(getSpectrum(whichrow));
|
---|
2306 | lineFinder.findLines(getCompositeChanMask(whichrow, mask), currentEdge, whichrow);
|
---|
2307 | chanMask = lineFinder.getMask();
|
---|
2308 | //-------------------------------------------------------
|
---|
2309 |
|
---|
2310 |
|
---|
2311 | //fitBaseline(chanMask, whichrow, fitter);
|
---|
2312 | //setSpectrum((getResidual ? fitter.getResidual() : fitter.getFit()), whichrow);
|
---|
2313 | std::vector<float> params;
|
---|
2314 | std::vector<float> res = doSinusoidFitting(getSpectrum(whichrow), chanMask, nWaves, maxWaveLength, params, thresClip, nIterClip, getResidual);
|
---|
2315 | setSpectrum(res, whichrow);
|
---|
2316 | //
|
---|
2317 |
|
---|
2318 | outputFittingResult(outLogger, outTextFile, chanMask, whichrow, coordInfo, hasSameNchan, ofs, "autoSinusoidBaseline()", params);
|
---|
2319 | }
|
---|
2320 |
|
---|
2321 | if (outTextFile) ofs.close();
|
---|
2322 | }
|
---|
2323 |
|
---|
2324 | std::vector<float> Scantable::doSinusoidFitting(const std::vector<float>& data, const std::vector<bool>& mask, const std::vector<int>& waveNumbers, float maxWaveLength, std::vector<float>& params, float thresClip, int nIterClip, bool getResidual)
|
---|
2325 | {
|
---|
2326 | if (data.size() != mask.size()) {
|
---|
2327 | throw(AipsError("data and mask sizes are not identical"));
|
---|
2328 | }
|
---|
2329 | if (data.size() < 2) {
|
---|
2330 | throw(AipsError("data size is too short"));
|
---|
2331 | }
|
---|
2332 | if (waveNumbers.size() == 0) {
|
---|
2333 | throw(AipsError("missing wave number info"));
|
---|
2334 | }
|
---|
2335 | std::vector<int> nWaves; // sorted and uniqued array of wave numbers
|
---|
2336 | nWaves.reserve(waveNumbers.size());
|
---|
2337 | copy(waveNumbers.begin(), waveNumbers.end(), back_inserter(nWaves));
|
---|
2338 | sort(nWaves.begin(), nWaves.end());
|
---|
2339 | std::vector<int>::iterator end_it = unique(nWaves.begin(), nWaves.end());
|
---|
2340 | nWaves.erase(end_it, nWaves.end());
|
---|
2341 |
|
---|
2342 | int minNWaves = nWaves[0];
|
---|
2343 | if (minNWaves < 0) {
|
---|
2344 | throw(AipsError("wave number must be positive or zero (i.e. constant)"));
|
---|
2345 | }
|
---|
2346 | bool hasConstantTerm = (minNWaves == 0);
|
---|
2347 |
|
---|
2348 | int nChan = data.size();
|
---|
2349 | std::vector<int> maskArray;
|
---|
2350 | std::vector<int> x;
|
---|
2351 | for (int i = 0; i < nChan; ++i) {
|
---|
2352 | maskArray.push_back(mask[i] ? 1 : 0);
|
---|
2353 | if (mask[i]) {
|
---|
2354 | x.push_back(i);
|
---|
2355 | }
|
---|
2356 | }
|
---|
2357 |
|
---|
2358 | int initNData = x.size();
|
---|
2359 |
|
---|
2360 | int nData = initNData;
|
---|
2361 | int nDOF = nWaves.size() * 2 - (hasConstantTerm ? 1 : 0); //number of parameters to solve.
|
---|
2362 |
|
---|
2363 | const double PI = 6.0 * asin(0.5); // PI (= 3.141592653...)
|
---|
2364 | double baseXFactor = 2.0*PI/(double)maxWaveLength/(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)
|
---|
2365 |
|
---|
2366 | // xArray : contains elemental values for computing the least-square matrix.
|
---|
2367 | // xArray.size() is nDOF and xArray[*].size() is nChan.
|
---|
2368 | // Each xArray element are as follows:
|
---|
2369 | // xArray[0] = {1.0, 1.0, 1.0, ..., 1.0},
|
---|
2370 | // xArray[2n-1] = {sin(nPI/L*x[0]), sin(nPI/L*x[1]), ..., sin(nPI/L*x[nChan])},
|
---|
2371 | // xArray[2n] = {cos(nPI/L*x[0]), cos(nPI/L*x[1]), ..., cos(nPI/L*x[nChan])},
|
---|
2372 | // where (1 <= n <= nMaxWavesInSW),
|
---|
2373 | // or,
|
---|
2374 | // xArray[2n-1] = {sin(wn[n]PI/L*x[0]), sin(wn[n]PI/L*x[1]), ..., sin(wn[n]PI/L*x[nChan])},
|
---|
2375 | // xArray[2n] = {cos(wn[n]PI/L*x[0]), cos(wn[n]PI/L*x[1]), ..., cos(wn[n]PI/L*x[nChan])},
|
---|
2376 | // where wn[n] denotes waveNumbers[n] (1 <= n <= waveNumbers.size()).
|
---|
2377 | std::vector<std::vector<double> > xArray;
|
---|
2378 | if (hasConstantTerm) {
|
---|
2379 | std::vector<double> xu;
|
---|
2380 | for (int j = 0; j < nChan; ++j) {
|
---|
2381 | xu.push_back(1.0);
|
---|
2382 | }
|
---|
2383 | xArray.push_back(xu);
|
---|
2384 | }
|
---|
2385 | for (uInt i = (hasConstantTerm ? 1 : 0); i < nWaves.size(); ++i) {
|
---|
2386 | double xFactor = baseXFactor*(double)nWaves[i];
|
---|
2387 | std::vector<double> xs, xc;
|
---|
2388 | xs.clear();
|
---|
2389 | xc.clear();
|
---|
2390 | for (int j = 0; j < nChan; ++j) {
|
---|
2391 | xs.push_back(sin(xFactor*(double)j));
|
---|
2392 | xc.push_back(cos(xFactor*(double)j));
|
---|
2393 | }
|
---|
2394 | xArray.push_back(xs);
|
---|
2395 | xArray.push_back(xc);
|
---|
2396 | }
|
---|
2397 |
|
---|
2398 | std::vector<double> z1, r1, residual;
|
---|
2399 | for (int i = 0; i < nChan; ++i) {
|
---|
2400 | z1.push_back((double)data[i]);
|
---|
2401 | r1.push_back(0.0);
|
---|
2402 | residual.push_back(0.0);
|
---|
2403 | }
|
---|
2404 |
|
---|
2405 | for (int nClip = 0; nClip < nIterClip+1; ++nClip) {
|
---|
2406 | // xMatrix : horizontal concatenation of
|
---|
2407 | // the least-sq. matrix (left) and an
|
---|
2408 | // identity matrix (right).
|
---|
2409 | // the right part is used to calculate the inverse matrix of the left part.
|
---|
2410 | double xMatrix[nDOF][2*nDOF];
|
---|
2411 | double zMatrix[nDOF];
|
---|
2412 | for (int i = 0; i < nDOF; ++i) {
|
---|
2413 | for (int j = 0; j < 2*nDOF; ++j) {
|
---|
2414 | xMatrix[i][j] = 0.0;
|
---|
2415 | }
|
---|
2416 | xMatrix[i][nDOF+i] = 1.0;
|
---|
2417 | zMatrix[i] = 0.0;
|
---|
2418 | }
|
---|
2419 |
|
---|
2420 | for (int k = 0; k < nChan; ++k) {
|
---|
2421 | if (maskArray[k] == 0) continue;
|
---|
2422 |
|
---|
2423 | for (int i = 0; i < nDOF; ++i) {
|
---|
2424 | for (int j = i; j < nDOF; ++j) {
|
---|
2425 | xMatrix[i][j] += xArray[i][k] * xArray[j][k];
|
---|
2426 | }
|
---|
2427 | zMatrix[i] += z1[k] * xArray[i][k];
|
---|
2428 | }
|
---|
2429 | }
|
---|
2430 |
|
---|
2431 | for (int i = 0; i < nDOF; ++i) {
|
---|
2432 | for (int j = 0; j < i; ++j) {
|
---|
2433 | xMatrix[i][j] = xMatrix[j][i];
|
---|
2434 | }
|
---|
2435 | }
|
---|
2436 |
|
---|
2437 | std::vector<double> invDiag;
|
---|
2438 | for (int i = 0; i < nDOF; ++i) {
|
---|
2439 | invDiag.push_back(1.0/xMatrix[i][i]);
|
---|
2440 | for (int j = 0; j < nDOF; ++j) {
|
---|
2441 | xMatrix[i][j] *= invDiag[i];
|
---|
2442 | }
|
---|
2443 | }
|
---|
2444 |
|
---|
2445 | for (int k = 0; k < nDOF; ++k) {
|
---|
2446 | for (int i = 0; i < nDOF; ++i) {
|
---|
2447 | if (i != k) {
|
---|
2448 | double factor1 = xMatrix[k][k];
|
---|
2449 | double factor2 = xMatrix[i][k];
|
---|
2450 | for (int j = k; j < 2*nDOF; ++j) {
|
---|
2451 | xMatrix[i][j] *= factor1;
|
---|
2452 | xMatrix[i][j] -= xMatrix[k][j]*factor2;
|
---|
2453 | xMatrix[i][j] /= factor1;
|
---|
2454 | }
|
---|
2455 | }
|
---|
2456 | }
|
---|
2457 | double xDiag = xMatrix[k][k];
|
---|
2458 | for (int j = k; j < 2*nDOF; ++j) {
|
---|
2459 | xMatrix[k][j] /= xDiag;
|
---|
2460 | }
|
---|
2461 | }
|
---|
2462 |
|
---|
2463 | for (int i = 0; i < nDOF; ++i) {
|
---|
2464 | for (int j = 0; j < nDOF; ++j) {
|
---|
2465 | xMatrix[i][nDOF+j] *= invDiag[j];
|
---|
2466 | }
|
---|
2467 | }
|
---|
2468 | //compute a vector y which consists of the coefficients of the sinusoids forming the
|
---|
2469 | //best-fit curves (a0,s1,c1,s2,c2,...), where a0 is constant and s* and c* are of sine
|
---|
2470 | //and cosine functions, respectively.
|
---|
2471 | std::vector<double> y;
|
---|
2472 | params.clear();
|
---|
2473 | for (int i = 0; i < nDOF; ++i) {
|
---|
2474 | y.push_back(0.0);
|
---|
2475 | for (int j = 0; j < nDOF; ++j) {
|
---|
2476 | y[i] += xMatrix[i][nDOF+j]*zMatrix[j];
|
---|
2477 | }
|
---|
2478 | params.push_back(y[i]);
|
---|
2479 | }
|
---|
2480 |
|
---|
2481 | for (int i = 0; i < nChan; ++i) {
|
---|
2482 | r1[i] = y[0];
|
---|
2483 | for (int j = 1; j < nDOF; ++j) {
|
---|
2484 | r1[i] += y[j]*xArray[j][i];
|
---|
2485 | }
|
---|
2486 | residual[i] = z1[i] - r1[i];
|
---|
2487 | }
|
---|
2488 |
|
---|
2489 | if ((nClip == nIterClip) || (thresClip <= 0.0)) {
|
---|
2490 | break;
|
---|
2491 | } else {
|
---|
2492 | double stdDev = 0.0;
|
---|
2493 | for (int i = 0; i < nChan; ++i) {
|
---|
2494 | stdDev += residual[i]*residual[i]*(double)maskArray[i];
|
---|
2495 | }
|
---|
2496 | stdDev = sqrt(stdDev/(double)nData);
|
---|
2497 |
|
---|
2498 | double thres = stdDev * thresClip;
|
---|
2499 | int newNData = 0;
|
---|
2500 | for (int i = 0; i < nChan; ++i) {
|
---|
2501 | if (abs(residual[i]) >= thres) {
|
---|
2502 | maskArray[i] = 0;
|
---|
2503 | }
|
---|
2504 | if (maskArray[i] > 0) {
|
---|
2505 | newNData++;
|
---|
2506 | }
|
---|
2507 | }
|
---|
2508 | if (newNData == nData) {
|
---|
2509 | break; //no more flag to add. iteration stops.
|
---|
2510 | } else {
|
---|
2511 | nData = newNData;
|
---|
2512 | }
|
---|
2513 | }
|
---|
2514 | }
|
---|
2515 |
|
---|
2516 | std::vector<float> result;
|
---|
2517 | if (getResidual) {
|
---|
2518 | for (int i = 0; i < nChan; ++i) {
|
---|
2519 | result.push_back((float)residual[i]);
|
---|
2520 | }
|
---|
2521 | } else {
|
---|
2522 | for (int i = 0; i < nChan; ++i) {
|
---|
2523 | result.push_back((float)r1[i]);
|
---|
2524 | }
|
---|
2525 | }
|
---|
2526 |
|
---|
2527 | return result;
|
---|
2528 | }
|
---|
2529 |
|
---|
2530 | void Scantable::fitBaseline(const std::vector<bool>& mask, int whichrow, Fitter& fitter)
|
---|
2531 | {
|
---|
2532 | std::vector<double> dAbcissa = getAbcissa(whichrow);
|
---|
2533 | std::vector<float> abcissa;
|
---|
2534 | for (uInt i = 0; i < dAbcissa.size(); ++i) {
|
---|
2535 | abcissa.push_back((float)dAbcissa[i]);
|
---|
2536 | }
|
---|
2537 | std::vector<float> spec = getSpectrum(whichrow);
|
---|
2538 |
|
---|
2539 | fitter.setData(abcissa, spec, mask);
|
---|
2540 | fitter.lfit();
|
---|
2541 | }
|
---|
2542 |
|
---|
2543 | std::vector<bool> Scantable::getCompositeChanMask(int whichrow, const std::vector<bool>& inMask)
|
---|
2544 | {
|
---|
2545 | std::vector<bool> chanMask = getMask(whichrow);
|
---|
2546 | uInt chanMaskSize = chanMask.size();
|
---|
2547 | if (chanMaskSize != inMask.size()) {
|
---|
2548 | throw(AipsError("different mask sizes"));
|
---|
2549 | }
|
---|
2550 | for (uInt i = 0; i < chanMaskSize; ++i) {
|
---|
2551 | chanMask[i] = chanMask[i] && inMask[i];
|
---|
2552 | }
|
---|
2553 |
|
---|
2554 | return chanMask;
|
---|
2555 | }
|
---|
2556 |
|
---|
2557 | /*
|
---|
2558 | std::vector<bool> Scantable::getCompositeChanMask(int whichrow, const std::vector<bool>& inMask, const std::vector<int>& edge, const int minEdgeSize, STLineFinder& lineFinder)
|
---|
2559 | {
|
---|
2560 | int edgeSize = edge.size();
|
---|
2561 | std::vector<int> currentEdge;
|
---|
2562 | if (edgeSize >= 2) {
|
---|
2563 | int idx = 0;
|
---|
2564 | if (edgeSize > 2) {
|
---|
2565 | if (edgeSize < minEdgeSize) {
|
---|
2566 | throw(AipsError("Length of edge element info is less than that of IFs"));
|
---|
2567 | }
|
---|
2568 | idx = 2 * getIF(whichrow);
|
---|
2569 | }
|
---|
2570 | currentEdge.push_back(edge[idx]);
|
---|
2571 | currentEdge.push_back(edge[idx+1]);
|
---|
2572 | } else {
|
---|
2573 | throw(AipsError("Wrong length of edge element"));
|
---|
2574 | }
|
---|
2575 |
|
---|
2576 | lineFinder.setData(getSpectrum(whichrow));
|
---|
2577 | lineFinder.findLines(getCompositeChanMask(whichrow, inMask), currentEdge, whichrow);
|
---|
2578 |
|
---|
2579 | return lineFinder.getMask();
|
---|
2580 | }
|
---|
2581 | */
|
---|
2582 |
|
---|
2583 | /* for poly. the variations of outputFittingResult() should be merged into one eventually (2011/3/10 WK) */
|
---|
2584 | void Scantable::outputFittingResult(bool outLogger, bool outTextFile, const std::vector<bool>& chanMask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, ofstream& ofs, const casa::String& funcName, Fitter& fitter) {
|
---|
2585 | if (outLogger || outTextFile) {
|
---|
2586 | std::vector<float> params = fitter.getParameters();
|
---|
2587 | std::vector<bool> fixed = fitter.getFixedParameters();
|
---|
2588 | float rms = getRms(chanMask, whichrow);
|
---|
2589 | String masklist = getMaskRangeList(chanMask, whichrow, coordInfo, hasSameNchan);
|
---|
2590 |
|
---|
2591 | if (outLogger) {
|
---|
2592 | LogIO ols(LogOrigin("Scantable", funcName, WHERE));
|
---|
2593 | ols << formatBaselineParams(params, fixed, rms, masklist, whichrow, false) << LogIO::POST ;
|
---|
2594 | }
|
---|
2595 | if (outTextFile) {
|
---|
2596 | ofs << formatBaselineParams(params, fixed, rms, masklist, whichrow, true) << flush;
|
---|
2597 | }
|
---|
2598 | }
|
---|
2599 | }
|
---|
2600 |
|
---|
2601 | /* for cspline. will be merged once cspline is available in fitter (2011/3/10 WK) */
|
---|
2602 | void Scantable::outputFittingResult(bool outLogger, bool outTextFile, 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) {
|
---|
2603 | if (outLogger || outTextFile) {
|
---|
2604 | float rms = getRms(chanMask, whichrow);
|
---|
2605 | String masklist = getMaskRangeList(chanMask, whichrow, coordInfo, hasSameNchan);
|
---|
2606 | std::vector<bool> fixed;
|
---|
2607 | fixed.clear();
|
---|
2608 |
|
---|
2609 | if (outLogger) {
|
---|
2610 | LogIO ols(LogOrigin("Scantable", funcName, WHERE));
|
---|
2611 | ols << formatPiecewiseBaselineParams(edge, params, fixed, rms, masklist, whichrow, false) << LogIO::POST ;
|
---|
2612 | }
|
---|
2613 | if (outTextFile) {
|
---|
2614 | ofs << formatPiecewiseBaselineParams(edge, params, fixed, rms, masklist, whichrow, true) << flush;
|
---|
2615 | }
|
---|
2616 | }
|
---|
2617 | }
|
---|
2618 |
|
---|
2619 | /* for sinusoid. will be merged once sinusoid is available in fitter (2011/3/10 WK) */
|
---|
2620 | void Scantable::outputFittingResult(bool outLogger, bool outTextFile, const std::vector<bool>& chanMask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, ofstream& ofs, const casa::String& funcName, const std::vector<float>& params) {
|
---|
2621 | if (outLogger || outTextFile) {
|
---|
2622 | float rms = getRms(chanMask, whichrow);
|
---|
2623 | String masklist = getMaskRangeList(chanMask, whichrow, coordInfo, hasSameNchan);
|
---|
2624 | std::vector<bool> fixed;
|
---|
2625 | fixed.clear();
|
---|
2626 |
|
---|
2627 | if (outLogger) {
|
---|
2628 | LogIO ols(LogOrigin("Scantable", funcName, WHERE));
|
---|
2629 | ols << formatBaselineParams(params, fixed, rms, masklist, whichrow, false) << LogIO::POST ;
|
---|
2630 | }
|
---|
2631 | if (outTextFile) {
|
---|
2632 | ofs << formatBaselineParams(params, fixed, rms, masklist, whichrow, true) << flush;
|
---|
2633 | }
|
---|
2634 | }
|
---|
2635 | }
|
---|
2636 |
|
---|
2637 | float Scantable::getRms(const std::vector<bool>& mask, int whichrow) {
|
---|
2638 | Vector<Float> spec;
|
---|
2639 | specCol_.get(whichrow, spec);
|
---|
2640 |
|
---|
2641 | float mean = 0.0;
|
---|
2642 | float smean = 0.0;
|
---|
2643 | int n = 0;
|
---|
2644 | for (uInt i = 0; i < spec.nelements(); ++i) {
|
---|
2645 | if (mask[i]) {
|
---|
2646 | mean += spec[i];
|
---|
2647 | smean += spec[i]*spec[i];
|
---|
2648 | n++;
|
---|
2649 | }
|
---|
2650 | }
|
---|
2651 |
|
---|
2652 | mean /= (float)n;
|
---|
2653 | smean /= (float)n;
|
---|
2654 |
|
---|
2655 | return sqrt(smean - mean*mean);
|
---|
2656 | }
|
---|
2657 |
|
---|
2658 |
|
---|
2659 | std::string Scantable::formatBaselineParamsHeader(int whichrow, const std::string& masklist, bool verbose) const
|
---|
2660 | {
|
---|
2661 | ostringstream oss;
|
---|
2662 |
|
---|
2663 | if (verbose) {
|
---|
2664 | oss << " Scan[" << getScan(whichrow) << "]";
|
---|
2665 | oss << " Beam[" << getBeam(whichrow) << "]";
|
---|
2666 | oss << " IF[" << getIF(whichrow) << "]";
|
---|
2667 | oss << " Pol[" << getPol(whichrow) << "]";
|
---|
2668 | oss << " Cycle[" << getCycle(whichrow) << "]: " << endl;
|
---|
2669 | oss << "Fitter range = " << masklist << endl;
|
---|
2670 | oss << "Baseline parameters" << endl;
|
---|
2671 | oss << flush;
|
---|
2672 | }
|
---|
2673 |
|
---|
2674 | return String(oss);
|
---|
2675 | }
|
---|
2676 |
|
---|
2677 | std::string Scantable::formatBaselineParamsFooter(float rms, bool verbose) const
|
---|
2678 | {
|
---|
2679 | ostringstream oss;
|
---|
2680 |
|
---|
2681 | if (verbose) {
|
---|
2682 | oss << "Results of baseline fit" << endl;
|
---|
2683 | oss << " rms = " << setprecision(6) << rms << endl;
|
---|
2684 | for (int i = 0; i < 60; ++i) {
|
---|
2685 | oss << "-";
|
---|
2686 | }
|
---|
2687 | oss << endl;
|
---|
2688 | oss << flush;
|
---|
2689 | }
|
---|
2690 |
|
---|
2691 | return String(oss);
|
---|
2692 | }
|
---|
2693 |
|
---|
2694 | std::string Scantable::formatBaselineParams(const std::vector<float>& params, const std::vector<bool>& fixed, float rms, const std::string& masklist, int whichrow, bool verbose, int start, int count, bool resetparamid) const
|
---|
2695 | {
|
---|
2696 | int nParam = (int)(params.size());
|
---|
2697 |
|
---|
2698 | if (nParam < 1) {
|
---|
2699 | return(" Not fitted");
|
---|
2700 | } else {
|
---|
2701 |
|
---|
2702 | ostringstream oss;
|
---|
2703 | oss << formatBaselineParamsHeader(whichrow, masklist, verbose);
|
---|
2704 |
|
---|
2705 | if (start < 0) start = 0;
|
---|
2706 | if (count < 0) count = nParam;
|
---|
2707 | int end = start + count;
|
---|
2708 | if (end > nParam) end = nParam;
|
---|
2709 | int paramidoffset = (resetparamid) ? (-start) : 0;
|
---|
2710 |
|
---|
2711 | for (int i = start; i < end; ++i) {
|
---|
2712 | if (i > start) {
|
---|
2713 | oss << ",";
|
---|
2714 | }
|
---|
2715 | std::string sFix = ((fixed.size() > 0) && (fixed[i]) && verbose) ? "(fixed)" : "";
|
---|
2716 | oss << " p" << (i+paramidoffset) << sFix << "= " << right << setw(13) << setprecision(6) << params[i];
|
---|
2717 | }
|
---|
2718 |
|
---|
2719 | oss << endl;
|
---|
2720 | oss << formatBaselineParamsFooter(rms, verbose);
|
---|
2721 |
|
---|
2722 | return String(oss);
|
---|
2723 | }
|
---|
2724 |
|
---|
2725 | }
|
---|
2726 |
|
---|
2727 | std::string Scantable::formatPiecewiseBaselineParams(const std::vector<int>& ranges, const std::vector<float>& params, const std::vector<bool>& fixed, float rms, const std::string& masklist, int whichrow, bool verbose) const
|
---|
2728 | {
|
---|
2729 | int nOutParam = (int)(params.size());
|
---|
2730 | int nPiece = (int)(ranges.size()) - 1;
|
---|
2731 |
|
---|
2732 | if (nOutParam < 1) {
|
---|
2733 | return(" Not fitted");
|
---|
2734 | } else if (nPiece < 0) {
|
---|
2735 | return formatBaselineParams(params, fixed, rms, masklist, whichrow, verbose);
|
---|
2736 | } else if (nPiece < 1) {
|
---|
2737 | return(" Bad count of the piece edge info");
|
---|
2738 | } else if (nOutParam % nPiece != 0) {
|
---|
2739 | return(" Bad count of the output baseline parameters");
|
---|
2740 | } else {
|
---|
2741 |
|
---|
2742 | int nParam = nOutParam / nPiece;
|
---|
2743 |
|
---|
2744 | ostringstream oss;
|
---|
2745 | oss << formatBaselineParamsHeader(whichrow, masklist, verbose);
|
---|
2746 |
|
---|
2747 | stringstream ss;
|
---|
2748 | ss << ranges[nPiece] << flush;
|
---|
2749 | int wRange = ss.str().size() * 2 + 5;
|
---|
2750 |
|
---|
2751 | for (int i = 0; i < nPiece; ++i) {
|
---|
2752 | ss.str("");
|
---|
2753 | ss << " [" << ranges[i] << "," << (ranges[i+1]-1) << "]";
|
---|
2754 | oss << left << setw(wRange) << ss.str();
|
---|
2755 | oss << formatBaselineParams(params, fixed, rms, masklist, whichrow, false, i*nParam, nParam, true);
|
---|
2756 | }
|
---|
2757 |
|
---|
2758 | oss << formatBaselineParamsFooter(rms, verbose);
|
---|
2759 |
|
---|
2760 | return String(oss);
|
---|
2761 | }
|
---|
2762 |
|
---|
2763 | }
|
---|
2764 |
|
---|
2765 | bool Scantable::hasSameNchanOverIFs()
|
---|
2766 | {
|
---|
2767 | int nIF = nif(-1);
|
---|
2768 | int nCh;
|
---|
2769 | int totalPositiveNChan = 0;
|
---|
2770 | int nPositiveNChan = 0;
|
---|
2771 |
|
---|
2772 | for (int i = 0; i < nIF; ++i) {
|
---|
2773 | nCh = nchan(i);
|
---|
2774 | if (nCh > 0) {
|
---|
2775 | totalPositiveNChan += nCh;
|
---|
2776 | nPositiveNChan++;
|
---|
2777 | }
|
---|
2778 | }
|
---|
2779 |
|
---|
2780 | return (totalPositiveNChan == (nPositiveNChan * nchan(0)));
|
---|
2781 | }
|
---|
2782 |
|
---|
2783 | std::string Scantable::getMaskRangeList(const std::vector<bool>& mask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, bool verbose)
|
---|
2784 | {
|
---|
2785 | if (mask.size() < 2) {
|
---|
2786 | throw(AipsError("The mask elements should be > 1"));
|
---|
2787 | }
|
---|
2788 | int IF = getIF(whichrow);
|
---|
2789 | if (mask.size() != (uInt)nchan(IF)) {
|
---|
2790 | throw(AipsError("Number of channels in scantable != number of mask elements"));
|
---|
2791 | }
|
---|
2792 |
|
---|
2793 | if (verbose) {
|
---|
2794 | LogIO logOs(LogOrigin("Scantable", "getMaskRangeList()", WHERE));
|
---|
2795 | logOs << LogIO::WARN << "The current mask window unit is " << coordInfo;
|
---|
2796 | if (!hasSameNchan) {
|
---|
2797 | logOs << endl << "This mask is only valid for IF=" << IF;
|
---|
2798 | }
|
---|
2799 | logOs << LogIO::POST;
|
---|
2800 | }
|
---|
2801 |
|
---|
2802 | std::vector<double> abcissa = getAbcissa(whichrow);
|
---|
2803 | std::vector<int> edge = getMaskEdgeIndices(mask);
|
---|
2804 |
|
---|
2805 | ostringstream oss;
|
---|
2806 | oss.setf(ios::fixed);
|
---|
2807 | oss << setprecision(1) << "[";
|
---|
2808 | for (uInt i = 0; i < edge.size(); i+=2) {
|
---|
2809 | if (i > 0) oss << ",";
|
---|
2810 | oss << "[" << (float)abcissa[edge[i]] << "," << (float)abcissa[edge[i+1]] << "]";
|
---|
2811 | }
|
---|
2812 | oss << "]" << flush;
|
---|
2813 |
|
---|
2814 | return String(oss);
|
---|
2815 | }
|
---|
2816 |
|
---|
2817 | std::vector<int> Scantable::getMaskEdgeIndices(const std::vector<bool>& mask)
|
---|
2818 | {
|
---|
2819 | if (mask.size() < 2) {
|
---|
2820 | throw(AipsError("The mask elements should be > 1"));
|
---|
2821 | }
|
---|
2822 |
|
---|
2823 | std::vector<int> out, startIndices, endIndices;
|
---|
2824 | int maskSize = mask.size();
|
---|
2825 |
|
---|
2826 | startIndices.clear();
|
---|
2827 | endIndices.clear();
|
---|
2828 |
|
---|
2829 | if (mask[0]) {
|
---|
2830 | startIndices.push_back(0);
|
---|
2831 | }
|
---|
2832 | for (int i = 1; i < maskSize; ++i) {
|
---|
2833 | if ((!mask[i-1]) && mask[i]) {
|
---|
2834 | startIndices.push_back(i);
|
---|
2835 | } else if (mask[i-1] && (!mask[i])) {
|
---|
2836 | endIndices.push_back(i-1);
|
---|
2837 | }
|
---|
2838 | }
|
---|
2839 | if (mask[maskSize-1]) {
|
---|
2840 | endIndices.push_back(maskSize-1);
|
---|
2841 | }
|
---|
2842 |
|
---|
2843 | if (startIndices.size() != endIndices.size()) {
|
---|
2844 | throw(AipsError("Inconsistent Mask Size: bad data?"));
|
---|
2845 | }
|
---|
2846 | for (uInt i = 0; i < startIndices.size(); ++i) {
|
---|
2847 | if (startIndices[i] > endIndices[i]) {
|
---|
2848 | throw(AipsError("Mask start index > mask end index"));
|
---|
2849 | }
|
---|
2850 | }
|
---|
2851 |
|
---|
2852 | out.clear();
|
---|
2853 | for (uInt i = 0; i < startIndices.size(); ++i) {
|
---|
2854 | out.push_back(startIndices[i]);
|
---|
2855 | out.push_back(endIndices[i]);
|
---|
2856 | }
|
---|
2857 |
|
---|
2858 | return out;
|
---|
2859 | }
|
---|
2860 |
|
---|
2861 |
|
---|
2862 | /*
|
---|
2863 | STFitEntry Scantable::polyBaseline(const std::vector<bool>& mask, int order, int rowno)
|
---|
2864 | {
|
---|
2865 | Fitter fitter = Fitter();
|
---|
2866 | fitter.setExpression("poly", order);
|
---|
2867 |
|
---|
2868 | std::vector<bool> fmask = getMask(rowno);
|
---|
2869 | if (fmask.size() != mask.size()) {
|
---|
2870 | throw(AipsError("different mask sizes"));
|
---|
2871 | }
|
---|
2872 | for (int i = 0; i < fmask.size(); ++i) {
|
---|
2873 | fmask[i] = fmask[i] && mask[i];
|
---|
2874 | }
|
---|
2875 |
|
---|
2876 | fitBaseline(fmask, rowno, fitter);
|
---|
2877 | setSpectrum(fitter.getResidual(), rowno);
|
---|
2878 | return fitter.getFitEntry();
|
---|
2879 | }
|
---|
2880 | */
|
---|
2881 |
|
---|
2882 | }
|
---|
2883 | //namespace asap
|
---|