source: trunk/src/MSFiller.cpp@ 2228

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

Simplified a way to fill FOCUS row.


File size: 63.0 KB
RevLine 
[1974]1//
2// C++ Interface: MSFiller
3//
4// Description:
5//
6// This class is specific filler 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 <iostream>
15#include <map>
16
17#include <tables/Tables/ExprNode.h>
18#include <tables/Tables/TableIter.h>
[1987]19#include <tables/Tables/TableColumn.h>
[1974]20#include <tables/Tables/ScalarColumn.h>
21#include <tables/Tables/ArrayColumn.h>
[1987]22#include <tables/Tables/TableParse.h>
[2002]23#include <tables/Tables/TableRow.h>
[1974]24
[2002]25#include <casa/Containers/RecordField.h>
[1974]26#include <casa/Logging/LogIO.h>
27#include <casa/Arrays/Slicer.h>
28#include <casa/Quanta/MVTime.h>
[1987]29#include <casa/OS/Path.h>
[1974]30
31#include <measures/Measures/Stokes.h>
32#include <measures/Measures/MEpoch.h>
[1987]33#include <measures/Measures/MCEpoch.h>
34#include <measures/Measures/MFrequency.h>
35#include <measures/Measures/MCFrequency.h>
36#include <measures/Measures/MPosition.h>
37#include <measures/Measures/MCPosition.h>
38#include <measures/Measures/MDirection.h>
39#include <measures/Measures/MCDirection.h>
40#include <measures/Measures/MeasConvert.h>
[1974]41#include <measures/TableMeasures/ScalarMeasColumn.h>
[1987]42#include <measures/TableMeasures/ArrayMeasColumn.h>
43#include <measures/TableMeasures/ScalarQuantColumn.h>
44#include <measures/TableMeasures/ArrayQuantColumn.h>
[1974]45
[2021]46#include <ms/MeasurementSets/MSAntennaIndex.h>
47
[1974]48#include <atnf/PKSIO/SrcType.h>
49
50#include "MSFiller.h"
51#include "STHeader.h"
52
[1988]53#include <ctime>
54#include <sys/time.h>
55
[2017]56using namespace casa ;
57using namespace std ;
58
59namespace asap {
60double MSFiller::gettimeofday_sec()
[1988]61{
62 struct timeval tv ;
63 gettimeofday( &tv, NULL ) ;
64 return tv.tv_sec + (double)tv.tv_usec*1.0e-6 ;
65}
66
[1974]67MSFiller::MSFiller( casa::CountedPtr<Scantable> stable )
68 : table_( stable ),
[1987]69 tablename_( "" ),
[1974]70 antenna_( -1 ),
[2021]71 antennaStr_(""),
[2206]72 getPt_( True ),
[1974]73 isFloatData_( False ),
74 isData_( False ),
75 isDoppler_( False ),
76 isFlagCmd_( False ),
77 isFreqOffset_( False ),
78 isHistory_( False ),
79 isProcessor_( False ),
80 isSysCal_( False ),
[1993]81 isWeather_( False ),
82 colTsys_( "TSYS_SPECTRUM" ),
83 colTcal_( "TCAL_SPECTRUM" )
[1974]84{
85 os_ = LogIO() ;
86 os_.origin( LogOrigin( "MSFiller", "MSFiller()", WHERE ) ) ;
87}
88
89MSFiller::~MSFiller()
90{
91 os_.origin( LogOrigin( "MSFiller", "~MSFiller()", WHERE ) ) ;
92}
93
94bool MSFiller::open( const std::string &filename, const casa::Record &rec )
95{
96 os_.origin( LogOrigin( "MSFiller", "open()", WHERE ) ) ;
[2004]97// double startSec = gettimeofday_sec() ;
98// os_ << "start MSFiller::open() startsec=" << startSec << LogIO::POST ;
[1974]99 //os_ << " filename = " << filename << endl ;
100
101 // parsing MS options
102 if ( rec.isDefined( "ms" ) ) {
103 Record msrec = rec.asRecord( "ms" ) ;
104 if ( msrec.isDefined( "getpt" ) ) {
105 getPt_ = msrec.asBool( "getpt" ) ;
106 }
107 if ( msrec.isDefined( "antenna" ) ) {
108 if ( msrec.type( msrec.fieldNumber( "antenna" ) ) == TpInt ) {
109 antenna_ = msrec.asInt( "antenna" ) ;
110 }
111 else {
[2021]112 //antenna_ = atoi( msrec.asString( "antenna" ).c_str() ) ;
113 antennaStr_ = msrec.asString( "antenna" ) ;
[1974]114 }
115 }
116 else {
117 antenna_ = 0 ;
118 }
119 }
120
[1987]121 MeasurementSet *tmpMS = new MeasurementSet( filename, Table::Old ) ;
122 //mstable_ = (*tmpMS)( tmpMS->col("ANTENNA1") == antenna_
123 // && tmpMS->col("ANTENNA1") == tmpMS->col("ANTENNA2") ) ;
124 tablename_ = tmpMS->tableName() ;
[2021]125 if ( antenna_ == -1 && antennaStr_.size() > 0 ) {
126 MSAntennaIndex msAntIdx( tmpMS->antenna() ) ;
127 Vector<Int> id = msAntIdx.matchAntennaName( antennaStr_ ) ;
128 if ( id.size() > 0 )
129 antenna_ = id[0] ;
130 }
[2025]131
132 os_ << "Parsing MS options" << endl ;
133 os_ << " getPt = " << getPt_ << endl ;
134 os_ << " antenna = " << antenna_ << endl ;
135 os_ << " antennaStr = " << antennaStr_ << LogIO::POST ;
136
[1987]137 mstable_ = MeasurementSet( (*tmpMS)( tmpMS->col("ANTENNA1") == antenna_
138 && tmpMS->col("ANTENNA1") == tmpMS->col("ANTENNA2") ) ) ;
139// stringstream ss ;
140// ss << "SELECT FROM $1 WHERE ANTENNA1 == ANTENNA2 && ANTENNA1 == " << antenna_ ;
141// String taql( ss.str() ) ;
142// mstable_ = MeasurementSet( tableCommand( taql, *tmpMS ) ) ;
143 delete tmpMS ;
[1974]144
145 // check which data column exists
[1987]146 isFloatData_ = mstable_.tableDesc().isColumn( "FLOAT_DATA" ) ;
147 isData_ = mstable_.tableDesc().isColumn( "DATA" ) ;
[1974]148
[2004]149// double endSec = gettimeofday_sec() ;
150// os_ << "end MSFiller::open() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]151 return true ;
152}
153
154void MSFiller::fill()
155{
156 os_.origin( LogOrigin( "MSFiller", "fill()", WHERE ) ) ;
[2004]157// double startSec = gettimeofday_sec() ;
158// os_ << "start MSFiller::fill() startSec=" << startSec << LogIO::POST ;
[1974]159
[2004]160// double time0 = gettimeofday_sec() ;
161// os_ << "start init fill: " << time0 << LogIO::POST ;
[1990]162
[1974]163 // Initialize header
164 STHeader sdh ;
165 sdh.nchan = 0 ;
166 sdh.npol = 0 ;
167 sdh.nif = 0 ;
168 sdh.nbeam = 0 ;
169 sdh.observer = "" ;
170 sdh.project = "" ;
171 sdh.obstype = "" ;
172 sdh.antennaname = "" ;
173 sdh.antennaposition.resize( 0 ) ;
174 sdh.equinox = 0.0 ;
175 sdh.freqref = "" ;
176 sdh.reffreq = -1.0 ;
177 sdh.bandwidth = 0.0 ;
178 sdh.utc = 0.0 ;
179 sdh.fluxunit = "" ;
180 sdh.epoch = "" ;
181 sdh.poltype = "" ;
182
183 // check if optional table exists
[1987]184 //const TableRecord msrec = tablesel_.keywordSet() ;
185 const TableRecord msrec = mstable_.keywordSet() ;
[1974]186 isDoppler_ = msrec.isDefined( "DOPPLER" ) ;
[2004]187 if ( isDoppler_ )
188 if ( mstable_.doppler().nrow() == 0 )
189 isDoppler_ = False ;
[1974]190 isFlagCmd_ = msrec.isDefined( "FLAG_CMD" ) ;
[2004]191 if ( isFlagCmd_ )
192 if ( mstable_.flagCmd().nrow() == 0 )
193 isFlagCmd_ = False ;
[1974]194 isFreqOffset_ = msrec.isDefined( "FREQ_OFFSET" ) ;
[2004]195 if ( isFreqOffset_ )
196 if ( mstable_.freqOffset().nrow() == 0 )
197 isFreqOffset_ = False ;
[1974]198 isHistory_ = msrec.isDefined( "HISTORY" ) ;
[2004]199 if ( isHistory_ )
200 if ( mstable_.history().nrow() == 0 )
201 isHistory_ = False ;
[1974]202 isProcessor_ = msrec.isDefined( "PROCESSOR" ) ;
[2004]203 if ( isProcessor_ )
204 if ( mstable_.processor().nrow() == 0 )
205 isProcessor_ = False ;
[1974]206 isSysCal_ = msrec.isDefined( "SYSCAL" ) ;
[2004]207 if ( isSysCal_ )
208 if ( mstable_.sysCal().nrow() == 0 )
209 isSysCal_ = False ;
[1974]210 isWeather_ = msrec.isDefined( "WEATHER" ) ;
[2004]211 if ( isWeather_ )
212 if ( mstable_.weather().nrow() == 0 )
213 isWeather_ = False ;
214
[1974]215 // Access to MS subtables
[1987]216 MSField fieldtab = mstable_.field() ;
217 MSPolarization poltab = mstable_.polarization() ;
218 MSDataDescription ddtab = mstable_.dataDescription() ;
219 MSObservation obstab = mstable_.observation() ;
220 MSSource srctab = mstable_.source() ;
221 MSSpectralWindow spwtab = mstable_.spectralWindow() ;
222 MSSysCal caltab = mstable_.sysCal() ;
[1974]223 if ( caltab.nrow() == 0 )
224 isSysCal_ = False ;
[1993]225 else {
[2159]226 if ( !caltab.tableDesc().isColumn( colTcal_ ) ) {
[1993]227 colTcal_ = "TCAL" ;
[2159]228 if ( !caltab.tableDesc().isColumn( colTcal_ ) )
229 colTcal_ = "NONE" ;
230 }
231 if ( !caltab.tableDesc().isColumn( colTsys_ ) ) {
[1993]232 colTsys_ = "TSYS" ;
[2159]233 if ( !caltab.tableDesc().isColumn( colTcal_ ) )
234 colTsys_ = "NONE" ;
235 }
[1993]236 }
[2004]237// colTcal_ = "TCAL" ;
238// colTsys_ = "TSYS" ;
[1987]239 MSPointing pointtab = mstable_.pointing() ;
240 if ( mstable_.weather().nrow() == 0 )
[1974]241 isWeather_ = False ;
[1987]242 MSState stattab = mstable_.state() ;
243 MSAntenna anttab = mstable_.antenna() ;
[1974]244
[1987]245 // TEST
246 // memory allocation by boost::object_pool
247 boost::object_pool<ROTableColumn> *tpoolr = new boost::object_pool<ROTableColumn> ;
248 //
[1974]249
[1987]250 // SUBTABLES: FREQUENCIES
[2222]251 //string freqFrame = getFrame() ;
252 string freqFrame = "LSRK" ;
[2217]253 table_->frequencies().setFrame( freqFrame ) ;
254 table_->frequencies().setFrame( freqFrame, True ) ;
[1974]255
256 // SUBTABLES: WEATHER
[2004]257 fillWeather() ;
[1974]258
259 // SUBTABLES: FOCUS
260 fillFocus() ;
261
262 // SUBTABLES: TCAL
[2004]263 fillTcal( tpoolr ) ;
[1974]264
265 // SUBTABLES: FIT
266 //fillFit() ;
267
268 // SUBTABLES: HISTORY
269 //fillHistory() ;
270
[1987]271 // shared pointers
272 ROTableColumn *tcolr ;
273
[1974]274 // MAIN
275 // Iterate over several ids
276 map<Int, uInt> ifmap ; // (IFNO, FREQ_ID) pair
[1987]277 ROArrayQuantColumn<Double> *sharedQDArrCol = new ROArrayQuantColumn<Double>( anttab, "POSITION" ) ;
278 Vector< Quantum<Double> > antpos = (*sharedQDArrCol)( antenna_ ) ;
279 delete sharedQDArrCol ;
[1974]280 MPosition mp( MVPosition( antpos ), MPosition::ITRF ) ;
[1990]281 if ( getPt_ ) {
282 //pointtab = pointtab( pointtab.col("ANTENNA_ID")==antenna_ ).sort("TIME") ;
283 pointtab = MSPointing( pointtab( pointtab.col("ANTENNA_ID")==antenna_ ).sort("TIME") ) ;
284 }
[1987]285 tcolr = tpoolr->construct( anttab, "STATION" ) ;
286 String stationName = tcolr->asString( antenna_ ) ;
287 tpoolr->destroy( tcolr ) ;
288 tcolr = tpoolr->construct( anttab, "NAME" ) ;
289 String antennaName = tcolr->asString( antenna_ ) ;
290 tpoolr->destroy( tcolr ) ;
291 sdh.antennaposition.resize( 3 ) ;
292 for ( int i = 0 ; i < 3 ; i++ )
293 sdh.antennaposition[i] = antpos[i].getValue( "m" ) ;
294 String telescopeName = "" ;
295
[2021]296// double time1 = gettimeofday_sec() ;
297// os_ << "end fill init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1990]298
[2002]299 // row based
300 Table &stab = table_->table() ;
301 TableRow row( stab ) ;
302 TableRecord &trec = row.record() ;
303 RecordFieldPtr< Array<Float> > spRF( trec, "SPECTRA" ) ;
304 RecordFieldPtr< Array<uChar> > ucarrRF( trec, "FLAGTRA" ) ;
305 RecordFieldPtr<Double> timeRF( trec, "TIME" ) ;
306 RecordFieldPtr< Array<Float> > tsysRF( trec, "TSYS" ) ;
307 RecordFieldPtr<Double> intervalRF( trec, "INTERVAL" ) ;
308 RecordFieldPtr< Array<Double> > dirRF( trec, "DIRECTION" ) ;
309 RecordFieldPtr<Float> azRF( trec, "AZIMUTH" ) ;
310 RecordFieldPtr<Float> elRF( trec, "ELEVATION" ) ;
311 RecordFieldPtr< Array<Double> > scrRF( trec, "SCANRATE" ) ;
312 RecordFieldPtr<uInt> cycleRF( trec, "CYCLENO" ) ;
313 RecordFieldPtr<uInt> flrRF( trec, "FLAGROW" ) ;
314 RecordFieldPtr<uInt> tcalidRF( trec, "TCAL_ID" ) ;
315 RecordFieldPtr<uInt> widRF( trec, "WEATHER_ID" ) ;
316 RecordFieldPtr<uInt> polnoRF( trec, "POLNO" ) ;
317
318
319 // REFBEAMNO
320 RecordFieldPtr<Int> intRF( trec, "REFBEAMNO" ) ;
321 *intRF = 0 ;
322
323 // FIT_ID
324 intRF.attachToRecord( trec, "FIT_ID" ) ;
325 *intRF = -1 ;
326
327 // OPACITY
328 RecordFieldPtr<Float> floatRF( trec, "OPACITY" ) ;
329 *floatRF = 0.0 ;
330
[1978]331 //
332 // ITERATION: OBSERVATION_ID
333 //
[1987]334 TableIterator iter0( mstable_, "OBSERVATION_ID" ) ;
[1974]335 while( !iter0.pastEnd() ) {
[2021]336// time0 = gettimeofday_sec() ;
337// os_ << "start 0th iteration: " << time0 << LogIO::POST ;
[1987]338 Table t0 = iter0.table() ;
339 tcolr = tpoolr->construct( t0, "OBSERVATION_ID" ) ;
340 Int obsId = tcolr->asInt( 0 ) ;
341 tpoolr->destroy( tcolr ) ;
342 if ( sdh.observer == "" ) {
343 tcolr = tpoolr->construct( obstab, "OBSERVER" ) ;
344 sdh.observer = tcolr->asString( obsId ) ;
345 tpoolr->destroy( tcolr ) ;
346 }
347 if ( sdh.project == "" ) {
348 tcolr = tpoolr->construct( obstab, "PROJECT" ) ;
349 sdh.observer = tcolr->asString( obsId ) ;
350 tpoolr->destroy( tcolr ) ;
351 }
352 ROArrayMeasColumn<MEpoch> *tmpMeasCol = new ROArrayMeasColumn<MEpoch>( obstab, "TIME_RANGE" ) ;
353 MEpoch me = (*tmpMeasCol)( obsId )( IPosition(1,0) ) ;
354 delete tmpMeasCol ;
[1974]355 if ( sdh.utc == 0.0 ) {
[2035]356 sdh.utc = me.get( "d" ).getValue() ;
[1974]357 }
[1987]358 if ( telescopeName == "" ) {
359 tcolr = tpoolr->construct( obstab, "TELESCOPE_NAME" ) ;
[2025]360 telescopeName = tcolr->asString( obsId ) ;
[1987]361 tpoolr->destroy( tcolr ) ;
362 }
[1978]363 Int nbeam = 0 ;
[2021]364// time1 = gettimeofday_sec() ;
365// os_ << "end 0th iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]366 //
367 // ITERATION: FEED1
368 //
369 TableIterator iter1( t0, "FEED1" ) ;
370 while( !iter1.pastEnd() ) {
[2021]371// time0 = gettimeofday_sec() ;
372// os_ << "start 1st iteration: " << time0 << LogIO::POST ;
[1987]373 Table t1 = iter1.table() ;
374 // assume FEED1 == FEED2
375 tcolr = tpoolr->construct( t1, "FEED1" ) ;
376 Int feedId = tcolr->asInt( 0 ) ;
377 tpoolr->destroy( tcolr ) ;
[1974]378 nbeam++ ;
[2002]379
380 // BEAMNO
381 RecordFieldPtr<uInt> uintRF( trec, "BEAMNO" ) ;
382 *uintRF = feedId ;
383
384 // FOCUS_ID
385 uintRF.attachToRecord( trec, "FOCUS_ID" ) ;
386 *uintRF = 0 ;
387
[2021]388// time1 = gettimeofday_sec() ;
389// os_ << "end 1st iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]390 //
391 // ITERATION: FIELD_ID
392 //
393 TableIterator iter2( t1, "FIELD_ID" ) ;
394 while( !iter2.pastEnd() ) {
[2021]395// time0 = gettimeofday_sec() ;
396// os_ << "start 2nd iteration: " << time0 << LogIO::POST ;
[1987]397 Table t2 = iter2.table() ;
398 tcolr = tpoolr->construct( t2, "FIELD_ID" ) ;
399 Int fieldId = tcolr->asInt( 0 ) ;
400 tpoolr->destroy( tcolr ) ;
401 tcolr = tpoolr->construct( fieldtab, "SOURCE_ID" ) ;
402 Int srcId = tcolr->asInt( fieldId ) ;
403 tpoolr->destroy( tcolr ) ;
404 tcolr = tpoolr->construct( fieldtab, "NAME" ) ;
405 String fieldName = tcolr->asString( fieldId ) + "__" + String::toString(fieldId) ;
406 tpoolr->destroy( tcolr ) ;
[2004]407 ROArrayMeasColumn<MDirection> *delayDirCol = new ROArrayMeasColumn<MDirection>( fieldtab, "DELAY_DIR" ) ;
408 Vector<MDirection> delayDir = (*delayDirCol)( fieldId ) ;
409 delete delayDirCol ;
410 Vector<Double> defaultScanrate( 2, 0.0 ) ;
411 Vector<Double> defaultDir = delayDir[0].getAngle( "rad" ).getValue() ;
412 if ( delayDir.nelements() > 1 )
413 defaultScanrate = delayDir[1].getAngle( "rad" ).getValue() ;
414
[2002]415
416 // FIELDNAME
417 RecordFieldPtr<String> strRF( trec, "FIELDNAME" ) ;
418 *strRF = fieldName ;
419
420
[2021]421// time1 = gettimeofday_sec() ;
422// os_ << "end 2nd iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]423 //
424 // ITERATION: DATA_DESC_ID
425 //
426 TableIterator iter3( t2, "DATA_DESC_ID" ) ;
427 while( !iter3.pastEnd() ) {
[2021]428// time0 = gettimeofday_sec() ;
429// os_ << "start 3rd iteration: " << time0 << LogIO::POST ;
[1987]430 Table t3 = iter3.table() ;
431 tcolr = tpoolr->construct( t3, "DATA_DESC_ID" ) ;
432 Int ddId = tcolr->asInt( 0 ) ;
433 tpoolr->destroy( tcolr ) ;
434 tcolr = tpoolr->construct( ddtab, "POLARIZATION_ID" ) ;
435 Int polId = tcolr->asInt( ddId ) ;
436 tpoolr->destroy( tcolr ) ;
437 tcolr = tpoolr->construct( ddtab, "SPECTRAL_WINDOW_ID" ) ;
438 Int spwId = tcolr->asInt( ddId ) ;
439 tpoolr->destroy( tcolr ) ;
[2002]440
441 // IFNO
442 uintRF.attachToRecord( trec, "IFNO" ) ;
443 *uintRF = (uInt)spwId ;
444
[1974]445 // polarization information
[1987]446 tcolr = tpoolr->construct( poltab, "NUM_CORR" ) ;
447 Int npol = tcolr->asInt( polId ) ;
448 tpoolr->destroy( tcolr ) ;
449 ROArrayColumn<Int> *roArrICol = new ROArrayColumn<Int>( poltab, "CORR_TYPE" ) ;
450 Vector<Int> corrtype = (*roArrICol)( polId ) ;
451 delete roArrICol ;
[2021]452// os_ << "npol = " << npol << LogIO::POST ;
453// os_ << "corrtype = " << corrtype << LogIO::POST ;
[1974]454 // source information
[2033]455// os_ << "srcId = " << srcId << ", spwId = " << spwId << LogIO::POST ;
[2004]456 MSSource srctabSel = srctab( srctab.col("SOURCE_ID") == srcId && srctab.col("SPECTRAL_WINDOW_ID") == spwId ) ;
457 if ( srctabSel.nrow() == 0 ) {
458 srctabSel = srctab( srctab.col("SOURCE_ID") == srcId && srctab.col("SPECTRAL_WINDOW_ID") == -1 ) ;
459 }
[2033]460 String srcName( "" ) ;
461 Vector<Double> srcPM( 2, 0.0 ) ;
462 Vector<Double> srcDir( 2, 0.0 ) ;
463 MDirection md ;
464 Int numLines = 0 ;
465 ROArrayColumn<Double> *roArrDCol = 0 ;
466 if ( srctabSel.nrow() > 0 ) {
467 // source name
468 tcolr = tpoolr->construct( srctabSel, "NAME" ) ;
469 srcName = tcolr->asString( 0 ) ;
470 tpoolr->destroy( tcolr ) ;
[2002]471
[2033]472 // source proper motion
473 roArrDCol = new ROArrayColumn<Double>( srctabSel, "PROPER_MOTION" ) ;
474 srcPM = (*roArrDCol)( 0 ) ;
475 delete roArrDCol ;
476
477 // source direction
478 roArrDCol = new ROArrayColumn<Double>( srctabSel, "DIRECTION" ) ;
479 srcDir = (*roArrDCol)( 0 ) ;
480 delete roArrDCol ;
481
482 // source direction as MDirection object
483 ROScalarMeasColumn<MDirection> *tmpMeasCol = new ROScalarMeasColumn<MDirection>( srctabSel, "DIRECTION" ) ;
484 md = (*tmpMeasCol)( 0 ) ;
485 delete tmpMeasCol ;
486
487 // number of lines
488 tcolr = tpoolr->construct( srctabSel, "NUM_LINES" ) ;
[2059]489 numLines = tcolr->asInt( 0 ) ;
[2033]490 tpoolr->destroy( tcolr ) ;
491
492 }
493 else {
494 md = MDirection( Quantum<Double>(0.0,Unit("rad")), Quantum<Double>(0.0,Unit("rad")) ) ;
495 }
496
[2002]497 // SRCNAME
498 strRF.attachToRecord( trec, "SRCNAME" ) ;
499 *strRF = srcName ;
500
[2033]501// os_ << "srcName = " << srcName << LogIO::POST ;
[2002]502
503 // SRCPROPERMOTION
504 RecordFieldPtr< Array<Double> > darrRF( trec, "SRCPROPERMOTION" ) ;
505 *darrRF = srcPM ;
506
[1974]507 //os_ << "srcPM = " << srcPM << LogIO::POST ;
[2002]508
509 // SRCDIRECTION
510 darrRF.attachToRecord( trec, "SRCDIRECTION" ) ;
511 *darrRF = srcDir ;
512
[1974]513 //os_ << "srcDir = " << srcDir << LogIO::POST ;
[2021]514
[1974]515 // for MOLECULES subtable
[2021]516// os_ << "numLines = " << numLines << LogIO::POST ;
517
[1974]518 Vector<Double> restFreqs( numLines, 0.0 ) ;
519 Vector<String> transitionName( numLines, "" ) ;
[2021]520 Vector<Double> sysVels ;
521 Double sysVel = 0.0 ;
[1974]522 if ( numLines != 0 ) {
[1987]523 if ( srctabSel.tableDesc().isColumn( "REST_FREQUENCY" ) ) {
524 sharedQDArrCol = new ROArrayQuantColumn<Double>( srctabSel, "REST_FREQUENCY" ) ;
525 Array< Quantum<Double> > qRestFreqs = (*sharedQDArrCol)( 0 ) ;
526 delete sharedQDArrCol ;
527 for ( int i = 0 ; i < numLines ; i++ ) {
528 restFreqs[i] = qRestFreqs( IPosition( 1, i ) ).getValue( "Hz" ) ;
529 }
[1974]530 }
[2033]531// os_ << "restFreqs = " << restFreqs << LogIO::POST ;
[1974]532 if ( srctabSel.tableDesc().isColumn( "TRANSITION" ) ) {
[2004]533 ROArrayColumn<String> transitionCol( srctabSel, "TRANSITION" ) ;
534 if ( transitionCol.isDefined( 0 ) )
535 transitionName = transitionCol( 0 ) ;
536 //os_ << "transitionNameCol.nrow() = " << transitionCol.nrow() << LogIO::POST ;
[1974]537 }
[2021]538 if ( srctabSel.tableDesc().isColumn( "SYSVEL" ) ) {
539 roArrDCol = new ROArrayColumn<Double>( srctabSel, "SYSVEL" ) ;
540 sysVels = (*roArrDCol)( 0 ) ;
541 delete roArrDCol ;
542 }
543 if ( !sysVels.empty() ) {
544 //os_ << "sysVels.shape() = " << sysVels.shape() << LogIO::POST ;
545 // NB: assume all SYSVEL values are the same
546 sysVel = sysVels( IPosition(1,0) ) ;
547 }
[1974]548 }
[2021]549
550 // SRCVELOCITY
551 RecordFieldPtr<Double> doubleRF( trec, "SRCVELOCITY" ) ;
552 *doubleRF = sysVel ;
553
554// os_ << "sysVel = " << sysVel << LogIO::POST ;
555
[1974]556 uInt molId = table_->molecules().addEntry( restFreqs, transitionName, transitionName ) ;
[2002]557
558 // MOLECULE_ID
559 uintRF.attachToRecord( trec, "MOLECULE_ID" ) ;
560 *uintRF = molId ;
561
[1974]562 // spectral setup
[2226]563 ROScalarQuantColumn<Double> *tmpQuantCol = new ROScalarQuantColumn<Double>( t3, "TIME" ) ;
564 me = MEpoch( (*tmpQuantCol)( 0 ), MEpoch::UTC ) ;
565 delete tmpQuantCol ;
[1974]566 MeasFrame mf( me, mp, md ) ;
[1987]567 tcolr = tpoolr->construct( spwtab, "MEAS_FREQ_REF" ) ;
568 MFrequency::Types freqRef = MFrequency::castType((uInt)(tcolr->asInt(spwId))) ;
569 tpoolr->destroy( tcolr ) ;
570 tcolr = tpoolr->construct( spwtab, "NUM_CHAN" ) ;
571 Int nchan = tcolr->asInt( spwId ) ;
[2102]572 Bool iswvr = False ;
573 if ( nchan == 4 ) iswvr = True ;
[1987]574 tpoolr->destroy( tcolr ) ;
[1974]575 Bool even = False ;
576 if ( (nchan/2)*2 == nchan ) even = True ;
[1993]577 sdh.nchan = max( sdh.nchan, nchan ) ;
[2226]578 tmpQuantCol = new ROScalarQuantColumn<Double>( spwtab, "TOTAL_BANDWIDTH" ) ;
[1987]579 Double totbw = (*tmpQuantCol)( spwId ).getValue( "Hz" ) ;
580 delete tmpQuantCol ;
[2223]581 if ( nchan != 4 )
582 sdh.bandwidth = max( sdh.bandwidth, totbw ) ;
[2219]583 if ( sdh.freqref == "" && nchan != 4)
[2222]584 //sdh.freqref = MFrequency::showType( freqRef ) ;
585 sdh.freqref = "LSRK" ;
[2223]586 if ( sdh.reffreq == -1.0 && nchan != 4 ) {
[1987]587 tmpQuantCol = new ROScalarQuantColumn<Double>( spwtab, "REF_FREQUENCY" ) ;
588 Quantum<Double> qreffreq = (*tmpQuantCol)( spwId ) ;
589 delete tmpQuantCol ;
[2224]590// sdh.reffreq = qreffreq.getValue("Hz") ;
[1974]591 if ( freqRef == MFrequency::LSRK ) {
592 sdh.reffreq = qreffreq.getValue("Hz") ;
593 }
594 else {
595 MFrequency::Convert tolsr( freqRef, MFrequency::Ref( MFrequency::LSRK, mf ) ) ;
596 sdh.reffreq = tolsr( qreffreq ).get("Hz").getValue() ;
597 }
598 }
599 Int refchan = nchan / 2 ;
600 IPosition refip( 1, refchan ) ;
[2083]601 Double refpix = 0.5*(nchan-1) ;
[1974]602 Double refval = 0.0 ;
[1987]603 sharedQDArrCol = new ROArrayQuantColumn<Double>( spwtab, "CHAN_WIDTH" ) ;
604 Double increment = (*sharedQDArrCol)( spwId )( refip ).getValue( "Hz" ) ;
605 delete sharedQDArrCol ;
[2033]606// os_ << "nchan = " << nchan << " refchan = " << refchan << "(even=" << even << ") refpix = " << refpix << LogIO::POST ;
[1987]607 sharedQDArrCol = new ROArrayQuantColumn<Double>( spwtab, "CHAN_FREQ" ) ;
608 Vector< Quantum<Double> > chanFreqs = (*sharedQDArrCol)( spwId ) ;
609 delete sharedQDArrCol ;
[2187]610 if ( nchan > 1 && chanFreqs[0].getValue("Hz") > chanFreqs[1].getValue("Hz") )
611 increment *= -1.0 ;
[2224]612// if ( even ) {
613// IPosition refip0( 1, refchan-1 ) ;
614// Double refval0 = chanFreqs(refip0).getValue("Hz") ;
615// Double refval1 = chanFreqs(refip).getValue("Hz") ;
616// refval = 0.5 * ( refval0 + refval1 ) ;
617// }
618// else {
619// refval = chanFreqs(refip).getValue("Hz") ;
620// }
[1974]621 if ( freqRef == MFrequency::LSRK ) {
622 if ( even ) {
623 IPosition refip0( 1, refchan-1 ) ;
624 Double refval0 = chanFreqs(refip0).getValue("Hz") ;
625 Double refval1 = chanFreqs(refip).getValue("Hz") ;
626 refval = 0.5 * ( refval0 + refval1 ) ;
627 }
628 else {
629 refval = chanFreqs(refip).getValue("Hz") ;
630 }
631 }
632 else {
633 MFrequency::Convert tolsr( freqRef, MFrequency::Ref( MFrequency::LSRK, mf ) ) ;
634 if ( even ) {
635 IPosition refip0( 1, refchan-1 ) ;
636 Double refval0 = chanFreqs(refip0).getValue("Hz") ;
637 Double refval1 = chanFreqs(refip).getValue("Hz") ;
638 refval = 0.5 * ( refval0 + refval1 ) ;
639 refval = tolsr( refval ).get("Hz").getValue() ;
640 }
641 else {
642 refval = tolsr( chanFreqs(refip) ).get("Hz").getValue() ;
643 }
644 }
645 uInt freqId = table_->frequencies().addEntry( refpix, refval, increment ) ;
646 if ( ifmap.find( spwId ) == ifmap.end() ) {
647 ifmap.insert( pair<Int, uInt>(spwId,freqId) ) ;
648 //os_ << "added to ifmap: (" << spwId << "," << freqId << ")" << LogIO::POST ;
649 }
[2002]650
651 // FREQ_ID
652 uintRF.attachToRecord( trec, "FREQ_ID" ) ;
653 *uintRF = freqId ;
654
[1974]655 // for TSYS and TCAL
[2004]656 Vector<MEpoch> scTime ;
657 Vector<Double> scInterval ;
658 ROArrayColumn<Float> scTsysCol ;
659 MSSysCal caltabsel ;
660 if ( isSysCal_ ) {
661 caltabsel = caltab( caltab.col("ANTENNA_ID") == antenna_ && caltab.col("FEED_ID") == feedId && caltab.col("SPECTRAL_WINDOW_ID") == spwId ).sort("TIME") ;
662 ROScalarMeasColumn<MEpoch> scTimeCol( caltabsel, "TIME" ) ;
663 scTime.resize( caltabsel.nrow() ) ;
664 for ( uInt irow = 0 ; irow < caltabsel.nrow() ; irow++ )
665 scTime[irow] = scTimeCol( irow ) ;
666 ROScalarColumn<Double> *scIntervalCol = new ROScalarColumn<Double>( caltabsel, "INTERVAL" ) ;
667 scInterval = scIntervalCol->getColumn() ;
668 delete scIntervalCol ;
[2159]669 if ( colTsys_ != "NONE" )
670 scTsysCol.attach( caltabsel, colTsys_ ) ;
[2004]671 }
[2102]672
673 sdh.npol = max( sdh.npol, npol ) ;
674 if ( !iswvr && sdh.poltype == "" ) sdh.poltype = getPolType( corrtype[0] ) ;
675
[2021]676// time1 = gettimeofday_sec() ;
677// os_ << "end 3rd iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]678 //
679 // ITERATION: SCAN_NUMBER
680 //
681 TableIterator iter4( t3, "SCAN_NUMBER" ) ;
682 while( !iter4.pastEnd() ) {
[2021]683// time0 = gettimeofday_sec() ;
684// os_ << "start 4th iteration: " << time0 << LogIO::POST ;
[1987]685 Table t4 = iter4.table() ;
686 tcolr = tpoolr->construct( t4, "SCAN_NUMBER" ) ;
687 Int scanNum = tcolr->asInt( 0 ) ;
688 tpoolr->destroy( tcolr ) ;
[2002]689
690 // SCANNO
691 uintRF.attachToRecord( trec, "SCANNO" ) ;
692 *uintRF = scanNum - 1 ;
693
[2021]694// time1 = gettimeofday_sec() ;
695// os_ << "end 4th iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]696 //
697 // ITERATION: STATE_ID
698 //
699 TableIterator iter5( t4, "STATE_ID" ) ;
700 while( !iter5.pastEnd() ) {
[2033]701// time0 = gettimeofday_sec() ;
702// os_ << "start 5th iteration: " << time0 << LogIO::POST ;
[1987]703 Table t5 = iter5.table() ;
704 tcolr = tpoolr->construct( t5, "STATE_ID" ) ;
705 Int stateId = tcolr->asInt( 0 ) ;
706 tpoolr->destroy( tcolr ) ;
707 tcolr = tpoolr->construct( stattab, "OBS_MODE" ) ;
708 String obstype = tcolr->asString( stateId ) ;
709 tpoolr->destroy( tcolr ) ;
[1974]710 if ( sdh.obstype == "" ) sdh.obstype = obstype ;
711
712 Int nrow = t5.nrow() ;
[2033]713// time1 = gettimeofday_sec() ;
714// os_ << "end 5th iteration init: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[2002]715
[2004]716 uInt cycle = 0 ;
717
[2002]718 Cube<Float> spArr ;
719 Cube<Bool> flArr ;
[1974]720 if ( isFloatData_ ) {
[2002]721 ROArrayColumn<Bool> mFlagCol( t5, "FLAG" ) ;
[1974]722 ROArrayColumn<Float> mFloatDataCol( t5, "FLOAT_DATA" ) ;
[2002]723 spArr = mFloatDataCol.getColumn() ;
724 flArr = mFlagCol.getColumn() ;
[1974]725 if ( sdh.fluxunit == "" ) {
[2002]726 const TableRecord &dataColKeys = mFloatDataCol.keywordSet() ;
[1974]727 if ( dataColKeys.isDefined( "UNIT" ) )
728 sdh.fluxunit = dataColKeys.asString( "UNIT" ) ;
[2221]729 else if ( dataColKeys.isDefined( "QuantumUnits" ) )
730 sdh.fluxunit = dataColKeys.asString( "QuantumUnits" ) ;
[1974]731 }
732 }
733 else if ( isData_ ) {
[2002]734 spArr.resize( npol, nchan, nrow ) ;
[2004]735 flArr.resize( npol, nchan, nrow ) ;
[2002]736 ROArrayColumn<Bool> mFlagCol( t5, "FLAG" ) ;
[1974]737 ROArrayColumn<Complex> mDataCol( t5, "DATA" ) ;
[1987]738 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
739 Bool crossOK = False ;
740 Matrix<Complex> sp = mDataCol( irow ) ;
[2002]741 Matrix<Bool> fl = mFlagCol( irow ) ;
[2004]742 Matrix<Float> spxy = spArr.xyPlane( irow ) ;
743 Matrix<Bool> flxy = flArr.xyPlane( irow ) ;
[1987]744 for ( Int ipol = 0 ; ipol < npol ; ipol++ ) {
745 if ( corrtype[ipol] == Stokes::XY || corrtype[ipol] == Stokes::YX
746 || corrtype[ipol] == Stokes::RL || corrtype[ipol] == Stokes::LR ) {
747 if ( !crossOK ) {
[2004]748 spxy.row( ipol ) = real( sp.row( ipol ) ) ;
749 flxy.row( ipol ) = fl.row( ipol ) ;
[2002]750 if ( corrtype[ipol] == Stokes::XY || corrtype[ipol] == Stokes::RL ) {
[2004]751 spxy.row( ipol+1 ) = imag( sp.row( ipol ) ) ;
752 flxy.row( ipol+1 ) = fl.row( ipol ) ;
[2002]753 }
754 else {
[2004]755 spxy.row( ipol+1 ) = imag( conj( sp.row( ipol ) ) ) ;
756 flxy.row( ipol+1 ) = fl.row( ipol ) ;
[2002]757 }
[2004]758 crossOK = True ;
[1974]759 }
760 }
[1987]761 else {
[2004]762 spxy.row( ipol ) = real( sp.row( ipol ) ) ;
763 flxy.row( ipol ) = fl.row( ipol ) ;
[1987]764 }
[1974]765 }
[1987]766 }
[1974]767 if ( sdh.fluxunit == "" ) {
[2002]768 const TableRecord &dataColKeys = mDataCol.keywordSet() ;
[1974]769 if ( dataColKeys.isDefined( "UNIT" ) )
[2002]770 sdh.fluxunit = dataColKeys.asString( "UNIT" ) ;
[2221]771 else if ( dataColKeys.isDefined( "QuantumUnits" ) )
772 sdh.fluxunit = dataColKeys.asString( "QuantumUnits" ) ;
[2002]773 }
[1974]774 }
[1987]775 ROScalarMeasColumn<MEpoch> *mTimeCol = new ROScalarMeasColumn<MEpoch>( t5, "TIME" ) ;
[2002]776 Block<MEpoch> mTimeB( nrow ) ;
777 for ( Int irow = 0 ; irow < nrow ; irow++ )
778 mTimeB[irow] = (*mTimeCol)( irow ) ;
779 ROTableColumn *mIntervalCol = tpoolr->construct( t5, "INTERVAL" ) ;
780 ROTableColumn *mFlagRowCol = tpoolr->construct( t5, "FLAG_ROW" ) ;
781 Block<Int> sysCalIdx( nrow, -1 ) ;
782 if ( isSysCal_ ) {
[2017]783 getSysCalTime( scTime, scInterval, mTimeB, sysCalIdx ) ;
[1974]784 }
[2002]785 delete mTimeCol ;
786 Matrix<Float> defaulttsys( npol, 1, 1.0 ) ;
787 Int srcType = getSrcType( stateId, tpoolr ) ;
788 uInt diridx = 0 ;
789 MDirection::Types dirType ;
790 uInt wid = 0 ;
791 Int pidx = 0 ;
792 Bool crossOK = False ;
793 Block<uInt> polnos( npol, 99 ) ;
794 for ( Int ipol = 0 ; ipol < npol ; ipol++ ) {
795 Block<uInt> p = getPolNo( corrtype[ipol] ) ;
796 if ( p.size() > 1 ) {
797 if ( crossOK ) continue ;
798 else {
799 polnos[pidx] = p[0] ;
800 pidx++ ;
801 polnos[pidx] = p[1] ;
802 pidx++ ;
803 crossOK = True ;
[1974]804 }
805 }
[2002]806 else {
807 polnos[pidx] = p[0] ;
808 pidx++ ;
809 }
[1974]810 }
[2002]811
812 // SRCTYPE
813 intRF.attachToRecord( trec, "SRCTYPE" ) ;
814 *intRF = srcType ;
[1974]815
[2002]816 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
817 // CYCLENO
818 *cycleRF = cycle ;
[1987]819
[2002]820 // FLAGROW
821 *flrRF = (uInt)mFlagRowCol->asBool( irow ) ;
822
823 // SPECTRA, FLAG
824 Matrix<Float> sp = spArr.xyPlane( irow ) ;
825 Matrix<Bool> flb = flArr.xyPlane( irow ) ;
826 Matrix<uChar> fl( flb.shape() ) ;
827 convertArray( fl, flb ) ;
828
829 // TIME
[2004]830 *timeRF = mTimeB[irow].get("d").getValue() ;
[2002]831
832 // INTERVAL
833 *intervalRF = (Double)(mIntervalCol->asdouble( irow )) ;
834
835 // TSYS
836 Matrix<Float> tsys ;
[2159]837 if ( sysCalIdx[irow] != -1 && colTsys_ != "NONE" )
[2204]838 tsys = scTsysCol( sysCalIdx[irow] ) ;
[2002]839 else
840 tsys = defaulttsys ;
841
842 // TCAL_ID
[2004]843 Block<uInt> tcalids( npol, 0 ) ;
[2159]844 if ( sysCalIdx[irow] != -1 && colTcal_ != "NONE" ) {
[2004]845 tcalids = getTcalId( feedId, spwId, scTime[sysCalIdx[irow]] ) ;
[1987]846 }
847
[2002]848 // WEATHER_ID
849 if ( isWeather_ )
850 wid = getWeatherId( wid, mTimeB[irow].get("s").getValue() ) ;
851 *widRF = wid ;
852
[1987]853
[2002]854 // DIRECTION, AZEL, SCANRATE
855 if ( getPt_ ) {
[1974]856 Vector<Double> dir ;
857 Vector<Double> scanrate ;
858 String refString ;
[2002]859 diridx = getDirection( diridx, dir, scanrate, refString, pointtab, mTimeB[irow].get("s").getValue() ) ;
[1974]860 MDirection::getType( dirType, refString ) ;
[2002]861 mf.resetEpoch( mTimeB[irow] ) ;
[1974]862 mf.resetDirection( MDirection( MVDirection(dir), dirType ) ) ;
863 if ( refString == "J2000" ) {
[2002]864 *dirRF = dir ;
[1974]865 MDirection::Convert toazel( dirType, MDirection::Ref( MDirection::AZEL, mf ) ) ;
866 Vector<Double> azel = toazel( dir ).getAngle("rad").getValue() ;
[2002]867 *azRF = (Float)azel(0) ;
868 *elRF = (Float)azel(1) ;
[1974]869 }
870 else if ( refString(0,4) == "AZEL" ) {
[2002]871 *azRF = (Float)dir(0) ;
872 *elRF = (Float)dir(1) ;
[1974]873 MDirection::Convert toj2000( dirType, MDirection::Ref( MDirection::J2000, mf ) ) ;
874 Vector<Double> newdir = toj2000( dir ).getAngle("rad").getValue() ;
[2002]875 *dirRF = newdir ;
[1974]876 }
877 else {
878 MDirection::Convert toazel( dirType, MDirection::Ref( MDirection::AZEL, mf ) ) ;
879 Vector<Double> azel = toazel( dir ).getAngle("rad").getValue() ;
880 MDirection::Convert toj2000( dirType, MDirection::Ref( MDirection::J2000, mf ) ) ;
881 Vector<Double> newdir = toj2000( dir ).getAngle("rad").getValue() ;
[2002]882 *dirRF = newdir ;
883 *azRF = (Float)azel(0) ;
884 *elRF = (Float)azel(1) ;
[1974]885 }
886 if ( scanrate.size() != 0 ) {
[2002]887 *scrRF = scanrate ;
[1974]888 }
889 else {
[2002]890 *scrRF = defaultScanrate ;
[1974]891 }
892 }
[2002]893 else {
894 String ref = md.getRefString() ;
[2004]895 //Vector<Double> defaultDir = srcDir ;
[2002]896 MDirection::getType( dirType, "J2000" ) ;
897 if ( ref != "J2000" ) {
898 ROScalarMeasColumn<MEpoch> tmCol( pointtab, "TIME" ) ;
899 mf.resetEpoch( tmCol( 0 ) ) ;
900 MDirection::Convert toj2000( dirType, MDirection::Ref( MDirection::J2000, mf ) ) ;
901 defaultDir = toj2000( defaultDir ).getAngle("rad").getValue() ;
[1974]902 }
[2002]903 mf.resetEpoch( mTimeB[irow] ) ;
904 MDirection::Convert toazel( dirType, MDirection::Ref( MDirection::AZEL, mf ) ) ;
905 Vector<Double> azel = toazel( defaultDir ).getAngle("rad").getValue() ;
906 *azRF = (Float)azel(0) ;
907 *elRF = (Float)azel(1) ;
908 *dirRF = defaultDir ;
909 *scrRF = defaultScanrate ;
[1974]910 }
911
[2002]912 // Polarization dependent things
913 for ( Int ipol = 0 ; ipol < npol ; ipol++ ) {
914 // POLNO
915 *polnoRF = polnos[ipol] ;
[1987]916
[2019]917 //*spRF = sp.row( ipol ) ;
918 //*ucarrRF = fl.row( ipol ) ;
919 //*tsysRF = tsys.row( ipol ) ;
920 spRF.define( sp.row( ipol ) ) ;
921 ucarrRF.define( fl.row( ipol ) ) ;
922 tsysRF.define( tsys.row( ipol ) ) ;
[2002]923 *tcalidRF = tcalids[ipol] ;
[1987]924
[2002]925 // Commit row
926 stab.addRow() ;
927 row.put( stab.nrow()-1 ) ;
[1987]928 }
[2004]929
930 cycle++ ;
[1987]931 }
[2002]932 tpoolr->destroy( mIntervalCol ) ;
933 tpoolr->destroy( mFlagRowCol ) ;
[1987]934
[2033]935// time1 = gettimeofday_sec() ;
936// os_ << "end 5th iteration: " << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
[1974]937
938 iter5.next() ;
939 }
[1978]940
941
[1974]942 iter4.next() ;
943 }
[1978]944
945
[1974]946 iter3.next() ;
947 }
[1978]948
[1990]949
[1974]950 iter2.next() ;
951 }
[1978]952
953
[1974]954 iter1.next() ;
955 }
956 if ( sdh.nbeam < nbeam ) sdh.nbeam = nbeam ;
[1978]957
[1974]958 iter0.next() ;
959 }
960
[1987]961
962 delete tpoolr ;
963
964
[1978]965 // Table Keywords
[2078]966 sdh.nif = ifmap.size() ;
[1987]967 if ( ( telescopeName == "" ) || ( antennaName == telescopeName ) ) {
[1974]968 sdh.antennaname = antennaName ;
969 }
970 else {
971 sdh.antennaname = telescopeName + "//" + antennaName ;
972 }
[2185]973 if ( stationName != "" && stationName != antennaName ) {
[1974]974 sdh.antennaname += "@" + stationName ;
975 }
[1987]976 ROArrayColumn<Double> pdirCol( pointtab, "DIRECTION" ) ;
977 String dirref = pdirCol.keywordSet().asRecord("MEASINFO").asString("Ref") ;
[1974]978 if ( dirref == "AZELGEO" || dirref == "AZEL" ) {
979 dirref = "J2000" ;
980 }
981 sscanf( dirref.chars()+1, "%f", &sdh.equinox ) ;
982 sdh.epoch = "UTC" ;
983 if (sdh.freqref == "TOPO") {
984 sdh.freqref = "TOPOCENT";
985 } else if (sdh.freqref == "GEO") {
986 sdh.freqref = "GEOCENTR";
987 } else if (sdh.freqref == "BARY") {
988 sdh.freqref = "BARYCENT";
989 } else if (sdh.freqref == "GALACTO") {
990 sdh.freqref = "GALACTOC";
991 } else if (sdh.freqref == "LGROUP") {
992 sdh.freqref = "LOCALGRP";
993 } else if (sdh.freqref == "CMB") {
994 sdh.freqref = "CMBDIPOL";
995 } else if (sdh.freqref == "REST") {
996 sdh.freqref = "SOURCE";
997 }
[2221]998 //if ( sdh.fluxunit == "" )
999 if ( sdh.fluxunit == "" || sdh.fluxunit == "CNTS" )
1000 sdh.fluxunit = "K" ;
[1974]1001 table_->setHeader( sdh ) ;
[1987]1002
1003 // save path to POINTING table
[2207]1004 // 2011/07/06 TN
1005 // Path to POINTING table in original MS will not be written
1006 // if getPt_ is True
[1987]1007 Path datapath( tablename_ ) ;
[2207]1008 if ( !getPt_ ) {
1009 String pTabName = datapath.absoluteName() + "/POINTING" ;
1010 stab.rwKeywordSet().define( "POINTING", pTabName ) ;
1011 }
[1987]1012
1013 // for GBT
[2024]1014 if ( antennaName.contains( "GBT" ) ) {
[1987]1015 String goTabName = datapath.absoluteName() + "/GBT_GO" ;
[1990]1016 stab.rwKeywordSet().define( "GBT_GO", goTabName ) ;
[1987]1017 }
[2022]1018
1019 // for MS created from ASDM
1020 //mstable_.keywordSet().print(cout) ;
1021 const TableRecord &msKeys = mstable_.keywordSet() ;
1022 uInt nfields = msKeys.nfields() ;
1023 for ( uInt ifield = 0 ; ifield < nfields ; ifield++ ) {
1024 String name = msKeys.name( ifield ) ;
1025 //os_ << "name = " << name << LogIO::POST ;
1026 if ( name.find( "ASDM" ) != String::npos ) {
1027 String asdmpath = msKeys.asTable( ifield ).tableName() ;
1028 os_ << "ASDM table: " << asdmpath << LogIO::POST ;
1029 stab.rwKeywordSet().define( name, asdmpath ) ;
1030 }
1031 }
1032
[2004]1033// double endSec = gettimeofday_sec() ;
1034// os_ << "end MSFiller::fill() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1035}
1036
1037void MSFiller::close()
1038{
[1987]1039 //tablesel_.closeSubTables() ;
[1974]1040 mstable_.closeSubTables() ;
[1987]1041 //tablesel_.unlock() ;
[1974]1042 mstable_.unlock() ;
1043}
1044
[1987]1045Int MSFiller::getSrcType( Int stateId, boost::object_pool<ROTableColumn> *tpool )
[1974]1046{
[2004]1047// double startSec = gettimeofday_sec() ;
1048// os_ << "start MSFiller::getSrcType() startSec=" << startSec << LogIO::POST ;
[1974]1049
1050 MSState statetab = mstable_.state() ;
[1987]1051 ROTableColumn *sharedCol ;
1052 sharedCol = tpool->construct( statetab, "OBS_MODE" ) ;
1053 String obsMode = sharedCol->asString( stateId ) ;
1054 tpool->destroy( sharedCol ) ;
1055 sharedCol = tpool->construct( statetab, "SIG" ) ;
1056 Bool sig = sharedCol->asBool( stateId ) ;
1057 tpool->destroy( sharedCol ) ;
1058 sharedCol = tpool->construct( statetab, "REF" ) ;
1059 Bool ref = sharedCol->asBool( stateId ) ;
1060 tpool->destroy( sharedCol ) ;
1061 sharedCol = tpool->construct( statetab, "CAL" ) ;
1062 Double cal = (Double)(sharedCol->asdouble( stateId )) ;
1063 tpool->destroy( sharedCol ) ;
[1974]1064 //os_ << "OBS_MODE = " << obsMode << LogIO::POST ;
1065
1066 // determine separator
1067 String sep = "" ;
[2039]1068 String tmpStr = obsMode.substr( 0, obsMode.find_first_of( "," ) ) ;
[2049]1069 //os_ << "tmpStr = " << tmpStr << LogIO::POST ;
[2039]1070 //if ( obsMode.find( ":" ) != String::npos ) {
1071 if ( tmpStr.find( ":" ) != String::npos ) {
[1974]1072 sep = ":" ;
1073 }
[2039]1074 //else if ( obsMode.find( "." ) != String::npos ) {
1075 else if ( tmpStr.find( "." ) != String::npos ) {
[1974]1076 sep = "." ;
1077 }
[2167]1078 else if ( tmpStr.find( "#" ) != String::npos ) {
1079 sep = "#" ;
1080 }
[2039]1081 //else if ( obsMode.find( "_" ) != String::npos ) {
1082 else if ( tmpStr.find( "_" ) != String::npos ) {
1083 sep = "_" ;
1084 }
[2049]1085 //os_ << "separator = " << sep << LogIO::POST ;
[2028]1086
[1974]1087 // determine SRCTYPE
1088 Int srcType = SrcType::NOTYPE ;
1089 if ( sep == ":" ) {
1090 // sep == ":"
1091 //
1092 // GBT case
1093 //
1094 // obsMode1=Nod
1095 // NOD
1096 // obsMode1=OffOn
1097 // obsMode2=PSWITCHON: PSON
1098 // obsMode2=PSWITCHOFF: PSOFF
1099 // obsMode1=??
1100 // obsMode2=FSWITCH:
1101 // SIG=1: FSON
1102 // REF=1: FSOFF
[1987]1103 // Calibration scan if CAL != 0
[1974]1104 Int epos = obsMode.find_first_of( sep ) ;
1105 Int nextpos = obsMode.find_first_of( sep, epos+1 ) ;
1106 String obsMode1 = obsMode.substr( 0, epos ) ;
1107 String obsMode2 = obsMode.substr( epos+1, nextpos-epos-1 ) ;
1108 if ( obsMode1 == "Nod" ) {
1109 srcType = SrcType::NOD ;
1110 }
1111 else if ( obsMode1 == "OffOn" ) {
1112 if ( obsMode2 == "PSWITCHON" ) srcType = SrcType::PSON ;
1113 if ( obsMode2 == "PSWITCHOFF" ) srcType = SrcType::PSOFF ;
1114 }
1115 else {
1116 if ( obsMode2 == "FSWITCH" ) {
1117 if ( sig ) srcType = SrcType::FSON ;
1118 if ( ref ) srcType = SrcType::FSOFF ;
1119 }
1120 }
[1987]1121 if ( cal > 0.0 ) {
1122 if ( srcType == SrcType::NOD )
1123 srcType = SrcType::NODCAL ;
1124 else if ( srcType == SrcType::PSON )
1125 srcType = SrcType::PONCAL ;
1126 else if ( srcType == SrcType::PSOFF )
1127 srcType = SrcType::POFFCAL ;
1128 else if ( srcType == SrcType::FSON )
1129 srcType = SrcType::FONCAL ;
1130 else if ( srcType == SrcType::FSOFF )
1131 srcType = SrcType::FOFFCAL ;
1132 else
1133 srcType = SrcType::CAL ;
1134 }
[1974]1135 }
[2167]1136 else if ( sep == "." || sep == "#" ) {
1137 // sep == "." or "#"
[1974]1138 //
[2039]1139 // ALMA & EVLA case (MS via ASDM) before3.1
[1974]1140 //
1141 // obsMode1=CALIBRATE_*
1142 // obsMode2=ON_SOURCE: PONCAL
1143 // obsMode2=OFF_SOURCE: POFFCAL
1144 // obsMode1=OBSERVE_TARGET
1145 // obsMode2=ON_SOURCE: PON
1146 // obsMode2=OFF_SOURCE: POFF
[2028]1147 string substr[2] ;
1148 int numSubstr = split( obsMode, substr, 2, "," ) ;
1149 //os_ << "numSubstr = " << numSubstr << LogIO::POST ;
1150 //for ( int i = 0 ; i < numSubstr ; i++ )
1151 //os_ << "substr[" << i << "] = " << substr[i] << LogIO::POST ;
1152 String obsType( substr[0] ) ;
1153 //os_ << "obsType = " << obsType << LogIO::POST ;
1154 Int epos = obsType.find_first_of( sep ) ;
1155 Int nextpos = obsType.find_first_of( sep, epos+1 ) ;
1156 String obsMode1 = obsType.substr( 0, epos ) ;
1157 String obsMode2 = obsType.substr( epos+1, nextpos-epos-1 ) ;
[2035]1158 //os_ << "obsMode1 = " << obsMode1 << LogIO::POST ;
1159 //os_ << "obsMode2 = " << obsMode2 << LogIO::POST ;
[1974]1160 if ( obsMode1.find( "CALIBRATE_" ) == 0 ) {
1161 if ( obsMode2 == "ON_SOURCE" ) srcType = SrcType::PONCAL ;
1162 if ( obsMode2 == "OFF_SOURCE" ) srcType = SrcType::POFFCAL ;
1163 }
1164 else if ( obsMode1 == "OBSERVE_TARGET" ) {
1165 if ( obsMode2 == "ON_SOURCE" ) srcType = SrcType::PSON ;
1166 if ( obsMode2 == "OFF_SOURCE" ) srcType = SrcType::PSOFF ;
1167 }
1168 }
[2039]1169 else if ( sep == "_" ) {
1170 // sep == "_"
1171 //
1172 // ALMA & EVLA case (MS via ASDM) after 3.2
1173 //
1174 // obsMode1=CALIBRATE_*
1175 // obsMode2=ON_SOURCE: PONCAL
1176 // obsMode2=OFF_SOURCE: POFFCAL
1177 // obsMode1=OBSERVE_TARGET
1178 // obsMode2=ON_SOURCE: PON
1179 // obsMode2=OFF_SOURCE: POFF
1180 string substr[2] ;
1181 int numSubstr = split( obsMode, substr, 2, "," ) ;
[2041]1182 //os_ << "numSubstr = " << numSubstr << LogIO::POST ;
[2039]1183 //for ( int i = 0 ; i < numSubstr ; i++ )
1184 //os_ << "substr[" << i << "] = " << substr[i] << LogIO::POST ;
1185 String obsType( substr[0] ) ;
1186 //os_ << "obsType = " << obsType << LogIO::POST ;
1187 string substr2[4] ;
1188 int numSubstr2 = split( obsType, substr2, 4, sep ) ;
1189 //Int epos = obsType.find_first_of( sep ) ;
1190 //Int nextpos = obsType.find_first_of( sep, epos+1 ) ;
1191 //String obsMode1 = obsType.substr( 0, epos ) ;
1192 //String obsMode2 = obsType.substr( epos+1, nextpos-epos-1 ) ;
1193 String obsMode1( substr2[0] ) ;
1194 String obsMode2( substr2[2] ) ;
1195 //os_ << "obsMode1 = " << obsMode1 << LogIO::POST ;
1196 //os_ << "obsMode2 = " << obsMode2 << LogIO::POST ;
1197 if ( obsMode1.find( "CALIBRATE" ) == 0 ) {
1198 if ( obsMode2 == "ON" ) srcType = SrcType::PONCAL ;
1199 if ( obsMode2 == "OFF" ) srcType = SrcType::POFFCAL ;
1200 }
1201 else if ( obsMode1 == "OBSERVE" ) {
1202 if ( obsMode2 == "ON" ) srcType = SrcType::PSON ;
1203 if ( obsMode2 == "OFF" ) srcType = SrcType::PSOFF ;
1204 }
1205 }
[1974]1206 else {
1207 if ( sig ) srcType = SrcType::SIG ;
1208 if ( ref ) srcType = SrcType::REF ;
1209 }
1210
1211 //os_ << "srcType = " << srcType << LogIO::POST ;
[2004]1212// double endSec = gettimeofday_sec() ;
1213// os_ << "end MSFiller::getSrcType() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1214 return srcType ;
1215}
1216
[2002]1217//Vector<uInt> MSFiller::getPolNo( Int corrType )
1218Block<uInt> MSFiller::getPolNo( Int corrType )
[1974]1219{
[2004]1220// double startSec = gettimeofday_sec() ;
1221// os_ << "start MSFiller::getPolNo() startSec=" << startSec << LogIO::POST ;
[2002]1222 Block<uInt> polno( 1 ) ;
[1974]1223
1224 if ( corrType == Stokes::I || corrType == Stokes::RR || corrType == Stokes::XX ) {
1225 polno = 0 ;
1226 }
1227 else if ( corrType == Stokes::Q || corrType == Stokes::LL || corrType == Stokes::YY ) {
1228 polno = 1 ;
1229 }
1230 else if ( corrType == Stokes::U ) {
1231 polno = 2 ;
1232 }
1233 else if ( corrType == Stokes::V ) {
1234 polno = 3 ;
1235 }
1236 else if ( corrType == Stokes::RL || corrType == Stokes::XY || corrType == Stokes::LR || corrType == Stokes::RL ) {
1237 polno.resize( 2 ) ;
1238 polno[0] = 2 ;
1239 polno[1] = 3 ;
1240 }
1241 else if ( corrType == Stokes::Plinear ) {
1242 polno[0] = 1 ;
1243 }
1244 else if ( corrType == Stokes::Pangle ) {
1245 polno[0] = 2 ;
1246 }
1247 else {
1248 polno = 99 ;
1249 }
1250 //os_ << "polno = " << polno << LogIO::POST ;
[2004]1251// double endSec = gettimeofday_sec() ;
1252// os_ << "end MSFiller::getPolNo() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1253
1254 return polno ;
1255}
1256
1257String MSFiller::getPolType( Int corrType )
1258{
[2004]1259// double startSec = gettimeofday_sec() ;
1260// os_ << "start MSFiller::getPolType() startSec=" << startSec << LogIO::POST ;
[1974]1261 String poltype = "" ;
1262
1263 if ( corrType == Stokes::I || corrType == Stokes::Q || corrType == Stokes::U || corrType == Stokes::V )
1264 poltype = "stokes" ;
1265 else if ( corrType == Stokes::XX || corrType == Stokes::YY || corrType == Stokes::XY || corrType == Stokes::YX )
1266 poltype = "linear" ;
1267 else if ( corrType == Stokes::RR || corrType == Stokes::LL || corrType == Stokes::RL || corrType == Stokes::LR )
1268 poltype = "circular" ;
1269 else if ( corrType == Stokes::Plinear || corrType == Stokes::Pangle )
1270 poltype = "linpol" ;
1271
[2004]1272// double endSec = gettimeofday_sec() ;
1273// os_ << "end MSFiller::getPolType() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1274 return poltype ;
1275}
1276
1277void MSFiller::fillWeather()
1278{
[2004]1279// double startSec = gettimeofday_sec() ;
1280// os_ << "start MSFiller::fillWeather() startSec=" << startSec << LogIO::POST ;
1281
1282 if ( !isWeather_ ) {
1283 // add dummy row
1284 table_->weather().table().addRow(1,True) ;
1285 return ;
1286 }
1287
[1987]1288 Table mWeather = mstable_.weather() ;
1289 //Table mWeatherSel = mWeather( mWeather.col("ANTENNA_ID") == antenna_ ).sort("TIME") ;
1290 Table mWeatherSel( mWeather( mWeather.col("ANTENNA_ID") == antenna_ ).sort("TIME") ) ;
[1974]1291 //os_ << "mWeatherSel.nrow() = " << mWeatherSel.nrow() << LogIO::POST ;
1292 if ( mWeatherSel.nrow() == 0 ) {
[2025]1293 os_ << "No rows with ANTENNA_ID = " << antenna_ << " in WEATHER table, Try -1..." << LogIO::POST ;
[1987]1294 mWeatherSel = Table( MSWeather( mWeather( mWeather.col("ANTENNA_ID") == -1 ) ) ) ;
[1974]1295 if ( mWeatherSel.nrow() == 0 ) {
1296 os_ << "No rows in WEATHER table" << LogIO::POST ;
1297 }
1298 }
[1987]1299 uInt wnrow = mWeatherSel.nrow() ;
[1974]1300 //os_ << "wnrow = " << wnrow << LogIO::POST ;
1301
1302 if ( wnrow == 0 )
1303 return ;
1304
1305 Table wtab = table_->weather().table() ;
1306 wtab.addRow( wnrow ) ;
1307
[1987]1308 ScalarColumn<Float> *fCol ;
1309 ROScalarColumn<Float> *sharedFloatCol ;
1310 if ( mWeatherSel.tableDesc().isColumn( "TEMPERATURE" ) ) {
1311 fCol = new ScalarColumn<Float>( wtab, "TEMPERATURE" ) ;
1312 sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "TEMPERATURE" ) ;
1313 fCol->putColumn( *sharedFloatCol ) ;
1314 delete sharedFloatCol ;
1315 delete fCol ;
1316 }
1317 if ( mWeatherSel.tableDesc().isColumn( "PRESSURE" ) ) {
1318 fCol = new ScalarColumn<Float>( wtab, "PRESSURE" ) ;
1319 sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "PRESSURE" ) ;
1320 fCol->putColumn( *sharedFloatCol ) ;
1321 delete sharedFloatCol ;
1322 delete fCol ;
1323 }
1324 if ( mWeatherSel.tableDesc().isColumn( "REL_HUMIDITY" ) ) {
1325 fCol = new ScalarColumn<Float>( wtab, "HUMIDITY" ) ;
1326 sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "REL_HUMIDITY" ) ;
1327 fCol->putColumn( *sharedFloatCol ) ;
1328 delete sharedFloatCol ;
1329 delete fCol ;
1330 }
1331 if ( mWeatherSel.tableDesc().isColumn( "WIND_SPEED" ) ) {
1332 fCol = new ScalarColumn<Float>( wtab, "WINDSPEED" ) ;
1333 sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "WIND_SPEED" ) ;
1334 fCol->putColumn( *sharedFloatCol ) ;
1335 delete sharedFloatCol ;
1336 delete fCol ;
1337 }
1338 if ( mWeatherSel.tableDesc().isColumn( "WIND_DIRECTION" ) ) {
1339 fCol = new ScalarColumn<Float>( wtab, "WINDAZ" ) ;
1340 sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "WIND_DIRECTION" ) ;
1341 fCol->putColumn( *sharedFloatCol ) ;
1342 delete sharedFloatCol ;
1343 delete fCol ;
1344 }
[1974]1345 ScalarColumn<uInt> idCol( wtab, "ID" ) ;
[1987]1346 for ( uInt irow = 0 ; irow < wnrow ; irow++ )
1347 idCol.put( irow, irow ) ;
[1974]1348
[1987]1349 ROScalarQuantColumn<Double> tqCol( mWeatherSel, "TIME" ) ;
1350 ROScalarColumn<Double> tCol( mWeatherSel, "TIME" ) ;
1351 String tUnit = tqCol.getUnits() ;
1352 mwTime_ = tCol.getColumn() ;
[1974]1353 if ( tUnit == "d" )
1354 mwTime_ *= 86400.0 ;
[1987]1355 tqCol.attach( mWeatherSel, "INTERVAL" ) ;
1356 tCol.attach( mWeatherSel, "INTERVAL" ) ;
1357 String iUnit = tqCol.getUnits() ;
1358 mwInterval_ = tCol.getColumn() ;
[1974]1359 if ( iUnit == "d" )
1360 mwInterval_ *= 86400.0 ;
1361 //os_ << "mwTime[0] = " << mwTime_[0] << " mwInterval[0] = " << mwInterval_[0] << LogIO::POST ;
[2004]1362// double endSec = gettimeofday_sec() ;
1363// os_ << "end MSFiller::fillWeather() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1364}
1365
1366void MSFiller::fillFocus()
1367{
[2004]1368// double startSec = gettimeofday_sec() ;
1369// os_ << "start MSFiller::fillFocus() startSec=" << startSec << LogIO::POST ;
[1974]1370 // tentative
[2228]1371 table_->focus().addEntry( 0.0, 0.0, 0.0, 0.0 ) ;
[2004]1372// double endSec = gettimeofday_sec() ;
1373// os_ << "end MSFiller::fillFocus() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1374}
1375
[2002]1376void MSFiller::fillTcal( boost::object_pool<ROTableColumn> *tpoolr )
[1974]1377{
[2004]1378// double startSec = gettimeofday_sec() ;
1379// os_ << "start MSFiller::fillTcal() startSec=" << startSec << LogIO::POST ;
[1987]1380
[2004]1381 if ( !isSysCal_ ) {
1382 // add dummy row
[2025]1383 os_ << "No SYSCAL rows" << LogIO::POST ;
[2004]1384 table_->tcal().table().addRow(1,True) ;
[2021]1385 Vector<Float> defaultTcal( 1, 1.0 ) ;
1386 ArrayColumn<Float> tcalCol( table_->tcal().table(), "TCAL" ) ;
1387 tcalCol.put( 0, defaultTcal ) ;
[2004]1388 return ;
1389 }
1390
[2159]1391 if ( colTcal_ == "NONE" ) {
1392 // add dummy row
1393 os_ << "No TCAL column" << LogIO::POST ;
1394 table_->tcal().table().addRow(1,True) ;
1395 Vector<Float> defaultTcal( 1, 1.0 ) ;
1396 ArrayColumn<Float> tcalCol( table_->tcal().table(), "TCAL" ) ;
1397 tcalCol.put( 0, defaultTcal ) ;
1398 return ;
1399 }
1400
[1987]1401 Table sctab = mstable_.sysCal() ;
[1974]1402 if ( sctab.nrow() == 0 ) {
[2025]1403 os_ << "No SYSCAL rows" << LogIO::POST ;
[1974]1404 return ;
1405 }
[1987]1406 Table sctabsel( sctab( sctab.col("ANTENNA_ID") == antenna_ ) ) ;
[1974]1407 if ( sctabsel.nrow() == 0 ) {
[2025]1408 os_ << "No SYSCAL rows" << LogIO::POST ;
[1974]1409 return ;
1410 }
[2164]1411 ROArrayColumn<Float> *tmpTcalCol = new ROArrayColumn<Float>( sctabsel, colTcal_ ) ;
1412 // return if any rows without Tcal value exists
1413 Bool notDefined = False ;
1414 for ( uInt irow = 0 ; irow < sctabsel.nrow() ; irow++ ) {
1415 if ( !tmpTcalCol->isDefined( irow ) ) {
1416 notDefined = True ;
1417 break ;
1418 }
1419 }
1420 if ( notDefined ) {
1421 os_ << "No TCAL value" << LogIO::POST ;
1422 delete tmpTcalCol ;
1423 table_->tcal().table().addRow(1,True) ;
1424 Vector<Float> defaultTcal( 1, 1.0 ) ;
1425 ArrayColumn<Float> tcalCol( table_->tcal().table(), "TCAL" ) ;
1426 tcalCol.put( 0, defaultTcal ) ;
1427 return ;
1428 }
[1987]1429 uInt npol = tmpTcalCol->shape( 0 )(0) ;
1430 delete tmpTcalCol ;
[1974]1431 //os_ << "fillTcal(): npol = " << npol << LogIO::POST ;
1432 Table tab = table_->tcal().table() ;
1433 ArrayColumn<Float> tcalCol( tab, "TCAL" ) ;
[1987]1434 ROTableColumn *sharedCol ;
[1974]1435 uInt oldnr = 0 ;
1436 uInt newnr = 0 ;
[2002]1437 TableRow row( tab ) ;
1438 TableRecord &trec = row.record() ;
1439 RecordFieldPtr<uInt> idRF( trec, "ID" ) ;
1440 RecordFieldPtr<String> timeRF( trec, "TIME" ) ;
1441 RecordFieldPtr< Array<Float> > tcalRF( trec, "TCAL" ) ;
[1974]1442 TableIterator iter0( sctabsel, "FEED_ID" ) ;
1443 while( !iter0.pastEnd() ) {
[1987]1444 Table t0 = iter0.table() ;
[1988]1445 sharedCol = tpoolr->construct( t0, "FEED_ID" ) ;
[1987]1446 Int feedId = sharedCol->asInt( 0 ) ;
[1988]1447 tpoolr->destroy( sharedCol ) ;
[1974]1448 TableIterator iter1( t0, "SPECTRAL_WINDOW_ID" ) ;
1449 while( !iter1.pastEnd() ) {
[1988]1450 Table t1 = iter1.table() ;
1451 sharedCol = tpoolr->construct( t1, "SPECTRAL_WINDOW_ID" ) ;
[1987]1452 Int spwId = sharedCol->asInt( 0 ) ;
[1988]1453 tpoolr->destroy( sharedCol ) ;
[2002]1454 tmpTcalCol = new ROArrayColumn<Float>( t1, colTcal_ ) ;
1455 ROScalarQuantColumn<Double> scTimeCol( t1, "TIME" ) ;
1456 Vector<uInt> idminmax( 2, oldnr ) ;
1457 for ( uInt irow = 0 ; irow < t1.nrow() ; irow++ ) {
1458 String sTime = MVTime( scTimeCol(irow) ).string( MVTime::YMD ) ;
1459 *timeRF = sTime ;
[1988]1460 uInt idx = oldnr ;
[2002]1461 Matrix<Float> subtcal = (*tmpTcalCol)( irow ) ;
[1988]1462 for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
[2002]1463 *idRF = idx++ ;
[2019]1464 //*tcalRF = subtcal.row( ipol ) ;
1465 tcalRF.define( subtcal.row( ipol ) ) ;
[2002]1466
1467 // commit row
1468 tab.addRow() ;
1469 row.put( tab.nrow()-1 ) ;
1470
1471 newnr++ ;
[1974]1472 }
[2002]1473 idminmax[0] = oldnr ;
[1988]1474 idminmax[1] = newnr - 1 ;
1475 oldnr = newnr ;
1476
[1990]1477 String key = keyTcal( feedId, spwId, sTime ) ;
[1988]1478 tcalrec_.define( key, idminmax ) ;
[1974]1479 }
[2002]1480 delete tmpTcalCol ;
[1974]1481 iter1++ ;
1482 }
1483 iter0++ ;
1484 }
1485
1486 //tcalrec_.print( std::cout ) ;
[2004]1487// double endSec = gettimeofday_sec() ;
1488// os_ << "end MSFiller::fillTcal() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1489}
1490
1491uInt MSFiller::getWeatherId( uInt idx, Double wtime )
1492{
[2004]1493// double startSec = gettimeofday_sec() ;
1494// os_ << "start MSFiller::getWeatherId() startSec=" << startSec << LogIO::POST ;
[1974]1495 uInt nrow = mwTime_.size() ;
[2019]1496 if ( nrow < 2 )
[1974]1497 return 0 ;
1498 uInt wid = nrow ;
1499 for ( uInt i = idx ; i < nrow-1 ; i++ ) {
1500 Double tStart = mwTime_[i]-0.5*mwInterval_[i] ;
1501 // use of INTERVAL column is problematic
1502 // since there are "blank" time of weather monitoring
1503 //Double tEnd = tStart + mwInterval_[i] ;
1504 Double tEnd = mwTime_[i+1]-0.5*mwInterval_[i+1] ;
1505 //os_ << "tStart = " << tStart << " dtEnd = " << tEnd-tStart << " dwtime = " << wtime-tStart << LogIO::POST ;
1506 if ( wtime >= tStart && wtime <= tEnd ) {
1507 wid = i ;
1508 break ;
1509 }
1510 }
1511 if ( wid == nrow ) {
1512 uInt i = nrow - 1 ;
1513 Double tStart = mwTime_[i-1]+0.5*mwInterval_[i-1] ;
1514 Double tEnd = mwTime_[i]+0.5*mwInterval_[i] ;
1515 //os_ << "tStart = " << tStart << " dtEnd = " << tEnd-tStart << " dwtime = " << wtime-tStart << LogIO::POST ;
1516 if ( wtime >= tStart && wtime <= tEnd )
[2021]1517 wid = i-1 ;
1518 else
[1974]1519 wid = i ;
1520 }
1521
1522 //if ( wid == nrow )
1523 //os_ << LogIO::WARN << "Couldn't find correct WEATHER_ID for time " << wtime << LogIO::POST ;
1524
[2004]1525// double endSec = gettimeofday_sec() ;
1526// os_ << "end MSFiller::getWeatherId() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1527 return wid ;
1528}
1529
[2017]1530void MSFiller::getSysCalTime( Vector<MEpoch> &scTime, Vector<Double> &scInterval, Block<MEpoch> &tcol, Block<Int> &tidx )
[1974]1531{
[2004]1532// double startSec = gettimeofday_sec() ;
1533// os_ << "start MSFiller::getSysCalTime() startSec=" << startSec << LogIO::POST ;
1534
1535 if ( !isSysCal_ )
1536 return ;
1537
[2017]1538 uInt nrow = tidx.nelements() ;
[2002]1539 if ( scTime.nelements() == 0 )
[1993]1540 return ;
[2019]1541 else if ( scTime.nelements() == 1 ) {
1542 tidx[0] = 0 ;
1543 return ;
1544 }
[2002]1545 uInt scnrow = scTime.nelements() ;
[1974]1546 uInt idx = 0 ;
1547 const Double half = 0.5e0 ;
[2003]1548 // execute binary search
1549 idx = binarySearch( scTime, tcol[0].get( "s" ).getValue() ) ;
1550 if ( idx != 0 )
1551 idx -= 1 ;
[1974]1552 for ( uInt i = 0 ; i < nrow ; i++ ) {
[2002]1553 Double t = tcol[i].get( "s" ).getValue() ;
[2017]1554 Double tsc = scTime[0].get( "s" ).getValue() ;
1555 if ( t < tsc ) {
1556 tidx[i] = 0 ;
1557 continue ;
1558 }
[1974]1559 for ( uInt j = idx ; j < scnrow-1 ; j++ ) {
[2002]1560 Double tsc1 = scTime[j].get( "s" ).getValue() ;
1561 Double dt1 = scInterval[j] ;
1562 Double tsc2 = scTime[j+1].get( "s" ).getValue() ;
1563 Double dt2 = scInterval[j+1] ;
[1974]1564 if ( t > tsc1-half*dt1 && t <= tsc2-half*dt2 ) {
[2002]1565 tidx[i] = j ;
[1974]1566 idx = j ;
1567 break ;
1568 }
1569 }
[2017]1570 if ( tidx[i] == -1 ) {
[2022]1571// Double tsc = scTime[scnrow-1].get( "s" ).getValue() ;
1572// Double dt = scInterval[scnrow-1] ;
[2017]1573// if ( t <= tsc+0.5*dt ) {
1574// tidx[i] = scnrow-1 ;
1575// }
1576 tidx[i] = scnrow-1 ;
[1974]1577 }
1578 }
[2004]1579// double endSec = gettimeofday_sec() ;
1580// os_ << "end MSFiller::getSysCalTime() endSec=" << endSec << " (" << endSec-startSec << "sec) scnrow = " << scnrow << " tcol.nelements = " << tcol.nelements() << LogIO::POST ;
[1993]1581 return ;
[1974]1582}
1583
[2004]1584Block<uInt> MSFiller::getTcalId( Int fid, Int spwid, MEpoch &t )
1585{
[2002]1586// double startSec = gettimeofday_sec() ;
[2004]1587// os_ << "start MSFiller::getTcalId() startSec=" << startSec << LogIO::POST ;
1588 //if ( table_->tcal().table().nrow() == 0 ) {
1589 if ( !isSysCal_ ) {
[1988]1590 os_ << "No TCAL rows" << LogIO::POST ;
[2184]1591 Block<uInt> tcalids( 4, 0 ) ;
[1988]1592 return tcalids ;
1593 }
[2004]1594 //String sctime = MVTime( Quantum<Double>(t,"s") ).string(MVTime::YMD) ;
1595 String sctime = MVTime( t.getValue() ).string(MVTime::YMD) ;
[1990]1596 String key = keyTcal( fid, spwid, sctime ) ;
[1988]1597 if ( !tcalrec_.isDefined( key ) ) {
[1974]1598 os_ << "No TCAL rows" << LogIO::POST ;
[2184]1599 Block<uInt> tcalids( 4, 0 ) ;
[1974]1600 return tcalids ;
1601 }
[1988]1602 Vector<uInt> ids = tcalrec_.asArrayuInt( key ) ;
1603 uInt npol = ids[1] - ids[0] + 1 ;
[2002]1604 Block<uInt> tcalids( npol ) ;
[1988]1605 tcalids[0] = ids[0] ;
1606 tcalids[1] = ids[1] ;
1607 for ( uInt ipol = 2 ; ipol < npol ; ipol++ )
1608 tcalids[ipol] = ids[0] + ipol - 1 ;
1609
[2004]1610// double endSec = gettimeofday_sec() ;
1611// os_ << "end MSFiller::getTcalId() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1612 return tcalids ;
1613}
1614
[1987]1615uInt MSFiller::getDirection( uInt idx, Vector<Double> &dir, Vector<Double> &srate, String &ref, MSPointing &tab, Double t )
[1974]1616{
[2004]1617// double startSec = gettimeofday_sec() ;
1618// os_ << "start MSFiller::getDirection() startSec=" << startSec << LogIO::POST ;
[1974]1619 // assume that cols is sorted by TIME
1620 Bool doInterp = False ;
[1987]1621 //uInt nrow = cols.nrow() ;
1622 uInt nrow = tab.nrow() ;
[1974]1623 if ( nrow == 0 )
1624 return 0 ;
[1987]1625 ROScalarMeasColumn<MEpoch> tcol( tab, "TIME" ) ;
1626 ROArrayMeasColumn<MDirection> dmcol( tab, "DIRECTION" ) ;
1627 ROArrayColumn<Double> dcol( tab, "DIRECTION" ) ;
[2003]1628 // binary search if idx == 0
1629 if ( idx == 0 ) {
1630 uInt nrowb = 75000 ;
[2004]1631 if ( nrow > nrowb ) {
[2003]1632 uInt nblock = nrow / nrowb + 1 ;
1633 for ( uInt iblock = 0 ; iblock < nblock ; iblock++ ) {
[2004]1634 uInt high = min( nrowb, nrow-iblock*nrowb ) ;
[2003]1635
1636 if ( tcol( high-1 ).get( "s" ).getValue() < t ) {
[2004]1637 idx = iblock * nrowb ;
[2003]1638 continue ;
1639 }
1640
1641 Vector<MEpoch> tarr( high ) ;
1642 for ( uInt irow = 0 ; irow < high ; irow++ ) {
[2004]1643 tarr[irow] = tcol( iblock*nrowb+irow ) ;
[2003]1644 }
1645
1646 uInt bidx = binarySearch( tarr, t ) ;
1647
[2004]1648 idx = iblock * nrowb + bidx ;
[2003]1649 break ;
1650 }
1651 }
1652 else {
1653 Vector<MEpoch> tarr( nrow ) ;
1654 for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1655 tarr[irow] = tcol( irow ) ;
1656 }
1657 idx = binarySearch( tarr, t ) ;
1658 }
1659 }
[1974]1660 // ensure that tcol(idx) < t
1661 //os_ << "tcol(idx) = " << tcol(idx).get("s").getValue() << " t = " << t << " diff = " << tcol(idx).get("s").getValue()-t << endl ;
1662 while ( tcol(idx).get("s").getValue() > t && idx > 0 )
1663 idx-- ;
1664 //os_ << "idx = " << idx << LogIO::POST ;
1665
1666 // index search
1667 for ( uInt i = idx ; i < nrow ; i++ ) {
1668 Double tref = tcol( i ).get( "s" ).getValue() ;
1669 if ( tref == t ) {
1670 idx = i ;
1671 break ;
1672 }
1673 else if ( tref > t ) {
1674 if ( i == 0 ) {
1675 idx = i ;
1676 }
1677 else {
1678 idx = i-1 ;
1679 doInterp = True ;
1680 }
1681 break ;
1682 }
1683 else {
1684 idx = nrow - 1 ;
1685 }
1686 }
1687 //os_ << "searched idx = " << idx << LogIO::POST ;
1688
1689 //os_ << "dmcol(idx).shape() = " << dmcol(idx).shape() << LogIO::POST ;
1690 IPosition ip( dmcol(idx).shape().nelements(), 0 ) ;
1691 //os_ << "ip = " << ip << LogIO::POST ;
1692 ref = dmcol(idx)(ip).getRefString() ;
1693 //os_ << "ref = " << ref << LogIO::POST ;
1694 if ( doInterp ) {
1695 //os_ << "do interpolation" << LogIO::POST ;
1696 //os_ << "dcol(idx).shape() = " << dcol(idx).shape() << LogIO::POST ;
1697 Double tref0 = tcol(idx).get("s").getValue() ;
1698 Double tref1 = tcol(idx+1).get("s").getValue() ;
[1987]1699 Matrix<Double> mdir0 = dcol( idx ) ;
1700 Matrix<Double> mdir1 = dcol( idx+1 ) ;
1701 Vector<Double> dir0 = mdir0.column( 0 ) ;
[1974]1702 //os_ << "dir0 = " << dir0 << LogIO::POST ;
[1987]1703 Vector<Double> dir1 = mdir1.column( 0 ) ;
[1974]1704 //os_ << "dir1 = " << dir1 << LogIO::POST ;
1705 Double dt0 = t - tref0 ;
1706 Double dt1 = tref1 - t ;
1707 dir.reference( (dt0*dir1+dt1*dir0)/(dt0+dt1) ) ;
[1987]1708 if ( mdir0.ncolumn() > 1 ) {
1709 if ( dt0 >= dt1 )
1710 srate.reference( mdir0.column( 1 ) ) ;
1711 else
1712 srate.reference( mdir1.column( 1 ) ) ;
[1974]1713 }
1714 //os_ << "dir = " << dir << LogIO::POST ;
1715 }
1716 else {
1717 //os_ << "no interpolation" << LogIO::POST ;
[1987]1718 Matrix<Double> mdir0 = dcol( idx ) ;
1719 dir.reference( mdir0.column( 0 ) ) ;
1720 if ( mdir0.ncolumn() > 1 )
1721 srate.reference( mdir0.column( 1 ) ) ;
[1974]1722 }
1723
[2004]1724// double endSec = gettimeofday_sec() ;
1725// os_ << "end MSFiller::getDirection() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
[1974]1726 return idx ;
1727}
1728
[1990]1729String MSFiller::keyTcal( Int feedid, Int spwid, String stime )
1730{
1731 String sfeed = "FEED" + String::toString( feedid ) ;
1732 String sspw = "SPW" + String::toString( spwid ) ;
1733 return sfeed+":"+sspw+":"+stime ;
1734}
1735
[2003]1736uInt MSFiller::binarySearch( Vector<MEpoch> &timeList, Double target )
1737{
1738 Int low = 0 ;
1739 Int high = timeList.nelements() ;
1740 uInt idx = 0 ;
1741
1742 while ( low <= high ) {
1743 idx = (Int)( 0.5 * ( low + high ) ) ;
1744 Double t = timeList[idx].get( "s" ).getValue() ;
1745 if ( t < target )
1746 low = idx + 1 ;
1747 else if ( t > target )
1748 high = idx - 1 ;
1749 else
1750 return idx ;
1751 }
1752
1753 idx = max( 0, min( low, high ) ) ;
1754
1755 return idx ;
1756
1757}
1758
[2217]1759string MSFiller::getFrame()
1760{
1761 MFrequency::Types frame = MFrequency::DEFAULT ;
1762 ROTableColumn numChanCol( mstable_.spectralWindow(), "NUM_CHAN" ) ;
1763 ROTableColumn measFreqRefCol( mstable_.spectralWindow(), "MEAS_FREQ_REF" ) ;
1764 uInt nrow = numChanCol.nrow() ;
1765 Vector<Int> measFreqRef( nrow, MFrequency::DEFAULT ) ;
1766 uInt nref = 0 ;
1767 for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1768 if ( numChanCol.asInt( irow ) != 4 ) { // exclude WVR
1769 measFreqRef[nref] = measFreqRefCol.asInt( irow ) ;
1770 nref++ ;
1771 }
1772 }
1773 if ( nref > 0 )
1774 frame = (MFrequency::Types)measFreqRef[0] ;
1775
1776 return MFrequency::showType( frame ) ;
1777}
[1974]1778} ;
1779
Note: See TracBrowser for help on using the repository browser.