[3] | 1 | #include <fstream> |
---|
| 2 | #include <iostream> |
---|
| 3 | #include <iomanip> |
---|
| 4 | #include <vector> |
---|
[120] | 5 | #include <duchamp.hh> |
---|
[3] | 6 | #include <Cubes/cubes.hh> |
---|
[144] | 7 | #include <Detection/detection.hh> |
---|
[3] | 8 | #include <ATrous/atrous.hh> |
---|
| 9 | #include <Utils/utils.hh> |
---|
[192] | 10 | #include <Utils/Statistics.hh> |
---|
[3] | 11 | |
---|
[103] | 12 | void Cube::ReconSearch() |
---|
| 13 | { |
---|
| 14 | /** |
---|
| 15 | * Cube::ReconSearch() |
---|
[188] | 16 | * The Cube is first reconstructed, using Cube::ReconCube(). |
---|
| 17 | * It is then searched, using searchReconArray. |
---|
| 18 | * The resulting object list is stored in the Cube, and outputted |
---|
| 19 | * to the log file if the user so requests. |
---|
[103] | 20 | */ |
---|
[188] | 21 | |
---|
| 22 | this->ReconCube(); |
---|
| 23 | |
---|
[189] | 24 | this->setCubeStats(); |
---|
| 25 | |
---|
[188] | 26 | std::cout << " Searching... " << std::flush; |
---|
| 27 | |
---|
| 28 | this->objectList = searchReconArray(this->axisDim,this->array, |
---|
[190] | 29 | this->recon,this->par,this->Stats); |
---|
[188] | 30 | |
---|
| 31 | this->updateDetectMap(); |
---|
| 32 | if(this->par.getFlagLog()) this->logDetectionList(); |
---|
| 33 | |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 37 | void Cube::ReconCube() |
---|
| 38 | { |
---|
| 39 | /** |
---|
| 40 | * Cube::ReconSearch() |
---|
| 41 | * A front-end to the various reconstruction functions, the choice of |
---|
| 42 | * which is determined by the use of the reconDim parameter. |
---|
| 43 | * Differs from ReconSearch only in that no searching is done. |
---|
| 44 | */ |
---|
[120] | 45 | int dimRecon = this->par.getReconDim(); |
---|
[146] | 46 | // Test whether we have eg. an image, but have requested a 3-d |
---|
| 47 | // reconstruction. |
---|
| 48 | // If dimension of data array is less than dimRecon, change dimRecon |
---|
| 49 | // to the dimension of the array. |
---|
[120] | 50 | int numGoodDim = 0; |
---|
| 51 | for(int i=0;i<this->numDim;i++) if(this->axisDim[i]>1) numGoodDim++; |
---|
| 52 | if(numGoodDim<dimRecon) dimRecon = numGoodDim; |
---|
| 53 | this->par.setReconDim(dimRecon); |
---|
| 54 | |
---|
| 55 | switch(dimRecon) |
---|
[103] | 56 | { |
---|
[188] | 57 | case 1: this->ReconCube1D(); break; |
---|
| 58 | case 2: this->ReconCube2D(); break; |
---|
| 59 | case 3: this->ReconCube3D(); break; |
---|
[103] | 60 | default: |
---|
[120] | 61 | if(dimRecon<=0){ |
---|
| 62 | std::stringstream errmsg; |
---|
| 63 | errmsg << "reconDim (" << dimRecon |
---|
| 64 | << ") is less than 1. Performing 1-D reconstruction.\n"; |
---|
[188] | 65 | duchampWarning("ReconCube", errmsg.str()); |
---|
[103] | 66 | this->par.setReconDim(1); |
---|
[188] | 67 | this->ReconCube1D(); |
---|
[103] | 68 | } |
---|
[120] | 69 | else if(dimRecon>3){ |
---|
| 70 | //this probably won't happen with new code above, but just in case... |
---|
| 71 | std::stringstream errmsg; |
---|
| 72 | errmsg << "reconDim (" << dimRecon |
---|
| 73 | << ") is more than 3. Performing 3-D reconstruction.\n"; |
---|
[188] | 74 | duchampWarning("ReconCube", errmsg.str()); |
---|
[103] | 75 | this->par.setReconDim(3); |
---|
[188] | 76 | this->ReconCube3D(); |
---|
[103] | 77 | } |
---|
| 78 | break; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
[146] | 82 | ///////////////////////////////////////////////////////////////////////////// |
---|
[188] | 83 | void Cube::ReconCube1D() |
---|
[3] | 84 | { |
---|
[103] | 85 | /** |
---|
[188] | 86 | * Cube::ReconCube1D() |
---|
[103] | 87 | * This reconstructs a cube by performing a 1D a trous reconstruction |
---|
| 88 | * in the spectrum of each spatial pixel. |
---|
| 89 | */ |
---|
| 90 | long xySize = this->axisDim[0] * this->axisDim[1]; |
---|
[188] | 91 | |
---|
[103] | 92 | long zdim = this->axisDim[2]; |
---|
[3] | 93 | |
---|
[187] | 94 | ProgressBar bar; |
---|
[103] | 95 | if(!this->reconExists){ |
---|
| 96 | std::cout<<" Reconstructing... "; |
---|
[187] | 97 | if(par.isVerbose()) bar.init(xySize); |
---|
[103] | 98 | for(int npix=0; npix<xySize; npix++){ |
---|
[175] | 99 | |
---|
[187] | 100 | if( par.isVerbose() ) bar.update(npix+1); |
---|
[175] | 101 | |
---|
[103] | 102 | float *spec = new float[zdim]; |
---|
| 103 | float *newSpec = new float[zdim]; |
---|
| 104 | for(int z=0;z<zdim;z++) spec[z] = this->array[z*xySize + npix]; |
---|
| 105 | bool verboseFlag = this->par.isVerbose(); |
---|
| 106 | this->par.setVerbosity(false); |
---|
| 107 | atrous1DReconstruct(this->axisDim[2],spec,newSpec,this->par); |
---|
| 108 | this->par.setVerbosity(verboseFlag); |
---|
| 109 | for(int z=0;z<zdim;z++) this->recon[z*xySize+npix] = newSpec[z]; |
---|
[205] | 110 | delete [] spec; |
---|
| 111 | delete [] newSpec; |
---|
[3] | 112 | } |
---|
[103] | 113 | this->reconExists = true; |
---|
[187] | 114 | bar.rewind(); |
---|
[175] | 115 | std::cout << " All Done."; |
---|
| 116 | printSpace(22); |
---|
[188] | 117 | std::cout << "\n"; |
---|
[3] | 118 | } |
---|
[103] | 119 | |
---|
[3] | 120 | } |
---|
| 121 | |
---|
[146] | 122 | ///////////////////////////////////////////////////////////////////////////// |
---|
[188] | 123 | void Cube::ReconCube2D() |
---|
[3] | 124 | { |
---|
[103] | 125 | /** |
---|
[188] | 126 | * Cube::ReconCube2D() |
---|
[103] | 127 | * This reconstructs a cube by performing a 2D a trous reconstruction |
---|
| 128 | * in each spatial image (ie. each channel map) of the cube. |
---|
| 129 | */ |
---|
| 130 | long xySize = this->axisDim[0] * this->axisDim[1]; |
---|
[187] | 131 | ProgressBar bar; |
---|
[3] | 132 | |
---|
[103] | 133 | if(!this->reconExists){ |
---|
| 134 | std::cout<<" Reconstructing... "; |
---|
[187] | 135 | if(par.isVerbose()) bar.init(this->axisDim[2]); |
---|
[103] | 136 | for(int z=0;z<this->axisDim[2];z++){ |
---|
[175] | 137 | |
---|
[187] | 138 | if( par.isVerbose() ) bar.update((z+1)); |
---|
[175] | 139 | |
---|
[103] | 140 | if(!this->par.isInMW(z)){ |
---|
| 141 | float *im = new float[xySize]; |
---|
| 142 | float *newIm = new float[xySize]; |
---|
[146] | 143 | for(int npix=0; npix<xySize; npix++) |
---|
| 144 | im[npix] = this->array[z*xySize+npix]; |
---|
[103] | 145 | bool verboseFlag = this->par.isVerbose(); |
---|
| 146 | this->par.setVerbosity(false); |
---|
[146] | 147 | atrous2DReconstruct(this->axisDim[0],this->axisDim[1], |
---|
| 148 | im,newIm,this->par); |
---|
[103] | 149 | this->par.setVerbosity(verboseFlag); |
---|
[146] | 150 | for(int npix=0; npix<xySize; npix++) |
---|
| 151 | this->recon[z*xySize+npix] = newIm[npix]; |
---|
[205] | 152 | delete [] im; |
---|
| 153 | delete [] newIm; |
---|
[3] | 154 | } |
---|
[103] | 155 | else { |
---|
[146] | 156 | for(int i=z*xySize; i<(z+1)*xySize; i++) |
---|
| 157 | this->recon[i] = this->array[i]; |
---|
[103] | 158 | } |
---|
[3] | 159 | } |
---|
[103] | 160 | this->reconExists = true; |
---|
[187] | 161 | bar.rewind(); |
---|
[175] | 162 | std::cout << " All Done."; |
---|
| 163 | printSpace(22); |
---|
[188] | 164 | std::cout << "\n"; |
---|
[3] | 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
[146] | 168 | ///////////////////////////////////////////////////////////////////////////// |
---|
[188] | 169 | void Cube::ReconCube3D() |
---|
[3] | 170 | { |
---|
[103] | 171 | /** |
---|
[188] | 172 | * Cube::ReconCube3D() |
---|
[103] | 173 | * This performs a full 3D a trous reconstruction of the cube |
---|
| 174 | */ |
---|
[188] | 175 | if(this->axisDim[2]==1) this->ReconCube2D(); |
---|
[3] | 176 | else { |
---|
| 177 | |
---|
[71] | 178 | if(!this->reconExists){ |
---|
| 179 | std::cout<<" Reconstructing... "<<std::flush; |
---|
| 180 | atrous3DReconstruct(this->axisDim[0],this->axisDim[1],this->axisDim[2], |
---|
| 181 | this->array,this->recon,this->par); |
---|
| 182 | this->reconExists = true; |
---|
[175] | 183 | std::cout << " All Done."; |
---|
| 184 | printSpace(22); |
---|
[188] | 185 | std::cout << "\n"; |
---|
[71] | 186 | } |
---|
| 187 | |
---|
[3] | 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
[146] | 191 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 192 | vector <Detection> searchReconArray(long *dim, float *originalArray, |
---|
[190] | 193 | float *reconArray, Param &par, |
---|
| 194 | StatsContainer<float> &stats) |
---|
[3] | 195 | { |
---|
[103] | 196 | /** |
---|
[146] | 197 | * searchReconArray(long *dim, float *originalArray, |
---|
| 198 | * float *reconArray, Param &par) |
---|
[103] | 199 | * This searches for objects in a cube that has been reconstructed. |
---|
| 200 | * |
---|
| 201 | * Inputs: - dimension array |
---|
| 202 | * - original, un-reconstructed image array |
---|
| 203 | * - reconstructed image array |
---|
| 204 | * - parameters |
---|
| 205 | * |
---|
| 206 | * Searches first in each spatial pixel (1D search), |
---|
| 207 | * then in each channel image (2D search). |
---|
| 208 | * |
---|
| 209 | * Returns: vector of Detections resulting from the search. |
---|
| 210 | */ |
---|
[3] | 211 | vector <Detection> outputList; |
---|
[103] | 212 | long zdim = dim[2]; |
---|
| 213 | long xySize = dim[0] * dim[1]; |
---|
[192] | 214 | int num=0; |
---|
[3] | 215 | |
---|
[189] | 216 | bool *doPixel = new bool[xySize]; |
---|
[192] | 217 | // doPixel is a bool array to say whether to look in a given spectrum |
---|
[103] | 218 | for(int npix=0; npix<xySize; npix++){ |
---|
[189] | 219 | doPixel[npix] = false; |
---|
| 220 | for(int z=0;z<zdim;z++) { |
---|
| 221 | doPixel[npix] = doPixel[npix] || |
---|
| 222 | (!par.isBlank(originalArray[npix]) && !par.isInMW(z)); |
---|
| 223 | } |
---|
| 224 | // doPixel[i] is false only when there are no good pixels in spectrum |
---|
| 225 | // of pixel #i. |
---|
[103] | 226 | } |
---|
[3] | 227 | |
---|
[187] | 228 | ProgressBar bar; |
---|
[3] | 229 | // First search -- in each spectrum. |
---|
| 230 | if(zdim > 1){ |
---|
[175] | 231 | if(par.isVerbose()){ |
---|
| 232 | std::cout << "1D: "; |
---|
[187] | 233 | bar.init(xySize); |
---|
[175] | 234 | } |
---|
[3] | 235 | |
---|
| 236 | for(int npix=0; npix<xySize; npix++){ |
---|
| 237 | |
---|
[187] | 238 | if( par.isVerbose() ) bar.update(npix+1); |
---|
[3] | 239 | |
---|
[189] | 240 | if(doPixel[npix]){ |
---|
[192] | 241 | |
---|
[103] | 242 | long *specdim = new long[2]; |
---|
| 243 | specdim[0] = zdim; specdim[1]=1; |
---|
| 244 | Image *spectrum = new Image(specdim); |
---|
| 245 | delete [] specdim; |
---|
| 246 | spectrum->saveParam(par); |
---|
[190] | 247 | spectrum->saveStats(stats); |
---|
[146] | 248 | // beam size: for spectrum, only neighbouring channels correlated |
---|
[103] | 249 | spectrum->extractSpectrum(reconArray,dim,npix); |
---|
[192] | 250 | spectrum->setMinSize(par.getMinChannels()); |
---|
[103] | 251 | spectrum->removeMW(); // only works if flagMW is true |
---|
| 252 | spectrum->spectrumDetect(); |
---|
[146] | 253 | num += spectrum->getNumObj(); |
---|
[103] | 254 | for(int obj=0;obj<spectrum->getNumObj();obj++){ |
---|
| 255 | Detection *object = new Detection; |
---|
| 256 | *object = spectrum->getObject(obj); |
---|
| 257 | for(int pix=0;pix<object->getSize();pix++) { |
---|
| 258 | // Fix up coordinates of each pixel to match original array |
---|
| 259 | object->setZ(pix, object->getX(pix)); |
---|
| 260 | object->setX(pix, npix%dim[0]); |
---|
| 261 | object->setY(pix, npix/dim[0]); |
---|
[146] | 262 | object->setF(pix, originalArray[object->getX(pix)+ |
---|
| 263 | object->getY(pix)*dim[0]+ |
---|
| 264 | object->getZ(pix)*xySize]); |
---|
[103] | 265 | // NB: set F to the original value, not the recon value. |
---|
| 266 | } |
---|
| 267 | object->addOffsets(par); |
---|
| 268 | object->calcParams(); |
---|
| 269 | mergeIntoList(*object,outputList,par); |
---|
| 270 | delete object; |
---|
[3] | 271 | } |
---|
[103] | 272 | delete spectrum; |
---|
[3] | 273 | } |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | num = outputList.size(); |
---|
[175] | 277 | if(par.isVerbose()) { |
---|
[187] | 278 | bar.rewind(); |
---|
[175] | 279 | std::cout <<"Found " << num <<"; " << std::flush; |
---|
| 280 | } |
---|
[86] | 281 | } |
---|
[3] | 282 | |
---|
| 283 | // Second search -- in each channel |
---|
[175] | 284 | if(par.isVerbose()){ |
---|
| 285 | std::cout << "2D: "; |
---|
[187] | 286 | bar.init(zdim); |
---|
[175] | 287 | } |
---|
[71] | 288 | |
---|
[146] | 289 | num = 0; |
---|
| 290 | |
---|
[86] | 291 | for(int z=0; z<zdim; z++){ // loop over all channels |
---|
[3] | 292 | |
---|
[187] | 293 | if( par.isVerbose() ) bar.update(z+1); |
---|
[3] | 294 | |
---|
[146] | 295 | if(!par.isInMW(z)){ |
---|
| 296 | // purpose of this is to ignore the Milky Way channels |
---|
| 297 | // if we are flagging them |
---|
| 298 | |
---|
[103] | 299 | long *imdim = new long[2]; |
---|
| 300 | imdim[0] = dim[0]; imdim[1] = dim[1]; |
---|
| 301 | Image *channelImage = new Image(imdim); |
---|
| 302 | channelImage->saveParam(par); |
---|
[190] | 303 | channelImage->saveStats(stats); |
---|
[103] | 304 | delete [] imdim; |
---|
[53] | 305 | channelImage->extractImage(reconArray,dim,z); |
---|
[13] | 306 | channelImage->setMinSize(par.getMinPix()); |
---|
[3] | 307 | channelImage->lutz_detect(); |
---|
[146] | 308 | num += channelImage->getNumObj(); |
---|
[3] | 309 | for(int obj=0;obj<channelImage->getNumObj();obj++){ |
---|
| 310 | Detection *object = new Detection; |
---|
| 311 | *object = channelImage->getObject(obj); |
---|
[146] | 312 | // Fix up z coordinates of each pixel to match original array |
---|
| 313 | // (x & y are fine) |
---|
[3] | 314 | for(int pix=0;pix<object->getSize();pix++){ |
---|
| 315 | object->setZ(pix, z); |
---|
[146] | 316 | object->setF(pix, originalArray[object->getX(pix)+ |
---|
| 317 | object->getY(pix)*dim[0]+ |
---|
| 318 | z*xySize]); |
---|
[3] | 319 | // NB: set F to the original value, not the recon value. |
---|
| 320 | } |
---|
| 321 | object->addOffsets(par); |
---|
| 322 | object->calcParams(); |
---|
| 323 | mergeIntoList(*object,outputList,par); |
---|
| 324 | delete object; |
---|
| 325 | } |
---|
[103] | 326 | delete channelImage; |
---|
| 327 | } |
---|
[86] | 328 | |
---|
[3] | 329 | } |
---|
| 330 | |
---|
[187] | 331 | bar.rewind(); |
---|
[175] | 332 | std::cout << "Found " << num << "."; |
---|
| 333 | printSpace(22); |
---|
| 334 | std::cout << std::endl << std::flush; |
---|
[86] | 335 | |
---|
[189] | 336 | delete [] doPixel; |
---|
[3] | 337 | |
---|
| 338 | return outputList; |
---|
| 339 | } |
---|