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