source: trunk/src/SDFITSImageWriter.cc @ 192

Last change on this file since 192 was 191, checked in by kil064, 19 years ago

class to write out SDMemTable rows as FITS Images

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
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 191 2005-01-14 00:05:40Z 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
61using namespace casa;
62using namespace asap;
63
64
65SDFITSImageWriter::SDFITSImageWriter()
66{;}
67
68SDFITSImageWriter::~SDFITSImageWriter()
69{;}
70
71
72Bool 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");
94
95// Output Image Shape; spectral axis to be updated
96
97   IPosition shapeOut(4,1);                 // spectral, direction (2), stokes
98
99// Axes (should be in header)
100
101   const uInt beamAxis = 0;
102   const uInt ifAxis = 1;
103   const uInt stokesAxis = 2;
104   const uInt chanAxis = 3;
105   const Unit RAD(String("rad"));
106
107// Temps
108
109   Vector<Int> whichStokes(1,1);
110   Array<Double> whichDir;
111   Vector<Double> latLon(2);
112   IPosition posDir(2,0);
113   Projection proj(Projection::SIN);                   // What should we use ?
114   Matrix<Double> xForm(2,2);
115   xForm = 0.0; xForm.diagonal() = 1.0;
116   Vector<Double> incLonLat(2,0.0);
117   Vector<Double> refPixLonLat(2,0.0);
118
119// Loop over rows
120
121   uInt maxMem = 128;
122   Bool preferVelocity = False;
123   Bool opticalVelocity = False;
124   Int bitPix = -32;                     // FLoating point
125   Float minPix = 1.0;
126   Float maxPix = -1.0;
127   Bool overWrite = True;
128   Bool degLast = False;
129   Bool reallyVerbose = False;
130//
131   String errMsg;
132   const uInt nRows = sdTable.nRow();
133   for (uInt iRow=0; iRow<nRows; iRow++) {
134
135// Get data
136
137      const MaskedArray<Float>& dataIn(sdTable.rowAsMaskedArray(iRow));
138      const Array<Float>& values = dataIn.getArray();
139      const Array<Bool>& mask = dataIn.getMask();
140      const IPosition& shapeIn = dataIn.shape();
141      shapeOut(0) = shapeIn(chanAxis);
142      TiledShape tShapeOut(shapeOut);
143
144// Update ObsInfo (time changes per integration)
145
146      Double dTmp;
147      time.get(iRow, dTmp);
148      MVEpoch tmp2(Quantum<Double>(dTmp, Unit(String("d"))));
149      MEpoch epoch(tmp2, timeRef);
150      oi.setObsDate(epoch);
151
152// Iterate through data in this row by spectra
153
154      ReadOnlyVectorIterator<Float> itData(values, chanAxis);
155      ReadOnlyVectorIterator<Bool> itMask(mask, chanAxis);
156      while (!itData.pastEnd()) {
157         const IPosition& pos = itData.pos();
158
159// Form SpectralCoordinate
160
161         Vector<uInt> iTmp;
162         freqid.get(iRow, iTmp);
163         SpectralCoordinate sC = sdTable.getCoordinate(iTmp(pos(ifAxis)));
164
165// Form DirectionCoordinate
166 
167         dir.get(iRow, whichDir);
168         posDir(0) = pos(beamAxis);
169         posDir(1) = 0;
170         latLon[0] = whichDir(posDir);
171//
172         posDir(0) = pos(beamAxis);
173         posDir(1) = 1;
174         latLon[1] = whichDir(posDir);
175         DirectionCoordinate dC(dirRef, proj, latLon[0], latLon[1],
176                               incLonLat[0], incLonLat[1], xForm,
177                               refPixLonLat[0], refPixLonLat[1]);
178
179// Form Stokes Coordinate (no true Stokes info yet);
180
181         whichStokes(0) = convertStokes(pos(stokesAxis));
182         StokesCoordinate stC(whichStokes);
183
184// Create CoordinateSystem
185
186         CoordinateSystem cSys;
187         cSys.addCoordinate(sC);
188         cSys.addCoordinate(dC);
189         cSys.addCoordinate(stC);
190         cSys.setObsInfo(oi);
191
192// Reform data into correct shape for output
193
194         Array<Float> t1(itData.array().reform(shapeOut));
195         Array<Bool> m1(itMask.array().reform(shapeOut));
196
197// Create aips++ Image
198
199     
200         TempImage<Float> tIm(tShapeOut, cSys);
201         tIm.put(t1);
202//
203         ArrayLattice<Bool> latMask(m1);
204         tIm.attachMask(latMask);
205
206// Write out as FITS Image file
207
208   
209         ostringstream oss;
210         oss << String("_row") << iRow << "_beam" << pos(0) << "_if" << pos(1) << "_pol" << pos(2) << ".fits";
211         String fileName = rootName + String(oss);
212         if (verbose) cerr << "Writing row " << iRow << " into file " << fileName << endl;
213//
214         Bool ok = ImageFITSConverter::ImageToFITS (errMsg, tIm, fileName, maxMem, preferVelocity,
215                                                    opticalVelocity, bitPix, minPix, maxPix, overWrite,
216                                                    degLast, reallyVerbose);
217         if (!ok) {
218            cerr << "Error writing fits - " << errMsg << endl;
219         }
220
221// Next spectrum
222
223         itData.next();
224         itMask.next();
225      }
226   }
227//
228   if (!verbose) {
229      cerr << "Wrote " << nRows << " into individual FITS files" << endl;
230   }
231//   
232   return True;
233}
234
235
236Int SDFITSImageWriter::convertStokes (Int val)
237{
238   Stokes::StokesTypes stokes = Stokes::RR;
239   if (val==0) {
240      stokes = Stokes::RR;
241   } else if (val==1) {
242      stokes = Stokes::LL;
243   } else if (val==2) {
244      stokes = Stokes::RL;
245   } else if (val==3) {
246      stokes = Stokes::LR;
247   } else {
248      stokes = Stokes::Undefined;
249   }
250//
251   return Int(stokes);
252}
Note: See TracBrowser for help on using the repository browser.