source: trunk/src/STAsciiWriter.cpp@ 3138

Last change on this file since 3138 was 3106, checked in by Takeshi Nakazato, 8 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes/No

Interface Changes: Yes/No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...


Check-in asap modifications from Jim regarding casacore namespace conversion.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
RevLine 
[1819]1
[198]2//#---------------------------------------------------------------------------
[988]3//# STAsciiWriter.cc: ASAP class to write out single dish spectra as FITS images
[198]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 3106 2016-10-04 07:20:50Z TakeshiNakazato $
31//#---------------------------------------------------------------------------
32
33#include <casa/aips.h>
34#include <casa/Arrays/Array.h>
[988]35#include <casa/Arrays/Matrix.h>
[198]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>
[496]41#include <casa/Utilities/Assert.h>
[198]42
[209]43#include <casa/fstream.h>
[717]44#include <casa/sstream.h>
[988]45#include <casa/iomanip.h>
[2658]46#include <casa/Logging/LogIO.h>
[209]47
[198]48#include <measures/Measures/MEpoch.h>
49
50#include <tables/Tables/Table.h>
[988]51#include <tables/Tables/TableIter.h>
52#include <tables/Tables/TableRecord.h>
53#include <casa/Containers/RecordField.h>
54#include <tables/Tables/TableRow.h>
[198]55#include <tables/Tables/ScalarColumn.h>
56#include <tables/Tables/ArrayColumn.h>
57
[988]58#include "STDefs.h"
59#include "STHeader.h"
60#include "Scantable.h"
61#include "STAsciiWriter.h"
[198]62
[3106]63using namespace casacore;
[198]64using namespace asap;
65
66
[988]67STAsciiWriter::STAsciiWriter()
[198]68{;}
69
[988]70STAsciiWriter::~STAsciiWriter()
[198]71{;}
72
73
[988]74Bool STAsciiWriter::write(const Scantable& stable, const String& fileName)
[198]75{
76
77// Get global Header from Table
78
[988]79 STHeader hdr = stable.getHeader();
[198]80
81// Column keywords
82
[988]83 Table tab = stable.table();
[198]84
85// Temps
86
87 const Unit RAD(String("rad"));
88
[496]89// Open and write header file
[198]90
[496]91 String rootName(fileName);
[1819]92
[988]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;
[1552]106
107 if (rootName.length()==0) {
108 rootName = String("ascii");
109 }
110 if (tab.nrow() > 1) {
111 if (stable.nscan() > 1)
112 onstr << "_SCAN" << rec.asuInt("SCANNO");
113 if (stable.ncycle(rec.asuInt("SCANNO")) > 1)
114 onstr << "_CYCLE" << rec.asuInt("CYCLENO");
115 if (stable.nbeam(rec.asuInt("SCANNO")) > 1)
116 onstr << "_BEAM" << rec.asuInt("BEAMNO");
117 if (stable.nif(rec.asuInt("SCANNO")) > 1)
118 onstr << "_IF" << rec.asuInt("IFNO");
119 }
120
[988]121 String fName = rootName + String(onstr) + String(".txt");
122 ofstream of(fName.chars(), ios::trunc);
[1295]123 int row0 = t.rowNumbers(tab)[0];
[988]124 MDirection mdir = stable.getDirection(row0);
125 of << setfill('#') << setw(70) << "" << setfill(' ') << endl;
126 addLine(of, "Name", rec.asString("SRCNAME"));
127 addLine(of, "Position", String(dirtype+ " "+formatDirection(mdir)));
128 addLine(of, "Time", stable.getTime(row0,true));
129 addLine(of, "Flux Unit", hdr.fluxunit);
130 addLine(of, "Pol Type", stable.getPolType());
131 addLine(of, "Abcissa", stable.getAbcissaLabel(row0));
132 addLine(of, "Beam No", rec.asuInt("BEAMNO"));
133 addLine(of, "IF No", rec.asuInt("IFNO"));
[1375]134 String wcs = stable.frequencies().print(rec.asuInt("FREQ_ID"), True);
135 addLine(of, "WCS", wcs);
[1819]136 std::vector<double> restfreqs= stable.molecules().getRestFrequency(rec.asuInt("MOLECULE_ID"));
137 int nf = restfreqs.size();
138 //addLine(of, "Rest Freq.",
139 // stable.molecules().getRestFrequency(rec.asuInt("MOLECULE_ID") ));
[2294]140 if ( nf > 0 ) {
141 addLine(of, "Rest Freq.", restfreqs[0]);
142 for ( int i=1; i<nf; ++i) {
143 addLine(of, " ", restfreqs[i]);
144 }
[1819]145 }
[2294]146 else {
147 addLine(of, "Rest Freq.", "NONE" );
148 }
[1819]149 ostringstream osflagrow;
150 for ( unsigned int i=0; i<t.nrow(); ++i) {
151 osflagrow << "Pol" << i << ":" << ((row.get(i).asuInt("FLAGROW") > 0) ? "True" : "False") << " ";
152 }
153 addLine(of, "Row_Flagged", String(osflagrow));
[988]154 of << setfill('#') << setw(70) << "" << setfill(' ') << endl;
[496]155
[2335]156 of << std::left << setw(16) << "#x";
[996]157 for ( unsigned int i=0; i<t.nrow(); ++i ) {
[994]158 ostringstream os,os1;
159 os << "y" << i;
160 os1 << "yf" << i;
161 of << setw(16) << String(os);
162 of << setw(7) << String(os1);
[988]163 }
164 of << endl;
165 std::vector<double> abc = stable.getAbcissa(row0);
166 ROArrayColumn<Float> specCol(t,"SPECTRA");
167 ROArrayColumn<uChar> flagCol(t,"FLAGTRA");
168 Matrix<Float> specs = specCol.getColumn();
169 Matrix<uChar> flags = flagCol.getColumn();
[996]170 for ( unsigned int i=0; i<specs.nrow(); ++i ) {
[988]171 of << setw(16) << setprecision(8) << abc[i] ;
[996]172 for ( unsigned int j=0; j<specs.ncolumn(); ++j ) {
[988]173 of << setw(16) << setprecision(8) << specs(i,j) ;
174 of << setw(7) << Int(flags(i,j));
[198]175 }
[988]176 of << endl;
177 }
178 of.close();
[2658]179 LogIO os( LogOrigin( "STAsciiWriter") ) ;
180 os << "Wrote " << fName << LogIO::POST;
[988]181 ++iter;
182 }
183 return True;
[198]184}
185
186
[988]187String STAsciiWriter::formatDirection(const MDirection& md) const
188{
189 Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
190 Int prec = 7;
[198]191
[988]192 MVAngle mvLon(t[0]);
193 String sLon = mvLon.string(MVAngle::TIME,prec);
194 uInt tp = md.getRef().getType();
195 if (tp == MDirection::GALACTIC ||
196 tp == MDirection::SUPERGAL ) {
197 sLon = mvLon(0.0).string(MVAngle::ANGLE_CLEAN,prec);
198 }
199 MVAngle mvLat(t[1]);
200 String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec);
201 return sLon + String(" ") + sLat;
202}
[717]203
[988]204template <class T>
205void STAsciiWriter::addLine(ostream& of, const String& lbl, const T& value)
206{
207 String label = lbl+String(": ");
208 of << std::right << "# " << setw(15) << label << std::left
209 << setw(52) << value << setw(0) << "#"<< endl;
[198]210}
Note: See TracBrowser for help on using the repository browser.