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