source: branches/mergetest/src/STFiller.cpp @ 1777

Last change on this file since 1777 was 1777, checked in by Malte Marquarding, 14 years ago

Created branch to test alma merge back into trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 KB
RevLine 
[805]1//
2// C++ Implementation: STFiller
3//
4// Description:
5//
6//
[1410]7// Author: Malte Marquarding <asap@atnf.csiro.au>, (C) 2006-2007
[805]8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
[404]12#include <casa/iostream.h>
13#include <casa/iomanip.h>
14
[125]15#include <casa/Exceptions.h>
[284]16#include <casa/OS/Path.h>
[290]17#include <casa/OS/File.h>
[338]18#include <casa/Quanta/Unit.h>
[805]19#include <casa/Arrays/ArrayMath.h>
[1060]20#include <casa/Arrays/ArrayLogical.h>
[805]21#include <casa/Utilities/Regex.h>
22
23#include <casa/Containers/RecordField.h>
24
25#include <tables/Tables/TableRow.h>
26
[1450]27#include <atnf/PKSIO/PKSrecord.h>
[2]28#include <atnf/PKSIO/PKSreader.h>
[1438]29#ifdef HAS_ALMA
30 #include <casa/System/ProgressMeter.h>
31#endif
[125]32
[835]33#include "STDefs.h"
[878]34#include "STAttr.h"
[2]35
[805]36#include "STFiller.h"
[901]37#include "STHeader.h"
[805]38
[125]39using namespace casa;
[2]40
[805]41namespace asap {
42
43STFiller::STFiller() :
[2]44  reader_(0),
[405]45  header_(0),
[1504]46  table_(0),
47  refRx_(".*(e|w|_R)$")
[420]48{
[2]49}
[805]50
51STFiller::STFiller( CountedPtr< Scantable > stbl ) :
[46]52  reader_(0),
[405]53  header_(0),
[1504]54  table_(stbl),
55  refRx_(".*(e|w|_R)$")
[420]56{
[46]57}
[2]58
[855]59STFiller::STFiller(const std::string& filename, int whichIF, int whichBeam ) :
[2]60  reader_(0),
[405]61  header_(0),
[1504]62  table_(0),
63  refRx_(".*(e|w|_R)$")
[420]64{
[805]65  open(filename, whichIF, whichBeam);
[2]66}
67
[805]68STFiller::~STFiller()
69{
70  close();
[2]71}
72
[855]73void STFiller::open( const std::string& filename, int whichIF, int whichBeam )
[805]74{
[846]75  if (table_.null())  {
76    table_ = new Scantable();
77  }
[805]78  if (reader_)  { delete reader_; reader_ = 0; }
79  Bool haveBase, haveSpectra;
[2]80
81  String inName(filename);
[284]82  Path path(inName);
83  inName = path.expandedName();
[405]84
[290]85  File file(inName);
[805]86  if ( !file.exists() ) {
[290]87     throw(AipsError("File does not exist"));
88  }
[87]89  filename_ = inName;
[332]90
[405]91  // Create reader and fill in values for arguments
[2]92  String format;
[1060]93  Vector<Bool> beams, ifs;
94  Vector<uInt> nchans,npols;
95  if ( (reader_ = getPKSreader(inName, 0, 0, format, beams, ifs,
96                              nchans, npols, haveXPol_,haveBase, haveSpectra
97                              )) == 0 )  {
[405]98    throw(AipsError("Creation of PKSreader failed"));
[2]99  }
100  if (!haveSpectra) {
101    delete reader_;
102    reader_ = 0;
[87]103    throw(AipsError("No spectral data in file."));
[2]104    return;
105  }
106  nBeam_ = beams.nelements();
[1060]107  nIF_ = ifs.nelements();
[2]108  // Get basic parameters.
[1060]109  if ( anyEQ(haveXPol_, True) ) {
[717]110    pushLog("Cross polarization present");
[1060]111    for (uInt i=0; i< npols.nelements();++i) {
112      if (npols[i] < 3) npols[i] += 2;// Convert Complex -> 2 Floats
113    }
[110]114  }
[405]115  if (header_) delete header_;
[901]116  header_ = new STHeader();
[1060]117  header_->nchan = max(nchans);
118  header_->npol = max(npols);
[405]119  header_->nbeam = nBeam_;
[1777]120
[405]121  Int status = reader_->getHeader(header_->observer, header_->project,
122                                  header_->antennaname, header_->antennaposition,
[1410]123                                  header_->obstype,
124                                  header_->fluxunit,
125                                  header_->equinox,
[405]126                                  header_->freqref,
127                                  header_->utc, header_->reffreq,
128                                  header_->bandwidth);
129
[2]130  if (status) {
131    delete reader_;
132    reader_ = 0;
[805]133    delete header_;
134    header_ = 0;
[87]135    throw(AipsError("Failed to get header."));
[2]136  }
[405]137  if ((header_->obstype).matches("*SW*")) {
[2]138    // need robust way here - probably read ahead of next timestamp
[717]139    pushLog("Header indicates frequency switched observation.\n"
[754]140               "setting # of IFs = 1 ");
[2]141    nIF_ = 1;
[805]142    header_->obstype = String("fswitch");
[2]143  }
[405]144  // Determine Telescope and set brightness unit
[342]145
[1391]146
[342]147  Bool throwIt = False;
[878]148  Instrument inst = STAttr::convertInstrument(header_->antennaname, throwIt);
[1410]149
[342]150  if (inst==ATMOPRA || inst==TIDBINBILLA) {
[1410]151    header_->fluxunit = "K";
152  } else {
153    // downcase for use with Quanta
154    if (header_->fluxunit == "JY") {
155      header_->fluxunit = "Jy";
156    }
[342]157  }
[1188]158  STAttr stattr;
159  header_->poltype = stattr.feedPolType(inst);
[405]160  header_->nif = nIF_;
161  header_->epoch = "UTC";
[805]162  // *** header_->frequnit = "Hz"
[2]163  // Apply selection criteria.
164  Vector<Int> ref;
[332]165  ifOffset_ = 0;
166  if (whichIF>=0) {
167    if (whichIF>=0 && whichIF<nIF_) {
[1060]168      ifs = False;
169      ifs(whichIF) = True;
[805]170      header_->nif = 1;
171      nIF_ = 1;
172      ifOffset_ = whichIF;
[332]173    } else {
[805]174      delete reader_;
175      reader_ = 0;
176      delete header_;
177      header_ = 0;
178      throw(AipsError("Illegal IF selection"));
[332]179    }
180  }
181  beamOffset_ = 0;
182  if (whichBeam>=0) {
[805]183    if (whichBeam>=0 && whichBeam<nBeam_) {
[1060]184      beams = False;
185      beams(whichBeam) = True;
[805]186      header_->nbeam = 1;
187      nBeam_ = 1;
188      beamOffset_ = whichBeam;
189    } else {
190      delete reader_;
191      reader_ = 0;
192      delete header_;
193      header_ = 0;
194      throw(AipsError("Illegal Beam selection"));
195    }
[332]196  }
197  Vector<Int> start(nIF_, 1);
198  Vector<Int> end(nIF_, 0);
[1450]199  reader_->select(beams, ifs, start, end, ref, True, haveXPol_[0]);
[846]200  table_->setHeader(*header_);
[1391]201  //For MS, add the location of POINTING of the input MS so one get
202  //pointing data from there, if necessary.
[1777]203  //Also find nrow in MS
[1391]204  nInDataRow = 0;
205  if (format == "MS2") {
[1777]206    Path datapath(inName);
[1391]207    String ptTabPath = datapath.absoluteName();
208    Table inMS(ptTabPath);
209    nInDataRow = inMS.nrow();
210    ptTabPath.append("/POINTING");
211    table_->table().rwKeywordSet().define("POINTING", ptTabPath);
212    if ((header_->antennaname).matches("GBT")) {
213      String GOTabPath = datapath.absoluteName();
214      GOTabPath.append("/GBT_GO");
215      table_->table().rwKeywordSet().define("GBT_GO", GOTabPath);
216    }
217  }
[1586]218  //table_->focus().setParallactify(true);
[805]219}
[405]220
[805]221void STFiller::close( )
222{
223  delete reader_;reader_=0;
224  delete header_;header_=0;
225  table_ = 0;
[2]226}
227
[805]228int asap::STFiller::read( )
229{
[87]230  int status = 0;
231
[1391]232  Double min = 0.0;
233  Double max = nInDataRow;
[1438]234#ifdef HAS_ALMA
235  ProgressMeter fillpm(min, max, "Data importing progress");
236#endif
[1450]237  PKSrecord pksrec;
[1391]238  int n = 0;
[805]239  while ( status == 0 ) {
[1450]240    status = reader_->read(pksrec);
[805]241    if ( status != 0 ) break;
[1391]242    n += 1;
[1081]243    Regex filterrx(".*[SL|PA]$");
[1110]244    Regex obsrx("^AT.+");
[1450]245    if ( header_->antennaname.matches(obsrx) &&
246         pksrec.obsType.matches(filterrx)) {
[1110]247        //cerr << "ignoring paddle scan" << endl;
[1081]248        continue;
249    }
[805]250    TableRow row(table_->table());
251    TableRecord& rec = row.record();
[999]252    // fields that don't get used and are just passed through asap
253    RecordFieldPtr<Array<Double> > srateCol(rec, "SCANRATE");
[1462]254    // MRC changed type from double to float
[1467]255    Vector<Double> sratedbl(pksrec.scanRate.nelements());
[1462]256    convertArray(sratedbl, pksrec.scanRate);
257    *srateCol = sratedbl;
[999]258    RecordFieldPtr<Array<Double> > spmCol(rec, "SRCPROPERMOTION");
[1450]259    *spmCol = pksrec.srcPM;
[999]260    RecordFieldPtr<Array<Double> > sdirCol(rec, "SRCDIRECTION");
[1450]261    *sdirCol = pksrec.srcDir;
[999]262    RecordFieldPtr<Double> svelCol(rec, "SRCVELOCITY");
[1450]263    *svelCol = pksrec.srcVel;
[999]264    // the real stuff
[972]265    RecordFieldPtr<Int> fitCol(rec, "FIT_ID");
266    *fitCol = -1;
[805]267    RecordFieldPtr<uInt> scanoCol(rec, "SCANNO");
[1450]268    *scanoCol = pksrec.scanNo-1;
[805]269    RecordFieldPtr<uInt> cyclenoCol(rec, "CYCLENO");
[1450]270    *cyclenoCol = pksrec.cycleNo-1;
[805]271    RecordFieldPtr<Double> mjdCol(rec, "TIME");
[1450]272    *mjdCol = pksrec.mjd;
[805]273    RecordFieldPtr<Double> intCol(rec, "INTERVAL");
[1450]274    *intCol = pksrec.interval;
[805]275    RecordFieldPtr<String> srcnCol(rec, "SRCNAME");
276    RecordFieldPtr<Int> srctCol(rec, "SRCTYPE");
[1391]277    RecordFieldPtr<String> fieldnCol(rec, "FIELDNAME");
[1450]278    *fieldnCol = pksrec.fieldName;
[805]279    // try to auto-identify if it is on or off.
[1504]280    Regex rx(refRx_);
[942]281    Regex rx2("_S$");
[1450]282    Int match = pksrec.srcName.matches(rx);
[805]283    if (match) {
[1450]284      *srcnCol = pksrec.srcName;
[805]285    } else {
[1450]286      *srcnCol = pksrec.srcName.before(rx2);
[805]287    }
[1450]288    //*srcnCol = pksrec.srcName;//.before(rx2);
[805]289    *srctCol = match;
290    RecordFieldPtr<uInt> beamCol(rec, "BEAMNO");
[1450]291    *beamCol = pksrec.beamNo-beamOffset_-1;
[805]292    RecordFieldPtr<Int> rbCol(rec, "REFBEAMNO");
293    Int rb = -1;
[1450]294    if (nBeam_ > 1 ) rb = pksrec.refBeam-1;
[805]295    *rbCol = rb;
296    RecordFieldPtr<uInt> ifCol(rec, "IFNO");
[1450]297    *ifCol = pksrec.IFno-ifOffset_- 1;
[805]298    uInt id;
299    /// @todo this has to change when nchan isn't global anymore
300    id = table_->frequencies().addEntry(Double(header_->nchan/2),
[1586]301                                        pksrec.refFreq, pksrec.freqInc);
[805]302    RecordFieldPtr<uInt> mfreqidCol(rec, "FREQ_ID");
303    *mfreqidCol = id;
[25]304
[1450]305    id = table_->molecules().addEntry(pksrec.restFreq);
[805]306    RecordFieldPtr<uInt> molidCol(rec, "MOLECULE_ID");
307    *molidCol = id;
[414]308
[1450]309    id = table_->tcal().addEntry(pksrec.tcalTime, pksrec.tcal);
[805]310    RecordFieldPtr<uInt> mcalidCol(rec, "TCAL_ID");
311    *mcalidCol = id;
[1450]312    id = table_->weather().addEntry(pksrec.temperature, pksrec.pressure,
313                                    pksrec.humidity, pksrec.windSpeed,
314                                    pksrec.windAz);
[805]315    RecordFieldPtr<uInt> mweatheridCol(rec, "WEATHER_ID");
316    *mweatheridCol = id;
[1586]317
[805]318    RecordFieldPtr<uInt> mfocusidCol(rec, "FOCUS_ID");
[1777]319    id = table_->focus().addEntry(pksrec.parAngle, pksrec.focusAxi,
[1586]320                                  pksrec.focusTan, pksrec.focusRot);
[805]321    *mfocusidCol = id;
322    RecordFieldPtr<Array<Double> > dirCol(rec, "DIRECTION");
[1450]323    *dirCol = pksrec.direction;
[922]324    RecordFieldPtr<Float> azCol(rec, "AZIMUTH");
[1450]325    *azCol = pksrec.azimuth;
[922]326    RecordFieldPtr<Float> elCol(rec, "ELEVATION");
[1450]327    *elCol = pksrec.elevation;
[405]328
[805]329    RecordFieldPtr< Array<Float> > specCol(rec, "SPECTRA");
330    RecordFieldPtr< Array<uChar> > flagCol(rec, "FLAGTRA");
331    RecordFieldPtr< uInt > polnoCol(rec, "POLNO");
[405]332
[805]333    RecordFieldPtr< Array<Float> > tsysCol(rec, "TSYS");
334    // Turn the (nchan,npol) matrix and possible complex xPol vector
335    // into 2-4 rows in the scantable
336    Vector<Float> tsysvec(1);
[1450]337    // Why is pksrec.spectra.ncolumn() == 3 for haveXPol_ == True
338    uInt npol = (pksrec.spectra.ncolumn()==1 ? 1: 2);
[805]339    for ( uInt i=0; i< npol; ++i ) {
[1450]340      tsysvec = pksrec.tsys(i);
[805]341      *tsysCol = tsysvec;
342      *polnoCol = i;
[405]343
[1450]344      *specCol = pksrec.spectra.column(i);
[1462]345      *flagCol = pksrec.flagged.column(i);
[805]346      table_->table().addRow();
347      row.put(table_->table().nrow()-1, rec);
[2]348    }
[1060]349    if ( haveXPol_[0] ) {
[805]350      // no tsys given for xpol, so emulate it
[1450]351      tsysvec = sqrt(pksrec.tsys[0]*pksrec.tsys[1]);
[805]352      *tsysCol = tsysvec;
353      // add real part of cross pol
354      *polnoCol = 2;
[1450]355      Vector<Float> r(real(pksrec.xPol));
[805]356      *specCol = r;
357      // make up flags from linears
358      /// @fixme this has to be a bitwise or of both pols
[1462]359      *flagCol = pksrec.flagged.column(0);// | pksrec.flagged.column(1);
[805]360      table_->table().addRow();
361      row.put(table_->table().nrow()-1, rec);
362      // ad imaginary part of cross pol
363      *polnoCol = 3;
[1450]364      Vector<Float> im(imag(pksrec.xPol));
[805]365      *specCol = im;
366      table_->table().addRow();
367      row.put(table_->table().nrow()-1, rec);
[2]368    }
[1438]369#ifdef HAS_ALMA
370    fillpm._update(n);
371#endif
[805]372  }
373  if (status > 0) {
374    close();
375    throw(AipsError("Reading error occured, data possibly corrupted."));
376  }
[1438]377#ifdef HAS_ALMA
378  fillpm.done();
379#endif
[2]380  return status;
381}
[805]382
383}//namespace asap
Note: See TracBrowser for help on using the repository browser.