source: trunk/src/SDWriter.cc @ 194

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

adjust to allow FITSImage writing (use SDFITSImageWriter)
delete pointer in destructor

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1 //#---------------------------------------------------------------------------
2//# SDWriter.cc: ASAP class to write out single dish spectra.
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: SDWriter.cc 192 2005-01-14 00:06:15Z kil064 $
30//#---------------------------------------------------------------------------
31
32#include <string>
33
34#include <casa/aips.h>
35#include <casa/Arrays.h>
36#include <casa/BasicSL/Complex.h>
37#include <casa/Utilities/CountedPtr.h>
38
39#include <atnf/PKSIO/PKSMS2writer.h>
40#include <atnf/PKSIO/PKSSDwriter.h>
41
42#include "SDContainer.h"
43#include "SDMemTable.h"
44#include "SDWriter.h"
45#include "SDFITSImageWriter.h"
46
47using namespace casa;
48using namespace asap;
49
50//--------------------------------------------------------- SDWriter::SDWriter
51
52// Default constructor.
53
54SDWriter::SDWriter(const std::string &format)
55{
56  cFormat = format;
57//
58  if (cFormat == "MS2") {
59    cWriter = new PKSMS2writer();
60  } else if (cFormat == "SDFITS") {
61    cWriter = new PKSSDwriter();
62  } else if (cFormat == "FITS") {
63    cWriter = 0;
64  }
65}
66
67//-------------------------------------------------------- SDWriter::~SDWriter
68
69// Destructor.
70
71SDWriter::~SDWriter()
72{
73   if (cWriter) {
74     delete cWriter;
75   }
76}
77
78//-------------------------------------------------------- SDWriter::setFormat
79
80// Reset the output format.
81
82Int SDWriter::setFormat(const std::string &format)
83{
84  if (format != cFormat) {
85    if (cWriter) delete cWriter;
86//
87    cFormat = format;
88    if (cFormat == "MS2") {
89      cWriter = new PKSMS2writer();
90    } else if (cFormat == "SDFITS") {
91      cWriter = new PKSSDwriter();
92    } else if (cFormat == "FITS") {
93      cWriter = 0;
94    }
95  }
96  return 0;
97}
98
99//------------------------------------------------------------ SDWriter::write
100
101// Write an SDMemTable to file in the desired format, closing the file when
102// finished.
103
104Int SDWriter::write(const CountedPtr<SDMemTable> table,
105                    const std::string &filename)
106{
107
108// Image FITS
109
110  if (cFormat=="FITS") {
111     Bool verbose = True;
112     SDFITSImageWriter iw;
113     if (iw.write(*table, filename, verbose)) {
114        return 0;
115     } else {
116        return 1;
117     }
118  }
119
120// MS or SDFITS
121
122  // Extract the header from the table.
123  SDHeader hdr = table->getSDHeader();
124  Int nPol  = hdr.npol;
125  Int nChan = hdr.nchan;
126
127  // Create the output file and write static data.
128  Int status;
129  if (status = cWriter->create(filename, hdr.observer, hdr.project,
130                               hdr.antennaname, hdr.antennaposition,
131                               hdr.obstype, hdr.equinox, hdr.freqref,
132                               nChan, nPol, False, False)) {
133    cerr << "Failed to create output file." << endl;
134    return 1;
135  }
136
137  Int scanNo = -1;
138  Int cycleNo;
139  Double mjd0 = 0.0;
140
141  Int count = 0;
142  for (Int iRow = 0; iRow < table->nRow(); iRow++) {
143    // Extract the next integration from the table.
144    SDContainer sd = table->getSDContainer(iRow);
145    if (sd.scanid != scanNo) {
146      scanNo = sd.scanid;
147      mjd0 = sd.timestamp;
148      cycleNo = 1;
149    } else if (fabs(sd.timestamp-mjd0) > sd.interval) {
150      cycleNo++;
151    }
152
153    // Write it out beam by beam.
154    for (Int iBeam = 0; iBeam < hdr.nbeam; iBeam++) {
155
156      // Write it out IF by IF.
157      for (Int iIF = 0; iIF < hdr.nif; iIF++) {
158        // None of these are stored in SDMemTable by SDReader.
159        //String          fieldName = "";
160        //Vector<Double>  srcDir(2, 0.0);
161        Vector<Double>  srcPM(2, 0.0);
162        Double          srcVel = 0.0;
163        Double          freqInc = 0.0;
164        Double          restFreq = 0.0;
165        //Vector<Float>   tcal(2, 0.0f);
166        //String          tcalTime = "";
167        //Float           azimuth = 0.0f;
168        //Float           elevation = 0.0f;
169        //Float           parAngle = 0.0f;
170        Float           focusAxi = 0.0f;
171        Float           focusTan = 0.0f;
172        Float           focusRot = 0.0f;
173        Float           temperature = 0.0f;
174        Float           pressure = 0.0f;
175        Float           humidity = 0.0f;
176        Float           windSpeed = 0.0f;
177        Float           windAz = 0.0f;
178        //Int             refBeam = 0;
179        //Vector<Double>  direction(2, 0.0);
180        Vector<Double>  scanRate(2, 0.0);
181        Vector<Float>   sigma(nPol, 0.0f);
182        Vector<Float>   calFctr(nPol, 0.0f);
183        Matrix<Float>   baseLin(nPol,2, 0.0f);
184        Matrix<Float>   baseSub(nPol,9, 0.0f);
185        Complex         xCalFctr;
186        Vector<Complex> xPol;
187        if (status = cWriter->write(sd.scanid, cycleNo, sd.timestamp,
188                                    sd.interval, sd.fieldname, sd.sourcename,
189                                    sd.getDirection(iBeam),
190                                    srcPM, srcVel, iIF+1, hdr.reffreq,
191                                    hdr.bandwidth, freqInc, restFreq, sd.tcal,
192                                    sd.tcaltime, sd.azimuth, sd.elevation,
193                                    sd.parangle,
194                                    focusAxi, focusTan, focusRot, temperature,
195                                    pressure, humidity, windSpeed, windAz,
196                                    sd.refbeam, iBeam+1,
197                                    sd.getDirection(iBeam),
198                                    scanRate,
199                                    sd.getTsys(iBeam, iIF), sigma, calFctr,
200                                    baseLin, baseSub,
201                                    sd.getSpectrum(iBeam, iIF),
202                                    sd.getFlags(iBeam, iIF),
203                                    xCalFctr, xPol)) {
204          cerr << "Error writing output file." << endl;
205          return 1;
206        }
207
208        count++;
209      }
210    }
211  }
212
213  cout << "SDWriter: wrote " << count << " rows to " << filename << endl;
214  cWriter->close();
215
216  return 0;
217}
Note: See TracBrowser for help on using the repository browser.