[2356] | 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>
|
---|
[2361] | 9 | #include <casa/Arrays/ArrayPartMath.h>
|
---|
[2356] | 10 | #include <casa/Quanta/Quantum.h>
|
---|
| 11 | #include <casa/Quanta/QuantumHolder.h>
|
---|
| 12 | #include <casa/Utilities/CountedPtr.h>
|
---|
[2361] | 13 | #include <casa/Logging/LogIO.h>
|
---|
[2356] | 14 |
|
---|
| 15 | #include <tables/Tables/Table.h>
|
---|
[2360] | 16 | #include <tables/Tables/TableRecord.h>
|
---|
| 17 | #include <tables/Tables/ExprNode.h>
|
---|
[2356] | 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 | {
|
---|
[2362] | 47 | ifno_ = -1 ;
|
---|
[2356] | 48 | nx_ = -1 ;
|
---|
| 49 | ny_ = -1 ;
|
---|
[2361] | 50 | npol_ = 0 ;
|
---|
| 51 | nchan_ = 0 ;
|
---|
| 52 | nrow_ = 0 ;
|
---|
[2356] | 53 | cellx_ = 0.0 ;
|
---|
| 54 | celly_ = 0.0 ;
|
---|
| 55 | center_ = Vector<Double> ( 2, 0.0 ) ;
|
---|
| 56 | convType_ = "BOX" ;
|
---|
[2361] | 57 | wtype_ = "UNIFORM" ;
|
---|
[2356] | 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 |
|
---|
[2360] | 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 |
|
---|
[2361] | 79 | void STGrid::setWeight( const string wType )
|
---|
| 80 | {
|
---|
| 81 | wtype_ = String( wType ) ;
|
---|
| 82 | wtype_.upcase() ;
|
---|
| 83 | cout << "wtype_ = " << wtype_ << endl ;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[2356] | 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 | {
|
---|
[2361] | 148 | LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ;
|
---|
[2356] | 149 |
|
---|
| 150 | // retrieve data
|
---|
[2360] | 151 | Cube<Float> spectra ;
|
---|
[2356] | 152 | Matrix<Double> direction ;
|
---|
[2360] | 153 | Cube<uChar> flagtra ;
|
---|
| 154 | Matrix<uInt> rflag ;
|
---|
[2361] | 155 | Matrix<Float> weight ;
|
---|
[2362] | 156 | getData( spectra, direction, flagtra, rflag, weight ) ;
|
---|
[2356] | 157 | IPosition sshape = spectra.shape() ;
|
---|
[2361] | 158 | //os << "spectra.shape()=" << spectra.shape() << LogIO::POST ;
|
---|
| 159 | //os << "max(spectra) = " << max(spectra) << LogIO::POST ;
|
---|
| 160 | //os << "weight = " << weight << LogIO::POST ;
|
---|
[2356] | 161 |
|
---|
| 162 | // flagtra: uChar -> Int
|
---|
| 163 | // rflag: uInt -> Int
|
---|
[2360] | 164 | Cube<Int> flagI ;
|
---|
| 165 | Matrix<Int> rflagI ;
|
---|
| 166 | toInt( &flagtra, &flagI ) ;
|
---|
| 167 | toInt( &rflag, &rflagI ) ;
|
---|
[2356] | 168 |
|
---|
| 169 | // grid parameter
|
---|
[2362] | 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 ;
|
---|
[2356] | 178 |
|
---|
[2359] | 179 | // convolution kernel
|
---|
| 180 | Vector<Float> convFunc ;
|
---|
| 181 | setConvFunc( convFunc ) ;
|
---|
| 182 | //cout << "convSupport=" << convSupport_ << endl ;
|
---|
| 183 | //cout << "convFunc=" << convFunc << endl ;
|
---|
| 184 |
|
---|
[2356] | 185 | // world -> pixel
|
---|
| 186 | Matrix<Double> xypos( direction.shape(), 0.0 ) ;
|
---|
| 187 | toPixel( direction, xypos ) ;
|
---|
| 188 |
|
---|
[2361] | 189 | // call ggridsd
|
---|
[2356] | 190 | Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ;
|
---|
| 191 | Double *xypos_p = xypos.getStorage( deletePos ) ;
|
---|
[2360] | 192 | Cube<Complex> dataC( spectra.shape(), 0.0 ) ;
|
---|
[2356] | 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 ) ;
|
---|
[2359] | 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 ;
|
---|
[2361] | 203 | IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
|
---|
[2356] | 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 ;
|
---|
[2361] | 210 | Int *chanMap = new Int[nchan_] ;
|
---|
[2356] | 211 | {
|
---|
| 212 | Int *work_p = chanMap ;
|
---|
[2361] | 213 | for ( Int i = 0 ; i < nchan_ ; i++ ) {
|
---|
[2356] | 214 | *work_p = i ;
|
---|
| 215 | work_p++ ;
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
[2361] | 218 | Int *polMap = new Int[npol_] ;
|
---|
[2356] | 219 | {
|
---|
| 220 | Int *work_p = polMap ;
|
---|
[2361] | 221 | for ( Int i = 0 ; i < npol_ ; i++ ) {
|
---|
[2356] | 222 | *work_p = i ;
|
---|
| 223 | work_p++ ;
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
[2361] | 226 | Double *sumw_p = new Double[npol_*nchan_] ;
|
---|
[2356] | 227 | {
|
---|
| 228 | Double *work_p = sumw_p ;
|
---|
[2361] | 229 | for ( Int i = 0 ; i < npol_*nchan_ ; i++ ) {
|
---|
[2356] | 230 | *work_p = 0.0 ;
|
---|
| 231 | work_p++ ;
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 | ggridsd( xypos_p,
|
---|
| 235 | data_p,
|
---|
[2361] | 236 | &npol_,
|
---|
| 237 | &nchan_,
|
---|
[2356] | 238 | &idopsf,
|
---|
| 239 | flag_p,
|
---|
| 240 | rflag_p,
|
---|
| 241 | wgt_p,
|
---|
[2361] | 242 | &nrow_,
|
---|
[2356] | 243 | &irow,
|
---|
| 244 | gdata_p,
|
---|
| 245 | wdata_p,
|
---|
[2359] | 246 | &gnx,
|
---|
| 247 | &gny,
|
---|
[2361] | 248 | &npol_,
|
---|
| 249 | &nchan_,
|
---|
[2356] | 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++ ) {
|
---|
[2361] | 271 | for ( Int ip = 0 ; ip < npol_ ; ip++ ) {
|
---|
| 272 | for ( Int ic = 0 ; ic < nchan_ ; ic++ ) {
|
---|
[2356] | 273 | IPosition pos( 4, ix, iy, ip, ic ) ;
|
---|
[2359] | 274 | IPosition gpos( 4, ix+convSupport_, iy+convSupport_, ip, ic ) ;
|
---|
| 275 | if ( gwgtArr( gpos ) > 0.0 )
|
---|
| 276 | data_( pos ) = gdataArr( gpos ) / gwgtArr( gpos ) ;
|
---|
[2356] | 277 | }
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
[2361] | 281 | //Matrix<Double> sumWeight( IPosition( 2, npol_, nchan_ ), sumw_p, TAKE_OVER ) ;
|
---|
| 282 | delete sumw_p ;
|
---|
[2356] | 283 | //cout << "sumWeight = " << sumWeight << endl ;
|
---|
| 284 | //cout << "gdataArr = " << gdataArr << endl ;
|
---|
| 285 | //cout << "gwgtArr = " << gwgtArr << endl ;
|
---|
[2359] | 286 | //cout << "data_ " << data_ << endl ;
|
---|
[2356] | 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 ;
|
---|
[2359] | 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
|
---|
[2356] | 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 |
|
---|
[2362] | 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,
|
---|
[2356] | 412 | Matrix<Double> &direction,
|
---|
[2360] | 413 | Cube<uChar> &flagtra,
|
---|
[2361] | 414 | Matrix<uInt> &rflag,
|
---|
| 415 | Matrix<Float> &weight )
|
---|
[2356] | 416 | {
|
---|
[2362] | 417 | Table tab ;
|
---|
| 418 | selectData( tab ) ;
|
---|
[2360] | 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 | }
|
---|
[2361] | 444 | npol_ = pollist_.size() ;
|
---|
[2360] | 445 | ROArrayColumn<uChar> tmpCol( tab, "FLAGTRA" ) ;
|
---|
[2361] | 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++ ) {
|
---|
[2360] | 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" ) ;
|
---|
[2361] | 462 | ROArrayColumn<Float> tsysCol( subt, "TSYS" ) ;
|
---|
| 463 | ROScalarColumn<Double> tintCol( subt, "INTERVAL" ) ;
|
---|
[2360] | 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 ) ;
|
---|
[2361] | 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 ) ;
|
---|
[2360] | 481 | }
|
---|
[2361] | 482 |
|
---|
| 483 | getWeight( weight, tsys, tint ) ;
|
---|
[2356] | 484 | }
|
---|
| 485 |
|
---|
[2361] | 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) ) ;
|
---|
[2362] | 534 | os << LogIO::WARN << "STGrid doesn't support assigning polarization-dependent weight. Use averaged weight over polarization." << LogIO::POST ;
|
---|
[2361] | 535 | }
|
---|
| 536 | }
|
---|
| 537 |
|
---|
[2360] | 538 | void STGrid::toInt( Array<uChar> *u, Array<Int> *v )
|
---|
[2356] | 539 | {
|
---|
[2360] | 540 | uInt len = u->nelements() ;
|
---|
[2356] | 541 | Int *int_p = new Int[len] ;
|
---|
| 542 | Bool deleteIt ;
|
---|
[2360] | 543 | const uChar *data_p = u->getStorage( deleteIt ) ;
|
---|
[2356] | 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 | }
|
---|
[2360] | 551 | u->freeStorage( data_p, deleteIt ) ;
|
---|
| 552 | v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
|
---|
[2356] | 553 | }
|
---|
| 554 |
|
---|
[2360] | 555 | void STGrid::toInt( Array<uInt> *u, Array<Int> *v )
|
---|
[2356] | 556 | {
|
---|
[2360] | 557 | uInt len = u->nelements() ;
|
---|
[2356] | 558 | Int *int_p = new Int[len] ;
|
---|
| 559 | Bool deleteIt ;
|
---|
[2360] | 560 | const uInt *data_p = u->getStorage( deleteIt ) ;
|
---|
[2356] | 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 | }
|
---|
[2360] | 568 | u->freeStorage( data_p, deleteIt ) ;
|
---|
| 569 | v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
|
---|
[2356] | 570 | }
|
---|
| 571 |
|
---|
| 572 | void STGrid::toPixel( Matrix<Double> &world, Matrix<Double> &pixel )
|
---|
| 573 | {
|
---|
[2359] | 574 | // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_)
|
---|
| 575 | // grid plane to avoid unexpected behavior on grid edge
|
---|
[2356] | 576 | Vector<Double> pixc( 2 ) ;
|
---|
[2359] | 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 ;
|
---|
[2356] | 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 | for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
|
---|
| 626 | Double hwhm = convSampling_ * convSupport_ * 0.25 ;
|
---|
| 627 | Double val = Double(i) / hwhm ;
|
---|
| 628 | convFunc(i) = exp( -log(2)*val*val ) ;
|
---|
| 629 | }
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | void STGrid::pbFunc( Vector<Float> &convFunc )
|
---|
| 633 | {
|
---|
| 634 | convFunc = 0.0 ;
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | void STGrid::setConvFunc( Vector<Float> &convFunc )
|
---|
| 638 | {
|
---|
| 639 | convSupport_ = userSupport_ ;
|
---|
| 640 | if ( convType_ == "BOX" ) {
|
---|
| 641 | if ( convSupport_ < 0 )
|
---|
| 642 | convSupport_ = 0 ;
|
---|
| 643 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
| 644 | convFunc.resize( convSize ) ;
|
---|
| 645 | boxFunc( convFunc, convSize ) ;
|
---|
| 646 | }
|
---|
| 647 | else if ( convType_ == "SF" ) {
|
---|
| 648 | if ( convSupport_ < 0 )
|
---|
| 649 | convSupport_ = 3 ;
|
---|
| 650 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
| 651 | convFunc.resize( convSize ) ;
|
---|
| 652 | spheroidalFunc( convFunc ) ;
|
---|
| 653 | }
|
---|
| 654 | else if ( convType_ == "GAUSS" ) {
|
---|
| 655 | if ( convSupport_ < 0 )
|
---|
| 656 | convSupport_ = 3 ;
|
---|
| 657 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
| 658 | convFunc.resize( convSize ) ;
|
---|
| 659 | gaussFunc( convFunc ) ;
|
---|
| 660 | }
|
---|
[2359] | 661 | else if ( convType_ == "PB" ) {
|
---|
| 662 | if ( convSupport_ < 0 )
|
---|
| 663 | convSupport_ = 0 ;
|
---|
[2356] | 664 | pbFunc( convFunc ) ;
|
---|
[2359] | 665 | }
|
---|
[2356] | 666 | else {
|
---|
| 667 | throw AipsError( "Unsupported convolution function" ) ;
|
---|
| 668 | }
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | string STGrid::saveData( string outfile )
|
---|
| 672 | {
|
---|
[2360] | 673 | //Int polno = 0 ;
|
---|
[2356] | 674 | string outfile_ ;
|
---|
| 675 | if ( outfile.size() == 0 ) {
|
---|
| 676 | if ( infile_.lastchar() == '/' ) {
|
---|
| 677 | outfile_ = infile_.substr( 0, infile_.size()-1 ) ;
|
---|
| 678 | }
|
---|
| 679 | else {
|
---|
| 680 | outfile_ = infile_ ;
|
---|
| 681 | }
|
---|
| 682 | outfile_ += ".grid" ;
|
---|
| 683 | }
|
---|
| 684 | else {
|
---|
| 685 | outfile_ = outfile ;
|
---|
| 686 | }
|
---|
| 687 | CountedPtr<Scantable> ref( new Scantable( infile_, Table::Memory ) ) ;
|
---|
| 688 | //cout << "ref->nchan()=" << ref->nchan() << endl ;
|
---|
| 689 | CountedPtr<Scantable> out( new Scantable( *ref, True ) ) ;
|
---|
| 690 | Table tab = out->table() ;
|
---|
| 691 | IPosition dshape = data_.shape() ;
|
---|
[2361] | 692 | Int nrow = nx_ * ny_ * npol_ ;
|
---|
| 693 | tab.rwKeywordSet().define( "nPol", npol_ ) ;
|
---|
[2360] | 694 | tab.addRow( nrow ) ;
|
---|
[2356] | 695 | Vector<Double> cpix( 2 ) ;
|
---|
| 696 | cpix(0) = Double( nx_ - 1 ) * 0.5 ;
|
---|
| 697 | cpix(1) = Double( ny_ - 1 ) * 0.5 ;
|
---|
| 698 | Vector<Double> dir( 2 ) ;
|
---|
| 699 | ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
|
---|
| 700 | ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
|
---|
| 701 | ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
|
---|
| 702 | Int irow = 0 ;
|
---|
| 703 | for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
|
---|
| 704 | for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
|
---|
[2361] | 705 | for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
|
---|
[2356] | 706 | IPosition start( 4, ix, iy, ipol, 0 ) ;
|
---|
[2361] | 707 | IPosition end( 4, ix, iy, ipol, nchan_-1 ) ;
|
---|
[2356] | 708 | IPosition inc( 4, 1, 1, 1, 1 ) ;
|
---|
| 709 | Vector<Float> sp = data_( start, end, inc ) ;
|
---|
| 710 | dir(0) = center_(0) - ( cpix(0) - (Double)ix ) * cellx_ ;
|
---|
| 711 | dir(1) = center_(1) - ( cpix(1) - (Double)iy ) * celly_ ;
|
---|
| 712 | spectraCol.put( irow, sp ) ;
|
---|
| 713 | directionCol.put( irow, dir ) ;
|
---|
[2360] | 714 | polnoCol.put( irow, pollist_[ipol] ) ;
|
---|
[2356] | 715 | irow++ ;
|
---|
| 716 | }
|
---|
| 717 | }
|
---|
| 718 | }
|
---|
| 719 | //cout << "outfile_=" << outfile_ << endl ;
|
---|
| 720 | out->makePersistent( outfile_ ) ;
|
---|
| 721 |
|
---|
| 722 | return outfile_ ;
|
---|
| 723 | }
|
---|
| 724 |
|
---|
| 725 | }
|
---|