[198] | 1 | //#--------------------------------------------------------------------------- |
---|
[988] | 2 | //# STAsciiWriter.cc: ASAP class to write out single dish spectra as FITS images |
---|
[198] | 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 1293 2006-11-06 10:54:21Z mar637 $ |
---|
| 30 | //#--------------------------------------------------------------------------- |
---|
| 31 | |
---|
| 32 | #include <casa/aips.h> |
---|
| 33 | #include <casa/Arrays/Array.h> |
---|
[988] | 34 | #include <casa/Arrays/Matrix.h> |
---|
[198] | 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> |
---|
[496] | 40 | #include <casa/Utilities/Assert.h> |
---|
[198] | 41 | |
---|
[209] | 42 | #include <casa/fstream.h> |
---|
[717] | 43 | #include <casa/sstream.h> |
---|
[988] | 44 | #include <casa/iomanip.h> |
---|
[209] | 45 | |
---|
[198] | 46 | #include <measures/Measures/MEpoch.h> |
---|
| 47 | |
---|
| 48 | #include <tables/Tables/Table.h> |
---|
[988] | 49 | #include <tables/Tables/TableIter.h> |
---|
| 50 | #include <tables/Tables/TableRecord.h> |
---|
| 51 | #include <casa/Containers/RecordField.h> |
---|
| 52 | #include <tables/Tables/TableRow.h> |
---|
[198] | 53 | #include <tables/Tables/ScalarColumn.h> |
---|
| 54 | #include <tables/Tables/ArrayColumn.h> |
---|
| 55 | |
---|
[988] | 56 | #include "STDefs.h" |
---|
| 57 | #include "STHeader.h" |
---|
| 58 | #include "Scantable.h" |
---|
| 59 | #include "STAsciiWriter.h" |
---|
[198] | 60 | |
---|
| 61 | using namespace casa; |
---|
| 62 | using namespace asap; |
---|
| 63 | |
---|
| 64 | |
---|
[988] | 65 | STAsciiWriter::STAsciiWriter() |
---|
[198] | 66 | {;} |
---|
| 67 | |
---|
[988] | 68 | STAsciiWriter::~STAsciiWriter() |
---|
[198] | 69 | {;} |
---|
| 70 | |
---|
| 71 | |
---|
[988] | 72 | Bool STAsciiWriter::write(const Scantable& stable, const String& fileName) |
---|
[198] | 73 | { |
---|
| 74 | |
---|
| 75 | // Get global Header from Table |
---|
| 76 | |
---|
[988] | 77 | STHeader hdr = stable.getHeader(); |
---|
[198] | 78 | |
---|
| 79 | // Column keywords |
---|
| 80 | |
---|
[988] | 81 | Table tab = stable.table(); |
---|
[198] | 82 | |
---|
| 83 | // Temps |
---|
| 84 | |
---|
| 85 | const Unit RAD(String("rad")); |
---|
| 86 | |
---|
[496] | 87 | // Open and write header file |
---|
[198] | 88 | |
---|
[496] | 89 | String rootName(fileName); |
---|
| 90 | if (rootName.length()==0) rootName = String("ascii"); |
---|
| 91 | |
---|
[988] | 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 | onstr << "SCAN" << rec.asuInt("SCANNO") |
---|
| 106 | << "_CYCLE" << rec.asuInt("CYCLENO") |
---|
| 107 | << "_BEAM" << rec.asuInt("BEAMNO") |
---|
| 108 | << "_IF" << rec.asuInt("IFNO"); |
---|
| 109 | String fName = rootName + String(onstr) + String(".txt"); |
---|
| 110 | ofstream of(fName.chars(), ios::trunc); |
---|
[1293] | 111 | int row0 = t.rowNumbers(tab)[0]; |
---|
[988] | 112 | MDirection mdir = stable.getDirection(row0); |
---|
| 113 | of << setfill('#') << setw(70) << "" << setfill(' ') << endl; |
---|
| 114 | addLine(of, "Name", rec.asString("SRCNAME")); |
---|
| 115 | addLine(of, "Position", String(dirtype+ " "+formatDirection(mdir))); |
---|
| 116 | addLine(of, "Time", stable.getTime(row0,true)); |
---|
| 117 | addLine(of, "Flux Unit", hdr.fluxunit); |
---|
| 118 | addLine(of, "Pol Type", stable.getPolType()); |
---|
| 119 | addLine(of, "Abcissa", stable.getAbcissaLabel(row0)); |
---|
| 120 | addLine(of, "Beam No", rec.asuInt("BEAMNO")); |
---|
| 121 | addLine(of, "IF No", rec.asuInt("IFNO")); |
---|
| 122 | of << setfill('#') << setw(70) << "" << setfill(' ') << endl; |
---|
[496] | 123 | |
---|
[988] | 124 | of << std::left << setw(16) << "x"; |
---|
[996] | 125 | for ( unsigned int i=0; i<t.nrow(); ++i ) { |
---|
[994] | 126 | ostringstream os,os1; |
---|
| 127 | os << "y" << i; |
---|
| 128 | os1 << "yf" << i; |
---|
| 129 | of << setw(16) << String(os); |
---|
| 130 | of << setw(7) << String(os1); |
---|
[988] | 131 | } |
---|
| 132 | of << endl; |
---|
| 133 | std::vector<double> abc = stable.getAbcissa(row0); |
---|
| 134 | ROArrayColumn<Float> specCol(t,"SPECTRA"); |
---|
| 135 | ROArrayColumn<uChar> flagCol(t,"FLAGTRA"); |
---|
| 136 | Matrix<Float> specs = specCol.getColumn(); |
---|
| 137 | Matrix<uChar> flags = flagCol.getColumn(); |
---|
[996] | 138 | for ( unsigned int i=0; i<specs.nrow(); ++i ) { |
---|
[988] | 139 | of << setw(16) << setprecision(8) << abc[i] ; |
---|
[996] | 140 | for ( unsigned int j=0; j<specs.ncolumn(); ++j ) { |
---|
[988] | 141 | of << setw(16) << setprecision(8) << specs(i,j) ; |
---|
| 142 | of << setw(7) << Int(flags(i,j)); |
---|
[198] | 143 | } |
---|
[988] | 144 | of << endl; |
---|
| 145 | } |
---|
| 146 | of.close(); |
---|
| 147 | ostringstream oss; |
---|
| 148 | oss << "Wrote " << fName; |
---|
| 149 | pushLog(String(oss)); |
---|
| 150 | ++iter; |
---|
| 151 | } |
---|
| 152 | return True; |
---|
[198] | 153 | } |
---|
| 154 | |
---|
| 155 | |
---|
[988] | 156 | String STAsciiWriter::formatDirection(const MDirection& md) const |
---|
| 157 | { |
---|
| 158 | Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue(); |
---|
| 159 | Int prec = 7; |
---|
[198] | 160 | |
---|
[988] | 161 | MVAngle mvLon(t[0]); |
---|
| 162 | String sLon = mvLon.string(MVAngle::TIME,prec); |
---|
| 163 | uInt tp = md.getRef().getType(); |
---|
| 164 | if (tp == MDirection::GALACTIC || |
---|
| 165 | tp == MDirection::SUPERGAL ) { |
---|
| 166 | sLon = mvLon(0.0).string(MVAngle::ANGLE_CLEAN,prec); |
---|
| 167 | } |
---|
| 168 | MVAngle mvLat(t[1]); |
---|
| 169 | String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec); |
---|
| 170 | return sLon + String(" ") + sLat; |
---|
| 171 | } |
---|
[717] | 172 | |
---|
[988] | 173 | template <class T> |
---|
| 174 | void STAsciiWriter::addLine(ostream& of, const String& lbl, const T& value) |
---|
| 175 | { |
---|
| 176 | String label = lbl+String(": "); |
---|
| 177 | of << std::right << "# " << setw(15) << label << std::left |
---|
| 178 | << setw(52) << value << setw(0) << "#"<< endl; |
---|
[198] | 179 | } |
---|