[299] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // cubes.cc: Member functions for the DataArray, Cube and Image classes. |
---|
| 3 | // ----------------------------------------------------------------------- |
---|
| 4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 5 | // |
---|
| 6 | // This program is free software; you can redistribute it and/or modify it |
---|
| 7 | // under the terms of the GNU General Public License as published by the |
---|
| 8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 9 | // option) any later version. |
---|
| 10 | // |
---|
| 11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 14 | // for more details. |
---|
| 15 | // |
---|
| 16 | // You should have received a copy of the GNU General Public License |
---|
| 17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 19 | // |
---|
| 20 | // Correspondence concerning Duchamp may be directed to: |
---|
| 21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 22 | // Postal address: Dr. Matthew Whiting |
---|
| 23 | // Australia Telescope National Facility, CSIRO |
---|
| 24 | // PO Box 76 |
---|
| 25 | // Epping NSW 1710 |
---|
| 26 | // AUSTRALIA |
---|
| 27 | // ----------------------------------------------------------------------- |
---|
[136] | 28 | #include <unistd.h> |
---|
[3] | 29 | #include <iostream> |
---|
| 30 | #include <iomanip> |
---|
| 31 | #include <vector> |
---|
[212] | 32 | #include <algorithm> |
---|
[3] | 33 | #include <string> |
---|
[211] | 34 | #include <math.h> |
---|
[136] | 35 | |
---|
[394] | 36 | #include <wcslib/wcs.h> |
---|
[136] | 37 | |
---|
[393] | 38 | #include <duchamp/pgheader.hh> |
---|
[263] | 39 | |
---|
[393] | 40 | #include <duchamp/duchamp.hh> |
---|
| 41 | #include <duchamp/param.hh> |
---|
| 42 | #include <duchamp/fitsHeader.hh> |
---|
| 43 | #include <duchamp/Cubes/cubes.hh> |
---|
| 44 | #include <duchamp/PixelMap/Voxel.hh> |
---|
| 45 | #include <duchamp/PixelMap/Object3D.hh> |
---|
| 46 | #include <duchamp/Detection/detection.hh> |
---|
| 47 | #include <duchamp/Detection/columns.hh> |
---|
[582] | 48 | #include <duchamp/Detection/finders.hh> |
---|
[393] | 49 | #include <duchamp/Utils/utils.hh> |
---|
[777] | 50 | #include <duchamp/Utils/feedback.hh> |
---|
[393] | 51 | #include <duchamp/Utils/mycpgplot.hh> |
---|
| 52 | #include <duchamp/Utils/Statistics.hh> |
---|
[789] | 53 | #include <duchamp/FitsIO/DuchampBeam.hh> |
---|
[378] | 54 | |
---|
[146] | 55 | using namespace mycpgplot; |
---|
[190] | 56 | using namespace Statistics; |
---|
[258] | 57 | using namespace PixelInfo; |
---|
[3] | 58 | |
---|
[263] | 59 | #ifdef TEST_DEBUG |
---|
| 60 | const bool TESTING=true; |
---|
| 61 | #else |
---|
| 62 | const bool TESTING=false; |
---|
| 63 | #endif |
---|
| 64 | |
---|
[378] | 65 | namespace duchamp |
---|
| 66 | { |
---|
[3] | 67 | |
---|
[378] | 68 | using namespace Column; |
---|
[220] | 69 | |
---|
[378] | 70 | /****************************************************************/ |
---|
| 71 | /////////////////////////////////////////////////// |
---|
| 72 | //// Functions for DataArray class: |
---|
| 73 | /////////////////////////////////////////////////// |
---|
[220] | 74 | |
---|
[378] | 75 | DataArray::DataArray(){ |
---|
[528] | 76 | /// Fundamental constructor for DataArray. |
---|
| 77 | /// Number of dimensions and pixels are set to 0. Nothing else allocated. |
---|
| 78 | |
---|
[378] | 79 | this->numDim=0; |
---|
| 80 | this->numPixels=0; |
---|
| 81 | this->objectList = new std::vector<Detection>; |
---|
[443] | 82 | this->axisDimAllocated = false; |
---|
[444] | 83 | this->arrayAllocated = false; |
---|
[419] | 84 | } |
---|
[378] | 85 | //-------------------------------------------------------------------- |
---|
[139] | 86 | |
---|
[736] | 87 | DataArray::DataArray(const DataArray &d){ |
---|
| 88 | operator=(d); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | DataArray& DataArray::operator=(const DataArray &d){ |
---|
[737] | 92 | if(this==&d) return *this; |
---|
[736] | 93 | this->numDim = d.numDim; |
---|
[737] | 94 | if(this->axisDimAllocated) delete [] this->axisDim; |
---|
[736] | 95 | this->axisDimAllocated = d.axisDimAllocated; |
---|
[737] | 96 | if(this->axisDimAllocated){ |
---|
[884] | 97 | this->axisDim = new size_t[this->numDim]; |
---|
[741] | 98 | for(size_t i=0;i<size_t(this->numDim);i++) this->axisDim[i] = d.axisDim[i]; |
---|
[737] | 99 | } |
---|
[736] | 100 | this->numPixels = d.numPixels; |
---|
[737] | 101 | if(this->arrayAllocated) delete [] this->array; |
---|
[736] | 102 | this->arrayAllocated = d.arrayAllocated; |
---|
[737] | 103 | if(this->arrayAllocated) { |
---|
| 104 | this->array = new float[this->numPixels]; |
---|
[741] | 105 | for(size_t i=0;i<size_t(this->numPixels);i++) this->array[i] = d.array[i]; |
---|
[737] | 106 | } |
---|
[736] | 107 | this->objectList = d.objectList; |
---|
| 108 | this->par = d.par; |
---|
| 109 | this->Stats = d.Stats; |
---|
| 110 | return *this; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | |
---|
[378] | 114 | DataArray::DataArray(short int nDim){ |
---|
[528] | 115 | /// @details |
---|
| 116 | /// N-dimensional constructor for DataArray. |
---|
| 117 | /// Number of dimensions defined, and dimension array allocated. |
---|
| 118 | /// Number of pixels are set to 0. |
---|
| 119 | /// \param nDim Number of dimensions. |
---|
| 120 | |
---|
[443] | 121 | this->axisDimAllocated = false; |
---|
[444] | 122 | this->arrayAllocated = false; |
---|
[443] | 123 | if(nDim>0){ |
---|
[884] | 124 | this->axisDim = new size_t[nDim]; |
---|
[443] | 125 | this->axisDimAllocated = true; |
---|
| 126 | } |
---|
[378] | 127 | this->numDim=nDim; |
---|
| 128 | this->numPixels=0; |
---|
| 129 | this->objectList = new std::vector<Detection>; |
---|
[419] | 130 | } |
---|
[378] | 131 | //-------------------------------------------------------------------- |
---|
[3] | 132 | |
---|
[884] | 133 | DataArray::DataArray(short int nDim, size_t size){ |
---|
[528] | 134 | /// @details |
---|
| 135 | /// N-dimensional constructor for DataArray. |
---|
| 136 | /// Number of dimensions and number of pixels defined. |
---|
| 137 | /// Arrays allocated based on these values. |
---|
| 138 | /// \param nDim Number of dimensions. |
---|
| 139 | /// \param size Number of pixels. |
---|
| 140 | /// |
---|
| 141 | /// Note that we can assign values to the dimension array. |
---|
[378] | 142 | |
---|
[443] | 143 | this->axisDimAllocated = false; |
---|
[444] | 144 | this->arrayAllocated = false; |
---|
[913] | 145 | if(size<0){ |
---|
| 146 | DUCHAMPERROR("DataArray(nDim,size)", "Negative size -- could not define DataArray"); |
---|
| 147 | } |
---|
| 148 | else if(nDim<0){ |
---|
| 149 | DUCHAMPERROR("DataArray(nDim,size)", "Negative number of dimensions: could not define DataArray"); |
---|
| 150 | } |
---|
[378] | 151 | else { |
---|
[443] | 152 | if(size>0){ |
---|
| 153 | this->array = new float[size]; |
---|
[444] | 154 | this->arrayAllocated = true; |
---|
| 155 | } |
---|
| 156 | this->numPixels = size; |
---|
| 157 | if(nDim>0){ |
---|
[884] | 158 | this->axisDim = new size_t[nDim]; |
---|
[443] | 159 | this->axisDimAllocated = true; |
---|
| 160 | } |
---|
[378] | 161 | this->numDim = nDim; |
---|
[139] | 162 | } |
---|
[378] | 163 | this->objectList = new std::vector<Detection>; |
---|
[3] | 164 | } |
---|
[378] | 165 | //-------------------------------------------------------------------- |
---|
[3] | 166 | |
---|
[884] | 167 | DataArray::DataArray(short int nDim, size_t *dimensions) |
---|
[378] | 168 | { |
---|
[528] | 169 | /// @details |
---|
| 170 | /// Most robust constructor for DataArray. |
---|
| 171 | /// Number and sizes of dimensions are defined, and hence the number of |
---|
| 172 | /// pixels. Arrays allocated based on these values. |
---|
| 173 | /// \param nDim Number of dimensions. |
---|
| 174 | /// \param dimensions Array giving sizes of dimensions. |
---|
| 175 | |
---|
[443] | 176 | this->axisDimAllocated = false; |
---|
[444] | 177 | this->arrayAllocated = false; |
---|
[913] | 178 | if(nDim<0){ |
---|
| 179 | DUCHAMPERROR("DataArray(nDim,dimArray)", "Negative number of dimensions: could not define DataArray"); |
---|
| 180 | } |
---|
[378] | 181 | else { |
---|
| 182 | int size = dimensions[0]; |
---|
| 183 | for(int i=1;i<nDim;i++) size *= dimensions[i]; |
---|
[913] | 184 | if(size<0){ |
---|
| 185 | DUCHAMPERROR("DataArray(nDim,dimArray)", "Negative size: could not define DataArray"); |
---|
| 186 | } |
---|
[378] | 187 | else{ |
---|
| 188 | this->numPixels = size; |
---|
[443] | 189 | if(size>0){ |
---|
| 190 | this->array = new float[size]; |
---|
[444] | 191 | this->arrayAllocated = true; |
---|
| 192 | } |
---|
| 193 | this->numDim=nDim; |
---|
| 194 | if(nDim>0){ |
---|
[884] | 195 | this->axisDim = new size_t[nDim]; |
---|
[443] | 196 | this->axisDimAllocated = true; |
---|
| 197 | } |
---|
[378] | 198 | for(int i=0;i<nDim;i++) this->axisDim[i] = dimensions[i]; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | //-------------------------------------------------------------------- |
---|
[137] | 203 | |
---|
[378] | 204 | DataArray::~DataArray() |
---|
| 205 | { |
---|
[528] | 206 | /// @details |
---|
| 207 | /// Destructor -- arrays deleted if they have been allocated, and the |
---|
| 208 | /// object list is deleted. |
---|
| 209 | |
---|
[444] | 210 | if(this->numPixels>0 && this->arrayAllocated){ |
---|
| 211 | delete [] this->array; |
---|
| 212 | this->arrayAllocated = false; |
---|
| 213 | } |
---|
[443] | 214 | if(this->numDim>0 && this->axisDimAllocated){ |
---|
| 215 | delete [] this->axisDim; |
---|
[468] | 216 | this->axisDimAllocated = false; |
---|
[443] | 217 | } |
---|
[378] | 218 | delete this->objectList; |
---|
| 219 | } |
---|
| 220 | //-------------------------------------------------------------------- |
---|
| 221 | //-------------------------------------------------------------------- |
---|
[220] | 222 | |
---|
[884] | 223 | void DataArray::getDim(size_t &x, size_t &y, size_t &z) |
---|
[528] | 224 | { |
---|
| 225 | /// @details |
---|
| 226 | /// The sizes of the first three dimensions (if they exist) are returned. |
---|
| 227 | /// \param x The first dimension. Defaults to 0 if numDim \f$\le\f$ 0. |
---|
| 228 | /// \param y The second dimension. Defaults to 1 if numDim \f$\le\f$ 1. |
---|
| 229 | /// \param z The third dimension. Defaults to 1 if numDim \f$\le\f$ 2. |
---|
| 230 | |
---|
[378] | 231 | if(this->numDim>0) x=this->axisDim[0]; |
---|
| 232 | else x=0; |
---|
| 233 | if(this->numDim>1) y=this->axisDim[1]; |
---|
| 234 | else y=1; |
---|
| 235 | if(this->numDim>2) z=this->axisDim[2]; |
---|
| 236 | else z=1; |
---|
| 237 | } |
---|
| 238 | //-------------------------------------------------------------------- |
---|
[3] | 239 | |
---|
[884] | 240 | void DataArray::getDimArray(size_t *output) |
---|
[528] | 241 | { |
---|
| 242 | /// @details |
---|
| 243 | /// The axisDim array is written to output. This needs to be defined |
---|
| 244 | /// beforehand: no checking is done on the memory. |
---|
| 245 | /// \param output The array that is written to. |
---|
| 246 | |
---|
[378] | 247 | for(int i=0;i<this->numDim;i++) output[i] = this->axisDim[i]; |
---|
| 248 | } |
---|
| 249 | //-------------------------------------------------------------------- |
---|
[3] | 250 | |
---|
[528] | 251 | void DataArray::getArray(float *output) |
---|
| 252 | { |
---|
| 253 | /// @details |
---|
| 254 | /// The pixel value array is written to output. This needs to be defined |
---|
| 255 | /// beforehand: no checking is done on the memory. |
---|
| 256 | /// \param output The array that is written to. |
---|
| 257 | |
---|
[894] | 258 | for(size_t i=0;i<this->numPixels;i++) output[i] = this->array[i]; |
---|
[139] | 259 | } |
---|
[378] | 260 | //-------------------------------------------------------------------- |
---|
[3] | 261 | |
---|
[884] | 262 | void DataArray::saveArray(float *input, size_t size) |
---|
[528] | 263 | { |
---|
| 264 | /// @details |
---|
| 265 | /// Saves the array in input to the pixel array DataArray::array. |
---|
| 266 | /// The size of the array given must be the same as the current number of |
---|
| 267 | /// pixels, else an error message is returned and nothing is done. |
---|
| 268 | /// \param input The array of values to be saved. |
---|
| 269 | /// \param size The size of input. |
---|
| 270 | |
---|
[913] | 271 | if(size != this->numPixels){ |
---|
| 272 | DUCHAMPERROR("DataArray::saveArray", "Input array different size to existing array. Cannot save."); |
---|
| 273 | } |
---|
[378] | 274 | else { |
---|
[444] | 275 | if(this->numPixels>0 && this->arrayAllocated) delete [] this->array; |
---|
[378] | 276 | this->numPixels = size; |
---|
| 277 | this->array = new float[size]; |
---|
[444] | 278 | this->arrayAllocated = true; |
---|
[894] | 279 | for(size_t i=0;i<size;i++) this->array[i] = input[i]; |
---|
[378] | 280 | } |
---|
| 281 | } |
---|
| 282 | //-------------------------------------------------------------------- |
---|
[3] | 283 | |
---|
[528] | 284 | void DataArray::addObject(Detection object) |
---|
| 285 | { |
---|
| 286 | /// \param object The object to be added to the object list. |
---|
| 287 | |
---|
[378] | 288 | // objectList is a vector, so just use push_back() |
---|
| 289 | this->objectList->push_back(object); |
---|
| 290 | } |
---|
| 291 | //-------------------------------------------------------------------- |
---|
[3] | 292 | |
---|
[528] | 293 | void DataArray::addObjectList(std::vector <Detection> newlist) |
---|
| 294 | { |
---|
| 295 | /// \param newlist The list of objects to be added to the object list. |
---|
| 296 | |
---|
[623] | 297 | std::vector<Detection>::iterator obj; |
---|
| 298 | for(obj=newlist.begin();obj<newlist.end();obj++) this->objectList->push_back(*obj); |
---|
[378] | 299 | } |
---|
| 300 | //-------------------------------------------------------------------- |
---|
[3] | 301 | |
---|
[528] | 302 | bool DataArray::isDetection(float value) |
---|
| 303 | { |
---|
| 304 | /// @details |
---|
| 305 | /// Is a given value a detection, based on the statistics in the |
---|
| 306 | /// DataArray's StatsContainer? |
---|
| 307 | /// \param value The pixel value to test. |
---|
| 308 | |
---|
[378] | 309 | if(par.isBlank(value)) return false; |
---|
| 310 | else return Stats.isDetection(value); |
---|
[419] | 311 | } |
---|
[378] | 312 | //-------------------------------------------------------------------- |
---|
[220] | 313 | |
---|
[884] | 314 | bool DataArray::isDetection(size_t voxel) |
---|
[528] | 315 | { |
---|
| 316 | /// @details |
---|
| 317 | /// Is a given pixel a detection, based on the statistics in the |
---|
| 318 | /// DataArray's StatsContainer? |
---|
| 319 | /// If the pixel lies outside the valid range for the data array, return false. |
---|
| 320 | /// \param voxel Location of the DataArray's pixel to be tested. |
---|
| 321 | |
---|
[378] | 322 | if((voxel<0)||(voxel>this->numPixels)) return false; |
---|
| 323 | else if(par.isBlank(this->array[voxel])) return false; |
---|
| 324 | else return Stats.isDetection(this->array[voxel]); |
---|
[419] | 325 | } |
---|
[378] | 326 | //-------------------------------------------------------------------- |
---|
| 327 | |
---|
| 328 | std::ostream& operator<< ( std::ostream& theStream, DataArray &array) |
---|
| 329 | { |
---|
[528] | 330 | /// @details |
---|
| 331 | /// A way to print out the pixel coordinates & flux values of the |
---|
| 332 | /// list of detected objects belonging to the DataArray. |
---|
| 333 | /// These are formatted nicely according to the << operator for Detection, |
---|
| 334 | /// with a line indicating the number of detections at the start. |
---|
| 335 | /// \param theStream The ostream object to which the output should be sent. |
---|
| 336 | /// \param array The DataArray containing the list of objects. |
---|
| 337 | |
---|
[378] | 338 | for(int i=0;i<array.numDim;i++){ |
---|
| 339 | if(i>0) theStream<<"x"; |
---|
| 340 | theStream<<array.axisDim[i]; |
---|
| 341 | } |
---|
| 342 | theStream<<std::endl; |
---|
[475] | 343 | |
---|
| 344 | theStream<<"Threshold\tmiddle\tspread\trobust\n" << array.stats().getThreshold() << "\t"; |
---|
| 345 | if(array.pars().getFlagUserThreshold()) |
---|
| 346 | theStream << "0.0000\t" << array.stats().getThreshold() << "\t"; |
---|
| 347 | else |
---|
| 348 | theStream << array.stats().getMiddle() << " " << array.stats().getSpread() << "\t"; |
---|
| 349 | theStream << array.stats().getRobust()<<"\n"; |
---|
| 350 | |
---|
[378] | 351 | theStream<<array.objectList->size()<<" detections:\n--------------\n"; |
---|
[623] | 352 | std::vector<Detection>::iterator obj; |
---|
| 353 | for(obj=array.objectList->begin();obj<array.objectList->end();obj++){ |
---|
| 354 | theStream << "Detection #" << obj->getID()<<std::endl; |
---|
| 355 | Detection *newobj = new Detection; |
---|
| 356 | *newobj = *obj; |
---|
| 357 | newobj->addOffsets(); |
---|
| 358 | theStream<<*newobj; |
---|
| 359 | delete newobj; |
---|
[378] | 360 | } |
---|
| 361 | theStream<<"--------------\n"; |
---|
| 362 | return theStream; |
---|
[3] | 363 | } |
---|
| 364 | |
---|
[378] | 365 | /****************************************************************/ |
---|
| 366 | ///////////////////////////////////////////////////////////// |
---|
| 367 | //// Functions for Cube class |
---|
| 368 | ///////////////////////////////////////////////////////////// |
---|
[3] | 369 | |
---|
[528] | 370 | Cube::Cube() |
---|
| 371 | { |
---|
| 372 | /// @details |
---|
| 373 | /// Basic Constructor for Cube class. |
---|
| 374 | /// numDim set to 3, but numPixels to 0 and all bool flags to false. |
---|
| 375 | /// No allocation done. |
---|
| 376 | |
---|
[220] | 377 | numPixels=0; numDim=3; |
---|
| 378 | reconExists = false; reconAllocated = false; baselineAllocated = false; |
---|
[419] | 379 | } |
---|
[378] | 380 | //-------------------------------------------------------------------- |
---|
[3] | 381 | |
---|
[884] | 382 | Cube::Cube(size_t size) |
---|
[528] | 383 | { |
---|
| 384 | /// @details |
---|
| 385 | /// Alternative Cube constructor, where size is given but not individual |
---|
| 386 | /// dimensions. Arrays are allocated as appropriate (according to the |
---|
| 387 | /// relevant flags in Param set), but the Cube::axisDim array is not. |
---|
| 388 | |
---|
[378] | 389 | this->reconAllocated = false; |
---|
| 390 | this->baselineAllocated = false; |
---|
[443] | 391 | this->axisDimAllocated = false; |
---|
[444] | 392 | this->arrayAllocated = false; |
---|
[378] | 393 | this->numPixels = this->numDim = 0; |
---|
[913] | 394 | if(size<0){ |
---|
| 395 | DUCHAMPERROR("Cube(size)","Negative size -- could not define Cube"); |
---|
| 396 | } |
---|
[378] | 397 | else{ |
---|
| 398 | if(size>0){ |
---|
| 399 | this->array = new float[size]; |
---|
[444] | 400 | this->arrayAllocated = true; |
---|
[378] | 401 | if(this->par.getFlagATrous()||this->par.getFlagSmooth()){ |
---|
| 402 | this->recon = new float[size]; |
---|
| 403 | this->reconAllocated = true; |
---|
| 404 | } |
---|
| 405 | if(this->par.getFlagBaseline()){ |
---|
| 406 | this->baseline = new float[size]; |
---|
| 407 | this->baselineAllocated = true; |
---|
| 408 | } |
---|
[219] | 409 | } |
---|
[378] | 410 | this->numPixels = size; |
---|
[884] | 411 | this->axisDim = new size_t[3]; |
---|
[444] | 412 | this->axisDimAllocated = true; |
---|
[378] | 413 | this->numDim = 3; |
---|
| 414 | this->reconExists = false; |
---|
[139] | 415 | } |
---|
[3] | 416 | } |
---|
[378] | 417 | //-------------------------------------------------------------------- |
---|
[3] | 418 | |
---|
[884] | 419 | Cube::Cube(size_t *dimensions) |
---|
[528] | 420 | { |
---|
| 421 | /// Alternative Cube constructor, where sizes of dimensions are given. |
---|
| 422 | /// Arrays are allocated as appropriate (according to the |
---|
| 423 | /// relevant flags in Param set), as is the Cube::axisDim array. |
---|
| 424 | |
---|
[378] | 425 | int size = dimensions[0] * dimensions[1] * dimensions[2]; |
---|
| 426 | int imsize = dimensions[0] * dimensions[1]; |
---|
| 427 | this->reconAllocated = false; |
---|
| 428 | this->baselineAllocated = false; |
---|
[443] | 429 | this->axisDimAllocated = false; |
---|
[444] | 430 | this->arrayAllocated = false; |
---|
[378] | 431 | this->numPixels = this->numDim = 0; |
---|
[913] | 432 | if((size<0) || (imsize<0) ){ |
---|
| 433 | DUCHAMPERROR("Cube(dimArray)","Negative size -- could not define Cube"); |
---|
| 434 | } |
---|
[378] | 435 | else{ |
---|
| 436 | this->numPixels = size; |
---|
| 437 | if(size>0){ |
---|
| 438 | this->array = new float[size]; |
---|
[502] | 439 | this->arrayAllocated = true; |
---|
[378] | 440 | this->detectMap = new short[imsize]; |
---|
| 441 | if(this->par.getFlagATrous()||this->par.getFlagSmooth()){ |
---|
| 442 | this->recon = new float[size]; |
---|
| 443 | this->reconAllocated = true; |
---|
| 444 | } |
---|
| 445 | if(this->par.getFlagBaseline()){ |
---|
| 446 | this->baseline = new float[size]; |
---|
| 447 | this->baselineAllocated = true; |
---|
| 448 | } |
---|
[205] | 449 | } |
---|
[378] | 450 | this->numDim = 3; |
---|
[884] | 451 | this->axisDim = new size_t[3]; |
---|
[443] | 452 | this->axisDimAllocated = true; |
---|
[378] | 453 | for(int i=0;i<3 ;i++) this->axisDim[i] = dimensions[i]; |
---|
| 454 | for(int i=0;i<imsize;i++) this->detectMap[i] = 0; |
---|
| 455 | this->reconExists = false; |
---|
[139] | 456 | } |
---|
[3] | 457 | } |
---|
[378] | 458 | //-------------------------------------------------------------------- |
---|
[3] | 459 | |
---|
[378] | 460 | Cube::~Cube() |
---|
| 461 | { |
---|
[528] | 462 | /// @details |
---|
| 463 | /// The destructor deletes the memory allocated for Cube::detectMap, and, |
---|
| 464 | /// if these have been allocated, Cube::recon and Cube::baseline. |
---|
| 465 | |
---|
[502] | 466 | if(this->numPixels>0 && this->arrayAllocated) delete [] this->detectMap; |
---|
[378] | 467 | if(this->reconAllocated) delete [] this->recon; |
---|
| 468 | if(this->baselineAllocated) delete [] this->baseline; |
---|
| 469 | } |
---|
| 470 | //-------------------------------------------------------------------- |
---|
[137] | 471 | |
---|
[736] | 472 | Cube::Cube(const Cube &c): |
---|
| 473 | DataArray(c) |
---|
| 474 | { |
---|
| 475 | this->operator=(c); |
---|
| 476 | } |
---|
[877] | 477 | //-------------------------------------------------------------------- |
---|
[736] | 478 | |
---|
[737] | 479 | Cube& Cube::operator=(const Cube &c) |
---|
[736] | 480 | { |
---|
[737] | 481 | if(this==&c) return *this; |
---|
[736] | 482 | if(this->arrayAllocated) delete [] this->detectMap; |
---|
[737] | 483 | ((DataArray &) *this) = c; |
---|
| 484 | this->reconExists = c.reconExists; |
---|
| 485 | if(this->reconAllocated) delete [] this->recon; |
---|
| 486 | this->reconAllocated = c.reconAllocated; |
---|
| 487 | if(this->reconAllocated) { |
---|
| 488 | this->recon = new float[this->numPixels]; |
---|
[741] | 489 | for(size_t i=0;i<size_t(this->numPixels);i++) this->recon[i] = c.recon[i]; |
---|
[737] | 490 | } |
---|
| 491 | if(this->arrayAllocated){ |
---|
| 492 | this->detectMap = new short[this->axisDim[0]*this->axisDim[1]]; |
---|
[741] | 493 | for(size_t i=0;i<size_t(this->axisDim[0]*this->axisDim[1]);i++) this->detectMap[i] = c.detectMap[i]; |
---|
[737] | 494 | } |
---|
| 495 | if(this->baselineAllocated) delete [] this->baseline; |
---|
| 496 | this->baselineAllocated = c.baselineAllocated; |
---|
| 497 | if(this->baselineAllocated){ |
---|
| 498 | this->baseline = new float[this->numPixels]; |
---|
[741] | 499 | for(size_t i=0;i<size_t(this->numPixels);i++) this->baseline[i] = c.baseline[i]; |
---|
[737] | 500 | } |
---|
| 501 | this->head = c.head; |
---|
| 502 | this->fullCols = c.fullCols; |
---|
| 503 | this->logCols = c.logCols; |
---|
[736] | 504 | return *this; |
---|
| 505 | } |
---|
[877] | 506 | //-------------------------------------------------------------------- |
---|
[736] | 507 | |
---|
[878] | 508 | Cube* Cube::slice(Section subsection) |
---|
[877] | 509 | { |
---|
[878] | 510 | Cube *output = new Cube; |
---|
[882] | 511 | Section thisSection; |
---|
| 512 | std::string nullsec=nullSection(this->numDim); |
---|
| 513 | if(this->par.section().isParsed()) thisSection=this->par.section(); |
---|
| 514 | else{ |
---|
| 515 | thisSection = Section(nullsec); |
---|
| 516 | thisSection.parse(this->axisDim, this->numDim); |
---|
| 517 | } |
---|
| 518 | |
---|
[877] | 519 | subsection.parse(this->axisDim, this->numDim); |
---|
| 520 | if(subsection.isValid()){ |
---|
[878] | 521 | output->par = this->par; |
---|
[882] | 522 | output->par.section() = thisSection * subsection; |
---|
| 523 | output->par.setXOffset(output->par.getXOffset()+subsection.getStart(0)); |
---|
| 524 | output->par.setYOffset(output->par.getYOffset()+subsection.getStart(1)); |
---|
| 525 | output->par.setZOffset(output->par.getZOffset()+subsection.getStart(2)); |
---|
[878] | 526 | output->head = this->head; |
---|
[883] | 527 | // correct the reference pixel in the WCS |
---|
| 528 | output->head.WCS().crpix[output->head.WCS().lng] -= subsection.getStart(output->head.WCS().lng); |
---|
| 529 | output->head.WCS().crpix[output->head.WCS().lat] -= subsection.getStart(output->head.WCS().lat); |
---|
| 530 | if(output->head.WCS().spec>0) |
---|
| 531 | output->head.WCS().crpix[output->head.WCS().spec] -= subsection.getStart(output->head.WCS().spec); |
---|
[878] | 532 | output->Stats = this->Stats; |
---|
| 533 | output->fullCols = this->fullCols; |
---|
| 534 | output->logCols = this->logCols; |
---|
[884] | 535 | size_t *dims = new size_t[3]; |
---|
[877] | 536 | for(size_t i=0;i<3;i++){ |
---|
| 537 | dims[i] = subsection.getDimList()[i]; |
---|
| 538 | std::cout << "Dim " << i+1 << " = " << dims[i] << "\n"; |
---|
| 539 | } |
---|
[878] | 540 | |
---|
| 541 | output->initialiseCube(dims,true); |
---|
| 542 | for(size_t z=0;z<output->axisDim[2];z++){ |
---|
| 543 | for(size_t y=0;y<output->axisDim[1];y++){ |
---|
| 544 | for(size_t x=0;x<output->axisDim[0];x++){ |
---|
| 545 | size_t impos=x+y*output->axisDim[0]; |
---|
| 546 | size_t pos=impos+z*output->axisDim[0]*output->axisDim[1]; |
---|
[913] | 547 | if(pos>=output->numPixels) DUCHAMPERROR("cube slicer","Out of bounds in new Cube"); |
---|
[877] | 548 | size_t imposIn=(x+subsection.getStart(0)) + (y+subsection.getStart(1))*this->axisDim[0]; |
---|
| 549 | size_t posIn=imposIn + (z+subsection.getStart(2))*this->axisDim[0]*this->axisDim[1]; |
---|
[913] | 550 | if(posIn>=this->numPixels) DUCHAMPERROR("cube slicer","Out of bounds in new Cube"); |
---|
[878] | 551 | output->array[pos] = this->array[posIn]; |
---|
| 552 | output->detectMap[impos] = this->detectMap[imposIn]; |
---|
| 553 | if(this->reconAllocated) output->recon[pos] = this->recon[posIn]; |
---|
| 554 | if(this->baselineAllocated) output->baseline[pos] = this->baseline[posIn]; |
---|
[877] | 555 | } |
---|
| 556 | } |
---|
| 557 | } |
---|
[878] | 558 | std::cout << this->par << "\n"<<output->par <<"\n"; |
---|
[877] | 559 | } |
---|
[878] | 560 | else{ |
---|
[913] | 561 | DUCHAMPERROR("cube slicer","Subsection does not parse"); |
---|
[878] | 562 | } |
---|
[877] | 563 | |
---|
| 564 | return output; |
---|
| 565 | |
---|
| 566 | } |
---|
| 567 | //-------------------------------------------------------------------- |
---|
| 568 | |
---|
[679] | 569 | OUTCOME Cube::initialiseCube(long *dimensions, bool allocateArrays) |
---|
[378] | 570 | { |
---|
[884] | 571 | int numAxes = this->head.getNumAxes(); |
---|
| 572 | if(numAxes<=0) numAxes=3; |
---|
| 573 | size_t *dim = new size_t[numAxes]; |
---|
| 574 | for(int i=0;i<numAxes;i++) dim[i]=dimensions[i]; |
---|
[894] | 575 | OUTCOME outcome=this->initialiseCube(dim,allocateArrays); |
---|
[895] | 576 | delete [] dim; |
---|
[894] | 577 | return outcome; |
---|
[884] | 578 | } |
---|
| 579 | |
---|
| 580 | |
---|
| 581 | OUTCOME Cube::initialiseCube(size_t *dimensions, bool allocateArrays) |
---|
| 582 | { |
---|
[528] | 583 | /// @details |
---|
| 584 | /// This function will set the sizes of all arrays that will be used by Cube. |
---|
| 585 | /// It will also define the values of the axis dimensions: this will be done |
---|
| 586 | /// using the WCS in the FitsHeader class, so the WCS needs to be good and |
---|
| 587 | /// have three axes. If this is not the case, the axes are assumed to be |
---|
| 588 | /// ordered in the sense of lng,lat,spc. |
---|
| 589 | /// |
---|
| 590 | /// \param dimensions An array of values giving the dimensions (sizes) for |
---|
| 591 | /// all axes. |
---|
| 592 | /// \param allocateArrays A flag indicating whether to allocate |
---|
| 593 | /// the memory for the data arrays: the default is true. The |
---|
| 594 | /// dimension arrays will be allocated and filled. |
---|
[186] | 595 | |
---|
[679] | 596 | int lng,lat,spc; |
---|
[884] | 597 | size_t size,imsize; |
---|
[365] | 598 | |
---|
[467] | 599 | int numAxes = this->head.getNumAxes(); |
---|
[519] | 600 | if(numAxes<=0) numAxes=3; |
---|
[467] | 601 | |
---|
[477] | 602 | if(this->head.isWCS() && (numAxes>=3) && (this->head.WCS().spec>=0)){ |
---|
[378] | 603 | // if there is a WCS and there is at least 3 axes |
---|
| 604 | lng = this->head.WCS().lng; |
---|
| 605 | lat = this->head.WCS().lat; |
---|
| 606 | spc = this->head.WCS().spec; |
---|
| 607 | } |
---|
| 608 | else{ |
---|
| 609 | // just take dimensions[] at face value |
---|
| 610 | lng = 0; |
---|
| 611 | lat = 1; |
---|
| 612 | spc = 2; |
---|
| 613 | } |
---|
[186] | 614 | |
---|
[378] | 615 | size = dimensions[lng]; |
---|
[467] | 616 | if(numAxes>1) size *= dimensions[lat]; |
---|
| 617 | if(this->head.canUseThirdAxis() && numAxes>spc) size *= dimensions[spc]; |
---|
| 618 | |
---|
[378] | 619 | imsize = dimensions[lng]; |
---|
[467] | 620 | if(numAxes>1) imsize *= dimensions[lat]; |
---|
[271] | 621 | |
---|
[378] | 622 | this->reconAllocated = false; |
---|
| 623 | this->baselineAllocated = false; |
---|
[186] | 624 | |
---|
[443] | 625 | if(this->axisDimAllocated){ |
---|
| 626 | delete [] this->axisDim; |
---|
| 627 | this->axisDimAllocated = false; |
---|
| 628 | } |
---|
| 629 | |
---|
[444] | 630 | if(this->arrayAllocated){ |
---|
| 631 | delete [] this->array; |
---|
[726] | 632 | delete [] this->detectMap; |
---|
[444] | 633 | this->arrayAllocated = false; |
---|
| 634 | } |
---|
[726] | 635 | if(this->reconAllocated){ |
---|
| 636 | delete [] this->recon; |
---|
| 637 | this->reconAllocated = false; |
---|
| 638 | } |
---|
| 639 | if(this->baselineAllocated){ |
---|
| 640 | delete [] this->baseline; |
---|
| 641 | this->baselineAllocated = false; |
---|
| 642 | } |
---|
[444] | 643 | |
---|
[679] | 644 | if((size<0) || (imsize<0) ) { |
---|
[913] | 645 | DUCHAMPERROR("Cube::initialiseCube(dimArray)", "Negative size -- could not define Cube."); |
---|
[679] | 646 | return FAILURE; |
---|
| 647 | } |
---|
[378] | 648 | else{ |
---|
| 649 | this->numPixels = size; |
---|
[496] | 650 | this->numDim = 3; |
---|
[758] | 651 | |
---|
[884] | 652 | this->axisDim = new size_t[this->numDim]; |
---|
[496] | 653 | this->axisDimAllocated = true; |
---|
| 654 | this->axisDim[0] = dimensions[lng]; |
---|
| 655 | if(numAxes>1) this->axisDim[1] = dimensions[lat]; |
---|
| 656 | else this->axisDim[1] = 1; |
---|
| 657 | if(this->head.canUseThirdAxis() && numAxes>spc) this->axisDim[2] = dimensions[spc]; |
---|
| 658 | else this->axisDim[2] = 1; |
---|
[758] | 659 | |
---|
[977] | 660 | this->numNondegDim=0; |
---|
| 661 | for(int i=0;i<3;i++) if(this->axisDim[i]>1) this->numNondegDim++; |
---|
[817] | 662 | |
---|
[978] | 663 | if(this->numNondegDim == 1){ |
---|
[986] | 664 | if(!head.isWCS()) std::swap(this->axisDim[0],this->axisDim[2]); |
---|
[984] | 665 | imsize=this->axisDim[2]; |
---|
[978] | 666 | } |
---|
| 667 | |
---|
[986] | 668 | bool haveChanged=false; |
---|
| 669 | int change=0; |
---|
| 670 | if(this->par.getMinPix() > this->axisDim[0]*this->axisDim[1]){ |
---|
| 671 | DUCHAMPWARN("Cube::initialiseCube", "The value of minPix ("<<this->par.getMinPix()<<") is greater than the image size. Setting to "<<this->axisDim[0]*this->axisDim[1]); |
---|
| 672 | change=this->par.getMinPix() - this->axisDim[0]*this->axisDim[1]; |
---|
| 673 | haveChanged=true; |
---|
| 674 | this->par.setMinPix(this->axisDim[0]*this->axisDim[1]); |
---|
| 675 | } |
---|
| 676 | if(this->par.getMinChannels() > this->axisDim[2]){ |
---|
| 677 | DUCHAMPWARN("Cube::initialiseCube", "The value of minChannels ("<<this->par.getMinChannels()<<") is greater than the spectral size. Setting to "<<this->axisDim[2]); |
---|
| 678 | change=this->par.getMinPix() - this->axisDim[0]*this->axisDim[1]; |
---|
| 679 | haveChanged=true; |
---|
| 680 | this->par.setMinChannels(this->axisDim[2]); |
---|
| 681 | } |
---|
| 682 | if(haveChanged){ |
---|
| 683 | DUCHAMPWARN("Cube::initialiseCube","Reducing minVoxels to "<<this->par.getMinVoxels() - change<<" to accomodate these changes" ); |
---|
| 684 | this->par.setMinVoxels(this->par.getMinVoxels() - change); |
---|
| 685 | } |
---|
| 686 | |
---|
[978] | 687 | if(this->par.getFlagSmooth()){ |
---|
[977] | 688 | if(this->par.getSmoothType()=="spectral" && this->numNondegDim==2){ |
---|
[913] | 689 | DUCHAMPWARN("Cube::initialiseCube", "Spectral smooth requested, but have a 2D image. Setting flagSmooth=false"); |
---|
[817] | 690 | this->par.setFlagSmooth(false); |
---|
| 691 | } |
---|
[977] | 692 | if(this->par.getSmoothType()=="spatial" && this->numNondegDim==1){ |
---|
[913] | 693 | DUCHAMPWARN("Cube::initialiseCube", "Spatial smooth requested, but have a 1D image. Setting flagSmooth=false"); |
---|
[817] | 694 | this->par.setFlagSmooth(false); |
---|
| 695 | } |
---|
| 696 | } |
---|
| 697 | if(this->par.getFlagATrous()){ |
---|
| 698 | for(int d=3; d>=1; d--){ |
---|
[977] | 699 | if(this->par.getReconDim()==d && this->numNondegDim==(d-1)){ |
---|
[913] | 700 | DUCHAMPWARN("Cube::initialiseCube", d << "D reconstruction requested, but image is " << d-1 <<"D. Setting flagAtrous=false"); |
---|
[817] | 701 | this->par.setFlagATrous(false); |
---|
| 702 | } |
---|
| 703 | } |
---|
| 704 | } |
---|
| 705 | |
---|
[758] | 706 | if(allocateArrays && this->par.isVerbose()) this->reportMemorySize(std::cout,allocateArrays); |
---|
| 707 | |
---|
[496] | 708 | this->reconExists = false; |
---|
| 709 | if(size>0 && allocateArrays){ |
---|
[378] | 710 | this->array = new float[size]; |
---|
[444] | 711 | this->arrayAllocated = true; |
---|
[378] | 712 | this->detectMap = new short[imsize]; |
---|
[894] | 713 | for(size_t i=0;i<imsize;i++) this->detectMap[i] = 0; |
---|
[378] | 714 | if(this->par.getFlagATrous() || this->par.getFlagSmooth()){ |
---|
| 715 | this->recon = new float[size]; |
---|
| 716 | this->reconAllocated = true; |
---|
[894] | 717 | for(size_t i=0;i<size;i++) this->recon[i] = 0.; |
---|
[378] | 718 | } |
---|
| 719 | if(this->par.getFlagBaseline()){ |
---|
| 720 | this->baseline = new float[size]; |
---|
| 721 | this->baselineAllocated = true; |
---|
[894] | 722 | for(size_t i=0;i<size;i++) this->baseline[i] = 0.; |
---|
[378] | 723 | } |
---|
[205] | 724 | } |
---|
[139] | 725 | } |
---|
[679] | 726 | return SUCCESS; |
---|
[3] | 727 | } |
---|
[378] | 728 | //-------------------------------------------------------------------- |
---|
[3] | 729 | |
---|
[758] | 730 | void Cube::reportMemorySize(std::ostream &theStream, bool allocateArrays) |
---|
| 731 | { |
---|
| 732 | std::string unitlist[4]={"kB","MB","GB","TB"}; |
---|
[884] | 733 | size_t size=axisDim[0]*axisDim[1]*axisDim[2]; |
---|
| 734 | size_t imsize=axisDim[0]*axisDim[1]; |
---|
[758] | 735 | |
---|
| 736 | // Calculate and report the total size of memory to be allocated. |
---|
[884] | 737 | float allocSize=3*sizeof(size_t); // axisDim |
---|
[758] | 738 | float arrAllocSize=0.; |
---|
| 739 | if(size>0 && allocateArrays){ |
---|
| 740 | allocSize += size * sizeof(float); // array |
---|
| 741 | arrAllocSize = size*sizeof(float); |
---|
| 742 | allocSize += imsize * sizeof(short); // detectMap |
---|
| 743 | if(this->par.getFlagATrous() || this->par.getFlagSmooth()) |
---|
| 744 | allocSize += size * sizeof(float); // recon |
---|
| 745 | if(this->par.getFlagBaseline()) |
---|
| 746 | allocSize += size * sizeof(float); // baseline |
---|
| 747 | } |
---|
| 748 | std::string units="bytes"; |
---|
| 749 | for(int i=0;i<4 && allocSize>1024.;i++){ |
---|
| 750 | allocSize/=1024.; |
---|
| 751 | arrAllocSize /= 1024.; |
---|
| 752 | units=unitlist[i]; |
---|
| 753 | } |
---|
| 754 | |
---|
| 755 | theStream << "\n About to allocate " << allocSize << units; |
---|
| 756 | if(arrAllocSize > 0.) theStream << " of which " << arrAllocSize << units << " is for the image\n"; |
---|
| 757 | else theStream << "\n"; |
---|
| 758 | } |
---|
| 759 | |
---|
| 760 | |
---|
[513] | 761 | bool Cube::is2D() |
---|
| 762 | { |
---|
[528] | 763 | /// @details |
---|
| 764 | /// Check whether the image is 2-dimensional, by counting |
---|
| 765 | /// the number of dimensions that are greater than 1 |
---|
| 766 | |
---|
[513] | 767 | if(this->head.WCS().naxis==2) return true; |
---|
| 768 | else{ |
---|
| 769 | int numDim=0; |
---|
| 770 | for(int i=0;i<this->numDim;i++) if(axisDim[i]>1) numDim++; |
---|
| 771 | return numDim<=2; |
---|
| 772 | } |
---|
| 773 | |
---|
| 774 | } |
---|
| 775 | //-------------------------------------------------------------------- |
---|
| 776 | |
---|
[698] | 777 | OUTCOME Cube::getCube() |
---|
[528] | 778 | { |
---|
| 779 | /// @details |
---|
| 780 | /// A front-end to the Cube::getCube() function, that does |
---|
| 781 | /// subsection checks. |
---|
| 782 | /// Assumes the Param is set up properly. |
---|
| 783 | |
---|
[232] | 784 | std::string fname = par.getImageFile(); |
---|
[220] | 785 | if(par.getFlagSubsection()) fname+=par.getSubsection(); |
---|
| 786 | return getCube(fname); |
---|
[419] | 787 | } |
---|
[378] | 788 | //-------------------------------------------------------------------- |
---|
[220] | 789 | |
---|
[884] | 790 | void Cube::saveArray(float *input, size_t size) |
---|
[528] | 791 | { |
---|
[378] | 792 | if(size != this->numPixels){ |
---|
[913] | 793 | DUCHAMPERROR("Cube::saveArray","Input array different size to existing array (" << size << " cf. " << this->numPixels << "). Cannot save."); |
---|
[378] | 794 | } |
---|
| 795 | else { |
---|
[444] | 796 | if(this->numPixels>0 && this->arrayAllocated) delete [] array; |
---|
[378] | 797 | this->numPixels = size; |
---|
| 798 | this->array = new float[size]; |
---|
[444] | 799 | this->arrayAllocated = true; |
---|
[894] | 800 | for(size_t i=0;i<size;i++) this->array[i] = input[i]; |
---|
[378] | 801 | } |
---|
[160] | 802 | } |
---|
[378] | 803 | //-------------------------------------------------------------------- |
---|
[3] | 804 | |
---|
[528] | 805 | void Cube::saveArray(std::vector<float> &input) |
---|
| 806 | { |
---|
| 807 | /// @details |
---|
| 808 | /// Saves the array in input to the pixel array Cube::array. |
---|
| 809 | /// The size of the array given must be the same as the current number of |
---|
| 810 | /// pixels, else an error message is returned and nothing is done. |
---|
| 811 | /// \param input The array of values to be saved. |
---|
| 812 | |
---|
[884] | 813 | if(input.size() != this->numPixels){ |
---|
[913] | 814 | DUCHAMPERROR("Cube::saveArray","Input array different size to existing array (" << input.size() << " cf. " << this->numPixels << "). Cannot save."); |
---|
[491] | 815 | } |
---|
| 816 | else { |
---|
| 817 | if(this->numPixels>0 && this->arrayAllocated) delete [] this->array; |
---|
| 818 | this->numPixels = input.size(); |
---|
| 819 | this->array = new float[input.size()]; |
---|
| 820 | this->arrayAllocated = true; |
---|
[623] | 821 | for(size_t i=0;i<input.size();i++) this->array[i] = input[i]; |
---|
[491] | 822 | } |
---|
| 823 | } |
---|
| 824 | //-------------------------------------------------------------------- |
---|
| 825 | |
---|
[884] | 826 | void Cube::saveRecon(float *input, size_t size) |
---|
[528] | 827 | { |
---|
| 828 | /// @details |
---|
| 829 | /// Saves the array in input to the reconstructed array Cube::recon |
---|
| 830 | /// The size of the array given must be the same as the current number of |
---|
| 831 | /// pixels, else an error message is returned and nothing is done. |
---|
| 832 | /// If the recon array has already been allocated, it is deleted first, and |
---|
| 833 | /// then the space is allocated. |
---|
| 834 | /// Afterwards, the appropriate flags are set. |
---|
| 835 | /// \param input The array of values to be saved. |
---|
| 836 | /// \param size The size of input. |
---|
| 837 | |
---|
[378] | 838 | if(size != this->numPixels){ |
---|
[913] | 839 | DUCHAMPERROR("Cube::saveRecon","Input array different size to existing array (" << size << " cf. " << this->numPixels << "). Cannot save."); |
---|
[205] | 840 | } |
---|
[378] | 841 | else { |
---|
| 842 | if(this->numPixels>0){ |
---|
| 843 | if(this->reconAllocated) delete [] this->recon; |
---|
| 844 | this->numPixels = size; |
---|
| 845 | this->recon = new float[size]; |
---|
| 846 | this->reconAllocated = true; |
---|
[894] | 847 | for(size_t i=0;i<size;i++) this->recon[i] = input[i]; |
---|
[378] | 848 | this->reconExists = true; |
---|
| 849 | } |
---|
| 850 | } |
---|
[139] | 851 | } |
---|
[378] | 852 | //-------------------------------------------------------------------- |
---|
[3] | 853 | |
---|
[528] | 854 | void Cube::getRecon(float *output) |
---|
| 855 | { |
---|
| 856 | /// @details |
---|
| 857 | /// The reconstructed array is written to output. The output array needs to |
---|
| 858 | /// be defined beforehand: no checking is done on the memory. |
---|
| 859 | /// \param output The array that is written to. |
---|
| 860 | |
---|
[378] | 861 | // Need check for change in number of pixels! |
---|
[894] | 862 | for(size_t i=0;i<this->numPixels;i++){ |
---|
[378] | 863 | if(this->reconExists) output[i] = this->recon[i]; |
---|
| 864 | else output[i] = 0.; |
---|
| 865 | } |
---|
[3] | 866 | } |
---|
[378] | 867 | //-------------------------------------------------------------------- |
---|
[3] | 868 | |
---|
[378] | 869 | void Cube::removeMW() |
---|
| 870 | { |
---|
[528] | 871 | /// @details |
---|
| 872 | /// The channels corresponding to the Milky Way range (as given by the Param |
---|
| 873 | /// set) are all set to 0 in the pixel array. |
---|
| 874 | /// Only done if the appropriate flag is set, and the pixels are not BLANK. |
---|
| 875 | /// \deprecated |
---|
| 876 | |
---|
[378] | 877 | if(this->par.getFlagMW()){ |
---|
[894] | 878 | for(size_t pix=0;pix<this->axisDim[0]*this->axisDim[1];pix++){ |
---|
| 879 | for(size_t z=0;z<this->axisDim[2];z++){ |
---|
| 880 | size_t pos = z*this->axisDim[0]*this->axisDim[1] + pix; |
---|
[378] | 881 | if(!this->isBlank(pos) && this->par.isInMW(z)) this->array[pos]=0.; |
---|
| 882 | } |
---|
[103] | 883 | } |
---|
[86] | 884 | } |
---|
| 885 | } |
---|
[378] | 886 | //-------------------------------------------------------------------- |
---|
[86] | 887 | |
---|
[378] | 888 | void Cube::setCubeStats() |
---|
| 889 | { |
---|
[528] | 890 | /// @details |
---|
| 891 | /// Calculates the full statistics for the cube: |
---|
| 892 | /// mean, rms, median, madfm |
---|
| 893 | /// Only do this if the threshold has not been defined (ie. is still 0., |
---|
| 894 | /// its default). |
---|
| 895 | /// Also work out the threshold and store it in the par set. |
---|
| 896 | /// |
---|
| 897 | /// Different from Cube::setCubeStatsOld() as it doesn't use the |
---|
| 898 | /// getStats functions but has own versions of them hardcoded to |
---|
| 899 | /// ignore BLANKs and MW channels. This saves on memory usage -- necessary |
---|
| 900 | /// for dealing with very big files. |
---|
| 901 | /// |
---|
| 902 | /// Three cases exist: |
---|
| 903 | /// <ul><li>Simple case, with no reconstruction/smoothing: all stats |
---|
| 904 | /// calculated from the original array. |
---|
| 905 | /// <li>Wavelet reconstruction: mean & median calculated from the |
---|
| 906 | /// original array, and stddev & madfm from the residual. |
---|
| 907 | /// <li>Smoothing: all four stats calculated from the recon array |
---|
| 908 | /// (which holds the smoothed data). |
---|
| 909 | /// </ul> |
---|
[189] | 910 | |
---|
[460] | 911 | if(this->par.getFlagUserThreshold() ){ |
---|
[378] | 912 | // if the user has defined a threshold, set this in the StatsContainer |
---|
| 913 | this->Stats.setThreshold( this->par.getThreshold() ); |
---|
| 914 | } |
---|
| 915 | else{ |
---|
| 916 | // only work out the stats if we need to. |
---|
| 917 | // the only reason we don't is if the user has specified a threshold. |
---|
[205] | 918 | |
---|
[460] | 919 | this->Stats.setRobust(this->par.getFlagRobustStats()); |
---|
| 920 | |
---|
[378] | 921 | if(this->par.isVerbose()) |
---|
| 922 | std::cout << "Calculating the cube statistics... " << std::flush; |
---|
[205] | 923 | |
---|
[884] | 924 | // size_t xysize = this->axisDim[0]*this->axisDim[1]; |
---|
[211] | 925 | |
---|
[378] | 926 | bool *mask = new bool[this->numPixels]; |
---|
[894] | 927 | size_t vox=0,goodSize = 0; |
---|
| 928 | for(size_t z=0;z<this->axisDim[2];z++){ |
---|
| 929 | for(size_t y=0;y<this->axisDim[1];y++){ |
---|
| 930 | for(size_t x=0;x<this->axisDim[0];x++){ |
---|
[741] | 931 | // vox = z * xysize + y*this->axisDim[0] + x; |
---|
| 932 | bool isBlank=this->isBlank(vox); |
---|
| 933 | bool isMW = this->par.isInMW(z); |
---|
| 934 | bool statOK = this->par.isStatOK(x,y,z); |
---|
| 935 | mask[vox] = (!isBlank && !isMW && statOK ); |
---|
[378] | 936 | if(mask[vox]) goodSize++; |
---|
[741] | 937 | vox++; |
---|
[378] | 938 | } |
---|
[211] | 939 | } |
---|
[205] | 940 | } |
---|
[212] | 941 | |
---|
[649] | 942 | // float mean,median,stddev,madfm; |
---|
[378] | 943 | if( this->par.getFlagATrous() ){ |
---|
| 944 | // Case #2 -- wavelet reconstruction |
---|
| 945 | // just get mean & median from orig array, and rms & madfm from |
---|
| 946 | // residual recompute array values to be residuals & then find |
---|
| 947 | // stddev & madfm |
---|
[913] | 948 | if(!this->reconExists){ |
---|
| 949 | DUCHAMPERROR("setCubeStats", "Reconstruction not yet done! Cannot calculate stats!"); |
---|
| 950 | } |
---|
[378] | 951 | else{ |
---|
| 952 | float *tempArray = new float[goodSize]; |
---|
[211] | 953 | |
---|
[378] | 954 | goodSize=0; |
---|
[751] | 955 | vox=0; |
---|
[894] | 956 | for(size_t z=0;z<this->axisDim[2];z++){ |
---|
| 957 | for(size_t y=0;y<this->axisDim[1];y++){ |
---|
| 958 | for(size_t x=0;x<this->axisDim[0];x++){ |
---|
[741] | 959 | // vox = z * xysize + y*this->axisDim[0] + x; |
---|
[378] | 960 | if(mask[vox]) tempArray[goodSize++] = this->array[vox]; |
---|
[741] | 961 | vox++; |
---|
[378] | 962 | } |
---|
[275] | 963 | } |
---|
| 964 | } |
---|
[258] | 965 | |
---|
[378] | 966 | // First, find the mean of the original array. Store it. |
---|
[889] | 967 | this->Stats.setMean( findMean<float>(tempArray, goodSize) ); |
---|
[275] | 968 | |
---|
[378] | 969 | // Now sort it and find the median. Store it. |
---|
[889] | 970 | this->Stats.setMedian( findMedian<float>(tempArray, goodSize, true) ); |
---|
[275] | 971 | |
---|
[649] | 972 | // Now calculate the residuals and find the mean & median of |
---|
| 973 | // them. We don't store these, but they are necessary to find |
---|
| 974 | // the sttdev & madfm. |
---|
[378] | 975 | goodSize = 0; |
---|
[741] | 976 | // for(int p=0;p<xysize;p++){ |
---|
| 977 | vox=0; |
---|
[894] | 978 | for(size_t z=0;z<this->axisDim[2];z++){ |
---|
| 979 | for(size_t y=0;y<this->axisDim[1];y++){ |
---|
| 980 | for(size_t x=0;x<this->axisDim[0];x++){ |
---|
[741] | 981 | // vox = z * xysize + p; |
---|
[378] | 982 | if(mask[vox]) |
---|
| 983 | tempArray[goodSize++] = this->array[vox] - this->recon[vox]; |
---|
[741] | 984 | vox++; |
---|
| 985 | } |
---|
[378] | 986 | } |
---|
[211] | 987 | } |
---|
[741] | 988 | |
---|
[889] | 989 | this->Stats.setStddev( findStddev<float>(tempArray, goodSize) ); |
---|
[275] | 990 | |
---|
[378] | 991 | // Now find the madfm of the residuals. Store it. |
---|
[762] | 992 | this->Stats.setMadfm( findMADFM(tempArray, goodSize, true) ); |
---|
[378] | 993 | |
---|
| 994 | delete [] tempArray; |
---|
[275] | 995 | } |
---|
[378] | 996 | } |
---|
| 997 | else if(this->par.getFlagSmooth()) { |
---|
| 998 | // Case #3 -- smoothing |
---|
| 999 | // get all four stats from the recon array, which holds the |
---|
| 1000 | // smoothed data. This can just be done with the |
---|
| 1001 | // StatsContainer::calculate function, using the mask generated |
---|
| 1002 | // earlier. |
---|
[913] | 1003 | if(!this->reconExists){ |
---|
| 1004 | DUCHAMPERROR("setCubeStats","Smoothing not yet done! Cannot calculate stats!"); |
---|
| 1005 | } |
---|
[378] | 1006 | else this->Stats.calculate(this->recon,this->numPixels,mask); |
---|
| 1007 | } |
---|
| 1008 | else{ |
---|
| 1009 | // Case #1 -- default case, with no smoothing or reconstruction. |
---|
| 1010 | // get all four stats from the original array. This can just be |
---|
| 1011 | // done with the StatsContainer::calculate function, using the |
---|
| 1012 | // mask generated earlier. |
---|
| 1013 | this->Stats.calculate(this->array,this->numPixels,mask); |
---|
[211] | 1014 | } |
---|
[205] | 1015 | |
---|
[378] | 1016 | this->Stats.setUseFDR( this->par.getFlagFDR() ); |
---|
| 1017 | // If the FDR method has been requested, define the P-value |
---|
| 1018 | // threshold |
---|
| 1019 | if(this->par.getFlagFDR()) this->setupFDR(); |
---|
| 1020 | else{ |
---|
| 1021 | // otherwise, calculate threshold based on the requested SNR cut |
---|
| 1022 | // level, and then set the threshold parameter in the Par set. |
---|
| 1023 | this->Stats.setThresholdSNR( this->par.getCut() ); |
---|
| 1024 | this->par.setThreshold( this->Stats.getThreshold() ); |
---|
| 1025 | } |
---|
| 1026 | |
---|
| 1027 | delete [] mask; |
---|
| 1028 | |
---|
[275] | 1029 | } |
---|
[206] | 1030 | |
---|
[378] | 1031 | if(this->par.isVerbose()){ |
---|
| 1032 | std::cout << "Using "; |
---|
| 1033 | if(this->par.getFlagFDR()) std::cout << "effective "; |
---|
| 1034 | std::cout << "flux threshold of: "; |
---|
| 1035 | float thresh = this->Stats.getThreshold(); |
---|
| 1036 | if(this->par.getFlagNegative()) thresh *= -1.; |
---|
[539] | 1037 | std::cout << thresh; |
---|
| 1038 | if(this->par.getFlagGrowth()){ |
---|
| 1039 | std::cout << " and growing to threshold of: "; |
---|
[777] | 1040 | if(this->par.getFlagUserGrowthThreshold()) thresh= this->par.getGrowthThreshold(); |
---|
| 1041 | else thresh= this->Stats.snrToValue(this->par.getGrowthCut()); |
---|
| 1042 | if(this->par.getFlagNegative()) thresh *= -1.; |
---|
| 1043 | std::cout << thresh; |
---|
[539] | 1044 | } |
---|
| 1045 | std::cout << std::endl; |
---|
[205] | 1046 | } |
---|
[309] | 1047 | |
---|
[205] | 1048 | } |
---|
[378] | 1049 | //-------------------------------------------------------------------- |
---|
[211] | 1050 | |
---|
[378] | 1051 | void Cube::setupFDR() |
---|
| 1052 | { |
---|
[528] | 1053 | /// @details |
---|
| 1054 | /// Call the setupFDR(float *) function on the pixel array of the |
---|
| 1055 | /// cube. This is the usual way of running it. |
---|
| 1056 | /// |
---|
| 1057 | /// However, if we are in smoothing mode, we calculate the FDR |
---|
| 1058 | /// parameters using the recon array, which holds the smoothed |
---|
| 1059 | /// data. Gives an error message if the reconExists flag is not set. |
---|
| 1060 | |
---|
[378] | 1061 | if(this->par.getFlagSmooth()) |
---|
| 1062 | if(this->reconExists) this->setupFDR(this->recon); |
---|
| 1063 | else{ |
---|
[913] | 1064 | DUCHAMPERROR("setupFDR", "Smoothing not done properly! Using original array for defining threshold."); |
---|
[378] | 1065 | this->setupFDR(this->array); |
---|
| 1066 | } |
---|
| 1067 | else if( this->par.getFlagATrous() ){ |
---|
[905] | 1068 | if(this->reconExists) this->setupFDR(this->recon); |
---|
| 1069 | else{ |
---|
[913] | 1070 | DUCHAMPERROR("setupFDR", "Reconstruction not done properly! Using original array for defining threshold."); |
---|
[905] | 1071 | this->setupFDR(this->array); |
---|
| 1072 | } |
---|
[378] | 1073 | } |
---|
[275] | 1074 | else{ |
---|
| 1075 | this->setupFDR(this->array); |
---|
| 1076 | } |
---|
| 1077 | } |
---|
[378] | 1078 | //-------------------------------------------------------------------- |
---|
[275] | 1079 | |
---|
[378] | 1080 | void Cube::setupFDR(float *input) |
---|
| 1081 | { |
---|
[528] | 1082 | /// @details |
---|
| 1083 | /// Determines the critical Probability value for the False |
---|
| 1084 | /// Discovery Rate detection routine. All pixels in the given arry |
---|
| 1085 | /// with Prob less than this value will be considered detections. |
---|
| 1086 | /// |
---|
| 1087 | /// Note that the Stats of the cube need to be calculated first. |
---|
| 1088 | /// |
---|
| 1089 | /// The Prob here is the probability, assuming a Normal |
---|
| 1090 | /// distribution, of obtaining a value as high or higher than the |
---|
| 1091 | /// pixel value (ie. only the positive tail of the PDF). |
---|
| 1092 | /// |
---|
| 1093 | /// The probabilities are calculated using the |
---|
| 1094 | /// StatsContainer::getPValue(), which calculates the z-statistic, |
---|
| 1095 | /// and then the probability via |
---|
| 1096 | /// \f$0.5\operatorname{erfc}(z/\sqrt{2})\f$ -- giving the positive |
---|
| 1097 | /// tail probability. |
---|
[189] | 1098 | |
---|
[378] | 1099 | // first calculate p-value for each pixel -- assume Gaussian for now. |
---|
[190] | 1100 | |
---|
[378] | 1101 | float *orderedP = new float[this->numPixels]; |
---|
[894] | 1102 | size_t count = 0; |
---|
| 1103 | for(size_t x=0;x<this->axisDim[0];x++){ |
---|
| 1104 | for(size_t y=0;y<this->axisDim[1];y++){ |
---|
| 1105 | for(size_t z=0;z<this->axisDim[2];z++){ |
---|
| 1106 | size_t pix = z * this->axisDim[0]*this->axisDim[1] + |
---|
[378] | 1107 | y*this->axisDim[0] + x; |
---|
[190] | 1108 | |
---|
[378] | 1109 | if(!(this->par.isBlank(this->array[pix])) && !this->par.isInMW(z)){ |
---|
| 1110 | // only look at non-blank, valid pixels |
---|
| 1111 | // orderedP[count++] = this->Stats.getPValue(this->array[pix]); |
---|
| 1112 | orderedP[count++] = this->Stats.getPValue(input[pix]); |
---|
| 1113 | } |
---|
[263] | 1114 | } |
---|
| 1115 | } |
---|
[190] | 1116 | } |
---|
[3] | 1117 | |
---|
[378] | 1118 | // now order them |
---|
| 1119 | std::stable_sort(orderedP,orderedP+count); |
---|
[190] | 1120 | |
---|
[378] | 1121 | // now find the maximum P value. |
---|
[894] | 1122 | size_t max = 0; |
---|
[777] | 1123 | double cN = 0.; |
---|
[543] | 1124 | // Calculate number of correlated pixels. Assume all spatial |
---|
| 1125 | // pixels within the beam are correlated, and multiply this by the |
---|
[788] | 1126 | // number of correlated pixels as determined by the beam |
---|
[800] | 1127 | int numVox; |
---|
[803] | 1128 | if(this->head.beam().isDefined()) numVox = int(ceil(this->head.beam().area())); |
---|
[800] | 1129 | else numVox = 1; |
---|
[543] | 1130 | if(this->head.canUseThirdAxis()) numVox *= this->par.getFDRnumCorChan(); |
---|
[378] | 1131 | for(int psfCtr=1;psfCtr<=numVox;psfCtr++) cN += 1./float(psfCtr); |
---|
[190] | 1132 | |
---|
[378] | 1133 | double slope = this->par.getAlpha()/cN; |
---|
[894] | 1134 | for(size_t loopCtr=0;loopCtr<count;loopCtr++) { |
---|
[378] | 1135 | if( orderedP[loopCtr] < (slope * double(loopCtr+1)/ double(count)) ){ |
---|
| 1136 | max = loopCtr; |
---|
| 1137 | } |
---|
[190] | 1138 | } |
---|
| 1139 | |
---|
[378] | 1140 | this->Stats.setPThreshold( orderedP[max] ); |
---|
[190] | 1141 | |
---|
| 1142 | |
---|
[378] | 1143 | // Find real value of the P threshold by finding the inverse of the |
---|
| 1144 | // error function -- root finding with brute force technique |
---|
| 1145 | // (relatively slow, but we only do it once). |
---|
| 1146 | double zStat = 0.; |
---|
| 1147 | double deltaZ = 0.1; |
---|
| 1148 | double tolerance = 1.e-6; |
---|
| 1149 | double initial = 0.5 * erfc(zStat/M_SQRT2) - this->Stats.getPThreshold(); |
---|
| 1150 | do{ |
---|
| 1151 | zStat+=deltaZ; |
---|
| 1152 | double current = 0.5 * erfc(zStat/M_SQRT2) - this->Stats.getPThreshold(); |
---|
| 1153 | if((initial*current)<0.){ |
---|
| 1154 | zStat-=deltaZ; |
---|
| 1155 | deltaZ/=2.; |
---|
| 1156 | } |
---|
| 1157 | }while(deltaZ>tolerance); |
---|
| 1158 | this->Stats.setThreshold( zStat*this->Stats.getSpread() + |
---|
| 1159 | this->Stats.getMiddle() ); |
---|
[192] | 1160 | |
---|
[378] | 1161 | /////////////////////////// |
---|
| 1162 | // if(TESTING){ |
---|
| 1163 | // std::stringstream ss; |
---|
| 1164 | // float *xplot = new float[2*max]; |
---|
| 1165 | // for(int i=0;i<2*max;i++) xplot[i]=float(i)/float(count); |
---|
| 1166 | // cpgopen("latestFDR.ps/vcps"); |
---|
| 1167 | // cpgpap(8.,1.); |
---|
| 1168 | // cpgslw(3); |
---|
| 1169 | // cpgenv(0,float(2*max)/float(count),0,orderedP[2*max],0,0); |
---|
| 1170 | // cpglab("i/N (index)", "p-value",""); |
---|
| 1171 | // cpgpt(2*max,xplot,orderedP,DOT); |
---|
[263] | 1172 | |
---|
[378] | 1173 | // ss.str(""); |
---|
| 1174 | // ss << "\\gm = " << this->Stats.getMiddle(); |
---|
| 1175 | // cpgtext(max/(4.*count),0.9*orderedP[2*max],ss.str().c_str()); |
---|
| 1176 | // ss.str(""); |
---|
| 1177 | // ss << "\\gs = " << this->Stats.getSpread(); |
---|
| 1178 | // cpgtext(max/(4.*count),0.85*orderedP[2*max],ss.str().c_str()); |
---|
| 1179 | // ss.str(""); |
---|
| 1180 | // ss << "Slope = " << slope; |
---|
| 1181 | // cpgtext(max/(4.*count),0.8*orderedP[2*max],ss.str().c_str()); |
---|
| 1182 | // ss.str(""); |
---|
| 1183 | // ss << "Alpha = " << this->par.getAlpha(); |
---|
| 1184 | // cpgtext(max/(4.*count),0.75*orderedP[2*max],ss.str().c_str()); |
---|
| 1185 | // ss.str(""); |
---|
| 1186 | // ss << "c\\dN\\u = " << cN; |
---|
| 1187 | // cpgtext(max/(4.*count),0.7*orderedP[2*max],ss.str().c_str()); |
---|
| 1188 | // ss.str(""); |
---|
| 1189 | // ss << "max = "<<max << " (out of " << count << ")"; |
---|
| 1190 | // cpgtext(max/(4.*count),0.65*orderedP[2*max],ss.str().c_str()); |
---|
| 1191 | // ss.str(""); |
---|
| 1192 | // ss << "Threshold = "<<zStat*this->Stats.getSpread()+this->Stats.getMiddle(); |
---|
| 1193 | // cpgtext(max/(4.*count),0.6*orderedP[2*max],ss.str().c_str()); |
---|
[263] | 1194 | |
---|
[378] | 1195 | // cpgslw(1); |
---|
| 1196 | // cpgsci(RED); |
---|
| 1197 | // cpgmove(0,0); |
---|
| 1198 | // cpgdraw(1,slope); |
---|
| 1199 | // cpgsci(BLUE); |
---|
| 1200 | // cpgsls(DOTTED); |
---|
| 1201 | // cpgmove(0,orderedP[max]); |
---|
| 1202 | // cpgdraw(2*max/float(count),orderedP[max]); |
---|
| 1203 | // cpgmove(max/float(count),0); |
---|
| 1204 | // cpgdraw(max/float(count),orderedP[2*max]); |
---|
| 1205 | // cpgsci(GREEN); |
---|
| 1206 | // cpgsls(SOLID); |
---|
| 1207 | // for(int i=1;i<=10;i++) { |
---|
| 1208 | // ss.str(""); |
---|
| 1209 | // ss << float(i)/2. << "\\gs"; |
---|
| 1210 | // float prob = 0.5*erfc((float(i)/2.)/M_SQRT2); |
---|
| 1211 | // cpgtick(0, 0, 0, orderedP[2*max], |
---|
| 1212 | // prob/orderedP[2*max], |
---|
| 1213 | // 0, 1, 1.5, 90., ss.str().c_str()); |
---|
| 1214 | // } |
---|
| 1215 | // cpgend(); |
---|
| 1216 | // delete [] xplot; |
---|
| 1217 | // } |
---|
| 1218 | delete [] orderedP; |
---|
[263] | 1219 | |
---|
[378] | 1220 | } |
---|
| 1221 | //-------------------------------------------------------------------- |
---|
[87] | 1222 | |
---|
[834] | 1223 | void Cube::Search() |
---|
[729] | 1224 | { |
---|
| 1225 | /// @details |
---|
| 1226 | /// This acts as a switching function to select the correct searching function based on the user's parameters. |
---|
| 1227 | /// @param verboseFlag If true, text is written to stdout describing the search function being used. |
---|
| 1228 | if(this->par.getFlagATrous()){ |
---|
[834] | 1229 | if(this->par.isVerbose()) std::cout<<"Commencing search in reconstructed cube..."<<std::endl; |
---|
[729] | 1230 | this->ReconSearch(); |
---|
| 1231 | } |
---|
| 1232 | else if(this->par.getFlagSmooth()){ |
---|
[834] | 1233 | if(this->par.isVerbose()) std::cout<<"Commencing search in smoothed cube..."<<std::endl; |
---|
[729] | 1234 | this->SmoothSearch(); |
---|
| 1235 | } |
---|
| 1236 | else{ |
---|
[834] | 1237 | if(this->par.isVerbose()) std::cout<<"Commencing search in cube..."<<std::endl; |
---|
[729] | 1238 | this->CubicSearch(); |
---|
| 1239 | } |
---|
| 1240 | |
---|
| 1241 | } |
---|
| 1242 | |
---|
[884] | 1243 | bool Cube::isDetection(size_t x, size_t y, size_t z) |
---|
[378] | 1244 | { |
---|
[528] | 1245 | /// @details |
---|
| 1246 | /// Is a given voxel at position (x,y,z) a detection, based on the statistics |
---|
| 1247 | /// in the Cube's StatsContainer? |
---|
| 1248 | /// If the pixel lies outside the valid range for the data array, |
---|
| 1249 | /// return false. |
---|
| 1250 | /// \param x X-value of the Cube's voxel to be tested. |
---|
| 1251 | /// \param y Y-value of the Cube's voxel to be tested. |
---|
| 1252 | /// \param z Z-value of the Cube's voxel to be tested. |
---|
| 1253 | |
---|
[884] | 1254 | size_t voxel = z*axisDim[0]*axisDim[1] + y*axisDim[0] + x; |
---|
[220] | 1255 | return DataArray::isDetection(array[voxel]); |
---|
[419] | 1256 | } |
---|
[378] | 1257 | //-------------------------------------------------------------------- |
---|
[220] | 1258 | |
---|
[420] | 1259 | void Cube::calcObjectFluxes() |
---|
| 1260 | { |
---|
[528] | 1261 | /// @details |
---|
| 1262 | /// A function to calculate the fluxes and centroids for each |
---|
| 1263 | /// object in the Cube's list of detections. Uses |
---|
| 1264 | /// Detection::calcFluxes() for each object. |
---|
| 1265 | |
---|
[420] | 1266 | std::vector<Detection>::iterator obj; |
---|
| 1267 | for(obj=this->objectList->begin();obj<this->objectList->end();obj++){ |
---|
| 1268 | obj->calcFluxes(this->array, this->axisDim); |
---|
| 1269 | if(this->par.getFlagUserThreshold()) |
---|
| 1270 | obj->setPeakSNR( obj->getPeakFlux() / this->Stats.getThreshold() ); |
---|
| 1271 | else |
---|
| 1272 | obj->setPeakSNR( (obj->getPeakFlux() - this->Stats.getMiddle()) / this->Stats.getSpread() ); |
---|
| 1273 | } |
---|
| 1274 | } |
---|
| 1275 | //-------------------------------------------------------------------- |
---|
| 1276 | |
---|
[378] | 1277 | void Cube::calcObjectWCSparams() |
---|
| 1278 | { |
---|
[528] | 1279 | /// @details |
---|
| 1280 | /// A function that calculates the WCS parameters for each object in the |
---|
| 1281 | /// Cube's list of detections. |
---|
| 1282 | /// Each object gets an ID number assigned to it (which is simply its order |
---|
| 1283 | /// in the list), and if the WCS is good, the WCS paramters are calculated. |
---|
[460] | 1284 | |
---|
| 1285 | std::vector<Detection>::iterator obj; |
---|
[461] | 1286 | int ct=0; |
---|
[777] | 1287 | ProgressBar bar; |
---|
| 1288 | if(this->par.isVerbose()) bar.init(this->objectList->size()); |
---|
[460] | 1289 | for(obj=this->objectList->begin();obj<this->objectList->end();obj++){ |
---|
[777] | 1290 | // std::cerr << ct << ' ' << this->array << '\n'; |
---|
| 1291 | if(this->par.isVerbose()) bar.update(ct); |
---|
[461] | 1292 | obj->setID(ct++); |
---|
[681] | 1293 | if(!obj->hasParams()){ |
---|
| 1294 | obj->setCentreType(this->par.getPixelCentre()); |
---|
| 1295 | obj->calcFluxes(this->array,this->axisDim); |
---|
| 1296 | // obj->calcWCSparams(this->array,this->axisDim,this->head); |
---|
| 1297 | obj->calcWCSparams(this->head); |
---|
| 1298 | obj->calcIntegFlux(this->array,this->axisDim,this->head); |
---|
[986] | 1299 | |
---|
[681] | 1300 | if(this->par.getFlagUserThreshold()) |
---|
| 1301 | obj->setPeakSNR( obj->getPeakFlux() / this->Stats.getThreshold() ); |
---|
| 1302 | else |
---|
| 1303 | obj->setPeakSNR( (obj->getPeakFlux() - this->Stats.getMiddle()) / this->Stats.getSpread() ); |
---|
| 1304 | } |
---|
[378] | 1305 | } |
---|
[777] | 1306 | if(this->par.isVerbose()) bar.remove(); |
---|
[220] | 1307 | |
---|
[378] | 1308 | if(!this->head.isWCS()){ |
---|
| 1309 | // if the WCS is bad, set the object names to Obj01 etc |
---|
| 1310 | int numspaces = int(log10(this->objectList->size())) + 1; |
---|
| 1311 | std::stringstream ss; |
---|
[623] | 1312 | for(size_t i=0;i<this->objectList->size();i++){ |
---|
[378] | 1313 | ss.str(""); |
---|
| 1314 | ss << "Obj" << std::setfill('0') << std::setw(numspaces) << i+1; |
---|
[623] | 1315 | this->objectList->at(i).setName(ss.str()); |
---|
[378] | 1316 | } |
---|
[220] | 1317 | } |
---|
[378] | 1318 | |
---|
[220] | 1319 | } |
---|
[378] | 1320 | //-------------------------------------------------------------------- |
---|
[220] | 1321 | |
---|
[418] | 1322 | void Cube::calcObjectWCSparams(std::vector< std::vector<PixelInfo::Voxel> > bigVoxList) |
---|
| 1323 | { |
---|
[528] | 1324 | /// @details |
---|
| 1325 | /// A function that calculates the WCS parameters for each object in the |
---|
| 1326 | /// Cube's list of detections. |
---|
| 1327 | /// Each object gets an ID number assigned to it (which is simply its order |
---|
| 1328 | /// in the list), and if the WCS is good, the WCS paramters are calculated. |
---|
| 1329 | /// |
---|
| 1330 | /// This version uses vectors of Voxels to define the fluxes. |
---|
| 1331 | /// |
---|
| 1332 | /// \param bigVoxList A vector of vectors of Voxels, with the same |
---|
| 1333 | /// number of elements as this->objectList, where each element is a |
---|
| 1334 | /// vector of Voxels corresponding to the same voxels in each |
---|
| 1335 | /// detection and indicating the flux of each voxel. |
---|
[418] | 1336 | |
---|
[460] | 1337 | std::vector<Detection>::iterator obj; |
---|
[461] | 1338 | int ct=0; |
---|
[460] | 1339 | for(obj=this->objectList->begin();obj<this->objectList->end();obj++){ |
---|
[461] | 1340 | obj->setID(ct+1); |
---|
[727] | 1341 | if(!obj->hasParams()){ |
---|
[681] | 1342 | obj->setCentreType(this->par.getPixelCentre()); |
---|
| 1343 | obj->calcFluxes(bigVoxList[ct]); |
---|
| 1344 | obj->calcWCSparams(this->head); |
---|
[719] | 1345 | obj->calcIntegFlux(this->axisDim[2],bigVoxList[ct],this->head); |
---|
[681] | 1346 | |
---|
| 1347 | if(this->par.getFlagUserThreshold()) |
---|
| 1348 | obj->setPeakSNR( obj->getPeakFlux() / this->Stats.getThreshold() ); |
---|
| 1349 | else |
---|
| 1350 | obj->setPeakSNR( (obj->getPeakFlux() - this->Stats.getMiddle()) / this->Stats.getSpread() ); |
---|
| 1351 | } |
---|
[461] | 1352 | ct++; |
---|
[418] | 1353 | } |
---|
| 1354 | |
---|
| 1355 | if(!this->head.isWCS()){ |
---|
| 1356 | // if the WCS is bad, set the object names to Obj01 etc |
---|
| 1357 | int numspaces = int(log10(this->objectList->size())) + 1; |
---|
| 1358 | std::stringstream ss; |
---|
[623] | 1359 | for(size_t i=0;i<this->objectList->size();i++){ |
---|
[418] | 1360 | ss.str(""); |
---|
| 1361 | ss << "Obj" << std::setfill('0') << std::setw(numspaces) << i+1; |
---|
[623] | 1362 | this->objectList->at(i).setName(ss.str()); |
---|
[418] | 1363 | } |
---|
| 1364 | } |
---|
| 1365 | |
---|
| 1366 | } |
---|
| 1367 | //-------------------------------------------------------------------- |
---|
| 1368 | |
---|
[863] | 1369 | void Cube::calcObjectWCSparams(std::map<PixelInfo::Voxel,float> &voxelMap) |
---|
| 1370 | { |
---|
| 1371 | /// @details |
---|
| 1372 | /// A function that calculates the WCS parameters for each object in the |
---|
| 1373 | /// Cube's list of detections. |
---|
| 1374 | /// Each object gets an ID number assigned to it (which is simply its order |
---|
| 1375 | /// in the list), and if the WCS is good, the WCS paramters are calculated. |
---|
| 1376 | /// |
---|
| 1377 | /// This version uses vectors of Voxels to define the fluxes. |
---|
| 1378 | /// |
---|
| 1379 | /// \param bigVoxList A vector of vectors of Voxels, with the same |
---|
| 1380 | /// number of elements as this->objectList, where each element is a |
---|
| 1381 | /// vector of Voxels corresponding to the same voxels in each |
---|
| 1382 | /// detection and indicating the flux of each voxel. |
---|
| 1383 | |
---|
| 1384 | std::vector<Detection>::iterator obj; |
---|
| 1385 | int ct=0; |
---|
| 1386 | for(obj=this->objectList->begin();obj<this->objectList->end();obj++){ |
---|
| 1387 | obj->setID(ct+1); |
---|
| 1388 | if(!obj->hasParams()){ |
---|
| 1389 | obj->setCentreType(this->par.getPixelCentre()); |
---|
| 1390 | obj->calcFluxes(voxelMap); |
---|
| 1391 | obj->calcWCSparams(this->head); |
---|
| 1392 | obj->calcIntegFlux(this->axisDim[2],voxelMap,this->head); |
---|
| 1393 | |
---|
| 1394 | if(this->par.getFlagUserThreshold()) |
---|
| 1395 | obj->setPeakSNR( obj->getPeakFlux() / this->Stats.getThreshold() ); |
---|
| 1396 | else |
---|
| 1397 | obj->setPeakSNR( (obj->getPeakFlux() - this->Stats.getMiddle()) / this->Stats.getSpread() ); |
---|
| 1398 | } |
---|
| 1399 | ct++; |
---|
| 1400 | } |
---|
| 1401 | |
---|
| 1402 | if(!this->head.isWCS()){ |
---|
| 1403 | // if the WCS is bad, set the object names to Obj01 etc |
---|
| 1404 | int numspaces = int(log10(this->objectList->size())) + 1; |
---|
| 1405 | std::stringstream ss; |
---|
| 1406 | for(size_t i=0;i<this->objectList->size();i++){ |
---|
| 1407 | ss.str(""); |
---|
| 1408 | ss << "Obj" << std::setfill('0') << std::setw(numspaces) << i+1; |
---|
| 1409 | this->objectList->at(i).setName(ss.str()); |
---|
| 1410 | } |
---|
| 1411 | } |
---|
| 1412 | |
---|
| 1413 | } |
---|
| 1414 | //-------------------------------------------------------------------- |
---|
| 1415 | |
---|
[378] | 1416 | void Cube::updateDetectMap() |
---|
| 1417 | { |
---|
[570] | 1418 | /// @details A function that, for each detected object in the |
---|
| 1419 | /// cube's list, increments the cube's detection map by the |
---|
| 1420 | /// required amount at each pixel. Uses |
---|
| 1421 | /// updateDetectMap(Detection). |
---|
[220] | 1422 | |
---|
[570] | 1423 | std::vector<Detection>::iterator obj; |
---|
| 1424 | for(obj=this->objectList->begin();obj<this->objectList->end();obj++){ |
---|
| 1425 | this->updateDetectMap(*obj); |
---|
| 1426 | } |
---|
[378] | 1427 | |
---|
| 1428 | } |
---|
| 1429 | //-------------------------------------------------------------------- |
---|
| 1430 | |
---|
| 1431 | void Cube::updateDetectMap(Detection obj) |
---|
| 1432 | { |
---|
[528] | 1433 | /// @details |
---|
| 1434 | /// A function that, for the given object, increments the cube's |
---|
| 1435 | /// detection map by the required amount at each pixel. |
---|
| 1436 | /// |
---|
| 1437 | /// \param obj A Detection object that is being incorporated into the map. |
---|
[378] | 1438 | |
---|
[570] | 1439 | std::vector<Voxel> vlist = obj.getPixelSet(); |
---|
[984] | 1440 | for(std::vector<Voxel>::iterator vox=vlist.begin();vox<vlist.end();vox++) { |
---|
| 1441 | if(this->numNondegDim==1) |
---|
| 1442 | this->detectMap[vox->getZ()]++; |
---|
| 1443 | else |
---|
| 1444 | this->detectMap[vox->getX()+vox->getY()*this->axisDim[0]]++; |
---|
| 1445 | } |
---|
[378] | 1446 | } |
---|
| 1447 | //-------------------------------------------------------------------- |
---|
[220] | 1448 | |
---|
[378] | 1449 | float Cube::enclosedFlux(Detection obj) |
---|
| 1450 | { |
---|
[528] | 1451 | /// @details |
---|
| 1452 | /// A function to calculate the flux enclosed by the range |
---|
| 1453 | /// of pixels detected in the object obj (not necessarily all |
---|
| 1454 | /// pixels will have been detected). |
---|
| 1455 | /// |
---|
| 1456 | /// \param obj The Detection under consideration. |
---|
| 1457 | |
---|
[378] | 1458 | obj.calcFluxes(this->array, this->axisDim); |
---|
| 1459 | int xsize = obj.getXmax()-obj.getXmin()+1; |
---|
| 1460 | int ysize = obj.getYmax()-obj.getYmin()+1; |
---|
| 1461 | int zsize = obj.getZmax()-obj.getZmin()+1; |
---|
| 1462 | std::vector <float> fluxArray(xsize*ysize*zsize,0.); |
---|
| 1463 | for(int x=0;x<xsize;x++){ |
---|
| 1464 | for(int y=0;y<ysize;y++){ |
---|
| 1465 | for(int z=0;z<zsize;z++){ |
---|
| 1466 | fluxArray[x+y*xsize+z*ysize*xsize] = |
---|
| 1467 | this->getPixValue(x+obj.getXmin(), |
---|
| 1468 | y+obj.getYmin(), |
---|
| 1469 | z+obj.getZmin()); |
---|
| 1470 | if(this->par.getFlagNegative()) |
---|
| 1471 | fluxArray[x+y*xsize+z*ysize*xsize] *= -1.; |
---|
| 1472 | } |
---|
[87] | 1473 | } |
---|
| 1474 | } |
---|
[378] | 1475 | float sum = 0.; |
---|
[623] | 1476 | for(size_t i=0;i<fluxArray.size();i++) |
---|
[378] | 1477 | if(!this->par.isBlank(fluxArray[i])) sum+=fluxArray[i]; |
---|
| 1478 | return sum; |
---|
[87] | 1479 | } |
---|
[378] | 1480 | //-------------------------------------------------------------------- |
---|
[87] | 1481 | |
---|
[378] | 1482 | void Cube::setupColumns() |
---|
| 1483 | { |
---|
[528] | 1484 | /// @details |
---|
| 1485 | /// A front-end to the two setup routines in columns.cc. |
---|
| 1486 | /// |
---|
| 1487 | /// This first gets the starting precisions, which may be from |
---|
| 1488 | /// the input parameters. It then sets up the columns (calculates |
---|
| 1489 | /// their widths and precisions and so on based on the values |
---|
| 1490 | /// within). The precisions are also stored in each Detection |
---|
| 1491 | /// object. |
---|
| 1492 | /// |
---|
| 1493 | /// Need to have called calcObjectWCSparams() somewhere |
---|
| 1494 | /// beforehand. |
---|
[438] | 1495 | |
---|
| 1496 | std::vector<Detection>::iterator obj; |
---|
| 1497 | for(obj=this->objectList->begin();obj<this->objectList->end();obj++){ |
---|
| 1498 | obj->setVelPrec( this->par.getPrecVel() ); |
---|
| 1499 | obj->setFpeakPrec( this->par.getPrecFlux() ); |
---|
[439] | 1500 | obj->setXYZPrec( Column::prXYZ ); |
---|
| 1501 | obj->setPosPrec( Column::prWPOS ); |
---|
[438] | 1502 | obj->setFintPrec( this->par.getPrecFlux() ); |
---|
| 1503 | obj->setSNRPrec( this->par.getPrecSNR() ); |
---|
| 1504 | } |
---|
[187] | 1505 | |
---|
[378] | 1506 | this->fullCols.clear(); |
---|
| 1507 | this->fullCols = getFullColSet(*(this->objectList), this->head); |
---|
[136] | 1508 | |
---|
[378] | 1509 | this->logCols.clear(); |
---|
| 1510 | this->logCols = getLogColSet(*(this->objectList), this->head); |
---|
[136] | 1511 | |
---|
[378] | 1512 | int vel,fpeak,fint,pos,xyz,snr; |
---|
| 1513 | vel = fullCols[VEL].getPrecision(); |
---|
| 1514 | fpeak = fullCols[FPEAK].getPrecision(); |
---|
| 1515 | snr = fullCols[SNRPEAK].getPrecision(); |
---|
| 1516 | xyz = fullCols[X].getPrecision(); |
---|
| 1517 | xyz = std::max(xyz, fullCols[Y].getPrecision()); |
---|
| 1518 | xyz = std::max(xyz, fullCols[Z].getPrecision()); |
---|
| 1519 | if(this->head.isWCS()) fint = fullCols[FINT].getPrecision(); |
---|
| 1520 | else fint = fullCols[FTOT].getPrecision(); |
---|
| 1521 | pos = fullCols[WRA].getPrecision(); |
---|
| 1522 | pos = std::max(pos, fullCols[WDEC].getPrecision()); |
---|
[144] | 1523 | |
---|
[424] | 1524 | for(obj=this->objectList->begin();obj<this->objectList->end();obj++){ |
---|
| 1525 | obj->setVelPrec(vel); |
---|
| 1526 | obj->setFpeakPrec(fpeak); |
---|
| 1527 | obj->setXYZPrec(xyz); |
---|
| 1528 | obj->setPosPrec(pos); |
---|
| 1529 | obj->setFintPrec(fint); |
---|
| 1530 | obj->setSNRPrec(snr); |
---|
[378] | 1531 | } |
---|
| 1532 | |
---|
[144] | 1533 | } |
---|
[378] | 1534 | //-------------------------------------------------------------------- |
---|
[136] | 1535 | |
---|
[378] | 1536 | bool Cube::objAtSpatialEdge(Detection obj) |
---|
| 1537 | { |
---|
[528] | 1538 | /// @details |
---|
| 1539 | /// A function to test whether the object obj |
---|
| 1540 | /// lies at the edge of the cube's spatial field -- |
---|
| 1541 | /// either at the boundary, or next to BLANKs. |
---|
| 1542 | /// |
---|
| 1543 | /// \param obj The Detection under consideration. |
---|
[136] | 1544 | |
---|
[378] | 1545 | bool atEdge = false; |
---|
[87] | 1546 | |
---|
[623] | 1547 | size_t pix = 0; |
---|
[570] | 1548 | std::vector<Voxel> voxlist = obj.getPixelSet(); |
---|
[378] | 1549 | while(!atEdge && pix<voxlist.size()){ |
---|
| 1550 | // loop over each pixel in the object, until we find an edge pixel. |
---|
| 1551 | for(int dx=-1;dx<=1;dx+=2){ |
---|
| 1552 | if( ((voxlist[pix].getX()+dx)<0) || |
---|
[935] | 1553 | ((voxlist[pix].getX()+dx)>=int(this->axisDim[0])) ) |
---|
[378] | 1554 | atEdge = true; |
---|
| 1555 | else if(this->isBlank(voxlist[pix].getX()+dx, |
---|
| 1556 | voxlist[pix].getY(), |
---|
| 1557 | voxlist[pix].getZ())) |
---|
| 1558 | atEdge = true; |
---|
| 1559 | } |
---|
| 1560 | for(int dy=-1;dy<=1;dy+=2){ |
---|
| 1561 | if( ((voxlist[pix].getY()+dy)<0) || |
---|
[935] | 1562 | ((voxlist[pix].getY()+dy)>=int(this->axisDim[1])) ) |
---|
[378] | 1563 | atEdge = true; |
---|
| 1564 | else if(this->isBlank(voxlist[pix].getX(), |
---|
| 1565 | voxlist[pix].getY()+dy, |
---|
| 1566 | voxlist[pix].getZ())) |
---|
| 1567 | atEdge = true; |
---|
| 1568 | } |
---|
| 1569 | pix++; |
---|
| 1570 | } |
---|
[87] | 1571 | |
---|
[378] | 1572 | return atEdge; |
---|
[192] | 1573 | } |
---|
[378] | 1574 | //-------------------------------------------------------------------- |
---|
[192] | 1575 | |
---|
[378] | 1576 | bool Cube::objAtSpectralEdge(Detection obj) |
---|
| 1577 | { |
---|
[528] | 1578 | /// @details |
---|
| 1579 | /// A function to test whether the object obj |
---|
| 1580 | /// lies at the edge of the cube's spectral extent -- |
---|
| 1581 | /// either at the boundary, or next to BLANKs. |
---|
| 1582 | /// |
---|
[529] | 1583 | /// \param obj The Detection under consideration. |
---|
[192] | 1584 | |
---|
[378] | 1585 | bool atEdge = false; |
---|
[192] | 1586 | |
---|
[623] | 1587 | size_t pix = 0; |
---|
[570] | 1588 | std::vector<Voxel> voxlist = obj.getPixelSet(); |
---|
[378] | 1589 | while(!atEdge && pix<voxlist.size()){ |
---|
| 1590 | // loop over each pixel in the object, until we find an edge pixel. |
---|
| 1591 | for(int dz=-1;dz<=1;dz+=2){ |
---|
| 1592 | if( ((voxlist[pix].getZ()+dz)<0) || |
---|
[935] | 1593 | ((voxlist[pix].getZ()+dz)>=int(this->axisDim[2])) ) |
---|
[378] | 1594 | atEdge = true; |
---|
| 1595 | else if(this->isBlank(voxlist[pix].getX(), |
---|
| 1596 | voxlist[pix].getY(), |
---|
| 1597 | voxlist[pix].getZ()+dz)) |
---|
| 1598 | atEdge = true; |
---|
| 1599 | } |
---|
| 1600 | pix++; |
---|
| 1601 | } |
---|
[192] | 1602 | |
---|
[378] | 1603 | return atEdge; |
---|
[87] | 1604 | } |
---|
[378] | 1605 | //-------------------------------------------------------------------- |
---|
[87] | 1606 | |
---|
[960] | 1607 | bool Cube::objNextToMW(Detection obj) |
---|
| 1608 | { |
---|
| 1609 | /// @details A function to test whether the object obj lies |
---|
| 1610 | /// adjacent to the MW range or straddles it (conceivably, you |
---|
| 1611 | /// could have disconnected channels in your object that don't |
---|
| 1612 | /// touch the MW range, but lie either side - in this case we |
---|
| 1613 | /// want the flag). If flagMW=false we will always return false. |
---|
| 1614 | /// |
---|
| 1615 | /// \param obj The Detection under consideration. |
---|
| 1616 | |
---|
| 1617 | bool isNext = this->par.getFlagMW() && |
---|
| 1618 | ((obj.getZmin() <= this->par.getMaxMW()+1) && (obj.getZmax() >= this->par.getMinMW()-1)); |
---|
| 1619 | |
---|
| 1620 | return isNext; |
---|
| 1621 | |
---|
| 1622 | } |
---|
| 1623 | //-------------------------------------------------------------------- |
---|
| 1624 | |
---|
[378] | 1625 | void Cube::setObjectFlags() |
---|
| 1626 | { |
---|
[528] | 1627 | /// @details |
---|
| 1628 | /// A function to set any warning flags for all the detected objects |
---|
| 1629 | /// associated with the cube. |
---|
| 1630 | /// Flags to be looked for: |
---|
| 1631 | /// <ul><li> Negative enclosed flux (N) |
---|
| 1632 | /// <li> Detection at edge of field (spatially) (E) |
---|
| 1633 | /// <li> Detection at edge of spectral region (S) |
---|
| 1634 | /// </ul> |
---|
[87] | 1635 | |
---|
[460] | 1636 | std::vector<Detection>::iterator obj; |
---|
| 1637 | for(obj=this->objectList->begin();obj<this->objectList->end();obj++){ |
---|
[87] | 1638 | |
---|
[460] | 1639 | if( this->enclosedFlux(*obj) < 0. ) |
---|
| 1640 | obj->addToFlagText("N"); |
---|
[87] | 1641 | |
---|
[460] | 1642 | if( this->objAtSpatialEdge(*obj) ) |
---|
| 1643 | obj->addToFlagText("E"); |
---|
[87] | 1644 | |
---|
[460] | 1645 | if( this->objAtSpectralEdge(*obj) && (this->axisDim[2] > 2)) |
---|
| 1646 | obj->addToFlagText("S"); |
---|
[87] | 1647 | |
---|
[960] | 1648 | if( this->objNextToMW(*obj) ) |
---|
| 1649 | obj->addToFlagText("M"); |
---|
| 1650 | |
---|
[510] | 1651 | if(obj->getFlagText()=="") obj->addToFlagText("-"); |
---|
| 1652 | |
---|
[378] | 1653 | } |
---|
[192] | 1654 | |
---|
[87] | 1655 | } |
---|
[378] | 1656 | //-------------------------------------------------------------------- |
---|
[87] | 1657 | |
---|
[378] | 1658 | /****************************************************************/ |
---|
| 1659 | ///////////////////////////////////////////////////////////// |
---|
| 1660 | //// Functions for Image class |
---|
| 1661 | ///////////////////////////////////////////////////////////// |
---|
[220] | 1662 | |
---|
[884] | 1663 | Image::Image(size_t size) |
---|
[528] | 1664 | { |
---|
[378] | 1665 | // need error handling in case size<0 !!! |
---|
| 1666 | this->numPixels = this->numDim = 0; |
---|
[732] | 1667 | this->minSize = 2; |
---|
[913] | 1668 | if(size<0){ |
---|
| 1669 | DUCHAMPERROR("Image(size)","Negative size -- could not define Image"); |
---|
| 1670 | } |
---|
[378] | 1671 | else{ |
---|
[444] | 1672 | if(size>0 && !this->arrayAllocated){ |
---|
| 1673 | this->array = new float[size]; |
---|
| 1674 | this->arrayAllocated = true; |
---|
| 1675 | } |
---|
[378] | 1676 | this->numPixels = size; |
---|
[884] | 1677 | this->axisDim = new size_t[2]; |
---|
[444] | 1678 | this->axisDimAllocated = true; |
---|
[378] | 1679 | this->numDim = 2; |
---|
| 1680 | } |
---|
[220] | 1681 | } |
---|
[378] | 1682 | //-------------------------------------------------------------------- |
---|
[220] | 1683 | |
---|
[884] | 1684 | Image::Image(size_t *dimensions) |
---|
[528] | 1685 | { |
---|
[378] | 1686 | this->numPixels = this->numDim = 0; |
---|
[732] | 1687 | this->minSize = 2; |
---|
[378] | 1688 | int size = dimensions[0] * dimensions[1]; |
---|
[913] | 1689 | if(size<0){ |
---|
| 1690 | DUCHAMPERROR("Image(dimArray)","Negative size -- could not define Image"); |
---|
| 1691 | } |
---|
[378] | 1692 | else{ |
---|
| 1693 | this->numPixels = size; |
---|
[444] | 1694 | if(size>0){ |
---|
| 1695 | this->array = new float[size]; |
---|
| 1696 | this->arrayAllocated = true; |
---|
| 1697 | } |
---|
[378] | 1698 | this->numDim=2; |
---|
[884] | 1699 | this->axisDim = new size_t[2]; |
---|
[468] | 1700 | this->axisDimAllocated = true; |
---|
[378] | 1701 | for(int i=0;i<2;i++) this->axisDim[i] = dimensions[i]; |
---|
| 1702 | } |
---|
[220] | 1703 | } |
---|
[378] | 1704 | //-------------------------------------------------------------------- |
---|
[736] | 1705 | Image::Image(const Image &i): |
---|
| 1706 | DataArray(i) |
---|
| 1707 | { |
---|
| 1708 | this->operator=(i); |
---|
| 1709 | } |
---|
| 1710 | |
---|
[737] | 1711 | Image& Image::operator=(const Image &i) |
---|
[736] | 1712 | { |
---|
[737] | 1713 | if(this==&i) return *this; |
---|
[736] | 1714 | ((DataArray &) *this) = i; |
---|
| 1715 | this->minSize = i.minSize; |
---|
| 1716 | return *this; |
---|
| 1717 | } |
---|
| 1718 | |
---|
[378] | 1719 | //-------------------------------------------------------------------- |
---|
[220] | 1720 | |
---|
[884] | 1721 | void Image::saveArray(float *input, size_t size) |
---|
[378] | 1722 | { |
---|
[528] | 1723 | /// @details |
---|
| 1724 | /// Saves the array in input to the pixel array Image::array. |
---|
| 1725 | /// The size of the array given must be the same as the current number of |
---|
| 1726 | /// pixels, else an error message is returned and nothing is done. |
---|
| 1727 | /// \param input The array of values to be saved. |
---|
| 1728 | /// \param size The size of input. |
---|
| 1729 | |
---|
[913] | 1730 | if(size != this->numPixels){ |
---|
| 1731 | DUCHAMPERROR("Image::saveArray", "Input array different size to existing array. Cannot save."); |
---|
| 1732 | } |
---|
[378] | 1733 | else { |
---|
[444] | 1734 | if(this->numPixels>0 && this->arrayAllocated) delete [] array; |
---|
[378] | 1735 | this->numPixels = size; |
---|
[444] | 1736 | if(this->numPixels>0){ |
---|
| 1737 | this->array = new float[size]; |
---|
| 1738 | this->arrayAllocated = true; |
---|
[894] | 1739 | for(size_t i=0;i<size;i++) this->array[i] = input[i]; |
---|
[444] | 1740 | } |
---|
[378] | 1741 | } |
---|
[220] | 1742 | } |
---|
[378] | 1743 | //-------------------------------------------------------------------- |
---|
[220] | 1744 | |
---|
[884] | 1745 | void Image::extractSpectrum(float *Array, size_t *dim, size_t pixel) |
---|
[378] | 1746 | { |
---|
[528] | 1747 | /// @details |
---|
| 1748 | /// A function to extract a 1-D spectrum from a 3-D array. |
---|
| 1749 | /// The array is assumed to be 3-D with the third dimension the spectral one. |
---|
| 1750 | /// The spectrum extracted is the one lying in the spatial pixel referenced |
---|
| 1751 | /// by the third argument. |
---|
| 1752 | /// The extracted spectrum is stored in the pixel array Image::array. |
---|
| 1753 | /// \param Array The array containing the pixel values, from which |
---|
| 1754 | /// the spectrum is extracted. |
---|
| 1755 | /// \param dim The array of dimension values. |
---|
| 1756 | /// \param pixel The spatial pixel that contains the desired spectrum. |
---|
| 1757 | |
---|
[913] | 1758 | if((pixel<0)||(pixel>=dim[0]*dim[1])){ |
---|
| 1759 | DUCHAMPERROR("Image::extractSpectrum", "Requested spatial pixel outside allowed range. Cannot save."); |
---|
| 1760 | } |
---|
| 1761 | else if(dim[2] != this->numPixels){ |
---|
| 1762 | DUCHAMPERROR("Image::extractSpectrum", "Input array different size to existing array. Cannot save."); |
---|
| 1763 | } |
---|
[378] | 1764 | else { |
---|
[444] | 1765 | if(this->numPixels>0 && this->arrayAllocated) delete [] array; |
---|
[378] | 1766 | this->numPixels = dim[2]; |
---|
[444] | 1767 | if(this->numPixels>0){ |
---|
| 1768 | this->array = new float[dim[2]]; |
---|
| 1769 | this->arrayAllocated = true; |
---|
[894] | 1770 | for(size_t z=0;z<dim[2];z++) this->array[z] = Array[z*dim[0]*dim[1] + pixel]; |
---|
[444] | 1771 | } |
---|
[378] | 1772 | } |
---|
[258] | 1773 | } |
---|
[378] | 1774 | //-------------------------------------------------------------------- |
---|
[220] | 1775 | |
---|
[884] | 1776 | void Image::extractSpectrum(Cube &cube, size_t pixel) |
---|
[378] | 1777 | { |
---|
[528] | 1778 | /// @details |
---|
| 1779 | /// A function to extract a 1-D spectrum from a Cube class |
---|
| 1780 | /// The spectrum extracted is the one lying in the spatial pixel referenced |
---|
| 1781 | /// by the second argument. |
---|
| 1782 | /// The extracted spectrum is stored in the pixel array Image::array. |
---|
| 1783 | /// \param cube The Cube containing the pixel values, from which the spectrum is extracted. |
---|
| 1784 | /// \param pixel The spatial pixel that contains the desired spectrum. |
---|
| 1785 | |
---|
[884] | 1786 | size_t zdim = cube.getDimZ(); |
---|
| 1787 | size_t spatSize = cube.getDimX()*cube.getDimY(); |
---|
[913] | 1788 | if((pixel<0)||(pixel>=spatSize)){ |
---|
| 1789 | DUCHAMPERROR("Image::extractSpectrum", "Requested spatial pixel outside allowed range. Cannot save."); |
---|
| 1790 | } |
---|
| 1791 | else if(zdim != this->numPixels){ |
---|
| 1792 | DUCHAMPERROR("Image::extractSpectrum", "Input array different size to existing array. Cannot save."); |
---|
| 1793 | } |
---|
[378] | 1794 | else { |
---|
[444] | 1795 | if(this->numPixels>0 && this->arrayAllocated) delete [] array; |
---|
[378] | 1796 | this->numPixels = zdim; |
---|
[444] | 1797 | if(this->numPixels>0){ |
---|
| 1798 | this->array = new float[zdim]; |
---|
| 1799 | this->arrayAllocated = true; |
---|
[894] | 1800 | for(size_t z=0;z<zdim;z++) |
---|
[444] | 1801 | this->array[z] = cube.getPixValue(z*spatSize + pixel); |
---|
| 1802 | } |
---|
[378] | 1803 | } |
---|
[258] | 1804 | } |
---|
[378] | 1805 | //-------------------------------------------------------------------- |
---|
[220] | 1806 | |
---|
[884] | 1807 | void Image::extractImage(float *Array, size_t *dim, size_t channel) |
---|
[378] | 1808 | { |
---|
[528] | 1809 | /// @details |
---|
| 1810 | /// A function to extract a 2-D image from a 3-D array. |
---|
| 1811 | /// The array is assumed to be 3-D with the third dimension the spectral one. |
---|
| 1812 | /// The dimensions of the array are in the dim[] array. |
---|
| 1813 | /// The image extracted is the one lying in the channel referenced |
---|
| 1814 | /// by the third argument. |
---|
| 1815 | /// The extracted image is stored in the pixel array Image::array. |
---|
| 1816 | /// \param Array The array containing the pixel values, from which the image is extracted. |
---|
| 1817 | /// \param dim The array of dimension values. |
---|
| 1818 | /// \param channel The spectral channel that contains the desired image. |
---|
[258] | 1819 | |
---|
[884] | 1820 | size_t spatSize = dim[0]*dim[1]; |
---|
[913] | 1821 | if((channel<0)||(channel>=dim[2])){ |
---|
| 1822 | DUCHAMPERROR("Image::extractImage", "Requested channel outside allowed range. Cannot save."); |
---|
| 1823 | } |
---|
| 1824 | else if(spatSize != this->numPixels){ |
---|
| 1825 | DUCHAMPERROR("Image::extractImage", "Input array different size to existing array. Cannot save."); |
---|
| 1826 | } |
---|
[378] | 1827 | else { |
---|
[444] | 1828 | if(this->numPixels>0 && this->arrayAllocated) delete [] array; |
---|
[378] | 1829 | this->numPixels = spatSize; |
---|
[444] | 1830 | if(this->numPixels>0){ |
---|
| 1831 | this->array = new float[spatSize]; |
---|
| 1832 | this->arrayAllocated = true; |
---|
[894] | 1833 | for(size_t npix=0; npix<spatSize; npix++) |
---|
[444] | 1834 | this->array[npix] = Array[channel*spatSize + npix]; |
---|
| 1835 | } |
---|
[378] | 1836 | } |
---|
[220] | 1837 | } |
---|
[378] | 1838 | //-------------------------------------------------------------------- |
---|
[220] | 1839 | |
---|
[884] | 1840 | void Image::extractImage(Cube &cube, size_t channel) |
---|
[378] | 1841 | { |
---|
[528] | 1842 | /// @details |
---|
| 1843 | /// A function to extract a 2-D image from Cube class. |
---|
| 1844 | /// The image extracted is the one lying in the channel referenced |
---|
| 1845 | /// by the second argument. |
---|
| 1846 | /// The extracted image is stored in the pixel array Image::array. |
---|
| 1847 | /// \param cube The Cube containing the pixel values, from which the image is extracted. |
---|
| 1848 | /// \param channel The spectral channel that contains the desired image. |
---|
| 1849 | |
---|
[884] | 1850 | size_t spatSize = cube.getDimX()*cube.getDimY(); |
---|
[913] | 1851 | if((channel<0)||(channel>=cube.getDimZ())){ |
---|
| 1852 | DUCHAMPERROR("Image::extractImage", "Requested channel outside allowed range. Cannot save."); |
---|
| 1853 | } |
---|
| 1854 | else if(spatSize != this->numPixels){ |
---|
| 1855 | DUCHAMPERROR("Image::extractImage", "Input array different size to existing array. Cannot save."); |
---|
| 1856 | } |
---|
[378] | 1857 | else { |
---|
[444] | 1858 | if(this->numPixels>0 && this->arrayAllocated) delete [] array; |
---|
[378] | 1859 | this->numPixels = spatSize; |
---|
[444] | 1860 | if(this->numPixels>0){ |
---|
| 1861 | this->array = new float[spatSize]; |
---|
| 1862 | this->arrayAllocated = true; |
---|
[894] | 1863 | for(size_t npix=0; npix<spatSize; npix++) |
---|
[444] | 1864 | this->array[npix] = cube.getPixValue(channel*spatSize + npix); |
---|
| 1865 | } |
---|
[378] | 1866 | } |
---|
[258] | 1867 | } |
---|
[378] | 1868 | //-------------------------------------------------------------------- |
---|
[220] | 1869 | |
---|
[378] | 1870 | void Image::removeMW() |
---|
| 1871 | { |
---|
[528] | 1872 | /// @details |
---|
| 1873 | /// A function to remove the Milky Way range of channels from a 1-D spectrum. |
---|
| 1874 | /// The array in this Image is assumed to be 1-D, with only the first axisDim |
---|
| 1875 | /// equal to 1. |
---|
| 1876 | /// The values of the MW channels are set to 0, unless they are BLANK. |
---|
| 1877 | |
---|
[378] | 1878 | if(this->par.getFlagMW() && (this->axisDim[1]==1) ){ |
---|
[894] | 1879 | for(size_t z=0;z<this->axisDim[0];z++){ |
---|
[378] | 1880 | if(!this->isBlank(z) && this->par.isInMW(z)) this->array[z]=0.; |
---|
| 1881 | } |
---|
[220] | 1882 | } |
---|
| 1883 | } |
---|
[582] | 1884 | //-------------------------------------------------------------------- |
---|
[378] | 1885 | |
---|
[582] | 1886 | std::vector<Object2D> Image::findSources2D() |
---|
| 1887 | { |
---|
| 1888 | std::vector<bool> thresholdedArray(this->axisDim[0]*this->axisDim[1]); |
---|
[894] | 1889 | for(size_t posY=0;posY<this->axisDim[1];posY++){ |
---|
| 1890 | for(size_t posX=0;posX<this->axisDim[0];posX++){ |
---|
| 1891 | size_t loc = posX + this->axisDim[0]*posY; |
---|
[582] | 1892 | thresholdedArray[loc] = this->isDetection(posX,posY); |
---|
| 1893 | } |
---|
| 1894 | } |
---|
| 1895 | return lutz_detect(thresholdedArray, this->axisDim[0], this->axisDim[1], this->minSize); |
---|
| 1896 | } |
---|
| 1897 | |
---|
| 1898 | std::vector<Scan> Image::findSources1D() |
---|
| 1899 | { |
---|
| 1900 | std::vector<bool> thresholdedArray(this->axisDim[0]); |
---|
[894] | 1901 | for(size_t posX=0;posX<this->axisDim[0];posX++){ |
---|
[582] | 1902 | thresholdedArray[posX] = this->isDetection(posX,0); |
---|
| 1903 | } |
---|
| 1904 | return spectrumDetect(thresholdedArray, this->axisDim[0], this->minSize); |
---|
| 1905 | } |
---|
| 1906 | |
---|
| 1907 | |
---|
[659] | 1908 | std::vector< std::vector<PixelInfo::Voxel> > Cube::getObjVoxList() |
---|
| 1909 | { |
---|
| 1910 | |
---|
| 1911 | std::vector< std::vector<PixelInfo::Voxel> > biglist; |
---|
| 1912 | |
---|
| 1913 | std::vector<Detection>::iterator obj; |
---|
| 1914 | for(obj=this->objectList->begin(); obj<this->objectList->end(); obj++) { |
---|
| 1915 | |
---|
| 1916 | Cube *subcube = new Cube; |
---|
| 1917 | subcube->pars() = this->par; |
---|
| 1918 | subcube->pars().setVerbosity(false); |
---|
| 1919 | subcube->pars().setFlagSubsection(true); |
---|
| 1920 | duchamp::Section sec = obj->getBoundingSection(); |
---|
| 1921 | subcube->pars().setSubsection( sec.getSection() ); |
---|
[700] | 1922 | if(subcube->pars().verifySubsection() == FAILURE) |
---|
[913] | 1923 | DUCHAMPERROR("get object voxel list","Unable to verify the subsection - something's wrong!"); |
---|
[700] | 1924 | if(subcube->getCube() == FAILURE) |
---|
[913] | 1925 | DUCHAMPERROR("get object voxel list","Unable to read the FITS file - something's wrong!"); |
---|
[659] | 1926 | std::vector<PixelInfo::Voxel> voxlist = obj->getPixelSet(); |
---|
| 1927 | std::vector<PixelInfo::Voxel>::iterator vox; |
---|
| 1928 | for(vox=voxlist.begin(); vox<voxlist.end(); vox++){ |
---|
[884] | 1929 | size_t pix = (vox->getX()-subcube->pars().getXOffset()) + |
---|
[659] | 1930 | subcube->getDimX()*(vox->getY()-subcube->pars().getYOffset()) + |
---|
| 1931 | subcube->getDimX()*subcube->getDimY()*(vox->getZ()-subcube->pars().getZOffset()); |
---|
| 1932 | vox->setF( subcube->getPixValue(pix) ); |
---|
| 1933 | } |
---|
| 1934 | biglist.push_back(voxlist); |
---|
| 1935 | delete subcube; |
---|
| 1936 | |
---|
| 1937 | } |
---|
| 1938 | |
---|
| 1939 | return biglist; |
---|
| 1940 | |
---|
| 1941 | } |
---|
| 1942 | |
---|
[220] | 1943 | } |
---|