1 | |
---|
2 | //#--------------------------------------------------------------------------- |
---|
3 | //# STAsciiWriter.cc: ASAP class to write out single dish spectra as FITS images |
---|
4 | //#--------------------------------------------------------------------------- |
---|
5 | //# Copyright (C) 2004 |
---|
6 | //# ATNF |
---|
7 | //# |
---|
8 | //# This program is free software; you can redistribute it and/or modify it |
---|
9 | //# under the terms of the GNU General Public License as published by the Free |
---|
10 | //# Software Foundation; either version 2 of the License, or (at your option) |
---|
11 | //# any later version. |
---|
12 | //# |
---|
13 | //# This program is distributed in the hope that it will be useful, but |
---|
14 | //# WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
16 | //# Public License for more details. |
---|
17 | //# |
---|
18 | //# You should have received a copy of the GNU General Public License along |
---|
19 | //# with this program; if not, write to the Free Software Foundation, Inc., |
---|
20 | //# 675 Massachusetts Ave, Cambridge, MA 02139, USA. |
---|
21 | //# |
---|
22 | //# Correspondence concerning this software should be addressed as follows: |
---|
23 | //# Internet email: Malte.Marquarding@csiro.au |
---|
24 | //# Postal address: Malte Marquarding, |
---|
25 | //# Australia Telescope National Facility, |
---|
26 | //# P.O. Box 76, |
---|
27 | //# Epping, NSW, 2121, |
---|
28 | //# AUSTRALIA |
---|
29 | //# |
---|
30 | //# $Id: STAsciiWriter.cpp 1757 2010-06-09 09:03:06Z KanaSugimoto $ |
---|
31 | //#--------------------------------------------------------------------------- |
---|
32 | |
---|
33 | #include <casa/aips.h> |
---|
34 | #include <casa/Arrays/Array.h> |
---|
35 | #include <casa/Arrays/Matrix.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 | #include <casa/Utilities/Assert.h> |
---|
42 | |
---|
43 | #include <casa/fstream.h> |
---|
44 | #include <casa/sstream.h> |
---|
45 | #include <casa/iomanip.h> |
---|
46 | |
---|
47 | #include <measures/Measures/MEpoch.h> |
---|
48 | |
---|
49 | #include <tables/Tables/Table.h> |
---|
50 | #include <tables/Tables/TableIter.h> |
---|
51 | #include <tables/Tables/TableRecord.h> |
---|
52 | #include <casa/Containers/RecordField.h> |
---|
53 | #include <tables/Tables/TableRow.h> |
---|
54 | #include <tables/Tables/ScalarColumn.h> |
---|
55 | #include <tables/Tables/ArrayColumn.h> |
---|
56 | |
---|
57 | #include "STDefs.h" |
---|
58 | #include "STHeader.h" |
---|
59 | #include "Scantable.h" |
---|
60 | #include "STAsciiWriter.h" |
---|
61 | |
---|
62 | using namespace casa; |
---|
63 | using namespace asap; |
---|
64 | |
---|
65 | |
---|
66 | STAsciiWriter::STAsciiWriter() |
---|
67 | {;} |
---|
68 | |
---|
69 | STAsciiWriter::~STAsciiWriter() |
---|
70 | {;} |
---|
71 | |
---|
72 | |
---|
73 | Bool STAsciiWriter::write(const Scantable& stable, const String& fileName) |
---|
74 | { |
---|
75 | |
---|
76 | // Get global Header from Table |
---|
77 | |
---|
78 | STHeader hdr = stable.getHeader(); |
---|
79 | |
---|
80 | // Column keywords |
---|
81 | |
---|
82 | Table tab = stable.table(); |
---|
83 | |
---|
84 | // Temps |
---|
85 | |
---|
86 | const Unit RAD(String("rad")); |
---|
87 | |
---|
88 | // Open and write header file |
---|
89 | |
---|
90 | String rootName(fileName); |
---|
91 | |
---|
92 | Block<String> cols(4); |
---|
93 | cols[0] = String("SCANNO"); |
---|
94 | cols[1] = String("CYCLENO"); |
---|
95 | cols[2] = String("BEAMNO"); |
---|
96 | cols[3] = String("IFNO"); |
---|
97 | TableIterator iter(tab, cols); |
---|
98 | // Open data file |
---|
99 | while ( !iter.pastEnd() ) { |
---|
100 | Table t = iter.table(); |
---|
101 | ROTableRow row(t); |
---|
102 | const TableRecord& rec = row.get(0); |
---|
103 | String dirtype = stable.getDirectionRefString(); |
---|
104 | ostringstream onstr; |
---|
105 | |
---|
106 | if (rootName.length()==0) { |
---|
107 | rootName = String("ascii"); |
---|
108 | } |
---|
109 | if (tab.nrow() > 1) { |
---|
110 | if (stable.nscan() > 1) |
---|
111 | onstr << "_SCAN" << rec.asuInt("SCANNO"); |
---|
112 | if (stable.ncycle(rec.asuInt("SCANNO")) > 1) |
---|
113 | onstr << "_CYCLE" << rec.asuInt("CYCLENO"); |
---|
114 | if (stable.nbeam(rec.asuInt("SCANNO")) > 1) |
---|
115 | onstr << "_BEAM" << rec.asuInt("BEAMNO"); |
---|
116 | if (stable.nif(rec.asuInt("SCANNO")) > 1) |
---|
117 | onstr << "_IF" << rec.asuInt("IFNO"); |
---|
118 | } |
---|
119 | |
---|
120 | String fName = rootName + String(onstr) + String(".txt"); |
---|
121 | ofstream of(fName.chars(), ios::trunc); |
---|
122 | int row0 = t.rowNumbers(tab)[0]; |
---|
123 | MDirection mdir = stable.getDirection(row0); |
---|
124 | of << setfill('#') << setw(70) << "" << setfill(' ') << endl; |
---|
125 | addLine(of, "Name", rec.asString("SRCNAME")); |
---|
126 | addLine(of, "Position", String(dirtype+ " "+formatDirection(mdir))); |
---|
127 | addLine(of, "Time", stable.getTime(row0,true)); |
---|
128 | addLine(of, "Flux Unit", hdr.fluxunit); |
---|
129 | addLine(of, "Pol Type", stable.getPolType()); |
---|
130 | addLine(of, "Abcissa", stable.getAbcissaLabel(row0)); |
---|
131 | addLine(of, "Beam No", rec.asuInt("BEAMNO")); |
---|
132 | addLine(of, "IF No", rec.asuInt("IFNO")); |
---|
133 | String wcs = stable.frequencies().print(rec.asuInt("FREQ_ID"), True); |
---|
134 | addLine(of, "WCS", wcs); |
---|
135 | std::vector<double> restfreqs= stable.molecules().getRestFrequency(rec.asuInt("MOLECULE_ID")); |
---|
136 | int nf = restfreqs.size(); |
---|
137 | //addLine(of, "Rest Freq.", |
---|
138 | // stable.molecules().getRestFrequency(rec.asuInt("MOLECULE_ID") )); |
---|
139 | addLine(of, "Rest Freq.", restfreqs[0]); |
---|
140 | for ( unsigned int i=1; i<nf; ++i) { |
---|
141 | addLine(of, " ", restfreqs[i]); |
---|
142 | } |
---|
143 | ostringstream osflagrow; |
---|
144 | for ( unsigned int i=0; i<t.nrow(); ++i) { |
---|
145 | osflagrow << "Pol" << i << ":" << ((row.get(i).asuInt("FLAGROW") > 0) ? "True" : "False") << " "; |
---|
146 | } |
---|
147 | addLine(of, "Row_Flagged", String(osflagrow)); |
---|
148 | of << setfill('#') << setw(70) << "" << setfill(' ') << endl; |
---|
149 | |
---|
150 | of << std::left << setw(16) << "x"; |
---|
151 | for ( unsigned int i=0; i<t.nrow(); ++i ) { |
---|
152 | ostringstream os,os1; |
---|
153 | os << "y" << i; |
---|
154 | os1 << "yf" << i; |
---|
155 | of << setw(16) << String(os); |
---|
156 | of << setw(7) << String(os1); |
---|
157 | } |
---|
158 | of << endl; |
---|
159 | std::vector<double> abc = stable.getAbcissa(row0); |
---|
160 | ROArrayColumn<Float> specCol(t,"SPECTRA"); |
---|
161 | ROArrayColumn<uChar> flagCol(t,"FLAGTRA"); |
---|
162 | Matrix<Float> specs = specCol.getColumn(); |
---|
163 | Matrix<uChar> flags = flagCol.getColumn(); |
---|
164 | for ( unsigned int i=0; i<specs.nrow(); ++i ) { |
---|
165 | of << setw(16) << setprecision(8) << abc[i] ; |
---|
166 | for ( unsigned int j=0; j<specs.ncolumn(); ++j ) { |
---|
167 | of << setw(16) << setprecision(8) << specs(i,j) ; |
---|
168 | of << setw(7) << Int(flags(i,j)); |
---|
169 | } |
---|
170 | of << endl; |
---|
171 | } |
---|
172 | of.close(); |
---|
173 | ostringstream oss; |
---|
174 | oss << "Wrote " << fName; |
---|
175 | pushLog(String(oss)); |
---|
176 | ++iter; |
---|
177 | } |
---|
178 | return True; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | String STAsciiWriter::formatDirection(const MDirection& md) const |
---|
183 | { |
---|
184 | Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue(); |
---|
185 | Int prec = 7; |
---|
186 | |
---|
187 | MVAngle mvLon(t[0]); |
---|
188 | String sLon = mvLon.string(MVAngle::TIME,prec); |
---|
189 | uInt tp = md.getRef().getType(); |
---|
190 | if (tp == MDirection::GALACTIC || |
---|
191 | tp == MDirection::SUPERGAL ) { |
---|
192 | sLon = mvLon(0.0).string(MVAngle::ANGLE_CLEAN,prec); |
---|
193 | } |
---|
194 | MVAngle mvLat(t[1]); |
---|
195 | String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec); |
---|
196 | return sLon + String(" ") + sLat; |
---|
197 | } |
---|
198 | |
---|
199 | template <class T> |
---|
200 | void STAsciiWriter::addLine(ostream& of, const String& lbl, const T& value) |
---|
201 | { |
---|
202 | String label = lbl+String(": "); |
---|
203 | of << std::right << "# " << setw(15) << label << std::left |
---|
204 | << setw(52) << value << setw(0) << "#"<< endl; |
---|
205 | } |
---|