Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/STGrid.cpp

    r2371 r2461  
    1 #include <iostream>
    2 #include <fstream>
    3 
     1//
     2// C++ Implementation: STGrid
     3//
     4// Description:
     5//
     6//
     7// Author: Takeshi Nakazato <takeshi.nakazato@nao.ac.jp>, (C) 2011
     8//
     9// Copyright: See COPYING file that comes with this distribution
     10//
     11//
    412#include <casa/BasicSL/String.h>
    513#include <casa/Arrays/Vector.h>
    6 #include <casa/Arrays/Matrix.h>
    7 #include <casa/Arrays/Cube.h>
    814#include <casa/Arrays/ArrayMath.h>
    9 #include <casa/Arrays/ArrayPartMath.h>
    1015#include <casa/Quanta/Quantum.h>
    1116#include <casa/Quanta/QuantumHolder.h>
     
    1520#include <tables/Tables/Table.h>
    1621#include <tables/Tables/TableRecord.h>
     22#include <tables/Tables/TableRow.h>
    1723#include <tables/Tables/ExprNode.h>
    1824#include <tables/Tables/ScalarColumn.h>
    1925#include <tables/Tables/ArrayColumn.h>
     26#include <tables/Tables/TableCopy.h>
    2027
    2128#include <measures/Measures/MDirection.h>
    2229
    23 #include <MathUtils.h>
     30#include "MathUtils.h"
     31#include <atnf/PKSIO/SrcType.h>
    2432
    2533#include "STGrid.h"
    2634
    2735using namespace std ;
     36using namespace concurrent ;
    2837using namespace casa ;
    2938using namespace asap ;
     
    3140namespace asap {
    3241
     42// for performance check
     43double eToInt = 0.0 ;
     44double eGetWeight = 0.0 ;
     45
    3346// constructor
    3447STGrid::STGrid()
     48  : vshape_( 1 ), wshape_( 2 ), dshape_( 2 )
    3549{
    3650  init() ;
     
    3852
    3953STGrid::STGrid( const string infile )
     54  : vshape_( 1 ), wshape_( 2 ), dshape_( 2 )
    4055{
    4156  init() ;
    4257
    4358  setFileIn( infile ) ;
     59}
     60
     61STGrid::STGrid( const vector<string> infile )
     62{
     63  init() ;
     64
     65  setFileList( infile ) ;
    4466}
    4567
     
    6082  userSupport_ = -1 ;
    6183  convSampling_ = 100 ;
     84  nprocessed_ = 0 ;
     85  nchunk_ = 0 ;
     86
     87  // initialize user input
     88  nxUI_ = -1 ;
     89  nyUI_ = -1 ;
     90  cellxUI_ = "" ;
     91  cellyUI_ = "" ;
     92  centerUI_ = "" ;
     93  doclip_ = False ;
    6294}
    6395
    6496void STGrid::setFileIn( const string infile )
    6597{
     98  nfile_ = 1 ;
    6699  String name( infile ) ;
    67   if ( infile_.compare( name ) != 0 ) {
    68     infile_ = String( infile ) ;
    69     tab_ = Table( infile_ ) ;
     100  infileList_.resize( nfile_ ) ;
     101  infileList_[0] = String(infile) ;
     102}
     103
     104void STGrid::setFileList( const vector<string> infile )
     105{
     106  nfile_ = infile.size() ;
     107  infileList_.resize( nfile_ ) ;
     108  for ( uInt i = 0 ; i < nfile_ ; i++ ) {
     109    infileList_[i] = infile[i] ;
    70110  }
    71111}
     
    74114{
    75115  pollist_.assign( Vector<uInt>( pols ) ) ;
    76   cout << "pollist_ = " << pollist_ << endl ;
    77116}
    78117
     
    80119{
    81120  scanlist_.assign( Vector<uInt>( scans ) ) ;
    82   cout << "scanlist_ = " << scanlist_ << endl ;
    83121}
    84122
     
    87125  wtype_ = String( wType ) ;
    88126  wtype_.upcase() ;
    89   cout << "wtype_ = " << wtype_ << endl ;
    90127}
    91128
     
    96133                          string scenter )
    97134{
    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 ) ;
     135  nxUI_ = (Int)nx ;
     136  nyUI_ = (Int)ny ;
     137  cellxUI_ = String( scellx ) ;
     138  cellyUI_ = String( scelly ) ;
     139  centerUI_ = String( scenter ) ;
    114140}
    115141 
     
    150176                Double*);
    151177}
    152 void STGrid::grid()
     178void STGrid::call_ggridsd( Array<Double> &xypos,
     179                           Array<Complex> &spectra,
     180                           Int &nvispol,
     181                           Int &nvischan,
     182                           Array<Int> &flagtra,
     183                           Array<Int> &flagrow,
     184                           Array<Float> &weight,
     185                           Int &nrow,
     186                           Int &irow,
     187                           Array<Complex> &gdata,
     188                           Array<Float> &gwgt,
     189                           Int &nx,
     190                           Int &ny,
     191                           Int &npol,
     192                           Int &nchan,
     193                           Int &support,
     194                           Int &sampling,
     195                           Vector<Float> &convFunc,
     196                           Int *chanMap,
     197                           Int *polMap )
     198{
     199  // parameters for gridding
     200  Int idopsf = 0 ;
     201  Int len = npol*nchan ;
     202  Double *sumw_p = new Double[len] ;
     203  {
     204    Double *work_p = sumw_p ;
     205    for ( Int i = 0 ; i < len ; i++ ) {
     206      *work_p = 0.0 ;
     207      work_p++ ;
     208    }
     209  }
     210
     211  // prepare pointer
     212  Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ;
     213  Double *xy_p = xypos.getStorage( deletePos ) ;
     214  const Complex *values_p = spectra.getStorage( deleteData ) ;
     215  const Int *flag_p = flagtra.getStorage( deleteFlag ) ;
     216  const Int *rflag_p = flagrow.getStorage( deleteFlagR ) ;
     217  const Float *wgt_p = weight.getStorage( deleteWgt ) ;
     218  Complex *grid_p = gdata.getStorage( deleteDataG ) ;
     219  Float *wgrid_p = gwgt.getStorage( deleteWgtG ) ;
     220  Float *conv_p = convFunc.getStorage( deleteConv ) ;
     221
     222  // pass copy of irow to ggridsd since it will be modified in theroutine
     223  Int irowCopy = irow ;
     224     
     225  // call ggridsd
     226  ggridsd( xy_p,
     227           values_p,
     228           &nvispol,
     229           &nvischan,
     230           &idopsf,
     231           flag_p,
     232           rflag_p,
     233           wgt_p,
     234           &nrow,
     235           &irowCopy,
     236           grid_p,
     237           wgrid_p,
     238           &nx,
     239           &ny,
     240           &npol,
     241           &nchan,
     242           &support,
     243           &sampling,
     244           conv_p,
     245           chanMap,
     246           polMap,
     247           sumw_p ) ;
     248
     249  // finalization
     250  xypos.putStorage( xy_p, deletePos ) ;
     251  spectra.freeStorage( values_p, deleteData ) ;
     252  flagtra.freeStorage( flag_p, deleteFlag ) ;
     253  flagrow.freeStorage( rflag_p, deleteFlagR ) ;
     254  weight.freeStorage( wgt_p, deleteWgt ) ;
     255  gdata.putStorage( grid_p, deleteDataG ) ;
     256  gwgt.putStorage( wgrid_p, deleteWgtG ) ;
     257  convFunc.putStorage( conv_p, deleteConv ) ;
     258  delete sumw_p ;
     259}
     260
     261#define NEED_UNDERSCORES
     262#if defined(NEED_UNDERSCORES)
     263#define ggridsd2 ggridsd2_
     264#endif
     265extern "C" {
     266   void ggridsd2(Double*,
     267                 const Complex*,
     268                 Int*,
     269                 Int*,
     270                 Int*,
     271                 const Int*,
     272                 const Int*,
     273                 const Float*,
     274                 Int*,
     275                 Int*,
     276                 Complex*,
     277                 Float*,
     278                 Int*,
     279                 Complex*,
     280                 Float*,
     281                 Float*,
     282                 Complex*,
     283                 Float*,
     284                 Float*,
     285                 Int*,
     286                 Int*,
     287                 Int *,
     288                 Int *,
     289                 Int*,
     290                 Int*,
     291                 Float*,
     292                 Int*,
     293                 Int*,
     294                 Double*);
     295}
     296void STGrid::call_ggridsd2( Array<Double> &xypos,
     297                            Array<Complex> &spectra,
     298                            Int &nvispol,
     299                            Int &nvischan,
     300                            Array<Int> &flagtra,
     301                            Array<Int> &flagrow,
     302                            Array<Float> &weight,
     303                            Int &nrow,
     304                            Int &irow,
     305                            Array<Complex> &gdata,
     306                            Array<Float> &gwgt,
     307                            Array<Int> &npoints,
     308                            Array<Complex> &clipmin,
     309                            Array<Float> &clipwmin,
     310                            Array<Float> &clipcmin,
     311                            Array<Complex> &clipmax,
     312                            Array<Float> &clipwmax,
     313                            Array<Float> &clipcmax,
     314                            Int &nx,
     315                            Int &ny,
     316                            Int &npol,
     317                            Int &nchan,
     318                            Int &support,
     319                            Int &sampling,
     320                            Vector<Float> &convFunc,
     321                            Int *chanMap,
     322                            Int *polMap )
     323{
     324  // parameters for gridding
     325  Int idopsf = 0 ;
     326  Int len = npol*nchan ;
     327  Double *sumw_p = new Double[len] ;
     328  {
     329    Double *work_p = sumw_p ;
     330    for ( Int i = 0 ; i < len ; i++ ) {
     331      *work_p = 0.0 ;
     332      work_p++ ;
     333    }
     334  }
     335
     336  // prepare pointer
     337  Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG, deleteNpts, deleteCMin, deleteCWMin, deleteCCMin, deleteCMax, deleteCWMax, deleteCCMax ;
     338  Double *xy_p = xypos.getStorage( deletePos ) ;
     339  const Complex *values_p = spectra.getStorage( deleteData ) ;
     340  const Int *flag_p = flagtra.getStorage( deleteFlag ) ;
     341  const Int *rflag_p = flagrow.getStorage( deleteFlagR ) ;
     342  const Float *wgt_p = weight.getStorage( deleteWgt ) ;
     343  Complex *grid_p = gdata.getStorage( deleteDataG ) ;
     344  Float *wgrid_p = gwgt.getStorage( deleteWgtG ) ;
     345  Float *conv_p = convFunc.getStorage( deleteConv ) ;
     346  Int *npts_p = npoints.getStorage( deleteNpts ) ;
     347  Complex *cmin_p = clipmin.getStorage( deleteCMin ) ;
     348  Float *cwmin_p = clipwmin.getStorage( deleteCWMin ) ;
     349  Float *ccmin_p = clipcmin.getStorage( deleteCCMin ) ;
     350  Complex *cmax_p = clipmax.getStorage( deleteCMax ) ;
     351  Float *cwmax_p = clipwmax.getStorage( deleteCWMax ) ;
     352  Float *ccmax_p = clipcmax.getStorage( deleteCCMax ) ;
     353
     354  // pass copy of irow to ggridsd since it will be modified in theroutine
     355  Int irowCopy = irow ;
     356     
     357  // call ggridsd
     358  ggridsd2( xy_p,
     359            values_p,
     360            &nvispol,
     361            &nvischan,
     362            &idopsf,
     363            flag_p,
     364            rflag_p,
     365            wgt_p,
     366            &nrow,
     367            &irowCopy,
     368            grid_p,
     369            wgrid_p,
     370            npts_p,
     371            cmin_p,
     372            cwmin_p,
     373            ccmin_p,
     374            cmax_p,
     375            cwmax_p,
     376            ccmax_p,
     377            &nx,
     378            &ny,
     379            &npol,
     380            &nchan,
     381            &support,
     382            &sampling,
     383            conv_p,
     384            chanMap,
     385            polMap,
     386            sumw_p ) ;
     387
     388  // finalization
     389  xypos.putStorage( xy_p, deletePos ) ;
     390  spectra.freeStorage( values_p, deleteData ) ;
     391  flagtra.freeStorage( flag_p, deleteFlag ) ;
     392  flagrow.freeStorage( rflag_p, deleteFlagR ) ;
     393  weight.freeStorage( wgt_p, deleteWgt ) ;
     394  gdata.putStorage( grid_p, deleteDataG ) ;
     395  gwgt.putStorage( wgrid_p, deleteWgtG ) ;
     396  convFunc.putStorage( conv_p, deleteConv ) ;
     397  clipmin.putStorage( cmin_p, deleteCMin ) ;
     398  clipwmin.putStorage( cwmin_p, deleteCWMin ) ;
     399  clipcmin.putStorage( ccmin_p, deleteCCMin ) ;
     400  clipmax.putStorage( cmax_p, deleteCMax ) ;
     401  clipwmax.putStorage( cwmax_p, deleteCWMax ) ;
     402  clipcmax.putStorage( ccmax_p, deleteCCMax ) ;
     403  delete sumw_p ;
     404}
     405
     406void STGrid::grid()
    153407{
    154408  LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
    155 
    156   // retrieve data
    157   Cube<Float> spectra ;
    158   Matrix<Double> direction ;
    159   Cube<uChar> flagtra ;
    160   Matrix<uInt> rflag ;
    161   Matrix<Float> weight ;
    162   double t0, t1 ;
     409  double t0,t1 ;
     410
     411  // data selection
    163412  t0 = mathutil::gettimeofday_sec() ;
    164   getData( spectra, direction, flagtra, rflag, weight ) ;
     413  selectData() ;
    165414  t1 = mathutil::gettimeofday_sec() ;
    166   os << "getData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
    167   IPosition sshape = spectra.shape() ;
    168   //os << "spectra.shape()=" << spectra.shape() << LogIO::POST ;
    169   //os << "max(spectra) = " << max(spectra) << LogIO::POST ;
    170   //os << "weight = " << weight << LogIO::POST ;
    171 
    172   // flagtra: uChar -> Int
    173   // rflag: uInt -> Int
    174   Cube<Int> flagI ;
    175   Matrix<Int> rflagI ;
    176   t0 = mathutil::gettimeofday_sec() ;
    177   toInt( &flagtra, &flagI ) ;
    178   toInt( &rflag, &rflagI ) ;
    179   t1 = mathutil::gettimeofday_sec() ;
    180   os << "toInt: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
    181  
     415  os << LogIO::DEBUGGING << "selectData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
     416
     417  setupGrid() ;
     418  setupArray() ;
     419
     420  if ( wtype_.compare("UNIFORM") != 0 &&
     421       wtype_.compare("TINT") != 0 &&
     422       wtype_.compare("TSYS") != 0 &&
     423       wtype_.compare("TINTSYS") != 0 ) {
     424    LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
     425    os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
     426    wtype_ = "UNIFORM" ;
     427  }
     428
    182429  // grid parameter
    183430  os << LogIO::DEBUGGING ;
     431  os << "----------" << endl ;
     432  os << "Data selection summary" << endl ;
     433  os << "   ifno = " << ifno_ << endl ;
     434  os << "   pollist = " << pollist_ << endl ;
     435  os << "   scanlist = " << scanlist_ << endl ;
    184436  os << "----------" << endl ;
    185437  os << "Grid parameter summary" << endl ;
     
    187439  os << "   (cellx,celly) = (" << cellx_ << "," << celly_ << ")" << endl ;
    188440  os << "   center = " << center_ << endl ;
     441  os << "   weighting = " << wtype_ << endl ;
     442  os << "   convfunc = " << convType_ << " with support " << convSupport_ << endl ;
     443  os << "   doclip = " << (doclip_?"True":"False") << endl ;
    189444  os << "----------" << LogIO::POST ;
    190445  os << LogIO::NORMAL ;
    191446
     447  if ( doclip_ )
     448    gridPerRowWithClipping() ;
     449  else
     450    gridPerRow() ;
     451}
     452
     453void STGrid::updateChunkShape()
     454{
     455  // TODO: nchunk_ must be determined from nchan_, npol_, and (nx_,ny_)
     456  //       by considering data size to be allocated for ggridsd input/output
     457  nchunk_ = 400 ;
     458  nchunk_ = min( nchunk_, nrow_ ) ;
     459  vshape_ = IPosition( 1, nchunk_ ) ;
     460  wshape_ = IPosition( 2, nchan_, nchunk_ ) ;
     461  dshape_ = IPosition( 2, 2, nchunk_ ) ;
     462}
     463
     464struct STGChunk {
     465  Int nrow ;
     466  Array<Complex> spectra;
     467  Array<Int> flagtra;
     468  Array<Int> rflag;
     469  Array<Float> weight;
     470  Array<Double> direction;
     471  STGChunk(IPosition const &wshape, IPosition const &vshape,
     472           IPosition const &dshape)
     473    : spectra(wshape), flagtra(wshape), rflag(vshape), weight(wshape),
     474      direction(dshape)
     475  { }
     476};
     477
     478struct STCommonData {
     479  Int gnx;
     480  Int gny;
     481  Int *chanMap;
     482  Vector<Float> convFunc ;
     483  Array<Complex> gdataArrC;
     484  Array<Float> gwgtArr;
     485  STCommonData(IPosition const &gshape, Array<Float> const &data)
     486    : gdataArrC(gshape, 0.0), gwgtArr(data) {}
     487};
     488
     489struct STCommonDataWithClipping {
     490  Int gnx;
     491  Int gny;
     492  Int *chanMap;
     493  Vector<Float> convFunc ;
     494  Array<Complex> gdataArrC;
     495  Array<Float> gwgtArr;
     496  Array<Int> npoints ;
     497  Array<Complex> clipMin ;
     498  Array<Float> clipWMin ;
     499  Array<Float> clipCMin ;
     500  Array<Complex> clipMax ;
     501  Array<Float> clipWMax ;
     502  Array<Float> clipCMax ; 
     503  STCommonDataWithClipping(IPosition const &gshape,
     504                           IPosition const &pshape,
     505                           Array<Float> const &data)
     506    : gdataArrC(gshape, 0.0),
     507      gwgtArr(data),
     508      npoints(pshape, 0),
     509      clipMin(gshape, Complex(FLT_MAX,0.0)),
     510      clipWMin(gshape, 0.0),
     511      clipCMin(gshape, 0.0),
     512      clipMax(gshape, Complex(-FLT_MAX,0.0)),
     513      clipWMax(gshape, 0.0),
     514      clipCMax(gshape, 0.0)
     515  {}
     516};
     517
     518#define DO_AHEAD 3
     519
     520struct STContext {
     521  STCommonData &common;
     522  FIFO<STGChunk *, DO_AHEAD> queue;
     523  STGrid *const self;
     524  const Int pol;
     525  STContext(STGrid *obj, STCommonData &common, Int pol)
     526    : self(obj), common(common), pol(pol) {}
     527};
     528
     529struct STContextWithClipping {
     530  STCommonDataWithClipping &common;
     531  FIFO<STGChunk *, DO_AHEAD> queue;
     532  STGrid *const self;
     533  const Int pol;
     534  STContextWithClipping(STGrid *obj, STCommonDataWithClipping &common, Int pol)
     535    : self(obj), common(common), pol(pol) {}
     536};
     537
     538
     539bool STGrid::produceChunk(void *ctx) throw(PCException)
     540{
     541  STContext &context = *(STContext *)ctx;
     542  if ( context.self->nprocessed_ >= context.self->nrow_ ) {
     543    return false;
     544  }
     545  STGChunk *chunk = new STGChunk(context.self->wshape_,
     546                                 context.self->vshape_,
     547                                 context.self->dshape_);
     548
     549  double t0 = mathutil::gettimeofday_sec() ;
     550  chunk->nrow = context.self->getDataChunk(
     551        context.self->wshape_, context.self->vshape_, context.self->dshape_,
     552        chunk->spectra, chunk->direction,
     553        chunk->flagtra, chunk->rflag, chunk->weight);
     554  double t1 = mathutil::gettimeofday_sec() ;
     555  context.self->eGetData_ += t1-t0 ;
     556
     557  context.queue.lock();
     558  context.queue.put(chunk);
     559  context.queue.unlock();
     560  return true;
     561}
     562
     563void STGrid::consumeChunk(void *ctx) throw(PCException)
     564{
     565  STContext &context = *(STContext *)ctx;
     566  STGChunk *chunk = NULL;
     567  try {
     568    context.queue.lock();
     569    chunk = context.queue.get();
     570    context.queue.unlock();
     571  } catch (FullException &e) {
     572    context.queue.unlock();
     573    // TODO: log error
     574    throw PCException();
     575  }
     576
     577  double t0, t1 ;
     578  // world -> pixel
     579  Array<Double> xypos( context.self->dshape_ ) ;
     580  t0 = mathutil::gettimeofday_sec() ;
     581  context.self->toPixel( chunk->direction, xypos ) ;
     582  t1 = mathutil::gettimeofday_sec() ;
     583  context.self->eToPixel_ += t1-t0 ;
     584   
     585  // call ggridsd
     586  Int nvispol = 1 ;
     587  Int irow = -1 ;
     588  t0 = mathutil::gettimeofday_sec() ;
     589  context.self->call_ggridsd( xypos,
     590                chunk->spectra,
     591                nvispol,
     592                context.self->nchan_,
     593                chunk->flagtra,
     594                chunk->rflag,
     595                chunk->weight,
     596                chunk->nrow,
     597                irow,
     598                context.common.gdataArrC,
     599                context.common.gwgtArr,
     600                context.common.gnx,
     601                context.common.gny,
     602                context.self->npol_,
     603                context.self->nchan_,
     604                context.self->convSupport_,
     605                context.self->convSampling_,
     606                context.common.convFunc,
     607                context.common.chanMap,
     608                (Int*)&context.pol ) ;
     609  t1 = mathutil::gettimeofday_sec() ;
     610  context.self->eGGridSD_ += t1-t0 ;
     611 
     612  delete chunk;
     613}
     614
     615void STGrid::gridPerRow()
     616{
     617  LogIO os( LogOrigin("STGrid", "gridPerRow", WHERE) ) ;
     618  double t0, t1 ;
     619
     620
     621  // grid data
     622  // Extend grid plane with convSupport_
     623  //   Int gnx = nx_+convSupport_*2 ;
     624  //   Int gny = ny_+convSupport_*2 ;
     625  Int gnx = nx_;
     626  Int gny = ny_;
     627
     628  IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
     629  // 2011/12/20 TN
     630  // data_ and gwgtArr share storage
     631  data_.resize( gshape ) ;
     632  data_ = 0.0 ;
     633  STCommonData common = STCommonData(gshape, data_);
     634  common.gnx = gnx ;
     635  common.gny = gny ;
     636
     637  // parameters for gridding
     638  Int *chanMap = new Int[nchan_] ;
     639  for ( Int i = 0 ; i < nchan_ ; i++ ) {
     640    chanMap[i] = i ;
     641  }
     642  common.chanMap = chanMap;
     643
    192644  // convolution kernel
    193   Vector<Float> convFunc ;
    194645  t0 = mathutil::gettimeofday_sec() ;
    195   setConvFunc( convFunc ) ;
     646  setConvFunc( common.convFunc ) ;
    196647  t1 = mathutil::gettimeofday_sec() ;
    197   os << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
    198   //cout << "convSupport=" << convSupport_ << endl ;
    199   //cout << "convFunc=" << convFunc << endl ;
    200 
     648  os << LogIO::DEBUGGING << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
     649
     650  // for performance check
     651  eGetData_ = 0.0 ;
     652  eToPixel_ = 0.0 ;
     653  eGGridSD_ = 0.0 ;
     654  double eInitPol = 0.0 ;
     655
     656  for ( uInt ifile = 0 ; ifile < nfile_ ; ifile++ ) {
     657    initTable( ifile ) ;
     658
     659    os << "start table " << ifile << ": " << infileList_[ifile] << LogIO::POST ;   
     660    Broker broker = Broker(produceChunk, consumeChunk);
     661    for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
     662      t0 = mathutil::gettimeofday_sec() ;
     663      initPol( ipol ) ; // set ptab_ and attach()
     664      t1 = mathutil::gettimeofday_sec() ;
     665      eInitPol += t1-t0 ;
     666     
     667      STContext context(this, common, ipol);
     668     
     669      os << "start pol " << ipol << LogIO::POST ;
     670     
     671      nprocessed_ = 0 ;
     672#if 1
     673      broker.runProducerAsMasterThread(&context, DO_AHEAD);
     674#else
     675      for (;;) {
     676        bool produced = produceChunk(&context);
     677        if (! produced) {
     678          break;
     679        }
     680        consumeChunk(&context);
     681      }
     682#endif
     683
     684      os << "end pol " << ipol << LogIO::POST ;
     685
     686    }
     687    os << "end table " << ifile << LogIO::POST ;   
     688  }
     689  os << LogIO::DEBUGGING << "initPol: elapsed time is " << eInitPol << " sec." << LogIO::POST ;
     690  os << LogIO::DEBUGGING << "getData: elapsed time is " << eGetData_-eToInt-eGetWeight << " sec." << LogIO::POST ;
     691  os << LogIO::DEBUGGING << "toPixel: elapsed time is " << eToPixel_ << " sec." << LogIO::POST ;
     692  os << LogIO::DEBUGGING << "ggridsd: elapsed time is " << eGGridSD_ << " sec." << LogIO::POST ;
     693  os << LogIO::DEBUGGING << "toInt: elapsed time is " << eToInt << " sec." << LogIO::POST ;
     694  os << LogIO::DEBUGGING << "getWeight: elapsed time is " << eGetWeight << " sec." << LogIO::POST ;
     695 
     696  delete chanMap ;
     697
     698  // set data
     699  setData( common.gdataArrC, common.gwgtArr ) ;
     700
     701}
     702
     703void STGrid::consumeChunkWithClipping(void *ctx) throw(PCException)
     704{
     705  STContextWithClipping &context = *(STContextWithClipping *)ctx;
     706  STGChunk *chunk = NULL;
     707  try {
     708    context.queue.lock();
     709    chunk = context.queue.get();
     710    context.queue.unlock();
     711  } catch (FullException &e) {
     712    context.queue.unlock();
     713    // TODO: log error
     714    throw PCException();
     715  }
     716
     717  double t0, t1 ;
    201718  // world -> pixel
    202   Matrix<Double> xypos( direction.shape(), 0.0 ) ;
     719  Array<Double> xypos( context.self->dshape_ ) ;
    203720  t0 = mathutil::gettimeofday_sec() ;
    204   toPixel( direction, xypos ) ; 
     721  context.self->toPixel( chunk->direction, xypos ) ;
    205722  t1 = mathutil::gettimeofday_sec() ;
    206   os << "toPixel: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
     723  context.self->eToPixel_ += t1-t0 ;
     724   
     725  // call ggridsd
     726  Int nvispol = 1 ;
     727  Int irow = -1 ;
     728  t0 = mathutil::gettimeofday_sec() ;
     729  context.self->call_ggridsd2( xypos,
     730                chunk->spectra,
     731                nvispol,
     732                context.self->nchan_,
     733                chunk->flagtra,
     734                chunk->rflag,
     735                chunk->weight,
     736                chunk->nrow,
     737                irow,
     738                context.common.gdataArrC,
     739                context.common.gwgtArr,
     740                context.common.npoints,
     741                context.common.clipMin,
     742                context.common.clipWMin,
     743                context.common.clipCMin,
     744                context.common.clipMax,
     745                context.common.clipWMax,
     746                context.common.clipCMax,
     747                context.common.gnx,
     748                context.common.gny,
     749                context.self->npol_,
     750                context.self->nchan_,
     751                context.self->convSupport_,
     752                context.self->convSampling_,
     753                context.common.convFunc,
     754                context.common.chanMap,
     755                (Int*)&context.pol ) ;
     756  t1 = mathutil::gettimeofday_sec() ;
     757  context.self->eGGridSD_ += t1-t0 ;
    207758 
    208   // call ggridsd
    209   Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ;
    210   Double *xypos_p = xypos.getStorage( deletePos ) ;
    211   Cube<Complex> dataC( spectra.shape(), 0.0 ) ;
    212   setReal( dataC, spectra ) ;
    213   const Complex *data_p = dataC.getStorage( deleteData ) ;
    214   const Float *wgt_p = weight.getStorage( deleteWgt ) ;
    215   const Int *flag_p = flagI.getStorage( deleteFlag ) ;
    216   const Int *rflag_p = rflagI.getStorage( deleteFlagR ) ;
    217   Float *conv_p = convFunc.getStorage( deleteConv ) ;
     759  delete chunk;
     760}
     761
     762void STGrid::gridPerRowWithClipping()
     763{
     764  LogIO os( LogOrigin("STGrid", "gridPerRowWithClipping", WHERE) ) ;
     765  double t0, t1 ;
     766
     767
     768  // grid data
    218769  // Extend grid plane with convSupport_
    219   Int gnx = nx_ ;
    220   Int gny = ny_ ;
    221 //   Int gnx = nx_+convSupport_*2 ;
    222 //   Int gny = ny_+convSupport_*2 ;
     770  //   Int gnx = nx_+convSupport_*2 ;
     771  //   Int gny = ny_+convSupport_*2 ;
     772  Int gnx = nx_;
     773  Int gny = ny_;
     774
    223775  IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
    224   Array<Complex> gdataArrC( gshape, 0.0 ) ;
    225   Array<Float> gwgtArr( gshape, 0.0 ) ;
    226   Complex *gdata_p = gdataArrC.getStorage( deleteDataG ) ;
    227   Float *wdata_p = gwgtArr.getStorage( deleteWgtG ) ;
    228   Int idopsf = 0 ;
     776  IPosition pshape( 3, gnx, gny, npol_ ) ;
     777  // 2011/12/20 TN
     778  // data_ and gwgtArr share storage
     779  data_.resize( gshape ) ;
     780  data_ = 0.0 ;
     781  STCommonDataWithClipping common = STCommonDataWithClipping( gshape,
     782                                                              pshape,
     783                                                              data_ ) ;
     784  common.gnx = gnx ;
     785  common.gny = gny ;
     786
     787  // parameters for gridding
    229788  Int *chanMap = new Int[nchan_] ;
    230   {
    231     Int *work_p = chanMap ;
    232     for ( Int i = 0 ; i < nchan_ ; i++ ) {
    233       *work_p = i ;
    234       work_p++ ;
    235     }
    236   }
    237   Int *polMap = new Int[npol_] ;
    238   {
    239     Int *work_p = polMap ;
    240     for ( Int i = 0 ; i < npol_ ; i++ ) {
    241       *work_p = i ;
    242       work_p++ ;
    243     }
    244   }
    245   Double *sumw_p = new Double[npol_*nchan_] ;
    246   {
    247     Double *work_p = sumw_p ;
    248     for ( Int i = 0 ; i < npol_*nchan_ ; i++ ) {
    249       *work_p = 0.0 ;
    250       work_p++ ;
    251     }
    252   }
     789  for ( Int i = 0 ; i < nchan_ ; i++ ) {
     790    chanMap[i] = i ;
     791  }
     792  common.chanMap = chanMap;
     793
     794  // convolution kernel
    253795  t0 = mathutil::gettimeofday_sec() ;
    254   Int irow = -1 ;
    255   ggridsd( xypos_p,
    256            data_p,
    257            &npol_,
    258            &nchan_,
    259            &idopsf,
    260            flag_p,
    261            rflag_p,
    262            wgt_p,
    263            &nrow_,
    264            &irow,
    265            gdata_p,
    266            wdata_p,
    267            &gnx,
    268            &gny,
    269            &npol_,
    270            &nchan_,
    271            &convSupport_,
    272            &convSampling_,
    273            conv_p,
    274            chanMap,
    275            polMap,
    276            sumw_p ) ;
     796  setConvFunc( common.convFunc ) ;
    277797  t1 = mathutil::gettimeofday_sec() ;
    278   os << "ggridsd: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
    279   xypos.putStorage( xypos_p, deletePos ) ;
    280   dataC.freeStorage( data_p, deleteData ) ;
    281   weight.freeStorage( wgt_p, deleteWgt ) ;
    282   flagI.freeStorage( flag_p, deleteFlag ) ;
    283   rflagI.freeStorage( rflag_p, deleteFlagR ) ;
    284   convFunc.putStorage( conv_p, deleteConv ) ;
    285   delete polMap ;
     798  os << LogIO::DEBUGGING << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
     799
     800  // for performance check
     801  eGetData_ = 0.0 ;
     802  eToPixel_ = 0.0 ;
     803  eGGridSD_ = 0.0 ;
     804  double eInitPol = 0.0 ;
     805
     806  for ( uInt ifile = 0 ; ifile < nfile_ ; ifile++ ) {
     807    initTable( ifile ) ;
     808
     809    os << "start table " << ifile << ": " << infileList_[ifile] << LogIO::POST ;   
     810    Broker broker = Broker(produceChunk, consumeChunkWithClipping);
     811    for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
     812      t0 = mathutil::gettimeofday_sec() ;
     813      initPol( ipol ) ; // set ptab_ and attach()
     814      t1 = mathutil::gettimeofday_sec() ;
     815      eInitPol += t1-t0 ;
     816     
     817      STContextWithClipping context(this, common, ipol);
     818     
     819      os << "start pol " << ipol << LogIO::POST ;
     820     
     821      nprocessed_ = 0 ;
     822#if 1
     823      broker.runProducerAsMasterThread(&context, DO_AHEAD);
     824#else
     825      for (;;) {
     826        bool produced = produceChunk(&context);
     827        if (! produced) {
     828          break;
     829        }
     830        consumeChunkWithClipping(&context);
     831      }
     832#endif
     833
     834      os << "end pol " << ipol << LogIO::POST ;
     835
     836    }
     837    os << "end table " << ifile << LogIO::POST ;   
     838  }
     839  os << LogIO::DEBUGGING << "initPol: elapsed time is " << eInitPol << " sec." << LogIO::POST ;
     840  os << LogIO::DEBUGGING << "getData: elapsed time is " << eGetData_-eToInt-eGetWeight << " sec." << LogIO::POST ;
     841  os << LogIO::DEBUGGING << "toPixel: elapsed time is " << eToPixel_ << " sec." << LogIO::POST ;
     842  os << LogIO::DEBUGGING << "ggridsd2: elapsed time is " << eGGridSD_ << " sec." << LogIO::POST ;
     843  os << LogIO::DEBUGGING << "toInt: elapsed time is " << eToInt << " sec." << LogIO::POST ;
     844  os << LogIO::DEBUGGING << "getWeight: elapsed time is " << eGetWeight << " sec." << LogIO::POST ;
     845 
    286846  delete chanMap ;
    287   gdataArrC.putStorage( gdata_p, deleteDataG ) ;
    288   gwgtArr.putStorage( wdata_p, deleteWgtG ) ;
    289   Array<Float> gdataArr = real( gdataArrC ) ;
    290   setData( data_, gdataArr, gwgtArr ) ;
    291   //Matrix<Double> sumWeight( IPosition( 2, npol_, nchan_ ), sumw_p, TAKE_OVER ) ;
    292   delete sumw_p ;
    293   //cout << "sumWeight = " << sumWeight << endl ;
    294 //   os << "gdataArr = " << gdataArr << LogIO::POST ;
    295 //   os << "gwgtArr = " << gwgtArr << LogIO::POST ;
    296 //   os << "data_ " << data_ << LogIO::POST ;
    297 }
    298 
    299 void STGrid::setData( Array<Float> &data,
    300                       Array<Float> &gdata,
     847
     848  // clip min and max in each grid
     849//   os << "BEFORE CLIPPING" << LogIO::POST ;
     850//   os << "gdataArrC=" << common.gdataArrC << LogIO::POST ;
     851//   os << "gwgtArr=" << common.gwgtArr << LogIO::POST ;
     852  t0 = mathutil::gettimeofday_sec() ;
     853  clipMinMax( common.gdataArrC,
     854              common.gwgtArr,
     855              common.npoints,
     856              common.clipMin,
     857              common.clipWMin,
     858              common.clipCMin,
     859              common.clipMax,
     860              common.clipWMax,
     861              common.clipCMax ) ;
     862  t1 = mathutil::gettimeofday_sec() ;
     863  os << LogIO::DEBUGGING << "clipMinMax: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
     864//   os << "AFTER CLIPPING" << LogIO::POST ;
     865//   os << "gdataArrC=" << common.gdataArrC << LogIO::POST ;
     866//   os << "gwgtArr=" << common.gwgtArr << LogIO::POST ;
     867
     868  // set data
     869  setData( common.gdataArrC, common.gwgtArr ) ;
     870
     871}
     872
     873void STGrid::clipMinMax( Array<Complex> &grid,
     874                         Array<Float> &weight,
     875                         Array<Int> &npoints,
     876                         Array<Complex> &clipmin,
     877                         Array<Float> &clipwmin,
     878                         Array<Float> &clipcmin,
     879                         Array<Complex> &clipmax,
     880                         Array<Float> &clipwmax,
     881                         Array<Float> &clipcmax )
     882{
     883  //LogIO os( LogOrigin("STGrid","clipMinMax",WHERE) ) ;
     884
     885  // prepare pointers
     886  Bool delG, delW, delNP, delCMin, delCWMin, delCCMin, delCMax, delCWMax, delCCMax ;
     887  Complex *grid_p = grid.getStorage( delG ) ;
     888  Float *wgt_p = weight.getStorage( delW ) ;
     889  const Int *npts_p = npoints.getStorage( delNP ) ;
     890  const Complex *cmin_p = clipmin.getStorage( delCMin ) ;
     891  const Float *cwmin_p = clipwmin.getStorage( delCWMin ) ;
     892  const Float *ccmin_p = clipcmin.getStorage( delCCMin ) ;
     893  const Complex *cmax_p = clipmax.getStorage( delCMax ) ;
     894  const Float *cwmax_p = clipwmax.getStorage( delCWMax ) ;
     895  const Float *ccmax_p = clipcmax.getStorage( delCCMax ) ;
     896
     897  const IPosition &gshape = grid.shape() ;
     898  long offset = gshape[0] * gshape[1] * gshape[2] ; // nx * ny * npol
     899  Int nchan = gshape[3] ;
     900  long origin = nchan * offset ;
     901  for ( long i = 0 ; i < offset ; i++ ) {
     902    if ( *npts_p > 2 ) {
     903      for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
     904        // clip minimum and maximum
     905        *grid_p -= (*cmin_p)*(*cwmin_p)*(*ccmin_p)
     906          + (*cmax_p)*(*cwmax_p)*(*ccmax_p) ;
     907        *wgt_p -= (*cwmin_p)*(*ccmin_p)
     908          + (*cwmax_p)*(*ccmax_p) ;
     909       
     910        grid_p += offset ;
     911        wgt_p += offset ;
     912        cmin_p += offset ;
     913        cwmin_p += offset ;
     914        ccmin_p += offset ;
     915        cmax_p += offset ;
     916        cwmax_p += offset ;
     917        ccmax_p += offset ;
     918      }
     919      grid_p -= origin ;
     920      wgt_p -= origin ;
     921      cmin_p -= origin ;
     922      cwmin_p -= origin ;
     923      ccmin_p -= origin ;
     924      cmax_p -= origin ;
     925      cwmax_p -= origin ;
     926      ccmax_p -= origin ;
     927    }
     928    grid_p++ ;
     929    wgt_p++ ;
     930    npts_p++ ;
     931    cmin_p++ ;
     932    cwmin_p++ ;
     933    ccmin_p++ ;
     934    cmax_p++ ;
     935    cwmax_p++ ;
     936    ccmax_p++ ;
     937  }
     938  grid_p -= offset ;
     939  wgt_p -= offset ;
     940  npts_p -= offset ;
     941  cmin_p -= offset ;
     942  cwmin_p -= offset ;
     943  ccmin_p -= offset ;
     944  cmax_p -= offset ;
     945  cwmax_p -= offset ;
     946  ccmax_p -= offset ; 
     947
     948  // finalization
     949  grid.putStorage( grid_p, delG ) ;
     950  weight.putStorage( wgt_p, delW ) ;
     951  npoints.freeStorage( npts_p, delNP ) ;
     952  clipmin.freeStorage( cmin_p, delCMin ) ;
     953  clipwmin.freeStorage( cwmin_p, delCWMin ) ;
     954  clipcmin.freeStorage( ccmin_p, delCCMin ) ;
     955  clipmax.freeStorage( cmax_p, delCMax ) ;
     956  clipwmax.freeStorage( cwmax_p, delCWMax ) ;
     957  clipcmax.freeStorage( ccmax_p, delCCMax ) ;
     958}
     959
     960void STGrid::initPol( Int ipol )
     961{
     962  LogIO os( LogOrigin("STGrid","initPol",WHERE) ) ;
     963  if ( npolOrg_ == 1 ) {
     964    os << "single polarization data." << LogIO::POST ;
     965    ptab_ = tab_ ;
     966  }
     967  else
     968    ptab_ = tab_( tab_.col("POLNO") == pollist_[ipol] ) ;
     969
     970  attach( ptab_ ) ;
     971}
     972
     973void STGrid::initTable( uInt idx )
     974{
     975  tab_ = tableList_[idx] ;
     976  nrow_ = rows_[idx] ;
     977  updateChunkShape() ;
     978}
     979
     980void STGrid::setData( Array<Complex> &gdata,
    301981                      Array<Float> &gwgt )
    302982{
     983  // 2011/12/20 TN
     984  // gwgt and data_ share storage
    303985  LogIO os( LogOrigin("STGrid","setData",WHERE) ) ;
    304986  double t0, t1 ;
    305987  t0 = mathutil::gettimeofday_sec() ;
    306   data.resize( gdata.shape() ) ;
    307   uInt len = data.nelements() ;
    308   Float *w0_p ;
    309   const Float *w1_p, *w2_p ;
    310   Bool b0, b1, b2 ;
    311   Float *data_p = data.getStorage( b0 ) ;
    312   const Float *gdata_p = gdata.getStorage( b1 ) ;
    313   const Float *gwgt_p = gwgt.getStorage( b2 ) ;
    314   w0_p = data_p ;
     988  uInt len = data_.nelements() ;
     989  const Complex *w1_p ;
     990  Float *w2_p ;
     991  Bool b1, b2 ;
     992  const Complex *gdata_p = gdata.getStorage( b1 ) ;
     993  Float *gwgt_p = data_.getStorage( b2 ) ;
    315994  w1_p = gdata_p ;
    316995  w2_p = gwgt_p ;
    317996  for ( uInt i = 0 ; i < len ; i++ ) {
    318     *w0_p = (*w2_p > 0.0) ? (*w1_p / *w2_p) : 0.0 ;
    319     w0_p++ ;
     997    if ( *w2_p > 0.0 ) *w2_p = (*w1_p).real() / *w2_p ;
    320998    w1_p++ ;
    321999    w2_p++ ;
    3221000  }
    323   data.putStorage( data_p, b0 ) ;
    3241001  gdata.freeStorage( gdata_p, b1 ) ;
    325   gwgt.freeStorage( gwgt_p, b2 ) ;
     1002  data_.putStorage( gwgt_p, b2 ) ;
    3261003  t1 = mathutil::gettimeofday_sec() ;
    327   os << "setData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
     1004  os << LogIO::DEBUGGING << "setData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
     1005}
     1006
     1007void STGrid::setupGrid()
     1008{
     1009  Double xmin,xmax,ymin,ymax ;
     1010  mapExtent( xmin, xmax, ymin, ymax ) ;
     1011 
     1012  setupGrid( nxUI_, nyUI_, cellxUI_, cellyUI_,
     1013             xmin, xmax, ymin, ymax, centerUI_ ) ;
    3281014}
    3291015
     
    3381024                        String &center )
    3391025{
     1026  LogIO os( LogOrigin("STGrid","setupGrid",WHERE) ) ;
    3401027  //cout << "nx=" << nx << ", ny=" << ny << endl ;
    3411028
     
    3711058    center_(0) = xcen.getValue( "rad" ) ;
    3721059    center_(1) = ycen.getValue( "rad" ) ;
    373   }
    374 
     1060    double base = 0.5 * (xmin + xmax) ;
     1061    int maxrotate = 1 ;
     1062    int nelem = 2 * maxrotate + 1 ;
     1063    double *sep = new double[nelem] ;
     1064    for ( int i = 0 ; i < nelem ; i++ )
     1065      sep[i] = abs(base - center_[0] - (i-maxrotate) * C::_2pi) ;
     1066//     os << "sep[0]=" << sep[0] << endl 
     1067//        << "sep[1]=" << sep[1] << endl
     1068//        << "sep[2]=" << sep[2] << LogIO::POST ;
     1069    int idx = 0 ;
     1070    base = sep[0] ;
     1071    int nrotate = 0 ;
     1072    while ( idx < nelem ) {
     1073      if ( base > sep[idx] ) {
     1074        base = sep[idx] ;
     1075        nrotate = idx ;
     1076      }
     1077      idx++ ;
     1078    }
     1079    delete sep ;
     1080    nrotate -= maxrotate ;
     1081//     os << "nrotate = " << nrotate << LogIO::POST ;
     1082    center_[0] += nrotate * C::_2pi ;
     1083  }
     1084//   os << "xmin=" << xmin << LogIO::POST ;
     1085//   os << "center_=" << center_ << LogIO::POST ;
     1086
     1087
     1088  nx_ = nx ;
     1089  ny_ = ny ;
     1090  if ( nx < 0 && ny > 0 ) {
     1091    nx_ = ny ;
     1092    ny_ = ny ;
     1093  }
     1094  if ( ny < 0 && nx > 0 ) {
     1095    nx_ = nx ;
     1096    ny_ = nx ;
     1097  }
    3751098
    3761099  //Double wx = xmax - xmin ;
     
    3811104  wx *= 1.10 ;
    3821105  wy *= 1.10 ;
     1106
    3831107  Quantum<Double> qcellx ;
    3841108  Quantum<Double> qcelly ;
    385   nx_ = nx ;
    386   ny_ = ny ;
    387   if ( nx < 0 && ny > 0 ) {
    388     nx_ = ny ;
    389     ny_ = ny ;
    390   }
    391   if ( ny < 0 && nx > 0 ) {
    392     nx_ = nx ;
    393     ny_ = nx ;
    394   }
    3951109  //cout << "nx_ = " << nx_ << ",  ny_ = " << ny_ << endl ;
    3961110  if ( cellx.size() != 0 && celly.size() != 0 ) {
     
    3991113  }
    4001114  else if ( celly.size() != 0 ) {
    401     cout << "Using celly to x-axis..." << endl ;
     1115    os << "Using celly to x-axis..." << LogIO::POST ;
    4021116    readQuantity( qcelly, celly ) ;
    4031117    qcellx = qcelly ;
    4041118  }
    4051119  else if ( cellx.size() != 0 ) {
    406     cout << "Using cellx to y-axis..." << endl ;
     1120    os << "Using cellx to y-axis..." << LogIO::POST ;
    4071121    readQuantity( qcellx, cellx ) ;
    4081122    qcelly = qcellx ;
     
    4101124  else {
    4111125    if ( nx_ < 0 ) {
    412       cout << "No user preference in grid setting. Using default..." << endl ;
     1126      os << "No user preference in grid setting. Using default..." << LogIO::POST ;
    4131127      readQuantity( qcellx, "1.0arcmin" ) ;
    4141128      qcelly = qcellx ;
    4151129    }
    4161130    else {
     1131      if ( wx == 0.0 ) {
     1132        os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ;
     1133        wx = 0.00290888 ;
     1134      }
     1135      if ( wy == 0.0 ) {
     1136        os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ;
     1137        wy = 0.00290888 ;
     1138      }
    4171139      qcellx = Quantum<Double>( wx/nx_, "rad" ) ;
    4181140      qcelly = Quantum<Double>( wy/ny_, "rad" ) ;
     
    4201142  }
    4211143  cellx_ = qcellx.getValue( "rad" ) ;
     1144  // DEC correction
     1145  cellx_ /= cos( center_[1] ) ;
    4221146  celly_ = qcelly.getValue( "rad" ) ;
     1147  //os << "cellx_=" << cellx_ << ", celly_=" << celly_ << ", cos("<<center_(1)<<")=" << cos(center_(1)) << LogIO::POST ;
    4231148  if ( nx_ < 0 ) {
     1149    if ( wx == 0.0 ) {
     1150      os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ;
     1151      wx = 0.00290888 ;
     1152    }
     1153    if ( wy == 0.0 ) {
     1154      os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ;
     1155      wy = 0.00290888 ;
     1156    }
    4241157    nx_ = Int( ceil( wx/cellx_ ) ) ;
    4251158    ny_ = Int( ceil( wy/celly_ ) ) ;
     
    4271160}
    4281161
    429 void STGrid::selectData( Table &tab )
    430 {
     1162void STGrid::mapExtent( Double &xmin, Double &xmax,
     1163                        Double &ymin, Double &ymax )
     1164{
     1165  //LogIO os( LogOrigin("STGrid","mapExtent",WHERE) ) ;
     1166  directionCol_.attach( tableList_[0], "DIRECTION" ) ;
     1167  Matrix<Double> direction = directionCol_.getColumn() ;
     1168  //os << "dirCol.nrow() = " << dirCol.nrow() << LogIO::POST ;
     1169  minMax( xmin, xmax, direction.row( 0 ) ) ;
     1170  minMax( ymin, ymax, direction.row( 1 ) ) ;
     1171  Double amin, amax, bmin, bmax ;
     1172  for ( uInt i = 1 ; i < nfile_ ; i++ ) {
     1173    directionCol_.attach( tableList_[i], "DIRECTION" ) ;
     1174    direction.assign( directionCol_.getColumn() ) ;
     1175    //os << "dirCol.nrow() = " << dirCol.nrow() << LogIO::POST ;
     1176    minMax( amin, amax, direction.row( 0 ) ) ;
     1177    minMax( bmin, bmax, direction.row( 1 ) ) ;
     1178    xmin = min( xmin, amin ) ;
     1179    xmax = max( xmax, amax ) ;
     1180    ymin = min( ymin, bmin ) ;
     1181    ymax = max( ymax, bmax ) ;
     1182  }
     1183  //os << "(xmin,xmax)=(" << xmin << "," << xmax << ")" << LogIO::POST ;
     1184  //os << "(ymin,ymax)=(" << ymin << "," << ymax << ")" << LogIO::POST ;
     1185}
     1186
     1187void STGrid::selectData()
     1188{
     1189  LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;   
    4311190  Int ifno = ifno_ ;
    432   Table taborg( infile_ ) ;
    433   if ( ifno == -1 ) {
    434     LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
    435 //     os << LogIO::SEVERE
    436 //        << "Please set IFNO before actual gridding"
    437 //        << LogIO::EXCEPTION ;
     1191  tableList_.resize( nfile_ ) ;
     1192  if ( ifno_ == -1 ) {
     1193    Table taborg( infileList_[0] ) ;
    4381194    ROScalarColumn<uInt> ifnoCol( taborg, "IFNO" ) ;
    439     ifno = ifnoCol( 0 ) ;
     1195    ifno_ = ifnoCol( 0 ) ;
    4401196    os << LogIO::WARN
    441        << "IFNO is not given. Using default IFNO: " << ifno << LogIO::POST ;
    442   }
    443 //   tab = taborg( taborg.col("IFNO") == ifno ) ;
    444   TableExprNode node ;
    445   node = taborg.col("IFNO") == ifno ;
    446   if ( scanlist_.size() > 0 ) {
    447     node = node && taborg.col("SCANNO").in( scanlist_ ) ;
    448   }
    449   tab = taborg( node ) ;
    450   if ( tab.nrow() == 0 ) {
    451     LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
    452     os << LogIO::SEVERE
    453        << "No corresponding rows for given selection: IFNO " << ifno
    454        << " SCANNO " << scanlist_
    455        << LogIO::EXCEPTION ;
    456   }
    457 }
    458 
    459 void STGrid::getData( Cube<Float> &spectra,
    460                       Matrix<Double> &direction,
    461                       Cube<uChar> &flagtra,
    462                       Matrix<uInt> &rflag,
    463                       Matrix<Float> &weight )
    464 {
    465   Table tab ;
    466   selectData( tab ) ;
    467   updatePolList( tab ) ;
    468 //   cout << "npol_ = " << npol_ << endl ;
    469 //   cout << "nchan_ = " << nchan_ << endl ;
    470 //   cout << "nrow_ = " << nrow_ << endl ;
    471   spectra.resize( npol_, nchan_, nrow_ ) ;
    472   flagtra.resize( npol_, nchan_, nrow_ ) ;
    473   rflag.resize( npol_, nrow_ ) ;
    474   Cube<Float> tsys( npol_, nchan_, nrow_ ) ;
    475   Matrix<Double> tint( npol_, nrow_ ) ;
    476   // boolean for pointer access
    477   Bool bsp, bfl, bfr, bts, bti ;
    478   // pointer to the data
    479   Float *sp_p = spectra.getStorage( bsp ) ;
    480   uChar *fl_p = flagtra.getStorage( bfl ) ;
    481   uInt *fr_p = rflag.getStorage( bfr ) ;
    482   Float *ts_p = tsys.getStorage( bts ) ;
    483   Double *ti_p = tint.getStorage( bti ) ;
    484   // working pointer
    485   Float *wsp_p = sp_p ;
    486   uChar *wfl_p = fl_p ;
    487   uInt *wfr_p = fr_p ;
    488   Float *wts_p = ts_p ;
    489   Double *wti_p = ti_p ;
    490   uInt len = nchan_ * nrow_ ;
    491   IPosition mshape( 2, nchan_, nrow_ ) ;
    492   IPosition vshape( 1, nrow_ ) ;
    493   for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
    494     Table subt = tab( tab.col("POLNO") == pollist_[ipol] ) ;
    495     ROArrayColumn<Float> spectraCol( subt, "SPECTRA" ) ;
    496     ROArrayColumn<Double> directionCol( subt, "DIRECTION" ) ;
    497     ROArrayColumn<uChar> flagtraCol( subt, "FLAGTRA" ) ;
    498     ROScalarColumn<uInt> rflagCol( subt, "FLAGROW" ) ;
    499     ROArrayColumn<Float> tsysCol( subt, "TSYS" ) ;
    500     ROScalarColumn<Double> tintCol( subt, "INTERVAL" ) ;
    501     Matrix<Float> spSlice( mshape, wsp_p, SHARE ) ;
    502     Matrix<uChar> flSlice( mshape, wfl_p, SHARE ) ;
    503     Vector<uInt> frSlice( vshape, wfr_p, SHARE ) ;
    504     spectraCol.getColumn( spSlice ) ;
    505     flagtraCol.getColumn( flSlice ) ;
    506     rflagCol.getColumn( frSlice ) ;
    507     if ( ipol == 0 )
    508       directionCol.getColumn( direction ) ;
    509     Vector<Float> tmpF = tsysCol( 0 ) ;
    510     Vector<Double> tmpD( vshape, wti_p, SHARE ) ;
    511     Matrix<Float> tsSlice( mshape, wts_p, SHARE ) ;
    512     if ( tmpF.nelements() == (uInt)nchan_ ) {
    513       tsysCol.getColumn( tsSlice ) ;
     1197       << "IFNO is not given. Using default IFNO: " << ifno_ << LogIO::POST ;
     1198  }
     1199  for ( uInt i = 0 ; i < nfile_ ; i++ ) {
     1200    Table taborg( infileList_[i] ) ;
     1201    TableExprNode node ;
     1202    if ( ifno != -1 || isMultiIF( taborg ) ) {
     1203      os << "apply selection on IFNO" << LogIO::POST ;
     1204      node = taborg.col("IFNO") == ifno_ ;
     1205    }
     1206    if ( scanlist_.size() > 0 ) {
     1207      os << "apply selection on SCANNO" << LogIO::POST ;
     1208      node = node && taborg.col("SCANNO").in( scanlist_ ) ;
     1209    }
     1210    if ( node.isNull() ) {
     1211      tableList_[i] = taborg ;
    5141212    }
    5151213    else {
    516       tsSlice = tmpF( 0 ) ;
    517     }
    518     tintCol.getColumn( tmpD ) ;
    519 
    520     wsp_p += len ;
    521     wfl_p += len ;
    522     wfr_p += nrow_ ;
    523     wts_p += len ;
    524     wti_p += nrow_ ;
    525   }
    526   spectra.putStorage( sp_p, bsp ) ;
    527   flagtra.putStorage( fl_p, bfl ) ;
    528   rflag.putStorage( fr_p, bfr ) ;
    529   tsys.putStorage( ts_p, bts ) ;
    530   tint.putStorage( ti_p, bti ) ;
    531 
     1214      tableList_[i] = taborg( node ) ;
     1215    }
     1216    os << LogIO::DEBUGGING << "tableList_[" << i << "].nrow()=" << tableList_[i].nrow() << LogIO::POST ;
     1217    if ( tableList_[i].nrow() == 0 ) {
     1218      os << LogIO::SEVERE
     1219         << "No corresponding rows for given selection: IFNO " << ifno_ ;
     1220      if ( scanlist_.size() > 0 )
     1221        os << " SCANNO " << scanlist_ ;
     1222      os << LogIO::EXCEPTION ;
     1223    }
     1224  }
     1225}
     1226
     1227Bool STGrid::isMultiIF( Table &tab )
     1228{
     1229  ROScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
     1230  Vector<uInt> ifnos = ifnoCol.getColumn() ;
     1231  return anyNE( ifnos, ifnos[0] ) ;
     1232}
     1233
     1234void STGrid::attach( Table &tab )
     1235{
     1236  // attach to table
     1237  spectraCol_.attach( tab, "SPECTRA" ) ;
     1238  flagtraCol_.attach( tab, "FLAGTRA" ) ;
     1239  directionCol_.attach( tab, "DIRECTION" ) ;
     1240  flagRowCol_.attach( tab, "FLAGROW" ) ;
     1241  tsysCol_.attach( tab, "TSYS" ) ;
     1242  intervalCol_.attach( tab, "INTERVAL" ) ;
     1243}
     1244
     1245Int STGrid::getDataChunk(
     1246                         IPosition const &wshape,
     1247                         IPosition const &vshape,
     1248                         IPosition const &dshape,
     1249                         Array<Complex> &spectra,
     1250                         Array<Double> &direction,
     1251                         Array<Int> &flagtra,
     1252                         Array<Int> &rflag,
     1253                         Array<Float> &weight )
     1254{
     1255  LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
     1256
     1257  Array<Float> spectraF_(wshape);
     1258  Array<uChar> flagtraUC_(wshape);
     1259  Array<uInt> rflagUI_(vshape);
     1260  Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
     1261  if ( nrow < nchunk_ ) {
     1262    spectra.resize( spectraF_.shape() ) ;
     1263    flagtra.resize( flagtraUC_.shape() ) ;
     1264    rflag.resize( rflagUI_.shape() ) ;
     1265  }
     1266  double t0, t1 ;
     1267  t0 = mathutil::gettimeofday_sec() ;
     1268  convertArray( spectra, spectraF_ ) ;
     1269  toInt( flagtraUC_, flagtra ) ;
     1270  toInt( rflagUI_, rflag ) ;
     1271  t1 = mathutil::gettimeofday_sec() ;
     1272  eToInt = t1 - t0 ;
     1273 
     1274  return nrow ;
     1275}
     1276
     1277#if 0
     1278Int STGrid::getDataChunk( Array<Complex> &spectra,
     1279                          Array<Double> &direction,
     1280                          Array<Int> &flagtra,
     1281                          Array<Int> &rflag,
     1282                          Array<Float> &weight )
     1283{
     1284  LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
     1285  Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
     1286  if ( nrow < nchunk_ ) {
     1287    spectra.resize( spectraF_.shape() ) ;
     1288    flagtra.resize( flagtraUC_.shape() ) ;
     1289    rflag.resize( rflagUI_.shape() ) ;
     1290  }
     1291  double t0, t1 ;
     1292  t0 = mathutil::gettimeofday_sec() ;
     1293  convertArray( spectra, spectraF_ ) ;
     1294  toInt( flagtraUC_, flagtra ) ;
     1295  toInt( rflagUI_, rflag ) ;
     1296  t1 = mathutil::gettimeofday_sec() ;
     1297  eToInt = t1 - t0 ;
     1298 
     1299  return nrow ;
     1300}
     1301#endif
     1302
     1303Int STGrid::getDataChunk( Array<Float> &spectra,
     1304                          Array<Double> &direction,
     1305                          Array<uChar> &flagtra,
     1306                          Array<uInt> &rflag,
     1307                          Array<Float> &weight )
     1308{
     1309  LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
     1310  Int nrow = spectra.shape()[1] ;
     1311  Int remainingRow = nrow_ - nprocessed_ ;
     1312  if ( remainingRow < nrow ) {
     1313    nrow = remainingRow ;
     1314    IPosition mshape( 2, nchan_, nrow ) ;
     1315    IPosition vshape( 1, nrow ) ;
     1316    spectra.resize( mshape ) ;
     1317    flagtra.resize( mshape ) ;
     1318    direction.resize( IPosition(2,2,nrow) ) ;
     1319    rflag.resize( vshape ) ;
     1320    weight.resize( mshape ) ;
     1321  }
     1322  // 2011/12/22 TN
     1323  // tsys shares its storage with weight
     1324  Array<Float> tsys( weight ) ;
     1325  Array<Double> tint( rflag.shape() ) ;
     1326
     1327  Vector<uInt> rflagVec( rflag ) ;
     1328  Vector<Double> tintVec( tint ) ;
     1329
     1330  RefRows rows( nprocessed_, nprocessed_+nrow-1, 1 ) ;
     1331  //os<<LogIO::DEBUGGING<<"nprocessed_="<<nprocessed_<<": rows.nrows()="<<rows.nrows()<<LogIO::POST ;
     1332  spectraCol_.getColumnCells( rows, spectra ) ;
     1333  flagtraCol_.getColumnCells( rows, flagtra ) ;
     1334  directionCol_.getColumnCells( rows, direction ) ;
     1335  flagRowCol_.getColumnCells( rows, rflagVec ) ;
     1336  intervalCol_.getColumnCells( rows, tintVec ) ;
     1337  Vector<Float> tsysTemp = tsysCol_( nprocessed_ ) ;
     1338  if ( tsysTemp.nelements() == (uInt)nchan_ )
     1339    tsysCol_.getColumnCells( rows, tsys ) ;
     1340  else
     1341    tsys = tsysTemp[0] ;
     1342
     1343  double t0,t1 ;
     1344  t0 = mathutil::gettimeofday_sec() ;
    5321345  getWeight( weight, tsys, tint ) ;
    533 }
    534 
    535 void STGrid::updatePolList( Table &tab )
    536 {
    537   ROScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
     1346  t1 = mathutil::gettimeofday_sec() ;
     1347  eGetWeight += t1-t0 ;
     1348
     1349  nprocessed_ += nrow ;
     1350 
     1351  return nrow ;
     1352}
     1353
     1354void STGrid::setupArray()
     1355{
     1356  LogIO os( LogOrigin("STGrid","setupArray",WHERE) ) ;
     1357  ROScalarColumn<uInt> polnoCol( tableList_[0], "POLNO" ) ;
    5381358  Vector<uInt> pols = polnoCol.getColumn() ;
     1359  //os << pols << LogIO::POST ;
    5391360  Vector<uInt> pollistOrg ;
    540   uInt npolOrg = 0 ;
     1361  npolOrg_ = 0 ;
    5411362  uInt polno ;
    5421363  for ( uInt i = 0 ; i < polnoCol.nrow() ; i++ ) {
     
    5441365    polno = pols( i ) ;
    5451366    if ( allNE( pollistOrg, polno ) ) {
    546       pollistOrg.resize( npolOrg+1, True ) ;
    547       pollistOrg[npolOrg] = polno ;
    548       npolOrg++ ;
     1367      pollistOrg.resize( npolOrg_+1, True ) ;
     1368      pollistOrg[npolOrg_] = polno ;
     1369      npolOrg_++ ;
    5491370    }
    5501371  }
     
    5651386  npol_ = pollist_.size() ;
    5661387  if ( npol_ == 0 ) {
    567     LogIO os( LogOrigin("STGrid","updatePolList",WHERE) ) ;
    5681388    os << LogIO::SEVERE << "Empty pollist" << LogIO::EXCEPTION ;
    5691389  }
    570   nrow_ = tab.nrow() / npolOrg ;
    571   ROArrayColumn<uChar> tmpCol( tab, "FLAGTRA" ) ;
    572   nchan_ = tmpCol( 0 ).nelements() ;
    573 //   LogIO os( LogOrigin("STGrid","updatePolList",WHERE) ) ;
     1390  rows_.resize( nfile_ ) ;
     1391  for ( uInt i = 0 ; i < nfile_ ; i++ ) {
     1392    rows_[i] = tableList_[i].nrow() / npolOrg_ ;
     1393    //if ( nrow_ < rows_[i] )
     1394    //  nrow_ = rows_[i] ;
     1395  }
     1396  flagtraCol_.attach( tableList_[0], "FLAGTRA" ) ;
     1397  nchan_ = flagtraCol_( 0 ).nelements() ;
    5741398//   os << "npol_ = " << npol_ << "(" << pollist_ << ")" << endl
    5751399//      << "nchan_ = " << nchan_ << endl
     
    5771401}
    5781402
    579 void STGrid::getWeight( Matrix<Float> &w,
    580                         Cube<Float> &tsys,
    581                         Matrix<Double> &tint )
     1403void STGrid::getWeight( Array<Float> &w,
     1404                              Array<Float> &tsys,
     1405                              Array<Double> &tint )
    5821406{
    5831407  LogIO os( LogOrigin("STGrid","getWeight",WHERE) ) ;
    584   double t0, t1 ;
    585   t0 = mathutil::gettimeofday_sec() ;
    586   // resize
    587   w.resize( nchan_, nrow_ ) ;
     1408
     1409  // 2011/12/22 TN
     1410  // w (weight) and tsys share storage
     1411  IPosition refShape = tsys.shape() ;
     1412  Int nchan = refShape[0] ;
     1413  Int nrow = refShape[1] ;
     1414//   os << "nchan=" << nchan << ", nrow=" << nrow << LogIO::POST ;
     1415//   os << "w.shape()=" << w.shape() << endl
     1416//      << "tsys.shape()=" << tsys.shape() << endl
     1417//      << "tint.shape()=" << tint.shape() << LogIO::POST ;
    5881418
    5891419  // set weight
    590   Bool warn = False ;
    5911420  if ( wtype_.compare( "UNIFORM" ) == 0 ) {
    5921421    w = 1.0 ;
    5931422  }
    5941423  else if ( wtype_.compare( "TINT" ) == 0 ) {
    595     if ( npol_ > 1 ) warn = True ;
    5961424    Bool b0, b1 ;
    5971425    Float *w_p = w.getStorage( b0 ) ;
     
    5991427    const Double *ti_p = tint.getStorage( b1 ) ;
    6001428    const Double *w1_p = ti_p ;
    601     for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
    602       Float val = (Float)(polMean( w1_p )) ;
    603       for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
    604         *w0_p = val ;
     1429    for ( Int irow = 0 ; irow < nrow ; irow++ ) {
     1430      for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
     1431        *w0_p = *w1_p ;
    6051432        w0_p++ ;
    6061433      }
     1434      w1_p++ ;
    6071435    }
    6081436    w.putStorage( w_p, b0 ) ;
     
    6101438  }
    6111439  else if ( wtype_.compare( "TSYS" ) == 0 ) {
    612     if ( npol_ > 1 ) warn = True ;
    613     Bool b0, b1 ;
     1440    Bool b0 ;
    6141441    Float *w_p = w.getStorage( b0 ) ;
    6151442    Float *w0_p = w_p ;
    616     const Float *ts_p = tsys.getStorage( b1 ) ;
    617     const Float *w1_p = ts_p ;
    618     for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
    619       for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
    620         Float val = polMean( w1_p ) ;
    621         *w0_p = 1.0 / ( val * val ) ;
     1443    for ( Int irow = 0 ; irow < nrow ; irow++ ) {
     1444      for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
     1445        Float temp = *w0_p ;
     1446        *w0_p = 1.0 / ( temp * temp ) ;
    6221447        w0_p++ ;
    6231448      }
    6241449    }
    6251450    w.putStorage( w_p, b0 ) ;
    626     tsys.freeStorage( ts_p, b1 ) ;
    6271451  }
    6281452  else if ( wtype_.compare( "TINTSYS" ) == 0 ) {
    629     if ( npol_ > 1 ) warn = True ;
    630     Bool b0, b1, b2 ;
     1453    Bool b0, b1 ;
    6311454    Float *w_p = w.getStorage( b0 ) ;
    6321455    Float *w0_p = w_p ;
    6331456    const Double *ti_p = tint.getStorage( b1 ) ;
    6341457    const Double *w1_p = ti_p ;
    635     const Float *ts_p = tsys.getStorage( b2 ) ;
    636     const Float *w2_p = ts_p ;
    637     for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
    638       Float interval = (Float)(polMean( w1_p )) ;
    639       for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
    640         Float temp = polMean( w2_p ) ;
     1458    for ( Int irow = 0 ; irow < nrow ; irow++ ) {
     1459      Float interval = *w1_p ;
     1460      for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
     1461        Float temp = *w0_p ;
    6411462        *w0_p = interval / ( temp * temp ) ;
    6421463        w0_p++ ;
    6431464      }
     1465      w1_p++ ;
    6441466    }
    6451467    w.putStorage( w_p, b0 ) ;
    6461468    tint.freeStorage( ti_p, b1 ) ;
    647     tsys.freeStorage( ts_p, b2 ) ;
    6481469  }
    6491470  else {
    6501471    //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
    651     os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
     1472    //os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
    6521473    w = 1.0 ;
    6531474  }
    654 
    655   if ( npol_ > 1 ) {
    656     //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
    657     os << LogIO::WARN << "STGrid doesn't support assigning polarization-dependent weight. Use averaged weight over polarization." << LogIO::POST ;
    658   }
    659   t1 = mathutil::gettimeofday_sec() ;
    660   os << "getWeight: elapsed time is " << t1-t0 << " sec" << LogIO::POST ;
    661 }
    662 
    663 Float STGrid::polMean( const Float *p )
    664 {
    665   Float v = 0.0 ;
    666   for ( Int i = 0 ; i < npol_ ; i++ ) {
    667     v += *p ;
    668     p++ ;
    669   }
    670   v /= npol_ ;
    671   return v ;
    672 }
    673 
    674 Double STGrid::polMean( const Double *p )
    675 {
    676   Double v = 0.0 ;
    677   for ( Int i = 0 ; i < npol_ ; i++ ) {
    678     v += *p ;
    679     p++ ;
    680   }
    681   v /= npol_ ;
    682   return v ;
    683 }
    684 
    685 void STGrid::toInt( Array<uChar> *u, Array<Int> *v )
    686 {
    687   uInt len = u->nelements() ;
     1475}
     1476
     1477void STGrid::toInt( Array<uChar> &u, Array<Int> &v )
     1478{
     1479  uInt len = u.nelements() ;
    6881480  Int *int_p = new Int[len] ;
    6891481  Bool deleteIt ;
    690   const uChar *data_p = u->getStorage( deleteIt ) ;
     1482  const uChar *data_p = u.getStorage( deleteIt ) ;
    6911483  Int *i_p = int_p ;
    6921484  const uChar *u_p = data_p ;
     
    6961488    u_p++ ;
    6971489  }
    698   u->freeStorage( data_p, deleteIt ) ;
    699   v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
    700 }
    701 
    702 void STGrid::toInt( Array<uInt> *u, Array<Int> *v )
    703 {
    704   uInt len = u->nelements() ;
     1490  u.freeStorage( data_p, deleteIt ) ;
     1491  v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
     1492}
     1493
     1494void STGrid::toInt( Array<uInt> &u, Array<Int> &v )
     1495{
     1496  uInt len = u.nelements() ;
    7051497  Int *int_p = new Int[len] ;
    7061498  Bool deleteIt ;
    707   const uInt *data_p = u->getStorage( deleteIt ) ;
     1499  const uInt *data_p = u.getStorage( deleteIt ) ;
    7081500  Int *i_p = int_p ;
    7091501  const uInt *u_p = data_p ;
     
    7131505    u_p++ ;
    7141506  }
    715   u->freeStorage( data_p, deleteIt ) ;
    716   v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
    717 }
    718 
    719 void STGrid::toPixel( Matrix<Double> &world, Matrix<Double> &pixel )
     1507  u.freeStorage( data_p, deleteIt ) ;
     1508  v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
     1509}
     1510
     1511void STGrid::toPixel( Array<Double> &world, Array<Double> &pixel )
    7201512{
    7211513  // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_)
    7221514  // grid plane to avoid unexpected behavior on grid edge
    723   Vector<Double> pixc( 2 ) ;
    724   pixc(0) = Double( nx_-1 ) * 0.5 ;
    725   pixc(1) = Double( ny_-1 ) * 0.5 ;
    726 //   pixc(0) = Double( nx_+2*convSupport_-1 ) * 0.5 ;
    727 //   pixc(1) = Double( ny_+2*convSupport_-1 ) * 0.5 ;
     1515  Block<Double> pixc( 2 ) ;
     1516  pixc[0] = Double( nx_-1 ) * 0.5 ;
     1517  pixc[1] = Double( ny_-1 ) * 0.5 ;
     1518//   pixc[0] = Double( nx_+2*convSupport_-1 ) * 0.5 ;
     1519//   pixc[1] = Double( ny_+2*convSupport_-1 ) * 0.5 ;
    7281520  uInt nrow = world.shape()[1] ;
    729   Vector<Double> cell( 2 ) ;
    730   cell(0) = cellx_ ;
    731   cell(1) = celly_ ;
    732   for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
    733     for ( uInt i = 0 ; i < 2 ; i++ ) {
    734       pixel( i, irow ) = pixc(i) + ( world(i, irow) - center_(i) ) / cell(i) ;
    735     }
    736   }
    737 //   String gridfile = "grid."+convType_+"."+String::toString(convSupport_)+".dat" ;
    738 //   ofstream ofs( gridfile.c_str(), ios::out ) ;
    739 //   ofs << "center " << center_(0) << " " << pixc(0)
    740 //       << " " << center_(1) << " " << pixc(1) << endl ;
    741 //   for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
    742 //     ofs << irow ;
    743 //     for ( uInt i = 0 ; i < 2 ; i++ ) {
    744 //       ofs << " " << world(i, irow) << " " << pixel(i, irow) ;
    745 //     }
    746 //     ofs << endl ;
    747 //   }
    748 //   ofs.close() ;
     1521  Bool bw, bp ;
     1522  const Double *w_p = world.getStorage( bw ) ;
     1523  Double *p_p = pixel.getStorage( bp ) ;
     1524  const Double *ww_p = w_p ;
     1525  Double *wp_p = p_p ;
     1526  for ( uInt i = 0 ; i < nrow ; i++ ) {
     1527    *wp_p = pixc[0] + ( *ww_p - center_[0] ) / cellx_ ;
     1528    wp_p++ ;
     1529    ww_p++ ;
     1530    *wp_p = pixc[1] + ( *ww_p - center_[1] ) / celly_ ;
     1531    wp_p++ ;
     1532    ww_p++ ;
     1533  }
     1534  world.freeStorage( w_p, bw ) ;
     1535  pixel.putStorage( p_p, bp ) ; 
    7491536}
    7501537
     
    8121599    // to take into account Gaussian tail
    8131600    if ( convSupport_ < 0 )
    814       convSupport_ = 12 ; // 3 * 4
     1601      convSupport_ = 4 ; // 1 * 4
    8151602    else {
    8161603      convSupport_ = userSupport_ * 4 ;
     
    8391626  String outfile_ ;
    8401627  if ( outfile.size() == 0 ) {
    841     if ( infile_.lastchar() == '/' ) {
    842       outfile_ = infile_.substr( 0, infile_.size()-1 ) ;
     1628    if ( infileList_[0].lastchar() == '/' ) {
     1629      outfile_ = infileList_[0].substr( 0, infileList_[0].size()-1 ) ;
    8431630    }
    8441631    else {
    845       outfile_ = infile_ ;
     1632      outfile_ = infileList_[0] ;
    8461633    }
    8471634    outfile_ += ".grid" ;
     
    8971684
    8981685  t1 = mathutil::gettimeofday_sec() ;
    899   os << "saveData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
    900  
     1686  os << LogIO::DEBUGGING << "saveData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
     1687
     1688  fillMainColumns( tab ) ;
     1689
    9011690  return outfile_ ;
    9021691}
     
    9041693void STGrid::prepareTable( Table &tab, String &name )
    9051694{
    906   Table t( infile_, Table::Old ) ;
     1695  Table t( infileList_[0], Table::Old ) ;
    9071696  t.deepCopy( name, Table::New, False, t.endianFormat(), True ) ;
    9081697  tab = Table( name, Table::Update ) ;
    909 }
    910 }
     1698  // 2012/02/13 TN
     1699  // explicitly copy subtables since no rows including subtables are
     1700  // copied by Table::deepCopy with noRows=True
     1701  TableCopy::copySubTables( tab, t ) ;
     1702}
     1703
     1704void STGrid::fillMainColumns( Table &tab )
     1705{
     1706  // values for fill
     1707  Table t( infileList_[0], Table::Old ) ;
     1708  Table tsel = t( t.col( "IFNO" ) == (uInt)ifno_, 1 ) ;
     1709  ROTableRow row( tsel ) ;
     1710  row.get( 0 ) ;
     1711  const TableRecord &rec = row.record() ;
     1712  uInt freqId = rec.asuInt( "FREQ_ID" ) ;
     1713  uInt molId = rec.asuInt( "MOLECULE_ID" ) ;
     1714  uInt tcalId = rec.asuInt( "TCAL_ID" ) ;
     1715  uInt focusId = rec.asuInt( "FOCUS_ID" ) ;
     1716  uInt weatherId = rec.asuInt( "WEATHER_ID" ) ;
     1717  String srcname = rec.asString( "SRCNAME" ) ;
     1718  String fieldname = rec.asString( "FIELDNAME" ) ;
     1719  Vector<Float> defaultTsys( 1, 1.0 ) ;
     1720  // @todo how to set flagtra for gridded spectra?
     1721  Vector<uChar> flagtra = rec.asArrayuChar( "FLAGTRA" ) ;
     1722  flagtra = (uChar)0 ;
     1723  Float opacity = rec.asFloat( "OPACITY" ) ;
     1724  Double srcvel = rec.asDouble( "SRCVELOCITY" ) ;
     1725  Vector<Double> srcpm = rec.asArrayDouble( "SRCPROPERMOTION" ) ;
     1726  Vector<Double> srcdir = rec.asArrayDouble( "SRCDIRECTION" ) ;
     1727  Vector<Double> scanrate = rec.asArrayDouble( "SCANRATE" ) ;
     1728  Double time = rec.asDouble( "TIME" ) ;
     1729  Double interval = rec.asDouble( "INTERVAL" ) ;
     1730
     1731  // fill columns
     1732  Int nrow = tab.nrow() ;
     1733  ScalarColumn<uInt> scannoCol( tab, "SCANNO" ) ;
     1734  ScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
     1735  ScalarColumn<uInt> freqIdCol( tab, "FREQ_ID" ) ;
     1736  ScalarColumn<uInt> molIdCol( tab, "MOLECULE_ID" ) ;
     1737  ScalarColumn<uInt> tcalidCol( tab, "TCAL_ID" ) ;
     1738  ScalarColumn<Int> fitidCol( tab, "FIT_ID" ) ;
     1739  ScalarColumn<uInt> focusidCol( tab, "FOCUS_ID" ) ;
     1740  ScalarColumn<uInt> weatheridCol( tab, "WEATHER_ID" ) ;
     1741  ArrayColumn<uChar> flagtraCol( tab, "FLAGTRA" ) ;
     1742  ScalarColumn<uInt> rflagCol( tab, "FLAGROW" ) ;
     1743  ArrayColumn<Float> tsysCol( tab, "TSYS" ) ;
     1744  ScalarColumn<String> srcnameCol( tab, "SRCNAME" ) ;
     1745  ScalarColumn<String> fieldnameCol( tab, "FIELDNAME" ) ;
     1746  ScalarColumn<Int> srctypeCol( tab, "SRCTYPE" ) ;
     1747  ScalarColumn<Float> opacityCol( tab, "OPACITY" ) ;
     1748  ScalarColumn<Double> srcvelCol( tab, "SRCVELOCITY" ) ;
     1749  ArrayColumn<Double> srcpmCol( tab, "SRCPROPERMOTION" ) ;
     1750  ArrayColumn<Double> srcdirCol( tab, "SRCDIRECTION" ) ;
     1751  ArrayColumn<Double> scanrateCol( tab, "SCANRATE" ) ;
     1752  ScalarColumn<Double> timeCol( tab, "TIME" ) ;
     1753  ScalarColumn<Double> intervalCol( tab, "INTERVAL" ) ;
     1754  for ( Int i = 0 ; i < nrow ; i++ ) {
     1755    scannoCol.put( i, (uInt)i ) ;
     1756    ifnoCol.put( i, (uInt)ifno_ ) ;
     1757    freqIdCol.put( i, freqId ) ;
     1758    molIdCol.put( i, molId ) ;
     1759    tcalidCol.put( i, tcalId ) ;
     1760    fitidCol.put( i, -1 ) ;
     1761    focusidCol.put( i, focusId ) ;
     1762    weatheridCol.put( i, weatherId ) ;
     1763    flagtraCol.put( i, flagtra ) ;
     1764    rflagCol.put( i, 0 ) ;
     1765    tsysCol.put( i, defaultTsys ) ;
     1766    srcnameCol.put( i, srcname ) ;
     1767    fieldnameCol.put( i, fieldname ) ;
     1768    srctypeCol.put( i, (Int)SrcType::PSON ) ;
     1769    opacityCol.put( i, opacity ) ;
     1770    srcvelCol.put( i, srcvel ) ;
     1771    srcpmCol.put( i, srcpm ) ;
     1772    srcdirCol.put( i, srcdir ) ;
     1773    scanrateCol.put( i, scanrate ) ;
     1774    timeCol.put( i, time ) ;
     1775    intervalCol.put( i, interval ) ;
     1776  }
     1777}
     1778
     1779}
Note: See TracChangeset for help on using the changeset viewer.