source: trunk/src/STWriter.cpp @ 988

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

implemented data export for the asap2 Scantable as in Ticket #4.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 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 988 2006-04-05 04:31:47Z mar637 $
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== "FITS") {
[988]68    writer_ = 0;
[450]69  } else if (t== "ASCII") {
[988]70    writer_ = 0;
[450]71  } else {
[988]72    throw (AipsError("Unrecognized export format"));
[28]73  }
74}
75
[822]76STWriter::~STWriter()
[28]77{
[988]78   if (writer_) {
79     delete writer_;
[192]80   }
[28]81}
82
[822]83Int STWriter::setFormat(const std::string &format)
[28]84{
[988]85  if (format != format_) {
86    if (writer_) delete writer_;
[450]87  }
[827]88
[988]89  format_ = format;
90  String t(format_);
[450]91  t.upcase();
92  if (t== "MS2") {
[988]93    writer_ = new PKSMS2writer();
[450]94  } else if (t== "SDFITS") {
[988]95    writer_ = new PKSSDwriter();
[450]96  } else if (t== "FITS") {
[988]97    writer_ = 0;
[450]98  } else if (t== "ASCII") {
[988]99    writer_ = 0;
[450]100  } else {
101    throw (AipsError("Unrecognized Format"));
[28]102  }
103  return 0;
104}
105
[827]106Int STWriter::write(const CountedPtr<Scantable> in,
107                    const std::string &filename)
[28]108{
[192]109
110// Image FITS
111
[988]112  if (format_=="FITS") {
[827]113//      Bool verbose = True;
114//      SDFITSImageWriter iw;
115//      if (iw.write(*in, filename, verbose)) {
116//         return 0;
117//      } else {
118//         return 1;
119//      }
[988]120  } else if (format_=="ASCII") {
121     STAsciiWriter iw;
[827]122     if (iw.write(*in, filename)) {
[199]123        return 0;
124     } else {
125        return 1;
[988]126     }
[199]127  }
[192]128
[827]129  // MS or SDFITS
[192]130
[28]131  // Extract the header from the table.
[901]132  STHeader hdr = in->getHeader();
[324]133  const Int nPol  = hdr.npol;
134  const Int nChan = hdr.nchan;
[28]135
[324]136  const Table table = in->table();
[827]137//   ROArrayColumn<uInt> freqIDCol(table, "FREQ_ID");
138//   Vector<uInt> freqIDs;
[324]139
[28]140  // Create the output file and write static data.
141  Int status;
[988]142  Bool havexpol = Bool(in->npol() > 2);
143  if (status = writer_->create(filename, hdr.observer, hdr.project,
[28]144                               hdr.antennaname, hdr.antennaposition,
145                               hdr.obstype, hdr.equinox, hdr.freqref,
[988]146                               nChan, nPol, False, havexpol)) {
[717]147    throw(AipsError("Failed to create output file"));
[28]148  }
149
[822]150  Double          srcVel = 0.0;
151
152  String          fieldName, srcName, tcalTime;
153  Vector<Float>   calFctr, sigma, tcal, tsys;
154  Vector<Double>  direction(2), scanRate(2), srcDir(2), srcPM(2,0.0);
155  Matrix<Float>   spectra;
156  Matrix<uChar>   flagtra;
157  Complex         xCalFctr;
[28]158  Int count = 0;
[822]159  Int scanno = 1;
160  // use spearate iterators to ensure renumbering of all numbers
161  TableIterator scanit(table, "SCANNO");
162  while (!scanit.pastEnd() ) {
163    Table stable = scanit.table();
[988]164    TableIterator beamit(stable, "BEAMNO");
[822]165    Int beamno = 1;
166    while (!beamit.pastEnd() ) {
167      Table btable = beamit.table();
[827]168      // position only varies by beam
169      MDirection::ScalarColumn dirCol(btable, "DIRECTION");
170      Vector<Double> direction = dirCol(0).getAngle("rad").getValue();
[988]171      TableIterator cycit(btable, "CYCLENO");
172      Int cycno = 1;
173      while (!cycit.pastEnd() ) {
174        Table ctable = cycit.table();
175        TableIterator ifit(ctable, "IFNO");
176        Int ifno = 1;
177        while (!ifit.pastEnd() ) {
178          Table itable = ifit.table();
179          TableRow row(itable);
[822]180          // use the first row to fill in all the "metadata"
[827]181          const TableRecord& rec = row.get(0);
[988]182          ROArrayColumn<Float> specCol(itable, "SPECTRA");
[822]183          uInt nchan = specCol(0).nelements();
184          Double cdelt,crval,crpix, restfreq;
[827]185          Float focusAxi, focusTan, focusRot,
186                temperature, pressure, humidity, windSpeed, windAz;
[988]187          Float tmp0,tmp1,tmp2,tmp3,tmp4;
[827]188          Vector<Float> tcalval;
[988]189          String stmp0,stmp1, tcalt;
[822]190          in->frequencies().getEntry(crpix,crval,cdelt, rec.asuInt("FREQ_ID"));
[988]191          in->focus().getEntry(focusAxi, focusTan, focusRot,
192                               tmp0,tmp1,tmp2,tmp3,tmp4,
193                               rec.asuInt("FOCUS_ID"));
194          in->molecules().getEntry(restfreq,stmp0,stmp1,rec.asuInt("MOLECULE_ID"));
[827]195          in->tcal().getEntry(tcalt,tcalval,rec.asuInt("TCAL_ID"));
196          in->weather().getEntry(temperature, pressure, humidity,
197                                 windSpeed, windAz,
198                                 rec.asuInt("WEATHER_ID"));
[822]199          Double pixel = Double(nchan/2);
200          Double refFreqNew = (pixel-crpix)*cdelt + crval;
201          // ok, now we have nrows for the n polarizations in this table
202          Matrix<Float> specs;
203          Matrix<uChar> flags;
204          Vector<Complex> xpol;
[988]205          polConversion(specs, flags, xpol, itable);
206          Vector<Float> tsys = tsysFromTable(itable);
[822]207          // dummy data
[827]208          uInt npol = specs.ncolumn();
[988]209
[822]210          Matrix<Float>   baseLin(npol,2, 0.0f);
211          Matrix<Float>   baseSub(npol,9, 0.0f);
212          Complex         xCalFctr;
213          Vector<Double>  scanRate(2, 0.0);
214          Vector<Float>   sigma(npol, 0.0f);
215          Vector<Float>   calFctr(npol, 0.0f);
[988]216          if (status = writer_->write(scanno, cycno, rec.asDouble("TIME"),
[822]217                                      rec.asDouble("INTERVAL"),
218                                      rec.asString("FIELDNAME"),
219                                      rec.asString("SRCNAME"),
[827]220                                      direction,
[822]221                                      srcPM, srcVel, // not in scantable yet
222                                      ifno,
223                                      refFreqNew, nchan*abs(cdelt), cdelt,
224                                      restfreq,
[827]225                                      tcal,
226                                      tcalt,
[822]227                                      rec.asFloat("AZIMUTH"),
228                                      rec.asFloat("ELEVATION"),
229                                      rec.asFloat("PARANGLE"),
[988]230                                      focusAxi, focusTan, focusRot,
[827]231                                      temperature,
232                                      pressure, humidity, windSpeed, windAz,
[988]233                                      rec.asInt("REFBEAMNO")+1, beamno,
[827]234                                      direction,
[822]235                                      scanRate,// not in scantable
[827]236                                      tsys,
[822]237                                      sigma, calFctr,// not in scantable
238                                      baseLin, baseSub,// not in scantable
239                                      specs, flags,
240                                      xCalFctr,//
241                                      xpol)
242                                      ) {
[988]243            writer_->close();
244            throw(AipsError("STWriter: Failed to export Scantable."));
[822]245          }
[470]246
[988]247          ++ifno;
248          ++ifit;
[470]249        }
[988]250        ++cycno;
251        ++cycit;
[28]252      }
[822]253      ++beamno;
254      ++beamit;
[28]255    }
[822]256    ++scanno;
257    ++scanit;
[28]258  }
[717]259  ostringstream oss;
[822]260  oss << "STWriter: wrote " << count << " rows to " << filename << endl;
[717]261  pushLog(String(oss));
[988]262  writer_->close();
[28]263
264  return 0;
265}
[470]266
[827]267Vector<Float> STWriter::tsysFromTable(const Table& tab)
268{
269  ROArrayColumn<Float> tsysCol(tab, "TSYS");
270  Vector<Float> out(tab.nrow());
271  Vector<Float> tmp;
272  for (uInt i=0; i<tab.nrow(); ++i) {
[988]273    tmp.resize();
[827]274    tmp = tsysCol(i);
275    out[i] = tmp[0];
276  }
[988]277  return out;
[827]278}
279
280void STWriter::polConversion( Matrix< Float >& specs, Matrix< uChar >& flags,
[822]281                              Vector< Complex > & xpol, const Table & tab )
282{
283  TableRow row(tab);
284  String poltype = tab.keywordSet().asString("POLTYPE");
[988]285  if ( poltype != "linear") {
[822]286    String msg = "poltype = " + poltype + " not yet supported in output.";
[988]287    throw(AipsError(msg));
288  }
[822]289  // use the first row to fill in all the "metadata"
[827]290  const TableRecord& rec = row.get(0);
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.