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