source: trunk/src/STWriter.cpp@ 2315

Last change on this file since 2315 was 2023, checked in by Takeshi Nakazato, 14 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...

Change according to introducing MSFiller.


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