source: trunk/src/MSWriter.cpp @ 2020

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

New Development: No

JIRA Issue: Yes CAS-2718

Ready for Test: Yes/No?

Interface Changes: Yes/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 on FEED subtable.

File size: 61.1 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  RecordFieldPtr< Array<Bool> > flagcatRF( trec, "FLAG_CATEGORY" ) ;
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              //*flagcatRF = Cube<Bool>( nrow, nchan, 1, False ) ;
458              flagcatRF.define( Cube<Bool>( nrow, nchan, 1, False ) ) ;
459             
460              // add row
461              mstable_->addRow( 1, True ) ;
462              row.put( mstable_->nrow()-1 ) ;
463             
464              iter5.next() ;
465            }
466           
467            iter4.next() ;
468          }
469
470          // add SPECTRAL_WINDOW row
471          if ( allNE( processedFreqId, freqId ) ) {
472            uInt vsize = processedFreqId.size() ;
473            processedFreqId.resize( vsize+1, True ) ;
474            processedFreqId[vsize] = freqId ;
475            addSpectralWindow( ifNo, freqId ) ;
476          }
477         
478          iter3.next() ;
479        }
480       
481        iter2.next() ;
482      }
483     
484      // add FEED row
485      addFeed( beamNo ) ;
486     
487      iter1.next() ;
488    }
489   
490    // add FIELD row
491    addField( fieldId, fieldName, srcName, minTime, scanRate ) ;
492
493    iter0.next() ;
494  }
495
496//   delete tpoolr ;
497  delete dataRF ;
498
499  // SYSCAL
500  if ( isTcal_ )
501    fillSysCal() ;
502
503  // replace POINTING table with original one if exists
504  if ( ptTabName_ != "" ) {
505    delete mstable_ ;
506    mstable_ = 0 ;
507    Table newPtTab( ptTabName_, Table::Old ) ;
508    newPtTab.copy( filename_+"/POINTING", Table::New ) ;
509  }
510
511  double endSec = gettimeofday_sec() ;
512  os_ << "end MSWriter::write() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
513
514  return True ;
515}
516 
517void MSWriter::init()
518{
519//   os_.origin( LogOrigin( "MSWriter", "init()", WHERE ) ) ;
520  double startSec = gettimeofday_sec() ;
521  os_ << "start MSWriter::init() startSec=" << startSec << LogIO::POST ;
522 
523  // access to scantable
524  header_ = table_->getHeader() ;
525
526  // FLOAT_DATA? or DATA?
527  if ( header_.npol > 2 ) {
528    useFloatData_ = False ;
529    useData_ = True ;
530  }
531  else {
532    useFloatData_ = True ;
533    useData_ = False ;
534  }
535
536  // polarization type
537  polType_ = header_.poltype ;
538  if ( polType_ == "" )
539    polType_ = "stokes" ;
540  else if ( polType_.find( "linear" ) != String::npos )
541    polType_ = "linear" ;
542  else if ( polType_.find( "circular" ) != String::npos )
543    polType_ = "circular" ;
544  else if ( polType_.find( "stokes" ) != String::npos )
545    polType_ = "stokes" ;
546  else if ( polType_.find( "linpol" ) != String::npos )
547    polType_ = "linpol" ;
548  else
549    polType_ = "notype" ;
550
551  // Check if some subtables are exists
552  if ( table_->tcal().table().nrow() != 0 ) {
553    ROTableColumn col( table_->tcal().table(), "TCAL" ) ;
554    if ( col.isDefined( 0 ) ) {
555      os_ << "nrow=" << table_->tcal().table().nrow() << ": TCAL table exists" << LogIO::POST ;
556      isTcal_ = True ;
557    }
558    else {
559      os_ << "no TCAL rows" << LogIO::POST ;
560    }
561  }
562  else {
563    os_ << "no TCAL rows" << LogIO::POST ;
564  }
565  if ( table_->weather().table().nrow() != 0 ) {
566    ROTableColumn col( table_->weather().table(), "TEMPERATURE" ) ;
567    if ( col.isDefined( 0 ) ) {
568      os_ << "nrow=" << table_->weather().table().nrow() << ": WEATHER table exists" << LogIO::POST ;
569      isWeather_ =True ;
570    }
571    else {
572      os_ << "no WEATHER rows" << LogIO::POST ;
573    }
574  }
575  else {
576    os_ << "no WEATHER rows" << LogIO::POST ;
577  }
578
579  // Are TCAL_SPECTRUM and TSYS_SPECTRUM necessary?
580  if ( isTcal_ && header_.nchan != 1 ) {
581    // examine TCAL subtable
582    Table tcaltab = table_->tcal().table() ;
583    ROArrayColumn<Float> tcalCol( tcaltab, "TCAL" ) ;
584    for ( uInt irow = 0 ; irow < tcaltab.nrow() ; irow++ ) {
585      if ( tcalCol( irow ).size() != 1 )
586        tcalSpec_ = True ;
587    }
588    // examine spectral data
589    TableIterator iter0( table_->table(), "IFNO" ) ;
590    while( !iter0.pastEnd() ) {
591      Table t0( iter0.table() ) ;
592      ROArrayColumn<Float> sharedFloatArrCol( t0, "SPECTRA" ) ;
593      uInt len = sharedFloatArrCol( 0 ).size() ;
594      if ( len != 1 ) {
595        sharedFloatArrCol.attach( t0, "TSYS" ) ;
596        if ( sharedFloatArrCol( 0 ).size() != 1 )
597          tsysSpec_ = True ;
598      }
599      iter0.next() ;
600    }
601  }
602
603  // check if reference for POINTING table exists
604  const TableRecord &rec = table_->table().keywordSet() ;
605  if ( rec.isDefined( "POINTING" ) ) {
606    ptTabName_ = rec.asString( "POINTING" ) ;
607    if ( !Table::isReadable( ptTabName_ ) ) {
608      ptTabName_ = "" ;
609    }
610  }
611
612  double endSec = gettimeofday_sec() ;
613  os_ << "end MSWriter::init() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
614}
615
616void MSWriter::setupMS()
617{
618//   os_.origin( LogOrigin( "MSWriter", "setupMS()", WHERE ) ) ;
619  double startSec = gettimeofday_sec() ;
620  os_ << "start MSWriter::setupMS() startSec=" << startSec << LogIO::POST ;
621
622  TableDesc msDesc = MeasurementSet::requiredTableDesc() ;
623  if ( useFloatData_ )
624    MeasurementSet::addColumnToDesc( msDesc, MSMainEnums::FLOAT_DATA, 2 ) ;
625  else if ( useData_ )
626    MeasurementSet::addColumnToDesc( msDesc, MSMainEnums::DATA, 2 ) ;
627
628  SetupNewTable newtab( filename_, msDesc, Table::New ) ;
629
630  mstable_ = new MeasurementSet( newtab ) ;
631
632  // create subtables
633  TableDesc antennaDesc = MSAntenna::requiredTableDesc() ;
634  SetupNewTable antennaTab( mstable_->antennaTableName(), antennaDesc, Table::New ) ;
635  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::ANTENNA ), Table( antennaTab ) ) ;
636
637  TableDesc dataDescDesc = MSDataDescription::requiredTableDesc() ;
638  SetupNewTable dataDescTab( mstable_->dataDescriptionTableName(), dataDescDesc, Table::New ) ;
639  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::DATA_DESCRIPTION ), Table( dataDescTab ) ) ;
640
641  TableDesc dopplerDesc = MSDoppler::requiredTableDesc() ;
642  SetupNewTable dopplerTab( mstable_->dopplerTableName(), dopplerDesc, Table::New ) ;
643  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::DOPPLER ), Table( dopplerTab ) ) ;
644
645  TableDesc feedDesc = MSFeed::requiredTableDesc() ;
646  SetupNewTable feedTab( mstable_->feedTableName(), feedDesc, Table::New ) ;
647  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FEED ), Table( feedTab ) ) ;
648
649  TableDesc fieldDesc = MSField::requiredTableDesc() ;
650  SetupNewTable fieldTab( mstable_->fieldTableName(), fieldDesc, Table::New ) ;
651  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FIELD ), Table( fieldTab ) ) ;
652
653  TableDesc flagCmdDesc = MSFlagCmd::requiredTableDesc() ;
654  SetupNewTable flagCmdTab( mstable_->flagCmdTableName(), flagCmdDesc, Table::New ) ;
655  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FLAG_CMD ), Table( flagCmdTab ) ) ;
656
657  TableDesc freqOffsetDesc = MSFreqOffset::requiredTableDesc() ;
658  SetupNewTable freqOffsetTab( mstable_->freqOffsetTableName(), freqOffsetDesc, Table::New ) ;
659  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FREQ_OFFSET ), Table( freqOffsetTab ) ) ;
660
661  TableDesc historyDesc = MSHistory::requiredTableDesc() ;
662  SetupNewTable historyTab( mstable_->historyTableName(), historyDesc, Table::New ) ;
663  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::HISTORY ), Table( historyTab ) ) ;
664
665  TableDesc observationDesc = MSObservation::requiredTableDesc() ;
666  SetupNewTable observationTab( mstable_->observationTableName(), observationDesc, Table::New ) ;
667  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::OBSERVATION ), Table( observationTab ) ) ;
668
669  TableDesc pointingDesc = MSPointing::requiredTableDesc() ;
670  SetupNewTable pointingTab( mstable_->pointingTableName(), pointingDesc, Table::New ) ;
671  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::POINTING ), Table( pointingTab ) ) ;
672
673  TableDesc polarizationDesc = MSPolarization::requiredTableDesc() ;
674  SetupNewTable polarizationTab( mstable_->polarizationTableName(), polarizationDesc, Table::New ) ;
675  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::POLARIZATION ), Table( polarizationTab ) ) ;
676
677  TableDesc processorDesc = MSProcessor::requiredTableDesc() ;
678  SetupNewTable processorTab( mstable_->processorTableName(), processorDesc, Table::New ) ;
679  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::PROCESSOR ), Table( processorTab ) ) ;
680
681  TableDesc sourceDesc = MSSource::requiredTableDesc() ;
682  MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::TRANSITION, 1 ) ;
683  MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::REST_FREQUENCY, 1 ) ;
684  MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::SYSVEL, 1 ) ;
685  SetupNewTable sourceTab( mstable_->sourceTableName(), sourceDesc, Table::New ) ;
686  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SOURCE ), Table( sourceTab ) ) ;
687
688  TableDesc spwDesc = MSSpectralWindow::requiredTableDesc() ;
689  SetupNewTable spwTab( mstable_->spectralWindowTableName(), spwDesc, Table::New ) ;
690  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SPECTRAL_WINDOW ), Table( spwTab ) ) ;
691
692  TableDesc stateDesc = MSState::requiredTableDesc() ;
693  SetupNewTable stateTab( mstable_->stateTableName(), stateDesc, Table::New ) ;
694  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::STATE ), Table( stateTab ) ) ;
695
696  // TODO: add TCAL_SPECTRUM and TSYS_SPECTRUM if necessary
697  TableDesc sysCalDesc = MSSysCal::requiredTableDesc() ;
698  MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TCAL, 2 ) ;
699  MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TSYS, 2 ) ;
700  if ( tcalSpec_ )
701    MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TCAL_SPECTRUM, 2 ) ;
702  if ( tsysSpec_ )
703    MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TSYS_SPECTRUM, 2 ) ;
704  SetupNewTable sysCalTab( mstable_->sysCalTableName(), sysCalDesc, Table::New ) ;
705  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SYSCAL ), Table( sysCalTab ) ) ;
706
707  TableDesc weatherDesc = MSWeather::requiredTableDesc() ;
708  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::TEMPERATURE ) ;
709  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::PRESSURE ) ;
710  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::REL_HUMIDITY ) ;
711  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::WIND_SPEED ) ;
712  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::WIND_DIRECTION ) ;
713  SetupNewTable weatherTab( mstable_->weatherTableName(), weatherDesc, Table::New ) ;
714  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::WEATHER ), Table( weatherTab ) ) ;
715
716  mstable_->initRefs() ;
717
718  double endSec = gettimeofday_sec() ;
719  os_ << "end MSWriter::setupMS() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
720}
721
722void MSWriter::fillObservation()
723{
724  double startSec = gettimeofday_sec() ;
725  os_ << "start MSWriter::fillObservation() startSec=" << startSec << LogIO::POST ;
726
727  // only 1 row
728  mstable_->observation().addRow( 1, True ) ;
729  MSObservationColumns msObsCols( mstable_->observation() ) ;
730  msObsCols.observer().put( 0, header_.observer ) ;
731  // tentatively put antennaname (from ANTENNA subtable)
732  String hAntennaName = header_.antennaname ;
733  String::size_type pos = hAntennaName.find( "//" ) ;
734  String telescopeName ;
735  if ( pos != String::npos ) {
736    telescopeName = hAntennaName.substr( 0, pos ) ;
737  }
738  else {
739    pos = hAntennaName.find( "@" ) ;
740    telescopeName = hAntennaName.substr( 0, pos ) ;
741  }
742  os_ << "telescopeName = " << telescopeName << LogIO::POST ;
743  msObsCols.telescopeName().put( 0, telescopeName ) ;
744  msObsCols.project().put( 0, header_.project ) ;
745  //ScalarMeasColumn<MEpoch> timeCol( table_->table().sort("TIME"), "TIME" ) ;
746  Table sortedtable = table_->table().sort("TIME") ;
747  ScalarMeasColumn<MEpoch> timeCol( sortedtable, "TIME" ) ;
748  Vector<MEpoch> trange( 2 ) ;
749  trange[0] = timeCol( 0 ) ;
750  trange[1] = timeCol( table_->nrow()-1 ) ;
751  msObsCols.timeRangeMeas().put( 0, trange ) ;
752
753  double endSec = gettimeofday_sec() ;
754  os_ << "end MSWriter::fillObservation() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
755}
756
757void MSWriter::fillAntenna()
758{
759  double startSec = gettimeofday_sec() ;
760  os_ << "start MSWriter::fillAntenna() startSec=" << startSec << LogIO::POST ;
761
762  // only 1 row
763  mstable_->antenna().addRow( 1, True ) ;
764  MSAntennaColumns msAntCols( mstable_->antenna() ) ;
765
766  String hAntennaName = header_.antennaname ;
767  String::size_type pos = hAntennaName.find( "//" ) ;
768  String antennaName ;
769  String stationName ;
770  if ( pos != String::npos ) {
771    hAntennaName = hAntennaName.substr( pos+2 ) ;
772  }
773  pos = hAntennaName.find( "@" ) ;
774  if ( pos != String::npos ) {
775    antennaName = hAntennaName.substr( 0, pos ) ;
776    stationName = hAntennaName.substr( pos+1 ) ;
777  }
778  else {
779    antennaName = hAntennaName ;
780    stationName = hAntennaName ;
781  }
782  os_ << "antennaName = " << antennaName << LogIO::POST ;
783  os_ << "stationName = " << stationName << LogIO::POST ;
784 
785  msAntCols.name().put( 0, antennaName ) ;
786  msAntCols.station().put( 0, stationName ) ;
787
788  os_ << "antennaPosition = " << header_.antennaposition << LogIO::POST ;
789 
790  msAntCols.position().put( 0, header_.antennaposition ) ;
791
792  double endSec = gettimeofday_sec() ;
793  os_ << "end MSWriter::fillAntenna() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
794}
795
796void MSWriter::fillProcessor()
797{
798  double startSec = gettimeofday_sec() ;
799  os_ << "start MSWriter::fillProcessor() startSec=" << startSec << LogIO::POST ;
800 
801  // only add empty 1 row
802  MSProcessor msProc = mstable_->processor() ;
803  msProc.addRow( 1, True ) ;
804
805  double endSec = gettimeofday_sec() ;
806  os_ << "end MSWriter::fillProcessor() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
807}
808
809void MSWriter::fillSource()
810{
811  double startSec = gettimeofday_sec() ;
812  os_ << "start MSWriter::fillSource() startSec=" << startSec << LogIO::POST ;
813 
814  // access to MS SOURCE subtable
815  MSSource msSrc = mstable_->source() ;
816
817  // access to MOLECULE subtable
818  STMolecules stm = table_->molecules() ;
819
820  Int srcId = 0 ;
821  Vector<Double> restFreq ;
822  Vector<String> molName ;
823  Vector<String> fMolName ;
824
825  // row based
826  TableRow row( msSrc ) ;
827  TableRecord &rec = row.record() ;
828  RecordFieldPtr<Int> srcidRF( rec, "SOURCE_ID" ) ;
829  RecordFieldPtr<String> nameRF( rec, "NAME" ) ;
830  RecordFieldPtr< Array<Double> > srcpmRF( rec, "PROPER_MOTION" ) ;
831  RecordFieldPtr< Array<Double> > srcdirRF( rec, "DIRECTION" ) ;
832  RecordFieldPtr<Int> numlineRF( rec, "NUM_LINES" ) ;
833  RecordFieldPtr< Array<Double> > restfreqRF( rec, "REST_FREQUENCY" ) ;
834  RecordFieldPtr< Array<Double> > sysvelRF( rec, "SYSVEL" ) ;
835  RecordFieldPtr< Array<String> > transitionRF( rec, "TRANSITION" ) ;
836  RecordFieldPtr<Double> timeRF( rec, "TIME" ) ;
837  RecordFieldPtr<Double> intervalRF( rec, "INTERVAL" ) ;
838  RecordFieldPtr<Int> spwidRF( rec, "SPECTRAL_WINDOW_ID" ) ;
839
840  //
841  // ITERATION: SRCNAME
842  //
843  TableIterator iter0( table_->table(), "SRCNAME" ) ;
844  while( !iter0.pastEnd() ) {
845    //Table t0( iter0.table() ) ;
846    Table t0 =  iter0.table()  ;
847
848    // get necessary information
849    ROScalarColumn<String> srcNameCol( t0, "SRCNAME" ) ;
850    String srcName = srcNameCol( 0 ) ;
851    ROArrayColumn<Double> sharedDArrRCol( t0, "SRCPROPERMOTION" ) ;
852    Vector<Double> srcPM = sharedDArrRCol( 0 ) ;
853    sharedDArrRCol.attach( t0, "SRCDIRECTION" ) ;
854    Vector<Double> srcDir = sharedDArrRCol( 0 ) ;
855    ROScalarColumn<Double> srcVelCol( t0, "SRCVELOCITY" ) ;
856    Double srcVel = srcVelCol( 0 ) ;
857
858    // NAME
859    *nameRF = srcName ;
860
861    // SOURCE_ID
862    *srcidRF = srcId ;
863
864    // PROPER_MOTION
865    *srcpmRF = srcPM ;
866   
867    // DIRECTION
868    *srcdirRF = srcDir ;
869
870    //
871    // ITERATION: MOLECULE_ID
872    //
873    TableIterator iter1( t0, "MOLECULE_ID" ) ;
874    while( !iter1.pastEnd() ) {
875      //Table t1( iter1.table() ) ;
876      Table t1 = iter1.table() ;
877
878      // get necessary information
879      ROScalarColumn<uInt> molIdCol( t1, "MOLECULE_ID" ) ;
880      uInt molId = molIdCol( 0 ) ;
881      stm.getEntry( restFreq, molName, fMolName, molId ) ;
882
883      uInt numFreq = restFreq.size() ;
884     
885      // NUM_LINES
886      *numlineRF = numFreq ;
887
888      // REST_FREQUENCY
889      *restfreqRF = restFreq ;
890
891      // TRANSITION
892      //*transitionRF = fMolName ;
893      Vector<String> transition ;
894      if ( fMolName.size() != 0 ) {
895        transition = fMolName ;
896      }
897      else if ( molName.size() != 0 ) {
898        transition = molName ;
899      }
900      else {
901        transition.resize( numFreq ) ;
902        transition = "" ;
903      }
904      *transitionRF = transition ;
905
906      // SYSVEL
907      Vector<Double> sysvelArr( numFreq, srcVel ) ;
908      *sysvelRF = sysvelArr ;
909
910      //
911      // ITERATION: IFNO
912      //
913      TableIterator iter2( t1, "IFNO" ) ;
914      while( !iter2.pastEnd() ) {
915        //Table t2( iter2.table() ) ;
916        Table t2 = iter2.table() ;
917        uInt nrow = msSrc.nrow() ;
918
919        // get necessary information
920        ROScalarColumn<uInt> ifNoCol( t2, "IFNO" ) ;
921        uInt ifno = ifNoCol( 0 ) ; // IFNO = SPECTRAL_WINDOW_ID
922        Double midTime ;
923        Double interval ;
924        getValidTimeRange( midTime, interval, t2 ) ;
925
926        // fill SPECTRAL_WINDOW_ID
927        *spwidRF = ifno ;
928
929        // fill TIME, INTERVAL
930        *timeRF = midTime ;
931        *intervalRF = interval ;
932
933        // add row
934        msSrc.addRow( 1, True ) ;
935        row.put( nrow ) ;
936
937        iter2.next() ;
938      }
939
940      iter1.next() ;
941    }
942
943    // increment srcId if SRCNAME changed
944    srcId++ ;
945
946    iter0.next() ;
947  }
948
949  double endSec = gettimeofday_sec() ;
950  os_ << "end MSWriter::fillSource() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
951}
952
953void MSWriter::fillWeather()
954{
955  double startSec = gettimeofday_sec() ;
956  os_ << "start MSWriter::fillWeather() startSec=" << startSec << LogIO::POST ;
957
958  // access to MS WEATHER subtable
959  MSWeather msw = mstable_->weather() ;
960
961  // access to WEATHER subtable
962  Table stw = table_->weather().table() ;
963  uInt nrow = stw.nrow() ;
964
965  if ( nrow == 0 )
966    return ;
967
968  msw.addRow( nrow, True ) ;
969  MSWeatherColumns mswCols( msw ) ;
970
971  // ANTENNA_ID is always 0
972  Vector<Int> antIdArr( nrow, 0 ) ;
973  mswCols.antennaId().putColumn( antIdArr ) ;
974
975  // fill weather status
976  ROScalarColumn<Float> sharedFloatCol( stw, "TEMPERATURE" ) ;
977  mswCols.temperature().putColumn( sharedFloatCol ) ;
978  sharedFloatCol.attach( stw, "PRESSURE" ) ;
979  mswCols.pressure().putColumn( sharedFloatCol ) ;
980  sharedFloatCol.attach( stw, "HUMIDITY" ) ;
981  mswCols.relHumidity().putColumn( sharedFloatCol ) ;
982  sharedFloatCol.attach( stw, "WINDSPEED" ) ;
983  mswCols.windSpeed().putColumn( sharedFloatCol ) ;
984  sharedFloatCol.attach( stw, "WINDAZ" ) ;
985  mswCols.windDirection().putColumn( sharedFloatCol ) ;
986
987  // fill TIME and INTERVAL
988  Double midTime ;
989  Double interval ;
990  Vector<Double> intervalArr( nrow, 0.0 ) ;
991  TableIterator iter( table_->table(), "WEATHER_ID" ) ;
992  while( !iter.pastEnd() ) {
993    //Table tab( iter.table() ) ;
994    Table tab = iter.table() ;
995
996    ROScalarColumn<uInt> widCol( tab, "WEATHER_ID" ) ;
997    uInt wid = widCol( 0 ) ;
998
999    getValidTimeRange( midTime, interval, tab ) ;
1000    mswCols.time().put( wid, midTime ) ;
1001    intervalArr[wid] = interval ;
1002
1003    iter.next() ;
1004  }
1005  mswCols.interval().putColumn( intervalArr ) ;
1006
1007  double endSec = gettimeofday_sec() ;
1008  os_ << "end MSWriter::fillWeather() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1009}
1010
1011void MSWriter::fillSysCal()
1012{
1013  double startSec = gettimeofday_sec() ;
1014  os_ << "start MSWriter::fillSysCal() startSec=" << startSec << LogIO::POST ;
1015
1016  //tcalIdRec_.print( cout ) ;
1017
1018  // access to MS SYSCAL subtable
1019  MSSysCal mssc = mstable_->sysCal() ;
1020
1021  // access to TCAL subtable
1022  Table stt = table_->tcal().table() ;
1023  uInt nrow = stt.nrow() ;
1024
1025  // access to MAIN table
1026  Block<String> cols( 6 ) ;
1027  cols[0] = "TIME" ;
1028  cols[1] = "TCAL_ID" ;
1029  cols[2] = "TSYS" ;
1030  cols[3] = "BEAMNO" ;
1031  cols[4] = "IFNO" ;
1032  cols[5] = "INTERVAL" ;
1033  Table tab = table_->table().project( cols ) ;
1034
1035  if ( nrow == 0 )
1036    return ;
1037
1038  nrow = tcalIdRec_.nfields() ;
1039
1040  Double midTime ;
1041  Double interval ;
1042  String timeStr ;
1043
1044  // row base
1045  TableRow row( mssc ) ;
1046  TableRecord &trec = row.record() ;
1047  RecordFieldPtr<Int> antennaRF( trec, "ANTENNA_ID" ) ;
1048  RecordFieldPtr<Int> feedRF( trec, "FEED_ID" ) ;
1049  RecordFieldPtr<Int> spwRF( trec, "SPECTRAL_WINDOW_ID" ) ;
1050  RecordFieldPtr<Double> timeRF( trec, "TIME" ) ;
1051  RecordFieldPtr<Double> intervalRF( trec, "INTERVAL" ) ;
1052  RecordFieldPtr< Array<Float> > tsysRF( trec, "TSYS" ) ;
1053  RecordFieldPtr< Array<Float> > tcalRF( trec, "TCAL" ) ;
1054  RecordFieldPtr< Array<Float> > tsysspRF ;
1055  RecordFieldPtr< Array<Float> > tcalspRF ;
1056  if ( tsysSpec_ )
1057    tsysspRF.attachToRecord( trec, "TSYS_SPECTRUM" ) ;
1058  if ( tcalSpec_ )
1059    tcalspRF.attachToRecord( trec, "TCAL_SPECTRUM" ) ;
1060
1061  // ANTENNA_ID is always 0
1062  *antennaRF = 0 ;
1063
1064  Table sortedstt = stt.sort( "ID" ) ;
1065  ROArrayColumn<Float> tcalCol( sortedstt, "TCAL" ) ;
1066  ROTableColumn idCol( sortedstt, "ID" ) ;
1067  ROArrayColumn<Float> tsysCol( tab, "TSYS" ) ;
1068  ROTableColumn tcalidCol( tab, "TCAL_ID" ) ;
1069  ROTableColumn timeCol( tab, "TIME" ) ;
1070  ROTableColumn intervalCol( tab, "INTERVAL" ) ;
1071  ROTableColumn beamnoCol( tab, "BEAMNO" ) ;
1072  ROTableColumn ifnoCol( tab, "IFNO" ) ;
1073  for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1074    double t1 = gettimeofday_sec() ;
1075    Vector<uInt> ids = tcalIdRec_.asArrayuInt( irow ) ;
1076    os_ << "ids = " << ids << LogIO::POST ;
1077    uInt npol = ids.size() ;
1078    Vector<uInt> rows = tcalRowRec_.asArrayuInt( irow ) ;
1079    os_ << "rows = " << rows << LogIO::POST ;
1080    Vector<Double> atime( rows.nelements() ) ;
1081    Vector<Double> ainterval( rows.nelements() ) ;
1082    Vector<uInt> atcalid( rows.nelements() ) ;
1083    for( uInt jrow = 0 ; jrow < rows.nelements() ; jrow++ ) {
1084      atime[jrow] = (Double)timeCol.asdouble( rows[jrow] ) ;
1085      ainterval[jrow] = (Double)intervalCol.asdouble( rows[jrow] ) ;
1086      atcalid[jrow] = tcalidCol.asuInt( rows[jrow] ) ;
1087    }
1088    Vector<Float> dummy = tsysCol( rows[0] ) ;
1089    Matrix<Float> tsys( npol,dummy.nelements() ) ;
1090    tsys.row( 0 ) = dummy ;
1091    for ( uInt jrow = 1 ; jrow < npol ; jrow++ )
1092      tsys.row( jrow ) = tsysCol( rows[jrow] ) ;
1093
1094    // FEED_ID
1095    *feedRF = beamnoCol.asuInt( rows[0] ) ;
1096
1097    // SPECTRAL_WINDOW_ID
1098    *spwRF = ifnoCol.asuInt( rows[0] ) ;
1099
1100    // TIME and INTERVAL
1101    getValidTimeRange( midTime, interval, atime, ainterval ) ;
1102    *timeRF = midTime ;
1103    *intervalRF = interval ;
1104
1105    // TCAL and TSYS
1106    Matrix<Float> tcal ;
1107    Table t ;
1108    if ( idCol.asuInt( ids[0] ) == ids[0] ) {
1109      os_ << "sorted at irow=" << irow << " ids[0]=" << ids[0] << LogIO::POST ;
1110      Vector<Float> dummyC = tcalCol( ids[0] ) ;
1111      tcal.resize( npol, dummyC.size() ) ;
1112      tcal.row( 0 ) = dummyC ;
1113    }
1114    else {
1115      os_ << "NOT sorted at irow=" << irow << " ids[0]=" << ids[0] << LogIO::POST ;
1116      t = stt( stt.col("ID") == ids[0] ) ;
1117      Vector<Float> dummyC = tcalCol( 0 ) ;
1118      tcal.resize( npol, dummyC.size() ) ;
1119      tcal.row( 0 ) = dummyC ;
1120    }
1121    if ( npol == 2 ) {
1122      if ( idCol.asuInt( ids[1] ) == ids[1] ) {
1123        os_ << "sorted at irow=" << irow << " ids[1]=" << ids[1] << LogIO::POST ;
1124        tcal.row( 1 ) = tcalCol( ids[1] ) ;
1125      }
1126      else {
1127        os_ << "NOT sorted at irow=" << irow << " ids[1]=" << ids[1] << LogIO::POST ;
1128        t = stt( stt.col("ID") == ids[1] ) ;
1129        tcalCol.attach( t, "TCAL" ) ;
1130        tcal.row( 1 ) = tcalCol( 1 ) ;
1131      }
1132    }
1133    else if ( npol == 3 ) {
1134      if ( idCol.asuInt( ids[2] ) == ids[2] )
1135        tcal.row( 1 ) = tcalCol( ids[2] ) ;
1136      else {
1137        t = stt( stt.col("ID") == ids[2] ) ;
1138        tcalCol.attach( t, "TCAL" ) ;
1139        tcal.row( 1 ) = tcalCol( 0 ) ;
1140      }
1141      if ( idCol.asuInt( ids[1] ) == ids[1] )
1142        tcal.row( 2 ) = tcalCol( ids[1] ) ;
1143      else {
1144        t = stt( stt.col("ID") == ids[1] ) ;
1145        tcalCol.attach( t, "TCAL" ) ;
1146        tcal.row( 2 ) = tcalCol( 0 ) ;
1147      }
1148    }
1149    else if ( npol == 4 ) {
1150      if ( idCol.asuInt( ids[2] ) == ids[2] )
1151        tcal.row( 1 ) = tcalCol( ids[2] ) ;
1152      else {
1153        t = stt( stt.col("ID") == ids[2] ) ;
1154        tcalCol.attach( t, "TCAL" ) ;
1155        tcal.row( 1 ) = tcalCol( 0 ) ;
1156      }
1157      if ( idCol.asuInt( ids[3] ) == ids[3] )
1158        tcal.row( 2 ) = tcalCol( ids[3] ) ;
1159      else {
1160        t = stt( stt.col("ID") == ids[3] ) ;
1161        tcalCol.attach( t, "TCAL" ) ;
1162        tcal.row( 2 ) = tcalCol( 0 ) ;
1163      }
1164      if ( idCol.asuInt( ids[1] ) == ids[1] )
1165        tcal.row( 2 ) = tcalCol( ids[1] ) ;
1166      else {
1167        t = stt( stt.col("ID") == ids[1] ) ;
1168        tcalCol.attach( t, "TCAL" ) ;
1169        tcal.row( 3 ) = tcalCol( 0 ) ;
1170      }
1171    }
1172    if ( tcalSpec_ ) {
1173      // put TCAL_SPECTRUM
1174      //*tcalspRF = tcal ;
1175      tcalspRF.define( tcal ) ;
1176      // set TCAL (mean of TCAL_SPECTRUM)
1177      Matrix<Float> tcalMean( npol, 1 ) ;
1178      for ( uInt iid = 0 ; iid < npol ; iid++ ) {
1179        tcalMean( iid, 0 ) = mean( tcal.row(iid) ) ;
1180      }
1181      // put TCAL
1182      *tcalRF = tcalMean ;
1183    }
1184    else {
1185      // put TCAL
1186      *tcalRF = tcal ;
1187    }
1188   
1189    if ( tsysSpec_ ) {
1190      // put TSYS_SPECTRUM
1191      //*tsysspRF = tsys ;
1192      tsysspRF.define( tsys ) ;
1193      // set TSYS (mean of TSYS_SPECTRUM)
1194      Matrix<Float> tsysMean( npol, 1 ) ;
1195      for ( uInt iid = 0 ; iid < npol ; iid++ ) {
1196        tsysMean( iid, 0 ) = mean( tsys.row(iid) ) ;
1197      }
1198      // put TSYS
1199      *tsysRF = tsysMean ;
1200    }
1201    else {
1202      // put TSYS
1203      *tsysRF = tsys ;
1204    }
1205
1206    // add row
1207    mssc.addRow( 1, True ) ;
1208    row.put( mssc.nrow()-1 ) ;
1209
1210    double t2 = gettimeofday_sec() ;
1211    os_ << irow << "th loop elapsed time = " << t2-t1 << "sec" << LogIO::POST ;
1212  }
1213 
1214  double endSec = gettimeofday_sec() ;
1215  os_ << "end MSWriter::fillSysCal() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1216}
1217
1218void MSWriter::addFeed( Int id )
1219{
1220  double startSec = gettimeofday_sec() ;
1221  os_ << "start MSWriter::addFeed() startSec=" << startSec << LogIO::POST ;
1222
1223  // add row
1224  MSFeed msFeed = mstable_->feed() ;
1225  msFeed.addRow( 1, True ) ;
1226  Int nrow = msFeed.nrow() ;
1227  Int numReceptors = 2 ;
1228  Vector<String> polType( numReceptors ) ;
1229  Matrix<Double> beamOffset( 2, numReceptors ) ;
1230  beamOffset = 0.0 ;
1231  Vector<Double> receptorAngle( numReceptors, 0.0 ) ;
1232  if ( polType_ == "linear" ) {
1233    polType[0] = "X" ;
1234    polType[1] = "Y" ;
1235  }
1236  else if ( polType_ == "circular" ) {
1237    polType[0] = "R" ;
1238    polType[1] = "L" ;
1239  }
1240  else {
1241    polType[0] = "X" ;
1242    polType[1] = "Y" ;
1243  }
1244  Matrix<Complex> polResponse( numReceptors, numReceptors, 0.0 ) ;
1245  for ( Int i = 0 ; i < numReceptors ; i++ )
1246    polResponse( i, i ) = 0.0 ;
1247
1248  MSFeedColumns msFeedCols( mstable_->feed() ) ;
1249
1250  msFeedCols.feedId().put( nrow-1, id ) ;
1251  msFeedCols.antennaId().put( nrow-1, 0 ) ;
1252  msFeedCols.numReceptors().put( nrow-1, numReceptors ) ;
1253  msFeedCols.polarizationType().put( nrow-1, polType ) ;
1254  msFeedCols.beamOffset().put( nrow-1, beamOffset ) ;
1255  msFeedCols.receptorAngle().put( nrow-1, receptorAngle ) ;
1256  msFeedCols.polResponse().put( nrow-1, polResponse ) ;
1257
1258  double endSec = gettimeofday_sec() ;
1259  os_ << "end MSWriter::addFeed() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1260}
1261
1262void MSWriter::addSpectralWindow( Int spwid, Int freqid )
1263{
1264  double startSec = gettimeofday_sec() ;
1265  os_ << "start MSWriter::addSpectralWindow() startSec=" << startSec << LogIO::POST ;
1266 
1267  // add row
1268  MSSpectralWindow msSpw = mstable_->spectralWindow() ;
1269  while( (Int)msSpw.nrow() <= spwid ) {
1270    msSpw.addRow( 1, True ) ;
1271  }
1272 
1273  MSSpWindowColumns msSpwCols( msSpw ) ;
1274
1275  STFrequencies stf = table_->frequencies() ;
1276
1277  // MEAS_FREQ_REF
1278  msSpwCols.measFreqRef().put( spwid, stf.getFrame( True ) ) ;
1279
1280  Double refpix ;
1281  Double refval ;
1282  Double inc ;
1283  stf.getEntry( refpix, refval, inc, (uInt)freqid ) ;
1284
1285  // NUM_CHAN
1286  Int nchan = (Int)(refpix * 2) ;
1287  msSpwCols.numChan().put( spwid, nchan ) ;
1288
1289  // TOTAL_BANDWIDTH
1290  Double bw = nchan * inc ;
1291  msSpwCols.totalBandwidth().put( spwid, bw ) ;
1292
1293  // REF_FREQUENCY
1294  Double refFreq = refval - refpix * inc ;
1295  msSpwCols.refFrequency().put( spwid, refFreq ) ;
1296
1297  // NET_SIDEBAND
1298  // tentative: USB->0, LSB->1
1299  Int netSideband = 0 ;
1300  if ( inc < 0 )
1301    netSideband = 1 ;
1302  msSpwCols.netSideband().put( spwid, netSideband ) ;
1303
1304  // RESOLUTION, CHAN_WIDTH, EFFECTIVE_BW
1305  Vector<Double> sharedDoubleArr( nchan, inc ) ;
1306  msSpwCols.resolution().put( spwid, sharedDoubleArr ) ;
1307  msSpwCols.chanWidth().put( spwid, sharedDoubleArr ) ;
1308  msSpwCols.effectiveBW().put( spwid, sharedDoubleArr ) ;
1309
1310  // CHAN_FREQ
1311  indgen( sharedDoubleArr, refFreq, inc ) ;
1312  msSpwCols.chanFreq().put( spwid, sharedDoubleArr ) ;
1313
1314  double endSec = gettimeofday_sec() ;
1315  os_ << "end MSWriter::addSpectralWindow() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1316}
1317
1318void MSWriter::addField( Int fid, String fieldname, String srcname, Double t, Vector<Double> rate )
1319{
1320  double startSec = gettimeofday_sec() ;
1321  os_ << "start MSWriter::addField() startSec=" << startSec << LogIO::POST ;
1322 
1323  MSField msField = mstable_->field() ;
1324  while( (Int)msField.nrow() <= fid ) {
1325    msField.addRow( 1, True ) ;
1326  }
1327  MSFieldColumns msFieldCols( msField ) ;
1328
1329  // Access to SOURCE table
1330  MSSource msSrc = mstable_->source() ;
1331
1332  // fill target row
1333  msFieldCols.name().put( fid, fieldname ) ;
1334  msFieldCols.time().put( fid, t ) ;
1335  Int numPoly = 0 ;
1336  if ( anyNE( rate, 0.0 ) )
1337    numPoly = 1 ;
1338  msFieldCols.numPoly().put( fid, numPoly ) ;
1339  MSSourceIndex msSrcIdx( msSrc ) ;
1340  Int srcId = -1 ;
1341  Vector<Int> srcIdArr = msSrcIdx.matchSourceName( srcname ) ;
1342  if ( srcIdArr.size() != 0 ) {
1343    srcId = srcIdArr[0] ;
1344    MSSource msSrcSel = msSrc( msSrc.col("SOURCE_ID") == srcId ) ;
1345    ROMSSourceColumns msSrcCols( msSrcSel ) ;
1346    Vector<Double> srcDir = msSrcCols.direction()( 0 ) ;
1347    Matrix<Double> srcDirA( IPosition( 2, 2, 1+numPoly ) ) ;
1348    os_ << "srcDirA = " << srcDirA << LogIO::POST ;
1349    os_ << "sliced srcDirA = " << srcDirA.column( 0 ) << LogIO::POST ;
1350    srcDirA.column( 0 ) = srcDir ;
1351    os_ << "srcDirA = " << srcDirA << LogIO::POST ;
1352    if ( numPoly != 0 )
1353      srcDirA.column( 1 ) = rate ;
1354    msFieldCols.phaseDir().put( fid, srcDirA ) ;
1355    msFieldCols.referenceDir().put( fid, srcDirA ) ;
1356    msFieldCols.delayDir().put( fid, srcDirA ) ;
1357  }
1358  msFieldCols.sourceId().put( fid, srcId ) ;
1359
1360  double endSec = gettimeofday_sec() ;
1361  os_ << "end MSWriter::addField() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1362}
1363
1364void MSWriter::addPointing( String &name, Double &me, Double &interval, Matrix<Double> &dir )
1365{
1366  double startSec = gettimeofday_sec() ;
1367  os_ << "start MSWriter::addPointing() startSec=" << startSec << LogIO::POST ;
1368 
1369  // access to POINTING subtable
1370  MSPointing msp = mstable_->pointing() ;
1371  uInt nrow = msp.nrow() ;
1372
1373  // add row
1374  msp.addRow( 1, True ) ;
1375
1376  // fill row
1377  TableRow row( msp ) ;
1378  TableRecord &rec = row.record() ;
1379  RecordFieldPtr<Int> antennaRF( rec, "ANTENNA_ID" ) ;
1380  *antennaRF = 0 ;
1381  RecordFieldPtr<Int> numpolyRF( rec, "NUM_POLY" ) ;
1382  *numpolyRF = dir.ncolumn() ;
1383  RecordFieldPtr<Double> timeRF( rec, "TIME" ) ;
1384  *timeRF = me ;
1385  RecordFieldPtr<Double> toriginRF( rec, "TIME_ORIGIN" ) ;
1386  *toriginRF = me ;
1387  RecordFieldPtr<Double> intervalRF( rec, "INTERVAL" ) ;
1388  *intervalRF = interval ;
1389  RecordFieldPtr<String> nameRF( rec, "NAME" ) ;
1390  *nameRF = name ;
1391  RecordFieldPtr<Bool> trackRF( rec, "TRACKING" ) ;
1392  *trackRF = True ;
1393  RecordFieldPtr< Array<Double> > dirRF( rec, "DIRECTION" ) ;
1394  *dirRF = dir ;
1395  RecordFieldPtr< Array<Double> > targetRF( rec, "TARGET" ) ;
1396  *dirRF = dir ;
1397  row.put( nrow ) ;
1398
1399  double endSec = gettimeofday_sec() ;
1400  os_ << "end MSWriter::addPointing() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1401}
1402
1403Int MSWriter::addPolarization( Vector<Int> polnos )
1404{
1405  double startSec = gettimeofday_sec() ;
1406  os_ << "start MSWriter::addPolarization() startSec=" << startSec << LogIO::POST ;
1407
1408  os_ << "polnos = " << polnos << LogIO::POST ;
1409  MSPolarization msPol = mstable_->polarization() ;
1410  uInt nrow = msPol.nrow() ;
1411
1412  // only 1 POLARIZATION row for 1 scantable
1413  if ( nrow > 0 )
1414    return 0 ;
1415 
1416  Vector<Int> corrType = toCorrType( polnos ) ;
1417 
1418  ROArrayColumn<Int> corrtCol( msPol, "CORR_TYPE" ) ;
1419  Matrix<Int> corrTypeArr = corrtCol.getColumn() ;
1420  Int polid = -1 ;
1421  for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1422    if ( allEQ( corrType, corrTypeArr.column( irow ) ) ) {
1423      polid = irow ;
1424      break ;
1425    }
1426  }
1427
1428  if ( polid == -1 ) {
1429    MSPolarizationColumns msPolCols( msPol ) ;
1430
1431    // add row
1432    msPol.addRow( 1, True ) ;
1433    polid = (Int)nrow ;
1434
1435    // CORR_TYPE
1436    msPolCols.corrType().put( nrow, corrType ) ;
1437
1438    // NUM_CORR
1439    uInt npol = corrType.size() ;
1440    msPolCols.numCorr().put( nrow, npol ) ;
1441
1442    // CORR_PRODUCT
1443    Matrix<Int> corrProd( 2, npol, -1 ) ;
1444    if ( npol == 1 ) {
1445      corrProd = 0 ;
1446    }
1447    else if ( npol == 2 ) {
1448      corrProd.column( 0 ) = 0 ;
1449      corrProd.column( 1 ) = 1 ;
1450    }
1451    else {
1452      corrProd.column( 0 ) = 0 ;
1453      corrProd.column( 3 ) = 1 ;
1454      corrProd( 0,1 ) = 0 ;
1455      corrProd( 1,1 ) = 1 ;
1456      corrProd( 0,2 ) = 1 ;
1457      corrProd( 1,2 ) = 0 ;
1458    }
1459    msPolCols.corrProduct().put( nrow, corrProd ) ;   
1460  }
1461
1462  double endSec = gettimeofday_sec() ;
1463  os_ << "end MSWriter::addPolarization() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1464
1465  return polid ;
1466}
1467
1468Int MSWriter::addDataDescription( Int polid, Int spwid )
1469{
1470  double startSec = gettimeofday_sec() ;
1471  os_ << "start MSWriter::addDataDescription() startSec=" << startSec << LogIO::POST ;
1472
1473  MSDataDescription msDataDesc = mstable_->dataDescription() ;
1474  uInt nrow = msDataDesc.nrow() ;
1475
1476  // only 1 POLARIZATION_ID for 1 scantable
1477  Int ddid = -1 ;
1478  ROScalarColumn<Int> spwCol( msDataDesc, "SPECTRAL_WINDOW_ID" ) ;
1479  Vector<Int> spwIds = spwCol.getColumn() ;
1480  //ROScalarColumn<Int> polCol( msDataDesc, "POLARIZATION_ID" ) ;
1481  //Vector<Int> polIds = polCol.getColumn() ;
1482  for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1483    //if ( spwid == spwIds[irow] && polid == polIds[irow] ) {
1484    if ( spwid == spwIds[irow] ) {
1485      ddid = irow ;
1486      break ;
1487    }
1488  }
1489  os_ << "ddid = " << ddid << LogIO::POST ;
1490 
1491
1492  if ( ddid == -1 ) {
1493    msDataDesc.addRow( 1, True ) ;
1494    MSDataDescColumns msDataDescCols( msDataDesc ) ;
1495    msDataDescCols.polarizationId().put( nrow, polid ) ;
1496    msDataDescCols.spectralWindowId().put( nrow, spwid ) ;
1497    ddid = (Int)nrow ;
1498  }
1499
1500  double endSec = gettimeofday_sec() ;
1501  os_ << "end MSWriter::addDataDescription() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1502
1503  return ddid ;
1504}
1505
1506Int MSWriter::addState( Int st, Int &subscan )
1507{
1508  double startSec = gettimeofday_sec() ;
1509  os_ << "start MSWriter::addState() startSec=" << startSec << LogIO::POST ;
1510
1511  // access to STATE subtable
1512  MSState msState = mstable_->state() ;
1513  uInt nrow = msState.nrow() ;
1514
1515  String obsMode ;
1516  Bool isSignal ;
1517  Double tnoise ;
1518  Double tload ;
1519  queryType( st, obsMode, isSignal, tnoise, tload ) ;
1520  os_ << "obsMode = " << obsMode << " isSignal = " << isSignal << LogIO::POST ;
1521
1522  Int idx = -1 ;
1523  ROScalarColumn<String> obsModeCol( msState, "OBS_MODE" ) ;
1524  //ROScalarColumn<Bool> sigCol( msState, "SIG" ) ;
1525  //ROScalarColumn<Bool> refCol( msState, "REF" ) ;
1526  ROScalarColumn<Int> subscanCol( msState, "SUB_SCAN" ) ;
1527//   Vector<String> obsModeArr = obsModeCol.getColumn() ;
1528//   Vector<Bool> sigArr = sigCol.getColumn() ;
1529//   Vector<Bool> refArr = refCol.getColumn() ;
1530//   Vector<Int> subscanArr = subscanCol.getColumn() ;
1531  for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1532    if ( obsModeCol(irow) == obsMode
1533         //&& sigCol(irow) == isSignal
1534         //&& refCol(irow) != isSignal
1535         && subscanCol(irow) == subscan ) {
1536//     if ( obsModeArr[irow] == obsMode
1537//          && sigArr[irow] == isSignal
1538//          && refArr[irow] != isSignal
1539//          && subscanArr[irow] == subscan ) {
1540      idx = irow ;
1541      break ;
1542    }
1543  }
1544  if ( idx == -1 ) {
1545    msState.addRow( 1, True ) ;
1546    TableRow row( msState ) ;
1547    TableRecord &rec = row.record() ;
1548    RecordFieldPtr<String> obsmodeRF( rec, "OBS_MODE" ) ;
1549    *obsmodeRF = obsMode ;
1550    RecordFieldPtr<Bool> sigRF( rec, "SIG" ) ;
1551    *sigRF = isSignal ;
1552    RecordFieldPtr<Bool> refRF( rec, "REF" ) ;
1553    *refRF = !isSignal ;
1554    RecordFieldPtr<Int> subscanRF( rec, "SUB_SCAN" ) ;
1555    *subscanRF = subscan ;
1556    RecordFieldPtr<Double> noiseRF( rec, "CAL" ) ;
1557    *noiseRF = tnoise ;
1558    RecordFieldPtr<Double> loadRF( rec, "LOAD" ) ;
1559    *loadRF = tload ;
1560    row.put( nrow ) ;
1561//     ScalarColumn<String> obsModeCol( msState, "OBS_MODE" ) ;
1562//     obsModeCol.put( nrow, obsMode ) ;
1563//     ScalarColumn<Bool> sharedBCol( msState, "SIG" ) ;
1564//     sharedBCol.put( nrow, isSignal ) ;
1565//     sharedBCol.attach( msState, "REF" ) ;
1566//     sharedBCol.put( nrow, !isSignal ) ;
1567//     ScalarColumn<Int> subscanCol( msState, "SUB_SCAN" ) ;
1568//     subscanCol.put( nrow, subscan ) ;
1569    idx = nrow ;
1570  }
1571  subscan++ ;
1572
1573  double endSec = gettimeofday_sec() ;
1574  os_ << "end MSWriter::addState() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1575
1576  return idx ;
1577}
1578
1579Vector<Int> MSWriter::toCorrType( Vector<Int> polnos )
1580{
1581  double startSec = gettimeofday_sec() ;
1582  os_ << "start MSWriter::toCorrType() startSec=" << startSec << LogIO::POST ;
1583
1584  uInt npol = polnos.size() ;
1585  Vector<Int> corrType( npol, Stokes::Undefined ) ;
1586 
1587  if ( npol == 4 ) {
1588    if ( polType_ == "linear" ) {
1589      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1590        if ( polnos[ipol] == 0 )
1591          corrType[ipol] = Stokes::XX ;
1592        else if ( polnos[ipol] == 1 )
1593          corrType[ipol] = Stokes::XY ;
1594        else if ( polnos[ipol] == 2 )
1595          corrType[ipol] = Stokes::YX ;
1596        else if ( polnos[ipol] == 3 )
1597          corrType[ipol] = Stokes::YY ;
1598      }
1599    }
1600    else if ( polType_ == "circular" ) {
1601      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1602        if ( polnos[ipol] == 0 )
1603          corrType[ipol] = Stokes::RR ;
1604        else if ( polnos[ipol] == 1 )
1605          corrType[ipol] = Stokes::RL ;
1606        else if ( polnos[ipol] == 2 )
1607          corrType[ipol] = Stokes::LR ;
1608        else if ( polnos[ipol] == 3 )
1609          corrType[ipol] = Stokes::LL ;
1610      }
1611    }
1612    else if ( polType_ == "stokes" ) {
1613      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1614        if ( polnos[ipol] == 0 )
1615          corrType[ipol] = Stokes::I ;
1616        else if ( polnos[ipol] == 1 )
1617          corrType[ipol] = Stokes::Q ;
1618        else if ( polnos[ipol] == 2 )
1619          corrType[ipol] = Stokes::U ;
1620        else if ( polnos[ipol] == 3 )
1621          corrType[ipol] = Stokes::V ;
1622      }
1623    }
1624  }
1625  else if ( npol == 2 ) {
1626    if ( polType_ == "linear" ) {
1627      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1628        if ( polnos[ipol] == 0 )
1629          corrType[ipol] = Stokes::XX ;
1630        else if ( polnos[ipol] == 1 )
1631          corrType[ipol] = Stokes::YY ;
1632      }
1633    }
1634    else if ( polType_ == "circular" ) {
1635      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1636        if ( polnos[ipol] == 0 )
1637          corrType[ipol] = Stokes::RR ;
1638        else if ( polnos[ipol] == 1 )
1639          corrType[ipol] = Stokes::LL ;
1640      }
1641    }
1642    else if ( polType_ == "stokes" ) {
1643      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1644        if ( polnos[ipol] == 0 )
1645          corrType[ipol] = Stokes::I ;
1646        else if ( polnos[ipol] == 1 )
1647          corrType[ipol] = Stokes::V ;
1648      }
1649    }
1650    else if ( polType_ == "linpol" ) {
1651      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1652        if ( polnos[ipol] == 1 )
1653          corrType[ipol] = Stokes::Plinear ;
1654        else if ( polnos[ipol] == 2 )
1655          corrType[ipol] = Stokes::Pangle ;
1656      }
1657    }
1658  }     
1659  else if ( npol == 1 ) {
1660    if ( polType_ == "linear" )
1661      corrType[0] = Stokes::XX ;
1662    else if ( polType_ == "circular" )
1663      corrType[0] = Stokes::RR ;
1664    else if ( polType_ == "stokes" )
1665      corrType[0] = Stokes::I ;
1666  }
1667
1668  double endSec = gettimeofday_sec() ;
1669  os_ << "end MSWriter::toCorrType() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1670
1671  return corrType ;
1672}
1673
1674void MSWriter::getValidTimeRange( Double &me, Double &interval, Table &tab )
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  ROScalarColumn<Double> timeCol( tab, "TIME" ) ;
1683  Vector<Double> timeArr = timeCol.getColumn() ;
1684  Double minTime ;
1685  Double maxTime ;
1686  minMax( minTime, maxTime, timeArr ) ;
1687  Double midTime = 0.5 * ( minTime + maxTime ) * 86400.0 ;
1688  // unit for TIME
1689  // Scantable: "d"
1690  // MS: "s"
1691  me = midTime ;
1692  interval = ( maxTime - minTime ) * 86400.0 ;
1693
1694  double endSec = gettimeofday_sec() ;
1695  os_ << "end MSWriter::getValidTimeRange() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1696}
1697
1698void MSWriter::getValidTimeRange( Double &me, Double &interval, Vector<Double> &atime, Vector<Double> &ainterval )
1699{
1700  double startSec = gettimeofday_sec() ;
1701  os_ << "start MSWriter::getVaridTimeRange() startSec=" << startSec << LogIO::POST ;
1702
1703  // sort table
1704  //Table stab = tab.sort( "TIME" ) ;
1705
1706  Double minTime ;
1707  Double maxTime ;
1708  minMax( minTime, maxTime, atime ) ;
1709  Double midTime = 0.5 * ( minTime + maxTime ) * 86400.0 ;
1710  // unit for TIME
1711  // Scantable: "d"
1712  // MS: "s"
1713  me = midTime ;
1714  interval = ( maxTime - minTime ) * 86400.0 + mean( ainterval ) ;
1715
1716  double endSec = gettimeofday_sec() ;
1717  os_ << "end MSWriter::getValidTimeRange() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1718}
1719
1720//void MSWriter::queryType( Int type, String &stype, Bool &b )
1721void MSWriter::queryType( Int type, String &stype, Bool &b, Double &t, Double &l )
1722{
1723  double startSec = gettimeofday_sec() ;
1724  os_ << "start MSWriter::queryType() startSec=" << startSec << LogIO::POST ;
1725
1726  switch ( type ) {
1727  case SrcType::PSON:
1728    stype = "POSITION_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1729    b = True ;
1730    t = 0.0 ;
1731    l = 0.0 ;
1732    break ;
1733  case SrcType::PSOFF:
1734    stype = "POSITION_SWITCH.OBSERVE_TARGET.OFF_SOURCE" ;
1735    b = False ;
1736    t = 0.0 ;
1737    l = 0.0 ;
1738    break ;
1739  case SrcType::NOD:
1740    stype = "NOD.OBSERVE_TARGET.ON_SOURCE" ;
1741    b = True ;
1742    t = 0.0 ;
1743    l = 0.0 ;
1744    break ;
1745  case SrcType::FSON:
1746    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1747    b = True ;
1748    t = 0.0 ;
1749    l = 0.0 ;
1750    break ;
1751  case SrcType::FSOFF:
1752    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1753    b = False ;
1754    t = 0.0 ;
1755    l = 0.0 ;
1756    break ;
1757  case SrcType::SKY:
1758    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1759    b = False ;
1760    t = 0.0 ;
1761    l = 1.0 ;
1762    break ;
1763  case SrcType::HOT:
1764    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1765    b = False ;
1766    t = 0.0 ;
1767    l = 1.0 ;
1768    break ;
1769  case SrcType::WARM:
1770    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1771    t = 0.0 ;
1772    b = False ;
1773    l = 1.0 ;
1774    break ;
1775  case SrcType::COLD:
1776    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1777    b = False ;
1778    t = 0.0 ;
1779    l = 1.0 ;
1780    break ;
1781  case SrcType::PONCAL:
1782    stype = "POSITION_SWITCH.CALIBRATE_TEMPERATURE.ON_SOURCE" ;
1783    b = True ;
1784    t = 1.0 ;
1785    l = 0.0 ;
1786    break ;
1787  case SrcType::POFFCAL:
1788    stype = "POSITION_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1789    b = False ;
1790    t = 1.0 ;
1791    l = 0.0 ;
1792    break ;
1793  case SrcType::NODCAL:
1794    stype = "NOD.CALIBRATE_TEMPERATURE.ON_SOURCE" ;
1795    b = True ;
1796    t = 1.0 ;
1797    l = 0.0 ;
1798    break ;
1799  case SrcType::FONCAL:
1800    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.ON_SOURCE" ;
1801    b = True ;
1802    t = 1.0 ;
1803    l = 0.0 ;
1804    break ;
1805  case SrcType::FOFFCAL:
1806    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1807    b = False ;
1808    t = 1.0 ;
1809    l = 0.0 ;
1810    break ;
1811  case SrcType::FSLO:
1812    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1813    b = True ;
1814    t = 0.0 ;
1815    l = 0.0 ;
1816    break ;
1817  case SrcType::FLOOFF:
1818    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.OFF_SOURCE" ;
1819    b = False ;
1820    t = 0.0 ;
1821    l = 0.0 ;
1822    break ;
1823  case SrcType::FLOSKY:
1824    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1825    b = False ;
1826    t = 0.0 ;
1827    l = 1.0 ;
1828    break ;
1829  case SrcType::FLOHOT:
1830    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1831    b = False ;
1832    t = 0.0 ;
1833    l = 1.0 ;
1834    break ;
1835  case SrcType::FLOWARM:
1836    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1837    b = False ;
1838    t = 0.0 ;
1839    l = 1.0 ;
1840    break ;
1841  case SrcType::FLOCOLD:
1842    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1843    b = False ;
1844    t = 0.0 ;
1845    l = 1.0 ;
1846    break ;
1847  case SrcType::FSHI:
1848    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1849    b = True ;
1850    t = 0.0 ;
1851    l = 0.0 ;
1852    break ;
1853  case SrcType::FHIOFF:
1854    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1855    b = False ;
1856    t = 0.0 ;
1857    l = 0.0 ;
1858    break ;
1859  case SrcType::FHISKY:
1860    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1861    b = False ;
1862    t = 0.0 ;
1863    l = 1.0 ;
1864    break ;
1865  case SrcType::FHIHOT:
1866    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1867    b = False ;
1868    t = 0.0 ;
1869    l = 1.0 ;
1870    break ;
1871  case SrcType::FHIWARM:
1872    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1873    b = False ;
1874    t = 0.0 ;
1875    l = 1.0 ;
1876    break ;
1877  case SrcType::FHICOLD:
1878    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1879    b = False ;
1880    t = 0.0 ;
1881    l = 1.0 ;
1882    break ;
1883  case SrcType::SIG:
1884    stype = "UNSPECIFIED.OBSERVE_TARGET.ON_SOURCE" ;
1885    b = True ;
1886    t = 0.0 ;
1887    l = 0.0 ;
1888    break ;
1889  case SrcType::REF:
1890    stype = "UNSPECIFIED.OBSERVE_TARGET.ON_SOURCE" ;
1891    b = False ;
1892    t = 0.0 ;
1893    l = 0.0 ;
1894    break ;
1895  default:
1896    stype = "UNSPECIFIED" ;
1897    b = True ;
1898    t = 0.0 ;
1899    l = 0.0 ;
1900    break ;
1901  }
1902
1903  double endSec = gettimeofday_sec() ;
1904  os_ << "end MSWriter::queryType() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1905}
1906}
Note: See TracBrowser for help on using the repository browser.