source: trunk/src/STGrid.cpp@ 2368

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

Speed up code.

  • setData: defined. Changed Array access, which was using Slicer, to

direct data access using getStorage.

  • saveData: reference table is created as Plain table instead of Memory

table to save memory usage

  • getWeight: skip weight initialization.


File size: 22.6 KB
RevLine 
[2356]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>
[2361]9#include <casa/Arrays/ArrayPartMath.h>
[2356]10#include <casa/Quanta/Quantum.h>
11#include <casa/Quanta/QuantumHolder.h>
12#include <casa/Utilities/CountedPtr.h>
[2361]13#include <casa/Logging/LogIO.h>
[2356]14
15#include <tables/Tables/Table.h>
[2360]16#include <tables/Tables/TableRecord.h>
17#include <tables/Tables/ExprNode.h>
[2356]18#include <tables/Tables/ScalarColumn.h>
19#include <tables/Tables/ArrayColumn.h>
20
21#include <measures/Measures/MDirection.h>
22
23#include <Scantable.h>
[2364]24#include <MathUtils.h>
25
[2356]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{
[2362]49 ifno_ = -1 ;
[2356]50 nx_ = -1 ;
51 ny_ = -1 ;
[2361]52 npol_ = 0 ;
53 nchan_ = 0 ;
54 nrow_ = 0 ;
[2356]55 cellx_ = 0.0 ;
56 celly_ = 0.0 ;
57 center_ = Vector<Double> ( 2, 0.0 ) ;
58 convType_ = "BOX" ;
[2361]59 wtype_ = "UNIFORM" ;
[2356]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
[2360]74void STGrid::setPolList( vector<unsigned int> pols )
75{
76 pollist_.assign( Vector<uInt>( pols ) ) ;
77 cout << "pollist_ = " << pollist_ << endl ;
78}
79
[2364]80void STGrid::setScanList( vector<unsigned int> scans )
81{
82 scanlist_.assign( Vector<uInt>( scans ) ) ;
83 cout << "scanlist_ = " << scanlist_ << endl ;
84}
85
[2361]86void STGrid::setWeight( const string wType )
87{
88 wtype_ = String( wType ) ;
89 wtype_.upcase() ;
90 cout << "wtype_ = " << wtype_ << endl ;
91}
92
[2356]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
[2364]117void STGrid::setFunc( string convType,
118 int convSupport )
[2356]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{
[2361]155 LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
[2356]156
157 // retrieve data
[2360]158 Cube<Float> spectra ;
[2356]159 Matrix<Double> direction ;
[2360]160 Cube<uChar> flagtra ;
161 Matrix<uInt> rflag ;
[2361]162 Matrix<Float> weight ;
[2364]163 double t0, t1 ;
164 t0 = mathutil::gettimeofday_sec() ;
[2362]165 getData( spectra, direction, flagtra, rflag, weight ) ;
[2364]166 t1 = mathutil::gettimeofday_sec() ;
167 os << "getData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2356]168 IPosition sshape = spectra.shape() ;
[2361]169 //os << "spectra.shape()=" << spectra.shape() << LogIO::POST ;
170 //os << "max(spectra) = " << max(spectra) << LogIO::POST ;
171 //os << "weight = " << weight << LogIO::POST ;
[2356]172
173 // flagtra: uChar -> Int
174 // rflag: uInt -> Int
[2360]175 Cube<Int> flagI ;
176 Matrix<Int> rflagI ;
[2364]177 t0 = mathutil::gettimeofday_sec() ;
[2360]178 toInt( &flagtra, &flagI ) ;
179 toInt( &rflag, &rflagI ) ;
[2364]180 t1 = mathutil::gettimeofday_sec() ;
181 os << "toInt: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2356]182
183 // grid parameter
[2362]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 ;
[2356]192
[2359]193 // convolution kernel
194 Vector<Float> convFunc ;
[2364]195 t0 = mathutil::gettimeofday_sec() ;
[2359]196 setConvFunc( convFunc ) ;
[2364]197 t1 = mathutil::gettimeofday_sec() ;
198 os << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2359]199 //cout << "convSupport=" << convSupport_ << endl ;
200 //cout << "convFunc=" << convFunc << endl ;
201
[2356]202 // world -> pixel
203 Matrix<Double> xypos( direction.shape(), 0.0 ) ;
[2364]204 t0 = mathutil::gettimeofday_sec() ;
[2356]205 toPixel( direction, xypos ) ;
[2364]206 t1 = mathutil::gettimeofday_sec() ;
207 os << "toPixel: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2356]208
[2361]209 // call ggridsd
[2356]210 Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ;
211 Double *xypos_p = xypos.getStorage( deletePos ) ;
[2360]212 Cube<Complex> dataC( spectra.shape(), 0.0 ) ;
[2356]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 ) ;
[2359]219 // Extend grid plane with convSupport_
[2364]220 Int gnx = nx_ ;
221 Int gny = ny_ ;
222// Int gnx = nx_+convSupport_*2 ;
223// Int gny = ny_+convSupport_*2 ;
[2361]224 IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
[2356]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 ;
[2361]230 Int *chanMap = new Int[nchan_] ;
[2356]231 {
232 Int *work_p = chanMap ;
[2361]233 for ( Int i = 0 ; i < nchan_ ; i++ ) {
[2356]234 *work_p = i ;
235 work_p++ ;
236 }
237 }
[2361]238 Int *polMap = new Int[npol_] ;
[2356]239 {
240 Int *work_p = polMap ;
[2361]241 for ( Int i = 0 ; i < npol_ ; i++ ) {
[2356]242 *work_p = i ;
243 work_p++ ;
244 }
245 }
[2361]246 Double *sumw_p = new Double[npol_*nchan_] ;
[2356]247 {
248 Double *work_p = sumw_p ;
[2361]249 for ( Int i = 0 ; i < npol_*nchan_ ; i++ ) {
[2356]250 *work_p = 0.0 ;
251 work_p++ ;
252 }
253 }
[2364]254 t0 = mathutil::gettimeofday_sec() ;
255 Int irow = -1 ;
[2356]256 ggridsd( xypos_p,
257 data_p,
[2361]258 &npol_,
259 &nchan_,
[2356]260 &idopsf,
261 flag_p,
262 rflag_p,
263 wgt_p,
[2361]264 &nrow_,
[2356]265 &irow,
266 gdata_p,
267 wdata_p,
[2359]268 &gnx,
269 &gny,
[2361]270 &npol_,
271 &nchan_,
[2356]272 &convSupport_,
273 &convSampling_,
274 conv_p,
275 chanMap,
276 polMap,
277 sumw_p ) ;
[2364]278 t1 = mathutil::gettimeofday_sec() ;
279 os << "ggridsd: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2356]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 ) ;
[2368]291 setData( data_, gdataArr, gwgtArr ) ;
[2361]292 //Matrix<Double> sumWeight( IPosition( 2, npol_, nchan_ ), sumw_p, TAKE_OVER ) ;
293 delete sumw_p ;
[2356]294 //cout << "sumWeight = " << sumWeight << endl ;
[2364]295// os << "gdataArr = " << gdataArr << LogIO::POST ;
296// os << "gwgtArr = " << gwgtArr << LogIO::POST ;
297// os << "data_ " << data_ << LogIO::POST ;
[2356]298}
299
[2368]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
[2356]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 ;
[2359]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
[2356]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
[2362]430void STGrid::selectData( Table &tab )
431{
432 Int ifno = ifno_ ;
433 Table taborg( infile_ ) ;
434 if ( ifno == -1 ) {
[2368]435 LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
[2362]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 }
[2364]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 ) ;
[2362]451 if ( tab.nrow() == 0 ) {
[2368]452 LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
[2362]453 os << LogIO::SEVERE
[2365]454 << "No corresponding rows for given selection: IFNO " << ifno
455 << " SCANNO " << scanlist_
[2362]456 << LogIO::EXCEPTION ;
457 }
458}
459
460void STGrid::getData( Cube<Float> &spectra,
[2356]461 Matrix<Double> &direction,
[2360]462 Cube<uChar> &flagtra,
[2361]463 Matrix<uInt> &rflag,
464 Matrix<Float> &weight )
[2356]465{
[2362]466 Table tab ;
467 selectData( tab ) ;
[2360]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 }
[2361]493 npol_ = pollist_.size() ;
[2360]494 ROArrayColumn<uChar> tmpCol( tab, "FLAGTRA" ) ;
[2361]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++ ) {
[2360]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" ) ;
[2361]511 ROArrayColumn<Float> tsysCol( subt, "TSYS" ) ;
512 ROScalarColumn<Double> tintCol( subt, "INTERVAL" ) ;
[2360]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 ) ;
[2361]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 ) ;
[2360]530 }
[2361]531
532 getWeight( weight, tsys, tint ) ;
[2356]533}
534
[2361]535void STGrid::getWeight( Matrix<Float> &w,
536 Cube<Float> &tsys,
537 Matrix<Double> &tint )
538{
[2368]539 LogIO os( LogOrigin("STGrid","getWeight",WHERE) ) ;
540 double t0, t1 ;
541 t0 = mathutil::gettimeofday_sec() ;
[2361]542 // resize
543 w.resize( nchan_, nrow_ ) ;
544
545 // set weight
546 Bool warn = False ;
547 if ( wtype_.compare( "UNIFORM" ) == 0 ) {
[2368]548 w = 1.0 ;
[2361]549 }
550 else if ( wtype_.compare( "TINT" ) == 0 ) {
551 if ( npol_ > 1 ) warn = True ;
552 for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
553 Float val = mean( tint.column( irow ) ) ;
[2368]554 w.column( irow ) = val ;
[2361]555 }
556 }
557 else if ( wtype_.compare( "TSYS" ) == 0 ) {
558 if ( npol_ > 1 ) warn = True ;
559 for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
560 Matrix<Float> arr = tsys.xyPlane( irow ) ;
561 for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
562 Float val = mean( arr.column( ichan ) ) ;
[2368]563 w(ichan,irow) = 1.0 / ( val * val ) ;
[2361]564 }
565 }
566 }
567 else if ( wtype_.compare( "TINTSYS" ) == 0 ) {
568 if ( npol_ > 1 ) warn = True ;
569 for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
570 Float interval = mean( tint.column( irow ) ) ;
571 Matrix<Float> arr = tsys.xyPlane( irow ) ;
572 for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
573 Float temp = mean( arr.column( ichan ) ) ;
[2368]574 w(ichan,irow) = interval / ( temp * temp ) ;
[2361]575 }
576 }
577 }
578 else {
[2368]579 //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
580 os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
581 w = 1.0 ;
[2361]582 }
583
584 if ( npol_ > 1 ) {
[2368]585 //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
[2362]586 os << LogIO::WARN << "STGrid doesn't support assigning polarization-dependent weight. Use averaged weight over polarization." << LogIO::POST ;
[2361]587 }
[2368]588 t1 = mathutil::gettimeofday_sec() ;
589 os << "getWeight: elapsed time is " << t1-t0 << " sec" << LogIO::POST ;
[2361]590}
591
[2360]592void STGrid::toInt( Array<uChar> *u, Array<Int> *v )
[2356]593{
[2360]594 uInt len = u->nelements() ;
[2356]595 Int *int_p = new Int[len] ;
596 Bool deleteIt ;
[2360]597 const uChar *data_p = u->getStorage( deleteIt ) ;
[2356]598 Int *i_p = int_p ;
599 const uChar *u_p = data_p ;
600 for ( uInt i = 0 ; i < len ; i++ ) {
601 *i_p = ( *u_p == 0 ) ? 0 : 1 ;
602 i_p++ ;
603 u_p++ ;
604 }
[2360]605 u->freeStorage( data_p, deleteIt ) ;
606 v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
[2356]607}
608
[2360]609void STGrid::toInt( Array<uInt> *u, Array<Int> *v )
[2356]610{
[2360]611 uInt len = u->nelements() ;
[2356]612 Int *int_p = new Int[len] ;
613 Bool deleteIt ;
[2360]614 const uInt *data_p = u->getStorage( deleteIt ) ;
[2356]615 Int *i_p = int_p ;
616 const uInt *u_p = data_p ;
617 for ( uInt i = 0 ; i < len ; i++ ) {
618 *i_p = ( *u_p == 0 ) ? 0 : 1 ;
619 i_p++ ;
620 u_p++ ;
621 }
[2360]622 u->freeStorage( data_p, deleteIt ) ;
623 v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
[2356]624}
625
626void STGrid::toPixel( Matrix<Double> &world, Matrix<Double> &pixel )
627{
[2359]628 // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_)
629 // grid plane to avoid unexpected behavior on grid edge
[2356]630 Vector<Double> pixc( 2 ) ;
[2364]631 pixc(0) = Double( nx_-1 ) * 0.5 ;
632 pixc(1) = Double( ny_-1 ) * 0.5 ;
633// pixc(0) = Double( nx_+2*convSupport_-1 ) * 0.5 ;
634// pixc(1) = Double( ny_+2*convSupport_-1 ) * 0.5 ;
[2356]635 uInt nrow = world.shape()[1] ;
636 Vector<Double> cell( 2 ) ;
637 cell(0) = cellx_ ;
638 cell(1) = celly_ ;
639 for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
640 for ( uInt i = 0 ; i < 2 ; i++ ) {
641 pixel( i, irow ) = pixc(i) + ( world(i, irow) - center_(i) ) / cell(i) ;
642 }
643 }
[2364]644// String gridfile = "grid."+convType_+"."+String::toString(convSupport_)+".dat" ;
645// ofstream ofs( gridfile.c_str(), ios::out ) ;
646// ofs << "center " << center_(0) << " " << pixc(0)
647// << " " << center_(1) << " " << pixc(1) << endl ;
648// for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
649// ofs << irow ;
650// for ( uInt i = 0 ; i < 2 ; i++ ) {
651// ofs << " " << world(i, irow) << " " << pixel(i, irow) ;
652// }
653// ofs << endl ;
654// }
655// ofs.close() ;
[2356]656}
657
658void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize )
659{
660 convFunc = 0.0 ;
661 for ( Int i = 0 ; i < convSize/2 ; i++ )
662 convFunc(i) = 1.0 ;
663}
664
665#define NEED_UNDERSCORES
666#if defined(NEED_UNDERSCORES)
667#define grdsf grdsf_
668#endif
669extern "C" {
670 void grdsf(Double*, Double*);
671}
672void STGrid::spheroidalFunc( Vector<Float> &convFunc )
673{
674 convFunc = 0.0 ;
675 for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
676 Double nu = Double(i) / Double(convSupport_*convSampling_) ;
677 Double val ;
678 grdsf( &nu, &val ) ;
679 convFunc(i) = ( 1.0 - nu * nu ) * val ;
680 }
681}
682
683void STGrid::gaussFunc( Vector<Float> &convFunc )
684{
685 convFunc = 0.0 ;
[2363]686 // HWHM of the Gaussian is convSupport_ / 4
687 // To take into account Gaussian tail, kernel cutoff is set to 4 * HWHM
688 Int len = convSampling_ * convSupport_ ;
689 Double hwhm = len * 0.25 ;
690 for ( Int i = 0 ; i < len ; i++ ) {
[2356]691 Double val = Double(i) / hwhm ;
692 convFunc(i) = exp( -log(2)*val*val ) ;
693 }
694}
695
696void STGrid::pbFunc( Vector<Float> &convFunc )
697{
698 convFunc = 0.0 ;
699}
700
701void STGrid::setConvFunc( Vector<Float> &convFunc )
702{
703 convSupport_ = userSupport_ ;
704 if ( convType_ == "BOX" ) {
705 if ( convSupport_ < 0 )
706 convSupport_ = 0 ;
707 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
708 convFunc.resize( convSize ) ;
709 boxFunc( convFunc, convSize ) ;
710 }
711 else if ( convType_ == "SF" ) {
712 if ( convSupport_ < 0 )
713 convSupport_ = 3 ;
714 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
715 convFunc.resize( convSize ) ;
716 spheroidalFunc( convFunc ) ;
717 }
718 else if ( convType_ == "GAUSS" ) {
[2363]719 // to take into account Gaussian tail
[2356]720 if ( convSupport_ < 0 )
[2363]721 convSupport_ = 12 ; // 3 * 4
722 else {
723 convSupport_ = userSupport_ * 4 ;
724 }
[2356]725 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
726 convFunc.resize( convSize ) ;
727 gaussFunc( convFunc ) ;
728 }
[2359]729 else if ( convType_ == "PB" ) {
730 if ( convSupport_ < 0 )
731 convSupport_ = 0 ;
[2356]732 pbFunc( convFunc ) ;
[2359]733 }
[2356]734 else {
735 throw AipsError( "Unsupported convolution function" ) ;
736 }
737}
738
739string STGrid::saveData( string outfile )
740{
[2368]741 LogIO os( LogOrigin("STGrid", "saveData", WHERE) ) ;
742 double t0, t1 ;
743 t0 = mathutil::gettimeofday_sec() ;
744
[2360]745 //Int polno = 0 ;
[2356]746 string outfile_ ;
747 if ( outfile.size() == 0 ) {
748 if ( infile_.lastchar() == '/' ) {
749 outfile_ = infile_.substr( 0, infile_.size()-1 ) ;
750 }
751 else {
752 outfile_ = infile_ ;
753 }
754 outfile_ += ".grid" ;
755 }
756 else {
757 outfile_ = outfile ;
758 }
[2368]759 CountedPtr<Scantable> ref( new Scantable( infile_, Table::Plain ) ) ;
[2356]760 //cout << "ref->nchan()=" << ref->nchan() << endl ;
761 CountedPtr<Scantable> out( new Scantable( *ref, True ) ) ;
762 Table tab = out->table() ;
763 IPosition dshape = data_.shape() ;
[2361]764 Int nrow = nx_ * ny_ * npol_ ;
765 tab.rwKeywordSet().define( "nPol", npol_ ) ;
[2360]766 tab.addRow( nrow ) ;
[2356]767 Vector<Double> cpix( 2 ) ;
768 cpix(0) = Double( nx_ - 1 ) * 0.5 ;
769 cpix(1) = Double( ny_ - 1 ) * 0.5 ;
770 Vector<Double> dir( 2 ) ;
771 ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
772 ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
773 ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
774 Int irow = 0 ;
775 for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
776 for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
[2361]777 for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
[2356]778 IPosition start( 4, ix, iy, ipol, 0 ) ;
[2361]779 IPosition end( 4, ix, iy, ipol, nchan_-1 ) ;
[2356]780 IPosition inc( 4, 1, 1, 1, 1 ) ;
781 Vector<Float> sp = data_( start, end, inc ) ;
782 dir(0) = center_(0) - ( cpix(0) - (Double)ix ) * cellx_ ;
783 dir(1) = center_(1) - ( cpix(1) - (Double)iy ) * celly_ ;
784 spectraCol.put( irow, sp ) ;
785 directionCol.put( irow, dir ) ;
[2360]786 polnoCol.put( irow, pollist_[ipol] ) ;
[2356]787 irow++ ;
788 }
789 }
790 }
791 //cout << "outfile_=" << outfile_ << endl ;
792 out->makePersistent( outfile_ ) ;
[2368]793
794 t1 = mathutil::gettimeofday_sec() ;
795 os << "saveData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2356]796
797 return outfile_ ;
798}
799
800}
Note: See TracBrowser for help on using the repository browser.