source: trunk/src/MSFiller.cpp @ 2004

Last change on this file since 2004 was 2004, checked in by Takeshi Nakazato, 13 years ago

New Development: No

JIRA Issue: Yes .CAS-2718

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No?

Module(s): Module Names change impacts.

Description: Describe your changes here...

The MSFiller is called instead of PKSFiller when input data is MS.
I have tested all task regressions as well as sdsave unit test and passed.

A few modification was needed for STMath::dototalpower() and
STWriter::write().


File size: 55.9 KB
RevLine 
[1974]1//
2// C++ Interface: MSFiller
3//
4// Description:
5//
6// This class is specific filler for MS format
7//
8// Takeshi Nakazato <takeshi.nakazato@nao.ac.jp>, (C) 2010
9//
10// Copyright: See COPYING file that comes with this distribution
11//
12//
13
14#include <iostream>
15#include <map>
16
17#include <tables/Tables/ExprNode.h>
18#include <tables/Tables/TableIter.h>
[1987]19#include <tables/Tables/TableColumn.h>
[1974]20#include <tables/Tables/ScalarColumn.h>
21#include <tables/Tables/ArrayColumn.h>
22#include <tables/Tables/RefRows.h>
[1987]23#include <tables/Tables/TableParse.h>
24#include <tables/Tables/RefRows.h>
[2002]25#include <tables/Tables/TableRow.h>
[1974]26
[2002]27#include <casa/Containers/RecordField.h>
[1974]28#include <casa/Logging/LogIO.h>
29#include <casa/Arrays/Slicer.h>
30#include <casa/Quanta/MVTime.h>
[1987]31#include <casa/OS/Path.h>
[1974]32
33#include <measures/Measures/Stokes.h>
34#include <measures/Measures/MEpoch.h>
[1987]35#include <measures/Measures/MCEpoch.h>
36#include <measures/Measures/MFrequency.h>
37#include <measures/Measures/MCFrequency.h>
38#include <measures/Measures/MPosition.h>
39#include <measures/Measures/MCPosition.h>
40#include <measures/Measures/MDirection.h>
41#include <measures/Measures/MCDirection.h>
42#include <measures/Measures/MeasConvert.h>
[1974]43#include <measures/TableMeasures/ScalarMeasColumn.h>
[1987]44#include <measures/TableMeasures/ArrayMeasColumn.h>
45#include <measures/TableMeasures/ScalarQuantColumn.h>
46#include <measures/TableMeasures/ArrayQuantColumn.h>
[1974]47
48#include <atnf/PKSIO/SrcType.h>
49
50#include "MSFiller.h"
51#include "STHeader.h"
52
[1988]53#include <ctime>
54#include <sys/time.h>
55
56double gettimeofday_sec()
57{
58  struct timeval tv ;
59  gettimeofday( &tv, NULL ) ;
60  return tv.tv_sec + (double)tv.tv_usec*1.0e-6 ;
61}
62
[1974]63using namespace casa ;
64using namespace std ;
65
66namespace asap {
67MSFiller::MSFiller( casa::CountedPtr<Scantable> stable )
68  : table_( stable ),
[1987]69    tablename_( "" ),
[1974]70    antenna_( -1 ),
71    getPt_( False ),
72    isFloatData_( False ),
73    isData_( False ),
74    isDoppler_( False ),
75    isFlagCmd_( False ),
76    isFreqOffset_( False ),
77    isHistory_( False ),
78    isProcessor_( False ),
79    isSysCal_( False ),
[1993]80    isWeather_( False ),
81    colTsys_( "TSYS_SPECTRUM" ),
82    colTcal_( "TCAL_SPECTRUM" )
[1974]83{
84  os_ = LogIO() ;
85  os_.origin( LogOrigin( "MSFiller", "MSFiller()", WHERE ) ) ;
86}
87
88MSFiller::~MSFiller()
89{
90  os_.origin( LogOrigin( "MSFiller", "~MSFiller()", WHERE ) ) ;
91}
92
93bool MSFiller::open( const std::string &filename, const casa::Record &rec )
94{
95  os_.origin( LogOrigin( "MSFiller", "open()", WHERE ) ) ;
[2004]96//   double startSec = gettimeofday_sec() ;
97//   os_ << "start MSFiller::open() startsec=" << startSec << LogIO::POST ;
[1974]98  //os_ << "   filename = " << filename << endl ;
99
100  // parsing MS options
101  if ( rec.isDefined( "ms" ) ) {
102    Record msrec = rec.asRecord( "ms" ) ;
103    if ( msrec.isDefined( "getpt" ) ) {
104      getPt_ = msrec.asBool( "getpt" ) ;
105    }
106    if ( msrec.isDefined( "antenna" ) ) {
107      if ( msrec.type( msrec.fieldNumber( "antenna" ) ) == TpInt ) {
108        antenna_ = msrec.asInt( "antenna" ) ;
109      }
110      else {
111        antenna_ = atoi( msrec.asString( "antenna" ).c_str() ) ;
112      }
113    }
114    else {
115      antenna_ = 0 ;
116    }
117  }
118
119  os_ << "Parsing MS options" << endl ;
120  os_ << "   getPt = " << getPt_ << endl ;
121  os_ << "   antenna = " << antenna_ << LogIO::POST ;
122
[1987]123  MeasurementSet *tmpMS = new MeasurementSet( filename, Table::Old ) ;
124  //mstable_ = (*tmpMS)( tmpMS->col("ANTENNA1") == antenna_
125  //                     && tmpMS->col("ANTENNA1") == tmpMS->col("ANTENNA2") ) ;
126  tablename_ = tmpMS->tableName() ;
127  mstable_ = MeasurementSet( (*tmpMS)( tmpMS->col("ANTENNA1") == antenna_
128                                       && tmpMS->col("ANTENNA1") == tmpMS->col("ANTENNA2") ) ) ;
129//   stringstream ss ;
130//   ss << "SELECT FROM $1 WHERE ANTENNA1 == ANTENNA2 && ANTENNA1 == " << antenna_ ;
131//   String taql( ss.str() ) ;
132//   mstable_ = MeasurementSet( tableCommand( taql, *tmpMS ) ) ;
133  delete tmpMS ;
[1974]134
135  // check which data column exists
[1987]136  isFloatData_ = mstable_.tableDesc().isColumn( "FLOAT_DATA" ) ;
137  isData_ = mstable_.tableDesc().isColumn( "DATA" ) ;
[1974]138
[2004]139//   double endSec = gettimeofday_sec() ;
140//   os_ << "end MSFiller::open() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]141  return true ;
142}
143
144void MSFiller::fill()
145{
146  os_.origin( LogOrigin( "MSFiller", "fill()", WHERE ) ) ;
[2004]147//   double startSec = gettimeofday_sec() ;
148//   os_ << "start MSFiller::fill() startSec=" << startSec << LogIO::POST ;
[1974]149
[2004]150//   double time0 = gettimeofday_sec() ;
151//   os_ << "start init fill: " << time0 << LogIO::POST ;
[1990]152
[1974]153  // Initialize header
154  STHeader sdh ; 
155  sdh.nchan = 0 ;
156  sdh.npol = 0 ;
157  sdh.nif = 0 ;
158  sdh.nbeam = 0 ;
159  sdh.observer = "" ;
160  sdh.project = "" ;
161  sdh.obstype = "" ;
162  sdh.antennaname = "" ;
163  sdh.antennaposition.resize( 0 ) ;
164  sdh.equinox = 0.0 ;
165  sdh.freqref = "" ;
166  sdh.reffreq = -1.0 ;
167  sdh.bandwidth = 0.0 ;
168  sdh.utc = 0.0 ;
169  sdh.fluxunit = "" ;
170  sdh.epoch = "" ;
171  sdh.poltype = "" ;
172 
173  // check if optional table exists
[1987]174  //const TableRecord msrec = tablesel_.keywordSet() ;
175  const TableRecord msrec = mstable_.keywordSet() ;
[1974]176  isDoppler_ = msrec.isDefined( "DOPPLER" ) ;
[2004]177  if ( isDoppler_ )
178    if ( mstable_.doppler().nrow() == 0 )
179      isDoppler_ = False ;
[1974]180  isFlagCmd_ = msrec.isDefined( "FLAG_CMD" ) ;
[2004]181  if ( isFlagCmd_ )
182    if ( mstable_.flagCmd().nrow() == 0 )
183      isFlagCmd_ = False ;
[1974]184  isFreqOffset_ = msrec.isDefined( "FREQ_OFFSET" ) ;
[2004]185  if ( isFreqOffset_ )
186    if ( mstable_.freqOffset().nrow() == 0 )
187      isFreqOffset_ = False ;
[1974]188  isHistory_ = msrec.isDefined( "HISTORY" ) ;
[2004]189  if ( isHistory_ )
190    if ( mstable_.history().nrow() == 0 )
191      isHistory_ = False ;
[1974]192  isProcessor_ = msrec.isDefined( "PROCESSOR" ) ;
[2004]193  if ( isProcessor_ )
194    if ( mstable_.processor().nrow() == 0 )
195      isProcessor_ = False ;
[1974]196  isSysCal_ = msrec.isDefined( "SYSCAL" ) ;
[2004]197  if ( isSysCal_ )
198    if ( mstable_.sysCal().nrow() == 0 )
199      isSysCal_ = False ;
[1974]200  isWeather_ = msrec.isDefined( "WEATHER" ) ;
[2004]201  if ( isWeather_ )
202    if ( mstable_.weather().nrow() == 0 )
203      isWeather_ = False ;
204
[1974]205  // Access to MS subtables
[1987]206  MSField fieldtab = mstable_.field() ;
207  MSPolarization poltab = mstable_.polarization() ;
208  MSDataDescription ddtab = mstable_.dataDescription() ;
209  MSObservation obstab = mstable_.observation() ;
210  MSSource srctab = mstable_.source() ;
211  MSSpectralWindow spwtab = mstable_.spectralWindow() ;
212  MSSysCal caltab = mstable_.sysCal() ;
[1974]213  if ( caltab.nrow() == 0 )
214    isSysCal_ = False ;
[1993]215  else {
216    if ( !caltab.tableDesc().isColumn( colTcal_ ) )
217      colTcal_ = "TCAL" ;
218    if ( !caltab.tableDesc().isColumn( colTsys_ ) )
219      colTsys_ = "TSYS" ;
220  }
[2004]221//   colTcal_ = "TCAL" ;
222//   colTsys_ = "TSYS" ;
[1987]223  MSPointing pointtab = mstable_.pointing() ;
224  if ( mstable_.weather().nrow() == 0 )
[1974]225    isWeather_ = False ;
[1987]226  MSState stattab = mstable_.state() ;
227  MSAntenna anttab = mstable_.antenna() ;
[1974]228
[1987]229  // TEST
230  // memory allocation by boost::object_pool
231  boost::object_pool<ROTableColumn> *tpoolr = new boost::object_pool<ROTableColumn> ;
232  //
[1974]233
[1987]234  // SUBTABLES: FREQUENCIES
[1974]235  table_->frequencies().setFrame( "LSRK" ) ;
236  table_->frequencies().setFrame( "LSRK", True ) ;
237
238  // SUBTABLES: WEATHER
[2004]239  fillWeather() ;
[1974]240
241  // SUBTABLES: FOCUS
242  fillFocus() ;
243
244  // SUBTABLES: TCAL
[2004]245  fillTcal( tpoolr ) ;
[1974]246
247  // SUBTABLES: FIT
248  //fillFit() ;
249
250  // SUBTABLES: HISTORY
251  //fillHistory() ;
252
[1987]253  // shared pointers
254  ROTableColumn *tcolr ;
255
[1974]256  // MAIN
257  // Iterate over several ids
258  map<Int, uInt> ifmap ; // (IFNO, FREQ_ID) pair
[1987]259  ROArrayQuantColumn<Double> *sharedQDArrCol = new ROArrayQuantColumn<Double>( anttab, "POSITION" ) ;
260  Vector< Quantum<Double> > antpos = (*sharedQDArrCol)( antenna_ ) ;
261  delete sharedQDArrCol ;
[1974]262  MPosition mp( MVPosition( antpos ), MPosition::ITRF ) ;
[1990]263  if ( getPt_ ) {
264    //pointtab = pointtab( pointtab.col("ANTENNA_ID")==antenna_ ).sort("TIME") ;
265    pointtab = MSPointing( pointtab( pointtab.col("ANTENNA_ID")==antenna_ ).sort("TIME") ) ;
266  }
[1987]267  tcolr = tpoolr->construct( anttab, "STATION" ) ;
268  String stationName = tcolr->asString( antenna_ ) ;
269  tpoolr->destroy( tcolr ) ;
270  tcolr = tpoolr->construct( anttab, "NAME" ) ;
271  String antennaName = tcolr->asString( antenna_ ) ;
272  tpoolr->destroy( tcolr ) ;
273  sdh.antennaposition.resize( 3 ) ;
274  for ( int i = 0 ; i < 3 ; i++ )
275    sdh.antennaposition[i] = antpos[i].getValue( "m" ) ;
276  String telescopeName = "" ;
277
[2004]278  //double time1 = gettimeofday_sec() ;
279  //os_ << "end fill init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1990]280
[2002]281  // row based
282  Table &stab = table_->table() ;
283  TableRow row( stab ) ;
284  TableRecord &trec = row.record() ;
285  RecordFieldPtr< Array<Float> > spRF( trec, "SPECTRA" ) ;
286  RecordFieldPtr< Array<uChar> > ucarrRF( trec, "FLAGTRA" ) ;
287  RecordFieldPtr<Double> timeRF( trec, "TIME" ) ;
288  RecordFieldPtr< Array<Float> > tsysRF( trec, "TSYS" ) ;
289  RecordFieldPtr<Double> intervalRF( trec, "INTERVAL" ) ;
290  RecordFieldPtr< Array<Double> > dirRF( trec, "DIRECTION" ) ;
291  RecordFieldPtr<Float> azRF( trec, "AZIMUTH" ) ;
292  RecordFieldPtr<Float> elRF( trec, "ELEVATION" ) ;
293  RecordFieldPtr< Array<Double> > scrRF( trec, "SCANRATE" ) ;
294  RecordFieldPtr<uInt> cycleRF( trec, "CYCLENO" ) ;
295  RecordFieldPtr<uInt> flrRF( trec, "FLAGROW" ) ;
296  RecordFieldPtr<uInt> tcalidRF( trec, "TCAL_ID" ) ;
297  RecordFieldPtr<uInt> widRF( trec, "WEATHER_ID" ) ;
298  RecordFieldPtr<uInt> polnoRF( trec, "POLNO" ) ;
299
300
301  // REFBEAMNO
302  RecordFieldPtr<Int> intRF( trec, "REFBEAMNO" ) ;
303  *intRF = 0 ;
304
305  // FIT_ID
306  intRF.attachToRecord( trec, "FIT_ID" ) ;
307  *intRF = -1 ;
308
309  // OPACITY
310  RecordFieldPtr<Float> floatRF( trec, "OPACITY" ) ;
311  *floatRF = 0.0 ;
312
[1978]313  //
314  // ITERATION: OBSERVATION_ID
315  //
[1987]316  TableIterator iter0( mstable_, "OBSERVATION_ID" ) ;
[1974]317  while( !iter0.pastEnd() ) {
[2004]318    //time0 = gettimeofday_sec() ;
319    //os_ << "start 0th iteration: " << time0 << LogIO::POST ;
[1987]320    Table t0 = iter0.table() ;
321    tcolr = tpoolr->construct( t0, "OBSERVATION_ID" ) ;
322    Int obsId = tcolr->asInt( 0 ) ;
323    tpoolr->destroy( tcolr ) ;
324    if ( sdh.observer == "" ) {
325      tcolr = tpoolr->construct( obstab, "OBSERVER" ) ;
326      sdh.observer = tcolr->asString( obsId ) ;
327      tpoolr->destroy( tcolr ) ;
328    }
329    if ( sdh.project == "" ) {
330      tcolr = tpoolr->construct( obstab, "PROJECT" ) ;
331      sdh.observer = tcolr->asString( obsId ) ;
332      tpoolr->destroy( tcolr ) ;
333    }
334    ROArrayMeasColumn<MEpoch> *tmpMeasCol = new ROArrayMeasColumn<MEpoch>( obstab, "TIME_RANGE" ) ;
335    MEpoch me = (*tmpMeasCol)( obsId )( IPosition(1,0) ) ;
336    delete tmpMeasCol ;
[1974]337    if ( sdh.utc == 0.0 ) {
[1987]338      sdh.utc = me.get( "s" ).getValue() ;
[1974]339    }
[1987]340    if ( telescopeName == "" ) {
341      tcolr = tpoolr->construct( obstab, "TELESCOPE_NAME" ) ;
342      sdh.observer = tcolr->asString( obsId ) ;
343      tpoolr->destroy( tcolr ) ;
344    }
[1978]345    Int nbeam = 0 ;
[2004]346    //time1 = gettimeofday_sec() ;
347    //os_ << "end 0th iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]348    //
349    // ITERATION: FEED1
350    //
351    TableIterator iter1( t0, "FEED1" ) ;
352    while( !iter1.pastEnd() ) {
[2004]353      //time0 = gettimeofday_sec() ;
354      //os_ << "start 1st iteration: " << time0 << LogIO::POST ;
[1987]355      Table t1 = iter1.table() ;
356      // assume FEED1 == FEED2
357      tcolr = tpoolr->construct( t1, "FEED1" ) ;
358      Int feedId = tcolr->asInt( 0 ) ;
359      tpoolr->destroy( tcolr ) ;
[1974]360      nbeam++ ;
[2002]361
362      // BEAMNO
363      RecordFieldPtr<uInt> uintRF( trec, "BEAMNO" ) ;
364      *uintRF = feedId ;
365
366      // FOCUS_ID
367      uintRF.attachToRecord( trec, "FOCUS_ID" ) ;
368      *uintRF = 0 ;
369
[2004]370      //time1 = gettimeofday_sec() ;
371      //os_ << "end 1st iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]372      //
373      // ITERATION: FIELD_ID
374      //
375      TableIterator iter2( t1, "FIELD_ID" ) ;
376      while( !iter2.pastEnd() ) {
[2004]377        //time0 = gettimeofday_sec() ;
378        //os_ << "start 2nd iteration: " << time0 << LogIO::POST ;
[1987]379        Table t2 = iter2.table() ;
380        tcolr = tpoolr->construct( t2, "FIELD_ID" ) ;
381        Int fieldId = tcolr->asInt( 0 ) ;
382        tpoolr->destroy( tcolr ) ;
383        tcolr = tpoolr->construct( fieldtab, "SOURCE_ID" ) ;
384        Int srcId = tcolr->asInt( fieldId ) ;
385        tpoolr->destroy( tcolr ) ;
386        tcolr = tpoolr->construct( fieldtab, "NAME" ) ;
387        String fieldName = tcolr->asString( fieldId ) + "__" + String::toString(fieldId) ;
388        tpoolr->destroy( tcolr ) ;
[2004]389        ROArrayMeasColumn<MDirection> *delayDirCol = new ROArrayMeasColumn<MDirection>( fieldtab, "DELAY_DIR" ) ;
390        Vector<MDirection> delayDir = (*delayDirCol)( fieldId ) ;
391        delete delayDirCol ;
392        Vector<Double> defaultScanrate( 2, 0.0 ) ;
393        Vector<Double> defaultDir = delayDir[0].getAngle( "rad" ).getValue() ;
394        if ( delayDir.nelements() > 1 )
395          defaultScanrate = delayDir[1].getAngle( "rad" ).getValue() ;
396         
[2002]397
398        // FIELDNAME
399        RecordFieldPtr<String> strRF( trec, "FIELDNAME" ) ;
400        *strRF = fieldName ;
401
402
[2004]403        //time1 = gettimeofday_sec() ;
404        //os_ << "end 2nd iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]405        //
406        // ITERATION: DATA_DESC_ID
407        //
408        TableIterator iter3( t2, "DATA_DESC_ID" ) ;
409        while( !iter3.pastEnd() ) {
[2004]410          //time0 = gettimeofday_sec() ;
411          //os_ << "start 3rd iteration: " << time0 << LogIO::POST ;
[1987]412          Table t3 = iter3.table() ;
413          tcolr = tpoolr->construct( t3, "DATA_DESC_ID" ) ;
414          Int ddId = tcolr->asInt( 0 ) ;
415          tpoolr->destroy( tcolr ) ;
416          tcolr = tpoolr->construct( ddtab, "POLARIZATION_ID" ) ;
417          Int polId = tcolr->asInt( ddId ) ;
418          tpoolr->destroy( tcolr ) ;
419          tcolr = tpoolr->construct( ddtab, "SPECTRAL_WINDOW_ID" ) ;
420          Int spwId = tcolr->asInt( ddId ) ;
421          tpoolr->destroy( tcolr ) ;
[2004]422          // TODO: This must be updated if writer will be replaced from STWriter to MSWriter
423          sdh.nif = max( sdh.nif, spwId ) ;
[2002]424
425          // IFNO
426          uintRF.attachToRecord( trec, "IFNO" ) ;
427          *uintRF = (uInt)spwId ;
428
[1974]429          // polarization information
[1987]430          tcolr = tpoolr->construct( poltab, "NUM_CORR" ) ;
431          Int npol = tcolr->asInt( polId ) ;
432          tpoolr->destroy( tcolr ) ;
433          ROArrayColumn<Int> *roArrICol = new ROArrayColumn<Int>( poltab, "CORR_TYPE" ) ;
434          Vector<Int> corrtype = (*roArrICol)( polId ) ;
435          delete roArrICol ;
[1974]436          //os_ << "npol = " << npol << LogIO::POST ;
437          //os_ << "corrtype = " << corrtype << LogIO::POST ;
[1993]438          sdh.npol = max( sdh.npol, npol ) ;
[1974]439          if ( sdh.poltype == "" ) sdh.poltype = getPolType( corrtype[0] ) ;
440          // source information
[2004]441          //os_ << "srcId = " << srcId << ", spwId = " << spwId << LogIO::POST ;
442          MSSource srctabSel = srctab( srctab.col("SOURCE_ID") == srcId && srctab.col("SPECTRAL_WINDOW_ID") == spwId ) ;
443          if ( srctabSel.nrow() == 0 ) {
444            srctabSel = srctab( srctab.col("SOURCE_ID") == srcId && srctab.col("SPECTRAL_WINDOW_ID") == -1 ) ;
445          }
[1987]446          tcolr = tpoolr->construct( srctabSel, "NAME" ) ;
447          String srcName = tcolr->asString( 0 ) ;
448          tpoolr->destroy( tcolr ) ;
[2002]449
450          // SRCNAME
451          strRF.attachToRecord( trec, "SRCNAME" ) ;
452          *strRF = srcName ;
453
[1974]454          //os_ << "srcName = " << srcName << LogIO::POST ;
[1987]455          ROArrayColumn<Double> *roArrDCol = new ROArrayColumn<Double>( srctabSel, "PROPER_MOTION" ) ;
456          Array<Double> srcPM = (*roArrDCol)( 0 ) ;
457          delete roArrDCol ;
[2002]458
459          // SRCPROPERMOTION
460          RecordFieldPtr< Array<Double> > darrRF( trec, "SRCPROPERMOTION" ) ;
461          *darrRF = srcPM ;
462
[1974]463          //os_ << "srcPM = " << srcPM << LogIO::POST ;
[1987]464          roArrDCol = new ROArrayColumn<Double>( srctabSel, "DIRECTION" ) ;
465          Array<Double> srcDir = (*roArrDCol)( 0 ) ;
466          delete roArrDCol ;
[2002]467
468          // SRCDIRECTION
469          darrRF.attachToRecord( trec, "SRCDIRECTION" ) ;
470          *darrRF = srcDir ;
471
[1974]472          //os_ << "srcDir = " << srcDir << LogIO::POST ;
[1987]473          Array<Double> sysVels ;
[1974]474          Double sysVel = 0.0 ;
[1987]475          if ( srctabSel.tableDesc().isColumn( "SYSVEL" ) ) {
476            roArrDCol = new ROArrayColumn<Double>( srctabSel, "SYSVEL" ) ;
477            sysVels = (*roArrDCol)( 0 ) ;
478            delete roArrDCol ;
479          }
[1974]480          if ( !sysVels.empty() ) {
481            //os_ << "sysVels.shape() = " << sysVels.shape() << LogIO::POST ;
[1975]482            // NB: assume all SYSVEL values are the same
[1987]483            sysVel = sysVels( IPosition(1,0) ) ;
[1974]484          }
[2002]485
486          // SRCVELOCITY
487          RecordFieldPtr<Double> doubleRF( trec, "SRCVELOCITY" ) ;
488          *doubleRF = sysVel ;
489
[1987]490          //delete tmpArrCol ;
[1974]491          //os_ << "sysVel = " << sysVel << LogIO::POST ;
[1987]492          ROScalarMeasColumn<MDirection> *tmpMeasCol = new ROScalarMeasColumn<MDirection>( srctabSel, "DIRECTION" ) ;
493          MDirection md = (*tmpMeasCol)( 0 ) ;
494          delete tmpMeasCol ;
[1974]495          // for MOLECULES subtable
[1987]496          tcolr = tpoolr->construct( srctabSel, "NUM_LINES" ) ;
497          Int numLines = tcolr->asInt( 0 ) ;
498          tpoolr->destroy( tcolr ) ;
[1974]499          //os_ << "numLines = " << numLines << LogIO::POST ;
500          Vector<Double> restFreqs( numLines, 0.0 ) ;
501          Vector<String> transitionName( numLines, "" ) ;
502          if ( numLines != 0 ) {
[1987]503            if ( srctabSel.tableDesc().isColumn( "REST_FREQUENCY" ) ) {
504              sharedQDArrCol = new ROArrayQuantColumn<Double>( srctabSel, "REST_FREQUENCY" ) ;
505              Array< Quantum<Double> > qRestFreqs = (*sharedQDArrCol)( 0 ) ;
506              delete sharedQDArrCol ;
507              for ( int i = 0 ; i < numLines ; i++ ) {
508                restFreqs[i] = qRestFreqs( IPosition( 1, i ) ).getValue( "Hz" ) ;
509              }
[1974]510            }
511            //os_ << "restFreqs = " << restFreqs << LogIO::POST ;
512            if ( srctabSel.tableDesc().isColumn( "TRANSITION" ) ) {
[2004]513              ROArrayColumn<String> transitionCol( srctabSel, "TRANSITION" ) ;
514              if ( transitionCol.isDefined( 0 ) )
515                transitionName = transitionCol( 0 ) ;
516              //os_ << "transitionNameCol.nrow() = " << transitionCol.nrow() << LogIO::POST ;
[1974]517            }
518          }
519          uInt molId = table_->molecules().addEntry( restFreqs, transitionName, transitionName ) ;
[2002]520
521          // MOLECULE_ID
522          uintRF.attachToRecord( trec, "MOLECULE_ID" ) ;
523          *uintRF = molId ;
524
[1974]525          // spectral setup
526          MeasFrame mf( me, mp, md ) ;
[1987]527          tcolr = tpoolr->construct( spwtab, "MEAS_FREQ_REF" ) ;
528          MFrequency::Types freqRef = MFrequency::castType((uInt)(tcolr->asInt(spwId))) ;
529          tpoolr->destroy( tcolr ) ;
530          tcolr = tpoolr->construct( spwtab, "NUM_CHAN" ) ;
531          Int nchan = tcolr->asInt( spwId ) ;
532          tpoolr->destroy( tcolr ) ;
[1974]533          Bool even = False ;
534          if ( (nchan/2)*2 == nchan ) even = True ;
[1993]535          sdh.nchan = max( sdh.nchan, nchan ) ;
[1987]536          ROScalarQuantColumn<Double> *tmpQuantCol = new ROScalarQuantColumn<Double>( spwtab, "TOTAL_BANDWIDTH" ) ;
537          Double totbw = (*tmpQuantCol)( spwId ).getValue( "Hz" ) ;
538          delete tmpQuantCol ;
[1993]539          sdh.bandwidth = max( sdh.bandwidth, totbw ) ;
[1974]540          if ( sdh.freqref == "" )
541            //sdh.freqref = MFrequency::showType( freqRef ) ;
542            sdh.freqref = "LSRK" ;
543          if ( sdh.reffreq == -1.0 ) {
[1987]544            tmpQuantCol = new ROScalarQuantColumn<Double>( spwtab, "REF_FREQUENCY" ) ;
545            Quantum<Double> qreffreq = (*tmpQuantCol)( spwId ) ;
546            delete tmpQuantCol ;
[1974]547            if ( freqRef == MFrequency::LSRK ) {
548              sdh.reffreq = qreffreq.getValue("Hz") ;
549            }
550            else {
551              MFrequency::Convert tolsr( freqRef, MFrequency::Ref( MFrequency::LSRK, mf ) ) ;
552              sdh.reffreq = tolsr( qreffreq ).get("Hz").getValue() ;
553            }
554          }
555          Int refchan = nchan / 2 ;
556          IPosition refip( 1, refchan ) ;
557          Double refpix = 0.5*(nchan-1) ;
558          Double refval = 0.0 ;
[1987]559          sharedQDArrCol = new ROArrayQuantColumn<Double>( spwtab, "CHAN_WIDTH" ) ;
560          Double increment = (*sharedQDArrCol)( spwId )( refip ).getValue( "Hz" ) ;
561          delete sharedQDArrCol ;
[1974]562          //os_ << "nchan = " << nchan << " refchan = " << refchan << "(even=" << even << ") refpix = " << refpix << LogIO::POST ;
[1987]563          sharedQDArrCol = new ROArrayQuantColumn<Double>( spwtab, "CHAN_FREQ" ) ;
564          Vector< Quantum<Double> > chanFreqs = (*sharedQDArrCol)( spwId ) ;
565          delete sharedQDArrCol ;
[1974]566          if ( freqRef == MFrequency::LSRK ) {
567            if ( even ) {
568              IPosition refip0( 1, refchan-1 ) ;
569              Double refval0 = chanFreqs(refip0).getValue("Hz") ;
570              Double refval1 = chanFreqs(refip).getValue("Hz") ;
571              refval = 0.5 * ( refval0 + refval1 ) ;
572            }
573            else {
574              refval = chanFreqs(refip).getValue("Hz") ;
575            }
576          }
577          else {
578            MFrequency::Convert tolsr( freqRef, MFrequency::Ref( MFrequency::LSRK, mf ) ) ;
579            if ( even ) {
580              IPosition refip0( 1, refchan-1 ) ;
581              Double refval0 = chanFreqs(refip0).getValue("Hz") ;
582              Double refval1 = chanFreqs(refip).getValue("Hz") ;
583              refval = 0.5 * ( refval0 + refval1 ) ;
584              refval = tolsr( refval ).get("Hz").getValue() ;
585            }
586            else {
587              refval = tolsr( chanFreqs(refip) ).get("Hz").getValue() ;
588            }
589          }
590          uInt freqId = table_->frequencies().addEntry( refpix, refval, increment ) ;
591          if ( ifmap.find( spwId ) == ifmap.end() ) {
592            ifmap.insert( pair<Int, uInt>(spwId,freqId) ) ;
593            //os_ << "added to ifmap: (" << spwId << "," << freqId << ")" << LogIO::POST ;
594          }
[2002]595
596          // FREQ_ID
597          uintRF.attachToRecord( trec, "FREQ_ID" ) ;
598          *uintRF = freqId ;
599
[1974]600          // for TSYS and TCAL
[2004]601          Vector<MEpoch> scTime ;
602          Vector<Double> scInterval ;
603          ROArrayColumn<Float> scTsysCol ;
604          MSSysCal caltabsel ;
605          if ( isSysCal_ ) {
606            caltabsel = caltab( caltab.col("ANTENNA_ID") == antenna_ && caltab.col("FEED_ID") == feedId && caltab.col("SPECTRAL_WINDOW_ID") == spwId ).sort("TIME") ;
607            ROScalarMeasColumn<MEpoch> scTimeCol( caltabsel, "TIME" ) ;
608            scTime.resize( caltabsel.nrow() ) ;
609            for ( uInt irow = 0 ; irow < caltabsel.nrow() ; irow++ )
610              scTime[irow] = scTimeCol( irow ) ;
611            ROScalarColumn<Double> *scIntervalCol = new ROScalarColumn<Double>( caltabsel, "INTERVAL" ) ;
612            scInterval = scIntervalCol->getColumn() ;
613            delete scIntervalCol ;
614            scTsysCol.attach( caltabsel, colTsys_ ) ;
615          }
616          //time1 = gettimeofday_sec() ;
617          //os_ << "end 3rd iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]618          //
619          // ITERATION: SCAN_NUMBER
620          //
621          TableIterator iter4( t3, "SCAN_NUMBER" ) ;
622          while( !iter4.pastEnd() ) {
[2004]623            //time0 = gettimeofday_sec() ;
624            //os_ << "start 4th iteration: " << time0 << LogIO::POST ;
[1987]625            Table t4 = iter4.table() ;
626            tcolr = tpoolr->construct( t4, "SCAN_NUMBER" ) ;
627            Int scanNum = tcolr->asInt( 0 ) ;
628            tpoolr->destroy( tcolr ) ;
[2002]629
630            // SCANNO
631            uintRF.attachToRecord( trec, "SCANNO" ) ;
632            *uintRF = scanNum - 1 ;
633
[2004]634            //time1 = gettimeofday_sec() ;
635            //os_ << "end 4th iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]636            //
637            // ITERATION: STATE_ID
638            //
639            TableIterator iter5( t4, "STATE_ID" ) ;
640            while( !iter5.pastEnd() ) {
[2004]641              //time0 = gettimeofday_sec() ;
642              //os_ << "start 5th iteration: " << time0 << LogIO::POST ;
[1987]643              Table t5 = iter5.table() ;
644              tcolr = tpoolr->construct( t5, "STATE_ID" ) ;
645              Int stateId = tcolr->asInt( 0 ) ;
646              tpoolr->destroy( tcolr ) ;
647              tcolr = tpoolr->construct( stattab, "OBS_MODE" ) ;
648              String obstype = tcolr->asString( stateId ) ;
649              tpoolr->destroy( tcolr ) ;
[1974]650              if ( sdh.obstype == "" ) sdh.obstype = obstype ;
651
652              Int nrow = t5.nrow() ;
[2004]653              //time1 = gettimeofday_sec() ;
654              //os_ << "end 5th iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[2002]655
[2004]656              uInt cycle = 0 ;
657
[2002]658              Cube<Float> spArr ;
659              Cube<Bool> flArr ;
[1974]660              if ( isFloatData_ ) {
[2002]661                ROArrayColumn<Bool> mFlagCol( t5, "FLAG" ) ;
[1974]662                ROArrayColumn<Float> mFloatDataCol( t5, "FLOAT_DATA" ) ;
[2002]663                spArr = mFloatDataCol.getColumn() ;
664                flArr = mFlagCol.getColumn() ;
[1974]665                if ( sdh.fluxunit == "" ) {
[2002]666                  const TableRecord &dataColKeys = mFloatDataCol.keywordSet() ;
[1974]667                  if ( dataColKeys.isDefined( "UNIT" ) )
668                    sdh.fluxunit = dataColKeys.asString( "UNIT" ) ;
669                }
670              }
671              else if ( isData_ ) {
[2002]672                spArr.resize( npol, nchan, nrow ) ;
[2004]673                flArr.resize( npol, nchan, nrow ) ;
[2002]674                ROArrayColumn<Bool> mFlagCol( t5, "FLAG" ) ;
[1974]675                ROArrayColumn<Complex> mDataCol( t5, "DATA" ) ;
[1987]676                for ( Int irow = 0 ; irow < nrow ; irow++ ) {
677                  Bool crossOK = False ;
678                  Matrix<Complex> sp = mDataCol( irow ) ;
[2002]679                  Matrix<Bool> fl = mFlagCol( irow ) ;
[2004]680                  Matrix<Float> spxy = spArr.xyPlane( irow ) ;
681                  Matrix<Bool> flxy = flArr.xyPlane( irow ) ;
[1987]682                  for ( Int ipol = 0 ; ipol < npol ; ipol++ ) {
683                    if ( corrtype[ipol] == Stokes::XY || corrtype[ipol] == Stokes::YX
684                         || corrtype[ipol] == Stokes::RL || corrtype[ipol] == Stokes::LR ) {
685                      if ( !crossOK ) {
[2004]686                        spxy.row( ipol ) = real( sp.row( ipol ) ) ;
687                        flxy.row( ipol ) = fl.row( ipol ) ;
[2002]688                        if ( corrtype[ipol] == Stokes::XY || corrtype[ipol] == Stokes::RL ) {
[2004]689                          spxy.row( ipol+1 ) = imag( sp.row( ipol ) ) ;
690                          flxy.row( ipol+1 ) = fl.row( ipol ) ;
[2002]691                        }                       
692                        else {
[2004]693                          spxy.row( ipol+1 ) = imag( conj( sp.row( ipol ) ) ) ;
694                          flxy.row( ipol+1 ) = fl.row( ipol ) ;
[2002]695                        }
[2004]696                        crossOK = True ;
[1974]697                      }
698                    }
[1987]699                    else {
[2004]700                      spxy.row( ipol ) = real( sp.row( ipol ) ) ;
701                      flxy.row( ipol ) = fl.row( ipol ) ;
[1987]702                    }
[1974]703                  }
[1987]704                }
[1974]705                if ( sdh.fluxunit == "" ) {
[2002]706                  const TableRecord &dataColKeys = mDataCol.keywordSet() ;
[1974]707                  if ( dataColKeys.isDefined( "UNIT" ) )
[2002]708                    sdh.fluxunit = dataColKeys.asString( "UNIT" ) ;
709                }
[1974]710              }
[1987]711              ROScalarMeasColumn<MEpoch> *mTimeCol = new ROScalarMeasColumn<MEpoch>( t5, "TIME" ) ;
[2002]712              Block<MEpoch> mTimeB( nrow ) ;
713              for ( Int irow = 0 ; irow < nrow ; irow++ )
714                mTimeB[irow] = (*mTimeCol)( irow ) ;
715              ROTableColumn *mIntervalCol = tpoolr->construct( t5, "INTERVAL" ) ;
716              ROTableColumn *mFlagRowCol = tpoolr->construct( t5, "FLAG_ROW" ) ;
717              Block<Double> sysCalTime( nrow, -1.0 ) ;
718              Block<Int> sysCalIdx( nrow, -1 ) ;
719              if ( isSysCal_ ) {
720                getSysCalTime( scTime, scInterval, mTimeB, sysCalTime, sysCalIdx ) ;
[1974]721              }
[2002]722              delete mTimeCol ;
723              Matrix<Float> defaulttsys( npol, 1, 1.0 ) ;
724              Int srcType = getSrcType( stateId, tpoolr ) ;
725              uInt diridx = 0 ;
726              MDirection::Types dirType ;
727              uInt wid = 0 ;
728              Int pidx = 0 ;
729              Bool crossOK = False ;
730              Block<uInt> polnos( npol, 99 ) ;
731              for ( Int ipol = 0 ; ipol < npol ; ipol++ ) {
732                Block<uInt> p = getPolNo( corrtype[ipol] ) ;
733                if ( p.size() > 1 ) {
734                  if ( crossOK ) continue ;
735                  else {
736                    polnos[pidx] = p[0] ;
737                    pidx++ ;
738                    polnos[pidx] = p[1] ;
739                    pidx++ ;
740                    crossOK = True ;
[1974]741                  }
742                }
[2002]743                else {
744                  polnos[pidx] = p[0] ;
745                  pidx++ ;
746                }
[1974]747              }
[2002]748             
749              // SRCTYPE
750              intRF.attachToRecord( trec, "SRCTYPE" ) ;
751              *intRF = srcType ;
[1974]752
[2002]753              for ( Int irow = 0 ; irow < nrow ; irow++ ) {
754                // CYCLENO
755                *cycleRF = cycle ;
[1987]756
[2002]757                // FLAGROW
758                *flrRF = (uInt)mFlagRowCol->asBool( irow ) ;
759
760                // SPECTRA, FLAG
761                Matrix<Float> sp = spArr.xyPlane( irow ) ;
762                Matrix<Bool> flb = flArr.xyPlane( irow ) ;
763                Matrix<uChar> fl( flb.shape() ) ;
764                convertArray( fl, flb ) ;
765
766                // TIME
[2004]767                *timeRF = mTimeB[irow].get("d").getValue() ;
[2002]768
769                // INTERVAL
770                *intervalRF = (Double)(mIntervalCol->asdouble( irow )) ;
771
772                // TSYS
773                Matrix<Float> tsys ;
774                if ( sysCalIdx[irow] != -1 )
775                  tsys = scTsysCol( irow ) ;
776                else
777                  tsys = defaulttsys ;
778
779                // TCAL_ID
[2004]780                Block<uInt> tcalids( npol, 0 ) ;
781                if ( sysCalIdx[irow] != -1 ) {
782                  tcalids = getTcalId( feedId, spwId, scTime[sysCalIdx[irow]] ) ;
[1987]783                }
784
[2002]785                // WEATHER_ID
786                if ( isWeather_ )
787                  wid = getWeatherId( wid, mTimeB[irow].get("s").getValue() ) ;
788                *widRF = wid ;
789                 
[1987]790
[2002]791                // DIRECTION, AZEL, SCANRATE
792                if ( getPt_ ) {
[1974]793                  Vector<Double> dir ;
794                  Vector<Double> scanrate ;
795                  String refString ;
[2002]796                  diridx = getDirection( diridx, dir, scanrate, refString, pointtab, mTimeB[irow].get("s").getValue() ) ;
[1974]797                  MDirection::getType( dirType, refString ) ;
[2002]798                  mf.resetEpoch( mTimeB[irow] ) ;
[1974]799                  mf.resetDirection( MDirection( MVDirection(dir), dirType ) ) ;
800                  if ( refString == "J2000" ) {
[2002]801                    *dirRF = dir ;
[1974]802                    MDirection::Convert toazel( dirType, MDirection::Ref( MDirection::AZEL, mf ) ) ;
803                    Vector<Double> azel = toazel( dir ).getAngle("rad").getValue() ;
[2002]804                    *azRF = (Float)azel(0) ;
805                    *elRF = (Float)azel(1) ;
[1974]806                  }
807                  else if ( refString(0,4) == "AZEL" ) {
[2002]808                    *azRF = (Float)dir(0) ;
809                    *elRF = (Float)dir(1) ;
[1974]810                    MDirection::Convert toj2000( dirType, MDirection::Ref( MDirection::J2000, mf ) ) ;
811                    Vector<Double> newdir = toj2000( dir ).getAngle("rad").getValue() ;
[2002]812                    *dirRF = newdir ;
[1974]813                  }
814                  else {
815                    MDirection::Convert toazel( dirType, MDirection::Ref( MDirection::AZEL, mf ) ) ;
816                    Vector<Double> azel = toazel( dir ).getAngle("rad").getValue() ;
817                    MDirection::Convert toj2000( dirType, MDirection::Ref( MDirection::J2000, mf ) ) ;
818                    Vector<Double> newdir = toj2000( dir ).getAngle("rad").getValue() ;
[2002]819                    *dirRF = newdir ;
820                    *azRF = (Float)azel(0) ;
821                    *elRF = (Float)azel(1) ;
[1974]822                  }
823                  if ( scanrate.size() != 0 ) {
[2002]824                    *scrRF = scanrate ;
[1974]825                  }
826                  else {
[2002]827                    *scrRF = defaultScanrate ;
[1974]828                  }
829                }
[2002]830                else {
831                  String ref = md.getRefString() ;
[2004]832                  //Vector<Double> defaultDir = srcDir ;
[2002]833                  MDirection::getType( dirType, "J2000" ) ;
834                  if ( ref != "J2000" ) {
835                    ROScalarMeasColumn<MEpoch> tmCol( pointtab, "TIME" ) ;
836                    mf.resetEpoch( tmCol( 0 ) ) ;
837                    MDirection::Convert toj2000( dirType, MDirection::Ref( MDirection::J2000, mf ) ) ;
838                    defaultDir = toj2000( defaultDir ).getAngle("rad").getValue() ;
[1974]839                  }
[2002]840                  mf.resetEpoch( mTimeB[irow] ) ;
841                  MDirection::Convert toazel( dirType, MDirection::Ref( MDirection::AZEL, mf ) ) ;
842                  Vector<Double> azel = toazel( defaultDir ).getAngle("rad").getValue() ;
843                  *azRF = (Float)azel(0) ;
844                  *elRF = (Float)azel(1) ;
845                  *dirRF = defaultDir ;
846                  *scrRF = defaultScanrate ;
[1974]847                }
848
[2002]849                // Polarization dependent things
850                for ( Int ipol = 0 ; ipol < npol ; ipol++ ) {
851                  // POLNO
852                  *polnoRF = polnos[ipol] ;
[1987]853
[2002]854                  *spRF = sp.row( ipol ) ;
855                  *ucarrRF = fl.row( ipol ) ;
856                  *tsysRF = tsys.row( ipol ) ;
857                  *tcalidRF = tcalids[ipol] ;
[1987]858
[2002]859                  // Commit row
860                  stab.addRow() ;
861                  row.put( stab.nrow()-1 ) ;
[1987]862                }
[2004]863
864                cycle++ ;
[1987]865              }
[2002]866              tpoolr->destroy( mIntervalCol ) ;
867              tpoolr->destroy( mFlagRowCol ) ;
[1987]868
[2004]869              //time1 = gettimeofday_sec() ;
870              //os_ << "end 5th iteration: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]871
872              iter5.next() ;
873            }
[1978]874
875
[1974]876            iter4.next() ;
877          }
[1978]878
879
[1974]880          iter3.next() ;
881        }
[1978]882
[1990]883             
[1974]884        iter2.next() ;
885      }
[1978]886
887
[1974]888      iter1.next() ;
889    }
890    if ( sdh.nbeam < nbeam ) sdh.nbeam = nbeam ;
[1978]891
[1974]892    iter0.next() ;
893  }
894
[1987]895
896  delete tpoolr ;
897
898
[1978]899  // Table Keywords
[2004]900  // TODO: This must be updated if writer will be replaced from STWriter to MSWriter
901//   sdh.nif = ifmap.size() ;
902  sdh.nif++ ;
[1987]903  if ( ( telescopeName == "" ) || ( antennaName == telescopeName ) ) {
[1974]904    sdh.antennaname = antennaName ;
905  }
906  else {
907    sdh.antennaname = telescopeName + "//" + antennaName ;
908  }
909  if ( stationName != "" ) {
910    sdh.antennaname += "@" + stationName ;
911  }
[1987]912  ROArrayColumn<Double> pdirCol( pointtab, "DIRECTION" ) ;
913  String dirref = pdirCol.keywordSet().asRecord("MEASINFO").asString("Ref") ;
[1974]914  if ( dirref == "AZELGEO" || dirref == "AZEL" ) {
915    dirref = "J2000" ;
916  }
917  sscanf( dirref.chars()+1, "%f", &sdh.equinox ) ;
918  sdh.epoch = "UTC" ;
919  if (sdh.freqref == "TOPO") {
920    sdh.freqref = "TOPOCENT";
921  } else if (sdh.freqref == "GEO") {
922    sdh.freqref = "GEOCENTR";
923  } else if (sdh.freqref == "BARY") {
924    sdh.freqref = "BARYCENT";
925  } else if (sdh.freqref == "GALACTO") {
926    sdh.freqref = "GALACTOC";
927  } else if (sdh.freqref == "LGROUP") {
928    sdh.freqref = "LOCALGRP";
929  } else if (sdh.freqref == "CMB") {
930    sdh.freqref = "CMBDIPOL";
931  } else if (sdh.freqref == "REST") {
932    sdh.freqref = "SOURCE";
933  }
934  table_->setHeader( sdh ) ;
[1987]935
936  // save path to POINTING table
937  //Path datapath(mstable_.tableName()) ;
938  Path datapath( tablename_ ) ;
939  String pTabName = datapath.absoluteName() + "/POINTING" ;
[1990]940  stab.rwKeywordSet().define( "POINTING", pTabName ) ;
[1987]941
942  // for GBT
943  if ( antennaName == "GBT" ) {
944    String goTabName = datapath.absoluteName() + "/GBT_GO" ;
[1990]945    stab.rwKeywordSet().define( "GBT_GO", goTabName ) ;
[1987]946  }
[2004]947//   double endSec = gettimeofday_sec() ;
948//   os_ << "end MSFiller::fill() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]949}
950
951void MSFiller::close()
952{
[1987]953  //tablesel_.closeSubTables() ;
[1974]954  mstable_.closeSubTables() ;
[1987]955  //tablesel_.unlock() ;
[1974]956  mstable_.unlock() ;
957}
958
[1987]959Int MSFiller::getSrcType( Int stateId, boost::object_pool<ROTableColumn> *tpool )
[1974]960{
[2004]961//   double startSec = gettimeofday_sec() ;
962//   os_ << "start MSFiller::getSrcType() startSec=" << startSec << LogIO::POST ;
[1974]963
964  MSState statetab = mstable_.state() ;
[1987]965  ROTableColumn *sharedCol ;
966  sharedCol = tpool->construct( statetab, "OBS_MODE" ) ;
967  String obsMode = sharedCol->asString( stateId ) ;
968  tpool->destroy( sharedCol ) ;
969  sharedCol = tpool->construct( statetab, "SIG" ) ;
970  Bool sig = sharedCol->asBool( stateId ) ;
971  tpool->destroy( sharedCol ) ;
972  sharedCol = tpool->construct( statetab, "REF" ) ;
973  Bool ref = sharedCol->asBool( stateId ) ;
974  tpool->destroy( sharedCol ) ;
975  sharedCol = tpool->construct( statetab, "CAL" ) ;
976  Double cal = (Double)(sharedCol->asdouble( stateId )) ;
977  tpool->destroy( sharedCol ) ;
[1974]978  //os_ << "OBS_MODE = " << obsMode << LogIO::POST ;
979
980  // determine separator
981  String sep = "" ;
982  if ( obsMode.find( ":" ) != String::npos ) {
983    sep = ":" ;
984  }
985  else if ( obsMode.find( "." ) != String::npos ) {
986    sep = "." ;
987  }
988 
989  // determine SRCTYPE
990  Int srcType = SrcType::NOTYPE ;
991  if ( sep == ":" ) {
992    // sep == ":"
993    //
994    // GBT case
995    //
996    // obsMode1=Nod
997    //    NOD
998    // obsMode1=OffOn
999    //    obsMode2=PSWITCHON:  PSON
1000    //    obsMode2=PSWITCHOFF: PSOFF
1001    // obsMode1=??
1002    //    obsMode2=FSWITCH:
1003    //       SIG=1: FSON
1004    //       REF=1: FSOFF
[1987]1005    // Calibration scan if CAL != 0
[1974]1006    Int epos = obsMode.find_first_of( sep ) ;
1007    Int nextpos = obsMode.find_first_of( sep, epos+1 ) ;
1008    String obsMode1 = obsMode.substr( 0, epos ) ;
1009    String obsMode2 = obsMode.substr( epos+1, nextpos-epos-1 ) ;
1010    if ( obsMode1 == "Nod" ) {
1011      srcType = SrcType::NOD ;
1012    }
1013    else if ( obsMode1 == "OffOn" ) {
1014      if ( obsMode2 == "PSWITCHON" ) srcType = SrcType::PSON ;
1015      if ( obsMode2 == "PSWITCHOFF" ) srcType = SrcType::PSOFF ;
1016    }
1017    else {
1018      if ( obsMode2 == "FSWITCH" ) {
1019        if ( sig ) srcType = SrcType::FSON ;
1020        if ( ref ) srcType = SrcType::FSOFF ;
1021      }
1022    }
[1987]1023    if ( cal > 0.0 ) {
1024      if ( srcType == SrcType::NOD )
1025        srcType = SrcType::NODCAL ;
1026      else if ( srcType == SrcType::PSON )
1027        srcType = SrcType::PONCAL ;
1028      else if ( srcType == SrcType::PSOFF )
1029        srcType = SrcType::POFFCAL ;
1030      else if ( srcType == SrcType::FSON )
1031        srcType = SrcType::FONCAL ;
1032      else if ( srcType == SrcType::FSOFF )
1033        srcType = SrcType::FOFFCAL ;
1034      else
1035        srcType = SrcType::CAL ;
1036    }
[1974]1037  }
1038  else if ( sep == "." ) {
1039    // sep == "."
1040    //
1041    // ALMA & EVLA case (MS via ASDM)
1042    //
1043    // obsMode1=CALIBRATE_*
1044    //    obsMode2=ON_SOURCE: PONCAL
1045    //    obsMode2=OFF_SOURCE: POFFCAL
1046    // obsMode1=OBSERVE_TARGET
1047    //    obsMode2=ON_SOURCE: PON
1048    //    obsMode2=OFF_SOURCE: POFF
1049    Int epos = obsMode.find_first_of( sep ) ;
1050    Int nextpos = obsMode.find_first_of( sep, epos+1 ) ;
1051    String obsMode1 = obsMode.substr( 0, epos ) ;
1052    String obsMode2 = obsMode.substr( epos+1, nextpos-epos-1 ) ;
1053    if ( obsMode1.find( "CALIBRATE_" ) == 0 ) {
1054      if ( obsMode2 == "ON_SOURCE" ) srcType = SrcType::PONCAL ;
1055      if ( obsMode2 == "OFF_SOURCE" ) srcType = SrcType::POFFCAL ;
1056    }
1057    else if ( obsMode1 == "OBSERVE_TARGET" ) {
1058      if ( obsMode2 == "ON_SOURCE" ) srcType = SrcType::PSON ;
1059      if ( obsMode2 == "OFF_SOURCE" ) srcType = SrcType::PSOFF ;
1060    }
1061  }
1062  else {
1063    if ( sig ) srcType = SrcType::SIG ;
1064    if ( ref ) srcType = SrcType::REF ;
1065  }
1066   
1067  //os_ << "srcType = " << srcType << LogIO::POST ;
[2004]1068//   double endSec = gettimeofday_sec() ;
1069//   os_ << "end MSFiller::getSrcType() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1070  return srcType ;
1071}
1072
[2002]1073//Vector<uInt> MSFiller::getPolNo( Int corrType )
1074Block<uInt> MSFiller::getPolNo( Int corrType )
[1974]1075{
[2004]1076//   double startSec = gettimeofday_sec() ;
1077//   os_ << "start MSFiller::getPolNo() startSec=" << startSec << LogIO::POST ;
[2002]1078  Block<uInt> polno( 1 ) ;
[1974]1079
1080  if ( corrType == Stokes::I || corrType == Stokes::RR || corrType == Stokes::XX ) {
1081    polno = 0 ;
1082  }
1083  else if ( corrType == Stokes::Q || corrType == Stokes::LL || corrType == Stokes::YY ) {
1084    polno = 1 ;
1085  }
1086  else if ( corrType == Stokes::U ) {
1087    polno = 2 ;
1088  }
1089  else if ( corrType == Stokes::V ) {
1090    polno = 3 ;
1091  }
1092  else if ( corrType == Stokes::RL || corrType == Stokes::XY || corrType == Stokes::LR || corrType == Stokes::RL ) {
1093    polno.resize( 2 ) ;
1094    polno[0] = 2 ;
1095    polno[1] = 3 ;
1096  }
1097  else if ( corrType == Stokes::Plinear ) {
1098    polno[0] = 1 ;
1099  }
1100  else if ( corrType == Stokes::Pangle ) {
1101    polno[0] = 2 ;
1102  }
1103  else {
1104    polno = 99 ;
1105  }
1106  //os_ << "polno = " << polno << LogIO::POST ;
[2004]1107//   double endSec = gettimeofday_sec() ;
1108//   os_ << "end MSFiller::getPolNo() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1109 
1110  return polno ;
1111}
1112
1113String MSFiller::getPolType( Int corrType )
1114{
[2004]1115//   double startSec = gettimeofday_sec() ;
1116//   os_ << "start MSFiller::getPolType() startSec=" << startSec << LogIO::POST ;
[1974]1117  String poltype = "" ;
1118
1119  if ( corrType == Stokes::I || corrType == Stokes::Q || corrType == Stokes::U || corrType == Stokes::V )
1120    poltype = "stokes" ;
1121  else if ( corrType == Stokes::XX || corrType == Stokes::YY || corrType == Stokes::XY || corrType == Stokes::YX )
1122    poltype = "linear" ;
1123  else if ( corrType == Stokes::RR || corrType == Stokes::LL || corrType == Stokes::RL || corrType == Stokes::LR )
1124    poltype = "circular" ;
1125  else if ( corrType == Stokes::Plinear || corrType == Stokes::Pangle )
1126    poltype = "linpol" ;
1127
[2004]1128//   double endSec = gettimeofday_sec() ;
1129//   os_ << "end MSFiller::getPolType() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1130  return poltype ;
1131}
1132
1133void MSFiller::fillWeather()
1134{
[2004]1135//   double startSec = gettimeofday_sec() ;
1136//   os_ << "start MSFiller::fillWeather() startSec=" << startSec << LogIO::POST ;
1137
1138  if ( !isWeather_ ) {
1139    // add dummy row
1140    table_->weather().table().addRow(1,True) ;
1141    return ;
1142  }
1143
[1987]1144  Table mWeather = mstable_.weather()  ;
1145  //Table mWeatherSel = mWeather( mWeather.col("ANTENNA_ID") == antenna_ ).sort("TIME") ;
1146  Table mWeatherSel( mWeather( mWeather.col("ANTENNA_ID") == antenna_ ).sort("TIME") ) ;
[1974]1147  //os_ << "mWeatherSel.nrow() = " << mWeatherSel.nrow() << LogIO::POST ;
1148  if ( mWeatherSel.nrow() == 0 ) {
1149    os_ << "No rows with ANTENNA_ID = " << antenna_ << ", Try -1..." << LogIO::POST ;
[1987]1150    mWeatherSel = Table( MSWeather( mWeather( mWeather.col("ANTENNA_ID") == -1 ) ) ) ;
[1974]1151    if ( mWeatherSel.nrow() == 0 ) {
1152      os_ << "No rows in WEATHER table" << LogIO::POST ;
1153    }
1154  }
[1987]1155  uInt wnrow = mWeatherSel.nrow() ;
[1974]1156  //os_ << "wnrow = " << wnrow << LogIO::POST ;
1157
1158  if ( wnrow == 0 )
1159    return ;
1160
1161  Table wtab = table_->weather().table() ;
1162  wtab.addRow( wnrow ) ;
1163
[1987]1164  ScalarColumn<Float> *fCol ;
1165  ROScalarColumn<Float> *sharedFloatCol ;
1166  if ( mWeatherSel.tableDesc().isColumn( "TEMPERATURE" ) ) {
1167    fCol = new ScalarColumn<Float>( wtab, "TEMPERATURE" ) ;
1168    sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "TEMPERATURE" ) ;
1169    fCol->putColumn( *sharedFloatCol ) ;
1170    delete sharedFloatCol ;
1171    delete fCol ;
1172  }
1173  if ( mWeatherSel.tableDesc().isColumn( "PRESSURE" ) ) {
1174    fCol = new ScalarColumn<Float>( wtab, "PRESSURE" ) ;
1175    sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "PRESSURE" ) ;
1176    fCol->putColumn( *sharedFloatCol ) ;
1177    delete sharedFloatCol ;
1178    delete fCol ;
1179  }
1180  if ( mWeatherSel.tableDesc().isColumn( "REL_HUMIDITY" ) ) {
1181    fCol = new ScalarColumn<Float>( wtab, "HUMIDITY" ) ;
1182    sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "REL_HUMIDITY" ) ;
1183    fCol->putColumn( *sharedFloatCol ) ;
1184    delete sharedFloatCol ;
1185    delete fCol ;
1186  }
1187  if ( mWeatherSel.tableDesc().isColumn( "WIND_SPEED" ) ) { 
1188    fCol = new ScalarColumn<Float>( wtab, "WINDSPEED" ) ;
1189    sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "WIND_SPEED" ) ;
1190    fCol->putColumn( *sharedFloatCol ) ;
1191    delete sharedFloatCol ;
1192    delete fCol ;
1193  }
1194  if ( mWeatherSel.tableDesc().isColumn( "WIND_DIRECTION" ) ) {
1195    fCol = new ScalarColumn<Float>( wtab, "WINDAZ" ) ;
1196    sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "WIND_DIRECTION" ) ;
1197    fCol->putColumn( *sharedFloatCol ) ;
1198    delete sharedFloatCol ;
1199    delete fCol ;
1200  }
[1974]1201  ScalarColumn<uInt> idCol( wtab, "ID" ) ;
[1987]1202  for ( uInt irow = 0 ; irow < wnrow ; irow++ )
1203    idCol.put( irow, irow ) ;
[1974]1204
[1987]1205  ROScalarQuantColumn<Double> tqCol( mWeatherSel, "TIME" ) ;
1206  ROScalarColumn<Double> tCol( mWeatherSel, "TIME" ) ;
1207  String tUnit = tqCol.getUnits() ;
1208  mwTime_ = tCol.getColumn() ;
[1974]1209  if ( tUnit == "d" )
1210    mwTime_ *= 86400.0 ;
[1987]1211  tqCol.attach( mWeatherSel, "INTERVAL" ) ;
1212  tCol.attach( mWeatherSel, "INTERVAL" ) ;
1213  String iUnit = tqCol.getUnits() ;
1214  mwInterval_ = tCol.getColumn() ;
[1974]1215  if ( iUnit == "d" )
1216    mwInterval_ *= 86400.0 ;
1217  //os_ << "mwTime[0] = " << mwTime_[0] << " mwInterval[0] = " << mwInterval_[0] << LogIO::POST ;
[2004]1218//   double endSec = gettimeofday_sec() ;
1219//   os_ << "end MSFiller::fillWeather() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1220}
1221
1222void MSFiller::fillFocus()
1223{
[2004]1224//   double startSec = gettimeofday_sec() ;
1225//   os_ << "start MSFiller::fillFocus() startSec=" << startSec << LogIO::POST ;
[1974]1226  // tentative
1227  Table tab = table_->focus().table() ;
1228  tab.addRow( 1 ) ;
1229  ScalarColumn<uInt> idCol( tab, "ID" ) ;
1230  idCol.put( 0, 0 ) ;
[2004]1231//   double endSec = gettimeofday_sec() ;
1232//   os_ << "end MSFiller::fillFocus() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1233}
1234
[2002]1235void MSFiller::fillTcal( boost::object_pool<ROTableColumn> *tpoolr )
[1974]1236{
[2004]1237//   double startSec = gettimeofday_sec() ;
1238//   os_ << "start MSFiller::fillTcal() startSec=" << startSec << LogIO::POST ;
[1987]1239
[2004]1240  if ( !isSysCal_ ) {
1241    // add dummy row
1242    os_ << "No SysCal rows" << LogIO::POST ;
1243    table_->tcal().table().addRow(1,True) ;
1244    return ;
1245  }
1246
[1987]1247  Table sctab = mstable_.sysCal() ;
[1974]1248  if ( sctab.nrow() == 0 ) {
1249    os_ << "No SysCal rows" << LogIO::POST ;
1250    return ;
1251  }
[1987]1252  Table sctabsel( sctab( sctab.col("ANTENNA_ID") == antenna_ ) ) ;
[1974]1253  if ( sctabsel.nrow() == 0 ) {
1254    os_ << "No SysCal rows" << LogIO::POST ;
1255    return ;
1256  }
[1987]1257  ROArrayColumn<Float> *tmpTcalCol = new ROArrayColumn<Float>( sctabsel, "TCAL" ) ;
1258  uInt npol = tmpTcalCol->shape( 0 )(0) ;
1259  delete tmpTcalCol ;
[1974]1260  //os_ << "fillTcal(): npol = " << npol << LogIO::POST ;
1261  Table tab = table_->tcal().table() ;
1262  ArrayColumn<Float> tcalCol( tab, "TCAL" ) ;
[1987]1263  ROTableColumn *sharedCol ;
[1974]1264  uInt oldnr = 0 ;
1265  uInt newnr = 0 ;
[2002]1266  TableRow row( tab ) ;
1267  TableRecord &trec = row.record() ;
1268  RecordFieldPtr<uInt> idRF( trec, "ID" ) ;
1269  RecordFieldPtr<String> timeRF( trec, "TIME" ) ;
1270  RecordFieldPtr< Array<Float> > tcalRF( trec, "TCAL" ) ;
[1974]1271  TableIterator iter0( sctabsel, "FEED_ID" ) ;
1272  while( !iter0.pastEnd() ) {
[1987]1273    Table t0 = iter0.table() ;
[1988]1274    sharedCol = tpoolr->construct( t0, "FEED_ID" ) ;
[1987]1275    Int feedId = sharedCol->asInt( 0 ) ;
[1988]1276    tpoolr->destroy( sharedCol ) ;
[1974]1277    TableIterator iter1( t0, "SPECTRAL_WINDOW_ID" ) ;
1278    while( !iter1.pastEnd() ) {
[1988]1279      Table t1 = iter1.table() ;
1280      sharedCol = tpoolr->construct( t1, "SPECTRAL_WINDOW_ID" ) ;
[1987]1281      Int spwId = sharedCol->asInt( 0 ) ;
[1988]1282      tpoolr->destroy( sharedCol ) ;
[2002]1283      tmpTcalCol = new ROArrayColumn<Float>( t1, colTcal_ ) ;
1284      ROScalarQuantColumn<Double> scTimeCol( t1, "TIME" ) ;
1285      Vector<uInt> idminmax( 2, oldnr ) ;
1286      for ( uInt irow = 0 ; irow < t1.nrow() ; irow++ ) {
1287        String sTime = MVTime( scTimeCol(irow) ).string( MVTime::YMD ) ;
1288        *timeRF = sTime ;
[1988]1289        uInt idx = oldnr ;
[2002]1290        Matrix<Float> subtcal = (*tmpTcalCol)( irow ) ;
[1988]1291        for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
[2002]1292          *idRF = idx++ ;
1293          *tcalRF = subtcal.row( ipol ) ;
1294
1295          // commit row
1296          tab.addRow() ;
1297          row.put( tab.nrow()-1 ) ;
1298
1299          newnr++ ;
[1974]1300        }
[2002]1301        idminmax[0] = oldnr ;
[1988]1302        idminmax[1] = newnr - 1 ;
1303        oldnr = newnr ;
1304
[1990]1305        String key = keyTcal( feedId, spwId, sTime ) ;
[1988]1306        tcalrec_.define( key, idminmax ) ;
[1974]1307      }
[2002]1308      delete tmpTcalCol ;
[1974]1309      iter1++ ;
1310    }
1311    iter0++ ;
1312  }
1313
1314  //tcalrec_.print( std::cout ) ;
[2004]1315//   double endSec = gettimeofday_sec() ;
1316//   os_ << "end MSFiller::fillTcal() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1317}
1318
1319uInt MSFiller::getWeatherId( uInt idx, Double wtime )
1320{
[2004]1321//   double startSec = gettimeofday_sec() ;
1322//   os_ << "start MSFiller::getWeatherId() startSec=" << startSec << LogIO::POST ;
[1974]1323  uInt nrow = mwTime_.size() ;
1324  if ( nrow == 0 )
1325    return 0 ;
1326  uInt wid = nrow ;
1327  for ( uInt i = idx ; i < nrow-1 ; i++ ) {
1328    Double tStart = mwTime_[i]-0.5*mwInterval_[i] ;
1329    // use of INTERVAL column is problematic
1330    // since there are "blank" time of weather monitoring
1331    //Double tEnd = tStart + mwInterval_[i] ;
1332    Double tEnd = mwTime_[i+1]-0.5*mwInterval_[i+1] ;
1333    //os_ << "tStart = " << tStart << " dtEnd = " << tEnd-tStart << " dwtime = " << wtime-tStart << LogIO::POST ;
1334    if ( wtime >= tStart && wtime <= tEnd ) {
1335      wid = i ;
1336      break ;
1337    }
1338  }
1339  if ( wid == nrow ) {
1340    uInt i = nrow - 1 ;
1341    Double tStart = mwTime_[i-1]+0.5*mwInterval_[i-1] ;
1342    Double tEnd = mwTime_[i]+0.5*mwInterval_[i] ;
1343    //os_ << "tStart = " << tStart << " dtEnd = " << tEnd-tStart << " dwtime = " << wtime-tStart << LogIO::POST ;
1344    if ( wtime >= tStart && wtime <= tEnd )
1345      wid = i ;
1346  }
1347
1348  //if ( wid == nrow )
1349  //os_ << LogIO::WARN << "Couldn't find correct WEATHER_ID for time " << wtime << LogIO::POST ;
1350
[2004]1351//   double endSec = gettimeofday_sec() ;
1352//   os_ << "end MSFiller::getWeatherId() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1353  return wid ;
1354}
1355
[2003]1356void MSFiller::getSysCalTime( Vector<MEpoch> &scTime, Vector<Double> &scInterval, Block<MEpoch> &tcol, Block<Double> &tstr, Block<Int> &tidx )
[1974]1357{
[2004]1358//   double startSec = gettimeofday_sec() ;
1359//   os_ << "start MSFiller::getSysCalTime() startSec=" << startSec << LogIO::POST ;
1360
1361  if ( !isSysCal_ )
1362    return ;
1363
[1993]1364  uInt nrow = tstr.nelements() ;
[2002]1365  if ( scTime.nelements() == 0 )
[1993]1366    return ;
[2002]1367  uInt scnrow = scTime.nelements() ;
[1974]1368  uInt idx = 0 ;
1369  const Double half = 0.5e0 ;
[2003]1370  // execute  binary search
1371  idx = binarySearch( scTime, tcol[0].get( "s" ).getValue() ) ;
1372  if ( idx != 0 )
1373    idx -= 1 ;
[1974]1374  for ( uInt i = 0 ; i < nrow ; i++ ) {
[2002]1375    Double t = tcol[i].get( "s" ).getValue() ;
[1974]1376    for ( uInt j = idx ; j < scnrow-1 ; j++ ) {
[2002]1377      Double tsc1 = scTime[j].get( "s" ).getValue() ;
1378      Double dt1 = scInterval[j] ;
1379      Double tsc2 = scTime[j+1].get( "s" ).getValue() ;
1380      Double dt2 = scInterval[j+1] ;
[1974]1381      if ( t > tsc1-half*dt1 && t <= tsc2-half*dt2 ) {
1382        tstr[i] = tsc1 ;
[2002]1383        tidx[i] = j ;
[1974]1384        idx = j ;
1385        break ;
1386      }
1387    }
1388    if ( tstr[i] == -1.0 ) {
[2002]1389      Double tsc = scTime[scnrow-1].get( "s" ).getValue() ;
1390      Double dt = scInterval[scnrow-1] ;
1391      if ( t <= tsc+0.5*dt ) {
[1974]1392        tstr[i] = tsc ;
[2002]1393        tidx[i] = scnrow-1 ;
1394      }
[1974]1395    }
1396  }
[2004]1397//   double endSec = gettimeofday_sec() ;
1398//   os_ << "end MSFiller::getSysCalTime() endSec=" << endSec << " (" << endSec-startSec << "sec) scnrow = " << scnrow << " tcol.nelements = " << tcol.nelements() << LogIO::POST ;
[1993]1399  return ;
[1974]1400}
1401
[2004]1402Block<uInt> MSFiller::getTcalId( Int fid, Int spwid, MEpoch &t )
1403{
[2002]1404//   double startSec = gettimeofday_sec() ;
[2004]1405//   os_ << "start MSFiller::getTcalId() startSec=" << startSec << LogIO::POST ;
1406  //if ( table_->tcal().table().nrow() == 0 ) {
1407  if ( !isSysCal_ ) {
[1988]1408    os_ << "No TCAL rows" << LogIO::POST ;
[2002]1409    Block<uInt> tcalids( 0 ) ;
[1988]1410    return  tcalids ;
1411  }   
[2004]1412  //String sctime = MVTime( Quantum<Double>(t,"s") ).string(MVTime::YMD) ;
1413  String sctime = MVTime( t.getValue() ).string(MVTime::YMD) ;
[1990]1414  String key = keyTcal( fid, spwid, sctime ) ;
[1988]1415  if ( !tcalrec_.isDefined( key ) ) {
[1974]1416    os_ << "No TCAL rows" << LogIO::POST ;
[2002]1417    Block<uInt> tcalids( 0 ) ;
[1974]1418    return tcalids ;
1419  }
[1988]1420  Vector<uInt> ids = tcalrec_.asArrayuInt( key ) ;
1421  uInt npol = ids[1] - ids[0] + 1 ;
[2002]1422  Block<uInt> tcalids( npol ) ;
[1988]1423  tcalids[0] = ids[0] ;
1424  tcalids[1] = ids[1] ;
1425  for ( uInt ipol = 2 ; ipol < npol ; ipol++ )
1426    tcalids[ipol] = ids[0] + ipol - 1 ;
1427
[2004]1428//   double endSec = gettimeofday_sec() ;
1429//   os_ << "end MSFiller::getTcalId() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1430  return tcalids ;
1431}
1432
[1987]1433uInt MSFiller::getDirection( uInt idx, Vector<Double> &dir, Vector<Double> &srate, String &ref, MSPointing &tab, Double t )
[1974]1434{
[2004]1435//   double startSec = gettimeofday_sec() ;
1436//   os_ << "start MSFiller::getDirection() startSec=" << startSec << LogIO::POST ;
[1974]1437  // assume that cols is sorted by TIME
1438  Bool doInterp = False ;
[1987]1439  //uInt nrow = cols.nrow() ;
1440  uInt nrow = tab.nrow() ;
[1974]1441  if ( nrow == 0 )
1442    return 0 ;
[1987]1443  ROScalarMeasColumn<MEpoch> tcol( tab, "TIME" ) ;
1444  ROArrayMeasColumn<MDirection> dmcol( tab, "DIRECTION" ) ;
1445  ROArrayColumn<Double> dcol( tab, "DIRECTION" ) ;
[2003]1446  // binary search if idx == 0
1447  if ( idx == 0 ) {
1448    uInt nrowb = 75000 ;
[2004]1449    if ( nrow > nrowb ) {
[2003]1450      uInt nblock = nrow / nrowb + 1 ;
1451      for ( uInt iblock = 0 ; iblock < nblock ; iblock++ ) {
[2004]1452        uInt high = min( nrowb, nrow-iblock*nrowb ) ;
[2003]1453
1454        if ( tcol( high-1 ).get( "s" ).getValue() < t ) {
[2004]1455          idx = iblock * nrowb ;
[2003]1456          continue ;
1457        }
1458
1459        Vector<MEpoch> tarr( high ) ;
1460        for ( uInt irow = 0 ; irow < high ; irow++ ) {
[2004]1461          tarr[irow] = tcol( iblock*nrowb+irow ) ;
[2003]1462        }
1463
1464        uInt bidx = binarySearch( tarr, t ) ;
1465
[2004]1466        idx = iblock * nrowb + bidx ;
[2003]1467        break ;
1468      }
1469    }
1470    else {
1471      Vector<MEpoch> tarr( nrow ) ;
1472      for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1473        tarr[irow] = tcol( irow ) ;
1474      }
1475      idx = binarySearch( tarr, t ) ;
1476    }
1477  }
[1974]1478  // ensure that tcol(idx) < t
1479  //os_ << "tcol(idx) = " << tcol(idx).get("s").getValue() << " t = " << t << " diff = " << tcol(idx).get("s").getValue()-t << endl ;
1480  while ( tcol(idx).get("s").getValue() > t && idx > 0 )
1481    idx-- ;
1482  //os_ << "idx = " << idx << LogIO::POST ;
1483
1484  // index search
1485  for ( uInt i = idx ; i < nrow ; i++ ) {
1486    Double tref = tcol( i ).get( "s" ).getValue() ;
1487    if ( tref == t ) {
1488      idx = i ;
1489      break ;
1490    }
1491    else if ( tref > t ) {
1492      if ( i == 0 ) {
1493        idx = i ;
1494      }
1495      else {
1496        idx = i-1 ;
1497        doInterp = True ;
1498      }
1499      break ;
1500    }
1501    else {
1502      idx = nrow - 1 ;
1503    }
1504  }
1505  //os_ << "searched idx = " << idx << LogIO::POST ;
1506
1507  //os_ << "dmcol(idx).shape() = " << dmcol(idx).shape() << LogIO::POST ;
1508  IPosition ip( dmcol(idx).shape().nelements(), 0 ) ;
1509  //os_ << "ip = " << ip << LogIO::POST ;
1510  ref = dmcol(idx)(ip).getRefString() ;
1511  //os_ << "ref = " << ref << LogIO::POST ;
1512  if ( doInterp ) {
1513    //os_ << "do interpolation" << LogIO::POST ;
1514    //os_ << "dcol(idx).shape() = " << dcol(idx).shape() << LogIO::POST ;
1515    Double tref0 = tcol(idx).get("s").getValue() ;
1516    Double tref1 = tcol(idx+1).get("s").getValue() ;
[1987]1517    Matrix<Double> mdir0 = dcol( idx ) ;
1518    Matrix<Double> mdir1 = dcol( idx+1 ) ;
1519    Vector<Double> dir0 = mdir0.column( 0 ) ;
[1974]1520    //os_ << "dir0 = " << dir0 << LogIO::POST ;
[1987]1521    Vector<Double> dir1 = mdir1.column( 0 ) ;
[1974]1522    //os_ << "dir1 = " << dir1 << LogIO::POST ;
1523    Double dt0 = t - tref0 ;
1524    Double dt1 = tref1 - t ;
1525    dir.reference( (dt0*dir1+dt1*dir0)/(dt0+dt1) ) ;
[1987]1526    if ( mdir0.ncolumn() > 1 ) {
1527      if ( dt0 >= dt1 )
1528        srate.reference( mdir0.column( 1 ) ) ;
1529      else
1530        srate.reference( mdir1.column( 1 ) ) ;
[1974]1531    }
1532    //os_ << "dir = " << dir << LogIO::POST ;
1533  }
1534  else {
1535    //os_ << "no interpolation" << LogIO::POST ;
[1987]1536    Matrix<Double> mdir0 = dcol( idx ) ;
1537    dir.reference( mdir0.column( 0 ) ) ;
1538    if ( mdir0.ncolumn() > 1 )
1539      srate.reference( mdir0.column( 1 ) ) ;
[1974]1540  }
1541
[2004]1542//   double endSec = gettimeofday_sec() ;
1543//   os_ << "end MSFiller::getDirection() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1544  return idx ;
1545}
1546
[1990]1547String MSFiller::keyTcal( Int feedid, Int spwid, String stime )
1548{
1549  String sfeed = "FEED" + String::toString( feedid ) ;
1550  String sspw = "SPW" + String::toString( spwid ) ;
1551  return sfeed+":"+sspw+":"+stime ;
1552}
1553
[2003]1554uInt MSFiller::binarySearch( Vector<MEpoch> &timeList, Double target )
1555{
1556  Int low = 0 ;
1557  Int high = timeList.nelements() ;
1558  uInt idx = 0 ;
1559
1560  while ( low <= high ) {
1561    idx = (Int)( 0.5 * ( low + high ) ) ;
1562    Double t = timeList[idx].get( "s" ).getValue() ;
1563    if ( t < target )
1564      low = idx + 1 ;
1565    else if ( t > target )
1566      high = idx - 1 ;
1567    else
1568      return idx ;
1569  }
1570
1571  idx = max( 0, min( low, high ) ) ;
1572
1573  return idx ;
1574 
1575}
1576
[1974]1577} ;
1578
Note: See TracBrowser for help on using the repository browser.