source: trunk/src/SDAsciiWriter.cc @ 495

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

write time out as formatted UTC string

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 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 495 2005-02-21 07:15:32Z kil064 $
30//#---------------------------------------------------------------------------
31
32#include <casa/aips.h>
33#include <casa/Arrays/Array.h>
34#include <casa/Arrays/Vector.h>
35#include <casa/Arrays/VectorIter.h>
36#include <casa/Utilities/CountedPtr.h>
37#include <casa/Quanta/Quantum.h>
38#include <casa/Quanta/MVAngle.h>
39
40
41#include <casa/iostream.h>
42#include <casa/fstream.h>
43
44#include <coordinates/Coordinates/CoordinateUtil.h>
45#include <coordinates/Coordinates/SpectralCoordinate.h>
46#include <coordinates/Coordinates/DirectionCoordinate.h>
47#include <coordinates/Coordinates/StokesCoordinate.h>
48
49#include <measures/Measures/MEpoch.h>
50
51#include <tables/Tables/Table.h>
52#include <tables/Tables/ScalarColumn.h>
53#include <tables/Tables/ArrayColumn.h>
54
55#include "SDDefs.h"
56#include "SDContainer.h"
57#include "SDMemTable.h"
58#include "SDAsciiWriter.h"
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, Bool toStokes)
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// Temps
89
90   Vector<Int> whichStokes(1,1);
91   Array<Double> whichDir;
92   Vector<Double> lonLat(2);
93   IPosition posDir(2,0);
94   const Unit RAD(String("rad"));
95
96// Open file
97
98   String fName(fileName);
99   if (fileName.length()==0) fName = String("ascii.txt");
100   ofstream of(fName.chars(), ios::trunc);
101
102// Write header
103
104   of << "row beam IF pol source longitude latitude time nchan spectrum mask"
105      << endl;
106   
107// Loop over rows
108
109   const uInt nRows = sdTable.nRow();
110   for (uInt iRow=0; iRow<nRows; iRow++) {
111
112// Get data
113
114      const MaskedArray<Float>& dataIn(sdTable.rowAsMaskedArray(iRow,toStokes));
115      const Array<Float>& values = dataIn.getArray();
116      const Array<Bool>& mask = dataIn.getMask();
117
118// Iterate through data in this row by spectra
119
120      ReadOnlyVectorIterator<Float> itData(values, asap::ChanAxis);
121      ReadOnlyVectorIterator<Bool> itMask(mask, asap::ChanAxis);
122      while (!itData.pastEnd()) {
123         const IPosition& pos = itData.pos();
124
125// FreqID
126
127         Vector<uInt> iTmp;
128         freqid.get(iRow, iTmp);
129
130// Direction
131 
132         dir.get(iRow, whichDir);
133         posDir(0) = pos(asap::BeamAxis);
134         posDir(1) = 0;
135         lonLat[0] = whichDir(posDir);
136//
137         posDir(0) = pos(asap::BeamAxis);
138         posDir(1) = 1;
139         lonLat[1] = whichDir(posDir);
140
141// Write preamble
142
143         of << iRow << "  " << pos(asap::BeamAxis) << " " <<  pos(asap::IFAxis) << " " <<
144               pos(asap::PolAxis) << " " <<
145               src(iRow) <<  " " << formatDirection(lonLat) << " " <<
146               sdTable.getTime(iRow,True) << " " << itData.vector().nelements() << " ";
147// Write data
148
149         const Vector<Float>& data = itData.vector();
150         const Vector<Bool>& mask = itMask.vector();
151         const uInt n = data.nelements();
152         for (uInt i=0; i<n; i++) {
153            of << data[i] << " ";
154         }
155// Write mask
156
157         for (uInt i=0; i<n; i++) {
158            of << mask[i] << " ";
159         }
160         of << endl;
161
162// Next spectrum
163
164         itData.next();
165         itMask.next();
166      }
167   }
168//
169   of.close();
170   cout << "Wrote " << nRows << " rows into file " << fileName << endl;
171//   
172   return True;
173}
174
175
176
177String SDAsciiWriter::formatDirection(const Vector<Double>& lonLat)
178{
179   MVAngle x1(lonLat(0));
180   String s1 = x1.string(MVAngle::TIME, 12);
181//
182   MVAngle x2(lonLat(1));
183   String s2 = x2.string(MVAngle::ANGLE, 12);
184//
185   String ss = s1 + String(" ") + s2;
186   return ss;
187}
188
Note: See TracBrowser for help on using the repository browser.