[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 414 2005-02-11 06:50:42Z mar637 $
|
---|
| 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");
|
---|
[389] | 96 | ROArrayColumn<uInt> restfreqidCol(tab, "RESTFREQID");
|
---|
[279] | 97 | ROScalarColumn<String> srcCol(tab, "SRCNAME");
|
---|
[191] | 98 |
|
---|
| 99 | // Output Image Shape; spectral axis to be updated
|
---|
| 100 |
|
---|
| 101 | IPosition shapeOut(4,1); // spectral, direction (2), stokes
|
---|
| 102 |
|
---|
| 103 | // Axes (should be in header)
|
---|
| 104 |
|
---|
[209] | 105 | const uInt beamAxis = asap::BeamAxis;
|
---|
| 106 | const uInt ifAxis = asap::IFAxis;
|
---|
| 107 | const uInt stokesAxis = asap::PolAxis;
|
---|
| 108 | const uInt chanAxis = asap::ChanAxis;
|
---|
[191] | 109 | const Unit RAD(String("rad"));
|
---|
| 110 |
|
---|
[279] | 111 | // Output Directory
|
---|
| 112 |
|
---|
| 113 | String dirName2(dirName);
|
---|
| 114 | if (dirName.length()==0) {
|
---|
| 115 | dirName2 = String("asap_FITS");
|
---|
| 116 | }
|
---|
| 117 | Directory dir(dirName2);
|
---|
| 118 | dir.create(True);
|
---|
[414] | 119 | cout << "Created directory '" << dirName2 << "' for output files" << endl;
|
---|
[279] | 120 |
|
---|
[191] | 121 | // Temps
|
---|
| 122 |
|
---|
| 123 | Vector<Int> whichStokes(1,1);
|
---|
| 124 | Array<Double> whichDir;
|
---|
[197] | 125 | Vector<Double> lonLat(2);
|
---|
[191] | 126 | IPosition posDir(2,0);
|
---|
| 127 | Projection proj(Projection::SIN); // What should we use ?
|
---|
| 128 | Matrix<Double> xForm(2,2);
|
---|
| 129 | xForm = 0.0; xForm.diagonal() = 1.0;
|
---|
| 130 | Vector<Double> incLonLat(2,0.0);
|
---|
| 131 | Vector<Double> refPixLonLat(2,0.0);
|
---|
| 132 |
|
---|
| 133 | // Loop over rows
|
---|
| 134 |
|
---|
| 135 | uInt maxMem = 128;
|
---|
| 136 | Bool preferVelocity = False;
|
---|
| 137 | Bool opticalVelocity = False;
|
---|
| 138 | Int bitPix = -32; // FLoating point
|
---|
| 139 | Float minPix = 1.0;
|
---|
| 140 | Float maxPix = -1.0;
|
---|
| 141 | Bool overWrite = True;
|
---|
| 142 | Bool degLast = False;
|
---|
| 143 | Bool reallyVerbose = False;
|
---|
| 144 | //
|
---|
| 145 | String errMsg;
|
---|
| 146 | const uInt nRows = sdTable.nRow();
|
---|
| 147 | for (uInt iRow=0; iRow<nRows; iRow++) {
|
---|
| 148 |
|
---|
| 149 | // Get data
|
---|
| 150 |
|
---|
| 151 | const MaskedArray<Float>& dataIn(sdTable.rowAsMaskedArray(iRow));
|
---|
| 152 | const Array<Float>& values = dataIn.getArray();
|
---|
| 153 | const Array<Bool>& mask = dataIn.getMask();
|
---|
| 154 | const IPosition& shapeIn = dataIn.shape();
|
---|
| 155 | shapeOut(0) = shapeIn(chanAxis);
|
---|
| 156 | TiledShape tShapeOut(shapeOut);
|
---|
| 157 |
|
---|
| 158 | // Update ObsInfo (time changes per integration)
|
---|
| 159 |
|
---|
| 160 | Double dTmp;
|
---|
[279] | 161 | timeCol.get(iRow, dTmp);
|
---|
[191] | 162 | MVEpoch tmp2(Quantum<Double>(dTmp, Unit(String("d"))));
|
---|
| 163 | MEpoch epoch(tmp2, timeRef);
|
---|
| 164 | oi.setObsDate(epoch);
|
---|
| 165 |
|
---|
| 166 | // Iterate through data in this row by spectra
|
---|
| 167 |
|
---|
| 168 | ReadOnlyVectorIterator<Float> itData(values, chanAxis);
|
---|
| 169 | ReadOnlyVectorIterator<Bool> itMask(mask, chanAxis);
|
---|
| 170 | while (!itData.pastEnd()) {
|
---|
| 171 | const IPosition& pos = itData.pos();
|
---|
| 172 |
|
---|
| 173 | // Form SpectralCoordinate
|
---|
| 174 |
|
---|
| 175 | Vector<uInt> iTmp;
|
---|
[279] | 176 | freqidCol.get(iRow, iTmp);
|
---|
[389] | 177 | uInt freqID = iTmp(pos(ifAxis));
|
---|
| 178 | //
|
---|
| 179 | restfreqidCol.get(iRow, iTmp);
|
---|
| 180 | uInt restFreqID = iTmp(pos(ifAxis));
|
---|
| 181 | //
|
---|
| 182 | SpectralCoordinate sC = sdTable.getSpectralCoordinate(freqID, restFreqID, iRow);
|
---|
[191] | 183 |
|
---|
| 184 | // Form DirectionCoordinate
|
---|
| 185 |
|
---|
[279] | 186 | dirCol.get(iRow, whichDir);
|
---|
[191] | 187 | posDir(0) = pos(beamAxis);
|
---|
| 188 | posDir(1) = 0;
|
---|
[197] | 189 | lonLat[0] = whichDir(posDir);
|
---|
[191] | 190 | //
|
---|
| 191 | posDir(0) = pos(beamAxis);
|
---|
| 192 | posDir(1) = 1;
|
---|
[197] | 193 | lonLat[1] = whichDir(posDir);
|
---|
| 194 | DirectionCoordinate dC(dirRef, proj, lonLat[0], lonLat[1],
|
---|
[191] | 195 | incLonLat[0], incLonLat[1], xForm,
|
---|
| 196 | refPixLonLat[0], refPixLonLat[1]);
|
---|
| 197 |
|
---|
| 198 | // Form Stokes Coordinate (no true Stokes info yet);
|
---|
| 199 |
|
---|
| 200 | whichStokes(0) = convertStokes(pos(stokesAxis));
|
---|
| 201 | StokesCoordinate stC(whichStokes);
|
---|
| 202 |
|
---|
| 203 | // Create CoordinateSystem
|
---|
| 204 |
|
---|
| 205 | CoordinateSystem cSys;
|
---|
| 206 | cSys.addCoordinate(sC);
|
---|
| 207 | cSys.addCoordinate(dC);
|
---|
| 208 | cSys.addCoordinate(stC);
|
---|
| 209 | cSys.setObsInfo(oi);
|
---|
| 210 |
|
---|
| 211 | // Reform data into correct shape for output
|
---|
| 212 |
|
---|
| 213 | Array<Float> t1(itData.array().reform(shapeOut));
|
---|
| 214 | Array<Bool> m1(itMask.array().reform(shapeOut));
|
---|
| 215 |
|
---|
| 216 | // Create aips++ Image
|
---|
| 217 |
|
---|
| 218 | TempImage<Float> tIm(tShapeOut, cSys);
|
---|
| 219 | tIm.put(t1);
|
---|
| 220 | //
|
---|
| 221 | ArrayLattice<Bool> latMask(m1);
|
---|
| 222 | tIm.attachMask(latMask);
|
---|
| 223 |
|
---|
| 224 | // Write out as FITS Image file
|
---|
| 225 |
|
---|
| 226 | ostringstream oss;
|
---|
[209] | 227 | oss << "row" << iRow << "_beam" << pos(0) << "_if"
|
---|
[279] | 228 | << pos(1) << "_pol" << pos(2) << "_" << srcCol(iRow) << ".fits";
|
---|
| 229 | String tS(oss);
|
---|
| 230 | String fileName = dirName2 + String("/") + tS;
|
---|
[414] | 231 | if (verbose) cout << "Writing row " << iRow
|
---|
[279] | 232 | << " into file '" << tS << "'" << endl;
|
---|
[191] | 233 | //
|
---|
[209] | 234 | Bool ok = ImageFITSConverter::ImageToFITS(errMsg, tIm, fileName,
|
---|
| 235 | maxMem, preferVelocity,
|
---|
| 236 | opticalVelocity, bitPix,
|
---|
| 237 | minPix, maxPix, overWrite,
|
---|
| 238 | degLast, reallyVerbose);
|
---|
[191] | 239 | if (!ok) {
|
---|
| 240 | cerr << "Error writing fits - " << errMsg << endl;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | // Next spectrum
|
---|
| 244 |
|
---|
| 245 | itData.next();
|
---|
| 246 | itMask.next();
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 | //
|
---|
| 250 | if (!verbose) {
|
---|
[414] | 251 | cout << "Wrote " << nRows << " into individual FITS files" << endl;
|
---|
[191] | 252 | }
|
---|
| 253 | //
|
---|
| 254 | return True;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 |
|
---|
[209] | 258 | Int SDFITSImageWriter::convertStokes(Int val)
|
---|
[191] | 259 | {
|
---|
| 260 | Stokes::StokesTypes stokes = Stokes::RR;
|
---|
| 261 | if (val==0) {
|
---|
[213] | 262 | stokes = Stokes::XX;
|
---|
[191] | 263 | } else if (val==1) {
|
---|
[213] | 264 | stokes = Stokes::YY;
|
---|
[191] | 265 | } else if (val==2) {
|
---|
[213] | 266 | stokes = Stokes::XY;
|
---|
[191] | 267 | } else if (val==3) {
|
---|
[213] | 268 | stokes = Stokes::YX;
|
---|
[191] | 269 | } else {
|
---|
| 270 | stokes = Stokes::Undefined;
|
---|
| 271 | }
|
---|
| 272 | //
|
---|
| 273 | return Int(stokes);
|
---|
| 274 | }
|
---|