source: trunk/src/STGrid.cpp @ 2380

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

Fixed a shape mismatch in row flag data which must have a shape (nrow)
while it was (npol,nrow).


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