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
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/RefRows.h>
23#include <tables/Tables/TableParse.h>
24#include <tables/Tables/RefRows.h>
25#include <tables/Tables/TableRow.h>
26
27#include <casa/Containers/RecordField.h>
28#include <casa/Logging/LogIO.h>
29#include <casa/Arrays/Slicer.h>
30#include <casa/Quanta/MVTime.h>
31#include <casa/OS/Path.h>
32
33#include <measures/Measures/Stokes.h>
34#include <measures/Measures/MEpoch.h>
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>
43#include <measures/TableMeasures/ScalarMeasColumn.h>
44#include <measures/TableMeasures/ArrayMeasColumn.h>
45#include <measures/TableMeasures/ScalarQuantColumn.h>
46#include <measures/TableMeasures/ArrayQuantColumn.h>
47
48#include <atnf/PKSIO/SrcType.h>
49
50#include "MSFiller.h"
51#include "STHeader.h"
52
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
63using namespace casa ;
64using namespace std ;
65
66namespace asap {
67MSFiller::MSFiller( casa::CountedPtr<Scantable> stable )
68  : table_( stable ),
69    tablename_( "" ),
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 ),
80    isWeather_( False ),
81    colTsys_( "TSYS_SPECTRUM" ),
82    colTcal_( "TCAL_SPECTRUM" )
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 ) ) ;
96//   double startSec = gettimeofday_sec() ;
97//   os_ << "start MSFiller::open() startsec=" << startSec << LogIO::POST ;
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
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 ;
134
135  // check which data column exists
136  isFloatData_ = mstable_.tableDesc().isColumn( "FLOAT_DATA" ) ;
137  isData_ = mstable_.tableDesc().isColumn( "DATA" ) ;
138
139//   double endSec = gettimeofday_sec() ;
140//   os_ << "end MSFiller::open() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
141  return true ;
142}
143
144void MSFiller::fill()
145{
146  os_.origin( LogOrigin( "MSFiller", "fill()", WHERE ) ) ;
147//   double startSec = gettimeofday_sec() ;
148//   os_ << "start MSFiller::fill() startSec=" << startSec << LogIO::POST ;
149
150//   double time0 = gettimeofday_sec() ;
151//   os_ << "start init fill: " << time0 << LogIO::POST ;
152
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
174  //const TableRecord msrec = tablesel_.keywordSet() ;
175  const TableRecord msrec = mstable_.keywordSet() ;
176  isDoppler_ = msrec.isDefined( "DOPPLER" ) ;
177  if ( isDoppler_ )
178    if ( mstable_.doppler().nrow() == 0 )
179      isDoppler_ = False ;
180  isFlagCmd_ = msrec.isDefined( "FLAG_CMD" ) ;
181  if ( isFlagCmd_ )
182    if ( mstable_.flagCmd().nrow() == 0 )
183      isFlagCmd_ = False ;
184  isFreqOffset_ = msrec.isDefined( "FREQ_OFFSET" ) ;
185  if ( isFreqOffset_ )
186    if ( mstable_.freqOffset().nrow() == 0 )
187      isFreqOffset_ = False ;
188  isHistory_ = msrec.isDefined( "HISTORY" ) ;
189  if ( isHistory_ )
190    if ( mstable_.history().nrow() == 0 )
191      isHistory_ = False ;
192  isProcessor_ = msrec.isDefined( "PROCESSOR" ) ;
193  if ( isProcessor_ )
194    if ( mstable_.processor().nrow() == 0 )
195      isProcessor_ = False ;
196  isSysCal_ = msrec.isDefined( "SYSCAL" ) ;
197  if ( isSysCal_ )
198    if ( mstable_.sysCal().nrow() == 0 )
199      isSysCal_ = False ;
200  isWeather_ = msrec.isDefined( "WEATHER" ) ;
201  if ( isWeather_ )
202    if ( mstable_.weather().nrow() == 0 )
203      isWeather_ = False ;
204
205  // Access to MS subtables
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() ;
213  if ( caltab.nrow() == 0 )
214    isSysCal_ = False ;
215  else {
216    if ( !caltab.tableDesc().isColumn( colTcal_ ) )
217      colTcal_ = "TCAL" ;
218    if ( !caltab.tableDesc().isColumn( colTsys_ ) )
219      colTsys_ = "TSYS" ;
220  }
221//   colTcal_ = "TCAL" ;
222//   colTsys_ = "TSYS" ;
223  MSPointing pointtab = mstable_.pointing() ;
224  if ( mstable_.weather().nrow() == 0 )
225    isWeather_ = False ;
226  MSState stattab = mstable_.state() ;
227  MSAntenna anttab = mstable_.antenna() ;
228
229  // TEST
230  // memory allocation by boost::object_pool
231  boost::object_pool<ROTableColumn> *tpoolr = new boost::object_pool<ROTableColumn> ;
232  //
233
234  // SUBTABLES: FREQUENCIES
235  table_->frequencies().setFrame( "LSRK" ) ;
236  table_->frequencies().setFrame( "LSRK", True ) ;
237
238  // SUBTABLES: WEATHER
239  fillWeather() ;
240
241  // SUBTABLES: FOCUS
242  fillFocus() ;
243
244  // SUBTABLES: TCAL
245  fillTcal( tpoolr ) ;
246
247  // SUBTABLES: FIT
248  //fillFit() ;
249
250  // SUBTABLES: HISTORY
251  //fillHistory() ;
252
253  // shared pointers
254  ROTableColumn *tcolr ;
255
256  // MAIN
257  // Iterate over several ids
258  map<Int, uInt> ifmap ; // (IFNO, FREQ_ID) pair
259  ROArrayQuantColumn<Double> *sharedQDArrCol = new ROArrayQuantColumn<Double>( anttab, "POSITION" ) ;
260  Vector< Quantum<Double> > antpos = (*sharedQDArrCol)( antenna_ ) ;
261  delete sharedQDArrCol ;
262  MPosition mp( MVPosition( antpos ), MPosition::ITRF ) ;
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  }
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
278  //double time1 = gettimeofday_sec() ;
279  //os_ << "end fill init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
280
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
313  //
314  // ITERATION: OBSERVATION_ID
315  //
316  TableIterator iter0( mstable_, "OBSERVATION_ID" ) ;
317  while( !iter0.pastEnd() ) {
318    //time0 = gettimeofday_sec() ;
319    //os_ << "start 0th iteration: " << time0 << LogIO::POST ;
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 ;
337    if ( sdh.utc == 0.0 ) {
338      sdh.utc = me.get( "s" ).getValue() ;
339    }
340    if ( telescopeName == "" ) {
341      tcolr = tpoolr->construct( obstab, "TELESCOPE_NAME" ) ;
342      sdh.observer = tcolr->asString( obsId ) ;
343      tpoolr->destroy( tcolr ) ;
344    }
345    Int nbeam = 0 ;
346    //time1 = gettimeofday_sec() ;
347    //os_ << "end 0th iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
348    //
349    // ITERATION: FEED1
350    //
351    TableIterator iter1( t0, "FEED1" ) ;
352    while( !iter1.pastEnd() ) {
353      //time0 = gettimeofday_sec() ;
354      //os_ << "start 1st iteration: " << time0 << LogIO::POST ;
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 ) ;
360      nbeam++ ;
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
370      //time1 = gettimeofday_sec() ;
371      //os_ << "end 1st iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
372      //
373      // ITERATION: FIELD_ID
374      //
375      TableIterator iter2( t1, "FIELD_ID" ) ;
376      while( !iter2.pastEnd() ) {
377        //time0 = gettimeofday_sec() ;
378        //os_ << "start 2nd iteration: " << time0 << LogIO::POST ;
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 ) ;
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         
397
398        // FIELDNAME
399        RecordFieldPtr<String> strRF( trec, "FIELDNAME" ) ;
400        *strRF = fieldName ;
401
402
403        //time1 = gettimeofday_sec() ;
404        //os_ << "end 2nd iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
405        //
406        // ITERATION: DATA_DESC_ID
407        //
408        TableIterator iter3( t2, "DATA_DESC_ID" ) ;
409        while( !iter3.pastEnd() ) {
410          //time0 = gettimeofday_sec() ;
411          //os_ << "start 3rd iteration: " << time0 << LogIO::POST ;
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 ) ;
422          // TODO: This must be updated if writer will be replaced from STWriter to MSWriter
423          sdh.nif = max( sdh.nif, spwId ) ;
424
425          // IFNO
426          uintRF.attachToRecord( trec, "IFNO" ) ;
427          *uintRF = (uInt)spwId ;
428
429          // polarization information
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 ;
436          //os_ << "npol = " << npol << LogIO::POST ;
437          //os_ << "corrtype = " << corrtype << LogIO::POST ;
438          sdh.npol = max( sdh.npol, npol ) ;
439          if ( sdh.poltype == "" ) sdh.poltype = getPolType( corrtype[0] ) ;
440          // source information
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          }
446          tcolr = tpoolr->construct( srctabSel, "NAME" ) ;
447          String srcName = tcolr->asString( 0 ) ;
448          tpoolr->destroy( tcolr ) ;
449
450          // SRCNAME
451          strRF.attachToRecord( trec, "SRCNAME" ) ;
452          *strRF = srcName ;
453
454          //os_ << "srcName = " << srcName << LogIO::POST ;
455          ROArrayColumn<Double> *roArrDCol = new ROArrayColumn<Double>( srctabSel, "PROPER_MOTION" ) ;
456          Array<Double> srcPM = (*roArrDCol)( 0 ) ;
457          delete roArrDCol ;
458
459          // SRCPROPERMOTION
460          RecordFieldPtr< Array<Double> > darrRF( trec, "SRCPROPERMOTION" ) ;
461          *darrRF = srcPM ;
462
463          //os_ << "srcPM = " << srcPM << LogIO::POST ;
464          roArrDCol = new ROArrayColumn<Double>( srctabSel, "DIRECTION" ) ;
465          Array<Double> srcDir = (*roArrDCol)( 0 ) ;
466          delete roArrDCol ;
467
468          // SRCDIRECTION
469          darrRF.attachToRecord( trec, "SRCDIRECTION" ) ;
470          *darrRF = srcDir ;
471
472          //os_ << "srcDir = " << srcDir << LogIO::POST ;
473          Array<Double> sysVels ;
474          Double sysVel = 0.0 ;
475          if ( srctabSel.tableDesc().isColumn( "SYSVEL" ) ) {
476            roArrDCol = new ROArrayColumn<Double>( srctabSel, "SYSVEL" ) ;
477            sysVels = (*roArrDCol)( 0 ) ;
478            delete roArrDCol ;
479          }
480          if ( !sysVels.empty() ) {
481            //os_ << "sysVels.shape() = " << sysVels.shape() << LogIO::POST ;
482            // NB: assume all SYSVEL values are the same
483            sysVel = sysVels( IPosition(1,0) ) ;
484          }
485
486          // SRCVELOCITY
487          RecordFieldPtr<Double> doubleRF( trec, "SRCVELOCITY" ) ;
488          *doubleRF = sysVel ;
489
490          //delete tmpArrCol ;
491          //os_ << "sysVel = " << sysVel << LogIO::POST ;
492          ROScalarMeasColumn<MDirection> *tmpMeasCol = new ROScalarMeasColumn<MDirection>( srctabSel, "DIRECTION" ) ;
493          MDirection md = (*tmpMeasCol)( 0 ) ;
494          delete tmpMeasCol ;
495          // for MOLECULES subtable
496          tcolr = tpoolr->construct( srctabSel, "NUM_LINES" ) ;
497          Int numLines = tcolr->asInt( 0 ) ;
498          tpoolr->destroy( tcolr ) ;
499          //os_ << "numLines = " << numLines << LogIO::POST ;
500          Vector<Double> restFreqs( numLines, 0.0 ) ;
501          Vector<String> transitionName( numLines, "" ) ;
502          if ( numLines != 0 ) {
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              }
510            }
511            //os_ << "restFreqs = " << restFreqs << LogIO::POST ;
512            if ( srctabSel.tableDesc().isColumn( "TRANSITION" ) ) {
513              ROArrayColumn<String> transitionCol( srctabSel, "TRANSITION" ) ;
514              if ( transitionCol.isDefined( 0 ) )
515                transitionName = transitionCol( 0 ) ;
516              //os_ << "transitionNameCol.nrow() = " << transitionCol.nrow() << LogIO::POST ;
517            }
518          }
519          uInt molId = table_->molecules().addEntry( restFreqs, transitionName, transitionName ) ;
520
521          // MOLECULE_ID
522          uintRF.attachToRecord( trec, "MOLECULE_ID" ) ;
523          *uintRF = molId ;
524
525          // spectral setup
526          MeasFrame mf( me, mp, md ) ;
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 ) ;
533          Bool even = False ;
534          if ( (nchan/2)*2 == nchan ) even = True ;
535          sdh.nchan = max( sdh.nchan, nchan ) ;
536          ROScalarQuantColumn<Double> *tmpQuantCol = new ROScalarQuantColumn<Double>( spwtab, "TOTAL_BANDWIDTH" ) ;
537          Double totbw = (*tmpQuantCol)( spwId ).getValue( "Hz" ) ;
538          delete tmpQuantCol ;
539          sdh.bandwidth = max( sdh.bandwidth, totbw ) ;
540          if ( sdh.freqref == "" )
541            //sdh.freqref = MFrequency::showType( freqRef ) ;
542            sdh.freqref = "LSRK" ;
543          if ( sdh.reffreq == -1.0 ) {
544            tmpQuantCol = new ROScalarQuantColumn<Double>( spwtab, "REF_FREQUENCY" ) ;
545            Quantum<Double> qreffreq = (*tmpQuantCol)( spwId ) ;
546            delete tmpQuantCol ;
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 ;
559          sharedQDArrCol = new ROArrayQuantColumn<Double>( spwtab, "CHAN_WIDTH" ) ;
560          Double increment = (*sharedQDArrCol)( spwId )( refip ).getValue( "Hz" ) ;
561          delete sharedQDArrCol ;
562          //os_ << "nchan = " << nchan << " refchan = " << refchan << "(even=" << even << ") refpix = " << refpix << LogIO::POST ;
563          sharedQDArrCol = new ROArrayQuantColumn<Double>( spwtab, "CHAN_FREQ" ) ;
564          Vector< Quantum<Double> > chanFreqs = (*sharedQDArrCol)( spwId ) ;
565          delete sharedQDArrCol ;
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          }
595
596          // FREQ_ID
597          uintRF.attachToRecord( trec, "FREQ_ID" ) ;
598          *uintRF = freqId ;
599
600          // for TSYS and TCAL
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 ;
618          //
619          // ITERATION: SCAN_NUMBER
620          //
621          TableIterator iter4( t3, "SCAN_NUMBER" ) ;
622          while( !iter4.pastEnd() ) {
623            //time0 = gettimeofday_sec() ;
624            //os_ << "start 4th iteration: " << time0 << LogIO::POST ;
625            Table t4 = iter4.table() ;
626            tcolr = tpoolr->construct( t4, "SCAN_NUMBER" ) ;
627            Int scanNum = tcolr->asInt( 0 ) ;
628            tpoolr->destroy( tcolr ) ;
629
630            // SCANNO
631            uintRF.attachToRecord( trec, "SCANNO" ) ;
632            *uintRF = scanNum - 1 ;
633
634            //time1 = gettimeofday_sec() ;
635            //os_ << "end 4th iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
636            //
637            // ITERATION: STATE_ID
638            //
639            TableIterator iter5( t4, "STATE_ID" ) ;
640            while( !iter5.pastEnd() ) {
641              //time0 = gettimeofday_sec() ;
642              //os_ << "start 5th iteration: " << time0 << LogIO::POST ;
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 ) ;
650              if ( sdh.obstype == "" ) sdh.obstype = obstype ;
651
652              Int nrow = t5.nrow() ;
653              //time1 = gettimeofday_sec() ;
654              //os_ << "end 5th iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
655
656              uInt cycle = 0 ;
657
658              Cube<Float> spArr ;
659              Cube<Bool> flArr ;
660              if ( isFloatData_ ) {
661                ROArrayColumn<Bool> mFlagCol( t5, "FLAG" ) ;
662                ROArrayColumn<Float> mFloatDataCol( t5, "FLOAT_DATA" ) ;
663                spArr = mFloatDataCol.getColumn() ;
664                flArr = mFlagCol.getColumn() ;
665                if ( sdh.fluxunit == "" ) {
666                  const TableRecord &dataColKeys = mFloatDataCol.keywordSet() ;
667                  if ( dataColKeys.isDefined( "UNIT" ) )
668                    sdh.fluxunit = dataColKeys.asString( "UNIT" ) ;
669                }
670              }
671              else if ( isData_ ) {
672                spArr.resize( npol, nchan, nrow ) ;
673                flArr.resize( npol, nchan, nrow ) ;
674                ROArrayColumn<Bool> mFlagCol( t5, "FLAG" ) ;
675                ROArrayColumn<Complex> mDataCol( t5, "DATA" ) ;
676                for ( Int irow = 0 ; irow < nrow ; irow++ ) {
677                  Bool crossOK = False ;
678                  Matrix<Complex> sp = mDataCol( irow ) ;
679                  Matrix<Bool> fl = mFlagCol( irow ) ;
680                  Matrix<Float> spxy = spArr.xyPlane( irow ) ;
681                  Matrix<Bool> flxy = flArr.xyPlane( irow ) ;
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 ) {
686                        spxy.row( ipol ) = real( sp.row( ipol ) ) ;
687                        flxy.row( ipol ) = fl.row( ipol ) ;
688                        if ( corrtype[ipol] == Stokes::XY || corrtype[ipol] == Stokes::RL ) {
689                          spxy.row( ipol+1 ) = imag( sp.row( ipol ) ) ;
690                          flxy.row( ipol+1 ) = fl.row( ipol ) ;
691                        }                       
692                        else {
693                          spxy.row( ipol+1 ) = imag( conj( sp.row( ipol ) ) ) ;
694                          flxy.row( ipol+1 ) = fl.row( ipol ) ;
695                        }
696                        crossOK = True ;
697                      }
698                    }
699                    else {
700                      spxy.row( ipol ) = real( sp.row( ipol ) ) ;
701                      flxy.row( ipol ) = fl.row( ipol ) ;
702                    }
703                  }
704                }
705                if ( sdh.fluxunit == "" ) {
706                  const TableRecord &dataColKeys = mDataCol.keywordSet() ;
707                  if ( dataColKeys.isDefined( "UNIT" ) )
708                    sdh.fluxunit = dataColKeys.asString( "UNIT" ) ;
709                }
710              }
711              ROScalarMeasColumn<MEpoch> *mTimeCol = new ROScalarMeasColumn<MEpoch>( t5, "TIME" ) ;
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 ) ;
721              }
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 ;
741                  }
742                }
743                else {
744                  polnos[pidx] = p[0] ;
745                  pidx++ ;
746                }
747              }
748             
749              // SRCTYPE
750              intRF.attachToRecord( trec, "SRCTYPE" ) ;
751              *intRF = srcType ;
752
753              for ( Int irow = 0 ; irow < nrow ; irow++ ) {
754                // CYCLENO
755                *cycleRF = cycle ;
756
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
767                *timeRF = mTimeB[irow].get("d").getValue() ;
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
780                Block<uInt> tcalids( npol, 0 ) ;
781                if ( sysCalIdx[irow] != -1 ) {
782                  tcalids = getTcalId( feedId, spwId, scTime[sysCalIdx[irow]] ) ;
783                }
784
785                // WEATHER_ID
786                if ( isWeather_ )
787                  wid = getWeatherId( wid, mTimeB[irow].get("s").getValue() ) ;
788                *widRF = wid ;
789                 
790
791                // DIRECTION, AZEL, SCANRATE
792                if ( getPt_ ) {
793                  Vector<Double> dir ;
794                  Vector<Double> scanrate ;
795                  String refString ;
796                  diridx = getDirection( diridx, dir, scanrate, refString, pointtab, mTimeB[irow].get("s").getValue() ) ;
797                  MDirection::getType( dirType, refString ) ;
798                  mf.resetEpoch( mTimeB[irow] ) ;
799                  mf.resetDirection( MDirection( MVDirection(dir), dirType ) ) ;
800                  if ( refString == "J2000" ) {
801                    *dirRF = dir ;
802                    MDirection::Convert toazel( dirType, MDirection::Ref( MDirection::AZEL, mf ) ) ;
803                    Vector<Double> azel = toazel( dir ).getAngle("rad").getValue() ;
804                    *azRF = (Float)azel(0) ;
805                    *elRF = (Float)azel(1) ;
806                  }
807                  else if ( refString(0,4) == "AZEL" ) {
808                    *azRF = (Float)dir(0) ;
809                    *elRF = (Float)dir(1) ;
810                    MDirection::Convert toj2000( dirType, MDirection::Ref( MDirection::J2000, mf ) ) ;
811                    Vector<Double> newdir = toj2000( dir ).getAngle("rad").getValue() ;
812                    *dirRF = newdir ;
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() ;
819                    *dirRF = newdir ;
820                    *azRF = (Float)azel(0) ;
821                    *elRF = (Float)azel(1) ;
822                  }
823                  if ( scanrate.size() != 0 ) {
824                    *scrRF = scanrate ;
825                  }
826                  else {
827                    *scrRF = defaultScanrate ;
828                  }
829                }
830                else {
831                  String ref = md.getRefString() ;
832                  //Vector<Double> defaultDir = srcDir ;
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() ;
839                  }
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 ;
847                }
848
849                // Polarization dependent things
850                for ( Int ipol = 0 ; ipol < npol ; ipol++ ) {
851                  // POLNO
852                  *polnoRF = polnos[ipol] ;
853
854                  *spRF = sp.row( ipol ) ;
855                  *ucarrRF = fl.row( ipol ) ;
856                  *tsysRF = 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
1295          // commit row
1296          tab.addRow() ;
1297          row.put( tab.nrow()-1 ) ;
1298
1299          newnr++ ;
1300        }
1301        idminmax[0] = oldnr ;
1302        idminmax[1] = newnr - 1 ;
1303        oldnr = newnr ;
1304
1305        String key = keyTcal( feedId, spwId, sTime ) ;
1306        tcalrec_.define( key, idminmax ) ;
1307      }
1308      delete tmpTcalCol ;
1309      iter1++ ;
1310    }
1311    iter0++ ;
1312  }
1313
1314  //tcalrec_.print( std::cout ) ;
1315//   double endSec = gettimeofday_sec() ;
1316//   os_ << "end MSFiller::fillTcal() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1317}
1318
1319uInt MSFiller::getWeatherId( uInt idx, Double wtime )
1320{
1321//   double startSec = gettimeofday_sec() ;
1322//   os_ << "start MSFiller::getWeatherId() startSec=" << startSec << LogIO::POST ;
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
1351//   double endSec = gettimeofday_sec() ;
1352//   os_ << "end MSFiller::getWeatherId() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1353  return wid ;
1354}
1355
1356void MSFiller::getSysCalTime( Vector<MEpoch> &scTime, Vector<Double> &scInterval, Block<MEpoch> &tcol, Block<Double> &tstr, Block<Int> &tidx )
1357{
1358//   double startSec = gettimeofday_sec() ;
1359//   os_ << "start MSFiller::getSysCalTime() startSec=" << startSec << LogIO::POST ;
1360
1361  if ( !isSysCal_ )
1362    return ;
1363
1364  uInt nrow = tstr.nelements() ;
1365  if ( scTime.nelements() == 0 )
1366    return ;
1367  uInt scnrow = scTime.nelements() ;
1368  uInt idx = 0 ;
1369  const Double half = 0.5e0 ;
1370  // execute  binary search
1371  idx = binarySearch( scTime, tcol[0].get( "s" ).getValue() ) ;
1372  if ( idx != 0 )
1373    idx -= 1 ;
1374  for ( uInt i = 0 ; i < nrow ; i++ ) {
1375    Double t = tcol[i].get( "s" ).getValue() ;
1376    for ( uInt j = idx ; j < scnrow-1 ; j++ ) {
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] ;
1381      if ( t > tsc1-half*dt1 && t <= tsc2-half*dt2 ) {
1382        tstr[i] = tsc1 ;
1383        tidx[i] = j ;
1384        idx = j ;
1385        break ;
1386      }
1387    }
1388    if ( tstr[i] == -1.0 ) {
1389      Double tsc = scTime[scnrow-1].get( "s" ).getValue() ;
1390      Double dt = scInterval[scnrow-1] ;
1391      if ( t <= tsc+0.5*dt ) {
1392        tstr[i] = tsc ;
1393        tidx[i] = scnrow-1 ;
1394      }
1395    }
1396  }
1397//   double endSec = gettimeofday_sec() ;
1398//   os_ << "end MSFiller::getSysCalTime() endSec=" << endSec << " (" << endSec-startSec << "sec) scnrow = " << scnrow << " tcol.nelements = " << tcol.nelements() << LogIO::POST ;
1399  return ;
1400}
1401
1402Block<uInt> MSFiller::getTcalId( Int fid, Int spwid, MEpoch &t )
1403{
1404//   double startSec = gettimeofday_sec() ;
1405//   os_ << "start MSFiller::getTcalId() startSec=" << startSec << LogIO::POST ;
1406  //if ( table_->tcal().table().nrow() == 0 ) {
1407  if ( !isSysCal_ ) {
1408    os_ << "No TCAL rows" << LogIO::POST ;
1409    Block<uInt> tcalids( 0 ) ;
1410    return  tcalids ;
1411  }   
1412  //String sctime = MVTime( Quantum<Double>(t,"s") ).string(MVTime::YMD) ;
1413  String sctime = MVTime( t.getValue() ).string(MVTime::YMD) ;
1414  String key = keyTcal( fid, spwid, sctime ) ;
1415  if ( !tcalrec_.isDefined( key ) ) {
1416    os_ << "No TCAL rows" << LogIO::POST ;
1417    Block<uInt> tcalids( 0 ) ;
1418    return tcalids ;
1419  }
1420  Vector<uInt> ids = tcalrec_.asArrayuInt( key ) ;
1421  uInt npol = ids[1] - ids[0] + 1 ;
1422  Block<uInt> tcalids( npol ) ;
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
1428//   double endSec = gettimeofday_sec() ;
1429//   os_ << "end MSFiller::getTcalId() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1430  return tcalids ;
1431}
1432
1433uInt MSFiller::getDirection( uInt idx, Vector<Double> &dir, Vector<Double> &srate, String &ref, MSPointing &tab, Double t )
1434{
1435//   double startSec = gettimeofday_sec() ;
1436//   os_ << "start MSFiller::getDirection() startSec=" << startSec << LogIO::POST ;
1437  // assume that cols is sorted by TIME
1438  Bool doInterp = False ;
1439  //uInt nrow = cols.nrow() ;
1440  uInt nrow = tab.nrow() ;
1441  if ( nrow == 0 )
1442    return 0 ;
1443  ROScalarMeasColumn<MEpoch> tcol( tab, "TIME" ) ;
1444  ROArrayMeasColumn<MDirection> dmcol( tab, "DIRECTION" ) ;
1445  ROArrayColumn<Double> dcol( tab, "DIRECTION" ) ;
1446  // binary search if idx == 0
1447  if ( idx == 0 ) {
1448    uInt nrowb = 75000 ;
1449    if ( nrow > nrowb ) {
1450      uInt nblock = nrow / nrowb + 1 ;
1451      for ( uInt iblock = 0 ; iblock < nblock ; iblock++ ) {
1452        uInt high = min( nrowb, nrow-iblock*nrowb ) ;
1453
1454        if ( tcol( high-1 ).get( "s" ).getValue() < t ) {
1455          idx = iblock * nrowb ;
1456          continue ;
1457        }
1458
1459        Vector<MEpoch> tarr( high ) ;
1460        for ( uInt irow = 0 ; irow < high ; irow++ ) {
1461          tarr[irow] = tcol( iblock*nrowb+irow ) ;
1462        }
1463
1464        uInt bidx = binarySearch( tarr, t ) ;
1465
1466        idx = iblock * nrowb + bidx ;
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  }
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() ;
1517    Matrix<Double> mdir0 = dcol( idx ) ;
1518    Matrix<Double> mdir1 = dcol( idx+1 ) ;
1519    Vector<Double> dir0 = mdir0.column( 0 ) ;
1520    //os_ << "dir0 = " << dir0 << LogIO::POST ;
1521    Vector<Double> dir1 = mdir1.column( 0 ) ;
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) ) ;
1526    if ( mdir0.ncolumn() > 1 ) {
1527      if ( dt0 >= dt1 )
1528        srate.reference( mdir0.column( 1 ) ) ;
1529      else
1530        srate.reference( mdir1.column( 1 ) ) ;
1531    }
1532    //os_ << "dir = " << dir << LogIO::POST ;
1533  }
1534  else {
1535    //os_ << "no interpolation" << LogIO::POST ;
1536    Matrix<Double> mdir0 = dcol( idx ) ;
1537    dir.reference( mdir0.column( 0 ) ) ;
1538    if ( mdir0.ncolumn() > 1 )
1539      srate.reference( mdir0.column( 1 ) ) ;
1540  }
1541
1542//   double endSec = gettimeofday_sec() ;
1543//   os_ << "end MSFiller::getDirection() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1544  return idx ;
1545}
1546
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
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
1577} ;
1578
Note: See TracBrowser for help on using the repository browser.