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