source: trunk/src/STWriter.cpp@ 3012

Last change on this file since 3012 was 2658, checked in by Malte Marquarding, 12 years ago

Ticket #199: Excised Logger::pushLog; now everything is using LogIO

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