[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 996 2006-04-06 03:45:58Z 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] | 55 | using namespace casa;
|
---|
[827] | 56 | namespace asap {
|
---|
[28] | 57 |
|
---|
[822] | 58 | STWriter::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] | 76 | STWriter::~STWriter()
|
---|
[28] | 77 | {
|
---|
[988] | 78 | if (writer_) {
|
---|
| 79 | delete writer_;
|
---|
[192] | 80 | }
|
---|
[28] | 81 | }
|
---|
| 82 |
|
---|
[822] | 83 | Int 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] | 106 | Int 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);
|
---|
[996] | 143 | status = writer_->create(filename, hdr.observer, hdr.project,
|
---|
[28] | 144 | hdr.antennaname, hdr.antennaposition,
|
---|
| 145 | hdr.obstype, hdr.equinox, hdr.freqref,
|
---|
[996] | 146 | nChan, nPol, False, havexpol);
|
---|
| 147 | if ( status ) {
|
---|
[717] | 148 | throw(AipsError("Failed to create output file"));
|
---|
[28] | 149 | }
|
---|
| 150 |
|
---|
[822] | 151 | Double srcVel = 0.0;
|
---|
| 152 |
|
---|
| 153 | String fieldName, srcName, tcalTime;
|
---|
| 154 | Vector<Float> calFctr, sigma, tcal, tsys;
|
---|
| 155 | Vector<Double> direction(2), scanRate(2), srcDir(2), srcPM(2,0.0);
|
---|
| 156 | Matrix<Float> spectra;
|
---|
| 157 | Matrix<uChar> flagtra;
|
---|
| 158 | Complex xCalFctr;
|
---|
[28] | 159 | Int count = 0;
|
---|
[822] | 160 | Int scanno = 1;
|
---|
| 161 | // use spearate iterators to ensure renumbering of all numbers
|
---|
| 162 | TableIterator scanit(table, "SCANNO");
|
---|
| 163 | while (!scanit.pastEnd() ) {
|
---|
| 164 | Table stable = scanit.table();
|
---|
[988] | 165 | TableIterator beamit(stable, "BEAMNO");
|
---|
[822] | 166 | Int beamno = 1;
|
---|
| 167 | while (!beamit.pastEnd() ) {
|
---|
| 168 | Table btable = beamit.table();
|
---|
[827] | 169 | // position only varies by beam
|
---|
| 170 | MDirection::ScalarColumn dirCol(btable, "DIRECTION");
|
---|
| 171 | Vector<Double> direction = dirCol(0).getAngle("rad").getValue();
|
---|
[988] | 172 | TableIterator cycit(btable, "CYCLENO");
|
---|
| 173 | Int cycno = 1;
|
---|
| 174 | while (!cycit.pastEnd() ) {
|
---|
| 175 | Table ctable = cycit.table();
|
---|
| 176 | TableIterator ifit(ctable, "IFNO");
|
---|
| 177 | Int ifno = 1;
|
---|
| 178 | while (!ifit.pastEnd() ) {
|
---|
| 179 | Table itable = ifit.table();
|
---|
| 180 | TableRow row(itable);
|
---|
[822] | 181 | // use the first row to fill in all the "metadata"
|
---|
[827] | 182 | const TableRecord& rec = row.get(0);
|
---|
[988] | 183 | ROArrayColumn<Float> specCol(itable, "SPECTRA");
|
---|
[822] | 184 | uInt nchan = specCol(0).nelements();
|
---|
| 185 | Double cdelt,crval,crpix, restfreq;
|
---|
[827] | 186 | Float focusAxi, focusTan, focusRot,
|
---|
| 187 | temperature, pressure, humidity, windSpeed, windAz;
|
---|
[988] | 188 | Float tmp0,tmp1,tmp2,tmp3,tmp4;
|
---|
[827] | 189 | Vector<Float> tcalval;
|
---|
[988] | 190 | String stmp0,stmp1, tcalt;
|
---|
[822] | 191 | in->frequencies().getEntry(crpix,crval,cdelt, rec.asuInt("FREQ_ID"));
|
---|
[988] | 192 | in->focus().getEntry(focusAxi, focusTan, focusRot,
|
---|
| 193 | tmp0,tmp1,tmp2,tmp3,tmp4,
|
---|
| 194 | rec.asuInt("FOCUS_ID"));
|
---|
| 195 | in->molecules().getEntry(restfreq,stmp0,stmp1,rec.asuInt("MOLECULE_ID"));
|
---|
[827] | 196 | in->tcal().getEntry(tcalt,tcalval,rec.asuInt("TCAL_ID"));
|
---|
| 197 | in->weather().getEntry(temperature, pressure, humidity,
|
---|
| 198 | windSpeed, windAz,
|
---|
| 199 | rec.asuInt("WEATHER_ID"));
|
---|
[822] | 200 | Double pixel = Double(nchan/2);
|
---|
| 201 | Double refFreqNew = (pixel-crpix)*cdelt + crval;
|
---|
| 202 | // ok, now we have nrows for the n polarizations in this table
|
---|
| 203 | Matrix<Float> specs;
|
---|
| 204 | Matrix<uChar> flags;
|
---|
| 205 | Vector<Complex> xpol;
|
---|
[988] | 206 | polConversion(specs, flags, xpol, itable);
|
---|
| 207 | Vector<Float> tsys = tsysFromTable(itable);
|
---|
[822] | 208 | // dummy data
|
---|
[827] | 209 | uInt npol = specs.ncolumn();
|
---|
[988] | 210 |
|
---|
[822] | 211 | Matrix<Float> baseLin(npol,2, 0.0f);
|
---|
| 212 | Matrix<Float> baseSub(npol,9, 0.0f);
|
---|
| 213 | Complex xCalFctr;
|
---|
| 214 | Vector<Double> scanRate(2, 0.0);
|
---|
| 215 | Vector<Float> sigma(npol, 0.0f);
|
---|
| 216 | Vector<Float> calFctr(npol, 0.0f);
|
---|
[996] | 217 | status = writer_->write(scanno, cycno, rec.asDouble("TIME"),
|
---|
[822] | 218 | rec.asDouble("INTERVAL"),
|
---|
| 219 | rec.asString("FIELDNAME"),
|
---|
| 220 | rec.asString("SRCNAME"),
|
---|
[827] | 221 | direction,
|
---|
[822] | 222 | srcPM, srcVel, // not in scantable yet
|
---|
| 223 | ifno,
|
---|
| 224 | refFreqNew, nchan*abs(cdelt), cdelt,
|
---|
| 225 | restfreq,
|
---|
[827] | 226 | tcal,
|
---|
| 227 | tcalt,
|
---|
[822] | 228 | rec.asFloat("AZIMUTH"),
|
---|
| 229 | rec.asFloat("ELEVATION"),
|
---|
| 230 | rec.asFloat("PARANGLE"),
|
---|
[988] | 231 | focusAxi, focusTan, focusRot,
|
---|
[827] | 232 | temperature,
|
---|
| 233 | pressure, humidity, windSpeed, windAz,
|
---|
[988] | 234 | rec.asInt("REFBEAMNO")+1, beamno,
|
---|
[827] | 235 | direction,
|
---|
[822] | 236 | scanRate,// not in scantable
|
---|
[827] | 237 | tsys,
|
---|
[822] | 238 | sigma, calFctr,// not in scantable
|
---|
| 239 | baseLin, baseSub,// not in scantable
|
---|
| 240 | specs, flags,
|
---|
| 241 | xCalFctr,//
|
---|
[996] | 242 | xpol);
|
---|
| 243 | if ( status ) {
|
---|
[988] | 244 | writer_->close();
|
---|
| 245 | throw(AipsError("STWriter: Failed to export Scantable."));
|
---|
[822] | 246 | }
|
---|
[470] | 247 |
|
---|
[988] | 248 | ++ifno;
|
---|
| 249 | ++ifit;
|
---|
[470] | 250 | }
|
---|
[988] | 251 | ++cycno;
|
---|
| 252 | ++cycit;
|
---|
[28] | 253 | }
|
---|
[822] | 254 | ++beamno;
|
---|
| 255 | ++beamit;
|
---|
[28] | 256 | }
|
---|
[822] | 257 | ++scanno;
|
---|
| 258 | ++scanit;
|
---|
[28] | 259 | }
|
---|
[717] | 260 | ostringstream oss;
|
---|
[822] | 261 | oss << "STWriter: wrote " << count << " rows to " << filename << endl;
|
---|
[717] | 262 | pushLog(String(oss));
|
---|
[988] | 263 | writer_->close();
|
---|
[28] | 264 |
|
---|
| 265 | return 0;
|
---|
| 266 | }
|
---|
[470] | 267 |
|
---|
[827] | 268 | Vector<Float> STWriter::tsysFromTable(const Table& tab)
|
---|
| 269 | {
|
---|
| 270 | ROArrayColumn<Float> tsysCol(tab, "TSYS");
|
---|
| 271 | Vector<Float> out(tab.nrow());
|
---|
| 272 | Vector<Float> tmp;
|
---|
| 273 | for (uInt i=0; i<tab.nrow(); ++i) {
|
---|
[988] | 274 | tmp.resize();
|
---|
[827] | 275 | tmp = tsysCol(i);
|
---|
| 276 | out[i] = tmp[0];
|
---|
| 277 | }
|
---|
[988] | 278 | return out;
|
---|
[827] | 279 | }
|
---|
| 280 |
|
---|
| 281 | void STWriter::polConversion( Matrix< Float >& specs, Matrix< uChar >& flags,
|
---|
[822] | 282 | Vector< Complex > & xpol, const Table & tab )
|
---|
| 283 | {
|
---|
| 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 | }
|
---|
[827] | 289 | ROArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
| 290 | ROArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
[822] | 291 | uInt nchan = specCol(0).nelements();
|
---|
[988] | 292 | uInt ncol = (tab.nrow()==1 ? 1: 2 );
|
---|
| 293 | specs.resize(nchan, ncol);
|
---|
| 294 | flags.resize(nchan, ncol);
|
---|
[822] | 295 | // the linears
|
---|
[988] | 296 | for (uInt i=0; i<ncol; ++i) {
|
---|
[822] | 297 | specs.column(i) = specCol(i);
|
---|
| 298 | flags.column(i) = flagCol(i);
|
---|
| 299 | }
|
---|
| 300 | // now the complex if exists
|
---|
| 301 | Bool hasxpol = False;
|
---|
| 302 | xpol.resize();
|
---|
[827] | 303 | if ( tab.nrow() == 4 ) {
|
---|
[822] | 304 | hasxpol = True;
|
---|
| 305 | xpol.resize(nchan);
|
---|
| 306 | Vector<Float> reals, imags;
|
---|
| 307 | reals = specCol(2); imags = specCol(3);
|
---|
| 308 | for (uInt k=0; k < nchan; ++k) {
|
---|
[827] | 309 | xpol[k] = Complex(reals[k], imags[k]);
|
---|
[822] | 310 | }
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
[827] | 313 |
|
---|
| 314 |
|
---|
| 315 | }
|
---|