source: trunk/src/MSWriter.cpp @ 1987

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

New Development: No

JIRA Issue: Yes CAS-2718

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No?

Module(s): Module Names change impacts.

Description: Describe your changes here...

Improved MSFiller/MSWriter.
Changed Scantable::get_antennaName() to fit with MSFiller/MSWriter.


File size: 50.6 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
20#include <tables/Tables/ExprNode.h>
21#include <tables/Tables/TableDesc.h>
22#include <tables/Tables/SetupNewTab.h>
23#include <tables/Tables/TableIter.h>
24#include <tables/Tables/RefRows.h>
25
26#include <ms/MeasurementSets/MeasurementSet.h>
27#include <ms/MeasurementSets/MSColumns.h>
28#include <ms/MeasurementSets/MSPolIndex.h>
29#include <ms/MeasurementSets/MSDataDescIndex.h>
30#include <ms/MeasurementSets/MSSourceIndex.h>
31
32#include "MSWriter.h"
33#include "STHeader.h"
34#include "STFrequencies.h"
35#include "STMolecules.h"
36#include "STTcal.h"
37
38using namespace casa ;
39
40namespace asap
41{
42
43MSWriter::MSWriter(CountedPtr<Scantable> stable)
44  : table_(stable)
45{
46  os_ = LogIO() ;
47  os_.origin( LogOrigin( "MSWriter", "MSWriter()", WHERE ) ) ;
48  os_ << "MSWriter::MSWriter()" << LogIO::POST ;
49
50  // initialize writer
51  init() ;
52}
53
54MSWriter::~MSWriter()
55{
56  os_.origin( LogOrigin( "MSWriter", "~MSWriter()", WHERE ) ) ;
57  os_ << "MSWriter::~MSWriter()" << LogIO::POST ;
58}
59 
60bool MSWriter::write(const string& filename, const Record& rec)
61{
62  os_.origin( LogOrigin( "MSWriter", "write()", WHERE ) ) ;
63  os_ << "MSWriter::write()" << LogIO::POST ;
64
65  filename_ = filename ;
66
67  // parsing MS options
68  Bool overwrite = False ;
69  if ( rec.isDefined( "ms" ) ) {
70    Record msrec = rec.asRecord( "ms" ) ;
71    if ( msrec.isDefined( "overwrite" ) ) {
72      overwrite = msrec.asBool( "overwrite" ) ;
73    }
74  }
75
76  os_ << "Parsing MS options" << endl ;
77  os_ << "   overwrite = " << overwrite << LogIO::POST ;
78
79  File file( filename_ ) ;
80  if ( file.exists() ) {
81    if ( overwrite ) {
82      os_ << filename_ << " exists. Overwrite existing data... " << LogIO::POST ;
83      if ( file.isRegular() ) RegularFile(file).remove() ;
84      else if ( file.isDirectory() ) Directory(file).removeRecursive() ;
85      else SymLink(file).remove() ;
86    }
87    else {
88      os_ << LogIO::SEVERE << "ERROR: " << filename_ << " exists..." << LogIO::POST ;
89      return False ;
90    }
91  }
92
93  // set up MS
94  setupMS() ;
95 
96  // subtables
97  // OBSERVATION
98  fillObservation() ;
99
100  // ANTENNA
101  fillAntenna() ;
102
103  // PROCESSOR
104  fillProcessor() ;
105
106  // SOURCE
107  fillSource() ;
108
109  // WEATHER
110  fillWeather() ;
111
112  // MAIN
113  // Iterate over several ids
114  Vector<uInt> processedFreqId( 0 ) ;
115  Int defaultFieldId = 0 ;
116  //
117  // ITERATION: FIELDNAME
118  //
119  Int added0 = 0 ;
120  Int current0 = mstable_->nrow() ;
121  TableIterator iter0( table_->table(), "FIELDNAME" ) ;
122  while( !iter0.pastEnd() ) {
123    Table t0( iter0.table() ) ;
124    ROScalarColumn<String> sharedStrCol( t0, "FIELDNAME" ) ;
125    String fieldName = sharedStrCol( 0 ) ;
126    sharedStrCol.attach( t0, "SRCNAME" ) ;
127    String srcName = sharedStrCol( 0 ) ;
128    ROScalarColumn<Double> timeCol( t0, "TIME" ) ;
129    Double minTime = min( timeCol.getColumn() ) ;
130    ROArrayColumn<Double> scanRateCol( t0, "SCANRATE" ) ;
131    Vector<Double> scanRate = scanRateCol.getColumn()[0] ;
132    String::size_type pos = fieldName.find( "__" ) ;
133    Int fieldId = -1 ;
134    if ( pos != String::npos ) {
135      os_ << "fieldName.substr( pos+2 )=" << fieldName.substr( pos+2 ) << LogIO::POST ;
136      fieldId = String::toInt( fieldName.substr( pos+2 ) ) ;
137      fieldName = fieldName.substr( 0, pos ) ;
138    }
139    else {
140      os_ << "use default field id" << LogIO::POST ;
141      fieldId = defaultFieldId ;
142      defaultFieldId++ ;
143    }
144    os_ << "fieldId" << fieldId << ": " << fieldName << LogIO::POST ;
145    //
146    // ITERATION: BEAMNO
147    //
148    Int added1 = 0 ;
149    Int current1 = mstable_->nrow() ;
150    TableIterator iter1( t0, "BEAMNO" ) ;
151    while( !iter1.pastEnd() ) {
152      Table t1( iter1.table() ) ;
153      ROScalarColumn<uInt> beamNoCol( t1, "BEAMNO" ) ;
154      uInt beamNo = beamNoCol( 0 ) ;
155      os_ << "beamNo = " << beamNo << LogIO::POST ;
156      //
157      // ITERATION: SCANNO
158      //
159      Int added2 = 0 ;
160      Int current2 = mstable_->nrow() ;
161      TableIterator iter2( t1, "SCANNO" ) ;
162      while( !iter2.pastEnd() ) {
163        Table t2( iter2.table() ) ;
164        ROScalarColumn<uInt> scanNoCol( t2, "SCANNO" ) ;
165        uInt scanNo = scanNoCol( 0 ) ;
166        os_ << "scanNo = " << scanNo << LogIO::POST ;
167        //
168        // ITERATION: IFNO
169        //
170        Int added3 = 0 ;
171        Int current3 = mstable_->nrow() ;
172        TableIterator iter3( t2, "IFNO" ) ;
173        while( !iter3.pastEnd() ) {
174          Table t3( iter3.table() ) ;
175          ROScalarColumn<uInt> ifNoCol( t3, "IFNO" ) ;
176          uInt ifNo = ifNoCol( 0 ) ;
177          os_ << "ifNo = " << ifNo << LogIO::POST ;
178          ROScalarColumn<uInt> freqIdCol( t3, "FREQ_ID" ) ;
179          uInt freqId = freqIdCol( 0 ) ;
180          os_ << "freqId = " << freqId << LogIO::POST ;
181          Int subscan = 0 ;
182          //
183          // ITERATION: CYCLENO
184          //
185          Int added4 = 0 ;
186          Int current4 = mstable_->nrow() ;
187          TableIterator iter4( t3, "CYCLENO" ) ;
188          while( !iter4.pastEnd() ) {
189            Table t4( iter4.table() ) ;
190            //
191            // ITERATION: TIME
192            //
193            Int added5 = 0 ;
194            Int current5 = mstable_->nrow() ;
195            TableIterator iter5( t4, "TIME" ) ;
196            while( !iter5.pastEnd() ) {
197              Table t5( iter5.table().sort("POLNO") ) ;
198              Int prevnr = mstable_->nrow() ;
199              Int nrow = t5.nrow() ;
200              os_ << "nrow = " << nrow << LogIO::POST ;
201             
202              // add row
203              mstable_->addRow( 1, True ) ;
204             
205              Vector<Int> polnos( nrow ) ;
206              indgen( polnos, 0 ) ;
207              Int polid = addPolarization( polnos ) ;
208              os_ << "polid = " << polid << LogIO::POST ;
209              //
210              // LOOP: POLNO
211              //
212              ROArrayColumn<Float> specCol( t5, "SPECTRA" ) ;
213              ROArrayColumn<uChar> flagCol( t5, "FLAGTRA" ) ;
214              ROScalarColumn<uInt> flagRowCol( t5, "FLAGROW" ) ;
215              uInt nchan = specCol( 0 ).size() ;
216              IPosition newshape( 2,1,nchan ) ;
217              IPosition cellshape( 2, nrow, nchan ) ;
218              if ( useFloatData_ ) {
219                // FLOAT_DATA
220                Array<Float> dataArr( cellshape ) ;
221                Array<Bool> flagArr( cellshape ) ;
222                for ( Int ipol = 0 ; ipol < nrow ; ipol++ ) {
223                  Slicer slicer( Slice(ipol), Slice(0,nchan,1) ) ;
224                  dataArr( slicer ) = specCol( ipol ).reform( newshape ) ;
225                  Array<uChar> tmpUC = flagCol( ipol ).reform( newshape ) ;
226                  Array<Bool> tmpB( tmpUC.shape() ) ;
227                  //convertArray( flagArr( slicer ), flagCol( ipol ).reform( newshape ) ) ;
228                  convertArray( tmpB, tmpUC ) ;
229                  flagArr( slicer ) = tmpB ;
230                }
231                ArrayColumn<Float> msDataCol( *mstable_, "FLOAT_DATA" ) ;
232                msDataCol.put( prevnr, dataArr ) ;
233               
234                // FLAG
235                ArrayColumn<Bool> msFlagCol( *mstable_, "FLAG" ) ;
236                msFlagCol.put( prevnr, flagArr ) ;
237              }
238              else if ( useData_ ) {
239                // DATA
240                // assume nrow = 4
241                Array<Complex> dataArr( cellshape ) ;
242                Vector<Float> zeroIm( nchan, 0 ) ;
243                Array<Float> dummy( IPosition( 2, 2, nchan ) ) ;
244                Slicer slicer0( Slice(0), Slice(0,nchan,1) ) ;
245                Slicer slicer1( Slice(1), Slice(0,nchan,1) ) ;
246                Slicer slicer2( Slice(2), Slice(0,nchan,1) ) ;
247                Slicer slicer3( Slice(3), Slice(0,nchan,1) ) ;
248                dummy( slicer0 ) = specCol( 0 ).reform( newshape ) ;
249                dummy( slicer1 ) = zeroIm.reform( newshape ) ;
250                dataArr( slicer0 ) = RealToComplex( dummy ) ;
251                dummy( slicer0 ) = specCol( 1 ).reform( newshape ) ;
252                dataArr( slicer3 ) = RealToComplex( dummy ) ;
253                dummy( slicer0 ) = specCol( 2 ).reform( newshape ) ;
254                dummy( slicer1 ) = specCol( 3 ).reform( newshape ) ;
255                dataArr( slicer1 ) = RealToComplex( dummy ) ;
256                dataArr( slicer2 ) = conj( RealToComplex( dummy ) ) ;
257                ArrayColumn<Complex> msDataCol( *mstable_, "DATA" ) ;
258                msDataCol.put( prevnr, dataArr ) ;
259
260                // FLAG
261                Array<Bool> flagArr( cellshape ) ;
262                Array<uChar> tmpUC = flagCol( 0 ).reform( newshape ) ;
263                Array<Bool> tmpB( tmpUC.shape() ) ;
264                convertArray( tmpB, tmpUC ) ;
265                flagArr( slicer0 ) = tmpB ;
266                tmpUC = flagCol( 3 ).reform( newshape ) ;
267                convertArray( tmpB, tmpUC ) ;
268                flagArr( slicer3 ) = tmpB ;
269                Vector<uChar> bitOr = flagCol( 2 ) | flagCol( 3 ) ;
270                tmpUC = bitOr.reform( newshape ) ;
271                convertArray( tmpB, tmpUC ) ;
272                flagArr( slicer1 ) = tmpB ;
273                flagArr( slicer2 ) = tmpB ;
274                ArrayColumn<Bool> msFlagCol( *mstable_, "FLAG" ) ;
275                msFlagCol.put( prevnr, flagArr ) ;
276              }
277
278              // FLAG_ROW
279              ScalarColumn<Bool> msFlagRowCol( *mstable_, "FLAG_ROW" ) ;
280              msFlagRowCol.put( prevnr, anyNE( flagRowCol.getColumn(), (uInt)0 ) ) ;
281             
282              // TIME and TIME_CENTROID
283              ROScalarMeasColumn<MEpoch> timeCol( t5, "TIME" ) ;
284              MEpoch mTime = timeCol( 0 ) ;
285              ScalarMeasColumn<MEpoch> msTimeCol( *mstable_, "TIME" ) ;
286              msTimeCol.put( prevnr, mTime ) ;
287              msTimeCol.attach( *mstable_, "TIME_CENTROID" ) ;
288              msTimeCol.put( prevnr, mTime ) ;
289             
290              // INTERVAL and EXPOSURE
291              ROScalarColumn<Double> intervalCol( t5, "INTERVAL" ) ;
292              Double interval = intervalCol( 0 ) ;
293              ScalarColumn<Double> msIntervalCol( *mstable_, "INTERVAL" ) ;
294              msIntervalCol.put( prevnr, interval ) ;
295              msIntervalCol.attach( *mstable_, "EXPOSURE" ) ;
296              msIntervalCol.put( prevnr, interval ) ;
297
298              // WEIGHT and SIGMA
299              // always 1 at the moment
300              Vector<Float> wArr( nrow, 1.0 ) ;
301              ArrayColumn<Float> wArrCol( *mstable_, "WEIGHT" ) ;
302              wArrCol.put( prevnr, wArr ) ;
303              wArrCol.attach( *mstable_, "SIGMA" ) ;
304              wArrCol.put( prevnr, wArr ) ;
305             
306              // add DATA_DESCRIPTION row
307              Int ddid = addDataDescription( polid, ifNo ) ;
308              os_ << "ddid = " << ddid << LogIO::POST ;
309              ScalarColumn<Int> ddIdCol( *mstable_, "DATA_DESC_ID" ) ;
310              ddIdCol.put( prevnr, ddid ) ;
311             
312              // for SYSCAL table
313              ROScalarColumn<uInt> tcalIdCol( t5, "TCAL_ID" ) ;
314              Vector<uInt> tcalIdArr = tcalIdCol.getColumn() ;
315              os_ << "tcalIdArr = " << tcalIdArr << LogIO::POST ;
316              String key = String::toString( tcalIdArr[0] ) ;
317              if ( !tcalIdRec_.isDefined( key ) ) {
318                tcalIdRec_.define( key, tcalIdArr ) ;
319              }
320             
321              // fill STATE_ID
322              ROScalarColumn<Int> srcTypeCol( t5, "SRCTYPE" ) ;
323              Int srcType = srcTypeCol( 0 ) ;
324              Int stateId = addState( srcType, subscan ) ;
325              ScalarColumn<Int> msStateIdCol( *mstable_, "STATE_ID" ) ;
326              msStateIdCol.put( prevnr, stateId ) ;
327
328              // for POINTING table
329              Matrix<Double> msDir( 2, 2 ) ;
330              ROArrayColumn<Double> dirCol( t5, "DIRECTION" ) ;
331              msDir.column( 0 ) = dirCol( 0 ) ;
332              dirCol.attach( t5, "SCANRATE" ) ;
333              msDir.column( 1 ) = dirCol( 0 ) ;
334              addPointing( fieldName, mTime, interval, msDir ) ;
335
336              added5 += 1 ;
337              os_ << "added5 = " << added5 << " current5 = " << current5 << LogIO::POST ;
338              iter5.next() ;
339            }
340           
341            added4 += added5 ;
342            os_ << "added4 = " << added4 << " current4 = " << current4 << LogIO::POST ;
343            iter4.next() ;
344          }
345
346          // add SPECTRAL_WINDOW row
347          if ( allNE( processedFreqId, freqId ) ) {
348            uInt vsize = processedFreqId.size() ;
349            processedFreqId.resize( vsize+1, True ) ;
350            processedFreqId[vsize] = freqId ;
351            addSpectralWindow( ifNo, freqId ) ;
352          }
353         
354          added3 += added4 ;
355          os_ << "added3 = " << added3 << " current3 = " << current3 << LogIO::POST ;
356          iter3.next() ;
357        }
358       
359        // SCAN_NUMBER
360        // MS: 1-based
361        // Scantable: 0-based
362        RefRows rows3( current3, current3+added3-1 ) ;
363        Vector<Int> scanNum( added3, scanNo+1 ) ;
364        ScalarColumn<Int> scanNumCol( *mstable_, "SCAN_NUMBER" ) ;
365        scanNumCol.putColumnCells( rows3, scanNum ) ;
366       
367        added2 += added3 ;
368        os_ << "added2 = " << added2 << " current2 = " << current2 << LogIO::POST ;
369        iter2.next() ;
370      }
371     
372      // FEED1 and FEED2
373      RefRows rows2( current2, current2+added2-1 ) ;
374      Vector<Int> feedId( added2, beamNo ) ;
375      ScalarColumn<Int> feedCol( *mstable_, "FEED1" ) ;
376      feedCol.putColumnCells( rows2, feedId ) ;
377      feedCol.attach( *mstable_, "FEED2" ) ;
378      feedCol.putColumnCells( rows2, feedId ) ;
379     
380      // add FEED row
381      addFeed( beamNo ) ;
382     
383      added1 += added2 ;
384      os_ << "added1 = " << added1 << " current1 = " << current1 << LogIO::POST ;
385      iter1.next() ;
386    }
387   
388    // FIELD_ID
389    RefRows rows1( current1, current1+added1-1 ) ;
390    Vector<Int> fieldIds( added1, fieldId ) ;
391    ScalarColumn<Int> fieldIdCol( *mstable_, "FIELD_ID" ) ;
392    fieldIdCol.putColumnCells( rows1, fieldIds ) ;
393
394    // add FIELD row
395    addField( fieldId, fieldName, srcName, minTime, scanRate ) ;
396
397    added0 += added1 ;
398    os_ << "added0 = " << added0 << " current0 = " << current0 << LogIO::POST ;
399    iter0.next() ;
400  }
401
402  // OBSERVATION_ID is always 0
403  ScalarColumn<Int> sharedIntCol( *mstable_, "OBSERVATION_ID" ) ;
404  Vector<Int> sharedIntArr( added0, 0 ) ;
405  sharedIntCol.putColumn( sharedIntArr ) ;
406
407  // ANTENNA1 and ANTENNA2 are always 0
408  sharedIntArr = 0 ;
409  sharedIntCol.attach( *mstable_, "ANTENNA1" ) ;
410  sharedIntCol.putColumn( sharedIntArr ) ;
411  sharedIntCol.attach( *mstable_, "ANTENNA2" ) ;
412  sharedIntCol.putColumn( sharedIntArr ) ;
413
414  // ARRAY_ID is tentatively set to 0
415  sharedIntArr = 0 ;
416  sharedIntCol.attach( *mstable_, "ARRAY_ID" ) ;
417  sharedIntCol.putColumn( sharedIntArr ) ;
418
419  // PROCESSOR_ID is tentatively set to 0
420  sharedIntArr = 0 ;
421  sharedIntCol.attach( *mstable_, "PROCESSOR_ID" ) ;
422  sharedIntCol.putColumn( sharedIntArr ) ;
423
424
425  // SYSCAL
426  fillSysCal() ;
427
428  return True ;
429}
430 
431void MSWriter::init()
432{
433//   os_.origin( LogOrigin( "MSWriter", "init()", WHERE ) ) ;
434  os_ << "MSWriter::init()" << LogIO::POST ;
435 
436  // access to scantable
437  header_ = table_->getHeader() ;
438
439  // FLOAT_DATA? or DATA?
440  if ( header_.npol > 2 ) {
441    useFloatData_ = False ;
442    useData_ = True ;
443  }
444  else {
445    useFloatData_ = True ;
446    useData_ = False ;
447  }
448
449  // polarization type
450  polType_ = header_.poltype ;
451  if ( polType_ == "" )
452    polType_ = "stokes" ;
453  else if ( polType_.find( "linear" ) != String::npos )
454    polType_ = "linear" ;
455  else if ( polType_.find( "circular" ) != String::npos )
456    polType_ = "circular" ;
457  else if ( polType_.find( "stokes" ) != String::npos )
458    polType_ = "stokes" ;
459  else if ( polType_.find( "linpol" ) != String::npos )
460    polType_ = "linpol" ;
461  else
462    polType_ = "notype" ;
463
464  // Are TCAL_SPECTRUM and TSYS_SPECTRUM necessary?
465  tcalSpec_ = False ;
466  tsysSpec_ = False ;
467  if ( header_.nchan != 1 ) {
468    // examine TCAL subtable
469    Table tcaltab = table_->tcal().table() ;
470    ROArrayColumn<Float> tcalCol( tcaltab, "TCAL" ) ;
471    for ( uInt irow = 0 ; irow < tcaltab.nrow() ; irow++ ) {
472      if ( tcalCol( irow ).size() != 1 )
473        tcalSpec_ = True ;
474    }
475    // examine spectral data
476    TableIterator iter0( table_->table(), "IFNO" ) ;
477    while( !iter0.pastEnd() ) {
478      Table t0( iter0.table() ) ;
479      ROArrayColumn<Float> sharedFloatArrCol( t0, "SPECTRA" ) ;
480      uInt len = sharedFloatArrCol( 0 ).size() ;
481      if ( len != 1 ) {
482        sharedFloatArrCol.attach( t0, "TSYS" ) ;
483        if ( sharedFloatArrCol( 0 ).size() != 1 )
484          tsysSpec_ = True ;
485      }
486      iter0.next() ;
487    }
488  }
489}
490
491void MSWriter::setupMS()
492{
493//   os_.origin( LogOrigin( "MSWriter", "setupMS()", WHERE ) ) ;
494  os_ << "MSWriter::setupMS()" << LogIO::POST ;
495
496  TableDesc msDesc = MeasurementSet::requiredTableDesc() ;
497  if ( useFloatData_ )
498    MeasurementSet::addColumnToDesc( msDesc, MSMainEnums::FLOAT_DATA, 2 ) ;
499  else if ( useData_ )
500    MeasurementSet::addColumnToDesc( msDesc, MSMainEnums::DATA, 2 ) ;
501
502  SetupNewTable newtab( filename_, msDesc, Table::New ) ;
503
504  mstable_ = new MeasurementSet( newtab ) ;
505
506  // create subtables
507  TableDesc antennaDesc = MSAntenna::requiredTableDesc() ;
508  SetupNewTable antennaTab( mstable_->antennaTableName(), antennaDesc, Table::New ) ;
509  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::ANTENNA ), Table( antennaTab ) ) ;
510
511  TableDesc dataDescDesc = MSDataDescription::requiredTableDesc() ;
512  SetupNewTable dataDescTab( mstable_->dataDescriptionTableName(), dataDescDesc, Table::New ) ;
513  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::DATA_DESCRIPTION ), Table( dataDescTab ) ) ;
514
515  TableDesc dopplerDesc = MSDoppler::requiredTableDesc() ;
516  SetupNewTable dopplerTab( mstable_->dopplerTableName(), dopplerDesc, Table::New ) ;
517  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::DOPPLER ), Table( dopplerTab ) ) ;
518
519  TableDesc feedDesc = MSFeed::requiredTableDesc() ;
520  SetupNewTable feedTab( mstable_->feedTableName(), feedDesc, Table::New ) ;
521  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FEED ), Table( feedTab ) ) ;
522
523  TableDesc fieldDesc = MSField::requiredTableDesc() ;
524  SetupNewTable fieldTab( mstable_->fieldTableName(), fieldDesc, Table::New ) ;
525  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FIELD ), Table( fieldTab ) ) ;
526
527  TableDesc flagCmdDesc = MSFlagCmd::requiredTableDesc() ;
528  SetupNewTable flagCmdTab( mstable_->flagCmdTableName(), flagCmdDesc, Table::New ) ;
529  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FLAG_CMD ), Table( flagCmdTab ) ) ;
530
531  TableDesc freqOffsetDesc = MSFreqOffset::requiredTableDesc() ;
532  SetupNewTable freqOffsetTab( mstable_->freqOffsetTableName(), freqOffsetDesc, Table::New ) ;
533  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::FREQ_OFFSET ), Table( freqOffsetTab ) ) ;
534
535  TableDesc historyDesc = MSHistory::requiredTableDesc() ;
536  SetupNewTable historyTab( mstable_->historyTableName(), historyDesc, Table::New ) ;
537  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::HISTORY ), Table( historyTab ) ) ;
538
539  TableDesc observationDesc = MSObservation::requiredTableDesc() ;
540  SetupNewTable observationTab( mstable_->observationTableName(), observationDesc, Table::New ) ;
541  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::OBSERVATION ), Table( observationTab ) ) ;
542
543  TableDesc pointingDesc = MSPointing::requiredTableDesc() ;
544  SetupNewTable pointingTab( mstable_->pointingTableName(), pointingDesc, Table::New ) ;
545  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::POINTING ), Table( pointingTab ) ) ;
546
547  TableDesc polarizationDesc = MSPolarization::requiredTableDesc() ;
548  SetupNewTable polarizationTab( mstable_->polarizationTableName(), polarizationDesc, Table::New ) ;
549  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::POLARIZATION ), Table( polarizationTab ) ) ;
550
551  TableDesc processorDesc = MSProcessor::requiredTableDesc() ;
552  SetupNewTable processorTab( mstable_->processorTableName(), processorDesc, Table::New ) ;
553  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::PROCESSOR ), Table( processorTab ) ) ;
554
555  TableDesc sourceDesc = MSSource::requiredTableDesc() ;
556  MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::TRANSITION, 1 ) ;
557  MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::REST_FREQUENCY, 1 ) ;
558  MSSource::addColumnToDesc( sourceDesc, MSSourceEnums::SYSVEL, 1 ) ;
559  SetupNewTable sourceTab( mstable_->sourceTableName(), sourceDesc, Table::New ) ;
560  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SOURCE ), Table( sourceTab ) ) ;
561
562  TableDesc spwDesc = MSSpectralWindow::requiredTableDesc() ;
563  SetupNewTable spwTab( mstable_->spectralWindowTableName(), spwDesc, Table::New ) ;
564  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SPECTRAL_WINDOW ), Table( spwTab ) ) ;
565
566  TableDesc stateDesc = MSState::requiredTableDesc() ;
567  SetupNewTable stateTab( mstable_->stateTableName(), stateDesc, Table::New ) ;
568  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::STATE ), Table( stateTab ) ) ;
569
570  // TODO: add TCAL_SPECTRUM and TSYS_SPECTRUM if necessary
571  TableDesc sysCalDesc = MSSysCal::requiredTableDesc() ;
572  MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TCAL, 2 ) ;
573  MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TSYS, 2 ) ;
574  if ( tcalSpec_ )
575    MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TCAL_SPECTRUM, 2 ) ;
576  if ( tsysSpec_ )
577    MSSysCal::addColumnToDesc( sysCalDesc, MSSysCalEnums::TSYS_SPECTRUM, 2 ) ;
578  SetupNewTable sysCalTab( mstable_->sysCalTableName(), sysCalDesc, Table::New ) ;
579  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::SYSCAL ), Table( sysCalTab ) ) ;
580
581  TableDesc weatherDesc = MSWeather::requiredTableDesc() ;
582  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::TEMPERATURE ) ;
583  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::PRESSURE ) ;
584  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::REL_HUMIDITY ) ;
585  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::WIND_SPEED ) ;
586  MSWeather::addColumnToDesc( weatherDesc, MSWeatherEnums::WIND_DIRECTION ) ;
587  SetupNewTable weatherTab( mstable_->weatherTableName(), weatherDesc, Table::New ) ;
588  mstable_->rwKeywordSet().defineTable( MeasurementSet::keywordName( MeasurementSet::WEATHER ), Table( weatherTab ) ) ;
589
590  mstable_->initRefs() ;
591
592}
593
594void MSWriter::fillObservation()
595{
596  os_ << "set up OBSERVATION subtable" << LogIO::POST ;
597  // only 1 row
598  mstable_->observation().addRow( 1, True ) ;
599  MSObservationColumns msObsCols( mstable_->observation() ) ;
600  msObsCols.observer().put( 0, header_.observer ) ;
601  // tentatively put antennaname (from ANTENNA subtable)
602  String hAntennaName = header_.antennaname ;
603  String::size_type pos = hAntennaName.find( "//" ) ;
604  String telescopeName ;
605  if ( pos != String::npos ) {
606    telescopeName = hAntennaName.substr( 0, pos ) ;
607  }
608  else {
609    pos = hAntennaName.find( "@" ) ;
610    telescopeName = hAntennaName.substr( 0, pos ) ;
611  }
612  os_ << "telescopeName = " << telescopeName << LogIO::POST ;
613  msObsCols.telescopeName().put( 0, telescopeName ) ;
614  msObsCols.project().put( 0, header_.project ) ;
615  //ScalarMeasColumn<MEpoch> timeCol( table_->table().sort("TIME"), "TIME" ) ;
616  Table sortedtable = table_->table().sort("TIME") ;
617  ScalarMeasColumn<MEpoch> timeCol( sortedtable, "TIME" ) ;
618  Vector<MEpoch> trange( 2 ) ;
619  trange[0] = timeCol( 0 ) ;
620  trange[1] = timeCol( table_->nrow()-1 ) ;
621  msObsCols.timeRangeMeas().put( 0, trange ) ;
622}
623
624void MSWriter::fillAntenna()
625{
626  os_ << "set up ANTENNA subtable" << LogIO::POST ;
627  // only 1 row
628  mstable_->antenna().addRow( 1, True ) ;
629  MSAntennaColumns msAntCols( mstable_->antenna() ) ;
630
631  String hAntennaName = header_.antennaname ;
632  String::size_type pos = hAntennaName.find( "//" ) ;
633  String antennaName ;
634  String stationName ;
635  if ( pos != String::npos ) {
636    hAntennaName = hAntennaName.substr( pos+2 ) ;
637  }
638  pos = hAntennaName.find( "@" ) ;
639  if ( pos != String::npos ) {
640    antennaName = hAntennaName.substr( 0, pos ) ;
641    stationName = hAntennaName.substr( pos+1 ) ;
642  }
643  else {
644    antennaName = hAntennaName ;
645    stationName = hAntennaName ;
646  }
647  os_ << "antennaName = " << antennaName << LogIO::POST ;
648  os_ << "stationName = " << stationName << LogIO::POST ;
649 
650  msAntCols.name().put( 0, antennaName ) ;
651  msAntCols.station().put( 0, stationName ) ;
652
653  os_ << "antennaPosition = " << header_.antennaposition << LogIO::POST ;
654 
655  msAntCols.position().put( 0, header_.antennaposition ) ;
656}
657
658void MSWriter::fillProcessor()
659{
660  os_ << "set up PROCESSOR subtable" << LogIO::POST ;
661 
662  // only add empty 1 row
663  MSProcessor msProc = mstable_->processor() ;
664  msProc.addRow( 1, True ) ;
665}
666
667void MSWriter::fillSource()
668{
669  os_ << "set up SOURCE subtable" << LogIO::POST ;
670 
671
672  // access to MS SOURCE subtable
673  MSSource msSrc = mstable_->source() ;
674
675  // access to MOLECULE subtable
676  STMolecules stm = table_->molecules() ;
677
678  Int srcId = 0 ;
679  Vector<Double> restFreq ;
680  Vector<String> molName ;
681  Vector<String> fMolName ;
682  //
683  // ITERATION: SRCNAME
684  //
685  Int added0 = 0 ;
686  Int current0 = msSrc.nrow() ;
687  TableIterator iter0( table_->table(), "SRCNAME" ) ;
688  while( !iter0.pastEnd() ) {
689    Table t0( iter0.table() ) ;
690
691    // get necessary information
692    ROScalarColumn<String> srcNameCol( t0, "SRCNAME" ) ;
693    String srcName = srcNameCol( 0 ) ;
694    ROArrayColumn<Double> sharedDArrRCol( t0, "SRCPROPERMOTION" ) ;
695    Vector<Double> srcPM = sharedDArrRCol( 0 ) ;
696    sharedDArrRCol.attach( t0, "SRCDIRECTION" ) ;
697    Vector<Double> srcDir = sharedDArrRCol( 0 ) ;
698    ROScalarColumn<Double> srcVelCol( t0, "SRCVELOCITY" ) ;
699    Double srcVel = srcVelCol( 0 ) ;
700
701    //
702    // ITERATION: MOLECULE_ID
703    //
704    Int added1 = 0 ;
705    Int current1 = msSrc.nrow() ;
706    TableIterator iter1( t0, "MOLECULE_ID" ) ;
707    while( !iter1.pastEnd() ) {
708      Table t1( iter1.table() ) ;
709
710      // get necessary information
711      ROScalarColumn<uInt> molIdCol( t1, "MOLECULE_ID" ) ;
712      uInt molId = molIdCol( 0 ) ;
713      stm.getEntry( restFreq, molName, fMolName, molId ) ;
714
715      //
716      // ITERATION: IFNO
717      //
718      Int added2 = 0 ;
719      Int current2 = msSrc.nrow() ;
720      TableIterator iter2( t1, "IFNO" ) ;
721      while( !iter2.pastEnd() ) {
722        Table t2( iter2.table() ) ;
723        uInt prevnr = msSrc.nrow() ;
724
725        // get necessary information
726        ROScalarColumn<uInt> ifNoCol( t2, "IFNO" ) ;
727        uInt ifno = ifNoCol( 0 ) ; // IFNO = SPECTRAL_WINDOW_ID
728        MEpoch midTimeMeas ;
729        Double interval ;
730        getValidTimeRange( midTimeMeas, interval, t2 ) ;
731
732        // add row
733        msSrc.addRow( 1, True ) ;
734
735        // fill SPECTRAL_WINDOW_ID
736        ScalarColumn<Int> sSpwIdCol( msSrc, "SPECTRAL_WINDOW_ID" ) ;
737        sSpwIdCol.put( prevnr, ifno ) ;
738
739        // fill TIME, INTERVAL
740        ScalarMeasColumn<MEpoch> sTimeMeasCol( msSrc, "TIME" ) ;
741        sTimeMeasCol.put( prevnr, midTimeMeas ) ;
742        ScalarColumn<Double> sIntervalCol( msSrc, "INTERVAL" ) ;
743        sIntervalCol.put( prevnr, interval ) ;
744
745        added2++ ;
746        iter2.next() ;
747      }
748
749      // fill NUM_LINES, REST_FREQUENCY, TRANSITION, SYSVEL
750      RefRows rows2( current2, current2+added2-1 ) ;
751      uInt numFreq = restFreq.size() ;
752      Vector<Int> numLines( added2, numFreq ) ;
753      ScalarColumn<Int> numLinesCol( msSrc, "NUM_LINES" ) ;
754      numLinesCol.putColumnCells( rows2, numLines ) ;
755      if ( numFreq != 0 ) {
756        Array<Double> rf( IPosition( 2, numFreq, added2 ) ) ;
757        Array<String> trans( IPosition( 2, numFreq, added2 ) ) ;
758        Array<Double> srcVelArr( IPosition( 2, numFreq, added2 ), srcVel ) ;
759        for ( uInt ifreq = 0 ; ifreq < numFreq ; ifreq++ ) {
760          Slicer slice( Slice(ifreq),Slice(0,added2,1) ) ;
761          rf( slice ) = restFreq[ifreq] ;
762          String transStr = fMolName[ifreq] ;
763          if ( transStr.size() == 0 ) {
764            transStr = molName[ifreq] ;
765          }
766          trans( slice ) = transStr ;
767        }
768        ArrayColumn<Double> sharedDArrCol( msSrc, "REST_FREQUENCY" ) ;
769        sharedDArrCol.putColumnCells( rows2, rf ) ;
770        sharedDArrCol.attach( msSrc, "SYSVEL" ) ;
771        sharedDArrCol.putColumnCells( rows2, srcVelArr ) ;
772        ArrayColumn<String> transCol( msSrc, "TRANSITION" ) ;
773        transCol.putColumnCells( rows2, trans ) ;
774      }
775
776      added1 += added2 ;
777      iter1.next() ;
778    }
779
780    // fill NAME, SOURCE_ID
781    RefRows rows1( current1, current1+added1-1 ) ;
782    Vector<String> nameArr( added1, srcName ) ;
783    Vector<Int> srcIdArr( added1, srcId ) ;
784    ScalarColumn<String> sNameCol( msSrc, "NAME" ) ;
785    ScalarColumn<Int> srcIdCol( msSrc, "SOURCE_ID" ) ;
786    sNameCol.putColumnCells( rows1, nameArr ) ;
787    srcIdCol.putColumnCells( rows1, srcIdArr ) ;
788
789    // fill DIRECTION, PROPER_MOTION
790    Array<Double> srcPMArr( IPosition( 2, 2, added1 ) ) ;
791    Array<Double> srcDirArr( IPosition( 2, 2, added1 ) ) ;
792    for ( uInt i = 0 ; i < 2 ; i++ ) {
793      Slicer slice( Slice(i),Slice(0,added1,1) ) ;
794      srcPMArr( slice ) = srcPM[i] ;
795      srcDirArr( slice ) = srcDir[i] ;
796    }
797    ArrayColumn<Double> sharedDArrCol( msSrc, "DIRECTION" ) ;
798    sharedDArrCol.putColumnCells( rows1, srcDirArr ) ;
799    sharedDArrCol.attach( msSrc, "PROPER_MOTION" ) ;
800    sharedDArrCol.putColumnCells( rows1, srcPMArr ) ;
801
802    // fill TIME, INTERVAL
803
804    // increment srcId if SRCNAME changed
805    srcId++ ;
806
807    added0 += added1 ;
808    iter0.next() ;
809  }
810}
811
812void MSWriter::fillWeather()
813{
814  os_ << "set up WEATHER subtable" << LogIO::POST ;
815
816  // access to MS WEATHER subtable
817  MSWeather msw = mstable_->weather() ;
818
819  // access to WEATHER subtable
820  Table stw = table_->weather().table() ;
821  uInt nrow = stw.nrow() ;
822
823  if ( nrow == 0 )
824    return ;
825
826  msw.addRow( nrow, True ) ;
827  MSWeatherColumns mswCols( msw ) ;
828
829  // ANTENNA_ID is always 0
830  Vector<Int> antIdArr( nrow, 0 ) ;
831  mswCols.antennaId().putColumn( antIdArr ) ;
832
833  // fill weather status
834  ROScalarColumn<Float> sharedFloatCol( stw, "TEMPERATURE" ) ;
835  mswCols.temperature().putColumn( sharedFloatCol ) ;
836  sharedFloatCol.attach( stw, "PRESSURE" ) ;
837  mswCols.pressure().putColumn( sharedFloatCol ) ;
838  sharedFloatCol.attach( stw, "HUMIDITY" ) ;
839  mswCols.relHumidity().putColumn( sharedFloatCol ) ;
840  sharedFloatCol.attach( stw, "WINDSPEED" ) ;
841  mswCols.windSpeed().putColumn( sharedFloatCol ) ;
842  sharedFloatCol.attach( stw, "WINDAZ" ) ;
843  mswCols.windDirection().putColumn( sharedFloatCol ) ;
844
845  // fill TIME and INTERVAL
846  MEpoch me ;
847  Double interval ;
848  Vector<Double> intervalArr( nrow, 0.0 ) ;
849  TableIterator iter( table_->table(), "WEATHER_ID" ) ;
850  while( !iter.pastEnd() ) {
851    Table tab( iter.table() ) ;
852
853    ROScalarColumn<uInt> widCol( tab, "WEATHER_ID" ) ;
854    uInt wid = widCol( 0 ) ;
855
856    getValidTimeRange( me, interval, tab ) ;
857    mswCols.timeMeas().put( wid, me ) ;
858    intervalArr[wid] = interval ;
859
860    iter.next() ;
861  }
862  mswCols.interval().putColumn( intervalArr ) ;
863}
864
865void MSWriter::fillSysCal()
866{
867  os_ << "set up SYSCAL subtable" << LogIO::POST ;
868
869  tcalIdRec_.print( cout ) ;
870
871  // access to MS SYSCAL subtable
872  MSSysCal mssc = mstable_->sysCal() ;
873
874  // access to TCAL subtable
875  STTcal stt = table_->tcal() ;
876  uInt nrow = stt.table().nrow() ;
877
878  if ( nrow == 0 )
879    return ;
880
881  nrow = tcalIdRec_.nfields() ;
882
883  MEpoch me ;
884  Double interval ;
885  String timeStr ;
886  //
887  // ITERATION: BEAMNO
888  //
889  Int added0 = 0 ;
890  Int current0 = mssc.nrow() ;
891  TableIterator iter0( table_->table(), "BEAMNO" ) ;
892  while( !iter0.pastEnd() ) {
893    Table t0( iter0.table() ) ;
894    ROScalarColumn<uInt> beamNoCol( t0, "BEAMNO" ) ;
895    uInt beamNo = beamNoCol( 0 ) ;
896    //
897    // ITERATION: IFNO
898    //
899    Int added1 = 0 ;
900    Int current1 = mssc.nrow() ;
901    TableIterator iter1( t0, "IFNO" ) ;
902    while( !iter1.pastEnd() ) {
903      Table t1( iter1.table() ) ;
904      ROScalarColumn<uInt> ifNoCol( t1, "IFNO" ) ;
905      uInt ifNo = ifNoCol( 0 ) ;
906      uInt prevnr = mssc.nrow() ;
907
908      //
909      // LOOP: TCAL_ID
910      //
911      Int added2 = 0 ;
912      Int current2 = mssc.nrow() ;
913      for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
914        Vector<uInt> ids = tcalIdRec_.asArrayuInt( irow ) ;
915        uInt npol = ids.size() ;
916        Table tsel = t1( t1.col("TCAL_ID").in(ids) ) ;
917        os_ << "nrow = " << tsel.nrow() << "@TCAL_ID " << tcalIdRec_.asArrayuInt(irow) << " beamno " << beamNo << "ifno " << ifNo << LogIO::POST ;
918        if ( tsel.nrow() != 0 ) {
919          uInt idx = current2 + added2 ;
920
921          // TIME and INTERVAL
922          mssc.addRow( 1, True ) ;
923          getValidTimeRange( me, interval, t1 ) ;
924          os_ << "me = " << me.get("s").getValue() << " interval = " << interval << LogIO::POST ;
925          ScalarMeasColumn<MEpoch> timeMeasCol( mssc, "TIME" ) ;
926          timeMeasCol.put( idx, me ) ;
927          ScalarColumn<Double> intervalCol( mssc, "INTERVAL" ) ;
928          intervalCol.put( idx, interval ) ;
929
930          // TCAL and TSYS
931          Array<Float> tcal ;
932          Array<Float> tsys ;
933          Vector<Float> dummyC ;
934          stt.getEntry( timeStr, dummyC, ids[0] ) ;
935          os_ << "dummyC[0] = " << dummyC[0] << LogIO::POST ;
936          uInt nchanC = dummyC.size() ;
937          os_ << "nchanC = " << nchanC << LogIO::POST ;
938          tcal.resize( IPosition(2,npol,nchanC) ) ;
939          IPosition shapeC( 2, 1, nchanC ) ;
940          tcal( Slicer(Slice(0),Slice(0,nchanC,1)) ) = dummyC.reform( shapeC ) ;
941          Table tsel1 = tsel( tsel.col("TCAL_ID") == ids[0] ) ;
942          ROArrayColumn<Float> tsysCol( tsel1, "TSYS" ) ;
943          Vector<Float> dummyS = tsysCol( 0 ) ;
944          os_ << "dummyS[0] = " << dummyS[0] << LogIO::POST ;
945          uInt nchanS = dummyS.size() ;
946          os_ << "nchanS = " << nchanS << LogIO::POST ;
947          IPosition shapeS( 2, 1, nchanS ) ;
948          tsys.resize( IPosition(2,npol,nchanS) ) ;
949          tsys( Slicer(Slice(0),Slice(0,nchanS,1)) ) = dummyS.reform( shapeS ) ;
950          os_ << "tsys = " << tsys << LogIO::POST ;
951          for ( uInt iid = 1 ; iid < npol ; iid++ ) {
952            // get TCAL and TSYS
953            stt.getEntry( timeStr, dummyC, ids[iid] ) ;
954            tcal( Slicer(Slice(iid),Slice(0,nchanC,1)) ) = dummyC.reform( shapeC ) ;
955            tsel1 = tsel( tsel.col("TCAL_ID") == ids[iid] ) ;
956            tsysCol.attach( tsel1, "TSYS" ) ;
957            tsys( Slicer(Slice(iid),Slice(0,nchanS,1)) ) = tsysCol( 0 ).reform( shapeS ) ;
958          }
959          os_ << "tsys = " << tsys << LogIO::POST ;
960          ArrayColumn<Float> sharedFloatArrCol ;
961          if ( tcalSpec_ ) {
962            // put TCAL_SPECTRUM
963            sharedFloatArrCol.attach( mssc, "TCAL_SPECTRUM" ) ;
964            sharedFloatArrCol.put( idx, tcal ) ;
965            // set TCAL (mean of TCAL_SPECTRUM)
966            Vector<Float> tcalMean( npol ) ;
967            for ( uInt iid = 0 ; iid < npol ; iid++ ) {
968              tcalMean[iid] = mean( tcal(Slicer(Slice(iid),Slice(0,nchanC,1))) ) ;
969            }
970            tcal.assign( tcalMean.reform(IPosition(2,npol,1)) ) ;
971          }
972          os_ << "tcal = " << tcal << LogIO::POST ;
973          // put TCAL
974          sharedFloatArrCol.attach( mssc, "TCAL" ) ;
975          sharedFloatArrCol.put( idx, tcal ) ;
976         
977          if ( tsysSpec_ ) {
978            // put TSYS_SPECTRUM
979            sharedFloatArrCol.attach( mssc, "TSYS_SPECTRUM" ) ;
980            sharedFloatArrCol.put( idx, tsys ) ;
981            // set TSYS (mean of TSYS_SPECTRUM)
982            Vector<Float> tsysMean( npol ) ;
983            for ( uInt iid = 0 ; iid < npol ; iid++ ) {
984              tsysMean[iid] = mean( tsys(Slicer(Slice(iid),Slice(0,nchanS,1))) ) ;
985            }
986            tsys.assign( tsysMean.reform(IPosition(2,npol,1)) ) ;
987          }
988          os_ << "tsys = " << tsys << LogIO::POST ;
989          // put TSYS
990          sharedFloatArrCol.attach( mssc, "TSYS" ) ;
991          sharedFloatArrCol.put( idx, tsys ) ;
992
993          added2++ ;
994        }
995      }
996
997      // SPECTRAL_WINDOW_ID
998      RefRows rows2( current2, current2+added2-1 ) ;
999      ScalarColumn<Int> spwIdCol( mssc, "SPECTRAL_WINDOW_ID" ) ;
1000      Vector<Int> ifNoArr( added2, ifNo ) ;
1001      spwIdCol.putColumnCells( rows2, ifNoArr ) ;
1002
1003      added1 += added2 ;
1004      iter1.next() ;
1005    }
1006   
1007    // FEED_ID
1008    RefRows rows1( current1, current1+added1-1 ) ;
1009    Vector<Int> beamNoArr( added1, beamNo ) ;
1010    ScalarColumn<Int> feedIdCol( mssc, "FEED_ID" ) ;
1011    feedIdCol.putColumnCells( rows1, beamNoArr ) ;
1012
1013    added0 += added1 ;
1014    iter0.next() ;
1015  }
1016
1017  // ANTENNA_ID
1018  Vector<Int> id( added0, 0 ) ;
1019  ScalarColumn<Int> antennaIdCol( mssc, "ANTENNA_ID" ) ;
1020  antennaIdCol.putColumn( id ) ;
1021 
1022}
1023
1024void MSWriter::addFeed( Int id )
1025{
1026  os_ << "set up FEED subtable" << LogIO::POST ;
1027
1028  // add row
1029  MSFeed msFeed = mstable_->feed() ;
1030  msFeed.addRow( 1, True ) ;
1031  Int nrow = msFeed.nrow() ;
1032
1033  MSFeedColumns msFeedCols( mstable_->feed() ) ;
1034
1035  msFeedCols.feedId().put( nrow-1, id ) ;
1036  msFeedCols.antennaId().put( nrow-1, 0 ) ;
1037}
1038
1039void MSWriter::addSpectralWindow( Int spwid, Int freqid )
1040{
1041  os_ << "set up SPECTRAL_WINDOW subtable" << LogIO::POST ;
1042 
1043  // add row
1044  MSSpectralWindow msSpw = mstable_->spectralWindow() ;
1045  while( (Int)msSpw.nrow() <= spwid ) {
1046    msSpw.addRow( 1, True ) ;
1047  }
1048 
1049  MSSpWindowColumns msSpwCols( msSpw ) ;
1050
1051  STFrequencies stf = table_->frequencies() ;
1052
1053  // MEAS_FREQ_REF
1054  msSpwCols.measFreqRef().put( spwid, stf.getFrame( True ) ) ;
1055
1056  Double refpix ;
1057  Double refval ;
1058  Double inc ;
1059  stf.getEntry( refpix, refval, inc, (uInt)freqid ) ;
1060
1061  // NUM_CHAN
1062  Int nchan = refpix * 2 ;
1063  msSpwCols.numChan().put( spwid, nchan ) ;
1064
1065  // TOTAL_BANDWIDTH
1066  Double bw = nchan * inc ;
1067  msSpwCols.totalBandwidth().put( spwid, bw ) ;
1068
1069  // REF_FREQUENCY
1070  Double refFreq = refval - refpix * inc ;
1071  msSpwCols.refFrequency().put( spwid, refFreq ) ;
1072
1073  // NET_SIDEBAND
1074  // tentative: USB->0, LSB->1
1075  Int netSideband = 0 ;
1076  if ( inc < 0 )
1077    netSideband = 1 ;
1078  msSpwCols.netSideband().put( spwid, netSideband ) ;
1079
1080  // RESOLUTION, CHAN_WIDTH, EFFECTIVE_BW
1081  Vector<Double> sharedDoubleArr( nchan, inc ) ;
1082  msSpwCols.resolution().put( spwid, sharedDoubleArr ) ;
1083  msSpwCols.chanWidth().put( spwid, sharedDoubleArr ) ;
1084  msSpwCols.effectiveBW().put( spwid, sharedDoubleArr ) ;
1085
1086  // CHAN_FREQ
1087  indgen( sharedDoubleArr, refFreq, inc ) ;
1088  msSpwCols.chanFreq().put( spwid, sharedDoubleArr ) ;
1089}
1090
1091void MSWriter::addField( Int fid, String fieldname, String srcname, Double t, Vector<Double> rate )
1092{
1093  os_ << "set up FIELD subtable" << LogIO::POST ;
1094 
1095  MSField msField = mstable_->field() ;
1096  while( (Int)msField.nrow() <= fid ) {
1097    msField.addRow( 1, True ) ;
1098  }
1099  MSFieldColumns msFieldCols( msField ) ;
1100
1101  // Access to SOURCE table
1102  MSSource msSrc = mstable_->source() ;
1103
1104  // fill target row
1105  msFieldCols.name().put( fid, fieldname ) ;
1106  msFieldCols.time().put( fid, t ) ;
1107  Int numPoly = 0 ;
1108  if ( anyNE( rate, 0.0 ) )
1109    numPoly = 1 ;
1110  msFieldCols.numPoly().put( fid, numPoly ) ;
1111  MSSourceIndex msSrcIdx( msSrc ) ;
1112  Int srcId = -1 ;
1113  Vector<Int> srcIdArr = msSrcIdx.matchSourceName( srcname ) ;
1114  if ( srcIdArr.size() != 0 ) {
1115    srcId = srcIdArr[0] ;
1116    MSSource msSrcSel = msSrc( msSrc.col("SOURCE_ID") == srcId ) ;
1117    ROMSSourceColumns msSrcCols( msSrcSel ) ;
1118    Vector<Double> srcDir = msSrcCols.direction()( 0 ) ;
1119    Array<Double> srcDirA( IPosition( 2, 2, 1+numPoly ) ) ;
1120    os_ << "srcDirA = " << srcDirA << LogIO::POST ;
1121    os_ << "sliced srcDirA = " << srcDirA( Slicer( Slice(0,2,1), Slice(0) ) ) << LogIO::POST ;
1122    srcDirA( Slicer( Slice(0,2,1), Slice(0) ) ) = srcDir.reform( IPosition(2,2,1) ) ;
1123    os_ << "srcDirA = " << srcDirA << LogIO::POST ;
1124    if ( numPoly != 0 )
1125      srcDirA( Slicer( Slice(0,2,1), Slice(1) ) ) = rate ;
1126    msFieldCols.phaseDir().put( fid, srcDirA ) ;
1127    msFieldCols.referenceDir().put( fid, srcDirA ) ;
1128    msFieldCols.delayDir().put( fid, srcDirA ) ;
1129  }
1130  msFieldCols.sourceId().put( fid, srcId ) ;
1131}
1132
1133void MSWriter::addPointing( String &name, MEpoch &me, Double &interval, Matrix<Double> &dir )
1134{
1135  os_ << "set up POINTING subtable" << LogIO::POST ;
1136 
1137  // access to POINTING subtable
1138  MSPointing msp = mstable_->pointing() ;
1139  uInt nrow = msp.nrow() ;
1140
1141  // add row
1142  msp.addRow( 1, True ) ;
1143
1144  // fill row
1145  MSPointingColumns mspCols( msp ) ;
1146  mspCols.antennaId().put( nrow, 0 ) ;
1147  mspCols.timeMeas().put( nrow, me ) ;
1148  mspCols.interval().put( nrow, interval ) ;
1149  mspCols.name().put( nrow, name ) ;
1150  mspCols.numPoly().put( nrow, 1 ) ;
1151  mspCols.timeOriginMeas().put( nrow, me ) ;
1152  mspCols.direction().put( nrow, dir ) ;
1153  mspCols.target().put( nrow, dir ) ;
1154  mspCols.tracking().put( nrow, True ) ;
1155}
1156
1157Int MSWriter::addPolarization( Vector<Int> polnos )
1158{
1159  os_ << "set up POLARIZATION subtable" << LogIO::POST ;
1160
1161  os_ << "polnos = " << polnos << LogIO::POST ;
1162  MSPolarization msPol = mstable_->polarization() ;
1163  uInt nrow = msPol.nrow() ;
1164 
1165  Vector<Int> corrType = toCorrType( polnos ) ;
1166  os_ << "corrType = " << corrType << LogIO::POST ;
1167 
1168  MSPolarizationIndex msPolIdx( msPol ) ;
1169  Vector<Int> polids = msPolIdx.matchCorrType( corrType ) ;
1170  os_ << "polids = " << polids << LogIO::POST ;
1171
1172  Int polid = -1 ;
1173
1174  if ( polids.size() == 0 ) {
1175    // add row
1176    msPol.addRow( 1, True ) ;
1177    polid = (Int)nrow ;
1178
1179    MSPolarizationColumns msPolCols( msPol ) ;
1180   
1181    // CORR_TYPE
1182    msPolCols.corrType().put( nrow, corrType ) ;
1183
1184    // NUM_CORR
1185    uInt npol = corrType.size() ;
1186    msPolCols.numCorr().put( nrow, npol ) ;
1187
1188    // CORR_PRODUCT
1189    Matrix<Int> corrProd( 2, npol, -1 ) ;
1190    if ( npol == 1 ) {
1191      corrProd = 0 ;
1192    }
1193    else if ( npol == 2 ) {
1194      corrProd.column( 0 ) = 0 ;
1195      corrProd.column( 1 ) = 1 ;
1196    }
1197    else {
1198      corrProd.column( 0 ) = 0 ;
1199      corrProd.column( 3 ) = 1 ;
1200      corrProd( 0,1 ) = 0 ;
1201      corrProd( 1,1 ) = 1 ;
1202      corrProd( 0,2 ) = 1 ;
1203      corrProd( 1,2 ) = 0 ;
1204    }
1205    msPolCols.corrProduct().put( nrow, corrProd ) ;
1206   
1207  }
1208  else {
1209    polid = polids[0] ;
1210  }
1211
1212  return polid ;
1213}
1214
1215Int MSWriter::addDataDescription( Int polid, Int spwid )
1216{
1217  os_ << "set up DATA_DESCRIPTION subtable" << LogIO::POST ;
1218
1219  MSDataDescription msDataDesc = mstable_->dataDescription() ;
1220  uInt nrow = msDataDesc.nrow() ;
1221
1222  MSDataDescIndex msDataDescIdx( msDataDesc ) ;
1223 
1224  Vector<Int> ddids = msDataDescIdx.matchSpwIdAndPolznId( spwid, polid ) ;
1225  os_ << "ddids = " << ddids << LogIO::POST ;
1226
1227  Int ddid = -1 ;
1228  if ( ddids.size() == 0 ) {
1229    msDataDesc.addRow( 1, True ) ;
1230    MSDataDescColumns msDataDescCols( msDataDesc ) ;
1231    msDataDescCols.polarizationId().put( nrow, polid ) ;
1232    msDataDescCols.spectralWindowId().put( nrow, spwid ) ;
1233    ddid = (Int)nrow ;
1234  }
1235  else {
1236    ddid = ddids[0] ;
1237  }
1238
1239  return ddid ;
1240}
1241
1242Int MSWriter::addState( Int st, Int &subscan )
1243{
1244  os_ << "set up STATE subtable" << LogIO::POST ;
1245
1246  // access to STATE subtable
1247  MSState msState = mstable_->state() ;
1248  uInt nrow = msState.nrow() ;
1249
1250  String obsMode ;
1251  Bool isSignal ;
1252  queryType( st, obsMode, isSignal ) ;
1253  os_ << "obsMode = " << obsMode << " isSignal = " << isSignal << LogIO::POST ;
1254
1255  MSState msStatSel = msState( msState.col("OBS_MODE")==obsMode
1256                               && msState.col("SIG")==isSignal
1257                               && msState.col("REF")!=isSignal
1258                               && msState.col("SUB_SCAN") == subscan ) ;
1259  uInt nrowSel = msStatSel.nrow() ;
1260
1261  Int idx = -1 ;
1262  if ( nrowSel == 0 ) {
1263    msState.addRow( 1, True ) ;
1264    ScalarColumn<String> obsModeCol( msState, "OBS_MODE" ) ;
1265    obsModeCol.put( nrow, obsMode ) ;
1266    ScalarColumn<Bool> sharedBCol( msState, "SIG" ) ;
1267    sharedBCol.put( nrow, isSignal ) ;
1268    sharedBCol.attach( msState, "REF" ) ;
1269    sharedBCol.put( nrow, !isSignal ) ;
1270    ScalarColumn<Int> subscanCol( msState, "SUB_SCAN" ) ;
1271    subscanCol.put( nrow, subscan ) ;
1272    idx = nrow ;
1273  }
1274  else {
1275    ScalarColumn<String> obsModeCol( msState, "OBS_MODE" ) ;
1276    ScalarColumn<Bool> sigCol( msState, "SIG" ) ;
1277    ScalarColumn<Bool> refCol( msState, "REF" ) ;
1278    ScalarColumn<Int> subscanCol( msState, "SUB_SCAN" ) ;
1279    for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1280      if ( obsModeCol(irow) == obsMode
1281           && sigCol(irow) == isSignal
1282           && refCol(irow) != isSignal
1283           && subscanCol(irow) == subscan ) {
1284        idx = irow ;
1285        break ;
1286      }
1287    }
1288  }
1289  subscan++ ;
1290  return idx ;
1291}
1292
1293Vector<Int> MSWriter::toCorrType( Vector<Int> polnos )
1294{
1295  uInt npol = polnos.size() ;
1296  Vector<Int> corrType( npol, Stokes::Undefined ) ;
1297 
1298  if ( npol == 4 ) {
1299    if ( polType_ == "linear" ) {
1300      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1301        if ( polnos[ipol] == 0 )
1302          corrType[ipol] = Stokes::XX ;
1303        else if ( polnos[ipol] == 1 )
1304          corrType[ipol] = Stokes::XY ;
1305        else if ( polnos[ipol] == 2 )
1306          corrType[ipol] = Stokes::YX ;
1307        else if ( polnos[ipol] == 3 )
1308          corrType[ipol] = Stokes::YY ;
1309      }
1310    }
1311    else if ( polType_ == "circular" ) {
1312      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1313        if ( polnos[ipol] == 0 )
1314          corrType[ipol] = Stokes::RR ;
1315        else if ( polnos[ipol] == 1 )
1316          corrType[ipol] = Stokes::RL ;
1317        else if ( polnos[ipol] == 2 )
1318          corrType[ipol] = Stokes::LR ;
1319        else if ( polnos[ipol] == 3 )
1320          corrType[ipol] = Stokes::LL ;
1321      }
1322    }
1323    else if ( polType_ == "stokes" ) {
1324      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1325        if ( polnos[ipol] == 0 )
1326          corrType[ipol] = Stokes::I ;
1327        else if ( polnos[ipol] == 1 )
1328          corrType[ipol] = Stokes::Q ;
1329        else if ( polnos[ipol] == 2 )
1330          corrType[ipol] = Stokes::U ;
1331        else if ( polnos[ipol] == 3 )
1332          corrType[ipol] = Stokes::V ;
1333      }
1334    }
1335  }
1336  else if ( npol == 2 ) {
1337    if ( polType_ == "linear" ) {
1338      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1339        if ( polnos[ipol] == 0 )
1340          corrType[ipol] = Stokes::XX ;
1341        else if ( polnos[ipol] == 1 )
1342          corrType[ipol] = Stokes::YY ;
1343      }
1344    }
1345    else if ( polType_ == "circular" ) {
1346      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1347        if ( polnos[ipol] == 0 )
1348          corrType[ipol] = Stokes::RR ;
1349        else if ( polnos[ipol] == 1 )
1350          corrType[ipol] = Stokes::LL ;
1351      }
1352    }
1353    else if ( polType_ == "stokes" ) {
1354      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1355        if ( polnos[ipol] == 0 )
1356          corrType[ipol] = Stokes::I ;
1357        else if ( polnos[ipol] == 1 )
1358          corrType[ipol] = Stokes::V ;
1359      }
1360    }
1361    else if ( polType_ == "linpol" ) {
1362      for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1363        if ( polnos[ipol] == 1 )
1364          corrType[ipol] = Stokes::Plinear ;
1365        else if ( polnos[ipol] == 2 )
1366          corrType[ipol] = Stokes::Pangle ;
1367      }
1368    }
1369  }     
1370  else if ( npol == 1 ) {
1371    if ( polType_ == "linear" )
1372      corrType[0] = Stokes::XX ;
1373    else if ( polType_ == "circular" )
1374      corrType[0] = Stokes::RR ;
1375    else if ( polType_ == "stokes" )
1376      corrType[0] = Stokes::I ;
1377  }
1378
1379  return corrType ;
1380}
1381
1382void MSWriter::getValidTimeRange( MEpoch &me, Double &interval, Table &tab )
1383{
1384  ROScalarMeasColumn<MEpoch> timeMeasCol( tab, "TIME" ) ;
1385  ROScalarColumn<Double> timeCol( tab, "TIME" ) ;
1386  String refStr = timeMeasCol( 0 ).getRefString() ;
1387  Vector<Double> timeArr = timeCol.getColumn() ;
1388  MEpoch::Types meType ;
1389  MEpoch::getType( meType, refStr ) ;
1390  Unit tUnit = timeMeasCol.measDesc().getUnits()( 0 ) ;
1391  Double minTime ;
1392  Double maxTime ;
1393  minMax( minTime, maxTime, timeArr ) ;
1394  Double midTime = 0.5 * ( minTime + maxTime ) ;
1395  me = MEpoch( Quantity( midTime, tUnit ), meType ) ;
1396  interval = Quantity( maxTime-minTime, tUnit ).getValue( "s" ) ;
1397}
1398
1399void MSWriter::queryType( Int type, String &stype, Bool &b )
1400{
1401  switch ( type ) {
1402  case SrcType::PSON:
1403    stype = "POSITION_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1404    b = True ;
1405    break ;
1406  case SrcType::PSOFF:
1407    stype = "POSITION_SWITCH.OBSERVE_TARGET.OFF_SOURCE" ;
1408    b = False ;
1409    break ;
1410  case SrcType::NOD:
1411    stype = "NOD.OBSERVE_TARGET.ON_SOURCE" ;
1412    b = True ;
1413    break ;
1414  case SrcType::FSON:
1415    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1416    b = True ;
1417    break ;
1418  case SrcType::FSOFF:
1419    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1420    b = False ;
1421    break ;
1422  case SrcType::SKY:
1423    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1424    b = False ;
1425    break ;
1426  case SrcType::HOT:
1427    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1428    b = False ;
1429    break ;
1430  case SrcType::WARM:
1431    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1432    b = False ;
1433   break ;
1434  case SrcType::COLD:
1435    stype = "UNSPECIFIED.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1436    b = False ;
1437    break ;
1438  case SrcType::PONCAL:
1439    stype = "POSITION_SWITCH.CALIBRATE_TEMPERATURE.ON_SOURCE" ;
1440    b = True ;
1441    break ;
1442  case SrcType::POFFCAL:
1443    stype = "POSITION_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1444    b = False ;
1445    break ;
1446  case SrcType::NODCAL:
1447    stype = "NOD.CALIBRATE_TEMPERATURE.ON_SOURCE" ;
1448    b = True ;
1449    break ;
1450  case SrcType::FONCAL:
1451    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.ON_SOURCE" ;
1452    b = True ;
1453    break ;
1454  case SrcType::FOFFCAL:
1455    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1456    b = False ;
1457    break ;
1458  case SrcType::FSLO:
1459    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1460    b = True ;
1461    break ;
1462  case SrcType::FLOOFF:
1463    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.OFF_SOURCE" ;
1464    b = False ;
1465    break ;
1466  case SrcType::FLOSKY:
1467    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1468    b = False ;
1469    break ;
1470  case SrcType::FLOHOT:
1471    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1472    b = False ;
1473    break ;
1474  case SrcType::FLOWARM:
1475    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1476    b = False ;
1477    break ;
1478  case SrcType::FLOCOLD:
1479    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1480    b = False ;
1481    break ;
1482  case SrcType::FSHI:
1483    stype = "FREQUENCY_SWITCH.OBSERVE_TARGET.ON_SOURCE" ;
1484    b = True ;
1485    break ;
1486  case SrcType::FHIOFF:
1487    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1488    b = False ;
1489    break ;
1490  case SrcType::FHISKY:
1491    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1492    b = False ;
1493    break ;
1494  case SrcType::FHIHOT:
1495    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1496    b = False ;
1497    break ;
1498  case SrcType::FHIWARM:
1499    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1500    b = False ;
1501    break ;
1502  case SrcType::FHICOLD:
1503    stype = "FREQUENCY_SWITCH.CALIBRATE_TEMPERATURE.OFF_SOURCE" ;
1504    b = False ;
1505    break ;
1506  case SrcType::SIG:
1507    stype = "UNSPECIFIED.OBSERVE_TARGET.ON_SOURCE" ;
1508    b = True ;
1509    break ;
1510  case SrcType::REF:
1511    stype = "UNSPECIFIED.OBSERVE_TARGET.ON_SOURCE" ;
1512    b = False ;
1513    break ;
1514  default:
1515    stype = "UNSPECIFIED" ;
1516    b = True ;
1517    break ;
1518  }
1519}
1520}
Note: See TracBrowser for help on using the repository browser.