source: trunk/src/SDReader.cc @ 414

Last change on this file since 414 was 414, checked in by mar637, 19 years ago

cerr to cout changes were appropriate.

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