source: trunk/src/MSWriter.cpp@ 2253

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

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

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

Minor bug fix on MSWriter.cpp


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