source: trunk/src/STGrid.cpp@ 2417

Last change on this file since 2417 was 2414, checked in by Takeshi Nakazato, 13 years ago

New Development: No

JIRA Issue: Yes CAS-2816

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Fill Main columns: FLAGTRA, TSYS


File size: 47.6 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
[2364]30#include <MathUtils.h>
31
[2356]32#include "STGrid.h"
33
34using namespace std ;
[2393]35using namespace concurrent ;
[2356]36using namespace casa ;
37using namespace asap ;
38
39namespace asap {
40
[2384]41// for performance check
42double eToInt = 0.0 ;
43double eGetWeight = 0.0 ;
44
[2356]45// constructor
46STGrid::STGrid()
[2393]47 : vshape_( 1 ), wshape_( 2 ), dshape_( 2 )
[2356]48{
49 init() ;
50}
51
52STGrid::STGrid( const string infile )
[2393]53 : vshape_( 1 ), wshape_( 2 ), dshape_( 2 )
[2356]54{
55 init() ;
56
57 setFileIn( infile ) ;
58}
59
[2390]60STGrid::STGrid( const vector<string> infile )
61{
62 init() ;
63
64 setFileList( infile ) ;
65}
66
[2356]67void STGrid::init()
68{
[2362]69 ifno_ = -1 ;
[2356]70 nx_ = -1 ;
71 ny_ = -1 ;
[2361]72 npol_ = 0 ;
73 nchan_ = 0 ;
74 nrow_ = 0 ;
[2356]75 cellx_ = 0.0 ;
76 celly_ = 0.0 ;
77 center_ = Vector<Double> ( 2, 0.0 ) ;
78 convType_ = "BOX" ;
[2361]79 wtype_ = "UNIFORM" ;
[2356]80 convSupport_ = -1 ;
81 userSupport_ = -1 ;
82 convSampling_ = 100 ;
[2379]83 nprocessed_ = 0 ;
84 nchunk_ = 0 ;
[2388]85
86 // initialize user input
87 nxUI_ = -1 ;
88 nyUI_ = -1 ;
89 cellxUI_ = "" ;
90 cellyUI_ = "" ;
91 centerUI_ = "" ;
[2396]92 doclip_ = False ;
[2356]93}
94
95void STGrid::setFileIn( const string infile )
96{
[2393]97 nfile_ = 1 ;
[2356]98 String name( infile ) ;
[2393]99 infileList_.resize( nfile_ ) ;
100 infileList_[0] = String(infile) ;
[2356]101}
102
[2390]103void STGrid::setFileList( const vector<string> infile )
104{
105 nfile_ = infile.size() ;
106 infileList_.resize( nfile_ ) ;
107 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
108 infileList_[i] = infile[i] ;
109 }
110}
111
[2360]112void STGrid::setPolList( vector<unsigned int> pols )
113{
114 pollist_.assign( Vector<uInt>( pols ) ) ;
115}
116
[2364]117void STGrid::setScanList( vector<unsigned int> scans )
118{
119 scanlist_.assign( Vector<uInt>( scans ) ) ;
120}
121
[2361]122void STGrid::setWeight( const string wType )
123{
124 wtype_ = String( wType ) ;
125 wtype_.upcase() ;
126}
127
[2356]128void STGrid::defineImage( int nx,
129 int ny,
130 string scellx,
131 string scelly,
132 string scenter )
133{
[2388]134 nxUI_ = (Int)nx ;
135 nyUI_ = (Int)ny ;
136 cellxUI_ = String( scellx ) ;
137 cellyUI_ = String( scelly ) ;
138 centerUI_ = String( scenter ) ;
[2356]139}
140
[2364]141void STGrid::setFunc( string convType,
142 int convSupport )
[2356]143{
144 convType_ = String( convType ) ;
145 convType_.upcase() ;
146 userSupport_ = (Int)convSupport ;
147}
148
149#define NEED_UNDERSCORES
150#if defined(NEED_UNDERSCORES)
151#define ggridsd ggridsd_
152#endif
153extern "C" {
154 void ggridsd(Double*,
155 const Complex*,
156 Int*,
157 Int*,
158 Int*,
159 const Int*,
160 const Int*,
161 const Float*,
162 Int*,
163 Int*,
164 Complex*,
165 Float*,
166 Int*,
167 Int*,
168 Int *,
169 Int *,
170 Int*,
171 Int*,
172 Float*,
173 Int*,
174 Int*,
175 Double*);
176}
[2384]177void STGrid::call_ggridsd( Array<Double> &xypos,
178 Array<Complex> &spectra,
179 Int &nvispol,
180 Int &nvischan,
181 Array<Int> &flagtra,
182 Array<Int> &flagrow,
183 Array<Float> &weight,
184 Int &nrow,
185 Int &irow,
186 Array<Complex> &gdata,
187 Array<Float> &gwgt,
188 Int &nx,
189 Int &ny,
190 Int &npol,
191 Int &nchan,
192 Int &support,
193 Int &sampling,
194 Vector<Float> &convFunc,
[2381]195 Int *chanMap,
196 Int *polMap )
197{
198 // parameters for gridding
199 Int idopsf = 0 ;
[2384]200 Int len = npol*nchan ;
[2381]201 Double *sumw_p = new Double[len] ;
202 {
203 Double *work_p = sumw_p ;
204 for ( Int i = 0 ; i < len ; i++ ) {
205 *work_p = 0.0 ;
206 work_p++ ;
207 }
208 }
209
[2384]210 // prepare pointer
211 Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ;
212 Double *xy_p = xypos.getStorage( deletePos ) ;
213 const Complex *values_p = spectra.getStorage( deleteData ) ;
214 const Int *flag_p = flagtra.getStorage( deleteFlag ) ;
215 const Int *rflag_p = flagrow.getStorage( deleteFlagR ) ;
216 const Float *wgt_p = weight.getStorage( deleteWgt ) ;
217 Complex *grid_p = gdata.getStorage( deleteDataG ) ;
218 Float *wgrid_p = gwgt.getStorage( deleteWgtG ) ;
219 Float *conv_p = convFunc.getStorage( deleteConv ) ;
[2385]220
221 // pass copy of irow to ggridsd since it will be modified in theroutine
222 Int irowCopy = irow ;
[2384]223
[2381]224 // call ggridsd
[2384]225 ggridsd( xy_p,
226 values_p,
227 &nvispol,
228 &nvischan,
[2381]229 &idopsf,
[2384]230 flag_p,
231 rflag_p,
232 wgt_p,
233 &nrow,
[2385]234 &irowCopy,
[2384]235 grid_p,
236 wgrid_p,
237 &nx,
238 &ny,
239 &npol,
240 &nchan,
241 &support,
242 &sampling,
243 conv_p,
[2381]244 chanMap,
245 polMap,
246 sumw_p ) ;
247
248 // finalization
[2384]249 xypos.putStorage( xy_p, deletePos ) ;
250 spectra.freeStorage( values_p, deleteData ) ;
251 flagtra.freeStorage( flag_p, deleteFlag ) ;
252 flagrow.freeStorage( rflag_p, deleteFlagR ) ;
253 weight.freeStorage( wgt_p, deleteWgt ) ;
254 gdata.putStorage( grid_p, deleteDataG ) ;
255 gwgt.putStorage( wgrid_p, deleteWgtG ) ;
256 convFunc.putStorage( conv_p, deleteConv ) ;
[2381]257 delete sumw_p ;
258}
259
[2396]260#define NEED_UNDERSCORES
261#if defined(NEED_UNDERSCORES)
262#define ggridsd2 ggridsd2_
263#endif
264extern "C" {
265 void ggridsd2(Double*,
266 const Complex*,
267 Int*,
268 Int*,
269 Int*,
270 const Int*,
271 const Int*,
272 const Float*,
273 Int*,
274 Int*,
275 Complex*,
276 Float*,
277 Int*,
278 Complex*,
279 Float*,
280 Float*,
281 Complex*,
282 Float*,
283 Float*,
284 Int*,
285 Int*,
286 Int *,
287 Int *,
288 Int*,
289 Int*,
290 Float*,
291 Int*,
292 Int*,
293 Double*);
294}
295void STGrid::call_ggridsd2( Array<Double> &xypos,
296 Array<Complex> &spectra,
297 Int &nvispol,
298 Int &nvischan,
299 Array<Int> &flagtra,
300 Array<Int> &flagrow,
301 Array<Float> &weight,
302 Int &nrow,
303 Int &irow,
304 Array<Complex> &gdata,
305 Array<Float> &gwgt,
306 Array<Int> &npoints,
307 Array<Complex> &clipmin,
308 Array<Float> &clipwmin,
309 Array<Float> &clipcmin,
310 Array<Complex> &clipmax,
311 Array<Float> &clipwmax,
312 Array<Float> &clipcmax,
313 Int &nx,
314 Int &ny,
315 Int &npol,
316 Int &nchan,
317 Int &support,
318 Int &sampling,
319 Vector<Float> &convFunc,
320 Int *chanMap,
321 Int *polMap )
322{
323 // parameters for gridding
324 Int idopsf = 0 ;
325 Int len = npol*nchan ;
326 Double *sumw_p = new Double[len] ;
327 {
328 Double *work_p = sumw_p ;
329 for ( Int i = 0 ; i < len ; i++ ) {
330 *work_p = 0.0 ;
331 work_p++ ;
332 }
333 }
[2383]334
[2396]335 // prepare pointer
336 Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG, deleteNpts, deleteCMin, deleteCWMin, deleteCCMin, deleteCMax, deleteCWMax, deleteCCMax ;
337 Double *xy_p = xypos.getStorage( deletePos ) ;
338 const Complex *values_p = spectra.getStorage( deleteData ) ;
339 const Int *flag_p = flagtra.getStorage( deleteFlag ) ;
340 const Int *rflag_p = flagrow.getStorage( deleteFlagR ) ;
341 const Float *wgt_p = weight.getStorage( deleteWgt ) ;
342 Complex *grid_p = gdata.getStorage( deleteDataG ) ;
343 Float *wgrid_p = gwgt.getStorage( deleteWgtG ) ;
344 Float *conv_p = convFunc.getStorage( deleteConv ) ;
345 Int *npts_p = npoints.getStorage( deleteNpts ) ;
346 Complex *cmin_p = clipmin.getStorage( deleteCMin ) ;
347 Float *cwmin_p = clipwmin.getStorage( deleteCWMin ) ;
348 Float *ccmin_p = clipcmin.getStorage( deleteCCMin ) ;
349 Complex *cmax_p = clipmax.getStorage( deleteCMax ) ;
350 Float *cwmax_p = clipwmax.getStorage( deleteCWMax ) ;
351 Float *ccmax_p = clipcmax.getStorage( deleteCCMax ) ;
352
353 // pass copy of irow to ggridsd since it will be modified in theroutine
354 Int irowCopy = irow ;
355
356 // call ggridsd
357 ggridsd2( xy_p,
358 values_p,
359 &nvispol,
360 &nvischan,
361 &idopsf,
362 flag_p,
363 rflag_p,
364 wgt_p,
365 &nrow,
366 &irowCopy,
367 grid_p,
368 wgrid_p,
369 npts_p,
370 cmin_p,
371 cwmin_p,
372 ccmin_p,
373 cmax_p,
374 cwmax_p,
375 ccmax_p,
376 &nx,
377 &ny,
378 &npol,
379 &nchan,
380 &support,
381 &sampling,
382 conv_p,
383 chanMap,
384 polMap,
385 sumw_p ) ;
386
387 // finalization
388 xypos.putStorage( xy_p, deletePos ) ;
389 spectra.freeStorage( values_p, deleteData ) ;
390 flagtra.freeStorage( flag_p, deleteFlag ) ;
391 flagrow.freeStorage( rflag_p, deleteFlagR ) ;
392 weight.freeStorage( wgt_p, deleteWgt ) ;
393 gdata.putStorage( grid_p, deleteDataG ) ;
394 gwgt.putStorage( wgrid_p, deleteWgtG ) ;
395 convFunc.putStorage( conv_p, deleteConv ) ;
396 clipmin.putStorage( cmin_p, deleteCMin ) ;
397 clipwmin.putStorage( cwmin_p, deleteCWMin ) ;
398 clipcmin.putStorage( ccmin_p, deleteCCMin ) ;
399 clipmax.putStorage( cmax_p, deleteCMax ) ;
400 clipwmax.putStorage( cwmax_p, deleteCWMax ) ;
401 clipcmax.putStorage( ccmax_p, deleteCCMax ) ;
402 delete sumw_p ;
403}
404
[2383]405void STGrid::grid()
406{
[2384]407 LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
[2385]408 double t0,t1 ;
[2384]409
[2383]410 // data selection
[2385]411 t0 = mathutil::gettimeofday_sec() ;
[2383]412 selectData() ;
[2385]413 t1 = mathutil::gettimeofday_sec() ;
414 os << "selectData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
415
[2388]416 setupGrid() ;
[2383]417 setupArray() ;
418
419 if ( wtype_.compare("UNIFORM") != 0 &&
420 wtype_.compare("TINT") != 0 &&
421 wtype_.compare("TSYS") != 0 &&
422 wtype_.compare("TINTSYS") != 0 ) {
423 LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
424 os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
425 wtype_ = "UNIFORM" ;
426 }
427
[2384]428 // grid parameter
429 os << LogIO::DEBUGGING ;
430 os << "----------" << endl ;
431 os << "Data selection summary" << endl ;
432 os << " ifno = " << ifno_ << endl ;
433 os << " pollist = " << pollist_ << endl ;
434 os << " scanlist = " << scanlist_ << endl ;
435 os << "----------" << endl ;
436 os << "Grid parameter summary" << endl ;
437 os << " (nx,ny) = (" << nx_ << "," << ny_ << ")" << endl ;
438 os << " (cellx,celly) = (" << cellx_ << "," << celly_ << ")" << endl ;
439 os << " center = " << center_ << endl ;
440 os << " weighting = " << wtype_ << endl ;
[2386]441 os << " convfunc = " << convType_ << " with support " << convSupport_ << endl ;
[2396]442 os << " doclip = " << (doclip_?"True":"False") << endl ;
[2384]443 os << "----------" << LogIO::POST ;
444 os << LogIO::NORMAL ;
445
[2398]446 if ( doclip_ )
[2396]447 gridPerRowWithClipping() ;
[2383]448 else
449 gridPerRow() ;
450}
451
[2398]452void STGrid::updateChunkShape()
[2383]453{
454 // TODO: nchunk_ must be determined from nchan_, npol_, and (nx_,ny_)
455 // by considering data size to be allocated for ggridsd input/output
[2385]456 nchunk_ = 400 ;
[2383]457 nchunk_ = min( nchunk_, nrow_ ) ;
[2393]458 vshape_ = IPosition( 1, nchunk_ ) ;
459 wshape_ = IPosition( 2, nchan_, nchunk_ ) ;
460 dshape_ = IPosition( 2, 2, nchunk_ ) ;
[2383]461}
462
[2393]463struct STGChunk {
464 Int nrow ;
465 Array<Complex> spectra;
466 Array<Int> flagtra;
467 Array<Int> rflag;
468 Array<Float> weight;
469 Array<Double> direction;
470 STGChunk(IPosition const &wshape, IPosition const &vshape,
471 IPosition const &dshape)
472 : spectra(wshape), flagtra(wshape), rflag(vshape), weight(wshape),
473 direction(dshape)
474 { }
475};
476
477struct STCommonData {
478 Int gnx;
479 Int gny;
480 Int *chanMap;
481 Vector<Float> convFunc ;
482 Array<Complex> gdataArrC;
483 Array<Float> gwgtArr;
484 STCommonData(IPosition const &gshape, Array<Float> const &data)
485 : gdataArrC(gshape, 0.0), gwgtArr(data) {}
486};
487
[2396]488struct STCommonDataWithClipping {
489 Int gnx;
490 Int gny;
491 Int *chanMap;
492 Vector<Float> convFunc ;
493 Array<Complex> gdataArrC;
494 Array<Float> gwgtArr;
495 Array<Int> npoints ;
496 Array<Complex> clipMin ;
497 Array<Float> clipWMin ;
498 Array<Float> clipCMin ;
499 Array<Complex> clipMax ;
500 Array<Float> clipWMax ;
501 Array<Float> clipCMax ;
502 STCommonDataWithClipping(IPosition const &gshape,
503 IPosition const &pshape,
504 Array<Float> const &data)
505 : gdataArrC(gshape, 0.0),
506 gwgtArr(data),
507 npoints(pshape, 0),
508 clipMin(gshape, Complex(FLT_MAX,0.0)),
509 clipWMin(gshape, 0.0),
510 clipCMin(gshape, 0.0),
511 clipMax(gshape, Complex(-FLT_MAX,0.0)),
512 clipWMax(gshape, 0.0),
513 clipCMax(gshape, 0.0)
514 {}
515};
516
[2393]517#define DO_AHEAD 3
518
519struct STContext {
520 STCommonData &common;
521 FIFO<STGChunk *, DO_AHEAD> queue;
522 STGrid *const self;
523 const Int pol;
524 STContext(STGrid *obj, STCommonData &common, Int pol)
525 : self(obj), common(common), pol(pol) {}
526};
527
[2396]528struct STContextWithClipping {
529 STCommonDataWithClipping &common;
530 FIFO<STGChunk *, DO_AHEAD> queue;
531 STGrid *const self;
532 const Int pol;
533 STContextWithClipping(STGrid *obj, STCommonDataWithClipping &common, Int pol)
534 : self(obj), common(common), pol(pol) {}
535};
[2393]536
[2396]537
[2393]538bool STGrid::produceChunk(void *ctx) throw(PCException)
539{
540 STContext &context = *(STContext *)ctx;
541 if ( context.self->nprocessed_ >= context.self->nrow_ ) {
542 return false;
543 }
544 STGChunk *chunk = new STGChunk(context.self->wshape_,
545 context.self->vshape_,
546 context.self->dshape_);
547
548 double t0 = mathutil::gettimeofday_sec() ;
549 chunk->nrow = context.self->getDataChunk(
550 context.self->wshape_, context.self->vshape_, context.self->dshape_,
551 chunk->spectra, chunk->direction,
552 chunk->flagtra, chunk->rflag, chunk->weight);
553 double t1 = mathutil::gettimeofday_sec() ;
554 context.self->eGetData_ += t1-t0 ;
555
556 context.queue.lock();
557 context.queue.put(chunk);
558 context.queue.unlock();
559 return true;
560}
561
562void STGrid::consumeChunk(void *ctx) throw(PCException)
563{
564 STContext &context = *(STContext *)ctx;
565 STGChunk *chunk = NULL;
566 try {
567 context.queue.lock();
568 chunk = context.queue.get();
569 context.queue.unlock();
570 } catch (FullException &e) {
571 context.queue.unlock();
572 // TODO: log error
573 throw PCException();
574 }
575
576 double t0, t1 ;
577 // world -> pixel
578 Array<Double> xypos( context.self->dshape_ ) ;
579 t0 = mathutil::gettimeofday_sec() ;
580 context.self->toPixel( chunk->direction, xypos ) ;
581 t1 = mathutil::gettimeofday_sec() ;
582 context.self->eToPixel_ += t1-t0 ;
583
584 // call ggridsd
585 Int nvispol = 1 ;
586 Int irow = -1 ;
587 t0 = mathutil::gettimeofday_sec() ;
588 context.self->call_ggridsd( xypos,
589 chunk->spectra,
590 nvispol,
591 context.self->nchan_,
592 chunk->flagtra,
593 chunk->rflag,
594 chunk->weight,
595 chunk->nrow,
596 irow,
597 context.common.gdataArrC,
598 context.common.gwgtArr,
599 context.common.gnx,
600 context.common.gny,
601 context.self->npol_,
602 context.self->nchan_,
603 context.self->convSupport_,
604 context.self->convSampling_,
605 context.common.convFunc,
606 context.common.chanMap,
607 (Int*)&context.pol ) ;
608 t1 = mathutil::gettimeofday_sec() ;
609 context.self->eGGridSD_ += t1-t0 ;
610
611 delete chunk;
612}
613
[2378]614void STGrid::gridPerRow()
615{
616 LogIO os( LogOrigin("STGrid", "gridPerRow", WHERE) ) ;
617 double t0, t1 ;
618
619
[2379]620 // grid data
621 // Extend grid plane with convSupport_
[2393]622 // Int gnx = nx_+convSupport_*2 ;
623 // Int gny = ny_+convSupport_*2 ;
624 Int gnx = nx_;
625 Int gny = ny_;
626
[2378]627 IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
628 // 2011/12/20 TN
629 // data_ and gwgtArr share storage
630 data_.resize( gshape ) ;
631 data_ = 0.0 ;
[2393]632 STCommonData common = STCommonData(gshape, data_);
633 common.gnx = gnx ;
634 common.gny = gny ;
[2378]635
[2379]636 // parameters for gridding
637 Int *chanMap = new Int[nchan_] ;
[2393]638 for ( Int i = 0 ; i < nchan_ ; i++ ) {
639 chanMap[i] = i ;
[2379]640 }
[2393]641 common.chanMap = chanMap;
[2379]642
[2393]643 // convolution kernel
644 t0 = mathutil::gettimeofday_sec() ;
645 setConvFunc( common.convFunc ) ;
646 t1 = mathutil::gettimeofday_sec() ;
647 os << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
648
[2379]649 // for performance check
[2393]650 eGetData_ = 0.0 ;
651 eToPixel_ = 0.0 ;
652 eGGridSD_ = 0.0 ;
[2384]653 double eInitPol = 0.0 ;
[2379]654
[2390]655 for ( uInt ifile = 0 ; ifile < nfile_ ; ifile++ ) {
656 initTable( ifile ) ;
[2380]657
[2393]658 os << "start table " << ifile << ": " << infileList_[ifile] << LogIO::POST ;
[2398]659 Broker broker = Broker(produceChunk, consumeChunk);
[2390]660 for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
[2383]661 t0 = mathutil::gettimeofday_sec() ;
[2393]662 initPol( ipol ) ; // set ptab_ and attach()
[2383]663 t1 = mathutil::gettimeofday_sec() ;
[2390]664 eInitPol += t1-t0 ;
[2383]665
[2393]666 STContext context(this, common, ipol);
[2390]667
668 os << "start pol " << ipol << LogIO::POST ;
669
[2393]670 nprocessed_ = 0 ;
671#if 1
672 broker.runProducerAsMasterThread(&context, DO_AHEAD);
673#else
674 for (;;) {
675 bool produced = produceChunk(&context);
676 if (! produced) {
677 break;
678 }
679 consumeChunk(&context);
[2390]680 }
[2393]681#endif
682
[2390]683 os << "end pol " << ipol << LogIO::POST ;
[2393]684
[2383]685 }
[2393]686 os << "end table " << ifile << LogIO::POST ;
[2378]687 }
[2384]688 os << "initPol: elapsed time is " << eInitPol << " sec." << LogIO::POST ;
[2393]689 os << "getData: elapsed time is " << eGetData_-eToInt-eGetWeight << " sec." << LogIO::POST ;
690 os << "toPixel: elapsed time is " << eToPixel_ << " sec." << LogIO::POST ;
691 os << "ggridsd: elapsed time is " << eGGridSD_ << " sec." << LogIO::POST ;
[2381]692 os << "toInt: elapsed time is " << eToInt << " sec." << LogIO::POST ;
[2384]693 os << "getWeight: elapsed time is " << eGetWeight << " sec." << LogIO::POST ;
[2383]694
[2379]695 delete chanMap ;
696
[2378]697 // set data
[2393]698 setData( common.gdataArrC, common.gwgtArr ) ;
[2379]699
[2378]700}
701
[2396]702void STGrid::consumeChunkWithClipping(void *ctx) throw(PCException)
703{
704 STContextWithClipping &context = *(STContextWithClipping *)ctx;
705 STGChunk *chunk = NULL;
706 try {
707 context.queue.lock();
708 chunk = context.queue.get();
709 context.queue.unlock();
710 } catch (FullException &e) {
711 context.queue.unlock();
712 // TODO: log error
713 throw PCException();
714 }
715
716 double t0, t1 ;
717 // world -> pixel
718 Array<Double> xypos( context.self->dshape_ ) ;
719 t0 = mathutil::gettimeofday_sec() ;
720 context.self->toPixel( chunk->direction, xypos ) ;
721 t1 = mathutil::gettimeofday_sec() ;
722 context.self->eToPixel_ += t1-t0 ;
723
724 // call ggridsd
725 Int nvispol = 1 ;
726 Int irow = -1 ;
727 t0 = mathutil::gettimeofday_sec() ;
728 context.self->call_ggridsd2( xypos,
729 chunk->spectra,
730 nvispol,
731 context.self->nchan_,
732 chunk->flagtra,
733 chunk->rflag,
734 chunk->weight,
735 chunk->nrow,
736 irow,
737 context.common.gdataArrC,
738 context.common.gwgtArr,
739 context.common.npoints,
740 context.common.clipMin,
741 context.common.clipWMin,
742 context.common.clipCMin,
743 context.common.clipMax,
744 context.common.clipWMax,
745 context.common.clipCMax,
746 context.common.gnx,
747 context.common.gny,
748 context.self->npol_,
749 context.self->nchan_,
750 context.self->convSupport_,
751 context.self->convSampling_,
752 context.common.convFunc,
753 context.common.chanMap,
754 (Int*)&context.pol ) ;
755 t1 = mathutil::gettimeofday_sec() ;
756 context.self->eGGridSD_ += t1-t0 ;
757
758 delete chunk;
759}
760
761void STGrid::gridPerRowWithClipping()
762{
763 LogIO os( LogOrigin("STGrid", "gridPerRowWithClipping", WHERE) ) ;
764 double t0, t1 ;
765
766
767 // grid data
768 // Extend grid plane with convSupport_
769 // Int gnx = nx_+convSupport_*2 ;
770 // Int gny = ny_+convSupport_*2 ;
771 Int gnx = nx_;
772 Int gny = ny_;
773
774 IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
775 IPosition pshape( 3, gnx, gny, npol_ ) ;
776 // 2011/12/20 TN
777 // data_ and gwgtArr share storage
778 data_.resize( gshape ) ;
779 data_ = 0.0 ;
780 STCommonDataWithClipping common = STCommonDataWithClipping( gshape,
781 pshape,
782 data_ ) ;
783 common.gnx = gnx ;
784 common.gny = gny ;
785
786 // parameters for gridding
787 Int *chanMap = new Int[nchan_] ;
788 for ( Int i = 0 ; i < nchan_ ; i++ ) {
789 chanMap[i] = i ;
790 }
791 common.chanMap = chanMap;
792
793 // convolution kernel
794 t0 = mathutil::gettimeofday_sec() ;
795 setConvFunc( common.convFunc ) ;
796 t1 = mathutil::gettimeofday_sec() ;
797 os << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
798
799 // for performance check
800 eGetData_ = 0.0 ;
801 eToPixel_ = 0.0 ;
802 eGGridSD_ = 0.0 ;
803 double eInitPol = 0.0 ;
804
805 for ( uInt ifile = 0 ; ifile < nfile_ ; ifile++ ) {
806 initTable( ifile ) ;
807
808 os << "start table " << ifile << ": " << infileList_[ifile] << LogIO::POST ;
[2398]809 Broker broker = Broker(produceChunk, consumeChunkWithClipping);
[2396]810 for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
811 t0 = mathutil::gettimeofday_sec() ;
812 initPol( ipol ) ; // set ptab_ and attach()
813 t1 = mathutil::gettimeofday_sec() ;
814 eInitPol += t1-t0 ;
815
816 STContextWithClipping context(this, common, ipol);
817
818 os << "start pol " << ipol << LogIO::POST ;
819
820 nprocessed_ = 0 ;
821#if 1
822 broker.runProducerAsMasterThread(&context, DO_AHEAD);
823#else
824 for (;;) {
825 bool produced = produceChunk(&context);
826 if (! produced) {
827 break;
828 }
829 consumeChunkWithClipping(&context);
830 }
831#endif
832
833 os << "end pol " << ipol << LogIO::POST ;
834
835 }
836 os << "end table " << ifile << LogIO::POST ;
837 }
838 os << "initPol: elapsed time is " << eInitPol << " sec." << LogIO::POST ;
839 os << "getData: elapsed time is " << eGetData_-eToInt-eGetWeight << " sec." << LogIO::POST ;
840 os << "toPixel: elapsed time is " << eToPixel_ << " sec." << LogIO::POST ;
[2397]841 os << "ggridsd2: elapsed time is " << eGGridSD_ << " sec." << LogIO::POST ;
[2396]842 os << "toInt: elapsed time is " << eToInt << " sec." << LogIO::POST ;
843 os << "getWeight: elapsed time is " << eGetWeight << " sec." << LogIO::POST ;
844
845 delete chanMap ;
846
847 // clip min and max in each grid
848// os << "BEFORE CLIPPING" << LogIO::POST ;
849// os << "gdataArrC=" << common.gdataArrC << LogIO::POST ;
850// os << "gwgtArr=" << common.gwgtArr << LogIO::POST ;
851 t0 = mathutil::gettimeofday_sec() ;
852 clipMinMax( common.gdataArrC,
853 common.gwgtArr,
854 common.npoints,
855 common.clipMin,
856 common.clipWMin,
857 common.clipCMin,
858 common.clipMax,
859 common.clipWMax,
860 common.clipCMax ) ;
861 t1 = mathutil::gettimeofday_sec() ;
862 os << "clipMinMax: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
863// os << "AFTER CLIPPING" << LogIO::POST ;
864// os << "gdataArrC=" << common.gdataArrC << LogIO::POST ;
865// os << "gwgtArr=" << common.gwgtArr << LogIO::POST ;
866
867 // set data
868 setData( common.gdataArrC, common.gwgtArr ) ;
869
870}
871
872void STGrid::clipMinMax( Array<Complex> &grid,
873 Array<Float> &weight,
874 Array<Int> &npoints,
875 Array<Complex> &clipmin,
876 Array<Float> &clipwmin,
877 Array<Float> &clipcmin,
878 Array<Complex> &clipmax,
879 Array<Float> &clipwmax,
880 Array<Float> &clipcmax )
881{
882 //LogIO os( LogOrigin("STGrid","clipMinMax",WHERE) ) ;
883
884 // prepare pointers
885 Bool delG, delW, delNP, delCMin, delCWMin, delCCMin, delCMax, delCWMax, delCCMax ;
886 Complex *grid_p = grid.getStorage( delG ) ;
887 Float *wgt_p = weight.getStorage( delW ) ;
888 const Int *npts_p = npoints.getStorage( delNP ) ;
889 const Complex *cmin_p = clipmin.getStorage( delCMin ) ;
890 const Float *cwmin_p = clipwmin.getStorage( delCWMin ) ;
891 const Float *ccmin_p = clipcmin.getStorage( delCCMin ) ;
892 const Complex *cmax_p = clipmax.getStorage( delCMax ) ;
893 const Float *cwmax_p = clipwmax.getStorage( delCWMax ) ;
894 const Float *ccmax_p = clipcmax.getStorage( delCCMax ) ;
895
896 const IPosition &gshape = grid.shape() ;
897 long offset = gshape[0] * gshape[1] * gshape[2] ; // nx * ny * npol
898 Int nchan = gshape[3] ;
899 long origin = nchan * offset ;
900 for ( long i = 0 ; i < offset ; i++ ) {
901 if ( *npts_p > 2 ) {
902 for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
903 // clip minimum and maximum
904 *grid_p -= (*cmin_p)*(*cwmin_p)*(*ccmin_p)
905 + (*cmax_p)*(*cwmax_p)*(*ccmax_p) ;
906 *wgt_p -= (*cwmin_p)*(*ccmin_p)
907 + (*cwmax_p)*(*ccmax_p) ;
908
909 grid_p += offset ;
910 wgt_p += offset ;
911 cmin_p += offset ;
912 cwmin_p += offset ;
913 ccmin_p += offset ;
914 cmax_p += offset ;
915 cwmax_p += offset ;
916 ccmax_p += offset ;
917 }
918 grid_p -= origin ;
919 wgt_p -= origin ;
920 cmin_p -= origin ;
921 cwmin_p -= origin ;
922 ccmin_p -= origin ;
923 cmax_p -= origin ;
924 cwmax_p -= origin ;
925 ccmax_p -= origin ;
926 }
927 grid_p++ ;
928 wgt_p++ ;
929 npts_p++ ;
930 cmin_p++ ;
931 cwmin_p++ ;
932 ccmin_p++ ;
933 cmax_p++ ;
934 cwmax_p++ ;
935 ccmax_p++ ;
936 }
937 grid_p -= offset ;
938 wgt_p -= offset ;
939 npts_p -= offset ;
940 cmin_p -= offset ;
941 cwmin_p -= offset ;
942 ccmin_p -= offset ;
943 cmax_p -= offset ;
944 cwmax_p -= offset ;
945 ccmax_p -= offset ;
946
947 // finalization
948 grid.putStorage( grid_p, delG ) ;
949 weight.putStorage( wgt_p, delW ) ;
950 npoints.freeStorage( npts_p, delNP ) ;
951 clipmin.freeStorage( cmin_p, delCMin ) ;
952 clipwmin.freeStorage( cwmin_p, delCWMin ) ;
953 clipcmin.freeStorage( ccmin_p, delCCMin ) ;
954 clipmax.freeStorage( cmax_p, delCMax ) ;
955 clipwmax.freeStorage( cwmax_p, delCWMax ) ;
956 clipcmax.freeStorage( ccmax_p, delCCMax ) ;
957}
958
[2382]959void STGrid::initPol( Int ipol )
960{
[2386]961 LogIO os( LogOrigin("STGrid","initPol",WHERE) ) ;
962 if ( npolOrg_ == 1 ) {
963 os << "single polarization data." << LogIO::POST ;
[2385]964 ptab_ = tab_ ;
[2386]965 }
[2385]966 else
967 ptab_ = tab_( tab_.col("POLNO") == (uInt)ipol ) ;
[2382]968
969 attach( ptab_ ) ;
970}
971
[2390]972void STGrid::initTable( uInt idx )
973{
974 tab_ = tableList_[idx] ;
975 nrow_ = rows_[idx] ;
[2398]976 updateChunkShape() ;
[2390]977}
978
[2378]979void STGrid::setData( Array<Complex> &gdata,
[2368]980 Array<Float> &gwgt )
981{
[2378]982 // 2011/12/20 TN
983 // gwgt and data_ share storage
[2368]984 LogIO os( LogOrigin("STGrid","setData",WHERE) ) ;
985 double t0, t1 ;
986 t0 = mathutil::gettimeofday_sec() ;
[2378]987 uInt len = data_.nelements() ;
[2374]988 const Complex *w1_p ;
[2378]989 Float *w2_p ;
[2379]990 Bool b1, b2 ;
[2374]991 const Complex *gdata_p = gdata.getStorage( b1 ) ;
[2378]992 Float *gwgt_p = data_.getStorage( b2 ) ;
[2368]993 w1_p = gdata_p ;
994 w2_p = gwgt_p ;
995 for ( uInt i = 0 ; i < len ; i++ ) {
[2378]996 if ( *w2_p > 0.0 ) *w2_p = (*w1_p).real() / *w2_p ;
[2368]997 w1_p++ ;
998 w2_p++ ;
999 }
1000 gdata.freeStorage( gdata_p, b1 ) ;
[2378]1001 data_.putStorage( gwgt_p, b2 ) ;
[2368]1002 t1 = mathutil::gettimeofday_sec() ;
1003 os << "setData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
1004}
1005
[2388]1006void STGrid::setupGrid()
1007{
1008 Double xmin,xmax,ymin,ymax ;
1009 mapExtent( xmin, xmax, ymin, ymax ) ;
1010
1011 setupGrid( nxUI_, nyUI_, cellxUI_, cellyUI_,
1012 xmin, xmax, ymin, ymax, centerUI_ ) ;
1013}
1014
[2356]1015void STGrid::setupGrid( Int &nx,
1016 Int &ny,
1017 String &cellx,
1018 String &celly,
1019 Double &xmin,
1020 Double &xmax,
1021 Double &ymin,
1022 Double &ymax,
1023 String &center )
1024{
[2375]1025 LogIO os( LogOrigin("STGrid","setupGrid",WHERE) ) ;
[2356]1026 //cout << "nx=" << nx << ", ny=" << ny << endl ;
[2359]1027
1028 // center position
1029 if ( center.size() == 0 ) {
1030 center_(0) = 0.5 * ( xmin + xmax ) ;
1031 center_(1) = 0.5 * ( ymin + ymax ) ;
1032 }
1033 else {
1034 String::size_type pos0 = center.find( " " ) ;
1035 if ( pos0 == String::npos ) {
1036 throw AipsError( "bad string format in parameter center" ) ;
1037 }
1038 String::size_type pos1 = center.find( " ", pos0+1 ) ;
1039 String typestr, xstr, ystr ;
1040 if ( pos1 != String::npos ) {
1041 typestr = center.substr( 0, pos0 ) ;
1042 xstr = center.substr( pos0+1, pos1-pos0 ) ;
1043 ystr = center.substr( pos1+1 ) ;
1044 // todo: convert to J2000 (or direction ref for DIRECTION column)
1045 }
1046 else {
1047 typestr = "J2000" ;
1048 xstr = center.substr( 0, pos0 ) ;
1049 ystr = center.substr( pos0+1 ) ;
1050 }
1051 QuantumHolder qh ;
1052 String err ;
1053 qh.fromString( err, xstr ) ;
1054 Quantum<Double> xcen = qh.asQuantumDouble() ;
1055 qh.fromString( err, ystr ) ;
1056 Quantum<Double> ycen = qh.asQuantumDouble() ;
1057 center_(0) = xcen.getValue( "rad" ) ;
1058 center_(1) = ycen.getValue( "rad" ) ;
1059 }
1060
1061
[2356]1062 nx_ = nx ;
1063 ny_ = ny ;
1064 if ( nx < 0 && ny > 0 ) {
1065 nx_ = ny ;
1066 ny_ = ny ;
1067 }
1068 if ( ny < 0 && nx > 0 ) {
1069 nx_ = nx ;
1070 ny_ = nx ;
1071 }
[2375]1072
1073 //Double wx = xmax - xmin ;
1074 //Double wy = ymax - ymin ;
1075 Double wx = max( abs(xmax-center_(0)), abs(xmin-center_(0)) ) * 2 ;
1076 Double wy = max( abs(ymax-center_(1)), abs(ymin-center_(1)) ) * 2 ;
1077 // take 10% margin
1078 wx *= 1.10 ;
1079 wy *= 1.10 ;
1080
1081 Quantum<Double> qcellx ;
1082 Quantum<Double> qcelly ;
[2356]1083 //cout << "nx_ = " << nx_ << ", ny_ = " << ny_ << endl ;
1084 if ( cellx.size() != 0 && celly.size() != 0 ) {
1085 readQuantity( qcellx, cellx ) ;
1086 readQuantity( qcelly, celly ) ;
1087 }
1088 else if ( celly.size() != 0 ) {
[2375]1089 os << "Using celly to x-axis..." << LogIO::POST ;
[2356]1090 readQuantity( qcelly, celly ) ;
1091 qcellx = qcelly ;
1092 }
1093 else if ( cellx.size() != 0 ) {
[2375]1094 os << "Using cellx to y-axis..." << LogIO::POST ;
[2356]1095 readQuantity( qcellx, cellx ) ;
1096 qcelly = qcellx ;
1097 }
1098 else {
1099 if ( nx_ < 0 ) {
[2375]1100 os << "No user preference in grid setting. Using default..." << LogIO::POST ;
[2356]1101 readQuantity( qcellx, "1.0arcmin" ) ;
1102 qcelly = qcellx ;
1103 }
1104 else {
[2375]1105 if ( wx == 0.0 ) {
1106 os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ;
1107 wx = 0.00290888 ;
1108 }
1109 if ( wy == 0.0 ) {
1110 os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ;
1111 wy = 0.00290888 ;
1112 }
[2356]1113 qcellx = Quantum<Double>( wx/nx_, "rad" ) ;
1114 qcelly = Quantum<Double>( wy/ny_, "rad" ) ;
1115 }
1116 }
1117 cellx_ = qcellx.getValue( "rad" ) ;
1118 celly_ = qcelly.getValue( "rad" ) ;
1119 if ( nx_ < 0 ) {
[2375]1120 if ( wx == 0.0 ) {
1121 os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ;
1122 wx = 0.00290888 ;
1123 }
1124 if ( wy == 0.0 ) {
1125 os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ;
1126 wy = 0.00290888 ;
1127 }
[2356]1128 nx_ = Int( ceil( wx/cellx_ ) ) ;
1129 ny_ = Int( ceil( wy/celly_ ) ) ;
1130 }
1131}
1132
[2388]1133void STGrid::mapExtent( Double &xmin, Double &xmax,
1134 Double &ymin, Double &ymax )
1135{
1136 //LogIO os( LogOrigin("STGrid","mapExtent",WHERE) ) ;
[2390]1137 directionCol_.attach( tableList_[0], "DIRECTION" ) ;
1138 Matrix<Double> direction = directionCol_.getColumn() ;
[2388]1139 //os << "dirCol.nrow() = " << dirCol.nrow() << LogIO::POST ;
1140 minMax( xmin, xmax, direction.row( 0 ) ) ;
1141 minMax( ymin, ymax, direction.row( 1 ) ) ;
[2390]1142 Double amin, amax, bmin, bmax ;
1143 for ( uInt i = 1 ; i < nfile_ ; i++ ) {
1144 directionCol_.attach( tableList_[i], "DIRECTION" ) ;
1145 direction.assign( directionCol_.getColumn() ) ;
1146 //os << "dirCol.nrow() = " << dirCol.nrow() << LogIO::POST ;
1147 minMax( amin, amax, direction.row( 0 ) ) ;
1148 minMax( bmin, bmax, direction.row( 1 ) ) ;
1149 xmin = min( xmin, amin ) ;
1150 xmax = max( xmax, amax ) ;
1151 ymin = min( ymin, bmin ) ;
1152 ymax = max( ymax, bmax ) ;
1153 }
[2388]1154 //os << "(xmin,xmax)=(" << xmin << "," << xmax << ")" << LogIO::POST ;
1155 //os << "(ymin,ymax)=(" << ymin << "," << ymax << ")" << LogIO::POST ;
1156}
1157
[2379]1158void STGrid::selectData()
[2362]1159{
[2386]1160 LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
[2362]1161 Int ifno = ifno_ ;
[2390]1162 tableList_.resize( nfile_ ) ;
[2393]1163 if ( ifno == -1 ) {
1164 Table taborg( infileList_[0] ) ;
1165 ROScalarColumn<uInt> ifnoCol( taborg, "IFNO" ) ;
1166 ifno = ifnoCol( 0 ) ;
1167 os << LogIO::WARN
1168 << "IFNO is not given. Using default IFNO: " << ifno << LogIO::POST ;
1169 }
[2390]1170 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
[2389]1171 Table taborg( infileList_[i] ) ;
1172 TableExprNode node ;
1173 if ( isMultiIF( taborg ) ) {
1174 os << "apply selection on IFNO" << LogIO::POST ;
1175 node = taborg.col("IFNO") == ifno ;
1176 }
1177 if ( scanlist_.size() > 0 ) {
1178 os << "apply selection on SCANNO" << LogIO::POST ;
1179 node = node && taborg.col("SCANNO").in( scanlist_ ) ;
1180 }
1181 if ( node.isNull() ) {
1182 tableList_[i] = taborg ;
1183 }
1184 else {
1185 tableList_[i] = taborg( node ) ;
1186 }
[2393]1187 os << "tableList_[" << i << "].nrow()=" << tableList_[i].nrow() << LogIO::POST ;
[2389]1188 if ( tableList_[i].nrow() == 0 ) {
1189 os << LogIO::SEVERE
1190 << "No corresponding rows for given selection: IFNO " << ifno ;
1191 if ( scanlist_.size() > 0 )
1192 os << " SCANNO " << scanlist_ ;
1193 os << LogIO::EXCEPTION ;
1194 }
[2362]1195 }
1196}
1197
[2386]1198Bool STGrid::isMultiIF( Table &tab )
1199{
1200 ROScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
1201 Vector<uInt> ifnos = ifnoCol.getColumn() ;
1202 return anyNE( ifnos, ifnos[0] ) ;
1203}
1204
[2382]1205void STGrid::attach( Table &tab )
[2379]1206{
1207 // attach to table
[2382]1208 spectraCol_.attach( tab, "SPECTRA" ) ;
1209 flagtraCol_.attach( tab, "FLAGTRA" ) ;
1210 directionCol_.attach( tab, "DIRECTION" ) ;
1211 flagRowCol_.attach( tab, "FLAGROW" ) ;
1212 tsysCol_.attach( tab, "TSYS" ) ;
1213 intervalCol_.attach( tab, "INTERVAL" ) ;
[2379]1214}
1215
[2393]1216Int STGrid::getDataChunk(
1217 IPosition const &wshape,
1218 IPosition const &vshape,
1219 IPosition const &dshape,
1220 Array<Complex> &spectra,
1221 Array<Double> &direction,
1222 Array<Int> &flagtra,
1223 Array<Int> &rflag,
1224 Array<Float> &weight )
1225{
1226 LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
1227
1228 Array<Float> spectraF_(wshape);
1229 Array<uChar> flagtraUC_(wshape);
1230 Array<uInt> rflagUI_(vshape);
1231 Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
1232 if ( nrow < nchunk_ ) {
1233 spectra.resize( spectraF_.shape() ) ;
1234 flagtra.resize( flagtraUC_.shape() ) ;
1235 rflag.resize( rflagUI_.shape() ) ;
1236 }
1237 double t0, t1 ;
1238 t0 = mathutil::gettimeofday_sec() ;
1239 convertArray( spectra, spectraF_ ) ;
1240 toInt( flagtraUC_, flagtra ) ;
1241 toInt( rflagUI_, rflag ) ;
1242 t1 = mathutil::gettimeofday_sec() ;
1243 eToInt = t1 - t0 ;
1244
1245 return nrow ;
1246}
1247
1248#if 0
[2379]1249Int STGrid::getDataChunk( Array<Complex> &spectra,
1250 Array<Double> &direction,
1251 Array<Int> &flagtra,
1252 Array<Int> &rflag,
1253 Array<Float> &weight )
[2378]1254{
[2385]1255 LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
[2381]1256 Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
[2383]1257 if ( nrow < nchunk_ ) {
1258 spectra.resize( spectraF_.shape() ) ;
1259 flagtra.resize( flagtraUC_.shape() ) ;
1260 rflag.resize( rflagUI_.shape() ) ;
1261 }
[2381]1262 double t0, t1 ;
1263 t0 = mathutil::gettimeofday_sec() ;
1264 convertArray( spectra, spectraF_ ) ;
[2382]1265 toInt( flagtraUC_, flagtra ) ;
1266 toInt( rflagUI_, rflag ) ;
[2381]1267 t1 = mathutil::gettimeofday_sec() ;
[2384]1268 eToInt = t1 - t0 ;
[2381]1269
[2379]1270 return nrow ;
[2378]1271}
[2393]1272#endif
[2378]1273
[2379]1274Int STGrid::getDataChunk( Array<Float> &spectra,
1275 Array<Double> &direction,
1276 Array<uChar> &flagtra,
1277 Array<uInt> &rflag,
1278 Array<Float> &weight )
[2375]1279{
[2379]1280 LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
[2383]1281 Int nrow = spectra.shape()[1] ;
1282 Int remainingRow = nrow_ - nprocessed_ ;
1283 if ( remainingRow < nrow ) {
1284 nrow = remainingRow ;
1285 IPosition mshape( 2, nchan_, nrow ) ;
1286 IPosition vshape( 1, nrow ) ;
1287 spectra.resize( mshape ) ;
1288 flagtra.resize( mshape ) ;
1289 direction.resize( IPosition(2,2,nrow) ) ;
1290 rflag.resize( vshape ) ;
1291 weight.resize( mshape ) ;
[2379]1292 }
[2383]1293 // 2011/12/22 TN
1294 // tsys shares its storage with weight
1295 Array<Float> tsys( weight ) ;
1296 Array<Double> tint( rflag.shape() ) ;
[2380]1297
[2383]1298 Vector<uInt> rflagVec( rflag ) ;
1299 Vector<Double> tintVec( tint ) ;
[2379]1300
[2383]1301 RefRows rows( nprocessed_, nprocessed_+nrow-1, 1 ) ;
[2386]1302 //os<<LogIO::DEBUGGING<<"nprocessed_="<<nprocessed_<<": rows.nrows()="<<rows.nrows()<<LogIO::POST ;
[2383]1303 spectraCol_.getColumnCells( rows, spectra ) ;
1304 flagtraCol_.getColumnCells( rows, flagtra ) ;
1305 directionCol_.getColumnCells( rows, direction ) ;
1306 flagRowCol_.getColumnCells( rows, rflagVec ) ;
1307 intervalCol_.getColumnCells( rows, tintVec ) ;
1308 Vector<Float> tsysTemp = tsysCol_( nprocessed_ ) ;
1309 if ( tsysTemp.nelements() == (uInt)nchan_ )
1310 tsysCol_.getColumnCells( rows, tsys ) ;
1311 else
1312 tsys = tsysTemp[0] ;
[2385]1313
[2384]1314 double t0,t1 ;
1315 t0 = mathutil::gettimeofday_sec() ;
[2379]1316 getWeight( weight, tsys, tint ) ;
[2384]1317 t1 = mathutil::gettimeofday_sec() ;
1318 eGetWeight += t1-t0 ;
[2379]1319
1320 nprocessed_ += nrow ;
1321
1322 return nrow ;
1323}
1324
1325void STGrid::setupArray()
1326{
[2376]1327 LogIO os( LogOrigin("STGrid","setupArray",WHERE) ) ;
[2390]1328 ROScalarColumn<uInt> polnoCol( tableList_[0], "POLNO" ) ;
[2371]1329 Vector<uInt> pols = polnoCol.getColumn() ;
[2376]1330 //os << pols << LogIO::POST ;
[2371]1331 Vector<uInt> pollistOrg ;
[2386]1332 npolOrg_ = 0 ;
[2371]1333 uInt polno ;
1334 for ( uInt i = 0 ; i < polnoCol.nrow() ; i++ ) {
[2393]1335 //polno = polnoCol( i ) ;
[2371]1336 polno = pols( i ) ;
1337 if ( allNE( pollistOrg, polno ) ) {
[2386]1338 pollistOrg.resize( npolOrg_+1, True ) ;
1339 pollistOrg[npolOrg_] = polno ;
1340 npolOrg_++ ;
[2371]1341 }
1342 }
1343 if ( pollist_.size() == 0 )
1344 pollist_ = pollistOrg ;
1345 else {
1346 Vector<uInt> newlist ;
1347 uInt newsize = 0 ;
1348 for ( uInt i = 0 ; i < pollist_.size() ; i++ ) {
1349 if ( anyEQ( pollistOrg, pollist_[i] ) ) {
1350 newlist.resize( newsize+1, True ) ;
1351 newlist[newsize] = pollist_[i] ;
1352 newsize++ ;
1353 }
1354 }
1355 pollist_.assign( newlist ) ;
1356 }
1357 npol_ = pollist_.size() ;
1358 if ( npol_ == 0 ) {
1359 os << LogIO::SEVERE << "Empty pollist" << LogIO::EXCEPTION ;
1360 }
[2390]1361 rows_.resize( nfile_ ) ;
1362 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
1363 rows_[i] = tableList_[i].nrow() / npolOrg_ ;
[2398]1364 //if ( nrow_ < rows_[i] )
1365 // nrow_ = rows_[i] ;
[2390]1366 }
1367 flagtraCol_.attach( tableList_[0], "FLAGTRA" ) ;
1368 nchan_ = flagtraCol_( 0 ).nelements() ;
[2371]1369// os << "npol_ = " << npol_ << "(" << pollist_ << ")" << endl
1370// << "nchan_ = " << nchan_ << endl
1371// << "nrow_ = " << nrow_ << LogIO::POST ;
1372}
1373
[2375]1374void STGrid::getWeight( Array<Float> &w,
[2382]1375 Array<Float> &tsys,
1376 Array<Double> &tint )
1377{
[2383]1378 LogIO os( LogOrigin("STGrid","getWeight",WHERE) ) ;
[2384]1379
[2382]1380 // 2011/12/22 TN
1381 // w (weight) and tsys share storage
1382 IPosition refShape = tsys.shape() ;
1383 Int nchan = refShape[0] ;
1384 Int nrow = refShape[1] ;
1385// os << "nchan=" << nchan << ", nrow=" << nrow << LogIO::POST ;
1386// os << "w.shape()=" << w.shape() << endl
1387// << "tsys.shape()=" << tsys.shape() << endl
1388// << "tint.shape()=" << tint.shape() << LogIO::POST ;
1389
1390 // set weight
1391 if ( wtype_.compare( "UNIFORM" ) == 0 ) {
1392 w = 1.0 ;
1393 }
1394 else if ( wtype_.compare( "TINT" ) == 0 ) {
1395 Bool b0, b1 ;
1396 Float *w_p = w.getStorage( b0 ) ;
1397 Float *w0_p = w_p ;
1398 const Double *ti_p = tint.getStorage( b1 ) ;
1399 const Double *w1_p = ti_p ;
1400 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1401 for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1402 *w0_p = *w1_p ;
1403 w0_p++ ;
1404 }
1405 w1_p++ ;
1406 }
1407 w.putStorage( w_p, b0 ) ;
1408 tint.freeStorage( ti_p, b1 ) ;
1409 }
1410 else if ( wtype_.compare( "TSYS" ) == 0 ) {
1411 Bool b0 ;
1412 Float *w_p = w.getStorage( b0 ) ;
1413 Float *w0_p = w_p ;
1414 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1415 for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1416 Float temp = *w0_p ;
1417 *w0_p = 1.0 / ( temp * temp ) ;
1418 w0_p++ ;
1419 }
1420 }
1421 w.putStorage( w_p, b0 ) ;
1422 }
1423 else if ( wtype_.compare( "TINTSYS" ) == 0 ) {
1424 Bool b0, b1 ;
1425 Float *w_p = w.getStorage( b0 ) ;
1426 Float *w0_p = w_p ;
1427 const Double *ti_p = tint.getStorage( b1 ) ;
1428 const Double *w1_p = ti_p ;
1429 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1430 Float interval = *w1_p ;
1431 for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1432 Float temp = *w0_p ;
1433 *w0_p = interval / ( temp * temp ) ;
1434 w0_p++ ;
1435 }
1436 w1_p++ ;
1437 }
1438 w.putStorage( w_p, b0 ) ;
1439 tint.freeStorage( ti_p, b1 ) ;
1440 }
1441 else {
1442 //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
1443 //os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
1444 w = 1.0 ;
1445 }
1446}
1447
[2375]1448void STGrid::toInt( Array<uChar> &u, Array<Int> &v )
[2356]1449{
[2375]1450 uInt len = u.nelements() ;
[2356]1451 Int *int_p = new Int[len] ;
1452 Bool deleteIt ;
[2375]1453 const uChar *data_p = u.getStorage( deleteIt ) ;
[2356]1454 Int *i_p = int_p ;
1455 const uChar *u_p = data_p ;
1456 for ( uInt i = 0 ; i < len ; i++ ) {
1457 *i_p = ( *u_p == 0 ) ? 0 : 1 ;
1458 i_p++ ;
1459 u_p++ ;
1460 }
[2375]1461 u.freeStorage( data_p, deleteIt ) ;
1462 v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
[2356]1463}
1464
[2375]1465void STGrid::toInt( Array<uInt> &u, Array<Int> &v )
[2356]1466{
[2375]1467 uInt len = u.nelements() ;
[2356]1468 Int *int_p = new Int[len] ;
1469 Bool deleteIt ;
[2375]1470 const uInt *data_p = u.getStorage( deleteIt ) ;
[2356]1471 Int *i_p = int_p ;
1472 const uInt *u_p = data_p ;
1473 for ( uInt i = 0 ; i < len ; i++ ) {
1474 *i_p = ( *u_p == 0 ) ? 0 : 1 ;
1475 i_p++ ;
1476 u_p++ ;
1477 }
[2375]1478 u.freeStorage( data_p, deleteIt ) ;
1479 v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
[2356]1480}
1481
[2375]1482void STGrid::toPixel( Array<Double> &world, Array<Double> &pixel )
[2356]1483{
[2359]1484 // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_)
1485 // grid plane to avoid unexpected behavior on grid edge
[2375]1486 Block<Double> pixc( 2 ) ;
1487 pixc[0] = Double( nx_-1 ) * 0.5 ;
1488 pixc[1] = Double( ny_-1 ) * 0.5 ;
1489// pixc[0] = Double( nx_+2*convSupport_-1 ) * 0.5 ;
1490// pixc[1] = Double( ny_+2*convSupport_-1 ) * 0.5 ;
[2356]1491 uInt nrow = world.shape()[1] ;
[2375]1492 Bool bw, bp ;
1493 const Double *w_p = world.getStorage( bw ) ;
1494 Double *p_p = pixel.getStorage( bp ) ;
1495 const Double *ww_p = w_p ;
1496 Double *wp_p = p_p ;
1497 for ( uInt i = 0 ; i < nrow ; i++ ) {
1498 *wp_p = pixc[0] + ( *ww_p - center_[0] ) / cellx_ ;
1499 wp_p++ ;
1500 ww_p++ ;
1501 *wp_p = pixc[1] + ( *ww_p - center_[1] ) / celly_ ;
1502 wp_p++ ;
1503 ww_p++ ;
[2356]1504 }
[2375]1505 world.freeStorage( w_p, bw ) ;
1506 pixel.putStorage( p_p, bp ) ;
[2356]1507}
1508
1509void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize )
1510{
1511 convFunc = 0.0 ;
1512 for ( Int i = 0 ; i < convSize/2 ; i++ )
1513 convFunc(i) = 1.0 ;
1514}
1515
1516#define NEED_UNDERSCORES
1517#if defined(NEED_UNDERSCORES)
1518#define grdsf grdsf_
1519#endif
1520extern "C" {
1521 void grdsf(Double*, Double*);
1522}
1523void STGrid::spheroidalFunc( Vector<Float> &convFunc )
1524{
1525 convFunc = 0.0 ;
1526 for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
1527 Double nu = Double(i) / Double(convSupport_*convSampling_) ;
1528 Double val ;
1529 grdsf( &nu, &val ) ;
1530 convFunc(i) = ( 1.0 - nu * nu ) * val ;
1531 }
1532}
1533
1534void STGrid::gaussFunc( Vector<Float> &convFunc )
1535{
1536 convFunc = 0.0 ;
[2363]1537 // HWHM of the Gaussian is convSupport_ / 4
1538 // To take into account Gaussian tail, kernel cutoff is set to 4 * HWHM
1539 Int len = convSampling_ * convSupport_ ;
1540 Double hwhm = len * 0.25 ;
1541 for ( Int i = 0 ; i < len ; i++ ) {
[2356]1542 Double val = Double(i) / hwhm ;
1543 convFunc(i) = exp( -log(2)*val*val ) ;
1544 }
1545}
1546
1547void STGrid::pbFunc( Vector<Float> &convFunc )
1548{
1549 convFunc = 0.0 ;
1550}
1551
1552void STGrid::setConvFunc( Vector<Float> &convFunc )
1553{
1554 convSupport_ = userSupport_ ;
1555 if ( convType_ == "BOX" ) {
1556 if ( convSupport_ < 0 )
1557 convSupport_ = 0 ;
1558 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
1559 convFunc.resize( convSize ) ;
1560 boxFunc( convFunc, convSize ) ;
1561 }
1562 else if ( convType_ == "SF" ) {
1563 if ( convSupport_ < 0 )
1564 convSupport_ = 3 ;
1565 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
1566 convFunc.resize( convSize ) ;
1567 spheroidalFunc( convFunc ) ;
1568 }
1569 else if ( convType_ == "GAUSS" ) {
[2363]1570 // to take into account Gaussian tail
[2356]1571 if ( convSupport_ < 0 )
[2363]1572 convSupport_ = 12 ; // 3 * 4
1573 else {
1574 convSupport_ = userSupport_ * 4 ;
1575 }
[2356]1576 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
1577 convFunc.resize( convSize ) ;
1578 gaussFunc( convFunc ) ;
1579 }
[2359]1580 else if ( convType_ == "PB" ) {
1581 if ( convSupport_ < 0 )
1582 convSupport_ = 0 ;
[2356]1583 pbFunc( convFunc ) ;
[2359]1584 }
[2356]1585 else {
1586 throw AipsError( "Unsupported convolution function" ) ;
1587 }
1588}
1589
1590string STGrid::saveData( string outfile )
1591{
[2368]1592 LogIO os( LogOrigin("STGrid", "saveData", WHERE) ) ;
1593 double t0, t1 ;
1594 t0 = mathutil::gettimeofday_sec() ;
1595
[2393]1596 //Int polno = 0 ;
[2371]1597 String outfile_ ;
[2356]1598 if ( outfile.size() == 0 ) {
[2389]1599 if ( infileList_[0].lastchar() == '/' ) {
1600 outfile_ = infileList_[0].substr( 0, infileList_[0].size()-1 ) ;
[2356]1601 }
1602 else {
[2389]1603 outfile_ = infileList_[0] ;
[2356]1604 }
1605 outfile_ += ".grid" ;
1606 }
1607 else {
1608 outfile_ = outfile ;
1609 }
[2371]1610 Table tab ;
1611 prepareTable( tab, outfile_ ) ;
[2356]1612 IPosition dshape = data_.shape() ;
[2361]1613 Int nrow = nx_ * ny_ * npol_ ;
1614 tab.rwKeywordSet().define( "nPol", npol_ ) ;
[2360]1615 tab.addRow( nrow ) ;
[2356]1616 Vector<Double> cpix( 2 ) ;
1617 cpix(0) = Double( nx_ - 1 ) * 0.5 ;
1618 cpix(1) = Double( ny_ - 1 ) * 0.5 ;
1619 Vector<Double> dir( 2 ) ;
1620 ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
1621 ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
1622 ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
1623 Int irow = 0 ;
[2371]1624 Vector<Float> sp( nchan_ ) ;
1625 Bool bsp, bdata ;
1626 const Float *data_p = data_.getStorage( bdata ) ;
1627 Float *wsp_p, *sp_p ;
1628 const Float *wdata_p = data_p ;
1629 long step = nx_ * ny_ * npol_ ;
1630 long offset ;
[2356]1631 for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
[2371]1632 dir(1) = center_(1) - ( cpix(1) - (Double)iy ) * celly_ ;
[2356]1633 for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
[2371]1634 dir(0) = center_(0) - ( cpix(0) - (Double)ix ) * cellx_ ;
[2361]1635 for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
[2371]1636 offset = ix + iy * nx_ + ipol * nx_ * ny_ ;
1637 //os << "offset = " << offset << LogIO::POST ;
1638 sp_p = sp.getStorage( bsp ) ;
1639 wsp_p = sp_p ;
1640 wdata_p = data_p + offset ;
1641 for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
1642 *wsp_p = *wdata_p ;
1643 wsp_p++ ;
1644 wdata_p += step ;
1645 }
1646 sp.putStorage( sp_p, bsp ) ;
[2356]1647 spectraCol.put( irow, sp ) ;
1648 directionCol.put( irow, dir ) ;
[2360]1649 polnoCol.put( irow, pollist_[ipol] ) ;
[2356]1650 irow++ ;
1651 }
1652 }
1653 }
[2371]1654 data_.freeStorage( data_p, bdata ) ;
[2368]1655
1656 t1 = mathutil::gettimeofday_sec() ;
1657 os << "saveData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2374]1658
[2413]1659 fillMainColumns( tab ) ;
1660
[2356]1661 return outfile_ ;
1662}
1663
[2371]1664void STGrid::prepareTable( Table &tab, String &name )
1665{
[2389]1666 Table t( infileList_[0], Table::Old ) ;
[2371]1667 t.deepCopy( name, Table::New, False, t.endianFormat(), True ) ;
1668 tab = Table( name, Table::Update ) ;
[2405]1669 // 2012/02/13 TN
1670 // explicitly copy subtables since no rows including subtables are
1671 // copied by Table::deepCopy with noRows=True
1672 TableCopy::copySubTables( tab, t ) ;
[2356]1673}
[2379]1674
[2413]1675void STGrid::fillMainColumns( Table &tab )
1676{
[2414]1677 // values for fill
[2413]1678 Table t( infileList_[0], Table::Old ) ;
1679 Table tsel = t( t.col( "IFNO" ) == (uInt)ifno_, 1 ) ;
[2414]1680 ROTableRow row( tsel ) ;
[2413]1681 row.get( 0 ) ;
1682 const TableRecord &rec = row.record() ;
1683 uInt freqId = rec.asuInt( "FREQ_ID" ) ;
[2414]1684 Vector<Float> defaultTsys( 1, 1.0 ) ;
1685 // @todo how to set flagtra for gridded spectra?
1686 Vector<uChar> flagtra = rec.asArrayuChar( "FLAGTRA" ) ;
1687 flagtra = (uChar)0 ;
1688
1689 // fill columns
[2413]1690 Int nrow = tab.nrow() ;
1691 ScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
1692 ScalarColumn<uInt> freqIdCol( tab, "FREQ_ID" ) ;
[2414]1693 ArrayColumn<uChar> flagtraCol( tab, "FLAGTRA" ) ;
1694 ArrayColumn<Float> tsysCol( tab, "TSYS" ) ;
[2413]1695 for ( Int i = 0 ; i < nrow ; i++ ) {
1696 ifnoCol.put( i, (uInt)ifno_ ) ;
1697 freqIdCol.put( i, freqId ) ;
[2414]1698 flagtraCol.put( i, flagtra ) ;
1699 tsysCol.put( i, defaultTsys ) ;
[2413]1700 }
[2371]1701}
[2413]1702
1703}
Note: See TracBrowser for help on using the repository browser.