source: trunk/src/SDReader.cc @ 404

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

reference pixel caluclation was 1-rel instead of 0-rel

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