source: trunk/src/STWriter.cpp@ 3137

Last change on this file since 3137 was 3106, checked in by Takeshi Nakazato, 8 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes/No

Interface Changes: Yes/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...


Check-in asap modifications from Jim regarding casacore namespace conversion.

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