source: trunk/src/STGrid.cpp@ 2937

Last change on this file since 2937 was 2894, checked in by Takeshi Nakazato, 11 years ago

New Development: No

JIRA Issue: Yes CAS-6121

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...

Fix for latitude inversion.


File size: 63.0 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
[2638]20#include <coordinates/Coordinates/DirectionCoordinate.h>
21
[2356]22#include <tables/Tables/Table.h>
[2360]23#include <tables/Tables/TableRecord.h>
[2413]24#include <tables/Tables/TableRow.h>
[2360]25#include <tables/Tables/ExprNode.h>
[2356]26#include <tables/Tables/ScalarColumn.h>
27#include <tables/Tables/ArrayColumn.h>
[2405]28#include <tables/Tables/TableCopy.h>
[2356]29
30#include <measures/Measures/MDirection.h>
31
[2454]32#include "MathUtils.h"
[2424]33#include <atnf/PKSIO/SrcType.h>
[2364]34
[2356]35#include "STGrid.h"
36
37using namespace std ;
[2393]38using namespace concurrent ;
[2356]39using namespace casa ;
40using namespace asap ;
41
42namespace asap {
43
[2384]44// for performance check
45double eToInt = 0.0 ;
46double eGetWeight = 0.0 ;
47
[2356]48// constructor
49STGrid::STGrid()
[2393]50 : vshape_( 1 ), wshape_( 2 ), dshape_( 2 )
[2356]51{
52 init() ;
53}
54
55STGrid::STGrid( const string infile )
[2393]56 : vshape_( 1 ), wshape_( 2 ), dshape_( 2 )
[2356]57{
58 init() ;
59
60 setFileIn( infile ) ;
61}
62
[2390]63STGrid::STGrid( const vector<string> infile )
64{
65 init() ;
66
67 setFileList( infile ) ;
68}
69
[2356]70void STGrid::init()
71{
[2362]72 ifno_ = -1 ;
[2356]73 nx_ = -1 ;
74 ny_ = -1 ;
[2361]75 npol_ = 0 ;
76 nchan_ = 0 ;
77 nrow_ = 0 ;
[2356]78 cellx_ = 0.0 ;
79 celly_ = 0.0 ;
80 center_ = Vector<Double> ( 2, 0.0 ) ;
81 convType_ = "BOX" ;
[2361]82 wtype_ = "UNIFORM" ;
[2356]83 convSupport_ = -1 ;
84 userSupport_ = -1 ;
[2671]85 truncate_ = "";
86 gwidth_ = "";
87 jwidth_ = "";
[2356]88 convSampling_ = 100 ;
[2379]89 nprocessed_ = 0 ;
90 nchunk_ = 0 ;
[2388]91
92 // initialize user input
93 nxUI_ = -1 ;
94 nyUI_ = -1 ;
95 cellxUI_ = "" ;
96 cellyUI_ = "" ;
97 centerUI_ = "" ;
[2396]98 doclip_ = False ;
[2356]99}
100
101void STGrid::setFileIn( const string infile )
102{
[2393]103 nfile_ = 1 ;
[2356]104 String name( infile ) ;
[2393]105 infileList_.resize( nfile_ ) ;
106 infileList_[0] = String(infile) ;
[2356]107}
108
[2390]109void STGrid::setFileList( const vector<string> infile )
110{
111 nfile_ = infile.size() ;
112 infileList_.resize( nfile_ ) ;
113 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
114 infileList_[i] = infile[i] ;
115 }
116}
117
[2360]118void STGrid::setPolList( vector<unsigned int> pols )
119{
120 pollist_.assign( Vector<uInt>( pols ) ) ;
121}
122
[2364]123void STGrid::setScanList( vector<unsigned int> scans )
124{
125 scanlist_.assign( Vector<uInt>( scans ) ) ;
126}
127
[2361]128void STGrid::setWeight( const string wType )
129{
130 wtype_ = String( wType ) ;
131 wtype_.upcase() ;
132}
133
[2356]134void STGrid::defineImage( int nx,
135 int ny,
136 string scellx,
137 string scelly,
138 string scenter )
139{
[2388]140 nxUI_ = (Int)nx ;
141 nyUI_ = (Int)ny ;
142 cellxUI_ = String( scellx ) ;
143 cellyUI_ = String( scelly ) ;
144 centerUI_ = String( scenter ) ;
[2356]145}
146
[2364]147void STGrid::setFunc( string convType,
[2671]148 int convSupport,
149 string truncate,
150 string gwidth,
151 string jwidth )
[2356]152{
153 convType_ = String( convType ) ;
154 convType_.upcase() ;
155 userSupport_ = (Int)convSupport ;
[2671]156 truncate_ = String( truncate );
157 gwidth_ = String( gwidth );
158 jwidth_ = String( jwidth );
[2356]159}
160
161#define NEED_UNDERSCORES
162#if defined(NEED_UNDERSCORES)
163#define ggridsd ggridsd_
164#endif
165extern "C" {
166 void ggridsd(Double*,
167 const Complex*,
168 Int*,
169 Int*,
170 Int*,
171 const Int*,
172 const Int*,
173 const Float*,
174 Int*,
175 Int*,
176 Complex*,
177 Float*,
178 Int*,
179 Int*,
180 Int *,
181 Int *,
182 Int*,
183 Int*,
184 Float*,
185 Int*,
186 Int*,
187 Double*);
188}
[2384]189void STGrid::call_ggridsd( Array<Double> &xypos,
190 Array<Complex> &spectra,
191 Int &nvispol,
192 Int &nvischan,
193 Array<Int> &flagtra,
194 Array<Int> &flagrow,
195 Array<Float> &weight,
196 Int &nrow,
197 Int &irow,
198 Array<Complex> &gdata,
199 Array<Float> &gwgt,
200 Int &nx,
201 Int &ny,
202 Int &npol,
203 Int &nchan,
204 Int &support,
205 Int &sampling,
206 Vector<Float> &convFunc,
[2381]207 Int *chanMap,
208 Int *polMap )
209{
210 // parameters for gridding
211 Int idopsf = 0 ;
[2384]212 Int len = npol*nchan ;
[2381]213 Double *sumw_p = new Double[len] ;
214 {
215 Double *work_p = sumw_p ;
216 for ( Int i = 0 ; i < len ; i++ ) {
217 *work_p = 0.0 ;
218 work_p++ ;
219 }
220 }
221
[2384]222 // prepare pointer
223 Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ;
224 Double *xy_p = xypos.getStorage( deletePos ) ;
225 const Complex *values_p = spectra.getStorage( deleteData ) ;
226 const Int *flag_p = flagtra.getStorage( deleteFlag ) ;
227 const Int *rflag_p = flagrow.getStorage( deleteFlagR ) ;
228 const Float *wgt_p = weight.getStorage( deleteWgt ) ;
229 Complex *grid_p = gdata.getStorage( deleteDataG ) ;
230 Float *wgrid_p = gwgt.getStorage( deleteWgtG ) ;
231 Float *conv_p = convFunc.getStorage( deleteConv ) ;
[2385]232
233 // pass copy of irow to ggridsd since it will be modified in theroutine
234 Int irowCopy = irow ;
[2384]235
[2381]236 // call ggridsd
[2384]237 ggridsd( xy_p,
238 values_p,
239 &nvispol,
240 &nvischan,
[2381]241 &idopsf,
[2384]242 flag_p,
243 rflag_p,
244 wgt_p,
245 &nrow,
[2385]246 &irowCopy,
[2384]247 grid_p,
248 wgrid_p,
249 &nx,
250 &ny,
251 &npol,
252 &nchan,
253 &support,
254 &sampling,
255 conv_p,
[2381]256 chanMap,
257 polMap,
258 sumw_p ) ;
259
260 // finalization
[2384]261 xypos.putStorage( xy_p, deletePos ) ;
262 spectra.freeStorage( values_p, deleteData ) ;
263 flagtra.freeStorage( flag_p, deleteFlag ) ;
264 flagrow.freeStorage( rflag_p, deleteFlagR ) ;
265 weight.freeStorage( wgt_p, deleteWgt ) ;
266 gdata.putStorage( grid_p, deleteDataG ) ;
267 gwgt.putStorage( wgrid_p, deleteWgtG ) ;
268 convFunc.putStorage( conv_p, deleteConv ) ;
[2381]269 delete sumw_p ;
270}
271
[2396]272#define NEED_UNDERSCORES
273#if defined(NEED_UNDERSCORES)
274#define ggridsd2 ggridsd2_
275#endif
276extern "C" {
277 void ggridsd2(Double*,
278 const Complex*,
279 Int*,
280 Int*,
281 Int*,
282 const Int*,
283 const Int*,
284 const Float*,
285 Int*,
286 Int*,
287 Complex*,
288 Float*,
289 Int*,
290 Complex*,
291 Float*,
292 Float*,
293 Complex*,
294 Float*,
295 Float*,
296 Int*,
297 Int*,
298 Int *,
299 Int *,
300 Int*,
301 Int*,
302 Float*,
303 Int*,
304 Int*,
305 Double*);
306}
307void STGrid::call_ggridsd2( Array<Double> &xypos,
308 Array<Complex> &spectra,
309 Int &nvispol,
310 Int &nvischan,
311 Array<Int> &flagtra,
312 Array<Int> &flagrow,
313 Array<Float> &weight,
314 Int &nrow,
315 Int &irow,
316 Array<Complex> &gdata,
317 Array<Float> &gwgt,
318 Array<Int> &npoints,
319 Array<Complex> &clipmin,
320 Array<Float> &clipwmin,
321 Array<Float> &clipcmin,
322 Array<Complex> &clipmax,
323 Array<Float> &clipwmax,
324 Array<Float> &clipcmax,
325 Int &nx,
326 Int &ny,
327 Int &npol,
328 Int &nchan,
329 Int &support,
330 Int &sampling,
331 Vector<Float> &convFunc,
332 Int *chanMap,
333 Int *polMap )
334{
335 // parameters for gridding
336 Int idopsf = 0 ;
337 Int len = npol*nchan ;
338 Double *sumw_p = new Double[len] ;
339 {
340 Double *work_p = sumw_p ;
341 for ( Int i = 0 ; i < len ; i++ ) {
342 *work_p = 0.0 ;
343 work_p++ ;
344 }
345 }
[2383]346
[2396]347 // prepare pointer
348 Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG, deleteNpts, deleteCMin, deleteCWMin, deleteCCMin, deleteCMax, deleteCWMax, deleteCCMax ;
349 Double *xy_p = xypos.getStorage( deletePos ) ;
350 const Complex *values_p = spectra.getStorage( deleteData ) ;
351 const Int *flag_p = flagtra.getStorage( deleteFlag ) ;
352 const Int *rflag_p = flagrow.getStorage( deleteFlagR ) ;
353 const Float *wgt_p = weight.getStorage( deleteWgt ) ;
354 Complex *grid_p = gdata.getStorage( deleteDataG ) ;
355 Float *wgrid_p = gwgt.getStorage( deleteWgtG ) ;
356 Float *conv_p = convFunc.getStorage( deleteConv ) ;
357 Int *npts_p = npoints.getStorage( deleteNpts ) ;
358 Complex *cmin_p = clipmin.getStorage( deleteCMin ) ;
359 Float *cwmin_p = clipwmin.getStorage( deleteCWMin ) ;
360 Float *ccmin_p = clipcmin.getStorage( deleteCCMin ) ;
361 Complex *cmax_p = clipmax.getStorage( deleteCMax ) ;
362 Float *cwmax_p = clipwmax.getStorage( deleteCWMax ) ;
363 Float *ccmax_p = clipcmax.getStorage( deleteCCMax ) ;
364
365 // pass copy of irow to ggridsd since it will be modified in theroutine
366 Int irowCopy = irow ;
367
368 // call ggridsd
369 ggridsd2( xy_p,
370 values_p,
371 &nvispol,
372 &nvischan,
373 &idopsf,
374 flag_p,
375 rflag_p,
376 wgt_p,
377 &nrow,
378 &irowCopy,
379 grid_p,
380 wgrid_p,
381 npts_p,
382 cmin_p,
383 cwmin_p,
384 ccmin_p,
385 cmax_p,
386 cwmax_p,
387 ccmax_p,
388 &nx,
389 &ny,
390 &npol,
391 &nchan,
392 &support,
393 &sampling,
394 conv_p,
395 chanMap,
396 polMap,
397 sumw_p ) ;
398
399 // finalization
400 xypos.putStorage( xy_p, deletePos ) ;
401 spectra.freeStorage( values_p, deleteData ) ;
402 flagtra.freeStorage( flag_p, deleteFlag ) ;
403 flagrow.freeStorage( rflag_p, deleteFlagR ) ;
404 weight.freeStorage( wgt_p, deleteWgt ) ;
405 gdata.putStorage( grid_p, deleteDataG ) ;
406 gwgt.putStorage( wgrid_p, deleteWgtG ) ;
407 convFunc.putStorage( conv_p, deleteConv ) ;
408 clipmin.putStorage( cmin_p, deleteCMin ) ;
409 clipwmin.putStorage( cwmin_p, deleteCWMin ) ;
410 clipcmin.putStorage( ccmin_p, deleteCCMin ) ;
411 clipmax.putStorage( cmax_p, deleteCMax ) ;
412 clipwmax.putStorage( cwmax_p, deleteCWMax ) ;
413 clipcmax.putStorage( ccmax_p, deleteCCMax ) ;
414 delete sumw_p ;
415}
416
[2383]417void STGrid::grid()
418{
[2384]419 LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
[2385]420 double t0,t1 ;
[2384]421
[2383]422 // data selection
[2385]423 t0 = mathutil::gettimeofday_sec() ;
[2383]424 selectData() ;
[2385]425 t1 = mathutil::gettimeofday_sec() ;
[2438]426 os << LogIO::DEBUGGING << "selectData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2385]427
[2388]428 setupGrid() ;
[2383]429 setupArray() ;
430
431 if ( wtype_.compare("UNIFORM") != 0 &&
432 wtype_.compare("TINT") != 0 &&
433 wtype_.compare("TSYS") != 0 &&
434 wtype_.compare("TINTSYS") != 0 ) {
435 LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
436 os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
437 wtype_ = "UNIFORM" ;
438 }
439
[2671]440 // Warn if gauss or gjinc gridding with non-square cells
441 if ((cellx_ != celly_) && (convType_=="GAUSS"||convType_=="GJINC")) {
442 os << LogIO::WARN
443 << "The " << convType_ << " gridding doesn't support non-square grid." << endl
444 << "Result may be wrong." << LogIO::POST;
445 }
446
[2384]447 // grid parameter
448 os << LogIO::DEBUGGING ;
449 os << "----------" << endl ;
450 os << "Data selection summary" << endl ;
451 os << " ifno = " << ifno_ << endl ;
452 os << " pollist = " << pollist_ << endl ;
453 os << " scanlist = " << scanlist_ << endl ;
454 os << "----------" << endl ;
455 os << "Grid parameter summary" << endl ;
456 os << " (nx,ny) = (" << nx_ << "," << ny_ << ")" << endl ;
457 os << " (cellx,celly) = (" << cellx_ << "," << celly_ << ")" << endl ;
458 os << " center = " << center_ << endl ;
459 os << " weighting = " << wtype_ << endl ;
[2671]460 os << " convfunc = " << convType_ << endl;
461 if (convType_ == "GAUSS") {
462 os << " gwidth = " << gwidth_ << endl;
463 os << " truncate = " << truncate_ << endl;
464 }
465 else if (convType_ == "GJINC") {
466 os << " gwidth = " << gwidth_ << endl;
467 os << " jwidth = " << jwidth_ << endl;
468 os << " truncate = " << truncate_ << endl;
469 }
470 else {
[2678]471 os << " support = " << userSupport_ << endl;
[2671]472 }
[2396]473 os << " doclip = " << (doclip_?"True":"False") << endl ;
[2384]474 os << "----------" << LogIO::POST ;
475 os << LogIO::NORMAL ;
476
[2398]477 if ( doclip_ )
[2396]478 gridPerRowWithClipping() ;
[2383]479 else
480 gridPerRow() ;
481}
482
[2398]483void STGrid::updateChunkShape()
[2383]484{
485 // TODO: nchunk_ must be determined from nchan_, npol_, and (nx_,ny_)
486 // by considering data size to be allocated for ggridsd input/output
[2385]487 nchunk_ = 400 ;
[2383]488 nchunk_ = min( nchunk_, nrow_ ) ;
[2393]489 vshape_ = IPosition( 1, nchunk_ ) ;
490 wshape_ = IPosition( 2, nchan_, nchunk_ ) ;
491 dshape_ = IPosition( 2, 2, nchunk_ ) ;
[2383]492}
493
[2393]494struct STGChunk {
495 Int nrow ;
496 Array<Complex> spectra;
497 Array<Int> flagtra;
498 Array<Int> rflag;
499 Array<Float> weight;
500 Array<Double> direction;
501 STGChunk(IPosition const &wshape, IPosition const &vshape,
502 IPosition const &dshape)
503 : spectra(wshape), flagtra(wshape), rflag(vshape), weight(wshape),
504 direction(dshape)
505 { }
506};
507
508struct STCommonData {
509 Int gnx;
510 Int gny;
511 Int *chanMap;
512 Vector<Float> convFunc ;
513 Array<Complex> gdataArrC;
514 Array<Float> gwgtArr;
515 STCommonData(IPosition const &gshape, Array<Float> const &data)
516 : gdataArrC(gshape, 0.0), gwgtArr(data) {}
517};
518
[2396]519struct STCommonDataWithClipping {
520 Int gnx;
521 Int gny;
522 Int *chanMap;
523 Vector<Float> convFunc ;
524 Array<Complex> gdataArrC;
525 Array<Float> gwgtArr;
526 Array<Int> npoints ;
527 Array<Complex> clipMin ;
528 Array<Float> clipWMin ;
529 Array<Float> clipCMin ;
530 Array<Complex> clipMax ;
531 Array<Float> clipWMax ;
532 Array<Float> clipCMax ;
533 STCommonDataWithClipping(IPosition const &gshape,
534 IPosition const &pshape,
535 Array<Float> const &data)
536 : gdataArrC(gshape, 0.0),
537 gwgtArr(data),
538 npoints(pshape, 0),
539 clipMin(gshape, Complex(FLT_MAX,0.0)),
540 clipWMin(gshape, 0.0),
541 clipCMin(gshape, 0.0),
542 clipMax(gshape, Complex(-FLT_MAX,0.0)),
543 clipWMax(gshape, 0.0),
544 clipCMax(gshape, 0.0)
545 {}
546};
547
[2393]548#define DO_AHEAD 3
549
550struct STContext {
551 STCommonData &common;
552 FIFO<STGChunk *, DO_AHEAD> queue;
553 STGrid *const self;
554 const Int pol;
555 STContext(STGrid *obj, STCommonData &common, Int pol)
[2868]556 : common(common), self(obj), pol(pol) {}
[2393]557};
558
[2396]559struct STContextWithClipping {
560 STCommonDataWithClipping &common;
561 FIFO<STGChunk *, DO_AHEAD> queue;
562 STGrid *const self;
563 const Int pol;
564 STContextWithClipping(STGrid *obj, STCommonDataWithClipping &common, Int pol)
[2868]565 : common(common), self(obj), pol(pol) {}
[2396]566};
[2393]567
[2396]568
[2393]569bool STGrid::produceChunk(void *ctx) throw(PCException)
570{
571 STContext &context = *(STContext *)ctx;
572 if ( context.self->nprocessed_ >= context.self->nrow_ ) {
573 return false;
574 }
575 STGChunk *chunk = new STGChunk(context.self->wshape_,
576 context.self->vshape_,
577 context.self->dshape_);
578
579 double t0 = mathutil::gettimeofday_sec() ;
580 chunk->nrow = context.self->getDataChunk(
581 context.self->wshape_, context.self->vshape_, context.self->dshape_,
582 chunk->spectra, chunk->direction,
583 chunk->flagtra, chunk->rflag, chunk->weight);
584 double t1 = mathutil::gettimeofday_sec() ;
585 context.self->eGetData_ += t1-t0 ;
586
587 context.queue.lock();
588 context.queue.put(chunk);
589 context.queue.unlock();
590 return true;
591}
592
593void STGrid::consumeChunk(void *ctx) throw(PCException)
594{
595 STContext &context = *(STContext *)ctx;
596 STGChunk *chunk = NULL;
597 try {
598 context.queue.lock();
599 chunk = context.queue.get();
600 context.queue.unlock();
601 } catch (FullException &e) {
602 context.queue.unlock();
603 // TODO: log error
604 throw PCException();
605 }
606
607 double t0, t1 ;
608 // world -> pixel
609 Array<Double> xypos( context.self->dshape_ ) ;
610 t0 = mathutil::gettimeofday_sec() ;
611 context.self->toPixel( chunk->direction, xypos ) ;
612 t1 = mathutil::gettimeofday_sec() ;
613 context.self->eToPixel_ += t1-t0 ;
614
615 // call ggridsd
616 Int nvispol = 1 ;
617 Int irow = -1 ;
618 t0 = mathutil::gettimeofday_sec() ;
619 context.self->call_ggridsd( xypos,
620 chunk->spectra,
621 nvispol,
622 context.self->nchan_,
623 chunk->flagtra,
624 chunk->rflag,
625 chunk->weight,
626 chunk->nrow,
627 irow,
628 context.common.gdataArrC,
629 context.common.gwgtArr,
630 context.common.gnx,
631 context.common.gny,
632 context.self->npol_,
633 context.self->nchan_,
634 context.self->convSupport_,
635 context.self->convSampling_,
636 context.common.convFunc,
637 context.common.chanMap,
638 (Int*)&context.pol ) ;
639 t1 = mathutil::gettimeofday_sec() ;
640 context.self->eGGridSD_ += t1-t0 ;
641
642 delete chunk;
643}
644
[2378]645void STGrid::gridPerRow()
646{
647 LogIO os( LogOrigin("STGrid", "gridPerRow", WHERE) ) ;
648 double t0, t1 ;
649
650
[2379]651 // grid data
652 // Extend grid plane with convSupport_
[2393]653 // Int gnx = nx_+convSupport_*2 ;
654 // Int gny = ny_+convSupport_*2 ;
655 Int gnx = nx_;
656 Int gny = ny_;
657
[2378]658 IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
659 // 2011/12/20 TN
660 // data_ and gwgtArr share storage
661 data_.resize( gshape ) ;
662 data_ = 0.0 ;
[2851]663 flag_.resize( gshape ) ;
664 flag_ = (uChar)0;
[2393]665 STCommonData common = STCommonData(gshape, data_);
666 common.gnx = gnx ;
667 common.gny = gny ;
[2378]668
[2379]669 // parameters for gridding
670 Int *chanMap = new Int[nchan_] ;
[2393]671 for ( Int i = 0 ; i < nchan_ ; i++ ) {
672 chanMap[i] = i ;
[2379]673 }
[2393]674 common.chanMap = chanMap;
[2379]675
[2393]676 // convolution kernel
677 t0 = mathutil::gettimeofday_sec() ;
678 setConvFunc( common.convFunc ) ;
679 t1 = mathutil::gettimeofday_sec() ;
[2438]680 os << LogIO::DEBUGGING << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2393]681
[2379]682 // for performance check
[2393]683 eGetData_ = 0.0 ;
684 eToPixel_ = 0.0 ;
685 eGGridSD_ = 0.0 ;
[2384]686 double eInitPol = 0.0 ;
[2379]687
[2390]688 for ( uInt ifile = 0 ; ifile < nfile_ ; ifile++ ) {
689 initTable( ifile ) ;
[2380]690
[2393]691 os << "start table " << ifile << ": " << infileList_[ifile] << LogIO::POST ;
[2398]692 Broker broker = Broker(produceChunk, consumeChunk);
[2390]693 for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
[2383]694 t0 = mathutil::gettimeofday_sec() ;
[2393]695 initPol( ipol ) ; // set ptab_ and attach()
[2383]696 t1 = mathutil::gettimeofday_sec() ;
[2390]697 eInitPol += t1-t0 ;
[2383]698
[2393]699 STContext context(this, common, ipol);
[2390]700
701 os << "start pol " << ipol << LogIO::POST ;
702
[2393]703 nprocessed_ = 0 ;
704#if 1
705 broker.runProducerAsMasterThread(&context, DO_AHEAD);
706#else
707 for (;;) {
708 bool produced = produceChunk(&context);
709 if (! produced) {
710 break;
711 }
712 consumeChunk(&context);
[2390]713 }
[2393]714#endif
715
[2390]716 os << "end pol " << ipol << LogIO::POST ;
[2393]717
[2383]718 }
[2393]719 os << "end table " << ifile << LogIO::POST ;
[2378]720 }
[2438]721 os << LogIO::DEBUGGING << "initPol: elapsed time is " << eInitPol << " sec." << LogIO::POST ;
722 os << LogIO::DEBUGGING << "getData: elapsed time is " << eGetData_-eToInt-eGetWeight << " sec." << LogIO::POST ;
723 os << LogIO::DEBUGGING << "toPixel: elapsed time is " << eToPixel_ << " sec." << LogIO::POST ;
724 os << LogIO::DEBUGGING << "ggridsd: elapsed time is " << eGGridSD_ << " sec." << LogIO::POST ;
725 os << LogIO::DEBUGGING << "toInt: elapsed time is " << eToInt << " sec." << LogIO::POST ;
726 os << LogIO::DEBUGGING << "getWeight: elapsed time is " << eGetWeight << " sec." << LogIO::POST ;
[2383]727
[2379]728 delete chanMap ;
729
[2378]730 // set data
[2393]731 setData( common.gdataArrC, common.gwgtArr ) ;
[2379]732
[2378]733}
734
[2396]735void STGrid::consumeChunkWithClipping(void *ctx) throw(PCException)
736{
737 STContextWithClipping &context = *(STContextWithClipping *)ctx;
738 STGChunk *chunk = NULL;
739 try {
740 context.queue.lock();
741 chunk = context.queue.get();
742 context.queue.unlock();
743 } catch (FullException &e) {
744 context.queue.unlock();
745 // TODO: log error
746 throw PCException();
747 }
748
749 double t0, t1 ;
750 // world -> pixel
751 Array<Double> xypos( context.self->dshape_ ) ;
752 t0 = mathutil::gettimeofday_sec() ;
753 context.self->toPixel( chunk->direction, xypos ) ;
754 t1 = mathutil::gettimeofday_sec() ;
755 context.self->eToPixel_ += t1-t0 ;
756
757 // call ggridsd
758 Int nvispol = 1 ;
759 Int irow = -1 ;
760 t0 = mathutil::gettimeofday_sec() ;
761 context.self->call_ggridsd2( xypos,
762 chunk->spectra,
763 nvispol,
764 context.self->nchan_,
765 chunk->flagtra,
766 chunk->rflag,
767 chunk->weight,
768 chunk->nrow,
769 irow,
770 context.common.gdataArrC,
771 context.common.gwgtArr,
772 context.common.npoints,
773 context.common.clipMin,
774 context.common.clipWMin,
775 context.common.clipCMin,
776 context.common.clipMax,
777 context.common.clipWMax,
778 context.common.clipCMax,
779 context.common.gnx,
780 context.common.gny,
781 context.self->npol_,
782 context.self->nchan_,
783 context.self->convSupport_,
784 context.self->convSampling_,
785 context.common.convFunc,
786 context.common.chanMap,
787 (Int*)&context.pol ) ;
788 t1 = mathutil::gettimeofday_sec() ;
789 context.self->eGGridSD_ += t1-t0 ;
790
791 delete chunk;
792}
793
794void STGrid::gridPerRowWithClipping()
795{
796 LogIO os( LogOrigin("STGrid", "gridPerRowWithClipping", WHERE) ) ;
797 double t0, t1 ;
798
799
800 // grid data
801 // Extend grid plane with convSupport_
802 // Int gnx = nx_+convSupport_*2 ;
803 // Int gny = ny_+convSupport_*2 ;
804 Int gnx = nx_;
805 Int gny = ny_;
806
807 IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
808 IPosition pshape( 3, gnx, gny, npol_ ) ;
809 // 2011/12/20 TN
810 // data_ and gwgtArr share storage
811 data_.resize( gshape ) ;
812 data_ = 0.0 ;
[2851]813 flag_.resize( gshape ) ;
814 flag_ = (uChar)0;
[2396]815 STCommonDataWithClipping common = STCommonDataWithClipping( gshape,
816 pshape,
817 data_ ) ;
818 common.gnx = gnx ;
819 common.gny = gny ;
820
821 // parameters for gridding
822 Int *chanMap = new Int[nchan_] ;
823 for ( Int i = 0 ; i < nchan_ ; i++ ) {
824 chanMap[i] = i ;
825 }
826 common.chanMap = chanMap;
827
828 // convolution kernel
829 t0 = mathutil::gettimeofday_sec() ;
830 setConvFunc( common.convFunc ) ;
831 t1 = mathutil::gettimeofday_sec() ;
[2438]832 os << LogIO::DEBUGGING << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2396]833
834 // for performance check
835 eGetData_ = 0.0 ;
836 eToPixel_ = 0.0 ;
837 eGGridSD_ = 0.0 ;
838 double eInitPol = 0.0 ;
839
840 for ( uInt ifile = 0 ; ifile < nfile_ ; ifile++ ) {
841 initTable( ifile ) ;
842
843 os << "start table " << ifile << ": " << infileList_[ifile] << LogIO::POST ;
[2398]844 Broker broker = Broker(produceChunk, consumeChunkWithClipping);
[2396]845 for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
846 t0 = mathutil::gettimeofday_sec() ;
847 initPol( ipol ) ; // set ptab_ and attach()
848 t1 = mathutil::gettimeofday_sec() ;
849 eInitPol += t1-t0 ;
850
851 STContextWithClipping context(this, common, ipol);
852
853 os << "start pol " << ipol << LogIO::POST ;
854
855 nprocessed_ = 0 ;
856#if 1
857 broker.runProducerAsMasterThread(&context, DO_AHEAD);
858#else
859 for (;;) {
860 bool produced = produceChunk(&context);
861 if (! produced) {
862 break;
863 }
864 consumeChunkWithClipping(&context);
865 }
866#endif
867
868 os << "end pol " << ipol << LogIO::POST ;
869
870 }
871 os << "end table " << ifile << LogIO::POST ;
872 }
[2438]873 os << LogIO::DEBUGGING << "initPol: elapsed time is " << eInitPol << " sec." << LogIO::POST ;
874 os << LogIO::DEBUGGING << "getData: elapsed time is " << eGetData_-eToInt-eGetWeight << " sec." << LogIO::POST ;
875 os << LogIO::DEBUGGING << "toPixel: elapsed time is " << eToPixel_ << " sec." << LogIO::POST ;
876 os << LogIO::DEBUGGING << "ggridsd2: elapsed time is " << eGGridSD_ << " sec." << LogIO::POST ;
877 os << LogIO::DEBUGGING << "toInt: elapsed time is " << eToInt << " sec." << LogIO::POST ;
878 os << LogIO::DEBUGGING << "getWeight: elapsed time is " << eGetWeight << " sec." << LogIO::POST ;
[2396]879
880 delete chanMap ;
881
882 // clip min and max in each grid
883// os << "BEFORE CLIPPING" << LogIO::POST ;
884// os << "gdataArrC=" << common.gdataArrC << LogIO::POST ;
885// os << "gwgtArr=" << common.gwgtArr << LogIO::POST ;
886 t0 = mathutil::gettimeofday_sec() ;
887 clipMinMax( common.gdataArrC,
888 common.gwgtArr,
889 common.npoints,
890 common.clipMin,
891 common.clipWMin,
892 common.clipCMin,
893 common.clipMax,
894 common.clipWMax,
895 common.clipCMax ) ;
896 t1 = mathutil::gettimeofday_sec() ;
[2438]897 os << LogIO::DEBUGGING << "clipMinMax: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2396]898// os << "AFTER CLIPPING" << LogIO::POST ;
899// os << "gdataArrC=" << common.gdataArrC << LogIO::POST ;
900// os << "gwgtArr=" << common.gwgtArr << LogIO::POST ;
901
902 // set data
903 setData( common.gdataArrC, common.gwgtArr ) ;
904
905}
906
907void STGrid::clipMinMax( Array<Complex> &grid,
908 Array<Float> &weight,
909 Array<Int> &npoints,
910 Array<Complex> &clipmin,
911 Array<Float> &clipwmin,
912 Array<Float> &clipcmin,
913 Array<Complex> &clipmax,
914 Array<Float> &clipwmax,
915 Array<Float> &clipcmax )
916{
917 //LogIO os( LogOrigin("STGrid","clipMinMax",WHERE) ) ;
918
919 // prepare pointers
920 Bool delG, delW, delNP, delCMin, delCWMin, delCCMin, delCMax, delCWMax, delCCMax ;
921 Complex *grid_p = grid.getStorage( delG ) ;
922 Float *wgt_p = weight.getStorage( delW ) ;
923 const Int *npts_p = npoints.getStorage( delNP ) ;
924 const Complex *cmin_p = clipmin.getStorage( delCMin ) ;
925 const Float *cwmin_p = clipwmin.getStorage( delCWMin ) ;
926 const Float *ccmin_p = clipcmin.getStorage( delCCMin ) ;
927 const Complex *cmax_p = clipmax.getStorage( delCMax ) ;
928 const Float *cwmax_p = clipwmax.getStorage( delCWMax ) ;
929 const Float *ccmax_p = clipcmax.getStorage( delCCMax ) ;
930
931 const IPosition &gshape = grid.shape() ;
932 long offset = gshape[0] * gshape[1] * gshape[2] ; // nx * ny * npol
933 Int nchan = gshape[3] ;
934 long origin = nchan * offset ;
935 for ( long i = 0 ; i < offset ; i++ ) {
936 if ( *npts_p > 2 ) {
937 for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
938 // clip minimum and maximum
939 *grid_p -= (*cmin_p)*(*cwmin_p)*(*ccmin_p)
940 + (*cmax_p)*(*cwmax_p)*(*ccmax_p) ;
941 *wgt_p -= (*cwmin_p)*(*ccmin_p)
942 + (*cwmax_p)*(*ccmax_p) ;
943
944 grid_p += offset ;
945 wgt_p += offset ;
946 cmin_p += offset ;
947 cwmin_p += offset ;
948 ccmin_p += offset ;
949 cmax_p += offset ;
950 cwmax_p += offset ;
951 ccmax_p += offset ;
952 }
953 grid_p -= origin ;
954 wgt_p -= origin ;
955 cmin_p -= origin ;
956 cwmin_p -= origin ;
957 ccmin_p -= origin ;
958 cmax_p -= origin ;
959 cwmax_p -= origin ;
960 ccmax_p -= origin ;
961 }
962 grid_p++ ;
963 wgt_p++ ;
964 npts_p++ ;
965 cmin_p++ ;
966 cwmin_p++ ;
967 ccmin_p++ ;
968 cmax_p++ ;
969 cwmax_p++ ;
970 ccmax_p++ ;
971 }
972 grid_p -= offset ;
973 wgt_p -= offset ;
974 npts_p -= offset ;
975 cmin_p -= offset ;
976 cwmin_p -= offset ;
977 ccmin_p -= offset ;
978 cmax_p -= offset ;
979 cwmax_p -= offset ;
980 ccmax_p -= offset ;
981
982 // finalization
983 grid.putStorage( grid_p, delG ) ;
984 weight.putStorage( wgt_p, delW ) ;
985 npoints.freeStorage( npts_p, delNP ) ;
986 clipmin.freeStorage( cmin_p, delCMin ) ;
987 clipwmin.freeStorage( cwmin_p, delCWMin ) ;
988 clipcmin.freeStorage( ccmin_p, delCCMin ) ;
989 clipmax.freeStorage( cmax_p, delCMax ) ;
990 clipwmax.freeStorage( cwmax_p, delCWMax ) ;
991 clipcmax.freeStorage( ccmax_p, delCCMax ) ;
992}
993
[2382]994void STGrid::initPol( Int ipol )
995{
[2386]996 LogIO os( LogOrigin("STGrid","initPol",WHERE) ) ;
997 if ( npolOrg_ == 1 ) {
998 os << "single polarization data." << LogIO::POST ;
[2385]999 ptab_ = tab_ ;
[2386]1000 }
[2385]1001 else
[2426]1002 ptab_ = tab_( tab_.col("POLNO") == pollist_[ipol] ) ;
[2382]1003
1004 attach( ptab_ ) ;
1005}
1006
[2390]1007void STGrid::initTable( uInt idx )
1008{
1009 tab_ = tableList_[idx] ;
1010 nrow_ = rows_[idx] ;
[2398]1011 updateChunkShape() ;
[2390]1012}
1013
[2378]1014void STGrid::setData( Array<Complex> &gdata,
[2368]1015 Array<Float> &gwgt )
1016{
[2378]1017 // 2011/12/20 TN
1018 // gwgt and data_ share storage
[2368]1019 LogIO os( LogOrigin("STGrid","setData",WHERE) ) ;
1020 double t0, t1 ;
1021 t0 = mathutil::gettimeofday_sec() ;
[2378]1022 uInt len = data_.nelements() ;
[2851]1023 Bool b1, b2, b3 ;
[2374]1024 const Complex *gdata_p = gdata.getStorage( b1 ) ;
[2868]1025 Float *gwgt_p = gwgt.getStorage( b2 ) ; // storage shared with data_
[2851]1026 uChar *gflg_p = flag_.getStorage( b3 ) ;
[2368]1027 for ( uInt i = 0 ; i < len ; i++ ) {
[2851]1028 if (gwgt_p[i] > 0.0) {
1029 gwgt_p[i] = (gdata_p[i]).real() / gwgt_p[i];
1030 gflg_p[i] = (uChar)0;
1031 }
1032 else {
1033 gflg_p[i] = (uChar)1;
1034 }
[2368]1035 }
1036 gdata.freeStorage( gdata_p, b1 ) ;
[2378]1037 data_.putStorage( gwgt_p, b2 ) ;
[2851]1038 flag_.putStorage( gflg_p, b3 ) ;
[2368]1039 t1 = mathutil::gettimeofday_sec() ;
[2438]1040 os << LogIO::DEBUGGING << "setData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
[2368]1041}
1042
[2388]1043void STGrid::setupGrid()
1044{
1045 Double xmin,xmax,ymin,ymax ;
1046 mapExtent( xmin, xmax, ymin, ymax ) ;
1047
1048 setupGrid( nxUI_, nyUI_, cellxUI_, cellyUI_,
1049 xmin, xmax, ymin, ymax, centerUI_ ) ;
1050}
1051
[2356]1052void STGrid::setupGrid( Int &nx,
1053 Int &ny,
1054 String &cellx,
1055 String &celly,
1056 Double &xmin,
1057 Double &xmax,
1058 Double &ymin,
1059 Double &ymax,
1060 String &center )
1061{
[2375]1062 LogIO os( LogOrigin("STGrid","setupGrid",WHERE) ) ;
[2356]1063 //cout << "nx=" << nx << ", ny=" << ny << endl ;
[2359]1064
1065 // center position
1066 if ( center.size() == 0 ) {
1067 center_(0) = 0.5 * ( xmin + xmax ) ;
1068 center_(1) = 0.5 * ( ymin + ymax ) ;
1069 }
1070 else {
1071 String::size_type pos0 = center.find( " " ) ;
1072 if ( pos0 == String::npos ) {
1073 throw AipsError( "bad string format in parameter center" ) ;
1074 }
1075 String::size_type pos1 = center.find( " ", pos0+1 ) ;
1076 String typestr, xstr, ystr ;
1077 if ( pos1 != String::npos ) {
1078 typestr = center.substr( 0, pos0 ) ;
1079 xstr = center.substr( pos0+1, pos1-pos0 ) ;
1080 ystr = center.substr( pos1+1 ) ;
1081 // todo: convert to J2000 (or direction ref for DIRECTION column)
1082 }
1083 else {
1084 typestr = "J2000" ;
1085 xstr = center.substr( 0, pos0 ) ;
1086 ystr = center.substr( pos0+1 ) ;
1087 }
1088 QuantumHolder qh ;
1089 String err ;
1090 qh.fromString( err, xstr ) ;
1091 Quantum<Double> xcen = qh.asQuantumDouble() ;
1092 qh.fromString( err, ystr ) ;
1093 Quantum<Double> ycen = qh.asQuantumDouble() ;
1094 center_(0) = xcen.getValue( "rad" ) ;
1095 center_(1) = ycen.getValue( "rad" ) ;
[2461]1096 double base = 0.5 * (xmin + xmax) ;
1097 int maxrotate = 1 ;
1098 int nelem = 2 * maxrotate + 1 ;
1099 double *sep = new double[nelem] ;
1100 for ( int i = 0 ; i < nelem ; i++ )
1101 sep[i] = abs(base - center_[0] - (i-maxrotate) * C::_2pi) ;
1102// os << "sep[0]=" << sep[0] << endl
1103// << "sep[1]=" << sep[1] << endl
1104// << "sep[2]=" << sep[2] << LogIO::POST ;
1105 int idx = 0 ;
1106 base = sep[0] ;
1107 int nrotate = 0 ;
1108 while ( idx < nelem ) {
1109 if ( base > sep[idx] ) {
1110 base = sep[idx] ;
1111 nrotate = idx ;
1112 }
1113 idx++ ;
1114 }
1115 delete sep ;
1116 nrotate -= maxrotate ;
1117// os << "nrotate = " << nrotate << LogIO::POST ;
1118 center_[0] += nrotate * C::_2pi ;
[2359]1119 }
[2461]1120// os << "xmin=" << xmin << LogIO::POST ;
1121// os << "center_=" << center_ << LogIO::POST ;
[2359]1122
[2356]1123 nx_ = nx ;
1124 ny_ = ny ;
1125 if ( nx < 0 && ny > 0 ) {
1126 nx_ = ny ;
1127 ny_ = ny ;
1128 }
1129 if ( ny < 0 && nx > 0 ) {
1130 nx_ = nx ;
1131 ny_ = nx ;
1132 }
[2375]1133
1134 //Double wx = xmax - xmin ;
1135 //Double wy = ymax - ymin ;
1136 Double wx = max( abs(xmax-center_(0)), abs(xmin-center_(0)) ) * 2 ;
1137 Double wy = max( abs(ymax-center_(1)), abs(ymin-center_(1)) ) * 2 ;
1138 // take 10% margin
1139 wx *= 1.10 ;
1140 wy *= 1.10 ;
1141
1142 Quantum<Double> qcellx ;
1143 Quantum<Double> qcelly ;
[2356]1144 //cout << "nx_ = " << nx_ << ", ny_ = " << ny_ << endl ;
1145 if ( cellx.size() != 0 && celly.size() != 0 ) {
1146 readQuantity( qcellx, cellx ) ;
1147 readQuantity( qcelly, celly ) ;
1148 }
1149 else if ( celly.size() != 0 ) {
[2375]1150 os << "Using celly to x-axis..." << LogIO::POST ;
[2356]1151 readQuantity( qcelly, celly ) ;
1152 qcellx = qcelly ;
1153 }
1154 else if ( cellx.size() != 0 ) {
[2375]1155 os << "Using cellx to y-axis..." << LogIO::POST ;
[2356]1156 readQuantity( qcellx, cellx ) ;
1157 qcelly = qcellx ;
1158 }
1159 else {
1160 if ( nx_ < 0 ) {
[2375]1161 os << "No user preference in grid setting. Using default..." << LogIO::POST ;
[2356]1162 readQuantity( qcellx, "1.0arcmin" ) ;
1163 qcelly = qcellx ;
1164 }
1165 else {
[2375]1166 if ( wx == 0.0 ) {
1167 os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ;
1168 wx = 0.00290888 ;
1169 }
1170 if ( wy == 0.0 ) {
1171 os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ;
1172 wy = 0.00290888 ;
1173 }
[2692]1174 qcellx = Quantum<Double>( wx/nx_*cos(center_[1]), "rad" ) ;
[2356]1175 qcelly = Quantum<Double>( wy/ny_, "rad" ) ;
1176 }
1177 }
1178 cellx_ = qcellx.getValue( "rad" ) ;
1179 celly_ = qcelly.getValue( "rad" ) ;
[2422]1180 //os << "cellx_=" << cellx_ << ", celly_=" << celly_ << ", cos("<<center_(1)<<")=" << cos(center_(1)) << LogIO::POST ;
[2356]1181 if ( nx_ < 0 ) {
[2375]1182 if ( wx == 0.0 ) {
1183 os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ;
1184 wx = 0.00290888 ;
1185 }
1186 if ( wy == 0.0 ) {
1187 os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ;
1188 wy = 0.00290888 ;
1189 }
[2669]1190 nx_ = Int( ceil( wx/(cellx_/cos(center_[1])) ) ) ;
[2356]1191 ny_ = Int( ceil( wy/celly_ ) ) ;
1192 }
[2669]1193
1194 // create DirectionCoordinate
1195 Matrix<Double> xform(2,2) ;
1196 xform = 0.0 ;
1197 xform.diagonal() = 1.0 ;
1198 dircoord_ = new DirectionCoordinate(MDirection::J2000,
1199 Projection( Projection::SIN ),
1200 center_[0], center_[1],
1201 -cellx_, celly_,
1202 xform,
1203 0.5*Double(nx_-1),
1204 0.5*Double(ny_-1)) ;
[2356]1205}
1206
[2388]1207void STGrid::mapExtent( Double &xmin, Double &xmax,
1208 Double &ymin, Double &ymax )
1209{
1210 //LogIO os( LogOrigin("STGrid","mapExtent",WHERE) ) ;
[2390]1211 directionCol_.attach( tableList_[0], "DIRECTION" ) ;
1212 Matrix<Double> direction = directionCol_.getColumn() ;
[2388]1213 //os << "dirCol.nrow() = " << dirCol.nrow() << LogIO::POST ;
[2790]1214 Vector<Double> ra( direction.row(0) ) ;
1215 mathutil::rotateRA( ra ) ;
[2388]1216 minMax( xmin, xmax, direction.row( 0 ) ) ;
1217 minMax( ymin, ymax, direction.row( 1 ) ) ;
[2390]1218 Double amin, amax, bmin, bmax ;
1219 for ( uInt i = 1 ; i < nfile_ ; i++ ) {
1220 directionCol_.attach( tableList_[i], "DIRECTION" ) ;
1221 direction.assign( directionCol_.getColumn() ) ;
1222 //os << "dirCol.nrow() = " << dirCol.nrow() << LogIO::POST ;
[2629]1223 // to make contiguous RA distribution (no 2pi jump)
1224 Vector<Double> ra( direction.row(0) ) ;
1225 mathutil::rotateRA( ra ) ;
[2390]1226 minMax( amin, amax, direction.row( 0 ) ) ;
1227 minMax( bmin, bmax, direction.row( 1 ) ) ;
1228 xmin = min( xmin, amin ) ;
1229 xmax = max( xmax, amax ) ;
1230 ymin = min( ymin, bmin ) ;
1231 ymax = max( ymax, bmax ) ;
1232 }
[2388]1233 //os << "(xmin,xmax)=(" << xmin << "," << xmax << ")" << LogIO::POST ;
1234 //os << "(ymin,ymax)=(" << ymin << "," << ymax << ")" << LogIO::POST ;
1235}
1236
[2593]1237void STGrid::table( Table &tab, uInt i )
1238{
[2868]1239 if ( i < nfile_ )
[2593]1240 tab = Table( infileList_[i] ) ;
1241}
1242
[2379]1243void STGrid::selectData()
[2362]1244{
[2386]1245 LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
[2438]1246 Int ifno = ifno_ ;
[2390]1247 tableList_.resize( nfile_ ) ;
[2418]1248 if ( ifno_ == -1 ) {
[2593]1249 //Table taborg( infileList_[0] ) ;
1250 Table taborg ;
1251 table( taborg, 0 ) ;
[2393]1252 ROScalarColumn<uInt> ifnoCol( taborg, "IFNO" ) ;
[2418]1253 ifno_ = ifnoCol( 0 ) ;
[2393]1254 os << LogIO::WARN
[2418]1255 << "IFNO is not given. Using default IFNO: " << ifno_ << LogIO::POST ;
[2393]1256 }
[2390]1257 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
[2593]1258 //Table taborg( infileList_[i] ) ;
1259 Table taborg ;
1260 table( taborg, i ) ;
[2389]1261 TableExprNode node ;
[2438]1262 if ( ifno != -1 || isMultiIF( taborg ) ) {
[2389]1263 os << "apply selection on IFNO" << LogIO::POST ;
[2418]1264 node = taborg.col("IFNO") == ifno_ ;
[2389]1265 }
1266 if ( scanlist_.size() > 0 ) {
1267 os << "apply selection on SCANNO" << LogIO::POST ;
1268 node = node && taborg.col("SCANNO").in( scanlist_ ) ;
1269 }
1270 if ( node.isNull() ) {
1271 tableList_[i] = taborg ;
1272 }
1273 else {
1274 tableList_[i] = taborg( node ) ;
1275 }
[2438]1276 os << LogIO::DEBUGGING << "tableList_[" << i << "].nrow()=" << tableList_[i].nrow() << LogIO::POST ;
[2389]1277 if ( tableList_[i].nrow() == 0 ) {
1278 os << LogIO::SEVERE
[2418]1279 << "No corresponding rows for given selection: IFNO " << ifno_ ;
[2389]1280 if ( scanlist_.size() > 0 )
1281 os << " SCANNO " << scanlist_ ;
1282 os << LogIO::EXCEPTION ;
1283 }
[2362]1284 }
1285}
1286
[2386]1287Bool STGrid::isMultiIF( Table &tab )
1288{
1289 ROScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
1290 Vector<uInt> ifnos = ifnoCol.getColumn() ;
1291 return anyNE( ifnos, ifnos[0] ) ;
1292}
1293
[2382]1294void STGrid::attach( Table &tab )
[2379]1295{
1296 // attach to table
[2382]1297 spectraCol_.attach( tab, "SPECTRA" ) ;
1298 flagtraCol_.attach( tab, "FLAGTRA" ) ;
1299 directionCol_.attach( tab, "DIRECTION" ) ;
1300 flagRowCol_.attach( tab, "FLAGROW" ) ;
1301 tsysCol_.attach( tab, "TSYS" ) ;
1302 intervalCol_.attach( tab, "INTERVAL" ) ;
[2379]1303}
1304
[2393]1305Int STGrid::getDataChunk(
1306 IPosition const &wshape,
1307 IPosition const &vshape,
[2868]1308 IPosition const &/* dshape */,
[2393]1309 Array<Complex> &spectra,
1310 Array<Double> &direction,
1311 Array<Int> &flagtra,
1312 Array<Int> &rflag,
1313 Array<Float> &weight )
1314{
1315 LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
1316
1317 Array<Float> spectraF_(wshape);
1318 Array<uChar> flagtraUC_(wshape);
1319 Array<uInt> rflagUI_(vshape);
1320 Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
1321 if ( nrow < nchunk_ ) {
1322 spectra.resize( spectraF_.shape() ) ;
1323 flagtra.resize( flagtraUC_.shape() ) ;
1324 rflag.resize( rflagUI_.shape() ) ;
1325 }
1326 double t0, t1 ;
1327 t0 = mathutil::gettimeofday_sec() ;
1328 convertArray( spectra, spectraF_ ) ;
1329 toInt( flagtraUC_, flagtra ) ;
1330 toInt( rflagUI_, rflag ) ;
1331 t1 = mathutil::gettimeofday_sec() ;
1332 eToInt = t1 - t0 ;
1333
1334 return nrow ;
1335}
1336
1337#if 0
[2379]1338Int STGrid::getDataChunk( Array<Complex> &spectra,
1339 Array<Double> &direction,
1340 Array<Int> &flagtra,
1341 Array<Int> &rflag,
1342 Array<Float> &weight )
[2378]1343{
[2385]1344 LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
[2381]1345 Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
[2383]1346 if ( nrow < nchunk_ ) {
1347 spectra.resize( spectraF_.shape() ) ;
1348 flagtra.resize( flagtraUC_.shape() ) ;
1349 rflag.resize( rflagUI_.shape() ) ;
1350 }
[2381]1351 double t0, t1 ;
1352 t0 = mathutil::gettimeofday_sec() ;
1353 convertArray( spectra, spectraF_ ) ;
[2382]1354 toInt( flagtraUC_, flagtra ) ;
1355 toInt( rflagUI_, rflag ) ;
[2381]1356 t1 = mathutil::gettimeofday_sec() ;
[2384]1357 eToInt = t1 - t0 ;
[2381]1358
[2379]1359 return nrow ;
[2378]1360}
[2393]1361#endif
[2378]1362
[2379]1363Int STGrid::getDataChunk( Array<Float> &spectra,
1364 Array<Double> &direction,
1365 Array<uChar> &flagtra,
1366 Array<uInt> &rflag,
1367 Array<Float> &weight )
[2375]1368{
[2379]1369 LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
[2383]1370 Int nrow = spectra.shape()[1] ;
1371 Int remainingRow = nrow_ - nprocessed_ ;
1372 if ( remainingRow < nrow ) {
1373 nrow = remainingRow ;
1374 IPosition mshape( 2, nchan_, nrow ) ;
1375 IPosition vshape( 1, nrow ) ;
1376 spectra.resize( mshape ) ;
1377 flagtra.resize( mshape ) ;
1378 direction.resize( IPosition(2,2,nrow) ) ;
1379 rflag.resize( vshape ) ;
1380 weight.resize( mshape ) ;
[2379]1381 }
[2383]1382 // 2011/12/22 TN
1383 // tsys shares its storage with weight
1384 Array<Float> tsys( weight ) ;
1385 Array<Double> tint( rflag.shape() ) ;
[2380]1386
[2383]1387 Vector<uInt> rflagVec( rflag ) ;
1388 Vector<Double> tintVec( tint ) ;
[2379]1389
[2383]1390 RefRows rows( nprocessed_, nprocessed_+nrow-1, 1 ) ;
[2386]1391 //os<<LogIO::DEBUGGING<<"nprocessed_="<<nprocessed_<<": rows.nrows()="<<rows.nrows()<<LogIO::POST ;
[2383]1392 spectraCol_.getColumnCells( rows, spectra ) ;
1393 flagtraCol_.getColumnCells( rows, flagtra ) ;
1394 directionCol_.getColumnCells( rows, direction ) ;
[2629]1395 // to make contiguous RA distribution (no 2pi jump)
1396 Vector<Double> v( Matrix<Double>(direction).row(0) ) ;
1397 mathutil::rotateRA( v ) ;
[2383]1398 flagRowCol_.getColumnCells( rows, rflagVec ) ;
1399 intervalCol_.getColumnCells( rows, tintVec ) ;
1400 Vector<Float> tsysTemp = tsysCol_( nprocessed_ ) ;
1401 if ( tsysTemp.nelements() == (uInt)nchan_ )
1402 tsysCol_.getColumnCells( rows, tsys ) ;
1403 else
1404 tsys = tsysTemp[0] ;
[2385]1405
[2384]1406 double t0,t1 ;
1407 t0 = mathutil::gettimeofday_sec() ;
[2379]1408 getWeight( weight, tsys, tint ) ;
[2384]1409 t1 = mathutil::gettimeofday_sec() ;
1410 eGetWeight += t1-t0 ;
[2379]1411
1412 nprocessed_ += nrow ;
1413
1414 return nrow ;
1415}
1416
1417void STGrid::setupArray()
1418{
[2376]1419 LogIO os( LogOrigin("STGrid","setupArray",WHERE) ) ;
[2390]1420 ROScalarColumn<uInt> polnoCol( tableList_[0], "POLNO" ) ;
[2371]1421 Vector<uInt> pols = polnoCol.getColumn() ;
[2376]1422 //os << pols << LogIO::POST ;
[2371]1423 Vector<uInt> pollistOrg ;
[2386]1424 npolOrg_ = 0 ;
[2371]1425 uInt polno ;
1426 for ( uInt i = 0 ; i < polnoCol.nrow() ; i++ ) {
[2393]1427 //polno = polnoCol( i ) ;
[2371]1428 polno = pols( i ) ;
1429 if ( allNE( pollistOrg, polno ) ) {
[2386]1430 pollistOrg.resize( npolOrg_+1, True ) ;
1431 pollistOrg[npolOrg_] = polno ;
1432 npolOrg_++ ;
[2371]1433 }
1434 }
1435 if ( pollist_.size() == 0 )
1436 pollist_ = pollistOrg ;
1437 else {
1438 Vector<uInt> newlist ;
1439 uInt newsize = 0 ;
1440 for ( uInt i = 0 ; i < pollist_.size() ; i++ ) {
1441 if ( anyEQ( pollistOrg, pollist_[i] ) ) {
1442 newlist.resize( newsize+1, True ) ;
1443 newlist[newsize] = pollist_[i] ;
1444 newsize++ ;
1445 }
1446 }
1447 pollist_.assign( newlist ) ;
1448 }
1449 npol_ = pollist_.size() ;
1450 if ( npol_ == 0 ) {
1451 os << LogIO::SEVERE << "Empty pollist" << LogIO::EXCEPTION ;
1452 }
[2390]1453 rows_.resize( nfile_ ) ;
1454 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
1455 rows_[i] = tableList_[i].nrow() / npolOrg_ ;
[2398]1456 //if ( nrow_ < rows_[i] )
1457 // nrow_ = rows_[i] ;
[2390]1458 }
1459 flagtraCol_.attach( tableList_[0], "FLAGTRA" ) ;
1460 nchan_ = flagtraCol_( 0 ).nelements() ;
[2371]1461// os << "npol_ = " << npol_ << "(" << pollist_ << ")" << endl
1462// << "nchan_ = " << nchan_ << endl
1463// << "nrow_ = " << nrow_ << LogIO::POST ;
1464}
1465
[2375]1466void STGrid::getWeight( Array<Float> &w,
[2382]1467 Array<Float> &tsys,
1468 Array<Double> &tint )
1469{
[2383]1470 LogIO os( LogOrigin("STGrid","getWeight",WHERE) ) ;
[2384]1471
[2382]1472 // 2011/12/22 TN
1473 // w (weight) and tsys share storage
1474 IPosition refShape = tsys.shape() ;
1475 Int nchan = refShape[0] ;
1476 Int nrow = refShape[1] ;
1477// os << "nchan=" << nchan << ", nrow=" << nrow << LogIO::POST ;
1478// os << "w.shape()=" << w.shape() << endl
1479// << "tsys.shape()=" << tsys.shape() << endl
1480// << "tint.shape()=" << tint.shape() << LogIO::POST ;
1481
1482 // set weight
1483 if ( wtype_.compare( "UNIFORM" ) == 0 ) {
1484 w = 1.0 ;
1485 }
1486 else if ( wtype_.compare( "TINT" ) == 0 ) {
1487 Bool b0, b1 ;
1488 Float *w_p = w.getStorage( b0 ) ;
1489 Float *w0_p = w_p ;
1490 const Double *ti_p = tint.getStorage( b1 ) ;
1491 const Double *w1_p = ti_p ;
1492 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1493 for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1494 *w0_p = *w1_p ;
1495 w0_p++ ;
1496 }
1497 w1_p++ ;
1498 }
1499 w.putStorage( w_p, b0 ) ;
1500 tint.freeStorage( ti_p, b1 ) ;
1501 }
1502 else if ( wtype_.compare( "TSYS" ) == 0 ) {
1503 Bool b0 ;
1504 Float *w_p = w.getStorage( b0 ) ;
1505 Float *w0_p = w_p ;
1506 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1507 for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1508 Float temp = *w0_p ;
1509 *w0_p = 1.0 / ( temp * temp ) ;
1510 w0_p++ ;
1511 }
1512 }
1513 w.putStorage( w_p, b0 ) ;
1514 }
1515 else if ( wtype_.compare( "TINTSYS" ) == 0 ) {
1516 Bool b0, b1 ;
1517 Float *w_p = w.getStorage( b0 ) ;
1518 Float *w0_p = w_p ;
1519 const Double *ti_p = tint.getStorage( b1 ) ;
1520 const Double *w1_p = ti_p ;
1521 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1522 Float interval = *w1_p ;
1523 for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1524 Float temp = *w0_p ;
1525 *w0_p = interval / ( temp * temp ) ;
1526 w0_p++ ;
1527 }
1528 w1_p++ ;
1529 }
1530 w.putStorage( w_p, b0 ) ;
1531 tint.freeStorage( ti_p, b1 ) ;
1532 }
1533 else {
1534 //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
1535 //os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
1536 w = 1.0 ;
1537 }
1538}
1539
[2375]1540void STGrid::toInt( Array<uChar> &u, Array<Int> &v )
[2356]1541{
[2375]1542 uInt len = u.nelements() ;
[2356]1543 Int *int_p = new Int[len] ;
1544 Bool deleteIt ;
[2375]1545 const uChar *data_p = u.getStorage( deleteIt ) ;
[2356]1546 Int *i_p = int_p ;
1547 const uChar *u_p = data_p ;
1548 for ( uInt i = 0 ; i < len ; i++ ) {
1549 *i_p = ( *u_p == 0 ) ? 0 : 1 ;
1550 i_p++ ;
1551 u_p++ ;
1552 }
[2375]1553 u.freeStorage( data_p, deleteIt ) ;
1554 v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
[2356]1555}
1556
[2375]1557void STGrid::toInt( Array<uInt> &u, Array<Int> &v )
[2356]1558{
[2375]1559 uInt len = u.nelements() ;
[2356]1560 Int *int_p = new Int[len] ;
1561 Bool deleteIt ;
[2375]1562 const uInt *data_p = u.getStorage( deleteIt ) ;
[2356]1563 Int *i_p = int_p ;
1564 const uInt *u_p = data_p ;
1565 for ( uInt i = 0 ; i < len ; i++ ) {
1566 *i_p = ( *u_p == 0 ) ? 0 : 1 ;
1567 i_p++ ;
1568 u_p++ ;
1569 }
[2375]1570 u.freeStorage( data_p, deleteIt ) ;
1571 v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
[2356]1572}
1573
[2375]1574void STGrid::toPixel( Array<Double> &world, Array<Double> &pixel )
[2356]1575{
1576 uInt nrow = world.shape()[1] ;
[2375]1577 Bool bw, bp ;
[2638]1578 Double *w_p = world.getStorage( bw ) ;
[2375]1579 Double *p_p = pixel.getStorage( bp ) ;
[2638]1580 Double *ww_p = w_p ;
[2375]1581 Double *wp_p = p_p ;
[2638]1582 IPosition vshape( 1, 2 ) ;
1583 Vector<Double> _world, _pixel ;
[2375]1584 for ( uInt i = 0 ; i < nrow ; i++ ) {
[2638]1585 _world.takeStorage( vshape, ww_p, SHARE ) ;
1586 _pixel.takeStorage( vshape, wp_p, SHARE ) ;
[2669]1587 dircoord_->toPixel( _pixel, _world ) ;
[2638]1588 ww_p += 2 ;
1589 wp_p += 2 ;
[2356]1590 }
[2638]1591 world.putStorage( w_p, bw ) ;
1592 pixel.putStorage( p_p, bp ) ;
[2356]1593}
1594
1595void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize )
1596{
1597 convFunc = 0.0 ;
1598 for ( Int i = 0 ; i < convSize/2 ; i++ )
1599 convFunc(i) = 1.0 ;
1600}
1601
1602#define NEED_UNDERSCORES
1603#if defined(NEED_UNDERSCORES)
1604#define grdsf grdsf_
[2671]1605#define grdgauss grdgauss_
1606#define grdjinc1 grdjinc1_
[2356]1607#endif
[2673]1608#if defined(USE_CASAPY)
[2356]1609extern "C" {
[2671]1610 void grdsf(Double*, Double*);
1611 void grdgauss(Double*, Double*, Double*);
1612 void grdjinc1(Double*, Double*, Int*, Double*);
[2356]1613}
[2673]1614#else
1615extern "C" {
1616 void grdsf(Double*, Double*);
1617}
1618void grdgauss(Double *hwhm, Double *val, Double *out)
1619{
1620 *out = exp(-log(2.0) * (*val / *hwhm) * (*val / *hwhm));
1621}
1622void grdjinc1(Double *c, Double *val, Int *normalize, Double *out)
1623{
1624 // Calculate J_1(x) using approximate formula
1625 Double x = C::pi * *val / *c;
1626 Double ax = fabs(x);
1627 Double ans;
1628 if ( ax < 8.0 ) {
1629 Double y = x * x;
1630 Double ans1 = x * (72362614232.0 + y * (-7895059235.0
1631 + y * (242396853.1 + y * (-2972611.439
1632 + y * (15704.48260 + y * (-30.16036606))))));
1633 Double ans2 = 144725228442.0 + y * (2300535178.0
1634 + y * (18583304.74 + y * (99447.43394
1635 + y * (376.9991397 + y * 1.0))));
1636 ans = ans1 / ans2;
1637 }
1638 else {
1639 Double z = 8.0 / ax;
1640 Double y = z * z;
1641 Double xx = ax - 2.356194491;
1642 Double ans1 = 1.0 + y * (0.183105e-2 + y * (-0.3516396496e-4
1643 + y * (0.2457520174e-5 + y * (-0.240337019e-6))));
1644 Double ans2 = 0.04687499995 + y * (-0.2002690873e-3
1645 + y * (0.8449199096e-5 + y * (-0.88228987e-6
1646 + y * (0.105787412e-6))));
1647 ans = sqrt(0.636619772 / ax) * (cos(xx) * ans1
1648 - z * sin(xx) * ans2);
1649 if (x < 0.0)
1650 ans = -ans;
1651 }
1652
1653 // Then, calculate Jinc
1654 if (x == 0.0) {
1655 *out = 0.5;
1656 }
1657 else {
1658 *out = ans / x;
1659 }
1660
1661 if (*normalize == 1)
1662 *out = *out / 0.5;
1663}
1664#endif
[2356]1665void STGrid::spheroidalFunc( Vector<Float> &convFunc )
1666{
1667 convFunc = 0.0 ;
1668 for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
1669 Double nu = Double(i) / Double(convSupport_*convSampling_) ;
1670 Double val ;
1671 grdsf( &nu, &val ) ;
1672 convFunc(i) = ( 1.0 - nu * nu ) * val ;
1673 }
1674}
1675
[2671]1676void STGrid::gaussFunc( Vector<Float> &convFunc, Double hwhm, Double truncate )
[2356]1677{
1678 convFunc = 0.0 ;
[2671]1679 Int len = (Int)(truncate*Double(convSampling_)+0.5);
1680 Double out, val;
[2363]1681 for ( Int i = 0 ; i < len ; i++ ) {
[2671]1682 val = Double(i) / Double(convSampling_) ;
1683 grdgauss(&hwhm, &val, &out);
1684 convFunc(i) = out;
[2356]1685 }
1686}
1687
[2671]1688void STGrid::gjincFunc( Vector<Float> &convFunc, Double hwhm, Double c, Double truncate )
1689{
1690 convFunc = 0.0;
1691 Double out1, out2, val;
1692 Int normalize = 1;
1693 if (truncate >= 0.0) {
1694 Int len = (Int)(truncate*Double(convSampling_)+0.5);
1695 for (Int i = 0 ; i < len ; i++) {
1696 val = Double(i) / Double(convSampling_);
1697 grdgauss(&hwhm, &val, &out1);
1698 grdjinc1(&c, &val, &normalize, &out2);
1699 convFunc(i) = out1 * out2;
1700 }
1701 }
1702 else {
1703 Int len = convFunc.nelements();
1704 for (Int i = 0 ; i < len ; i++) {
1705 val = Double(i) / Double(convSampling_);
1706 grdjinc1(&c, &val, &normalize, &out2);
1707 if (out2 <= 0.0) {
1708 LogIO os(LogOrigin("STGrid","gjincFunc",WHERE));
1709 os << LogIO::DEBUG1 << "convFunc is automatically truncated at radius " << val << LogIO::POST;
1710 break;
1711 }
1712 grdgauss(&hwhm, &val, &out1);
1713 convFunc(i) = out1 * out2;
1714 }
1715 }
1716}
1717
[2356]1718void STGrid::pbFunc( Vector<Float> &convFunc )
1719{
1720 convFunc = 0.0 ;
1721}
1722
[2671]1723vector<float> STGrid::getConvFunc()
1724{
1725 LogIO os(LogOrigin("STGrid","getConvFunc",WHERE));
1726 Vector<Float> convFunc;
1727 vector<float> out;
1728
1729 if (cellx_ <= 0.0 || celly_ <= 0.0) {
1730 selectData();
1731 setupGrid();
1732 }
1733
1734 if (convType_ == "BOX" || convType_ == "SF") {
1735 setConvFunc(convFunc);
1736 }
1737 else if (convType_ == "GAUSS") {
1738 Quantum<Double> q1,q2;
1739 readQuantity(q1,gwidth_);
1740 readQuantity(q2,truncate_);
1741// if (celly_ <= 0.0
1742// && ((!q1.getUnit().empty()&&q1.getUnit()!="pixel") ||
1743// (!q2.getUnit().empty()&&q2.getUnit()!="pixel"))) {
1744// throw AipsError("You have to call defineImage to get correct convFunc");
1745// }
1746 setConvFunc(convFunc);
1747 }
1748 else if (convType_ == "GJINC") {
1749 Quantum<Double> q1,q2,q3;
1750 readQuantity(q1,gwidth_);
1751 readQuantity(q2,truncate_);
1752 readQuantity(q3,jwidth_);
1753// if (celly_ <= 0.0
1754// && ((!q1.getUnit().empty()&&q1.getUnit()!="pixel") ||
1755// (!q2.getUnit().empty()&&q2.getUnit()!="pixel") ||
1756// (!q3.getUnit().empty()&&q3.getUnit()!="pixel"))) {
1757// throw AipsError("You have to call defineImage to get correct convFunc");
1758// }
1759 setConvFunc(convFunc);
1760 }
1761 else if (convType_ == "PB") {
1762 throw AipsError("Grid function PB is not available");
1763 }
1764 else {
1765 throw AipsError("Unknown grid function: "+convType_);
1766 }
1767
1768 convFunc.tovector(out);
1769 return out;
1770}
1771
[2356]1772void STGrid::setConvFunc( Vector<Float> &convFunc )
1773{
[2671]1774 LogIO os(LogOrigin("STGrid","setConvFunc",WHERE));
[2356]1775 convSupport_ = userSupport_ ;
1776 if ( convType_ == "BOX" ) {
1777 if ( convSupport_ < 0 )
1778 convSupport_ = 0 ;
1779 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
1780 convFunc.resize( convSize ) ;
1781 boxFunc( convFunc, convSize ) ;
[2671]1782 os << LogIO::DEBUGGING
1783 << "convType_ = " << convType_ << endl
1784 << "convSupport_ = " << convSupport_ << LogIO::POST;
[2356]1785 }
1786 else if ( convType_ == "SF" ) {
1787 if ( convSupport_ < 0 )
1788 convSupport_ = 3 ;
1789 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
1790 convFunc.resize( convSize ) ;
1791 spheroidalFunc( convFunc ) ;
[2671]1792 os << LogIO::DEBUGGING
1793 << "convType_ = " << convType_ << endl
1794 << "convSupport_ = " << convSupport_ << LogIO::POST;
[2356]1795 }
1796 else if ( convType_ == "GAUSS" ) {
[2671]1797 // determine pixel gwidth
1798 // default is HWHM corresponding to b = 1.0 (Mangum et al. 2007)
[2868]1799 Double pixelGW = -1.0;
[2671]1800 Quantum<Double> q ;
1801 if (!gwidth_.empty()) {
1802 readQuantity( q, gwidth_ );
1803 if ( q.getUnit().empty() || q.getUnit()=="pixel" ) {
1804 pixelGW = q.getValue();
1805 }
1806 else {
1807 pixelGW = q.getValue("rad")/celly_;
1808 }
[2363]1809 }
[2678]1810 pixelGW = (pixelGW >= 0.0) ? pixelGW : sqrt(log(2.0));
1811 if (pixelGW < 0.0) {
1812 os << LogIO::SEVERE
1813 << "Negative width is specified for gaussian" << LogIO::EXCEPTION;
1814 }
[2671]1815 // determine truncation radius
1816 // default is 3 * HWHM
[2868]1817 Double truncate = -1.0;
[2671]1818 if (!truncate_.empty()) {
1819 readQuantity( q, truncate_ );
1820 if ( q.getUnit().empty() || q.getUnit()=="pixel" ) {
1821 truncate = q.getValue();
1822 }
1823 else {
1824 truncate = q.getValue("rad")/celly_;
1825 }
1826 }
[2675]1827 //convSupport_ = (Int)(truncate+0.5);
[2678]1828 truncate = (truncate >= 0.0) ? truncate : 3.0 * pixelGW;
1829 convSupport_ = Int(truncate);
[2675]1830 convSupport_ += (((truncate-(Double)convSupport_) > 0.0) ? 1 : 0);
[2671]1831 Int convSize = convSampling_ * ( 2*convSupport_ + 2 ) ;
[2356]1832 convFunc.resize( convSize ) ;
[2671]1833 gaussFunc( convFunc, pixelGW, truncate ) ;
1834 os << LogIO::DEBUGGING
1835 << "convType_ = " << convType_ << endl
1836 << "convSupport_ = " << convSupport_ << endl
[2673]1837 << "truncate_ = " << truncate << "pixel" << endl
[2671]1838 << "gwidth_ = " << pixelGW << "pixel" << LogIO::POST;
[2356]1839 }
[2671]1840 else if ( convType_ == "GJINC" ) {
1841 // determine pixel gwidth
1842 // default is HWHM corresponding to b = 2.52 (Mangum et al. 2007)
[2868]1843 Double pixelGW = -1.0;
[2671]1844 Quantum<Double> q ;
1845 if (!gwidth_.empty()) {
1846 readQuantity( q, gwidth_ );
1847 if ( q.getUnit().empty() || q.getUnit()=="pixel" ) {
1848 pixelGW = q.getValue();
1849 }
1850 else {
1851 pixelGW = q.getValue("rad")/celly_;
1852 }
1853 }
[2678]1854 pixelGW = (pixelGW >= 0.0) ? pixelGW : sqrt(log(2.0)) * 2.52;
1855 if (pixelGW < 0.0) {
1856 os << LogIO::SEVERE
1857 << "Negative width is specified for gaussian" << LogIO::EXCEPTION;
1858 }
[2671]1859 // determine pixel c
1860 // default is c = 1.55 (Mangum et al. 2007)
[2868]1861 Double pixelJW = -1.0;
[2671]1862 if (!jwidth_.empty()) {
1863 readQuantity( q, jwidth_ );
1864 if ( q.getUnit().empty() || q.getUnit()=="pixel" ) {
1865 pixelJW = q.getValue();
1866 }
1867 else {
1868 pixelJW = q.getValue("rad")/celly_;
1869 }
[2678]1870 }
1871 pixelJW = (pixelJW >= 0.0) ? pixelJW : 1.55;
1872 if (pixelJW < 0.0) {
1873 os << LogIO::SEVERE
1874 << "Negative width is specified for jinc" << LogIO::EXCEPTION;
1875 }
[2671]1876 // determine truncation radius
1877 // default is -1.0 (truncate at first null)
1878 Double truncate = -1.0;
1879 if (!truncate_.empty()) {
1880 readQuantity( q, truncate_ );
1881 if ( q.getUnit().empty() || q.getUnit()=="pixel" ) {
1882 truncate = q.getValue();
1883 }
1884 else {
1885 truncate = q.getValue("rad")/celly_;
1886 }
1887 }
[2675]1888 //convSupport_ = (truncate >= 0.0) ? (Int)(truncate+0.5) : (Int)(2*pixelJW+0.5);
1889 Double convSupportF = (truncate >= 0.0) ? truncate : (2*pixelJW);
1890 convSupport_ = (Int)convSupportF;
1891 convSupport_ += (((convSupportF-(Double)convSupport_) > 0.0) ? 1 : 0);
[2671]1892 Int convSize = convSampling_ * ( 2*convSupport_ + 2 ) ;
1893 convFunc.resize( convSize ) ;
1894 gjincFunc( convFunc, pixelGW, pixelJW, truncate ) ;
1895 os << LogIO::DEBUGGING
1896 << "convType_ = " << convType_ << endl
1897 << "convSupport_ = " << convSupport_ << endl
[2673]1898 << "truncate_ = " << truncate << "pixel" << endl
[2671]1899 << "gwidth_ = " << pixelGW << "pixel" << endl
1900 << "jwidth_ = " << pixelJW << "pixel" << LogIO::POST;
1901 }
[2359]1902 else if ( convType_ == "PB" ) {
1903 if ( convSupport_ < 0 )
1904 convSupport_ = 0 ;
[2356]1905 pbFunc( convFunc ) ;
[2359]1906 }
[2356]1907 else {
1908 throw AipsError( "Unsupported convolution function" ) ;
1909 }
1910}
1911
1912string STGrid::saveData( string outfile )
1913{
[2368]1914 LogIO os( LogOrigin("STGrid", "saveData", WHERE) ) ;
1915 double t0, t1 ;
1916 t0 = mathutil::gettimeofday_sec() ;
1917
[2393]1918 //Int polno = 0 ;
[2371]1919 String outfile_ ;
[2356]1920 if ( outfile.size() == 0 ) {
[2389]1921 if ( infileList_[0].lastchar() == '/' ) {
1922 outfile_ = infileList_[0].substr( 0, infileList_[0].size()-1 ) ;
[2356]1923 }
1924 else {
[2389]1925 outfile_ = infileList_[0] ;
[2356]1926 }
1927 outfile_ += ".grid" ;
1928 }
1929 else {
1930 outfile_ = outfile ;
1931 }
[2371]1932 Table tab ;
1933 prepareTable( tab, outfile_ ) ;
[2593]1934 fillTable( tab ) ;
1935
1936 t1 = mathutil::gettimeofday_sec() ;
1937 os << LogIO::DEBUGGING << "saveData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
1938
1939 return outfile_ ;
1940}
1941
1942void STGrid::prepareTable( Table &tab, String &name )
1943{
1944 Table t( infileList_[0], Table::Old ) ;
1945 t.deepCopy( name, Table::New, False, t.endianFormat(), True ) ;
1946 tab = Table( name, Table::Update ) ;
1947 // 2012/02/13 TN
1948 // explicitly copy subtables since no rows including subtables are
1949 // copied by Table::deepCopy with noRows=True
[2684]1950 //TableCopy::copySubTables( tab, t ) ;
1951 const TableRecord &inrec = t.keywordSet();
1952 TableRecord &outrec = tab.rwKeywordSet();
1953 for (uInt i = 0 ; i < inrec.nfields() ; i++) {
1954 if (inrec.type(i) == TpTable) {
1955 String name = inrec.name(i);
1956 Table intable = inrec.asTable(name);
1957 Table outtable = outrec.asTable(name);
1958 TableCopy::copyRows(outtable, intable);
1959 }
1960 }
[2593]1961}
1962
1963void STGrid::fillTable( Table &tab )
1964{
[2868]1965 //IPosition dshape = data_.shape() ;
[2361]1966 Int nrow = nx_ * ny_ * npol_ ;
1967 tab.rwKeywordSet().define( "nPol", npol_ ) ;
[2360]1968 tab.addRow( nrow ) ;
[2356]1969 Vector<Double> cpix( 2 ) ;
1970 cpix(0) = Double( nx_ - 1 ) * 0.5 ;
1971 cpix(1) = Double( ny_ - 1 ) * 0.5 ;
1972 Vector<Double> dir( 2 ) ;
[2669]1973 Vector<Double> pix( 2 );
[2356]1974 ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
1975 ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
[2803]1976 ArrayColumn<uChar> flagtraCol( tab, "FLAGTRA" ) ;
[2356]1977 ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
[2478]1978 ScalarColumn<uInt> scannoCol( tab, "SCANNO" ) ;
[2356]1979 Int irow = 0 ;
[2371]1980 Vector<Float> sp( nchan_ ) ;
[2851]1981 Vector<uChar> flag( nchan_ ) ;
1982 Bool bsp, bdata, bflag ;
[2371]1983 const Float *data_p = data_.getStorage( bdata ) ;
[2851]1984 const uChar *flag_p = flag_.getStorage( bflag ) ;
[2371]1985 Float *wsp_p, *sp_p ;
1986 const Float *wdata_p = data_p ;
[2851]1987 const uChar *wflag_p = flag_p ;
[2371]1988 long step = nx_ * ny_ * npol_ ;
1989 long offset ;
[2478]1990 uInt scanno = 0 ;
[2356]1991 for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
[2669]1992 pix(1) = (Double)(iy);
[2356]1993 for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
[2894]1994 pix(0) = (Double)(ix);
[2669]1995 dircoord_->toWorld(dir,pix);
1996 //os << "dir[" << ix << "," << iy << "]=" << dir << LogIO::POST;
[2361]1997 for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
[2669]1998 offset = ix + nx_ * (iy + ipol * ny_) ;
[2371]1999 //os << "offset = " << offset << LogIO::POST ;
2000 sp_p = sp.getStorage( bsp ) ;
2001 wsp_p = sp_p ;
2002 wdata_p = data_p + offset ;
[2851]2003 wflag_p = flag_p + offset ;
[2371]2004 for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
2005 *wsp_p = *wdata_p ;
2006 wsp_p++ ;
2007 wdata_p += step ;
[2851]2008 flag[ichan] = *wflag_p;
2009 wflag_p += step ;
[2371]2010 }
2011 sp.putStorage( sp_p, bsp ) ;
[2356]2012 spectraCol.put( irow, sp ) ;
[2851]2013 flagtraCol.put( irow, flag ) ;
[2356]2014 directionCol.put( irow, dir ) ;
[2360]2015 polnoCol.put( irow, pollist_[ipol] ) ;
[2478]2016 scannoCol.put( irow, scanno ) ;
[2356]2017 irow++ ;
2018 }
[2478]2019 scanno++ ;
[2356]2020 }
2021 }
[2371]2022 data_.freeStorage( data_p, bdata ) ;
[2851]2023 flag_.freeStorage( flag_p, bflag ) ;
[2368]2024
[2413]2025 fillMainColumns( tab ) ;
[2356]2026}
2027
[2413]2028void STGrid::fillMainColumns( Table &tab )
2029{
[2414]2030 // values for fill
[2594]2031 //Table t( infileList_[0], Table::Old ) ;
2032 Table t ;
2033 table( t, 0 ) ;
[2413]2034 Table tsel = t( t.col( "IFNO" ) == (uInt)ifno_, 1 ) ;
[2414]2035 ROTableRow row( tsel ) ;
[2413]2036 row.get( 0 ) ;
2037 const TableRecord &rec = row.record() ;
2038 uInt freqId = rec.asuInt( "FREQ_ID" ) ;
[2424]2039 uInt molId = rec.asuInt( "MOLECULE_ID" ) ;
2040 uInt tcalId = rec.asuInt( "TCAL_ID" ) ;
2041 uInt focusId = rec.asuInt( "FOCUS_ID" ) ;
2042 uInt weatherId = rec.asuInt( "WEATHER_ID" ) ;
2043 String srcname = rec.asString( "SRCNAME" ) ;
2044 String fieldname = rec.asString( "FIELDNAME" ) ;
[2414]2045 Vector<Float> defaultTsys( 1, 1.0 ) ;
2046 // @todo how to set flagtra for gridded spectra?
2047 Vector<uChar> flagtra = rec.asArrayuChar( "FLAGTRA" ) ;
2048 flagtra = (uChar)0 ;
[2424]2049 Float opacity = rec.asFloat( "OPACITY" ) ;
2050 Double srcvel = rec.asDouble( "SRCVELOCITY" ) ;
2051 Vector<Double> srcpm = rec.asArrayDouble( "SRCPROPERMOTION" ) ;
2052 Vector<Double> srcdir = rec.asArrayDouble( "SRCDIRECTION" ) ;
2053 Vector<Double> scanrate = rec.asArrayDouble( "SCANRATE" ) ;
[2439]2054 Double time = rec.asDouble( "TIME" ) ;
2055 Double interval = rec.asDouble( "INTERVAL" ) ;
[2414]2056
2057 // fill columns
[2413]2058 Int nrow = tab.nrow() ;
2059 ScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
[2688]2060 ScalarColumn<uInt> beamnoCol(tab, "BEAMNO");
[2413]2061 ScalarColumn<uInt> freqIdCol( tab, "FREQ_ID" ) ;
[2424]2062 ScalarColumn<uInt> molIdCol( tab, "MOLECULE_ID" ) ;
2063 ScalarColumn<uInt> tcalidCol( tab, "TCAL_ID" ) ;
2064 ScalarColumn<Int> fitidCol( tab, "FIT_ID" ) ;
2065 ScalarColumn<uInt> focusidCol( tab, "FOCUS_ID" ) ;
2066 ScalarColumn<uInt> weatheridCol( tab, "WEATHER_ID" ) ;
[2414]2067 ArrayColumn<uChar> flagtraCol( tab, "FLAGTRA" ) ;
[2424]2068 ScalarColumn<uInt> rflagCol( tab, "FLAGROW" ) ;
[2414]2069 ArrayColumn<Float> tsysCol( tab, "TSYS" ) ;
[2424]2070 ScalarColumn<String> srcnameCol( tab, "SRCNAME" ) ;
2071 ScalarColumn<String> fieldnameCol( tab, "FIELDNAME" ) ;
2072 ScalarColumn<Int> srctypeCol( tab, "SRCTYPE" ) ;
2073 ScalarColumn<Float> opacityCol( tab, "OPACITY" ) ;
2074 ScalarColumn<Double> srcvelCol( tab, "SRCVELOCITY" ) ;
2075 ArrayColumn<Double> srcpmCol( tab, "SRCPROPERMOTION" ) ;
2076 ArrayColumn<Double> srcdirCol( tab, "SRCDIRECTION" ) ;
2077 ArrayColumn<Double> scanrateCol( tab, "SCANRATE" ) ;
[2439]2078 ScalarColumn<Double> timeCol( tab, "TIME" ) ;
2079 ScalarColumn<Double> intervalCol( tab, "INTERVAL" ) ;
[2413]2080 for ( Int i = 0 ; i < nrow ; i++ ) {
2081 ifnoCol.put( i, (uInt)ifno_ ) ;
[2688]2082 beamnoCol.put(i, 0);
[2413]2083 freqIdCol.put( i, freqId ) ;
[2424]2084 molIdCol.put( i, molId ) ;
2085 tcalidCol.put( i, tcalId ) ;
2086 fitidCol.put( i, -1 ) ;
2087 focusidCol.put( i, focusId ) ;
2088 weatheridCol.put( i, weatherId ) ;
[2803]2089 //flagtraCol.put( i, flagtra ) ;
[2424]2090 rflagCol.put( i, 0 ) ;
[2414]2091 tsysCol.put( i, defaultTsys ) ;
[2424]2092 srcnameCol.put( i, srcname ) ;
2093 fieldnameCol.put( i, fieldname ) ;
2094 srctypeCol.put( i, (Int)SrcType::PSON ) ;
2095 opacityCol.put( i, opacity ) ;
2096 srcvelCol.put( i, srcvel ) ;
2097 srcpmCol.put( i, srcpm ) ;
2098 srcdirCol.put( i, srcdir ) ;
2099 scanrateCol.put( i, scanrate ) ;
[2439]2100 timeCol.put( i, time ) ;
2101 intervalCol.put( i, interval ) ;
[2413]2102 }
[2371]2103}
[2413]2104
[2686]2105vector<int> STGrid::getResultantMapSize()
2106{
2107 vector<int> r(2);
2108 r[0] = nx_;
2109 r[1] = ny_;
2110 return r;
2111}
2112
2113vector<double> STGrid::getResultantCellSize()
2114{
2115 vector<double> r(2);
2116 r[0] = cellx_;
2117 r[1] = celly_;
2118 return r;
2119}
2120
[2593]2121// STGrid2
2122STGrid2::STGrid2()
2123 : STGrid()
2124{
[2413]2125}
[2593]2126
2127STGrid2::STGrid2( const ScantableWrapper &s )
2128 : STGrid()
2129{
2130 setScantable( s ) ;
2131}
2132
2133STGrid2::STGrid2( const vector<ScantableWrapper> &v )
2134 : STGrid()
2135{
2136 setScantableList( v ) ;
2137}
2138
2139void STGrid2::setScantable( const ScantableWrapper &s )
2140{
2141 nfile_ = 1 ;
2142 dataList_.resize( nfile_ ) ;
2143 dataList_[0] = s ;
2144 infileList_.resize( nfile_ ) ;
2145 infileList_[0] = s.getCP()->table().tableName() ;
2146}
2147
2148void STGrid2::setScantableList( const vector<ScantableWrapper> &v )
2149{
2150 nfile_ = v.size() ;
2151 dataList_.resize( nfile_ ) ;
2152 infileList_.resize( nfile_ ) ;
2153 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
2154 dataList_[i] = v[i] ;
2155 infileList_[i] = v[i].getCP()->table().tableName() ;
2156 }
2157}
2158
[2594]2159ScantableWrapper STGrid2::getResultAsScantable( int tp )
[2593]2160{
[2682]2161 ScantableWrapper sw( tp ) ;
[2594]2162 CountedPtr<Scantable> s = sw.getCP() ;
[2593]2163 s->setHeader( dataList_[0].getCP()->getHeader() ) ;
2164 Table tout, tin ;
2165 String subt[] = { "FREQUENCIES", "FOCUS", "WEATHER",
2166 "TCAL", "MOLECULES", "HISTORY", "FIT" } ;
2167 for ( uInt i = 0 ; i < 7 ; i++ ) {
2168 tout = s->table().rwKeywordSet().asTable(subt[i]) ;
2169 tin = dataList_[0].getCP()->table().rwKeywordSet().asTable(subt[i]) ;
2170 TableCopy::copyRows( tout, tin ) ;
[2706]2171 tout.rwKeywordSet() = tin.rwKeywordSet();
[2593]2172 }
2173 fillTable( s->table() ) ;
2174 return sw ;
2175}
2176
2177void STGrid2::table( Table &tab, uInt i )
2178{
2179 if ( i < nfile_ )
2180 tab = dataList_[i].getCP()->table() ;
2181}
2182
2183}
Note: See TracBrowser for help on using the repository browser.