[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 1688 2010-02-10 00:54:56Z 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 | |
---|
[1449] | 41 | #include <atnf/PKSIO/PKSrecord.h> |
---|
[28] | 42 | #include <atnf/PKSIO/PKSMS2writer.h> |
---|
| 43 | #include <atnf/PKSIO/PKSSDwriter.h> |
---|
| 44 | |
---|
[827] | 45 | #include <tables/Tables/Table.h> |
---|
| 46 | #include <tables/Tables/TableIter.h> |
---|
| 47 | #include <tables/Tables/TableRow.h> |
---|
[324] | 48 | #include <tables/Tables/ArrayColumn.h> |
---|
| 49 | |
---|
[1443] | 50 | #include "STFITSImageWriter.h" |
---|
[988] | 51 | #include "STAsciiWriter.h" |
---|
[901] | 52 | #include "STHeader.h" |
---|
[1575] | 53 | #include "STMath.h" |
---|
[324] | 54 | |
---|
[1575] | 55 | |
---|
[822] | 56 | #include "STWriter.h" |
---|
[28] | 57 | |
---|
[125] | 58 | using namespace casa; |
---|
[827] | 59 | namespace asap { |
---|
[28] | 60 | |
---|
[822] | 61 | STWriter::STWriter(const std::string &format) |
---|
[28] | 62 | { |
---|
[988] | 63 | format_ = format; |
---|
| 64 | String t(format_); |
---|
[450] | 65 | t.upcase(); |
---|
[1443] | 66 | if (t == "MS2") { |
---|
[988] | 67 | writer_ = new PKSMS2writer(); |
---|
[1443] | 68 | } else if (t == "SDFITS") { |
---|
[988] | 69 | writer_ = new PKSSDwriter(); |
---|
[1443] | 70 | } else if (t == "ASCII" || t == "FITS" || t == "CLASS") { |
---|
[988] | 71 | writer_ = 0; |
---|
[450] | 72 | } else { |
---|
[988] | 73 | throw (AipsError("Unrecognized export format")); |
---|
[28] | 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
[822] | 77 | STWriter::~STWriter() |
---|
[28] | 78 | { |
---|
[988] | 79 | if (writer_) { |
---|
| 80 | delete writer_; |
---|
[192] | 81 | } |
---|
[28] | 82 | } |
---|
| 83 | |
---|
[822] | 84 | Int STWriter::setFormat(const std::string &format) |
---|
[28] | 85 | { |
---|
[988] | 86 | if (format != format_) { |
---|
| 87 | if (writer_) delete writer_; |
---|
[450] | 88 | } |
---|
[827] | 89 | |
---|
[988] | 90 | format_ = format; |
---|
| 91 | String t(format_); |
---|
[450] | 92 | t.upcase(); |
---|
| 93 | if (t== "MS2") { |
---|
[988] | 94 | writer_ = new PKSMS2writer(); |
---|
[450] | 95 | } else if (t== "SDFITS") { |
---|
[988] | 96 | writer_ = new PKSSDwriter(); |
---|
[1443] | 97 | } else if (t == "ASCII" || t == "FITS" || t == "CLASS") { |
---|
[988] | 98 | writer_ = 0; |
---|
[450] | 99 | } else { |
---|
| 100 | throw (AipsError("Unrecognized Format")); |
---|
[28] | 101 | } |
---|
| 102 | return 0; |
---|
| 103 | } |
---|
| 104 | |
---|
[827] | 105 | Int STWriter::write(const CountedPtr<Scantable> in, |
---|
| 106 | const std::string &filename) |
---|
[28] | 107 | { |
---|
[1575] | 108 | // If we write out foreign formats we have to convert the frequency system |
---|
| 109 | // into the output frame, as we do everything related to SPectarlCoordinates |
---|
| 110 | // in asap on-the-fly. |
---|
[192] | 111 | |
---|
[1575] | 112 | CountedPtr<Scantable> inst = in; |
---|
| 113 | if (in->frequencies().getFrame(true) != in->frequencies().getFrame(false)) { |
---|
| 114 | STMath stm(false); |
---|
| 115 | inst = stm.frequencyAlign(in); |
---|
| 116 | } |
---|
| 117 | |
---|
[1280] | 118 | if (format_=="ASCII") { |
---|
| 119 | STAsciiWriter iw; |
---|
[1575] | 120 | // ASCII calls SpectralCoordinate::toWorld so no freqAlign use 'in' |
---|
[1280] | 121 | if (iw.write(*in, filename)) { |
---|
| 122 | return 0; |
---|
| 123 | } else { |
---|
| 124 | return 1; |
---|
| 125 | } |
---|
[1443] | 126 | } else if ( format_ == "FITS" || format_ == "CLASS") { |
---|
| 127 | STFITSImageWriter iw; |
---|
| 128 | if (format_ == "CLASS") { |
---|
| 129 | iw.setClass(True); |
---|
| 130 | } |
---|
[1575] | 131 | if (iw.write(*inst, filename)) { |
---|
[1443] | 132 | return 0; |
---|
| 133 | } |
---|
[199] | 134 | } |
---|
[192] | 135 | |
---|
[827] | 136 | // MS or SDFITS |
---|
[192] | 137 | |
---|
[28] | 138 | // Extract the header from the table. |
---|
[1391] | 139 | // this is a little different from what I have done |
---|
| 140 | // before. Need to check with the Offline User Test data |
---|
[1575] | 141 | STHeader hdr = inst->getHeader(); |
---|
[1060] | 142 | //const Int nPol = hdr.npol; |
---|
| 143 | //const Int nChan = hdr.nchan; |
---|
[1575] | 144 | std::vector<uint> ifs = inst->getIFNos(); |
---|
| 145 | int nIF = inst->nif();//ifs.size(); |
---|
[1060] | 146 | Vector<uInt> nPol(nIF),nChan(nIF); |
---|
| 147 | Vector<Bool> havexpol(nIF); |
---|
[1391] | 148 | String fluxUnit = hdr.fluxunit; |
---|
[1688] | 149 | fluxUnit.upcase(); |
---|
[1295] | 150 | nPol = 0;nChan = 0; havexpol = False; |
---|
[1305] | 151 | for (uint i=0;i<ifs.size();++i) { |
---|
[1575] | 152 | nPol(ifs[i]) = inst->npol(); |
---|
| 153 | nChan(ifs[i]) = inst->nchan(ifs[i]); |
---|
[1295] | 154 | havexpol(ifs[i]) = nPol(ifs[i]) > 2; |
---|
[1060] | 155 | } |
---|
[1663] | 156 | Vector<String> obstypes(2); |
---|
| 157 | obstypes(0) = "TR";//on |
---|
| 158 | obstypes(1) = "RF TR";//off |
---|
[1575] | 159 | const Table table = inst->table(); |
---|
[324] | 160 | |
---|
[1663] | 161 | |
---|
[28] | 162 | // Create the output file and write static data. |
---|
| 163 | Int status; |
---|
[1060] | 164 | status = writer_->create(String(filename), hdr.observer, hdr.project, |
---|
[1410] | 165 | hdr.antennaname, hdr.antennaposition, |
---|
| 166 | hdr.obstype, hdr.fluxunit, |
---|
| 167 | hdr.equinox, hdr.freqref, |
---|
| 168 | nChan, nPol, havexpol, False); |
---|
[996] | 169 | if ( status ) { |
---|
[717] | 170 | throw(AipsError("Failed to create output file")); |
---|
[28] | 171 | } |
---|
| 172 | |
---|
| 173 | Int count = 0; |
---|
[1449] | 174 | PKSrecord pksrec; |
---|
| 175 | pksrec.scanNo = 1; |
---|
[822] | 176 | // use spearate iterators to ensure renumbering of all numbers |
---|
| 177 | TableIterator scanit(table, "SCANNO"); |
---|
| 178 | while (!scanit.pastEnd() ) { |
---|
| 179 | Table stable = scanit.table(); |
---|
[988] | 180 | TableIterator beamit(stable, "BEAMNO"); |
---|
[1449] | 181 | pksrec.beamNo = 1; |
---|
[822] | 182 | while (!beamit.pastEnd() ) { |
---|
| 183 | Table btable = beamit.table(); |
---|
[988] | 184 | TableIterator cycit(btable, "CYCLENO"); |
---|
[999] | 185 | ROArrayColumn<Double> srateCol(btable, "SCANRATE"); |
---|
[1462] | 186 | Vector<Double> sratedbl; |
---|
| 187 | srateCol.get(0, sratedbl); |
---|
[1477] | 188 | Vector<Float> srateflt(sratedbl.nelements()); |
---|
[1462] | 189 | convertArray(srateflt, sratedbl); |
---|
| 190 | pksrec.scanRate = srateflt; |
---|
[999] | 191 | ROArrayColumn<Double> spmCol(btable, "SRCPROPERMOTION"); |
---|
[1449] | 192 | spmCol.get(0, pksrec.srcPM); |
---|
[999] | 193 | ROArrayColumn <Double> sdirCol(btable, "SRCDIRECTION"); |
---|
[1449] | 194 | sdirCol.get(0, pksrec.srcDir); |
---|
[999] | 195 | ROScalarColumn<Double> svelCol(btable, "SRCVELOCITY"); |
---|
[1449] | 196 | svelCol.get(0, pksrec.srcVel); |
---|
[1295] | 197 | ROScalarColumn<uInt> bCol(btable, "BEAMNO"); |
---|
[1449] | 198 | pksrec.beamNo = bCol(0)+1; |
---|
| 199 | pksrec.cycleNo = 1; |
---|
[988] | 200 | while (!cycit.pastEnd() ) { |
---|
| 201 | Table ctable = cycit.table(); |
---|
| 202 | TableIterator ifit(ctable, "IFNO"); |
---|
[1390] | 203 | MDirection::ScalarColumn dirCol(ctable, "DIRECTION"); |
---|
[1449] | 204 | pksrec.direction = dirCol(0).getAngle("rad").getValue(); |
---|
| 205 | pksrec.IFno = 1; |
---|
[988] | 206 | while (!ifit.pastEnd() ) { |
---|
| 207 | Table itable = ifit.table(); |
---|
| 208 | TableRow row(itable); |
---|
[822] | 209 | // use the first row to fill in all the "metadata" |
---|
[827] | 210 | const TableRecord& rec = row.get(0); |
---|
[988] | 211 | ROArrayColumn<Float> specCol(itable, "SPECTRA"); |
---|
[1449] | 212 | pksrec.IFno = rec.asuInt("IFNO")+1; |
---|
[822] | 213 | uInt nchan = specCol(0).nelements(); |
---|
[1449] | 214 | Double crval,crpix; |
---|
[988] | 215 | Float tmp0,tmp1,tmp2,tmp3,tmp4; |
---|
[1449] | 216 | String stmp0,stmp1; |
---|
[1575] | 217 | inst->frequencies().getEntry(crpix,crval, pksrec.freqInc, |
---|
[1449] | 218 | rec.asuInt("FREQ_ID")); |
---|
[1586] | 219 | inst->focus().getEntry(pksrec.parAngle, pksrec.focusAxi, pksrec.focusTan, |
---|
| 220 | pksrec.focusRot, tmp0,tmp1,tmp2,tmp3,tmp4, |
---|
| 221 | rec.asuInt("FOCUS_ID")); |
---|
[1575] | 222 | inst->molecules().getEntry(pksrec.restFreq,stmp0,stmp1, |
---|
[1449] | 223 | rec.asuInt("MOLECULE_ID")); |
---|
[1575] | 224 | inst->tcal().getEntry(pksrec.tcalTime, pksrec.tcal, |
---|
[1449] | 225 | rec.asuInt("TCAL_ID")); |
---|
[1575] | 226 | inst->weather().getEntry(pksrec.temperature, pksrec.pressure, |
---|
[1449] | 227 | pksrec.humidity, pksrec.windSpeed, |
---|
| 228 | pksrec.windAz, rec.asuInt("WEATHER_ID")); |
---|
[822] | 229 | Double pixel = Double(nchan/2); |
---|
[1449] | 230 | pksrec.refFreq = (pixel-crpix)*pksrec.freqInc + crval; |
---|
[822] | 231 | // ok, now we have nrows for the n polarizations in this table |
---|
[1462] | 232 | polConversion(pksrec.spectra, pksrec.flagged, pksrec.xPol, itable); |
---|
[1449] | 233 | pksrec.tsys = tsysFromTable(itable); |
---|
[822] | 234 | // dummy data |
---|
[1462] | 235 | uInt npol = pksrec.spectra.ncolumn(); |
---|
[988] | 236 | |
---|
[1449] | 237 | pksrec.mjd = rec.asDouble("TIME"); |
---|
| 238 | pksrec.interval = rec.asDouble("INTERVAL"); |
---|
| 239 | pksrec.fieldName = rec.asString("FIELDNAME"); |
---|
| 240 | pksrec.srcName = rec.asString("SRCNAME"); |
---|
[1663] | 241 | pksrec.obsType = obstypes[rec.asInt("SRCTYPE")]; |
---|
[1449] | 242 | pksrec.bandwidth = nchan * abs(pksrec.freqInc); |
---|
| 243 | pksrec.azimuth = rec.asFloat("AZIMUTH"); |
---|
| 244 | pksrec.elevation = rec.asFloat("ELEVATION"); |
---|
| 245 | pksrec.refBeam = rec.asInt("REFBEAMNO") + 1; |
---|
| 246 | pksrec.sigma.resize(npol); |
---|
| 247 | pksrec.sigma = 0.0f; |
---|
| 248 | pksrec.calFctr.resize(npol); |
---|
| 249 | pksrec.calFctr = 0.0f; |
---|
| 250 | pksrec.baseLin.resize(npol,2); |
---|
| 251 | pksrec.baseLin = 0.0f; |
---|
| 252 | pksrec.baseSub.resize(npol,9); |
---|
| 253 | pksrec.baseSub = 0.0f; |
---|
| 254 | pksrec.xCalFctr = 0.0; |
---|
| 255 | |
---|
| 256 | status = writer_->write(pksrec); |
---|
[996] | 257 | if ( status ) { |
---|
[988] | 258 | writer_->close(); |
---|
| 259 | throw(AipsError("STWriter: Failed to export Scantable.")); |
---|
[822] | 260 | } |
---|
[999] | 261 | ++count; |
---|
[1449] | 262 | //++pksrec.IFno; |
---|
[988] | 263 | ++ifit; |
---|
[470] | 264 | } |
---|
[1449] | 265 | ++pksrec.cycleNo; |
---|
[988] | 266 | ++cycit; |
---|
[28] | 267 | } |
---|
[1449] | 268 | //++pksrec.beamNo; |
---|
[822] | 269 | ++beamit; |
---|
[28] | 270 | } |
---|
[1449] | 271 | ++pksrec.scanNo; |
---|
[822] | 272 | ++scanit; |
---|
[28] | 273 | } |
---|
[717] | 274 | ostringstream oss; |
---|
[999] | 275 | oss << "STWriter: wrote " << count << " rows to " << filename; |
---|
[717] | 276 | pushLog(String(oss)); |
---|
[988] | 277 | writer_->close(); |
---|
[1391] | 278 | //if MS2 delete POINTING table exists and copy the one in the keyword |
---|
| 279 | if ( format_ == "MS2" ) { |
---|
| 280 | replacePtTab(table, filename); |
---|
| 281 | } |
---|
[28] | 282 | return 0; |
---|
| 283 | } |
---|
[470] | 284 | |
---|
[827] | 285 | Vector<Float> STWriter::tsysFromTable(const Table& tab) |
---|
| 286 | { |
---|
| 287 | ROArrayColumn<Float> tsysCol(tab, "TSYS"); |
---|
| 288 | Vector<Float> out(tab.nrow()); |
---|
| 289 | Vector<Float> tmp; |
---|
| 290 | for (uInt i=0; i<tab.nrow(); ++i) { |
---|
[988] | 291 | tmp.resize(); |
---|
[827] | 292 | tmp = tsysCol(i); |
---|
| 293 | out[i] = tmp[0]; |
---|
| 294 | } |
---|
[988] | 295 | return out; |
---|
[827] | 296 | } |
---|
| 297 | |
---|
| 298 | void STWriter::polConversion( Matrix< Float >& specs, Matrix< uChar >& flags, |
---|
[822] | 299 | Vector< Complex > & xpol, const Table & tab ) |
---|
| 300 | { |
---|
| 301 | String poltype = tab.keywordSet().asString("POLTYPE"); |
---|
[1664] | 302 | // Full stokes is not supported. Just allow stokes I |
---|
| 303 | if ( poltype == "stokes" && tab.nrow() != 1) { |
---|
[822] | 304 | String msg = "poltype = " + poltype + " not yet supported in output."; |
---|
[988] | 305 | throw(AipsError(msg)); |
---|
| 306 | } |
---|
[827] | 307 | ROArrayColumn<Float> specCol(tab, "SPECTRA"); |
---|
| 308 | ROArrayColumn<uChar> flagCol(tab, "FLAGTRA"); |
---|
[822] | 309 | uInt nchan = specCol(0).nelements(); |
---|
[988] | 310 | uInt ncol = (tab.nrow()==1 ? 1: 2 ); |
---|
| 311 | specs.resize(nchan, ncol); |
---|
| 312 | flags.resize(nchan, ncol); |
---|
[822] | 313 | // the linears |
---|
[988] | 314 | for (uInt i=0; i<ncol; ++i) { |
---|
[822] | 315 | specs.column(i) = specCol(i); |
---|
| 316 | flags.column(i) = flagCol(i); |
---|
| 317 | } |
---|
| 318 | // now the complex if exists |
---|
| 319 | Bool hasxpol = False; |
---|
| 320 | xpol.resize(); |
---|
[827] | 321 | if ( tab.nrow() == 4 ) { |
---|
[822] | 322 | hasxpol = True; |
---|
| 323 | xpol.resize(nchan); |
---|
| 324 | Vector<Float> reals, imags; |
---|
| 325 | reals = specCol(2); imags = specCol(3); |
---|
| 326 | for (uInt k=0; k < nchan; ++k) { |
---|
[827] | 327 | xpol[k] = Complex(reals[k], imags[k]); |
---|
[822] | 328 | } |
---|
| 329 | } |
---|
| 330 | } |
---|
[827] | 331 | |
---|
[1391] | 332 | // For writing MS data, if there is the reference to |
---|
| 333 | // original pointing table it replace it by it. |
---|
| 334 | void STWriter::replacePtTab (const Table& tab, const std::string& fname) |
---|
| 335 | { |
---|
| 336 | String oldPtTabName = fname; |
---|
| 337 | oldPtTabName.append("/POINTING"); |
---|
| 338 | if ( tab.keywordSet().isDefined("POINTING") ) { |
---|
| 339 | String PointingTab = tab.keywordSet().asString("POINTING"); |
---|
| 340 | if ( Table::isReadable(PointingTab) ) { |
---|
| 341 | Table newPtTab(PointingTab, Table::Old); |
---|
| 342 | newPtTab.copy(oldPtTabName, Table::New); |
---|
| 343 | ostringstream oss; |
---|
| 344 | oss << "STWriter: copied " <<PointingTab << " to " << fname; |
---|
| 345 | pushLog(String(oss)); |
---|
| 346 | } |
---|
| 347 | } |
---|
| 348 | } |
---|
[827] | 349 | |
---|
| 350 | } |
---|