source: trunk/src/STGrid.cpp @ 2359

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

  • Disabled debug message to stdout.
  • Total extent of the grid area will be determined to cover whole observed position.
  • number of pixels for ggridsd_ is (nx+convSupport*2,ny+convSupport+2) where (nx,ny) is output grid since ogridsd is defined such that convSupport pixels from the edge are ignored.


File size: 15.9 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/Inputs/Input.h>
10#include <casa/Quanta/Quantum.h>
11#include <casa/Quanta/QuantumHolder.h>
12#include <casa/Utilities/CountedPtr.h>
13
14#include <tables/Tables/Table.h>
15#include <tables/Tables/ScalarColumn.h>
16#include <tables/Tables/ArrayColumn.h>
17
18#include <measures/Measures/MDirection.h>
19
20#include <Scantable.h>
21#include "STGrid.h"
22
23using namespace std ;
24using namespace casa ;
25using namespace asap ;
26
27namespace asap {
28
29// constructor
30STGrid::STGrid()
31{
32  init() ;
33}
34
35STGrid::STGrid( const string infile )
36{
37  init() ;
38
39  setFileIn( infile ) ;
40}
41
42void  STGrid::init()
43{
44  nx_ = -1 ;
45  ny_ = -1 ;
46  cellx_ = 0.0 ;
47  celly_ = 0.0 ;
48  center_ = Vector<Double> ( 2, 0.0 ) ;
49  convType_ = "BOX" ;
50  convSupport_ = -1 ;
51  userSupport_ = -1 ;
52  convSampling_ = 100 ;
53}
54
55void STGrid::setFileIn( const string infile )
56{
57  String name( infile ) ;
58  if ( infile_.compare( name ) != 0 ) {
59    infile_ = String( infile ) ;
60    tab_ = Table( infile_ ) ;
61  }
62}
63
64void STGrid::defineImage( int nx,
65                          int ny,
66                          string scellx,
67                          string scelly,
68                          string scenter )
69{
70  ROArrayColumn<Double> dirCol( tab_, "DIRECTION" ) ;
71  Matrix<Double> direction = dirCol.getColumn() ;
72  Double rmax, rmin, dmax, dmin ;
73  minMax( rmin, rmax, direction.row( 0 ) ) ;
74  minMax( dmin, dmax, direction.row( 1 ) ) ;
75
76  Int npx = (Int)nx ;
77  Int npy = (Int)ny ;
78  String cellx( scellx ) ;
79  String celly( scelly ) ;
80  String center( scenter ) ;
81  setupGrid( npx, npy,
82             cellx, celly,
83             rmin, rmax,
84             dmin, dmax,
85             center ) ;
86}
87 
88void STGrid::setOption( string convType,
89                        int convSupport )
90{
91  convType_ = String( convType ) ;
92  convType_.upcase() ;
93  userSupport_ = (Int)convSupport ;
94}
95
96#define NEED_UNDERSCORES
97#if defined(NEED_UNDERSCORES)
98#define ggridsd ggridsd_
99#endif
100extern "C" {
101   void ggridsd(Double*,
102                const Complex*,
103                Int*,
104                Int*,
105                Int*,
106                const Int*,
107                const Int*,
108                const Float*,
109                Int*,
110                Int*,
111                Complex*,
112                Float*,
113                Int*,
114                Int*,
115                Int *,
116                Int *,
117                Int*,
118                Int*,
119                Float*,
120                Int*,
121                Int*,
122                Double*);
123}
124void STGrid::grid()
125{
126
127  // retrieve data
128  Matrix<Float> spectra ;
129  Matrix<Double> direction ;
130  Matrix<uChar> flagtra ;
131  Vector<uInt> rflag ;
132  getData( infile_, spectra, direction, flagtra, rflag ) ;
133  IPosition sshape = spectra.shape() ;
134  Int nchan = sshape[0] ;
135  Int nrow = sshape[1] ;
136  //cout << "data.shape()=" << data.shape() << endl ;
137
138  // flagtra: uChar -> Int
139  // rflag: uInt -> Int
140  Matrix<Int> flagI ;
141  Vector<Int> rflagI ;
142  toInt( flagtra, flagI ) ;
143  toInt( rflag, rflagI ) ;
144 
145  //cout << "flagI.shape() = " << flagI.shape() << endl ;
146  //cout << "rflagI.shape() = " << rflagI.shape() << endl ;
147 
148  // grid parameter
149//   cout << "----------" << endl ;
150//   cout << "Grid parameter summary" << endl ;
151//   cout << "   (nx,ny) = (" << nx_ << "," << ny_ << ")" << endl ;
152//   cout << "   (cellx,celly) = (" << cellx_ << "," << celly_ << ")" << endl ;
153//   cout << "   center = " << center_ << endl ;
154//   cout << "----------" << endl ;
155
156  // convolution kernel
157  Vector<Float> convFunc ;
158  setConvFunc( convFunc ) ;
159  //cout << "convSupport=" << convSupport_ << endl ;
160  //cout << "convFunc=" << convFunc << endl ;
161
162  // world -> pixel
163  Matrix<Double> xypos( direction.shape(), 0.0 ) ;
164  toPixel( direction, xypos ) ; 
165  //cout << "max(xypos.row(0))=" << max(xypos.row(0)) << endl ;
166  //cout << "min(xypos.row(0))=" << min(xypos.row(0)) << endl ;
167  //cout << "max(xypos.row(1))=" << max(xypos.row(1)) << endl ;
168  //cout << "min(xypos.row(1))=" << min(xypos.row(1)) << endl ;
169//   for ( Int irow = 0 ; irow < nrow ; irow++ ) {
170//     if ( spectra.column( irow )(0) > 0.0 ) {
171//       cout << irow << ": xypos=" << xypos.column( irow )
172//            << " data = " << spectra.column( irow ) << endl ;
173//     }
174//   }
175 
176  // weighting factor
177  Matrix<Float> weight( IPosition( 2, nchan, nrow ), 1.0 ) ;
178
179  // call ggridsd
180  Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ;
181  Double *xypos_p = xypos.getStorage( deletePos ) ;
182  Matrix<Complex> dataC( spectra.shape(), 0.0 ) ;
183  setReal( dataC, spectra ) ;
184  const Complex *data_p = dataC.getStorage( deleteData ) ;
185  const Float *wgt_p = weight.getStorage( deleteWgt ) ;
186  const Int *flag_p = flagI.getStorage( deleteFlag ) ;
187  const Int *rflag_p = rflagI.getStorage( deleteFlagR ) ;
188  Float *conv_p = convFunc.getStorage( deleteConv ) ;
189  Int npol = 1 ;
190  // Extend grid plane with convSupport_
191  //IPosition gshape( 4, nx_, ny_, npol, nchan ) ;
192  Int gnx = nx_+convSupport_*2 ;
193  Int gny = ny_+convSupport_*2 ;
194  IPosition gshape( 4, gnx, gny, npol, nchan ) ;
195  Array<Complex> gdataArrC( gshape, 0.0 ) ;
196  Array<Float> gwgtArr( gshape, 0.0 ) ;
197  Complex *gdata_p = gdataArrC.getStorage( deleteDataG ) ;
198  Float *wdata_p = gwgtArr.getStorage( deleteWgtG ) ;
199  Int idopsf = 0 ;
200  Int irow = -1 ;
201  Int *chanMap = new Int[nchan] ;
202  {
203    Int *work_p = chanMap ;
204    for ( Int i = 0 ; i < nchan ; i++ ) {
205      *work_p = i ;
206      work_p++ ;
207    }
208  }
209  Int *polMap = new Int[npol] ;
210  {
211    Int *work_p = polMap ;
212    for ( Int i = 0 ; i < npol ; i++ ) {
213      *work_p = i ;
214      work_p++ ;
215    }
216  }
217  Double *sumw_p = new Double[npol*nchan] ;
218  {
219    Double *work_p = sumw_p ;
220    for ( Int i = 0 ; i < npol*nchan ; i++ ) {
221      *work_p = 0.0 ;
222      work_p++ ;
223    }
224  }
225  ggridsd( xypos_p,
226           data_p,
227           &npol,
228           &nchan,
229           &idopsf,
230           flag_p,
231           rflag_p,
232           wgt_p,
233           &nrow,
234           &irow,
235           gdata_p,
236           wdata_p,
237           //&nx_,
238           //&ny_,
239           &gnx,
240           &gny,
241           &npol,
242           &nchan,
243           &convSupport_,
244           &convSampling_,
245           conv_p,
246           chanMap,
247           polMap,
248           sumw_p ) ;
249  xypos.putStorage( xypos_p, deletePos ) ;
250  dataC.freeStorage( data_p, deleteData ) ;
251  weight.freeStorage( wgt_p, deleteWgt ) ;
252  flagI.freeStorage( flag_p, deleteFlag ) ;
253  rflagI.freeStorage( rflag_p, deleteFlagR ) ;
254  convFunc.putStorage( conv_p, deleteConv ) ;
255  delete polMap ;
256  delete chanMap ;
257  gdataArrC.putStorage( gdata_p, deleteDataG ) ;
258  gwgtArr.putStorage( wdata_p, deleteWgtG ) ;
259  Array<Float> gdataArr = real( gdataArrC ) ;
260  data_.resize( gdataArr.shape() ) ;
261  data_ = 0.0 ;
262  for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
263    for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
264      for ( Int ip = 0 ; ip < npol ; ip++ ) {
265        for ( Int ic = 0 ; ic < nchan ; ic++ ) {
266          IPosition pos( 4, ix, iy, ip, ic ) ;
267          //if ( gwgtArr( pos ) > 0.0 )
268          //  data_( pos ) = gdataArr( pos ) / gwgtArr( pos ) ;
269          IPosition gpos( 4, ix+convSupport_, iy+convSupport_, ip, ic ) ;
270          if ( gwgtArr( gpos ) > 0.0 )
271            data_( pos ) = gdataArr( gpos ) / gwgtArr( gpos ) ;
272        }
273      }
274    }
275  }
276  Matrix<Double> sumWeight( IPosition( 2, npol, nchan ), sumw_p, TAKE_OVER ) ;
277  //cout << "sumWeight = " << sumWeight << endl ;
278  //cout << "gdataArr = " << gdataArr << endl ;
279  //cout << "gwgtArr = " << gwgtArr << endl ;
280  //cout << "data_ " << data_ << endl ;
281}
282
283void STGrid::setupGrid( Int &nx,
284                        Int &ny,
285                        String &cellx,
286                        String &celly,
287                        Double &xmin,
288                        Double &xmax,
289                        Double &ymin,
290                        Double &ymax,
291                        String &center )
292{
293  //cout << "nx=" << nx << ", ny=" << ny << endl ;
294
295  // center position
296  if ( center.size() == 0 ) {
297    center_(0) = 0.5 * ( xmin + xmax ) ;
298    center_(1) = 0.5 * ( ymin + ymax ) ;
299  }
300  else {
301    String::size_type pos0 = center.find( " " ) ;
302    if ( pos0 == String::npos ) {
303      throw AipsError( "bad string format in parameter center" ) ;
304    }
305    String::size_type pos1 = center.find( " ", pos0+1 ) ;
306    String typestr, xstr, ystr ;
307    if ( pos1 != String::npos ) {
308      typestr = center.substr( 0, pos0 ) ;
309      xstr = center.substr( pos0+1, pos1-pos0 ) ;
310      ystr = center.substr( pos1+1 ) ;
311      // todo: convert to J2000 (or direction ref for DIRECTION column)
312    }
313    else {
314      typestr = "J2000" ;
315      xstr = center.substr( 0, pos0 ) ;
316      ystr = center.substr( pos0+1 ) ;
317    }
318    QuantumHolder qh ;
319    String err ;
320    qh.fromString( err, xstr ) ;
321    Quantum<Double> xcen = qh.asQuantumDouble() ;
322    qh.fromString( err, ystr ) ;
323    Quantum<Double> ycen = qh.asQuantumDouble() ;
324    center_(0) = xcen.getValue( "rad" ) ;
325    center_(1) = ycen.getValue( "rad" ) ;
326  }
327
328
329  //Double wx = xmax - xmin ;
330  //Double wy = ymax - ymin ;
331  Double wx = max( abs(xmax-center_(0)), abs(xmin-center_(0)) ) * 2 ;
332  Double wy = max( abs(ymax-center_(1)), abs(ymin-center_(1)) ) * 2 ;
333  // take 10% margin
334  wx *= 1.10 ;
335  wy *= 1.10 ;
336  Quantum<Double> qcellx ;
337  Quantum<Double> qcelly ;
338  nx_ = nx ;
339  ny_ = ny ;
340  if ( nx < 0 && ny > 0 ) {
341    nx_ = ny ;
342    ny_ = ny ;
343  }
344  if ( ny < 0 && nx > 0 ) {
345    nx_ = nx ;
346    ny_ = nx ;
347  }
348  //cout << "nx_ = " << nx_ << ",  ny_ = " << ny_ << endl ;
349  if ( cellx.size() != 0 && celly.size() != 0 ) {
350    readQuantity( qcellx, cellx ) ;
351    readQuantity( qcelly, celly ) ;
352  }
353  else if ( celly.size() != 0 ) {
354    cout << "Using celly to x-axis..." << endl ;
355    readQuantity( qcelly, celly ) ;
356    qcellx = qcelly ;
357  }
358  else if ( cellx.size() != 0 ) {
359    cout << "Using cellx to y-axis..." << endl ;
360    readQuantity( qcellx, cellx ) ;
361    qcelly = qcellx ;
362  }
363  else {
364    if ( nx_ < 0 ) {
365      cout << "No user preference in grid setting. Using default..." << endl ;
366      readQuantity( qcellx, "1.0arcmin" ) ;
367      qcelly = qcellx ;
368    }
369    else {
370      qcellx = Quantum<Double>( wx/nx_, "rad" ) ;
371      qcelly = Quantum<Double>( wy/ny_, "rad" ) ;
372    }
373  }
374  cellx_ = qcellx.getValue( "rad" ) ;
375  celly_ = qcelly.getValue( "rad" ) ;
376  if ( nx_ < 0 ) {
377    nx_ = Int( ceil( wx/cellx_ ) ) ;
378    ny_ = Int( ceil( wy/celly_ ) ) ;
379  }
380}
381
382void STGrid::getData( String &infile,
383                      Matrix<Float> &spectra,
384                      Matrix<Double> &direction,
385                      Matrix<uChar> &flagtra,
386                      Vector<uInt> &rflag )
387{
388  Table tab( infile ) ;
389  ROArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
390  ROArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
391  ROArrayColumn<uChar> flagtraCol( tab, "FLAGTRA" ) ;
392  ROScalarColumn<uInt> rflagCol( tab, "FLAGROW" ) ;
393  spectraCol.getColumn( spectra ) ;
394  directionCol.getColumn( direction ) ;
395  flagtraCol.getColumn( flagtra ) ;
396  rflagCol.getColumn( rflag ) ;
397}
398
399void STGrid::toInt( Matrix<uChar> &u, Matrix<Int> &v )
400{
401  uInt len = u.nelements() ;
402  Int *int_p = new Int[len] ;
403  Bool deleteIt ;
404  const uChar *data_p = u.getStorage( deleteIt ) ;
405  Int *i_p = int_p ;
406  const uChar *u_p = data_p ;
407  for ( uInt i = 0 ; i < len ; i++ ) {
408    *i_p = ( *u_p == 0 ) ? 0 : 1 ;
409    i_p++ ;
410    u_p++ ;
411  }
412  u.freeStorage( data_p, deleteIt ) ;
413  v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
414}
415
416void STGrid::toInt( Vector<uInt> &u, Vector<Int> &v )
417{
418  uInt len = u.nelements() ;
419  Int *int_p = new Int[len] ;
420  Bool deleteIt ;
421  const uInt *data_p = u.getStorage( deleteIt ) ;
422  Int *i_p = int_p ;
423  const uInt *u_p = data_p ;
424  for ( uInt i = 0 ; i < len ; i++ ) {
425    *i_p = ( *u_p == 0 ) ? 0 : 1 ;
426    i_p++ ;
427    u_p++ ;
428  }
429  u.freeStorage( data_p, deleteIt ) ;
430  v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
431}
432
433void STGrid::toPixel( Matrix<Double> &world, Matrix<Double> &pixel )
434{
435  // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_)
436  // grid plane to avoid unexpected behavior on grid edge
437  Vector<Double> pixc( 2 ) ;
438  //pixc(0) = Double( nx_-1 ) * 0.5 ;
439  //pixc(1) = Double( ny_-1 ) * 0.5 ;
440  pixc(0) = Double( nx_+2*convSupport_-1 ) * 0.5 ;
441  pixc(1) = Double( ny_+2*convSupport_-1 ) * 0.5 ;
442  uInt nrow = world.shape()[1] ;
443  Vector<Double> cell( 2 ) ;
444  cell(0) = cellx_ ;
445  cell(1) = celly_ ;
446  //ofstream ofs( "grid.dat", ios::out ) ;
447  for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
448    //ofs << irow ;
449    for ( uInt i = 0 ; i < 2 ; i++ ) {
450      pixel( i, irow ) = pixc(i) + ( world(i, irow) - center_(i) ) / cell(i) ;
451      //ofs << " " << world(i, irow) << " " << pixel(i, irow) ;
452    }
453    //ofs << endl ;
454  }
455  //ofs.close() ;
456}
457
458void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize )
459{
460  convFunc = 0.0 ;
461  for ( Int i = 0 ; i < convSize/2 ; i++ )
462    convFunc(i) = 1.0 ;
463}
464
465#define NEED_UNDERSCORES
466#if defined(NEED_UNDERSCORES)
467#define grdsf grdsf_
468#endif
469extern "C" {
470   void grdsf(Double*, Double*);
471}
472void STGrid::spheroidalFunc( Vector<Float> &convFunc )
473{
474  convFunc = 0.0 ;
475  for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
476    Double nu = Double(i) / Double(convSupport_*convSampling_) ;
477    Double val ;
478    grdsf( &nu, &val ) ;
479    convFunc(i) = ( 1.0 - nu * nu ) * val ;
480  }
481}
482
483void STGrid::gaussFunc( Vector<Float> &convFunc )
484{
485  convFunc = 0.0 ;
486  for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
487    Double hwhm = convSampling_ * convSupport_ * 0.25 ;
488    Double val = Double(i) / hwhm ;
489    convFunc(i) = exp( -log(2)*val*val ) ;
490  }
491}
492
493void STGrid::pbFunc( Vector<Float> &convFunc )
494{
495  convFunc = 0.0 ;
496}
497
498void STGrid::setConvFunc( Vector<Float> &convFunc )
499{
500  convSupport_ = userSupport_ ;
501  if ( convType_ == "BOX" ) {
502    if ( convSupport_ < 0 )
503      convSupport_ = 0 ;
504    Int convSize = convSampling_ * ( 2 * convSupport_ + 2 )  ;
505    convFunc.resize( convSize ) ;
506    boxFunc( convFunc, convSize ) ;
507  }
508  else if ( convType_ == "SF" ) {
509    if ( convSupport_ < 0 )
510      convSupport_ = 3 ;
511    Int convSize = convSampling_ * ( 2 * convSupport_ + 2 )  ;
512    convFunc.resize( convSize ) ;
513    spheroidalFunc( convFunc ) ;
514  }
515  else if ( convType_ == "GAUSS" ) {
516    if ( convSupport_ < 0 )
517      convSupport_ = 3 ;
518    Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
519    convFunc.resize( convSize ) ;
520    gaussFunc( convFunc ) ;
521  }
522  else if ( convType_ == "PB" ) {
523    if ( convSupport_ < 0 )
524      convSupport_ = 0 ;
525    pbFunc( convFunc ) ;
526  }
527  else {
528    throw AipsError( "Unsupported convolution function" ) ;
529  }
530}
531
532string STGrid::saveData( string outfile )
533{
534  Int polno = 0 ;
535  string outfile_ ;
536  if ( outfile.size() == 0 ) {
537    if ( infile_.lastchar() == '/' ) {
538      outfile_ = infile_.substr( 0, infile_.size()-1 ) ;
539    }
540    else {
541      outfile_ = infile_ ;
542    }
543    outfile_ += ".grid" ;
544  }
545  else {
546    outfile_ = outfile ;
547  }
548  CountedPtr<Scantable> ref( new Scantable( infile_, Table::Memory ) ) ;
549  //cout << "ref->nchan()=" << ref->nchan() << endl ;
550  CountedPtr<Scantable> out( new Scantable( *ref, True ) ) ;
551  Table tab = out->table() ;
552  Int nrow = nx_ * ny_ ;
553  tab.addRow( nrow ) ;
554  IPosition dshape = data_.shape() ;
555  Int npol = dshape[2] ;
556  Int nchan = dshape[3] ;
557  Vector<Double> cpix( 2 ) ;
558  cpix(0) = Double( nx_ - 1 ) * 0.5 ;
559  cpix(1) = Double( ny_ - 1 ) * 0.5 ;
560  Vector<Double> dir( 2 ) ;
561  ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
562  ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
563  ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
564  Int irow = 0 ;
565  for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
566    for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
567      for ( Int ipol = 0 ; ipol < npol ; ipol++ ) {
568        IPosition start( 4, ix, iy, ipol, 0 ) ;
569        IPosition end( 4, ix, iy, ipol, nchan-1 ) ;
570        IPosition inc( 4, 1, 1, 1, 1 ) ;
571        Vector<Float> sp = data_( start, end, inc ) ;
572        dir(0) = center_(0) - ( cpix(0) - (Double)ix ) * cellx_ ;
573        dir(1) = center_(1) - ( cpix(1) - (Double)iy ) * celly_ ;
574        spectraCol.put( irow, sp ) ;
575        directionCol.put( irow, dir ) ;
576        polnoCol.put( irow, polno ) ;
577        irow++ ;
578      }
579    }
580  }
581  //cout << "outfile_=" << outfile_ << endl ;
582  out->makePersistent( outfile_ ) ;
583 
584  return outfile_ ;
585}
586
587}
Note: See TracBrowser for help on using the repository browser.