source: trunk/src/MSFiller.cpp@ 2221

Last change on this file since 2221 was 2221, checked in by Takeshi Nakazato, 14 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes/No

Interface Changes: Yes/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...

Default flux unit changed from "" to "K".


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