source: branches/alma/src/STAsciiWriter.cpp@ 1667

Last change on this file since 1667 was 1657, checked in by WataruKawasaki, 15 years ago

New Development: Yes

JIRA Issue: Yes (CAS-1433)

Ready to Release: Yes

Interface Changes: No

Put in Release Notes: No

Module(s): sd.save()

Description: Modified to read/write row-based flagging info.


  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
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 1657 2009-11-05 10:50:20Z WataruKawasaki $
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
62using namespace casa;
63using namespace asap;
64
65
66STAsciiWriter::STAsciiWriter()
67{;}
68
69STAsciiWriter::~STAsciiWriter()
70{;}
71
72
73Bool 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 if (rootName.length()==0) rootName = String("ascii");
92
93 Block<String> cols(4);
94 cols[0] = String("SCANNO");
95 cols[1] = String("CYCLENO");
96 cols[2] = String("BEAMNO");
97 cols[3] = String("IFNO");
98 TableIterator iter(tab, cols);
99 // Open data file
100 while ( !iter.pastEnd() ) {
101 Table t = iter.table();
102 ROTableRow row(t);
103 const TableRecord& rec = row.get(0);
104 String dirtype = stable.getDirectionRefString();
105 ostringstream onstr;
106 onstr << "SCAN" << rec.asuInt("SCANNO")
107 << "_CYCLE" << rec.asuInt("CYCLENO")
108 << "_BEAM" << rec.asuInt("BEAMNO")
109 << "_IF" << rec.asuInt("IFNO");
110 String fName = rootName + String(onstr) + String(".txt");
111 ofstream of(fName.chars(), ios::trunc);
112 int row0 = t.rowNumbers(tab)[0];
113 MDirection mdir = stable.getDirection(row0);
114 of << setfill('#') << setw(70) << "" << setfill(' ') << endl;
115 addLine(of, "Name", rec.asString("SRCNAME"));
116 addLine(of, "Position", String(dirtype+ " "+formatDirection(mdir)));
117 addLine(of, "Time", stable.getTime(row0,true));
118 addLine(of, "Flux Unit", hdr.fluxunit);
119 addLine(of, "Pol Type", stable.getPolType());
120 addLine(of, "Abcissa", stable.getAbcissaLabel(row0));
121 addLine(of, "Beam No", rec.asuInt("BEAMNO"));
122 addLine(of, "IF No", rec.asuInt("IFNO"));
123 String wcs = stable.frequencies().print(rec.asuInt("FREQ_ID"), True);
124 addLine(of, "WCS", wcs);
125 std::vector<double> restfreqs= stable.molecules().getRestFrequency(rec.asuInt("MOLECULE_ID"));
126 int nf = restfreqs.size();
127 //addLine(of, "Rest Freq.",
128 // stable.molecules().getRestFrequency(rec.asuInt("MOLECULE_ID") ));
129 addLine(of, "Rest Freq.", restfreqs[0]);
130 for ( unsigned int i=1; i<nf; ++i) {
131 addLine(of, " ", restfreqs[i]);
132 }
133 ostringstream osflagrow;
134 for ( unsigned int i=0; i<t.nrow(); ++i) {
135 osflagrow << "Pol" << i << ":" << ((row.get(i).asuInt("FLAGROW") > 0) ? "True" : "False") << " ";
136 }
137 addLine(of, "Row_Flagged", String(osflagrow));
138 of << setfill('#') << setw(70) << "" << setfill(' ') << endl;
139
140 of << std::left << setw(16) << "x";
141 for ( unsigned int i=0; i<t.nrow(); ++i ) {
142 ostringstream os,os1;
143 os << "y" << i;
144 os1 << "yf" << i;
145 of << setw(16) << String(os);
146 of << setw(7) << String(os1);
147 }
148 of << endl;
149 std::vector<double> abc = stable.getAbcissa(row0);
150 ROArrayColumn<Float> specCol(t,"SPECTRA");
151 ROArrayColumn<uChar> flagCol(t,"FLAGTRA");
152 Matrix<Float> specs = specCol.getColumn();
153 Matrix<uChar> flags = flagCol.getColumn();
154 for ( unsigned int i=0; i<specs.nrow(); ++i ) {
155 of << setw(16) << setprecision(8) << abc[i] ;
156 for ( unsigned int j=0; j<specs.ncolumn(); ++j ) {
157 of << setw(16) << setprecision(8) << specs(i,j) ;
158 of << setw(7) << Int(flags(i,j));
159 }
160 of << endl;
161 }
162 of.close();
163 ostringstream oss;
164 oss << "Wrote " << fName;
165 pushLog(String(oss));
166 ++iter;
167 }
168 return True;
169}
170
171
172String STAsciiWriter::formatDirection(const MDirection& md) const
173{
174 Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
175 Int prec = 7;
176
177 MVAngle mvLon(t[0]);
178 String sLon = mvLon.string(MVAngle::TIME,prec);
179 uInt tp = md.getRef().getType();
180 if (tp == MDirection::GALACTIC ||
181 tp == MDirection::SUPERGAL ) {
182 sLon = mvLon(0.0).string(MVAngle::ANGLE_CLEAN,prec);
183 }
184 MVAngle mvLat(t[1]);
185 String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec);
186 return sLon + String(" ") + sLat;
187}
188
189template <class T>
190void STAsciiWriter::addLine(ostream& of, const String& lbl, const T& value)
191{
192 String label = lbl+String(": ");
193 of << std::right << "# " << setw(15) << label << std::left
194 << setw(52) << value << setw(0) << "#"<< endl;
195}
Note: See TracBrowser for help on using the repository browser.