source: trunk/src/MSFiller.cpp@ 2245

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

Changed a way to retrieve spectral data.


File size: 68.6 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 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( sysCalIdx[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 != "" && stationName != antennaName ) {
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/07/06 TN
742 // Path to POINTING table in original MS will not be written
743 // if getPt_ is True
744 Path datapath( tablename_ ) ;
745 if ( !getPt_ ) {
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 ( tmpStr.find( "#" ) != String::npos ) {
807 sep = "#" ;
808 }
809 //else if ( obsMode.find( "_" ) != String::npos ) {
810 else if ( tmpStr.find( "_" ) != String::npos ) {
811 sep = "_" ;
812 }
813 //os_ << "separator = " << sep << LogIO::POST ;
814
815 // determine SRCTYPE
816 Int srcType = SrcType::NOTYPE ;
817 if ( sep == ":" ) {
818 // sep == ":"
819 //
820 // GBT case
821 //
822 // obsMode1=Nod
823 // NOD
824 // obsMode1=OffOn
825 // obsMode2=PSWITCHON: PSON
826 // obsMode2=PSWITCHOFF: PSOFF
827 // obsMode1=??
828 // obsMode2=FSWITCH:
829 // SIG=1: FSON
830 // REF=1: FSOFF
831 // Calibration scan if CAL != 0
832 Int epos = obsMode.find_first_of( sep ) ;
833 Int nextpos = obsMode.find_first_of( sep, epos+1 ) ;
834 String obsMode1 = obsMode.substr( 0, epos ) ;
835 String obsMode2 = obsMode.substr( epos+1, nextpos-epos-1 ) ;
836 if ( obsMode1 == "Nod" ) {
837 srcType = SrcType::NOD ;
838 }
839 else if ( obsMode1 == "OffOn" ) {
840 if ( obsMode2 == "PSWITCHON" ) srcType = SrcType::PSON ;
841 if ( obsMode2 == "PSWITCHOFF" ) srcType = SrcType::PSOFF ;
842 }
843 else {
844 if ( obsMode2 == "FSWITCH" ) {
845 if ( sig ) srcType = SrcType::FSON ;
846 if ( ref ) srcType = SrcType::FSOFF ;
847 }
848 }
849 if ( cal > 0.0 ) {
850 if ( srcType == SrcType::NOD )
851 srcType = SrcType::NODCAL ;
852 else if ( srcType == SrcType::PSON )
853 srcType = SrcType::PONCAL ;
854 else if ( srcType == SrcType::PSOFF )
855 srcType = SrcType::POFFCAL ;
856 else if ( srcType == SrcType::FSON )
857 srcType = SrcType::FONCAL ;
858 else if ( srcType == SrcType::FSOFF )
859 srcType = SrcType::FOFFCAL ;
860 else
861 srcType = SrcType::CAL ;
862 }
863 }
864 else if ( sep == "." || sep == "#" ) {
865 // sep == "." or "#"
866 //
867 // ALMA & EVLA case (MS via ASDM) before3.1
868 //
869 // obsMode1=CALIBRATE_*
870 // obsMode2=ON_SOURCE: PONCAL
871 // obsMode2=OFF_SOURCE: POFFCAL
872 // obsMode1=OBSERVE_TARGET
873 // obsMode2=ON_SOURCE: PON
874 // obsMode2=OFF_SOURCE: POFF
875 string substr[2] ;
876 int numSubstr = split( obsMode, substr, 2, "," ) ;
877 //os_ << "numSubstr = " << numSubstr << LogIO::POST ;
878 //for ( int i = 0 ; i < numSubstr ; i++ )
879 //os_ << "substr[" << i << "] = " << substr[i] << LogIO::POST ;
880 String obsType( substr[0] ) ;
881 //os_ << "obsType = " << obsType << LogIO::POST ;
882 Int epos = obsType.find_first_of( sep ) ;
883 Int nextpos = obsType.find_first_of( sep, epos+1 ) ;
884 String obsMode1 = obsType.substr( 0, epos ) ;
885 String obsMode2 = obsType.substr( epos+1, nextpos-epos-1 ) ;
886 //os_ << "obsMode1 = " << obsMode1 << LogIO::POST ;
887 //os_ << "obsMode2 = " << obsMode2 << LogIO::POST ;
888 if ( obsMode1.find( "CALIBRATE_" ) == 0 ) {
889 if ( obsMode2 == "ON_SOURCE" ) srcType = SrcType::PONCAL ;
890 if ( obsMode2 == "OFF_SOURCE" ) srcType = SrcType::POFFCAL ;
891 }
892 else if ( obsMode1 == "OBSERVE_TARGET" ) {
893 if ( obsMode2 == "ON_SOURCE" ) srcType = SrcType::PSON ;
894 if ( obsMode2 == "OFF_SOURCE" ) srcType = SrcType::PSOFF ;
895 }
896 }
897 else if ( sep == "_" ) {
898 // sep == "_"
899 //
900 // ALMA & EVLA case (MS via ASDM) after 3.2
901 //
902 // obsMode1=CALIBRATE_*
903 // obsMode2=ON_SOURCE: PONCAL
904 // obsMode2=OFF_SOURCE: POFFCAL
905 // obsMode1=OBSERVE_TARGET
906 // obsMode2=ON_SOURCE: PON
907 // obsMode2=OFF_SOURCE: POFF
908 string substr[2] ;
909 int numSubstr = split( obsMode, substr, 2, "," ) ;
910 //os_ << "numSubstr = " << numSubstr << LogIO::POST ;
911 //for ( int i = 0 ; i < numSubstr ; i++ )
912 //os_ << "substr[" << i << "] = " << substr[i] << LogIO::POST ;
913 String obsType( substr[0] ) ;
914 //os_ << "obsType = " << obsType << LogIO::POST ;
915 string substr2[4] ;
916 int numSubstr2 = split( obsType, substr2, 4, sep ) ;
917 //Int epos = obsType.find_first_of( sep ) ;
918 //Int nextpos = obsType.find_first_of( sep, epos+1 ) ;
919 //String obsMode1 = obsType.substr( 0, epos ) ;
920 //String obsMode2 = obsType.substr( epos+1, nextpos-epos-1 ) ;
921 String obsMode1( substr2[0] ) ;
922 String obsMode2( substr2[2] ) ;
923 //os_ << "obsMode1 = " << obsMode1 << LogIO::POST ;
924 //os_ << "obsMode2 = " << obsMode2 << LogIO::POST ;
925 if ( obsMode1.find( "CALIBRATE" ) == 0 ) {
926 if ( obsMode2 == "ON" ) srcType = SrcType::PONCAL ;
927 if ( obsMode2 == "OFF" ) srcType = SrcType::POFFCAL ;
928 }
929 else if ( obsMode1 == "OBSERVE" ) {
930 if ( obsMode2 == "ON" ) srcType = SrcType::PSON ;
931 if ( obsMode2 == "OFF" ) srcType = SrcType::PSOFF ;
932 }
933 }
934 else {
935 if ( sig ) srcType = SrcType::SIG ;
936 if ( ref ) srcType = SrcType::REF ;
937 }
938
939 //os_ << "srcType = " << srcType << LogIO::POST ;
940 //double endSec = gettimeofday_sec() ;
941 //os_ << "end MSFiller::getSrcType() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
942 return srcType ;
943}
944
945//Vector<uInt> MSFiller::getPolNo( Int corrType )
946Block<uInt> MSFiller::getPolNo( Int corrType )
947{
948 //double startSec = gettimeofday_sec() ;
949 //os_ << "start MSFiller::getPolNo() startSec=" << startSec << LogIO::POST ;
950 Block<uInt> polno( 1 ) ;
951
952 if ( corrType == Stokes::I || corrType == Stokes::RR || corrType == Stokes::XX ) {
953 polno = 0 ;
954 }
955 else if ( corrType == Stokes::Q || corrType == Stokes::LL || corrType == Stokes::YY ) {
956 polno = 1 ;
957 }
958 else if ( corrType == Stokes::U ) {
959 polno = 2 ;
960 }
961 else if ( corrType == Stokes::V ) {
962 polno = 3 ;
963 }
964 else if ( corrType == Stokes::RL || corrType == Stokes::XY || corrType == Stokes::LR || corrType == Stokes::RL ) {
965 polno.resize( 2 ) ;
966 polno[0] = 2 ;
967 polno[1] = 3 ;
968 }
969 else if ( corrType == Stokes::Plinear ) {
970 polno[0] = 1 ;
971 }
972 else if ( corrType == Stokes::Pangle ) {
973 polno[0] = 2 ;
974 }
975 else {
976 polno = 99 ;
977 }
978 //os_ << "polno = " << polno << LogIO::POST ;
979 //double endSec = gettimeofday_sec() ;
980 //os_ << "end MSFiller::getPolNo() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
981
982 return polno ;
983}
984
985String MSFiller::getPolType( Int corrType )
986{
987 //double startSec = gettimeofday_sec() ;
988 //os_ << "start MSFiller::getPolType() startSec=" << startSec << LogIO::POST ;
989 String poltype = "" ;
990
991 if ( corrType == Stokes::I || corrType == Stokes::Q || corrType == Stokes::U || corrType == Stokes::V )
992 poltype = "stokes" ;
993 else if ( corrType == Stokes::XX || corrType == Stokes::YY || corrType == Stokes::XY || corrType == Stokes::YX )
994 poltype = "linear" ;
995 else if ( corrType == Stokes::RR || corrType == Stokes::LL || corrType == Stokes::RL || corrType == Stokes::LR )
996 poltype = "circular" ;
997 else if ( corrType == Stokes::Plinear || corrType == Stokes::Pangle )
998 poltype = "linpol" ;
999
1000 //double endSec = gettimeofday_sec() ;
1001 //os_ << "end MSFiller::getPolType() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1002 return poltype ;
1003}
1004
1005void MSFiller::fillWeather()
1006{
1007 //double startSec = gettimeofday_sec() ;
1008 //os_ << "start MSFiller::fillWeather() startSec=" << startSec << LogIO::POST ;
1009
1010 if ( !isWeather_ ) {
1011 // add dummy row
1012 table_->weather().table().addRow(1,True) ;
1013 return ;
1014 }
1015
1016 Table mWeather = mstable_.weather() ;
1017 //Table mWeatherSel = mWeather( mWeather.col("ANTENNA_ID") == antenna_ ).sort("TIME") ;
1018 Table mWeatherSel( mWeather( mWeather.col("ANTENNA_ID") == antenna_ ).sort("TIME") ) ;
1019 //os_ << "mWeatherSel.nrow() = " << mWeatherSel.nrow() << LogIO::POST ;
1020 if ( mWeatherSel.nrow() == 0 ) {
1021 os_ << "No rows with ANTENNA_ID = " << antenna_ << " in WEATHER table, Try -1..." << LogIO::POST ;
1022 mWeatherSel = Table( MSWeather( mWeather( mWeather.col("ANTENNA_ID") == -1 ) ) ) ;
1023 if ( mWeatherSel.nrow() == 0 ) {
1024 os_ << "No rows in WEATHER table" << LogIO::POST ;
1025 }
1026 }
1027 uInt wnrow = mWeatherSel.nrow() ;
1028 //os_ << "wnrow = " << wnrow << LogIO::POST ;
1029
1030 if ( wnrow == 0 )
1031 return ;
1032
1033 Table wtab = table_->weather().table() ;
1034 wtab.addRow( wnrow ) ;
1035
1036 Bool stationInfoExists = mWeatherSel.tableDesc().isColumn( "NS_WX_STATION_ID" ) ;
1037 Int stationId = -1 ;
1038 if ( stationInfoExists ) {
1039 // determine which station is closer
1040 ROScalarColumn<Int> stationCol( mWeatherSel, "NS_WX_STATION_ID" ) ;
1041 ROArrayColumn<Double> stationPosCol( mWeatherSel, "NS_WX_STATION_POSITION" ) ;
1042 Vector<Int> stationIds = stationCol.getColumn() ;
1043 Vector<Int> stationIdList( 0 ) ;
1044 Matrix<Double> stationPosList( 0, 3, 0.0 ) ;
1045 uInt numStation = 0 ;
1046 for ( uInt i = 0 ; i < stationIds.size() ; i++ ) {
1047 if ( !anyEQ( stationIdList, stationIds[i] ) ) {
1048 numStation++ ;
1049 stationIdList.resize( numStation, True ) ;
1050 stationIdList[numStation-1] = stationIds[i] ;
1051 stationPosList.resize( numStation, 3, True ) ;
1052 stationPosList.row( numStation-1 ) = stationPosCol( i ) ;
1053 }
1054 }
1055 //os_ << "staionIdList = " << stationIdList << endl ;
1056 Table mAntenna = mstable_.antenna() ;
1057 ROArrayColumn<Double> antposCol( mAntenna, "POSITION" ) ;
1058 Vector<Double> antpos = antposCol( antenna_ ) ;
1059 Double minDiff = -1.0 ;
1060 for ( uInt i = 0 ; i < stationIdList.size() ; i++ ) {
1061 Double diff = sum( square( antpos - stationPosList.row( i ) ) ) ;
1062 if ( minDiff < 0.0 || minDiff > diff ) {
1063 minDiff = diff ;
1064 stationId = stationIdList[i] ;
1065 }
1066 }
1067 }
1068 //os_ << "stationId = " << stationId << endl ;
1069
1070 ScalarColumn<Float> *fCol ;
1071 ROScalarColumn<Float> *sharedFloatCol ;
1072 if ( mWeatherSel.tableDesc().isColumn( "TEMPERATURE" ) ) {
1073 fCol = new ScalarColumn<Float>( wtab, "TEMPERATURE" ) ;
1074 sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "TEMPERATURE" ) ;
1075 fCol->putColumn( *sharedFloatCol ) ;
1076 delete sharedFloatCol ;
1077 delete fCol ;
1078 }
1079 if ( mWeatherSel.tableDesc().isColumn( "PRESSURE" ) ) {
1080 fCol = new ScalarColumn<Float>( wtab, "PRESSURE" ) ;
1081 sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "PRESSURE" ) ;
1082 fCol->putColumn( *sharedFloatCol ) ;
1083 delete sharedFloatCol ;
1084 delete fCol ;
1085 }
1086 if ( mWeatherSel.tableDesc().isColumn( "REL_HUMIDITY" ) ) {
1087 fCol = new ScalarColumn<Float>( wtab, "HUMIDITY" ) ;
1088 sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "REL_HUMIDITY" ) ;
1089 fCol->putColumn( *sharedFloatCol ) ;
1090 delete sharedFloatCol ;
1091 delete fCol ;
1092 }
1093 if ( mWeatherSel.tableDesc().isColumn( "WIND_SPEED" ) ) {
1094 fCol = new ScalarColumn<Float>( wtab, "WINDSPEED" ) ;
1095 sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "WIND_SPEED" ) ;
1096 fCol->putColumn( *sharedFloatCol ) ;
1097 delete sharedFloatCol ;
1098 delete fCol ;
1099 }
1100 if ( mWeatherSel.tableDesc().isColumn( "WIND_DIRECTION" ) ) {
1101 fCol = new ScalarColumn<Float>( wtab, "WINDAZ" ) ;
1102 sharedFloatCol = new ROScalarColumn<Float>( mWeatherSel, "WIND_DIRECTION" ) ;
1103 fCol->putColumn( *sharedFloatCol ) ;
1104 delete sharedFloatCol ;
1105 delete fCol ;
1106 }
1107 ScalarColumn<uInt> idCol( wtab, "ID" ) ;
1108 for ( uInt irow = 0 ; irow < wnrow ; irow++ )
1109 idCol.put( irow, irow ) ;
1110
1111 ROScalarQuantColumn<Double> tqCol( mWeatherSel, "TIME" ) ;
1112 ROScalarColumn<Double> tCol( mWeatherSel, "TIME" ) ;
1113 String tUnit = tqCol.getUnits() ;
1114 Vector<Double> mwTime = tCol.getColumn() ;
1115 if ( tUnit == "d" )
1116 mwTime *= 86400.0 ;
1117 tqCol.attach( mWeatherSel, "INTERVAL" ) ;
1118 tCol.attach( mWeatherSel, "INTERVAL" ) ;
1119 String iUnit = tqCol.getUnits() ;
1120 Vector<Double> mwInterval = tCol.getColumn() ;
1121 if ( iUnit == "d" )
1122 mwInterval *= 86400.0 ;
1123
1124 if ( stationId > 0 ) {
1125 ROScalarColumn<Int> stationCol( mWeatherSel, "NS_WX_STATION_ID" ) ;
1126 Vector<Int> stationVec = stationCol.getColumn() ;
1127 uInt wsnrow = ntrue( stationVec == stationId ) ;
1128 mwTime_.resize( wsnrow ) ;
1129 mwInterval_.resize( wsnrow ) ;
1130 mwIndex_.resize( wsnrow ) ;
1131 uInt wsidx = 0 ;
1132 for ( uInt irow = 0 ; irow < wnrow ; irow++ ) {
1133 if ( stationId == stationVec[irow] ) {
1134 mwTime_[wsidx] = mwTime[irow] ;
1135 mwInterval_[wsidx] = mwInterval[irow] ;
1136 mwIndex_[wsidx] = irow ;
1137 wsidx++ ;
1138 }
1139 }
1140 }
1141 else {
1142 mwTime_ = mwTime ;
1143 mwInterval_ = mwInterval ;
1144 mwIndex_.resize( mwTime_.size() ) ;
1145 indgen( mwIndex_ ) ;
1146 }
1147 //os_ << "mwTime[0] = " << mwTime_[0] << " mwInterval[0] = " << mwInterval_[0] << LogIO::POST ;
1148 //double endSec = gettimeofday_sec() ;
1149 //os_ << "end MSFiller::fillWeather() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1150}
1151
1152void MSFiller::fillFocus()
1153{
1154 //double startSec = gettimeofday_sec() ;
1155 //os_ << "start MSFiller::fillFocus() startSec=" << startSec << LogIO::POST ;
1156 // tentative
1157 table_->focus().addEntry( 0.0, 0.0, 0.0, 0.0 ) ;
1158 //double endSec = gettimeofday_sec() ;
1159 //os_ << "end MSFiller::fillFocus() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1160}
1161
1162void MSFiller::fillTcal( boost::object_pool<ROTableColumn> *tpoolr )
1163{
1164 //double startSec = gettimeofday_sec() ;
1165 //os_ << "start MSFiller::fillTcal() startSec=" << startSec << LogIO::POST ;
1166
1167 if ( !isSysCal_ ) {
1168 // add dummy row
1169 os_ << "No SYSCAL rows" << LogIO::POST ;
1170 table_->tcal().table().addRow(1,True) ;
1171 Vector<Float> defaultTcal( 1, 1.0 ) ;
1172 ArrayColumn<Float> tcalCol( table_->tcal().table(), "TCAL" ) ;
1173 tcalCol.put( 0, defaultTcal ) ;
1174 return ;
1175 }
1176
1177 if ( colTcal_ == "NONE" ) {
1178 // add dummy row
1179 os_ << "No TCAL column" << LogIO::POST ;
1180 table_->tcal().table().addRow(1,True) ;
1181 Vector<Float> defaultTcal( 1, 1.0 ) ;
1182 ArrayColumn<Float> tcalCol( table_->tcal().table(), "TCAL" ) ;
1183 tcalCol.put( 0, defaultTcal ) ;
1184 return ;
1185 }
1186
1187 Table sctab = mstable_.sysCal() ;
1188 if ( sctab.nrow() == 0 ) {
1189 os_ << "No SYSCAL rows" << LogIO::POST ;
1190 return ;
1191 }
1192 Table sctabsel( sctab( sctab.col("ANTENNA_ID") == antenna_ ) ) ;
1193 if ( sctabsel.nrow() == 0 ) {
1194 os_ << "No SYSCAL rows" << LogIO::POST ;
1195 return ;
1196 }
1197 ROArrayColumn<Float> *tmpTcalCol = new ROArrayColumn<Float>( sctabsel, colTcal_ ) ;
1198 // return if any rows without Tcal value exists
1199 Bool notDefined = False ;
1200 for ( uInt irow = 0 ; irow < sctabsel.nrow() ; irow++ ) {
1201 if ( !tmpTcalCol->isDefined( irow ) ) {
1202 notDefined = True ;
1203 break ;
1204 }
1205 }
1206 if ( notDefined ) {
1207 os_ << "No TCAL value" << LogIO::POST ;
1208 delete tmpTcalCol ;
1209 table_->tcal().table().addRow(1,True) ;
1210 Vector<Float> defaultTcal( 1, 1.0 ) ;
1211 ArrayColumn<Float> tcalCol( table_->tcal().table(), "TCAL" ) ;
1212 tcalCol.put( 0, defaultTcal ) ;
1213 return ;
1214 }
1215 uInt npol = tmpTcalCol->shape( 0 )(0) ;
1216 delete tmpTcalCol ;
1217 //os_ << "fillTcal(): npol = " << npol << LogIO::POST ;
1218 Table tab = table_->tcal().table() ;
1219 ArrayColumn<Float> tcalCol( tab, "TCAL" ) ;
1220 uInt oldnr = 0 ;
1221 uInt newnr = 0 ;
1222 TableRow row( tab ) ;
1223 TableRecord &trec = row.record() ;
1224 RecordFieldPtr<uInt> idRF( trec, "ID" ) ;
1225 RecordFieldPtr<String> timeRF( trec, "TIME" ) ;
1226 RecordFieldPtr< Array<Float> > tcalRF( trec, "TCAL" ) ;
1227 TableIterator iter0( sctabsel, "FEED_ID" ) ;
1228 while( !iter0.pastEnd() ) {
1229 Table t0 = iter0.table() ;
1230 Int feedId = asInt( "FEED_ID", 0, t0, tpoolr ) ;
1231 TableIterator iter1( t0, "SPECTRAL_WINDOW_ID" ) ;
1232 while( !iter1.pastEnd() ) {
1233 Table t1 = iter1.table() ;
1234 Int spwId = asInt( "SPECTRAL_WINDOW_ID", 0, t1, tpoolr ) ;
1235 tmpTcalCol = new ROArrayColumn<Float>( t1, colTcal_ ) ;
1236 ROScalarQuantColumn<Double> scTimeCol( t1, "TIME" ) ;
1237 Vector<uInt> idminmax( 2, oldnr ) ;
1238 for ( uInt irow = 0 ; irow < t1.nrow() ; irow++ ) {
1239 String sTime = MVTime( scTimeCol(irow) ).string( MVTime::YMD ) ;
1240 *timeRF = sTime ;
1241 uInt idx = oldnr ;
1242 Matrix<Float> subtcal = (*tmpTcalCol)( irow ) ;
1243 for ( uInt ipol = 0 ; ipol < npol ; ipol++ ) {
1244 *idRF = idx++ ;
1245 //*tcalRF = subtcal.row( ipol ) ;
1246 tcalRF.define( subtcal.row( ipol ) ) ;
1247
1248 // commit row
1249 tab.addRow() ;
1250 row.put( tab.nrow()-1 ) ;
1251
1252 newnr++ ;
1253 }
1254 idminmax[0] = oldnr ;
1255 idminmax[1] = newnr - 1 ;
1256 oldnr = newnr ;
1257
1258 String key = keyTcal( feedId, spwId, sTime ) ;
1259 tcalrec_.define( key, idminmax ) ;
1260 }
1261 delete tmpTcalCol ;
1262 iter1++ ;
1263 }
1264 iter0++ ;
1265 }
1266
1267 //tcalrec_.print( std::cout ) ;
1268 //double endSec = gettimeofday_sec() ;
1269 //os_ << "end MSFiller::fillTcal() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1270}
1271
1272uInt MSFiller::getWeatherId( uInt idx, Double wtime )
1273{
1274 //double startSec = gettimeofday_sec() ;
1275 //os_ << "start MSFiller::getWeatherId() startSec=" << startSec << LogIO::POST ;
1276 uInt nrow = mwTime_.size() ;
1277 if ( nrow < 2 )
1278 return 0 ;
1279 uInt wid = nrow ;
1280 if ( idx == 0 ) {
1281 wid = 0 ;
1282 Double tStart = mwTime_[wid]-0.5*mwInterval_[wid] ;
1283 if ( wtime < tStart )
1284 return wid ;
1285 }
1286 for ( uInt i = idx ; i < nrow-1 ; i++ ) {
1287 Double tStart = mwTime_[i]-0.5*mwInterval_[i] ;
1288 // use of INTERVAL column is problematic
1289 // since there are "blank" time of weather monitoring
1290 //Double tEnd = tStart + mwInterval_[i] ;
1291 Double tEnd = mwTime_[i+1]-0.5*mwInterval_[i+1] ;
1292 //os_ << "tStart = " << tStart << " dtEnd = " << tEnd-tStart << " dwtime = " << wtime-tStart << LogIO::POST ;
1293 if ( wtime >= tStart && wtime <= tEnd ) {
1294 wid = i ;
1295 break ;
1296 }
1297 }
1298 if ( wid == nrow ) {
1299 uInt i = nrow - 1 ;
1300 Double tStart = mwTime_[i-1]+0.5*mwInterval_[i-1] ;
1301 Double tEnd = mwTime_[i]+0.5*mwInterval_[i] ;
1302 //os_ << "tStart = " << tStart << " dtEnd = " << tEnd-tStart << " dwtime = " << wtime-tStart << LogIO::POST ;
1303 if ( wtime >= tStart && wtime <= tEnd )
1304 wid = i-1 ;
1305 else
1306 wid = i ;
1307 }
1308
1309 //if ( wid == nrow )
1310 //os_ << LogIO::WARN << "Couldn't find correct WEATHER_ID for time " << wtime << LogIO::POST ;
1311
1312 //double endSec = gettimeofday_sec() ;
1313 //os_ << "end MSFiller::getWeatherId() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1314 return wid ;
1315}
1316
1317void MSFiller::getSysCalTime( Vector<MEpoch> &scTime, Vector<Double> &scInterval, Block<MEpoch> &tcol, Block<Int> &tidx )
1318{
1319 //double startSec = gettimeofday_sec() ;
1320 //os_ << "start MSFiller::getSysCalTime() startSec=" << startSec << LogIO::POST ;
1321
1322 if ( !isSysCal_ )
1323 return ;
1324
1325 uInt nrow = tidx.nelements() ;
1326 if ( scTime.nelements() == 0 )
1327 return ;
1328 else if ( scTime.nelements() == 1 ) {
1329 tidx[0] = 0 ;
1330 return ;
1331 }
1332 uInt scnrow = scTime.nelements() ;
1333 uInt idx = 0 ;
1334 const Double half = 0.5e0 ;
1335 // execute binary search
1336 idx = binarySearch( scTime, tcol[0].get( "s" ).getValue() ) ;
1337 if ( idx != 0 )
1338 idx -= 1 ;
1339 for ( uInt i = 0 ; i < nrow ; i++ ) {
1340 Double t = tcol[i].get( "s" ).getValue() ;
1341 Double tsc = scTime[0].get( "s" ).getValue() ;
1342 if ( t < tsc ) {
1343 tidx[i] = 0 ;
1344 continue ;
1345 }
1346 for ( uInt j = idx ; j < scnrow-1 ; j++ ) {
1347 Double tsc1 = scTime[j].get( "s" ).getValue() ;
1348 Double dt1 = scInterval[j] ;
1349 Double tsc2 = scTime[j+1].get( "s" ).getValue() ;
1350 Double dt2 = scInterval[j+1] ;
1351 if ( t > tsc1-half*dt1 && t <= tsc2-half*dt2 ) {
1352 tidx[i] = j ;
1353 idx = j ;
1354 break ;
1355 }
1356 }
1357 if ( tidx[i] == -1 ) {
1358// Double tsc = scTime[scnrow-1].get( "s" ).getValue() ;
1359// Double dt = scInterval[scnrow-1] ;
1360// if ( t <= tsc+0.5*dt ) {
1361// tidx[i] = scnrow-1 ;
1362// }
1363 tidx[i] = scnrow-1 ;
1364 }
1365 }
1366 //double endSec = gettimeofday_sec() ;
1367 //os_ << "end MSFiller::getSysCalTime() endSec=" << endSec << " (" << endSec-startSec << "sec) scnrow = " << scnrow << " tcol.nelements = " << tcol.nelements() << LogIO::POST ;
1368 return ;
1369}
1370
1371Block<uInt> MSFiller::getTcalId( Int fid, Int spwid, MEpoch &t )
1372{
1373 //double startSec = gettimeofday_sec() ;
1374 //os_ << "start MSFiller::getTcalId() startSec=" << startSec << LogIO::POST ;
1375 //if ( table_->tcal().table().nrow() == 0 ) {
1376 if ( !isSysCal_ ) {
1377 os_ << "No TCAL rows" << LogIO::POST ;
1378 Block<uInt> tcalids( 4, 0 ) ;
1379 return tcalids ;
1380 }
1381 //String sctime = MVTime( Quantum<Double>(t,"s") ).string(MVTime::YMD) ;
1382 String sctime = MVTime( t.getValue() ).string(MVTime::YMD) ;
1383 String key = keyTcal( fid, spwid, sctime ) ;
1384 if ( !tcalrec_.isDefined( key ) ) {
1385 os_ << "No TCAL rows" << LogIO::POST ;
1386 Block<uInt> tcalids( 4, 0 ) ;
1387 return tcalids ;
1388 }
1389 Vector<uInt> ids = tcalrec_.asArrayuInt( key ) ;
1390 uInt npol = ids[1] - ids[0] + 1 ;
1391 Block<uInt> tcalids( npol ) ;
1392 tcalids[0] = ids[0] ;
1393 tcalids[1] = ids[1] ;
1394 for ( uInt ipol = 2 ; ipol < npol ; ipol++ )
1395 tcalids[ipol] = ids[0] + ipol - 1 ;
1396
1397 //double endSec = gettimeofday_sec() ;
1398 //os_ << "end MSFiller::getTcalId() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1399 return tcalids ;
1400}
1401
1402uInt MSFiller::getDirection( uInt idx,
1403 Vector<Double> &dir,
1404 Vector<Double> &srate,
1405 String &ref,
1406 ROScalarColumn<Double> &tcol,
1407 ROArrayColumn<Double> &dcol,
1408 Double t )
1409{
1410 //double startSec = gettimeofday_sec() ;
1411 //os_ << "start MSFiller::getDirection1() startSec=" << startSec << LogIO::POST ;
1412 //double time0 = gettimeofday_sec() ;
1413 //os_ << "start getDirection 1st stage startSec=" << time0 << LogIO::POST ;
1414 // assume that cols is sorted by TIME
1415 Bool doInterp = False ;
1416 uInt nrow = tcol.nrow() ;
1417 if ( nrow == 0 )
1418 return 0 ;
1419 TableRecord trec = tcol.keywordSet() ;
1420 String tUnit = trec.asArrayString( "QuantumUnits" ).data()[0] ;
1421 Double factor = 1.0 ;
1422 if ( tUnit == "d" )
1423 factor = 86400.0 ;
1424 // binary search if idx == 0
1425 if ( idx == 0 ) {
1426 uInt nrowb = 1000 ;
1427 if ( nrow > nrowb ) {
1428 uInt nblock = nrow / nrowb + 1 ;
1429 for ( uInt iblock = 0 ; iblock < nblock ; iblock++ ) {
1430 uInt high = min( nrowb, nrow-iblock*nrowb ) ;
1431
1432 if ( tcol( high-1 ) * factor < t ) {
1433 idx = iblock * nrowb ;
1434 continue ;
1435 }
1436
1437 RefRows refrows( iblock*nrowb, iblock*nrowb+high, 1 ) ;
1438 Vector<Double> tarr = tcol.getColumnCells( refrows ) ;
1439 if ( tUnit == "d" )
1440 tarr *= factor ;
1441
1442 uInt bidx = binarySearch( tarr, t ) ;
1443
1444 idx = iblock * nrowb + bidx ;
1445 break ;
1446 }
1447 }
1448 else {
1449 Vector<Double> tarr = tcol.getColumn() ;
1450 if ( tUnit == "d" )
1451 tarr *= factor ;
1452 idx = binarySearch( tarr, t ) ;
1453 }
1454 }
1455 //double time1 = gettimeofday_sec() ;
1456 //os_ << "end getDirection 1st stage endSec=" << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
1457 // ensure that tcol(idx) < t
1458 //os_ << "tcol(idx) = " << tcol(idx).get("s").getValue() << " t = " << t << " diff = " << tcol(idx).get("s").getValue()-t << endl ;
1459 //time0 = gettimeofday_sec() ;
1460 //os_ << "start getDirection 2nd stage startSec=" << time0 << LogIO::POST ;
1461 while( tcol( idx ) * factor > t && idx > 0 )
1462 idx-- ;
1463 //os_ << "idx = " << idx << LogIO::POST ;
1464
1465 // index search
1466 for ( uInt i = idx ; i < nrow ; i++ ) {
1467 //Double tref = tcol( i ).get( "s" ).getValue() ;
1468 Double tref = tcol( i ) * factor ;
1469 if ( tref == t ) {
1470 idx = i ;
1471 break ;
1472 }
1473 else if ( tref > t ) {
1474 if ( i == 0 ) {
1475 idx = i ;
1476 }
1477 else {
1478 idx = i-1 ;
1479 doInterp = True ;
1480 }
1481 break ;
1482 }
1483 else {
1484 idx = nrow - 1 ;
1485 }
1486 }
1487 //time1 = gettimeofday_sec() ;
1488 //os_ << "end getDirection 2nd stage endSec=" << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
1489 //os_ << "searched idx = " << idx << LogIO::POST ;
1490
1491 //time0 = gettimeofday_sec() ;
1492 //os_ << "start getDirection 3rd stage startSec=" << time0 << LogIO::POST ;
1493 //os_ << "dmcol(idx).shape() = " << dmcol(idx).shape() << LogIO::POST ;
1494 //IPosition ip( dmcol(idx).shape().nelements(), 0 ) ;
1495 IPosition ip( dcol(idx).shape().nelements(), 0 ) ;
1496 //os_ << "ip = " << ip << LogIO::POST ;
1497 //ref = dmcol(idx)(ip).getRefString() ;
1498 trec = dcol.keywordSet() ;
1499 Record rec = trec.asRecord( "MEASINFO" ) ;
1500 ref = rec.asString( "Ref" ) ;
1501 //os_ << "ref = " << ref << LogIO::POST ;
1502 if ( doInterp ) {
1503 //os_ << "do interpolation" << LogIO::POST ;
1504 //os_ << "dcol(idx).shape() = " << dcol(idx).shape() << LogIO::POST ;
1505// Double tref0 = tcol(idx).get("s").getValue() ;
1506// Double tref1 = tcol(idx+1).get("s").getValue() ;
1507 Double tref0 = tcol(idx) * factor ;
1508 Double tref1 = tcol(idx+1) * factor ;
1509 Matrix<Double> mdir0 = dcol( idx ) ;
1510 Matrix<Double> mdir1 = dcol( idx+1 ) ;
1511 Vector<Double> dir0 = mdir0.column( 0 ) ;
1512 //os_ << "dir0 = " << dir0 << LogIO::POST ;
1513 Vector<Double> dir1 = mdir1.column( 0 ) ;
1514 //os_ << "dir1 = " << dir1 << LogIO::POST ;
1515 Double dt0 = t - tref0 ;
1516 Double dt1 = tref1 - t ;
1517 dir.reference( (dt0*dir1+dt1*dir0)/(dt0+dt1) ) ;
1518 if ( mdir0.ncolumn() > 1 ) {
1519 if ( dt0 >= dt1 )
1520 srate.reference( mdir0.column( 1 ) ) ;
1521 else
1522 srate.reference( mdir1.column( 1 ) ) ;
1523 }
1524 //os_ << "dir = " << dir << LogIO::POST ;
1525 }
1526 else {
1527 //os_ << "no interpolation" << LogIO::POST ;
1528 Matrix<Double> mdir0 = dcol( idx ) ;
1529 dir.reference( mdir0.column( 0 ) ) ;
1530 if ( mdir0.ncolumn() > 1 )
1531 srate.reference( mdir0.column( 1 ) ) ;
1532 }
1533
1534 //time1 = gettimeofday_sec() ;
1535 //os_ << "end getDirection 3rd stage endSec=" << time1 << " (" << time1-time0 << "sec)" << LogIO::POST ;
1536 //double endSec = gettimeofday_sec() ;
1537 //os_ << "end MSFiller::getDirection1() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1538 return idx ;
1539}
1540
1541String MSFiller::keyTcal( Int feedid, Int spwid, String stime )
1542{
1543 String sfeed = "FEED" + String::toString( feedid ) ;
1544 String sspw = "SPW" + String::toString( spwid ) ;
1545 return sfeed+":"+sspw+":"+stime ;
1546}
1547
1548uInt MSFiller::binarySearch( Vector<MEpoch> &timeList, Double target )
1549{
1550 Int low = 0 ;
1551 Int high = timeList.nelements() ;
1552 uInt idx = 0 ;
1553
1554 while ( low <= high ) {
1555 idx = (Int)( 0.5 * ( low + high ) ) ;
1556 Double t = timeList[idx].get( "s" ).getValue() ;
1557 if ( t < target )
1558 low = idx + 1 ;
1559 else if ( t > target )
1560 high = idx - 1 ;
1561 else {
1562 return idx ;
1563 }
1564 }
1565
1566 idx = max( 0, min( low, high ) ) ;
1567
1568 return idx ;
1569}
1570
1571uInt MSFiller::binarySearch( Vector<Double> &timeList, Double target )
1572{
1573 Int low = 0 ;
1574 Int high = timeList.nelements() ;
1575 uInt idx = 0 ;
1576
1577 while ( low <= high ) {
1578 idx = (Int)( 0.5 * ( low + high ) ) ;
1579 Double t = timeList[idx] ;
1580 if ( t < target )
1581 low = idx + 1 ;
1582 else if ( t > target )
1583 high = idx - 1 ;
1584 else {
1585 return idx ;
1586 }
1587 }
1588
1589 idx = max( 0, min( low, high ) ) ;
1590
1591 return idx ;
1592}
1593
1594string MSFiller::getFrame()
1595{
1596 MFrequency::Types frame = MFrequency::DEFAULT ;
1597 ROTableColumn numChanCol( mstable_.spectralWindow(), "NUM_CHAN" ) ;
1598 ROTableColumn measFreqRefCol( mstable_.spectralWindow(), "MEAS_FREQ_REF" ) ;
1599 uInt nrow = numChanCol.nrow() ;
1600 Vector<Int> measFreqRef( nrow, MFrequency::DEFAULT ) ;
1601 uInt nref = 0 ;
1602 for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
1603 if ( numChanCol.asInt( irow ) != 4 ) { // exclude WVR
1604 measFreqRef[nref] = measFreqRefCol.asInt( irow ) ;
1605 nref++ ;
1606 }
1607 }
1608 if ( nref > 0 )
1609 frame = (MFrequency::Types)measFreqRef[0] ;
1610
1611 return MFrequency::showType( frame ) ;
1612}
1613
1614void MSFiller::reshapeSpectraAndFlagtra( Cube<Float> &sp,
1615 Cube<Bool> &fl,
1616 Table &tab,
1617 Int &npol,
1618 Int &nchan,
1619 Int &nrow,
1620 Vector<Int> &corrtype )
1621{
1622 //double startSec = gettimeofday_sec() ;
1623 //os_ << "start MSFiller::reshapeSpectraAndFlagtra() startSec=" << startSec << LogIO::POST ;
1624 if ( isFloatData_ ) {
1625 ROArrayColumn<Bool> mFlagCol( tab, "FLAG" ) ;
1626 ROArrayColumn<Float> mFloatDataCol( tab, "FLOAT_DATA" ) ;
1627 mFloatDataCol.getColumn( sp ) ;
1628 mFlagCol.getColumn( fl ) ;
1629 }
1630 else if ( isData_ ) {
1631 sp.resize( npol, nchan, nrow ) ;
1632 fl.resize( npol, nchan, nrow ) ;
1633 ROArrayColumn<Bool> mFlagCol( tab, "FLAG" ) ;
1634 ROArrayColumn<Complex> mDataCol( tab, "DATA" ) ;
1635 if ( npol < 3 ) {
1636 Cube<Float> tmp = ComplexToReal( mDataCol.getColumn() ) ;
1637 IPosition start( 3, 0, 0, 0 ) ;
1638 IPosition end( 3, 2*npol-1, nchan-1, nrow-1 ) ;
1639 IPosition inc( 3, 2, 1, 1 ) ;
1640 sp = tmp( start, end, inc ) ;
1641 fl = mFlagCol.getColumn() ;
1642 }
1643 else {
1644 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1645 Bool crossOK = False ;
1646 Matrix<Complex> mSp = mDataCol( irow ) ;
1647 Matrix<Bool> mFl = mFlagCol( irow ) ;
1648 Matrix<Float> spxy = sp.xyPlane( irow ) ;
1649 Matrix<Bool> flxy = fl.xyPlane( irow ) ;
1650 for ( Int ipol = 0 ; ipol < npol ; ipol++ ) {
1651 if ( corrtype[ipol] == Stokes::XY || corrtype[ipol] == Stokes::YX
1652 || corrtype[ipol] == Stokes::RL || corrtype[ipol] == Stokes::LR ) {
1653 if ( !crossOK ) {
1654 Vector<Float> tmp = ComplexToReal( mSp.row( ipol ) ) ;
1655 IPosition start( 1, 0 ) ;
1656 IPosition end( 1, 2*nchan-1 ) ;
1657 IPosition inc( 1, 2 ) ;
1658 spxy.row( ipol ) = tmp( start, end, inc ) ;
1659 flxy.row( ipol ) = mFl.row( ipol ) ;
1660 start = IPosition( 1, 1 ) ;
1661 spxy.row( ipol+1 ) = tmp( start, end, inc ) ;
1662 flxy.row( ipol+1 ) = mFl.row( ipol ) ;
1663 if ( corrtype[ipol] == Stokes::YX || corrtype[ipol] == Stokes::LR ) {
1664 spxy.row( ipol+1 ) = spxy.row( ipol+1 ) * (Float)-1.0 ;
1665 }
1666 crossOK = True ;
1667 }
1668 }
1669 else {
1670 Vector<Float> tmp = ComplexToReal( mSp.row( ipol ) ) ;
1671 IPosition start( 1, 0 ) ;
1672 IPosition end( 1, 2*nchan-1 ) ;
1673 IPosition inc( 1, 2 ) ;
1674 spxy.row( ipol ) = tmp( start, end, inc ) ;
1675 flxy.row( ipol ) = mFl.row( ipol ) ;
1676 }
1677 }
1678 }
1679 }
1680 }
1681 //double endSec = gettimeofday_sec() ;
1682 //os_ << "end MSFiller::reshapeSpectraAndFlagtra() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1683}
1684
1685uInt MSFiller::getDirection( uInt idx,
1686 Vector<Double> &dir,
1687 Vector<Double> &azel,
1688 Vector<Double> &srate,
1689 ROScalarColumn<Double> &ptcol,
1690 ROArrayColumn<Double> &pdcol,
1691 MEpoch &t,
1692 MPosition &antpos )
1693{
1694 //double startSec = gettimeofday_sec() ;
1695 //os_ << "start MSFiller::getDirection2() startSec=" << startSec << LogIO::POST ;
1696 String refString ;
1697 MDirection::Types dirType ;
1698 uInt diridx = getDirection( idx, dir, srate, refString, ptcol, pdcol, t.get("s").getValue() ) ;
1699 MDirection::getType( dirType, refString ) ;
1700 MeasFrame mf( t, antpos ) ;
1701 if ( refString == "J2000" ) {
1702 MDirection::Convert toazel( dirType, MDirection::Ref( MDirection::AZEL, mf ) ) ;
1703 azel = toazel( dir ).getAngle("rad").getValue() ;
1704 }
1705 else if ( refString(0,4) == "AZEL" ) {
1706 azel = dir.copy() ;
1707 MDirection::Convert toj2000( dirType, MDirection::Ref( MDirection::J2000, mf ) ) ;
1708 dir = toj2000( dir ).getAngle("rad").getValue() ;
1709 }
1710 else {
1711 MDirection::Convert toazel( dirType, MDirection::Ref( MDirection::AZEL, mf ) ) ;
1712 azel = toazel( dir ).getAngle("rad").getValue() ;
1713 MDirection::Convert toj2000( dirType, MDirection::Ref( MDirection::J2000, mf ) ) ;
1714 dir = toj2000( dir ).getAngle("rad").getValue() ;
1715 }
1716 if ( srate.size() == 0 ) {
1717 srate.resize( 2 ) ;
1718 srate = 0.0 ;
1719 }
1720 //double endSec = gettimeofday_sec() ;
1721 //os_ << "end MSFiller::getDirection2() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1722 return diridx ;
1723}
1724
1725void MSFiller::getSourceDirection( Vector<Double> &dir,
1726 Vector<Double> &azel,
1727 Vector<Double> &srate,
1728 MEpoch &t,
1729 MPosition &antpos,
1730 Vector<MDirection> &srcdir )
1731{
1732 //double startSec = gettimeofday_sec() ;
1733 //os_ << "start MSFiller::getSourceDirection() startSec=" << startSec << LogIO::POST ;
1734 Vector<Double> defaultScanrate( 2, 0.0 ) ;
1735 Vector<Double> defaultDir = srcdir[0].getAngle( "rad" ).getValue() ;
1736 if ( srcdir.nelements() > 1 )
1737 defaultScanrate = srcdir[1].getAngle( "rad" ).getValue() ;
1738 String ref = srcdir[0].getRefString() ;
1739 MDirection::Types dirType ;
1740 MDirection::getType( dirType, ref ) ;
1741 MeasFrame mf( t, antpos ) ;
1742 if ( ref != "J2000" ) {
1743 MDirection::Convert toj2000( dirType, MDirection::Ref( MDirection::J2000, mf ) ) ;
1744 dir = toj2000( defaultDir ).getAngle("rad").getValue() ;
1745 }
1746 else
1747 dir = defaultDir ;
1748 if ( ref != "AZELGEO" ) {
1749 MDirection::Convert toazel( dirType, MDirection::Ref( MDirection::AZELGEO, mf ) ) ;
1750 azel = toazel( defaultDir ).getAngle("rad").getValue() ;
1751 }
1752 else
1753 azel = defaultDir ;
1754 srate = defaultScanrate ;
1755 //double endSec = gettimeofday_sec() ;
1756 //os_ << "end MSFiller::getSourceDirection() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1757}
1758
1759void MSFiller::initHeader( STHeader &header )
1760{
1761 header.nchan = 0 ;
1762 header.npol = 0 ;
1763 header.nif = 0 ;
1764 header.nbeam = 0 ;
1765 header.observer = "" ;
1766 header.project = "" ;
1767 header.obstype = "" ;
1768 header.antennaname = "" ;
1769 header.antennaposition.resize( 0 ) ;
1770 header.equinox = 0.0 ;
1771 header.freqref = "" ;
1772 header.reffreq = -1.0 ;
1773 header.bandwidth = 0.0 ;
1774 header.utc = 0.0 ;
1775 header.fluxunit = "" ;
1776 header.epoch = "" ;
1777 header.poltype = "" ;
1778}
1779
1780String MSFiller::asString( String name,
1781 uInt idx,
1782 Table tab,
1783 boost::object_pool<ROTableColumn> *pool )
1784{
1785 ROTableColumn *col = pool->construct( tab, name ) ;
1786 String v = col->asString( idx ) ;
1787 pool->destroy( col ) ;
1788 return v ;
1789}
1790
1791Bool MSFiller::asBool( String name,
1792 uInt idx,
1793 Table &tab,
1794 boost::object_pool<ROTableColumn> *pool )
1795{
1796 ROTableColumn *col = pool->construct( tab, name ) ;
1797 Bool v = col->asBool( idx ) ;
1798 pool->destroy( col ) ;
1799 return v ;
1800}
1801
1802uInt MSFiller::asuInt( String name,
1803 uInt idx,
1804 Table &tab,
1805 boost::object_pool<ROTableColumn> *pool )
1806{
1807 ROTableColumn *col = pool->construct( tab, name ) ;
1808 uInt v = col->asuInt( idx ) ;
1809 pool->destroy( col ) ;
1810 return v ;
1811}
1812
1813Int MSFiller::asInt( String name,
1814 uInt idx,
1815 Table &tab,
1816 boost::object_pool<ROTableColumn> *pool )
1817{
1818 ROTableColumn *col = pool->construct( tab, name ) ;
1819 Int v = col->asInt( idx ) ;
1820 pool->destroy( col ) ;
1821 return v ;
1822}
1823
1824Float MSFiller::asFloat( String name,
1825 uInt idx,
1826 Table &tab,
1827 boost::object_pool<ROTableColumn> *pool )
1828{
1829 ROTableColumn *col = pool->construct( tab, name ) ;
1830 Float v = col->asfloat( idx ) ;
1831 pool->destroy( col ) ;
1832 return v ;
1833}
1834
1835Double MSFiller::asDouble( String name,
1836 uInt idx,
1837 Table &tab,
1838 boost::object_pool<ROTableColumn> *pool )
1839{
1840 ROTableColumn *col = pool->construct( tab, name ) ;
1841 Double v = col->asdouble( idx ) ;
1842 pool->destroy( col ) ;
1843 return v ;
1844}
1845
1846void MSFiller::sourceInfo( Int sourceId,
1847 Int spwId,
1848 String &name,
1849 MDirection &direction,
1850 Vector<casa::Double> &properMotion,
1851 Vector<casa::Double> &restFreqs,
1852 Vector<casa::String> &transitions,
1853 Vector<casa::Double> &sysVels,
1854 boost::object_pool<ROTableColumn> *tpoolr )
1855{
1856 //double startSec = gettimeofday_sec() ;
1857 //os_ << "start MSFiller::sourceInfo() startSec=" << startSec << LogIO::POST ;
1858
1859 MSSource srctab = mstable_.source() ;
1860 MSSource srctabSel = srctab( srctab.col("SOURCE_ID") == sourceId && srctab.col("SPECTRAL_WINDOW_ID") == spwId ) ;
1861 if ( srctabSel.nrow() == 0 ) {
1862 srctabSel = srctab( srctab.col("SOURCE_ID") == sourceId && srctab.col("SPECTRAL_WINDOW_ID") == -1 ) ;
1863 }
1864 Int numLines = 0 ;
1865 if ( srctabSel.nrow() > 0 ) {
1866 // source name
1867 name = asString( "NAME", 0, srctabSel, tpoolr ) ;
1868
1869 // source proper motion
1870 ROArrayColumn<Double> roArrDCol( srctabSel, "PROPER_MOTION" ) ;
1871 properMotion = roArrDCol( 0 ) ;
1872
1873 // source direction as MDirection object
1874 ROScalarMeasColumn<MDirection> tmpMeasCol( srctabSel, "DIRECTION" ) ;
1875 direction = tmpMeasCol( 0 ) ;
1876
1877 // number of lines
1878 numLines = asInt( "NUM_LINES", 0, srctabSel, tpoolr ) ;
1879 }
1880 else {
1881 name = "" ;
1882 properMotion = Vector<Double>( 2, 0.0 ) ;
1883 direction = MDirection( Quantum<Double>(0.0,Unit("rad")), Quantum<Double>(0.0,Unit("rad")) ) ;
1884 }
1885
1886 restFreqs.resize( numLines ) ;
1887 transitions.resize( numLines ) ;
1888 sysVels.resize( numLines ) ;
1889 if ( numLines > 0 ) {
1890 if ( srctabSel.tableDesc().isColumn( "REST_FREQUENCY" ) ) {
1891 ROArrayQuantColumn<Double> quantArrCol( srctabSel, "REST_FREQUENCY" ) ;
1892 Array< Quantum<Double> > qRestFreqs = quantArrCol( 0 ) ;
1893 for ( int i = 0 ; i < numLines ; i++ ) {
1894 restFreqs[i] = qRestFreqs( IPosition( 1, i ) ).getValue( "Hz" ) ;
1895 }
1896 }
1897 //os_ << "restFreqs = " << restFreqs << LogIO::POST ;
1898 if ( srctabSel.tableDesc().isColumn( "TRANSITION" ) ) {
1899 ROArrayColumn<String> transitionCol( srctabSel, "TRANSITION" ) ;
1900 if ( transitionCol.isDefined( 0 ) )
1901 transitions = transitionCol( 0 ) ;
1902 //os_ << "transitionNameCol.nrow() = " << transitionCol.nrow() << LogIO::POST ;
1903 }
1904 if ( srctabSel.tableDesc().isColumn( "SYSVEL" ) ) {
1905 ROArrayColumn<Double> roArrDCol( srctabSel, "SYSVEL" ) ;
1906 sysVels = roArrDCol( 0 ) ;
1907 }
1908 }
1909
1910 //double endSec = gettimeofday_sec() ;
1911 //os_ << "end MSFiller::sourceInfo() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1912}
1913
1914void MSFiller::spectralSetup( Int spwId,
1915 MEpoch &me,
1916 MPosition &mp,
1917 MDirection &md,
1918 Double &refpix,
1919 Double &refval,
1920 Double &increment,
1921 Int &nchan,
1922 String &freqref,
1923 Double &reffreq,
1924 Double &bandwidth,
1925 boost::object_pool<ROTableColumn> *tpoolr )
1926{
1927 //double startSec = gettimeofday_sec() ;
1928 //os_ << "start MSFiller::spectralSetup() startSec=" << startSec << LogIO::POST ;
1929
1930 MSSpectralWindow spwtab = mstable_.spectralWindow() ;
1931 MeasFrame mf( me, mp, md ) ;
1932 MFrequency::Types freqRef = MFrequency::castType( (uInt)asInt( "MEAS_FREQ_REF", spwId, spwtab, tpoolr ) ) ;
1933 Bool even = False ;
1934 if ( (nchan/2)*2 == nchan ) even = True ;
1935 ROScalarQuantColumn<Double> tmpQuantCol( spwtab, "TOTAL_BANDWIDTH" ) ;
1936 Double totbw = tmpQuantCol( spwId ).getValue( "Hz" ) ;
1937 if ( nchan != 4 )
1938 bandwidth = max( bandwidth, totbw ) ;
1939 if ( freqref == "" && nchan != 4)
1940 //sdh.freqref = MFrequency::showType( freqRef ) ;
1941 freqref = "LSRK" ;
1942 if ( reffreq == -1.0 && nchan != 4 ) {
1943 tmpQuantCol.attach( spwtab, "REF_FREQUENCY" ) ;
1944 Quantum<Double> qreffreq = tmpQuantCol( spwId ) ;
1945 if ( freqRef == MFrequency::LSRK ) {
1946 reffreq = qreffreq.getValue("Hz") ;
1947 }
1948 else {
1949 MFrequency::Convert tolsr( freqRef, MFrequency::Ref( MFrequency::LSRK, mf ) ) ;
1950 reffreq = tolsr( qreffreq ).get("Hz").getValue() ;
1951 }
1952 }
1953 Int refchan = nchan / 2 ;
1954 IPosition refip( 1, refchan ) ;
1955 refpix = 0.5*(nchan-1) ;
1956 refval = 0.0 ;
1957 ROArrayQuantColumn<Double> sharedQDArrCol( spwtab, "CHAN_WIDTH" ) ;
1958 increment = sharedQDArrCol( spwId )( refip ).getValue( "Hz" ) ;
1959 // os_ << "nchan = " << nchan << " refchan = " << refchan << "(even=" << even << ") refpix = " << refpix << LogIO::POST ;
1960 sharedQDArrCol.attach( spwtab, "CHAN_FREQ" ) ;
1961 Vector< Quantum<Double> > chanFreqs = sharedQDArrCol( spwId ) ;
1962 if ( nchan > 1 && chanFreqs[0].getValue("Hz") > chanFreqs[1].getValue("Hz") )
1963 increment *= -1.0 ;
1964 if ( freqRef == MFrequency::LSRK ) {
1965 if ( even ) {
1966 IPosition refip0( 1, refchan-1 ) ;
1967 Double refval0 = chanFreqs(refip0).getValue("Hz") ;
1968 Double refval1 = chanFreqs(refip).getValue("Hz") ;
1969 refval = 0.5 * ( refval0 + refval1 ) ;
1970 }
1971 else {
1972 refval = chanFreqs(refip).getValue("Hz") ;
1973 }
1974 }
1975 else {
1976 MFrequency::Convert tolsr( freqRef, MFrequency::Ref( MFrequency::LSRK, mf ) ) ;
1977 if ( even ) {
1978 IPosition refip0( 1, refchan-1 ) ;
1979 Double refval0 = chanFreqs(refip0).getValue("Hz") ;
1980 Double refval1 = chanFreqs(refip).getValue("Hz") ;
1981 refval = 0.5 * ( refval0 + refval1 ) ;
1982 refval = tolsr( refval ).get("Hz").getValue() ;
1983 }
1984 else {
1985 refval = tolsr( chanFreqs(refip) ).get("Hz").getValue() ;
1986 }
1987 }
1988
1989 //double endSec = gettimeofday_sec() ;
1990 //os_ << "end MSFiller::spectralSetup() endSec=" << endSec << " (" << endSec-startSec << "sec)" << LogIO::POST ;
1991}
1992
1993} ;
1994
Note: See TracBrowser for help on using the repository browser.