source: trunk/src/MSWriter.cpp @ 2259

Last change on this file since 2259 was 2259, 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...

Bug fix: bandwidth must be positive value.


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