source: trunk/src/STGrid.cpp@ 2369

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

New Development: No

JIRA Issue: Yes CAS-2816

Ready for Test: No

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...

Gridding speed up: direct array access using getStorage()

in STGrid::getWeight().


File size: 23.6 KB
Line 
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/ArrayPartMath.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
21#include <measures/Measures/MDirection.h>
22
23#include <Scantable.h>
24#include <MathUtils.h>
25
26#include "STGrid.h"
27
28using namespace std ;
29using namespace casa ;
30using namespace asap ;
31
32namespace asap {
33
34// constructor
35STGrid::STGrid()
36{
37 init() ;
38}
39
40STGrid::STGrid( const string infile )
41{
42 init() ;
43
44 setFileIn( infile ) ;
45}
46
47void STGrid::init()
48{
49 ifno_ = -1 ;
50 nx_ = -1 ;
51 ny_ = -1 ;
52 npol_ = 0 ;
53 nchan_ = 0 ;
54 nrow_ = 0 ;
55 cellx_ = 0.0 ;
56 celly_ = 0.0 ;
57 center_ = Vector<Double> ( 2, 0.0 ) ;
58 convType_ = "BOX" ;
59 wtype_ = "UNIFORM" ;
60 convSupport_ = -1 ;
61 userSupport_ = -1 ;
62 convSampling_ = 100 ;
63}
64
65void STGrid::setFileIn( const string infile )
66{
67 String name( infile ) ;
68 if ( infile_.compare( name ) != 0 ) {
69 infile_ = String( infile ) ;
70 tab_ = Table( infile_ ) ;
71 }
72}
73
74void STGrid::setPolList( vector<unsigned int> pols )
75{
76 pollist_.assign( Vector<uInt>( pols ) ) ;
77 cout << "pollist_ = " << pollist_ << endl ;
78}
79
80void STGrid::setScanList( vector<unsigned int> scans )
81{
82 scanlist_.assign( Vector<uInt>( scans ) ) ;
83 cout << "scanlist_ = " << scanlist_ << endl ;
84}
85
86void STGrid::setWeight( const string wType )
87{
88 wtype_ = String( wType ) ;
89 wtype_.upcase() ;
90 cout << "wtype_ = " << wtype_ << endl ;
91}
92
93void STGrid::defineImage( int nx,
94 int ny,
95 string scellx,
96 string scelly,
97 string scenter )
98{
99 ROArrayColumn<Double> dirCol( tab_, "DIRECTION" ) ;
100 Matrix<Double> direction = dirCol.getColumn() ;
101 Double rmax, rmin, dmax, dmin ;
102 minMax( rmin, rmax, direction.row( 0 ) ) ;
103 minMax( dmin, dmax, direction.row( 1 ) ) ;
104
105 Int npx = (Int)nx ;
106 Int npy = (Int)ny ;
107 String cellx( scellx ) ;
108 String celly( scelly ) ;
109 String center( scenter ) ;
110 setupGrid( npx, npy,
111 cellx, celly,
112 rmin, rmax,
113 dmin, dmax,
114 center ) ;
115}
116
117void STGrid::setFunc( string convType,
118 int convSupport )
119{
120 convType_ = String( convType ) ;
121 convType_.upcase() ;
122 userSupport_ = (Int)convSupport ;
123}
124
125#define NEED_UNDERSCORES
126#if defined(NEED_UNDERSCORES)
127#define ggridsd ggridsd_
128#endif
129extern "C" {
130 void ggridsd(Double*,
131 const Complex*,
132 Int*,
133 Int*,
134 Int*,
135 const Int*,
136 const Int*,
137 const Float*,
138 Int*,
139 Int*,
140 Complex*,
141 Float*,
142 Int*,
143 Int*,
144 Int *,
145 Int *,
146 Int*,
147 Int*,
148 Float*,
149 Int*,
150 Int*,
151 Double*);
152}
153void STGrid::grid()
154{
155 LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
156
157 // retrieve data
158 Cube<Float> spectra ;
159 Matrix<Double> direction ;
160 Cube<uChar> flagtra ;
161 Matrix<uInt> rflag ;
162 Matrix<Float> weight ;
163 double t0, t1 ;
164 t0 = mathutil::gettimeofday_sec() ;
165 getData( spectra, direction, flagtra, rflag, weight ) ;
166 t1 = mathutil::gettimeofday_sec() ;
167 os << "getData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
168 IPosition sshape = spectra.shape() ;
169 //os << "spectra.shape()=" << spectra.shape() << LogIO::POST ;
170 //os << "max(spectra) = " << max(spectra) << LogIO::POST ;
171 //os << "weight = " << weight << LogIO::POST ;
172
173 // flagtra: uChar -> Int
174 // rflag: uInt -> Int
175 Cube<Int> flagI ;
176 Matrix<Int> rflagI ;
177 t0 = mathutil::gettimeofday_sec() ;
178 toInt( &flagtra, &flagI ) ;
179 toInt( &rflag, &rflagI ) ;
180 t1 = mathutil::gettimeofday_sec() ;
181 os << "toInt: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
182
183 // grid parameter
184 os << LogIO::DEBUGGING ;
185 os << "----------" << endl ;
186 os << "Grid parameter summary" << endl ;
187 os << " (nx,ny) = (" << nx_ << "," << ny_ << ")" << endl ;
188 os << " (cellx,celly) = (" << cellx_ << "," << celly_ << ")" << endl ;
189 os << " center = " << center_ << endl ;
190 os << "----------" << LogIO::POST ;
191 os << LogIO::NORMAL ;
192
193 // convolution kernel
194 Vector<Float> convFunc ;
195 t0 = mathutil::gettimeofday_sec() ;
196 setConvFunc( convFunc ) ;
197 t1 = mathutil::gettimeofday_sec() ;
198 os << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
199 //cout << "convSupport=" << convSupport_ << endl ;
200 //cout << "convFunc=" << convFunc << endl ;
201
202 // world -> pixel
203 Matrix<Double> xypos( direction.shape(), 0.0 ) ;
204 t0 = mathutil::gettimeofday_sec() ;
205 toPixel( direction, xypos ) ;
206 t1 = mathutil::gettimeofday_sec() ;
207 os << "toPixel: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
208
209 // call ggridsd
210 Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ;
211 Double *xypos_p = xypos.getStorage( deletePos ) ;
212 Cube<Complex> dataC( spectra.shape(), 0.0 ) ;
213 setReal( dataC, spectra ) ;
214 const Complex *data_p = dataC.getStorage( deleteData ) ;
215 const Float *wgt_p = weight.getStorage( deleteWgt ) ;
216 const Int *flag_p = flagI.getStorage( deleteFlag ) ;
217 const Int *rflag_p = rflagI.getStorage( deleteFlagR ) ;
218 Float *conv_p = convFunc.getStorage( deleteConv ) ;
219 // Extend grid plane with convSupport_
220 Int gnx = nx_ ;
221 Int gny = ny_ ;
222// Int gnx = nx_+convSupport_*2 ;
223// Int gny = ny_+convSupport_*2 ;
224 IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
225 Array<Complex> gdataArrC( gshape, 0.0 ) ;
226 Array<Float> gwgtArr( gshape, 0.0 ) ;
227 Complex *gdata_p = gdataArrC.getStorage( deleteDataG ) ;
228 Float *wdata_p = gwgtArr.getStorage( deleteWgtG ) ;
229 Int idopsf = 0 ;
230 Int *chanMap = new Int[nchan_] ;
231 {
232 Int *work_p = chanMap ;
233 for ( Int i = 0 ; i < nchan_ ; i++ ) {
234 *work_p = i ;
235 work_p++ ;
236 }
237 }
238 Int *polMap = new Int[npol_] ;
239 {
240 Int *work_p = polMap ;
241 for ( Int i = 0 ; i < npol_ ; i++ ) {
242 *work_p = i ;
243 work_p++ ;
244 }
245 }
246 Double *sumw_p = new Double[npol_*nchan_] ;
247 {
248 Double *work_p = sumw_p ;
249 for ( Int i = 0 ; i < npol_*nchan_ ; i++ ) {
250 *work_p = 0.0 ;
251 work_p++ ;
252 }
253 }
254 t0 = mathutil::gettimeofday_sec() ;
255 Int irow = -1 ;
256 ggridsd( xypos_p,
257 data_p,
258 &npol_,
259 &nchan_,
260 &idopsf,
261 flag_p,
262 rflag_p,
263 wgt_p,
264 &nrow_,
265 &irow,
266 gdata_p,
267 wdata_p,
268 &gnx,
269 &gny,
270 &npol_,
271 &nchan_,
272 &convSupport_,
273 &convSampling_,
274 conv_p,
275 chanMap,
276 polMap,
277 sumw_p ) ;
278 t1 = mathutil::gettimeofday_sec() ;
279 os << "ggridsd: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
280 xypos.putStorage( xypos_p, deletePos ) ;
281 dataC.freeStorage( data_p, deleteData ) ;
282 weight.freeStorage( wgt_p, deleteWgt ) ;
283 flagI.freeStorage( flag_p, deleteFlag ) ;
284 rflagI.freeStorage( rflag_p, deleteFlagR ) ;
285 convFunc.putStorage( conv_p, deleteConv ) ;
286 delete polMap ;
287 delete chanMap ;
288 gdataArrC.putStorage( gdata_p, deleteDataG ) ;
289 gwgtArr.putStorage( wdata_p, deleteWgtG ) ;
290 Array<Float> gdataArr = real( gdataArrC ) ;
291 setData( data_, gdataArr, gwgtArr ) ;
292 //Matrix<Double> sumWeight( IPosition( 2, npol_, nchan_ ), sumw_p, TAKE_OVER ) ;
293 delete sumw_p ;
294 //cout << "sumWeight = " << sumWeight << endl ;
295// os << "gdataArr = " << gdataArr << LogIO::POST ;
296// os << "gwgtArr = " << gwgtArr << LogIO::POST ;
297// os << "data_ " << data_ << LogIO::POST ;
298}
299
300void STGrid::setData( Array<Float> &data,
301 Array<Float> &gdata,
302 Array<Float> &gwgt )
303{
304 LogIO os( LogOrigin("STGrid","setData",WHERE) ) ;
305 double t0, t1 ;
306 t0 = mathutil::gettimeofday_sec() ;
307 data.resize( gdata.shape() ) ; //data = 0.0 ;
308 uInt len = data.nelements() ;
309 Float *w0_p ;
310 const Float *w1_p, *w2_p ;
311 Bool b0, b1, b2 ;
312 Float *data_p = data.getStorage( b0 ) ;
313 const Float *gdata_p = gdata.getStorage( b1 ) ;
314 const Float *gwgt_p = gwgt.getStorage( b2 ) ;
315 w0_p = data_p ;
316 w1_p = gdata_p ;
317 w2_p = gwgt_p ;
318 for ( uInt i = 0 ; i < len ; i++ ) {
319 *w0_p = (*w2_p > 0.0) ? (*w1_p / *w2_p) : 0.0 ;
320 w0_p++ ;
321 w1_p++ ;
322 w2_p++ ;
323 }
324 data.putStorage( data_p, b0 ) ;
325 gdata.freeStorage( gdata_p, b1 ) ;
326 gwgt.freeStorage( gwgt_p, b2 ) ;
327 t1 = mathutil::gettimeofday_sec() ;
328 os << "setData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
329}
330
331void STGrid::setupGrid( Int &nx,
332 Int &ny,
333 String &cellx,
334 String &celly,
335 Double &xmin,
336 Double &xmax,
337 Double &ymin,
338 Double &ymax,
339 String &center )
340{
341 //cout << "nx=" << nx << ", ny=" << ny << endl ;
342
343 // center position
344 if ( center.size() == 0 ) {
345 center_(0) = 0.5 * ( xmin + xmax ) ;
346 center_(1) = 0.5 * ( ymin + ymax ) ;
347 }
348 else {
349 String::size_type pos0 = center.find( " " ) ;
350 if ( pos0 == String::npos ) {
351 throw AipsError( "bad string format in parameter center" ) ;
352 }
353 String::size_type pos1 = center.find( " ", pos0+1 ) ;
354 String typestr, xstr, ystr ;
355 if ( pos1 != String::npos ) {
356 typestr = center.substr( 0, pos0 ) ;
357 xstr = center.substr( pos0+1, pos1-pos0 ) ;
358 ystr = center.substr( pos1+1 ) ;
359 // todo: convert to J2000 (or direction ref for DIRECTION column)
360 }
361 else {
362 typestr = "J2000" ;
363 xstr = center.substr( 0, pos0 ) ;
364 ystr = center.substr( pos0+1 ) ;
365 }
366 QuantumHolder qh ;
367 String err ;
368 qh.fromString( err, xstr ) ;
369 Quantum<Double> xcen = qh.asQuantumDouble() ;
370 qh.fromString( err, ystr ) ;
371 Quantum<Double> ycen = qh.asQuantumDouble() ;
372 center_(0) = xcen.getValue( "rad" ) ;
373 center_(1) = ycen.getValue( "rad" ) ;
374 }
375
376
377 //Double wx = xmax - xmin ;
378 //Double wy = ymax - ymin ;
379 Double wx = max( abs(xmax-center_(0)), abs(xmin-center_(0)) ) * 2 ;
380 Double wy = max( abs(ymax-center_(1)), abs(ymin-center_(1)) ) * 2 ;
381 // take 10% margin
382 wx *= 1.10 ;
383 wy *= 1.10 ;
384 Quantum<Double> qcellx ;
385 Quantum<Double> qcelly ;
386 nx_ = nx ;
387 ny_ = ny ;
388 if ( nx < 0 && ny > 0 ) {
389 nx_ = ny ;
390 ny_ = ny ;
391 }
392 if ( ny < 0 && nx > 0 ) {
393 nx_ = nx ;
394 ny_ = nx ;
395 }
396 //cout << "nx_ = " << nx_ << ", ny_ = " << ny_ << endl ;
397 if ( cellx.size() != 0 && celly.size() != 0 ) {
398 readQuantity( qcellx, cellx ) ;
399 readQuantity( qcelly, celly ) ;
400 }
401 else if ( celly.size() != 0 ) {
402 cout << "Using celly to x-axis..." << endl ;
403 readQuantity( qcelly, celly ) ;
404 qcellx = qcelly ;
405 }
406 else if ( cellx.size() != 0 ) {
407 cout << "Using cellx to y-axis..." << endl ;
408 readQuantity( qcellx, cellx ) ;
409 qcelly = qcellx ;
410 }
411 else {
412 if ( nx_ < 0 ) {
413 cout << "No user preference in grid setting. Using default..." << endl ;
414 readQuantity( qcellx, "1.0arcmin" ) ;
415 qcelly = qcellx ;
416 }
417 else {
418 qcellx = Quantum<Double>( wx/nx_, "rad" ) ;
419 qcelly = Quantum<Double>( wy/ny_, "rad" ) ;
420 }
421 }
422 cellx_ = qcellx.getValue( "rad" ) ;
423 celly_ = qcelly.getValue( "rad" ) ;
424 if ( nx_ < 0 ) {
425 nx_ = Int( ceil( wx/cellx_ ) ) ;
426 ny_ = Int( ceil( wy/celly_ ) ) ;
427 }
428}
429
430void STGrid::selectData( Table &tab )
431{
432 Int ifno = ifno_ ;
433 Table taborg( infile_ ) ;
434 if ( ifno == -1 ) {
435 LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
436// os << LogIO::SEVERE
437// << "Please set IFNO before actual gridding"
438// << LogIO::EXCEPTION ;
439 ROScalarColumn<uInt> ifnoCol( taborg, "IFNO" ) ;
440 ifno = ifnoCol( 0 ) ;
441 os << LogIO::WARN
442 << "IFNO is not given. Using default IFNO: " << ifno << LogIO::POST ;
443 }
444// tab = taborg( taborg.col("IFNO") == ifno ) ;
445 TableExprNode node ;
446 node = taborg.col("IFNO") == ifno ;
447 if ( scanlist_.size() > 0 ) {
448 node = node && taborg.col("SCANNO").in( scanlist_ ) ;
449 }
450 tab = taborg( node ) ;
451 if ( tab.nrow() == 0 ) {
452 LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
453 os << LogIO::SEVERE
454 << "No corresponding rows for given selection: IFNO " << ifno
455 << " SCANNO " << scanlist_
456 << LogIO::EXCEPTION ;
457 }
458}
459
460void STGrid::getData( Cube<Float> &spectra,
461 Matrix<Double> &direction,
462 Cube<uChar> &flagtra,
463 Matrix<uInt> &rflag,
464 Matrix<Float> &weight )
465{
466 Table tab ;
467 selectData( tab ) ;
468 ROScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
469 Vector<uInt> pols = polnoCol.getColumn() ;
470 Vector<uInt> pollistOrg ;
471 uInt npolOrg = 0 ;
472 for ( uInt i = 0 ; i < pols.size() ; i++ ) {
473 if ( allNE( pollistOrg, pols[i] ) ) {
474 pollistOrg.resize( npolOrg+1, True ) ;
475 pollistOrg[npolOrg] = pols[i] ;
476 npolOrg++ ;
477 }
478 }
479 if ( pollist_.size() == 0 )
480 pollist_ = pollistOrg ;
481 else {
482 Vector<uInt> newlist ;
483 uInt newsize = 0 ;
484 for ( uInt i = 0 ; i < pollist_.size() ; i++ ) {
485 if ( anyEQ( pols, pollist_[i] ) ) {
486 newlist.resize( newsize+1, True ) ;
487 newlist[newsize] = pollist_[i] ;
488 newsize++ ;
489 }
490 }
491 pollist_ = newlist ;
492 }
493 npol_ = pollist_.size() ;
494 ROArrayColumn<uChar> tmpCol( tab, "FLAGTRA" ) ;
495 nchan_ = tmpCol( 0 ).nelements() ;
496 nrow_ = tab.nrow() / npolOrg ;
497// cout << "npol_ = " << npol_ << endl ;
498// cout << "nchan_ = " << nchan_ << endl ;
499// cout << "nrow_ = " << nrow_ << endl ;
500 spectra.resize( npol_, nchan_, nrow_ ) ;
501 flagtra.resize( npol_, nchan_, nrow_ ) ;
502 rflag.resize( npol_, nrow_ ) ;
503 Cube<Float> tsys( npol_, nchan_, nrow_ ) ;
504 Matrix<Double> tint( npol_, nrow_ ) ;
505 for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
506 Table subt = tab( tab.col("POLNO") == pollist_[ipol] ) ;
507 ROArrayColumn<Float> spectraCol( subt, "SPECTRA" ) ;
508 ROArrayColumn<Double> directionCol( subt, "DIRECTION" ) ;
509 ROArrayColumn<uChar> flagtraCol( subt, "FLAGTRA" ) ;
510 ROScalarColumn<uInt> rflagCol( subt, "FLAGROW" ) ;
511 ROArrayColumn<Float> tsysCol( subt, "TSYS" ) ;
512 ROScalarColumn<Double> tintCol( subt, "INTERVAL" ) ;
513 Matrix<Float> tmpF = spectra.yzPlane( ipol ) ;
514 Matrix<uChar> tmpUC = flagtra.yzPlane( ipol ) ;
515 Vector<uInt> tmpUI = rflag.row( ipol ) ;
516 spectraCol.getColumn( tmpF ) ;
517 flagtraCol.getColumn( tmpUC ) ;
518 rflagCol.getColumn( tmpUI ) ;
519 if ( ipol == 0 )
520 directionCol.getColumn( direction ) ;
521 Matrix<Float> tmpF2 = tsysCol.getColumn() ;
522 Vector<Double> tmpD = tint.row( ipol ) ;
523 if ( tmpF2.shape()(0) == nchan_ ) {
524 tsys.yzPlane( ipol ) = tmpF2 ;
525 }
526 else {
527 tsys.yzPlane( ipol ) = tmpF2(0,0) ;
528 }
529 tintCol.getColumn( tmpD ) ;
530 }
531
532 getWeight( weight, tsys, tint ) ;
533}
534
535void STGrid::getWeight( Matrix<Float> &w,
536 Cube<Float> &tsys,
537 Matrix<Double> &tint )
538{
539 LogIO os( LogOrigin("STGrid","getWeight",WHERE) ) ;
540 double t0, t1 ;
541 t0 = mathutil::gettimeofday_sec() ;
542 // resize
543 w.resize( nchan_, nrow_ ) ;
544
545 // set weight
546 Bool warn = False ;
547 if ( wtype_.compare( "UNIFORM" ) == 0 ) {
548 w = 1.0 ;
549 }
550 else if ( wtype_.compare( "TINT" ) == 0 ) {
551 if ( npol_ > 1 ) warn = True ;
552 Bool b0, b1 ;
553 Float *w_p = w.getStorage( b0 ) ;
554 Float *w0_p = w_p ;
555 const Double *ti_p = tint.getStorage( b1 ) ;
556 const Double *w1_p = ti_p ;
557 for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
558 Float val = (Float)(polMean( w1_p )) ;
559 for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
560 *w0_p = val ;
561 w0_p++ ;
562 }
563 }
564 w.putStorage( w_p, b0 ) ;
565 tint.freeStorage( ti_p, b1 ) ;
566 }
567 else if ( wtype_.compare( "TSYS" ) == 0 ) {
568 if ( npol_ > 1 ) warn = True ;
569 Bool b0, b1 ;
570 Float *w_p = w.getStorage( b0 ) ;
571 Float *w0_p = w_p ;
572 const Float *ts_p = tsys.getStorage( b1 ) ;
573 const Float *w1_p = ts_p ;
574 for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
575 for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
576 Float val = polMean( w1_p ) ;
577 *w0_p = 1.0 / ( val * val ) ;
578 w0_p++ ;
579 }
580 }
581 w.putStorage( w_p, b0 ) ;
582 tsys.freeStorage( ts_p, b1 ) ;
583 }
584 else if ( wtype_.compare( "TINTSYS" ) == 0 ) {
585 if ( npol_ > 1 ) warn = True ;
586 Bool b0, b1, b2 ;
587 Float *w_p = w.getStorage( b0 ) ;
588 Float *w0_p = w_p ;
589 const Double *ti_p = tint.getStorage( b1 ) ;
590 const Double *w1_p = ti_p ;
591 const Float *ts_p = tsys.getStorage( b2 ) ;
592 const Float *w2_p = ts_p ;
593 for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
594 Float interval = (Float)(polMean( w1_p )) ;
595 for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
596 Float temp = polMean( w2_p ) ;
597 *w0_p = interval / ( temp * temp ) ;
598 w0_p++ ;
599 }
600 }
601 w.putStorage( w_p, b0 ) ;
602 tint.freeStorage( ti_p, b1 ) ;
603 tsys.freeStorage( ts_p, b2 ) ;
604 }
605 else {
606 //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
607 os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
608 w = 1.0 ;
609 }
610
611 if ( npol_ > 1 ) {
612 //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
613 os << LogIO::WARN << "STGrid doesn't support assigning polarization-dependent weight. Use averaged weight over polarization." << LogIO::POST ;
614 }
615 t1 = mathutil::gettimeofday_sec() ;
616 os << "getWeight: elapsed time is " << t1-t0 << " sec" << LogIO::POST ;
617}
618
619Float STGrid::polMean( const Float *p )
620{
621 Float v = 0.0 ;
622 for ( Int i = 0 ; i < npol_ ; i++ ) {
623 v += *p ;
624 p++ ;
625 }
626 v /= npol_ ;
627 return v ;
628}
629
630Double STGrid::polMean( const Double *p )
631{
632 Double v = 0.0 ;
633 for ( Int i = 0 ; i < npol_ ; i++ ) {
634 v += *p ;
635 p++ ;
636 }
637 v /= npol_ ;
638 return v ;
639}
640
641void STGrid::toInt( Array<uChar> *u, Array<Int> *v )
642{
643 uInt len = u->nelements() ;
644 Int *int_p = new Int[len] ;
645 Bool deleteIt ;
646 const uChar *data_p = u->getStorage( deleteIt ) ;
647 Int *i_p = int_p ;
648 const uChar *u_p = data_p ;
649 for ( uInt i = 0 ; i < len ; i++ ) {
650 *i_p = ( *u_p == 0 ) ? 0 : 1 ;
651 i_p++ ;
652 u_p++ ;
653 }
654 u->freeStorage( data_p, deleteIt ) ;
655 v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
656}
657
658void STGrid::toInt( Array<uInt> *u, Array<Int> *v )
659{
660 uInt len = u->nelements() ;
661 Int *int_p = new Int[len] ;
662 Bool deleteIt ;
663 const uInt *data_p = u->getStorage( deleteIt ) ;
664 Int *i_p = int_p ;
665 const uInt *u_p = data_p ;
666 for ( uInt i = 0 ; i < len ; i++ ) {
667 *i_p = ( *u_p == 0 ) ? 0 : 1 ;
668 i_p++ ;
669 u_p++ ;
670 }
671 u->freeStorage( data_p, deleteIt ) ;
672 v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
673}
674
675void STGrid::toPixel( Matrix<Double> &world, Matrix<Double> &pixel )
676{
677 // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_)
678 // grid plane to avoid unexpected behavior on grid edge
679 Vector<Double> pixc( 2 ) ;
680 pixc(0) = Double( nx_-1 ) * 0.5 ;
681 pixc(1) = Double( ny_-1 ) * 0.5 ;
682// pixc(0) = Double( nx_+2*convSupport_-1 ) * 0.5 ;
683// pixc(1) = Double( ny_+2*convSupport_-1 ) * 0.5 ;
684 uInt nrow = world.shape()[1] ;
685 Vector<Double> cell( 2 ) ;
686 cell(0) = cellx_ ;
687 cell(1) = celly_ ;
688 for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
689 for ( uInt i = 0 ; i < 2 ; i++ ) {
690 pixel( i, irow ) = pixc(i) + ( world(i, irow) - center_(i) ) / cell(i) ;
691 }
692 }
693// String gridfile = "grid."+convType_+"."+String::toString(convSupport_)+".dat" ;
694// ofstream ofs( gridfile.c_str(), ios::out ) ;
695// ofs << "center " << center_(0) << " " << pixc(0)
696// << " " << center_(1) << " " << pixc(1) << endl ;
697// for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
698// ofs << irow ;
699// for ( uInt i = 0 ; i < 2 ; i++ ) {
700// ofs << " " << world(i, irow) << " " << pixel(i, irow) ;
701// }
702// ofs << endl ;
703// }
704// ofs.close() ;
705}
706
707void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize )
708{
709 convFunc = 0.0 ;
710 for ( Int i = 0 ; i < convSize/2 ; i++ )
711 convFunc(i) = 1.0 ;
712}
713
714#define NEED_UNDERSCORES
715#if defined(NEED_UNDERSCORES)
716#define grdsf grdsf_
717#endif
718extern "C" {
719 void grdsf(Double*, Double*);
720}
721void STGrid::spheroidalFunc( Vector<Float> &convFunc )
722{
723 convFunc = 0.0 ;
724 for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
725 Double nu = Double(i) / Double(convSupport_*convSampling_) ;
726 Double val ;
727 grdsf( &nu, &val ) ;
728 convFunc(i) = ( 1.0 - nu * nu ) * val ;
729 }
730}
731
732void STGrid::gaussFunc( Vector<Float> &convFunc )
733{
734 convFunc = 0.0 ;
735 // HWHM of the Gaussian is convSupport_ / 4
736 // To take into account Gaussian tail, kernel cutoff is set to 4 * HWHM
737 Int len = convSampling_ * convSupport_ ;
738 Double hwhm = len * 0.25 ;
739 for ( Int i = 0 ; i < len ; i++ ) {
740 Double val = Double(i) / hwhm ;
741 convFunc(i) = exp( -log(2)*val*val ) ;
742 }
743}
744
745void STGrid::pbFunc( Vector<Float> &convFunc )
746{
747 convFunc = 0.0 ;
748}
749
750void STGrid::setConvFunc( Vector<Float> &convFunc )
751{
752 convSupport_ = userSupport_ ;
753 if ( convType_ == "BOX" ) {
754 if ( convSupport_ < 0 )
755 convSupport_ = 0 ;
756 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
757 convFunc.resize( convSize ) ;
758 boxFunc( convFunc, convSize ) ;
759 }
760 else if ( convType_ == "SF" ) {
761 if ( convSupport_ < 0 )
762 convSupport_ = 3 ;
763 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
764 convFunc.resize( convSize ) ;
765 spheroidalFunc( convFunc ) ;
766 }
767 else if ( convType_ == "GAUSS" ) {
768 // to take into account Gaussian tail
769 if ( convSupport_ < 0 )
770 convSupport_ = 12 ; // 3 * 4
771 else {
772 convSupport_ = userSupport_ * 4 ;
773 }
774 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
775 convFunc.resize( convSize ) ;
776 gaussFunc( convFunc ) ;
777 }
778 else if ( convType_ == "PB" ) {
779 if ( convSupport_ < 0 )
780 convSupport_ = 0 ;
781 pbFunc( convFunc ) ;
782 }
783 else {
784 throw AipsError( "Unsupported convolution function" ) ;
785 }
786}
787
788string STGrid::saveData( string outfile )
789{
790 LogIO os( LogOrigin("STGrid", "saveData", WHERE) ) ;
791 double t0, t1 ;
792 t0 = mathutil::gettimeofday_sec() ;
793
794 //Int polno = 0 ;
795 string outfile_ ;
796 if ( outfile.size() == 0 ) {
797 if ( infile_.lastchar() == '/' ) {
798 outfile_ = infile_.substr( 0, infile_.size()-1 ) ;
799 }
800 else {
801 outfile_ = infile_ ;
802 }
803 outfile_ += ".grid" ;
804 }
805 else {
806 outfile_ = outfile ;
807 }
808 CountedPtr<Scantable> ref( new Scantable( infile_, Table::Plain ) ) ;
809 //cout << "ref->nchan()=" << ref->nchan() << endl ;
810 CountedPtr<Scantable> out( new Scantable( *ref, True ) ) ;
811 Table tab = out->table() ;
812 IPosition dshape = data_.shape() ;
813 Int nrow = nx_ * ny_ * npol_ ;
814 tab.rwKeywordSet().define( "nPol", npol_ ) ;
815 tab.addRow( nrow ) ;
816 Vector<Double> cpix( 2 ) ;
817 cpix(0) = Double( nx_ - 1 ) * 0.5 ;
818 cpix(1) = Double( ny_ - 1 ) * 0.5 ;
819 Vector<Double> dir( 2 ) ;
820 ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
821 ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
822 ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
823 Int irow = 0 ;
824 for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
825 for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
826 for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
827 IPosition start( 4, ix, iy, ipol, 0 ) ;
828 IPosition end( 4, ix, iy, ipol, nchan_-1 ) ;
829 IPosition inc( 4, 1, 1, 1, 1 ) ;
830 Vector<Float> sp = data_( start, end, inc ) ;
831 dir(0) = center_(0) - ( cpix(0) - (Double)ix ) * cellx_ ;
832 dir(1) = center_(1) - ( cpix(1) - (Double)iy ) * celly_ ;
833 spectraCol.put( irow, sp ) ;
834 directionCol.put( irow, dir ) ;
835 polnoCol.put( irow, pollist_[ipol] ) ;
836 irow++ ;
837 }
838 }
839 }
840 //cout << "outfile_=" << outfile_ << endl ;
841 out->makePersistent( outfile_ ) ;
842
843 t1 = mathutil::gettimeofday_sec() ;
844 os << "saveData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
845
846 return outfile_ ;
847}
848
849}
Note: See TracBrowser for help on using the repository browser.