source: trunk/src/MSFiller.cpp@ 2128

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

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Bug fix on calibration script and polarization type resolution.


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