source: trunk/src/STAsciiWriter.cpp @ 2658

Last change on this file since 2658 was 2658, checked in by Malte Marquarding, 12 years ago

Ticket #199: Excised Logger::pushLog; now everything is using LogIO

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 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 2658 2012-10-10 03:29:17Z MalteMarquarding $
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#include <casa/Logging/LogIO.h>
47
48#include <measures/Measures/MEpoch.h>
49
50#include <tables/Tables/Table.h>
51#include <tables/Tables/TableIter.h>
52#include <tables/Tables/TableRecord.h>
53#include <casa/Containers/RecordField.h>
54#include <tables/Tables/TableRow.h>
55#include <tables/Tables/ScalarColumn.h>
56#include <tables/Tables/ArrayColumn.h>
57
58#include "STDefs.h"
59#include "STHeader.h"
60#include "Scantable.h"
61#include "STAsciiWriter.h"
62
63using namespace casa;
64using namespace asap;
65
66
67STAsciiWriter::STAsciiWriter()
68{;}
69
70STAsciiWriter::~STAsciiWriter()
71{;}
72
73
74Bool STAsciiWriter::write(const Scantable& stable, const String& fileName)
75{
76
77// Get global Header from Table
78
79   STHeader hdr = stable.getHeader();
80
81// Column keywords
82
83   Table tab = stable.table();
84
85// Temps
86
87   const Unit RAD(String("rad"));
88
89// Open and write header file
90
91   String rootName(fileName);
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
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
121    String fName = rootName + String(onstr) + String(".txt");
122    ofstream of(fName.chars(), ios::trunc);
123    int row0 = t.rowNumbers(tab)[0];
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"));
134    String wcs = stable.frequencies().print(rec.asuInt("FREQ_ID"), True);
135    addLine(of, "WCS", wcs);
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") ));
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      }
145    }
146    else {
147      addLine(of, "Rest Freq.", "NONE" );
148    }
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));
154    of << setfill('#') << setw(70) << "" << setfill(' ') << endl;
155
156    of << std::left << setw(16) << "#x";
157    for ( unsigned int i=0; i<t.nrow(); ++i ) {
158      ostringstream os,os1;
159      os << "y" << i;
160      os1 << "yf" << i;
161      of << setw(16) << String(os);
162      of << setw(7) << String(os1);
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();
170    for ( unsigned int i=0; i<specs.nrow(); ++i ) {
171      of << setw(16) << setprecision(8) << abc[i] ;
172      for ( unsigned int j=0; j<specs.ncolumn(); ++j ) {
173        of << setw(16) << setprecision(8) << specs(i,j) ;
174        of << setw(7) << Int(flags(i,j));
175      }
176      of << endl;
177    }
178    of.close();
179    LogIO os( LogOrigin( "STAsciiWriter") ) ;   
180    os << "Wrote " << fName << LogIO::POST;
181    ++iter;
182  }
183  return True;
184}
185
186
187String STAsciiWriter::formatDirection(const MDirection& md) const
188{
189  Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
190  Int prec = 7;
191
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}
203
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;
210}
Note: See TracBrowser for help on using the repository browser.