source: trunk/src/STWriter.cpp @ 901

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

SDContainer -> STHeader

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