source: trunk/src/STWriter.cpp @ 822

Last change on this file since 822 was 822, checked in by mar637, 18 years ago

changes to use the new scantable. this revision is not working.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 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 822 2006-02-16 23:05:16Z mar637 $
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/PKSMS2writer.h>
42#include <atnf/PKSIO/PKSSDwriter.h>
43
44#include <tables/Tables/ArrayColumn.h>
45
46
47
48
49#include "SDContainer.h"
50#include "SDMemTable.h"
51#include "STWriter.h"
52#include "SDFITSImageWriter.h"
53#include "SDAsciiWriter.h"
54#include "SDPol.h"
55
56using namespace casa;
57using namespace asap;
58
59//--------------------------------------------------------- STWriter::STWriter
60
61// Default constructor.
62
63STWriter::STWriter(const std::string &format)
64{
65  cFormat = format;
66//
67  String t(cFormat);
68  t.upcase();
69  if (t== "MS2") {
70    cWriter = new PKSMS2writer();
71  } else if (t== "SDFITS") {
72    cWriter = new PKSSDwriter();
73  } else if (t== "FITS") {
74    cWriter = 0;
75  } else if (t== "ASCII") {
76    cWriter = 0;
77  } else {
78    throw (AipsError("Unrecognized Format"));
79  }
80}
81
82//-------------------------------------------------------- STWriter::~STWriter
83
84// Destructor.
85
86STWriter::~STWriter()
87{
88   if (cWriter) {
89     delete cWriter;
90   }
91}
92
93//-------------------------------------------------------- STWriter::setFormat
94
95// Reset the output format.
96
97Int STWriter::setFormat(const std::string &format)
98{
99  if (format != cFormat) {
100    if (cWriter) delete cWriter;
101  }
102//
103  cFormat = format;
104  String t(cFormat);
105  t.upcase();
106  if (t== "MS2") {
107    cWriter = new PKSMS2writer();
108  } else if (t== "SDFITS") {
109    cWriter = new PKSSDwriter();
110  } else if (t== "FITS") {
111    cWriter = 0;
112  } else if (t== "ASCII") {
113    cWriter = 0;
114  } else {
115    throw (AipsError("Unrecognized Format"));
116  }
117  return 0;
118}
119
120//------------------------------------------------------------ STWriter::write
121
122// Write an SDMemTable to file in the desired format, closing the file when
123// finished.
124
125Int STWriter::write(const CountedPtr<SDMemTable> in,
126                    const std::string &filename, Bool toStokes)
127{
128
129// Image FITS
130
131  if (cFormat=="FITS") {
132     Bool verbose = True;
133     SDFITSImageWriter iw;
134     if (iw.write(*in, filename, verbose, toStokes)) {
135        return 0;
136     } else {
137        return 1;
138     }
139  } else if (cFormat=="ASCII") {
140     SDAsciiWriter iw;
141     if (iw.write(*in, filename, toStokes)) {
142        return 0;
143     } else {
144        return 1;
145     }
146  }
147
148// MS or SDFITS
149
150  // Extract the header from the table.
151  SDHeader hdr = in->getSDHeader();
152  const Int nPol  = hdr.npol;
153  const Int nChan = hdr.nchan;
154
155// Get Freq table
156
157  SDFrequencyTable sdft = in->getSDFreqTable();
158  Vector<Double> restFreqs;
159  String restFreqUnit;
160  sdft.restFrequencies(restFreqs, restFreqUnit);
161  Double restFreq = 0.0;
162  if (restFreqs.nelements()>0) {
163     Quantum<Double> rF(restFreqs(0), Unit(restFreqUnit));
164     restFreq = rF.getValue(Unit("Hz"));
165  }
166
167// Table columns
168
169  const Table table = in->table();
170  ROArrayColumn<uInt> freqIDCol(table, "FREQID");
171  Vector<uInt> freqIDs;
172
173  // Create the output file and write static data.
174  Int status;
175  if (status = cWriter->create(filename, hdr.observer, hdr.project,
176                               hdr.antennaname, hdr.antennaposition,
177                               hdr.obstype, hdr.equinox, hdr.freqref,
178                               nChan, nPol, False, False)) {
179    throw(AipsError("Failed to create output file"));
180  }
181
182  Vector<Double>  srcPM(2, 0.0);
183  Double          srcVel = 0.0;
184
185  Array<Float> spectra, tSys, stokes;
186  Array<uChar> flags;
187  Bool doLinear = True;
188
189  String          fieldName, srcName, tcalTime;
190  Vector<Float>   calFctr, sigma, tcal, tsys;
191  Vector<Double>  direction(2), scanRate(2), srcDir(2), srcPM(2,0.0);
192  Matrix<Float>   spectra;
193  Matrix<uChar>   flagtra;
194  Complex         xCalFctr;
195  Int count = 0;
196  Int scanno = 1;
197  // use spearate iterators to ensure renumbering of all numbers
198  TableIterator scanit(table, "SCANNO");
199  while (!scanit.pastEnd() ) {
200    Table stable = scanit.table();
201    TableIterator beamit(table, "BEAMNO");
202    Int beamno = 1;
203    while (!beamit.pastEnd() ) {
204      Table btable = beamit.table();
205      TableIterator ifit(btable, "IFNO");
206      Int ifno = 1;
207      while (!ifit.pastEnd() ) {
208        Table itable = ifit.table();
209        TableIterator cycit(itable, "CYCLENO");
210        Int cycno = 1;
211        while (!cycit.pastEnd() ) {
212          Table ctable = cycit.table();
213          TableRow row(ctable);
214          // use the first row to fill in all the "metadata"
215          const TableRecord& rec = row.record(0);
216          ROArrayColumn<Float> specCol(ctable, "SPECTRA");
217          uInt nchan = specCol(0).nelements();
218          Double cdelt,crval,crpix, restfreq;
219          String tmp,tmp2;
220          in->frequencies().getEntry(crpix,crval,cdelt, rec.asuInt("FREQ_ID"));
221          in->molecules().getEntry(restfreq,tmp,tmp2,rec.asuInt("RESTFREQ_ID"));
222          Double pixel = Double(nchan/2);
223          Double refFreqNew = (pixel-crpix)*cdelt + crval;
224          // ok, now we have nrows for the n polarizations in this table
225          Matrix<Float> specs;
226          Matrix<uChar> flags;
227          Vector<Complex> xpol;
228          polConversion(specs, flags, xpol, ctable);
229          // dummy data
230          uInt nPol = specs.ncolumns();
231          Matrix<Float>   baseLin(npol,2, 0.0f);
232          Matrix<Float>   baseSub(npol,9, 0.0f);
233          Complex         xCalFctr;
234          Vector<Double>  scanRate(2, 0.0);
235          Vector<Float>   sigma(npol, 0.0f);
236          Vector<Float>   calFctr(npol, 0.0f);
237
238
239          if (status = cWriter->write(scano, cycno, rec.asDouble("TIME"),
240                                      rec.asDouble("INTERVAL"),
241                                      rec.asString("FIELDNAME"),
242                                      rec.asString("SRCNAME"),
243                                      direction ,//
244                                      srcPM, srcVel, // not in scantable yet
245                                      ifno,
246                                      refFreqNew, nchan*abs(cdelt), cdelt,
247                                      restfreq,
248                                      tcal,//
249                                      tcaltime,//
250                                      rec.asFloat("AZIMUTH"),
251                                      rec.asFloat("ELEVATION"),
252                                      rec.asFloat("PARANGLE"),
253                                      focusAxi, focusTan, focusRot,//
254                                      temperature,//
255                                      pressure, humidity, windSpeed, windAz,//
256                                      rec.asInt("REFBEAM"), beamno,
257                                      direction,//
258                                      scanRate,// not in scantable
259                                      tSys, //
260                                      sigma, calFctr,// not in scantable
261                                      baseLin, baseSub,// not in scantable
262                                      specs, flags,
263                                      xCalFctr,//
264                                      xpol)
265                                      ) {
266            cerr << "Error writing output file." << endl;
267            return 1;
268          }
269
270          ++cycno;
271          ++cycit;
272        }
273        ++ifno;
274        ++ifit;
275      }
276      ++beamno;
277      ++beamit;
278    }
279    ++scanno;
280    ++scanit;
281  }
282  ostringstream oss;
283  oss << "STWriter: wrote " << count << " rows to " << filename << endl;
284  pushLog(String(oss));
285  cWriter->close();
286
287  return 0;
288}
289
290void STWriter::polConversion( Matrix< Float >& specs, Matrix< Float >& flags,
291                              Vector< Complex > & xpol, const Table & tab )
292{
293  TableRow row(tab);
294  String poltype = tab.keywordSet().asString("POLTYPE");
295  if ( poltype != "linear")
296    String msg = "poltype = " + poltype + " not yet supported in output.";
297    throw(AipsError("msg"));
298  // use the first row to fill in all the "metadata"
299  const TableRecord& rec = row.record(0);
300  ROArrayColumn<Float> specCol(ctable, "SPECTRA");
301  ROArrayColumn<uChar> flagCol(ctable, "FLAGTRA");
302  uInt nchan = specCol(0).nelements();
303  uInt ncols = ( ctable.nrow()==1 ? 1: 2 );
304  specs.resize(nchan, ncols);
305  flags.resize(nchan, ncols);
306  // the linears
307  for (uInt i=0; i<ncols; ++i) {
308    specs.column(i) = specCol(i);
309    flags.column(i) = flagCol(i);
310  }
311  // now the complex if exists
312  Vector<Complex> xpol;
313  Bool hasxpol = False;
314  xpol.resize();
315  if ( ctable.nrow() == 4 ) {
316    hasxpol = True;
317    xpol.resize(nchan);
318    Vector<Float> reals, imags;
319    reals = specCol(2); imags = specCol(3);
320    for (uInt k=0; k < nchan; ++k) {
321  `   xpol[k] = Complex(reals[k], imags[k]);
322    }
323  }
324}
Note: See TracBrowser for help on using the repository browser.