Ignore:
Timestamp:
04/05/06 14:31:47 (18 years ago)
Author:
mar637
Message:

implemented data export for the asap2 Scantable as in Ticket #4.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/STAsciiWriter.cpp

    r823 r988  
    11//#---------------------------------------------------------------------------
    2 //# SDAsciiWriter.cc: ASAP class to write out single dish spectra as FITS images
     2//# STAsciiWriter.cc: ASAP class to write out single dish spectra as FITS images
    33//#---------------------------------------------------------------------------
    44//# Copyright (C) 2004
     
    3232#include <casa/aips.h>
    3333#include <casa/Arrays/Array.h>
     34#include <casa/Arrays/Matrix.h>
    3435#include <casa/Arrays/Vector.h>
    3536#include <casa/Arrays/VectorIter.h>
     
    3940#include <casa/Utilities/Assert.h>
    4041
    41 #include <casa/iostream.h>
    4242#include <casa/fstream.h>
    4343#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>
    4945
    5046#include <measures/Measures/MEpoch.h>
    5147
    5248#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>
    5353#include <tables/Tables/ScalarColumn.h>
    5454#include <tables/Tables/ArrayColumn.h>
    5555
    56 #include "SDDefs.h"
    57 #include "SDContainer.h"
    58 #include "SDMemTable.h"
    59 #include "SDAsciiWriter.h"
     56#include "STDefs.h"
     57#include "STHeader.h"
     58#include "Scantable.h"
     59#include "STAsciiWriter.h"
    6060
    6161using namespace casa;
     
    6363
    6464
    65 SDAsciiWriter::SDAsciiWriter()
     65STAsciiWriter::STAsciiWriter()
    6666{;}
    6767
    68 SDAsciiWriter::~SDAsciiWriter()
     68STAsciiWriter::~STAsciiWriter()
    6969{;}
    7070
    7171
    72 Bool SDAsciiWriter::write(const SDMemTable& sdTable, const String& fileName, Bool toStokes)
     72Bool STAsciiWriter::write(const Scantable& stable, const String& fileName)
    7373{
    7474
    7575// Get global Header from Table
    7676
    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();
    8078
    8179// Column keywords
    8280
    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();
    8882
    8983// Temps
    9084
    91    Vector<Int> whichStokes(1,1);
    92    Array<Double> whichDir;
    93    Vector<Double> lonLat(2);
    94    IPosition posDir(2,0);
    9585   const Unit RAD(String("rad"));
    9686
     
    9989   String rootName(fileName);
    10090   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    }
    10991
    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;
    111123
    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));
    193142      }
    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;
    201152}
    202153
    203154
     155String STAsciiWriter::formatDirection(const MDirection& md) const
     156{
     157  Vector<Double> t = md.getAngle(Unit(String("rad"))).getValue();
     158  Int prec = 7;
    204159
    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;
    215170}
    216171
     172template <class T>
     173void 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.