1 | #include <iostream>
|
---|
2 | #include <fstream>
|
---|
3 |
|
---|
4 | #include <casa/BasicSL/String.h>
|
---|
5 | #include <casa/Arrays/Vector.h>
|
---|
6 | #include <casa/Arrays/Matrix.h>
|
---|
7 | #include <casa/Arrays/Cube.h>
|
---|
8 | #include <casa/Arrays/ArrayMath.h>
|
---|
9 | #include <casa/Arrays/ArrayIter.h>
|
---|
10 | #include <casa/Quanta/Quantum.h>
|
---|
11 | #include <casa/Quanta/QuantumHolder.h>
|
---|
12 | #include <casa/Utilities/CountedPtr.h>
|
---|
13 | #include <casa/Logging/LogIO.h>
|
---|
14 |
|
---|
15 | #include <tables/Tables/Table.h>
|
---|
16 | #include <tables/Tables/TableRecord.h>
|
---|
17 | #include <tables/Tables/ExprNode.h>
|
---|
18 | #include <tables/Tables/ScalarColumn.h>
|
---|
19 | #include <tables/Tables/ArrayColumn.h>
|
---|
20 | #include <tables/Tables/TableIter.h>
|
---|
21 |
|
---|
22 | #include <measures/Measures/MDirection.h>
|
---|
23 |
|
---|
24 | #include <MathUtils.h>
|
---|
25 |
|
---|
26 | #include "STGrid.h"
|
---|
27 |
|
---|
28 | using namespace std ;
|
---|
29 | using namespace casa ;
|
---|
30 | using namespace asap ;
|
---|
31 |
|
---|
32 | namespace asap {
|
---|
33 |
|
---|
34 | // for performance check
|
---|
35 | double eToInt = 0.0 ;
|
---|
36 | double eGetWeight = 0.0 ;
|
---|
37 |
|
---|
38 | // constructor
|
---|
39 | STGrid::STGrid()
|
---|
40 | {
|
---|
41 | init() ;
|
---|
42 | }
|
---|
43 |
|
---|
44 | STGrid::STGrid( const string infile )
|
---|
45 | {
|
---|
46 | init() ;
|
---|
47 |
|
---|
48 | setFileIn( infile ) ;
|
---|
49 | }
|
---|
50 |
|
---|
51 | void STGrid::init()
|
---|
52 | {
|
---|
53 | ifno_ = -1 ;
|
---|
54 | nx_ = -1 ;
|
---|
55 | ny_ = -1 ;
|
---|
56 | npol_ = 0 ;
|
---|
57 | nchan_ = 0 ;
|
---|
58 | nrow_ = 0 ;
|
---|
59 | cellx_ = 0.0 ;
|
---|
60 | celly_ = 0.0 ;
|
---|
61 | center_ = Vector<Double> ( 2, 0.0 ) ;
|
---|
62 | convType_ = "BOX" ;
|
---|
63 | wtype_ = "UNIFORM" ;
|
---|
64 | convSupport_ = -1 ;
|
---|
65 | userSupport_ = -1 ;
|
---|
66 | convSampling_ = 100 ;
|
---|
67 | nprocessed_ = 0 ;
|
---|
68 | nchunk_ = 0 ;
|
---|
69 |
|
---|
70 | // initialize user input
|
---|
71 | nxUI_ = -1 ;
|
---|
72 | nyUI_ = -1 ;
|
---|
73 | cellxUI_ = "" ;
|
---|
74 | cellyUI_ = "" ;
|
---|
75 | centerUI_ = "" ;
|
---|
76 | }
|
---|
77 |
|
---|
78 | void STGrid::setFileIn( const string infile )
|
---|
79 | {
|
---|
80 | String name( infile ) ;
|
---|
81 | if ( infile_.compare( name ) != 0 ) {
|
---|
82 | infile_ = String( infile ) ;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | void STGrid::setPolList( vector<unsigned int> pols )
|
---|
87 | {
|
---|
88 | pollist_.assign( Vector<uInt>( pols ) ) ;
|
---|
89 | }
|
---|
90 |
|
---|
91 | void STGrid::setScanList( vector<unsigned int> scans )
|
---|
92 | {
|
---|
93 | scanlist_.assign( Vector<uInt>( scans ) ) ;
|
---|
94 | }
|
---|
95 |
|
---|
96 | void STGrid::setWeight( const string wType )
|
---|
97 | {
|
---|
98 | wtype_ = String( wType ) ;
|
---|
99 | wtype_.upcase() ;
|
---|
100 | }
|
---|
101 |
|
---|
102 | void STGrid::defineImage( int nx,
|
---|
103 | int ny,
|
---|
104 | string scellx,
|
---|
105 | string scelly,
|
---|
106 | string scenter )
|
---|
107 | {
|
---|
108 | nxUI_ = (Int)nx ;
|
---|
109 | nyUI_ = (Int)ny ;
|
---|
110 | cellxUI_ = String( scellx ) ;
|
---|
111 | cellyUI_ = String( scelly ) ;
|
---|
112 | centerUI_ = String( scenter ) ;
|
---|
113 | }
|
---|
114 |
|
---|
115 | void STGrid::setFunc( string convType,
|
---|
116 | int convSupport )
|
---|
117 | {
|
---|
118 | convType_ = String( convType ) ;
|
---|
119 | convType_.upcase() ;
|
---|
120 | userSupport_ = (Int)convSupport ;
|
---|
121 | }
|
---|
122 |
|
---|
123 | #define NEED_UNDERSCORES
|
---|
124 | #if defined(NEED_UNDERSCORES)
|
---|
125 | #define ggridsd ggridsd_
|
---|
126 | #endif
|
---|
127 | extern "C" {
|
---|
128 | void ggridsd(Double*,
|
---|
129 | const Complex*,
|
---|
130 | Int*,
|
---|
131 | Int*,
|
---|
132 | Int*,
|
---|
133 | const Int*,
|
---|
134 | const Int*,
|
---|
135 | const Float*,
|
---|
136 | Int*,
|
---|
137 | Int*,
|
---|
138 | Complex*,
|
---|
139 | Float*,
|
---|
140 | Int*,
|
---|
141 | Int*,
|
---|
142 | Int *,
|
---|
143 | Int *,
|
---|
144 | Int*,
|
---|
145 | Int*,
|
---|
146 | Float*,
|
---|
147 | Int*,
|
---|
148 | Int*,
|
---|
149 | Double*);
|
---|
150 | }
|
---|
151 | void STGrid::call_ggridsd( Array<Double> &xypos,
|
---|
152 | Array<Complex> &spectra,
|
---|
153 | Int &nvispol,
|
---|
154 | Int &nvischan,
|
---|
155 | Array<Int> &flagtra,
|
---|
156 | Array<Int> &flagrow,
|
---|
157 | Array<Float> &weight,
|
---|
158 | Int &nrow,
|
---|
159 | Int &irow,
|
---|
160 | Array<Complex> &gdata,
|
---|
161 | Array<Float> &gwgt,
|
---|
162 | Int &nx,
|
---|
163 | Int &ny,
|
---|
164 | Int &npol,
|
---|
165 | Int &nchan,
|
---|
166 | Int &support,
|
---|
167 | Int &sampling,
|
---|
168 | Vector<Float> &convFunc,
|
---|
169 | Int *chanMap,
|
---|
170 | Int *polMap )
|
---|
171 | {
|
---|
172 | // parameters for gridding
|
---|
173 | Int idopsf = 0 ;
|
---|
174 | Int len = npol*nchan ;
|
---|
175 | Double *sumw_p = new Double[len] ;
|
---|
176 | {
|
---|
177 | Double *work_p = sumw_p ;
|
---|
178 | for ( Int i = 0 ; i < len ; i++ ) {
|
---|
179 | *work_p = 0.0 ;
|
---|
180 | work_p++ ;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | // prepare pointer
|
---|
185 | Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ;
|
---|
186 | Double *xy_p = xypos.getStorage( deletePos ) ;
|
---|
187 | const Complex *values_p = spectra.getStorage( deleteData ) ;
|
---|
188 | const Int *flag_p = flagtra.getStorage( deleteFlag ) ;
|
---|
189 | const Int *rflag_p = flagrow.getStorage( deleteFlagR ) ;
|
---|
190 | const Float *wgt_p = weight.getStorage( deleteWgt ) ;
|
---|
191 | Complex *grid_p = gdata.getStorage( deleteDataG ) ;
|
---|
192 | Float *wgrid_p = gwgt.getStorage( deleteWgtG ) ;
|
---|
193 | Float *conv_p = convFunc.getStorage( deleteConv ) ;
|
---|
194 |
|
---|
195 | // pass copy of irow to ggridsd since it will be modified in theroutine
|
---|
196 | Int irowCopy = irow ;
|
---|
197 |
|
---|
198 | // call ggridsd
|
---|
199 | ggridsd( xy_p,
|
---|
200 | values_p,
|
---|
201 | &nvispol,
|
---|
202 | &nvischan,
|
---|
203 | &idopsf,
|
---|
204 | flag_p,
|
---|
205 | rflag_p,
|
---|
206 | wgt_p,
|
---|
207 | &nrow,
|
---|
208 | &irowCopy,
|
---|
209 | grid_p,
|
---|
210 | wgrid_p,
|
---|
211 | &nx,
|
---|
212 | &ny,
|
---|
213 | &npol,
|
---|
214 | &nchan,
|
---|
215 | &support,
|
---|
216 | &sampling,
|
---|
217 | conv_p,
|
---|
218 | chanMap,
|
---|
219 | polMap,
|
---|
220 | sumw_p ) ;
|
---|
221 |
|
---|
222 | // finalization
|
---|
223 | xypos.putStorage( xy_p, deletePos ) ;
|
---|
224 | spectra.freeStorage( values_p, deleteData ) ;
|
---|
225 | flagtra.freeStorage( flag_p, deleteFlag ) ;
|
---|
226 | flagrow.freeStorage( rflag_p, deleteFlagR ) ;
|
---|
227 | weight.freeStorage( wgt_p, deleteWgt ) ;
|
---|
228 | gdata.putStorage( grid_p, deleteDataG ) ;
|
---|
229 | gwgt.putStorage( wgrid_p, deleteWgtG ) ;
|
---|
230 | convFunc.putStorage( conv_p, deleteConv ) ;
|
---|
231 | delete sumw_p ;
|
---|
232 | }
|
---|
233 |
|
---|
234 | Bool STGrid::pastEnd()
|
---|
235 | {
|
---|
236 | LogIO os( LogOrigin("STGrid","pastEnd",WHERE) ) ;
|
---|
237 | Bool b = nprocessed_ >= nrow_ ;
|
---|
238 | return b ;
|
---|
239 | }
|
---|
240 |
|
---|
241 | void STGrid::grid()
|
---|
242 | {
|
---|
243 | LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
|
---|
244 | double t0,t1 ;
|
---|
245 |
|
---|
246 | // data selection
|
---|
247 | t0 = mathutil::gettimeofday_sec() ;
|
---|
248 | selectData() ;
|
---|
249 | t1 = mathutil::gettimeofday_sec() ;
|
---|
250 | os << "selectData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
|
---|
251 |
|
---|
252 | setupGrid() ;
|
---|
253 | setupArray() ;
|
---|
254 |
|
---|
255 | if ( wtype_.compare("UNIFORM") != 0 &&
|
---|
256 | wtype_.compare("TINT") != 0 &&
|
---|
257 | wtype_.compare("TSYS") != 0 &&
|
---|
258 | wtype_.compare("TINTSYS") != 0 ) {
|
---|
259 | LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
|
---|
260 | os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
|
---|
261 | wtype_ = "UNIFORM" ;
|
---|
262 | }
|
---|
263 |
|
---|
264 | // grid parameter
|
---|
265 | os << LogIO::DEBUGGING ;
|
---|
266 | os << "----------" << endl ;
|
---|
267 | os << "Data selection summary" << endl ;
|
---|
268 | os << " ifno = " << ifno_ << endl ;
|
---|
269 | os << " pollist = " << pollist_ << endl ;
|
---|
270 | os << " scanlist = " << scanlist_ << endl ;
|
---|
271 | os << "----------" << endl ;
|
---|
272 | os << "Grid parameter summary" << endl ;
|
---|
273 | os << " (nx,ny) = (" << nx_ << "," << ny_ << ")" << endl ;
|
---|
274 | os << " (cellx,celly) = (" << cellx_ << "," << celly_ << ")" << endl ;
|
---|
275 | os << " center = " << center_ << endl ;
|
---|
276 | os << " weighting = " << wtype_ << endl ;
|
---|
277 | os << " convfunc = " << convType_ << " with support " << convSupport_ << endl ;
|
---|
278 | os << "----------" << LogIO::POST ;
|
---|
279 | os << LogIO::NORMAL ;
|
---|
280 |
|
---|
281 | Bool doAll = examine() ;
|
---|
282 |
|
---|
283 | if ( doAll )
|
---|
284 | gridPerPol() ;
|
---|
285 | else
|
---|
286 | gridPerRow() ;
|
---|
287 | }
|
---|
288 |
|
---|
289 | Bool STGrid::examine()
|
---|
290 | {
|
---|
291 | // TODO: nchunk_ must be determined from nchan_, npol_, and (nx_,ny_)
|
---|
292 | // by considering data size to be allocated for ggridsd input/output
|
---|
293 | nchunk_ = 400 ;
|
---|
294 | Bool b = nchunk_ >= nrow_ ;
|
---|
295 | nchunk_ = min( nchunk_, nrow_ ) ;
|
---|
296 | return b ;
|
---|
297 | }
|
---|
298 |
|
---|
299 | void STGrid::gridPerRow()
|
---|
300 | {
|
---|
301 | LogIO os( LogOrigin("STGrid", "gridPerRow", WHERE) ) ;
|
---|
302 | double t0, t1 ;
|
---|
303 |
|
---|
304 | // convolution kernel
|
---|
305 | Vector<Float> convFunc ;
|
---|
306 | t0 = mathutil::gettimeofday_sec() ;
|
---|
307 | setConvFunc( convFunc ) ;
|
---|
308 | t1 = mathutil::gettimeofday_sec() ;
|
---|
309 | os << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
|
---|
310 |
|
---|
311 | // grid data
|
---|
312 | Int gnx = nx_ ;
|
---|
313 | Int gny = ny_ ;
|
---|
314 | // Extend grid plane with convSupport_
|
---|
315 | // Int gnx = nx_+convSupport_*2 ;
|
---|
316 | // Int gny = ny_+convSupport_*2 ;
|
---|
317 | IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
|
---|
318 | Array<Complex> gdataArrC( gshape, 0.0 ) ;
|
---|
319 | // 2011/12/20 TN
|
---|
320 | // data_ and gwgtArr share storage
|
---|
321 | data_.resize( gshape ) ;
|
---|
322 | data_ = 0.0 ;
|
---|
323 | Array<Float> gwgtArr( data_ ) ;
|
---|
324 |
|
---|
325 | // parameters for gridding
|
---|
326 | Int *chanMap = new Int[nchan_] ;
|
---|
327 | {
|
---|
328 | Int *work_p = chanMap ;
|
---|
329 | for ( Int i = 0 ; i < nchan_ ; i++ ) {
|
---|
330 | *work_p = i ;
|
---|
331 | work_p++ ;
|
---|
332 | }
|
---|
333 | }
|
---|
334 | Int *polMap = new Int[1] ;
|
---|
335 | Int nvispol = 1 ;
|
---|
336 | Int irow = -1 ;
|
---|
337 |
|
---|
338 | // for performance check
|
---|
339 | double eGetData = 0.0 ;
|
---|
340 | double eToPixel = 0.0 ;
|
---|
341 | double eGGridSD = 0.0 ;
|
---|
342 | double eInitPol = 0.0 ;
|
---|
343 |
|
---|
344 | for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
|
---|
345 | t0 = mathutil::gettimeofday_sec() ;
|
---|
346 | initPol( ipol ) ;
|
---|
347 | t1 = mathutil::gettimeofday_sec() ;
|
---|
348 | eInitPol += t1-t0 ;
|
---|
349 |
|
---|
350 | polMap[0] = ipol ;
|
---|
351 |
|
---|
352 | os << "start pol " << ipol << LogIO::POST ;
|
---|
353 |
|
---|
354 | while( !pastEnd() ) {
|
---|
355 | // data storage
|
---|
356 | IPosition cshape( 3, npol_, nchan_, nchunk_ ) ;
|
---|
357 | IPosition mshape( 2, npol_, nchunk_ ) ;
|
---|
358 | IPosition vshape( 1, nchunk_ ) ;
|
---|
359 | IPosition dshape( 2, 2, nchunk_ ) ;
|
---|
360 | IPosition wshape( 2, nchan_, nchunk_ ) ;
|
---|
361 | Array<Complex> spectra( wshape ) ;
|
---|
362 | Array<Int> flagtra( wshape ) ;
|
---|
363 | Array<Int> rflag( vshape ) ;
|
---|
364 | Array<Float> weight( wshape ) ;
|
---|
365 | Array<Double> direction( dshape ) ;
|
---|
366 | Array<Double> xypos( dshape ) ;
|
---|
367 |
|
---|
368 | spectraF_.resize( wshape ) ;
|
---|
369 | flagtraUC_.resize( wshape ) ;
|
---|
370 | rflagUI_.resize( vshape ) ;
|
---|
371 |
|
---|
372 | // retrieve data
|
---|
373 | t0 = mathutil::gettimeofday_sec() ;
|
---|
374 | Int nrow = getDataChunk( spectra, direction, flagtra, rflag, weight ) ;
|
---|
375 | t1 = mathutil::gettimeofday_sec() ;
|
---|
376 | eGetData += t1-t0 ;
|
---|
377 |
|
---|
378 | // world -> pixel
|
---|
379 | t0 = mathutil::gettimeofday_sec() ;
|
---|
380 | toPixel( direction, xypos ) ;
|
---|
381 | t1 = mathutil::gettimeofday_sec() ;
|
---|
382 | eToPixel += t1-t0 ;
|
---|
383 |
|
---|
384 | // call ggridsd
|
---|
385 | t0 = mathutil::gettimeofday_sec() ;
|
---|
386 | call_ggridsd( xypos,
|
---|
387 | spectra,
|
---|
388 | nvispol,
|
---|
389 | nchan_,
|
---|
390 | flagtra,
|
---|
391 | rflag,
|
---|
392 | weight,
|
---|
393 | nrow,
|
---|
394 | irow,
|
---|
395 | gdataArrC,
|
---|
396 | gwgtArr,
|
---|
397 | gnx,
|
---|
398 | gny,
|
---|
399 | npol_,
|
---|
400 | nchan_,
|
---|
401 | convSupport_,
|
---|
402 | convSampling_,
|
---|
403 | convFunc,
|
---|
404 | chanMap,
|
---|
405 | polMap ) ;
|
---|
406 | t1 = mathutil::gettimeofday_sec() ;
|
---|
407 | eGGridSD += t1-t0 ;
|
---|
408 |
|
---|
409 | }
|
---|
410 |
|
---|
411 | os << "end pol " << ipol << LogIO::POST ;
|
---|
412 |
|
---|
413 | nprocessed_ = 0 ;
|
---|
414 | }
|
---|
415 | os << "initPol: elapsed time is " << eInitPol << " sec." << LogIO::POST ;
|
---|
416 | os << "getData: elapsed time is " << eGetData-eToInt-eGetWeight << " sec." << LogIO::POST ;
|
---|
417 | os << "toPixel: elapsed time is " << eToPixel << " sec." << LogIO::POST ;
|
---|
418 | os << "ggridsd: elapsed time is " << eGGridSD << " sec." << LogIO::POST ;
|
---|
419 | os << "toInt: elapsed time is " << eToInt << " sec." << LogIO::POST ;
|
---|
420 | os << "getWeight: elapsed time is " << eGetWeight << " sec." << LogIO::POST ;
|
---|
421 |
|
---|
422 | delete polMap ;
|
---|
423 | delete chanMap ;
|
---|
424 |
|
---|
425 | // set data
|
---|
426 | setData( gdataArrC, gwgtArr ) ;
|
---|
427 |
|
---|
428 | }
|
---|
429 |
|
---|
430 | void STGrid::gridPerPol()
|
---|
431 | {
|
---|
432 | LogIO os( LogOrigin("STGrid", "gridPerPol", WHERE) ) ;
|
---|
433 | double t0, t1 ;
|
---|
434 |
|
---|
435 | // convolution kernel
|
---|
436 | Vector<Float> convFunc ;
|
---|
437 | t0 = mathutil::gettimeofday_sec() ;
|
---|
438 | setConvFunc( convFunc ) ;
|
---|
439 | t1 = mathutil::gettimeofday_sec() ;
|
---|
440 | os << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
|
---|
441 |
|
---|
442 | // prepare grid data storage
|
---|
443 | Int gnx = nx_ ;
|
---|
444 | Int gny = ny_ ;
|
---|
445 | // // Extend grid plane with convSupport_
|
---|
446 | // Int gnx = nx_+convSupport_*2 ;
|
---|
447 | // Int gny = ny_+convSupport_*2 ;
|
---|
448 | IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
|
---|
449 | Array<Complex> gdataArrC( gshape, 0.0 ) ;
|
---|
450 | //Array<Float> gwgtArr( gshape, 0.0 ) ;
|
---|
451 | // 2011/12/20 TN
|
---|
452 | // data_ and weight array shares storage
|
---|
453 | data_.resize( gshape ) ;
|
---|
454 | data_ = 0.0 ;
|
---|
455 | Array<Float> gwgtArr( data_ ) ;
|
---|
456 |
|
---|
457 | // to read data from the table
|
---|
458 | IPosition mshape( 2, nchan_, nrow_ ) ;
|
---|
459 | IPosition dshape( 2, 2, nrow_ ) ;
|
---|
460 | Array<Complex> spectra( mshape ) ;
|
---|
461 | Array<Double> direction( dshape ) ;
|
---|
462 | Array<Int> flagtra( mshape ) ;
|
---|
463 | Array<Int> rflag( IPosition(1,nrow_) ) ;
|
---|
464 | Array<Float> weight( mshape ) ;
|
---|
465 | Array<Double> xypos( dshape ) ;
|
---|
466 |
|
---|
467 | // maps
|
---|
468 | Int *chanMap = new Int[nchan_] ;
|
---|
469 | {
|
---|
470 | Int *work_p = chanMap ;
|
---|
471 | for ( Int i = 0 ; i < nchan_ ; i++ ) {
|
---|
472 | *work_p = i ;
|
---|
473 | work_p++ ;
|
---|
474 | }
|
---|
475 | }
|
---|
476 | Int *polMap = new Int[1] ;
|
---|
477 |
|
---|
478 | // some parameters for ggridsd
|
---|
479 | Int nvispol = 1 ;
|
---|
480 | Int irow = -1 ;
|
---|
481 |
|
---|
482 | // for performance check
|
---|
483 | double eInitPol = 0.0 ;
|
---|
484 | double eGetData = 0.0 ;
|
---|
485 | double eToPixel = 0.0 ;
|
---|
486 | double eGGridSD = 0.0 ;
|
---|
487 | double eToInt = 0.0 ;
|
---|
488 |
|
---|
489 | for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
|
---|
490 | t0 = mathutil::gettimeofday_sec() ;
|
---|
491 | initPol( ipol ) ;
|
---|
492 | t1 = mathutil::gettimeofday_sec() ;
|
---|
493 | eInitPol += t1-t0 ;
|
---|
494 |
|
---|
495 | // retrieve data
|
---|
496 | t0 = mathutil::gettimeofday_sec() ;
|
---|
497 | getDataPerPol( spectra, direction, flagtra, rflag, weight ) ;
|
---|
498 | t1 = mathutil::gettimeofday_sec() ;
|
---|
499 | eGetData += t1-t0 ;
|
---|
500 |
|
---|
501 | // world -> pixel
|
---|
502 | t0 = mathutil::gettimeofday_sec() ;
|
---|
503 | toPixel( direction, xypos ) ;
|
---|
504 | t1 = mathutil::gettimeofday_sec() ;
|
---|
505 | eToPixel += t1-t0 ;
|
---|
506 |
|
---|
507 | // call ggridsd
|
---|
508 | polMap[0] = ipol ;
|
---|
509 | t0 = mathutil::gettimeofday_sec() ;
|
---|
510 | call_ggridsd( xypos,
|
---|
511 | spectra,
|
---|
512 | nvispol,
|
---|
513 | nchan_,
|
---|
514 | flagtra,
|
---|
515 | rflag,
|
---|
516 | weight,
|
---|
517 | nrow_,
|
---|
518 | irow,
|
---|
519 | gdataArrC,
|
---|
520 | gwgtArr,
|
---|
521 | gnx,
|
---|
522 | gny,
|
---|
523 | npol_,
|
---|
524 | nchan_,
|
---|
525 | convSupport_,
|
---|
526 | convSampling_,
|
---|
527 | convFunc,
|
---|
528 | chanMap,
|
---|
529 | polMap ) ;
|
---|
530 | t1 = mathutil::gettimeofday_sec() ;
|
---|
531 | eGGridSD += t1-t0 ;
|
---|
532 | }
|
---|
533 | os << "initPol: elapsed time is " << eInitPol << " sec." << LogIO::POST ;
|
---|
534 | os << "getData: elapsed time is " << eGetData-eToInt-eGetWeight << " sec." << LogIO::POST ;
|
---|
535 | os << "toPixel: elapsed time is " << eToPixel << " sec." << LogIO::POST ;
|
---|
536 | os << "ggridsd: elapsed time is " << eGGridSD << " sec." << LogIO::POST ;
|
---|
537 | os << "toInt: elapsed time is " << eToInt << " sec." << LogIO::POST ;
|
---|
538 | os << "getWeight: elapsed time is " << eGetWeight << " sec." << LogIO::POST ;
|
---|
539 |
|
---|
540 | // delete maps
|
---|
541 | delete polMap ;
|
---|
542 | delete chanMap ;
|
---|
543 |
|
---|
544 | setData( gdataArrC, gwgtArr ) ;
|
---|
545 | // os << "gdataArr = " << gdataArr << LogIO::POST ;
|
---|
546 | // os << "gwgtArr = " << gwgtArr << LogIO::POST ;
|
---|
547 | // os << "data_ " << data_ << LogIO::POST ;
|
---|
548 | }
|
---|
549 |
|
---|
550 | void STGrid::initPol( Int ipol )
|
---|
551 | {
|
---|
552 | LogIO os( LogOrigin("STGrid","initPol",WHERE) ) ;
|
---|
553 | if ( npolOrg_ == 1 ) {
|
---|
554 | os << "single polarization data." << LogIO::POST ;
|
---|
555 | ptab_ = tab_ ;
|
---|
556 | }
|
---|
557 | else
|
---|
558 | ptab_ = tab_( tab_.col("POLNO") == (uInt)ipol ) ;
|
---|
559 |
|
---|
560 | attach( ptab_ ) ;
|
---|
561 | }
|
---|
562 |
|
---|
563 | void STGrid::setData( Array<Complex> &gdata,
|
---|
564 | Array<Float> &gwgt )
|
---|
565 | {
|
---|
566 | // 2011/12/20 TN
|
---|
567 | // gwgt and data_ share storage
|
---|
568 | LogIO os( LogOrigin("STGrid","setData",WHERE) ) ;
|
---|
569 | double t0, t1 ;
|
---|
570 | t0 = mathutil::gettimeofday_sec() ;
|
---|
571 | uInt len = data_.nelements() ;
|
---|
572 | const Complex *w1_p ;
|
---|
573 | Float *w2_p ;
|
---|
574 | Bool b1, b2 ;
|
---|
575 | const Complex *gdata_p = gdata.getStorage( b1 ) ;
|
---|
576 | Float *gwgt_p = data_.getStorage( b2 ) ;
|
---|
577 | w1_p = gdata_p ;
|
---|
578 | w2_p = gwgt_p ;
|
---|
579 | for ( uInt i = 0 ; i < len ; i++ ) {
|
---|
580 | if ( *w2_p > 0.0 ) *w2_p = (*w1_p).real() / *w2_p ;
|
---|
581 | w1_p++ ;
|
---|
582 | w2_p++ ;
|
---|
583 | }
|
---|
584 | gdata.freeStorage( gdata_p, b1 ) ;
|
---|
585 | data_.putStorage( gwgt_p, b2 ) ;
|
---|
586 | t1 = mathutil::gettimeofday_sec() ;
|
---|
587 | os << "setData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
|
---|
588 | }
|
---|
589 |
|
---|
590 | void STGrid::setupGrid()
|
---|
591 | {
|
---|
592 | Double xmin,xmax,ymin,ymax ;
|
---|
593 | mapExtent( xmin, xmax, ymin, ymax ) ;
|
---|
594 |
|
---|
595 | setupGrid( nxUI_, nyUI_, cellxUI_, cellyUI_,
|
---|
596 | xmin, xmax, ymin, ymax, centerUI_ ) ;
|
---|
597 | }
|
---|
598 |
|
---|
599 | void STGrid::setupGrid( Int &nx,
|
---|
600 | Int &ny,
|
---|
601 | String &cellx,
|
---|
602 | String &celly,
|
---|
603 | Double &xmin,
|
---|
604 | Double &xmax,
|
---|
605 | Double &ymin,
|
---|
606 | Double &ymax,
|
---|
607 | String ¢er )
|
---|
608 | {
|
---|
609 | LogIO os( LogOrigin("STGrid","setupGrid",WHERE) ) ;
|
---|
610 | //cout << "nx=" << nx << ", ny=" << ny << endl ;
|
---|
611 |
|
---|
612 | // center position
|
---|
613 | if ( center.size() == 0 ) {
|
---|
614 | center_(0) = 0.5 * ( xmin + xmax ) ;
|
---|
615 | center_(1) = 0.5 * ( ymin + ymax ) ;
|
---|
616 | }
|
---|
617 | else {
|
---|
618 | String::size_type pos0 = center.find( " " ) ;
|
---|
619 | if ( pos0 == String::npos ) {
|
---|
620 | throw AipsError( "bad string format in parameter center" ) ;
|
---|
621 | }
|
---|
622 | String::size_type pos1 = center.find( " ", pos0+1 ) ;
|
---|
623 | String typestr, xstr, ystr ;
|
---|
624 | if ( pos1 != String::npos ) {
|
---|
625 | typestr = center.substr( 0, pos0 ) ;
|
---|
626 | xstr = center.substr( pos0+1, pos1-pos0 ) ;
|
---|
627 | ystr = center.substr( pos1+1 ) ;
|
---|
628 | // todo: convert to J2000 (or direction ref for DIRECTION column)
|
---|
629 | }
|
---|
630 | else {
|
---|
631 | typestr = "J2000" ;
|
---|
632 | xstr = center.substr( 0, pos0 ) ;
|
---|
633 | ystr = center.substr( pos0+1 ) ;
|
---|
634 | }
|
---|
635 | QuantumHolder qh ;
|
---|
636 | String err ;
|
---|
637 | qh.fromString( err, xstr ) ;
|
---|
638 | Quantum<Double> xcen = qh.asQuantumDouble() ;
|
---|
639 | qh.fromString( err, ystr ) ;
|
---|
640 | Quantum<Double> ycen = qh.asQuantumDouble() ;
|
---|
641 | center_(0) = xcen.getValue( "rad" ) ;
|
---|
642 | center_(1) = ycen.getValue( "rad" ) ;
|
---|
643 | }
|
---|
644 |
|
---|
645 |
|
---|
646 | nx_ = nx ;
|
---|
647 | ny_ = ny ;
|
---|
648 | if ( nx < 0 && ny > 0 ) {
|
---|
649 | nx_ = ny ;
|
---|
650 | ny_ = ny ;
|
---|
651 | }
|
---|
652 | if ( ny < 0 && nx > 0 ) {
|
---|
653 | nx_ = nx ;
|
---|
654 | ny_ = nx ;
|
---|
655 | }
|
---|
656 |
|
---|
657 | //Double wx = xmax - xmin ;
|
---|
658 | //Double wy = ymax - ymin ;
|
---|
659 | Double wx = max( abs(xmax-center_(0)), abs(xmin-center_(0)) ) * 2 ;
|
---|
660 | Double wy = max( abs(ymax-center_(1)), abs(ymin-center_(1)) ) * 2 ;
|
---|
661 | // take 10% margin
|
---|
662 | wx *= 1.10 ;
|
---|
663 | wy *= 1.10 ;
|
---|
664 |
|
---|
665 | Quantum<Double> qcellx ;
|
---|
666 | Quantum<Double> qcelly ;
|
---|
667 | //cout << "nx_ = " << nx_ << ", ny_ = " << ny_ << endl ;
|
---|
668 | if ( cellx.size() != 0 && celly.size() != 0 ) {
|
---|
669 | readQuantity( qcellx, cellx ) ;
|
---|
670 | readQuantity( qcelly, celly ) ;
|
---|
671 | }
|
---|
672 | else if ( celly.size() != 0 ) {
|
---|
673 | os << "Using celly to x-axis..." << LogIO::POST ;
|
---|
674 | readQuantity( qcelly, celly ) ;
|
---|
675 | qcellx = qcelly ;
|
---|
676 | }
|
---|
677 | else if ( cellx.size() != 0 ) {
|
---|
678 | os << "Using cellx to y-axis..." << LogIO::POST ;
|
---|
679 | readQuantity( qcellx, cellx ) ;
|
---|
680 | qcelly = qcellx ;
|
---|
681 | }
|
---|
682 | else {
|
---|
683 | if ( nx_ < 0 ) {
|
---|
684 | os << "No user preference in grid setting. Using default..." << LogIO::POST ;
|
---|
685 | readQuantity( qcellx, "1.0arcmin" ) ;
|
---|
686 | qcelly = qcellx ;
|
---|
687 | }
|
---|
688 | else {
|
---|
689 | if ( wx == 0.0 ) {
|
---|
690 | os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ;
|
---|
691 | wx = 0.00290888 ;
|
---|
692 | }
|
---|
693 | if ( wy == 0.0 ) {
|
---|
694 | os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ;
|
---|
695 | wy = 0.00290888 ;
|
---|
696 | }
|
---|
697 | qcellx = Quantum<Double>( wx/nx_, "rad" ) ;
|
---|
698 | qcelly = Quantum<Double>( wy/ny_, "rad" ) ;
|
---|
699 | }
|
---|
700 | }
|
---|
701 | cellx_ = qcellx.getValue( "rad" ) ;
|
---|
702 | celly_ = qcelly.getValue( "rad" ) ;
|
---|
703 | if ( nx_ < 0 ) {
|
---|
704 | if ( wx == 0.0 ) {
|
---|
705 | os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ;
|
---|
706 | wx = 0.00290888 ;
|
---|
707 | }
|
---|
708 | if ( wy == 0.0 ) {
|
---|
709 | os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ;
|
---|
710 | wy = 0.00290888 ;
|
---|
711 | }
|
---|
712 | nx_ = Int( ceil( wx/cellx_ ) ) ;
|
---|
713 | ny_ = Int( ceil( wy/celly_ ) ) ;
|
---|
714 | }
|
---|
715 | }
|
---|
716 |
|
---|
717 | void STGrid::mapExtent( Double &xmin, Double &xmax,
|
---|
718 | Double &ymin, Double &ymax )
|
---|
719 | {
|
---|
720 | //LogIO os( LogOrigin("STGrid","mapExtent",WHERE) ) ;
|
---|
721 | ROArrayColumn<Double> dirCol( tab_, "DIRECTION" ) ;
|
---|
722 | Matrix<Double> direction = dirCol.getColumn() ;
|
---|
723 | //os << "dirCol.nrow() = " << dirCol.nrow() << LogIO::POST ;
|
---|
724 | minMax( xmin, xmax, direction.row( 0 ) ) ;
|
---|
725 | minMax( ymin, ymax, direction.row( 1 ) ) ;
|
---|
726 | //os << "(xmin,xmax)=(" << xmin << "," << xmax << ")" << LogIO::POST ;
|
---|
727 | //os << "(ymin,ymax)=(" << ymin << "," << ymax << ")" << LogIO::POST ;
|
---|
728 | }
|
---|
729 |
|
---|
730 | void STGrid::selectData()
|
---|
731 | {
|
---|
732 | LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
|
---|
733 | Int ifno = ifno_ ;
|
---|
734 | Table taborg( infile_ ) ;
|
---|
735 | if ( ifno == -1 ) {
|
---|
736 | ROScalarColumn<uInt> ifnoCol( taborg, "IFNO" ) ;
|
---|
737 | ifno = ifnoCol( 0 ) ;
|
---|
738 | os << LogIO::WARN
|
---|
739 | << "IFNO is not given. Using default IFNO: " << ifno << LogIO::POST ;
|
---|
740 | }
|
---|
741 | TableExprNode node ;
|
---|
742 | if ( isMultiIF( taborg ) ) {
|
---|
743 | os << "apply selection on IFNO" << LogIO::POST ;
|
---|
744 | node = taborg.col("IFNO") == ifno ;
|
---|
745 | }
|
---|
746 | if ( scanlist_.size() > 0 ) {
|
---|
747 | os << "apply selection on SCANNO" << LogIO::POST ;
|
---|
748 | node = node && taborg.col("SCANNO").in( scanlist_ ) ;
|
---|
749 | }
|
---|
750 | if ( node.isNull() ) {
|
---|
751 | tab_ = taborg ;
|
---|
752 | }
|
---|
753 | else {
|
---|
754 | tab_ = taborg( node ) ;
|
---|
755 | }
|
---|
756 | if ( tab_.nrow() == 0 ) {
|
---|
757 | os << LogIO::SEVERE
|
---|
758 | << "No corresponding rows for given selection: IFNO " << ifno ;
|
---|
759 | if ( scanlist_.size() > 0 )
|
---|
760 | os << " SCANNO " << scanlist_ ;
|
---|
761 | os << LogIO::EXCEPTION ;
|
---|
762 | }
|
---|
763 | }
|
---|
764 |
|
---|
765 | Bool STGrid::isMultiIF( Table &tab )
|
---|
766 | {
|
---|
767 | ROScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
|
---|
768 | Vector<uInt> ifnos = ifnoCol.getColumn() ;
|
---|
769 | return anyNE( ifnos, ifnos[0] ) ;
|
---|
770 | }
|
---|
771 |
|
---|
772 | void STGrid::attach( Table &tab )
|
---|
773 | {
|
---|
774 | // attach to table
|
---|
775 | spectraCol_.attach( tab, "SPECTRA" ) ;
|
---|
776 | flagtraCol_.attach( tab, "FLAGTRA" ) ;
|
---|
777 | directionCol_.attach( tab, "DIRECTION" ) ;
|
---|
778 | flagRowCol_.attach( tab, "FLAGROW" ) ;
|
---|
779 | tsysCol_.attach( tab, "TSYS" ) ;
|
---|
780 | intervalCol_.attach( tab, "INTERVAL" ) ;
|
---|
781 | // Vector<String> colnames( 6 ) ;
|
---|
782 | // colnames[0] = "SPECTRA" ;
|
---|
783 | // colnames[1] = "FLAGTRA" ;
|
---|
784 | // colnames[2] = "DIRECTION" ;
|
---|
785 | // colnames[3] = "FLAGROW" ;
|
---|
786 | // colnames[4] = "TSYS" ;
|
---|
787 | // colnames[5] = "INTERVAL" ;
|
---|
788 | // row_ = ROTableRow( tab_, colnames ) ;
|
---|
789 | // const TableRecord &rec = row_.record() ;
|
---|
790 | // spectraRF_.attachToRecord( rec, colnames[0] ) ;
|
---|
791 | // flagtraRF_.attachToRecord( rec, colnames[1] ) ;
|
---|
792 | // directionRF_.attachToRecord( rec, colnames[2] ) ;
|
---|
793 | // flagRowRF_.attachToRecord( rec, colnames[3] ) ;
|
---|
794 | // tsysRF_.attachToRecord( rec, colnames[4] ) ;
|
---|
795 | // intervalRF_.attachToRecord( rec, colnames[5] ) ;
|
---|
796 | }
|
---|
797 |
|
---|
798 | void STGrid::getDataPerPol( Array<Float> &spectra,
|
---|
799 | Array<Double> &direction,
|
---|
800 | Array<uChar> &flagtra,
|
---|
801 | Array<uInt> &rflag,
|
---|
802 | Array<Float> &weight )
|
---|
803 | {
|
---|
804 | LogIO os( LogOrigin("STGrid","getDataPerPol",WHERE) ) ;
|
---|
805 |
|
---|
806 | // 2011/12/22 TN
|
---|
807 | // weight and tsys shares its storage
|
---|
808 | Array<Float> tsys( weight ) ;
|
---|
809 | Array<Double> tint( rflag.shape() ) ;
|
---|
810 |
|
---|
811 | Vector<uInt> rflagVec( rflag ) ;
|
---|
812 | Vector<Double> tintVec( tint ) ;
|
---|
813 |
|
---|
814 | spectraCol_.getColumn( spectra ) ;
|
---|
815 | flagtraCol_.getColumn( flagtra ) ;
|
---|
816 | flagRowCol_.getColumn( rflagVec ) ;
|
---|
817 | directionCol_.getColumn( direction ) ;
|
---|
818 | intervalCol_.getColumn( tintVec ) ;
|
---|
819 | Vector<Float> tsysTemp = tsysCol_( 0 ) ;
|
---|
820 | if ( tsysTemp.nelements() == (uInt)nchan_ ) {
|
---|
821 | tsysCol_.getColumn( tsys ) ;
|
---|
822 | }
|
---|
823 | else {
|
---|
824 | tsys = tsysTemp[0] ;
|
---|
825 | }
|
---|
826 |
|
---|
827 | double t0,t1 ;
|
---|
828 | t0 = mathutil::gettimeofday_sec() ;
|
---|
829 | getWeight( weight, tsys, tint ) ;
|
---|
830 | t1 = mathutil::gettimeofday_sec() ;
|
---|
831 | eGetWeight += t1-t0 ;
|
---|
832 | }
|
---|
833 |
|
---|
834 | void STGrid::getDataPerPol( Array<Complex> &spectra,
|
---|
835 | Array<Double> &direction,
|
---|
836 | Array<Int> &flagtra,
|
---|
837 | Array<Int> &rflag,
|
---|
838 | Array<Float> &weight )
|
---|
839 | {
|
---|
840 | LogIO os( LogOrigin("STGrid","getDataPerPol",WHERE) ) ;
|
---|
841 | double t0, t1 ;
|
---|
842 |
|
---|
843 | Array<uChar> flagUC( flagtra.shape() ) ;
|
---|
844 | Array<uInt> rflagUI( rflag.shape() ) ;
|
---|
845 | Array<Float> spectraF( spectra.shape() ) ;
|
---|
846 | getDataPerPol( spectraF, direction, flagUC, rflagUI, weight ) ;
|
---|
847 |
|
---|
848 | convertArray( spectra, spectraF ) ;
|
---|
849 | t0 = mathutil::gettimeofday_sec() ;
|
---|
850 | toInt( flagUC, flagtra ) ;
|
---|
851 | toInt( rflagUI, rflag ) ;
|
---|
852 | t1 = mathutil::gettimeofday_sec() ;
|
---|
853 | eToInt += t1-t0 ;
|
---|
854 | //os << "toInt: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
|
---|
855 | }
|
---|
856 |
|
---|
857 | Int STGrid::getDataChunk( Array<Complex> &spectra,
|
---|
858 | Array<Double> &direction,
|
---|
859 | Array<Int> &flagtra,
|
---|
860 | Array<Int> &rflag,
|
---|
861 | Array<Float> &weight )
|
---|
862 | {
|
---|
863 | LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
|
---|
864 | Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
|
---|
865 | if ( nrow < nchunk_ ) {
|
---|
866 | spectra.resize( spectraF_.shape() ) ;
|
---|
867 | flagtra.resize( flagtraUC_.shape() ) ;
|
---|
868 | rflag.resize( rflagUI_.shape() ) ;
|
---|
869 | }
|
---|
870 | double t0, t1 ;
|
---|
871 | t0 = mathutil::gettimeofday_sec() ;
|
---|
872 | convertArray( spectra, spectraF_ ) ;
|
---|
873 | toInt( flagtraUC_, flagtra ) ;
|
---|
874 | toInt( rflagUI_, rflag ) ;
|
---|
875 | t1 = mathutil::gettimeofday_sec() ;
|
---|
876 | eToInt = t1 - t0 ;
|
---|
877 |
|
---|
878 | return nrow ;
|
---|
879 | }
|
---|
880 |
|
---|
881 | Int STGrid::getDataChunk( Array<Float> &spectra,
|
---|
882 | Array<Double> &direction,
|
---|
883 | Array<uChar> &flagtra,
|
---|
884 | Array<uInt> &rflag,
|
---|
885 | Array<Float> &weight )
|
---|
886 | {
|
---|
887 | LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
|
---|
888 | Int nrow = spectra.shape()[1] ;
|
---|
889 | Int remainingRow = nrow_ - nprocessed_ ;
|
---|
890 | if ( remainingRow < nrow ) {
|
---|
891 | nrow = remainingRow ;
|
---|
892 | IPosition mshape( 2, nchan_, nrow ) ;
|
---|
893 | IPosition vshape( 1, nrow ) ;
|
---|
894 | spectra.resize( mshape ) ;
|
---|
895 | flagtra.resize( mshape ) ;
|
---|
896 | direction.resize( IPosition(2,2,nrow) ) ;
|
---|
897 | rflag.resize( vshape ) ;
|
---|
898 | weight.resize( mshape ) ;
|
---|
899 | }
|
---|
900 | // 2011/12/22 TN
|
---|
901 | // tsys shares its storage with weight
|
---|
902 | Array<Float> tsys( weight ) ;
|
---|
903 | Array<Double> tint( rflag.shape() ) ;
|
---|
904 |
|
---|
905 | Vector<uInt> rflagVec( rflag ) ;
|
---|
906 | Vector<Double> tintVec( tint ) ;
|
---|
907 |
|
---|
908 | RefRows rows( nprocessed_, nprocessed_+nrow-1, 1 ) ;
|
---|
909 | //os<<LogIO::DEBUGGING<<"nprocessed_="<<nprocessed_<<": rows.nrows()="<<rows.nrows()<<LogIO::POST ;
|
---|
910 | spectraCol_.getColumnCells( rows, spectra ) ;
|
---|
911 | flagtraCol_.getColumnCells( rows, flagtra ) ;
|
---|
912 | directionCol_.getColumnCells( rows, direction ) ;
|
---|
913 | flagRowCol_.getColumnCells( rows, rflagVec ) ;
|
---|
914 | intervalCol_.getColumnCells( rows, tintVec ) ;
|
---|
915 | Vector<Float> tsysTemp = tsysCol_( nprocessed_ ) ;
|
---|
916 | if ( tsysTemp.nelements() == (uInt)nchan_ )
|
---|
917 | tsysCol_.getColumnCells( rows, tsys ) ;
|
---|
918 | else
|
---|
919 | tsys = tsysTemp[0] ;
|
---|
920 |
|
---|
921 | double t0,t1 ;
|
---|
922 | t0 = mathutil::gettimeofday_sec() ;
|
---|
923 | getWeight( weight, tsys, tint ) ;
|
---|
924 | t1 = mathutil::gettimeofday_sec() ;
|
---|
925 | eGetWeight += t1-t0 ;
|
---|
926 |
|
---|
927 | nprocessed_ += nrow ;
|
---|
928 |
|
---|
929 | return nrow ;
|
---|
930 | }
|
---|
931 |
|
---|
932 | void STGrid::setupArray()
|
---|
933 | {
|
---|
934 | LogIO os( LogOrigin("STGrid","setupArray",WHERE) ) ;
|
---|
935 | ROScalarColumn<uInt> polnoCol( tab_, "POLNO" ) ;
|
---|
936 | Vector<uInt> pols = polnoCol.getColumn() ;
|
---|
937 | //os << pols << LogIO::POST ;
|
---|
938 | Vector<uInt> pollistOrg ;
|
---|
939 | npolOrg_ = 0 ;
|
---|
940 | uInt polno ;
|
---|
941 | for ( uInt i = 0 ; i < polnoCol.nrow() ; i++ ) {
|
---|
942 | //polno = polnoCol( i ) ;
|
---|
943 | polno = pols( i ) ;
|
---|
944 | if ( allNE( pollistOrg, polno ) ) {
|
---|
945 | pollistOrg.resize( npolOrg_+1, True ) ;
|
---|
946 | pollistOrg[npolOrg_] = polno ;
|
---|
947 | npolOrg_++ ;
|
---|
948 | }
|
---|
949 | }
|
---|
950 | if ( pollist_.size() == 0 )
|
---|
951 | pollist_ = pollistOrg ;
|
---|
952 | else {
|
---|
953 | Vector<uInt> newlist ;
|
---|
954 | uInt newsize = 0 ;
|
---|
955 | for ( uInt i = 0 ; i < pollist_.size() ; i++ ) {
|
---|
956 | if ( anyEQ( pollistOrg, pollist_[i] ) ) {
|
---|
957 | newlist.resize( newsize+1, True ) ;
|
---|
958 | newlist[newsize] = pollist_[i] ;
|
---|
959 | newsize++ ;
|
---|
960 | }
|
---|
961 | }
|
---|
962 | pollist_.assign( newlist ) ;
|
---|
963 | }
|
---|
964 | npol_ = pollist_.size() ;
|
---|
965 | if ( npol_ == 0 ) {
|
---|
966 | os << LogIO::SEVERE << "Empty pollist" << LogIO::EXCEPTION ;
|
---|
967 | }
|
---|
968 | nrow_ = tab_.nrow() / npolOrg_ ;
|
---|
969 | ROArrayColumn<uChar> tmpCol( tab_, "FLAGTRA" ) ;
|
---|
970 | nchan_ = tmpCol( 0 ).nelements() ;
|
---|
971 | // os << "npol_ = " << npol_ << "(" << pollist_ << ")" << endl
|
---|
972 | // << "nchan_ = " << nchan_ << endl
|
---|
973 | // << "nrow_ = " << nrow_ << LogIO::POST ;
|
---|
974 | }
|
---|
975 |
|
---|
976 | void STGrid::getWeight( Array<Float> &w,
|
---|
977 | Array<Float> &tsys,
|
---|
978 | Array<Double> &tint )
|
---|
979 | {
|
---|
980 | LogIO os( LogOrigin("STGrid","getWeight",WHERE) ) ;
|
---|
981 |
|
---|
982 | // 2011/12/22 TN
|
---|
983 | // w (weight) and tsys share storage
|
---|
984 | IPosition refShape = tsys.shape() ;
|
---|
985 | Int nchan = refShape[0] ;
|
---|
986 | Int nrow = refShape[1] ;
|
---|
987 | // os << "nchan=" << nchan << ", nrow=" << nrow << LogIO::POST ;
|
---|
988 | // os << "w.shape()=" << w.shape() << endl
|
---|
989 | // << "tsys.shape()=" << tsys.shape() << endl
|
---|
990 | // << "tint.shape()=" << tint.shape() << LogIO::POST ;
|
---|
991 |
|
---|
992 | // set weight
|
---|
993 | if ( wtype_.compare( "UNIFORM" ) == 0 ) {
|
---|
994 | w = 1.0 ;
|
---|
995 | }
|
---|
996 | else if ( wtype_.compare( "TINT" ) == 0 ) {
|
---|
997 | Bool b0, b1 ;
|
---|
998 | Float *w_p = w.getStorage( b0 ) ;
|
---|
999 | Float *w0_p = w_p ;
|
---|
1000 | const Double *ti_p = tint.getStorage( b1 ) ;
|
---|
1001 | const Double *w1_p = ti_p ;
|
---|
1002 | for ( Int irow = 0 ; irow < nrow ; irow++ ) {
|
---|
1003 | for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
|
---|
1004 | *w0_p = *w1_p ;
|
---|
1005 | w0_p++ ;
|
---|
1006 | }
|
---|
1007 | w1_p++ ;
|
---|
1008 | }
|
---|
1009 | w.putStorage( w_p, b0 ) ;
|
---|
1010 | tint.freeStorage( ti_p, b1 ) ;
|
---|
1011 | }
|
---|
1012 | else if ( wtype_.compare( "TSYS" ) == 0 ) {
|
---|
1013 | Bool b0 ;
|
---|
1014 | Float *w_p = w.getStorage( b0 ) ;
|
---|
1015 | Float *w0_p = w_p ;
|
---|
1016 | for ( Int irow = 0 ; irow < nrow ; irow++ ) {
|
---|
1017 | for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
|
---|
1018 | Float temp = *w0_p ;
|
---|
1019 | *w0_p = 1.0 / ( temp * temp ) ;
|
---|
1020 | w0_p++ ;
|
---|
1021 | }
|
---|
1022 | }
|
---|
1023 | w.putStorage( w_p, b0 ) ;
|
---|
1024 | }
|
---|
1025 | else if ( wtype_.compare( "TINTSYS" ) == 0 ) {
|
---|
1026 | Bool b0, b1 ;
|
---|
1027 | Float *w_p = w.getStorage( b0 ) ;
|
---|
1028 | Float *w0_p = w_p ;
|
---|
1029 | const Double *ti_p = tint.getStorage( b1 ) ;
|
---|
1030 | const Double *w1_p = ti_p ;
|
---|
1031 | for ( Int irow = 0 ; irow < nrow ; irow++ ) {
|
---|
1032 | Float interval = *w1_p ;
|
---|
1033 | for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
|
---|
1034 | Float temp = *w0_p ;
|
---|
1035 | *w0_p = interval / ( temp * temp ) ;
|
---|
1036 | w0_p++ ;
|
---|
1037 | }
|
---|
1038 | w1_p++ ;
|
---|
1039 | }
|
---|
1040 | w.putStorage( w_p, b0 ) ;
|
---|
1041 | tint.freeStorage( ti_p, b1 ) ;
|
---|
1042 | }
|
---|
1043 | else {
|
---|
1044 | //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
|
---|
1045 | //os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
|
---|
1046 | w = 1.0 ;
|
---|
1047 | }
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | void STGrid::toInt( Array<uChar> &u, Array<Int> &v )
|
---|
1051 | {
|
---|
1052 | uInt len = u.nelements() ;
|
---|
1053 | Int *int_p = new Int[len] ;
|
---|
1054 | Bool deleteIt ;
|
---|
1055 | const uChar *data_p = u.getStorage( deleteIt ) ;
|
---|
1056 | Int *i_p = int_p ;
|
---|
1057 | const uChar *u_p = data_p ;
|
---|
1058 | for ( uInt i = 0 ; i < len ; i++ ) {
|
---|
1059 | *i_p = ( *u_p == 0 ) ? 0 : 1 ;
|
---|
1060 | i_p++ ;
|
---|
1061 | u_p++ ;
|
---|
1062 | }
|
---|
1063 | u.freeStorage( data_p, deleteIt ) ;
|
---|
1064 | v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | void STGrid::toInt( Array<uInt> &u, Array<Int> &v )
|
---|
1068 | {
|
---|
1069 | uInt len = u.nelements() ;
|
---|
1070 | Int *int_p = new Int[len] ;
|
---|
1071 | Bool deleteIt ;
|
---|
1072 | const uInt *data_p = u.getStorage( deleteIt ) ;
|
---|
1073 | Int *i_p = int_p ;
|
---|
1074 | const uInt *u_p = data_p ;
|
---|
1075 | for ( uInt i = 0 ; i < len ; i++ ) {
|
---|
1076 | *i_p = ( *u_p == 0 ) ? 0 : 1 ;
|
---|
1077 | i_p++ ;
|
---|
1078 | u_p++ ;
|
---|
1079 | }
|
---|
1080 | u.freeStorage( data_p, deleteIt ) ;
|
---|
1081 | v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | void STGrid::toPixel( Array<Double> &world, Array<Double> &pixel )
|
---|
1085 | {
|
---|
1086 | // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_)
|
---|
1087 | // grid plane to avoid unexpected behavior on grid edge
|
---|
1088 | Block<Double> pixc( 2 ) ;
|
---|
1089 | pixc[0] = Double( nx_-1 ) * 0.5 ;
|
---|
1090 | pixc[1] = Double( ny_-1 ) * 0.5 ;
|
---|
1091 | // pixc[0] = Double( nx_+2*convSupport_-1 ) * 0.5 ;
|
---|
1092 | // pixc[1] = Double( ny_+2*convSupport_-1 ) * 0.5 ;
|
---|
1093 | uInt nrow = world.shape()[1] ;
|
---|
1094 | Bool bw, bp ;
|
---|
1095 | const Double *w_p = world.getStorage( bw ) ;
|
---|
1096 | Double *p_p = pixel.getStorage( bp ) ;
|
---|
1097 | const Double *ww_p = w_p ;
|
---|
1098 | Double *wp_p = p_p ;
|
---|
1099 | for ( uInt i = 0 ; i < nrow ; i++ ) {
|
---|
1100 | *wp_p = pixc[0] + ( *ww_p - center_[0] ) / cellx_ ;
|
---|
1101 | wp_p++ ;
|
---|
1102 | ww_p++ ;
|
---|
1103 | *wp_p = pixc[1] + ( *ww_p - center_[1] ) / celly_ ;
|
---|
1104 | wp_p++ ;
|
---|
1105 | ww_p++ ;
|
---|
1106 | }
|
---|
1107 | world.freeStorage( w_p, bw ) ;
|
---|
1108 | pixel.putStorage( p_p, bp ) ;
|
---|
1109 | // String gridfile = "grid."+convType_+"."+String::toString(convSupport_)+".dat" ;
|
---|
1110 | // ofstream ofs( gridfile.c_str(), ios::out ) ;
|
---|
1111 | // ofs << "center " << center_(0) << " " << pixc(0)
|
---|
1112 | // << " " << center_(1) << " " << pixc(1) << endl ;
|
---|
1113 | // for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
|
---|
1114 | // ofs << irow ;
|
---|
1115 | // for ( uInt i = 0 ; i < 2 ; i++ ) {
|
---|
1116 | // ofs << " " << world(i, irow) << " " << pixel(i, irow) ;
|
---|
1117 | // }
|
---|
1118 | // ofs << endl ;
|
---|
1119 | // }
|
---|
1120 | // ofs.close() ;
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize )
|
---|
1124 | {
|
---|
1125 | convFunc = 0.0 ;
|
---|
1126 | for ( Int i = 0 ; i < convSize/2 ; i++ )
|
---|
1127 | convFunc(i) = 1.0 ;
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | #define NEED_UNDERSCORES
|
---|
1131 | #if defined(NEED_UNDERSCORES)
|
---|
1132 | #define grdsf grdsf_
|
---|
1133 | #endif
|
---|
1134 | extern "C" {
|
---|
1135 | void grdsf(Double*, Double*);
|
---|
1136 | }
|
---|
1137 | void STGrid::spheroidalFunc( Vector<Float> &convFunc )
|
---|
1138 | {
|
---|
1139 | convFunc = 0.0 ;
|
---|
1140 | for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
|
---|
1141 | Double nu = Double(i) / Double(convSupport_*convSampling_) ;
|
---|
1142 | Double val ;
|
---|
1143 | grdsf( &nu, &val ) ;
|
---|
1144 | convFunc(i) = ( 1.0 - nu * nu ) * val ;
|
---|
1145 | }
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | void STGrid::gaussFunc( Vector<Float> &convFunc )
|
---|
1149 | {
|
---|
1150 | convFunc = 0.0 ;
|
---|
1151 | // HWHM of the Gaussian is convSupport_ / 4
|
---|
1152 | // To take into account Gaussian tail, kernel cutoff is set to 4 * HWHM
|
---|
1153 | Int len = convSampling_ * convSupport_ ;
|
---|
1154 | Double hwhm = len * 0.25 ;
|
---|
1155 | for ( Int i = 0 ; i < len ; i++ ) {
|
---|
1156 | Double val = Double(i) / hwhm ;
|
---|
1157 | convFunc(i) = exp( -log(2)*val*val ) ;
|
---|
1158 | }
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | void STGrid::pbFunc( Vector<Float> &convFunc )
|
---|
1162 | {
|
---|
1163 | convFunc = 0.0 ;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | void STGrid::setConvFunc( Vector<Float> &convFunc )
|
---|
1167 | {
|
---|
1168 | convSupport_ = userSupport_ ;
|
---|
1169 | if ( convType_ == "BOX" ) {
|
---|
1170 | if ( convSupport_ < 0 )
|
---|
1171 | convSupport_ = 0 ;
|
---|
1172 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
1173 | convFunc.resize( convSize ) ;
|
---|
1174 | boxFunc( convFunc, convSize ) ;
|
---|
1175 | }
|
---|
1176 | else if ( convType_ == "SF" ) {
|
---|
1177 | if ( convSupport_ < 0 )
|
---|
1178 | convSupport_ = 3 ;
|
---|
1179 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
1180 | convFunc.resize( convSize ) ;
|
---|
1181 | spheroidalFunc( convFunc ) ;
|
---|
1182 | }
|
---|
1183 | else if ( convType_ == "GAUSS" ) {
|
---|
1184 | // to take into account Gaussian tail
|
---|
1185 | if ( convSupport_ < 0 )
|
---|
1186 | convSupport_ = 12 ; // 3 * 4
|
---|
1187 | else {
|
---|
1188 | convSupport_ = userSupport_ * 4 ;
|
---|
1189 | }
|
---|
1190 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
1191 | convFunc.resize( convSize ) ;
|
---|
1192 | gaussFunc( convFunc ) ;
|
---|
1193 | }
|
---|
1194 | else if ( convType_ == "PB" ) {
|
---|
1195 | if ( convSupport_ < 0 )
|
---|
1196 | convSupport_ = 0 ;
|
---|
1197 | pbFunc( convFunc ) ;
|
---|
1198 | }
|
---|
1199 | else {
|
---|
1200 | throw AipsError( "Unsupported convolution function" ) ;
|
---|
1201 | }
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | string STGrid::saveData( string outfile )
|
---|
1205 | {
|
---|
1206 | LogIO os( LogOrigin("STGrid", "saveData", WHERE) ) ;
|
---|
1207 | double t0, t1 ;
|
---|
1208 | t0 = mathutil::gettimeofday_sec() ;
|
---|
1209 |
|
---|
1210 | //Int polno = 0 ;
|
---|
1211 | String outfile_ ;
|
---|
1212 | if ( outfile.size() == 0 ) {
|
---|
1213 | if ( infile_.lastchar() == '/' ) {
|
---|
1214 | outfile_ = infile_.substr( 0, infile_.size()-1 ) ;
|
---|
1215 | }
|
---|
1216 | else {
|
---|
1217 | outfile_ = infile_ ;
|
---|
1218 | }
|
---|
1219 | outfile_ += ".grid" ;
|
---|
1220 | }
|
---|
1221 | else {
|
---|
1222 | outfile_ = outfile ;
|
---|
1223 | }
|
---|
1224 | Table tab ;
|
---|
1225 | prepareTable( tab, outfile_ ) ;
|
---|
1226 | IPosition dshape = data_.shape() ;
|
---|
1227 | Int nrow = nx_ * ny_ * npol_ ;
|
---|
1228 | tab.rwKeywordSet().define( "nPol", npol_ ) ;
|
---|
1229 | tab.addRow( nrow ) ;
|
---|
1230 | Vector<Double> cpix( 2 ) ;
|
---|
1231 | cpix(0) = Double( nx_ - 1 ) * 0.5 ;
|
---|
1232 | cpix(1) = Double( ny_ - 1 ) * 0.5 ;
|
---|
1233 | Vector<Double> dir( 2 ) ;
|
---|
1234 | ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
|
---|
1235 | ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
|
---|
1236 | ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
|
---|
1237 | Int irow = 0 ;
|
---|
1238 | Vector<Float> sp( nchan_ ) ;
|
---|
1239 | Bool bsp, bdata ;
|
---|
1240 | const Float *data_p = data_.getStorage( bdata ) ;
|
---|
1241 | Float *wsp_p, *sp_p ;
|
---|
1242 | const Float *wdata_p = data_p ;
|
---|
1243 | long step = nx_ * ny_ * npol_ ;
|
---|
1244 | long offset ;
|
---|
1245 | for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
|
---|
1246 | dir(1) = center_(1) - ( cpix(1) - (Double)iy ) * celly_ ;
|
---|
1247 | for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
|
---|
1248 | dir(0) = center_(0) - ( cpix(0) - (Double)ix ) * cellx_ ;
|
---|
1249 | for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
|
---|
1250 | offset = ix + iy * nx_ + ipol * nx_ * ny_ ;
|
---|
1251 | //os << "offset = " << offset << LogIO::POST ;
|
---|
1252 | sp_p = sp.getStorage( bsp ) ;
|
---|
1253 | wsp_p = sp_p ;
|
---|
1254 | wdata_p = data_p + offset ;
|
---|
1255 | for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
|
---|
1256 | *wsp_p = *wdata_p ;
|
---|
1257 | wsp_p++ ;
|
---|
1258 | wdata_p += step ;
|
---|
1259 | }
|
---|
1260 | sp.putStorage( sp_p, bsp ) ;
|
---|
1261 | spectraCol.put( irow, sp ) ;
|
---|
1262 | directionCol.put( irow, dir ) ;
|
---|
1263 | polnoCol.put( irow, pollist_[ipol] ) ;
|
---|
1264 | irow++ ;
|
---|
1265 | }
|
---|
1266 | }
|
---|
1267 | }
|
---|
1268 | data_.freeStorage( data_p, bdata ) ;
|
---|
1269 |
|
---|
1270 | t1 = mathutil::gettimeofday_sec() ;
|
---|
1271 | os << "saveData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
|
---|
1272 |
|
---|
1273 | return outfile_ ;
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | void STGrid::prepareTable( Table &tab, String &name )
|
---|
1277 | {
|
---|
1278 | Table t( infile_, Table::Old ) ;
|
---|
1279 | t.deepCopy( name, Table::New, False, t.endianFormat(), True ) ;
|
---|
1280 | tab = Table( name, Table::Update ) ;
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | }
|
---|