source: trunk/src/STGrid.cpp@ 2859

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

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: test_sdgrid[sdgrid_flagging]

Put in Release Notes: No

Module(s): sd

Description: Describe your changes here...

Handle channel flags properly.


File size: 63.0 KB
Line 
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//
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>
18#include <casa/Logging/LogIO.h>
19
20#include <coordinates/Coordinates/DirectionCoordinate.h>
21
22#include <tables/Tables/Table.h>
23#include <tables/Tables/TableRecord.h>
24#include <tables/Tables/TableRow.h>
25#include <tables/Tables/ExprNode.h>
26#include <tables/Tables/ScalarColumn.h>
27#include <tables/Tables/ArrayColumn.h>
28#include <tables/Tables/TableCopy.h>
29
30#include <measures/Measures/MDirection.h>
31
32#include "MathUtils.h"
33#include <atnf/PKSIO/SrcType.h>
34
35#include "STGrid.h"
36
37using namespace std ;
38using namespace concurrent ;
39using namespace casa ;
40using namespace asap ;
41
42namespace asap {
43
44// for performance check
45double eToInt = 0.0 ;
46double eGetWeight = 0.0 ;
47
48// constructor
49STGrid::STGrid()
50 : vshape_( 1 ), wshape_( 2 ), dshape_( 2 )
51{
52 init() ;
53}
54
55STGrid::STGrid( const string infile )
56 : vshape_( 1 ), wshape_( 2 ), dshape_( 2 )
57{
58 init() ;
59
60 setFileIn( infile ) ;
61}
62
63STGrid::STGrid( const vector<string> infile )
64{
65 init() ;
66
67 setFileList( infile ) ;
68}
69
70void STGrid::init()
71{
72 ifno_ = -1 ;
73 nx_ = -1 ;
74 ny_ = -1 ;
75 npol_ = 0 ;
76 nchan_ = 0 ;
77 nrow_ = 0 ;
78 cellx_ = 0.0 ;
79 celly_ = 0.0 ;
80 center_ = Vector<Double> ( 2, 0.0 ) ;
81 convType_ = "BOX" ;
82 wtype_ = "UNIFORM" ;
83 convSupport_ = -1 ;
84 userSupport_ = -1 ;
85 truncate_ = "";
86 gwidth_ = "";
87 jwidth_ = "";
88 convSampling_ = 100 ;
89 nprocessed_ = 0 ;
90 nchunk_ = 0 ;
91
92 // initialize user input
93 nxUI_ = -1 ;
94 nyUI_ = -1 ;
95 cellxUI_ = "" ;
96 cellyUI_ = "" ;
97 centerUI_ = "" ;
98 doclip_ = False ;
99}
100
101void STGrid::setFileIn( const string infile )
102{
103 nfile_ = 1 ;
104 String name( infile ) ;
105 infileList_.resize( nfile_ ) ;
106 infileList_[0] = String(infile) ;
107}
108
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
118void STGrid::setPolList( vector<unsigned int> pols )
119{
120 pollist_.assign( Vector<uInt>( pols ) ) ;
121}
122
123void STGrid::setScanList( vector<unsigned int> scans )
124{
125 scanlist_.assign( Vector<uInt>( scans ) ) ;
126}
127
128void STGrid::setWeight( const string wType )
129{
130 wtype_ = String( wType ) ;
131 wtype_.upcase() ;
132}
133
134void STGrid::defineImage( int nx,
135 int ny,
136 string scellx,
137 string scelly,
138 string scenter )
139{
140 nxUI_ = (Int)nx ;
141 nyUI_ = (Int)ny ;
142 cellxUI_ = String( scellx ) ;
143 cellyUI_ = String( scelly ) ;
144 centerUI_ = String( scenter ) ;
145}
146
147void STGrid::setFunc( string convType,
148 int convSupport,
149 string truncate,
150 string gwidth,
151 string jwidth )
152{
153 convType_ = String( convType ) ;
154 convType_.upcase() ;
155 userSupport_ = (Int)convSupport ;
156 truncate_ = String( truncate );
157 gwidth_ = String( gwidth );
158 jwidth_ = String( jwidth );
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}
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,
207 Int *chanMap,
208 Int *polMap )
209{
210 // parameters for gridding
211 Int idopsf = 0 ;
212 Int len = npol*nchan ;
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
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 ) ;
232
233 // pass copy of irow to ggridsd since it will be modified in theroutine
234 Int irowCopy = irow ;
235
236 // call ggridsd
237 ggridsd( xy_p,
238 values_p,
239 &nvispol,
240 &nvischan,
241 &idopsf,
242 flag_p,
243 rflag_p,
244 wgt_p,
245 &nrow,
246 &irowCopy,
247 grid_p,
248 wgrid_p,
249 &nx,
250 &ny,
251 &npol,
252 &nchan,
253 &support,
254 &sampling,
255 conv_p,
256 chanMap,
257 polMap,
258 sumw_p ) ;
259
260 // finalization
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 ) ;
269 delete sumw_p ;
270}
271
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 }
346
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
417void STGrid::grid()
418{
419 LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
420 double t0,t1 ;
421
422 // data selection
423 t0 = mathutil::gettimeofday_sec() ;
424 selectData() ;
425 t1 = mathutil::gettimeofday_sec() ;
426 os << LogIO::DEBUGGING << "selectData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
427
428 setupGrid() ;
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
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
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 ;
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 {
471 os << " support = " << userSupport_ << endl;
472 }
473 os << " doclip = " << (doclip_?"True":"False") << endl ;
474 os << "----------" << LogIO::POST ;
475 os << LogIO::NORMAL ;
476
477 if ( doclip_ )
478 gridPerRowWithClipping() ;
479 else
480 gridPerRow() ;
481}
482
483void STGrid::updateChunkShape()
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
487 nchunk_ = 400 ;
488 nchunk_ = min( nchunk_, nrow_ ) ;
489 vshape_ = IPosition( 1, nchunk_ ) ;
490 wshape_ = IPosition( 2, nchan_, nchunk_ ) ;
491 dshape_ = IPosition( 2, 2, nchunk_ ) ;
492}
493
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
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
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)
556 : self(obj), common(common), pol(pol) {}
557};
558
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)
565 : self(obj), common(common), pol(pol) {}
566};
567
568
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
645void STGrid::gridPerRow()
646{
647 LogIO os( LogOrigin("STGrid", "gridPerRow", WHERE) ) ;
648 double t0, t1 ;
649
650
651 // grid data
652 // Extend grid plane with convSupport_
653 // Int gnx = nx_+convSupport_*2 ;
654 // Int gny = ny_+convSupport_*2 ;
655 Int gnx = nx_;
656 Int gny = ny_;
657
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 ;
663 flag_.resize( gshape ) ;
664 flag_ = (uChar)0;
665 STCommonData common = STCommonData(gshape, data_);
666 common.gnx = gnx ;
667 common.gny = gny ;
668
669 // parameters for gridding
670 Int *chanMap = new Int[nchan_] ;
671 for ( Int i = 0 ; i < nchan_ ; i++ ) {
672 chanMap[i] = i ;
673 }
674 common.chanMap = chanMap;
675
676 // convolution kernel
677 t0 = mathutil::gettimeofday_sec() ;
678 setConvFunc( common.convFunc ) ;
679 t1 = mathutil::gettimeofday_sec() ;
680 os << LogIO::DEBUGGING << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
681
682 // for performance check
683 eGetData_ = 0.0 ;
684 eToPixel_ = 0.0 ;
685 eGGridSD_ = 0.0 ;
686 double eInitPol = 0.0 ;
687
688 for ( uInt ifile = 0 ; ifile < nfile_ ; ifile++ ) {
689 initTable( ifile ) ;
690
691 os << "start table " << ifile << ": " << infileList_[ifile] << LogIO::POST ;
692 Broker broker = Broker(produceChunk, consumeChunk);
693 for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
694 t0 = mathutil::gettimeofday_sec() ;
695 initPol( ipol ) ; // set ptab_ and attach()
696 t1 = mathutil::gettimeofday_sec() ;
697 eInitPol += t1-t0 ;
698
699 STContext context(this, common, ipol);
700
701 os << "start pol " << ipol << LogIO::POST ;
702
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);
713 }
714#endif
715
716 os << "end pol " << ipol << LogIO::POST ;
717
718 }
719 os << "end table " << ifile << LogIO::POST ;
720 }
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 ;
727
728 delete chanMap ;
729
730 // set data
731 setData( common.gdataArrC, common.gwgtArr ) ;
732
733}
734
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 ;
813 flag_.resize( gshape ) ;
814 flag_ = (uChar)0;
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() ;
832 os << LogIO::DEBUGGING << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
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 ;
844 Broker broker = Broker(produceChunk, consumeChunkWithClipping);
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 }
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 ;
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() ;
897 os << LogIO::DEBUGGING << "clipMinMax: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
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
994void STGrid::initPol( Int ipol )
995{
996 LogIO os( LogOrigin("STGrid","initPol",WHERE) ) ;
997 if ( npolOrg_ == 1 ) {
998 os << "single polarization data." << LogIO::POST ;
999 ptab_ = tab_ ;
1000 }
1001 else
1002 ptab_ = tab_( tab_.col("POLNO") == pollist_[ipol] ) ;
1003
1004 attach( ptab_ ) ;
1005}
1006
1007void STGrid::initTable( uInt idx )
1008{
1009 tab_ = tableList_[idx] ;
1010 nrow_ = rows_[idx] ;
1011 updateChunkShape() ;
1012}
1013
1014void STGrid::setData( Array<Complex> &gdata,
1015 Array<Float> &gwgt )
1016{
1017 // 2011/12/20 TN
1018 // gwgt and data_ share storage
1019 LogIO os( LogOrigin("STGrid","setData",WHERE) ) ;
1020 double t0, t1 ;
1021 t0 = mathutil::gettimeofday_sec() ;
1022 uInt len = data_.nelements() ;
1023 const Complex *w1_p ;
1024 Float *w2_p ;
1025 Bool b1, b2, b3 ;
1026 const Complex *gdata_p = gdata.getStorage( b1 ) ;
1027 Float *gwgt_p = data_.getStorage( b2 ) ;
1028 uChar *gflg_p = flag_.getStorage( b3 ) ;
1029 for ( uInt i = 0 ; i < len ; i++ ) {
1030 if (gwgt_p[i] > 0.0) {
1031 gwgt_p[i] = (gdata_p[i]).real() / gwgt_p[i];
1032 gflg_p[i] = (uChar)0;
1033 }
1034 else {
1035 gflg_p[i] = (uChar)1;
1036 }
1037 }
1038 gdata.freeStorage( gdata_p, b1 ) ;
1039 data_.putStorage( gwgt_p, b2 ) ;
1040 flag_.putStorage( gflg_p, b3 ) ;
1041 t1 = mathutil::gettimeofday_sec() ;
1042 os << LogIO::DEBUGGING << "setData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
1043}
1044
1045void STGrid::setupGrid()
1046{
1047 Double xmin,xmax,ymin,ymax ;
1048 mapExtent( xmin, xmax, ymin, ymax ) ;
1049
1050 setupGrid( nxUI_, nyUI_, cellxUI_, cellyUI_,
1051 xmin, xmax, ymin, ymax, centerUI_ ) ;
1052}
1053
1054void STGrid::setupGrid( Int &nx,
1055 Int &ny,
1056 String &cellx,
1057 String &celly,
1058 Double &xmin,
1059 Double &xmax,
1060 Double &ymin,
1061 Double &ymax,
1062 String &center )
1063{
1064 LogIO os( LogOrigin("STGrid","setupGrid",WHERE) ) ;
1065 //cout << "nx=" << nx << ", ny=" << ny << endl ;
1066
1067 // center position
1068 if ( center.size() == 0 ) {
1069 center_(0) = 0.5 * ( xmin + xmax ) ;
1070 center_(1) = 0.5 * ( ymin + ymax ) ;
1071 }
1072 else {
1073 String::size_type pos0 = center.find( " " ) ;
1074 if ( pos0 == String::npos ) {
1075 throw AipsError( "bad string format in parameter center" ) ;
1076 }
1077 String::size_type pos1 = center.find( " ", pos0+1 ) ;
1078 String typestr, xstr, ystr ;
1079 if ( pos1 != String::npos ) {
1080 typestr = center.substr( 0, pos0 ) ;
1081 xstr = center.substr( pos0+1, pos1-pos0 ) ;
1082 ystr = center.substr( pos1+1 ) ;
1083 // todo: convert to J2000 (or direction ref for DIRECTION column)
1084 }
1085 else {
1086 typestr = "J2000" ;
1087 xstr = center.substr( 0, pos0 ) ;
1088 ystr = center.substr( pos0+1 ) ;
1089 }
1090 QuantumHolder qh ;
1091 String err ;
1092 qh.fromString( err, xstr ) ;
1093 Quantum<Double> xcen = qh.asQuantumDouble() ;
1094 qh.fromString( err, ystr ) ;
1095 Quantum<Double> ycen = qh.asQuantumDouble() ;
1096 center_(0) = xcen.getValue( "rad" ) ;
1097 center_(1) = ycen.getValue( "rad" ) ;
1098 double base = 0.5 * (xmin + xmax) ;
1099 int maxrotate = 1 ;
1100 int nelem = 2 * maxrotate + 1 ;
1101 double *sep = new double[nelem] ;
1102 for ( int i = 0 ; i < nelem ; i++ )
1103 sep[i] = abs(base - center_[0] - (i-maxrotate) * C::_2pi) ;
1104// os << "sep[0]=" << sep[0] << endl
1105// << "sep[1]=" << sep[1] << endl
1106// << "sep[2]=" << sep[2] << LogIO::POST ;
1107 int idx = 0 ;
1108 base = sep[0] ;
1109 int nrotate = 0 ;
1110 while ( idx < nelem ) {
1111 if ( base > sep[idx] ) {
1112 base = sep[idx] ;
1113 nrotate = idx ;
1114 }
1115 idx++ ;
1116 }
1117 delete sep ;
1118 nrotate -= maxrotate ;
1119// os << "nrotate = " << nrotate << LogIO::POST ;
1120 center_[0] += nrotate * C::_2pi ;
1121 }
1122// os << "xmin=" << xmin << LogIO::POST ;
1123// os << "center_=" << center_ << LogIO::POST ;
1124
1125 nx_ = nx ;
1126 ny_ = ny ;
1127 if ( nx < 0 && ny > 0 ) {
1128 nx_ = ny ;
1129 ny_ = ny ;
1130 }
1131 if ( ny < 0 && nx > 0 ) {
1132 nx_ = nx ;
1133 ny_ = nx ;
1134 }
1135
1136 //Double wx = xmax - xmin ;
1137 //Double wy = ymax - ymin ;
1138 Double wx = max( abs(xmax-center_(0)), abs(xmin-center_(0)) ) * 2 ;
1139 Double wy = max( abs(ymax-center_(1)), abs(ymin-center_(1)) ) * 2 ;
1140 // take 10% margin
1141 wx *= 1.10 ;
1142 wy *= 1.10 ;
1143
1144 Quantum<Double> qcellx ;
1145 Quantum<Double> qcelly ;
1146 //cout << "nx_ = " << nx_ << ", ny_ = " << ny_ << endl ;
1147 if ( cellx.size() != 0 && celly.size() != 0 ) {
1148 readQuantity( qcellx, cellx ) ;
1149 readQuantity( qcelly, celly ) ;
1150 }
1151 else if ( celly.size() != 0 ) {
1152 os << "Using celly to x-axis..." << LogIO::POST ;
1153 readQuantity( qcelly, celly ) ;
1154 qcellx = qcelly ;
1155 }
1156 else if ( cellx.size() != 0 ) {
1157 os << "Using cellx to y-axis..." << LogIO::POST ;
1158 readQuantity( qcellx, cellx ) ;
1159 qcelly = qcellx ;
1160 }
1161 else {
1162 if ( nx_ < 0 ) {
1163 os << "No user preference in grid setting. Using default..." << LogIO::POST ;
1164 readQuantity( qcellx, "1.0arcmin" ) ;
1165 qcelly = qcellx ;
1166 }
1167 else {
1168 if ( wx == 0.0 ) {
1169 os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ;
1170 wx = 0.00290888 ;
1171 }
1172 if ( wy == 0.0 ) {
1173 os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ;
1174 wy = 0.00290888 ;
1175 }
1176 qcellx = Quantum<Double>( wx/nx_*cos(center_[1]), "rad" ) ;
1177 qcelly = Quantum<Double>( wy/ny_, "rad" ) ;
1178 }
1179 }
1180 cellx_ = qcellx.getValue( "rad" ) ;
1181 celly_ = qcelly.getValue( "rad" ) ;
1182 //os << "cellx_=" << cellx_ << ", celly_=" << celly_ << ", cos("<<center_(1)<<")=" << cos(center_(1)) << LogIO::POST ;
1183 if ( nx_ < 0 ) {
1184 if ( wx == 0.0 ) {
1185 os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ;
1186 wx = 0.00290888 ;
1187 }
1188 if ( wy == 0.0 ) {
1189 os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ;
1190 wy = 0.00290888 ;
1191 }
1192 nx_ = Int( ceil( wx/(cellx_/cos(center_[1])) ) ) ;
1193 ny_ = Int( ceil( wy/celly_ ) ) ;
1194 }
1195
1196 // create DirectionCoordinate
1197 Matrix<Double> xform(2,2) ;
1198 xform = 0.0 ;
1199 xform.diagonal() = 1.0 ;
1200 dircoord_ = new DirectionCoordinate(MDirection::J2000,
1201 Projection( Projection::SIN ),
1202 center_[0], center_[1],
1203 -cellx_, celly_,
1204 xform,
1205 0.5*Double(nx_-1),
1206 0.5*Double(ny_-1)) ;
1207}
1208
1209void STGrid::mapExtent( Double &xmin, Double &xmax,
1210 Double &ymin, Double &ymax )
1211{
1212 //LogIO os( LogOrigin("STGrid","mapExtent",WHERE) ) ;
1213 directionCol_.attach( tableList_[0], "DIRECTION" ) ;
1214 Matrix<Double> direction = directionCol_.getColumn() ;
1215 //os << "dirCol.nrow() = " << dirCol.nrow() << LogIO::POST ;
1216 Vector<Double> ra( direction.row(0) ) ;
1217 mathutil::rotateRA( ra ) ;
1218 minMax( xmin, xmax, direction.row( 0 ) ) ;
1219 minMax( ymin, ymax, direction.row( 1 ) ) ;
1220 Double amin, amax, bmin, bmax ;
1221 for ( uInt i = 1 ; i < nfile_ ; i++ ) {
1222 directionCol_.attach( tableList_[i], "DIRECTION" ) ;
1223 direction.assign( directionCol_.getColumn() ) ;
1224 //os << "dirCol.nrow() = " << dirCol.nrow() << LogIO::POST ;
1225 // to make contiguous RA distribution (no 2pi jump)
1226 Vector<Double> ra( direction.row(0) ) ;
1227 mathutil::rotateRA( ra ) ;
1228 minMax( amin, amax, direction.row( 0 ) ) ;
1229 minMax( bmin, bmax, direction.row( 1 ) ) ;
1230 xmin = min( xmin, amin ) ;
1231 xmax = max( xmax, amax ) ;
1232 ymin = min( ymin, bmin ) ;
1233 ymax = max( ymax, bmax ) ;
1234 }
1235 //os << "(xmin,xmax)=(" << xmin << "," << xmax << ")" << LogIO::POST ;
1236 //os << "(ymin,ymax)=(" << ymin << "," << ymax << ")" << LogIO::POST ;
1237}
1238
1239void STGrid::table( Table &tab, uInt i )
1240{
1241 if ( i >= 0 && i < nfile_ )
1242 tab = Table( infileList_[i] ) ;
1243}
1244
1245void STGrid::selectData()
1246{
1247 LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
1248 Int ifno = ifno_ ;
1249 tableList_.resize( nfile_ ) ;
1250 if ( ifno_ == -1 ) {
1251 //Table taborg( infileList_[0] ) ;
1252 Table taborg ;
1253 table( taborg, 0 ) ;
1254 ROScalarColumn<uInt> ifnoCol( taborg, "IFNO" ) ;
1255 ifno_ = ifnoCol( 0 ) ;
1256 os << LogIO::WARN
1257 << "IFNO is not given. Using default IFNO: " << ifno_ << LogIO::POST ;
1258 }
1259 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
1260 //Table taborg( infileList_[i] ) ;
1261 Table taborg ;
1262 table( taborg, i ) ;
1263 TableExprNode node ;
1264 if ( ifno != -1 || isMultiIF( taborg ) ) {
1265 os << "apply selection on IFNO" << LogIO::POST ;
1266 node = taborg.col("IFNO") == ifno_ ;
1267 }
1268 if ( scanlist_.size() > 0 ) {
1269 os << "apply selection on SCANNO" << LogIO::POST ;
1270 node = node && taborg.col("SCANNO").in( scanlist_ ) ;
1271 }
1272 if ( node.isNull() ) {
1273 tableList_[i] = taborg ;
1274 }
1275 else {
1276 tableList_[i] = taborg( node ) ;
1277 }
1278 os << LogIO::DEBUGGING << "tableList_[" << i << "].nrow()=" << tableList_[i].nrow() << LogIO::POST ;
1279 if ( tableList_[i].nrow() == 0 ) {
1280 os << LogIO::SEVERE
1281 << "No corresponding rows for given selection: IFNO " << ifno_ ;
1282 if ( scanlist_.size() > 0 )
1283 os << " SCANNO " << scanlist_ ;
1284 os << LogIO::EXCEPTION ;
1285 }
1286 }
1287}
1288
1289Bool STGrid::isMultiIF( Table &tab )
1290{
1291 ROScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
1292 Vector<uInt> ifnos = ifnoCol.getColumn() ;
1293 return anyNE( ifnos, ifnos[0] ) ;
1294}
1295
1296void STGrid::attach( Table &tab )
1297{
1298 // attach to table
1299 spectraCol_.attach( tab, "SPECTRA" ) ;
1300 flagtraCol_.attach( tab, "FLAGTRA" ) ;
1301 directionCol_.attach( tab, "DIRECTION" ) ;
1302 flagRowCol_.attach( tab, "FLAGROW" ) ;
1303 tsysCol_.attach( tab, "TSYS" ) ;
1304 intervalCol_.attach( tab, "INTERVAL" ) ;
1305}
1306
1307Int STGrid::getDataChunk(
1308 IPosition const &wshape,
1309 IPosition const &vshape,
1310 IPosition const &dshape,
1311 Array<Complex> &spectra,
1312 Array<Double> &direction,
1313 Array<Int> &flagtra,
1314 Array<Int> &rflag,
1315 Array<Float> &weight )
1316{
1317 LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
1318
1319 Array<Float> spectraF_(wshape);
1320 Array<uChar> flagtraUC_(wshape);
1321 Array<uInt> rflagUI_(vshape);
1322 Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
1323 if ( nrow < nchunk_ ) {
1324 spectra.resize( spectraF_.shape() ) ;
1325 flagtra.resize( flagtraUC_.shape() ) ;
1326 rflag.resize( rflagUI_.shape() ) ;
1327 }
1328 double t0, t1 ;
1329 t0 = mathutil::gettimeofday_sec() ;
1330 convertArray( spectra, spectraF_ ) ;
1331 toInt( flagtraUC_, flagtra ) ;
1332 toInt( rflagUI_, rflag ) ;
1333 t1 = mathutil::gettimeofday_sec() ;
1334 eToInt = t1 - t0 ;
1335
1336 return nrow ;
1337}
1338
1339#if 0
1340Int STGrid::getDataChunk( Array<Complex> &spectra,
1341 Array<Double> &direction,
1342 Array<Int> &flagtra,
1343 Array<Int> &rflag,
1344 Array<Float> &weight )
1345{
1346 LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
1347 Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
1348 if ( nrow < nchunk_ ) {
1349 spectra.resize( spectraF_.shape() ) ;
1350 flagtra.resize( flagtraUC_.shape() ) ;
1351 rflag.resize( rflagUI_.shape() ) ;
1352 }
1353 double t0, t1 ;
1354 t0 = mathutil::gettimeofday_sec() ;
1355 convertArray( spectra, spectraF_ ) ;
1356 toInt( flagtraUC_, flagtra ) ;
1357 toInt( rflagUI_, rflag ) ;
1358 t1 = mathutil::gettimeofday_sec() ;
1359 eToInt = t1 - t0 ;
1360
1361 return nrow ;
1362}
1363#endif
1364
1365Int STGrid::getDataChunk( Array<Float> &spectra,
1366 Array<Double> &direction,
1367 Array<uChar> &flagtra,
1368 Array<uInt> &rflag,
1369 Array<Float> &weight )
1370{
1371 LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
1372 Int nrow = spectra.shape()[1] ;
1373 Int remainingRow = nrow_ - nprocessed_ ;
1374 if ( remainingRow < nrow ) {
1375 nrow = remainingRow ;
1376 IPosition mshape( 2, nchan_, nrow ) ;
1377 IPosition vshape( 1, nrow ) ;
1378 spectra.resize( mshape ) ;
1379 flagtra.resize( mshape ) ;
1380 direction.resize( IPosition(2,2,nrow) ) ;
1381 rflag.resize( vshape ) ;
1382 weight.resize( mshape ) ;
1383 }
1384 // 2011/12/22 TN
1385 // tsys shares its storage with weight
1386 Array<Float> tsys( weight ) ;
1387 Array<Double> tint( rflag.shape() ) ;
1388
1389 Vector<uInt> rflagVec( rflag ) ;
1390 Vector<Double> tintVec( tint ) ;
1391
1392 RefRows rows( nprocessed_, nprocessed_+nrow-1, 1 ) ;
1393 //os<<LogIO::DEBUGGING<<"nprocessed_="<<nprocessed_<<": rows.nrows()="<<rows.nrows()<<LogIO::POST ;
1394 spectraCol_.getColumnCells( rows, spectra ) ;
1395 flagtraCol_.getColumnCells( rows, flagtra ) ;
1396 directionCol_.getColumnCells( rows, direction ) ;
1397 // to make contiguous RA distribution (no 2pi jump)
1398 Vector<Double> v( Matrix<Double>(direction).row(0) ) ;
1399 mathutil::rotateRA( v ) ;
1400 flagRowCol_.getColumnCells( rows, rflagVec ) ;
1401 intervalCol_.getColumnCells( rows, tintVec ) ;
1402 Vector<Float> tsysTemp = tsysCol_( nprocessed_ ) ;
1403 if ( tsysTemp.nelements() == (uInt)nchan_ )
1404 tsysCol_.getColumnCells( rows, tsys ) ;
1405 else
1406 tsys = tsysTemp[0] ;
1407
1408 double t0,t1 ;
1409 t0 = mathutil::gettimeofday_sec() ;
1410 getWeight( weight, tsys, tint ) ;
1411 t1 = mathutil::gettimeofday_sec() ;
1412 eGetWeight += t1-t0 ;
1413
1414 nprocessed_ += nrow ;
1415
1416 return nrow ;
1417}
1418
1419void STGrid::setupArray()
1420{
1421 LogIO os( LogOrigin("STGrid","setupArray",WHERE) ) ;
1422 ROScalarColumn<uInt> polnoCol( tableList_[0], "POLNO" ) ;
1423 Vector<uInt> pols = polnoCol.getColumn() ;
1424 //os << pols << LogIO::POST ;
1425 Vector<uInt> pollistOrg ;
1426 npolOrg_ = 0 ;
1427 uInt polno ;
1428 for ( uInt i = 0 ; i < polnoCol.nrow() ; i++ ) {
1429 //polno = polnoCol( i ) ;
1430 polno = pols( i ) ;
1431 if ( allNE( pollistOrg, polno ) ) {
1432 pollistOrg.resize( npolOrg_+1, True ) ;
1433 pollistOrg[npolOrg_] = polno ;
1434 npolOrg_++ ;
1435 }
1436 }
1437 if ( pollist_.size() == 0 )
1438 pollist_ = pollistOrg ;
1439 else {
1440 Vector<uInt> newlist ;
1441 uInt newsize = 0 ;
1442 for ( uInt i = 0 ; i < pollist_.size() ; i++ ) {
1443 if ( anyEQ( pollistOrg, pollist_[i] ) ) {
1444 newlist.resize( newsize+1, True ) ;
1445 newlist[newsize] = pollist_[i] ;
1446 newsize++ ;
1447 }
1448 }
1449 pollist_.assign( newlist ) ;
1450 }
1451 npol_ = pollist_.size() ;
1452 if ( npol_ == 0 ) {
1453 os << LogIO::SEVERE << "Empty pollist" << LogIO::EXCEPTION ;
1454 }
1455 rows_.resize( nfile_ ) ;
1456 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
1457 rows_[i] = tableList_[i].nrow() / npolOrg_ ;
1458 //if ( nrow_ < rows_[i] )
1459 // nrow_ = rows_[i] ;
1460 }
1461 flagtraCol_.attach( tableList_[0], "FLAGTRA" ) ;
1462 nchan_ = flagtraCol_( 0 ).nelements() ;
1463// os << "npol_ = " << npol_ << "(" << pollist_ << ")" << endl
1464// << "nchan_ = " << nchan_ << endl
1465// << "nrow_ = " << nrow_ << LogIO::POST ;
1466}
1467
1468void STGrid::getWeight( Array<Float> &w,
1469 Array<Float> &tsys,
1470 Array<Double> &tint )
1471{
1472 LogIO os( LogOrigin("STGrid","getWeight",WHERE) ) ;
1473
1474 // 2011/12/22 TN
1475 // w (weight) and tsys share storage
1476 IPosition refShape = tsys.shape() ;
1477 Int nchan = refShape[0] ;
1478 Int nrow = refShape[1] ;
1479// os << "nchan=" << nchan << ", nrow=" << nrow << LogIO::POST ;
1480// os << "w.shape()=" << w.shape() << endl
1481// << "tsys.shape()=" << tsys.shape() << endl
1482// << "tint.shape()=" << tint.shape() << LogIO::POST ;
1483
1484 // set weight
1485 if ( wtype_.compare( "UNIFORM" ) == 0 ) {
1486 w = 1.0 ;
1487 }
1488 else if ( wtype_.compare( "TINT" ) == 0 ) {
1489 Bool b0, b1 ;
1490 Float *w_p = w.getStorage( b0 ) ;
1491 Float *w0_p = w_p ;
1492 const Double *ti_p = tint.getStorage( b1 ) ;
1493 const Double *w1_p = ti_p ;
1494 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1495 for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1496 *w0_p = *w1_p ;
1497 w0_p++ ;
1498 }
1499 w1_p++ ;
1500 }
1501 w.putStorage( w_p, b0 ) ;
1502 tint.freeStorage( ti_p, b1 ) ;
1503 }
1504 else if ( wtype_.compare( "TSYS" ) == 0 ) {
1505 Bool b0 ;
1506 Float *w_p = w.getStorage( b0 ) ;
1507 Float *w0_p = w_p ;
1508 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1509 for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1510 Float temp = *w0_p ;
1511 *w0_p = 1.0 / ( temp * temp ) ;
1512 w0_p++ ;
1513 }
1514 }
1515 w.putStorage( w_p, b0 ) ;
1516 }
1517 else if ( wtype_.compare( "TINTSYS" ) == 0 ) {
1518 Bool b0, b1 ;
1519 Float *w_p = w.getStorage( b0 ) ;
1520 Float *w0_p = w_p ;
1521 const Double *ti_p = tint.getStorage( b1 ) ;
1522 const Double *w1_p = ti_p ;
1523 for ( Int irow = 0 ; irow < nrow ; irow++ ) {
1524 Float interval = *w1_p ;
1525 for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) {
1526 Float temp = *w0_p ;
1527 *w0_p = interval / ( temp * temp ) ;
1528 w0_p++ ;
1529 }
1530 w1_p++ ;
1531 }
1532 w.putStorage( w_p, b0 ) ;
1533 tint.freeStorage( ti_p, b1 ) ;
1534 }
1535 else {
1536 //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
1537 //os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
1538 w = 1.0 ;
1539 }
1540}
1541
1542void STGrid::toInt( Array<uChar> &u, Array<Int> &v )
1543{
1544 uInt len = u.nelements() ;
1545 Int *int_p = new Int[len] ;
1546 Bool deleteIt ;
1547 const uChar *data_p = u.getStorage( deleteIt ) ;
1548 Int *i_p = int_p ;
1549 const uChar *u_p = data_p ;
1550 for ( uInt i = 0 ; i < len ; i++ ) {
1551 *i_p = ( *u_p == 0 ) ? 0 : 1 ;
1552 i_p++ ;
1553 u_p++ ;
1554 }
1555 u.freeStorage( data_p, deleteIt ) ;
1556 v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
1557}
1558
1559void STGrid::toInt( Array<uInt> &u, Array<Int> &v )
1560{
1561 uInt len = u.nelements() ;
1562 Int *int_p = new Int[len] ;
1563 Bool deleteIt ;
1564 const uInt *data_p = u.getStorage( deleteIt ) ;
1565 Int *i_p = int_p ;
1566 const uInt *u_p = data_p ;
1567 for ( uInt i = 0 ; i < len ; i++ ) {
1568 *i_p = ( *u_p == 0 ) ? 0 : 1 ;
1569 i_p++ ;
1570 u_p++ ;
1571 }
1572 u.freeStorage( data_p, deleteIt ) ;
1573 v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
1574}
1575
1576void STGrid::toPixel( Array<Double> &world, Array<Double> &pixel )
1577{
1578 uInt nrow = world.shape()[1] ;
1579 Bool bw, bp ;
1580 Double *w_p = world.getStorage( bw ) ;
1581 Double *p_p = pixel.getStorage( bp ) ;
1582 Double *ww_p = w_p ;
1583 Double *wp_p = p_p ;
1584 IPosition vshape( 1, 2 ) ;
1585 Vector<Double> _world, _pixel ;
1586 for ( uInt i = 0 ; i < nrow ; i++ ) {
1587 _world.takeStorage( vshape, ww_p, SHARE ) ;
1588 _pixel.takeStorage( vshape, wp_p, SHARE ) ;
1589 dircoord_->toPixel( _pixel, _world ) ;
1590 ww_p += 2 ;
1591 wp_p += 2 ;
1592 }
1593 world.putStorage( w_p, bw ) ;
1594 pixel.putStorage( p_p, bp ) ;
1595}
1596
1597void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize )
1598{
1599 convFunc = 0.0 ;
1600 for ( Int i = 0 ; i < convSize/2 ; i++ )
1601 convFunc(i) = 1.0 ;
1602}
1603
1604#define NEED_UNDERSCORES
1605#if defined(NEED_UNDERSCORES)
1606#define grdsf grdsf_
1607#define grdgauss grdgauss_
1608#define grdjinc1 grdjinc1_
1609#endif
1610#if defined(USE_CASAPY)
1611extern "C" {
1612 void grdsf(Double*, Double*);
1613 void grdgauss(Double*, Double*, Double*);
1614 void grdjinc1(Double*, Double*, Int*, Double*);
1615}
1616#else
1617extern "C" {
1618 void grdsf(Double*, Double*);
1619}
1620void grdgauss(Double *hwhm, Double *val, Double *out)
1621{
1622 *out = exp(-log(2.0) * (*val / *hwhm) * (*val / *hwhm));
1623}
1624void grdjinc1(Double *c, Double *val, Int *normalize, Double *out)
1625{
1626 // Calculate J_1(x) using approximate formula
1627 Double x = C::pi * *val / *c;
1628 Double ax = fabs(x);
1629 Double ans;
1630 if ( ax < 8.0 ) {
1631 Double y = x * x;
1632 Double ans1 = x * (72362614232.0 + y * (-7895059235.0
1633 + y * (242396853.1 + y * (-2972611.439
1634 + y * (15704.48260 + y * (-30.16036606))))));
1635 Double ans2 = 144725228442.0 + y * (2300535178.0
1636 + y * (18583304.74 + y * (99447.43394
1637 + y * (376.9991397 + y * 1.0))));
1638 ans = ans1 / ans2;
1639 }
1640 else {
1641 Double z = 8.0 / ax;
1642 Double y = z * z;
1643 Double xx = ax - 2.356194491;
1644 Double ans1 = 1.0 + y * (0.183105e-2 + y * (-0.3516396496e-4
1645 + y * (0.2457520174e-5 + y * (-0.240337019e-6))));
1646 Double ans2 = 0.04687499995 + y * (-0.2002690873e-3
1647 + y * (0.8449199096e-5 + y * (-0.88228987e-6
1648 + y * (0.105787412e-6))));
1649 ans = sqrt(0.636619772 / ax) * (cos(xx) * ans1
1650 - z * sin(xx) * ans2);
1651 if (x < 0.0)
1652 ans = -ans;
1653 }
1654
1655 // Then, calculate Jinc
1656 if (x == 0.0) {
1657 *out = 0.5;
1658 }
1659 else {
1660 *out = ans / x;
1661 }
1662
1663 if (*normalize == 1)
1664 *out = *out / 0.5;
1665}
1666#endif
1667void STGrid::spheroidalFunc( Vector<Float> &convFunc )
1668{
1669 convFunc = 0.0 ;
1670 for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
1671 Double nu = Double(i) / Double(convSupport_*convSampling_) ;
1672 Double val ;
1673 grdsf( &nu, &val ) ;
1674 convFunc(i) = ( 1.0 - nu * nu ) * val ;
1675 }
1676}
1677
1678void STGrid::gaussFunc( Vector<Float> &convFunc, Double hwhm, Double truncate )
1679{
1680 convFunc = 0.0 ;
1681 Int len = (Int)(truncate*Double(convSampling_)+0.5);
1682 Double out, val;
1683 for ( Int i = 0 ; i < len ; i++ ) {
1684 val = Double(i) / Double(convSampling_) ;
1685 grdgauss(&hwhm, &val, &out);
1686 convFunc(i) = out;
1687 }
1688}
1689
1690void STGrid::gjincFunc( Vector<Float> &convFunc, Double hwhm, Double c, Double truncate )
1691{
1692 convFunc = 0.0;
1693 Double out1, out2, val;
1694 Int normalize = 1;
1695 if (truncate >= 0.0) {
1696 Int len = (Int)(truncate*Double(convSampling_)+0.5);
1697 for (Int i = 0 ; i < len ; i++) {
1698 val = Double(i) / Double(convSampling_);
1699 grdgauss(&hwhm, &val, &out1);
1700 grdjinc1(&c, &val, &normalize, &out2);
1701 convFunc(i) = out1 * out2;
1702 }
1703 }
1704 else {
1705 Int len = convFunc.nelements();
1706 for (Int i = 0 ; i < len ; i++) {
1707 val = Double(i) / Double(convSampling_);
1708 grdjinc1(&c, &val, &normalize, &out2);
1709 if (out2 <= 0.0) {
1710 LogIO os(LogOrigin("STGrid","gjincFunc",WHERE));
1711 os << LogIO::DEBUG1 << "convFunc is automatically truncated at radius " << val << LogIO::POST;
1712 break;
1713 }
1714 grdgauss(&hwhm, &val, &out1);
1715 convFunc(i) = out1 * out2;
1716 }
1717 }
1718}
1719
1720void STGrid::pbFunc( Vector<Float> &convFunc )
1721{
1722 convFunc = 0.0 ;
1723}
1724
1725vector<float> STGrid::getConvFunc()
1726{
1727 LogIO os(LogOrigin("STGrid","getConvFunc",WHERE));
1728 Vector<Float> convFunc;
1729 vector<float> out;
1730
1731 if (cellx_ <= 0.0 || celly_ <= 0.0) {
1732 selectData();
1733 setupGrid();
1734 }
1735
1736 if (convType_ == "BOX" || convType_ == "SF") {
1737 setConvFunc(convFunc);
1738 }
1739 else if (convType_ == "GAUSS") {
1740 Quantum<Double> q1,q2;
1741 readQuantity(q1,gwidth_);
1742 readQuantity(q2,truncate_);
1743// if (celly_ <= 0.0
1744// && ((!q1.getUnit().empty()&&q1.getUnit()!="pixel") ||
1745// (!q2.getUnit().empty()&&q2.getUnit()!="pixel"))) {
1746// throw AipsError("You have to call defineImage to get correct convFunc");
1747// }
1748 setConvFunc(convFunc);
1749 }
1750 else if (convType_ == "GJINC") {
1751 Quantum<Double> q1,q2,q3;
1752 readQuantity(q1,gwidth_);
1753 readQuantity(q2,truncate_);
1754 readQuantity(q3,jwidth_);
1755// if (celly_ <= 0.0
1756// && ((!q1.getUnit().empty()&&q1.getUnit()!="pixel") ||
1757// (!q2.getUnit().empty()&&q2.getUnit()!="pixel") ||
1758// (!q3.getUnit().empty()&&q3.getUnit()!="pixel"))) {
1759// throw AipsError("You have to call defineImage to get correct convFunc");
1760// }
1761 setConvFunc(convFunc);
1762 }
1763 else if (convType_ == "PB") {
1764 throw AipsError("Grid function PB is not available");
1765 }
1766 else {
1767 throw AipsError("Unknown grid function: "+convType_);
1768 }
1769
1770 convFunc.tovector(out);
1771 return out;
1772}
1773
1774void STGrid::setConvFunc( Vector<Float> &convFunc )
1775{
1776 LogIO os(LogOrigin("STGrid","setConvFunc",WHERE));
1777 convSupport_ = userSupport_ ;
1778 if ( convType_ == "BOX" ) {
1779 if ( convSupport_ < 0 )
1780 convSupport_ = 0 ;
1781 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
1782 convFunc.resize( convSize ) ;
1783 boxFunc( convFunc, convSize ) ;
1784 os << LogIO::DEBUGGING
1785 << "convType_ = " << convType_ << endl
1786 << "convSupport_ = " << convSupport_ << LogIO::POST;
1787 }
1788 else if ( convType_ == "SF" ) {
1789 if ( convSupport_ < 0 )
1790 convSupport_ = 3 ;
1791 Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
1792 convFunc.resize( convSize ) ;
1793 spheroidalFunc( convFunc ) ;
1794 os << LogIO::DEBUGGING
1795 << "convType_ = " << convType_ << endl
1796 << "convSupport_ = " << convSupport_ << LogIO::POST;
1797 }
1798 else if ( convType_ == "GAUSS" ) {
1799 // determine pixel gwidth
1800 // default is HWHM corresponding to b = 1.0 (Mangum et al. 2007)
1801 Double pixelGW;
1802 Quantum<Double> q ;
1803 if (!gwidth_.empty()) {
1804 readQuantity( q, gwidth_ );
1805 if ( q.getUnit().empty() || q.getUnit()=="pixel" ) {
1806 pixelGW = q.getValue();
1807 }
1808 else {
1809 pixelGW = q.getValue("rad")/celly_;
1810 }
1811 }
1812 pixelGW = (pixelGW >= 0.0) ? pixelGW : sqrt(log(2.0));
1813 if (pixelGW < 0.0) {
1814 os << LogIO::SEVERE
1815 << "Negative width is specified for gaussian" << LogIO::EXCEPTION;
1816 }
1817 // determine truncation radius
1818 // default is 3 * HWHM
1819 Double truncate;
1820 if (!truncate_.empty()) {
1821 readQuantity( q, truncate_ );
1822 if ( q.getUnit().empty() || q.getUnit()=="pixel" ) {
1823 truncate = q.getValue();
1824 }
1825 else {
1826 truncate = q.getValue("rad")/celly_;
1827 }
1828 }
1829 //convSupport_ = (Int)(truncate+0.5);
1830 truncate = (truncate >= 0.0) ? truncate : 3.0 * pixelGW;
1831 convSupport_ = Int(truncate);
1832 convSupport_ += (((truncate-(Double)convSupport_) > 0.0) ? 1 : 0);
1833 Int convSize = convSampling_ * ( 2*convSupport_ + 2 ) ;
1834 convFunc.resize( convSize ) ;
1835 gaussFunc( convFunc, pixelGW, truncate ) ;
1836 os << LogIO::DEBUGGING
1837 << "convType_ = " << convType_ << endl
1838 << "convSupport_ = " << convSupport_ << endl
1839 << "truncate_ = " << truncate << "pixel" << endl
1840 << "gwidth_ = " << pixelGW << "pixel" << LogIO::POST;
1841 }
1842 else if ( convType_ == "GJINC" ) {
1843 // determine pixel gwidth
1844 // default is HWHM corresponding to b = 2.52 (Mangum et al. 2007)
1845 Double pixelGW;
1846 Quantum<Double> q ;
1847 if (!gwidth_.empty()) {
1848 readQuantity( q, gwidth_ );
1849 if ( q.getUnit().empty() || q.getUnit()=="pixel" ) {
1850 pixelGW = q.getValue();
1851 }
1852 else {
1853 pixelGW = q.getValue("rad")/celly_;
1854 }
1855 }
1856 pixelGW = (pixelGW >= 0.0) ? pixelGW : sqrt(log(2.0)) * 2.52;
1857 if (pixelGW < 0.0) {
1858 os << LogIO::SEVERE
1859 << "Negative width is specified for gaussian" << LogIO::EXCEPTION;
1860 }
1861 // determine pixel c
1862 // default is c = 1.55 (Mangum et al. 2007)
1863 Double pixelJW;
1864 if (!jwidth_.empty()) {
1865 readQuantity( q, jwidth_ );
1866 if ( q.getUnit().empty() || q.getUnit()=="pixel" ) {
1867 pixelJW = q.getValue();
1868 }
1869 else {
1870 pixelJW = q.getValue("rad")/celly_;
1871 }
1872 }
1873 pixelJW = (pixelJW >= 0.0) ? pixelJW : 1.55;
1874 if (pixelJW < 0.0) {
1875 os << LogIO::SEVERE
1876 << "Negative width is specified for jinc" << LogIO::EXCEPTION;
1877 }
1878 // determine truncation radius
1879 // default is -1.0 (truncate at first null)
1880 Double truncate = -1.0;
1881 if (!truncate_.empty()) {
1882 readQuantity( q, truncate_ );
1883 if ( q.getUnit().empty() || q.getUnit()=="pixel" ) {
1884 truncate = q.getValue();
1885 }
1886 else {
1887 truncate = q.getValue("rad")/celly_;
1888 }
1889 }
1890 //convSupport_ = (truncate >= 0.0) ? (Int)(truncate+0.5) : (Int)(2*pixelJW+0.5);
1891 Double convSupportF = (truncate >= 0.0) ? truncate : (2*pixelJW);
1892 convSupport_ = (Int)convSupportF;
1893 convSupport_ += (((convSupportF-(Double)convSupport_) > 0.0) ? 1 : 0);
1894 Int convSize = convSampling_ * ( 2*convSupport_ + 2 ) ;
1895 convFunc.resize( convSize ) ;
1896 gjincFunc( convFunc, pixelGW, pixelJW, truncate ) ;
1897 os << LogIO::DEBUGGING
1898 << "convType_ = " << convType_ << endl
1899 << "convSupport_ = " << convSupport_ << endl
1900 << "truncate_ = " << truncate << "pixel" << endl
1901 << "gwidth_ = " << pixelGW << "pixel" << endl
1902 << "jwidth_ = " << pixelJW << "pixel" << LogIO::POST;
1903 }
1904 else if ( convType_ == "PB" ) {
1905 if ( convSupport_ < 0 )
1906 convSupport_ = 0 ;
1907 pbFunc( convFunc ) ;
1908 }
1909 else {
1910 throw AipsError( "Unsupported convolution function" ) ;
1911 }
1912}
1913
1914string STGrid::saveData( string outfile )
1915{
1916 LogIO os( LogOrigin("STGrid", "saveData", WHERE) ) ;
1917 double t0, t1 ;
1918 t0 = mathutil::gettimeofday_sec() ;
1919
1920 //Int polno = 0 ;
1921 String outfile_ ;
1922 if ( outfile.size() == 0 ) {
1923 if ( infileList_[0].lastchar() == '/' ) {
1924 outfile_ = infileList_[0].substr( 0, infileList_[0].size()-1 ) ;
1925 }
1926 else {
1927 outfile_ = infileList_[0] ;
1928 }
1929 outfile_ += ".grid" ;
1930 }
1931 else {
1932 outfile_ = outfile ;
1933 }
1934 Table tab ;
1935 prepareTable( tab, outfile_ ) ;
1936 fillTable( tab ) ;
1937
1938 t1 = mathutil::gettimeofday_sec() ;
1939 os << LogIO::DEBUGGING << "saveData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
1940
1941 return outfile_ ;
1942}
1943
1944void STGrid::prepareTable( Table &tab, String &name )
1945{
1946 Table t( infileList_[0], Table::Old ) ;
1947 t.deepCopy( name, Table::New, False, t.endianFormat(), True ) ;
1948 tab = Table( name, Table::Update ) ;
1949 // 2012/02/13 TN
1950 // explicitly copy subtables since no rows including subtables are
1951 // copied by Table::deepCopy with noRows=True
1952 //TableCopy::copySubTables( tab, t ) ;
1953 const TableRecord &inrec = t.keywordSet();
1954 TableRecord &outrec = tab.rwKeywordSet();
1955 for (uInt i = 0 ; i < inrec.nfields() ; i++) {
1956 if (inrec.type(i) == TpTable) {
1957 String name = inrec.name(i);
1958 Table intable = inrec.asTable(name);
1959 Table outtable = outrec.asTable(name);
1960 TableCopy::copyRows(outtable, intable);
1961 }
1962 }
1963}
1964
1965void STGrid::fillTable( Table &tab )
1966{
1967 IPosition dshape = data_.shape() ;
1968 Int nrow = nx_ * ny_ * npol_ ;
1969 tab.rwKeywordSet().define( "nPol", npol_ ) ;
1970 tab.addRow( nrow ) ;
1971 Vector<Double> cpix( 2 ) ;
1972 cpix(0) = Double( nx_ - 1 ) * 0.5 ;
1973 cpix(1) = Double( ny_ - 1 ) * 0.5 ;
1974 Vector<Double> dir( 2 ) ;
1975 Vector<Double> pix( 2 );
1976 ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
1977 ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
1978 ArrayColumn<uChar> flagtraCol( tab, "FLAGTRA" ) ;
1979 ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
1980 ScalarColumn<uInt> scannoCol( tab, "SCANNO" ) ;
1981 Int irow = 0 ;
1982 Vector<Float> sp( nchan_ ) ;
1983 Vector<uChar> flag( nchan_ ) ;
1984 Bool bsp, bdata, bflag ;
1985 const Float *data_p = data_.getStorage( bdata ) ;
1986 const uChar *flag_p = flag_.getStorage( bflag ) ;
1987 Float *wsp_p, *sp_p ;
1988 const Float *wdata_p = data_p ;
1989 const uChar *wflag_p = flag_p ;
1990 long step = nx_ * ny_ * npol_ ;
1991 long offset ;
1992 uInt scanno = 0 ;
1993 for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
1994 pix(1) = (Double)(iy);
1995 for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
1996 pix(0) = (Double)(nx_-1-ix);
1997 dircoord_->toWorld(dir,pix);
1998 //os << "dir[" << ix << "," << iy << "]=" << dir << LogIO::POST;
1999 for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
2000 offset = ix + nx_ * (iy + ipol * ny_) ;
2001 //os << "offset = " << offset << LogIO::POST ;
2002 sp_p = sp.getStorage( bsp ) ;
2003 wsp_p = sp_p ;
2004 wdata_p = data_p + offset ;
2005 wflag_p = flag_p + offset ;
2006 for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
2007 *wsp_p = *wdata_p ;
2008 wsp_p++ ;
2009 wdata_p += step ;
2010 flag[ichan] = *wflag_p;
2011 wflag_p += step ;
2012 }
2013 sp.putStorage( sp_p, bsp ) ;
2014 spectraCol.put( irow, sp ) ;
2015 flagtraCol.put( irow, flag ) ;
2016 directionCol.put( irow, dir ) ;
2017 polnoCol.put( irow, pollist_[ipol] ) ;
2018 scannoCol.put( irow, scanno ) ;
2019 irow++ ;
2020 }
2021 scanno++ ;
2022 }
2023 }
2024 data_.freeStorage( data_p, bdata ) ;
2025 flag_.freeStorage( flag_p, bflag ) ;
2026
2027 fillMainColumns( tab ) ;
2028}
2029
2030void STGrid::fillMainColumns( Table &tab )
2031{
2032 // values for fill
2033 //Table t( infileList_[0], Table::Old ) ;
2034 Table t ;
2035 table( t, 0 ) ;
2036 Table tsel = t( t.col( "IFNO" ) == (uInt)ifno_, 1 ) ;
2037 ROTableRow row( tsel ) ;
2038 row.get( 0 ) ;
2039 const TableRecord &rec = row.record() ;
2040 uInt freqId = rec.asuInt( "FREQ_ID" ) ;
2041 uInt molId = rec.asuInt( "MOLECULE_ID" ) ;
2042 uInt tcalId = rec.asuInt( "TCAL_ID" ) ;
2043 uInt focusId = rec.asuInt( "FOCUS_ID" ) ;
2044 uInt weatherId = rec.asuInt( "WEATHER_ID" ) ;
2045 String srcname = rec.asString( "SRCNAME" ) ;
2046 String fieldname = rec.asString( "FIELDNAME" ) ;
2047 Vector<Float> defaultTsys( 1, 1.0 ) ;
2048 // @todo how to set flagtra for gridded spectra?
2049 Vector<uChar> flagtra = rec.asArrayuChar( "FLAGTRA" ) ;
2050 flagtra = (uChar)0 ;
2051 Float opacity = rec.asFloat( "OPACITY" ) ;
2052 Double srcvel = rec.asDouble( "SRCVELOCITY" ) ;
2053 Vector<Double> srcpm = rec.asArrayDouble( "SRCPROPERMOTION" ) ;
2054 Vector<Double> srcdir = rec.asArrayDouble( "SRCDIRECTION" ) ;
2055 Vector<Double> scanrate = rec.asArrayDouble( "SCANRATE" ) ;
2056 Double time = rec.asDouble( "TIME" ) ;
2057 Double interval = rec.asDouble( "INTERVAL" ) ;
2058
2059 // fill columns
2060 Int nrow = tab.nrow() ;
2061 ScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
2062 ScalarColumn<uInt> beamnoCol(tab, "BEAMNO");
2063 ScalarColumn<uInt> freqIdCol( tab, "FREQ_ID" ) ;
2064 ScalarColumn<uInt> molIdCol( tab, "MOLECULE_ID" ) ;
2065 ScalarColumn<uInt> tcalidCol( tab, "TCAL_ID" ) ;
2066 ScalarColumn<Int> fitidCol( tab, "FIT_ID" ) ;
2067 ScalarColumn<uInt> focusidCol( tab, "FOCUS_ID" ) ;
2068 ScalarColumn<uInt> weatheridCol( tab, "WEATHER_ID" ) ;
2069 ArrayColumn<uChar> flagtraCol( tab, "FLAGTRA" ) ;
2070 ScalarColumn<uInt> rflagCol( tab, "FLAGROW" ) ;
2071 ArrayColumn<Float> tsysCol( tab, "TSYS" ) ;
2072 ScalarColumn<String> srcnameCol( tab, "SRCNAME" ) ;
2073 ScalarColumn<String> fieldnameCol( tab, "FIELDNAME" ) ;
2074 ScalarColumn<Int> srctypeCol( tab, "SRCTYPE" ) ;
2075 ScalarColumn<Float> opacityCol( tab, "OPACITY" ) ;
2076 ScalarColumn<Double> srcvelCol( tab, "SRCVELOCITY" ) ;
2077 ArrayColumn<Double> srcpmCol( tab, "SRCPROPERMOTION" ) ;
2078 ArrayColumn<Double> srcdirCol( tab, "SRCDIRECTION" ) ;
2079 ArrayColumn<Double> scanrateCol( tab, "SCANRATE" ) ;
2080 ScalarColumn<Double> timeCol( tab, "TIME" ) ;
2081 ScalarColumn<Double> intervalCol( tab, "INTERVAL" ) ;
2082 for ( Int i = 0 ; i < nrow ; i++ ) {
2083 ifnoCol.put( i, (uInt)ifno_ ) ;
2084 beamnoCol.put(i, 0);
2085 freqIdCol.put( i, freqId ) ;
2086 molIdCol.put( i, molId ) ;
2087 tcalidCol.put( i, tcalId ) ;
2088 fitidCol.put( i, -1 ) ;
2089 focusidCol.put( i, focusId ) ;
2090 weatheridCol.put( i, weatherId ) ;
2091 //flagtraCol.put( i, flagtra ) ;
2092 rflagCol.put( i, 0 ) ;
2093 tsysCol.put( i, defaultTsys ) ;
2094 srcnameCol.put( i, srcname ) ;
2095 fieldnameCol.put( i, fieldname ) ;
2096 srctypeCol.put( i, (Int)SrcType::PSON ) ;
2097 opacityCol.put( i, opacity ) ;
2098 srcvelCol.put( i, srcvel ) ;
2099 srcpmCol.put( i, srcpm ) ;
2100 srcdirCol.put( i, srcdir ) ;
2101 scanrateCol.put( i, scanrate ) ;
2102 timeCol.put( i, time ) ;
2103 intervalCol.put( i, interval ) ;
2104 }
2105}
2106
2107vector<int> STGrid::getResultantMapSize()
2108{
2109 vector<int> r(2);
2110 r[0] = nx_;
2111 r[1] = ny_;
2112 return r;
2113}
2114
2115vector<double> STGrid::getResultantCellSize()
2116{
2117 vector<double> r(2);
2118 r[0] = cellx_;
2119 r[1] = celly_;
2120 return r;
2121}
2122
2123// STGrid2
2124STGrid2::STGrid2()
2125 : STGrid()
2126{
2127}
2128
2129STGrid2::STGrid2( const ScantableWrapper &s )
2130 : STGrid()
2131{
2132 setScantable( s ) ;
2133}
2134
2135STGrid2::STGrid2( const vector<ScantableWrapper> &v )
2136 : STGrid()
2137{
2138 setScantableList( v ) ;
2139}
2140
2141void STGrid2::setScantable( const ScantableWrapper &s )
2142{
2143 nfile_ = 1 ;
2144 dataList_.resize( nfile_ ) ;
2145 dataList_[0] = s ;
2146 infileList_.resize( nfile_ ) ;
2147 infileList_[0] = s.getCP()->table().tableName() ;
2148}
2149
2150void STGrid2::setScantableList( const vector<ScantableWrapper> &v )
2151{
2152 nfile_ = v.size() ;
2153 dataList_.resize( nfile_ ) ;
2154 infileList_.resize( nfile_ ) ;
2155 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
2156 dataList_[i] = v[i] ;
2157 infileList_[i] = v[i].getCP()->table().tableName() ;
2158 }
2159}
2160
2161ScantableWrapper STGrid2::getResultAsScantable( int tp )
2162{
2163 ScantableWrapper sw( tp ) ;
2164 CountedPtr<Scantable> s = sw.getCP() ;
2165 s->setHeader( dataList_[0].getCP()->getHeader() ) ;
2166 Table tout, tin ;
2167 String subt[] = { "FREQUENCIES", "FOCUS", "WEATHER",
2168 "TCAL", "MOLECULES", "HISTORY", "FIT" } ;
2169 for ( uInt i = 0 ; i < 7 ; i++ ) {
2170 tout = s->table().rwKeywordSet().asTable(subt[i]) ;
2171 tin = dataList_[0].getCP()->table().rwKeywordSet().asTable(subt[i]) ;
2172 TableCopy::copyRows( tout, tin ) ;
2173 tout.rwKeywordSet() = tin.rwKeywordSet();
2174 }
2175 fillTable( s->table() ) ;
2176 return sw ;
2177}
2178
2179void STGrid2::table( Table &tab, uInt i )
2180{
2181 if ( i < nfile_ )
2182 tab = dataList_[i].getCP()->table() ;
2183}
2184
2185}
Note: See TracBrowser for help on using the repository browser.