Changeset 988 for trunk/src/STAsciiWriter.cpp
- Timestamp:
- 04/05/06 14:31:47 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/STAsciiWriter.cpp
r823 r988 1 1 //#--------------------------------------------------------------------------- 2 //# S DAsciiWriter.cc: ASAP class to write out single dish spectra as FITS images2 //# STAsciiWriter.cc: ASAP class to write out single dish spectra as FITS images 3 3 //#--------------------------------------------------------------------------- 4 4 //# Copyright (C) 2004 … … 32 32 #include <casa/aips.h> 33 33 #include <casa/Arrays/Array.h> 34 #include <casa/Arrays/Matrix.h> 34 35 #include <casa/Arrays/Vector.h> 35 36 #include <casa/Arrays/VectorIter.h> … … 39 40 #include <casa/Utilities/Assert.h> 40 41 41 #include <casa/iostream.h>42 42 #include <casa/fstream.h> 43 43 #include <casa/sstream.h> 44 45 #include <coordinates/Coordinates/CoordinateUtil.h> 46 #include <coordinates/Coordinates/SpectralCoordinate.h> 47 #include <coordinates/Coordinates/DirectionCoordinate.h> 48 #include <coordinates/Coordinates/StokesCoordinate.h> 44 #include <casa/iomanip.h> 49 45 50 46 #include <measures/Measures/MEpoch.h> 51 47 52 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 53 #include <tables/Tables/ScalarColumn.h> 54 54 #include <tables/Tables/ArrayColumn.h> 55 55 56 #include "S DDefs.h"57 #include "S DContainer.h"58 #include "S DMemTable.h"59 #include "S DAsciiWriter.h"56 #include "STDefs.h" 57 #include "STHeader.h" 58 #include "Scantable.h" 59 #include "STAsciiWriter.h" 60 60 61 61 using namespace casa; … … 63 63 64 64 65 S DAsciiWriter::SDAsciiWriter()65 STAsciiWriter::STAsciiWriter() 66 66 {;} 67 67 68 S DAsciiWriter::~SDAsciiWriter()68 STAsciiWriter::~STAsciiWriter() 69 69 {;} 70 70 71 71 72 Bool S DAsciiWriter::write(const SDMemTable& sdTable, const String& fileName, Bool toStokes)72 Bool STAsciiWriter::write(const Scantable& stable, const String& fileName) 73 73 { 74 74 75 75 // Get global Header from Table 76 76 77 SDHeader header = sdTable.getSDHeader(); 78 MEpoch::Ref timeRef(MEpoch::UTC); // Should be in header 79 MDirection::Types dirRef(MDirection::J2000); // Should be in header 77 STHeader hdr = stable.getHeader(); 80 78 81 79 // Column keywords 82 80 83 Table tab = sdTable.table(); 84 ROArrayColumn<Double> dir(tab, String("DIRECTION")); 85 ROScalarColumn<Double> time(tab, "TIME"); 86 ROArrayColumn<uInt> freqid(tab, "FREQID"); 87 ROScalarColumn<String> src(tab, "SRCNAME"); 81 Table tab = stable.table(); 88 82 89 83 // Temps 90 84 91 Vector<Int> whichStokes(1,1);92 Array<Double> whichDir;93 Vector<Double> lonLat(2);94 IPosition posDir(2,0);95 85 const Unit RAD(String("rad")); 96 86 … … 99 89 String rootName(fileName); 100 90 if (rootName.length()==0) rootName = String("ascii"); 101 {102 String fName = String(rootName) + String("_header.txt");103 pushLog("Writing header to "+fName);104 ofstream of(fName.chars(), ios::trunc);105 std::string summary = sdTable.summary(true);106 of << summary;107 of.close();108 }109 91 110 // Open data file 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); 111 int row0 = t.rowNumbers()[0]; 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; 111 123 112 String fName = rootName + String(".txt"); 113 ofstream of(fName.chars(), ios::trunc); 114 115 // Write header 116 117 of << "row beam IF pol source longitude latitude time nchan spectrum mask" 118 << endl; 119 120 // Loop over rows 121 122 const uInt nRows = sdTable.nRow(); 123 for (uInt iRow=0; iRow<nRows; iRow++) { 124 125 // Get data 126 127 const MaskedArray<Float>& dataIn(sdTable.rowAsMaskedArray(iRow,toStokes)); 128 const Array<Float>& values = dataIn.getArray(); 129 const Array<Bool>& mask = dataIn.getMask(); 130 131 // Get abcissa 132 133 std::vector<double> abcissa = sdTable.getAbcissa(Int(iRow)); 134 const uInt n = abcissa.size(); 135 136 // Iterate through data in this row by spectra 137 138 ReadOnlyVectorIterator<Float> itData(values, asap::ChanAxis); 139 ReadOnlyVectorIterator<Bool> itMask(mask, asap::ChanAxis); 140 while (!itData.pastEnd()) { 141 const IPosition& pos = itData.pos(); 142 AlwaysAssert(itData.vector().nelements()==n,AipsError); 143 144 // FreqID 145 146 Vector<uInt> iTmp; 147 freqid.get(iRow, iTmp); 148 149 // Direction 150 151 dir.get(iRow, whichDir); 152 posDir(0) = pos(asap::BeamAxis); 153 posDir(1) = 0; 154 lonLat[0] = whichDir(posDir); 155 // 156 posDir(0) = pos(asap::BeamAxis); 157 posDir(1) = 1; 158 lonLat[1] = whichDir(posDir); 159 160 // Write preamble 161 162 of << iRow << " " << pos(asap::BeamAxis) << " " << pos(asap::IFAxis) << " " << 163 pos(asap::PolAxis) << " " << 164 src(iRow) << " " << formatDirection(lonLat) << " " << 165 sdTable.getTime(iRow,True) << " " << n << " "; 166 167 // Write abcissa 168 169 of.setf(ios::fixed, ios::floatfield); 170 of.precision(4); 171 for (uInt i=0; i<n; i++) { 172 of << abcissa[i] << " "; 173 } 174 175 // Write data 176 177 const Vector<Float>& data = itData.vector(); 178 const Vector<Bool>& mask = itMask.vector(); 179 for (uInt i=0; i<n; i++) { 180 of << data[i] << " "; 181 } 182 // Write mask 183 184 for (uInt i=0; i<n; i++) { 185 of << mask[i] << " "; 186 } 187 of << endl; 188 189 // Next spectrum 190 191 itData.next(); 192 itMask.next(); 124 of << std::left << setw(16) << "x"; 125 for ( int i=0; i<t.nrow(); ++i ) { 126 String y = "y"+ String(i); 127 String ym = "yflag"+ String(i); 128 of << setw(16) << y; 129 of << setw(7) << ym; 130 } 131 of << endl; 132 std::vector<double> abc = stable.getAbcissa(row0); 133 ROArrayColumn<Float> specCol(t,"SPECTRA"); 134 ROArrayColumn<uChar> flagCol(t,"FLAGTRA"); 135 Matrix<Float> specs = specCol.getColumn(); 136 Matrix<uChar> flags = flagCol.getColumn(); 137 for ( int i=0; i<specs.nrow(); ++i ) { 138 of << setw(16) << setprecision(8) << abc[i] ; 139 for ( int j=0; j<specs.ncolumn(); ++j ) { 140 of << setw(16) << setprecision(8) << specs(i,j) ; 141 of << setw(7) << Int(flags(i,j)); 193 142 } 194 } 195 196 of.close(); 197 ostringstream oss; 198 oss << "Wrote " << nRows << " rows into file " << fileName; 199 pushLog(String(oss)); 200 return True; 143 of << endl; 144 } 145 of.close(); 146 ostringstream oss; 147 oss << "Wrote " << fName; 148 pushLog(String(oss)); 149 ++iter; 150 } 151 return True; 201 152 } 202 153 203 154 155 String STAsciiWriter::formatDirection(const MDirection& md) const 156 { 157 Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue(); 158 Int prec = 7; 204 159 205 String SDAsciiWriter::formatDirection(const Vector<Double>& lonLat) 206 { 207 MVAngle x1(lonLat(0));208 String s1 = x1.string(MVAngle::TIME, 12);209 210 MVAngle x2(lonLat(1));211 String s2 = x2.string(MVAngle::ANGLE, 12);212 213 String ss = s1 + String(" ") + s2;214 return ss;160 MVAngle mvLon(t[0]); 161 String sLon = mvLon.string(MVAngle::TIME,prec); 162 uInt tp = md.getRef().getType(); 163 if (tp == MDirection::GALACTIC || 164 tp == MDirection::SUPERGAL ) { 165 sLon = mvLon(0.0).string(MVAngle::ANGLE_CLEAN,prec); 166 } 167 MVAngle mvLat(t[1]); 168 String sLat = mvLat.string(MVAngle::ANGLE+MVAngle::DIG2,prec); 169 return sLon + String(" ") + sLat; 215 170 } 216 171 172 template <class T> 173 void STAsciiWriter::addLine(ostream& of, const String& lbl, const T& value) 174 { 175 String label = lbl+String(": "); 176 of << std::right << "# " << setw(15) << label << std::left 177 << setw(52) << value << setw(0) << "#"<< endl; 178 }
Note: See TracChangeset
for help on using the changeset viewer.