source: trunk/src/STWriter.cpp@ 1281

Last change on this file since 1281 was 1280, checked in by mar637, 18 years ago

Merge from Release2.1.1b tag

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1//#---------------------------------------------------------------------------
2//# STWriter.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: STWriter.cpp 1280 2006-11-03 05:08:22Z mar637 $
30//#---------------------------------------------------------------------------
31
32#include <string>
33
34#include <casa/aips.h>
35#include <casa/Arrays/Array.h>
36#include <casa/Arrays/Vector.h>
37#include <casa/BasicSL/Complex.h>
38#include <casa/Utilities/CountedPtr.h>
39#include <casa/Utilities/Assert.h>
40
41#include <atnf/PKSIO/PKSMS2writer.h>
42#include <atnf/PKSIO/PKSSDwriter.h>
43
44#include <tables/Tables/Table.h>
45#include <tables/Tables/TableIter.h>
46#include <tables/Tables/TableRow.h>
47#include <tables/Tables/ArrayColumn.h>
48
49//#include "SDFITSImageWriter.h"
50#include "STAsciiWriter.h"
51#include "STHeader.h"
52
53#include "STWriter.h"
54
55using namespace casa;
56namespace asap {
57
58STWriter::STWriter(const std::string &format)
59{
60 format_ = format;
61 String t(format_);
62 t.upcase();
63 if (t== "MS2") {
64 writer_ = new PKSMS2writer();
65 } else if (t== "SDFITS") {
66 writer_ = new PKSSDwriter();
67 } else if (t== "ASCII") {
68 writer_ = 0;
69 } else {
70 throw (AipsError("Unrecognized export format"));
71 }
72}
73
74STWriter::~STWriter()
75{
76 if (writer_) {
77 delete writer_;
78 }
79}
80
81Int STWriter::setFormat(const std::string &format)
82{
83 if (format != format_) {
84 if (writer_) delete writer_;
85 }
86
87 format_ = format;
88 String t(format_);
89 t.upcase();
90 if (t== "MS2") {
91 writer_ = new PKSMS2writer();
92 } else if (t== "SDFITS") {
93 writer_ = new PKSSDwriter();
94 } else if (t== "ASCII") {
95 writer_ = 0;
96 } else {
97 throw (AipsError("Unrecognized Format"));
98 }
99 return 0;
100}
101
102Int STWriter::write(const CountedPtr<Scantable> in,
103 const std::string &filename)
104{
105
106 if (format_=="ASCII") {
107 STAsciiWriter iw;
108 if (iw.write(*in, filename)) {
109 return 0;
110 } else {
111 return 1;
112 }
113 }
114
115 // MS or SDFITS
116
117 // Extract the header from the table.
118 STHeader hdr = in->getHeader();
119 //const Int nPol = hdr.npol;
120 //const Int nChan = hdr.nchan;
121 int nIF = in->nif();
122 Vector<uInt> nPol(nIF),nChan(nIF);
123 Vector<Bool> havexpol(nIF);
124 for (int i=0;i<nIF;++i) {
125 nPol(i) = in->npol();
126 nChan(i) = in->nchan(i);
127 havexpol(i) = nPol(i) > 2;
128 }
129
130 const Table table = in->table();
131
132 // Create the output file and write static data.
133 Int status;
134 status = writer_->create(String(filename), hdr.observer, hdr.project,
135 hdr.antennaname, hdr.antennaposition,
136 hdr.obstype, hdr.equinox, hdr.freqref,
137 nChan, nPol, havexpol, False);
138 if ( status ) {
139 throw(AipsError("Failed to create output file"));
140 }
141
142 Double srcVel;
143
144 String fieldName, srcName, tcalTime;
145 Vector<Float> calFctr, sigma, tcal, tsys;
146 Vector<Double> direction(2), scanRate(2), srcDir(2), srcPM(2);
147 Matrix<Float> spectra;
148 Matrix<uChar> flagtra;
149 Complex xCalFctr;
150 Int count = 0;
151 Int scanno = 1;
152 // use spearate iterators to ensure renumbering of all numbers
153 TableIterator scanit(table, "SCANNO");
154 while (!scanit.pastEnd() ) {
155 Table stable = scanit.table();
156 TableIterator beamit(stable, "BEAMNO");
157 Int beamno = 1;
158 while (!beamit.pastEnd() ) {
159 Table btable = beamit.table();
160 // position only varies by beam
161 MDirection::ScalarColumn dirCol(btable, "DIRECTION");
162 Vector<Double> direction = dirCol(0).getAngle("rad").getValue();
163 TableIterator cycit(btable, "CYCLENO");
164 ROArrayColumn<Double> srateCol(btable, "SCANRATE");
165 srateCol.get(0, scanRate);
166 ROArrayColumn<Double> spmCol(btable, "SRCPROPERMOTION");
167 spmCol.get(0, srcPM);
168 ROArrayColumn <Double> sdirCol(btable, "SRCDIRECTION");
169 sdirCol.get(0, srcDir);
170 ROScalarColumn<Double> svelCol(btable, "SRCVELOCITY");
171 svelCol.get(0, srcVel);
172 Int cycno = 1;
173 while (!cycit.pastEnd() ) {
174 Table ctable = cycit.table();
175 TableIterator ifit(ctable, "IFNO");
176 Int ifno = 1;
177 while (!ifit.pastEnd() ) {
178 Table itable = ifit.table();
179 TableRow row(itable);
180 // use the first row to fill in all the "metadata"
181 const TableRecord& rec = row.get(0);
182 ROArrayColumn<Float> specCol(itable, "SPECTRA");
183 uInt nchan = specCol(0).nelements();
184 Double cdelt,crval,crpix, restfreq;
185 Float focusAxi, focusTan, focusRot,
186 temperature, pressure, humidity, windSpeed, windAz;
187 Float tmp0,tmp1,tmp2,tmp3,tmp4;
188 Vector<Float> tcalval;
189 String stmp0,stmp1, tcalt;
190 in->frequencies().getEntry(crpix,crval,cdelt, rec.asuInt("FREQ_ID"));
191 in->focus().getEntry(focusAxi, focusTan, focusRot,
192 tmp0,tmp1,tmp2,tmp3,tmp4,
193 rec.asuInt("FOCUS_ID"));
194 in->molecules().getEntry(restfreq,stmp0,stmp1,rec.asuInt("MOLECULE_ID"));
195 in->tcal().getEntry(tcalt,tcalval,rec.asuInt("TCAL_ID"));
196 in->weather().getEntry(temperature, pressure, humidity,
197 windSpeed, windAz,
198 rec.asuInt("WEATHER_ID"));
199 Double pixel = Double(nchan/2);
200 Double refFreqNew = (pixel-crpix)*cdelt + crval;
201 // ok, now we have nrows for the n polarizations in this table
202 Matrix<Float> specs;
203 Matrix<uChar> flags;
204 Vector<Complex> xpol;
205 polConversion(specs, flags, xpol, itable);
206 Vector<Float> tsys = tsysFromTable(itable);
207 // dummy data
208 uInt npol = specs.ncolumn();
209
210 Matrix<Float> baseLin(npol,2, 0.0f);
211 Matrix<Float> baseSub(npol,9, 0.0f);
212 Complex xCalFctr;
213 Vector<Double> scanRate(2, 0.0);
214 Vector<Float> sigma(npol, 0.0f);
215 Vector<Float> calFctr(npol, 0.0f);
216 status = writer_->write(scanno, cycno, rec.asDouble("TIME"),
217 rec.asDouble("INTERVAL"),
218 rec.asString("FIELDNAME"),
219 rec.asString("SRCNAME"),
220 srcDir, srcPM, srcVel,hdr.obstype,
221 ifno,
222 refFreqNew, nchan*abs(cdelt), cdelt,
223 restfreq,
224 tcal,
225 tcalt,
226 rec.asFloat("AZIMUTH"),
227 rec.asFloat("ELEVATION"),
228 rec.asFloat("PARANGLE"),
229 focusAxi, focusTan, focusRot,
230 temperature,
231 pressure, humidity, windSpeed, windAz,
232 rec.asInt("REFBEAMNO")+1, beamno,
233 direction,
234 scanRate,
235 tsys,
236 sigma, calFctr,// not in scantable
237 baseLin, baseSub,// not in scantable
238 specs, flags,
239 xCalFctr,//
240 xpol);
241 if ( status ) {
242 writer_->close();
243 throw(AipsError("STWriter: Failed to export Scantable."));
244 }
245 ++count;
246 ++ifno;
247 ++ifit;
248 }
249 ++cycno;
250 ++cycit;
251 }
252 ++beamno;
253 ++beamit;
254 }
255 ++scanno;
256 ++scanit;
257 }
258 ostringstream oss;
259 oss << "STWriter: wrote " << count << " rows to " << filename;
260 pushLog(String(oss));
261 writer_->close();
262
263 return 0;
264}
265
266Vector<Float> STWriter::tsysFromTable(const Table& tab)
267{
268 ROArrayColumn<Float> tsysCol(tab, "TSYS");
269 Vector<Float> out(tab.nrow());
270 Vector<Float> tmp;
271 for (uInt i=0; i<tab.nrow(); ++i) {
272 tmp.resize();
273 tmp = tsysCol(i);
274 out[i] = tmp[0];
275 }
276 return out;
277}
278
279void STWriter::polConversion( Matrix< Float >& specs, Matrix< uChar >& flags,
280 Vector< Complex > & xpol, const Table & tab )
281{
282 String poltype = tab.keywordSet().asString("POLTYPE");
283 if ( poltype == "stokes") {
284 String msg = "poltype = " + poltype + " not yet supported in output.";
285 throw(AipsError(msg));
286 }
287 ROArrayColumn<Float> specCol(tab, "SPECTRA");
288 ROArrayColumn<uChar> flagCol(tab, "FLAGTRA");
289 uInt nchan = specCol(0).nelements();
290 uInt ncol = (tab.nrow()==1 ? 1: 2 );
291 specs.resize(nchan, ncol);
292 flags.resize(nchan, ncol);
293 // the linears
294 for (uInt i=0; i<ncol; ++i) {
295 specs.column(i) = specCol(i);
296 flags.column(i) = flagCol(i);
297 }
298 // now the complex if exists
299 Bool hasxpol = False;
300 xpol.resize();
301 if ( tab.nrow() == 4 ) {
302 hasxpol = True;
303 xpol.resize(nchan);
304 Vector<Float> reals, imags;
305 reals = specCol(2); imags = specCol(3);
306 for (uInt k=0; k < nchan; ++k) {
307 xpol[k] = Complex(reals[k], imags[k]);
308 }
309 }
310}
311
312
313}
Note: See TracBrowser for help on using the repository browser.