source: trunk/src/SDReader.cc @ 341

Last change on this file since 341 was 338, checked in by kil064, 19 years ago

add brightness unit to constructor

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
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
40using namespace casa;
41using namespace asap;
42
43SDReader::SDReader() :
44  reader_(0),
45  table_(new SDMemTable()) {
46  cursor_ = 0;
47}
48SDReader::SDReader(const std::string& filename, const std::string& unit,
49                   int whichIF, int whichBeam) :
50  reader_(0),
51  table_(new SDMemTable()) {
52  cursor_ = 0;
53  open(filename, unit, whichIF, whichBeam);
54}
55
56SDReader::SDReader(CountedPtr<SDMemTable> tbl) :
57  reader_(0),
58  table_(tbl) {
59  cursor_ = 0;
60}
61
62SDReader::~SDReader() {
63  if (reader_) delete reader_; reader_ = 0;
64}
65
66void 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
84void SDReader::close() {
85  cerr << "disabled" << endl;
86}
87
88
89
90void SDReader::open(const std::string& filename, const std::string& unit,
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 units...
150
151  String sUnit(unit);
152  if (sUnit.empty()) {
153     Bool throwIt = False;
154     Instrument inst = SDMemTable::convertInstrument (header_.antennaname, throwIt);
155     header_.fluxunit = "Jy";
156     if (inst==ATMOPRA || inst==TIDBINBILLA) {
157        header_.fluxunit = "K";
158     }
159     cerr << "Assuming brightness unit is " << header_.fluxunit << endl;
160   } else {
161      Unit u(sUnit);
162      if (u==Unit("Jy") || u==Unit("K")) {
163         header_.fluxunit = unit;
164         cerr << "Setting brightness unit to " << header_.fluxunit << endl;
165      } else {
166         throw(AipsError("Specified brightness unit is illegal - must be consistent with Jy or K"));
167      }
168   }
169//
170  header_.nif = nIF_;
171  header_.epoch = "UTC";
172
173  // Apply selection criteria.
174
175  Vector<Int> ref;
176  Vector<Bool> beamSel(nBeam_,True);
177  Vector<Bool> IFsel(nIF_,True);
178//
179  ifOffset_ = 0;
180  if (whichIF>=0) {
181    if (whichIF>=0 && whichIF<nIF_) {
182       IFsel = False;
183       IFsel(whichIF) = True;
184       header_.nif = 1;
185       nIF_ = 1;
186       ifOffset_ = whichIF;
187    } else {
188       throw(AipsError("Illegal IF selection"));
189    }
190  }
191//
192  beamOffset_ = 0;
193  if (whichBeam>=0) {
194     if (whichBeam>=0 && whichBeam<nBeam_) {
195        beamSel = False;
196        beamSel(whichBeam) = True;
197        header_.nbeam = 1;
198        nBeam_ = 1;
199        beamOffset_ = whichBeam;
200     } else {
201       throw(AipsError("Illegal Beam selection"));
202     }
203  }
204//
205  Vector<Int> start(nIF_, 1);
206  Vector<Int> end(nIF_, 0);
207  reader_->select(beamSel, IFsel, start, end, ref, True, haveXPol);
208  table_->putSDHeader(header_);
209  frequencies_.setRefFrame(header_.freqref);
210  frequencies_.setRestFrequencyUnit("Hz");
211  frequencies_.setEquinox(header_.equinox);
212}
213
214int SDReader::read(const std::vector<int>& seq) {
215  int status = 0;
216
217  Int    beamNo, IFno, refBeam, scanNo, cycleNo;
218  Float  azimuth, elevation, focusAxi, focusRot, focusTan,
219    humidity, parAngle, pressure, temperature, windAz, windSpeed;
220  Double bandwidth, freqInc, interval, mjd, refFreq, restFreq, srcVel;
221  String          fieldName, srcName, tcalTime;
222  Vector<Float>   calFctr, sigma, tcal, tsys;
223  Matrix<Float>   baseLin, baseSub;
224  Vector<Double>  direction(2), scanRate(2), srcDir(2), srcPM(2);
225  Matrix<Float>   spectra;
226  Matrix<uChar>   flagtra;
227  Complex         xCalFctr;
228  Vector<Complex> xPol;
229  uInt n = seq.size();
230
231  uInt stepsize = header_.nif*header_.nbeam;
232  uInt seqi = 0;
233  Bool getAll = False;
234  if (seq[n-1] == -1) getAll = True;
235  while ( (cursor_ <= seq[n-1]) || getAll) {
236    // only needs to be create if in seq
237    SDContainer sc(header_.nbeam,header_.nif,header_.npol,header_.nchan);
238    // iterate over one correlator integration cycle = nBeam*nIF
239    for (uInt row=0; row < stepsize; row++) {
240      // stepsize as well
241      // spectra(nChan,nPol)!!!
242      status = reader_->read(scanNo, cycleNo, mjd, interval, fieldName,
243                             srcName, srcDir, srcPM, srcVel, IFno, refFreq,
244                             bandwidth, freqInc, restFreq, tcal, tcalTime,
245                             azimuth, elevation, parAngle, focusAxi,
246                             focusTan, focusRot, temperature, pressure,
247                             humidity, windSpeed, windAz, refBeam,
248                             beamNo, direction, scanRate,
249                             tsys, sigma, calFctr, baseLin, baseSub,
250                             spectra, flagtra, xCalFctr, xPol);
251
252// Make sure beam/IF numbers are 0-relative - dealing with possible IF or Beam selection
253
254      beamNo = beamNo - beamOffset_ - 1;     
255      IFno = IFno - ifOffset_ - 1;
256//
257      if (status) {
258        if (status == -1) {
259          // EOF.
260          if (row > 0 && row < stepsize-1)
261            cerr << "incomplete scan data.\n Probably means not all Beams/IFs/Pols within a scan are present." << endl;
262          //cerr << "EOF" << endl;
263          table_->putSDFreqTable(frequencies_);
264          return status;
265        }
266      }
267      // if in the given list
268      if (cursor_ == seq[seqi] || getAll) {
269        // add integration cycle
270        if (row==0) {
271          //add invariant info: scanNo, mjd, interval, fieldName,
272          //srcName, azimuth, elevation, parAngle, focusAxi, focusTan,
273          //focusRot, temperature, pressure, humidity, windSpeed,
274          //windAz  srcDir, srcPM, srcVel
275          sc.timestamp = mjd;
276          sc.interval = interval;
277          sc.sourcename = srcName;
278          sc.fieldname = fieldName;
279          sc.azimuth = azimuth;
280          sc.elevation = elevation;
281        }
282        // add specific info
283        // refPix = nChan/2+1 in  Integer arith.!
284        Int refPix = header_.nchan/2+1;
285        uInt frqslot = frequencies_.addFrequency(refPix, refFreq, freqInc);
286        frequencies_.addRestFrequency(restFreq);
287//   
288        sc.setFrequencyMap(frqslot,IFno);
289        sc.tcal[0] = tcal[0];sc.tcal[1] = tcal[1];
290        sc.tcaltime = tcalTime;
291        sc.parangle = parAngle;
292        sc.refbeam = refBeam;
293        sc.scanid = scanNo-1;//make it 0-based
294        sc.setSpectrum(spectra, beamNo, IFno);
295        sc.setFlags(flagtra,  beamNo, IFno);
296        sc.setTsys(tsys, beamNo, IFno);
297        sc.setDirection(direction, beamNo);
298      }
299    }
300    if (cursor_ == seq[seqi] || getAll) {
301      // insert container into table/list
302      table_->putSDContainer(sc);
303      seqi++;// next in list
304    }
305    cursor_++;// increment position in file
306  }
307  table_->putSDFreqTable(frequencies_);
308  return status;
309}
Note: See TracBrowser for help on using the repository browser.