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