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