source: trunk/src/STGrid.cpp @ 2374

Last change on this file since 2374 was 2374, checked in by Takeshi Nakazato, 12 years ago

New Development: No

JIRA Issue: Yes CAS-2816

Ready for Test: No

Interface Changes: Yes/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...

Load spectra (Float) as Complex at the beggining to save memory usage.


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