source: branches/alma/src/STWriter.cpp@ 1488

Last change on this file since 1488 was 1446, checked in by TakTsutsumi, 16 years ago

Merged recent updates (since 2007) from nrao-asap

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 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 1446 2008-11-12 06:04:01Z TakTsutsumi $
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 // this is a little different from what I have done
119 // before. Need to check with the Offline User Test data
120 STHeader hdr = in->getHeader();
121 //const Int nPol = hdr.npol;
122 //const Int nChan = hdr.nchan;
123 std::vector<uint> ifs = in->getIFNos();
124 int nIF = in->nif();//ifs.size();
125 Vector<uInt> nPol(nIF),nChan(nIF);
126 Vector<Bool> havexpol(nIF);
127 String fluxUnit = hdr.fluxunit;
128
129 nPol = 0;nChan = 0; havexpol = False;
130 for (uint i=0;i<ifs.size();++i) {
131 nPol(ifs[i]) = in->npol();
132 nChan(ifs[i]) = in->nchan(ifs[i]);
133 havexpol(ifs[i]) = nPol(ifs[i]) > 2;
134 }
135
136 const Table table = in->table();
137
138 // Create the output file and write static data.
139 Int status;
140 status = writer_->create(String(filename), hdr.observer, hdr.project,
141 hdr.antennaname, hdr.antennaposition,
142 hdr.obstype, hdr.equinox, hdr.freqref,
143 nChan, nPol, havexpol, False, fluxUnit);
144 if ( status ) {
145 throw(AipsError("Failed to create output file"));
146 }
147
148 Double srcVel;
149
150 String fieldName, srcName, tcalTime;
151 Vector<Float> calFctr, sigma, tcal, tsys;
152 Vector<Double> direction(2), scanRate(2), srcDir(2), srcPM(2);
153 Matrix<Float> spectra;
154 Matrix<uChar> flagtra;
155 Complex xCalFctr;
156 Int count = 0;
157 Int scanno = 1;
158 // use spearate iterators to ensure renumbering of all numbers
159 TableIterator scanit(table, "SCANNO");
160 while (!scanit.pastEnd() ) {
161 Table stable = scanit.table();
162 TableIterator beamit(stable, "BEAMNO");
163 Int beamno = 1;
164 while (!beamit.pastEnd() ) {
165 Table btable = beamit.table();
166 // position only varies by beam
167 // No, we want to pointing data which varies by cycle!
168 MDirection::ScalarColumn dirCol(btable, "DIRECTION");
169 Vector<Double> direction = dirCol(0).getAngle("rad").getValue();
170 TableIterator cycit(btable, "CYCLENO");
171 ROArrayColumn<Double> srateCol(btable, "SCANRATE");
172 srateCol.get(0, scanRate);
173 ROArrayColumn<Double> spmCol(btable, "SRCPROPERMOTION");
174 spmCol.get(0, srcPM);
175 ROArrayColumn <Double> sdirCol(btable, "SRCDIRECTION");
176 sdirCol.get(0, srcDir);
177 ROScalarColumn<Double> svelCol(btable, "SRCVELOCITY");
178 svelCol.get(0, srcVel);
179 ROScalarColumn<uInt> bCol(btable, "BEAMNO");
180 beamno = bCol(0)+1;
181 Int cycno = 1;
182 while (!cycit.pastEnd() ) {
183 Table ctable = cycit.table();
184 //MDirection::ScalarColumn dirCol(ctable, "DIRECTION");
185 //Vector<Double> direction = dirCol(0).getAngle("rad").getValue();
186 TableIterator ifit(ctable, "IFNO");
187 Int 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 ifno = rec.asuInt("IFNO")+1;
195 uInt nchan = specCol(0).nelements();
196 //Double cdelt,crval,crpix, restfreq;
197 Double cdelt,crval,crpix;
198 Vector<Double> restfreq;
199 Float focusAxi, focusTan, focusRot,
200 temperature, pressure, humidity, windSpeed, windAz;
201 Float tmp0,tmp1,tmp2,tmp3,tmp4;
202 Vector<Float> tcalval;
203 //String stmp0,stmp1, tcalt;
204 String tcalt;
205 Vector<String> stmp0, stmp1;
206 in->frequencies().getEntry(crpix,crval,cdelt, rec.asuInt("FREQ_ID"));
207 in->focus().getEntry(focusAxi, focusTan, focusRot,
208 tmp0,tmp1,tmp2,tmp3,tmp4,
209 rec.asuInt("FOCUS_ID"));
210 in->molecules().getEntry(restfreq,stmp0,stmp1,rec.asuInt("MOLECULE_ID"));
211 in->tcal().getEntry(tcalt,tcalval,rec.asuInt("TCAL_ID"));
212 in->weather().getEntry(temperature, pressure, humidity,
213 windSpeed, windAz,
214 rec.asuInt("WEATHER_ID"));
215 Double pixel = Double(nchan/2);
216 Double refFreqNew = (pixel-crpix)*cdelt + crval;
217 // ok, now we have nrows for the n polarizations in this table
218 Matrix<Float> specs;
219 Matrix<uChar> flags;
220 Vector<Complex> xpol;
221 polConversion(specs, flags, xpol, itable);
222 Vector<Float> tsys = tsysFromTable(itable);
223 // dummy data
224 //uInt npol;
225 //if ( hdr.antennaname == "GBT" ) {
226 // npol = nPolUsed;
227 //}
228 //else {
229 // npol = specs.ncolumn();
230 //}
231 uInt npol = specs.ncolumn();
232
233 Matrix<Float> baseLin(npol,2, 0.0f);
234 Matrix<Float> baseSub(npol,9, 0.0f);
235 Complex xCalFctr;
236 Vector<Double> scanRate(2, 0.0);
237 Vector<Float> sigma(npol, 0.0f);
238 Vector<Float> calFctr(npol, 0.0f);
239 status = writer_->write(scanno, cycno, rec.asDouble("TIME"),
240 rec.asDouble("INTERVAL"),
241 rec.asString("FIELDNAME"),
242 rec.asString("SRCNAME"),
243 srcDir, srcPM, srcVel,hdr.obstype,
244 ifno,
245 refFreqNew, nchan*abs(cdelt), cdelt,
246 restfreq,
247 tcalval,
248 tcalt,
249 rec.asFloat("AZIMUTH"),
250 rec.asFloat("ELEVATION"),
251 rec.asFloat("PARANGLE"),
252 focusAxi, focusTan, focusRot,
253 temperature,
254 pressure, humidity, windSpeed, windAz,
255 rec.asInt("REFBEAMNO")+1, beamno,
256 direction,
257 scanRate,
258 tsys,
259 sigma, calFctr,// not in scantable
260 baseLin, baseSub,// not in scantable
261 specs, flags,
262 xCalFctr,//
263 xpol);
264 if ( status ) {
265 writer_->close();
266 throw(AipsError("STWriter: Failed to export Scantable."));
267 }
268 ++count;
269 ++ifno;
270 ++ifit;
271 }
272 ++cycno;
273 ++cycit;
274 }
275 ++beamno;
276 ++beamit;
277 }
278 ++scanno;
279 ++scanit;
280 }
281 ostringstream oss;
282 oss << "STWriter: wrote " << count << " rows to " << filename;
283 pushLog(String(oss));
284 writer_->close();
285 //if MS2 delete POINTING table exists and copy the one in the keyword
286 if ( format_ == "MS2" ) {
287 replacePtTab(table, filename);
288 }
289 return 0;
290}
291
292Vector<Float> STWriter::tsysFromTable(const Table& tab)
293{
294 ROArrayColumn<Float> tsysCol(tab, "TSYS");
295 Vector<Float> out(tab.nrow());
296 Vector<Float> tmp;
297 for (uInt i=0; i<tab.nrow(); ++i) {
298 tmp.resize();
299 tmp = tsysCol(i);
300 out[i] = tmp[0];
301 }
302 return out;
303}
304
305void STWriter::polConversion( Matrix< Float >& specs, Matrix< uChar >& flags,
306 Vector< Complex > & xpol, const Table & tab )
307{
308 String poltype = tab.keywordSet().asString("POLTYPE");
309 if ( poltype == "stokes") {
310 String msg = "poltype = " + poltype + " not yet supported in output.";
311 throw(AipsError(msg));
312 }
313 ROArrayColumn<Float> specCol(tab, "SPECTRA");
314 ROArrayColumn<uChar> flagCol(tab, "FLAGTRA");
315 uInt nchan = specCol(0).nelements();
316 uInt ncol = (tab.nrow()==1 ? 1: 2 );
317 specs.resize(nchan, ncol);
318 flags.resize(nchan, ncol);
319 // the linears
320 for (uInt i=0; i<ncol; ++i) {
321 specs.column(i) = specCol(i);
322 flags.column(i) = flagCol(i);
323 }
324 // now the complex if exists
325 Bool hasxpol = False;
326 xpol.resize();
327 if ( tab.nrow() == 4 ) {
328 hasxpol = True;
329 xpol.resize(nchan);
330 Vector<Float> reals, imags;
331 reals = specCol(2); imags = specCol(3);
332 for (uInt k=0; k < nchan; ++k) {
333 xpol[k] = Complex(reals[k], imags[k]);
334 }
335 }
336}
337
338// For writing MS data, if there is the reference to
339// original pointing table it replace it by it.
340void STWriter::replacePtTab (const Table& tab, const std::string& fname)
341{
342 String oldPtTabName = fname;
343 oldPtTabName.append("/POINTING");
344 if ( tab.keywordSet().isDefined("POINTING") ) {
345 String PointingTab = tab.keywordSet().asString("POINTING");
346 if ( Table::isReadable(PointingTab) ) {
347 Table newPtTab(PointingTab, Table::Old);
348 newPtTab.copy(oldPtTabName, Table::New);
349 ostringstream oss;
350 oss << "STWriter: copied " <<PointingTab << " to " << fname;
351 pushLog(String(oss));
352 }
353 }
354}
355
356}
Note: See TracBrowser for help on using the repository browser.