[2] | 1 | //#--------------------------------------------------------------------------- |
---|
| 2 | //# SDMemTable.cc: A MemoryTable container for single dish integrations |
---|
| 3 | //#--------------------------------------------------------------------------- |
---|
| 4 | //# Copyright (C) 2004 |
---|
| 5 | //# Malte Marquarding, ATNF |
---|
| 6 | //# |
---|
| 7 | //# This program is free software; you can redistribute it and/or modify it |
---|
| 8 | //# under the terms of the GNU General Public License as published by the Free |
---|
| 9 | //# Software Foundation; either version 2 of the License, or (at your option) |
---|
| 10 | //# any later version. |
---|
| 11 | //# |
---|
| 12 | //# This program is distributed in the hope that it will be useful, but |
---|
| 13 | //# WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
| 15 | //# Public License for more details. |
---|
| 16 | //# |
---|
| 17 | //# You should have received a copy of the GNU General Public License along |
---|
| 18 | //# with this program; if not, write to the Free Software Foundation, Inc., |
---|
| 19 | //# 675 Massachusetts Ave, Cambridge, MA 02139, USA. |
---|
| 20 | //# |
---|
| 21 | //# Correspondence concerning this software should be addressed as follows: |
---|
| 22 | //# Internet email: Malte.Marquarding@csiro.au |
---|
| 23 | //# Postal address: Malte Marquarding, |
---|
| 24 | //# Australia Telescope National Facility, |
---|
| 25 | //# P.O. Box 76, |
---|
| 26 | //# Epping, NSW, 2121, |
---|
| 27 | //# AUSTRALIA |
---|
| 28 | //# |
---|
| 29 | //# $Id: |
---|
| 30 | //#--------------------------------------------------------------------------- |
---|
| 31 | |
---|
[16] | 32 | |
---|
[2] | 33 | #include <aips/iostream.h> |
---|
| 34 | #include <aips/Arrays/Array.h> |
---|
| 35 | #include <aips/Arrays/ArrayMath.h> |
---|
| 36 | #include <aips/Arrays/MaskArrMath.h> |
---|
| 37 | #include <aips/Arrays/ArrayLogical.h> |
---|
| 38 | #include <aips/Arrays/ArrayAccessor.h> |
---|
| 39 | |
---|
| 40 | #include <aips/Tables/TableParse.h> |
---|
| 41 | #include <aips/Tables/TableDesc.h> |
---|
| 42 | #include <aips/Tables/SetupNewTab.h> |
---|
| 43 | #include <aips/Tables/ScaColDesc.h> |
---|
| 44 | #include <aips/Tables/ArrColDesc.h> |
---|
| 45 | |
---|
| 46 | #include <aips/Tables/ExprNode.h> |
---|
| 47 | #include <aips/Tables/ScalarColumn.h> |
---|
| 48 | #include <aips/Tables/ArrayColumn.h> |
---|
[18] | 49 | #include <aips/Tables/TableRecord.h> |
---|
[2] | 50 | |
---|
[18] | 51 | |
---|
[2] | 52 | #include "SDMemTable.h" |
---|
| 53 | #include "SDContainer.h" |
---|
| 54 | |
---|
| 55 | using namespace atnf_sd; |
---|
| 56 | |
---|
[18] | 57 | SDMemTable::SDMemTable() : |
---|
| 58 | IFSel_(0), |
---|
| 59 | beamSel_(0), |
---|
| 60 | polSel_(0) { |
---|
| 61 | setup(); |
---|
| 62 | } |
---|
[2] | 63 | SDMemTable::SDMemTable(const std::string& name) : |
---|
| 64 | IFSel_(0), |
---|
| 65 | beamSel_(0), |
---|
| 66 | polSel_(0) { |
---|
| 67 | name_ = String(name); |
---|
[18] | 68 | Table tab(name_); |
---|
| 69 | table_ = tab.copyToMemoryTable(name_); |
---|
[2] | 70 | } |
---|
| 71 | |
---|
[16] | 72 | SDMemTable::SDMemTable(const SDMemTable& other, Bool clear) { |
---|
[2] | 73 | this->IFSel_= other.IFSel_; |
---|
| 74 | this->beamSel_= other.beamSel_; |
---|
| 75 | this->polSel_= other.polSel_; |
---|
| 76 | this->chanMask_ = other.chanMask_; |
---|
| 77 | this->name_ = String("dummy"); |
---|
| 78 | this->table_ = other.table_.copyToMemoryTable(String("dummy")); |
---|
| 79 | // clear all rows() |
---|
[16] | 80 | if (clear) { |
---|
| 81 | this->table_.removeRow(this->table_.rowNumbers()); |
---|
| 82 | } else { |
---|
| 83 | this->IFSel_ = other.IFSel_; |
---|
| 84 | this->beamSel_ = other.beamSel_; |
---|
| 85 | this->polSel_ = other.polSel_; |
---|
| 86 | } |
---|
[2] | 87 | } |
---|
| 88 | |
---|
| 89 | SDMemTable::SDMemTable(const Table& tab, Int scanID) : |
---|
| 90 | IFSel_(0), |
---|
| 91 | beamSel_(0), |
---|
| 92 | polSel_(0) { |
---|
| 93 | name_ = String("SDMemTable"); |
---|
| 94 | String exprs = String("select * from $1 where SCANID == ") |
---|
| 95 | +String::toString(scanID); |
---|
| 96 | cerr << exprs << endl; |
---|
| 97 | Table t = tableCommand(exprs,tab); |
---|
| 98 | table_ = t.copyToMemoryTable(name_); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | SDMemTable::~SDMemTable(){ |
---|
| 102 | cerr << "goodbye from SDMemTable @ " << this << endl; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | SDMemTable SDMemTable::getScan(Int scanID) { |
---|
| 106 | return SDMemTable(table_, scanID); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | void SDMemTable::setup() { |
---|
| 110 | TableDesc td("", "1", TableDesc::Scratch); |
---|
| 111 | td.comment() = "A SDMemTable"; |
---|
| 112 | td.addColumn(ScalarColumnDesc<Double>("TIME")); |
---|
| 113 | td.addColumn(ScalarColumnDesc<String>("SRCNAME")); |
---|
| 114 | td.addColumn(ArrayColumnDesc<Float>("SPECTRA")); |
---|
| 115 | td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA")); |
---|
| 116 | td.addColumn(ArrayColumnDesc<Float>("TSYS")); |
---|
| 117 | td.addColumn(ScalarColumnDesc<Int>("SCANID")); |
---|
[16] | 118 | td.addColumn(ScalarColumnDesc<Double>("INTERVAL")); |
---|
[2] | 119 | // Now create a new table from the description. |
---|
[18] | 120 | |
---|
[2] | 121 | SetupNewTable aNewTab(name_, td, Table::New); |
---|
| 122 | table_ = Table(aNewTab, Table::Memory, 0); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | std::string SDMemTable::name() const { |
---|
| 126 | return name_; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | std::string SDMemTable::getSourceName(Int whichRow) const { |
---|
| 130 | ROScalarColumn<String> src(table_, "SRCNAME"); |
---|
| 131 | String name; |
---|
| 132 | src.get(whichRow, name); |
---|
| 133 | return name; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | Double SDMemTable::getTime(Int whichRow) const { |
---|
| 137 | ROScalarColumn<Double> src(table_, "TIME"); |
---|
| 138 | Double tm; |
---|
| 139 | src.get(whichRow, tm); |
---|
| 140 | return tm; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | bool SDMemTable::setIF(Int whichIF) { |
---|
| 144 | //if ( whichIF >= 0 && whichIF < nIF_) { |
---|
| 145 | IFSel_ = whichIF; |
---|
| 146 | return true; |
---|
| 147 | //} |
---|
| 148 | //return false; |
---|
| 149 | } |
---|
| 150 | bool SDMemTable::setBeam(Int whichBeam) { |
---|
| 151 | //if ( whichBeam >= 0 && whichBeam < nBeam_) { |
---|
| 152 | beamSel_ = whichBeam; |
---|
| 153 | return true; |
---|
| 154 | //} |
---|
| 155 | //return false; |
---|
| 156 | |
---|
| 157 | } |
---|
| 158 | bool SDMemTable::setPol(Int whichPol) { |
---|
| 159 | //if ( whichPol >= 0 && whichPol < nPol_) { |
---|
| 160 | polSel_ = whichPol; |
---|
| 161 | return true; |
---|
| 162 | //} |
---|
| 163 | //return false; |
---|
| 164 | } |
---|
| 165 | |
---|
[16] | 166 | bool SDMemTable::setMask(const std::vector<int>& whichChans) { |
---|
| 167 | ROArrayColumn<uChar> spec(table_, "FLAGTRA"); |
---|
| 168 | |
---|
| 169 | std::vector<int>::iterator it; |
---|
| 170 | uInt n = spec.shape(0)(3); |
---|
| 171 | chanMask_.resize(n,true); |
---|
| 172 | for (it = whichChans.begin(); it != whichChans.end(); it++) { |
---|
| 173 | if (*it < n) |
---|
| 174 | chanMask_[*it] = false; |
---|
| 175 | } |
---|
[2] | 176 | return true; |
---|
| 177 | } |
---|
| 178 | |
---|
[16] | 179 | std::vector<bool> SDMemTable::getMask(Int whichRow) const { |
---|
| 180 | std::vector<bool> mask; |
---|
| 181 | ROArrayColumn<uChar> spec(table_, "FLAGTRA"); |
---|
| 182 | Array<uChar> arr; |
---|
| 183 | spec.get(whichRow, arr); |
---|
| 184 | ArrayAccessor<uChar, Axis<0> > aa0(arr); |
---|
| 185 | aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam |
---|
| 186 | ArrayAccessor<uChar, Axis<1> > aa1(aa0); |
---|
| 187 | aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF |
---|
| 188 | ArrayAccessor<uChar, Axis<2> > aa2(aa1); |
---|
| 189 | aa2.reset(aa2.begin(uInt(polSel_)));// go to pol |
---|
| 190 | |
---|
| 191 | Bool useUserMask = ( chanMask_.size() == arr.shape()(3) ); |
---|
| 192 | |
---|
| 193 | std::vector<bool> tmp; |
---|
| 194 | tmp = chanMask_; // WHY the fxxx do I have to make a copy here |
---|
| 195 | std::vector<bool>::iterator miter; |
---|
| 196 | miter = tmp.begin(); |
---|
| 197 | |
---|
| 198 | for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) { |
---|
| 199 | bool out =!static_cast<bool>(*i); |
---|
| 200 | if (useUserMask) { |
---|
| 201 | out = out && (*miter); |
---|
| 202 | miter++; |
---|
| 203 | } |
---|
| 204 | mask.push_back(out); |
---|
| 205 | } |
---|
| 206 | return mask; |
---|
[2] | 207 | } |
---|
| 208 | std::vector<float> SDMemTable::getSpectrum(Int whichRow) const { |
---|
| 209 | |
---|
| 210 | std::vector<float> spectrum; |
---|
| 211 | ROArrayColumn<Float> spec(table_, "SPECTRA"); |
---|
| 212 | Array<Float> arr; |
---|
| 213 | spec.get(whichRow, arr); |
---|
| 214 | ArrayAccessor<Float, Axis<0> > aa0(arr); |
---|
| 215 | aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam |
---|
| 216 | ArrayAccessor<Float, Axis<1> > aa1(aa0); |
---|
| 217 | aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF |
---|
[16] | 218 | ArrayAccessor<Float, Axis<2> > aa2(aa1); |
---|
| 219 | aa2.reset(aa2.begin(uInt(polSel_)));// go to pol |
---|
| 220 | for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) { |
---|
| 221 | spectrum.push_back(*i); |
---|
[2] | 222 | } |
---|
| 223 | return spectrum; |
---|
| 224 | } |
---|
| 225 | |
---|
[16] | 226 | MaskedArray<Float> SDMemTable::rowAsMaskedArray(uInt whichRow, |
---|
| 227 | Bool useSelection) { |
---|
[2] | 228 | ROArrayColumn<Float> spec(table_, "SPECTRA"); |
---|
| 229 | Array<Float> arr; |
---|
| 230 | ROArrayColumn<uChar> flag(table_, "FLAGTRA"); |
---|
| 231 | Array<uChar> farr; |
---|
| 232 | spec.get(whichRow, arr); |
---|
| 233 | flag.get(whichRow, farr); |
---|
| 234 | Array<Bool> barr(farr.shape());convertArray(barr, farr); |
---|
| 235 | MaskedArray<Float> marr; |
---|
[16] | 236 | if (useSelection) { |
---|
| 237 | ArrayAccessor<Float, Axis<0> > aa0(arr); |
---|
| 238 | aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam |
---|
| 239 | ArrayAccessor<Float, Axis<1> > aa1(aa0); |
---|
| 240 | aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF |
---|
| 241 | ArrayAccessor<Float, Axis<2> > aa2(aa1); |
---|
| 242 | aa2.reset(aa2.begin(uInt(polSel_)));// go to pol |
---|
| 243 | |
---|
| 244 | ArrayAccessor<Bool, Axis<0> > baa0(barr); |
---|
| 245 | baa0.reset(baa0.begin(uInt(beamSel_)));//go to beam |
---|
| 246 | ArrayAccessor<Bool, Axis<1> > baa1(baa0); |
---|
| 247 | baa1.reset(baa1.begin(uInt(IFSel_)));// go to IF |
---|
| 248 | ArrayAccessor<Bool, Axis<2> > baa2(baa1); |
---|
| 249 | baa2.reset(baa2.begin(uInt(polSel_)));// go to pol |
---|
| 250 | |
---|
| 251 | Vector<Float> a(arr.shape()(3)); |
---|
| 252 | Vector<Bool> b(barr.shape()(3)); |
---|
| 253 | ArrayAccessor<Float, Axis<0> > a0(a); |
---|
| 254 | ArrayAccessor<Bool, Axis<0> > b0(b); |
---|
| 255 | |
---|
| 256 | ArrayAccessor<Bool, Axis<3> > j(baa2); |
---|
| 257 | for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) { |
---|
| 258 | (*a0) = (*i); |
---|
| 259 | (*b0) = !(*j); |
---|
| 260 | j++; |
---|
| 261 | a0++; |
---|
| 262 | b0++; |
---|
| 263 | } |
---|
| 264 | marr.setData(a,b); |
---|
| 265 | } else { |
---|
| 266 | marr.setData(arr,!barr); |
---|
| 267 | } |
---|
[2] | 268 | return marr; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | Float SDMemTable::getTsys(Int whichRow) const { |
---|
| 272 | ROArrayColumn<Float> ts(table_, "TSYS"); |
---|
| 273 | Array<Float> arr; |
---|
| 274 | ts.get(whichRow, arr); |
---|
| 275 | Float out; |
---|
| 276 | IPosition ip(arr.shape()); |
---|
[16] | 277 | ip(0) = beamSel_;ip(1) = IFSel_;ip(2) = polSel_;ip(3)=0; |
---|
[2] | 278 | out = arr(ip); |
---|
| 279 | return out; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | bool SDMemTable::putSDContainer(const SDContainer& sdc) { |
---|
| 283 | ScalarColumn<Double> mjd(table_, "TIME"); |
---|
| 284 | ScalarColumn<String> srcn(table_, "SRCNAME"); |
---|
| 285 | ArrayColumn<Float> spec(table_, "SPECTRA"); |
---|
| 286 | ArrayColumn<uChar> flags(table_, "FLAGTRA"); |
---|
| 287 | ArrayColumn<Float> ts(table_, "TSYS"); |
---|
| 288 | ScalarColumn<Int> scan(table_, "SCANID"); |
---|
[16] | 289 | ScalarColumn<Double> integr(table_, "INTERVAL"); |
---|
[2] | 290 | |
---|
| 291 | uInt rno = table_.nrow(); |
---|
| 292 | table_.addRow(); |
---|
| 293 | |
---|
| 294 | mjd.put(rno, sdc.timestamp); |
---|
| 295 | srcn.put(rno, sdc.sourcename); |
---|
| 296 | spec.put(rno, sdc.getSpectrum()); |
---|
| 297 | flags.put(rno, sdc.getFlags()); |
---|
| 298 | ts.put(rno, sdc.getTsys()); |
---|
| 299 | scan.put(rno, sdc.scanid); |
---|
[16] | 300 | integr.put(rno, sdc.interval); |
---|
| 301 | |
---|
[2] | 302 | return true; |
---|
| 303 | } |
---|
[18] | 304 | |
---|
| 305 | bool SDMemTable::putSDHeader(const SDHeader& sdh) { |
---|
| 306 | table_.lock(); |
---|
| 307 | table_.rwKeywordSet().define("nIF", sdh.nif); |
---|
| 308 | table_.rwKeywordSet().define("nBeam", sdh.nbeam); |
---|
| 309 | table_.rwKeywordSet().define("nPol", sdh.npol); |
---|
| 310 | table_.rwKeywordSet().define("nChan", sdh.nchan); |
---|
| 311 | table_.rwKeywordSet().define("Observer", sdh.observer); |
---|
| 312 | table_.rwKeywordSet().define("Project", sdh.project); |
---|
| 313 | table_.rwKeywordSet().define("Obstype", sdh.obstype); |
---|
| 314 | table_.rwKeywordSet().define("AntennaName", sdh.antennaname); |
---|
| 315 | table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition); |
---|
| 316 | table_.rwKeywordSet().define("Equinox", sdh.equinox); |
---|
| 317 | table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref); |
---|
| 318 | table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq); |
---|
| 319 | table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth); |
---|
| 320 | table_.rwKeywordSet().define("UTC", sdh.utc); |
---|
| 321 | table_.unlock(); |
---|
| 322 | cerr << "Table Header set" << endl; |
---|
| 323 | return true; |
---|
| 324 | } |
---|
| 325 | |
---|
[2] | 326 | void SDMemTable::makePersistent(const std::string& filename) { |
---|
| 327 | table_.deepCopy(filename,Table::New); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | void SDMemTable::summary() const { |
---|
| 331 | ROScalarColumn<Int> scans(table_, "SCANID"); |
---|
| 332 | ROScalarColumn<String> srcs(table_, "SRCNAME"); |
---|
[18] | 333 | cout << "*************** Header ***************" << endl; |
---|
| 334 | cout << "nBeam = " << nBeam() << "\t" |
---|
| 335 | << "nIF = " << nIF() << endl |
---|
| 336 | << "nPol = " << nPol() << "\t" |
---|
| 337 | << "nChan = " << nChan() << "\t" << endl; |
---|
| 338 | cout << "*************** Header ***************" << endl; |
---|
[2] | 339 | uInt count = 0; |
---|
| 340 | String name; |
---|
| 341 | Int previous = -1;Int current=0; |
---|
| 342 | cout << "Scan\tSource" << endl; |
---|
| 343 | for (uInt i=0; i< scans.nrow();i++) { |
---|
| 344 | scans.getScalar(i,current); |
---|
| 345 | if (previous != current) { |
---|
| 346 | srcs.getScalar(i,name); |
---|
| 347 | previous = current; |
---|
| 348 | count++; |
---|
[16] | 349 | cout << count << "\t" |
---|
| 350 | << name |
---|
| 351 | << endl; |
---|
[2] | 352 | } |
---|
| 353 | } |
---|
[18] | 354 | cout << "Table contains " << table_.nrow() << " integration(s)." << endl; |
---|
| 355 | cout << "in " << count << " scan(s)." << endl; |
---|
[2] | 356 | } |
---|
[18] | 357 | |
---|
| 358 | Int SDMemTable::nBeam() const { |
---|
| 359 | Int n; |
---|
| 360 | table_.keywordSet().get("nBeam",n); |
---|
| 361 | return n; |
---|
| 362 | } |
---|
| 363 | Int SDMemTable::nIF() const { |
---|
| 364 | Int n; |
---|
| 365 | table_.keywordSet().get("nIF",n); |
---|
| 366 | return n; |
---|
| 367 | } |
---|
| 368 | Int SDMemTable::nPol() const { |
---|
| 369 | Int n; |
---|
| 370 | table_.keywordSet().get("nPol",n); |
---|
| 371 | return n; |
---|
| 372 | } |
---|
| 373 | Int SDMemTable::nChan() const { |
---|
| 374 | Int n; |
---|
| 375 | table_.keywordSet().get("nChan",n); |
---|
| 376 | return n; |
---|
| 377 | } |
---|
| 378 | |
---|
[16] | 379 | /* |
---|
[18] | 380 | void SDMemTable::maskChannels(const std::vector<Int>& whichChans ) { |
---|
[16] | 381 | |
---|
| 382 | std::vector<int>::iterator it; |
---|
| 383 | ArrayAccessor<uChar, Axis<2> > j(flags_); |
---|
| 384 | for (it = whichChans.begin(); it != whichChans.end(); it++) { |
---|
| 385 | j.reset(j.begin(uInt(*it))); |
---|
| 386 | for (ArrayAccessor<uChar, Axis<0> > i(j); i != i.end(); ++i) { |
---|
| 387 | for (ArrayAccessor<uChar, Axis<1> > ii(i); ii != ii.end(); ++ii) { |
---|
| 388 | for (ArrayAccessor<uChar, Axis<3> > iii(ii); |
---|
| 389 | iii != iii.end(); ++iii) { |
---|
| 390 | (*iii) = |
---|
| 391 | } |
---|
| 392 | } |
---|
| 393 | } |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | } |
---|
| 397 | */ |
---|