source: trunk/src/MSFiller.cpp@ 2249

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

More improvement


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