source: trunk/src/STWriter.cpp@ 1453

Last change on this file since 1453 was 1449, checked in by MarkCalabretta, 16 years ago

Adapted to new API, PKSwriter::write(const PKSrecord &).

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