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