source: trunk/src/STGrid.cpp@ 2384

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

Updated call_ggridsd to be easy to call.


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