[28] | 1 | //#--------------------------------------------------------------------------- |
---|
| 2 | //# SDWriter.cc: ASAP class to write out single dish spectra. |
---|
| 3 | //#--------------------------------------------------------------------------- |
---|
| 4 | //# Copyright (C) 2004 |
---|
| 5 | //# Mark Calabretta, ATNF |
---|
| 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 28 2004-07-07 01:24:23Z mcalabre $ |
---|
| 30 | //#--------------------------------------------------------------------------- |
---|
| 31 | |
---|
| 32 | #include <string> |
---|
| 33 | |
---|
| 34 | #include <aips/aips.h> |
---|
| 35 | #include <aips/Arrays.h> |
---|
| 36 | #include <aips/Mathematics/Complex.h> |
---|
| 37 | #include <aips/Utilities/CountedPtr.h> |
---|
| 38 | |
---|
| 39 | #include <atnf/PKSIO/PKSMS2writer.h> |
---|
| 40 | #include <atnf/PKSIO/PKSSDwriter.h> |
---|
| 41 | |
---|
| 42 | #include <SDMemTable.h> |
---|
| 43 | #include <SDWriter.h> |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | using namespace atnf_sd; |
---|
| 47 | |
---|
| 48 | //--------------------------------------------------------- SDWriter::SDWriter |
---|
| 49 | |
---|
| 50 | // Default constructor. |
---|
| 51 | |
---|
| 52 | SDWriter::SDWriter( |
---|
| 53 | const string &format = "SDFITS") |
---|
| 54 | { |
---|
| 55 | cFormat = format; |
---|
| 56 | |
---|
| 57 | if (cFormat == "MS2") { |
---|
| 58 | cWriter = new PKSMS2writer(); |
---|
| 59 | } else { |
---|
| 60 | cWriter = new PKSSDwriter(); |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | //-------------------------------------------------------- SDWriter::~SDWriter |
---|
| 65 | |
---|
| 66 | // Destructor. |
---|
| 67 | |
---|
| 68 | SDWriter::~SDWriter() |
---|
| 69 | { |
---|
| 70 | // Nothing. |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | //-------------------------------------------------------- SDWriter::setFormat |
---|
| 74 | |
---|
| 75 | // Reset the output format. |
---|
| 76 | |
---|
| 77 | Int SDWriter::setFormat( |
---|
| 78 | const string &format = "SDFITS") |
---|
| 79 | { |
---|
| 80 | if (format != cFormat) { |
---|
| 81 | delete cWriter; |
---|
| 82 | |
---|
| 83 | cFormat = format; |
---|
| 84 | if (cFormat == "MS2") { |
---|
| 85 | cWriter = new PKSMS2writer(); |
---|
| 86 | } else { |
---|
| 87 | cWriter = new PKSSDwriter(); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | return 0; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | //------------------------------------------------------------ SDWriter::write |
---|
| 95 | |
---|
| 96 | // Write an SDMemTable to file in the desired format, closing the file when |
---|
| 97 | // finished. |
---|
| 98 | |
---|
| 99 | Int SDWriter::write( |
---|
| 100 | const CountedPtr<SDMemTable> table, |
---|
| 101 | const string &filename) |
---|
| 102 | { |
---|
| 103 | // Extract the header from the table. |
---|
| 104 | SDHeader hdr = table->getSDHeader(); |
---|
| 105 | Int nPol = hdr.npol; |
---|
| 106 | Int nChan = hdr.nchan; |
---|
| 107 | |
---|
| 108 | // Create the output file and write static data. |
---|
| 109 | Int status; |
---|
| 110 | if (status = cWriter->create(filename, hdr.observer, hdr.project, |
---|
| 111 | hdr.antennaname, hdr.antennaposition, |
---|
| 112 | hdr.obstype, hdr.equinox, hdr.freqref, |
---|
| 113 | nChan, nPol, False, False)) { |
---|
| 114 | cerr << "Failed to create output file." << endl; |
---|
| 115 | return 1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | Int scanNo = -1; |
---|
| 119 | Int cycleNo; |
---|
| 120 | Double mjd0 = 0.0; |
---|
| 121 | |
---|
| 122 | Int count = 0; |
---|
| 123 | for (Int iRow = 0; iRow < table->nRows(); iRow++) { |
---|
| 124 | // Extract the next integration from the table. |
---|
| 125 | SDContainer sd = table->getSDContainer(iRow); |
---|
| 126 | if (sd.scanid != scanNo) { |
---|
| 127 | scanNo = sd.scanid; |
---|
| 128 | mjd0 = sd.timestamp; |
---|
| 129 | cycleNo = 1; |
---|
| 130 | } else if (fabs(sd.timestamp-mjd0) > sd.interval) { |
---|
| 131 | cycleNo++; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | // Write it out beam by beam. |
---|
| 135 | for (Int iBeam = 0; iBeam < hdr.nbeam; iBeam++) { |
---|
| 136 | |
---|
| 137 | // Write it out IF by IF. |
---|
| 138 | for (Int iIF = 0; iIF < hdr.nif; iIF++) { |
---|
| 139 | // None of these are stored in SDMemTable by SDReader. |
---|
| 140 | String fieldName = ""; |
---|
| 141 | Vector<Double> srcDir(2, 0.0); |
---|
| 142 | Vector<Double> srcPM(2, 0.0); |
---|
| 143 | Double srcVel = 0.0; |
---|
| 144 | Double freqInc = 0.0; |
---|
| 145 | Vector<Float> tcal(2, 0.0f); |
---|
| 146 | String tcalTime = ""; |
---|
| 147 | Float azimuth = 0.0f; |
---|
| 148 | Float elevation = 0.0f; |
---|
| 149 | Float parAngle = 0.0f; |
---|
| 150 | Float focusAxi = 0.0f; |
---|
| 151 | Float focusTan = 0.0f; |
---|
| 152 | Float focusRot = 0.0f; |
---|
| 153 | Float temperature = 0.0f; |
---|
| 154 | Float pressure = 0.0f; |
---|
| 155 | Float humidity = 0.0f; |
---|
| 156 | Float windSpeed = 0.0f; |
---|
| 157 | Float windAz = 0.0f; |
---|
| 158 | Int refBeam = 0; |
---|
| 159 | Vector<Double> direction(2, 0.0); |
---|
| 160 | Vector<Double> scanRate(2, 0.0); |
---|
| 161 | Vector<Float> sigma(nPol, 0.0f); |
---|
| 162 | Vector<Float> calFctr(nPol, 0.0f); |
---|
| 163 | Matrix<Float> baseLin(nPol,2, 0.0f); |
---|
| 164 | Matrix<Float> baseSub(nPol,9, 0.0f); |
---|
| 165 | Complex xCalFctr; |
---|
| 166 | Vector<Complex> xPol; |
---|
| 167 | |
---|
| 168 | if (status = cWriter->write(sd.scanid, cycleNo, sd.timestamp, |
---|
| 169 | sd.interval, fieldName, sd.sourcename, |
---|
| 170 | srcDir, srcPM, srcVel, iIF+1, hdr.reffreq, |
---|
| 171 | hdr.bandwidth, freqInc, tcal, tcalTime, |
---|
| 172 | azimuth, elevation, parAngle, |
---|
| 173 | focusAxi, focusTan, focusRot, temperature, |
---|
| 174 | pressure, humidity, windSpeed, windAz, |
---|
| 175 | refBeam, iBeam+1, direction, scanRate, |
---|
| 176 | sd.getTsys(iBeam, iIF), |
---|
| 177 | sigma, calFctr, baseLin, baseSub, |
---|
| 178 | sd.getSpectrum(iBeam, iIF), |
---|
| 179 | sd.getFlags(iBeam, iIF), |
---|
| 180 | xCalFctr, xPol)) { |
---|
| 181 | cerr << "Error writing output file." << endl; |
---|
| 182 | return 1; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | count++; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | cout << "SDWriter: wrote " << count << " rows to " << filename << endl; |
---|
| 191 | cWriter->close(); |
---|
| 192 | |
---|
| 193 | return 0; |
---|
| 194 | } |
---|