source: trunk/src/STWriter.cpp@ 1390

Last change on this file since 1390 was 1390, checked in by Malte Marquarding, 18 years ago

fix for scanning data, where direction can change per cycle not only per beam

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 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 1390 2007-07-27 01:29:54Z 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>
[28]40
41#include <atnf/PKSIO/PKSMS2writer.h>
42#include <atnf/PKSIO/PKSSDwriter.h>
43
[827]44#include <tables/Tables/Table.h>
45#include <tables/Tables/TableIter.h>
46#include <tables/Tables/TableRow.h>
[324]47#include <tables/Tables/ArrayColumn.h>
48
[827]49//#include "SDFITSImageWriter.h"
[988]50#include "STAsciiWriter.h"
[901]51#include "STHeader.h"
[324]52
[822]53#include "STWriter.h"
[28]54
[125]55using namespace casa;
[827]56namespace asap {
[28]57
[822]58STWriter::STWriter(const std::string &format)
[28]59{
[988]60 format_ = format;
61 String t(format_);
[450]62 t.upcase();
63 if (t== "MS2") {
[988]64 writer_ = new PKSMS2writer();
[450]65 } else if (t== "SDFITS") {
[988]66 writer_ = new PKSSDwriter();
[450]67 } else if (t== "ASCII") {
[988]68 writer_ = 0;
[450]69 } else {
[988]70 throw (AipsError("Unrecognized export format"));
[28]71 }
72}
73
[822]74STWriter::~STWriter()
[28]75{
[988]76 if (writer_) {
77 delete writer_;
[192]78 }
[28]79}
80
[822]81Int STWriter::setFormat(const std::string &format)
[28]82{
[988]83 if (format != format_) {
84 if (writer_) delete writer_;
[450]85 }
[827]86
[988]87 format_ = format;
88 String t(format_);
[450]89 t.upcase();
90 if (t== "MS2") {
[988]91 writer_ = new PKSMS2writer();
[450]92 } else if (t== "SDFITS") {
[988]93 writer_ = new PKSSDwriter();
[450]94 } else if (t== "ASCII") {
[988]95 writer_ = 0;
[450]96 } else {
97 throw (AipsError("Unrecognized Format"));
[28]98 }
99 return 0;
100}
101
[827]102Int STWriter::write(const CountedPtr<Scantable> in,
103 const std::string &filename)
[28]104{
[192]105
[1280]106 if (format_=="ASCII") {
107 STAsciiWriter iw;
108 if (iw.write(*in, filename)) {
109 return 0;
110 } else {
111 return 1;
112 }
[199]113 }
[192]114
[827]115 // MS or SDFITS
[192]116
[28]117 // Extract the header from the table.
[901]118 STHeader hdr = in->getHeader();
[1060]119 //const Int nPol = hdr.npol;
120 //const Int nChan = hdr.nchan;
[1295]121 std::vector<uint> ifs = in->getIFNos();
122 int nIF = in->nif();//ifs.size();
[1060]123 Vector<uInt> nPol(nIF),nChan(nIF);
124 Vector<Bool> havexpol(nIF);
[1295]125 nPol = 0;nChan = 0; havexpol = False;
[1305]126 for (uint i=0;i<ifs.size();++i) {
[1295]127 nPol(ifs[i]) = in->npol();
128 nChan(ifs[i]) = in->nchan(ifs[i]);
129 havexpol(ifs[i]) = nPol(ifs[i]) > 2;
[1060]130 }
[28]131
[324]132 const Table table = in->table();
133
[28]134 // Create the output file and write static data.
135 Int status;
[1060]136 status = writer_->create(String(filename), hdr.observer, hdr.project,
[28]137 hdr.antennaname, hdr.antennaposition,
138 hdr.obstype, hdr.equinox, hdr.freqref,
[1060]139 nChan, nPol, havexpol, False);
[996]140 if ( status ) {
[717]141 throw(AipsError("Failed to create output file"));
[28]142 }
143
[999]144 Double srcVel;
[822]145
146 String fieldName, srcName, tcalTime;
147 Vector<Float> calFctr, sigma, tcal, tsys;
[999]148 Vector<Double> direction(2), scanRate(2), srcDir(2), srcPM(2);
[822]149 Matrix<Float> spectra;
150 Matrix<uChar> flagtra;
151 Complex xCalFctr;
[28]152 Int count = 0;
[822]153 Int scanno = 1;
154 // use spearate iterators to ensure renumbering of all numbers
155 TableIterator scanit(table, "SCANNO");
156 while (!scanit.pastEnd() ) {
157 Table stable = scanit.table();
[988]158 TableIterator beamit(stable, "BEAMNO");
[822]159 Int beamno = 1;
160 while (!beamit.pastEnd() ) {
161 Table btable = beamit.table();
[988]162 TableIterator cycit(btable, "CYCLENO");
[999]163 ROArrayColumn<Double> srateCol(btable, "SCANRATE");
164 srateCol.get(0, scanRate);
165 ROArrayColumn<Double> spmCol(btable, "SRCPROPERMOTION");
166 spmCol.get(0, srcPM);
167 ROArrayColumn <Double> sdirCol(btable, "SRCDIRECTION");
168 sdirCol.get(0, srcDir);
169 ROScalarColumn<Double> svelCol(btable, "SRCVELOCITY");
170 svelCol.get(0, srcVel);
[1295]171 ROScalarColumn<uInt> bCol(btable, "BEAMNO");
172 beamno = bCol(0)+1;
[988]173 Int cycno = 1;
174 while (!cycit.pastEnd() ) {
175 Table ctable = cycit.table();
176 TableIterator ifit(ctable, "IFNO");
[1390]177 MDirection::ScalarColumn dirCol(ctable, "DIRECTION");
178 Vector<Double> direction = dirCol(0).getAngle("rad").getValue();
[988]179 Int ifno = 1;
180 while (!ifit.pastEnd() ) {
181 Table itable = ifit.table();
182 TableRow row(itable);
[822]183 // use the first row to fill in all the "metadata"
[827]184 const TableRecord& rec = row.get(0);
[988]185 ROArrayColumn<Float> specCol(itable, "SPECTRA");
[1295]186 ifno = rec.asuInt("IFNO")+1;
[822]187 uInt nchan = specCol(0).nelements();
188 Double cdelt,crval,crpix, restfreq;
[827]189 Float focusAxi, focusTan, focusRot,
190 temperature, pressure, humidity, windSpeed, windAz;
[988]191 Float tmp0,tmp1,tmp2,tmp3,tmp4;
[827]192 Vector<Float> tcalval;
[988]193 String stmp0,stmp1, tcalt;
[822]194 in->frequencies().getEntry(crpix,crval,cdelt, rec.asuInt("FREQ_ID"));
[988]195 in->focus().getEntry(focusAxi, focusTan, focusRot,
196 tmp0,tmp1,tmp2,tmp3,tmp4,
197 rec.asuInt("FOCUS_ID"));
198 in->molecules().getEntry(restfreq,stmp0,stmp1,rec.asuInt("MOLECULE_ID"));
[827]199 in->tcal().getEntry(tcalt,tcalval,rec.asuInt("TCAL_ID"));
200 in->weather().getEntry(temperature, pressure, humidity,
201 windSpeed, windAz,
202 rec.asuInt("WEATHER_ID"));
[822]203 Double pixel = Double(nchan/2);
204 Double refFreqNew = (pixel-crpix)*cdelt + crval;
205 // ok, now we have nrows for the n polarizations in this table
206 Matrix<Float> specs;
207 Matrix<uChar> flags;
208 Vector<Complex> xpol;
[988]209 polConversion(specs, flags, xpol, itable);
210 Vector<Float> tsys = tsysFromTable(itable);
[822]211 // dummy data
[827]212 uInt npol = specs.ncolumn();
[988]213
[822]214 Matrix<Float> baseLin(npol,2, 0.0f);
215 Matrix<Float> baseSub(npol,9, 0.0f);
216 Complex xCalFctr;
217 Vector<Double> scanRate(2, 0.0);
218 Vector<Float> sigma(npol, 0.0f);
219 Vector<Float> calFctr(npol, 0.0f);
[996]220 status = writer_->write(scanno, cycno, rec.asDouble("TIME"),
[822]221 rec.asDouble("INTERVAL"),
222 rec.asString("FIELDNAME"),
223 rec.asString("SRCNAME"),
[1072]224 srcDir, srcPM, srcVel,hdr.obstype,
[822]225 ifno,
226 refFreqNew, nchan*abs(cdelt), cdelt,
227 restfreq,
[827]228 tcal,
229 tcalt,
[822]230 rec.asFloat("AZIMUTH"),
231 rec.asFloat("ELEVATION"),
232 rec.asFloat("PARANGLE"),
[988]233 focusAxi, focusTan, focusRot,
[827]234 temperature,
235 pressure, humidity, windSpeed, windAz,
[988]236 rec.asInt("REFBEAMNO")+1, beamno,
[827]237 direction,
[999]238 scanRate,
[827]239 tsys,
[822]240 sigma, calFctr,// not in scantable
241 baseLin, baseSub,// not in scantable
242 specs, flags,
243 xCalFctr,//
[996]244 xpol);
245 if ( status ) {
[988]246 writer_->close();
247 throw(AipsError("STWriter: Failed to export Scantable."));
[822]248 }
[999]249 ++count;
[1295]250 //++ifno;
[988]251 ++ifit;
[470]252 }
[988]253 ++cycno;
254 ++cycit;
[28]255 }
[1295]256 //++beamno;
[822]257 ++beamit;
[28]258 }
[822]259 ++scanno;
260 ++scanit;
[28]261 }
[717]262 ostringstream oss;
[999]263 oss << "STWriter: wrote " << count << " rows to " << filename;
[717]264 pushLog(String(oss));
[988]265 writer_->close();
[28]266
267 return 0;
268}
[470]269
[827]270Vector<Float> STWriter::tsysFromTable(const Table& tab)
271{
272 ROArrayColumn<Float> tsysCol(tab, "TSYS");
273 Vector<Float> out(tab.nrow());
274 Vector<Float> tmp;
275 for (uInt i=0; i<tab.nrow(); ++i) {
[988]276 tmp.resize();
[827]277 tmp = tsysCol(i);
278 out[i] = tmp[0];
279 }
[988]280 return out;
[827]281}
282
283void STWriter::polConversion( Matrix< Float >& specs, Matrix< uChar >& flags,
[822]284 Vector< Complex > & xpol, const Table & tab )
285{
286 String poltype = tab.keywordSet().asString("POLTYPE");
[1259]287 if ( poltype == "stokes") {
[822]288 String msg = "poltype = " + poltype + " not yet supported in output.";
[988]289 throw(AipsError(msg));
290 }
[827]291 ROArrayColumn<Float> specCol(tab, "SPECTRA");
292 ROArrayColumn<uChar> flagCol(tab, "FLAGTRA");
[822]293 uInt nchan = specCol(0).nelements();
[988]294 uInt ncol = (tab.nrow()==1 ? 1: 2 );
295 specs.resize(nchan, ncol);
296 flags.resize(nchan, ncol);
[822]297 // the linears
[988]298 for (uInt i=0; i<ncol; ++i) {
[822]299 specs.column(i) = specCol(i);
300 flags.column(i) = flagCol(i);
301 }
302 // now the complex if exists
303 Bool hasxpol = False;
304 xpol.resize();
[827]305 if ( tab.nrow() == 4 ) {
[822]306 hasxpol = True;
307 xpol.resize(nchan);
308 Vector<Float> reals, imags;
309 reals = specCol(2); imags = specCol(3);
310 for (uInt k=0; k < nchan; ++k) {
[827]311 xpol[k] = Complex(reals[k], imags[k]);
[822]312 }
313 }
314}
[827]315
316
317}
Note: See TracBrowser for help on using the repository browser.