[470] | 1 | //#---------------------------------------------------------------------------
|
---|
[28] | 2 | //# SDWriter.cc: ASAP class to write out single dish spectra.
|
---|
| 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: SDWriter.cc 717 2005-11-17 03:37:54Z 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"
|
---|
| 51 | #include "SDWriter.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 |
|
---|
| 59 | //--------------------------------------------------------- SDWriter::SDWriter
|
---|
| 60 |
|
---|
| 61 | // Default constructor.
|
---|
| 62 |
|
---|
[125] | 63 | SDWriter::SDWriter(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 |
|
---|
| 82 | //-------------------------------------------------------- SDWriter::~SDWriter
|
---|
| 83 |
|
---|
| 84 | // Destructor.
|
---|
| 85 |
|
---|
| 86 | SDWriter::~SDWriter()
|
---|
| 87 | {
|
---|
[192] | 88 | if (cWriter) {
|
---|
| 89 | delete cWriter;
|
---|
| 90 | }
|
---|
[28] | 91 | }
|
---|
| 92 |
|
---|
| 93 | //-------------------------------------------------------- SDWriter::setFormat
|
---|
| 94 |
|
---|
| 95 | // Reset the output format.
|
---|
| 96 |
|
---|
[192] | 97 | Int SDWriter::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 |
|
---|
| 120 | //------------------------------------------------------------ SDWriter::write
|
---|
| 121 |
|
---|
| 122 | // Write an SDMemTable to file in the desired format, closing the file when
|
---|
| 123 | // finished.
|
---|
| 124 |
|
---|
[324] | 125 | Int SDWriter::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 |
|
---|
| 182 | Int scanNo = -1;
|
---|
| 183 | Int cycleNo;
|
---|
| 184 | Double mjd0 = 0.0;
|
---|
[470] | 185 | //
|
---|
| 186 | Array<Float> spectra, tSys, stokes;
|
---|
| 187 | Array<uChar> flags;
|
---|
| 188 | Bool doLinear = True;
|
---|
| 189 | //
|
---|
[28] | 190 | Int count = 0;
|
---|
[324] | 191 | for (Int iRow = 0; iRow < in->nRow(); iRow++) {
|
---|
[28] | 192 | // Extract the next integration from the table.
|
---|
[324] | 193 | SDContainer sd = in->getSDContainer(iRow);
|
---|
[28] | 194 | if (sd.scanid != scanNo) {
|
---|
| 195 | scanNo = sd.scanid;
|
---|
| 196 | mjd0 = sd.timestamp;
|
---|
| 197 | cycleNo = 1;
|
---|
| 198 | } else if (fabs(sd.timestamp-mjd0) > sd.interval) {
|
---|
| 199 | cycleNo++;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[324] | 202 | // Get FreqID vector
|
---|
| 203 | freqIDCol.get(iRow, freqIDs);
|
---|
| 204 |
|
---|
[470] | 205 | // Get Stokes array (all beams/IFs/Stokes). Its not in SDContainer
|
---|
| 206 | if (toStokes) {
|
---|
| 207 | stokes = in->getStokesSpectrum(iRow,-1,-1);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | IPosition start(asap::nAxes,0);
|
---|
| 211 | IPosition end(stokes.shape()-1);
|
---|
| 212 |
|
---|
[28] | 213 | // Write it out beam by beam.
|
---|
| 214 | for (Int iBeam = 0; iBeam < hdr.nbeam; iBeam++) {
|
---|
[470] | 215 | start(asap::BeamAxis) = iBeam;
|
---|
| 216 | end(asap::BeamAxis) = iBeam;
|
---|
[28] | 217 |
|
---|
| 218 | // Write it out IF by IF.
|
---|
| 219 | for (Int iIF = 0; iIF < hdr.nif; iIF++) {
|
---|
[470] | 220 | start(asap::IFAxis) = iIF;
|
---|
| 221 | end(asap::IFAxis) = iIF;
|
---|
[324] | 222 | uInt freqID = freqIDs(iIF);
|
---|
| 223 |
|
---|
[28] | 224 | // None of these are stored in SDMemTable by SDReader.
|
---|
[112] | 225 | //String fieldName = "";
|
---|
[79] | 226 | //Vector<Double> srcDir(2, 0.0);
|
---|
[28] | 227 | Vector<Double> srcPM(2, 0.0);
|
---|
| 228 | Double srcVel = 0.0;
|
---|
[324] | 229 |
|
---|
[442] | 230 | // The writer will assume refPix = nChan/2 (0-rel). So recompute
|
---|
[324] | 231 | // the frequency at this location.
|
---|
| 232 |
|
---|
| 233 | Double cdelt = sdft.increment(freqID);
|
---|
| 234 | Double crval = sdft.referenceValue(freqID);
|
---|
| 235 | Double crpix = sdft.referencePixel(freqID);
|
---|
[442] | 236 | Double pixel = nChan/2;
|
---|
[324] | 237 | Double refFreqNew = (pixel-crpix)*cdelt + crval;
|
---|
| 238 | //
|
---|
[112] | 239 | //Vector<Float> tcal(2, 0.0f);
|
---|
| 240 | //String tcalTime = "";
|
---|
| 241 | //Float azimuth = 0.0f;
|
---|
| 242 | //Float elevation = 0.0f;
|
---|
| 243 | //Float parAngle = 0.0f;
|
---|
[28] | 244 | Float focusAxi = 0.0f;
|
---|
| 245 | Float focusTan = 0.0f;
|
---|
| 246 | Float focusRot = 0.0f;
|
---|
| 247 | Float temperature = 0.0f;
|
---|
| 248 | Float pressure = 0.0f;
|
---|
| 249 | Float humidity = 0.0f;
|
---|
| 250 | Float windSpeed = 0.0f;
|
---|
| 251 | Float windAz = 0.0f;
|
---|
[112] | 252 | //Int refBeam = 0;
|
---|
[79] | 253 | //Vector<Double> direction(2, 0.0);
|
---|
[28] | 254 | Vector<Double> scanRate(2, 0.0);
|
---|
| 255 | Vector<Float> sigma(nPol, 0.0f);
|
---|
| 256 | Vector<Float> calFctr(nPol, 0.0f);
|
---|
| 257 | Matrix<Float> baseLin(nPol,2, 0.0f);
|
---|
| 258 | Matrix<Float> baseSub(nPol,9, 0.0f);
|
---|
| 259 | Complex xCalFctr;
|
---|
| 260 | Vector<Complex> xPol;
|
---|
[470] | 261 |
|
---|
| 262 | // Get data.
|
---|
| 263 |
|
---|
| 264 | if (toStokes) {
|
---|
| 265 |
|
---|
| 266 | // Big pain in the ass because
|
---|
| 267 | // 1) Stokes vector may have less polarizations than input
|
---|
| 268 | // 2) Stokes column virtual and not in SDContainer
|
---|
| 269 | // 3) Tsys and FLags already selected for IF and Beam
|
---|
| 270 | // 3) Have to flip order
|
---|
| 271 |
|
---|
| 272 | Array<Float> t = sd.getTsys(iBeam,iIF);
|
---|
| 273 | tSys = SDPolUtil::computeStokesTSysForWriter (t, doLinear);
|
---|
| 274 | Array<uChar> t2 = sd.getFlags(iBeam,iIF);
|
---|
| 275 | flags = SDPolUtil::computeStokesFlagsForWriter (t2, doLinear);
|
---|
| 276 | spectra = SDPolUtil::extractStokesForWriter (stokes,start,end);
|
---|
| 277 | } else {
|
---|
| 278 | tSys = sd.getTsys(iBeam,iIF);
|
---|
| 279 | flags = sd.getFlags(iBeam,iIF);
|
---|
| 280 | spectra = sd.getSpectrum(iBeam,iIF);
|
---|
| 281 | }
|
---|
[324] | 282 | //
|
---|
[630] | 283 | if (status = cWriter->write(sd.scanid+1, cycleNo, sd.timestamp,
|
---|
[112] | 284 | sd.interval, sd.fieldname, sd.sourcename,
|
---|
[94] | 285 | sd.getDirection(iBeam),
|
---|
[324] | 286 | srcPM, srcVel, iIF+1, refFreqNew,
|
---|
| 287 | nChan*abs(cdelt), cdelt, restFreq, sd.tcal,
|
---|
[112] | 288 | sd.tcaltime, sd.azimuth, sd.elevation,
|
---|
| 289 | sd.parangle,
|
---|
[28] | 290 | focusAxi, focusTan, focusRot, temperature,
|
---|
| 291 | pressure, humidity, windSpeed, windAz,
|
---|
[112] | 292 | sd.refbeam, iBeam+1,
|
---|
[94] | 293 | sd.getDirection(iBeam),
|
---|
| 294 | scanRate,
|
---|
[470] | 295 | tSys, sigma, calFctr,
|
---|
[75] | 296 | baseLin, baseSub,
|
---|
[470] | 297 | spectra, flags,
|
---|
[28] | 298 | xCalFctr, xPol)) {
|
---|
| 299 | cerr << "Error writing output file." << endl;
|
---|
| 300 | return 1;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | count++;
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
[717] | 307 | ostringstream oss;
|
---|
| 308 | oss << "SDWriter: wrote " << count << " rows to " << filename << endl;
|
---|
| 309 | pushLog(String(oss));
|
---|
[28] | 310 | cWriter->close();
|
---|
| 311 |
|
---|
| 312 | return 0;
|
---|
| 313 | }
|
---|
[470] | 314 |
|
---|
| 315 |
|
---|