[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 1280 2006-11-03 05:08:22Z 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== "ASCII") {
|
---|
[988] | 68 | writer_ = 0;
|
---|
[450] | 69 | } else {
|
---|
[988] | 70 | throw (AipsError("Unrecognized export format"));
|
---|
[28] | 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[822] | 74 | STWriter::~STWriter()
|
---|
[28] | 75 | {
|
---|
[988] | 76 | if (writer_) {
|
---|
| 77 | delete writer_;
|
---|
[192] | 78 | }
|
---|
[28] | 79 | }
|
---|
| 80 |
|
---|
[822] | 81 | Int 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] | 102 | Int 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;
|
---|
| 121 | int nIF = in->nif();
|
---|
| 122 | Vector<uInt> nPol(nIF),nChan(nIF);
|
---|
| 123 | Vector<Bool> havexpol(nIF);
|
---|
| 124 | for (int i=0;i<nIF;++i) {
|
---|
| 125 | nPol(i) = in->npol();
|
---|
| 126 | nChan(i) = in->nchan(i);
|
---|
| 127 | havexpol(i) = nPol(i) > 2;
|
---|
| 128 | }
|
---|
[28] | 129 |
|
---|
[324] | 130 | const Table table = in->table();
|
---|
| 131 |
|
---|
[28] | 132 | // Create the output file and write static data.
|
---|
| 133 | Int status;
|
---|
[1060] | 134 | status = writer_->create(String(filename), hdr.observer, hdr.project,
|
---|
[28] | 135 | hdr.antennaname, hdr.antennaposition,
|
---|
| 136 | hdr.obstype, hdr.equinox, hdr.freqref,
|
---|
[1060] | 137 | nChan, nPol, havexpol, False);
|
---|
[996] | 138 | if ( status ) {
|
---|
[717] | 139 | throw(AipsError("Failed to create output file"));
|
---|
[28] | 140 | }
|
---|
| 141 |
|
---|
[999] | 142 | Double srcVel;
|
---|
[822] | 143 |
|
---|
| 144 | String fieldName, srcName, tcalTime;
|
---|
| 145 | Vector<Float> calFctr, sigma, tcal, tsys;
|
---|
[999] | 146 | Vector<Double> direction(2), scanRate(2), srcDir(2), srcPM(2);
|
---|
[822] | 147 | Matrix<Float> spectra;
|
---|
| 148 | Matrix<uChar> flagtra;
|
---|
| 149 | Complex xCalFctr;
|
---|
[28] | 150 | Int count = 0;
|
---|
[822] | 151 | Int scanno = 1;
|
---|
| 152 | // use spearate iterators to ensure renumbering of all numbers
|
---|
| 153 | TableIterator scanit(table, "SCANNO");
|
---|
| 154 | while (!scanit.pastEnd() ) {
|
---|
| 155 | Table stable = scanit.table();
|
---|
[988] | 156 | TableIterator beamit(stable, "BEAMNO");
|
---|
[822] | 157 | Int beamno = 1;
|
---|
| 158 | while (!beamit.pastEnd() ) {
|
---|
| 159 | Table btable = beamit.table();
|
---|
[827] | 160 | // position only varies by beam
|
---|
| 161 | MDirection::ScalarColumn dirCol(btable, "DIRECTION");
|
---|
| 162 | Vector<Double> direction = dirCol(0).getAngle("rad").getValue();
|
---|
[988] | 163 | TableIterator cycit(btable, "CYCLENO");
|
---|
[999] | 164 | ROArrayColumn<Double> srateCol(btable, "SCANRATE");
|
---|
| 165 | srateCol.get(0, scanRate);
|
---|
| 166 | ROArrayColumn<Double> spmCol(btable, "SRCPROPERMOTION");
|
---|
| 167 | spmCol.get(0, srcPM);
|
---|
| 168 | ROArrayColumn <Double> sdirCol(btable, "SRCDIRECTION");
|
---|
| 169 | sdirCol.get(0, srcDir);
|
---|
| 170 | ROScalarColumn<Double> svelCol(btable, "SRCVELOCITY");
|
---|
| 171 | svelCol.get(0, srcVel);
|
---|
[988] | 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);
|
---|
[996] | 216 | status = writer_->write(scanno, cycno, rec.asDouble("TIME"),
|
---|
[822] | 217 | rec.asDouble("INTERVAL"),
|
---|
| 218 | rec.asString("FIELDNAME"),
|
---|
| 219 | rec.asString("SRCNAME"),
|
---|
[1072] | 220 | srcDir, srcPM, srcVel,hdr.obstype,
|
---|
[822] | 221 | ifno,
|
---|
| 222 | refFreqNew, nchan*abs(cdelt), cdelt,
|
---|
| 223 | restfreq,
|
---|
[827] | 224 | tcal,
|
---|
| 225 | tcalt,
|
---|
[822] | 226 | rec.asFloat("AZIMUTH"),
|
---|
| 227 | rec.asFloat("ELEVATION"),
|
---|
| 228 | rec.asFloat("PARANGLE"),
|
---|
[988] | 229 | focusAxi, focusTan, focusRot,
|
---|
[827] | 230 | temperature,
|
---|
| 231 | pressure, humidity, windSpeed, windAz,
|
---|
[988] | 232 | rec.asInt("REFBEAMNO")+1, beamno,
|
---|
[827] | 233 | direction,
|
---|
[999] | 234 | scanRate,
|
---|
[827] | 235 | tsys,
|
---|
[822] | 236 | sigma, calFctr,// not in scantable
|
---|
| 237 | baseLin, baseSub,// not in scantable
|
---|
| 238 | specs, flags,
|
---|
| 239 | xCalFctr,//
|
---|
[996] | 240 | xpol);
|
---|
| 241 | if ( status ) {
|
---|
[988] | 242 | writer_->close();
|
---|
| 243 | throw(AipsError("STWriter: Failed to export Scantable."));
|
---|
[822] | 244 | }
|
---|
[999] | 245 | ++count;
|
---|
[988] | 246 | ++ifno;
|
---|
| 247 | ++ifit;
|
---|
[470] | 248 | }
|
---|
[988] | 249 | ++cycno;
|
---|
| 250 | ++cycit;
|
---|
[28] | 251 | }
|
---|
[822] | 252 | ++beamno;
|
---|
| 253 | ++beamit;
|
---|
[28] | 254 | }
|
---|
[822] | 255 | ++scanno;
|
---|
| 256 | ++scanit;
|
---|
[28] | 257 | }
|
---|
[717] | 258 | ostringstream oss;
|
---|
[999] | 259 | oss << "STWriter: wrote " << count << " rows to " << filename;
|
---|
[717] | 260 | pushLog(String(oss));
|
---|
[988] | 261 | writer_->close();
|
---|
[28] | 262 |
|
---|
| 263 | return 0;
|
---|
| 264 | }
|
---|
[470] | 265 |
|
---|
[827] | 266 | Vector<Float> STWriter::tsysFromTable(const Table& tab)
|
---|
| 267 | {
|
---|
| 268 | ROArrayColumn<Float> tsysCol(tab, "TSYS");
|
---|
| 269 | Vector<Float> out(tab.nrow());
|
---|
| 270 | Vector<Float> tmp;
|
---|
| 271 | for (uInt i=0; i<tab.nrow(); ++i) {
|
---|
[988] | 272 | tmp.resize();
|
---|
[827] | 273 | tmp = tsysCol(i);
|
---|
| 274 | out[i] = tmp[0];
|
---|
| 275 | }
|
---|
[988] | 276 | return out;
|
---|
[827] | 277 | }
|
---|
| 278 |
|
---|
| 279 | void STWriter::polConversion( Matrix< Float >& specs, Matrix< uChar >& flags,
|
---|
[822] | 280 | Vector< Complex > & xpol, const Table & tab )
|
---|
| 281 | {
|
---|
| 282 | String poltype = tab.keywordSet().asString("POLTYPE");
|
---|
[1259] | 283 | if ( poltype == "stokes") {
|
---|
[822] | 284 | String msg = "poltype = " + poltype + " not yet supported in output.";
|
---|
[988] | 285 | throw(AipsError(msg));
|
---|
| 286 | }
|
---|
[827] | 287 | ROArrayColumn<Float> specCol(tab, "SPECTRA");
|
---|
| 288 | ROArrayColumn<uChar> flagCol(tab, "FLAGTRA");
|
---|
[822] | 289 | uInt nchan = specCol(0).nelements();
|
---|
[988] | 290 | uInt ncol = (tab.nrow()==1 ? 1: 2 );
|
---|
| 291 | specs.resize(nchan, ncol);
|
---|
| 292 | flags.resize(nchan, ncol);
|
---|
[822] | 293 | // the linears
|
---|
[988] | 294 | for (uInt i=0; i<ncol; ++i) {
|
---|
[822] | 295 | specs.column(i) = specCol(i);
|
---|
| 296 | flags.column(i) = flagCol(i);
|
---|
| 297 | }
|
---|
| 298 | // now the complex if exists
|
---|
| 299 | Bool hasxpol = False;
|
---|
| 300 | xpol.resize();
|
---|
[827] | 301 | if ( tab.nrow() == 4 ) {
|
---|
[822] | 302 | hasxpol = True;
|
---|
| 303 | xpol.resize(nchan);
|
---|
| 304 | Vector<Float> reals, imags;
|
---|
| 305 | reals = specCol(2); imags = specCol(3);
|
---|
| 306 | for (uInt k=0; k < nchan; ++k) {
|
---|
[827] | 307 | xpol[k] = Complex(reals[k], imags[k]);
|
---|
[822] | 308 | }
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
[827] | 311 |
|
---|
| 312 |
|
---|
| 313 | }
|
---|