1 | #include <iostream>
|
---|
2 | #include <fstream>
|
---|
3 |
|
---|
4 | #include <casa/BasicSL/String.h>
|
---|
5 | #include <casa/Arrays/Vector.h>
|
---|
6 | #include <casa/Arrays/Matrix.h>
|
---|
7 | #include <casa/Arrays/Cube.h>
|
---|
8 | #include <casa/Arrays/ArrayMath.h>
|
---|
9 | #include <casa/Arrays/ArrayPartMath.h>
|
---|
10 | #include <casa/Quanta/Quantum.h>
|
---|
11 | #include <casa/Quanta/QuantumHolder.h>
|
---|
12 | #include <casa/Utilities/CountedPtr.h>
|
---|
13 | #include <casa/Logging/LogIO.h>
|
---|
14 |
|
---|
15 | #include <tables/Tables/Table.h>
|
---|
16 | #include <tables/Tables/TableRecord.h>
|
---|
17 | #include <tables/Tables/ExprNode.h>
|
---|
18 | #include <tables/Tables/ScalarColumn.h>
|
---|
19 | #include <tables/Tables/ArrayColumn.h>
|
---|
20 |
|
---|
21 | #include <measures/Measures/MDirection.h>
|
---|
22 |
|
---|
23 | #include <Scantable.h>
|
---|
24 | #include "STGrid.h"
|
---|
25 |
|
---|
26 | using namespace std ;
|
---|
27 | using namespace casa ;
|
---|
28 | using namespace asap ;
|
---|
29 |
|
---|
30 | namespace asap {
|
---|
31 |
|
---|
32 | // constructor
|
---|
33 | STGrid::STGrid()
|
---|
34 | {
|
---|
35 | init() ;
|
---|
36 | }
|
---|
37 |
|
---|
38 | STGrid::STGrid( const string infile )
|
---|
39 | {
|
---|
40 | init() ;
|
---|
41 |
|
---|
42 | setFileIn( infile ) ;
|
---|
43 | }
|
---|
44 |
|
---|
45 | void STGrid::init()
|
---|
46 | {
|
---|
47 | ifno_ = -1 ;
|
---|
48 | nx_ = -1 ;
|
---|
49 | ny_ = -1 ;
|
---|
50 | npol_ = 0 ;
|
---|
51 | nchan_ = 0 ;
|
---|
52 | nrow_ = 0 ;
|
---|
53 | cellx_ = 0.0 ;
|
---|
54 | celly_ = 0.0 ;
|
---|
55 | center_ = Vector<Double> ( 2, 0.0 ) ;
|
---|
56 | convType_ = "BOX" ;
|
---|
57 | wtype_ = "UNIFORM" ;
|
---|
58 | convSupport_ = -1 ;
|
---|
59 | userSupport_ = -1 ;
|
---|
60 | convSampling_ = 100 ;
|
---|
61 | }
|
---|
62 |
|
---|
63 | void STGrid::setFileIn( const string infile )
|
---|
64 | {
|
---|
65 | String name( infile ) ;
|
---|
66 | if ( infile_.compare( name ) != 0 ) {
|
---|
67 | infile_ = String( infile ) ;
|
---|
68 | tab_ = Table( infile_ ) ;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | void STGrid::setPolList( vector<unsigned int> pols )
|
---|
73 | {
|
---|
74 | //pollist_ = Vector<uInt>( pols ) ;
|
---|
75 | pollist_.assign( Vector<uInt>( pols ) ) ;
|
---|
76 | cout << "pollist_ = " << pollist_ << endl ;
|
---|
77 | }
|
---|
78 |
|
---|
79 | void STGrid::setWeight( const string wType )
|
---|
80 | {
|
---|
81 | wtype_ = String( wType ) ;
|
---|
82 | wtype_.upcase() ;
|
---|
83 | cout << "wtype_ = " << wtype_ << endl ;
|
---|
84 | }
|
---|
85 |
|
---|
86 | void STGrid::defineImage( int nx,
|
---|
87 | int ny,
|
---|
88 | string scellx,
|
---|
89 | string scelly,
|
---|
90 | string scenter )
|
---|
91 | {
|
---|
92 | ROArrayColumn<Double> dirCol( tab_, "DIRECTION" ) ;
|
---|
93 | Matrix<Double> direction = dirCol.getColumn() ;
|
---|
94 | Double rmax, rmin, dmax, dmin ;
|
---|
95 | minMax( rmin, rmax, direction.row( 0 ) ) ;
|
---|
96 | minMax( dmin, dmax, direction.row( 1 ) ) ;
|
---|
97 |
|
---|
98 | Int npx = (Int)nx ;
|
---|
99 | Int npy = (Int)ny ;
|
---|
100 | String cellx( scellx ) ;
|
---|
101 | String celly( scelly ) ;
|
---|
102 | String center( scenter ) ;
|
---|
103 | setupGrid( npx, npy,
|
---|
104 | cellx, celly,
|
---|
105 | rmin, rmax,
|
---|
106 | dmin, dmax,
|
---|
107 | center ) ;
|
---|
108 | }
|
---|
109 |
|
---|
110 | void STGrid::setOption( string convType,
|
---|
111 | int convSupport )
|
---|
112 | {
|
---|
113 | convType_ = String( convType ) ;
|
---|
114 | convType_.upcase() ;
|
---|
115 | userSupport_ = (Int)convSupport ;
|
---|
116 | }
|
---|
117 |
|
---|
118 | #define NEED_UNDERSCORES
|
---|
119 | #if defined(NEED_UNDERSCORES)
|
---|
120 | #define ggridsd ggridsd_
|
---|
121 | #endif
|
---|
122 | extern "C" {
|
---|
123 | void ggridsd(Double*,
|
---|
124 | const Complex*,
|
---|
125 | Int*,
|
---|
126 | Int*,
|
---|
127 | Int*,
|
---|
128 | const Int*,
|
---|
129 | const Int*,
|
---|
130 | const Float*,
|
---|
131 | Int*,
|
---|
132 | Int*,
|
---|
133 | Complex*,
|
---|
134 | Float*,
|
---|
135 | Int*,
|
---|
136 | Int*,
|
---|
137 | Int *,
|
---|
138 | Int *,
|
---|
139 | Int*,
|
---|
140 | Int*,
|
---|
141 | Float*,
|
---|
142 | Int*,
|
---|
143 | Int*,
|
---|
144 | Double*);
|
---|
145 | }
|
---|
146 | void STGrid::grid()
|
---|
147 | {
|
---|
148 | LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
|
---|
149 |
|
---|
150 | // retrieve data
|
---|
151 | Cube<Float> spectra ;
|
---|
152 | Matrix<Double> direction ;
|
---|
153 | Cube<uChar> flagtra ;
|
---|
154 | Matrix<uInt> rflag ;
|
---|
155 | Matrix<Float> weight ;
|
---|
156 | getData( spectra, direction, flagtra, rflag, weight ) ;
|
---|
157 | IPosition sshape = spectra.shape() ;
|
---|
158 | //os << "spectra.shape()=" << spectra.shape() << LogIO::POST ;
|
---|
159 | //os << "max(spectra) = " << max(spectra) << LogIO::POST ;
|
---|
160 | //os << "weight = " << weight << LogIO::POST ;
|
---|
161 |
|
---|
162 | // flagtra: uChar -> Int
|
---|
163 | // rflag: uInt -> Int
|
---|
164 | Cube<Int> flagI ;
|
---|
165 | Matrix<Int> rflagI ;
|
---|
166 | toInt( &flagtra, &flagI ) ;
|
---|
167 | toInt( &rflag, &rflagI ) ;
|
---|
168 |
|
---|
169 | // grid parameter
|
---|
170 | os << LogIO::DEBUGGING ;
|
---|
171 | os << "----------" << endl ;
|
---|
172 | os << "Grid parameter summary" << endl ;
|
---|
173 | os << " (nx,ny) = (" << nx_ << "," << ny_ << ")" << endl ;
|
---|
174 | os << " (cellx,celly) = (" << cellx_ << "," << celly_ << ")" << endl ;
|
---|
175 | os << " center = " << center_ << endl ;
|
---|
176 | os << "----------" << LogIO::POST ;
|
---|
177 | os << LogIO::NORMAL ;
|
---|
178 |
|
---|
179 | // convolution kernel
|
---|
180 | Vector<Float> convFunc ;
|
---|
181 | setConvFunc( convFunc ) ;
|
---|
182 | //cout << "convSupport=" << convSupport_ << endl ;
|
---|
183 | //cout << "convFunc=" << convFunc << endl ;
|
---|
184 |
|
---|
185 | // world -> pixel
|
---|
186 | Matrix<Double> xypos( direction.shape(), 0.0 ) ;
|
---|
187 | toPixel( direction, xypos ) ;
|
---|
188 |
|
---|
189 | // call ggridsd
|
---|
190 | Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ;
|
---|
191 | Double *xypos_p = xypos.getStorage( deletePos ) ;
|
---|
192 | Cube<Complex> dataC( spectra.shape(), 0.0 ) ;
|
---|
193 | setReal( dataC, spectra ) ;
|
---|
194 | const Complex *data_p = dataC.getStorage( deleteData ) ;
|
---|
195 | const Float *wgt_p = weight.getStorage( deleteWgt ) ;
|
---|
196 | const Int *flag_p = flagI.getStorage( deleteFlag ) ;
|
---|
197 | const Int *rflag_p = rflagI.getStorage( deleteFlagR ) ;
|
---|
198 | Float *conv_p = convFunc.getStorage( deleteConv ) ;
|
---|
199 | // Extend grid plane with convSupport_
|
---|
200 | //IPosition gshape( 4, nx_, ny_, npol, nchan ) ;
|
---|
201 | Int gnx = nx_+convSupport_*2 ;
|
---|
202 | Int gny = ny_+convSupport_*2 ;
|
---|
203 | IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
|
---|
204 | Array<Complex> gdataArrC( gshape, 0.0 ) ;
|
---|
205 | Array<Float> gwgtArr( gshape, 0.0 ) ;
|
---|
206 | Complex *gdata_p = gdataArrC.getStorage( deleteDataG ) ;
|
---|
207 | Float *wdata_p = gwgtArr.getStorage( deleteWgtG ) ;
|
---|
208 | Int idopsf = 0 ;
|
---|
209 | Int irow = -1 ;
|
---|
210 | Int *chanMap = new Int[nchan_] ;
|
---|
211 | {
|
---|
212 | Int *work_p = chanMap ;
|
---|
213 | for ( Int i = 0 ; i < nchan_ ; i++ ) {
|
---|
214 | *work_p = i ;
|
---|
215 | work_p++ ;
|
---|
216 | }
|
---|
217 | }
|
---|
218 | Int *polMap = new Int[npol_] ;
|
---|
219 | {
|
---|
220 | Int *work_p = polMap ;
|
---|
221 | for ( Int i = 0 ; i < npol_ ; i++ ) {
|
---|
222 | *work_p = i ;
|
---|
223 | work_p++ ;
|
---|
224 | }
|
---|
225 | }
|
---|
226 | Double *sumw_p = new Double[npol_*nchan_] ;
|
---|
227 | {
|
---|
228 | Double *work_p = sumw_p ;
|
---|
229 | for ( Int i = 0 ; i < npol_*nchan_ ; i++ ) {
|
---|
230 | *work_p = 0.0 ;
|
---|
231 | work_p++ ;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | ggridsd( xypos_p,
|
---|
235 | data_p,
|
---|
236 | &npol_,
|
---|
237 | &nchan_,
|
---|
238 | &idopsf,
|
---|
239 | flag_p,
|
---|
240 | rflag_p,
|
---|
241 | wgt_p,
|
---|
242 | &nrow_,
|
---|
243 | &irow,
|
---|
244 | gdata_p,
|
---|
245 | wdata_p,
|
---|
246 | &gnx,
|
---|
247 | &gny,
|
---|
248 | &npol_,
|
---|
249 | &nchan_,
|
---|
250 | &convSupport_,
|
---|
251 | &convSampling_,
|
---|
252 | conv_p,
|
---|
253 | chanMap,
|
---|
254 | polMap,
|
---|
255 | sumw_p ) ;
|
---|
256 | xypos.putStorage( xypos_p, deletePos ) ;
|
---|
257 | dataC.freeStorage( data_p, deleteData ) ;
|
---|
258 | weight.freeStorage( wgt_p, deleteWgt ) ;
|
---|
259 | flagI.freeStorage( flag_p, deleteFlag ) ;
|
---|
260 | rflagI.freeStorage( rflag_p, deleteFlagR ) ;
|
---|
261 | convFunc.putStorage( conv_p, deleteConv ) ;
|
---|
262 | delete polMap ;
|
---|
263 | delete chanMap ;
|
---|
264 | gdataArrC.putStorage( gdata_p, deleteDataG ) ;
|
---|
265 | gwgtArr.putStorage( wdata_p, deleteWgtG ) ;
|
---|
266 | Array<Float> gdataArr = real( gdataArrC ) ;
|
---|
267 | data_.resize( gdataArr.shape() ) ;
|
---|
268 | data_ = 0.0 ;
|
---|
269 | for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
|
---|
270 | for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
|
---|
271 | for ( Int ip = 0 ; ip < npol_ ; ip++ ) {
|
---|
272 | for ( Int ic = 0 ; ic < nchan_ ; ic++ ) {
|
---|
273 | IPosition pos( 4, ix, iy, ip, ic ) ;
|
---|
274 | IPosition gpos( 4, ix+convSupport_, iy+convSupport_, ip, ic ) ;
|
---|
275 | if ( gwgtArr( gpos ) > 0.0 )
|
---|
276 | data_( pos ) = gdataArr( gpos ) / gwgtArr( gpos ) ;
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|
281 | //Matrix<Double> sumWeight( IPosition( 2, npol_, nchan_ ), sumw_p, TAKE_OVER ) ;
|
---|
282 | delete sumw_p ;
|
---|
283 | //cout << "sumWeight = " << sumWeight << endl ;
|
---|
284 | //cout << "gdataArr = " << gdataArr << endl ;
|
---|
285 | //cout << "gwgtArr = " << gwgtArr << endl ;
|
---|
286 | //cout << "data_ " << data_ << endl ;
|
---|
287 | }
|
---|
288 |
|
---|
289 | void STGrid::setupGrid( Int &nx,
|
---|
290 | Int &ny,
|
---|
291 | String &cellx,
|
---|
292 | String &celly,
|
---|
293 | Double &xmin,
|
---|
294 | Double &xmax,
|
---|
295 | Double &ymin,
|
---|
296 | Double &ymax,
|
---|
297 | String ¢er )
|
---|
298 | {
|
---|
299 | //cout << "nx=" << nx << ", ny=" << ny << endl ;
|
---|
300 |
|
---|
301 | // center position
|
---|
302 | if ( center.size() == 0 ) {
|
---|
303 | center_(0) = 0.5 * ( xmin + xmax ) ;
|
---|
304 | center_(1) = 0.5 * ( ymin + ymax ) ;
|
---|
305 | }
|
---|
306 | else {
|
---|
307 | String::size_type pos0 = center.find( " " ) ;
|
---|
308 | if ( pos0 == String::npos ) {
|
---|
309 | throw AipsError( "bad string format in parameter center" ) ;
|
---|
310 | }
|
---|
311 | String::size_type pos1 = center.find( " ", pos0+1 ) ;
|
---|
312 | String typestr, xstr, ystr ;
|
---|
313 | if ( pos1 != String::npos ) {
|
---|
314 | typestr = center.substr( 0, pos0 ) ;
|
---|
315 | xstr = center.substr( pos0+1, pos1-pos0 ) ;
|
---|
316 | ystr = center.substr( pos1+1 ) ;
|
---|
317 | // todo: convert to J2000 (or direction ref for DIRECTION column)
|
---|
318 | }
|
---|
319 | else {
|
---|
320 | typestr = "J2000" ;
|
---|
321 | xstr = center.substr( 0, pos0 ) ;
|
---|
322 | ystr = center.substr( pos0+1 ) ;
|
---|
323 | }
|
---|
324 | QuantumHolder qh ;
|
---|
325 | String err ;
|
---|
326 | qh.fromString( err, xstr ) ;
|
---|
327 | Quantum<Double> xcen = qh.asQuantumDouble() ;
|
---|
328 | qh.fromString( err, ystr ) ;
|
---|
329 | Quantum<Double> ycen = qh.asQuantumDouble() ;
|
---|
330 | center_(0) = xcen.getValue( "rad" ) ;
|
---|
331 | center_(1) = ycen.getValue( "rad" ) ;
|
---|
332 | }
|
---|
333 |
|
---|
334 |
|
---|
335 | //Double wx = xmax - xmin ;
|
---|
336 | //Double wy = ymax - ymin ;
|
---|
337 | Double wx = max( abs(xmax-center_(0)), abs(xmin-center_(0)) ) * 2 ;
|
---|
338 | Double wy = max( abs(ymax-center_(1)), abs(ymin-center_(1)) ) * 2 ;
|
---|
339 | // take 10% margin
|
---|
340 | wx *= 1.10 ;
|
---|
341 | wy *= 1.10 ;
|
---|
342 | Quantum<Double> qcellx ;
|
---|
343 | Quantum<Double> qcelly ;
|
---|
344 | nx_ = nx ;
|
---|
345 | ny_ = ny ;
|
---|
346 | if ( nx < 0 && ny > 0 ) {
|
---|
347 | nx_ = ny ;
|
---|
348 | ny_ = ny ;
|
---|
349 | }
|
---|
350 | if ( ny < 0 && nx > 0 ) {
|
---|
351 | nx_ = nx ;
|
---|
352 | ny_ = nx ;
|
---|
353 | }
|
---|
354 | //cout << "nx_ = " << nx_ << ", ny_ = " << ny_ << endl ;
|
---|
355 | if ( cellx.size() != 0 && celly.size() != 0 ) {
|
---|
356 | readQuantity( qcellx, cellx ) ;
|
---|
357 | readQuantity( qcelly, celly ) ;
|
---|
358 | }
|
---|
359 | else if ( celly.size() != 0 ) {
|
---|
360 | cout << "Using celly to x-axis..." << endl ;
|
---|
361 | readQuantity( qcelly, celly ) ;
|
---|
362 | qcellx = qcelly ;
|
---|
363 | }
|
---|
364 | else if ( cellx.size() != 0 ) {
|
---|
365 | cout << "Using cellx to y-axis..." << endl ;
|
---|
366 | readQuantity( qcellx, cellx ) ;
|
---|
367 | qcelly = qcellx ;
|
---|
368 | }
|
---|
369 | else {
|
---|
370 | if ( nx_ < 0 ) {
|
---|
371 | cout << "No user preference in grid setting. Using default..." << endl ;
|
---|
372 | readQuantity( qcellx, "1.0arcmin" ) ;
|
---|
373 | qcelly = qcellx ;
|
---|
374 | }
|
---|
375 | else {
|
---|
376 | qcellx = Quantum<Double>( wx/nx_, "rad" ) ;
|
---|
377 | qcelly = Quantum<Double>( wy/ny_, "rad" ) ;
|
---|
378 | }
|
---|
379 | }
|
---|
380 | cellx_ = qcellx.getValue( "rad" ) ;
|
---|
381 | celly_ = qcelly.getValue( "rad" ) ;
|
---|
382 | if ( nx_ < 0 ) {
|
---|
383 | nx_ = Int( ceil( wx/cellx_ ) ) ;
|
---|
384 | ny_ = Int( ceil( wy/celly_ ) ) ;
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | void STGrid::selectData( Table &tab )
|
---|
389 | {
|
---|
390 | Int ifno = ifno_ ;
|
---|
391 | Table taborg( infile_ ) ;
|
---|
392 | if ( ifno == -1 ) {
|
---|
393 | LogIO os( LogOrigin("STGrid","getData",WHERE) ) ;
|
---|
394 | // os << LogIO::SEVERE
|
---|
395 | // << "Please set IFNO before actual gridding"
|
---|
396 | // << LogIO::EXCEPTION ;
|
---|
397 | ROScalarColumn<uInt> ifnoCol( taborg, "IFNO" ) ;
|
---|
398 | ifno = ifnoCol( 0 ) ;
|
---|
399 | os << LogIO::WARN
|
---|
400 | << "IFNO is not given. Using default IFNO: " << ifno << LogIO::POST ;
|
---|
401 | }
|
---|
402 | tab = taborg( taborg.col("IFNO") == ifno ) ;
|
---|
403 | if ( tab.nrow() == 0 ) {
|
---|
404 | LogIO os( LogOrigin("STGrid","getData",WHERE) ) ;
|
---|
405 | os << LogIO::SEVERE
|
---|
406 | << "No corresponding rows for given IFNO: " << ifno
|
---|
407 | << LogIO::EXCEPTION ;
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | void STGrid::getData( Cube<Float> &spectra,
|
---|
412 | Matrix<Double> &direction,
|
---|
413 | Cube<uChar> &flagtra,
|
---|
414 | Matrix<uInt> &rflag,
|
---|
415 | Matrix<Float> &weight )
|
---|
416 | {
|
---|
417 | Table tab ;
|
---|
418 | selectData( tab ) ;
|
---|
419 | ROScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
|
---|
420 | Vector<uInt> pols = polnoCol.getColumn() ;
|
---|
421 | Vector<uInt> pollistOrg ;
|
---|
422 | uInt npolOrg = 0 ;
|
---|
423 | for ( uInt i = 0 ; i < pols.size() ; i++ ) {
|
---|
424 | if ( allNE( pollistOrg, pols[i] ) ) {
|
---|
425 | pollistOrg.resize( npolOrg+1, True ) ;
|
---|
426 | pollistOrg[npolOrg] = pols[i] ;
|
---|
427 | npolOrg++ ;
|
---|
428 | }
|
---|
429 | }
|
---|
430 | if ( pollist_.size() == 0 )
|
---|
431 | pollist_ = pollistOrg ;
|
---|
432 | else {
|
---|
433 | Vector<uInt> newlist ;
|
---|
434 | uInt newsize = 0 ;
|
---|
435 | for ( uInt i = 0 ; i < pollist_.size() ; i++ ) {
|
---|
436 | if ( anyEQ( pols, pollist_[i] ) ) {
|
---|
437 | newlist.resize( newsize+1, True ) ;
|
---|
438 | newlist[newsize] = pollist_[i] ;
|
---|
439 | newsize++ ;
|
---|
440 | }
|
---|
441 | }
|
---|
442 | pollist_ = newlist ;
|
---|
443 | }
|
---|
444 | npol_ = pollist_.size() ;
|
---|
445 | ROArrayColumn<uChar> tmpCol( tab, "FLAGTRA" ) ;
|
---|
446 | nchan_ = tmpCol( 0 ).nelements() ;
|
---|
447 | nrow_ = tab.nrow() / npolOrg ;
|
---|
448 | // cout << "npol_ = " << npol_ << endl ;
|
---|
449 | // cout << "nchan_ = " << nchan_ << endl ;
|
---|
450 | // cout << "nrow_ = " << nrow_ << endl ;
|
---|
451 | spectra.resize( npol_, nchan_, nrow_ ) ;
|
---|
452 | flagtra.resize( npol_, nchan_, nrow_ ) ;
|
---|
453 | rflag.resize( npol_, nrow_ ) ;
|
---|
454 | Cube<Float> tsys( npol_, nchan_, nrow_ ) ;
|
---|
455 | Matrix<Double> tint( npol_, nrow_ ) ;
|
---|
456 | for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
|
---|
457 | Table subt = tab( tab.col("POLNO") == pollist_[ipol] ) ;
|
---|
458 | ROArrayColumn<Float> spectraCol( subt, "SPECTRA" ) ;
|
---|
459 | ROArrayColumn<Double> directionCol( subt, "DIRECTION" ) ;
|
---|
460 | ROArrayColumn<uChar> flagtraCol( subt, "FLAGTRA" ) ;
|
---|
461 | ROScalarColumn<uInt> rflagCol( subt, "FLAGROW" ) ;
|
---|
462 | ROArrayColumn<Float> tsysCol( subt, "TSYS" ) ;
|
---|
463 | ROScalarColumn<Double> tintCol( subt, "INTERVAL" ) ;
|
---|
464 | Matrix<Float> tmpF = spectra.yzPlane( ipol ) ;
|
---|
465 | Matrix<uChar> tmpUC = flagtra.yzPlane( ipol ) ;
|
---|
466 | Vector<uInt> tmpUI = rflag.row( ipol ) ;
|
---|
467 | spectraCol.getColumn( tmpF ) ;
|
---|
468 | flagtraCol.getColumn( tmpUC ) ;
|
---|
469 | rflagCol.getColumn( tmpUI ) ;
|
---|
470 | if ( ipol == 0 )
|
---|
471 | directionCol.getColumn( direction ) ;
|
---|
472 | Matrix<Float> tmpF2 = tsysCol.getColumn() ;
|
---|
473 | Vector<Double> tmpD = tint.row( ipol ) ;
|
---|
474 | if ( tmpF2.shape()(0) == nchan_ ) {
|
---|
475 | tsys.yzPlane( ipol ) = tmpF2 ;
|
---|
476 | }
|
---|
477 | else {
|
---|
478 | tsys.yzPlane( ipol ) = tmpF2(0,0) ;
|
---|
479 | }
|
---|
480 | tintCol.getColumn( tmpD ) ;
|
---|
481 | }
|
---|
482 |
|
---|
483 | getWeight( weight, tsys, tint ) ;
|
---|
484 | }
|
---|
485 |
|
---|
486 | void STGrid::getWeight( Matrix<Float> &w,
|
---|
487 | Cube<Float> &tsys,
|
---|
488 | Matrix<Double> &tint )
|
---|
489 | {
|
---|
490 | // resize
|
---|
491 | w.resize( nchan_, nrow_ ) ;
|
---|
492 |
|
---|
493 | // set weight
|
---|
494 | w = 1.0 ;
|
---|
495 | Bool warn = False ;
|
---|
496 | if ( wtype_.compare( "UNIFORM" ) == 0 ) {
|
---|
497 | // do nothing
|
---|
498 | }
|
---|
499 | else if ( wtype_.compare( "TINT" ) == 0 ) {
|
---|
500 | if ( npol_ > 1 ) warn = True ;
|
---|
501 | for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
|
---|
502 | Float val = mean( tint.column( irow ) ) ;
|
---|
503 | w.column( irow ) = w.column( irow ) * val ;
|
---|
504 | }
|
---|
505 | }
|
---|
506 | else if ( wtype_.compare( "TSYS" ) == 0 ) {
|
---|
507 | if ( npol_ > 1 ) warn = True ;
|
---|
508 | for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
|
---|
509 | Matrix<Float> arr = tsys.xyPlane( irow ) ;
|
---|
510 | for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
|
---|
511 | Float val = mean( arr.column( ichan ) ) ;
|
---|
512 | w(ichan,irow) = w(ichan,irow) / ( val * val ) ;
|
---|
513 | }
|
---|
514 | }
|
---|
515 | }
|
---|
516 | else if ( wtype_.compare( "TINTSYS" ) == 0 ) {
|
---|
517 | if ( npol_ > 1 ) warn = True ;
|
---|
518 | for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
|
---|
519 | Float interval = mean( tint.column( irow ) ) ;
|
---|
520 | Matrix<Float> arr = tsys.xyPlane( irow ) ;
|
---|
521 | for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
|
---|
522 | Float temp = mean( arr.column( ichan ) ) ;
|
---|
523 | w(ichan,irow) = w(ichan,irow) * interval / ( temp * temp ) ;
|
---|
524 | }
|
---|
525 | }
|
---|
526 | }
|
---|
527 | else {
|
---|
528 | LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
|
---|
529 | os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
|
---|
530 | }
|
---|
531 |
|
---|
532 | if ( npol_ > 1 ) {
|
---|
533 | LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
|
---|
534 | os << LogIO::WARN << "STGrid doesn't support assigning polarization-dependent weight. Use averaged weight over polarization." << LogIO::POST ;
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | void STGrid::toInt( Array<uChar> *u, Array<Int> *v )
|
---|
539 | {
|
---|
540 | uInt len = u->nelements() ;
|
---|
541 | Int *int_p = new Int[len] ;
|
---|
542 | Bool deleteIt ;
|
---|
543 | const uChar *data_p = u->getStorage( deleteIt ) ;
|
---|
544 | Int *i_p = int_p ;
|
---|
545 | const uChar *u_p = data_p ;
|
---|
546 | for ( uInt i = 0 ; i < len ; i++ ) {
|
---|
547 | *i_p = ( *u_p == 0 ) ? 0 : 1 ;
|
---|
548 | i_p++ ;
|
---|
549 | u_p++ ;
|
---|
550 | }
|
---|
551 | u->freeStorage( data_p, deleteIt ) ;
|
---|
552 | v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
|
---|
553 | }
|
---|
554 |
|
---|
555 | void STGrid::toInt( Array<uInt> *u, Array<Int> *v )
|
---|
556 | {
|
---|
557 | uInt len = u->nelements() ;
|
---|
558 | Int *int_p = new Int[len] ;
|
---|
559 | Bool deleteIt ;
|
---|
560 | const uInt *data_p = u->getStorage( deleteIt ) ;
|
---|
561 | Int *i_p = int_p ;
|
---|
562 | const uInt *u_p = data_p ;
|
---|
563 | for ( uInt i = 0 ; i < len ; i++ ) {
|
---|
564 | *i_p = ( *u_p == 0 ) ? 0 : 1 ;
|
---|
565 | i_p++ ;
|
---|
566 | u_p++ ;
|
---|
567 | }
|
---|
568 | u->freeStorage( data_p, deleteIt ) ;
|
---|
569 | v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
|
---|
570 | }
|
---|
571 |
|
---|
572 | void STGrid::toPixel( Matrix<Double> &world, Matrix<Double> &pixel )
|
---|
573 | {
|
---|
574 | // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_)
|
---|
575 | // grid plane to avoid unexpected behavior on grid edge
|
---|
576 | Vector<Double> pixc( 2 ) ;
|
---|
577 | //pixc(0) = Double( nx_-1 ) * 0.5 ;
|
---|
578 | //pixc(1) = Double( ny_-1 ) * 0.5 ;
|
---|
579 | pixc(0) = Double( nx_+2*convSupport_-1 ) * 0.5 ;
|
---|
580 | pixc(1) = Double( ny_+2*convSupport_-1 ) * 0.5 ;
|
---|
581 | uInt nrow = world.shape()[1] ;
|
---|
582 | Vector<Double> cell( 2 ) ;
|
---|
583 | cell(0) = cellx_ ;
|
---|
584 | cell(1) = celly_ ;
|
---|
585 | //ofstream ofs( "grid.dat", ios::out ) ;
|
---|
586 | for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
|
---|
587 | //ofs << irow ;
|
---|
588 | for ( uInt i = 0 ; i < 2 ; i++ ) {
|
---|
589 | pixel( i, irow ) = pixc(i) + ( world(i, irow) - center_(i) ) / cell(i) ;
|
---|
590 | //ofs << " " << world(i, irow) << " " << pixel(i, irow) ;
|
---|
591 | }
|
---|
592 | //ofs << endl ;
|
---|
593 | }
|
---|
594 | //ofs.close() ;
|
---|
595 | }
|
---|
596 |
|
---|
597 | void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize )
|
---|
598 | {
|
---|
599 | convFunc = 0.0 ;
|
---|
600 | for ( Int i = 0 ; i < convSize/2 ; i++ )
|
---|
601 | convFunc(i) = 1.0 ;
|
---|
602 | }
|
---|
603 |
|
---|
604 | #define NEED_UNDERSCORES
|
---|
605 | #if defined(NEED_UNDERSCORES)
|
---|
606 | #define grdsf grdsf_
|
---|
607 | #endif
|
---|
608 | extern "C" {
|
---|
609 | void grdsf(Double*, Double*);
|
---|
610 | }
|
---|
611 | void STGrid::spheroidalFunc( Vector<Float> &convFunc )
|
---|
612 | {
|
---|
613 | convFunc = 0.0 ;
|
---|
614 | for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
|
---|
615 | Double nu = Double(i) / Double(convSupport_*convSampling_) ;
|
---|
616 | Double val ;
|
---|
617 | grdsf( &nu, &val ) ;
|
---|
618 | convFunc(i) = ( 1.0 - nu * nu ) * val ;
|
---|
619 | }
|
---|
620 | }
|
---|
621 |
|
---|
622 | void STGrid::gaussFunc( Vector<Float> &convFunc )
|
---|
623 | {
|
---|
624 | convFunc = 0.0 ;
|
---|
625 | // HWHM of the Gaussian is convSupport_ / 4
|
---|
626 | // To take into account Gaussian tail, kernel cutoff is set to 4 * HWHM
|
---|
627 | Int len = convSampling_ * convSupport_ ;
|
---|
628 | Double hwhm = len * 0.25 ;
|
---|
629 | for ( Int i = 0 ; i < len ; i++ ) {
|
---|
630 | Double val = Double(i) / hwhm ;
|
---|
631 | convFunc(i) = exp( -log(2)*val*val ) ;
|
---|
632 | }
|
---|
633 | }
|
---|
634 |
|
---|
635 | void STGrid::pbFunc( Vector<Float> &convFunc )
|
---|
636 | {
|
---|
637 | convFunc = 0.0 ;
|
---|
638 | }
|
---|
639 |
|
---|
640 | void STGrid::setConvFunc( Vector<Float> &convFunc )
|
---|
641 | {
|
---|
642 | convSupport_ = userSupport_ ;
|
---|
643 | if ( convType_ == "BOX" ) {
|
---|
644 | if ( convSupport_ < 0 )
|
---|
645 | convSupport_ = 0 ;
|
---|
646 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
647 | convFunc.resize( convSize ) ;
|
---|
648 | boxFunc( convFunc, convSize ) ;
|
---|
649 | }
|
---|
650 | else if ( convType_ == "SF" ) {
|
---|
651 | if ( convSupport_ < 0 )
|
---|
652 | convSupport_ = 3 ;
|
---|
653 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
654 | convFunc.resize( convSize ) ;
|
---|
655 | spheroidalFunc( convFunc ) ;
|
---|
656 | }
|
---|
657 | else if ( convType_ == "GAUSS" ) {
|
---|
658 | // to take into account Gaussian tail
|
---|
659 | if ( convSupport_ < 0 )
|
---|
660 | convSupport_ = 12 ; // 3 * 4
|
---|
661 | else {
|
---|
662 | convSupport_ = userSupport_ * 4 ;
|
---|
663 | }
|
---|
664 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
665 | convFunc.resize( convSize ) ;
|
---|
666 | gaussFunc( convFunc ) ;
|
---|
667 | }
|
---|
668 | else if ( convType_ == "PB" ) {
|
---|
669 | if ( convSupport_ < 0 )
|
---|
670 | convSupport_ = 0 ;
|
---|
671 | pbFunc( convFunc ) ;
|
---|
672 | }
|
---|
673 | else {
|
---|
674 | throw AipsError( "Unsupported convolution function" ) ;
|
---|
675 | }
|
---|
676 | }
|
---|
677 |
|
---|
678 | string STGrid::saveData( string outfile )
|
---|
679 | {
|
---|
680 | //Int polno = 0 ;
|
---|
681 | string outfile_ ;
|
---|
682 | if ( outfile.size() == 0 ) {
|
---|
683 | if ( infile_.lastchar() == '/' ) {
|
---|
684 | outfile_ = infile_.substr( 0, infile_.size()-1 ) ;
|
---|
685 | }
|
---|
686 | else {
|
---|
687 | outfile_ = infile_ ;
|
---|
688 | }
|
---|
689 | outfile_ += ".grid" ;
|
---|
690 | }
|
---|
691 | else {
|
---|
692 | outfile_ = outfile ;
|
---|
693 | }
|
---|
694 | CountedPtr<Scantable> ref( new Scantable( infile_, Table::Memory ) ) ;
|
---|
695 | //cout << "ref->nchan()=" << ref->nchan() << endl ;
|
---|
696 | CountedPtr<Scantable> out( new Scantable( *ref, True ) ) ;
|
---|
697 | Table tab = out->table() ;
|
---|
698 | IPosition dshape = data_.shape() ;
|
---|
699 | Int nrow = nx_ * ny_ * npol_ ;
|
---|
700 | tab.rwKeywordSet().define( "nPol", npol_ ) ;
|
---|
701 | tab.addRow( nrow ) ;
|
---|
702 | Vector<Double> cpix( 2 ) ;
|
---|
703 | cpix(0) = Double( nx_ - 1 ) * 0.5 ;
|
---|
704 | cpix(1) = Double( ny_ - 1 ) * 0.5 ;
|
---|
705 | Vector<Double> dir( 2 ) ;
|
---|
706 | ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
|
---|
707 | ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
|
---|
708 | ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
|
---|
709 | Int irow = 0 ;
|
---|
710 | for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
|
---|
711 | for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
|
---|
712 | for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
|
---|
713 | IPosition start( 4, ix, iy, ipol, 0 ) ;
|
---|
714 | IPosition end( 4, ix, iy, ipol, nchan_-1 ) ;
|
---|
715 | IPosition inc( 4, 1, 1, 1, 1 ) ;
|
---|
716 | Vector<Float> sp = data_( start, end, inc ) ;
|
---|
717 | dir(0) = center_(0) - ( cpix(0) - (Double)ix ) * cellx_ ;
|
---|
718 | dir(1) = center_(1) - ( cpix(1) - (Double)iy ) * celly_ ;
|
---|
719 | spectraCol.put( irow, sp ) ;
|
---|
720 | directionCol.put( irow, dir ) ;
|
---|
721 | polnoCol.put( irow, pollist_[ipol] ) ;
|
---|
722 | irow++ ;
|
---|
723 | }
|
---|
724 | }
|
---|
725 | }
|
---|
726 | //cout << "outfile_=" << outfile_ << endl ;
|
---|
727 | out->makePersistent( outfile_ ) ;
|
---|
728 |
|
---|
729 | return outfile_ ;
|
---|
730 | }
|
---|
731 |
|
---|
732 | }
|
---|