source: branches/hpc34/src/STWriter.cpp@ 2582

Last change on this file since 2582 was 2577, checked in by Takeshi Nakazato, 13 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Defined PKSMS option in cmake.
if PKSMS=ON, PKSMSreader/writer will be built while if PKSMS=OFF,
those classes will be ignored. Default is OFF.


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