source: trunk/src/STGrid.cpp @ 2376

Last change on this file since 2376 was 2376, checked in by Takeshi Nakazato, 12 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...

Sort table before process.


File size: 27.3 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  Block<String> cols( 3 ) ;
460  cols[0] = "TIME" ;
461  cols[1] = "BEAMNO" ;
462  cols[2] = "POLNO" ;
463  Block<Int> order( 3, Sort::Ascending ) ;
464  tab = taborg( node ).sort( cols, order ) ;
465  if ( tab.nrow() == 0 ) {
466    LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
467    os << LogIO::SEVERE
468       << "No corresponding rows for given selection: IFNO " << ifno ;
469    if ( scanlist_.size() > 0 )
470      os << " SCANNO " << scanlist_ ;
471    os << LogIO::EXCEPTION ;
472  }
473}
474
475void STGrid::getData( Array<Complex> &spectra,
476                      Array<Double> &direction,
477                      Array<uChar> &flagtra,
478                      Array<uInt> &rflag,
479                      Array<Float> &weight )
480{
481  LogIO os( LogOrigin("STGrid","getData",WHERE) ) ;
482//   os << "start" << LogIO::POST ;
483  Table tab ;
484  selectData( tab ) ;
485  setupArray( tab ) ;
486//   os << "npol_ = " << npol_ << LogIO::POST ;
487//   os << "nchan_ = " << nchan_ << LogIO::POST ;
488//   os << "nrow_ = " << nrow_ << LogIO::POST ;
489  IPosition cshape( 3, npol_, nchan_, nrow_ ) ;
490  IPosition mshape( 2, npol_, nrow_ ) ;
491  spectra.resize( cshape ) ;
492  flagtra.resize( cshape ) ;
493  rflag.resize( mshape ) ;
494  direction.resize( IPosition(2,2,nrow_) ) ;
495  Array<Float> tsys( cshape ) ;
496  Array<Double> tint( mshape ) ;
497
498  ArrayIterator<uChar> fli( flagtra, IPosition(2,1,2) ) ;
499  ArrayIterator<uInt> fri( rflag, IPosition(1,1) ) ;
500  ArrayIterator<Float> tsi( tsys, IPosition(2,1,2) ) ;
501  ArrayIterator<Double> tii( tint, IPosition(1,1) ) ;
502 
503  // boolean for pointer access
504  Bool bsp, bsps ;
505  // pointer to the data
506  Complex *sp_p = spectra.getStorage( bsp ) ;
507  // working pointer
508  Complex *wsp_p = sp_p ;
509  uInt len = nchan_ * nrow_ ;
510  IPosition mshape2( 2, nchan_, nrow_ ) ;
511  IPosition vshape( 1, nrow_ ) ;
512  Vector<Float> spSlice( nchan_ ) ;
513  const Float *sps_p = spSlice.getStorage( bsps ) ;
514  long cincr = npol_ ;
515  long rincr = npol_ * nchan_ ;
516  for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
517    Table subt = tab( tab.col("POLNO") == pollist_[ipol] ) ;
518    ROArrayColumn<Float> spectraCol( subt, "SPECTRA" ) ;
519    ROArrayColumn<Double> directionCol( subt, "DIRECTION" ) ;
520    ROArrayColumn<uChar> flagtraCol( subt, "FLAGTRA" ) ;
521    ROScalarColumn<uInt> rflagCol( subt, "FLAGROW" ) ;
522    ROArrayColumn<Float> tsysCol( subt, "TSYS" ) ;
523    ROScalarColumn<Double> tintCol( subt, "INTERVAL" ) ;
524    for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
525      spectraCol.get( irow, spSlice ) ;
526      const Float *wsps_p = sps_p ;
527      wsp_p = sp_p + (long)ipol + rincr * (long)irow ;
528      for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
529        *wsp_p = *wsps_p ;
530        wsps_p++ ;
531        wsp_p += cincr ;
532      }
533    }
534    Array<uChar> flSlice = fli.array() ;
535    Vector<uInt> frSlice = fri.array() ;
536    flagtraCol.getColumn( flSlice ) ;
537    rflagCol.getColumn( frSlice ) ;
538    if ( ipol == 0 )
539      directionCol.getColumn( direction ) ;
540    Vector<Float> tmpF = tsysCol( 0 ) ;
541    Array<Float> tsSlice = tsi.array() ;
542    if ( tmpF.nelements() == (uInt)nchan_ ) {
543      tsysCol.getColumn( tsSlice ) ;
544    }
545    else {
546      tsSlice = tmpF( 0 ) ;
547    }
548    Vector<Double> tmpD = tii.array() ;
549    tintCol.getColumn( tmpD ) ;
550
551    wsp_p += len ;
552
553    fli.next() ;
554    fri.next() ;
555    tsi.next() ;
556    tii.next() ;
557  }
558  spSlice.freeStorage( sps_p, bsps ) ;
559  spectra.putStorage( sp_p, bsp ) ;
560
561//   os << "spectra=" << spectra << LogIO::POST ;
562//   os << "flagtra=" << flagtra << LogIO::POST ;
563//   os << "rflag=" << rflag << LogIO::POST ;
564//   os << "direction=" << direction << LogIO::POST ;
565
566  getWeight( weight, tsys, tint ) ;
567}
568
569void STGrid::getData( Array<Complex> &spectra,
570                      Array<Double> &direction,
571                      Array<Int> &flagtra,
572                      Array<Int> &rflag,
573                      Array<Float> &weight )
574{
575  LogIO os( LogOrigin("STGrid","getData",WHERE) ) ;
576  double t0, t1 ;
577
578  Array<uChar> flagUC ;
579  Array<uInt> rflagUI ;
580  getData( spectra, direction, flagUC, rflagUI, weight ) ;
581
582  t0 = mathutil::gettimeofday_sec() ;
583  toInt( flagUC, flagtra ) ;
584  toInt( rflagUI, rflag ) ;
585  t1 = mathutil::gettimeofday_sec() ;
586  os << "toInt: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
587}
588
589void STGrid::setupArray( Table &tab )
590{
591  LogIO os( LogOrigin("STGrid","setupArray",WHERE) ) ;
592  ROScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
593  Vector<uInt> pols = polnoCol.getColumn() ;
594  //os << pols << LogIO::POST ;
595  Vector<uInt> pollistOrg ;
596  uInt npolOrg = 0 ;
597  uInt polno ;
598  for ( uInt i = 0 ; i < polnoCol.nrow() ; i++ ) {
599    //polno = polnoCol( i ) ;
600    polno = pols( i ) ;
601    if ( allNE( pollistOrg, polno ) ) {
602      pollistOrg.resize( npolOrg+1, True ) ;
603      pollistOrg[npolOrg] = polno ;
604      npolOrg++ ;
605    }
606  }
607  if ( pollist_.size() == 0 )
608    pollist_ = pollistOrg ;
609  else {
610    Vector<uInt> newlist ;
611    uInt newsize = 0 ;
612    for ( uInt i = 0 ; i < pollist_.size() ; i++ ) {
613      if ( anyEQ( pollistOrg, pollist_[i] ) ) {
614        newlist.resize( newsize+1, True ) ;
615        newlist[newsize] = pollist_[i] ;
616        newsize++ ;
617      }
618    }
619    pollist_.assign( newlist ) ;
620  }
621  npol_ = pollist_.size() ;
622  if ( npol_ == 0 ) {
623    os << LogIO::SEVERE << "Empty pollist" << LogIO::EXCEPTION ;
624  }
625  nrow_ = tab.nrow() / npolOrg ;
626  ROArrayColumn<uChar> tmpCol( tab, "FLAGTRA" ) ;
627  nchan_ = tmpCol( 0 ).nelements() ;
628//   os << "npol_ = " << npol_ << "(" << pollist_ << ")" << endl
629//      << "nchan_ = " << nchan_ << endl
630//      << "nrow_ = " << nrow_ << LogIO::POST ;
631}
632
633void STGrid::getWeight( Array<Float> &w,
634                        Array<Float> &tsys,
635                        Array<Double> &tint )
636{
637  LogIO os( LogOrigin("STGrid","getWeight",WHERE) ) ;
638  double t0, t1 ;
639  t0 = mathutil::gettimeofday_sec() ;
640  // resize
641  IPosition refShape = tsys.shape() ;
642  Int nchan = refShape[1] ;
643  Int nrow = refShape[2] ;
644  w.resize( IPosition(2,nchan,nrow) ) ;
645
646  // set weight
647  Bool warn = False ;
648  if ( wtype_.compare( "UNIFORM" ) == 0 ) {
649    w = 1.0 ;
650  }
651  else if ( wtype_.compare( "TINT" ) == 0 ) {
652    if ( npol_ > 1 ) warn = True ;
653    Bool b0, b1 ;
654    Float *w_p = w.getStorage( b0 ) ;
655    Float *w0_p = w_p ;
656    const Double *ti_p = tint.getStorage( b1 ) ;
657    const Double *w1_p = ti_p ;
658    for ( Int irow = 0 ; irow < nrow ; irow++ ) {
659      Float val = (Float)(polMean( w1_p )) ;
660      for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
661        *w0_p = val ;
662        w0_p++ ;
663      }
664    }
665    w.putStorage( w_p, b0 ) ;
666    tint.freeStorage( ti_p, b1 ) ;
667  }
668  else if ( wtype_.compare( "TSYS" ) == 0 ) {
669    if ( npol_ > 1 ) warn = True ;
670    Bool b0, b1 ;
671    Float *w_p = w.getStorage( b0 ) ;
672    Float *w0_p = w_p ;
673    const Float *ts_p = tsys.getStorage( b1 ) ;
674    const Float *w1_p = ts_p ;
675    for ( Int irow = 0 ; irow < nrow ; irow++ ) {
676      for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
677        Float val = polMean( w1_p ) ;
678        *w0_p = 1.0 / ( val * val ) ;
679        w0_p++ ;
680      }
681    }
682    w.putStorage( w_p, b0 ) ;
683    tsys.freeStorage( ts_p, b1 ) ;
684  }
685  else if ( wtype_.compare( "TINTSYS" ) == 0 ) {
686    if ( npol_ > 1 ) warn = True ;
687    Bool b0, b1, b2 ;
688    Float *w_p = w.getStorage( b0 ) ;
689    Float *w0_p = w_p ;
690    const Double *ti_p = tint.getStorage( b1 ) ;
691    const Double *w1_p = ti_p ;
692    const Float *ts_p = tsys.getStorage( b2 ) ;
693    const Float *w2_p = ts_p ;
694    for ( Int irow = 0 ; irow < nrow ; irow++ ) {
695      Float interval = (Float)(polMean( w1_p )) ;
696      for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
697        Float temp = polMean( w2_p ) ;
698        *w0_p = interval / ( temp * temp ) ;
699        w0_p++ ;
700      }
701    }
702    w.putStorage( w_p, b0 ) ;
703    tint.freeStorage( ti_p, b1 ) ;
704    tsys.freeStorage( ts_p, b2 ) ;
705  }
706  else {
707    //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
708    os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
709    w = 1.0 ;
710  }
711
712  if ( npol_ > 1 ) {
713    //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
714    os << LogIO::WARN << "STGrid doesn't support assigning polarization-dependent weight. Use averaged weight over polarization." << LogIO::POST ;
715  }
716  t1 = mathutil::gettimeofday_sec() ;
717  os << "getWeight: elapsed time is " << t1-t0 << " sec" << LogIO::POST ;
718}
719
720Float STGrid::polMean( const Float *p )
721{
722  Float v = 0.0 ;
723  for ( Int i = 0 ; i < npol_ ; i++ ) {
724    v += *p ;
725    p++ ;
726  }
727  v /= npol_ ;
728  return v ;
729}
730
731Double STGrid::polMean( const Double *p )
732{
733  Double v = 0.0 ;
734  for ( Int i = 0 ; i < npol_ ; i++ ) {
735    v += *p ;
736    p++ ;
737  }
738  v /= npol_ ;
739  return v ;
740}
741
742void STGrid::toInt( Array<uChar> &u, Array<Int> &v )
743{
744  uInt len = u.nelements() ;
745  Int *int_p = new Int[len] ;
746  Bool deleteIt ;
747  const uChar *data_p = u.getStorage( deleteIt ) ;
748  Int *i_p = int_p ;
749  const uChar *u_p = data_p ;
750  for ( uInt i = 0 ; i < len ; i++ ) {
751    *i_p = ( *u_p == 0 ) ? 0 : 1 ;
752    i_p++ ;
753    u_p++ ;
754  }
755  u.freeStorage( data_p, deleteIt ) ;
756  v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
757}
758
759void STGrid::toInt( Array<uInt> &u, Array<Int> &v )
760{
761  uInt len = u.nelements() ;
762  Int *int_p = new Int[len] ;
763  Bool deleteIt ;
764  const uInt *data_p = u.getStorage( deleteIt ) ;
765  Int *i_p = int_p ;
766  const uInt *u_p = data_p ;
767  for ( uInt i = 0 ; i < len ; i++ ) {
768    *i_p = ( *u_p == 0 ) ? 0 : 1 ;
769    i_p++ ;
770    u_p++ ;
771  }
772  u.freeStorage( data_p, deleteIt ) ;
773  v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
774}
775
776void STGrid::toPixel( Array<Double> &world, Array<Double> &pixel )
777{
778  // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_)
779  // grid plane to avoid unexpected behavior on grid edge
780  Block<Double> pixc( 2 ) ;
781  pixc[0] = Double( nx_-1 ) * 0.5 ;
782  pixc[1] = Double( ny_-1 ) * 0.5 ;
783//   pixc[0] = Double( nx_+2*convSupport_-1 ) * 0.5 ;
784//   pixc[1] = Double( ny_+2*convSupport_-1 ) * 0.5 ;
785  uInt nrow = world.shape()[1] ;
786  Bool bw, bp ;
787  const Double *w_p = world.getStorage( bw ) ;
788  Double *p_p = pixel.getStorage( bp ) ;
789  const Double *ww_p = w_p ;
790  Double *wp_p = p_p ;
791  for ( uInt i = 0 ; i < nrow ; i++ ) {
792    *wp_p = pixc[0] + ( *ww_p - center_[0] ) / cellx_ ;
793    wp_p++ ;
794    ww_p++ ;
795    *wp_p = pixc[1] + ( *ww_p - center_[1] ) / celly_ ;
796    wp_p++ ;
797    ww_p++ ;
798  }
799  world.freeStorage( w_p, bw ) ;
800  pixel.putStorage( p_p, bp ) ; 
801//   String gridfile = "grid."+convType_+"."+String::toString(convSupport_)+".dat" ;
802//   ofstream ofs( gridfile.c_str(), ios::out ) ;
803//   ofs << "center " << center_(0) << " " << pixc(0)
804//       << " " << center_(1) << " " << pixc(1) << endl ;
805//   for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
806//     ofs << irow ;
807//     for ( uInt i = 0 ; i < 2 ; i++ ) {
808//       ofs << " " << world(i, irow) << " " << pixel(i, irow) ;
809//     }
810//     ofs << endl ;
811//   }
812//   ofs.close() ;
813}
814
815void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize )
816{
817  convFunc = 0.0 ;
818  for ( Int i = 0 ; i < convSize/2 ; i++ )
819    convFunc(i) = 1.0 ;
820}
821
822#define NEED_UNDERSCORES
823#if defined(NEED_UNDERSCORES)
824#define grdsf grdsf_
825#endif
826extern "C" {
827   void grdsf(Double*, Double*);
828}
829void STGrid::spheroidalFunc( Vector<Float> &convFunc )
830{
831  convFunc = 0.0 ;
832  for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
833    Double nu = Double(i) / Double(convSupport_*convSampling_) ;
834    Double val ;
835    grdsf( &nu, &val ) ;
836    convFunc(i) = ( 1.0 - nu * nu ) * val ;
837  }
838}
839
840void STGrid::gaussFunc( Vector<Float> &convFunc )
841{
842  convFunc = 0.0 ;
843  // HWHM of the Gaussian is convSupport_ / 4
844  // To take into account Gaussian tail, kernel cutoff is set to 4 * HWHM
845  Int len = convSampling_ * convSupport_ ;
846  Double hwhm = len * 0.25 ;
847  for ( Int i = 0 ; i < len ; i++ ) {
848    Double val = Double(i) / hwhm ;
849    convFunc(i) = exp( -log(2)*val*val ) ;
850  }
851}
852
853void STGrid::pbFunc( Vector<Float> &convFunc )
854{
855  convFunc = 0.0 ;
856}
857
858void STGrid::setConvFunc( Vector<Float> &convFunc )
859{
860  convSupport_ = userSupport_ ;
861  if ( convType_ == "BOX" ) {
862    if ( convSupport_ < 0 )
863      convSupport_ = 0 ;
864    Int convSize = convSampling_ * ( 2 * convSupport_ + 2 )  ;
865    convFunc.resize( convSize ) ;
866    boxFunc( convFunc, convSize ) ;
867  }
868  else if ( convType_ == "SF" ) {
869    if ( convSupport_ < 0 )
870      convSupport_ = 3 ;
871    Int convSize = convSampling_ * ( 2 * convSupport_ + 2 )  ;
872    convFunc.resize( convSize ) ;
873    spheroidalFunc( convFunc ) ;
874  }
875  else if ( convType_ == "GAUSS" ) {
876    // to take into account Gaussian tail
877    if ( convSupport_ < 0 )
878      convSupport_ = 12 ; // 3 * 4
879    else {
880      convSupport_ = userSupport_ * 4 ;
881    }
882    Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
883    convFunc.resize( convSize ) ;
884    gaussFunc( convFunc ) ;
885  }
886  else if ( convType_ == "PB" ) {
887    if ( convSupport_ < 0 )
888      convSupport_ = 0 ;
889    pbFunc( convFunc ) ;
890  }
891  else {
892    throw AipsError( "Unsupported convolution function" ) ;
893  }
894}
895
896string STGrid::saveData( string outfile )
897{
898  LogIO os( LogOrigin("STGrid", "saveData", WHERE) ) ;
899  double t0, t1 ;
900  t0 = mathutil::gettimeofday_sec() ;
901
902  //Int polno = 0 ;
903  String outfile_ ;
904  if ( outfile.size() == 0 ) {
905    if ( infile_.lastchar() == '/' ) {
906      outfile_ = infile_.substr( 0, infile_.size()-1 ) ;
907    }
908    else {
909      outfile_ = infile_ ;
910    }
911    outfile_ += ".grid" ;
912  }
913  else {
914    outfile_ = outfile ;
915  }
916  Table tab ;
917  prepareTable( tab, outfile_ ) ;
918  IPosition dshape = data_.shape() ;
919  Int nrow = nx_ * ny_ * npol_ ;
920  tab.rwKeywordSet().define( "nPol", npol_ ) ;
921  tab.addRow( nrow ) ;
922  Vector<Double> cpix( 2 ) ;
923  cpix(0) = Double( nx_ - 1 ) * 0.5 ;
924  cpix(1) = Double( ny_ - 1 ) * 0.5 ;
925  Vector<Double> dir( 2 ) ;
926  ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
927  ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
928  ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
929  Int irow = 0 ;
930  Vector<Float> sp( nchan_ ) ;
931  Bool bsp, bdata ;
932  const Float *data_p = data_.getStorage( bdata ) ;
933  Float *wsp_p, *sp_p ;
934  const Float *wdata_p = data_p ;
935  long step = nx_ * ny_ * npol_ ;
936  long offset ;
937  for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
938    dir(1) = center_(1) - ( cpix(1) - (Double)iy ) * celly_ ;
939    for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
940      dir(0) = center_(0) - ( cpix(0) - (Double)ix ) * cellx_ ;
941      for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
942        offset = ix + iy * nx_ + ipol * nx_ * ny_ ;
943        //os << "offset = " << offset << LogIO::POST ;
944        sp_p = sp.getStorage( bsp ) ;
945        wsp_p = sp_p ;
946        wdata_p = data_p + offset ;
947        for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
948          *wsp_p = *wdata_p ;
949          wsp_p++ ;
950          wdata_p += step ;
951        }
952        sp.putStorage( sp_p, bsp ) ;
953        spectraCol.put( irow, sp ) ;
954        directionCol.put( irow, dir ) ;
955        polnoCol.put( irow, pollist_[ipol] ) ;
956        irow++ ;
957      }
958    }
959  }
960  data_.freeStorage( data_p, bdata ) ;
961
962  t1 = mathutil::gettimeofday_sec() ;
963  os << "saveData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
964
965  return outfile_ ;
966}
967
968void STGrid::prepareTable( Table &tab, String &name )
969{
970  Table t( infile_, Table::Old ) ;
971  t.deepCopy( name, Table::New, False, t.endianFormat(), True ) ;
972  tab = Table( name, Table::Update ) ;
973}
974}
Note: See TracBrowser for help on using the repository browser.