source: trunk/src/MSFiller.cpp@ 2207

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

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

If getPt_ is True, path to POINTING subtable is not stored in header
keyword.

Bug fix on MSWriter:

write TARGET column in POINTING table
NUM_POLY must be dir.ncolumn()-1, not dir.ncolumn()


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