source: trunk/src/MSFiller.cpp @ 2019

Last change on this file since 2019 was 2019, 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...

Bug fix

1) supporting variable shaped array input to some column (SPECTRAL DATA, TSYS, and TCAL)

2) put [1.0] instead of empty array as default TCAL in NROFiller

3) Check if some subtables have any effective rows in MSWriter


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