source: trunk/src/STGrid.cpp @ 2594

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

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: sdgrid unit test with sd.rcParamsscantable.storage?='memory'

Put in Release Notes: Yes/No?

Module(s): Module Names change impacts.

Description: Describe your changes here...

Bug fix on STGrid2.


File size: 52.9 KB
RevLine 
[2405]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//
[2356]12#include <casa/BasicSL/String.h>
13#include <casa/Arrays/Vector.h>
14#include <casa/Arrays/ArrayMath.h>
15#include <casa/Quanta/Quantum.h>
16#include <casa/Quanta/QuantumHolder.h>
17#include <casa/Utilities/CountedPtr.h>
[2361]18#include <casa/Logging/LogIO.h>
[2356]19
20#include <tables/Tables/Table.h>
[2360]21#include <tables/Tables/TableRecord.h>
[2413]22#include <tables/Tables/TableRow.h>
[2360]23#include <tables/Tables/ExprNode.h>
[2356]24#include <tables/Tables/ScalarColumn.h>
25#include <tables/Tables/ArrayColumn.h>
[2405]26#include <tables/Tables/TableCopy.h>
[2356]27
28#include <measures/Measures/MDirection.h>
29
[2454]30#include "MathUtils.h"
[2424]31#include <atnf/PKSIO/SrcType.h>
[2364]32
[2356]33#include "STGrid.h"
34
35using namespace std ;
[2393]36using namespace concurrent ;
[2356]37using namespace casa ;
38using namespace asap ;
39
40namespace asap {
41
[2384]42// for performance check
43double eToInt = 0.0 ;
44double eGetWeight = 0.0 ;
45
[2356]46// constructor
47STGrid::STGrid()
[2393]48  : vshape_( 1 ), wshape_( 2 ), dshape_( 2 )
[2356]49{
50  init() ;
51}
52
53STGrid::STGrid( const string infile )
[2393]54  : vshape_( 1 ), wshape_( 2 ), dshape_( 2 )
[2356]55{
56  init() ;
57
58  setFileIn( infile ) ;
59}
60
[2390]61STGrid::STGrid( const vector<string> infile )
62{
63  init() ;
64
65  setFileList( infile ) ;
66}
67
[2356]68void  STGrid::init()
69{
[2362]70  ifno_ = -1 ;
[2356]71  nx_ = -1 ;
72  ny_ = -1 ;
[2361]73  npol_ = 0 ;
74  nchan_ = 0 ;
75  nrow_ = 0 ;
[2356]76  cellx_ = 0.0 ;
77  celly_ = 0.0 ;
78  center_ = Vector<Double> ( 2, 0.0 ) ;
79  convType_ = "BOX" ;
[2361]80  wtype_ = "UNIFORM" ;
[2356]81  convSupport_ = -1 ;
82  userSupport_ = -1 ;
83  convSampling_ = 100 ;
[2379]84  nprocessed_ = 0 ;
85  nchunk_ = 0 ;
[2388]86
87  // initialize user input
88  nxUI_ = -1 ;
89  nyUI_ = -1 ;
90  cellxUI_ = "" ;
91  cellyUI_ = "" ;
92  centerUI_ = "" ;
[2396]93  doclip_ = False ;
[2356]94}
95
96void STGrid::setFileIn( const string infile )
97{
[2393]98  nfile_ = 1 ;
[2356]99  String name( infile ) ;
[2393]100  infileList_.resize( nfile_ ) ;
101  infileList_[0] = String(infile) ;
[2356]102}
103
[2390]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] ;
110  }
111}
112
[2360]113void STGrid::setPolList( vector<unsigned int> pols )
114{
115  pollist_.assign( Vector<uInt>( pols ) ) ;
116}
117
[2364]118void STGrid::setScanList( vector<unsigned int> scans )
119{
120  scanlist_.assign( Vector<uInt>( scans ) ) ;
121}
122
[2361]123void STGrid::setWeight( const string wType )
124{
125  wtype_ = String( wType ) ;
126  wtype_.upcase() ;
127}
128
[2356]129void STGrid::defineImage( int nx,
130                          int ny,
131                          string scellx,
132                          string scelly,
133                          string scenter )
134{
[2388]135  nxUI_ = (Int)nx ;
136  nyUI_ = (Int)ny ;
137  cellxUI_ = String( scellx ) ;
138  cellyUI_ = String( scelly ) ;
139  centerUI_ = String( scenter ) ;
[2356]140}
141 
[2364]142void STGrid::setFunc( string convType,
143                      int convSupport )
[2356]144{
145  convType_ = String( convType ) ;
146  convType_.upcase() ;
147  userSupport_ = (Int)convSupport ;
148}
149
150#define NEED_UNDERSCORES
151#if defined(NEED_UNDERSCORES)
152#define ggridsd ggridsd_
153#endif
154extern "C" {
155   void ggridsd(Double*,
156                const Complex*,
157                Int*,
158                Int*,
159                Int*,
160                const Int*,
161                const Int*,
162                const Float*,
163                Int*,
164                Int*,
165                Complex*,
166                Float*,
167                Int*,
168                Int*,
169                Int *,
170                Int *,
171                Int*,
172                Int*,
173                Float*,
174                Int*,
175                Int*,
176                Double*);
177}
[2384]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,
[2381]196                           Int *chanMap,
197                           Int *polMap )
198{
199  // parameters for gridding
200  Int idopsf = 0 ;
[2384]201  Int len = npol*nchan ;
[2381]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
[2384]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 ) ;
[2385]221
222  // pass copy of irow to ggridsd since it will be modified in theroutine
223  Int irowCopy = irow ;
[2384]224     
[2381]225  // call ggridsd
[2384]226  ggridsd( xy_p,
227           values_p,
228           &nvispol,
229           &nvischan,
[2381]230           &idopsf,
[2384]231           flag_p,
232           rflag_p,
233           wgt_p,
234           &nrow,
[2385]235           &irowCopy,
[2384]236           grid_p,
237           wgrid_p,
238           &nx,
239           &ny,
240           &npol,
241           &nchan,
242           &support,
243           &sampling,
244           conv_p,
[2381]245           chanMap,
246           polMap,
247           sumw_p ) ;
248
249  // finalization
[2384]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 ) ;
[2381]258  delete sumw_p ;
259}
260
[2396]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  }
[2383]335
[2396]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
[2383]406void STGrid::grid()
407{
[2384]408  LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
[2385]409  double t0,t1 ;
[2384]410
[2383]411  // data selection
[2385]412  t0 = mathutil::gettimeofday_sec() ;
[2383]413  selectData() ;
[2385]414  t1 = mathutil::gettimeofday_sec() ;
[2438]415  os << LogIO::DEBUGGING << "selectData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2385]416
[2388]417  setupGrid() ;
[2383]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
[2384]429  // grid parameter
430  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 ;
436  os << "----------" << endl ;
437  os << "Grid parameter summary" << endl ;
438  os << "   (nx,ny) = (" << nx_ << "," << ny_ << ")" << endl ;
439  os << "   (cellx,celly) = (" << cellx_ << "," << celly_ << ")" << endl ;
440  os << "   center = " << center_ << endl ;
441  os << "   weighting = " << wtype_ << endl ;
[2386]442  os << "   convfunc = " << convType_ << " with support " << convSupport_ << endl ;
[2396]443  os << "   doclip = " << (doclip_?"True":"False") << endl ;
[2384]444  os << "----------" << LogIO::POST ;
445  os << LogIO::NORMAL ;
446
[2398]447  if ( doclip_ )
[2396]448    gridPerRowWithClipping() ;
[2383]449  else
450    gridPerRow() ;
451}
452
[2398]453void STGrid::updateChunkShape()
[2383]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
[2385]457  nchunk_ = 400 ;
[2383]458  nchunk_ = min( nchunk_, nrow_ ) ;
[2393]459  vshape_ = IPosition( 1, nchunk_ ) ;
460  wshape_ = IPosition( 2, nchan_, nchunk_ ) ;
461  dshape_ = IPosition( 2, 2, nchunk_ ) ;
[2383]462}
463
[2393]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
[2396]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
[2393]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
[2396]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};
[2393]537
[2396]538
[2393]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
[2378]615void STGrid::gridPerRow()
616{
617  LogIO os( LogOrigin("STGrid", "gridPerRow", WHERE) ) ;
618  double t0, t1 ;
619
620
[2379]621  // grid data
622  // Extend grid plane with convSupport_
[2393]623  //   Int gnx = nx_+convSupport_*2 ;
624  //   Int gny = ny_+convSupport_*2 ;
625  Int gnx = nx_;
626  Int gny = ny_;
627
[2378]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 ;
[2393]633  STCommonData common = STCommonData(gshape, data_);
634  common.gnx = gnx ;
635  common.gny = gny ;
[2378]636
[2379]637  // parameters for gridding
638  Int *chanMap = new Int[nchan_] ;
[2393]639  for ( Int i = 0 ; i < nchan_ ; i++ ) {
640    chanMap[i] = i ;
[2379]641  }
[2393]642  common.chanMap = chanMap;
[2379]643
[2393]644  // convolution kernel
645  t0 = mathutil::gettimeofday_sec() ;
646  setConvFunc( common.convFunc ) ;
647  t1 = mathutil::gettimeofday_sec() ;
[2438]648  os << LogIO::DEBUGGING << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2393]649
[2379]650  // for performance check
[2393]651  eGetData_ = 0.0 ;
652  eToPixel_ = 0.0 ;
653  eGGridSD_ = 0.0 ;
[2384]654  double eInitPol = 0.0 ;
[2379]655
[2390]656  for ( uInt ifile = 0 ; ifile < nfile_ ; ifile++ ) {
657    initTable( ifile ) ;
[2380]658
[2393]659    os << "start table " << ifile << ": " << infileList_[ifile] << LogIO::POST ;   
[2398]660    Broker broker = Broker(produceChunk, consumeChunk);
[2390]661    for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
[2383]662      t0 = mathutil::gettimeofday_sec() ;
[2393]663      initPol( ipol ) ; // set ptab_ and attach()
[2383]664      t1 = mathutil::gettimeofday_sec() ;
[2390]665      eInitPol += t1-t0 ;
[2383]666     
[2393]667      STContext context(this, common, ipol);
[2390]668     
669      os << "start pol " << ipol << LogIO::POST ;
670     
[2393]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);
[2390]681      }
[2393]682#endif
683
[2390]684      os << "end pol " << ipol << LogIO::POST ;
[2393]685
[2383]686    }
[2393]687    os << "end table " << ifile << LogIO::POST ;   
[2378]688  }
[2438]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 ;
[2383]695 
[2379]696  delete chanMap ;
697
[2378]698  // set data
[2393]699  setData( common.gdataArrC, common.gwgtArr ) ;
[2379]700
[2378]701}
702
[2396]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 ;
718  // world -> pixel
719  Array<Double> xypos( context.self->dshape_ ) ;
720  t0 = mathutil::gettimeofday_sec() ;
721  context.self->toPixel( chunk->direction, xypos ) ;
722  t1 = mathutil::gettimeofday_sec() ;
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 ;
758 
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
769  // Extend grid plane with convSupport_
770  //   Int gnx = nx_+convSupport_*2 ;
771  //   Int gny = ny_+convSupport_*2 ;
772  Int gnx = nx_;
773  Int gny = ny_;
774
775  IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
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
788  Int *chanMap = new Int[nchan_] ;
789  for ( Int i = 0 ; i < nchan_ ; i++ ) {
790    chanMap[i] = i ;
791  }
792  common.chanMap = chanMap;
793
794  // convolution kernel
795  t0 = mathutil::gettimeofday_sec() ;
796  setConvFunc( common.convFunc ) ;
797  t1 = mathutil::gettimeofday_sec() ;
[2438]798  os << LogIO::DEBUGGING << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2396]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 ;   
[2398]810    Broker broker = Broker(produceChunk, consumeChunkWithClipping);
[2396]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  }
[2438]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 ;
[2396]845 
846  delete chanMap ;
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() ;
[2438]863  os << LogIO::DEBUGGING << "clipMinMax: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2396]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
[2382]960void STGrid::initPol( Int ipol )
961{
[2386]962  LogIO os( LogOrigin("STGrid","initPol",WHERE) ) ;
963  if ( npolOrg_ == 1 ) {
964    os << "single polarization data." << LogIO::POST ;
[2385]965    ptab_ = tab_ ;
[2386]966  }
[2385]967  else
[2426]968    ptab_ = tab_( tab_.col("POLNO") == pollist_[ipol] ) ;
[2382]969
970  attach( ptab_ ) ;
971}
972
[2390]973void STGrid::initTable( uInt idx )
974{
975  tab_ = tableList_[idx] ;
976  nrow_ = rows_[idx] ;
[2398]977  updateChunkShape() ;
[2390]978}
979
[2378]980void STGrid::setData( Array<Complex> &gdata,
[2368]981                      Array<Float> &gwgt )
982{
[2378]983  // 2011/12/20 TN
984  // gwgt and data_ share storage
[2368]985  LogIO os( LogOrigin("STGrid","setData",WHERE) ) ;
986  double t0, t1 ;
987  t0 = mathutil::gettimeofday_sec() ;
[2378]988  uInt len = data_.nelements() ;
[2374]989  const Complex *w1_p ;
[2378]990  Float *w2_p ;
[2379]991  Bool b1, b2 ;
[2374]992  const Complex *gdata_p = gdata.getStorage( b1 ) ;
[2378]993  Float *gwgt_p = data_.getStorage( b2 ) ;
[2368]994  w1_p = gdata_p ;
995  w2_p = gwgt_p ;
996  for ( uInt i = 0 ; i < len ; i++ ) {
[2378]997    if ( *w2_p > 0.0 ) *w2_p = (*w1_p).real() / *w2_p ;
[2368]998    w1_p++ ;
999    w2_p++ ;
1000  }
1001  gdata.freeStorage( gdata_p, b1 ) ;
[2378]1002  data_.putStorage( gwgt_p, b2 ) ;
[2368]1003  t1 = mathutil::gettimeofday_sec() ;
[2438]1004  os << LogIO::DEBUGGING << "setData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2368]1005}
1006
[2388]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_ ) ;
1014}
1015
[2356]1016void STGrid::setupGrid( Int &nx,
1017                        Int &ny,
1018                        String &cellx,
1019                        String &celly,
1020                        Double &xmin,
1021                        Double &xmax,
1022                        Double &ymin,
1023                        Double &ymax,
1024                        String &center )
1025{
[2375]1026  LogIO os( LogOrigin("STGrid","setupGrid",WHERE) ) ;
[2356]1027  //cout << "nx=" << nx << ", ny=" << ny << endl ;
[2359]1028
1029  // center position
1030  if ( center.size() == 0 ) {
1031    center_(0) = 0.5 * ( xmin + xmax ) ;
1032    center_(1) = 0.5 * ( ymin + ymax ) ;
1033  }
1034  else {
1035    String::size_type pos0 = center.find( " " ) ;
1036    if ( pos0 == String::npos ) {
1037      throw AipsError( "bad string format in parameter center" ) ;
1038    }
1039    String::size_type pos1 = center.find( " ", pos0+1 ) ;
1040    String typestr, xstr, ystr ;
1041    if ( pos1 != String::npos ) {
1042      typestr = center.substr( 0, pos0 ) ;
1043      xstr = center.substr( pos0+1, pos1-pos0 ) ;
1044      ystr = center.substr( pos1+1 ) ;
1045      // todo: convert to J2000 (or direction ref for DIRECTION column)
1046    }
1047    else {
1048      typestr = "J2000" ;
1049      xstr = center.substr( 0, pos0 ) ;
1050      ystr = center.substr( pos0+1 ) ;
1051    }
1052    QuantumHolder qh ;
1053    String err ;
1054    qh.fromString( err, xstr ) ;
1055    Quantum<Double> xcen = qh.asQuantumDouble() ;
1056    qh.fromString( err, ystr ) ;
1057    Quantum<Double> ycen = qh.asQuantumDouble() ;
1058    center_(0) = xcen.getValue( "rad" ) ;
1059    center_(1) = ycen.getValue( "rad" ) ;
[2461]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 ;
[2359]1083  }
[2461]1084//   os << "xmin=" << xmin << LogIO::POST ;
1085//   os << "center_=" << center_ << LogIO::POST ;
[2359]1086
1087
[2356]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  }
[2375]1098
1099  //Double wx = xmax - xmin ;
1100  //Double wy = ymax - ymin ;
1101  Double wx = max( abs(xmax-center_(0)), abs(xmin-center_(0)) ) * 2 ;
1102  Double wy = max( abs(ymax-center_(1)), abs(ymin-center_(1)) ) * 2 ;
1103  // take 10% margin
1104  wx *= 1.10 ;
1105  wy *= 1.10 ;
1106
1107  Quantum<Double> qcellx ;
1108  Quantum<Double> qcelly ;
[2356]1109  //cout << "nx_ = " << nx_ << ",  ny_ = " << ny_ << endl ;
1110  if ( cellx.size() != 0 && celly.size() != 0 ) {
1111    readQuantity( qcellx, cellx ) ;
1112    readQuantity( qcelly, celly ) ;
1113  }
1114  else if ( celly.size() != 0 ) {
[2375]1115    os << "Using celly to x-axis..." << LogIO::POST ;
[2356]1116    readQuantity( qcelly, celly ) ;
1117    qcellx = qcelly ;
1118  }
1119  else if ( cellx.size() != 0 ) {
[2375]1120    os << "Using cellx to y-axis..." << LogIO::POST ;
[2356]1121    readQuantity( qcellx, cellx ) ;
1122    qcelly = qcellx ;
1123  }
1124  else {
1125    if ( nx_ < 0 ) {
[2375]1126      os << "No user preference in grid setting. Using default..." << LogIO::POST ;
[2356]1127      readQuantity( qcellx, "1.0arcmin" ) ;
1128      qcelly = qcellx ;
1129    }
1130    else {
[2375]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      }
[2356]1139      qcellx = Quantum<Double>( wx/nx_, "rad" ) ;
1140      qcelly = Quantum<Double>( wy/ny_, "rad" ) ;
1141    }
1142  }
1143  cellx_ = qcellx.getValue( "rad" ) ;
[2422]1144  // DEC correction
1145  cellx_ /= cos( center_[1] ) ;
[2356]1146  celly_ = qcelly.getValue( "rad" ) ;
[2422]1147  //os << "cellx_=" << cellx_ << ", celly_=" << celly_ << ", cos("<<center_(1)<<")=" << cos(center_(1)) << LogIO::POST ;
[2356]1148  if ( nx_ < 0 ) {
[2375]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    }
[2356]1157    nx_ = Int( ceil( wx/cellx_ ) ) ;
1158    ny_ = Int( ceil( wy/celly_ ) ) ;
1159  }
1160}
1161
[2388]1162void STGrid::mapExtent( Double &xmin, Double &xmax,
1163                        Double &ymin, Double &ymax )
1164{
1165  //LogIO os( LogOrigin("STGrid","mapExtent",WHERE) ) ;
[2390]1166  directionCol_.attach( tableList_[0], "DIRECTION" ) ;
1167  Matrix<Double> direction = directionCol_.getColumn() ;
[2388]1168  //os << "dirCol.nrow() = " << dirCol.nrow() << LogIO::POST ;
1169  minMax( xmin, xmax, direction.row( 0 ) ) ;
1170  minMax( ymin, ymax, direction.row( 1 ) ) ;
[2390]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  }
[2388]1183  //os << "(xmin,xmax)=(" << xmin << "," << xmax << ")" << LogIO::POST ;
1184  //os << "(ymin,ymax)=(" << ymin << "," << ymax << ")" << LogIO::POST ;
1185}
1186
[2593]1187void STGrid::table( Table &tab, uInt i )
1188{
1189  if ( i >= 0 && i < nfile_ )
1190    tab = Table( infileList_[i] ) ;
1191}
1192
[2379]1193void STGrid::selectData()
[2362]1194{
[2386]1195  LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;   
[2438]1196  Int ifno = ifno_ ;
[2390]1197  tableList_.resize( nfile_ ) ;
[2418]1198  if ( ifno_ == -1 ) {
[2593]1199    //Table taborg( infileList_[0] ) ;
1200    Table taborg ;
1201    table( taborg, 0 ) ;
[2393]1202    ROScalarColumn<uInt> ifnoCol( taborg, "IFNO" ) ;
[2418]1203    ifno_ = ifnoCol( 0 ) ;
[2393]1204    os << LogIO::WARN
[2418]1205       << "IFNO is not given. Using default IFNO: " << ifno_ << LogIO::POST ;
[2393]1206  }
[2390]1207  for ( uInt i = 0 ; i < nfile_ ; i++ ) {
[2593]1208    //Table taborg( infileList_[i] ) ;
1209    Table taborg ;
1210    table( taborg, i ) ;
[2389]1211    TableExprNode node ;
[2438]1212    if ( ifno != -1 || isMultiIF( taborg ) ) {
[2389]1213      os << "apply selection on IFNO" << LogIO::POST ;
[2418]1214      node = taborg.col("IFNO") == ifno_ ;
[2389]1215    }
1216    if ( scanlist_.size() > 0 ) {
1217      os << "apply selection on SCANNO" << LogIO::POST ;
1218      node = node && taborg.col("SCANNO").in( scanlist_ ) ;
1219    }
1220    if ( node.isNull() ) {
1221      tableList_[i] = taborg ;
1222    }
1223    else {
1224      tableList_[i] = taborg( node ) ;
1225    }
[2438]1226    os << LogIO::DEBUGGING << "tableList_[" << i << "].nrow()=" << tableList_[i].nrow() << LogIO::POST ;
[2389]1227    if ( tableList_[i].nrow() == 0 ) {
1228      os << LogIO::SEVERE
[2418]1229         << "No corresponding rows for given selection: IFNO " << ifno_ ;
[2389]1230      if ( scanlist_.size() > 0 )
1231        os << " SCANNO " << scanlist_ ;
1232      os << LogIO::EXCEPTION ;
1233    }
[2362]1234  }
1235}
1236
[2386]1237Bool STGrid::isMultiIF( Table &tab )
1238{
1239  ROScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
1240  Vector<uInt> ifnos = ifnoCol.getColumn() ;
1241  return anyNE( ifnos, ifnos[0] ) ;
1242}
1243
[2382]1244void STGrid::attach( Table &tab )
[2379]1245{
1246  // attach to table
[2382]1247  spectraCol_.attach( tab, "SPECTRA" ) ;
1248  flagtraCol_.attach( tab, "FLAGTRA" ) ;
1249  directionCol_.attach( tab, "DIRECTION" ) ;
1250  flagRowCol_.attach( tab, "FLAGROW" ) ;
1251  tsysCol_.attach( tab, "TSYS" ) ;
1252  intervalCol_.attach( tab, "INTERVAL" ) ;
[2379]1253}
1254
[2393]1255Int STGrid::getDataChunk(
1256                         IPosition const &wshape,
1257                         IPosition const &vshape,
1258                         IPosition const &dshape,
1259                         Array<Complex> &spectra,
1260                         Array<Double> &direction,
1261                         Array<Int> &flagtra,
1262                         Array<Int> &rflag,
1263                         Array<Float> &weight )
1264{
1265  LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
1266
1267  Array<Float> spectraF_(wshape);
1268  Array<uChar> flagtraUC_(wshape);
1269  Array<uInt> rflagUI_(vshape);
1270  Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
1271  if ( nrow < nchunk_ ) {
1272    spectra.resize( spectraF_.shape() ) ;
1273    flagtra.resize( flagtraUC_.shape() ) ;
1274    rflag.resize( rflagUI_.shape() ) ;
1275  }
1276  double t0, t1 ;
1277  t0 = mathutil::gettimeofday_sec() ;
1278  convertArray( spectra, spectraF_ ) ;
1279  toInt( flagtraUC_, flagtra ) ;
1280  toInt( rflagUI_, rflag ) ;
1281  t1 = mathutil::gettimeofday_sec() ;
1282  eToInt = t1 - t0 ;
1283 
1284  return nrow ;
1285}
1286
1287#if 0
[2379]1288Int STGrid::getDataChunk( Array<Complex> &spectra,
1289                          Array<Double> &direction,
1290                          Array<Int> &flagtra,
1291                          Array<Int> &rflag,
1292                          Array<Float> &weight )
[2378]1293{
[2385]1294  LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
[2381]1295  Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
[2383]1296  if ( nrow < nchunk_ ) {
1297    spectra.resize( spectraF_.shape() ) ;
1298    flagtra.resize( flagtraUC_.shape() ) ;
1299    rflag.resize( rflagUI_.shape() ) ;
1300  }
[2381]1301  double t0, t1 ;
1302  t0 = mathutil::gettimeofday_sec() ;
1303  convertArray( spectra, spectraF_ ) ;
[2382]1304  toInt( flagtraUC_, flagtra ) ;
1305  toInt( rflagUI_, rflag ) ;
[2381]1306  t1 = mathutil::gettimeofday_sec() ;
[2384]1307  eToInt = t1 - t0 ;
[2381]1308 
[2379]1309  return nrow ;
[2378]1310}
[2393]1311#endif
[2378]1312
[2379]1313Int STGrid::getDataChunk( Array<Float> &spectra,
1314                          Array<Double> &direction,
1315                          Array<uChar> &flagtra,
1316                          Array<uInt> &rflag,
1317                          Array<Float> &weight )
[2375]1318{
[2379]1319  LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
[2383]1320  Int nrow = spectra.shape()[1] ;
1321  Int remainingRow = nrow_ - nprocessed_ ;
1322  if ( remainingRow < nrow ) {
1323    nrow = remainingRow ;
1324    IPosition mshape( 2, nchan_, nrow ) ;
1325    IPosition vshape( 1, nrow ) ;
1326    spectra.resize( mshape ) ;
1327    flagtra.resize( mshape ) ;
1328    direction.resize( IPosition(2,2,nrow) ) ;
1329    rflag.resize( vshape ) ;
1330    weight.resize( mshape ) ;
[2379]1331  }
[2383]1332  // 2011/12/22 TN
1333  // tsys shares its storage with weight
1334  Array<Float> tsys( weight ) ;
1335  Array<Double> tint( rflag.shape() ) ;
[2380]1336
[2383]1337  Vector<uInt> rflagVec( rflag ) ;
1338  Vector<Double> tintVec( tint ) ;
[2379]1339
[2383]1340  RefRows rows( nprocessed_, nprocessed_+nrow-1, 1 ) ;
[2386]1341  //os<<LogIO::DEBUGGING<<"nprocessed_="<<nprocessed_<<": rows.nrows()="<<rows.nrows()<<LogIO::POST ;
[2383]1342  spectraCol_.getColumnCells( rows, spectra ) ;
1343  flagtraCol_.getColumnCells( rows, flagtra ) ;
1344  directionCol_.getColumnCells( rows, direction ) ;
1345  flagRowCol_.getColumnCells( rows, rflagVec ) ;
1346  intervalCol_.getColumnCells( rows, tintVec ) ;
1347  Vector<Float> tsysTemp = tsysCol_( nprocessed_ ) ;
1348  if ( tsysTemp.nelements() == (uInt)nchan_ )
1349    tsysCol_.getColumnCells( rows, tsys ) ;
1350  else
1351    tsys = tsysTemp[0] ;
[2385]1352
[2384]1353  double t0,t1 ;
1354  t0 = mathutil::gettimeofday_sec() ;
[2379]1355  getWeight( weight, tsys, tint ) ;
[2384]1356  t1 = mathutil::gettimeofday_sec() ;
1357  eGetWeight += t1-t0 ;
[2379]1358
1359  nprocessed_ += nrow ;
1360 
1361  return nrow ;
1362}
1363
1364void STGrid::setupArray()
1365{
[2376]1366  LogIO os( LogOrigin("STGrid","setupArray",WHERE) ) ;
[2390]1367  ROScalarColumn<uInt> polnoCol( tableList_[0], "POLNO" ) ;
[2371]1368  Vector<uInt> pols = polnoCol.getColumn() ;
[2376]1369  //os << pols << LogIO::POST ;
[2371]1370  Vector<uInt> pollistOrg ;
[2386]1371  npolOrg_ = 0 ;
[2371]1372  uInt polno ;
1373  for ( uInt i = 0 ; i < polnoCol.nrow() ; i++ ) {
[2393]1374    //polno = polnoCol( i ) ;
[2371]1375    polno = pols( i ) ;
1376    if ( allNE( pollistOrg, polno ) ) {
[2386]1377      pollistOrg.resize( npolOrg_+1, True ) ;
1378      pollistOrg[npolOrg_] = polno ;
1379      npolOrg_++ ;
[2371]1380    }
1381  }
1382  if ( pollist_.size() == 0 )
1383    pollist_ = pollistOrg ;
1384  else {
1385    Vector<uInt> newlist ;
1386    uInt newsize = 0 ;
1387    for ( uInt i = 0 ; i < pollist_.size() ; i++ ) {
1388      if ( anyEQ( pollistOrg, pollist_[i] ) ) {
1389        newlist.resize( newsize+1, True ) ;
1390        newlist[newsize] = pollist_[i] ;
1391        newsize++ ;
1392      }
1393    }
1394    pollist_.assign( newlist ) ;
1395  }
1396  npol_ = pollist_.size() ;
1397  if ( npol_ == 0 ) {
1398    os << LogIO::SEVERE << "Empty pollist" << LogIO::EXCEPTION ;
1399  }
[2390]1400  rows_.resize( nfile_ ) ;
1401  for ( uInt i = 0 ; i < nfile_ ; i++ ) {
1402    rows_[i] = tableList_[i].nrow() / npolOrg_ ;
[2398]1403    //if ( nrow_ < rows_[i] )
1404    //  nrow_ = rows_[i] ;
[2390]1405  }
1406  flagtraCol_.attach( tableList_[0], "FLAGTRA" ) ;
1407  nchan_ = flagtraCol_( 0 ).nelements() ;
[2371]1408//   os << "npol_ = " << npol_ << "(" << pollist_ << ")" << endl
1409//      << "nchan_ = " << nchan_ << endl
1410//      << "nrow_ = " << nrow_ << LogIO::POST ;
1411}
1412
[2375]1413void STGrid::getWeight( Array<Float> &w,
[2382]1414                              Array<Float> &tsys,
1415                              Array<Double> &tint )
1416{
[2383]1417  LogIO os( LogOrigin("STGrid","getWeight",WHERE) ) ;
[2384]1418
[2382]1419  // 2011/12/22 TN
1420  // w (weight) and tsys share storage
1421  IPosition refShape = tsys.shape() ;
1422  Int nchan = refShape[0] ;
1423  Int nrow = refShape[1] ;
1424//   os << "nchan=" << nchan << ", nrow=" << nrow << LogIO::POST ;
1425//   os << "w.shape()=" << w.shape() << endl
1426//      << "tsys.shape()=" << tsys.shape() << endl
1427//      << "tint.shape()=" << tint.shape() << LogIO::POST ;
1428
1429  // set weight
1430  if ( wtype_.compare( "UNIFORM" ) == 0 ) {
1431    w = 1.0 ;
1432  }
1433  else if ( wtype_.compare( "TINT" ) == 0 ) {
1434    Bool b0, b1 ;
1435    Float *w_p = w.getStorage( b0 ) ;
1436    Float *w0_p = w_p ;
1437    const Double *ti_p = tint.getStorage( b1 ) ;
1438    const Double *w1_p = ti_p ;
1439    for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1440      for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1441        *w0_p = *w1_p ;
1442        w0_p++ ;
1443      }
1444      w1_p++ ;
1445    }
1446    w.putStorage( w_p, b0 ) ;
1447    tint.freeStorage( ti_p, b1 ) ;
1448  }
1449  else if ( wtype_.compare( "TSYS" ) == 0 ) {
1450    Bool b0 ;
1451    Float *w_p = w.getStorage( b0 ) ;
1452    Float *w0_p = w_p ;
1453    for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1454      for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1455        Float temp = *w0_p ;
1456        *w0_p = 1.0 / ( temp * temp ) ;
1457        w0_p++ ;
1458      }
1459    }
1460    w.putStorage( w_p, b0 ) ;
1461  }
1462  else if ( wtype_.compare( "TINTSYS" ) == 0 ) {
1463    Bool b0, b1 ;
1464    Float *w_p = w.getStorage( b0 ) ;
1465    Float *w0_p = w_p ;
1466    const Double *ti_p = tint.getStorage( b1 ) ;
1467    const Double *w1_p = ti_p ;
1468    for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1469      Float interval = *w1_p ;
1470      for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1471        Float temp = *w0_p ;
1472        *w0_p = interval / ( temp * temp ) ;
1473        w0_p++ ;
1474      }
1475      w1_p++ ;
1476    }
1477    w.putStorage( w_p, b0 ) ;
1478    tint.freeStorage( ti_p, b1 ) ;
1479  }
1480  else {
1481    //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
1482    //os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
1483    w = 1.0 ;
1484  }
1485}
1486
[2375]1487void STGrid::toInt( Array<uChar> &u, Array<Int> &v )
[2356]1488{
[2375]1489  uInt len = u.nelements() ;
[2356]1490  Int *int_p = new Int[len] ;
1491  Bool deleteIt ;
[2375]1492  const uChar *data_p = u.getStorage( deleteIt ) ;
[2356]1493  Int *i_p = int_p ;
1494  const uChar *u_p = data_p ;
1495  for ( uInt i = 0 ; i < len ; i++ ) {
1496    *i_p = ( *u_p == 0 ) ? 0 : 1 ;
1497    i_p++ ;
1498    u_p++ ;
1499  }
[2375]1500  u.freeStorage( data_p, deleteIt ) ;
1501  v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
[2356]1502}
1503
[2375]1504void STGrid::toInt( Array<uInt> &u, Array<Int> &v )
[2356]1505{
[2375]1506  uInt len = u.nelements() ;
[2356]1507  Int *int_p = new Int[len] ;
1508  Bool deleteIt ;
[2375]1509  const uInt *data_p = u.getStorage( deleteIt ) ;
[2356]1510  Int *i_p = int_p ;
1511  const uInt *u_p = data_p ;
1512  for ( uInt i = 0 ; i < len ; i++ ) {
1513    *i_p = ( *u_p == 0 ) ? 0 : 1 ;
1514    i_p++ ;
1515    u_p++ ;
1516  }
[2375]1517  u.freeStorage( data_p, deleteIt ) ;
1518  v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
[2356]1519}
1520
[2375]1521void STGrid::toPixel( Array<Double> &world, Array<Double> &pixel )
[2356]1522{
[2359]1523  // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_)
1524  // grid plane to avoid unexpected behavior on grid edge
[2375]1525  Block<Double> pixc( 2 ) ;
1526  pixc[0] = Double( nx_-1 ) * 0.5 ;
1527  pixc[1] = Double( ny_-1 ) * 0.5 ;
1528//   pixc[0] = Double( nx_+2*convSupport_-1 ) * 0.5 ;
1529//   pixc[1] = Double( ny_+2*convSupport_-1 ) * 0.5 ;
[2356]1530  uInt nrow = world.shape()[1] ;
[2375]1531  Bool bw, bp ;
1532  const Double *w_p = world.getStorage( bw ) ;
1533  Double *p_p = pixel.getStorage( bp ) ;
1534  const Double *ww_p = w_p ;
1535  Double *wp_p = p_p ;
1536  for ( uInt i = 0 ; i < nrow ; i++ ) {
1537    *wp_p = pixc[0] + ( *ww_p - center_[0] ) / cellx_ ;
1538    wp_p++ ;
1539    ww_p++ ;
1540    *wp_p = pixc[1] + ( *ww_p - center_[1] ) / celly_ ;
1541    wp_p++ ;
1542    ww_p++ ;
[2356]1543  }
[2375]1544  world.freeStorage( w_p, bw ) ;
1545  pixel.putStorage( p_p, bp ) ; 
[2356]1546}
1547
1548void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize )
1549{
1550  convFunc = 0.0 ;
1551  for ( Int i = 0 ; i < convSize/2 ; i++ )
1552    convFunc(i) = 1.0 ;
1553}
1554
1555#define NEED_UNDERSCORES
1556#if defined(NEED_UNDERSCORES)
1557#define grdsf grdsf_
1558#endif
1559extern "C" {
1560   void grdsf(Double*, Double*);
1561}
1562void STGrid::spheroidalFunc( Vector<Float> &convFunc )
1563{
1564  convFunc = 0.0 ;
1565  for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
1566    Double nu = Double(i) / Double(convSupport_*convSampling_) ;
1567    Double val ;
1568    grdsf( &nu, &val ) ;
1569    convFunc(i) = ( 1.0 - nu * nu ) * val ;
1570  }
1571}
1572
1573void STGrid::gaussFunc( Vector<Float> &convFunc )
1574{
1575  convFunc = 0.0 ;
[2363]1576  // HWHM of the Gaussian is convSupport_ / 4
1577  // To take into account Gaussian tail, kernel cutoff is set to 4 * HWHM
1578  Int len = convSampling_ * convSupport_ ;
1579  Double hwhm = len * 0.25 ;
1580  for ( Int i = 0 ; i < len ; i++ ) {
[2356]1581    Double val = Double(i) / hwhm ;
1582    convFunc(i) = exp( -log(2)*val*val ) ;
1583  }
1584}
1585
1586void STGrid::pbFunc( Vector<Float> &convFunc )
1587{
1588  convFunc = 0.0 ;
1589}
1590
1591void STGrid::setConvFunc( Vector<Float> &convFunc )
1592{
1593  convSupport_ = userSupport_ ;
1594  if ( convType_ == "BOX" ) {
1595    if ( convSupport_ < 0 )
1596      convSupport_ = 0 ;
1597    Int convSize = convSampling_ * ( 2 * convSupport_ + 2 )  ;
1598    convFunc.resize( convSize ) ;
1599    boxFunc( convFunc, convSize ) ;
1600  }
1601  else if ( convType_ == "SF" ) {
1602    if ( convSupport_ < 0 )
1603      convSupport_ = 3 ;
1604    Int convSize = convSampling_ * ( 2 * convSupport_ + 2 )  ;
1605    convFunc.resize( convSize ) ;
1606    spheroidalFunc( convFunc ) ;
1607  }
1608  else if ( convType_ == "GAUSS" ) {
[2363]1609    // to take into account Gaussian tail
[2356]1610    if ( convSupport_ < 0 )
[2438]1611      convSupport_ = 4 ; // 1 * 4
[2363]1612    else {
1613      convSupport_ = userSupport_ * 4 ;
1614    }
[2356]1615    Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
1616    convFunc.resize( convSize ) ;
1617    gaussFunc( convFunc ) ;
1618  }
[2359]1619  else if ( convType_ == "PB" ) {
1620    if ( convSupport_ < 0 )
1621      convSupport_ = 0 ;
[2356]1622    pbFunc( convFunc ) ;
[2359]1623  }
[2356]1624  else {
1625    throw AipsError( "Unsupported convolution function" ) ;
1626  }
1627}
1628
1629string STGrid::saveData( string outfile )
1630{
[2368]1631  LogIO os( LogOrigin("STGrid", "saveData", WHERE) ) ;
1632  double t0, t1 ;
1633  t0 = mathutil::gettimeofday_sec() ;
1634
[2393]1635  //Int polno = 0 ;
[2371]1636  String outfile_ ;
[2356]1637  if ( outfile.size() == 0 ) {
[2389]1638    if ( infileList_[0].lastchar() == '/' ) {
1639      outfile_ = infileList_[0].substr( 0, infileList_[0].size()-1 ) ;
[2356]1640    }
1641    else {
[2389]1642      outfile_ = infileList_[0] ;
[2356]1643    }
1644    outfile_ += ".grid" ;
1645  }
1646  else {
1647    outfile_ = outfile ;
1648  }
[2371]1649  Table tab ;
1650  prepareTable( tab, outfile_ ) ;
[2593]1651  fillTable( tab ) ;
1652
1653  t1 = mathutil::gettimeofday_sec() ;
1654  os << LogIO::DEBUGGING << "saveData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
1655
1656  return outfile_ ;
1657}
1658
1659void STGrid::prepareTable( Table &tab, String &name )
1660{
1661  Table t( infileList_[0], Table::Old ) ;
1662  t.deepCopy( name, Table::New, False, t.endianFormat(), True ) ;
1663  tab = Table( name, Table::Update ) ;
1664  // 2012/02/13 TN
1665  // explicitly copy subtables since no rows including subtables are
1666  // copied by Table::deepCopy with noRows=True
1667  TableCopy::copySubTables( tab, t ) ;
1668}
1669
1670void STGrid::fillTable( Table &tab )
1671{
[2356]1672  IPosition dshape = data_.shape() ;
[2361]1673  Int nrow = nx_ * ny_ * npol_ ;
1674  tab.rwKeywordSet().define( "nPol", npol_ ) ;
[2360]1675  tab.addRow( nrow ) ;
[2356]1676  Vector<Double> cpix( 2 ) ;
1677  cpix(0) = Double( nx_ - 1 ) * 0.5 ;
1678  cpix(1) = Double( ny_ - 1 ) * 0.5 ;
1679  Vector<Double> dir( 2 ) ;
1680  ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
1681  ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
1682  ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
[2478]1683  ScalarColumn<uInt> scannoCol( tab, "SCANNO" ) ;
[2356]1684  Int irow = 0 ;
[2371]1685  Vector<Float> sp( nchan_ ) ;
1686  Bool bsp, bdata ;
1687  const Float *data_p = data_.getStorage( bdata ) ;
1688  Float *wsp_p, *sp_p ;
1689  const Float *wdata_p = data_p ;
1690  long step = nx_ * ny_ * npol_ ;
1691  long offset ;
[2478]1692  uInt scanno = 0 ;
[2356]1693  for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
[2371]1694    dir(1) = center_(1) - ( cpix(1) - (Double)iy ) * celly_ ;
[2356]1695    for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
[2371]1696      dir(0) = center_(0) - ( cpix(0) - (Double)ix ) * cellx_ ;
[2361]1697      for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
[2371]1698        offset = ix + iy * nx_ + ipol * nx_ * ny_ ;
1699        //os << "offset = " << offset << LogIO::POST ;
1700        sp_p = sp.getStorage( bsp ) ;
1701        wsp_p = sp_p ;
1702        wdata_p = data_p + offset ;
1703        for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
1704          *wsp_p = *wdata_p ;
1705          wsp_p++ ;
1706          wdata_p += step ;
1707        }
1708        sp.putStorage( sp_p, bsp ) ;
[2356]1709        spectraCol.put( irow, sp ) ;
1710        directionCol.put( irow, dir ) ;
[2360]1711        polnoCol.put( irow, pollist_[ipol] ) ;
[2478]1712        scannoCol.put( irow, scanno ) ;
[2356]1713        irow++ ;
1714      }
[2478]1715      scanno++ ;
[2356]1716    }
1717  }
[2371]1718  data_.freeStorage( data_p, bdata ) ;
[2368]1719
[2413]1720  fillMainColumns( tab ) ;
[2356]1721}
1722
[2413]1723void STGrid::fillMainColumns( Table &tab )
1724{
[2414]1725  // values for fill
[2594]1726  //Table t( infileList_[0], Table::Old ) ;
1727  Table t ;
1728  table( t, 0 ) ;
[2413]1729  Table tsel = t( t.col( "IFNO" ) == (uInt)ifno_, 1 ) ;
[2414]1730  ROTableRow row( tsel ) ;
[2413]1731  row.get( 0 ) ;
1732  const TableRecord &rec = row.record() ;
1733  uInt freqId = rec.asuInt( "FREQ_ID" ) ;
[2424]1734  uInt molId = rec.asuInt( "MOLECULE_ID" ) ;
1735  uInt tcalId = rec.asuInt( "TCAL_ID" ) ;
1736  uInt focusId = rec.asuInt( "FOCUS_ID" ) ;
1737  uInt weatherId = rec.asuInt( "WEATHER_ID" ) ;
1738  String srcname = rec.asString( "SRCNAME" ) ;
1739  String fieldname = rec.asString( "FIELDNAME" ) ;
[2414]1740  Vector<Float> defaultTsys( 1, 1.0 ) ;
1741  // @todo how to set flagtra for gridded spectra?
1742  Vector<uChar> flagtra = rec.asArrayuChar( "FLAGTRA" ) ;
1743  flagtra = (uChar)0 ;
[2424]1744  Float opacity = rec.asFloat( "OPACITY" ) ;
1745  Double srcvel = rec.asDouble( "SRCVELOCITY" ) ;
1746  Vector<Double> srcpm = rec.asArrayDouble( "SRCPROPERMOTION" ) ;
1747  Vector<Double> srcdir = rec.asArrayDouble( "SRCDIRECTION" ) ;
1748  Vector<Double> scanrate = rec.asArrayDouble( "SCANRATE" ) ;
[2439]1749  Double time = rec.asDouble( "TIME" ) ;
1750  Double interval = rec.asDouble( "INTERVAL" ) ;
[2414]1751
1752  // fill columns
[2413]1753  Int nrow = tab.nrow() ;
1754  ScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
1755  ScalarColumn<uInt> freqIdCol( tab, "FREQ_ID" ) ;
[2424]1756  ScalarColumn<uInt> molIdCol( tab, "MOLECULE_ID" ) ;
1757  ScalarColumn<uInt> tcalidCol( tab, "TCAL_ID" ) ;
1758  ScalarColumn<Int> fitidCol( tab, "FIT_ID" ) ;
1759  ScalarColumn<uInt> focusidCol( tab, "FOCUS_ID" ) ;
1760  ScalarColumn<uInt> weatheridCol( tab, "WEATHER_ID" ) ;
[2414]1761  ArrayColumn<uChar> flagtraCol( tab, "FLAGTRA" ) ;
[2424]1762  ScalarColumn<uInt> rflagCol( tab, "FLAGROW" ) ;
[2414]1763  ArrayColumn<Float> tsysCol( tab, "TSYS" ) ;
[2424]1764  ScalarColumn<String> srcnameCol( tab, "SRCNAME" ) ;
1765  ScalarColumn<String> fieldnameCol( tab, "FIELDNAME" ) ;
1766  ScalarColumn<Int> srctypeCol( tab, "SRCTYPE" ) ;
1767  ScalarColumn<Float> opacityCol( tab, "OPACITY" ) ;
1768  ScalarColumn<Double> srcvelCol( tab, "SRCVELOCITY" ) ;
1769  ArrayColumn<Double> srcpmCol( tab, "SRCPROPERMOTION" ) ;
1770  ArrayColumn<Double> srcdirCol( tab, "SRCDIRECTION" ) ;
1771  ArrayColumn<Double> scanrateCol( tab, "SCANRATE" ) ;
[2439]1772  ScalarColumn<Double> timeCol( tab, "TIME" ) ;
1773  ScalarColumn<Double> intervalCol( tab, "INTERVAL" ) ;
[2413]1774  for ( Int i = 0 ; i < nrow ; i++ ) {
1775    ifnoCol.put( i, (uInt)ifno_ ) ;
1776    freqIdCol.put( i, freqId ) ;
[2424]1777    molIdCol.put( i, molId ) ;
1778    tcalidCol.put( i, tcalId ) ;
1779    fitidCol.put( i, -1 ) ;
1780    focusidCol.put( i, focusId ) ;
1781    weatheridCol.put( i, weatherId ) ;
[2414]1782    flagtraCol.put( i, flagtra ) ;
[2424]1783    rflagCol.put( i, 0 ) ;
[2414]1784    tsysCol.put( i, defaultTsys ) ;
[2424]1785    srcnameCol.put( i, srcname ) ;
1786    fieldnameCol.put( i, fieldname ) ;
1787    srctypeCol.put( i, (Int)SrcType::PSON ) ;
1788    opacityCol.put( i, opacity ) ;
1789    srcvelCol.put( i, srcvel ) ;
1790    srcpmCol.put( i, srcpm ) ;
1791    srcdirCol.put( i, srcdir ) ;
1792    scanrateCol.put( i, scanrate ) ;
[2439]1793    timeCol.put( i, time ) ;
1794    intervalCol.put( i, interval ) ;
[2413]1795  }
[2371]1796}
[2413]1797
[2593]1798// STGrid2
1799STGrid2::STGrid2()
1800  : STGrid()
1801{
[2413]1802}
[2593]1803
1804STGrid2::STGrid2( const ScantableWrapper &s )
1805  : STGrid()
1806{
1807  setScantable( s ) ;
1808}
1809
1810STGrid2::STGrid2( const vector<ScantableWrapper> &v )
1811  : STGrid()
1812{
1813  setScantableList( v ) ;
1814}
1815
1816void STGrid2::setScantable( const ScantableWrapper &s )
1817{
1818  nfile_ = 1 ;
1819  dataList_.resize( nfile_ ) ;
1820  dataList_[0] = s ;
1821  infileList_.resize( nfile_ ) ;
1822  infileList_[0] = s.getCP()->table().tableName() ;
1823}
1824
1825void STGrid2::setScantableList( const vector<ScantableWrapper> &v )
1826{
1827  nfile_ = v.size() ;
1828  dataList_.resize( nfile_ ) ;
1829  infileList_.resize( nfile_ ) ;
1830  for ( uInt i = 0 ; i < nfile_ ; i++ ) {
1831    dataList_[i] = v[i] ;
1832    infileList_[i] = v[i].getCP()->table().tableName() ;
1833  }
1834}
1835
[2594]1836ScantableWrapper STGrid2::getResultAsScantable( int tp )
[2593]1837{
[2594]1838  Table::TableType ttype = (tp==0) ? Table::Memory : Table::Plain ;
1839  ScantableWrapper sw( ttype ) ;
1840  CountedPtr<Scantable> s = sw.getCP() ;
[2593]1841  s->setHeader( dataList_[0].getCP()->getHeader() ) ;
1842  Table tout, tin ;
1843  String subt[] = { "FREQUENCIES", "FOCUS", "WEATHER",
1844                    "TCAL", "MOLECULES", "HISTORY", "FIT" } ;
1845  for ( uInt i = 0 ; i < 7 ; i++ ) {
1846    tout = s->table().rwKeywordSet().asTable(subt[i]) ;
1847    tin = dataList_[0].getCP()->table().rwKeywordSet().asTable(subt[i]) ;
1848    TableCopy::copyRows( tout, tin ) ;
1849  }
1850  fillTable( s->table() ) ;
1851  return sw ;
1852}
1853
1854void STGrid2::table( Table &tab, uInt i )
1855{
1856  if ( i < nfile_ )
1857    tab = dataList_[i].getCP()->table() ;
1858}
1859
1860}
Note: See TracBrowser for help on using the repository browser.