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 442 2005-02-15 00:57:48Z 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 <tables/Tables/ArrayColumn.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | #include "SDContainer.h"
|
---|
46 | #include "SDMemTable.h"
|
---|
47 | #include "SDWriter.h"
|
---|
48 | #include "SDFITSImageWriter.h"
|
---|
49 | #include "SDAsciiWriter.h"
|
---|
50 |
|
---|
51 | using namespace casa;
|
---|
52 | using namespace asap;
|
---|
53 |
|
---|
54 | //--------------------------------------------------------- SDWriter::SDWriter
|
---|
55 |
|
---|
56 | // Default constructor.
|
---|
57 |
|
---|
58 | SDWriter::SDWriter(const std::string &format)
|
---|
59 | {
|
---|
60 | cFormat = format;
|
---|
61 | //
|
---|
62 | if (cFormat == "MS2") {
|
---|
63 | cWriter = new PKSMS2writer();
|
---|
64 | } else if (cFormat == "SDFITS") {
|
---|
65 | cWriter = new PKSSDwriter();
|
---|
66 | } else if (cFormat == "FITS") {
|
---|
67 | cWriter = 0;
|
---|
68 | } else if (cFormat == "ASCII") {
|
---|
69 | cWriter = 0;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | //-------------------------------------------------------- SDWriter::~SDWriter
|
---|
74 |
|
---|
75 | // Destructor.
|
---|
76 |
|
---|
77 | SDWriter::~SDWriter()
|
---|
78 | {
|
---|
79 | if (cWriter) {
|
---|
80 | delete cWriter;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | //-------------------------------------------------------- SDWriter::setFormat
|
---|
85 |
|
---|
86 | // Reset the output format.
|
---|
87 |
|
---|
88 | Int SDWriter::setFormat(const std::string &format)
|
---|
89 | {
|
---|
90 | if (format != cFormat) {
|
---|
91 | if (cWriter) delete cWriter;
|
---|
92 | //
|
---|
93 | cFormat = format;
|
---|
94 | if (cFormat == "MS2") {
|
---|
95 | cWriter = new PKSMS2writer();
|
---|
96 | } else if (cFormat == "SDFITS") {
|
---|
97 | cWriter = new PKSSDwriter();
|
---|
98 | } else if (cFormat == "FITS") {
|
---|
99 | cWriter = 0;
|
---|
100 | } else if (cFormat == "ASCII") {
|
---|
101 | cWriter = 0;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return 0;
|
---|
105 | }
|
---|
106 |
|
---|
107 | //------------------------------------------------------------ SDWriter::write
|
---|
108 |
|
---|
109 | // Write an SDMemTable to file in the desired format, closing the file when
|
---|
110 | // finished.
|
---|
111 |
|
---|
112 | Int SDWriter::write(const CountedPtr<SDMemTable> in,
|
---|
113 | const std::string &filename, Bool toStokes)
|
---|
114 | {
|
---|
115 |
|
---|
116 | // Image FITS
|
---|
117 |
|
---|
118 | if (cFormat=="FITS") {
|
---|
119 | Bool verbose = True;
|
---|
120 | SDFITSImageWriter iw;
|
---|
121 | if (iw.write(*in, filename, verbose, toStokes)) {
|
---|
122 | return 0;
|
---|
123 | } else {
|
---|
124 | return 1;
|
---|
125 | }
|
---|
126 | } else if (cFormat=="ASCII") {
|
---|
127 | SDAsciiWriter iw;
|
---|
128 | if (iw.write(*in, filename, toStokes)) {
|
---|
129 | return 0;
|
---|
130 | } else {
|
---|
131 | return 1;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | // MS or SDFITS
|
---|
136 |
|
---|
137 | // Extract the header from the table.
|
---|
138 | SDHeader hdr = in->getSDHeader();
|
---|
139 | const Int nPol = hdr.npol;
|
---|
140 | const Int nChan = hdr.nchan;
|
---|
141 | if (toStokes) {
|
---|
142 | cerr << "Stokes conversion not yet available with SDFITS or MS" << endl;
|
---|
143 | }
|
---|
144 |
|
---|
145 | // Get Freq table
|
---|
146 |
|
---|
147 | SDFrequencyTable sdft = in->getSDFreqTable();
|
---|
148 | Vector<Double> restFreqs;
|
---|
149 | String restFreqUnit;
|
---|
150 | sdft.restFrequencies(restFreqs, restFreqUnit);
|
---|
151 | Double restFreq = 0.0;
|
---|
152 | if (restFreqs.nelements()>0) {
|
---|
153 | Quantum<Double> rF(restFreqs(0), Unit(restFreqUnit));
|
---|
154 | restFreq = rF.getValue(Unit("Hz"));
|
---|
155 | }
|
---|
156 |
|
---|
157 | // Table columns
|
---|
158 |
|
---|
159 | const Table table = in->table();
|
---|
160 | ROArrayColumn<uInt> freqIDCol(table, "FREQID");
|
---|
161 | Vector<uInt> freqIDs;
|
---|
162 |
|
---|
163 | // Create the output file and write static data.
|
---|
164 | Int status;
|
---|
165 | if (status = cWriter->create(filename, hdr.observer, hdr.project,
|
---|
166 | hdr.antennaname, hdr.antennaposition,
|
---|
167 | hdr.obstype, hdr.equinox, hdr.freqref,
|
---|
168 | nChan, nPol, False, False)) {
|
---|
169 | cerr << "Failed to create output file." << endl;
|
---|
170 | return 1;
|
---|
171 | }
|
---|
172 |
|
---|
173 | Int scanNo = -1;
|
---|
174 | Int cycleNo;
|
---|
175 | Double mjd0 = 0.0;
|
---|
176 |
|
---|
177 | Int count = 0;
|
---|
178 | for (Int iRow = 0; iRow < in->nRow(); iRow++) {
|
---|
179 | // Extract the next integration from the table.
|
---|
180 | SDContainer sd = in->getSDContainer(iRow);
|
---|
181 | if (sd.scanid != scanNo) {
|
---|
182 | scanNo = sd.scanid;
|
---|
183 | mjd0 = sd.timestamp;
|
---|
184 | cycleNo = 1;
|
---|
185 | } else if (fabs(sd.timestamp-mjd0) > sd.interval) {
|
---|
186 | cycleNo++;
|
---|
187 | }
|
---|
188 |
|
---|
189 | // Get FreqID vector
|
---|
190 | freqIDCol.get(iRow, freqIDs);
|
---|
191 |
|
---|
192 | // Write it out beam by beam.
|
---|
193 | for (Int iBeam = 0; iBeam < hdr.nbeam; iBeam++) {
|
---|
194 |
|
---|
195 | // Write it out IF by IF.
|
---|
196 | for (Int iIF = 0; iIF < hdr.nif; iIF++) {
|
---|
197 | uInt freqID = freqIDs(iIF);
|
---|
198 |
|
---|
199 | // None of these are stored in SDMemTable by SDReader.
|
---|
200 | //String fieldName = "";
|
---|
201 | //Vector<Double> srcDir(2, 0.0);
|
---|
202 | Vector<Double> srcPM(2, 0.0);
|
---|
203 | Double srcVel = 0.0;
|
---|
204 |
|
---|
205 | // The writer will assume refPix = nChan/2 (0-rel). So recompute
|
---|
206 | // the frequency at this location.
|
---|
207 |
|
---|
208 | Double cdelt = sdft.increment(freqID);
|
---|
209 | Double crval = sdft.referenceValue(freqID);
|
---|
210 | Double crpix = sdft.referencePixel(freqID);
|
---|
211 | Double pixel = nChan/2;
|
---|
212 | Double refFreqNew = (pixel-crpix)*cdelt + crval;
|
---|
213 | //
|
---|
214 | //Vector<Float> tcal(2, 0.0f);
|
---|
215 | //String tcalTime = "";
|
---|
216 | //Float azimuth = 0.0f;
|
---|
217 | //Float elevation = 0.0f;
|
---|
218 | //Float parAngle = 0.0f;
|
---|
219 | Float focusAxi = 0.0f;
|
---|
220 | Float focusTan = 0.0f;
|
---|
221 | Float focusRot = 0.0f;
|
---|
222 | Float temperature = 0.0f;
|
---|
223 | Float pressure = 0.0f;
|
---|
224 | Float humidity = 0.0f;
|
---|
225 | Float windSpeed = 0.0f;
|
---|
226 | Float windAz = 0.0f;
|
---|
227 | //Int refBeam = 0;
|
---|
228 | //Vector<Double> direction(2, 0.0);
|
---|
229 | Vector<Double> scanRate(2, 0.0);
|
---|
230 | Vector<Float> sigma(nPol, 0.0f);
|
---|
231 | Vector<Float> calFctr(nPol, 0.0f);
|
---|
232 | Matrix<Float> baseLin(nPol,2, 0.0f);
|
---|
233 | Matrix<Float> baseSub(nPol,9, 0.0f);
|
---|
234 | Complex xCalFctr;
|
---|
235 | Vector<Complex> xPol;
|
---|
236 | //
|
---|
237 | if (status = cWriter->write(sd.scanid, cycleNo, sd.timestamp,
|
---|
238 | sd.interval, sd.fieldname, sd.sourcename,
|
---|
239 | sd.getDirection(iBeam),
|
---|
240 | srcPM, srcVel, iIF+1, refFreqNew,
|
---|
241 | nChan*abs(cdelt), cdelt, restFreq, sd.tcal,
|
---|
242 | sd.tcaltime, sd.azimuth, sd.elevation,
|
---|
243 | sd.parangle,
|
---|
244 | focusAxi, focusTan, focusRot, temperature,
|
---|
245 | pressure, humidity, windSpeed, windAz,
|
---|
246 | sd.refbeam, iBeam+1,
|
---|
247 | sd.getDirection(iBeam),
|
---|
248 | scanRate,
|
---|
249 | sd.getTsys(iBeam, iIF), sigma, calFctr,
|
---|
250 | baseLin, baseSub,
|
---|
251 | sd.getSpectrum(iBeam, iIF),
|
---|
252 | sd.getFlags(iBeam, iIF),
|
---|
253 | xCalFctr, xPol)) {
|
---|
254 | cerr << "Error writing output file." << endl;
|
---|
255 | return 1;
|
---|
256 | }
|
---|
257 |
|
---|
258 | count++;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | cout << "SDWriter: wrote " << count << " rows to " << filename << endl;
|
---|
264 | cWriter->close();
|
---|
265 |
|
---|
266 | return 0;
|
---|
267 | }
|
---|