source: trunk/src/SDAsciiWriter.cc@ 207

Last change on this file since 207 was 198, checked in by kil064, 20 years ago

class to save SDMemTable as ascii text file

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1//#---------------------------------------------------------------------------
2//# SDAsciiWriter.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: SDAsciiWriter.cc 198 2005-01-14 04:03:22Z kil064 $
30//#---------------------------------------------------------------------------
31
32#include "SDAsciiWriter.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/Quanta/Quantum.h>
40#include <casa/Quanta/MVAngle.h>
41
42#include <coordinates/Coordinates/CoordinateUtil.h>
43#include <coordinates/Coordinates/SpectralCoordinate.h>
44#include <coordinates/Coordinates/DirectionCoordinate.h>
45#include <coordinates/Coordinates/StokesCoordinate.h>
46
47#include <measures/Measures/MEpoch.h>
48
49#include <tables/Tables/Table.h>
50#include <tables/Tables/ScalarColumn.h>
51#include <tables/Tables/ArrayColumn.h>
52
53#include "SDContainer.h"
54#include "SDMemTable.h"
55
56#include <casa/iostream.h>
57#include <casa/fstream.h>
58
59
60using namespace casa;
61using namespace asap;
62
63
64SDAsciiWriter::SDAsciiWriter()
65{;}
66
67SDAsciiWriter::~SDAsciiWriter()
68{;}
69
70
71Bool SDAsciiWriter::write(const SDMemTable& sdTable, const String& fileName)
72{
73
74// Get global Header from Table
75
76 SDHeader header = sdTable.getSDHeader();
77 MEpoch::Ref timeRef(MEpoch::UTC); // Should be in header
78 MDirection::Types dirRef(MDirection::J2000); // Should be in header
79
80// Column keywords
81
82 Table tab = sdTable.table();
83 ROArrayColumn<Double> dir(tab, String("DIRECTION"));
84 ROScalarColumn<Double> time(tab, "TIME");
85 ROArrayColumn<uInt> freqid(tab, "FREQID");
86 ROScalarColumn<String> src(tab, "SRCNAME");
87
88// Axes (should be in header)
89
90 const uInt beamAxis = 0;
91 const uInt ifAxis = 1;
92 const uInt polAxis = 2;
93 const uInt chanAxis = 3;
94
95// Temps
96
97 Vector<Int> whichStokes(1,1);
98 Array<Double> whichDir;
99 Vector<Double> lonLat(2);
100 IPosition posDir(2,0);
101 const Unit RAD(String("rad"));
102
103// Open file
104
105 String fName(fileName);
106 if (fileName.length()==0) fName = String("ascii.txt");
107 ofstream of(fName.chars(), ios::trunc);
108
109// Write header
110
111 of << "row beam IF pol source longitude latitude time nchan spectrum mask" << endl;
112
113// Loop over rows
114
115 const uInt nRows = sdTable.nRow();
116 for (uInt iRow=0; iRow<nRows; iRow++) {
117
118// Get data
119
120 const MaskedArray<Float>& dataIn(sdTable.rowAsMaskedArray(iRow));
121 const Array<Float>& values = dataIn.getArray();
122 const Array<Bool>& mask = dataIn.getMask();
123
124// Epoch
125
126 Double dTmp;
127 time.get(iRow, dTmp);
128 MVEpoch tmp2(Quantum<Double>(dTmp, Unit(String("d"))));
129 MEpoch epoch(tmp2, timeRef);
130
131// Iterate through data in this row by spectra
132
133 ReadOnlyVectorIterator<Float> itData(values, chanAxis);
134 ReadOnlyVectorIterator<Bool> itMask(mask, chanAxis);
135 while (!itData.pastEnd()) {
136 const IPosition& pos = itData.pos();
137
138// FreqID
139
140 Vector<uInt> iTmp;
141 freqid.get(iRow, iTmp);
142
143// Direction
144
145 dir.get(iRow, whichDir);
146 posDir(0) = pos(beamAxis);
147 posDir(1) = 0;
148 lonLat[0] = whichDir(posDir);
149//
150 posDir(0) = pos(beamAxis);
151 posDir(1) = 1;
152 lonLat[1] = whichDir(posDir);
153
154// Write. This formats the vectors as [,,,,] which we probably don't want.
155
156 of << iRow << " " << pos(beamAxis) << " " << pos(ifAxis) << " " << pos(polAxis) << " " <<
157 src(iRow) << " " << formatDirection(lonLat) << " " << dTmp << " " <<
158 itData.vector().nelements() << " " << itData.vector() << " " << itMask.vector() << endl;
159
160// Next spectrum
161
162 itData.next();
163 itMask.next();
164 }
165 }
166//
167 of.close();
168 cerr << "Wrote " << nRows << " rows into file " << fileName << endl;
169//
170 return True;
171}
172
173
174Int SDAsciiWriter::convertStokes (Int val)
175{
176 Stokes::StokesTypes stokes = Stokes::RR;
177 if (val==0) {
178 stokes = Stokes::RR;
179 } else if (val==1) {
180 stokes = Stokes::LL;
181 } else if (val==2) {
182 stokes = Stokes::RL;
183 } else if (val==3) {
184 stokes = Stokes::LR;
185 } else {
186 stokes = Stokes::Undefined;
187 }
188//
189 return Int(stokes);
190}
191
192
193String SDAsciiWriter::formatDirection (const Vector<Double>& lonLat)
194{
195 MVAngle x1(lonLat(0));
196 String s1 = x1.string(MVAngle::TIME, 12);
197//
198 MVAngle x2(lonLat(1));
199 String s2 = x2.string(MVAngle::ANGLE, 12);
200//
201 String ss = s1 + String(" ") + s2;
202 return ss;
203}
204
Note: See TracBrowser for help on using the repository browser.