[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 | {
|
---|
| 47 | nx_ = -1 ;
|
---|
| 48 | ny_ = -1 ;
|
---|
[2361] | 49 | npol_ = 0 ;
|
---|
| 50 | nchan_ = 0 ;
|
---|
| 51 | nrow_ = 0 ;
|
---|
| 52 | ngrid_ = 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 ;
|
---|
| 156 | getData( infile_, 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
|
---|
[2361] | 170 | // os << "----------" << endl ;
|
---|
| 171 | // os << "Grid parameter summary" << endl ;
|
---|
| 172 | // os << " (nx,ny) = (" << nx_ << "," << ny_ << ")" << endl ;
|
---|
| 173 | // os << " (cellx,celly) = (" << cellx_ << "," << celly_ << ")" << endl ;
|
---|
| 174 | // os << " center = " << center_ << endl ;
|
---|
| 175 | // os << "----------" << LogIO::POST ;
|
---|
[2356] | 176 |
|
---|
[2359] | 177 | // convolution kernel
|
---|
| 178 | Vector<Float> convFunc ;
|
---|
| 179 | setConvFunc( convFunc ) ;
|
---|
| 180 | //cout << "convSupport=" << convSupport_ << endl ;
|
---|
| 181 | //cout << "convFunc=" << convFunc << endl ;
|
---|
| 182 |
|
---|
[2356] | 183 | // world -> pixel
|
---|
| 184 | Matrix<Double> xypos( direction.shape(), 0.0 ) ;
|
---|
| 185 | toPixel( direction, xypos ) ;
|
---|
| 186 |
|
---|
[2361] | 187 | // call ggridsd
|
---|
[2356] | 188 | Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ;
|
---|
| 189 | Double *xypos_p = xypos.getStorage( deletePos ) ;
|
---|
[2360] | 190 | Cube<Complex> dataC( spectra.shape(), 0.0 ) ;
|
---|
[2356] | 191 | setReal( dataC, spectra ) ;
|
---|
| 192 | const Complex *data_p = dataC.getStorage( deleteData ) ;
|
---|
| 193 | const Float *wgt_p = weight.getStorage( deleteWgt ) ;
|
---|
| 194 | const Int *flag_p = flagI.getStorage( deleteFlag ) ;
|
---|
| 195 | const Int *rflag_p = rflagI.getStorage( deleteFlagR ) ;
|
---|
| 196 | Float *conv_p = convFunc.getStorage( deleteConv ) ;
|
---|
[2359] | 197 | // Extend grid plane with convSupport_
|
---|
| 198 | //IPosition gshape( 4, nx_, ny_, npol, nchan ) ;
|
---|
| 199 | Int gnx = nx_+convSupport_*2 ;
|
---|
| 200 | Int gny = ny_+convSupport_*2 ;
|
---|
[2361] | 201 | IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ;
|
---|
[2356] | 202 | Array<Complex> gdataArrC( gshape, 0.0 ) ;
|
---|
| 203 | Array<Float> gwgtArr( gshape, 0.0 ) ;
|
---|
| 204 | Complex *gdata_p = gdataArrC.getStorage( deleteDataG ) ;
|
---|
| 205 | Float *wdata_p = gwgtArr.getStorage( deleteWgtG ) ;
|
---|
| 206 | Int idopsf = 0 ;
|
---|
| 207 | Int irow = -1 ;
|
---|
[2361] | 208 | Int *chanMap = new Int[nchan_] ;
|
---|
[2356] | 209 | {
|
---|
| 210 | Int *work_p = chanMap ;
|
---|
[2361] | 211 | for ( Int i = 0 ; i < nchan_ ; i++ ) {
|
---|
[2356] | 212 | *work_p = i ;
|
---|
| 213 | work_p++ ;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
[2361] | 216 | Int *polMap = new Int[npol_] ;
|
---|
[2356] | 217 | {
|
---|
| 218 | Int *work_p = polMap ;
|
---|
[2361] | 219 | for ( Int i = 0 ; i < npol_ ; i++ ) {
|
---|
[2356] | 220 | *work_p = i ;
|
---|
| 221 | work_p++ ;
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
[2361] | 224 | Double *sumw_p = new Double[npol_*nchan_] ;
|
---|
[2356] | 225 | {
|
---|
| 226 | Double *work_p = sumw_p ;
|
---|
[2361] | 227 | for ( Int i = 0 ; i < npol_*nchan_ ; i++ ) {
|
---|
[2356] | 228 | *work_p = 0.0 ;
|
---|
| 229 | work_p++ ;
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | ggridsd( xypos_p,
|
---|
| 233 | data_p,
|
---|
[2361] | 234 | &npol_,
|
---|
| 235 | &nchan_,
|
---|
[2356] | 236 | &idopsf,
|
---|
| 237 | flag_p,
|
---|
| 238 | rflag_p,
|
---|
| 239 | wgt_p,
|
---|
[2361] | 240 | &nrow_,
|
---|
[2356] | 241 | &irow,
|
---|
| 242 | gdata_p,
|
---|
| 243 | wdata_p,
|
---|
[2359] | 244 | &gnx,
|
---|
| 245 | &gny,
|
---|
[2361] | 246 | &npol_,
|
---|
| 247 | &nchan_,
|
---|
[2356] | 248 | &convSupport_,
|
---|
| 249 | &convSampling_,
|
---|
| 250 | conv_p,
|
---|
| 251 | chanMap,
|
---|
| 252 | polMap,
|
---|
| 253 | sumw_p ) ;
|
---|
| 254 | xypos.putStorage( xypos_p, deletePos ) ;
|
---|
| 255 | dataC.freeStorage( data_p, deleteData ) ;
|
---|
| 256 | weight.freeStorage( wgt_p, deleteWgt ) ;
|
---|
| 257 | flagI.freeStorage( flag_p, deleteFlag ) ;
|
---|
| 258 | rflagI.freeStorage( rflag_p, deleteFlagR ) ;
|
---|
| 259 | convFunc.putStorage( conv_p, deleteConv ) ;
|
---|
| 260 | delete polMap ;
|
---|
| 261 | delete chanMap ;
|
---|
| 262 | gdataArrC.putStorage( gdata_p, deleteDataG ) ;
|
---|
| 263 | gwgtArr.putStorage( wdata_p, deleteWgtG ) ;
|
---|
| 264 | Array<Float> gdataArr = real( gdataArrC ) ;
|
---|
| 265 | data_.resize( gdataArr.shape() ) ;
|
---|
| 266 | data_ = 0.0 ;
|
---|
| 267 | for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
|
---|
| 268 | for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
|
---|
[2361] | 269 | for ( Int ip = 0 ; ip < npol_ ; ip++ ) {
|
---|
| 270 | for ( Int ic = 0 ; ic < nchan_ ; ic++ ) {
|
---|
[2356] | 271 | IPosition pos( 4, ix, iy, ip, ic ) ;
|
---|
[2359] | 272 | IPosition gpos( 4, ix+convSupport_, iy+convSupport_, ip, ic ) ;
|
---|
| 273 | if ( gwgtArr( gpos ) > 0.0 )
|
---|
| 274 | data_( pos ) = gdataArr( gpos ) / gwgtArr( gpos ) ;
|
---|
[2356] | 275 | }
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
[2361] | 279 | //Matrix<Double> sumWeight( IPosition( 2, npol_, nchan_ ), sumw_p, TAKE_OVER ) ;
|
---|
| 280 | delete sumw_p ;
|
---|
[2356] | 281 | //cout << "sumWeight = " << sumWeight << endl ;
|
---|
| 282 | //cout << "gdataArr = " << gdataArr << endl ;
|
---|
| 283 | //cout << "gwgtArr = " << gwgtArr << endl ;
|
---|
[2359] | 284 | //cout << "data_ " << data_ << endl ;
|
---|
[2356] | 285 | }
|
---|
| 286 |
|
---|
| 287 | void STGrid::setupGrid( Int &nx,
|
---|
| 288 | Int &ny,
|
---|
| 289 | String &cellx,
|
---|
| 290 | String &celly,
|
---|
| 291 | Double &xmin,
|
---|
| 292 | Double &xmax,
|
---|
| 293 | Double &ymin,
|
---|
| 294 | Double &ymax,
|
---|
| 295 | String ¢er )
|
---|
| 296 | {
|
---|
| 297 | //cout << "nx=" << nx << ", ny=" << ny << endl ;
|
---|
[2359] | 298 |
|
---|
| 299 | // center position
|
---|
| 300 | if ( center.size() == 0 ) {
|
---|
| 301 | center_(0) = 0.5 * ( xmin + xmax ) ;
|
---|
| 302 | center_(1) = 0.5 * ( ymin + ymax ) ;
|
---|
| 303 | }
|
---|
| 304 | else {
|
---|
| 305 | String::size_type pos0 = center.find( " " ) ;
|
---|
| 306 | if ( pos0 == String::npos ) {
|
---|
| 307 | throw AipsError( "bad string format in parameter center" ) ;
|
---|
| 308 | }
|
---|
| 309 | String::size_type pos1 = center.find( " ", pos0+1 ) ;
|
---|
| 310 | String typestr, xstr, ystr ;
|
---|
| 311 | if ( pos1 != String::npos ) {
|
---|
| 312 | typestr = center.substr( 0, pos0 ) ;
|
---|
| 313 | xstr = center.substr( pos0+1, pos1-pos0 ) ;
|
---|
| 314 | ystr = center.substr( pos1+1 ) ;
|
---|
| 315 | // todo: convert to J2000 (or direction ref for DIRECTION column)
|
---|
| 316 | }
|
---|
| 317 | else {
|
---|
| 318 | typestr = "J2000" ;
|
---|
| 319 | xstr = center.substr( 0, pos0 ) ;
|
---|
| 320 | ystr = center.substr( pos0+1 ) ;
|
---|
| 321 | }
|
---|
| 322 | QuantumHolder qh ;
|
---|
| 323 | String err ;
|
---|
| 324 | qh.fromString( err, xstr ) ;
|
---|
| 325 | Quantum<Double> xcen = qh.asQuantumDouble() ;
|
---|
| 326 | qh.fromString( err, ystr ) ;
|
---|
| 327 | Quantum<Double> ycen = qh.asQuantumDouble() ;
|
---|
| 328 | center_(0) = xcen.getValue( "rad" ) ;
|
---|
| 329 | center_(1) = ycen.getValue( "rad" ) ;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 |
|
---|
| 333 | //Double wx = xmax - xmin ;
|
---|
| 334 | //Double wy = ymax - ymin ;
|
---|
| 335 | Double wx = max( abs(xmax-center_(0)), abs(xmin-center_(0)) ) * 2 ;
|
---|
| 336 | Double wy = max( abs(ymax-center_(1)), abs(ymin-center_(1)) ) * 2 ;
|
---|
| 337 | // take 10% margin
|
---|
[2356] | 338 | wx *= 1.10 ;
|
---|
| 339 | wy *= 1.10 ;
|
---|
| 340 | Quantum<Double> qcellx ;
|
---|
| 341 | Quantum<Double> qcelly ;
|
---|
| 342 | nx_ = nx ;
|
---|
| 343 | ny_ = ny ;
|
---|
| 344 | if ( nx < 0 && ny > 0 ) {
|
---|
| 345 | nx_ = ny ;
|
---|
| 346 | ny_ = ny ;
|
---|
| 347 | }
|
---|
| 348 | if ( ny < 0 && nx > 0 ) {
|
---|
| 349 | nx_ = nx ;
|
---|
| 350 | ny_ = nx ;
|
---|
| 351 | }
|
---|
| 352 | //cout << "nx_ = " << nx_ << ", ny_ = " << ny_ << endl ;
|
---|
| 353 | if ( cellx.size() != 0 && celly.size() != 0 ) {
|
---|
| 354 | readQuantity( qcellx, cellx ) ;
|
---|
| 355 | readQuantity( qcelly, celly ) ;
|
---|
| 356 | }
|
---|
| 357 | else if ( celly.size() != 0 ) {
|
---|
| 358 | cout << "Using celly to x-axis..." << endl ;
|
---|
| 359 | readQuantity( qcelly, celly ) ;
|
---|
| 360 | qcellx = qcelly ;
|
---|
| 361 | }
|
---|
| 362 | else if ( cellx.size() != 0 ) {
|
---|
| 363 | cout << "Using cellx to y-axis..." << endl ;
|
---|
| 364 | readQuantity( qcellx, cellx ) ;
|
---|
| 365 | qcelly = qcellx ;
|
---|
| 366 | }
|
---|
| 367 | else {
|
---|
| 368 | if ( nx_ < 0 ) {
|
---|
| 369 | cout << "No user preference in grid setting. Using default..." << endl ;
|
---|
| 370 | readQuantity( qcellx, "1.0arcmin" ) ;
|
---|
| 371 | qcelly = qcellx ;
|
---|
| 372 | }
|
---|
| 373 | else {
|
---|
| 374 | qcellx = Quantum<Double>( wx/nx_, "rad" ) ;
|
---|
| 375 | qcelly = Quantum<Double>( wy/ny_, "rad" ) ;
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
| 378 | cellx_ = qcellx.getValue( "rad" ) ;
|
---|
| 379 | celly_ = qcelly.getValue( "rad" ) ;
|
---|
| 380 | if ( nx_ < 0 ) {
|
---|
| 381 | nx_ = Int( ceil( wx/cellx_ ) ) ;
|
---|
| 382 | ny_ = Int( ceil( wy/celly_ ) ) ;
|
---|
| 383 | }
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | void STGrid::getData( String &infile,
|
---|
[2360] | 387 | Cube<Float> &spectra,
|
---|
[2356] | 388 | Matrix<Double> &direction,
|
---|
[2360] | 389 | Cube<uChar> &flagtra,
|
---|
[2361] | 390 | Matrix<uInt> &rflag,
|
---|
| 391 | Matrix<Float> &weight )
|
---|
[2356] | 392 | {
|
---|
| 393 | Table tab( infile ) ;
|
---|
[2360] | 394 | //uInt npol = tab.keywordSet().asuInt( "nPol" ) ;
|
---|
| 395 | ROScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
|
---|
| 396 | //uInt npol = max( polnoCol.getColumn() ) + 1 ;
|
---|
| 397 | Vector<uInt> pols = polnoCol.getColumn() ;
|
---|
| 398 | Vector<uInt> pollistOrg ;
|
---|
| 399 | uInt npolOrg = 0 ;
|
---|
| 400 | for ( uInt i = 0 ; i < pols.size() ; i++ ) {
|
---|
| 401 | if ( allNE( pollistOrg, pols[i] ) ) {
|
---|
| 402 | pollistOrg.resize( npolOrg+1, True ) ;
|
---|
| 403 | pollistOrg[npolOrg] = pols[i] ;
|
---|
| 404 | npolOrg++ ;
|
---|
| 405 | }
|
---|
| 406 | }
|
---|
| 407 | if ( pollist_.size() == 0 )
|
---|
| 408 | pollist_ = pollistOrg ;
|
---|
| 409 | else {
|
---|
| 410 | Vector<uInt> newlist ;
|
---|
| 411 | uInt newsize = 0 ;
|
---|
| 412 | for ( uInt i = 0 ; i < pollist_.size() ; i++ ) {
|
---|
| 413 | if ( anyEQ( pols, pollist_[i] ) ) {
|
---|
| 414 | newlist.resize( newsize+1, True ) ;
|
---|
| 415 | newlist[newsize] = pollist_[i] ;
|
---|
| 416 | newsize++ ;
|
---|
| 417 | }
|
---|
| 418 | }
|
---|
| 419 | pollist_ = newlist ;
|
---|
| 420 | }
|
---|
[2361] | 421 | npol_ = pollist_.size() ;
|
---|
[2360] | 422 | ROArrayColumn<uChar> tmpCol( tab, "FLAGTRA" ) ;
|
---|
[2361] | 423 | nchan_ = tmpCol( 0 ).nelements() ;
|
---|
| 424 | nrow_ = tab.nrow() / npolOrg ;
|
---|
| 425 | // cout << "npol_ = " << npol_ << endl ;
|
---|
| 426 | // cout << "nchan_ = " << nchan_ << endl ;
|
---|
| 427 | // cout << "nrow_ = " << nrow_ << endl ;
|
---|
| 428 | spectra.resize( npol_, nchan_, nrow_ ) ;
|
---|
| 429 | flagtra.resize( npol_, nchan_, nrow_ ) ;
|
---|
| 430 | rflag.resize( npol_, nrow_ ) ;
|
---|
| 431 | Cube<Float> tsys( npol_, nchan_, nrow_ ) ;
|
---|
| 432 | Matrix<Double> tint( npol_, nrow_ ) ;
|
---|
| 433 | for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
|
---|
[2360] | 434 | Table subt = tab( tab.col("POLNO") == pollist_[ipol] ) ;
|
---|
| 435 | ROArrayColumn<Float> spectraCol( subt, "SPECTRA" ) ;
|
---|
| 436 | ROArrayColumn<Double> directionCol( subt, "DIRECTION" ) ;
|
---|
| 437 | ROArrayColumn<uChar> flagtraCol( subt, "FLAGTRA" ) ;
|
---|
| 438 | ROScalarColumn<uInt> rflagCol( subt, "FLAGROW" ) ;
|
---|
[2361] | 439 | ROArrayColumn<Float> tsysCol( subt, "TSYS" ) ;
|
---|
| 440 | ROScalarColumn<Double> tintCol( subt, "INTERVAL" ) ;
|
---|
[2360] | 441 | Matrix<Float> tmpF = spectra.yzPlane( ipol ) ;
|
---|
| 442 | Matrix<uChar> tmpUC = flagtra.yzPlane( ipol ) ;
|
---|
| 443 | Vector<uInt> tmpUI = rflag.row( ipol ) ;
|
---|
| 444 | spectraCol.getColumn( tmpF ) ;
|
---|
| 445 | flagtraCol.getColumn( tmpUC ) ;
|
---|
| 446 | rflagCol.getColumn( tmpUI ) ;
|
---|
| 447 | if ( ipol == 0 )
|
---|
| 448 | directionCol.getColumn( direction ) ;
|
---|
[2361] | 449 | Matrix<Float> tmpF2 = tsysCol.getColumn() ;
|
---|
| 450 | Vector<Double> tmpD = tint.row( ipol ) ;
|
---|
| 451 | if ( tmpF2.shape()(0) == nchan_ ) {
|
---|
| 452 | tsys.yzPlane( ipol ) = tmpF2 ;
|
---|
| 453 | }
|
---|
| 454 | else {
|
---|
| 455 | tsys.yzPlane( ipol ) = tmpF2(0,0) ;
|
---|
| 456 | }
|
---|
| 457 | tintCol.getColumn( tmpD ) ;
|
---|
[2360] | 458 | }
|
---|
[2361] | 459 |
|
---|
| 460 | getWeight( weight, tsys, tint ) ;
|
---|
[2356] | 461 | }
|
---|
| 462 |
|
---|
[2361] | 463 | void STGrid::getWeight( Matrix<Float> &w,
|
---|
| 464 | Cube<Float> &tsys,
|
---|
| 465 | Matrix<Double> &tint )
|
---|
| 466 | {
|
---|
| 467 | // resize
|
---|
| 468 | w.resize( nchan_, nrow_ ) ;
|
---|
| 469 |
|
---|
| 470 | // set weight
|
---|
| 471 | w = 1.0 ;
|
---|
| 472 | Bool warn = False ;
|
---|
| 473 | if ( wtype_.compare( "UNIFORM" ) == 0 ) {
|
---|
| 474 | // do nothing
|
---|
| 475 | }
|
---|
| 476 | else if ( wtype_.compare( "TINT" ) == 0 ) {
|
---|
| 477 | if ( npol_ > 1 ) warn = True ;
|
---|
| 478 | for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
|
---|
| 479 | Float val = mean( tint.column( irow ) ) ;
|
---|
| 480 | w.column( irow ) = w.column( irow ) * val ;
|
---|
| 481 | }
|
---|
| 482 | }
|
---|
| 483 | else if ( wtype_.compare( "TSYS" ) == 0 ) {
|
---|
| 484 | if ( npol_ > 1 ) warn = True ;
|
---|
| 485 | for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
|
---|
| 486 | Matrix<Float> arr = tsys.xyPlane( irow ) ;
|
---|
| 487 | for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
|
---|
| 488 | Float val = mean( arr.column( ichan ) ) ;
|
---|
| 489 | w(ichan,irow) = w(ichan,irow) / ( val * val ) ;
|
---|
| 490 | }
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 | else if ( wtype_.compare( "TINTSYS" ) == 0 ) {
|
---|
| 494 | if ( npol_ > 1 ) warn = True ;
|
---|
| 495 | for ( Int irow = 0 ; irow < nrow_ ; irow++ ) {
|
---|
| 496 | Float interval = mean( tint.column( irow ) ) ;
|
---|
| 497 | Matrix<Float> arr = tsys.xyPlane( irow ) ;
|
---|
| 498 | for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) {
|
---|
| 499 | Float temp = mean( arr.column( ichan ) ) ;
|
---|
| 500 | w(ichan,irow) = w(ichan,irow) * interval / ( temp * temp ) ;
|
---|
| 501 | }
|
---|
| 502 | }
|
---|
| 503 | }
|
---|
| 504 | else {
|
---|
| 505 | LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
|
---|
| 506 | os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | if ( npol_ > 1 ) {
|
---|
| 510 | LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ;
|
---|
| 511 | os << LogIO::WARN << "STGrid doesn't support assigning independent polarization-dependent weight. Use averaged weight over polarization." << LogIO::POST ;
|
---|
| 512 | }
|
---|
| 513 | }
|
---|
| 514 |
|
---|
[2360] | 515 | void STGrid::toInt( Array<uChar> *u, Array<Int> *v )
|
---|
[2356] | 516 | {
|
---|
[2360] | 517 | uInt len = u->nelements() ;
|
---|
[2356] | 518 | Int *int_p = new Int[len] ;
|
---|
| 519 | Bool deleteIt ;
|
---|
[2360] | 520 | const uChar *data_p = u->getStorage( deleteIt ) ;
|
---|
[2356] | 521 | Int *i_p = int_p ;
|
---|
| 522 | const uChar *u_p = data_p ;
|
---|
| 523 | for ( uInt i = 0 ; i < len ; i++ ) {
|
---|
| 524 | *i_p = ( *u_p == 0 ) ? 0 : 1 ;
|
---|
| 525 | i_p++ ;
|
---|
| 526 | u_p++ ;
|
---|
| 527 | }
|
---|
[2360] | 528 | u->freeStorage( data_p, deleteIt ) ;
|
---|
| 529 | v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
|
---|
[2356] | 530 | }
|
---|
| 531 |
|
---|
[2360] | 532 | void STGrid::toInt( Array<uInt> *u, Array<Int> *v )
|
---|
[2356] | 533 | {
|
---|
[2360] | 534 | uInt len = u->nelements() ;
|
---|
[2356] | 535 | Int *int_p = new Int[len] ;
|
---|
| 536 | Bool deleteIt ;
|
---|
[2360] | 537 | const uInt *data_p = u->getStorage( deleteIt ) ;
|
---|
[2356] | 538 | Int *i_p = int_p ;
|
---|
| 539 | const uInt *u_p = data_p ;
|
---|
| 540 | for ( uInt i = 0 ; i < len ; i++ ) {
|
---|
| 541 | *i_p = ( *u_p == 0 ) ? 0 : 1 ;
|
---|
| 542 | i_p++ ;
|
---|
| 543 | u_p++ ;
|
---|
| 544 | }
|
---|
[2360] | 545 | u->freeStorage( data_p, deleteIt ) ;
|
---|
| 546 | v->takeStorage( u->shape(), int_p, TAKE_OVER ) ;
|
---|
[2356] | 547 | }
|
---|
| 548 |
|
---|
| 549 | void STGrid::toPixel( Matrix<Double> &world, Matrix<Double> &pixel )
|
---|
| 550 | {
|
---|
[2359] | 551 | // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_)
|
---|
| 552 | // grid plane to avoid unexpected behavior on grid edge
|
---|
[2356] | 553 | Vector<Double> pixc( 2 ) ;
|
---|
[2359] | 554 | //pixc(0) = Double( nx_-1 ) * 0.5 ;
|
---|
| 555 | //pixc(1) = Double( ny_-1 ) * 0.5 ;
|
---|
| 556 | pixc(0) = Double( nx_+2*convSupport_-1 ) * 0.5 ;
|
---|
| 557 | pixc(1) = Double( ny_+2*convSupport_-1 ) * 0.5 ;
|
---|
[2356] | 558 | uInt nrow = world.shape()[1] ;
|
---|
| 559 | Vector<Double> cell( 2 ) ;
|
---|
| 560 | cell(0) = cellx_ ;
|
---|
| 561 | cell(1) = celly_ ;
|
---|
| 562 | //ofstream ofs( "grid.dat", ios::out ) ;
|
---|
| 563 | for ( uInt irow = 0 ; irow < nrow ; irow++ ) {
|
---|
| 564 | //ofs << irow ;
|
---|
| 565 | for ( uInt i = 0 ; i < 2 ; i++ ) {
|
---|
| 566 | pixel( i, irow ) = pixc(i) + ( world(i, irow) - center_(i) ) / cell(i) ;
|
---|
| 567 | //ofs << " " << world(i, irow) << " " << pixel(i, irow) ;
|
---|
| 568 | }
|
---|
| 569 | //ofs << endl ;
|
---|
| 570 | }
|
---|
| 571 | //ofs.close() ;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize )
|
---|
| 575 | {
|
---|
| 576 | convFunc = 0.0 ;
|
---|
| 577 | for ( Int i = 0 ; i < convSize/2 ; i++ )
|
---|
| 578 | convFunc(i) = 1.0 ;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | #define NEED_UNDERSCORES
|
---|
| 582 | #if defined(NEED_UNDERSCORES)
|
---|
| 583 | #define grdsf grdsf_
|
---|
| 584 | #endif
|
---|
| 585 | extern "C" {
|
---|
| 586 | void grdsf(Double*, Double*);
|
---|
| 587 | }
|
---|
| 588 | void STGrid::spheroidalFunc( Vector<Float> &convFunc )
|
---|
| 589 | {
|
---|
| 590 | convFunc = 0.0 ;
|
---|
| 591 | for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
|
---|
| 592 | Double nu = Double(i) / Double(convSupport_*convSampling_) ;
|
---|
| 593 | Double val ;
|
---|
| 594 | grdsf( &nu, &val ) ;
|
---|
| 595 | convFunc(i) = ( 1.0 - nu * nu ) * val ;
|
---|
| 596 | }
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | void STGrid::gaussFunc( Vector<Float> &convFunc )
|
---|
| 600 | {
|
---|
| 601 | convFunc = 0.0 ;
|
---|
| 602 | for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) {
|
---|
| 603 | Double hwhm = convSampling_ * convSupport_ * 0.25 ;
|
---|
| 604 | Double val = Double(i) / hwhm ;
|
---|
| 605 | convFunc(i) = exp( -log(2)*val*val ) ;
|
---|
| 606 | }
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | void STGrid::pbFunc( Vector<Float> &convFunc )
|
---|
| 610 | {
|
---|
| 611 | convFunc = 0.0 ;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | void STGrid::setConvFunc( Vector<Float> &convFunc )
|
---|
| 615 | {
|
---|
| 616 | convSupport_ = userSupport_ ;
|
---|
| 617 | if ( convType_ == "BOX" ) {
|
---|
| 618 | if ( convSupport_ < 0 )
|
---|
| 619 | convSupport_ = 0 ;
|
---|
| 620 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
| 621 | convFunc.resize( convSize ) ;
|
---|
| 622 | boxFunc( convFunc, convSize ) ;
|
---|
| 623 | }
|
---|
| 624 | else if ( convType_ == "SF" ) {
|
---|
| 625 | if ( convSupport_ < 0 )
|
---|
| 626 | convSupport_ = 3 ;
|
---|
| 627 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
| 628 | convFunc.resize( convSize ) ;
|
---|
| 629 | spheroidalFunc( convFunc ) ;
|
---|
| 630 | }
|
---|
| 631 | else if ( convType_ == "GAUSS" ) {
|
---|
| 632 | if ( convSupport_ < 0 )
|
---|
| 633 | convSupport_ = 3 ;
|
---|
| 634 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ;
|
---|
| 635 | convFunc.resize( convSize ) ;
|
---|
| 636 | gaussFunc( convFunc ) ;
|
---|
| 637 | }
|
---|
[2359] | 638 | else if ( convType_ == "PB" ) {
|
---|
| 639 | if ( convSupport_ < 0 )
|
---|
| 640 | convSupport_ = 0 ;
|
---|
[2356] | 641 | pbFunc( convFunc ) ;
|
---|
[2359] | 642 | }
|
---|
[2356] | 643 | else {
|
---|
| 644 | throw AipsError( "Unsupported convolution function" ) ;
|
---|
| 645 | }
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | string STGrid::saveData( string outfile )
|
---|
| 649 | {
|
---|
[2360] | 650 | //Int polno = 0 ;
|
---|
[2356] | 651 | string outfile_ ;
|
---|
| 652 | if ( outfile.size() == 0 ) {
|
---|
| 653 | if ( infile_.lastchar() == '/' ) {
|
---|
| 654 | outfile_ = infile_.substr( 0, infile_.size()-1 ) ;
|
---|
| 655 | }
|
---|
| 656 | else {
|
---|
| 657 | outfile_ = infile_ ;
|
---|
| 658 | }
|
---|
| 659 | outfile_ += ".grid" ;
|
---|
| 660 | }
|
---|
| 661 | else {
|
---|
| 662 | outfile_ = outfile ;
|
---|
| 663 | }
|
---|
| 664 | CountedPtr<Scantable> ref( new Scantable( infile_, Table::Memory ) ) ;
|
---|
| 665 | //cout << "ref->nchan()=" << ref->nchan() << endl ;
|
---|
| 666 | CountedPtr<Scantable> out( new Scantable( *ref, True ) ) ;
|
---|
| 667 | Table tab = out->table() ;
|
---|
| 668 | IPosition dshape = data_.shape() ;
|
---|
[2361] | 669 | //Int npol = dshape[2] ;
|
---|
| 670 | //Int nchan = dshape[3] ;
|
---|
| 671 | Int nrow = nx_ * ny_ * npol_ ;
|
---|
| 672 | tab.rwKeywordSet().define( "nPol", npol_ ) ;
|
---|
[2360] | 673 | tab.addRow( nrow ) ;
|
---|
[2356] | 674 | Vector<Double> cpix( 2 ) ;
|
---|
| 675 | cpix(0) = Double( nx_ - 1 ) * 0.5 ;
|
---|
| 676 | cpix(1) = Double( ny_ - 1 ) * 0.5 ;
|
---|
| 677 | Vector<Double> dir( 2 ) ;
|
---|
| 678 | ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ;
|
---|
| 679 | ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ;
|
---|
| 680 | ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ;
|
---|
| 681 | Int irow = 0 ;
|
---|
| 682 | for ( Int iy = 0 ; iy < ny_ ; iy++ ) {
|
---|
| 683 | for ( Int ix = 0 ; ix < nx_ ; ix++ ) {
|
---|
[2361] | 684 | for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) {
|
---|
[2356] | 685 | IPosition start( 4, ix, iy, ipol, 0 ) ;
|
---|
[2361] | 686 | IPosition end( 4, ix, iy, ipol, nchan_-1 ) ;
|
---|
[2356] | 687 | IPosition inc( 4, 1, 1, 1, 1 ) ;
|
---|
| 688 | Vector<Float> sp = data_( start, end, inc ) ;
|
---|
| 689 | dir(0) = center_(0) - ( cpix(0) - (Double)ix ) * cellx_ ;
|
---|
| 690 | dir(1) = center_(1) - ( cpix(1) - (Double)iy ) * celly_ ;
|
---|
| 691 | spectraCol.put( irow, sp ) ;
|
---|
| 692 | directionCol.put( irow, dir ) ;
|
---|
[2360] | 693 | polnoCol.put( irow, pollist_[ipol] ) ;
|
---|
[2356] | 694 | irow++ ;
|
---|
| 695 | }
|
---|
| 696 | }
|
---|
| 697 | }
|
---|
| 698 | //cout << "outfile_=" << outfile_ << endl ;
|
---|
| 699 | out->makePersistent( outfile_ ) ;
|
---|
| 700 |
|
---|
| 701 | return outfile_ ;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | }
|
---|