source: trunk/src/STGrid.cpp @ 2389

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

Introduced infileList_ (Block<String>) instead of infile_ (String).


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