[191] | 1 | //#---------------------------------------------------------------------------
|
---|
| 2 | //# SDFITSImageWriter.cc: ASAP class to write out single dish spectra as FITS images
|
---|
| 3 | //#---------------------------------------------------------------------------
|
---|
| 4 | //# Copyright (C) 2004
|
---|
| 5 | //# 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: SDFITSImageWriter.cc 195 2005-01-14 02:40:10Z kil064 $
|
---|
| 30 | //#---------------------------------------------------------------------------
|
---|
| 31 |
|
---|
| 32 | #include "SDFITSImageWriter.h"
|
---|
| 33 |
|
---|
| 34 | #include <casa/aips.h>
|
---|
| 35 | #include <casa/Arrays/Array.h>
|
---|
| 36 | #include <casa/Arrays/Vector.h>
|
---|
| 37 | #include <casa/Arrays/VectorIter.h>
|
---|
| 38 | #include <casa/Utilities/CountedPtr.h>
|
---|
| 39 |
|
---|
| 40 | #include <coordinates/Coordinates/CoordinateUtil.h>
|
---|
| 41 | #include <coordinates/Coordinates/CoordinateSystem.h>
|
---|
| 42 | #include <coordinates/Coordinates/SpectralCoordinate.h>
|
---|
| 43 | #include <coordinates/Coordinates/DirectionCoordinate.h>
|
---|
| 44 | #include <coordinates/Coordinates/StokesCoordinate.h>
|
---|
| 45 | #include <coordinates/Coordinates/ObsInfo.h>
|
---|
| 46 |
|
---|
| 47 | #include <images/Images/ImageFITSConverter.h>
|
---|
| 48 | #include <images/Images/TempImage.h>
|
---|
| 49 |
|
---|
| 50 | #include <lattices/Lattices/ArrayLattice.h>
|
---|
| 51 |
|
---|
| 52 | #include <measures/Measures/MEpoch.h>
|
---|
| 53 |
|
---|
| 54 | #include <tables/Tables/Table.h>
|
---|
| 55 | #include <tables/Tables/ScalarColumn.h>
|
---|
| 56 | #include <tables/Tables/ArrayColumn.h>
|
---|
| 57 |
|
---|
| 58 | #include "SDContainer.h"
|
---|
| 59 | #include "SDMemTable.h"
|
---|
| 60 |
|
---|
| 61 | using namespace casa;
|
---|
| 62 | using namespace asap;
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | SDFITSImageWriter::SDFITSImageWriter()
|
---|
| 66 | {;}
|
---|
| 67 |
|
---|
| 68 | SDFITSImageWriter::~SDFITSImageWriter()
|
---|
| 69 | {;}
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | Bool SDFITSImageWriter::write(const SDMemTable& sdTable,
|
---|
| 73 | const String& rootName, Bool verbose)
|
---|
| 74 | {
|
---|
| 75 |
|
---|
| 76 | // Get global Header from Table
|
---|
| 77 |
|
---|
| 78 | SDHeader header = sdTable.getSDHeader();
|
---|
| 79 | MEpoch::Ref timeRef(MEpoch::UTC); // Should be in header
|
---|
| 80 | MDirection::Types dirRef(MDirection::J2000); // Should be in header
|
---|
| 81 |
|
---|
| 82 | // Prepare initial ObsInfo
|
---|
| 83 |
|
---|
| 84 | ObsInfo oi;
|
---|
| 85 | oi.setTelescope(String(header.antennaname));
|
---|
| 86 | oi.setObserver(String(header.observer));
|
---|
| 87 |
|
---|
| 88 | // Column keywords
|
---|
| 89 |
|
---|
| 90 | Table tab = sdTable.table();
|
---|
| 91 | ROArrayColumn<Double> dir(tab, String("DIRECTION"));
|
---|
| 92 | ROScalarColumn<Double> time(tab, "TIME");
|
---|
| 93 | ROArrayColumn<uInt> freqid(tab, "FREQID");
|
---|
[195] | 94 | ROScalarColumn<String> src(tab, "SRCNAME");
|
---|
[191] | 95 |
|
---|
| 96 | // Output Image Shape; spectral axis to be updated
|
---|
| 97 |
|
---|
| 98 | IPosition shapeOut(4,1); // spectral, direction (2), stokes
|
---|
| 99 |
|
---|
| 100 | // Axes (should be in header)
|
---|
| 101 |
|
---|
| 102 | const uInt beamAxis = 0;
|
---|
| 103 | const uInt ifAxis = 1;
|
---|
| 104 | const uInt stokesAxis = 2;
|
---|
| 105 | const uInt chanAxis = 3;
|
---|
| 106 | const Unit RAD(String("rad"));
|
---|
| 107 |
|
---|
| 108 | // Temps
|
---|
| 109 |
|
---|
| 110 | Vector<Int> whichStokes(1,1);
|
---|
| 111 | Array<Double> whichDir;
|
---|
| 112 | Vector<Double> latLon(2);
|
---|
| 113 | IPosition posDir(2,0);
|
---|
| 114 | Projection proj(Projection::SIN); // What should we use ?
|
---|
| 115 | Matrix<Double> xForm(2,2);
|
---|
| 116 | xForm = 0.0; xForm.diagonal() = 1.0;
|
---|
| 117 | Vector<Double> incLonLat(2,0.0);
|
---|
| 118 | Vector<Double> refPixLonLat(2,0.0);
|
---|
| 119 |
|
---|
| 120 | // Loop over rows
|
---|
| 121 |
|
---|
| 122 | uInt maxMem = 128;
|
---|
| 123 | Bool preferVelocity = False;
|
---|
| 124 | Bool opticalVelocity = False;
|
---|
| 125 | Int bitPix = -32; // FLoating point
|
---|
| 126 | Float minPix = 1.0;
|
---|
| 127 | Float maxPix = -1.0;
|
---|
| 128 | Bool overWrite = True;
|
---|
| 129 | Bool degLast = False;
|
---|
| 130 | Bool reallyVerbose = False;
|
---|
| 131 | //
|
---|
| 132 | String errMsg;
|
---|
| 133 | const uInt nRows = sdTable.nRow();
|
---|
| 134 | for (uInt iRow=0; iRow<nRows; iRow++) {
|
---|
| 135 |
|
---|
| 136 | // Get data
|
---|
| 137 |
|
---|
| 138 | const MaskedArray<Float>& dataIn(sdTable.rowAsMaskedArray(iRow));
|
---|
| 139 | const Array<Float>& values = dataIn.getArray();
|
---|
| 140 | const Array<Bool>& mask = dataIn.getMask();
|
---|
| 141 | const IPosition& shapeIn = dataIn.shape();
|
---|
| 142 | shapeOut(0) = shapeIn(chanAxis);
|
---|
| 143 | TiledShape tShapeOut(shapeOut);
|
---|
| 144 |
|
---|
| 145 | // Update ObsInfo (time changes per integration)
|
---|
| 146 |
|
---|
| 147 | Double dTmp;
|
---|
| 148 | time.get(iRow, dTmp);
|
---|
| 149 | MVEpoch tmp2(Quantum<Double>(dTmp, Unit(String("d"))));
|
---|
| 150 | MEpoch epoch(tmp2, timeRef);
|
---|
| 151 | oi.setObsDate(epoch);
|
---|
| 152 |
|
---|
| 153 | // Iterate through data in this row by spectra
|
---|
| 154 |
|
---|
| 155 | ReadOnlyVectorIterator<Float> itData(values, chanAxis);
|
---|
| 156 | ReadOnlyVectorIterator<Bool> itMask(mask, chanAxis);
|
---|
| 157 | while (!itData.pastEnd()) {
|
---|
| 158 | const IPosition& pos = itData.pos();
|
---|
| 159 |
|
---|
| 160 | // Form SpectralCoordinate
|
---|
| 161 |
|
---|
| 162 | Vector<uInt> iTmp;
|
---|
| 163 | freqid.get(iRow, iTmp);
|
---|
| 164 | SpectralCoordinate sC = sdTable.getCoordinate(iTmp(pos(ifAxis)));
|
---|
| 165 |
|
---|
| 166 | // Form DirectionCoordinate
|
---|
| 167 |
|
---|
| 168 | dir.get(iRow, whichDir);
|
---|
| 169 | posDir(0) = pos(beamAxis);
|
---|
| 170 | posDir(1) = 0;
|
---|
| 171 | latLon[0] = whichDir(posDir);
|
---|
| 172 | //
|
---|
| 173 | posDir(0) = pos(beamAxis);
|
---|
| 174 | posDir(1) = 1;
|
---|
| 175 | latLon[1] = whichDir(posDir);
|
---|
| 176 | DirectionCoordinate dC(dirRef, proj, latLon[0], latLon[1],
|
---|
| 177 | incLonLat[0], incLonLat[1], xForm,
|
---|
| 178 | refPixLonLat[0], refPixLonLat[1]);
|
---|
| 179 |
|
---|
| 180 | // Form Stokes Coordinate (no true Stokes info yet);
|
---|
| 181 |
|
---|
| 182 | whichStokes(0) = convertStokes(pos(stokesAxis));
|
---|
| 183 | StokesCoordinate stC(whichStokes);
|
---|
| 184 |
|
---|
| 185 | // Create CoordinateSystem
|
---|
| 186 |
|
---|
| 187 | CoordinateSystem cSys;
|
---|
| 188 | cSys.addCoordinate(sC);
|
---|
| 189 | cSys.addCoordinate(dC);
|
---|
| 190 | cSys.addCoordinate(stC);
|
---|
| 191 | cSys.setObsInfo(oi);
|
---|
| 192 |
|
---|
| 193 | // Reform data into correct shape for output
|
---|
| 194 |
|
---|
| 195 | Array<Float> t1(itData.array().reform(shapeOut));
|
---|
| 196 | Array<Bool> m1(itMask.array().reform(shapeOut));
|
---|
| 197 |
|
---|
| 198 | // Create aips++ Image
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | TempImage<Float> tIm(tShapeOut, cSys);
|
---|
| 202 | tIm.put(t1);
|
---|
| 203 | //
|
---|
| 204 | ArrayLattice<Bool> latMask(m1);
|
---|
| 205 | tIm.attachMask(latMask);
|
---|
| 206 |
|
---|
| 207 | // Write out as FITS Image file
|
---|
| 208 |
|
---|
| 209 |
|
---|
| 210 | ostringstream oss;
|
---|
[195] | 211 | oss << "row" << iRow << "_beam" << pos(0) << "_if" << pos(1) << "_pol" << pos(2) << "_" << src(iRow) << ".fits";
|
---|
| 212 | String fileName;
|
---|
| 213 | if (rootName.length()>0) {
|
---|
| 214 | fileName = rootName + "_" + String(oss);
|
---|
| 215 | } else {
|
---|
| 216 | fileName = String(oss);
|
---|
| 217 | }
|
---|
[191] | 218 | if (verbose) cerr << "Writing row " << iRow << " into file " << fileName << endl;
|
---|
| 219 | //
|
---|
| 220 | Bool ok = ImageFITSConverter::ImageToFITS (errMsg, tIm, fileName, maxMem, preferVelocity,
|
---|
| 221 | opticalVelocity, bitPix, minPix, maxPix, overWrite,
|
---|
| 222 | degLast, reallyVerbose);
|
---|
| 223 | if (!ok) {
|
---|
| 224 | cerr << "Error writing fits - " << errMsg << endl;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | // Next spectrum
|
---|
| 228 |
|
---|
| 229 | itData.next();
|
---|
| 230 | itMask.next();
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | //
|
---|
| 234 | if (!verbose) {
|
---|
| 235 | cerr << "Wrote " << nRows << " into individual FITS files" << endl;
|
---|
| 236 | }
|
---|
| 237 | //
|
---|
| 238 | return True;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 |
|
---|
| 242 | Int SDFITSImageWriter::convertStokes (Int val)
|
---|
| 243 | {
|
---|
| 244 | Stokes::StokesTypes stokes = Stokes::RR;
|
---|
| 245 | if (val==0) {
|
---|
| 246 | stokes = Stokes::RR;
|
---|
| 247 | } else if (val==1) {
|
---|
| 248 | stokes = Stokes::LL;
|
---|
| 249 | } else if (val==2) {
|
---|
| 250 | stokes = Stokes::RL;
|
---|
| 251 | } else if (val==3) {
|
---|
| 252 | stokes = Stokes::LR;
|
---|
| 253 | } else {
|
---|
| 254 | stokes = Stokes::Undefined;
|
---|
| 255 | }
|
---|
| 256 | //
|
---|
| 257 | return Int(stokes);
|
---|
| 258 | }
|
---|