//#--------------------------------------------------------------------------- //# SDMemTable.cc: A MemoryTable container for single dish integrations //#--------------------------------------------------------------------------- //# Copyright (C) 2004 //# Malte Marquarding, ATNF //# //# This program is free software; you can redistribute it and/or modify it //# under the terms of the GNU General Public License as published by the Free //# Software Foundation; either version 2 of the License, or (at your option) //# any later version. //# //# This program is distributed in the hope that it will be useful, but //# WITHOUT ANY WARRANTY; without even the implied warranty of //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General //# Public License for more details. //# //# You should have received a copy of the GNU General Public License along //# with this program; if not, write to the Free Software Foundation, Inc., //# 675 Massachusetts Ave, Cambridge, MA 02139, USA. //# //# Correspondence concerning this software should be addressed as follows: //# Internet email: Malte.Marquarding@csiro.au //# Postal address: Malte Marquarding, //# Australia Telescope National Facility, //# P.O. Box 76, //# Epping, NSW, 2121, //# AUSTRALIA //# //# $Id: //#--------------------------------------------------------------------------- #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "SDMemTable.h" #include "SDContainer.h" using namespace atnf_sd; SDMemTable::SDMemTable() : IFSel_(0), beamSel_(0), polSel_(0) { setup(); } SDMemTable::SDMemTable(const std::string& name) : IFSel_(0), beamSel_(0), polSel_(0) { name_ = String(name); Table tab(name_); table_ = tab.copyToMemoryTable(name_); } SDMemTable::SDMemTable(const SDMemTable& other, Bool clear) { this->IFSel_= other.IFSel_; this->beamSel_= other.beamSel_; this->polSel_= other.polSel_; this->chanMask_ = other.chanMask_; this->name_ = String("dummy"); this->table_ = other.table_.copyToMemoryTable(String("dummy")); // clear all rows() if (clear) { this->table_.removeRow(this->table_.rowNumbers()); } else { this->IFSel_ = other.IFSel_; this->beamSel_ = other.beamSel_; this->polSel_ = other.polSel_; } } SDMemTable::SDMemTable(const Table& tab, Int scanID) : IFSel_(0), beamSel_(0), polSel_(0) { name_ = String("SDMemTable"); String exprs = String("select * from $1 where SCANID == ") +String::toString(scanID); cerr << exprs << endl; Table t = tableCommand(exprs,tab); table_ = t.copyToMemoryTable(name_); } SDMemTable::~SDMemTable(){ cerr << "goodbye from SDMemTable @ " << this << endl; } SDMemTable SDMemTable::getScan(Int scanID) { return SDMemTable(table_, scanID); } void SDMemTable::setup() { TableDesc td("", "1", TableDesc::Scratch); td.comment() = "A SDMemTable"; td.addColumn(ScalarColumnDesc("TIME")); td.addColumn(ScalarColumnDesc("SRCNAME")); td.addColumn(ArrayColumnDesc("SPECTRA")); td.addColumn(ArrayColumnDesc("FLAGTRA")); td.addColumn(ArrayColumnDesc("TSYS")); td.addColumn(ScalarColumnDesc("SCANID")); td.addColumn(ScalarColumnDesc("INTERVAL")); // Now create a new table from the description. SetupNewTable aNewTab(name_, td, Table::New); table_ = Table(aNewTab, Table::Memory, 0); } std::string SDMemTable::name() const { return name_; } std::string SDMemTable::getSourceName(Int whichRow) const { ROScalarColumn src(table_, "SRCNAME"); String name; src.get(whichRow, name); return name; } Double SDMemTable::getTime(Int whichRow) const { ROScalarColumn src(table_, "TIME"); Double tm; src.get(whichRow, tm); return tm; } bool SDMemTable::setIF(Int whichIF) { //if ( whichIF >= 0 && whichIF < nIF_) { IFSel_ = whichIF; return true; //} //return false; } bool SDMemTable::setBeam(Int whichBeam) { //if ( whichBeam >= 0 && whichBeam < nBeam_) { beamSel_ = whichBeam; return true; //} //return false; } bool SDMemTable::setPol(Int whichPol) { //if ( whichPol >= 0 && whichPol < nPol_) { polSel_ = whichPol; return true; //} //return false; } bool SDMemTable::setMask(const std::vector& whichChans) { ROArrayColumn spec(table_, "FLAGTRA"); std::vector::iterator it; uInt n = spec.shape(0)(3); chanMask_.resize(n,true); for (it = whichChans.begin(); it != whichChans.end(); it++) { if (*it < n) chanMask_[*it] = false; } return true; } std::vector SDMemTable::getMask(Int whichRow) const { std::vector mask; ROArrayColumn spec(table_, "FLAGTRA"); Array arr; spec.get(whichRow, arr); ArrayAccessor > aa0(arr); aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam ArrayAccessor > aa1(aa0); aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF ArrayAccessor > aa2(aa1); aa2.reset(aa2.begin(uInt(polSel_)));// go to pol Bool useUserMask = ( chanMask_.size() == arr.shape()(3) ); std::vector tmp; tmp = chanMask_; // WHY the fxxx do I have to make a copy here std::vector::iterator miter; miter = tmp.begin(); for (ArrayAccessor > i(aa2); i != i.end(); ++i) { bool out =!static_cast(*i); if (useUserMask) { out = out && (*miter); miter++; } mask.push_back(out); } return mask; } std::vector SDMemTable::getSpectrum(Int whichRow) const { std::vector spectrum; ROArrayColumn spec(table_, "SPECTRA"); Array arr; spec.get(whichRow, arr); ArrayAccessor > aa0(arr); aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam ArrayAccessor > aa1(aa0); aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF ArrayAccessor > aa2(aa1); aa2.reset(aa2.begin(uInt(polSel_)));// go to pol for (ArrayAccessor > i(aa2); i != i.end(); ++i) { spectrum.push_back(*i); } return spectrum; } MaskedArray SDMemTable::rowAsMaskedArray(uInt whichRow, Bool useSelection) { ROArrayColumn spec(table_, "SPECTRA"); Array arr; ROArrayColumn flag(table_, "FLAGTRA"); Array farr; spec.get(whichRow, arr); flag.get(whichRow, farr); Array barr(farr.shape());convertArray(barr, farr); MaskedArray marr; if (useSelection) { ArrayAccessor > aa0(arr); aa0.reset(aa0.begin(uInt(beamSel_)));//go to beam ArrayAccessor > aa1(aa0); aa1.reset(aa1.begin(uInt(IFSel_)));// go to IF ArrayAccessor > aa2(aa1); aa2.reset(aa2.begin(uInt(polSel_)));// go to pol ArrayAccessor > baa0(barr); baa0.reset(baa0.begin(uInt(beamSel_)));//go to beam ArrayAccessor > baa1(baa0); baa1.reset(baa1.begin(uInt(IFSel_)));// go to IF ArrayAccessor > baa2(baa1); baa2.reset(baa2.begin(uInt(polSel_)));// go to pol Vector a(arr.shape()(3)); Vector b(barr.shape()(3)); ArrayAccessor > a0(a); ArrayAccessor > b0(b); ArrayAccessor > j(baa2); for (ArrayAccessor > i(aa2); i != i.end(); ++i) { (*a0) = (*i); (*b0) = !(*j); j++; a0++; b0++; } marr.setData(a,b); } else { marr.setData(arr,!barr); } return marr; } Float SDMemTable::getTsys(Int whichRow) const { ROArrayColumn ts(table_, "TSYS"); Array arr; ts.get(whichRow, arr); Float out; IPosition ip(arr.shape()); ip(0) = beamSel_;ip(1) = IFSel_;ip(2) = polSel_;ip(3)=0; out = arr(ip); return out; } bool SDMemTable::putSDContainer(const SDContainer& sdc) { ScalarColumn mjd(table_, "TIME"); ScalarColumn srcn(table_, "SRCNAME"); ArrayColumn spec(table_, "SPECTRA"); ArrayColumn flags(table_, "FLAGTRA"); ArrayColumn ts(table_, "TSYS"); ScalarColumn scan(table_, "SCANID"); ScalarColumn integr(table_, "INTERVAL"); uInt rno = table_.nrow(); table_.addRow(); mjd.put(rno, sdc.timestamp); srcn.put(rno, sdc.sourcename); spec.put(rno, sdc.getSpectrum()); flags.put(rno, sdc.getFlags()); ts.put(rno, sdc.getTsys()); scan.put(rno, sdc.scanid); integr.put(rno, sdc.interval); return true; } bool SDMemTable::putSDHeader(const SDHeader& sdh) { table_.lock(); table_.rwKeywordSet().define("nIF", sdh.nif); table_.rwKeywordSet().define("nBeam", sdh.nbeam); table_.rwKeywordSet().define("nPol", sdh.npol); table_.rwKeywordSet().define("nChan", sdh.nchan); table_.rwKeywordSet().define("Observer", sdh.observer); table_.rwKeywordSet().define("Project", sdh.project); table_.rwKeywordSet().define("Obstype", sdh.obstype); table_.rwKeywordSet().define("AntennaName", sdh.antennaname); table_.rwKeywordSet().define("AntennaPosition", sdh.antennaposition); table_.rwKeywordSet().define("Equinox", sdh.equinox); table_.rwKeywordSet().define("FreqRefFrame", sdh.freqref); table_.rwKeywordSet().define("FreqRefVal", sdh.reffreq); table_.rwKeywordSet().define("Bandwidth", sdh.bandwidth); table_.rwKeywordSet().define("UTC", sdh.utc); table_.unlock(); cerr << "Table Header set" << endl; return true; } void SDMemTable::makePersistent(const std::string& filename) { table_.deepCopy(filename,Table::New); } void SDMemTable::summary() const { ROScalarColumn scans(table_, "SCANID"); ROScalarColumn srcs(table_, "SRCNAME"); cout << "*************** Header ***************" << endl; cout << "nBeam = " << nBeam() << "\t" << "nIF = " << nIF() << endl << "nPol = " << nPol() << "\t" << "nChan = " << nChan() << "\t" << endl; cout << "*************** Header ***************" << endl; uInt count = 0; String name; Int previous = -1;Int current=0; cout << "Scan\tSource" << endl; for (uInt i=0; i< scans.nrow();i++) { scans.getScalar(i,current); if (previous != current) { srcs.getScalar(i,name); previous = current; count++; cout << count << "\t" << name << endl; } } cout << "Table contains " << table_.nrow() << " integration(s)." << endl; cout << "in " << count << " scan(s)." << endl; } Int SDMemTable::nBeam() const { Int n; table_.keywordSet().get("nBeam",n); return n; } Int SDMemTable::nIF() const { Int n; table_.keywordSet().get("nIF",n); return n; } Int SDMemTable::nPol() const { Int n; table_.keywordSet().get("nPol",n); return n; } Int SDMemTable::nChan() const { Int n; table_.keywordSet().get("nChan",n); return n; } /* void SDMemTable::maskChannels(const std::vector& whichChans ) { std::vector::iterator it; ArrayAccessor > j(flags_); for (it = whichChans.begin(); it != whichChans.end(); it++) { j.reset(j.begin(uInt(*it))); for (ArrayAccessor > i(j); i != i.end(); ++i) { for (ArrayAccessor > ii(i); ii != ii.end(); ++ii) { for (ArrayAccessor > iii(ii); iii != iii.end(); ++iii) { (*iii) = } } } } } */