source: trunk/src/MSFiller.cpp @ 2228

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

New Development: No

JIRA Issue: No

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...

Simplified a way to fill FOCUS row.


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