[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> |
---|
[2375] | 9 | #include <casa/Arrays/ArrayIter.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> |
---|
[2379] | 20 | #include <tables/Tables/TableIter.h> |
---|
[2356] | 21 | |
---|
| 22 | #include <measures/Measures/MDirection.h> |
---|
| 23 | |
---|
[2364] | 24 | #include <MathUtils.h> |
---|
| 25 | |
---|
[2356] | 26 | #include "STGrid.h" |
---|
| 27 | |
---|
| 28 | using namespace std ; |
---|
| 29 | using namespace casa ; |
---|
| 30 | using namespace asap ; |
---|
| 31 | |
---|
| 32 | namespace asap { |
---|
| 33 | |
---|
[2384] | 34 | // for performance check |
---|
| 35 | double eToInt = 0.0 ; |
---|
| 36 | double eGetWeight = 0.0 ; |
---|
| 37 | |
---|
[2356] | 38 | // constructor |
---|
| 39 | STGrid::STGrid() |
---|
| 40 | { |
---|
| 41 | init() ; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | STGrid::STGrid( const string infile ) |
---|
| 45 | { |
---|
| 46 | init() ; |
---|
| 47 | |
---|
| 48 | setFileIn( infile ) ; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | void STGrid::init() |
---|
| 52 | { |
---|
[2362] | 53 | ifno_ = -1 ; |
---|
[2356] | 54 | nx_ = -1 ; |
---|
| 55 | ny_ = -1 ; |
---|
[2361] | 56 | npol_ = 0 ; |
---|
| 57 | nchan_ = 0 ; |
---|
| 58 | nrow_ = 0 ; |
---|
[2356] | 59 | cellx_ = 0.0 ; |
---|
| 60 | celly_ = 0.0 ; |
---|
| 61 | center_ = Vector<Double> ( 2, 0.0 ) ; |
---|
| 62 | convType_ = "BOX" ; |
---|
[2361] | 63 | wtype_ = "UNIFORM" ; |
---|
[2356] | 64 | convSupport_ = -1 ; |
---|
| 65 | userSupport_ = -1 ; |
---|
| 66 | convSampling_ = 100 ; |
---|
[2379] | 67 | nprocessed_ = 0 ; |
---|
| 68 | nchunk_ = 0 ; |
---|
[2356] | 69 | } |
---|
| 70 | |
---|
| 71 | void STGrid::setFileIn( const string infile ) |
---|
| 72 | { |
---|
| 73 | String name( infile ) ; |
---|
| 74 | if ( infile_.compare( name ) != 0 ) { |
---|
| 75 | infile_ = String( infile ) ; |
---|
| 76 | tab_ = Table( infile_ ) ; |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
[2360] | 80 | void STGrid::setPolList( vector<unsigned int> pols ) |
---|
| 81 | { |
---|
| 82 | pollist_.assign( Vector<uInt>( pols ) ) ; |
---|
| 83 | } |
---|
| 84 | |
---|
[2364] | 85 | void STGrid::setScanList( vector<unsigned int> scans ) |
---|
| 86 | { |
---|
| 87 | scanlist_.assign( Vector<uInt>( scans ) ) ; |
---|
| 88 | } |
---|
| 89 | |
---|
[2361] | 90 | void STGrid::setWeight( const string wType ) |
---|
| 91 | { |
---|
| 92 | wtype_ = String( wType ) ; |
---|
| 93 | wtype_.upcase() ; |
---|
| 94 | } |
---|
| 95 | |
---|
[2356] | 96 | void STGrid::defineImage( int nx, |
---|
| 97 | int ny, |
---|
| 98 | string scellx, |
---|
| 99 | string scelly, |
---|
| 100 | string scenter ) |
---|
| 101 | { |
---|
| 102 | ROArrayColumn<Double> dirCol( tab_, "DIRECTION" ) ; |
---|
| 103 | Matrix<Double> direction = dirCol.getColumn() ; |
---|
| 104 | Double rmax, rmin, dmax, dmin ; |
---|
| 105 | minMax( rmin, rmax, direction.row( 0 ) ) ; |
---|
| 106 | minMax( dmin, dmax, direction.row( 1 ) ) ; |
---|
| 107 | |
---|
| 108 | Int npx = (Int)nx ; |
---|
| 109 | Int npy = (Int)ny ; |
---|
| 110 | String cellx( scellx ) ; |
---|
| 111 | String celly( scelly ) ; |
---|
| 112 | String center( scenter ) ; |
---|
| 113 | setupGrid( npx, npy, |
---|
| 114 | cellx, celly, |
---|
| 115 | rmin, rmax, |
---|
| 116 | dmin, dmax, |
---|
| 117 | center ) ; |
---|
| 118 | } |
---|
| 119 | |
---|
[2364] | 120 | void STGrid::setFunc( string convType, |
---|
| 121 | int convSupport ) |
---|
[2356] | 122 | { |
---|
| 123 | convType_ = String( convType ) ; |
---|
| 124 | convType_.upcase() ; |
---|
| 125 | userSupport_ = (Int)convSupport ; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | #define NEED_UNDERSCORES |
---|
| 129 | #if defined(NEED_UNDERSCORES) |
---|
| 130 | #define ggridsd ggridsd_ |
---|
| 131 | #endif |
---|
| 132 | extern "C" { |
---|
| 133 | void ggridsd(Double*, |
---|
| 134 | const Complex*, |
---|
| 135 | Int*, |
---|
| 136 | Int*, |
---|
| 137 | Int*, |
---|
| 138 | const Int*, |
---|
| 139 | const Int*, |
---|
| 140 | const Float*, |
---|
| 141 | Int*, |
---|
| 142 | Int*, |
---|
| 143 | Complex*, |
---|
| 144 | Float*, |
---|
| 145 | Int*, |
---|
| 146 | Int*, |
---|
| 147 | Int *, |
---|
| 148 | Int *, |
---|
| 149 | Int*, |
---|
| 150 | Int*, |
---|
| 151 | Float*, |
---|
| 152 | Int*, |
---|
| 153 | Int*, |
---|
| 154 | Double*); |
---|
| 155 | } |
---|
[2384] | 156 | void STGrid::call_ggridsd( Array<Double> &xypos, |
---|
| 157 | Array<Complex> &spectra, |
---|
| 158 | Int &nvispol, |
---|
| 159 | Int &nvischan, |
---|
| 160 | Array<Int> &flagtra, |
---|
| 161 | Array<Int> &flagrow, |
---|
| 162 | Array<Float> &weight, |
---|
| 163 | Int &nrow, |
---|
| 164 | Int &irow, |
---|
| 165 | Array<Complex> &gdata, |
---|
| 166 | Array<Float> &gwgt, |
---|
| 167 | Int &nx, |
---|
| 168 | Int &ny, |
---|
| 169 | Int &npol, |
---|
| 170 | Int &nchan, |
---|
| 171 | Int &support, |
---|
| 172 | Int &sampling, |
---|
| 173 | Vector<Float> &convFunc, |
---|
[2381] | 174 | Int *chanMap, |
---|
| 175 | Int *polMap ) |
---|
| 176 | { |
---|
| 177 | // parameters for gridding |
---|
| 178 | Int idopsf = 0 ; |
---|
[2384] | 179 | Int len = npol*nchan ; |
---|
[2381] | 180 | Double *sumw_p = new Double[len] ; |
---|
| 181 | { |
---|
| 182 | Double *work_p = sumw_p ; |
---|
| 183 | for ( Int i = 0 ; i < len ; i++ ) { |
---|
| 184 | *work_p = 0.0 ; |
---|
| 185 | work_p++ ; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
[2384] | 189 | // prepare pointer |
---|
| 190 | Bool deletePos, deleteData, deleteWgt, deleteFlag, deleteFlagR, deleteConv, deleteDataG, deleteWgtG ; |
---|
| 191 | Double *xy_p = xypos.getStorage( deletePos ) ; |
---|
| 192 | const Complex *values_p = spectra.getStorage( deleteData ) ; |
---|
| 193 | const Int *flag_p = flagtra.getStorage( deleteFlag ) ; |
---|
| 194 | const Int *rflag_p = flagrow.getStorage( deleteFlagR ) ; |
---|
| 195 | const Float *wgt_p = weight.getStorage( deleteWgt ) ; |
---|
| 196 | Complex *grid_p = gdata.getStorage( deleteDataG ) ; |
---|
| 197 | Float *wgrid_p = gwgt.getStorage( deleteWgtG ) ; |
---|
| 198 | Float *conv_p = convFunc.getStorage( deleteConv ) ; |
---|
| 199 | |
---|
[2381] | 200 | // call ggridsd |
---|
[2384] | 201 | ggridsd( xy_p, |
---|
| 202 | values_p, |
---|
| 203 | &nvispol, |
---|
| 204 | &nvischan, |
---|
[2381] | 205 | &idopsf, |
---|
[2384] | 206 | flag_p, |
---|
| 207 | rflag_p, |
---|
| 208 | wgt_p, |
---|
| 209 | &nrow, |
---|
| 210 | &irow, |
---|
| 211 | grid_p, |
---|
| 212 | wgrid_p, |
---|
| 213 | &nx, |
---|
| 214 | &ny, |
---|
| 215 | &npol, |
---|
| 216 | &nchan, |
---|
| 217 | &support, |
---|
| 218 | &sampling, |
---|
| 219 | conv_p, |
---|
[2381] | 220 | chanMap, |
---|
| 221 | polMap, |
---|
| 222 | sumw_p ) ; |
---|
| 223 | |
---|
| 224 | // finalization |
---|
[2384] | 225 | xypos.putStorage( xy_p, deletePos ) ; |
---|
| 226 | spectra.freeStorage( values_p, deleteData ) ; |
---|
| 227 | flagtra.freeStorage( flag_p, deleteFlag ) ; |
---|
| 228 | flagrow.freeStorage( rflag_p, deleteFlagR ) ; |
---|
| 229 | weight.freeStorage( wgt_p, deleteWgt ) ; |
---|
| 230 | gdata.putStorage( grid_p, deleteDataG ) ; |
---|
| 231 | gwgt.putStorage( wgrid_p, deleteWgtG ) ; |
---|
| 232 | convFunc.putStorage( conv_p, deleteConv ) ; |
---|
[2381] | 233 | delete sumw_p ; |
---|
| 234 | } |
---|
| 235 | |
---|
[2383] | 236 | Bool STGrid::pastEnd() |
---|
| 237 | { |
---|
| 238 | LogIO os( LogOrigin("STGrid","pastEnd",WHERE) ) ; |
---|
| 239 | Bool b = nprocessed_ >= nrow_ ; |
---|
| 240 | return b ; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | void STGrid::grid() |
---|
| 244 | { |
---|
[2384] | 245 | LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ; |
---|
| 246 | |
---|
[2383] | 247 | // data selection |
---|
| 248 | selectData() ; |
---|
| 249 | setupArray() ; |
---|
| 250 | |
---|
| 251 | if ( wtype_.compare("UNIFORM") != 0 && |
---|
| 252 | wtype_.compare("TINT") != 0 && |
---|
| 253 | wtype_.compare("TSYS") != 0 && |
---|
| 254 | wtype_.compare("TINTSYS") != 0 ) { |
---|
| 255 | LogIO os( LogOrigin("STGrid", "grid", WHERE) ) ; |
---|
| 256 | os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ; |
---|
| 257 | wtype_ = "UNIFORM" ; |
---|
| 258 | } |
---|
| 259 | |
---|
[2384] | 260 | // grid parameter |
---|
| 261 | os << LogIO::DEBUGGING ; |
---|
| 262 | os << "----------" << endl ; |
---|
| 263 | os << "Data selection summary" << endl ; |
---|
| 264 | os << " ifno = " << ifno_ << endl ; |
---|
| 265 | os << " pollist = " << pollist_ << endl ; |
---|
| 266 | os << " scanlist = " << scanlist_ << endl ; |
---|
| 267 | os << "----------" << endl ; |
---|
| 268 | os << "Grid parameter summary" << endl ; |
---|
| 269 | os << " (nx,ny) = (" << nx_ << "," << ny_ << ")" << endl ; |
---|
| 270 | os << " (cellx,celly) = (" << cellx_ << "," << celly_ << ")" << endl ; |
---|
| 271 | os << " center = " << center_ << endl ; |
---|
| 272 | os << " weighting = " << wtype_ << endl ; |
---|
| 273 | os << " convfunc = " << convType_ << "with support " << convSupport_ << endl ; |
---|
| 274 | os << "----------" << LogIO::POST ; |
---|
| 275 | os << LogIO::NORMAL ; |
---|
| 276 | |
---|
[2383] | 277 | Bool doAll = examine() ; |
---|
| 278 | |
---|
| 279 | if ( doAll ) |
---|
| 280 | gridPerPol() ; |
---|
| 281 | else |
---|
| 282 | gridPerRow() ; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | Bool STGrid::examine() |
---|
| 286 | { |
---|
| 287 | // TODO: nchunk_ must be determined from nchan_, npol_, and (nx_,ny_) |
---|
| 288 | // by considering data size to be allocated for ggridsd input/output |
---|
| 289 | nchunk_ = 100 ; |
---|
| 290 | Bool b = nchunk_ >= nrow_ ; |
---|
| 291 | nchunk_ = min( nchunk_, nrow_ ) ; |
---|
| 292 | return b ; |
---|
| 293 | } |
---|
| 294 | |
---|
[2378] | 295 | void STGrid::gridPerRow() |
---|
| 296 | { |
---|
| 297 | LogIO os( LogOrigin("STGrid", "gridPerRow", WHERE) ) ; |
---|
| 298 | double t0, t1 ; |
---|
| 299 | |
---|
| 300 | // convolution kernel |
---|
| 301 | Vector<Float> convFunc ; |
---|
| 302 | t0 = mathutil::gettimeofday_sec() ; |
---|
| 303 | setConvFunc( convFunc ) ; |
---|
| 304 | t1 = mathutil::gettimeofday_sec() ; |
---|
| 305 | os << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ; |
---|
| 306 | |
---|
[2379] | 307 | // grid data |
---|
[2378] | 308 | Int gnx = nx_ ; |
---|
| 309 | Int gny = ny_ ; |
---|
[2379] | 310 | // Extend grid plane with convSupport_ |
---|
[2378] | 311 | // Int gnx = nx_+convSupport_*2 ; |
---|
| 312 | // Int gny = ny_+convSupport_*2 ; |
---|
| 313 | IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ; |
---|
| 314 | Array<Complex> gdataArrC( gshape, 0.0 ) ; |
---|
| 315 | // 2011/12/20 TN |
---|
| 316 | // data_ and gwgtArr share storage |
---|
| 317 | data_.resize( gshape ) ; |
---|
| 318 | data_ = 0.0 ; |
---|
| 319 | Array<Float> gwgtArr( data_ ) ; |
---|
| 320 | |
---|
[2379] | 321 | // parameters for gridding |
---|
| 322 | Int *chanMap = new Int[nchan_] ; |
---|
| 323 | { |
---|
| 324 | Int *work_p = chanMap ; |
---|
| 325 | for ( Int i = 0 ; i < nchan_ ; i++ ) { |
---|
| 326 | *work_p = i ; |
---|
| 327 | work_p++ ; |
---|
| 328 | } |
---|
| 329 | } |
---|
[2383] | 330 | Int *polMap = new Int[1] ; |
---|
| 331 | Int nvispol = 1 ; |
---|
[2384] | 332 | Int irow = -1 ; |
---|
[2379] | 333 | |
---|
| 334 | // for performance check |
---|
[2384] | 335 | double eGetData = 0.0 ; |
---|
[2379] | 336 | double eToPixel = 0.0 ; |
---|
| 337 | double eGGridSD = 0.0 ; |
---|
[2384] | 338 | double eInitPol = 0.0 ; |
---|
[2379] | 339 | |
---|
[2383] | 340 | for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) { |
---|
[2384] | 341 | t0 = mathutil::gettimeofday_sec() ; |
---|
[2383] | 342 | initPol( ipol ) ; |
---|
[2384] | 343 | t1 = mathutil::gettimeofday_sec() ; |
---|
| 344 | eInitPol += t1-t0 ; |
---|
[2383] | 345 | |
---|
| 346 | polMap[0] = ipol ; |
---|
[2380] | 347 | |
---|
[2384] | 348 | os << "start pol" << ipol << LogIO::POST ; |
---|
| 349 | |
---|
[2383] | 350 | while( !pastEnd() ) { |
---|
[2384] | 351 | // data storage |
---|
| 352 | IPosition cshape( 3, npol_, nchan_, nchunk_ ) ; |
---|
| 353 | IPosition mshape( 2, npol_, nchunk_ ) ; |
---|
| 354 | IPosition vshape( 1, nchunk_ ) ; |
---|
| 355 | IPosition dshape( 2, 2, nchunk_ ) ; |
---|
| 356 | IPosition wshape( 2, nchan_, nchunk_ ) ; |
---|
| 357 | Array<Complex> spectra( wshape ) ; |
---|
| 358 | Array<Int> flagtra( wshape ) ; |
---|
| 359 | Array<Int> rflag( vshape ) ; |
---|
| 360 | Array<Float> weight( wshape ) ; |
---|
| 361 | Array<Double> direction( dshape ) ; |
---|
| 362 | Array<Double> xypos( dshape ) ; |
---|
| 363 | |
---|
| 364 | spectraF_.resize( wshape ) ; |
---|
| 365 | flagtraUC_.resize( wshape ) ; |
---|
| 366 | rflagUI_.resize( vshape ) ; |
---|
| 367 | |
---|
[2383] | 368 | // retrieve data |
---|
| 369 | t0 = mathutil::gettimeofday_sec() ; |
---|
| 370 | Int nrow = getDataChunk( spectra, direction, flagtra, rflag, weight ) ; |
---|
| 371 | //os << "nrow = " << nrow << LogIO::POST ; |
---|
| 372 | t1 = mathutil::gettimeofday_sec() ; |
---|
[2384] | 373 | eGetData += t1-t0 ; |
---|
[2383] | 374 | |
---|
| 375 | // world -> pixel |
---|
| 376 | t0 = mathutil::gettimeofday_sec() ; |
---|
| 377 | toPixel( direction, xypos ) ; |
---|
| 378 | t1 = mathutil::gettimeofday_sec() ; |
---|
| 379 | eToPixel += t1-t0 ; |
---|
[2379] | 380 | |
---|
[2383] | 381 | // call ggridsd |
---|
| 382 | t0 = mathutil::gettimeofday_sec() ; |
---|
[2384] | 383 | call_ggridsd( xypos, |
---|
| 384 | spectra, |
---|
| 385 | nvispol, |
---|
| 386 | nchan_, |
---|
| 387 | flagtra, |
---|
| 388 | rflag, |
---|
| 389 | weight, |
---|
| 390 | nrow, |
---|
| 391 | irow, |
---|
| 392 | gdataArrC, |
---|
| 393 | gwgtArr, |
---|
| 394 | gnx, |
---|
| 395 | gny, |
---|
| 396 | npol_, |
---|
| 397 | nchan_, |
---|
| 398 | convSupport_, |
---|
| 399 | convSampling_, |
---|
| 400 | convFunc, |
---|
[2383] | 401 | chanMap, |
---|
| 402 | polMap ) ; |
---|
| 403 | t1 = mathutil::gettimeofday_sec() ; |
---|
| 404 | eGGridSD += t1-t0 ; |
---|
| 405 | |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | nprocessed_ = 0 ; |
---|
[2378] | 409 | } |
---|
[2384] | 410 | os << "initPol: elapsed time is " << eInitPol << " sec." << LogIO::POST ; |
---|
| 411 | os << "getData: elapsed time is " << eGetData-eToInt-eGetWeight << " sec." << LogIO::POST ; |
---|
[2379] | 412 | os << "toPixel: elapsed time is " << eToPixel << " sec." << LogIO::POST ; |
---|
| 413 | os << "ggridsd: elapsed time is " << eGGridSD << " sec." << LogIO::POST ; |
---|
[2381] | 414 | os << "toInt: elapsed time is " << eToInt << " sec." << LogIO::POST ; |
---|
[2384] | 415 | os << "getWeight: elapsed time is " << eGetWeight << " sec." << LogIO::POST ; |
---|
[2383] | 416 | |
---|
[2379] | 417 | delete polMap ; |
---|
| 418 | delete chanMap ; |
---|
| 419 | |
---|
[2378] | 420 | // set data |
---|
| 421 | setData( gdataArrC, gwgtArr ) ; |
---|
[2379] | 422 | |
---|
[2378] | 423 | } |
---|
| 424 | |
---|
[2382] | 425 | void STGrid::gridPerPol() |
---|
[2379] | 426 | { |
---|
[2382] | 427 | LogIO os( LogOrigin("STGrid", "gridPerPol", WHERE) ) ; |
---|
[2364] | 428 | double t0, t1 ; |
---|
[2356] | 429 | |
---|
[2359] | 430 | // convolution kernel |
---|
| 431 | Vector<Float> convFunc ; |
---|
[2364] | 432 | t0 = mathutil::gettimeofday_sec() ; |
---|
[2359] | 433 | setConvFunc( convFunc ) ; |
---|
[2364] | 434 | t1 = mathutil::gettimeofday_sec() ; |
---|
| 435 | os << "setConvFunc: elapsed time is " << t1-t0 << " sec." << LogIO::POST ; |
---|
[2359] | 436 | |
---|
[2382] | 437 | // prepare grid data storage |
---|
[2364] | 438 | Int gnx = nx_ ; |
---|
| 439 | Int gny = ny_ ; |
---|
[2382] | 440 | // // Extend grid plane with convSupport_ |
---|
[2364] | 441 | // Int gnx = nx_+convSupport_*2 ; |
---|
| 442 | // Int gny = ny_+convSupport_*2 ; |
---|
[2361] | 443 | IPosition gshape( 4, gnx, gny, npol_, nchan_ ) ; |
---|
[2356] | 444 | Array<Complex> gdataArrC( gshape, 0.0 ) ; |
---|
[2378] | 445 | //Array<Float> gwgtArr( gshape, 0.0 ) ; |
---|
| 446 | // 2011/12/20 TN |
---|
| 447 | // data_ and weight array shares storage |
---|
| 448 | data_.resize( gshape ) ; |
---|
| 449 | data_ = 0.0 ; |
---|
| 450 | Array<Float> gwgtArr( data_ ) ; |
---|
[2382] | 451 | |
---|
| 452 | // to read data from the table |
---|
| 453 | IPosition mshape( 2, nchan_, nrow_ ) ; |
---|
[2383] | 454 | IPosition dshape( 2, 2, nrow_ ) ; |
---|
[2382] | 455 | Array<Complex> spectra( mshape ) ; |
---|
[2383] | 456 | Array<Double> direction( dshape ) ; |
---|
[2382] | 457 | Array<Int> flagtra( mshape ) ; |
---|
| 458 | Array<Int> rflag( IPosition(1,nrow_) ) ; |
---|
| 459 | Array<Float> weight( mshape ) ; |
---|
[2383] | 460 | Array<Double> xypos( dshape ) ; |
---|
[2382] | 461 | |
---|
| 462 | // maps |
---|
[2361] | 463 | Int *chanMap = new Int[nchan_] ; |
---|
[2356] | 464 | { |
---|
| 465 | Int *work_p = chanMap ; |
---|
[2361] | 466 | for ( Int i = 0 ; i < nchan_ ; i++ ) { |
---|
[2356] | 467 | *work_p = i ; |
---|
| 468 | work_p++ ; |
---|
| 469 | } |
---|
| 470 | } |
---|
[2382] | 471 | Int *polMap = new Int[1] ; |
---|
[2384] | 472 | |
---|
| 473 | // some parameters for ggridsd |
---|
[2382] | 474 | Int nvispol = 1 ; |
---|
[2384] | 475 | Int irow = -1 ; |
---|
[2382] | 476 | |
---|
[2383] | 477 | // for performance check |
---|
| 478 | double eInitPol = 0.0 ; |
---|
| 479 | double eGetData = 0.0 ; |
---|
| 480 | double eToPixel = 0.0 ; |
---|
| 481 | double eGGridSD = 0.0 ; |
---|
| 482 | double eToInt = 0.0 ; |
---|
| 483 | |
---|
[2382] | 484 | for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) { |
---|
| 485 | t0 = mathutil::gettimeofday_sec() ; |
---|
| 486 | initPol( ipol ) ; |
---|
| 487 | t1 = mathutil::gettimeofday_sec() ; |
---|
[2383] | 488 | eInitPol += t1-t0 ; |
---|
[2382] | 489 | |
---|
| 490 | // retrieve data |
---|
| 491 | t0 = mathutil::gettimeofday_sec() ; |
---|
| 492 | getDataPerPol( spectra, direction, flagtra, rflag, weight ) ; |
---|
| 493 | t1 = mathutil::gettimeofday_sec() ; |
---|
[2383] | 494 | eGetData += t1-t0 ; |
---|
| 495 | |
---|
[2382] | 496 | // world -> pixel |
---|
| 497 | t0 = mathutil::gettimeofday_sec() ; |
---|
| 498 | toPixel( direction, xypos ) ; |
---|
| 499 | t1 = mathutil::gettimeofday_sec() ; |
---|
| 500 | //os << "xypos=" << xypos << LogIO::POST ; |
---|
| 501 | os << "toPixel: elapsed time is " << t1-t0 << " sec." << LogIO::POST ; |
---|
| 502 | |
---|
| 503 | // call ggridsd |
---|
| 504 | polMap[0] = ipol ; |
---|
| 505 | t0 = mathutil::gettimeofday_sec() ; |
---|
[2384] | 506 | call_ggridsd( xypos, |
---|
| 507 | spectra, |
---|
| 508 | nvispol, |
---|
| 509 | nchan_, |
---|
| 510 | flagtra, |
---|
| 511 | rflag, |
---|
| 512 | weight, |
---|
| 513 | nrow_, |
---|
| 514 | irow, |
---|
| 515 | gdataArrC, |
---|
| 516 | gwgtArr, |
---|
| 517 | gnx, |
---|
| 518 | gny, |
---|
| 519 | npol_, |
---|
| 520 | nchan_, |
---|
| 521 | convSupport_, |
---|
| 522 | convSampling_, |
---|
| 523 | convFunc, |
---|
[2382] | 524 | chanMap, |
---|
| 525 | polMap ) ; |
---|
| 526 | t1 = mathutil::gettimeofday_sec() ; |
---|
[2384] | 527 | eGGridSD += t1-t0 ; |
---|
[2356] | 528 | } |
---|
[2384] | 529 | os << "initPol: elapsed time is " << eInitPol << " sec." << LogIO::POST ; |
---|
| 530 | os << "getData: elapsed time is " << eGetData-eToInt-eGetWeight << " sec." << LogIO::POST ; |
---|
| 531 | os << "toPixel: elapsed time is " << eToPixel << " sec." << LogIO::POST ; |
---|
| 532 | os << "ggridsd: elapsed time is " << eGGridSD << " sec." << LogIO::POST ; |
---|
| 533 | os << "toInt: elapsed time is " << eToInt << " sec." << LogIO::POST ; |
---|
| 534 | os << "getWeight: elapsed time is " << eGetWeight << " sec." << LogIO::POST ; |
---|
[2382] | 535 | |
---|
| 536 | // delete maps |
---|
[2356] | 537 | delete polMap ; |
---|
| 538 | delete chanMap ; |
---|
[2382] | 539 | |
---|
[2378] | 540 | setData( gdataArrC, gwgtArr ) ; |
---|
[2364] | 541 | // os << "gdataArr = " << gdataArr << LogIO::POST ; |
---|
| 542 | // os << "gwgtArr = " << gwgtArr << LogIO::POST ; |
---|
| 543 | // os << "data_ " << data_ << LogIO::POST ; |
---|
[2356] | 544 | } |
---|
| 545 | |
---|
[2382] | 546 | void STGrid::initPol( Int ipol ) |
---|
| 547 | { |
---|
| 548 | ptab_ = tab_( tab_.col("POLNO") == (uInt)ipol ) ; |
---|
| 549 | |
---|
| 550 | attach( ptab_ ) ; |
---|
| 551 | } |
---|
| 552 | |
---|
[2378] | 553 | void STGrid::setData( Array<Complex> &gdata, |
---|
[2368] | 554 | Array<Float> &gwgt ) |
---|
| 555 | { |
---|
[2378] | 556 | // 2011/12/20 TN |
---|
| 557 | // gwgt and data_ share storage |
---|
[2368] | 558 | LogIO os( LogOrigin("STGrid","setData",WHERE) ) ; |
---|
| 559 | double t0, t1 ; |
---|
| 560 | t0 = mathutil::gettimeofday_sec() ; |
---|
[2378] | 561 | uInt len = data_.nelements() ; |
---|
[2374] | 562 | const Complex *w1_p ; |
---|
[2378] | 563 | Float *w2_p ; |
---|
[2379] | 564 | Bool b1, b2 ; |
---|
[2374] | 565 | const Complex *gdata_p = gdata.getStorage( b1 ) ; |
---|
[2378] | 566 | Float *gwgt_p = data_.getStorage( b2 ) ; |
---|
[2368] | 567 | w1_p = gdata_p ; |
---|
| 568 | w2_p = gwgt_p ; |
---|
| 569 | for ( uInt i = 0 ; i < len ; i++ ) { |
---|
[2378] | 570 | if ( *w2_p > 0.0 ) *w2_p = (*w1_p).real() / *w2_p ; |
---|
[2368] | 571 | w1_p++ ; |
---|
| 572 | w2_p++ ; |
---|
| 573 | } |
---|
| 574 | gdata.freeStorage( gdata_p, b1 ) ; |
---|
[2378] | 575 | data_.putStorage( gwgt_p, b2 ) ; |
---|
[2368] | 576 | t1 = mathutil::gettimeofday_sec() ; |
---|
| 577 | os << "setData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ; |
---|
| 578 | } |
---|
| 579 | |
---|
[2356] | 580 | void STGrid::setupGrid( Int &nx, |
---|
| 581 | Int &ny, |
---|
| 582 | String &cellx, |
---|
| 583 | String &celly, |
---|
| 584 | Double &xmin, |
---|
| 585 | Double &xmax, |
---|
| 586 | Double &ymin, |
---|
| 587 | Double &ymax, |
---|
| 588 | String ¢er ) |
---|
| 589 | { |
---|
[2375] | 590 | LogIO os( LogOrigin("STGrid","setupGrid",WHERE) ) ; |
---|
[2356] | 591 | //cout << "nx=" << nx << ", ny=" << ny << endl ; |
---|
[2359] | 592 | |
---|
| 593 | // center position |
---|
| 594 | if ( center.size() == 0 ) { |
---|
| 595 | center_(0) = 0.5 * ( xmin + xmax ) ; |
---|
| 596 | center_(1) = 0.5 * ( ymin + ymax ) ; |
---|
| 597 | } |
---|
| 598 | else { |
---|
| 599 | String::size_type pos0 = center.find( " " ) ; |
---|
| 600 | if ( pos0 == String::npos ) { |
---|
| 601 | throw AipsError( "bad string format in parameter center" ) ; |
---|
| 602 | } |
---|
| 603 | String::size_type pos1 = center.find( " ", pos0+1 ) ; |
---|
| 604 | String typestr, xstr, ystr ; |
---|
| 605 | if ( pos1 != String::npos ) { |
---|
| 606 | typestr = center.substr( 0, pos0 ) ; |
---|
| 607 | xstr = center.substr( pos0+1, pos1-pos0 ) ; |
---|
| 608 | ystr = center.substr( pos1+1 ) ; |
---|
| 609 | // todo: convert to J2000 (or direction ref for DIRECTION column) |
---|
| 610 | } |
---|
| 611 | else { |
---|
| 612 | typestr = "J2000" ; |
---|
| 613 | xstr = center.substr( 0, pos0 ) ; |
---|
| 614 | ystr = center.substr( pos0+1 ) ; |
---|
| 615 | } |
---|
| 616 | QuantumHolder qh ; |
---|
| 617 | String err ; |
---|
| 618 | qh.fromString( err, xstr ) ; |
---|
| 619 | Quantum<Double> xcen = qh.asQuantumDouble() ; |
---|
| 620 | qh.fromString( err, ystr ) ; |
---|
| 621 | Quantum<Double> ycen = qh.asQuantumDouble() ; |
---|
| 622 | center_(0) = xcen.getValue( "rad" ) ; |
---|
| 623 | center_(1) = ycen.getValue( "rad" ) ; |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | |
---|
[2356] | 627 | nx_ = nx ; |
---|
| 628 | ny_ = ny ; |
---|
| 629 | if ( nx < 0 && ny > 0 ) { |
---|
| 630 | nx_ = ny ; |
---|
| 631 | ny_ = ny ; |
---|
| 632 | } |
---|
| 633 | if ( ny < 0 && nx > 0 ) { |
---|
| 634 | nx_ = nx ; |
---|
| 635 | ny_ = nx ; |
---|
| 636 | } |
---|
[2375] | 637 | |
---|
| 638 | //Double wx = xmax - xmin ; |
---|
| 639 | //Double wy = ymax - ymin ; |
---|
| 640 | Double wx = max( abs(xmax-center_(0)), abs(xmin-center_(0)) ) * 2 ; |
---|
| 641 | Double wy = max( abs(ymax-center_(1)), abs(ymin-center_(1)) ) * 2 ; |
---|
| 642 | // take 10% margin |
---|
| 643 | wx *= 1.10 ; |
---|
| 644 | wy *= 1.10 ; |
---|
| 645 | |
---|
| 646 | Quantum<Double> qcellx ; |
---|
| 647 | Quantum<Double> qcelly ; |
---|
[2356] | 648 | //cout << "nx_ = " << nx_ << ", ny_ = " << ny_ << endl ; |
---|
| 649 | if ( cellx.size() != 0 && celly.size() != 0 ) { |
---|
| 650 | readQuantity( qcellx, cellx ) ; |
---|
| 651 | readQuantity( qcelly, celly ) ; |
---|
| 652 | } |
---|
| 653 | else if ( celly.size() != 0 ) { |
---|
[2375] | 654 | os << "Using celly to x-axis..." << LogIO::POST ; |
---|
[2356] | 655 | readQuantity( qcelly, celly ) ; |
---|
| 656 | qcellx = qcelly ; |
---|
| 657 | } |
---|
| 658 | else if ( cellx.size() != 0 ) { |
---|
[2375] | 659 | os << "Using cellx to y-axis..." << LogIO::POST ; |
---|
[2356] | 660 | readQuantity( qcellx, cellx ) ; |
---|
| 661 | qcelly = qcellx ; |
---|
| 662 | } |
---|
| 663 | else { |
---|
| 664 | if ( nx_ < 0 ) { |
---|
[2375] | 665 | os << "No user preference in grid setting. Using default..." << LogIO::POST ; |
---|
[2356] | 666 | readQuantity( qcellx, "1.0arcmin" ) ; |
---|
| 667 | qcelly = qcellx ; |
---|
| 668 | } |
---|
| 669 | else { |
---|
[2375] | 670 | if ( wx == 0.0 ) { |
---|
| 671 | os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ; |
---|
| 672 | wx = 0.00290888 ; |
---|
| 673 | } |
---|
| 674 | if ( wy == 0.0 ) { |
---|
| 675 | os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ; |
---|
| 676 | wy = 0.00290888 ; |
---|
| 677 | } |
---|
[2356] | 678 | qcellx = Quantum<Double>( wx/nx_, "rad" ) ; |
---|
| 679 | qcelly = Quantum<Double>( wy/ny_, "rad" ) ; |
---|
| 680 | } |
---|
| 681 | } |
---|
| 682 | cellx_ = qcellx.getValue( "rad" ) ; |
---|
| 683 | celly_ = qcelly.getValue( "rad" ) ; |
---|
| 684 | if ( nx_ < 0 ) { |
---|
[2375] | 685 | if ( wx == 0.0 ) { |
---|
| 686 | os << "Using default spatial extent (10arcmin) in x" << LogIO::POST ; |
---|
| 687 | wx = 0.00290888 ; |
---|
| 688 | } |
---|
| 689 | if ( wy == 0.0 ) { |
---|
| 690 | os << "Using default spatial extent (10arcmin) in y" << LogIO::POST ; |
---|
| 691 | wy = 0.00290888 ; |
---|
| 692 | } |
---|
[2356] | 693 | nx_ = Int( ceil( wx/cellx_ ) ) ; |
---|
| 694 | ny_ = Int( ceil( wy/celly_ ) ) ; |
---|
| 695 | } |
---|
| 696 | } |
---|
| 697 | |
---|
[2379] | 698 | void STGrid::selectData() |
---|
[2362] | 699 | { |
---|
| 700 | Int ifno = ifno_ ; |
---|
| 701 | Table taborg( infile_ ) ; |
---|
| 702 | if ( ifno == -1 ) { |
---|
[2368] | 703 | LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ; |
---|
[2362] | 704 | // os << LogIO::SEVERE |
---|
| 705 | // << "Please set IFNO before actual gridding" |
---|
| 706 | // << LogIO::EXCEPTION ; |
---|
| 707 | ROScalarColumn<uInt> ifnoCol( taborg, "IFNO" ) ; |
---|
| 708 | ifno = ifnoCol( 0 ) ; |
---|
| 709 | os << LogIO::WARN |
---|
| 710 | << "IFNO is not given. Using default IFNO: " << ifno << LogIO::POST ; |
---|
| 711 | } |
---|
[2364] | 712 | // tab = taborg( taborg.col("IFNO") == ifno ) ; |
---|
| 713 | TableExprNode node ; |
---|
| 714 | node = taborg.col("IFNO") == ifno ; |
---|
| 715 | if ( scanlist_.size() > 0 ) { |
---|
| 716 | node = node && taborg.col("SCANNO").in( scanlist_ ) ; |
---|
| 717 | } |
---|
[2379] | 718 | tab_ = taborg( node ) ; |
---|
| 719 | if ( tab_.nrow() == 0 ) { |
---|
[2368] | 720 | LogIO os( LogOrigin("STGrid","selectData",WHERE) ) ; |
---|
[2362] | 721 | os << LogIO::SEVERE |
---|
[2376] | 722 | << "No corresponding rows for given selection: IFNO " << ifno ; |
---|
| 723 | if ( scanlist_.size() > 0 ) |
---|
| 724 | os << " SCANNO " << scanlist_ ; |
---|
| 725 | os << LogIO::EXCEPTION ; |
---|
[2362] | 726 | } |
---|
[2383] | 727 | //attach( tab_ ) ; |
---|
[2362] | 728 | } |
---|
| 729 | |
---|
[2382] | 730 | void STGrid::attach( Table &tab ) |
---|
[2379] | 731 | { |
---|
| 732 | // attach to table |
---|
[2382] | 733 | spectraCol_.attach( tab, "SPECTRA" ) ; |
---|
| 734 | flagtraCol_.attach( tab, "FLAGTRA" ) ; |
---|
| 735 | directionCol_.attach( tab, "DIRECTION" ) ; |
---|
| 736 | flagRowCol_.attach( tab, "FLAGROW" ) ; |
---|
| 737 | tsysCol_.attach( tab, "TSYS" ) ; |
---|
| 738 | intervalCol_.attach( tab, "INTERVAL" ) ; |
---|
[2381] | 739 | // Vector<String> colnames( 6 ) ; |
---|
| 740 | // colnames[0] = "SPECTRA" ; |
---|
| 741 | // colnames[1] = "FLAGTRA" ; |
---|
| 742 | // colnames[2] = "DIRECTION" ; |
---|
| 743 | // colnames[3] = "FLAGROW" ; |
---|
| 744 | // colnames[4] = "TSYS" ; |
---|
| 745 | // colnames[5] = "INTERVAL" ; |
---|
| 746 | // row_ = ROTableRow( tab_, colnames ) ; |
---|
| 747 | // const TableRecord &rec = row_.record() ; |
---|
| 748 | // spectraRF_.attachToRecord( rec, colnames[0] ) ; |
---|
| 749 | // flagtraRF_.attachToRecord( rec, colnames[1] ) ; |
---|
| 750 | // directionRF_.attachToRecord( rec, colnames[2] ) ; |
---|
| 751 | // flagRowRF_.attachToRecord( rec, colnames[3] ) ; |
---|
| 752 | // tsysRF_.attachToRecord( rec, colnames[4] ) ; |
---|
| 753 | // intervalRF_.attachToRecord( rec, colnames[5] ) ; |
---|
[2379] | 754 | } |
---|
| 755 | |
---|
[2382] | 756 | void STGrid::getDataPerPol( Array<Float> &spectra, |
---|
| 757 | Array<Double> &direction, |
---|
| 758 | Array<uChar> &flagtra, |
---|
| 759 | Array<uInt> &rflag, |
---|
| 760 | Array<Float> &weight ) |
---|
[2356] | 761 | { |
---|
[2382] | 762 | LogIO os( LogOrigin("STGrid","getDataPerPol",WHERE) ) ; |
---|
[2383] | 763 | |
---|
[2382] | 764 | // 2011/12/22 TN |
---|
| 765 | // weight and tsys shares its storage |
---|
| 766 | Array<Float> tsys( weight ) ; |
---|
| 767 | Array<Double> tint( rflag.shape() ) ; |
---|
[2383] | 768 | |
---|
| 769 | Vector<uInt> rflagVec( rflag ) ; |
---|
[2382] | 770 | Vector<Double> tintVec( tint ) ; |
---|
[2375] | 771 | |
---|
[2382] | 772 | spectraCol_.getColumn( spectra ) ; |
---|
| 773 | flagtraCol_.getColumn( flagtra ) ; |
---|
| 774 | flagRowCol_.getColumn( rflagVec ) ; |
---|
| 775 | directionCol_.getColumn( direction ) ; |
---|
| 776 | intervalCol_.getColumn( tintVec ) ; |
---|
| 777 | Vector<Float> tsysTemp = tsysCol_( 0 ) ; |
---|
| 778 | if ( tsysTemp.nelements() == (uInt)nchan_ ) { |
---|
| 779 | tsysCol_.getColumn( tsys ) ; |
---|
[2360] | 780 | } |
---|
[2382] | 781 | else { |
---|
| 782 | tsys = tsysTemp[0] ; |
---|
| 783 | } |
---|
[2384] | 784 | |
---|
| 785 | double t0,t1 ; |
---|
| 786 | t0 = mathutil::gettimeofday_sec() ; |
---|
[2383] | 787 | getWeight( weight, tsys, tint ) ; |
---|
[2384] | 788 | t1 = mathutil::gettimeofday_sec() ; |
---|
| 789 | eGetWeight += t1-t0 ; |
---|
[2356] | 790 | } |
---|
| 791 | |
---|
[2382] | 792 | void STGrid::getDataPerPol( Array<Complex> &spectra, |
---|
| 793 | Array<Double> &direction, |
---|
| 794 | Array<Int> &flagtra, |
---|
| 795 | Array<Int> &rflag, |
---|
| 796 | Array<Float> &weight ) |
---|
[2371] | 797 | { |
---|
[2382] | 798 | LogIO os( LogOrigin("STGrid","getDataPerPol",WHERE) ) ; |
---|
[2375] | 799 | double t0, t1 ; |
---|
| 800 | |
---|
[2382] | 801 | Array<uChar> flagUC( flagtra.shape() ) ; |
---|
| 802 | Array<uInt> rflagUI( rflag.shape() ) ; |
---|
| 803 | Array<Float> spectraF( spectra.shape() ) ; |
---|
| 804 | getDataPerPol( spectraF, direction, flagUC, rflagUI, weight ) ; |
---|
[2375] | 805 | |
---|
[2382] | 806 | convertArray( spectra, spectraF ) ; |
---|
[2375] | 807 | t0 = mathutil::gettimeofday_sec() ; |
---|
| 808 | toInt( flagUC, flagtra ) ; |
---|
| 809 | toInt( rflagUI, rflag ) ; |
---|
| 810 | t1 = mathutil::gettimeofday_sec() ; |
---|
[2384] | 811 | eToInt += t1-t0 ; |
---|
[2383] | 812 | //os << "toInt: elapsed time is " << t1-t0 << " sec." << LogIO::POST ; |
---|
[2375] | 813 | } |
---|
| 814 | |
---|
[2379] | 815 | Int STGrid::getDataChunk( Array<Complex> &spectra, |
---|
| 816 | Array<Double> &direction, |
---|
| 817 | Array<Int> &flagtra, |
---|
| 818 | Array<Int> &rflag, |
---|
| 819 | Array<Float> &weight ) |
---|
[2378] | 820 | { |
---|
[2381] | 821 | Int nrow = getDataChunk( spectraF_, direction, flagtraUC_, rflagUI_, weight ) ; |
---|
[2383] | 822 | if ( nrow < nchunk_ ) { |
---|
| 823 | spectra.resize( spectraF_.shape() ) ; |
---|
| 824 | flagtra.resize( flagtraUC_.shape() ) ; |
---|
| 825 | rflag.resize( rflagUI_.shape() ) ; |
---|
| 826 | } |
---|
[2381] | 827 | double t0, t1 ; |
---|
| 828 | t0 = mathutil::gettimeofday_sec() ; |
---|
| 829 | convertArray( spectra, spectraF_ ) ; |
---|
[2382] | 830 | toInt( flagtraUC_, flagtra ) ; |
---|
| 831 | toInt( rflagUI_, rflag ) ; |
---|
[2381] | 832 | t1 = mathutil::gettimeofday_sec() ; |
---|
[2384] | 833 | eToInt = t1 - t0 ; |
---|
[2381] | 834 | |
---|
[2379] | 835 | return nrow ; |
---|
[2378] | 836 | } |
---|
| 837 | |
---|
[2379] | 838 | Int STGrid::getDataChunk( Array<Float> &spectra, |
---|
| 839 | Array<Double> &direction, |
---|
| 840 | Array<uChar> &flagtra, |
---|
| 841 | Array<uInt> &rflag, |
---|
| 842 | Array<Float> &weight ) |
---|
[2375] | 843 | { |
---|
[2379] | 844 | LogIO os( LogOrigin("STGrid","getDataChunk",WHERE) ) ; |
---|
[2383] | 845 | Int nrow = spectra.shape()[1] ; |
---|
| 846 | Int remainingRow = nrow_ - nprocessed_ ; |
---|
| 847 | if ( remainingRow < nrow ) { |
---|
| 848 | nrow = remainingRow ; |
---|
| 849 | IPosition mshape( 2, nchan_, nrow ) ; |
---|
| 850 | IPosition vshape( 1, nrow ) ; |
---|
| 851 | spectra.resize( mshape ) ; |
---|
| 852 | flagtra.resize( mshape ) ; |
---|
| 853 | direction.resize( IPosition(2,2,nrow) ) ; |
---|
| 854 | rflag.resize( vshape ) ; |
---|
| 855 | weight.resize( mshape ) ; |
---|
[2379] | 856 | } |
---|
[2383] | 857 | // 2011/12/22 TN |
---|
| 858 | // tsys shares its storage with weight |
---|
| 859 | Array<Float> tsys( weight ) ; |
---|
| 860 | Array<Double> tint( rflag.shape() ) ; |
---|
[2380] | 861 | |
---|
[2383] | 862 | Vector<uInt> rflagVec( rflag ) ; |
---|
| 863 | Vector<Double> tintVec( tint ) ; |
---|
[2379] | 864 | |
---|
[2383] | 865 | RefRows rows( nprocessed_, nprocessed_+nrow-1, 1 ) ; |
---|
| 866 | os << "rows.nrows()=" << rows.nrows() << LogIO::POST ; |
---|
| 867 | spectraCol_.getColumnCells( rows, spectra ) ; |
---|
| 868 | flagtraCol_.getColumnCells( rows, flagtra ) ; |
---|
| 869 | directionCol_.getColumnCells( rows, direction ) ; |
---|
| 870 | flagRowCol_.getColumnCells( rows, rflagVec ) ; |
---|
| 871 | intervalCol_.getColumnCells( rows, tintVec ) ; |
---|
| 872 | Vector<Float> tsysTemp = tsysCol_( nprocessed_ ) ; |
---|
| 873 | if ( tsysTemp.nelements() == (uInt)nchan_ ) |
---|
| 874 | tsysCol_.getColumnCells( rows, tsys ) ; |
---|
| 875 | else |
---|
| 876 | tsys = tsysTemp[0] ; |
---|
| 877 | |
---|
[2384] | 878 | double t0,t1 ; |
---|
| 879 | t0 = mathutil::gettimeofday_sec() ; |
---|
[2379] | 880 | getWeight( weight, tsys, tint ) ; |
---|
[2384] | 881 | t1 = mathutil::gettimeofday_sec() ; |
---|
| 882 | eGetWeight += t1-t0 ; |
---|
[2379] | 883 | |
---|
| 884 | nprocessed_ += nrow ; |
---|
| 885 | |
---|
| 886 | return nrow ; |
---|
| 887 | } |
---|
| 888 | |
---|
| 889 | void STGrid::setupArray() |
---|
| 890 | { |
---|
[2376] | 891 | LogIO os( LogOrigin("STGrid","setupArray",WHERE) ) ; |
---|
[2379] | 892 | ROScalarColumn<uInt> polnoCol( tab_, "POLNO" ) ; |
---|
[2371] | 893 | Vector<uInt> pols = polnoCol.getColumn() ; |
---|
[2376] | 894 | //os << pols << LogIO::POST ; |
---|
[2371] | 895 | Vector<uInt> pollistOrg ; |
---|
| 896 | uInt npolOrg = 0 ; |
---|
| 897 | uInt polno ; |
---|
| 898 | for ( uInt i = 0 ; i < polnoCol.nrow() ; i++ ) { |
---|
| 899 | //polno = polnoCol( i ) ; |
---|
| 900 | polno = pols( i ) ; |
---|
| 901 | if ( allNE( pollistOrg, polno ) ) { |
---|
| 902 | pollistOrg.resize( npolOrg+1, True ) ; |
---|
| 903 | pollistOrg[npolOrg] = polno ; |
---|
| 904 | npolOrg++ ; |
---|
| 905 | } |
---|
| 906 | } |
---|
| 907 | if ( pollist_.size() == 0 ) |
---|
| 908 | pollist_ = pollistOrg ; |
---|
| 909 | else { |
---|
| 910 | Vector<uInt> newlist ; |
---|
| 911 | uInt newsize = 0 ; |
---|
| 912 | for ( uInt i = 0 ; i < pollist_.size() ; i++ ) { |
---|
| 913 | if ( anyEQ( pollistOrg, pollist_[i] ) ) { |
---|
| 914 | newlist.resize( newsize+1, True ) ; |
---|
| 915 | newlist[newsize] = pollist_[i] ; |
---|
| 916 | newsize++ ; |
---|
| 917 | } |
---|
| 918 | } |
---|
| 919 | pollist_.assign( newlist ) ; |
---|
| 920 | } |
---|
| 921 | npol_ = pollist_.size() ; |
---|
| 922 | if ( npol_ == 0 ) { |
---|
| 923 | os << LogIO::SEVERE << "Empty pollist" << LogIO::EXCEPTION ; |
---|
| 924 | } |
---|
[2379] | 925 | nrow_ = tab_.nrow() / npolOrg ; |
---|
| 926 | ROArrayColumn<uChar> tmpCol( tab_, "FLAGTRA" ) ; |
---|
[2371] | 927 | nchan_ = tmpCol( 0 ).nelements() ; |
---|
| 928 | // os << "npol_ = " << npol_ << "(" << pollist_ << ")" << endl |
---|
| 929 | // << "nchan_ = " << nchan_ << endl |
---|
| 930 | // << "nrow_ = " << nrow_ << LogIO::POST ; |
---|
| 931 | } |
---|
| 932 | |
---|
[2375] | 933 | void STGrid::getWeight( Array<Float> &w, |
---|
[2382] | 934 | Array<Float> &tsys, |
---|
| 935 | Array<Double> &tint ) |
---|
| 936 | { |
---|
[2383] | 937 | LogIO os( LogOrigin("STGrid","getWeight",WHERE) ) ; |
---|
[2384] | 938 | |
---|
[2382] | 939 | // 2011/12/22 TN |
---|
| 940 | // w (weight) and tsys share storage |
---|
| 941 | IPosition refShape = tsys.shape() ; |
---|
| 942 | Int nchan = refShape[0] ; |
---|
| 943 | Int nrow = refShape[1] ; |
---|
| 944 | // os << "nchan=" << nchan << ", nrow=" << nrow << LogIO::POST ; |
---|
| 945 | // os << "w.shape()=" << w.shape() << endl |
---|
| 946 | // << "tsys.shape()=" << tsys.shape() << endl |
---|
| 947 | // << "tint.shape()=" << tint.shape() << LogIO::POST ; |
---|
| 948 | |
---|
| 949 | // set weight |
---|
| 950 | if ( wtype_.compare( "UNIFORM" ) == 0 ) { |
---|
| 951 | w = 1.0 ; |
---|
| 952 | } |
---|
| 953 | else if ( wtype_.compare( "TINT" ) == 0 ) { |
---|
| 954 | Bool b0, b1 ; |
---|
| 955 | Float *w_p = w.getStorage( b0 ) ; |
---|
| 956 | Float *w0_p = w_p ; |
---|
| 957 | const Double *ti_p = tint.getStorage( b1 ) ; |
---|
| 958 | const Double *w1_p = ti_p ; |
---|
| 959 | for ( Int irow = 0 ; irow < nrow ; irow++ ) { |
---|
| 960 | for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) { |
---|
| 961 | *w0_p = *w1_p ; |
---|
| 962 | w0_p++ ; |
---|
| 963 | } |
---|
| 964 | w1_p++ ; |
---|
| 965 | } |
---|
| 966 | w.putStorage( w_p, b0 ) ; |
---|
| 967 | tint.freeStorage( ti_p, b1 ) ; |
---|
| 968 | } |
---|
| 969 | else if ( wtype_.compare( "TSYS" ) == 0 ) { |
---|
| 970 | Bool b0 ; |
---|
| 971 | Float *w_p = w.getStorage( b0 ) ; |
---|
| 972 | Float *w0_p = w_p ; |
---|
| 973 | for ( Int irow = 0 ; irow < nrow ; irow++ ) { |
---|
| 974 | for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) { |
---|
| 975 | Float temp = *w0_p ; |
---|
| 976 | *w0_p = 1.0 / ( temp * temp ) ; |
---|
| 977 | w0_p++ ; |
---|
| 978 | } |
---|
| 979 | } |
---|
| 980 | w.putStorage( w_p, b0 ) ; |
---|
| 981 | } |
---|
| 982 | else if ( wtype_.compare( "TINTSYS" ) == 0 ) { |
---|
| 983 | Bool b0, b1 ; |
---|
| 984 | Float *w_p = w.getStorage( b0 ) ; |
---|
| 985 | Float *w0_p = w_p ; |
---|
| 986 | const Double *ti_p = tint.getStorage( b1 ) ; |
---|
| 987 | const Double *w1_p = ti_p ; |
---|
| 988 | for ( Int irow = 0 ; irow < nrow ; irow++ ) { |
---|
| 989 | Float interval = *w1_p ; |
---|
| 990 | for ( Int ichan = 0 ; ichan < nchan ; ichan++ ) { |
---|
| 991 | Float temp = *w0_p ; |
---|
| 992 | *w0_p = interval / ( temp * temp ) ; |
---|
| 993 | w0_p++ ; |
---|
| 994 | } |
---|
| 995 | w1_p++ ; |
---|
| 996 | } |
---|
| 997 | w.putStorage( w_p, b0 ) ; |
---|
| 998 | tint.freeStorage( ti_p, b1 ) ; |
---|
| 999 | } |
---|
| 1000 | else { |
---|
| 1001 | //LogIO os( LogOrigin("STGrid", "getWeight", WHERE) ) ; |
---|
| 1002 | //os << LogIO::WARN << "Unsupported weight type '" << wtype_ << "', apply UNIFORM weight" << LogIO::POST ; |
---|
| 1003 | w = 1.0 ; |
---|
| 1004 | } |
---|
| 1005 | } |
---|
| 1006 | |
---|
[2375] | 1007 | void STGrid::toInt( Array<uChar> &u, Array<Int> &v ) |
---|
[2356] | 1008 | { |
---|
[2375] | 1009 | uInt len = u.nelements() ; |
---|
[2356] | 1010 | Int *int_p = new Int[len] ; |
---|
| 1011 | Bool deleteIt ; |
---|
[2375] | 1012 | const uChar *data_p = u.getStorage( deleteIt ) ; |
---|
[2356] | 1013 | Int *i_p = int_p ; |
---|
| 1014 | const uChar *u_p = data_p ; |
---|
| 1015 | for ( uInt i = 0 ; i < len ; i++ ) { |
---|
| 1016 | *i_p = ( *u_p == 0 ) ? 0 : 1 ; |
---|
| 1017 | i_p++ ; |
---|
| 1018 | u_p++ ; |
---|
| 1019 | } |
---|
[2375] | 1020 | u.freeStorage( data_p, deleteIt ) ; |
---|
| 1021 | v.takeStorage( u.shape(), int_p, TAKE_OVER ) ; |
---|
[2356] | 1022 | } |
---|
| 1023 | |
---|
[2375] | 1024 | void STGrid::toInt( Array<uInt> &u, Array<Int> &v ) |
---|
[2356] | 1025 | { |
---|
[2375] | 1026 | uInt len = u.nelements() ; |
---|
[2356] | 1027 | Int *int_p = new Int[len] ; |
---|
| 1028 | Bool deleteIt ; |
---|
[2375] | 1029 | const uInt *data_p = u.getStorage( deleteIt ) ; |
---|
[2356] | 1030 | Int *i_p = int_p ; |
---|
| 1031 | const uInt *u_p = data_p ; |
---|
| 1032 | for ( uInt i = 0 ; i < len ; i++ ) { |
---|
| 1033 | *i_p = ( *u_p == 0 ) ? 0 : 1 ; |
---|
| 1034 | i_p++ ; |
---|
| 1035 | u_p++ ; |
---|
| 1036 | } |
---|
[2375] | 1037 | u.freeStorage( data_p, deleteIt ) ; |
---|
| 1038 | v.takeStorage( u.shape(), int_p, TAKE_OVER ) ; |
---|
[2356] | 1039 | } |
---|
| 1040 | |
---|
[2375] | 1041 | void STGrid::toPixel( Array<Double> &world, Array<Double> &pixel ) |
---|
[2356] | 1042 | { |
---|
[2359] | 1043 | // gridding will be done on (nx_+2*convSupport_) x (ny_+2*convSupport_) |
---|
| 1044 | // grid plane to avoid unexpected behavior on grid edge |
---|
[2375] | 1045 | Block<Double> pixc( 2 ) ; |
---|
| 1046 | pixc[0] = Double( nx_-1 ) * 0.5 ; |
---|
| 1047 | pixc[1] = Double( ny_-1 ) * 0.5 ; |
---|
| 1048 | // pixc[0] = Double( nx_+2*convSupport_-1 ) * 0.5 ; |
---|
| 1049 | // pixc[1] = Double( ny_+2*convSupport_-1 ) * 0.5 ; |
---|
[2356] | 1050 | uInt nrow = world.shape()[1] ; |
---|
[2375] | 1051 | Bool bw, bp ; |
---|
| 1052 | const Double *w_p = world.getStorage( bw ) ; |
---|
| 1053 | Double *p_p = pixel.getStorage( bp ) ; |
---|
| 1054 | const Double *ww_p = w_p ; |
---|
| 1055 | Double *wp_p = p_p ; |
---|
| 1056 | for ( uInt i = 0 ; i < nrow ; i++ ) { |
---|
| 1057 | *wp_p = pixc[0] + ( *ww_p - center_[0] ) / cellx_ ; |
---|
| 1058 | wp_p++ ; |
---|
| 1059 | ww_p++ ; |
---|
| 1060 | *wp_p = pixc[1] + ( *ww_p - center_[1] ) / celly_ ; |
---|
| 1061 | wp_p++ ; |
---|
| 1062 | ww_p++ ; |
---|
[2356] | 1063 | } |
---|
[2375] | 1064 | world.freeStorage( w_p, bw ) ; |
---|
| 1065 | pixel.putStorage( p_p, bp ) ; |
---|
[2364] | 1066 | // String gridfile = "grid."+convType_+"."+String::toString(convSupport_)+".dat" ; |
---|
| 1067 | // ofstream ofs( gridfile.c_str(), ios::out ) ; |
---|
| 1068 | // ofs << "center " << center_(0) << " " << pixc(0) |
---|
| 1069 | // << " " << center_(1) << " " << pixc(1) << endl ; |
---|
| 1070 | // for ( uInt irow = 0 ; irow < nrow ; irow++ ) { |
---|
| 1071 | // ofs << irow ; |
---|
| 1072 | // for ( uInt i = 0 ; i < 2 ; i++ ) { |
---|
| 1073 | // ofs << " " << world(i, irow) << " " << pixel(i, irow) ; |
---|
| 1074 | // } |
---|
| 1075 | // ofs << endl ; |
---|
| 1076 | // } |
---|
| 1077 | // ofs.close() ; |
---|
[2356] | 1078 | } |
---|
| 1079 | |
---|
| 1080 | void STGrid::boxFunc( Vector<Float> &convFunc, Int &convSize ) |
---|
| 1081 | { |
---|
| 1082 | convFunc = 0.0 ; |
---|
| 1083 | for ( Int i = 0 ; i < convSize/2 ; i++ ) |
---|
| 1084 | convFunc(i) = 1.0 ; |
---|
| 1085 | } |
---|
| 1086 | |
---|
| 1087 | #define NEED_UNDERSCORES |
---|
| 1088 | #if defined(NEED_UNDERSCORES) |
---|
| 1089 | #define grdsf grdsf_ |
---|
| 1090 | #endif |
---|
| 1091 | extern "C" { |
---|
| 1092 | void grdsf(Double*, Double*); |
---|
| 1093 | } |
---|
| 1094 | void STGrid::spheroidalFunc( Vector<Float> &convFunc ) |
---|
| 1095 | { |
---|
| 1096 | convFunc = 0.0 ; |
---|
| 1097 | for ( Int i = 0 ; i < convSampling_*convSupport_ ; i++ ) { |
---|
| 1098 | Double nu = Double(i) / Double(convSupport_*convSampling_) ; |
---|
| 1099 | Double val ; |
---|
| 1100 | grdsf( &nu, &val ) ; |
---|
| 1101 | convFunc(i) = ( 1.0 - nu * nu ) * val ; |
---|
| 1102 | } |
---|
| 1103 | } |
---|
| 1104 | |
---|
| 1105 | void STGrid::gaussFunc( Vector<Float> &convFunc ) |
---|
| 1106 | { |
---|
| 1107 | convFunc = 0.0 ; |
---|
[2363] | 1108 | // HWHM of the Gaussian is convSupport_ / 4 |
---|
| 1109 | // To take into account Gaussian tail, kernel cutoff is set to 4 * HWHM |
---|
| 1110 | Int len = convSampling_ * convSupport_ ; |
---|
| 1111 | Double hwhm = len * 0.25 ; |
---|
| 1112 | for ( Int i = 0 ; i < len ; i++ ) { |
---|
[2356] | 1113 | Double val = Double(i) / hwhm ; |
---|
| 1114 | convFunc(i) = exp( -log(2)*val*val ) ; |
---|
| 1115 | } |
---|
| 1116 | } |
---|
| 1117 | |
---|
| 1118 | void STGrid::pbFunc( Vector<Float> &convFunc ) |
---|
| 1119 | { |
---|
| 1120 | convFunc = 0.0 ; |
---|
| 1121 | } |
---|
| 1122 | |
---|
| 1123 | void STGrid::setConvFunc( Vector<Float> &convFunc ) |
---|
| 1124 | { |
---|
| 1125 | convSupport_ = userSupport_ ; |
---|
| 1126 | if ( convType_ == "BOX" ) { |
---|
| 1127 | if ( convSupport_ < 0 ) |
---|
| 1128 | convSupport_ = 0 ; |
---|
| 1129 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ; |
---|
| 1130 | convFunc.resize( convSize ) ; |
---|
| 1131 | boxFunc( convFunc, convSize ) ; |
---|
| 1132 | } |
---|
| 1133 | else if ( convType_ == "SF" ) { |
---|
| 1134 | if ( convSupport_ < 0 ) |
---|
| 1135 | convSupport_ = 3 ; |
---|
| 1136 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ; |
---|
| 1137 | convFunc.resize( convSize ) ; |
---|
| 1138 | spheroidalFunc( convFunc ) ; |
---|
| 1139 | } |
---|
| 1140 | else if ( convType_ == "GAUSS" ) { |
---|
[2363] | 1141 | // to take into account Gaussian tail |
---|
[2356] | 1142 | if ( convSupport_ < 0 ) |
---|
[2363] | 1143 | convSupport_ = 12 ; // 3 * 4 |
---|
| 1144 | else { |
---|
| 1145 | convSupport_ = userSupport_ * 4 ; |
---|
| 1146 | } |
---|
[2356] | 1147 | Int convSize = convSampling_ * ( 2 * convSupport_ + 2 ) ; |
---|
| 1148 | convFunc.resize( convSize ) ; |
---|
| 1149 | gaussFunc( convFunc ) ; |
---|
| 1150 | } |
---|
[2359] | 1151 | else if ( convType_ == "PB" ) { |
---|
| 1152 | if ( convSupport_ < 0 ) |
---|
| 1153 | convSupport_ = 0 ; |
---|
[2356] | 1154 | pbFunc( convFunc ) ; |
---|
[2359] | 1155 | } |
---|
[2356] | 1156 | else { |
---|
| 1157 | throw AipsError( "Unsupported convolution function" ) ; |
---|
| 1158 | } |
---|
| 1159 | } |
---|
| 1160 | |
---|
| 1161 | string STGrid::saveData( string outfile ) |
---|
| 1162 | { |
---|
[2368] | 1163 | LogIO os( LogOrigin("STGrid", "saveData", WHERE) ) ; |
---|
| 1164 | double t0, t1 ; |
---|
| 1165 | t0 = mathutil::gettimeofday_sec() ; |
---|
| 1166 | |
---|
[2360] | 1167 | //Int polno = 0 ; |
---|
[2371] | 1168 | String outfile_ ; |
---|
[2356] | 1169 | if ( outfile.size() == 0 ) { |
---|
| 1170 | if ( infile_.lastchar() == '/' ) { |
---|
| 1171 | outfile_ = infile_.substr( 0, infile_.size()-1 ) ; |
---|
| 1172 | } |
---|
| 1173 | else { |
---|
| 1174 | outfile_ = infile_ ; |
---|
| 1175 | } |
---|
| 1176 | outfile_ += ".grid" ; |
---|
| 1177 | } |
---|
| 1178 | else { |
---|
| 1179 | outfile_ = outfile ; |
---|
| 1180 | } |
---|
[2371] | 1181 | Table tab ; |
---|
| 1182 | prepareTable( tab, outfile_ ) ; |
---|
[2356] | 1183 | IPosition dshape = data_.shape() ; |
---|
[2361] | 1184 | Int nrow = nx_ * ny_ * npol_ ; |
---|
| 1185 | tab.rwKeywordSet().define( "nPol", npol_ ) ; |
---|
[2360] | 1186 | tab.addRow( nrow ) ; |
---|
[2356] | 1187 | Vector<Double> cpix( 2 ) ; |
---|
| 1188 | cpix(0) = Double( nx_ - 1 ) * 0.5 ; |
---|
| 1189 | cpix(1) = Double( ny_ - 1 ) * 0.5 ; |
---|
| 1190 | Vector<Double> dir( 2 ) ; |
---|
| 1191 | ArrayColumn<Double> directionCol( tab, "DIRECTION" ) ; |
---|
| 1192 | ArrayColumn<Float> spectraCol( tab, "SPECTRA" ) ; |
---|
| 1193 | ScalarColumn<uInt> polnoCol( tab, "POLNO" ) ; |
---|
| 1194 | Int irow = 0 ; |
---|
[2371] | 1195 | Vector<Float> sp( nchan_ ) ; |
---|
| 1196 | Bool bsp, bdata ; |
---|
| 1197 | const Float *data_p = data_.getStorage( bdata ) ; |
---|
| 1198 | Float *wsp_p, *sp_p ; |
---|
| 1199 | const Float *wdata_p = data_p ; |
---|
| 1200 | long step = nx_ * ny_ * npol_ ; |
---|
| 1201 | long offset ; |
---|
[2356] | 1202 | for ( Int iy = 0 ; iy < ny_ ; iy++ ) { |
---|
[2371] | 1203 | dir(1) = center_(1) - ( cpix(1) - (Double)iy ) * celly_ ; |
---|
[2356] | 1204 | for ( Int ix = 0 ; ix < nx_ ; ix++ ) { |
---|
[2371] | 1205 | dir(0) = center_(0) - ( cpix(0) - (Double)ix ) * cellx_ ; |
---|
[2361] | 1206 | for ( Int ipol = 0 ; ipol < npol_ ; ipol++ ) { |
---|
[2371] | 1207 | offset = ix + iy * nx_ + ipol * nx_ * ny_ ; |
---|
| 1208 | //os << "offset = " << offset << LogIO::POST ; |
---|
| 1209 | sp_p = sp.getStorage( bsp ) ; |
---|
| 1210 | wsp_p = sp_p ; |
---|
| 1211 | wdata_p = data_p + offset ; |
---|
| 1212 | for ( Int ichan = 0 ; ichan < nchan_ ; ichan++ ) { |
---|
| 1213 | *wsp_p = *wdata_p ; |
---|
| 1214 | wsp_p++ ; |
---|
| 1215 | wdata_p += step ; |
---|
| 1216 | } |
---|
| 1217 | sp.putStorage( sp_p, bsp ) ; |
---|
[2356] | 1218 | spectraCol.put( irow, sp ) ; |
---|
| 1219 | directionCol.put( irow, dir ) ; |
---|
[2360] | 1220 | polnoCol.put( irow, pollist_[ipol] ) ; |
---|
[2356] | 1221 | irow++ ; |
---|
| 1222 | } |
---|
| 1223 | } |
---|
| 1224 | } |
---|
[2371] | 1225 | data_.freeStorage( data_p, bdata ) ; |
---|
[2368] | 1226 | |
---|
| 1227 | t1 = mathutil::gettimeofday_sec() ; |
---|
| 1228 | os << "saveData: elapsed time is " << t1-t0 << " sec." << LogIO::POST ; |
---|
[2374] | 1229 | |
---|
[2356] | 1230 | return outfile_ ; |
---|
| 1231 | } |
---|
| 1232 | |
---|
[2371] | 1233 | void STGrid::prepareTable( Table &tab, String &name ) |
---|
| 1234 | { |
---|
| 1235 | Table t( infile_, Table::Old ) ; |
---|
| 1236 | t.deepCopy( name, Table::New, False, t.endianFormat(), True ) ; |
---|
| 1237 | tab = Table( name, Table::Update ) ; |
---|
[2356] | 1238 | } |
---|
[2379] | 1239 | |
---|
[2371] | 1240 | } |
---|