[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 1391 2007-07-30 01:59:36Z MalteMarquarding $ |
---|
| 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. |
---|
[1391] | 118 | // this is a little different from what I have done |
---|
| 119 | // before. Need to check with the Offline User Test data |
---|
[901] | 120 | STHeader hdr = in->getHeader(); |
---|
[1060] | 121 | //const Int nPol = hdr.npol; |
---|
| 122 | //const Int nChan = hdr.nchan; |
---|
[1295] | 123 | std::vector<uint> ifs = in->getIFNos(); |
---|
| 124 | int nIF = in->nif();//ifs.size(); |
---|
[1060] | 125 | Vector<uInt> nPol(nIF),nChan(nIF); |
---|
| 126 | Vector<Bool> havexpol(nIF); |
---|
[1391] | 127 | String fluxUnit = hdr.fluxunit; |
---|
| 128 | |
---|
[1295] | 129 | nPol = 0;nChan = 0; havexpol = False; |
---|
[1305] | 130 | for (uint i=0;i<ifs.size();++i) { |
---|
[1295] | 131 | nPol(ifs[i]) = in->npol(); |
---|
| 132 | nChan(ifs[i]) = in->nchan(ifs[i]); |
---|
| 133 | havexpol(ifs[i]) = nPol(ifs[i]) > 2; |
---|
[1060] | 134 | } |
---|
[28] | 135 | |
---|
[324] | 136 | const Table table = in->table(); |
---|
| 137 | |
---|
[28] | 138 | // Create the output file and write static data. |
---|
| 139 | Int status; |
---|
[1060] | 140 | status = writer_->create(String(filename), hdr.observer, hdr.project, |
---|
[28] | 141 | hdr.antennaname, hdr.antennaposition, |
---|
| 142 | hdr.obstype, hdr.equinox, hdr.freqref, |
---|
[1060] | 143 | nChan, nPol, havexpol, False); |
---|
[996] | 144 | if ( status ) { |
---|
[717] | 145 | throw(AipsError("Failed to create output file")); |
---|
[28] | 146 | } |
---|
| 147 | |
---|
[999] | 148 | Double srcVel; |
---|
[822] | 149 | |
---|
| 150 | String fieldName, srcName, tcalTime; |
---|
| 151 | Vector<Float> calFctr, sigma, tcal, tsys; |
---|
[999] | 152 | Vector<Double> direction(2), scanRate(2), srcDir(2), srcPM(2); |
---|
[822] | 153 | Matrix<Float> spectra; |
---|
| 154 | Matrix<uChar> flagtra; |
---|
| 155 | Complex xCalFctr; |
---|
[28] | 156 | Int count = 0; |
---|
[822] | 157 | Int scanno = 1; |
---|
| 158 | // use spearate iterators to ensure renumbering of all numbers |
---|
| 159 | TableIterator scanit(table, "SCANNO"); |
---|
| 160 | while (!scanit.pastEnd() ) { |
---|
| 161 | Table stable = scanit.table(); |
---|
[988] | 162 | TableIterator beamit(stable, "BEAMNO"); |
---|
[822] | 163 | Int beamno = 1; |
---|
| 164 | while (!beamit.pastEnd() ) { |
---|
| 165 | Table btable = beamit.table(); |
---|
[988] | 166 | TableIterator cycit(btable, "CYCLENO"); |
---|
[999] | 167 | ROArrayColumn<Double> srateCol(btable, "SCANRATE"); |
---|
| 168 | srateCol.get(0, scanRate); |
---|
| 169 | ROArrayColumn<Double> spmCol(btable, "SRCPROPERMOTION"); |
---|
| 170 | spmCol.get(0, srcPM); |
---|
| 171 | ROArrayColumn <Double> sdirCol(btable, "SRCDIRECTION"); |
---|
| 172 | sdirCol.get(0, srcDir); |
---|
| 173 | ROScalarColumn<Double> svelCol(btable, "SRCVELOCITY"); |
---|
| 174 | svelCol.get(0, srcVel); |
---|
[1295] | 175 | ROScalarColumn<uInt> bCol(btable, "BEAMNO"); |
---|
| 176 | beamno = bCol(0)+1; |
---|
[988] | 177 | Int cycno = 1; |
---|
| 178 | while (!cycit.pastEnd() ) { |
---|
| 179 | Table ctable = cycit.table(); |
---|
| 180 | TableIterator ifit(ctable, "IFNO"); |
---|
[1390] | 181 | MDirection::ScalarColumn dirCol(ctable, "DIRECTION"); |
---|
| 182 | Vector<Double> direction = dirCol(0).getAngle("rad").getValue(); |
---|
[988] | 183 | Int ifno = 1; |
---|
| 184 | while (!ifit.pastEnd() ) { |
---|
| 185 | Table itable = ifit.table(); |
---|
| 186 | TableRow row(itable); |
---|
[822] | 187 | // use the first row to fill in all the "metadata" |
---|
[827] | 188 | const TableRecord& rec = row.get(0); |
---|
[988] | 189 | ROArrayColumn<Float> specCol(itable, "SPECTRA"); |
---|
[1295] | 190 | ifno = rec.asuInt("IFNO")+1; |
---|
[822] | 191 | uInt nchan = specCol(0).nelements(); |
---|
| 192 | Double cdelt,crval,crpix, restfreq; |
---|
[827] | 193 | Float focusAxi, focusTan, focusRot, |
---|
| 194 | temperature, pressure, humidity, windSpeed, windAz; |
---|
[988] | 195 | Float tmp0,tmp1,tmp2,tmp3,tmp4; |
---|
[827] | 196 | Vector<Float> tcalval; |
---|
[988] | 197 | String stmp0,stmp1, tcalt; |
---|
[822] | 198 | in->frequencies().getEntry(crpix,crval,cdelt, rec.asuInt("FREQ_ID")); |
---|
[988] | 199 | in->focus().getEntry(focusAxi, focusTan, focusRot, |
---|
| 200 | tmp0,tmp1,tmp2,tmp3,tmp4, |
---|
| 201 | rec.asuInt("FOCUS_ID")); |
---|
| 202 | in->molecules().getEntry(restfreq,stmp0,stmp1,rec.asuInt("MOLECULE_ID")); |
---|
[827] | 203 | in->tcal().getEntry(tcalt,tcalval,rec.asuInt("TCAL_ID")); |
---|
| 204 | in->weather().getEntry(temperature, pressure, humidity, |
---|
| 205 | windSpeed, windAz, |
---|
| 206 | rec.asuInt("WEATHER_ID")); |
---|
[822] | 207 | Double pixel = Double(nchan/2); |
---|
| 208 | Double refFreqNew = (pixel-crpix)*cdelt + crval; |
---|
| 209 | // ok, now we have nrows for the n polarizations in this table |
---|
| 210 | Matrix<Float> specs; |
---|
| 211 | Matrix<uChar> flags; |
---|
| 212 | Vector<Complex> xpol; |
---|
[988] | 213 | polConversion(specs, flags, xpol, itable); |
---|
| 214 | Vector<Float> tsys = tsysFromTable(itable); |
---|
[822] | 215 | // dummy data |
---|
[827] | 216 | uInt npol = specs.ncolumn(); |
---|
[988] | 217 | |
---|
[822] | 218 | Matrix<Float> baseLin(npol,2, 0.0f); |
---|
| 219 | Matrix<Float> baseSub(npol,9, 0.0f); |
---|
| 220 | Complex xCalFctr; |
---|
| 221 | Vector<Double> scanRate(2, 0.0); |
---|
| 222 | Vector<Float> sigma(npol, 0.0f); |
---|
| 223 | Vector<Float> calFctr(npol, 0.0f); |
---|
[996] | 224 | status = writer_->write(scanno, cycno, rec.asDouble("TIME"), |
---|
[822] | 225 | rec.asDouble("INTERVAL"), |
---|
| 226 | rec.asString("FIELDNAME"), |
---|
| 227 | rec.asString("SRCNAME"), |
---|
[1072] | 228 | srcDir, srcPM, srcVel,hdr.obstype, |
---|
[822] | 229 | ifno, |
---|
| 230 | refFreqNew, nchan*abs(cdelt), cdelt, |
---|
| 231 | restfreq, |
---|
[827] | 232 | tcal, |
---|
| 233 | tcalt, |
---|
[822] | 234 | rec.asFloat("AZIMUTH"), |
---|
| 235 | rec.asFloat("ELEVATION"), |
---|
| 236 | rec.asFloat("PARANGLE"), |
---|
[988] | 237 | focusAxi, focusTan, focusRot, |
---|
[827] | 238 | temperature, |
---|
| 239 | pressure, humidity, windSpeed, windAz, |
---|
[988] | 240 | rec.asInt("REFBEAMNO")+1, beamno, |
---|
[827] | 241 | direction, |
---|
[999] | 242 | scanRate, |
---|
[827] | 243 | tsys, |
---|
[822] | 244 | sigma, calFctr,// not in scantable |
---|
| 245 | baseLin, baseSub,// not in scantable |
---|
| 246 | specs, flags, |
---|
| 247 | xCalFctr,// |
---|
[996] | 248 | xpol); |
---|
| 249 | if ( status ) { |
---|
[988] | 250 | writer_->close(); |
---|
| 251 | throw(AipsError("STWriter: Failed to export Scantable.")); |
---|
[822] | 252 | } |
---|
[999] | 253 | ++count; |
---|
[1295] | 254 | //++ifno; |
---|
[988] | 255 | ++ifit; |
---|
[470] | 256 | } |
---|
[988] | 257 | ++cycno; |
---|
| 258 | ++cycit; |
---|
[28] | 259 | } |
---|
[1295] | 260 | //++beamno; |
---|
[822] | 261 | ++beamit; |
---|
[28] | 262 | } |
---|
[822] | 263 | ++scanno; |
---|
| 264 | ++scanit; |
---|
[28] | 265 | } |
---|
[717] | 266 | ostringstream oss; |
---|
[999] | 267 | oss << "STWriter: wrote " << count << " rows to " << filename; |
---|
[717] | 268 | pushLog(String(oss)); |
---|
[988] | 269 | writer_->close(); |
---|
[1391] | 270 | //if MS2 delete POINTING table exists and copy the one in the keyword |
---|
| 271 | if ( format_ == "MS2" ) { |
---|
| 272 | replacePtTab(table, filename); |
---|
| 273 | } |
---|
[28] | 274 | return 0; |
---|
| 275 | } |
---|
[470] | 276 | |
---|
[827] | 277 | Vector<Float> STWriter::tsysFromTable(const Table& tab) |
---|
| 278 | { |
---|
| 279 | ROArrayColumn<Float> tsysCol(tab, "TSYS"); |
---|
| 280 | Vector<Float> out(tab.nrow()); |
---|
| 281 | Vector<Float> tmp; |
---|
| 282 | for (uInt i=0; i<tab.nrow(); ++i) { |
---|
[988] | 283 | tmp.resize(); |
---|
[827] | 284 | tmp = tsysCol(i); |
---|
| 285 | out[i] = tmp[0]; |
---|
| 286 | } |
---|
[988] | 287 | return out; |
---|
[827] | 288 | } |
---|
| 289 | |
---|
| 290 | void STWriter::polConversion( Matrix< Float >& specs, Matrix< uChar >& flags, |
---|
[822] | 291 | Vector< Complex > & xpol, const Table & tab ) |
---|
| 292 | { |
---|
| 293 | String poltype = tab.keywordSet().asString("POLTYPE"); |
---|
[1259] | 294 | if ( poltype == "stokes") { |
---|
[822] | 295 | String msg = "poltype = " + poltype + " not yet supported in output."; |
---|
[988] | 296 | throw(AipsError(msg)); |
---|
| 297 | } |
---|
[827] | 298 | ROArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 299 | ROArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
[822] | 300 | uInt nchan = specCol(0).nelements(); |
---|
[988] | 301 | uInt ncol = (tab.nrow()==1 ? 1: 2 ); |
---|
| 302 | specs.resize(nchan, ncol); |
---|
| 303 | flags.resize(nchan, ncol); |
---|
[822] | 304 | // the linears |
---|
[988] | 305 | for (uInt i=0; i<ncol; ++i) { |
---|
[822] | 306 | specs.column(i) = specCol(i); |
---|
| 307 | flags.column(i) = flagCol(i); |
---|
| 308 | } |
---|
| 309 | // now the complex if exists |
---|
| 310 | Bool hasxpol = False; |
---|
| 311 | xpol.resize(); |
---|
[827] | 312 | if ( tab.nrow() == 4 ) { |
---|
[822] | 313 | hasxpol = True; |
---|
| 314 | xpol.resize(nchan); |
---|
| 315 | Vector<Float> reals, imags; |
---|
| 316 | reals = specCol(2); imags = specCol(3); |
---|
| 317 | for (uInt k=0; k < nchan; ++k) { |
---|
[827] | 318 | xpol[k] = Complex(reals[k], imags[k]); |
---|
[822] | 319 | } |
---|
| 320 | } |
---|
| 321 | } |
---|
[827] | 322 | |
---|
[1391] | 323 | // For writing MS data, if there is the reference to |
---|
| 324 | // original pointing table it replace it by it. |
---|
| 325 | void STWriter::replacePtTab (const Table& tab, const std::string& fname) |
---|
| 326 | { |
---|
| 327 | String oldPtTabName = fname; |
---|
| 328 | oldPtTabName.append("/POINTING"); |
---|
| 329 | if ( tab.keywordSet().isDefined("POINTING") ) { |
---|
| 330 | String PointingTab = tab.keywordSet().asString("POINTING"); |
---|
| 331 | if ( Table::isReadable(PointingTab) ) { |
---|
| 332 | Table newPtTab(PointingTab, Table::Old); |
---|
| 333 | newPtTab.copy(oldPtTabName, Table::New); |
---|
| 334 | ostringstream oss; |
---|
| 335 | oss << "STWriter: copied " <<PointingTab << " to " << fname; |
---|
| 336 | pushLog(String(oss)); |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | } |
---|
[827] | 340 | |
---|
| 341 | } |
---|