Changeset 805 for trunk/src/STFiller.cpp


Ignore:
Timestamp:
02/16/06 12:02:18 (18 years ago)
Author:
mar637
Message:

Code replacemnts after the rename

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/STFiller.cpp

    r804 r805  
    1 //#---------------------------------------------------------------------------
    2 //# SDReader.cc: A class to read single dish spectra from SDFITS, RPFITS
    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:
    30 //#---------------------------------------------------------------------------
     1//
     2// C++ Implementation: STFiller
     3//
     4// Description:
     5//
     6//
     7// Author: Malte Marquarding <asap@atnf.csiro.au>, (C) 2006
     8//
     9// Copyright: See COPYING file that comes with this distribution
     10//
     11//
    3112#include <casa/iostream.h>
    3213#include <casa/iomanip.h>
     
    3617#include <casa/OS/File.h>
    3718#include <casa/Quanta/Unit.h>
     19#include <casa/Arrays/ArrayMath.h>
     20#include <casa/Utilities/Regex.h>
     21
     22#include <casa/Containers/RecordField.h>
     23
     24#include <tables/Tables/TableRow.h>
     25
    3826#include <atnf/PKSIO/PKSreader.h>
    3927
    40 #include "SDReader.h"
    4128#include "SDDefs.h"
    4229#include "SDAttr.h"
    4330
     31#include "STFiller.h"
     32
    4433using namespace casa;
    45 using namespace asap;
    46 
    47 SDReader::SDReader() :
     34
     35namespace asap {
     36
     37STFiller::STFiller() :
    4838  reader_(0),
    4939  header_(0),
    50   frequencies_(0),
    51   table_(new SDMemTable()),
    52   haveXPol_(False)
    53 {
    54   cursor_ = 0;
    55 }
    56 SDReader::SDReader(const std::string& filename,
    57                    int whichIF, int whichBeam) :
     40  table_(0)
     41{
     42}
     43
     44STFiller::STFiller( CountedPtr< Scantable > stbl ) :
    5845  reader_(0),
    5946  header_(0),
    60   frequencies_(0),
    61   table_(new SDMemTable()),
    62   haveXPol_(False)
    63 {
    64   cursor_ = 0;
    65   open(filename, whichIF, whichBeam);
    66 }
    67 
    68 SDReader::SDReader(CountedPtr<SDMemTable> tbl) :
     47  table_(stbl)
     48{
     49}
     50
     51STFiller::STFiller(const std::string & filename,
     52                                 int whichIF, int whichBeam ) :
    6953  reader_(0),
    7054  header_(0),
    71   table_(tbl),
    72   haveXPol_(False)
    73 {
    74   cursor_ = 0;
    75 }
    76 
    77 SDReader::~SDReader() {
    78   if (reader_) delete reader_;
    79   if (header_) delete header_;
    80   if (frequencies_) delete frequencies_;
    81 }
    82 
    83 void SDReader::reset() {
    84   cursor_ = 0;
    85   table_ = new SDMemTable();
    86   open(filename_,ifOffset_, beamOffset_);
    87 }
    88 
    89 
    90 void SDReader::close() {
    91   //cerr << "disabled" << endl;
    92 }
    93 
    94 void SDReader::open(const std::string& filename,
    95                     int whichIF, int whichBeam) {
    96   if (reader_) delete reader_; reader_ = 0;
    97   Bool   haveBase, haveSpectra;
     55  table_(0)
     56{
     57  open(filename, whichIF, whichBeam);
     58}
     59
     60STFiller::~STFiller()
     61{
     62  close();
     63}
     64
     65void STFiller::open( const std::string & filename,
     66                            int whichIF, int whichBeam )
     67{
     68  if (table_.null()) table_ = new Scantable();
     69  if (reader_)  { delete reader_; reader_ = 0; }
     70  Bool haveBase, haveSpectra;
    9871
    9972  String inName(filename);
     
    10275
    10376  File file(inName);
    104   if (!file.exists()) {
     77  if ( !file.exists() ) {
    10578     throw(AipsError("File does not exist"));
    10679  }
     
    11083  String format;
    11184  Vector<Bool> beams;
    112   if ((reader_ = getPKSreader(inName, 0, False, format, beams, nIF_,
     85  if ( (reader_ = getPKSreader(inName, 0, 0, format, beams, nIF_,
    11386                              nChan_, nPol_, haveBase, haveSpectra,
    114                               haveXPol_)) == 0)  {
     87                              haveXPol_)) == 0 )  {
    11588    throw(AipsError("Creation of PKSreader failed"));
    11689  }
     
    12497  nBeam_ = beams.nelements();
    12598  // Get basic parameters.
    126   if (haveXPol_) {
     99  if ( haveXPol_ ) {
    127100    pushLog("Cross polarization present");
    128101    nPol_ += 2;                          // Convert Complex -> 2 Floats
     
    145118    delete reader_;
    146119    reader_ = 0;
     120    delete header_;
     121    header_ = 0;
    147122    throw(AipsError("Failed to get header."));
    148     return;
    149123  }
    150124  if ((header_->obstype).matches("*SW*")) {
     
    153127               "setting # of IFs = 1 ");
    154128    nIF_ = 1;
     129    header_->obstype = String("fswitch");
    155130  }
    156131
     
    166141  header_->nif = nIF_;
    167142  header_->epoch = "UTC";
    168 
     143  // *** header_->frequnit = "Hz"
    169144  // Apply selection criteria.
    170145
     
    176151  if (whichIF>=0) {
    177152    if (whichIF>=0 && whichIF<nIF_) {
    178        IFsel = False;
    179        IFsel(whichIF) = True;
    180        header_->nif = 1;
    181        nIF_ = 1;
    182        ifOffset_ = whichIF;
     153      IFsel = False;
     154      IFsel(whichIF) = True;
     155      header_->nif = 1;
     156      nIF_ = 1;
     157      ifOffset_ = whichIF;
    183158    } else {
    184        throw(AipsError("Illegal IF selection"));
     159      delete reader_;
     160      reader_ = 0;
     161      delete header_;
     162      header_ = 0;
     163      throw(AipsError("Illegal IF selection"));
    185164    }
    186165  }
     
    188167  beamOffset_ = 0;
    189168  if (whichBeam>=0) {
    190      if (whichBeam>=0 && whichBeam<nBeam_) {
    191         beamSel = False;
    192         beamSel(whichBeam) = True;
    193         header_->nbeam = 1;
    194         nBeam_ = 1;
    195         beamOffset_ = whichBeam;
    196      } else {
    197        throw(AipsError("Illegal Beam selection"));
    198      }
     169    if (whichBeam>=0 && whichBeam<nBeam_) {
     170      beamSel = False;
     171      beamSel(whichBeam) = True;
     172      header_->nbeam = 1;
     173      nBeam_ = 1;
     174      beamOffset_ = whichBeam;
     175    } else {
     176      delete reader_;
     177      reader_ = 0;
     178      delete header_;
     179      header_ = 0;
     180      throw(AipsError("Illegal Beam selection"));
     181    }
    199182  }
    200183  Vector<Int> start(nIF_, 1);
     
    202185  reader_->select(beamSel, IFsel, start, end, ref, True, haveXPol_);
    203186  table_->putSDHeader(*header_);
    204 
    205   if (frequencies_) delete frequencies_;
    206   frequencies_ = new SDFrequencyTable();
    207   frequencies_->setRefFrame(header_->freqref);
    208   frequencies_->setBaseRefFrame(header_->freqref);
    209   frequencies_->setRestFrequencyUnit("Hz");
    210   frequencies_->setEquinox(header_->equinox);
    211 }
    212 
    213 int SDReader::read(const std::vector<int>& seq) {
     187}
     188
     189void STFiller::close( )
     190{
     191  delete reader_;reader_=0;
     192  delete header_;header_=0;
     193  table_ = 0;
     194}
     195
     196int asap::STFiller::read( )
     197{
    214198  int status = 0;
    215199
     
    226210  Complex         xCalFctr;
    227211  Vector<Complex> xPol;
    228   uInt n = seq.size();
    229 
    230 
    231   uInt stepsize = header_->nif*header_->nbeam;
    232   uInt seqi = 0;
    233   Bool getAll = False;
    234 
    235   if (seq[n-1] == -1) getAll = True;
    236   while ( (cursor_ <= seq[n-1]) || getAll) {
    237     // only needs to be create if in seq
    238     SDContainer sc(header_->nbeam,header_->nif,header_->npol,header_->nchan);
    239     // iterate over one correlator integration cycle = nBeam*nIF
    240     for (uInt row=0; row < stepsize; row++) {
    241       // stepsize as well
    242       // spectra(nChan,nPol)!!!
    243       status = reader_->read(scanNo, cycleNo, mjd, interval, fieldName,
    244                              srcName, srcDir, srcPM, srcVel, IFno, refFreq,
    245                              bandwidth, freqInc, restFreq, tcal, tcalTime,
    246                              azimuth, elevation, parAngle, focusAxi,
    247                              focusTan, focusRot, temperature, pressure,
    248                              humidity, windSpeed, windAz, refBeam,
    249                              beamNo, direction, scanRate,
    250                              tsys, sigma, calFctr, baseLin, baseSub,
    251                              spectra, flagtra, xCalFctr, xPol);
    252 
    253       // Make sure beam/IF numbers are 0-relative - dealing with
    254       // possible IF or Beam selection
    255       beamNo = beamNo - beamOffset_ - 1;
    256       IFno = IFno - ifOffset_ - 1;
    257 
    258       if (status) {
    259         if (status == -1) {
    260           // EOF.
    261           if (row > 0 && row < stepsize-1)
    262             pushLog("incomplete scan data.\n Probably means not all Beams/IFs/Pols within a scan are present.");
    263 
    264           // flush frequency table
    265           table_->putSDFreqTable(*frequencies_);
    266           return status;
    267         }
    268       }
    269       // if in the given list
    270       if (cursor_ == seq[seqi] || getAll) {
    271         // add integration cycle
    272         if (row==0) {
    273           //add invariant info: scanNo, mjd, interval, fieldName,
    274           //srcName, azimuth, elevation, parAngle, focusAxi, focusTan,
    275           //focusRot, temperature, pressure, humidity, windSpeed,
    276           //windAz  srcDir, srcPM, srcVel
    277           sc.timestamp = mjd;
    278           sc.interval = interval;
    279           sc.sourcename = srcName;
    280           sc.fieldname = fieldName;
    281           sc.azimuth = azimuth;
    282           sc.elevation = elevation;
    283         }
    284         // add specific info
    285         // refPix = nChan/2+1 in  1-rel Integer arith.!
    286         Int refPix = header_->nchan/2;       // 0-rel
    287         uInt freqID = frequencies_->addFrequency(refPix, refFreq, freqInc);
    288         uInt restFreqID = frequencies_->addRestFrequency(restFreq);
    289 
    290         sc.setFrequencyMap(freqID, IFno);
    291         sc.setRestFrequencyMap(restFreqID, IFno);
    292 
    293         sc.tcal[0] = tcal[0];sc.tcal[1] = tcal[1];
    294         sc.tcaltime = tcalTime;
    295         sc.parangle = parAngle;
    296         sc.refbeam = -1; //nbeams == 1
    297         if (nBeam_ > 1) // circumvent a bug "asap0000" in read which
    298                         // returns a random refbema number on multiple
    299                         // reads
    300           sc.refbeam = refBeam-1;//make it 0-based;
    301         sc.scanid = scanNo-1;//make it 0-based
    302         if (haveXPol_) {
    303            sc.setSpectrum(spectra, xPol, beamNo, IFno);
    304            sc.setFlags(flagtra,  beamNo, IFno, True);
    305         } else {
    306            sc.setSpectrum(spectra, beamNo, IFno);
    307            sc.setFlags(flagtra,  beamNo, IFno, False);
    308         }
    309         sc.setTsys(tsys, beamNo, IFno, haveXPol_);
    310         sc.setDirection(direction, beamNo);
    311       }
    312     }
    313 
    314     if (cursor_ == seq[seqi] || getAll) {
    315       // insert container into table/list
    316       table_->putSDContainer(sc);
    317       seqi++;// next in list
    318     }
    319     cursor_++;// increment position in file
    320 
    321   }
    322   table_->putSDFreqTable(*frequencies_);
     212  while ( status == 0 ) {
     213    status = reader_->read(scanNo, cycleNo, mjd, interval, fieldName,
     214                          srcName, srcDir, srcPM, srcVel, IFno, refFreq,
     215                          bandwidth, freqInc, restFreq, tcal, tcalTime,
     216                          azimuth, elevation, parAngle, focusAxi,
     217                          focusTan, focusRot, temperature, pressure,
     218                          humidity, windSpeed, windAz, refBeam,
     219                          beamNo, direction, scanRate,
     220                          tsys, sigma, calFctr, baseLin, baseSub,
     221                          spectra, flagtra, xCalFctr, xPol);
     222    if ( status != 0 ) break;
     223    TableRow row(table_->table());
     224    TableRecord& rec = row.record();
     225    RecordFieldPtr<uInt> scanoCol(rec, "SCANNO");
     226    *scanoCol = scanNo-1;
     227    RecordFieldPtr<uInt> cyclenoCol(rec, "CYCLENO");
     228    *cyclenoCol = cycleNo-1;
     229    RecordFieldPtr<Double> mjdCol(rec, "TIME");
     230    *mjdCol = mjd;
     231    RecordFieldPtr<Double> intCol(rec, "INTERVAL");
     232    *intCol = interval;
     233    RecordFieldPtr<String> srcnCol(rec, "SRCNAME");
     234    RecordFieldPtr<Int> srctCol(rec, "SRCTYPE");
     235    // try to auto-identify if it is on or off.
     236    Regex rx(".*_[e|w|R]$");
     237    Regex rx2("_[e|w|R|S]$");
     238    Int match = srcName.matches(rx);
     239    if (match) {
     240      *srcnCol = srcName;
     241    } else {
     242      *srcnCol = srcName.before(rx2);
     243    }
     244    //*srcnCol = srcName;//.before(rx2);
     245    *srctCol = match;
     246    RecordFieldPtr<uInt> beamCol(rec, "BEAMNO");
     247    *beamCol = beamNo-beamOffset_-1;
     248    RecordFieldPtr<Int> rbCol(rec, "REFBEAMNO");
     249    Int rb = -1;
     250    if (nBeam_ > 1 ) rb = refBeam-1;
     251    *rbCol = rb;
     252    RecordFieldPtr<uInt> ifCol(rec, "IFNO");
     253    *ifCol = IFno-ifOffset_- 1;
     254    uInt id;
     255    /// @todo this has to change when nchan isn't global anymore
     256    id = table_->frequencies().addEntry(Double(header_->nchan/2),
     257                                            refFreq, freqInc);
     258    RecordFieldPtr<uInt> mfreqidCol(rec, "FREQ_ID");
     259    *mfreqidCol = id;
     260
     261    id = table_->molecules().addEntry(restFreq);
     262    RecordFieldPtr<uInt> molidCol(rec, "MOLECULE_ID");
     263    *molidCol = id;
     264
     265    id = table_->tcal().addEntry(tcalTime, tcal);
     266    RecordFieldPtr<uInt> mcalidCol(rec, "TCAL_ID");
     267    *mcalidCol = id;
     268    id = table_->weather().addEntry(temperature, pressure, humidity,
     269                                    windSpeed, windAz);
     270    RecordFieldPtr<uInt> mweatheridCol(rec, "WEATHER_ID");
     271    *mweatheridCol = id;
     272    RecordFieldPtr<uInt> mfocusidCol(rec, "FOCUS_ID");
     273    id = table_->focus().addEntry(focusAxi, focusTan, focusRot);
     274    *mfocusidCol = id;
     275    RecordFieldPtr<Array<Double> > dirCol(rec, "DIRECTION");
     276    *dirCol = direction;
     277    RecordFieldPtr<Double> azCol(rec, "AZIMUTH");
     278    *azCol = azimuth;
     279    RecordFieldPtr<Double> elCol(rec, "ELEVATION");
     280    *elCol = elevation;
     281
     282    RecordFieldPtr<Float> parCol(rec, "PARANGLE");
     283    *parCol = parAngle;
     284
     285    RecordFieldPtr< Array<Float> > specCol(rec, "SPECTRA");
     286    RecordFieldPtr< Array<uChar> > flagCol(rec, "FLAGTRA");
     287    RecordFieldPtr< uInt > polnoCol(rec, "POLNO");
     288
     289    RecordFieldPtr< Array<Float> > tsysCol(rec, "TSYS");
     290    // Turn the (nchan,npol) matrix and possible complex xPol vector
     291    // into 2-4 rows in the scantable
     292    Vector<Float> tsysvec(1);
     293    // Why is spectra.ncolumn() == 3 for haveXPol_ == True
     294    uInt npol = (spectra.ncolumn()==1 ? 1: 2);
     295    for ( uInt i=0; i< npol; ++i ) {
     296      tsysvec = tsys(i);
     297      *tsysCol = tsysvec;
     298      *polnoCol = i;
     299
     300      *specCol = spectra.column(i);
     301      *flagCol = flagtra.column(i);
     302      table_->table().addRow();
     303      row.put(table_->table().nrow()-1, rec);
     304    }
     305    if ( haveXPol_ ) {
     306      // no tsys given for xpol, so emulate it
     307      tsysvec = sqrt(tsys[0]*tsys[1]);
     308      *tsysCol = tsysvec;
     309      // add real part of cross pol
     310      *polnoCol = 2;
     311      Vector<Float> r(real(xPol));
     312      *specCol = r;
     313      // make up flags from linears
     314      /// @fixme this has to be a bitwise or of both pols
     315      *flagCol = flagtra.column(0);// | flagtra.column(1);
     316      table_->table().addRow();
     317      row.put(table_->table().nrow()-1, rec);
     318      // ad imaginary part of cross pol
     319      *polnoCol = 3;
     320      Vector<Float> im(imag(xPol));
     321      *specCol = im;
     322      table_->table().addRow();
     323      row.put(table_->table().nrow()-1, rec);
     324    }
     325  }
     326  if (status > 0) {
     327    close();
     328    throw(AipsError("Reading error occured, data possibly corrupted."));
     329  }
     330
    323331  return status;
    324332}
     333
     334}//namespace asap
Note: See TracChangeset for help on using the changeset viewer.