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