source: branches/parallel/src/MSFiller.cpp@ 2317

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

Merge changes for speed up of MSFiller.
Revisions to be merged are r2217, r2219, r2221-2224, r2226, r2228,
r2231-2232, r2234, and r2237-2239.


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