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