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