source: trunk/src/MSWriter.cpp @ 2244

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

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No?

Module(s): Module Names change impacts.

Description: Describe your changes here...

Added maxRow = 1 in selection using TableExprNode? when
finding at least one row is enough.

Minor bug fix on MSWriter.cpp


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