source: trunk/src/STWriter.cpp@ 2695

Last change on this file since 2695 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
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 2658 2012-10-10 03:29:17Z MalteMarquarding $
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>
[2658]40#include <casa/Logging/LogIO.h>
[28]41
[1449]42#include <atnf/PKSIO/PKSrecord.h>
[28]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
[2657]51#include <fits/FITS/FITSSpectralUtil.h>
52
[1443]53#include "STFITSImageWriter.h"
[988]54#include "STAsciiWriter.h"
[901]55#include "STHeader.h"
[1575]56#include "STMath.h"
[324]57
[1575]58
[822]59#include "STWriter.h"
[28]60
[125]61using namespace casa;
[827]62namespace asap {
[28]63
[822]64STWriter::STWriter(const std::string &format)
[28]65{
[988]66 format_ = format;
67 String t(format_);
[450]68 t.upcase();
[1443]69 if (t == "MS2") {
[2577]70 throw (AipsError("MS2 OUTPUT FORMAT IS NO LONGER SUPPORTED"));
[1443]71 } else if (t == "SDFITS") {
[988]72 writer_ = new PKSSDwriter();
[1443]73 } else if (t == "ASCII" || t == "FITS" || t == "CLASS") {
[988]74 writer_ = 0;
[450]75 } else {
[988]76 throw (AipsError("Unrecognized export format"));
[28]77 }
78}
79
[822]80STWriter::~STWriter()
[28]81{
[988]82 if (writer_) {
83 delete writer_;
[192]84 }
[28]85}
86
[822]87Int STWriter::setFormat(const std::string &format)
[28]88{
[988]89 if (format != format_) {
90 if (writer_) delete writer_;
[450]91 }
[827]92
[988]93 format_ = format;
94 String t(format_);
[450]95 t.upcase();
96 if (t== "MS2") {
[2577]97 throw (AipsError("MS2 OUTPUT FORMAT IS NO LONGER SUPPORTED"));
[450]98 } else if (t== "SDFITS") {
[988]99 writer_ = new PKSSDwriter();
[1443]100 } else if (t == "ASCII" || t == "FITS" || t == "CLASS") {
[988]101 writer_ = 0;
[450]102 } else {
103 throw (AipsError("Unrecognized Format"));
[28]104 }
105 return 0;
106}
107
[827]108Int STWriter::write(const CountedPtr<Scantable> in,
109 const std::string &filename)
[28]110{
[1575]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.
[192]114
[2657]115 String freqframe;
116 FITSSpectralUtil::specsysFromFrame(freqframe,
117 in->frequencies().getFrame(true));
[1575]118 CountedPtr<Scantable> inst = in;
119 if (in->frequencies().getFrame(true) != in->frequencies().getFrame(false)) {
[2657]120 FITSSpectralUtil::specsysFromFrame(freqframe,
121 in->frequencies().getFrame(false));
122
[1575]123 STMath stm(false);
124 inst = stm.frequencyAlign(in);
125 }
126
[1280]127 if (format_=="ASCII") {
128 STAsciiWriter iw;
[1575]129 // ASCII calls SpectralCoordinate::toWorld so no freqAlign use 'in'
[1280]130 if (iw.write(*in, filename)) {
131 return 0;
132 } else {
133 return 1;
134 }
[1443]135 } else if ( format_ == "FITS" || format_ == "CLASS") {
136 STFITSImageWriter iw;
137 if (format_ == "CLASS") {
138 iw.setClass(True);
139 }
[1575]140 if (iw.write(*inst, filename)) {
[1443]141 return 0;
142 }
[199]143 }
[192]144
[827]145 // MS or SDFITS
[192]146
[28]147 // Extract the header from the table.
[1391]148 // this is a little different from what I have done
149 // before. Need to check with the Offline User Test data
[1575]150 STHeader hdr = inst->getHeader();
151 std::vector<uint> ifs = inst->getIFNos();
152 int nIF = inst->nif();//ifs.size();
[1060]153 Vector<uInt> nPol(nIF),nChan(nIF);
154 Vector<Bool> havexpol(nIF);
[1391]155 String fluxUnit = hdr.fluxunit;
[1688]156 fluxUnit.upcase();
[1295]157 nPol = 0;nChan = 0; havexpol = False;
[1305]158 for (uint i=0;i<ifs.size();++i) {
[2652]159 if ( inst->npol() > 2 ) {
160 havexpol(ifs[i]) = True;
161 nPol(ifs[i]) = 2;
162 } else {
163 nPol(ifs[i]) = inst->npol();
164 }
[1575]165 nChan(ifs[i]) = inst->nchan(ifs[i]);
[1060]166 }
[1819]167// Vector<String> obstypes(2);
168// obstypes(0) = "TR";//on
169// obstypes(1) = "RF TR";//off
[1575]170 const Table table = inst->table();
[324]171
[1663]172
[28]173 // Create the output file and write static data.
174 Int status;
[1060]175 status = writer_->create(String(filename), hdr.observer, hdr.project,
[2023]176 inst->getAntennaName(), hdr.antennaposition,
[1410]177 hdr.obstype, hdr.fluxunit,
[2657]178 hdr.equinox, freqframe,
[1410]179 nChan, nPol, havexpol, False);
[996]180 if ( status ) {
[717]181 throw(AipsError("Failed to create output file"));
[28]182 }
183
184 Int count = 0;
[1449]185 PKSrecord pksrec;
186 pksrec.scanNo = 1;
[822]187 // use spearate iterators to ensure renumbering of all numbers
188 TableIterator scanit(table, "SCANNO");
189 while (!scanit.pastEnd() ) {
190 Table stable = scanit.table();
[988]191 TableIterator beamit(stable, "BEAMNO");
[1449]192 pksrec.beamNo = 1;
[822]193 while (!beamit.pastEnd() ) {
194 Table btable = beamit.table();
[1819]195 //MDirection::ScalarColumn dirCol(btable, "DIRECTION");
196 //pksrec.direction = dirCol(0).getAngle("rad").getValue();
[988]197 TableIterator cycit(btable, "CYCLENO");
[999]198 ROArrayColumn<Double> srateCol(btable, "SCANRATE");
[1462]199 Vector<Double> sratedbl;
200 srateCol.get(0, sratedbl);
[1477]201 Vector<Float> srateflt(sratedbl.nelements());
[1462]202 convertArray(srateflt, sratedbl);
[1819]203 //pksrec.scanRate = srateflt;
204 pksrec.scanRate = sratedbl;
[999]205 ROArrayColumn<Double> spmCol(btable, "SRCPROPERMOTION");
[1449]206 spmCol.get(0, pksrec.srcPM);
[999]207 ROArrayColumn <Double> sdirCol(btable, "SRCDIRECTION");
[1449]208 sdirCol.get(0, pksrec.srcDir);
[999]209 ROScalarColumn<Double> svelCol(btable, "SRCVELOCITY");
[1449]210 svelCol.get(0, pksrec.srcVel);
[1295]211 ROScalarColumn<uInt> bCol(btable, "BEAMNO");
[1449]212 pksrec.beamNo = bCol(0)+1;
213 pksrec.cycleNo = 1;
[988]214 while (!cycit.pastEnd() ) {
215 Table ctable = cycit.table();
[2006]216 TableIterator typeit( ctable, "SRCTYPE" ) ;
217 while(!typeit.pastEnd() ) {
218 Table ttable = typeit.table() ;
[2652]219 TableIterator ifit(ttable, "IFNO",
220 TableIterator::Ascending, TableIterator::HeapSort);
[2006]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"));
[2652]239 inst->focus().getEntry(pksrec.parAngle, pksrec.focusAxi,
240 pksrec.focusTan,
[2006]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 }
[2004]271 }
[2006]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 }
[2004]282 }
[2006]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;
[2004]312 }
[2006]313 ++typeit ;
[470]314 }
[1449]315 ++pksrec.cycleNo;
[988]316 ++cycit;
[28]317 }
[1449]318 //++pksrec.beamNo;
[822]319 ++beamit;
[28]320 }
[1449]321 ++pksrec.scanNo;
[822]322 ++scanit;
[28]323 }
[2658]324 LogIO os( casa::LogOrigin("STWriter"));
325 os << "STWriter: wrote " << count << " rows to " << filename
326 << casa::LogIO::POST;
327
[988]328 writer_->close();
[2658]329
[28]330 return 0;
331}
[470]332
[827]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) {
[988]339 tmp.resize();
[827]340 tmp = tsysCol(i);
341 out[i] = tmp[0];
342 }
[988]343 return out;
[827]344}
345
346void STWriter::polConversion( Matrix< Float >& specs, Matrix< uChar >& flags,
[822]347 Vector< Complex > & xpol, const Table & tab )
348{
349 String poltype = tab.keywordSet().asString("POLTYPE");
[1664]350// Full stokes is not supported. Just allow stokes I
351 if ( poltype == "stokes" && tab.nrow() != 1) {
[822]352 String msg = "poltype = " + poltype + " not yet supported in output.";
[988]353 throw(AipsError(msg));
354 }
[827]355 ROArrayColumn<Float> specCol(tab, "SPECTRA");
356 ROArrayColumn<uChar> flagCol(tab, "FLAGTRA");
[822]357 uInt nchan = specCol(0).nelements();
[988]358 uInt ncol = (tab.nrow()==1 ? 1: 2 );
359 specs.resize(nchan, ncol);
360 flags.resize(nchan, ncol);
[822]361 // the linears
[988]362 for (uInt i=0; i<ncol; ++i) {
[822]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();
[827]369 if ( tab.nrow() == 4 ) {
[822]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) {
[827]375 xpol[k] = Complex(reals[k], imags[k]);
[822]376 }
377 }
378}
[827]379
380
[1819]381// get obsType string from SRCTYPE value
382String STWriter::getObsTypes( Int srctype )
383{
[2005]384 return SrcType::getName(srctype) ;
[827]385}
[1819]386
387}
Note: See TracBrowser for help on using the repository browser.