source: trunk/src/STGrid.cpp@ 2375

Last change on this file since 2375 was 2375, checked in by Takeshi Nakazato, 14 years ago

New Development: No

JIRA Issue: Yes CAS-2816

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Several bug fixes and updates

  • fixed misunderstanding of array shape when getting data
  • load flag data as Int (originally uChar/uInt) at the beginning of gridding
  • do not plot observed position and grid position by default
  • use Array (instead of Vector/Matrix/Cube) as much as possible


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