source: trunk/src/MSWriter.cpp @ 2019

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

New Development: No

JIRA Issue: Yes CAS-2718

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No?

Module(s): Module Names change impacts.

Description: Describe your changes here...

Bug fix

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

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

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


File size: 60.3 KB
Line 
1//
2// C++ Interface: MSWriter
3//
4// Description:
5//
6// This class is specific writer 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 <casa/OS/File.h>
15#include <casa/OS/RegularFile.h>
16#include <casa/OS/Directory.h>
17#include <casa/OS/SymLink.h>
18#include <casa/BasicSL/String.h>
19#include <casa/Containers/RecordField.h>
20#include <casa/Arrays/Cube.h>
21
22#include <tables/Tables/ExprNode.h>
23#include <tables/Tables/TableDesc.h>
24#include <tables/Tables/SetupNewTab.h>
25#include <tables/Tables/TableIter.h>
26#include <tables/Tables/RefRows.h>
27#include <tables/Tables/TableRow.h>
28
29#include <ms/MeasurementSets/MeasurementSet.h>
30#include <ms/MeasurementSets/MSColumns.h>
31#include <ms/MeasurementSets/MSPolIndex.h>
32#include <ms/MeasurementSets/MSDataDescIndex.h>
33#include <ms/MeasurementSets/MSSourceIndex.h>
34
35#include "MSWriter.h"
36#include "STHeader.h"
37#include "STFrequencies.h"
38#include "STMolecules.h"
39#include "STTcal.h"
40
41#include <ctime>
42#include <sys/time.h>
43
44// Boost
45#include <boost/pool/object_pool.hpp>
46
47using namespace casa ;
48using namespace std ;
49
50namespace asap {
51double MSWriter::gettimeofday_sec()
52{
53  struct timeval tv ;
54  gettimeofday( &tv, NULL ) ;
55  return tv.tv_sec + (double)tv.tv_usec*1.0e-6 ;
56}
57
58MSWriter::MSWriter(CountedPtr<Scantable> stable)
59  : table_(stable),
60    isTcal_(False),
61    isWeather_(False),
62    tcalSpec_(False),
63    tsysSpec_(False),
64    ptTabName_("")
65{
66  os_ = LogIO() ;
67  os_.origin( LogOrigin( "MSWriter", "MSWriter()", WHERE ) ) ;
68  os_ << "MSWriter::MSWriter()" << LogIO::POST ;
69
70  // initialize writer
71  init() ;
72}
73
74MSWriter::~MSWriter()
75{
76  os_.origin( LogOrigin( "MSWriter", "~MSWriter()", WHERE ) ) ;
77  os_ << "MSWriter::~MSWriter()" << LogIO::POST ;
78
79  if ( mstable_ != 0 )
80    delete mstable_ ;
81}
82 
83bool MSWriter::write(const string& filename, const Record& rec)
84{
85  os_.origin( LogOrigin( "MSWriter", "write()", WHERE ) ) ;
86  double startSec = gettimeofday_sec() ;
87  os_ << "start MSWriter::write() startSec=" << startSec << LogIO::POST ;
88
89  filename_ = filename ;
90
91  // parsing MS options
92  Bool overwrite = False ;
93  if ( rec.isDefined( "ms" ) ) {
94    Record msrec = rec.asRecord( "ms" ) ;
95    if ( msrec.isDefined( "overwrite" ) ) {
96      overwrite = msrec.asBool( "overwrite" ) ;
97    }
98  }
99
100  os_ << "Parsing MS options" << endl ;
101  os_ << "   overwrite = " << overwrite << LogIO::POST ;
102
103  File file( filename_ ) ;
104  if ( file.exists() ) {
105    if ( overwrite ) {
106      os_ << filename_ << " exists. Overwrite existing data... " << LogIO::POST ;
107      if ( file.isRegular() ) RegularFile(file).remove() ;
108      else if ( file.isDirectory() ) Directory(file).removeRecursive() ;
109      else SymLink(file).remove() ;
110    }
111    else {
112      os_ << LogIO::SEVERE << "ERROR: " << filename_ << " exists..." << LogIO::POST ;
113      return False ;
114    }
115  }
116
117  // TEST
118  // memory allocation by boost::object_pool
119//   boost::object_pool<ROTableColumn> *tpoolr = new boost::object_pool<ROTableColumn> ;
120//   ROTableColumn *tcol = 0 ;
121  //
122
123  // set up MS
124  setupMS() ;
125 
126  // subtables
127  // OBSERVATION
128  fillObservation() ;
129
130  // ANTENNA
131  fillAntenna() ;
132
133  // PROCESSOR
134  fillProcessor() ;
135
136  // SOURCE
137  fillSource() ;
138
139  // WEATHER
140  if ( isWeather_ )
141    fillWeather() ;
142
143  // MAIN
144  // Iterate over several ids
145  Vector<uInt> processedFreqId( 0 ) ;
146  Int defaultFieldId = 0 ;
147
148  // row based
149  TableRow row( *mstable_ ) ;
150  TableRecord &trec = row.record() ;
151  NoticeTarget *dataRF = 0 ;
152  if ( useFloatData_ )
153    dataRF = new RecordFieldPtr< Array<Float> >( trec, "FLOAT_DATA" ) ;
154  else if ( useData_ )
155    dataRF = new RecordFieldPtr< Array<Complex> >( trec, "DATA" ) ;
156  RecordFieldPtr< Array<Bool> > flagRF( trec, "FLAG" ) ;
157  RecordFieldPtr<Bool> flagrowRF( trec, "FLAG_ROW" ) ;
158  RecordFieldPtr<Double> timeRF( trec, "TIME" ) ;
159  RecordFieldPtr<Double> timecRF( trec, "TIME_CENTROID" ) ;
160  RecordFieldPtr<Double> intervalRF( trec, "INTERVAL" ) ;
161  RecordFieldPtr<Double> exposureRF( trec, "EXPOSURE" ) ;
162  RecordFieldPtr< Array<Float> > weightRF( trec, "WEIGHT" ) ;
163  RecordFieldPtr< Array<Float> > sigmaRF( trec, "SIGMA" ) ;
164  RecordFieldPtr<Int> ddidRF( trec, "DATA_DESC_ID" ) ;
165  RecordFieldPtr<Int> stateidRF( trec, "STATE_ID" ) ;
166
167
168  // OBSERVATION_ID is always 0
169  RecordFieldPtr<Int> intRF( trec, "OBSERVATION_ID" ) ;
170  *intRF = 0 ;
171 
172  // ANTENNA1 and ANTENNA2 are always 0
173  intRF.attachToRecord( trec, "ANTENNA1" ) ;
174  *intRF = 0 ;
175  intRF.attachToRecord( trec, "ANTENNA2" ) ;
176  *intRF = 0 ;
177 
178  // ARRAY_ID is tentatively set to 0
179  intRF.attachToRecord( trec, "ARRAY_ID" ) ;
180  *intRF = 0 ;
181
182  // PROCESSOR_ID is tentatively set to 0
183  intRF.attachToRecord( trec, "PROCESSOR_ID" ) ;
184  *intRF = 0 ;
185
186  // UVW is always [0,0,0]
187  RecordFieldPtr< Array<Double> > uvwRF( trec, "UVW" ) ;
188  *uvwRF = Vector<Double>( 3, 0.0 ) ;
189
190  //
191  // ITERATION: FIELDNAME
192  //
193  TableIterator iter0( table_->table(), "FIELDNAME" ) ;
194  while( !iter0.pastEnd() ) {
195    //Table t0( iter0.table() ) ;
196    Table t0 = iter0.table() ;
197    ROTableColumn sharedCol( t0, "FIELDNAME" ) ;
198    String fieldName = sharedCol.asString( 0 ) ;
199//     tcol = tpoolr->construct( t0, "FIELDNAME" ) ;
200//     String fieldName = tcol->asString( 0 ) ;
201//     tpoolr->destroy( tcol ) ;
202    sharedCol.attach( t0, "SRCNAME" ) ;
203    String srcName = sharedCol.asString( 0 ) ;
204//     tcol = tpoolr->construct( t0, "SRCNAME" ) ;
205//     String srcName = tcol->asString( 0 ) ;
206//     tpoolr->destroy( tcol ) ;
207    sharedCol.attach( t0, "TIME" ) ;
208    Double minTime = (Double)sharedCol.asdouble( 0 ) * 86400.0 ; // day->sec
209//     tcol = tpoolr->construct( t0, "TIME" ) ;
210//     Double minTime = (Double)(tcol->asdouble( 0 )) * 86400.0 ; // day -> sec
211//     tpoolr->destroy( tcol ) ;
212    ROArrayColumn<Double> scanRateCol( t0, "SCANRATE" ) ;
213    //Vector<Double> scanRate = scanRateCol.getColumn()[0] ;
214    Vector<Double> scanRate = scanRateCol( 0 ) ;
215    String::size_type pos = fieldName.find( "__" ) ;
216    Int fieldId = -1 ;
217    if ( pos != String::npos ) {
218      os_ << "fieldName.substr( pos+2 )=" << fieldName.substr( pos+2 ) << LogIO::POST ;
219      fieldId = String::toInt( fieldName.substr( pos+2 ) ) ;
220      fieldName = fieldName.substr( 0, pos ) ;
221    }
222    else {
223      os_ << "use default field id" << LogIO::POST ;
224      fieldId = defaultFieldId ;
225      defaultFieldId++ ;
226    }
227    os_ << "fieldId" << fieldId << ": " << fieldName << LogIO::POST ;
228
229    // FIELD_ID
230    intRF.attachToRecord( trec, "FIELD_ID" ) ;
231    *intRF = fieldId ;
232
233    //
234    // ITERATION: BEAMNO
235    //
236    TableIterator iter1( t0, "BEAMNO" ) ;
237    while( !iter1.pastEnd() ) {
238      Table t1 = iter1.table() ;
239//       tcol = tpoolr->construct( t1, "BEAMNO" ) ;
240//       uInt beamNo = tcol->asuInt( 0 ) ;
241//       tpoolr->destroy( tcol ) ;
242      sharedCol.attach( t1, "BEAMNO" ) ;
243      uInt beamNo = sharedCol.asuInt( 0 ) ;
244      os_ << "beamNo = " << beamNo << LogIO::POST ;
245
246      // FEED1 and FEED2
247      intRF.attachToRecord( trec, "FEED1" ) ;
248      *intRF = beamNo ;
249      intRF.attachToRecord( trec, "FEED2" ) ;
250      *intRF = beamNo ;
251
252      //
253      // ITERATION: SCANNO
254      //
255      TableIterator iter2( t1, "SCANNO" ) ;
256      while( !iter2.pastEnd() ) {
257        Table t2 = iter2.table() ;
258//         tcol = tpoolr->construct( t2, "SCANNO" ) ;
259//         uInt scanNo = tcol->asuInt( 0 ) ;
260//         tpoolr->destroy( tcol ) ;
261        sharedCol.attach( t2, "SCANNO" ) ;
262        uInt scanNo = sharedCol.asuInt( 0 ) ;
263        os_ << "scanNo = " << scanNo << LogIO::POST ;
264
265        // SCAN_NUMBER
266        // MS: 1-based
267        // Scantable: 0-based
268        intRF.attachToRecord( trec, "SCAN_NUMBER" ) ;
269        *intRF = scanNo + 1 ;
270
271        //
272        // ITERATION: IFNO
273        //
274        TableIterator iter3( t2, "IFNO" ) ;
275        while( !iter3.pastEnd() ) {
276          Table t3 = iter3.table() ;
277//           tcol = tpoolr->construct( t3, "IFNO" ) ;
278//           uInt ifNo = tcol->asuInt( 0 ) ;
279//           tpoolr->destroy( tcol ) ;
280          sharedCol.attach( t3, "IFNO" ) ;
281          uInt ifNo = sharedCol.asuInt( 0 ) ;
282          os_ << "ifNo = " << ifNo << LogIO::POST ;
283//           tcol = tpoolr->construct( t3, "FREQ_ID" ) ;
284//           uInt freqId = tcol->asuInt( 0 ) ;
285//           tpoolr->destroy( tcol ) ;
286          sharedCol.attach( t3, "FREQ_ID" ) ;
287          uInt freqId = sharedCol.asuInt( 0 ) ;
288          os_ << "freqId = " << freqId << LogIO::POST ;
289          Int subscan = 1 ; // 1-base
290          //
291          // ITERATION: SRCTYPE
292          //
293          TableIterator iter4( t3, "SRCTYPE" ) ;
294          while( !iter4.pastEnd() ) {
295            Table t4 = iter4.table() ;
296//             tcol = tpoolr->construct( t4, "SRCTYPE" ) ;
297//             uInt srcType = tcol->asInt( 0 ) ;
298//             tpoolr->destroy( tcol ) ;
299            sharedCol.attach( t4, "SRCTYPE" ) ;
300            Int srcType = sharedCol.asInt( 0 ) ;
301            Int stateId = addState( srcType, subscan ) ;
302            *stateidRF = stateId ;
303            //
304            // ITERATION: CYCLENO and TIME
305            //
306            Block<String> cols( 2 ) ;
307            cols[0] = "CYCLENO" ;
308            cols[1] = "TIME" ;
309            TableIterator iter5( t4, cols ) ;
310            while( !iter5.pastEnd() ) {
311              Table t5 =  iter5.table().sort("POLNO") ;
312              //sharedCol.attach( t5, "CYCLENO" ) ;
313              //uInt cycleNo = sharedCol.asuInt( 0 ) ;
314              Int nrow = t5.nrow() ;
315              os_ << "nrow = " << nrow << LogIO::POST ;
316             
317              Vector<Int> polnos( nrow ) ;
318              indgen( polnos, 0 ) ;
319              Int polid = addPolarization( polnos ) ;
320              os_ << "polid = " << polid << LogIO::POST ;
321             
322              // DATA/FLOAT_DATA
323              ROArrayColumn<Float> specCol( t5, "SPECTRA" ) ;
324              ROArrayColumn<uChar> flagCol( t5, "FLAGTRA" ) ;
325              uInt nchan = specCol( 0 ).size() ;
326              IPosition cellshape( 2, nrow, nchan ) ;
327              if ( useFloatData_ ) {
328                // FLOAT_DATA
329                Matrix<Float> dataArr( cellshape ) ;
330                Matrix<Bool> flagArr( cellshape ) ;
331                Vector<Bool> tmpB ;
332                for ( Int ipol = 0 ; ipol < nrow ; ipol++ ) {
333                  dataArr.row( ipol ) = specCol( ipol ) ;
334                  tmpB.reference( flagArr.row( ipol ) ) ;
335                  convertArray( tmpB, flagCol( ipol ) ) ;
336                }
337                //*(*((RecordFieldPtr< Array<Float> > *)dataRF)) = dataArr ;
338                ((RecordFieldPtr< Array<Float> > *)dataRF)->define( dataArr ) ;
339               
340                // FLAG
341                //*flagRF = flagArr ;
342                flagRF.define( flagArr ) ;
343              }
344              else if ( useData_ ) {
345                // DATA
346                // assume nrow = 4
347                Matrix<Complex> dataArr( cellshape ) ;
348                Vector<Float> zeroIm( nchan, 0 ) ;
349                Matrix<Float> dummy( IPosition( 2, 2, nchan ) ) ;
350                dummy.row( 0 ) = specCol( 0 ) ;
351                dummy.row( 1 ) = zeroIm ;
352                dataArr.row( 0 ) = RealToComplex( dummy ) ;
353                dummy.row( 0 ) = specCol( 1 ) ;
354                dataArr.row( 3 ) = RealToComplex( dummy ) ;
355                dummy.row( 0 ) = specCol( 2 ) ;
356                dummy.row( 1 ) = specCol( 3 ) ;
357                dataArr.row( 1 ) = RealToComplex( dummy ) ;
358                dataArr.row( 2 ) = conj( dataArr.row( 1 ) ) ;
359                //*(*((RecordFieldPtr< Array<Complex> > *)dataRF)) = dataArr ;
360                ((RecordFieldPtr< Array<Complex> > *)dataRF)->define( dataArr ) ;
361               
362               
363                // FLAG
364                Matrix<Bool> flagArr( cellshape ) ;
365                Vector<Bool> tmpB ;
366                tmpB.reference( flagArr.row( 0 ) ) ;
367                convertArray( tmpB, flagCol( 0 ) ) ;
368                tmpB.reference( flagArr.row( 3 ) ) ;
369                convertArray( tmpB, flagCol( 3 ) ) ;
370                tmpB.reference( flagArr.row( 1 ) ) ;
371                convertArray( tmpB, ( flagCol( 2 ) | flagCol( 3 ) ) ) ;
372                flagArr.row( 2 ) = flagArr.row( 1 ) ;
373                //*flagRF = flagArr ;
374                flagRF.define( flagArr ) ;
375              }
376
377              // FLAG_ROW
378//               tcol = tpoolr->construct( t5, "FLAGROW" ) ;
379              sharedCol.attach( t5, "FLAGROW" ) ;
380              Vector<uInt> flagRowArr( nrow ) ;
381              for ( Int irow = 0 ; irow < nrow ; irow++ )
382                flagRowArr[irow] = sharedCol.asuInt( irow ) ;
383//                 flagRowArr[irow] = tcol->asuInt( irow ) ;
384//               tpoolr->destroy( tcol ) ;
385              *flagrowRF = anyNE( flagRowArr, (uInt)0 ) ;
386
387              // TIME and TIME_CENTROID
388//               tcol = tpoolr->construct( t5, "TIME" ) ;
389//               Double mTimeV = (Double)(tcol->asdouble(0)) * 86400.0 ; // day->sec
390//               tpoolr->destroy( tcol ) ;
391              sharedCol.attach( t5, "TIME" ) ;
392              Double mTimeV = (Double)sharedCol.asdouble( 0 ) * 86400.0 ; // day -> sec
393              *timeRF = mTimeV ;
394              *timecRF = mTimeV ;
395
396              // INTERVAL and EXPOSURE
397//               tcol = tpoolr->construct( t5, "INTERVAL" ) ;
398//               Double interval = (Double)(tcol->asdouble( 0 )) ;
399//               tpoolr->destroy( tcol ) ;
400              sharedCol.attach( t5, "INTERVAL" ) ;
401              Double interval = (Double)sharedCol.asdouble( 0 ) ;
402              *intervalRF = interval ;
403              *exposureRF = interval ;
404             
405              // WEIGHT and SIGMA
406              // always 1 at the moment
407              Vector<Float> wArr( nrow, 1.0 ) ;
408              *weightRF = wArr ;
409              *sigmaRF = wArr ;
410             
411              // add DATA_DESCRIPTION row
412              Int ddid = addDataDescription( polid, ifNo ) ;
413              os_ << "ddid = " << ddid << LogIO::POST ;
414              *ddidRF = ddid ;
415             
416              // for SYSCAL table
417//               tcol = tpoolr->construct( t5, "TCAL_ID" ) ;
418              sharedCol.attach( t5, "TCAL_ID" ) ;
419              Vector<uInt> tcalIdArr( nrow ) ;
420              for ( Int irow = 0 ; irow < nrow ; irow++ )
421                tcalIdArr[irow] = sharedCol.asuInt( irow ) ;
422//                 tcalIdArr[irow] = tcol->asuInt( irow ) ;
423//               tpoolr->destroy( tcol ) ;
424              os_ << "tcalIdArr = " << tcalIdArr << LogIO::POST ;
425              String key = String::toString( tcalIdArr[0] ) ;
426              if ( !tcalIdRec_.isDefined( key ) ) {
427                tcalIdRec_.define( key, tcalIdArr ) ;
428                tcalRowRec_.define( key, t5.rowNumbers() ) ;
429              }
430              else {
431                Vector<uInt> pastrows = tcalRowRec_.asArrayuInt( key ) ;
432                tcalRowRec_.define( key, concatenateArray( pastrows, t5.rowNumbers() ) ) ;
433              }
434             
435              // fill STATE_ID
436              //ROScalarColumn<Int> srcTypeCol( t5, "SRCTYPE" ) ;
437              //Int srcType = srcTypeCol( 0 ) ;
438              //Int stateId = addState( srcType, subscan ) ;
439              //*stateidRF = stateId ;
440             
441              // for POINTING table
442              if ( ptTabName_ == "" ) {
443                ROArrayColumn<Double> dirCol( t5, "DIRECTION" ) ;
444                Vector<Double> dir = dirCol( 0 ) ;
445                dirCol.attach( t5, "SCANRATE" ) ;
446                Vector<Double> rate = dirCol( 0 ) ;
447                Matrix<Double> msDir( 2, 1 ) ;
448                msDir.column( 0 ) = dir ;
449                if ( anyNE( rate, 0.0 ) ) {
450                  msDir.resize( 2, 2 ) ;
451                  msDir.column( 1 ) = rate ;
452                }
453                addPointing( fieldName, mTimeV, interval, msDir ) ;
454              }
455             
456              // FLAG_CATEGORY is tentatively set
457              RecordFieldPtr< Array<Bool> > flagcatRF( trec, "FLAG_CATEGORY" ) ;
458              //*flagcatRF = Cube<Bool>( nrow, nchan, 1, False ) ;
459              flagcatRF.define( Cube<Bool>( nrow, nchan, 1, False ) ) ;
460             
461              // add row
462              mstable_->addRow( 1, True ) ;
463              row.put( mstable_->nrow()-1 ) ;
464             
465              iter5.next() ;
466            }
467           
468            iter4.next() ;
469          }
470
471          // add SPECTRAL_WINDOW row
472          if ( allNE( processedFreqId, freqId ) ) {
473            uInt vsize = processedFreqId.size() ;
474            processedFreqId.resize( vsize+1, True ) ;
475            processedFreqId[vsize] = freqId ;
476            addSpectralWindow( ifNo, freqId ) ;
477          }
478         
479          iter3.next() ;
480        }
481       
482        iter2.next() ;
483      }
484     
485      // add FEED row
486      addFeed( beamNo ) ;
487     
488      iter1.next() ;
489    }
490   
491    // add FIELD row
492    addField( fieldId, fieldName, srcName, minTime, scanRate ) ;
493
494    iter0.next() ;
495  }
496
497//   delete tpoolr ;
498  delete dataRF ;
499
500  // SYSCAL
501  if ( isTcal_ )
502    fillSysCal() ;
503
504  // replace POINTING table with original one if exists
505  if ( ptTabName_ != "" ) {
506    delete mstable_ ;
507    mstable_ = 0 ;
508    Table newPtTab( ptTabName_, Table::Old ) ;
509    newPtTab.copy( filename_+"/POINTING", Table::New ) ;
510  }
511
512  double endSec = gettimeofday_sec() ;
513  os_ << "end MSWriter::write() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
514
515  return True ;
516}
517 
518void MSWriter::init()
519{
520//   os_.origin( LogOrigin( "MSWriter", "init()", WHERE ) ) ;
521  double startSec = gettimeofday_sec() ;
522  os_ << "start MSWriter::init() startSec=" << startSec << LogIO::POST ;
523 
524  // access to scantable
525  header_ = table_->getHeader() ;
526
527  // FLOAT_DATA? or DATA?
528  if ( header_.npol > 2 ) {
529    useFloatData_ = False ;
530    useData_ = True ;
531  }
532  else {
533    useFloatData_ = True ;
534    useData_ = False ;
535  }
536
537  // polarization type
538  polType_ = header_.poltype ;
539  if ( polType_ == "" )
540    polType_ = "stokes" ;
541  else if ( polType_.find( "linear" ) != String::npos )
542    polType_ = "linear" ;
543  else if ( polType_.find( "circular" ) != String::npos )
544    polType_ = "circular" ;
545  else if ( polType_.find( "stokes" ) != String::npos )
546    polType_ = "stokes" ;
547  else if ( polType_.find( "linpol" ) != String::npos )
548    polType_ = "linpol" ;
549  else
550    polType_ = "notype" ;
551
552  // Check if some subtables are exists
553  if ( table_->tcal().table().nrow() != 0 ) {
554    ROTableColumn col( table_->tcal().table(), "TCAL" ) ;
555    if ( col.isDefined( 0 ) ) {
556      os_ << "nrow=" << table_->tcal().table().nrow() << ": TCAL table exists" << LogIO::POST ;
557      isTcal_ = True ;
558    }
559    else {
560      os_ << "no TCAL rows" << LogIO::POST ;
561    }
562  }
563  else {
564    os_ << "no TCAL rows" << LogIO::POST ;
565  }
566  if ( table_->weather().table().nrow() != 0 ) {
567    ROTableColumn col( table_->weather().table(), "TEMPERATURE" ) ;
568    if ( col.isDefined( 0 ) ) {
569      os_ << "nrow=" << table_->weather().table().nrow() << ": WEATHER table exists" << LogIO::POST ;
570      isWeather_ =True ;
571    }
572    else {
573      os_ << "no WEATHER rows" << LogIO::POST ;
574    }
575  }
576  else {
577    os_ << "no WEATHER rows" << LogIO::POST ;
578  }
579
580  // Are TCAL_SPECTRUM and TSYS_SPECTRUM necessary?
581  if ( isTcal_ && header_.nchan != 1 ) {
582    // examine TCAL subtable
583    Table tcaltab = table_->tcal().table() ;
584    ROArrayColumn<Float> tcalCol( tcaltab, "TCAL" ) ;
585    for ( uInt irow = 0 ; irow < tcaltab.nrow() ; irow++ ) {
586      if ( tcalCol( irow ).size() != 1 )
587        tcalSpec_ = True ;
588    }
589    // examine spectral data
590    TableIterator iter0( table_->table(), "IFNO" ) ;
591    while( !iter0.pastEnd() ) {
592      Table t0( iter0.table() ) ;
593      ROArrayColumn<Float> sharedFloatArrCol( t0, "SPECTRA" ) ;
594      uInt len = sharedFloatArrCol( 0 ).size() ;
595      if ( len != 1 ) {
596        sharedFloatArrCol.attach( t0, "TSYS" ) ;
597        if ( sharedFloatArrCol( 0 ).size() != 1 )
598          tsysSpec_ = True ;
599      }
600      iter0.next() ;
601    }
602  }
603
604  // check if reference for POINTING table exists
605  const TableRecord &rec = table_->table().keywordSet() ;
606  if ( rec.isDefined( "POINTING" ) ) {
607    ptTabName_ = rec.asString( "POINTING" ) ;
608    if ( !Table::isReadable( ptTabName_ ) ) {
609      ptTabName_ = "" ;
610    }
611  }
612
613  double endSec = gettimeofday_sec() ;
614  os_ << "end MSWriter::init() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
615}
616
617void MSWriter::setupMS()
618{
619//   os_.origin( LogOrigin( "MSWriter", "setupMS()", WHERE ) ) ;
620  double startSec = gettimeofday_sec() ;
621  os_ << "start MSWriter::setupMS() startSec=" << startSec << LogIO::POST ;
622
623  TableDesc msDesc = MeasurementSet::requiredTableDesc() ;
624  if ( useFloatData_ )
625    MeasurementSet::addColumnToDesc( msDesc, MSMainEnums::FLOAT_DATA, 2 ) ;
626  else if ( useData_ )
627    MeasurementSet::addColumnToDesc( msDesc, MSMainEnums::DATA, 2 ) ;
628
629  SetupNewTable newtab( filename_, msDesc, Table::New ) ;
630
631  mstable_ = new MeasurementSet( newtab ) ;
632
633  // create subtables
634  TableDesc antennaDesc = MSAntenna::requiredTableDesc() ;
635  SetupNewTable antennaTab( mstable_->antennaTableName(), antennaDesc, Table::New ) ;
636  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::ANTENNA ), Table( antennaTab ) ) ;
637
638  TableDesc dataDescDesc = MSDataDescription::requiredTableDesc() ;
639  SetupNewTable dataDescTab( mstable_->dataDescriptionTableName(), dataDescDesc, Table::New ) ;
640  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::DATA_DESCRIPTION ), Table( dataDescTab ) ) ;
641
642  TableDesc dopplerDesc = MSDoppler::requiredTableDesc() ;
643  SetupNewTable dopplerTab( mstable_->dopplerTableName(), dopplerDesc, Table::New ) ;
644  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::DOPPLER ), Table( dopplerTab ) ) ;
645
646  TableDesc feedDesc = MSFeed::requiredTableDesc() ;
647  SetupNewTable feedTab( mstable_->feedTableName(), feedDesc, Table::New ) ;
648  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FEED ), Table( feedTab ) ) ;
649
650  TableDesc fieldDesc = MSField::requiredTableDesc() ;
651  SetupNewTable fieldTab( mstable_->fieldTableName(), fieldDesc, Table::New ) ;
652  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FIELD ), Table( fieldTab ) ) ;
653
654  TableDesc flagCmdDesc = MSFlagCmd::requiredTableDesc() ;
655  SetupNewTable flagCmdTab( mstable_->flagCmdTableName(), flagCmdDesc, Table::New ) ;
656  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FLAG_CMD ), Table( flagCmdTab ) ) ;
657
658  TableDesc freqOffsetDesc = MSFreqOffset::requiredTableDesc() ;
659  SetupNewTable freqOffsetTab( mstable_->freqOffsetTableName(), freqOffsetDesc, Table::New ) ;
660  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FREQ_OFFSET ), Table( freqOffsetTab ) ) ;
661
662  TableDesc historyDesc = MSHistory::requiredTableDesc() ;
663  SetupNewTable historyTab( mstable_->historyTableName(), historyDesc, Table::New ) ;
664  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::HISTORY ), Table( historyTab ) ) ;
665
666  TableDesc observationDesc = MSObservation::requiredTableDesc() ;
667  SetupNewTable observationTab( mstable_->observationTableName(), observationDesc, Table::New ) ;
668  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::OBSERVATION ), Table( observationTab ) ) ;
669
670  TableDesc pointingDesc = MSPointing::requiredTableDesc() ;
671  SetupNewTable pointingTab( mstable_->pointingTableName(), pointingDesc, Table::New ) ;
672  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::POINTING ), Table( pointingTab ) ) ;
673
674  TableDesc polarizationDesc = MSPolarization::requiredTableDesc() ;
675  SetupNewTable polarizationTab( mstable_->polarizationTableName(), polarizationDesc, Table::New ) ;
676  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::POLARIZATION ), Table( polarizationTab ) ) ;
677
678  TableDesc processorDesc = MSProcessor::requiredTableDesc() ;
679  SetupNewTable processorTab( mstable_->processorTableName(), processorDesc, Table::New ) ;
680  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::PROCESSOR ), Table( processorTab ) ) ;
681
682  TableDesc sourceDesc = MSSource::requiredTableDesc() ;
683  MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::TRANSITION, 1 ) ;
684  MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::REST_FREQUENCY, 1 ) ;
685  MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::SYSVEL, 1 ) ;
686  SetupNewTable sourceTab( mstable_->sourceTableName(), sourceDesc, Table::New ) ;
687  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SOURCE ), Table( sourceTab ) ) ;
688
689  TableDesc spwDesc = MSSpectralWindow::requiredTableDesc() ;
690  SetupNewTable spwTab( mstable_->spectralWindowTableName(), spwDesc, Table::New ) ;
691  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SPECTRAL_WINDOW ), Table( spwTab ) ) ;
692
693  TableDesc stateDesc = MSState::requiredTableDesc() ;
694  SetupNewTable stateTab( mstable_->stateTableName(), stateDesc, Table::New ) ;
695  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::STATE ), Table( stateTab ) ) ;
696
697  // TODO: add TCAL_SPECTRUM and TSYS_SPECTRUM if necessary
698  TableDesc sysCalDesc = MSSysCal::requiredTableDesc() ;
699  MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TCAL, 2 ) ;
700  MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TSYS, 2 ) ;
701  if ( tcalSpec_ )
702    MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TCAL_SPECTRUM, 2 ) ;
703  if ( tsysSpec_ )
704    MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TSYS_SPECTRUM, 2 ) ;
705  SetupNewTable sysCalTab( mstable_->sysCalTableName(), sysCalDesc, Table::New ) ;
706  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SYSCAL ), Table( sysCalTab ) ) ;
707
708  TableDesc weatherDesc = MSWeather::requiredTableDesc() ;
709  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::TEMPERATURE ) ;
710  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::PRESSURE ) ;
711  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::REL_HUMIDITY ) ;
712  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::WIND_SPEED ) ;
713  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::WIND_DIRECTION ) ;
714  SetupNewTable weatherTab( mstable_->weatherTableName(), weatherDesc, Table::New ) ;
715  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::WEATHER ), Table( weatherTab ) ) ;
716
717  mstable_->initRefs() ;
718
719  double endSec = gettimeofday_sec() ;
720  os_ << "end MSWriter::setupMS() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
721}
722
723void MSWriter::fillObservation()
724{
725  double startSec = gettimeofday_sec() ;
726  os_ << "start MSWriter::fillObservation() startSec=" << startSec << LogIO::POST ;
727
728  // only 1 row
729  mstable_->observation().addRow( 1, True ) ;
730  MSObservationColumns msObsCols( mstable_->observation() ) ;
731  msObsCols.observer().put( 0, header_.observer ) ;
732  // tentatively put antennaname (from ANTENNA subtable)
733  String hAntennaName = header_.antennaname ;
734  String::size_type pos = hAntennaName.find( "//" ) ;
735  String telescopeName ;
736  if ( pos != String::npos ) {
737    telescopeName = hAntennaName.substr( 0, pos ) ;
738  }
739  else {
740    pos = hAntennaName.find( "@" ) ;
741    telescopeName = hAntennaName.substr( 0, pos ) ;
742  }
743  os_ << "telescopeName = " << telescopeName << LogIO::POST ;
744  msObsCols.telescopeName().put( 0, telescopeName ) ;
745  msObsCols.project().put( 0, header_.project ) ;
746  //ScalarMeasColumn<MEpoch> timeCol( table_->table().sort("TIME"), "TIME" ) ;
747  Table sortedtable = table_->table().sort("TIME") ;
748  ScalarMeasColumn<MEpoch> timeCol( sortedtable, "TIME" ) ;
749  Vector<MEpoch> trange( 2 ) ;
750  trange[0] = timeCol( 0 ) ;
751  trange[1] = timeCol( table_->nrow()-1 ) ;
752  msObsCols.timeRangeMeas().put( 0, trange ) ;
753
754  double endSec = gettimeofday_sec() ;
755  os_ << "end MSWriter::fillObservation() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
756}
757
758void MSWriter::fillAntenna()
759{
760  double startSec = gettimeofday_sec() ;
761  os_ << "start MSWriter::fillAntenna() startSec=" << startSec << LogIO::POST ;
762
763  // only 1 row
764  mstable_->antenna().addRow( 1, True ) ;
765  MSAntennaColumns msAntCols( mstable_->antenna() ) ;
766
767  String hAntennaName = header_.antennaname ;
768  String::size_type pos = hAntennaName.find( "//" ) ;
769  String antennaName ;
770  String stationName ;
771  if ( pos != String::npos ) {
772    hAntennaName = hAntennaName.substr( pos+2 ) ;
773  }
774  pos = hAntennaName.find( "@" ) ;
775  if ( pos != String::npos ) {
776    antennaName = hAntennaName.substr( 0, pos ) ;
777    stationName = hAntennaName.substr( pos+1 ) ;
778  }
779  else {
780    antennaName = hAntennaName ;
781    stationName = hAntennaName ;
782  }
783  os_ << "antennaName = " << antennaName << LogIO::POST ;
784  os_ << "stationName = " << stationName << LogIO::POST ;
785 
786  msAntCols.name().put( 0, antennaName ) ;
787  msAntCols.station().put( 0, stationName ) ;
788
789  os_ << "antennaPosition = " << header_.antennaposition << LogIO::POST ;
790 
791  msAntCols.position().put( 0, header_.antennaposition ) ;
792
793  double endSec = gettimeofday_sec() ;
794  os_ << "end MSWriter::fillAntenna() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
795}
796
797void MSWriter::fillProcessor()
798{
799  double startSec = gettimeofday_sec() ;
800  os_ << "start MSWriter::fillProcessor() startSec=" << startSec << LogIO::POST ;
801 
802  // only add empty 1 row
803  MSProcessor msProc = mstable_->processor() ;
804  msProc.addRow( 1, True ) ;
805
806  double endSec = gettimeofday_sec() ;
807  os_ << "end MSWriter::fillProcessor() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
808}
809
810void MSWriter::fillSource()
811{
812  double startSec = gettimeofday_sec() ;
813  os_ << "start MSWriter::fillSource() startSec=" << startSec << LogIO::POST ;
814 
815  // access to MS SOURCE subtable
816  MSSource msSrc = mstable_->source() ;
817
818  // access to MOLECULE subtable
819  STMolecules stm = table_->molecules() ;
820
821  Int srcId = 0 ;
822  Vector<Double> restFreq ;
823  Vector<String> molName ;
824  Vector<String> fMolName ;
825
826  // row based
827  TableRow row( msSrc ) ;
828  TableRecord &rec = row.record() ;
829  RecordFieldPtr<Int> srcidRF( rec, "SOURCE_ID" ) ;
830  RecordFieldPtr<String> nameRF( rec, "NAME" ) ;
831  RecordFieldPtr< Array<Double> > srcpmRF( rec, "PROPER_MOTION" ) ;
832  RecordFieldPtr< Array<Double> > srcdirRF( rec, "DIRECTION" ) ;
833  RecordFieldPtr<Int> numlineRF( rec, "NUM_LINES" ) ;
834  RecordFieldPtr< Array<Double> > restfreqRF( rec, "REST_FREQUENCY" ) ;
835  RecordFieldPtr< Array<Double> > sysvelRF( rec, "SYSVEL" ) ;
836  RecordFieldPtr< Array<String> > transitionRF( rec, "TRANSITION" ) ;
837  RecordFieldPtr<Double> timeRF( rec, "TIME" ) ;
838  RecordFieldPtr<Double> intervalRF( rec, "INTERVAL" ) ;
839  RecordFieldPtr<Int> spwidRF( rec, "SPECTRAL_WINDOW_ID" ) ;
840
841  //
842  // ITERATION: SRCNAME
843  //
844  TableIterator iter0( table_->table(), "SRCNAME" ) ;
845  while( !iter0.pastEnd() ) {
846    //Table t0( iter0.table() ) ;
847    Table t0 =  iter0.table()  ;
848
849    // get necessary information
850    ROScalarColumn<String> srcNameCol( t0, "SRCNAME" ) ;
851    String srcName = srcNameCol( 0 ) ;
852    ROArrayColumn<Double> sharedDArrRCol( t0, "SRCPROPERMOTION" ) ;
853    Vector<Double> srcPM = sharedDArrRCol( 0 ) ;
854    sharedDArrRCol.attach( t0, "SRCDIRECTION" ) ;
855    Vector<Double> srcDir = sharedDArrRCol( 0 ) ;
856    ROScalarColumn<Double> srcVelCol( t0, "SRCVELOCITY" ) ;
857    Double srcVel = srcVelCol( 0 ) ;
858
859    // NAME
860    *nameRF = srcName ;
861
862    // SOURCE_ID
863    *srcidRF = srcId ;
864
865    // PROPER_MOTION
866    *srcpmRF = srcPM ;
867   
868    // DIRECTION
869    *srcdirRF = srcDir ;
870
871    //
872    // ITERATION: MOLECULE_ID
873    //
874    TableIterator iter1( t0, "MOLECULE_ID" ) ;
875    while( !iter1.pastEnd() ) {
876      //Table t1( iter1.table() ) ;
877      Table t1 = iter1.table() ;
878
879      // get necessary information
880      ROScalarColumn<uInt> molIdCol( t1, "MOLECULE_ID" ) ;
881      uInt molId = molIdCol( 0 ) ;
882      stm.getEntry( restFreq, molName, fMolName, molId ) ;
883
884      uInt numFreq = restFreq.size() ;
885     
886      // NUM_LINES
887      *numlineRF = numFreq ;
888
889      // REST_FREQUENCY
890      *restfreqRF = restFreq ;
891
892      // TRANSITION
893      //*transitionRF = fMolName ;
894      Vector<String> transition ;
895      if ( fMolName.size() != 0 ) {
896        transition = fMolName ;
897      }
898      else if ( molName.size() != 0 ) {
899        transition = molName ;
900      }
901      else {
902        transition.resize( numFreq ) ;
903        transition = "" ;
904      }
905      *transitionRF = transition ;
906
907      // SYSVEL
908      Vector<Double> sysvelArr( numFreq, srcVel ) ;
909      *sysvelRF = sysvelArr ;
910
911      //
912      // ITERATION: IFNO
913      //
914      TableIterator iter2( t1, "IFNO" ) ;
915      while( !iter2.pastEnd() ) {
916        //Table t2( iter2.table() ) ;
917        Table t2 = iter2.table() ;
918        uInt nrow = msSrc.nrow() ;
919
920        // get necessary information
921        ROScalarColumn<uInt> ifNoCol( t2, "IFNO" ) ;
922        uInt ifno = ifNoCol( 0 ) ; // IFNO = SPECTRAL_WINDOW_ID
923        Double midTime ;
924        Double interval ;
925        getValidTimeRange( midTime, interval, t2 ) ;
926
927        // fill SPECTRAL_WINDOW_ID
928        *spwidRF = ifno ;
929
930        // fill TIME, INTERVAL
931        *timeRF = midTime ;
932        *intervalRF = interval ;
933
934        // add row
935        msSrc.addRow( 1, True ) ;
936        row.put( nrow ) ;
937
938        iter2.next() ;
939      }
940
941      iter1.next() ;
942    }
943
944    // increment srcId if SRCNAME changed
945    srcId++ ;
946
947    iter0.next() ;
948  }
949
950  double endSec = gettimeofday_sec() ;
951  os_ << "end MSWriter::fillSource() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
952}
953
954void MSWriter::fillWeather()
955{
956  double startSec = gettimeofday_sec() ;
957  os_ << "start MSWriter::fillWeather() startSec=" << startSec << LogIO::POST ;
958
959  // access to MS WEATHER subtable
960  MSWeather msw = mstable_->weather() ;
961
962  // access to WEATHER subtable
963  Table stw = table_->weather().table() ;
964  uInt nrow = stw.nrow() ;
965
966  if ( nrow == 0 )
967    return ;
968
969  msw.addRow( nrow, True ) ;
970  MSWeatherColumns mswCols( msw ) ;
971
972  // ANTENNA_ID is always 0
973  Vector<Int> antIdArr( nrow, 0 ) ;
974  mswCols.antennaId().putColumn( antIdArr ) ;
975
976  // fill weather status
977  ROScalarColumn<Float> sharedFloatCol( stw, "TEMPERATURE" ) ;
978  mswCols.temperature().putColumn( sharedFloatCol ) ;
979  sharedFloatCol.attach( stw, "PRESSURE" ) ;
980  mswCols.pressure().putColumn( sharedFloatCol ) ;
981  sharedFloatCol.attach( stw, "HUMIDITY" ) ;
982  mswCols.relHumidity().putColumn( sharedFloatCol ) ;
983  sharedFloatCol.attach( stw, "WINDSPEED" ) ;
984  mswCols.windSpeed().putColumn( sharedFloatCol ) ;
985  sharedFloatCol.attach( stw, "WINDAZ" ) ;
986  mswCols.windDirection().putColumn( sharedFloatCol ) ;
987
988  // fill TIME and INTERVAL
989  Double midTime ;
990  Double interval ;
991  Vector<Double> intervalArr( nrow, 0.0 ) ;
992  TableIterator iter( table_->table(), "WEATHER_ID" ) ;
993  while( !iter.pastEnd() ) {
994    //Table tab( iter.table() ) ;
995    Table tab = iter.table() ;
996
997    ROScalarColumn<uInt> widCol( tab, "WEATHER_ID" ) ;
998    uInt wid = widCol( 0 ) ;
999
1000    getValidTimeRange( midTime, interval, tab ) ;
1001    mswCols.time().put( wid, midTime ) ;
1002    intervalArr[wid] = interval ;
1003
1004    iter.next() ;
1005  }
1006  mswCols.interval().putColumn( intervalArr ) ;
1007
1008  double endSec = gettimeofday_sec() ;
1009  os_ << "end MSWriter::fillWeather() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1010}
1011
1012void MSWriter::fillSysCal()
1013{
1014  double startSec = gettimeofday_sec() ;
1015  os_ << "start MSWriter::fillSysCal() startSec=" << startSec << LogIO::POST ;
1016
1017  //tcalIdRec_.print( cout ) ;
1018
1019  // access to MS SYSCAL subtable
1020  MSSysCal mssc = mstable_->sysCal() ;
1021
1022  // access to TCAL subtable
1023  Table stt = table_->tcal().table() ;
1024  uInt nrow = stt.nrow() ;
1025
1026  // access to MAIN table
1027  Block<String> cols( 6 ) ;
1028  cols[0] = "TIME" ;
1029  cols[1] = "TCAL_ID" ;
1030  cols[2] = "TSYS" ;
1031  cols[3] = "BEAMNO" ;
1032  cols[4] = "IFNO" ;
1033  cols[5] = "INTERVAL" ;
1034  Table tab = table_->table().project( cols ) ;
1035
1036  if ( nrow == 0 )
1037    return ;
1038
1039  nrow = tcalIdRec_.nfields() ;
1040
1041  Double midTime ;
1042  Double interval ;
1043  String timeStr ;
1044
1045  // row base
1046  TableRow row( mssc ) ;
1047  TableRecord &trec = row.record() ;
1048  RecordFieldPtr<Int> antennaRF( trec, "ANTENNA_ID" ) ;
1049  RecordFieldPtr<Int> feedRF( trec, "FEED_ID" ) ;
1050  RecordFieldPtr<Int> spwRF( trec, "SPECTRAL_WINDOW_ID" ) ;
1051  RecordFieldPtr<Double> timeRF( trec, "TIME" ) ;
1052  RecordFieldPtr<Double> intervalRF( trec, "INTERVAL" ) ;
1053  RecordFieldPtr< Array<Float> > tsysRF( trec, "TSYS" ) ;
1054  RecordFieldPtr< Array<Float> > tcalRF( trec, "TCAL" ) ;
1055  RecordFieldPtr< Array<Float> > tsysspRF ;
1056  RecordFieldPtr< Array<Float> > tcalspRF ;
1057  if ( tsysSpec_ )
1058    tsysspRF.attachToRecord( trec, "TSYS_SPECTRUM" ) ;
1059  if ( tcalSpec_ )
1060    tcalspRF.attachToRecord( trec, "TCAL_SPECTRUM" ) ;
1061
1062  // ANTENNA_ID is always 0
1063  *antennaRF = 0 ;
1064
1065  Table sortedstt = stt.sort( "ID" ) ;
1066  ROArrayColumn<Float> tcalCol( sortedstt, "TCAL" ) ;
1067  ROTableColumn idCol( sortedstt, "ID" ) ;
1068  ROArrayColumn<Float> tsysCol( tab, "TSYS" ) ;
1069  ROTableColumn tcalidCol( tab, "TCAL_ID" ) ;
1070  ROTableColumn timeCol( tab, "TIME" ) ;
1071  ROTableColumn intervalCol( tab, "INTERVAL" ) ;
1072  ROTableColumn beamnoCol( tab, "BEAMNO" ) ;
1073  ROTableColumn ifnoCol( tab, "IFNO" ) ;
1074  for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1075    double t1 = gettimeofday_sec() ;
1076    Vector<uInt> ids = tcalIdRec_.asArrayuInt( irow ) ;
1077    os_ << "ids = " << ids << LogIO::POST ;
1078    uInt npol = ids.size() ;
1079    Vector<uInt> rows = tcalRowRec_.asArrayuInt( irow ) ;
1080    os_ << "rows = " << rows << LogIO::POST ;
1081    Vector<Double> atime( rows.nelements() ) ;
1082    Vector<Double> ainterval( rows.nelements() ) ;
1083    Vector<uInt> atcalid( rows.nelements() ) ;
1084    for( uInt jrow = 0 ; jrow < rows.nelements() ; jrow++ ) {
1085      atime[jrow] = (Double)timeCol.asdouble( rows[jrow] ) ;
1086      ainterval[jrow] = (Double)intervalCol.asdouble( rows[jrow] ) ;
1087      atcalid[jrow] = tcalidCol.asuInt( rows[jrow] ) ;
1088    }
1089    Vector<Float> dummy = tsysCol( rows[0] ) ;
1090    Matrix<Float> tsys( npol,dummy.nelements() ) ;
1091    tsys.row( 0 ) = dummy ;
1092    for ( uInt jrow = 1 ; jrow < npol ; jrow++ )
1093      tsys.row( jrow ) = tsysCol( rows[jrow] ) ;
1094
1095    // FEED_ID
1096    *feedRF = beamnoCol.asuInt( rows[0] ) ;
1097
1098    // SPECTRAL_WINDOW_ID
1099    *spwRF = ifnoCol.asuInt( rows[0] ) ;
1100
1101    // TIME and INTERVAL
1102    getValidTimeRange( midTime, interval, atime, ainterval ) ;
1103    *timeRF = midTime ;
1104    *intervalRF = interval ;
1105
1106    // TCAL and TSYS
1107    Matrix<Float> tcal ;
1108    Table t ;
1109    if ( idCol.asuInt( ids[0] ) == ids[0] ) {
1110      os_ << "sorted at irow=" << irow << " ids[0]=" << ids[0] << LogIO::POST ;
1111      Vector<Float> dummyC = tcalCol( ids[0] ) ;
1112      tcal.resize( npol, dummyC.size() ) ;
1113      tcal.row( 0 ) = dummyC ;
1114    }
1115    else {
1116      os_ << "NOT sorted at irow=" << irow << " ids[0]=" << ids[0] << LogIO::POST ;
1117      t = stt( stt.col("ID") == ids[0] ) ;
1118      Vector<Float> dummyC = tcalCol( 0 ) ;
1119      tcal.resize( npol, dummyC.size() ) ;
1120      tcal.row( 0 ) = dummyC ;
1121    }
1122    if ( npol == 2 ) {
1123      if ( idCol.asuInt( ids[1] ) == ids[1] ) {
1124        os_ << "sorted at irow=" << irow << " ids[1]=" << ids[1] << LogIO::POST ;
1125        tcal.row( 1 ) = tcalCol( ids[1] ) ;
1126      }
1127      else {
1128        os_ << "NOT sorted at irow=" << irow << " ids[1]=" << ids[1] << LogIO::POST ;
1129        t = stt( stt.col("ID") == ids[1] ) ;
1130        tcalCol.attach( t, "TCAL" ) ;
1131        tcal.row( 1 ) = tcalCol( 1 ) ;
1132      }
1133    }
1134    else if ( npol == 3 ) {
1135      if ( idCol.asuInt( ids[2] ) == ids[2] )
1136        tcal.row( 1 ) = tcalCol( ids[2] ) ;
1137      else {
1138        t = stt( stt.col("ID") == ids[2] ) ;
1139        tcalCol.attach( t, "TCAL" ) ;
1140        tcal.row( 1 ) = tcalCol( 0 ) ;
1141      }
1142      if ( idCol.asuInt( ids[1] ) == ids[1] )
1143        tcal.row( 2 ) = tcalCol( ids[1] ) ;
1144      else {
1145        t = stt( stt.col("ID") == ids[1] ) ;
1146        tcalCol.attach( t, "TCAL" ) ;
1147        tcal.row( 2 ) = tcalCol( 0 ) ;
1148      }
1149    }
1150    else if ( npol == 4 ) {
1151      if ( idCol.asuInt( ids[2] ) == ids[2] )
1152        tcal.row( 1 ) = tcalCol( ids[2] ) ;
1153      else {
1154        t = stt( stt.col("ID") == ids[2] ) ;
1155        tcalCol.attach( t, "TCAL" ) ;
1156        tcal.row( 1 ) = tcalCol( 0 ) ;
1157      }
1158      if ( idCol.asuInt( ids[3] ) == ids[3] )
1159        tcal.row( 2 ) = tcalCol( ids[3] ) ;
1160      else {
1161        t = stt( stt.col("ID") == ids[3] ) ;
1162        tcalCol.attach( t, "TCAL" ) ;
1163        tcal.row( 2 ) = tcalCol( 0 ) ;
1164      }
1165      if ( idCol.asuInt( ids[1] ) == ids[1] )
1166        tcal.row( 2 ) = tcalCol( ids[1] ) ;
1167      else {
1168        t = stt( stt.col("ID") == ids[1] ) ;
1169        tcalCol.attach( t, "TCAL" ) ;
1170        tcal.row( 3 ) = tcalCol( 0 ) ;
1171      }
1172    }
1173    if ( tcalSpec_ ) {
1174      // put TCAL_SPECTRUM
1175      //*tcalspRF = tcal ;
1176      tcalspRF.define( tcal ) ;
1177      // set TCAL (mean of TCAL_SPECTRUM)
1178      Matrix<Float> tcalMean( npol, 1 ) ;
1179      for ( uInt iid = 0 ; iid < npol ; iid++ ) {
1180        tcalMean( iid, 0 ) = mean( tcal.row(iid) ) ;
1181      }
1182      // put TCAL
1183      *tcalRF = tcalMean ;
1184    }
1185    else {
1186      // put TCAL
1187      *tcalRF = tcal ;
1188    }
1189   
1190    if ( tsysSpec_ ) {
1191      // put TSYS_SPECTRUM
1192      //*tsysspRF = tsys ;
1193      tsysspRF.define( tsys ) ;
1194      // set TSYS (mean of TSYS_SPECTRUM)
1195      Matrix<Float> tsysMean( npol, 1 ) ;
1196      for ( uInt iid = 0 ; iid < npol ; iid++ ) {
1197        tsysMean( iid, 0 ) = mean( tsys.row(iid) ) ;
1198      }
1199      // put TSYS
1200      *tsysRF = tsysMean ;
1201    }
1202    else {
1203      // put TSYS
1204      *tsysRF = tsys ;
1205    }
1206
1207    // add row
1208    mssc.addRow( 1, True ) ;
1209    row.put( mssc.nrow()-1 ) ;
1210
1211    double t2 = gettimeofday_sec() ;
1212    os_ << irow << "th loop elapsed time = " << t2-t1 << "sec" << LogIO::POST ;
1213  }
1214 
1215  double endSec = gettimeofday_sec() ;
1216  os_ << "end MSWriter::fillSysCal() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1217}
1218
1219void MSWriter::addFeed( Int id )
1220{
1221  double startSec = gettimeofday_sec() ;
1222  os_ << "start MSWriter::addFeed() startSec=" << startSec << LogIO::POST ;
1223
1224  // add row
1225  MSFeed msFeed = mstable_->feed() ;
1226  msFeed.addRow( 1, True ) ;
1227  Int nrow = msFeed.nrow() ;
1228
1229  MSFeedColumns msFeedCols( mstable_->feed() ) ;
1230
1231  msFeedCols.feedId().put( nrow-1, id ) ;
1232  msFeedCols.antennaId().put( nrow-1, 0 ) ;
1233
1234  double endSec = gettimeofday_sec() ;
1235  os_ << "end MSWriter::addFeed() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1236}
1237
1238void MSWriter::addSpectralWindow( Int spwid, Int freqid )
1239{
1240  double startSec = gettimeofday_sec() ;
1241  os_ << "start MSWriter::addSpectralWindow() startSec=" << startSec << LogIO::POST ;
1242 
1243  // add row
1244  MSSpectralWindow msSpw = mstable_->spectralWindow() ;
1245  while( (Int)msSpw.nrow() <= spwid ) {
1246    msSpw.addRow( 1, True ) ;
1247  }
1248 
1249  MSSpWindowColumns msSpwCols( msSpw ) ;
1250
1251  STFrequencies stf = table_->frequencies() ;
1252
1253  // MEAS_FREQ_REF
1254  msSpwCols.measFreqRef().put( spwid, stf.getFrame( True ) ) ;
1255
1256  Double refpix ;
1257  Double refval ;
1258  Double inc ;
1259  stf.getEntry( refpix, refval, inc, (uInt)freqid ) ;
1260
1261  // NUM_CHAN
1262  Int nchan = (Int)(refpix * 2) ;
1263  msSpwCols.numChan().put( spwid, nchan ) ;
1264
1265  // TOTAL_BANDWIDTH
1266  Double bw = nchan * inc ;
1267  msSpwCols.totalBandwidth().put( spwid, bw ) ;
1268
1269  // REF_FREQUENCY
1270  Double refFreq = refval - refpix * inc ;
1271  msSpwCols.refFrequency().put( spwid, refFreq ) ;
1272
1273  // NET_SIDEBAND
1274  // tentative: USB->0, LSB->1
1275  Int netSideband = 0 ;
1276  if ( inc < 0 )
1277    netSideband = 1 ;
1278  msSpwCols.netSideband().put( spwid, netSideband ) ;
1279
1280  // RESOLUTION, CHAN_WIDTH, EFFECTIVE_BW
1281  Vector<Double> sharedDoubleArr( nchan, inc ) ;
1282  msSpwCols.resolution().put( spwid, sharedDoubleArr ) ;
1283  msSpwCols.chanWidth().put( spwid, sharedDoubleArr ) ;
1284  msSpwCols.effectiveBW().put( spwid, sharedDoubleArr ) ;
1285
1286  // CHAN_FREQ
1287  indgen( sharedDoubleArr, refFreq, inc ) ;
1288  msSpwCols.chanFreq().put( spwid, sharedDoubleArr ) ;
1289
1290  double endSec = gettimeofday_sec() ;
1291  os_ << "end MSWriter::addSpectralWindow() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1292}
1293
1294void MSWriter::addField( Int fid, String fieldname, String srcname, Double t, Vector<Double> rate )
1295{
1296  double startSec = gettimeofday_sec() ;
1297  os_ << "start MSWriter::addField() startSec=" << startSec << LogIO::POST ;
1298 
1299  MSField msField = mstable_->field() ;
1300  while( (Int)msField.nrow() <= fid ) {
1301    msField.addRow( 1, True ) ;
1302  }
1303  MSFieldColumns msFieldCols( msField ) ;
1304
1305  // Access to SOURCE table
1306  MSSource msSrc = mstable_->source() ;
1307
1308  // fill target row
1309  msFieldCols.name().put( fid, fieldname ) ;
1310  msFieldCols.time().put( fid, t ) ;
1311  Int numPoly = 0 ;
1312  if ( anyNE( rate, 0.0 ) )
1313    numPoly = 1 ;
1314  msFieldCols.numPoly().put( fid, numPoly ) ;
1315  MSSourceIndex msSrcIdx( msSrc ) ;
1316  Int srcId = -1 ;
1317  Vector<Int> srcIdArr = msSrcIdx.matchSourceName( srcname ) ;
1318  if ( srcIdArr.size() != 0 ) {
1319    srcId = srcIdArr[0] ;
1320    MSSource msSrcSel = msSrc( msSrc.col("SOURCE_ID") == srcId ) ;
1321    ROMSSourceColumns msSrcCols( msSrcSel ) ;
1322    Vector<Double> srcDir = msSrcCols.direction()( 0 ) ;
1323    Matrix<Double> srcDirA( IPosition( 2, 2, 1+numPoly ) ) ;
1324    os_ << "srcDirA = " << srcDirA << LogIO::POST ;
1325    os_ << "sliced srcDirA = " << srcDirA.column( 0 ) << LogIO::POST ;
1326    srcDirA.column( 0 ) = srcDir ;
1327    os_ << "srcDirA = " << srcDirA << LogIO::POST ;
1328    if ( numPoly != 0 )
1329      srcDirA.column( 1 ) = rate ;
1330    msFieldCols.phaseDir().put( fid, srcDirA ) ;
1331    msFieldCols.referenceDir().put( fid, srcDirA ) ;
1332    msFieldCols.delayDir().put( fid, srcDirA ) ;
1333  }
1334  msFieldCols.sourceId().put( fid, srcId ) ;
1335
1336  double endSec = gettimeofday_sec() ;
1337  os_ << "end MSWriter::addField() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1338}
1339
1340void MSWriter::addPointing( String &name, Double &me, Double &interval, Matrix<Double> &dir )
1341{
1342  double startSec = gettimeofday_sec() ;
1343  os_ << "start MSWriter::addPointing() startSec=" << startSec << LogIO::POST ;
1344 
1345  // access to POINTING subtable
1346  MSPointing msp = mstable_->pointing() ;
1347  uInt nrow = msp.nrow() ;
1348
1349  // add row
1350  msp.addRow( 1, True ) ;
1351
1352  // fill row
1353  TableRow row( msp ) ;
1354  TableRecord &rec = row.record() ;
1355  RecordFieldPtr<Int> antennaRF( rec, "ANTENNA_ID" ) ;
1356  *antennaRF = 0 ;
1357  RecordFieldPtr<Int> numpolyRF( rec, "NUM_POLY" ) ;
1358  *numpolyRF = dir.ncolumn() ;
1359  RecordFieldPtr<Double> timeRF( rec, "TIME" ) ;
1360  *timeRF = me ;
1361  RecordFieldPtr<Double> toriginRF( rec, "TIME_ORIGIN" ) ;
1362  *toriginRF = me ;
1363  RecordFieldPtr<Double> intervalRF( rec, "INTERVAL" ) ;
1364  *intervalRF = interval ;
1365  RecordFieldPtr<String> nameRF( rec, "NAME" ) ;
1366  *nameRF = name ;
1367  RecordFieldPtr<Bool> trackRF( rec, "TRACKING" ) ;
1368  *trackRF = True ;
1369  RecordFieldPtr< Array<Double> > dirRF( rec, "DIRECTION" ) ;
1370  *dirRF = dir ;
1371  RecordFieldPtr< Array<Double> > targetRF( rec, "TARGET" ) ;
1372  *dirRF = dir ;
1373  row.put( nrow ) ;
1374
1375  double endSec = gettimeofday_sec() ;
1376  os_ << "end MSWriter::addPointing() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1377}
1378
1379Int MSWriter::addPolarization( Vector<Int> polnos )
1380{
1381  double startSec = gettimeofday_sec() ;
1382  os_ << "start MSWriter::addPolarization() startSec=" << startSec << LogIO::POST ;
1383
1384  os_ << "polnos = " << polnos << LogIO::POST ;
1385  MSPolarization msPol = mstable_->polarization() ;
1386  uInt nrow = msPol.nrow() ;
1387
1388  // only 1 POLARIZATION row for 1 scantable
1389  if ( nrow > 0 )
1390    return 0 ;
1391 
1392  Vector<Int> corrType = toCorrType( polnos ) ;
1393 
1394  ROArrayColumn<Int> corrtCol( msPol, "CORR_TYPE" ) ;
1395  Matrix<Int> corrTypeArr = corrtCol.getColumn() ;
1396  Int polid = -1 ;
1397  for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1398    if ( allEQ( corrType, corrTypeArr.column( irow ) ) ) {
1399      polid = irow ;
1400      break ;
1401    }
1402  }
1403
1404  if ( polid == -1 ) {
1405    MSPolarizationColumns msPolCols( msPol ) ;
1406
1407    // add row
1408    msPol.addRow( 1, True ) ;
1409    polid = (Int)nrow ;
1410
1411    // CORR_TYPE
1412    msPolCols.corrType().put( nrow, corrType ) ;
1413
1414    // NUM_CORR
1415    uInt npol = corrType.size() ;
1416    msPolCols.numCorr().put( nrow, npol ) ;
1417
1418    // CORR_PRODUCT
1419    Matrix<Int> corrProd( 2, npol, -1 ) ;
1420    if ( npol == 1 ) {
1421      corrProd = 0 ;
1422    }
1423    else if ( npol == 2 ) {
1424      corrProd.column( 0 ) = 0 ;
1425      corrProd.column( 1 ) = 1 ;
1426    }
1427    else {
1428      corrProd.column( 0 ) = 0 ;
1429      corrProd.column( 3 ) = 1 ;
1430      corrProd( 0,1 ) = 0 ;
1431      corrProd( 1,1 ) = 1 ;
1432      corrProd( 0,2 ) = 1 ;
1433      corrProd( 1,2 ) = 0 ;
1434    }
1435    msPolCols.corrProduct().put( nrow, corrProd ) ;   
1436  }
1437
1438  double endSec = gettimeofday_sec() ;
1439  os_ << "end MSWriter::addPolarization() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1440
1441  return polid ;
1442}
1443
1444Int MSWriter::addDataDescription( Int polid, Int spwid )
1445{
1446  double startSec = gettimeofday_sec() ;
1447  os_ << "start MSWriter::addDataDescription() startSec=" << startSec << LogIO::POST ;
1448
1449  MSDataDescription msDataDesc = mstable_->dataDescription() ;
1450  uInt nrow = msDataDesc.nrow() ;
1451
1452  // only 1 POLARIZATION_ID for 1 scantable
1453  Int ddid = -1 ;
1454  ROScalarColumn<Int> spwCol( msDataDesc, "SPECTRAL_WINDOW_ID" ) ;
1455  Vector<Int> spwIds = spwCol.getColumn() ;
1456  //ROScalarColumn<Int> polCol( msDataDesc, "POLARIZATION_ID" ) ;
1457  //Vector<Int> polIds = polCol.getColumn() ;
1458  for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1459    //if ( spwid == spwIds[irow] && polid == polIds[irow] ) {
1460    if ( spwid == spwIds[irow] ) {
1461      ddid = irow ;
1462      break ;
1463    }
1464  }
1465  os_ << "ddid = " << ddid << LogIO::POST ;
1466 
1467
1468  if ( ddid == -1 ) {
1469    msDataDesc.addRow( 1, True ) ;
1470    MSDataDescColumns msDataDescCols( msDataDesc ) ;
1471    msDataDescCols.polarizationId().put( nrow, polid ) ;
1472    msDataDescCols.spectralWindowId().put( nrow, spwid ) ;
1473    ddid = (Int)nrow ;
1474  }
1475
1476  double endSec = gettimeofday_sec() ;
1477  os_ << "end MSWriter::addDataDescription() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1478
1479  return ddid ;
1480}
1481
1482Int MSWriter::addState( Int st, Int &subscan )
1483{
1484  double startSec = gettimeofday_sec() ;
1485  os_ << "start MSWriter::addState() startSec=" << startSec << LogIO::POST ;
1486
1487  // access to STATE subtable
1488  MSState msState = mstable_->state() ;
1489  uInt nrow = msState.nrow() ;
1490
1491  String obsMode ;
1492  Bool isSignal ;
1493  Double tnoise ;
1494  Double tload ;
1495  queryType( st, obsMode, isSignal, tnoise, tload ) ;
1496  os_ << "obsMode = " << obsMode << " isSignal = " << isSignal << LogIO::POST ;
1497
1498  Int idx = -1 ;
1499  ROScalarColumn<String> obsModeCol( msState, "OBS_MODE" ) ;
1500  //ROScalarColumn<Bool> sigCol( msState, "SIG" ) ;
1501  //ROScalarColumn<Bool> refCol( msState, "REF" ) ;
1502  ROScalarColumn<Int> subscanCol( msState, "SUB_SCAN" ) ;
1503//   Vector<String> obsModeArr = obsModeCol.getColumn() ;
1504//   Vector<Bool> sigArr = sigCol.getColumn() ;
1505//   Vector<Bool> refArr = refCol.getColumn() ;
1506//   Vector<Int> subscanArr = subscanCol.getColumn() ;
1507  for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1508    if ( obsModeCol(irow) == obsMode
1509         //&& sigCol(irow) == isSignal
1510         //&& refCol(irow) != isSignal
1511         && subscanCol(irow) == subscan ) {
1512//     if ( obsModeArr[irow] == obsMode
1513//          && sigArr[irow] == isSignal
1514//          && refArr[irow] != isSignal
1515//          && subscanArr[irow] == subscan ) {
1516      idx = irow ;
1517      break ;
1518    }
1519  }
1520  if ( idx == -1 ) {
1521    msState.addRow( 1, True ) ;
1522    TableRow row( msState ) ;
1523    TableRecord &rec = row.record() ;
1524    RecordFieldPtr<String> obsmodeRF( rec, "OBS_MODE" ) ;
1525    *obsmodeRF = obsMode ;
1526    RecordFieldPtr<Bool> sigRF( rec, "SIG" ) ;
1527    *sigRF = isSignal ;
1528    RecordFieldPtr<Bool> refRF( rec, "REF" ) ;
1529    *refRF = !isSignal ;
1530    RecordFieldPtr<Int> subscanRF( rec, "SUB_SCAN" ) ;
1531    *subscanRF = subscan ;
1532    RecordFieldPtr<Double> noiseRF( rec, "CAL" ) ;
1533    *noiseRF = tnoise ;
1534    RecordFieldPtr<Double> loadRF( rec, "LOAD" ) ;
1535    *loadRF = tload ;
1536    row.put( nrow ) ;
1537//     ScalarColumn<String> obsModeCol( msState, "OBS_MODE" ) ;
1538//     obsModeCol.put( nrow, obsMode ) ;
1539//     ScalarColumn<Bool> sharedBCol( msState, "SIG" ) ;
1540//     sharedBCol.put( nrow, isSignal ) ;
1541//     sharedBCol.attach( msState, "REF" ) ;
1542//     sharedBCol.put( nrow, !isSignal ) ;
1543//     ScalarColumn<Int> subscanCol( msState, "SUB_SCAN" ) ;
1544//     subscanCol.put( nrow, subscan ) ;
1545    idx = nrow ;
1546  }
1547  subscan++ ;
1548
1549  double endSec = gettimeofday_sec() ;
1550  os_ << "end MSWriter::addState() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1551
1552  return idx ;
1553}
1554
1555Vector<Int> MSWriter::toCorrType( Vector<Int> polnos )
1556{
1557  double startSec = gettimeofday_sec() ;
1558  os_ << "start MSWriter::toCorrType() startSec=" << startSec << LogIO::POST ;
1559
1560  uInt npol = polnos.size() ;
1561  Vector<Int> corrType( npol, Stokes::Undefined ) ;
1562 
1563  if ( npol == 4 ) {
1564    if ( polType_ == "linear" ) {
1565      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1566        if ( polnos[ipol] == 0 )
1567          corrType[ipol] = Stokes::XX ;
1568        else if ( polnos[ipol] == 1 )
1569          corrType[ipol] = Stokes::XY ;
1570        else if ( polnos[ipol] == 2 )
1571          corrType[ipol] = Stokes::YX ;
1572        else if ( polnos[ipol] == 3 )
1573          corrType[ipol] = Stokes::YY ;
1574      }
1575    }
1576    else if ( polType_ == "circular" ) {
1577      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1578        if ( polnos[ipol] == 0 )
1579          corrType[ipol] = Stokes::RR ;
1580        else if ( polnos[ipol] == 1 )
1581          corrType[ipol] = Stokes::RL ;
1582        else if ( polnos[ipol] == 2 )
1583          corrType[ipol] = Stokes::LR ;
1584        else if ( polnos[ipol] == 3 )
1585          corrType[ipol] = Stokes::LL ;
1586      }
1587    }
1588    else if ( polType_ == "stokes" ) {
1589      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1590        if ( polnos[ipol] == 0 )
1591          corrType[ipol] = Stokes::I ;
1592        else if ( polnos[ipol] == 1 )
1593          corrType[ipol] = Stokes::Q ;
1594        else if ( polnos[ipol] == 2 )
1595          corrType[ipol] = Stokes::U ;
1596        else if ( polnos[ipol] == 3 )
1597          corrType[ipol] = Stokes::V ;
1598      }
1599    }
1600  }
1601  else if ( npol == 2 ) {
1602    if ( polType_ == "linear" ) {
1603      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1604        if ( polnos[ipol] == 0 )
1605          corrType[ipol] = Stokes::XX ;
1606        else if ( polnos[ipol] == 1 )
1607          corrType[ipol] = Stokes::YY ;
1608      }
1609    }
1610    else if ( polType_ == "circular" ) {
1611      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1612        if ( polnos[ipol] == 0 )
1613          corrType[ipol] = Stokes::RR ;
1614        else if ( polnos[ipol] == 1 )
1615          corrType[ipol] = Stokes::LL ;
1616      }
1617    }
1618    else if ( polType_ == "stokes" ) {
1619      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1620        if ( polnos[ipol] == 0 )
1621          corrType[ipol] = Stokes::I ;
1622        else if ( polnos[ipol] == 1 )
1623          corrType[ipol] = Stokes::V ;
1624      }
1625    }
1626    else if ( polType_ == "linpol" ) {
1627      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1628        if ( polnos[ipol] == 1 )
1629          corrType[ipol] = Stokes::Plinear ;
1630        else if ( polnos[ipol] == 2 )
1631          corrType[ipol] = Stokes::Pangle ;
1632      }
1633    }
1634  }     
1635  else if ( npol == 1 ) {
1636    if ( polType_ == "linear" )
1637      corrType[0] = Stokes::XX ;
1638    else if ( polType_ == "circular" )
1639      corrType[0] = Stokes::RR ;
1640    else if ( polType_ == "stokes" )
1641      corrType[0] = Stokes::I ;
1642  }
1643
1644  double endSec = gettimeofday_sec() ;
1645  os_ << "end MSWriter::toCorrType() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1646
1647  return corrType ;
1648}
1649
1650void MSWriter::getValidTimeRange( Double &me, Double &interval, Table &tab )
1651{
1652  double startSec = gettimeofday_sec() ;
1653  os_ << "start MSWriter::getVaridTimeRange() startSec=" << startSec << LogIO::POST ;
1654
1655  // sort table
1656  //Table stab = tab.sort( "TIME" ) ;
1657
1658  ROScalarColumn<Double> timeCol( tab, "TIME" ) ;
1659  Vector<Double> timeArr = timeCol.getColumn() ;
1660  Double minTime ;
1661  Double maxTime ;
1662  minMax( minTime, maxTime, timeArr ) ;
1663  Double midTime = 0.5 * ( minTime + maxTime ) * 86400.0 ;
1664  // unit for TIME
1665  // Scantable: "d"
1666  // MS: "s"
1667  me = midTime ;
1668  interval = ( maxTime - minTime ) * 86400.0 ;
1669
1670  double endSec = gettimeofday_sec() ;
1671  os_ << "end MSWriter::getValidTimeRange() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1672}
1673
1674void MSWriter::getValidTimeRange( Double &me, Double &interval, Vector<Double> &atime, Vector<Double> &ainterval )
1675{
1676  double startSec = gettimeofday_sec() ;
1677  os_ << "start MSWriter::getVaridTimeRange() startSec=" << startSec << LogIO::POST ;
1678
1679  // sort table
1680  //Table stab = tab.sort( "TIME" ) ;
1681
1682  Double minTime ;
1683  Double maxTime ;
1684  minMax( minTime, maxTime, atime ) ;
1685  Double midTime = 0.5 * ( minTime + maxTime ) * 86400.0 ;
1686  // unit for TIME
1687  // Scantable: "d"
1688  // MS: "s"
1689  me = midTime ;
1690  interval = ( maxTime - minTime ) * 86400.0 ;
1691
1692  double endSec = gettimeofday_sec() ;
1693  os_ << "end MSWriter::getValidTimeRange() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1694}
1695
1696//void MSWriter::queryType( Int type, String &stype, Bool &b )
1697void MSWriter::queryType( Int type, String &stype, Bool &b, Double &t, Double &l )
1698{
1699  double startSec = gettimeofday_sec() ;
1700  os_ << "start MSWriter::queryType() startSec=" << startSec << LogIO::POST ;
1701
1702  switch ( type ) {
1703  case SrcType::PSON:
1704    stype = "POSITION_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1705    b = True ;
1706    t = 0.0 ;
1707    l = 0.0 ;
1708    break ;
1709  case SrcType::PSOFF:
1710    stype = "POSITION_SWITCH.OBSERVE_TARGET.OFF_SOURCE" ;
1711    b = False ;
1712    t = 0.0 ;
1713    l = 0.0 ;
1714    break ;
1715  case SrcType::NOD:
1716    stype = "NOD.OBSERVE_TARGET.ON_SOURCE" ;
1717    b = True ;
1718    t = 0.0 ;
1719    l = 0.0 ;
1720    break ;
1721  case SrcType::FSON:
1722    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1723    b = True ;
1724    t = 0.0 ;
1725    l = 0.0 ;
1726    break ;
1727  case SrcType::FSOFF:
1728    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1729    b = False ;
1730    t = 0.0 ;
1731    l = 0.0 ;
1732    break ;
1733  case SrcType::SKY:
1734    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1735    b = False ;
1736    t = 0.0 ;
1737    l = 1.0 ;
1738    break ;
1739  case SrcType::HOT:
1740    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1741    b = False ;
1742    t = 0.0 ;
1743    l = 1.0 ;
1744    break ;
1745  case SrcType::WARM:
1746    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1747    t = 0.0 ;
1748    b = False ;
1749    l = 1.0 ;
1750    break ;
1751  case SrcType::COLD:
1752    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1753    b = False ;
1754    t = 0.0 ;
1755    l = 1.0 ;
1756    break ;
1757  case SrcType::PONCAL:
1758    stype = "POSITION_SWITCH.CALIBRATE_TEMPERATURE.ON_SOURCE" ;
1759    b = True ;
1760    t = 1.0 ;
1761    l = 0.0 ;
1762    break ;
1763  case SrcType::POFFCAL:
1764    stype = "POSITION_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1765    b = False ;
1766    t = 1.0 ;
1767    l = 0.0 ;
1768    break ;
1769  case SrcType::NODCAL:
1770    stype = "NOD.CALIBRATE_TEMPERATURE.ON_SOURCE" ;
1771    b = True ;
1772    t = 1.0 ;
1773    l = 0.0 ;
1774    break ;
1775  case SrcType::FONCAL:
1776    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.ON_SOURCE" ;
1777    b = True ;
1778    t = 1.0 ;
1779    l = 0.0 ;
1780    break ;
1781  case SrcType::FOFFCAL:
1782    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1783    b = False ;
1784    t = 1.0 ;
1785    l = 0.0 ;
1786    break ;
1787  case SrcType::FSLO:
1788    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1789    b = True ;
1790    t = 0.0 ;
1791    l = 0.0 ;
1792    break ;
1793  case SrcType::FLOOFF:
1794    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.OFF_SOURCE" ;
1795    b = False ;
1796    t = 0.0 ;
1797    l = 0.0 ;
1798    break ;
1799  case SrcType::FLOSKY:
1800    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1801    b = False ;
1802    t = 0.0 ;
1803    l = 1.0 ;
1804    break ;
1805  case SrcType::FLOHOT:
1806    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1807    b = False ;
1808    t = 0.0 ;
1809    l = 1.0 ;
1810    break ;
1811  case SrcType::FLOWARM:
1812    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1813    b = False ;
1814    t = 0.0 ;
1815    l = 1.0 ;
1816    break ;
1817  case SrcType::FLOCOLD:
1818    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1819    b = False ;
1820    t = 0.0 ;
1821    l = 1.0 ;
1822    break ;
1823  case SrcType::FSHI:
1824    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1825    b = True ;
1826    t = 0.0 ;
1827    l = 0.0 ;
1828    break ;
1829  case SrcType::FHIOFF:
1830    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1831    b = False ;
1832    t = 0.0 ;
1833    l = 0.0 ;
1834    break ;
1835  case SrcType::FHISKY:
1836    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1837    b = False ;
1838    t = 0.0 ;
1839    l = 1.0 ;
1840    break ;
1841  case SrcType::FHIHOT:
1842    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1843    b = False ;
1844    t = 0.0 ;
1845    l = 1.0 ;
1846    break ;
1847  case SrcType::FHIWARM:
1848    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1849    b = False ;
1850    t = 0.0 ;
1851    l = 1.0 ;
1852    break ;
1853  case SrcType::FHICOLD:
1854    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1855    b = False ;
1856    t = 0.0 ;
1857    l = 1.0 ;
1858    break ;
1859  case SrcType::SIG:
1860    stype = "UNSPECIFIED.OBSERVE_TARGET.ON_SOURCE" ;
1861    b = True ;
1862    t = 0.0 ;
1863    l = 0.0 ;
1864    break ;
1865  case SrcType::REF:
1866    stype = "UNSPECIFIED.OBSERVE_TARGET.ON_SOURCE" ;
1867    b = False ;
1868    t = 0.0 ;
1869    l = 0.0 ;
1870    break ;
1871  default:
1872    stype = "UNSPECIFIED" ;
1873    b = True ;
1874    t = 0.0 ;
1875    l = 0.0 ;
1876    break ;
1877  }
1878
1879  double endSec = gettimeofday_sec() ;
1880  os_ << "end MSWriter::queryType() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1881}
1882}
Note: See TracBrowser for help on using the repository browser.