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 279 2005-01-24 03:12:39Z 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 | #include <casa/OS/Directory.h>
|
---|
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 |
|
---|
59 | #include "SDDefs.h"
|
---|
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,
|
---|
75 | const String& dirName, Bool verbose)
|
---|
76 | {
|
---|
77 |
|
---|
78 | // Get global Header from Table
|
---|
79 |
|
---|
80 | SDHeader header = sdTable.getSDHeader();
|
---|
81 | MEpoch::Ref timeRef(sdTable.getTimeReference());
|
---|
82 | MDirection::Types dirRef = sdTable.getDirectionReference();
|
---|
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();
|
---|
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");
|
---|
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 |
|
---|
104 | const uInt beamAxis = asap::BeamAxis;
|
---|
105 | const uInt ifAxis = asap::IFAxis;
|
---|
106 | const uInt stokesAxis = asap::PolAxis;
|
---|
107 | const uInt chanAxis = asap::ChanAxis;
|
---|
108 | const Unit RAD(String("rad"));
|
---|
109 |
|
---|
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 |
|
---|
120 | // Temps
|
---|
121 |
|
---|
122 | Vector<Int> whichStokes(1,1);
|
---|
123 | Array<Double> whichDir;
|
---|
124 | Vector<Double> lonLat(2);
|
---|
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;
|
---|
160 | timeCol.get(iRow, dTmp);
|
---|
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;
|
---|
175 | freqidCol.get(iRow, iTmp);
|
---|
176 | SpectralCoordinate sC = sdTable.getCoordinate(iTmp(pos(ifAxis)));
|
---|
177 |
|
---|
178 | // Form DirectionCoordinate
|
---|
179 |
|
---|
180 | dirCol.get(iRow, whichDir);
|
---|
181 | posDir(0) = pos(beamAxis);
|
---|
182 | posDir(1) = 0;
|
---|
183 | lonLat[0] = whichDir(posDir);
|
---|
184 | //
|
---|
185 | posDir(0) = pos(beamAxis);
|
---|
186 | posDir(1) = 1;
|
---|
187 | lonLat[1] = whichDir(posDir);
|
---|
188 | DirectionCoordinate dC(dirRef, proj, lonLat[0], lonLat[1],
|
---|
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 |
|
---|
213 | TempImage<Float> tIm(tShapeOut, cSys);
|
---|
214 | tIm.put(t1);
|
---|
215 | //
|
---|
216 | ArrayLattice<Bool> latMask(m1);
|
---|
217 | tIm.attachMask(latMask);
|
---|
218 |
|
---|
219 | // Write out as FITS Image file
|
---|
220 |
|
---|
221 | ostringstream oss;
|
---|
222 | oss << "row" << iRow << "_beam" << pos(0) << "_if"
|
---|
223 | << pos(1) << "_pol" << pos(2) << "_" << srcCol(iRow) << ".fits";
|
---|
224 | String tS(oss);
|
---|
225 | String fileName = dirName2 + String("/") + tS;
|
---|
226 | if (verbose) cerr << "Writing row " << iRow
|
---|
227 | << " into file '" << tS << "'" << endl;
|
---|
228 | //
|
---|
229 | Bool ok = ImageFITSConverter::ImageToFITS(errMsg, tIm, fileName,
|
---|
230 | maxMem, preferVelocity,
|
---|
231 | opticalVelocity, bitPix,
|
---|
232 | minPix, maxPix, overWrite,
|
---|
233 | degLast, reallyVerbose);
|
---|
234 | if (!ok) {
|
---|
235 | cerr << "Error writing fits - " << errMsg << endl;
|
---|
236 | }
|
---|
237 |
|
---|
238 | // Next spectrum
|
---|
239 |
|
---|
240 | itData.next();
|
---|
241 | itMask.next();
|
---|
242 | }
|
---|
243 | }
|
---|
244 | //
|
---|
245 | if (!verbose) {
|
---|
246 | cerr << "Wrote " << nRows << " into individual FITS files" << endl;
|
---|
247 | }
|
---|
248 | //
|
---|
249 | return True;
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | Int SDFITSImageWriter::convertStokes(Int val)
|
---|
254 | {
|
---|
255 | Stokes::StokesTypes stokes = Stokes::RR;
|
---|
256 | if (val==0) {
|
---|
257 | stokes = Stokes::XX;
|
---|
258 | } else if (val==1) {
|
---|
259 | stokes = Stokes::YY;
|
---|
260 | } else if (val==2) {
|
---|
261 | stokes = Stokes::XY;
|
---|
262 | } else if (val==3) {
|
---|
263 | stokes = Stokes::YX;
|
---|
264 | } else {
|
---|
265 | stokes = Stokes::Undefined;
|
---|
266 | }
|
---|
267 | //
|
---|
268 | return Int(stokes);
|
---|
269 | }
|
---|