source: branches/casa-release-4_3/src/STGrid.cpp@ 3133

Last change on this file since 3133 was 2962, checked in by Kana Sugimoto, 10 years ago

New Development: Yes

JIRA Issue: Yes (CAS-6590)

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: runUnitTest.main(test_sdgrid[test302])

Put in Release Notes: Yes

Module(s): asapgrid, asapgrid2, sdgrid, sdplot(plottype='grid')

Description: FLAG_ROW is set to scantable rows if all channels in the rows are flagged.


File size: 63.2 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 : common(common), self(obj), 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 : common(common), self(obj), 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 Bool b1, b2, b3 ;
1024 const Complex *gdata_p = gdata.getStorage( b1 ) ;
1025 Float *gwgt_p = gwgt.getStorage( b2 ) ; // storage shared with data_
1026 uChar *gflg_p = flag_.getStorage( b3 ) ;
1027 for ( uInt i = 0 ; i < len ; i++ ) {
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 }
1035 }
1036 gdata.freeStorage( gdata_p, b1 ) ;
1037 data_.putStorage( gwgt_p, b2 ) ;
1038 flag_.putStorage( gflg_p, b3 ) ;
1039 t1 = mathutil::gettimeofday_sec() ;
1040 os << LogIO::DEBUGGING << "setData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ;
1041}
1042
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
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{
1062 LogIO os( LogOrigin("STGrid","setupGrid",WHERE) ) ;
1063 //cout << "nx=" << nx << ", ny=" << ny << endl ;
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" ) ;
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 ;
1119 }
1120// os << "xmin=" << xmin << LogIO::POST ;
1121// os << "center_=" << center_ << LogIO::POST ;
1122
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 }
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 ;
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 ) {
1150 os << "Using celly to x-axis..." << LogIO::POST ;
1151 readQuantity( qcelly, celly ) ;
1152 qcellx = qcelly ;
1153 }
1154 else if ( cellx.size() != 0 ) {
1155 os << "Using cellx to y-axis..." << LogIO::POST ;
1156 readQuantity( qcellx, cellx ) ;
1157 qcelly = qcellx ;
1158 }
1159 else {
1160 if ( nx_ < 0 ) {
1161 os << "No user preference in grid setting. Using default..." << LogIO::POST ;
1162 readQuantity( qcellx, "1.0arcmin" ) ;
1163 qcelly = qcellx ;
1164 }
1165 else {
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 }
1174 qcellx = Quantum<Double>( wx/nx_*cos(center_[1]), "rad" ) ;
1175 qcelly = Quantum<Double>( wy/ny_, "rad" ) ;
1176 }
1177 }
1178 cellx_ = qcellx.getValue( "rad" ) ;
1179 celly_ = qcelly.getValue( "rad" ) ;
1180 //os << "cellx_=" << cellx_ << ", celly_=" << celly_ << ", cos("<<center_(1)<<")=" << cos(center_(1)) << LogIO::POST ;
1181 if ( nx_ < 0 ) {
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 }
1190 nx_ = Int( ceil( wx/(cellx_/cos(center_[1])) ) ) ;
1191 ny_ = Int( ceil( wy/celly_ ) ) ;
1192 }
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)) ;
1205}
1206
1207void STGrid::mapExtent( Double &xmin, Double &xmax,
1208 Double &ymin, Double &ymax )
1209{
1210 //LogIO os( LogOrigin("STGrid","mapExtent",WHERE) ) ;
1211 directionCol_.attach( tableList_[0], "DIRECTION" ) ;
1212 Matrix<Double> direction = directionCol_.getColumn() ;
1213 //os << "dirCol.nrow() = " << dirCol.nrow() << LogIO::POST ;
1214 Vector<Double> ra( direction.row(0) ) ;
1215 mathutil::rotateRA( ra ) ;
1216 minMax( xmin, xmax, direction.row( 0 ) ) ;
1217 minMax( ymin, ymax, direction.row( 1 ) ) ;
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 ;
1223 // to make contiguous RA distribution (no 2pi jump)
1224 Vector<Double> ra( direction.row(0) ) ;
1225 mathutil::rotateRA( ra ) ;
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 }
1233 //os << "(xmin,xmax)=(" << xmin << "," << xmax << ")" << LogIO::POST ;
1234 //os << "(ymin,ymax)=(" << ymin << "," << ymax << ")" << LogIO::POST ;
1235}
1236
1237void STGrid::table( Table &tab, uInt i )
1238{
1239 if ( i < nfile_ )
1240 tab = Table( infileList_[i] ) ;
1241}
1242
1243void STGrid::selectData()
1244{
1245 LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ;
1246 Int ifno = ifno_ ;
1247 tableList_.resize( nfile_ ) ;
1248 if ( ifno_ == -1 ) {
1249 //Table taborg( infileList_[0] ) ;
1250 Table taborg ;
1251 table( taborg, 0 ) ;
1252 ROScalarColumn<uInt> ifnoCol( taborg, "IFNO" ) ;
1253 ifno_ = ifnoCol( 0 ) ;
1254 os << LogIO::WARN
1255 << "IFNO is not given. Using default IFNO: " << ifno_ << LogIO::POST ;
1256 }
1257 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
1258 //Table taborg( infileList_[i] ) ;
1259 Table taborg ;
1260 table( taborg, i ) ;
1261 TableExprNode node ;
1262 if ( ifno != -1 || isMultiIF( taborg ) ) {
1263 os << "apply selection on IFNO" << LogIO::POST ;
1264 node = taborg.col("IFNO") == ifno_ ;
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 }
1276 os << LogIO::DEBUGGING << "tableList_[" << i << "].nrow()=" << tableList_[i].nrow() << LogIO::POST ;
1277 if ( tableList_[i].nrow() == 0 ) {
1278 os << LogIO::SEVERE
1279 << "No corresponding rows for given selection: IFNO " << ifno_ ;
1280 if ( scanlist_.size() > 0 )
1281 os << " SCANNO " << scanlist_ ;
1282 os << LogIO::EXCEPTION ;
1283 }
1284 }
1285}
1286
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
1294void STGrid::attach( Table &tab )
1295{
1296 // attach to table
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" ) ;
1303}
1304
1305Int STGrid::getDataChunk(
1306 IPosition const &wshape,
1307 IPosition const &vshape,
1308 IPosition const &/* dshape */,
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
1338Int STGrid::getDataChunk( Array<Complex> &spectra,
1339 Array<Double> &direction,
1340 Array<Int> &flagtra,
1341 Array<Int> &rflag,
1342 Array<Float> &weight )
1343{
1344 LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
1345 Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ;
1346 if ( nrow < nchunk_ ) {
1347 spectra.resize( spectraF_.shape() ) ;
1348 flagtra.resize( flagtraUC_.shape() ) ;
1349 rflag.resize( rflagUI_.shape() ) ;
1350 }
1351 double t0, t1 ;
1352 t0 = mathutil::gettimeofday_sec() ;
1353 convertArray( spectra, spectraF_ ) ;
1354 toInt( flagtraUC_, flagtra ) ;
1355 toInt( rflagUI_, rflag ) ;
1356 t1 = mathutil::gettimeofday_sec() ;
1357 eToInt = t1 - t0 ;
1358
1359 return nrow ;
1360}
1361#endif
1362
1363Int STGrid::getDataChunk( Array<Float> &spectra,
1364 Array<Double> &direction,
1365 Array<uChar> &flagtra,
1366 Array<uInt> &rflag,
1367 Array<Float> &weight )
1368{
1369 LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ;
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 ) ;
1381 }
1382 // 2011/12/22 TN
1383 // tsys shares its storage with weight
1384 Array<Float> tsys( weight ) ;
1385 Array<Double> tint( rflag.shape() ) ;
1386
1387 Vector<uInt> rflagVec( rflag ) ;
1388 Vector<Double> tintVec( tint ) ;
1389
1390 RefRows rows( nprocessed_, nprocessed_+nrow-1, 1 ) ;
1391 //os<<LogIO::DEBUGGING<<"nprocessed_="<<nprocessed_<<": rows.nrows()="<<rows.nrows()<<LogIO::POST ;
1392 spectraCol_.getColumnCells( rows, spectra ) ;
1393 flagtraCol_.getColumnCells( rows, flagtra ) ;
1394 directionCol_.getColumnCells( rows, direction ) ;
1395 // to make contiguous RA distribution (no 2pi jump)
1396 Vector<Double> v( Matrix<Double>(direction).row(0) ) ;
1397 mathutil::rotateRA( v ) ;
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] ;
1405
1406 double t0,t1 ;
1407 t0 = mathutil::gettimeofday_sec() ;
1408 getWeight( weight, tsys, tint ) ;
1409 t1 = mathutil::gettimeofday_sec() ;
1410 eGetWeight += t1-t0 ;
1411
1412 nprocessed_ += nrow ;
1413
1414 return nrow ;
1415}
1416
1417void STGrid::setupArray()
1418{
1419 LogIO os( LogOrigin("STGrid","setupArray",WHERE) ) ;
1420 ROScalarColumn<uInt> polnoCol( tableList_[0], "POLNO" ) ;
1421 Vector<uInt> pols = polnoCol.getColumn() ;
1422 //os << pols << LogIO::POST ;
1423 Vector<uInt> pollistOrg ;
1424 npolOrg_ = 0 ;
1425 uInt polno ;
1426 for ( uInt i = 0 ; i < polnoCol.nrow() ; i++ ) {
1427 //polno = polnoCol( i ) ;
1428 polno = pols( i ) ;
1429 if ( allNE( pollistOrg, polno ) ) {
1430 pollistOrg.resize( npolOrg_+1, True ) ;
1431 pollistOrg[npolOrg_] = polno ;
1432 npolOrg_++ ;
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 }
1453 rows_.resize( nfile_ ) ;
1454 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
1455 rows_[i] = tableList_[i].nrow() / npolOrg_ ;
1456 //if ( nrow_ < rows_[i] )
1457 // nrow_ = rows_[i] ;
1458 }
1459 flagtraCol_.attach( tableList_[0], "FLAGTRA" ) ;
1460 nchan_ = flagtraCol_( 0 ).nelements() ;
1461// os << "npol_ = " << npol_ << "(" << pollist_ << ")" << endl
1462// << "nchan_ = " << nchan_ << endl
1463// << "nrow_ = " << nrow_ << LogIO::POST ;
1464}
1465
1466void STGrid::getWeight( Array<Float> &w,
1467 Array<Float> &tsys,
1468 Array<Double> &tint )
1469{
1470 LogIO os( LogOrigin("STGrid","getWeight",WHERE) ) ;
1471
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
1540void STGrid::toInt( Array<uChar> &u, Array<Int> &v )
1541{
1542 uInt len = u.nelements() ;
1543 Int *int_p = new Int[len] ;
1544 Bool deleteIt ;
1545 const uChar *data_p = u.getStorage( deleteIt ) ;
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 }
1553 u.freeStorage( data_p, deleteIt ) ;
1554 v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
1555}
1556
1557void STGrid::toInt( Array<uInt> &u, Array<Int> &v )
1558{
1559 uInt len = u.nelements() ;
1560 Int *int_p = new Int[len] ;
1561 Bool deleteIt ;
1562 const uInt *data_p = u.getStorage( deleteIt ) ;
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 }
1570 u.freeStorage( data_p, deleteIt ) ;
1571 v.takeStorage( u.shape(), int_p, TAKE_OVER ) ;
1572}
1573
1574void STGrid::toPixel( Array<Double> &world, Array<Double> &pixel )
1575{
1576 uInt nrow = world.shape()[1] ;
1577 Bool bw, bp ;
1578 Double *w_p = world.getStorage( bw ) ;
1579 Double *p_p = pixel.getStorage( bp ) ;
1580 Double *ww_p = w_p ;
1581 Double *wp_p = p_p ;
1582 IPosition vshape( 1, 2 ) ;
1583 Vector<Double> _world, _pixel ;
1584 for ( uInt i = 0 ; i < nrow ; i++ ) {
1585 _world.takeStorage( vshape, ww_p, SHARE ) ;
1586 _pixel.takeStorage( vshape, wp_p, SHARE ) ;
1587 dircoord_->toPixel( _pixel, _world ) ;
1588 ww_p += 2 ;
1589 wp_p += 2 ;
1590 }
1591 world.putStorage( w_p, bw ) ;
1592 pixel.putStorage( p_p, bp ) ;
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_
1605#define grdgauss grdgauss_
1606#define grdjinc1 grdjinc1_
1607#endif
1608#if defined(USE_CASAPY)
1609extern "C" {
1610 void grdsf(Double*, Double*);
1611 void grdgauss(Double*, Double*, Double*);
1612 void grdjinc1(Double*, Double*, Int*, Double*);
1613}
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
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
1676void STGrid::gaussFunc( Vector<Float> &convFunc, Double hwhm, Double truncate )
1677{
1678 convFunc = 0.0 ;
1679 Int len = (Int)(truncate*Double(convSampling_)+0.5);
1680 Double out, val;
1681 for ( Int i = 0 ; i < len ; i++ ) {
1682 val = Double(i) / Double(convSampling_) ;
1683 grdgauss(&hwhm, &val, &out);
1684 convFunc(i) = out;
1685 }
1686}
1687
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
1718void STGrid::pbFunc( Vector<Float> &convFunc )
1719{
1720 convFunc = 0.0 ;
1721}
1722
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
1772void STGrid::setConvFunc( Vector<Float> &convFunc )
1773{
1774 LogIO os(LogOrigin("STGrid","setConvFunc",WHERE));
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 ) ;
1782 os << LogIO::DEBUGGING
1783 << "convType_ = " << convType_ << endl
1784 << "convSupport_ = " << convSupport_ << LogIO::POST;
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 ) ;
1792 os << LogIO::DEBUGGING
1793 << "convType_ = " << convType_ << endl
1794 << "convSupport_ = " << convSupport_ << LogIO::POST;
1795 }
1796 else if ( convType_ == "GAUSS" ) {
1797 // determine pixel gwidth
1798 // default is HWHM corresponding to b = 1.0 (Mangum et al. 2007)
1799 Double pixelGW = -1.0;
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 }
1809 }
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 }
1815 // determine truncation radius
1816 // default is 3 * HWHM
1817 Double truncate = -1.0;
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 }
1827 //convSupport_ = (Int)(truncate+0.5);
1828 truncate = (truncate >= 0.0) ? truncate : 3.0 * pixelGW;
1829 convSupport_ = Int(truncate);
1830 convSupport_ += (((truncate-(Double)convSupport_) > 0.0) ? 1 : 0);
1831 Int convSize = convSampling_ * ( 2*convSupport_ + 2 ) ;
1832 convFunc.resize( convSize ) ;
1833 gaussFunc( convFunc, pixelGW, truncate ) ;
1834 os << LogIO::DEBUGGING
1835 << "convType_ = " << convType_ << endl
1836 << "convSupport_ = " << convSupport_ << endl
1837 << "truncate_ = " << truncate << "pixel" << endl
1838 << "gwidth_ = " << pixelGW << "pixel" << LogIO::POST;
1839 }
1840 else if ( convType_ == "GJINC" ) {
1841 // determine pixel gwidth
1842 // default is HWHM corresponding to b = 2.52 (Mangum et al. 2007)
1843 Double pixelGW = -1.0;
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 }
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 }
1859 // determine pixel c
1860 // default is c = 1.55 (Mangum et al. 2007)
1861 Double pixelJW = -1.0;
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 }
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 }
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 }
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);
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
1898 << "truncate_ = " << truncate << "pixel" << endl
1899 << "gwidth_ = " << pixelGW << "pixel" << endl
1900 << "jwidth_ = " << pixelJW << "pixel" << LogIO::POST;
1901 }
1902 else if ( convType_ == "PB" ) {
1903 if ( convSupport_ < 0 )
1904 convSupport_ = 0 ;
1905 pbFunc( convFunc ) ;
1906 }
1907 else {
1908 throw AipsError( "Unsupported convolution function" ) ;
1909 }
1910}
1911
1912string STGrid::saveData( string outfile )
1913{
1914 LogIO os( LogOrigin("STGrid", "saveData", WHERE) ) ;
1915 double t0, t1 ;
1916 t0 = mathutil::gettimeofday_sec() ;
1917
1918 //Int polno = 0 ;
1919 String outfile_ ;
1920 if ( outfile.size() == 0 ) {
1921 if ( infileList_[0].lastchar() == '/' ) {
1922 outfile_ = infileList_[0].substr( 0, infileList_[0].size()-1 ) ;
1923 }
1924 else {
1925 outfile_ = infileList_[0] ;
1926 }
1927 outfile_ += ".grid" ;
1928 }
1929 else {
1930 outfile_ = outfile ;
1931 }
1932 Table tab ;
1933 prepareTable( tab, outfile_ ) ;
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
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 }
1961}
1962
1963void STGrid::fillTable( Table &tab )
1964{
1965 //IPosition dshape = data_.shape() ;
1966 Int nrow = nx_ * ny_ * npol_ ;
1967 tab.rwKeywordSet().define( "nPol", npol_ ) ;
1968 tab.addRow( nrow ) ;
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 ) ;
1973 Vector<Double> pix( 2 );
1974 ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
1975 ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
1976 ArrayColumn<uChar> flagtraCol( tab, "FLAGTRA" ) ;
1977 ScalarColumn<uInt> flagRowCol( tab, "FLAGROW" );
1978 ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
1979 ScalarColumn<uInt> scannoCol( tab, "SCANNO" ) ;
1980 Int irow = 0 ;
1981 Vector<Float> sp( nchan_ ) ;
1982 Vector<uChar> flag( nchan_ ) ;
1983 Bool bsp, bdata, bflag ;
1984 const Float *data_p = data_.getStorage( bdata ) ;
1985 const uChar *flag_p = flag_.getStorage( bflag ) ;
1986 Float *wsp_p, *sp_p ;
1987 const Float *wdata_p = data_p ;
1988 const uChar *wflag_p = flag_p ;
1989 long step = nx_ * ny_ * npol_ ;
1990 long offset ;
1991 uInt scanno = 0 ;
1992 uChar rflag;
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)(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 rflag = ~0 ; //11111111
2007 for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
2008 *wsp_p = *wdata_p ;
2009 wsp_p++ ;
2010 wdata_p += step ;
2011 flag[ichan] = *wflag_p ;
2012 rflag &= flag[ichan] ;
2013 wflag_p += step ;
2014 }
2015 sp.putStorage( sp_p, bsp ) ;
2016 spectraCol.put( irow, sp ) ;
2017 flagtraCol.put( irow, flag ) ;
2018 flagRowCol.put( irow, ((rflag>0) ? 1 : 0) ) ;
2019 directionCol.put( irow, dir ) ;
2020 polnoCol.put( irow, pollist_[ipol] ) ;
2021 scannoCol.put( irow, scanno ) ;
2022 irow++ ;
2023 }
2024 scanno++ ;
2025 }
2026 }
2027 data_.freeStorage( data_p, bdata ) ;
2028 flag_.freeStorage( flag_p, bflag ) ;
2029
2030 fillMainColumns( tab ) ;
2031}
2032
2033void STGrid::fillMainColumns( Table &tab )
2034{
2035 // values for fill
2036 //Table t( infileList_[0], Table::Old ) ;
2037 Table t ;
2038 table( t, 0 ) ;
2039 Table tsel = t( t.col( "IFNO" ) == (uInt)ifno_, 1 ) ;
2040 ROTableRow row( tsel ) ;
2041 row.get( 0 ) ;
2042 const TableRecord &rec = row.record() ;
2043 uInt freqId = rec.asuInt( "FREQ_ID" ) ;
2044 uInt molId = rec.asuInt( "MOLECULE_ID" ) ;
2045 uInt tcalId = rec.asuInt( "TCAL_ID" ) ;
2046 uInt focusId = rec.asuInt( "FOCUS_ID" ) ;
2047 uInt weatherId = rec.asuInt( "WEATHER_ID" ) ;
2048 String srcname = rec.asString( "SRCNAME" ) ;
2049 String fieldname = rec.asString( "FIELDNAME" ) ;
2050 Vector<Float> defaultTsys( 1, 1.0 ) ;
2051 // @todo how to set flagtra for gridded spectra?
2052 Vector<uChar> flagtra = rec.asArrayuChar( "FLAGTRA" ) ;
2053 flagtra = (uChar)0 ;
2054 Float opacity = rec.asFloat( "OPACITY" ) ;
2055 Double srcvel = rec.asDouble( "SRCVELOCITY" ) ;
2056 Vector<Double> srcpm = rec.asArrayDouble( "SRCPROPERMOTION" ) ;
2057 Vector<Double> srcdir = rec.asArrayDouble( "SRCDIRECTION" ) ;
2058 Vector<Double> scanrate = rec.asArrayDouble( "SCANRATE" ) ;
2059 Double time = rec.asDouble( "TIME" ) ;
2060 Double interval = rec.asDouble( "INTERVAL" ) ;
2061
2062 // fill columns
2063 Int nrow = tab.nrow() ;
2064 ScalarColumn<uInt> ifnoCol( tab, "IFNO" ) ;
2065 ScalarColumn<uInt> beamnoCol(tab, "BEAMNO");
2066 ScalarColumn<uInt> freqIdCol( tab, "FREQ_ID" ) ;
2067 ScalarColumn<uInt> molIdCol( tab, "MOLECULE_ID" ) ;
2068 ScalarColumn<uInt> tcalidCol( tab, "TCAL_ID" ) ;
2069 ScalarColumn<Int> fitidCol( tab, "FIT_ID" ) ;
2070 ScalarColumn<uInt> focusidCol( tab, "FOCUS_ID" ) ;
2071 ScalarColumn<uInt> weatheridCol( tab, "WEATHER_ID" ) ;
2072 ArrayColumn<uChar> flagtraCol( tab, "FLAGTRA" ) ;
2073 ScalarColumn<uInt> rflagCol( tab, "FLAGROW" ) ;
2074 ArrayColumn<Float> tsysCol( tab, "TSYS" ) ;
2075 ScalarColumn<String> srcnameCol( tab, "SRCNAME" ) ;
2076 ScalarColumn<String> fieldnameCol( tab, "FIELDNAME" ) ;
2077 ScalarColumn<Int> srctypeCol( tab, "SRCTYPE" ) ;
2078 ScalarColumn<Float> opacityCol( tab, "OPACITY" ) ;
2079 ScalarColumn<Double> srcvelCol( tab, "SRCVELOCITY" ) ;
2080 ArrayColumn<Double> srcpmCol( tab, "SRCPROPERMOTION" ) ;
2081 ArrayColumn<Double> srcdirCol( tab, "SRCDIRECTION" ) ;
2082 ArrayColumn<Double> scanrateCol( tab, "SCANRATE" ) ;
2083 ScalarColumn<Double> timeCol( tab, "TIME" ) ;
2084 ScalarColumn<Double> intervalCol( tab, "INTERVAL" ) ;
2085 for ( Int i = 0 ; i < nrow ; i++ ) {
2086 ifnoCol.put( i, (uInt)ifno_ ) ;
2087 beamnoCol.put(i, 0);
2088 freqIdCol.put( i, freqId ) ;
2089 molIdCol.put( i, molId ) ;
2090 tcalidCol.put( i, tcalId ) ;
2091 fitidCol.put( i, -1 ) ;
2092 focusidCol.put( i, focusId ) ;
2093 weatheridCol.put( i, weatherId ) ;
2094 //flagtraCol.put( i, flagtra ) ;
2095 //rflagCol.put( i, 0 ) ;
2096 tsysCol.put( i, defaultTsys ) ;
2097 srcnameCol.put( i, srcname ) ;
2098 fieldnameCol.put( i, fieldname ) ;
2099 srctypeCol.put( i, (Int)SrcType::PSON ) ;
2100 opacityCol.put( i, opacity ) ;
2101 srcvelCol.put( i, srcvel ) ;
2102 srcpmCol.put( i, srcpm ) ;
2103 srcdirCol.put( i, srcdir ) ;
2104 scanrateCol.put( i, scanrate ) ;
2105 timeCol.put( i, time ) ;
2106 intervalCol.put( i, interval ) ;
2107 }
2108}
2109
2110vector<int> STGrid::getResultantMapSize()
2111{
2112 vector<int> r(2);
2113 r[0] = nx_;
2114 r[1] = ny_;
2115 return r;
2116}
2117
2118vector<double> STGrid::getResultantCellSize()
2119{
2120 vector<double> r(2);
2121 r[0] = cellx_;
2122 r[1] = celly_;
2123 return r;
2124}
2125
2126// STGrid2
2127STGrid2::STGrid2()
2128 : STGrid()
2129{
2130}
2131
2132STGrid2::STGrid2( const ScantableWrapper &s )
2133 : STGrid()
2134{
2135 setScantable( s ) ;
2136}
2137
2138STGrid2::STGrid2( const vector<ScantableWrapper> &v )
2139 : STGrid()
2140{
2141 setScantableList( v ) ;
2142}
2143
2144void STGrid2::setScantable( const ScantableWrapper &s )
2145{
2146 nfile_ = 1 ;
2147 dataList_.resize( nfile_ ) ;
2148 dataList_[0] = s ;
2149 infileList_.resize( nfile_ ) ;
2150 infileList_[0] = s.getCP()->table().tableName() ;
2151}
2152
2153void STGrid2::setScantableList( const vector<ScantableWrapper> &v )
2154{
2155 nfile_ = v.size() ;
2156 dataList_.resize( nfile_ ) ;
2157 infileList_.resize( nfile_ ) ;
2158 for ( uInt i = 0 ; i < nfile_ ; i++ ) {
2159 dataList_[i] = v[i] ;
2160 infileList_[i] = v[i].getCP()->table().tableName() ;
2161 }
2162}
2163
2164ScantableWrapper STGrid2::getResultAsScantable( int tp )
2165{
2166 ScantableWrapper sw( tp ) ;
2167 CountedPtr<Scantable> s = sw.getCP() ;
2168 s->setHeader( dataList_[0].getCP()->getHeader() ) ;
2169 Table tout, tin ;
2170 String subt[] = { "FREQUENCIES", "FOCUS", "WEATHER",
2171 "TCAL", "MOLECULES", "HISTORY", "FIT" } ;
2172 for ( uInt i = 0 ; i < 7 ; i++ ) {
2173 tout = s->table().rwKeywordSet().asTable(subt[i]) ;
2174 tin = dataList_[0].getCP()->table().rwKeywordSet().asTable(subt[i]) ;
2175 TableCopy::copyRows( tout, tin ) ;
2176 tout.rwKeywordSet() = tin.rwKeywordSet();
2177 }
2178 fillTable( s->table() ) ;
2179 return sw ;
2180}
2181
2182void STGrid2::table( Table &tab, uInt i )
2183{
2184 if ( i < nfile_ )
2185 tab = dataList_[i].getCP()->table() ;
2186}
2187
2188}
Note: See TracBrowser for help on using the repository browser.