source: trunk/src/SDReader.cc @ 315

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

check for file's existence and issue useful message if not there

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 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 <atnf/PKSIO/PKSreader.h>
35
36#include "SDReader.h"
37
38using namespace casa;
39using namespace asap;
40
41SDReader::SDReader() :
42  reader_(0),
43  table_(new SDMemTable()) {
44  cursor_ = 0;
45}
46SDReader::SDReader(const std::string& filename) :
47  reader_(0),
48  table_(new SDMemTable()) {
49  cursor_ = 0;
50  open(filename);
51}
52
53SDReader::SDReader(CountedPtr<SDMemTable> tbl) :
54  reader_(0),
55  table_(tbl) {
56  cursor_ = 0;
57}
58
59SDReader::~SDReader() {
60  if (reader_) delete reader_; reader_ = 0;
61}
62
63void SDReader::reset() {
64  cursor_ = 0;
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_);
78}
79
80void SDReader::close() {
81  cerr << "disabled" << endl;
82}
83
84void SDReader::open(const std::string& filename) {
85  if (reader_) delete reader_; reader_ = 0;
86  Bool   haveBase, haveSpectra, haveXPol;
87//
88  String inName(filename);
89  Path path(inName);
90  inName = path.expandedName();
91//
92  File file(inName);
93  if (!file.exists()) {
94     throw(AipsError("File does not exist"));
95  }
96  filename_ = inName;
97//
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)  {
103    throw(AipsError("PKSreader failed"));
104  }
105  if (!haveSpectra) {
106    delete reader_;
107    reader_ = 0;
108    throw(AipsError("No spectral data in file."));
109    return;
110  }
111  nBeam_ = beams.nelements();
112  // Get basic parameters.
113  if (haveXPol) {
114    cout << "Warning. Ignoring cross polarisation." << endl;
115    nPol_--;   
116  }
117  header_ = SDHeader();
118  header_.nchan = nChan_;
119  header_.npol = nPol_;
120  header_.nbeam = nBeam_;
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,
126                                  header_.bandwidth);
127  if (status) {
128    delete reader_;
129    reader_ = 0;
130    throw(AipsError("Failed to get header."));
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_;
140  header_.fluxunit = "K";
141  header_.epoch = "UTC";
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);
149  table_->putSDHeader(header_);
150  frequencies_.setRefFrame(header_.freqref);
151  frequencies_.setRestFrequencyUnit("Hz");
152  frequencies_.setEquinox(header_.equinox);
153}
154
155int SDReader::read(const std::vector<int>& seq) {
156  int status = 0;
157
158  Int    beamNo, IFno, refBeam, scanNo, cycleNo;
159  Float  azimuth, elevation, focusAxi, focusRot, focusTan,
160    humidity, parAngle, pressure, temperature, windAz, windSpeed;
161  Double bandwidth, freqInc, interval, mjd, refFreq, restFreq, srcVel;
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();
171
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
178    SDContainer sc(header_.nbeam,header_.nif,header_.npol,header_.nchan);
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)!!!
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);
192      if (status) {
193        if (status == -1) {
194          // EOF.
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;
197          //cerr << "EOF" << endl;
198          table_->putSDFreqTable(frequencies_);
199          return status;
200        }
201      }
202      // if in the given list
203      if (cursor_ == seq[seqi] || getAll) {
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;
213          sc.fieldname = fieldName;
214          sc.azimuth = azimuth;
215          sc.elevation = elevation;
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);
222        frequencies_.addRestFrequency(restFreq);
223        sc.setFrequencyMap(frqslot,IFno-1);
224        sc.tcal[0] = tcal[0];sc.tcal[1] = tcal[1];
225        sc.tcaltime = tcalTime;
226        sc.parangle = parAngle;
227        sc.refbeam = refBeam;
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);
233      }
234    }
235    if (cursor_ == seq[seqi] || getAll) {
236      // insert container into table/list
237      table_->putSDContainer(sc);
238      seqi++;// next in list
239    }
240    cursor_++;// increment position in file
241  }
242  table_->putSDFreqTable(frequencies_);
243  return status;
244}
Note: See TracBrowser for help on using the repository browser.