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