source: trunk/src/STAsciiWriter.cpp @ 1552

Last change on this file since 1552 was 1552, checked in by Malte Marquarding, 15 years ago

Enhancement Ticket #155; chnaged the wy output filenames are generated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1//#---------------------------------------------------------------------------
2//# STAsciiWriter.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: STAsciiWriter.cpp 1552 2009-03-27 10:25:44Z MalteMarquarding $
30//#---------------------------------------------------------------------------
31
32#include <casa/aips.h>
33#include <casa/Arrays/Array.h>
34#include <casa/Arrays/Matrix.h>
35#include <casa/Arrays/Vector.h>
36#include <casa/Arrays/VectorIter.h>
37#include <casa/Utilities/CountedPtr.h>
38#include <casa/Quanta/Quantum.h>
39#include <casa/Quanta/MVAngle.h>
40#include <casa/Utilities/Assert.h>
41
42#include <casa/fstream.h>
43#include <casa/sstream.h>
44#include <casa/iomanip.h>
45
46#include <measures/Measures/MEpoch.h>
47
48#include <tables/Tables/Table.h>
49#include <tables/Tables/TableIter.h>
50#include <tables/Tables/TableRecord.h>
51#include <casa/Containers/RecordField.h>
52#include <tables/Tables/TableRow.h>
53#include <tables/Tables/ScalarColumn.h>
54#include <tables/Tables/ArrayColumn.h>
55
56#include "STDefs.h"
57#include "STHeader.h"
58#include "Scantable.h"
59#include "STAsciiWriter.h"
60
61using namespace casa;
62using namespace asap;
63
64
65STAsciiWriter::STAsciiWriter()
66{;}
67
68STAsciiWriter::~STAsciiWriter()
69{;}
70
71
72Bool STAsciiWriter::write(const Scantable& stable, const String& fileName)
73{
74
75// Get global Header from Table
76
77   STHeader hdr = stable.getHeader();
78
79// Column keywords
80
81   Table tab = stable.table();
82
83// Temps
84
85   const Unit RAD(String("rad"));
86
87// Open and write header file
88
89   String rootName(fileName);
90   
91  Block<String> cols(4);
92  cols[0] = String("SCANNO");
93  cols[1] = String("CYCLENO");
94  cols[2] = String("BEAMNO");
95  cols[3] = String("IFNO");
96  TableIterator iter(tab, cols);
97  // Open data file
98  while ( !iter.pastEnd() ) {
99    Table t = iter.table();
100    ROTableRow row(t);
101    const TableRecord& rec = row.get(0);
102    String dirtype = stable.getDirectionRefString();
103    ostringstream onstr;
104
105    if (rootName.length()==0) {
106      rootName = String("ascii");
107    }
108    if (tab.nrow() > 1) {
109      if (stable.nscan() > 1)
110        onstr << "_SCAN" << rec.asuInt("SCANNO");
111      if (stable.ncycle(rec.asuInt("SCANNO")) > 1)
112        onstr << "_CYCLE" << rec.asuInt("CYCLENO");
113      if (stable.nbeam(rec.asuInt("SCANNO")) > 1)
114        onstr << "_BEAM" << rec.asuInt("BEAMNO");
115      if (stable.nif(rec.asuInt("SCANNO")) > 1)
116        onstr << "_IF" << rec.asuInt("IFNO");
117    }
118
119    String fName = rootName + String(onstr) + String(".txt");
120    ofstream of(fName.chars(), ios::trunc);
121    int row0 = t.rowNumbers(tab)[0];
122    MDirection mdir = stable.getDirection(row0);
123    of << setfill('#') << setw(70) << "" << setfill(' ') << endl;
124    addLine(of, "Name", rec.asString("SRCNAME"));
125    addLine(of, "Position", String(dirtype+ " "+formatDirection(mdir)));
126    addLine(of, "Time", stable.getTime(row0,true));
127    addLine(of, "Flux Unit", hdr.fluxunit);
128    addLine(of, "Pol Type", stable.getPolType());
129    addLine(of, "Abcissa", stable.getAbcissaLabel(row0));
130    addLine(of, "Beam No", rec.asuInt("BEAMNO"));
131    addLine(of, "IF No", rec.asuInt("IFNO"));
132    String wcs = stable.frequencies().print(rec.asuInt("FREQ_ID"), True);
133    addLine(of, "WCS", wcs);
134    addLine(of, "Rest Freq.",
135            stable.molecules().getRestFrequency(rec.asuInt("MOLECULE_ID") ));
136    of << setfill('#') << setw(70) << "" << setfill(' ') << endl;
137
138    of << std::left << setw(16) << "x";
139    for ( unsigned int i=0; i<t.nrow(); ++i ) {
140      ostringstream os,os1;
141      os << "y" << i;
142      os1 << "yf" << i;
143      of << setw(16) << String(os);
144      of << setw(7) << String(os1);
145    }
146    of << endl;
147    std::vector<double> abc = stable.getAbcissa(row0);
148    ROArrayColumn<Float> specCol(t,"SPECTRA");
149    ROArrayColumn<uChar> flagCol(t,"FLAGTRA");
150    Matrix<Float> specs = specCol.getColumn();
151    Matrix<uChar> flags = flagCol.getColumn();
152    for ( unsigned int i=0; i<specs.nrow(); ++i ) {
153      of << setw(16) << setprecision(8) << abc[i] ;
154      for ( unsigned int j=0; j<specs.ncolumn(); ++j ) {
155        of << setw(16) << setprecision(8) << specs(i,j) ;
156        of << setw(7) << Int(flags(i,j));
157      }
158      of << endl;
159    }
160    of.close();
161    ostringstream oss;
162    oss << "Wrote " << fName;
163    pushLog(String(oss));
164    ++iter;
165  }
166  return True;
167}
168
169
170String STAsciiWriter::formatDirection(const MDirection& md) const
171{
172  Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
173  Int prec = 7;
174
175  MVAngle mvLon(t[0]);
176  String sLon = mvLon.string(MVAngle::TIME,prec);
177  uInt tp = md.getRef().getType();
178  if (tp == MDirection::GALACTIC ||
179      tp == MDirection::SUPERGAL ) {
180    sLon = mvLon(0.0).string(MVAngle::ANGLE_CLEAN,prec);
181  }
182  MVAngle mvLat(t[1]);
183  String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec);
184  return sLon + String(" ") + sLat;
185}
186
187template <class T>
188void STAsciiWriter::addLine(ostream& of, const String& lbl, const T& value)
189{
190  String label = lbl+String(": ");
191  of << std::right << "# " << setw(15) << label << std::left
192     << setw(52) << value << setw(0) << "#"<< endl;
193}
Note: See TracBrowser for help on using the repository browser.