[2] | 1 | //#--------------------------------------------------------------------------- |
---|
| 2 | //# SDReader.cc: A class to read single dish spectra from SDFITS, RPFITS |
---|
| 3 | //#--------------------------------------------------------------------------- |
---|
| 4 | //# Copyright (C) 2004 |
---|
[125] | 5 | //# ATNF |
---|
[2] | 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 | //#--------------------------------------------------------------------------- |
---|
[404] | 31 | #include <casa/iostream.h> |
---|
| 32 | #include <casa/iomanip.h> |
---|
| 33 | |
---|
[125] | 34 | #include <casa/Exceptions.h> |
---|
[284] | 35 | #include <casa/OS/Path.h> |
---|
[290] | 36 | #include <casa/OS/File.h> |
---|
[338] | 37 | #include <casa/Quanta/Unit.h> |
---|
[2] | 38 | #include <atnf/PKSIO/PKSreader.h> |
---|
[125] | 39 | |
---|
[2] | 40 | #include "SDReader.h" |
---|
[337] | 41 | #include "SDDefs.h" |
---|
[2] | 42 | |
---|
[125] | 43 | using namespace casa; |
---|
[83] | 44 | using namespace asap; |
---|
[2] | 45 | |
---|
| 46 | SDReader::SDReader() : |
---|
| 47 | reader_(0), |
---|
[405] | 48 | header_(0), |
---|
| 49 | frequencies_(0), |
---|
[2] | 50 | table_(new SDMemTable()) { |
---|
| 51 | cursor_ = 0; |
---|
| 52 | } |
---|
[342] | 53 | SDReader::SDReader(const std::string& filename, |
---|
[338] | 54 | int whichIF, int whichBeam) : |
---|
[46] | 55 | reader_(0), |
---|
[405] | 56 | header_(0), |
---|
| 57 | frequencies_(0), |
---|
[46] | 58 | table_(new SDMemTable()) { |
---|
| 59 | cursor_ = 0; |
---|
[342] | 60 | open(filename, whichIF, whichBeam); |
---|
[46] | 61 | } |
---|
[2] | 62 | |
---|
| 63 | SDReader::SDReader(CountedPtr<SDMemTable> tbl) : |
---|
| 64 | reader_(0), |
---|
[405] | 65 | header_(0), |
---|
[2] | 66 | table_(tbl) { |
---|
| 67 | cursor_ = 0; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | SDReader::~SDReader() { |
---|
[405] | 71 | if (reader_) delete reader_; |
---|
| 72 | if (header_) delete header_; |
---|
| 73 | if (frequencies_) delete frequencies_; |
---|
[2] | 74 | } |
---|
| 75 | |
---|
| 76 | void SDReader::reset() { |
---|
[46] | 77 | cursor_ = 0; |
---|
[87] | 78 | table_ = new SDMemTable(); |
---|
[405] | 79 | open(filename_,ifOffset_, beamOffset_); |
---|
[2] | 80 | } |
---|
| 81 | |
---|
[332] | 82 | |
---|
[46] | 83 | void SDReader::close() { |
---|
| 84 | cerr << "disabled" << endl; |
---|
| 85 | } |
---|
| 86 | |
---|
[342] | 87 | void SDReader::open(const std::string& filename, |
---|
[338] | 88 | int whichIF, int whichBeam) { |
---|
[2] | 89 | if (reader_) delete reader_; reader_ = 0; |
---|
| 90 | Bool haveBase, haveSpectra, haveXPol; |
---|
[405] | 91 | |
---|
[2] | 92 | String inName(filename); |
---|
[284] | 93 | Path path(inName); |
---|
| 94 | inName = path.expandedName(); |
---|
[405] | 95 | |
---|
[290] | 96 | File file(inName); |
---|
| 97 | if (!file.exists()) { |
---|
| 98 | throw(AipsError("File does not exist")); |
---|
| 99 | } |
---|
[87] | 100 | filename_ = inName; |
---|
[332] | 101 | |
---|
[405] | 102 | // Create reader and fill in values for arguments |
---|
[2] | 103 | String format; |
---|
| 104 | Vector<Bool> beams; |
---|
| 105 | if ((reader_ = getPKSreader(inName, 0, False, format, beams, nIF_, |
---|
| 106 | nChan_, nPol_, haveBase, haveSpectra, |
---|
| 107 | haveXPol)) == 0) { |
---|
[405] | 108 | throw(AipsError("Creation of PKSreader failed")); |
---|
[2] | 109 | } |
---|
| 110 | if (!haveSpectra) { |
---|
| 111 | delete reader_; |
---|
| 112 | reader_ = 0; |
---|
[87] | 113 | throw(AipsError("No spectral data in file.")); |
---|
[2] | 114 | return; |
---|
| 115 | } |
---|
[405] | 116 | |
---|
[2] | 117 | nBeam_ = beams.nelements(); |
---|
| 118 | // Get basic parameters. |
---|
[110] | 119 | if (haveXPol) { |
---|
| 120 | cout << "Warning. Ignoring cross polarisation." << endl; |
---|
| 121 | nPol_--; |
---|
| 122 | } |
---|
[405] | 123 | if (header_) delete header_; |
---|
| 124 | header_ = new SDHeader(); |
---|
| 125 | header_->nchan = nChan_; |
---|
| 126 | header_->npol = nPol_; |
---|
| 127 | header_->nbeam = nBeam_; |
---|
| 128 | |
---|
| 129 | Int status = reader_->getHeader(header_->observer, header_->project, |
---|
| 130 | header_->antennaname, header_->antennaposition, |
---|
| 131 | header_->obstype,header_->equinox, |
---|
| 132 | header_->freqref, |
---|
| 133 | header_->utc, header_->reffreq, |
---|
| 134 | header_->bandwidth); |
---|
| 135 | |
---|
[2] | 136 | if (status) { |
---|
| 137 | delete reader_; |
---|
| 138 | reader_ = 0; |
---|
[87] | 139 | throw(AipsError("Failed to get header.")); |
---|
[2] | 140 | return; |
---|
| 141 | } |
---|
[405] | 142 | if ((header_->obstype).matches("*SW*")) { |
---|
[2] | 143 | // need robust way here - probably read ahead of next timestamp |
---|
| 144 | cout << "Header indicates frequency switched observation.\n" |
---|
| 145 | "setting # of IFs = 1 " << endl; |
---|
| 146 | nIF_ = 1; |
---|
| 147 | } |
---|
[337] | 148 | |
---|
[405] | 149 | // Determine Telescope and set brightness unit |
---|
[342] | 150 | |
---|
| 151 | Bool throwIt = False; |
---|
[405] | 152 | Instrument inst = SDMemTable::convertInstrument(header_->antennaname, |
---|
| 153 | throwIt); |
---|
| 154 | header_->fluxunit = "Jy"; |
---|
[342] | 155 | if (inst==ATMOPRA || inst==TIDBINBILLA) { |
---|
[405] | 156 | header_->fluxunit = "K"; |
---|
[342] | 157 | } |
---|
[332] | 158 | |
---|
[405] | 159 | header_->nif = nIF_; |
---|
| 160 | header_->epoch = "UTC"; |
---|
| 161 | |
---|
[2] | 162 | // Apply selection criteria. |
---|
[332] | 163 | |
---|
[2] | 164 | Vector<Int> ref; |
---|
| 165 | Vector<Bool> beamSel(nBeam_,True); |
---|
| 166 | Vector<Bool> IFsel(nIF_,True); |
---|
[405] | 167 | |
---|
[332] | 168 | ifOffset_ = 0; |
---|
| 169 | if (whichIF>=0) { |
---|
| 170 | if (whichIF>=0 && whichIF<nIF_) { |
---|
| 171 | IFsel = False; |
---|
| 172 | IFsel(whichIF) = True; |
---|
[405] | 173 | header_->nif = 1; |
---|
[332] | 174 | nIF_ = 1; |
---|
| 175 | ifOffset_ = whichIF; |
---|
| 176 | } else { |
---|
| 177 | throw(AipsError("Illegal IF selection")); |
---|
| 178 | } |
---|
| 179 | } |
---|
[405] | 180 | |
---|
[332] | 181 | beamOffset_ = 0; |
---|
| 182 | if (whichBeam>=0) { |
---|
| 183 | if (whichBeam>=0 && whichBeam<nBeam_) { |
---|
| 184 | beamSel = False; |
---|
| 185 | beamSel(whichBeam) = True; |
---|
[405] | 186 | header_->nbeam = 1; |
---|
[332] | 187 | nBeam_ = 1; |
---|
| 188 | beamOffset_ = whichBeam; |
---|
| 189 | } else { |
---|
| 190 | throw(AipsError("Illegal Beam selection")); |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | Vector<Int> start(nIF_, 1); |
---|
| 194 | Vector<Int> end(nIF_, 0); |
---|
[2] | 195 | reader_->select(beamSel, IFsel, start, end, ref, True, haveXPol); |
---|
[405] | 196 | table_->putSDHeader(*header_); |
---|
| 197 | |
---|
| 198 | if (frequencies_) delete frequencies_; |
---|
| 199 | frequencies_ = new SDFrequencyTable(); |
---|
| 200 | frequencies_->setRefFrame(header_->freqref); |
---|
| 201 | frequencies_->setRestFrequencyUnit("Hz"); |
---|
| 202 | frequencies_->setEquinox(header_->equinox); |
---|
[2] | 203 | } |
---|
| 204 | |
---|
| 205 | int SDReader::read(const std::vector<int>& seq) { |
---|
[87] | 206 | int status = 0; |
---|
| 207 | |
---|
[17] | 208 | Int beamNo, IFno, refBeam, scanNo, cycleNo; |
---|
[87] | 209 | Float azimuth, elevation, focusAxi, focusRot, focusTan, |
---|
[2] | 210 | humidity, parAngle, pressure, temperature, windAz, windSpeed; |
---|
[73] | 211 | Double bandwidth, freqInc, interval, mjd, refFreq, restFreq, srcVel; |
---|
[2] | 212 | String fieldName, srcName, tcalTime; |
---|
| 213 | Vector<Float> calFctr, sigma, tcal, tsys; |
---|
| 214 | Matrix<Float> baseLin, baseSub; |
---|
| 215 | Vector<Double> direction(2), scanRate(2), srcDir(2), srcPM(2); |
---|
| 216 | Matrix<Float> spectra; |
---|
| 217 | Matrix<uChar> flagtra; |
---|
| 218 | Complex xCalFctr; |
---|
| 219 | Vector<Complex> xPol; |
---|
| 220 | uInt n = seq.size(); |
---|
[25] | 221 | |
---|
[414] | 222 | |
---|
[405] | 223 | uInt stepsize = header_->nif*header_->nbeam; |
---|
[2] | 224 | uInt seqi = 0; |
---|
| 225 | Bool getAll = False; |
---|
[405] | 226 | |
---|
[2] | 227 | if (seq[n-1] == -1) getAll = True; |
---|
| 228 | while ( (cursor_ <= seq[n-1]) || getAll) { |
---|
| 229 | // only needs to be create if in seq |
---|
[405] | 230 | SDContainer sc(header_->nbeam,header_->nif,header_->npol,header_->nchan); |
---|
[2] | 231 | // iterate over one correlator integration cycle = nBeam*nIF |
---|
| 232 | for (uInt row=0; row < stepsize; row++) { |
---|
| 233 | // stepsize as well |
---|
| 234 | // spectra(nChan,nPol)!!! |
---|
[87] | 235 | status = reader_->read(scanNo, cycleNo, mjd, interval, fieldName, |
---|
| 236 | srcName, srcDir, srcPM, srcVel, IFno, refFreq, |
---|
| 237 | bandwidth, freqInc, restFreq, tcal, tcalTime, |
---|
| 238 | azimuth, elevation, parAngle, focusAxi, |
---|
| 239 | focusTan, focusRot, temperature, pressure, |
---|
| 240 | humidity, windSpeed, windAz, refBeam, |
---|
| 241 | beamNo, direction, scanRate, |
---|
| 242 | tsys, sigma, calFctr, baseLin, baseSub, |
---|
| 243 | spectra, flagtra, xCalFctr, xPol); |
---|
[332] | 244 | |
---|
[405] | 245 | // Make sure beam/IF numbers are 0-relative - dealing with |
---|
| 246 | // possible IF or Beam selection |
---|
[332] | 247 | beamNo = beamNo - beamOffset_ - 1; |
---|
| 248 | IFno = IFno - ifOffset_ - 1; |
---|
[405] | 249 | |
---|
[2] | 250 | if (status) { |
---|
[87] | 251 | if (status == -1) { |
---|
| 252 | // EOF. |
---|
[205] | 253 | if (row > 0 && row < stepsize-1) |
---|
| 254 | cerr << "incomplete scan data.\n Probably means not all Beams/IFs/Pols within a scan are present." << endl; |
---|
[405] | 255 | |
---|
| 256 | // flush frequency table |
---|
| 257 | table_->putSDFreqTable(*frequencies_); |
---|
[87] | 258 | return status; |
---|
| 259 | } |
---|
| 260 | } |
---|
[2] | 261 | // if in the given list |
---|
[33] | 262 | if (cursor_ == seq[seqi] || getAll) { |
---|
[87] | 263 | // add integration cycle |
---|
| 264 | if (row==0) { |
---|
| 265 | //add invariant info: scanNo, mjd, interval, fieldName, |
---|
| 266 | //srcName, azimuth, elevation, parAngle, focusAxi, focusTan, |
---|
| 267 | //focusRot, temperature, pressure, humidity, windSpeed, |
---|
| 268 | //windAz srcDir, srcPM, srcVel |
---|
| 269 | sc.timestamp = mjd; |
---|
| 270 | sc.interval = interval; |
---|
| 271 | sc.sourcename = srcName; |
---|
[110] | 272 | sc.fieldname = fieldName; |
---|
| 273 | sc.azimuth = azimuth; |
---|
| 274 | sc.elevation = elevation; |
---|
[87] | 275 | } |
---|
| 276 | // add specific info |
---|
[404] | 277 | // refPix = nChan/2+1 in 1-rel Integer arith.! |
---|
[405] | 278 | Int refPix = header_->nchan/2; // 0-rel |
---|
| 279 | uInt freqID = frequencies_->addFrequency(refPix, refFreq, freqInc); |
---|
| 280 | uInt restFreqID = frequencies_->addRestFrequency(restFreq); |
---|
| 281 | |
---|
[390] | 282 | sc.setFrequencyMap(freqID, IFno); |
---|
| 283 | sc.setRestFrequencyMap(restFreqID, IFno); |
---|
[405] | 284 | |
---|
[110] | 285 | sc.tcal[0] = tcal[0];sc.tcal[1] = tcal[1]; |
---|
| 286 | sc.tcaltime = tcalTime; |
---|
| 287 | sc.parangle = parAngle; |
---|
[374] | 288 | sc.refbeam = refBeam-1;//make it 0-based; -1 if nbeams == 1 |
---|
[87] | 289 | sc.scanid = scanNo-1;//make it 0-based |
---|
[332] | 290 | sc.setSpectrum(spectra, beamNo, IFno); |
---|
| 291 | sc.setFlags(flagtra, beamNo, IFno); |
---|
| 292 | sc.setTsys(tsys, beamNo, IFno); |
---|
| 293 | sc.setDirection(direction, beamNo); |
---|
[2] | 294 | } |
---|
| 295 | } |
---|
[404] | 296 | |
---|
[33] | 297 | if (cursor_ == seq[seqi] || getAll) { |
---|
[2] | 298 | // insert container into table/list |
---|
| 299 | table_->putSDContainer(sc); |
---|
| 300 | seqi++;// next in list |
---|
| 301 | } |
---|
[16] | 302 | cursor_++;// increment position in file |
---|
[404] | 303 | |
---|
[2] | 304 | } |
---|
[405] | 305 | table_->putSDFreqTable(*frequencies_); |
---|
[2] | 306 | return status; |
---|
| 307 | } |
---|