| [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> | 
|---|
|  | 49 |  | 
|---|
|  | 50 | #include "SDMemTable.h" | 
|---|
|  | 51 | #include "SDContainer.h" | 
|---|
|  | 52 |  | 
|---|
|  | 53 | using namespace atnf_sd; | 
|---|
|  | 54 |  | 
|---|
|  | 55 | SDMemTable::SDMemTable(const std::string& name) : | 
|---|
|  | 56 | IFSel_(0), | 
|---|
|  | 57 | beamSel_(0), | 
|---|
|  | 58 | polSel_(0) { | 
|---|
|  | 59 | name_ = String(name); | 
|---|
|  | 60 | setup(); | 
|---|
|  | 61 | } | 
|---|
|  | 62 |  | 
|---|
| [16] | 63 | SDMemTable::SDMemTable(const SDMemTable& other, Bool clear) { | 
|---|
| [2] | 64 | this->IFSel_= other.IFSel_; | 
|---|
|  | 65 | this->beamSel_= other.beamSel_; | 
|---|
|  | 66 | this->polSel_= other.polSel_; | 
|---|
|  | 67 | this->chanMask_ = other.chanMask_; | 
|---|
|  | 68 | this->name_ = String("dummy"); | 
|---|
|  | 69 | this->table_ = other.table_.copyToMemoryTable(String("dummy")); | 
|---|
|  | 70 | // clear all rows() | 
|---|
| [16] | 71 | if (clear) { | 
|---|
|  | 72 | this->table_.removeRow(this->table_.rowNumbers()); | 
|---|
|  | 73 | } else { | 
|---|
|  | 74 | this->IFSel_ = other.IFSel_; | 
|---|
|  | 75 | this->beamSel_ = other.beamSel_; | 
|---|
|  | 76 | this->polSel_ = other.polSel_; | 
|---|
|  | 77 | } | 
|---|
| [2] | 78 | } | 
|---|
|  | 79 |  | 
|---|
|  | 80 | SDMemTable::SDMemTable(const Table& tab, Int scanID) : | 
|---|
|  | 81 | IFSel_(0), | 
|---|
|  | 82 | beamSel_(0), | 
|---|
|  | 83 | polSel_(0) { | 
|---|
|  | 84 | name_ = String("SDMemTable"); | 
|---|
|  | 85 | String exprs = String("select * from $1 where SCANID == ") | 
|---|
|  | 86 | +String::toString(scanID); | 
|---|
|  | 87 | cerr << exprs << endl; | 
|---|
|  | 88 | Table t = tableCommand(exprs,tab); | 
|---|
|  | 89 | table_ = t.copyToMemoryTable(name_); | 
|---|
|  | 90 | } | 
|---|
|  | 91 |  | 
|---|
|  | 92 | SDMemTable::~SDMemTable(){ | 
|---|
|  | 93 | cerr << "goodbye from SDMemTable @ " << this << endl; | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
|  | 96 | SDMemTable SDMemTable::getScan(Int scanID) { | 
|---|
|  | 97 | return SDMemTable(table_, scanID); | 
|---|
|  | 98 | } | 
|---|
|  | 99 |  | 
|---|
|  | 100 | void SDMemTable::setup() { | 
|---|
|  | 101 | TableDesc td("", "1", TableDesc::Scratch); | 
|---|
|  | 102 | td.comment() = "A SDMemTable"; | 
|---|
|  | 103 | //td.rwKeywordSet().define("VERSION",Float(0.1)); | 
|---|
|  | 104 | td.addColumn(ScalarColumnDesc<Double>("TIME")); | 
|---|
|  | 105 | td.addColumn(ScalarColumnDesc<String>("SRCNAME")); | 
|---|
|  | 106 | td.addColumn(ArrayColumnDesc<Float>("SPECTRA")); | 
|---|
|  | 107 | td.addColumn(ArrayColumnDesc<uChar>("FLAGTRA")); | 
|---|
|  | 108 | td.addColumn(ArrayColumnDesc<Float>("TSYS")); | 
|---|
|  | 109 | td.addColumn(ScalarColumnDesc<Int>("SCANID")); | 
|---|
| [16] | 110 | td.addColumn(ScalarColumnDesc<Double>("INTERVAL")); | 
|---|
| [2] | 111 | // Now create a new table from the description. | 
|---|
|  | 112 | SetupNewTable aNewTab(name_, td, Table::New); | 
|---|
|  | 113 | table_ = Table(aNewTab, Table::Memory, 0); | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 | std::string SDMemTable::name() const { | 
|---|
|  | 117 | return name_; | 
|---|
|  | 118 | } | 
|---|
|  | 119 |  | 
|---|
|  | 120 | std::string SDMemTable::getSourceName(Int whichRow) const { | 
|---|
|  | 121 | ROScalarColumn<String> src(table_, "SRCNAME"); | 
|---|
|  | 122 | String name; | 
|---|
|  | 123 | src.get(whichRow, name); | 
|---|
|  | 124 | return name; | 
|---|
|  | 125 | } | 
|---|
|  | 126 |  | 
|---|
|  | 127 | Double SDMemTable::getTime(Int whichRow) const { | 
|---|
|  | 128 | ROScalarColumn<Double> src(table_, "TIME"); | 
|---|
|  | 129 | Double tm; | 
|---|
|  | 130 | src.get(whichRow, tm); | 
|---|
|  | 131 | return tm; | 
|---|
|  | 132 | } | 
|---|
|  | 133 |  | 
|---|
|  | 134 | bool SDMemTable::setIF(Int whichIF) { | 
|---|
|  | 135 | //if ( whichIF >= 0 && whichIF < nIF_) { | 
|---|
|  | 136 | IFSel_ = whichIF; | 
|---|
|  | 137 | return true; | 
|---|
|  | 138 | //} | 
|---|
|  | 139 | //return false; | 
|---|
|  | 140 | } | 
|---|
|  | 141 | bool SDMemTable::setBeam(Int whichBeam) { | 
|---|
|  | 142 | //if ( whichBeam >= 0 && whichBeam < nBeam_) { | 
|---|
|  | 143 | beamSel_ = whichBeam; | 
|---|
|  | 144 | return true; | 
|---|
|  | 145 | //} | 
|---|
|  | 146 | //return false; | 
|---|
|  | 147 |  | 
|---|
|  | 148 | } | 
|---|
|  | 149 | bool SDMemTable::setPol(Int whichPol) { | 
|---|
|  | 150 | //if ( whichPol >= 0 && whichPol < nPol_) { | 
|---|
|  | 151 | polSel_ = whichPol; | 
|---|
|  | 152 | return true; | 
|---|
|  | 153 | //} | 
|---|
|  | 154 | //return false; | 
|---|
|  | 155 | } | 
|---|
|  | 156 |  | 
|---|
| [16] | 157 | bool SDMemTable::setMask(const std::vector<int>& whichChans) { | 
|---|
|  | 158 | ROArrayColumn<uChar> spec(table_, "FLAGTRA"); | 
|---|
|  | 159 |  | 
|---|
|  | 160 | std::vector<int>::iterator it; | 
|---|
|  | 161 | uInt n = spec.shape(0)(3); | 
|---|
|  | 162 | chanMask_.resize(n,true); | 
|---|
|  | 163 | for (it = whichChans.begin(); it != whichChans.end(); it++) { | 
|---|
|  | 164 | if (*it < n) | 
|---|
|  | 165 | chanMask_[*it] = false; | 
|---|
|  | 166 | } | 
|---|
| [2] | 167 | return true; | 
|---|
|  | 168 | } | 
|---|
|  | 169 |  | 
|---|
| [16] | 170 | std::vector<bool> SDMemTable::getMask(Int whichRow) const { | 
|---|
|  | 171 | std::vector<bool> mask; | 
|---|
|  | 172 | ROArrayColumn<uChar> spec(table_, "FLAGTRA"); | 
|---|
|  | 173 | Array<uChar> arr; | 
|---|
|  | 174 | spec.get(whichRow, arr); | 
|---|
|  | 175 | ArrayAccessor<uChar, Axis<0> > aa0(arr); | 
|---|
|  | 176 | aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam | 
|---|
|  | 177 | ArrayAccessor<uChar, Axis<1> > aa1(aa0); | 
|---|
|  | 178 | aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF | 
|---|
|  | 179 | ArrayAccessor<uChar, Axis<2> > aa2(aa1); | 
|---|
|  | 180 | aa2.reset(aa2.begin(uInt(polSel_)));// go to pol | 
|---|
|  | 181 |  | 
|---|
|  | 182 | Bool useUserMask = ( chanMask_.size() == arr.shape()(3) ); | 
|---|
|  | 183 |  | 
|---|
|  | 184 | std::vector<bool> tmp; | 
|---|
|  | 185 | tmp = chanMask_; // WHY the fxxx do I have to make a copy here | 
|---|
|  | 186 | std::vector<bool>::iterator miter; | 
|---|
|  | 187 | miter = tmp.begin(); | 
|---|
|  | 188 |  | 
|---|
|  | 189 | for (ArrayAccessor<uChar, Axis<3> > i(aa2); i != i.end(); ++i) { | 
|---|
|  | 190 | bool out =!static_cast<bool>(*i); | 
|---|
|  | 191 | if (useUserMask) { | 
|---|
|  | 192 | out = out && (*miter); | 
|---|
|  | 193 | miter++; | 
|---|
|  | 194 | } | 
|---|
|  | 195 | mask.push_back(out); | 
|---|
|  | 196 | } | 
|---|
|  | 197 | return mask; | 
|---|
| [2] | 198 | } | 
|---|
|  | 199 | std::vector<float> SDMemTable::getSpectrum(Int whichRow) const { | 
|---|
|  | 200 |  | 
|---|
|  | 201 | std::vector<float> spectrum; | 
|---|
|  | 202 | ROArrayColumn<Float> spec(table_, "SPECTRA"); | 
|---|
|  | 203 | Array<Float> arr; | 
|---|
|  | 204 | spec.get(whichRow, arr); | 
|---|
|  | 205 | ArrayAccessor<Float, Axis<0> > aa0(arr); | 
|---|
|  | 206 | aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam | 
|---|
|  | 207 | ArrayAccessor<Float, Axis<1> > aa1(aa0); | 
|---|
|  | 208 | aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF | 
|---|
| [16] | 209 | ArrayAccessor<Float, Axis<2> > aa2(aa1); | 
|---|
|  | 210 | aa2.reset(aa2.begin(uInt(polSel_)));// go to pol | 
|---|
|  | 211 | for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) { | 
|---|
|  | 212 | spectrum.push_back(*i); | 
|---|
| [2] | 213 | } | 
|---|
|  | 214 | return spectrum; | 
|---|
|  | 215 | } | 
|---|
|  | 216 |  | 
|---|
| [16] | 217 | MaskedArray<Float> SDMemTable::rowAsMaskedArray(uInt whichRow, | 
|---|
|  | 218 | Bool useSelection) { | 
|---|
| [2] | 219 | ROArrayColumn<Float> spec(table_, "SPECTRA"); | 
|---|
|  | 220 | Array<Float> arr; | 
|---|
|  | 221 | ROArrayColumn<uChar> flag(table_, "FLAGTRA"); | 
|---|
|  | 222 | Array<uChar> farr; | 
|---|
|  | 223 | spec.get(whichRow, arr); | 
|---|
|  | 224 | flag.get(whichRow, farr); | 
|---|
|  | 225 | Array<Bool> barr(farr.shape());convertArray(barr, farr); | 
|---|
|  | 226 | MaskedArray<Float> marr; | 
|---|
| [16] | 227 | if (useSelection) { | 
|---|
|  | 228 | ArrayAccessor<Float, Axis<0> > aa0(arr); | 
|---|
|  | 229 | aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam | 
|---|
|  | 230 | ArrayAccessor<Float, Axis<1> > aa1(aa0); | 
|---|
|  | 231 | aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF | 
|---|
|  | 232 | ArrayAccessor<Float, Axis<2> > aa2(aa1); | 
|---|
|  | 233 | aa2.reset(aa2.begin(uInt(polSel_)));// go to pol | 
|---|
|  | 234 |  | 
|---|
|  | 235 | ArrayAccessor<Bool, Axis<0> > baa0(barr); | 
|---|
|  | 236 | baa0.reset(baa0.begin(uInt(beamSel_)));//go to beam | 
|---|
|  | 237 | ArrayAccessor<Bool, Axis<1> > baa1(baa0); | 
|---|
|  | 238 | baa1.reset(baa1.begin(uInt(IFSel_)));// go to IF | 
|---|
|  | 239 | ArrayAccessor<Bool, Axis<2> > baa2(baa1); | 
|---|
|  | 240 | baa2.reset(baa2.begin(uInt(polSel_)));// go to pol | 
|---|
|  | 241 |  | 
|---|
|  | 242 | Vector<Float> a(arr.shape()(3)); | 
|---|
|  | 243 | Vector<Bool> b(barr.shape()(3)); | 
|---|
|  | 244 | ArrayAccessor<Float, Axis<0> > a0(a); | 
|---|
|  | 245 | ArrayAccessor<Bool, Axis<0> > b0(b); | 
|---|
|  | 246 |  | 
|---|
|  | 247 | ArrayAccessor<Bool, Axis<3> > j(baa2); | 
|---|
|  | 248 | for (ArrayAccessor<Float, Axis<3> > i(aa2); i != i.end(); ++i) { | 
|---|
|  | 249 | (*a0) = (*i); | 
|---|
|  | 250 | (*b0) = !(*j); | 
|---|
|  | 251 | j++; | 
|---|
|  | 252 | a0++; | 
|---|
|  | 253 | b0++; | 
|---|
|  | 254 | } | 
|---|
|  | 255 | marr.setData(a,b); | 
|---|
|  | 256 | } else { | 
|---|
|  | 257 | marr.setData(arr,!barr); | 
|---|
|  | 258 | } | 
|---|
| [2] | 259 | return marr; | 
|---|
|  | 260 | } | 
|---|
|  | 261 |  | 
|---|
|  | 262 | Float SDMemTable::getTsys(Int whichRow) const { | 
|---|
|  | 263 | ROArrayColumn<Float> ts(table_, "TSYS"); | 
|---|
|  | 264 | Array<Float> arr; | 
|---|
|  | 265 | ts.get(whichRow, arr); | 
|---|
|  | 266 | Float out; | 
|---|
|  | 267 | IPosition ip(arr.shape()); | 
|---|
| [16] | 268 | ip(0) = beamSel_;ip(1) = IFSel_;ip(2) = polSel_;ip(3)=0; | 
|---|
| [2] | 269 | out = arr(ip); | 
|---|
|  | 270 | return out; | 
|---|
|  | 271 | } | 
|---|
|  | 272 |  | 
|---|
|  | 273 | bool SDMemTable::putSDContainer(const SDContainer& sdc) { | 
|---|
|  | 274 | ScalarColumn<Double> mjd(table_, "TIME"); | 
|---|
|  | 275 | ScalarColumn<String> srcn(table_, "SRCNAME"); | 
|---|
|  | 276 | ArrayColumn<Float> spec(table_, "SPECTRA"); | 
|---|
|  | 277 | ArrayColumn<uChar> flags(table_, "FLAGTRA"); | 
|---|
|  | 278 | ArrayColumn<Float> ts(table_, "TSYS"); | 
|---|
|  | 279 | ScalarColumn<Int> scan(table_, "SCANID"); | 
|---|
| [16] | 280 | ScalarColumn<Double> integr(table_, "INTERVAL"); | 
|---|
| [2] | 281 |  | 
|---|
|  | 282 | uInt rno = table_.nrow(); | 
|---|
|  | 283 | table_.addRow(); | 
|---|
|  | 284 |  | 
|---|
|  | 285 | mjd.put(rno, sdc.timestamp); | 
|---|
|  | 286 | srcn.put(rno, sdc.sourcename); | 
|---|
|  | 287 | spec.put(rno, sdc.getSpectrum()); | 
|---|
|  | 288 | flags.put(rno, sdc.getFlags()); | 
|---|
|  | 289 | ts.put(rno, sdc.getTsys()); | 
|---|
|  | 290 | scan.put(rno, sdc.scanid); | 
|---|
| [16] | 291 | integr.put(rno, sdc.interval); | 
|---|
|  | 292 |  | 
|---|
| [2] | 293 | return true; | 
|---|
|  | 294 | } | 
|---|
|  | 295 | void SDMemTable::makePersistent(const std::string& filename) { | 
|---|
|  | 296 | table_.deepCopy(filename,Table::New); | 
|---|
|  | 297 | } | 
|---|
|  | 298 |  | 
|---|
|  | 299 | void SDMemTable::summary() const { | 
|---|
|  | 300 | cerr << "SDMemTable::summary()" << endl; | 
|---|
|  | 301 | ROScalarColumn<Int> scans(table_, "SCANID"); | 
|---|
|  | 302 | ROScalarColumn<String> srcs(table_, "SRCNAME"); | 
|---|
|  | 303 | uInt count = 0; | 
|---|
|  | 304 | String name; | 
|---|
|  | 305 | Int previous = -1;Int current=0; | 
|---|
|  | 306 | cout << "Scan\tSource" << endl; | 
|---|
|  | 307 | for (uInt i=0; i< scans.nrow();i++) { | 
|---|
|  | 308 | scans.getScalar(i,current); | 
|---|
|  | 309 | if (previous != current) { | 
|---|
|  | 310 | srcs.getScalar(i,name); | 
|---|
|  | 311 | previous = current; | 
|---|
|  | 312 | count++; | 
|---|
| [16] | 313 | cout << count << "\t" | 
|---|
|  | 314 | << name | 
|---|
|  | 315 | << endl; | 
|---|
| [2] | 316 | } | 
|---|
|  | 317 | } | 
|---|
|  | 318 | cout << "Table contains " << table_.nrow() << "integrations." << endl; | 
|---|
|  | 319 | cout << "in " << count << "scans." << endl; | 
|---|
|  | 320 | } | 
|---|
| [16] | 321 | /* | 
|---|
|  | 322 | void maskChannels(const std::vector<Int>& whichChans ) { | 
|---|
|  | 323 |  | 
|---|
|  | 324 | std::vector<int>::iterator it; | 
|---|
|  | 325 | ArrayAccessor<uChar, Axis<2> > j(flags_); | 
|---|
|  | 326 | for (it = whichChans.begin(); it != whichChans.end(); it++) { | 
|---|
|  | 327 | j.reset(j.begin(uInt(*it))); | 
|---|
|  | 328 | for (ArrayAccessor<uChar, Axis<0> > i(j); i != i.end(); ++i) { | 
|---|
|  | 329 | for (ArrayAccessor<uChar, Axis<1> > ii(i); ii != ii.end(); ++ii) { | 
|---|
|  | 330 | for (ArrayAccessor<uChar, Axis<3> > iii(ii); | 
|---|
|  | 331 | iii != iii.end(); ++iii) { | 
|---|
|  | 332 | (*iii) = | 
|---|
|  | 333 | } | 
|---|
|  | 334 | } | 
|---|
|  | 335 | } | 
|---|
|  | 336 | } | 
|---|
|  | 337 |  | 
|---|
|  | 338 | } | 
|---|
|  | 339 | */ | 
|---|