source: trunk/src/STWriter.cpp@ 1626

Last change on this file since 1626 was 1586, checked in by Malte Marquarding, 15 years ago

Ticket #165: have removed the hard-coding of parallactifying the data. NOTE THIS breaks the Table structure as I have moved the PARANGLE column from the main table into the FOCUS table. We need to have a new release. Also one needs to explicitly tell the scantable via rc or member function to enable parallactifying

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