1 | #include <unistd.h> |
---|
2 | #include <iostream> |
---|
3 | #include <iomanip> |
---|
4 | #include <vector> |
---|
5 | #include <algorithm> |
---|
6 | #include <string> |
---|
7 | #include <math.h> |
---|
8 | |
---|
9 | #include <wcs.h> |
---|
10 | |
---|
11 | #include <cpgplot.h> |
---|
12 | |
---|
13 | #include <duchamp.hh> |
---|
14 | #include <param.hh> |
---|
15 | #include <fitsHeader.hh> |
---|
16 | #include <Cubes/cubes.hh> |
---|
17 | #include <PixelMap/Voxel.hh> |
---|
18 | #include <PixelMap/Object3D.hh> |
---|
19 | #include <Detection/detection.hh> |
---|
20 | #include <Detection/columns.hh> |
---|
21 | #include <Utils/utils.hh> |
---|
22 | #include <Utils/mycpgplot.hh> |
---|
23 | #include <Utils/Statistics.hh> |
---|
24 | using namespace Column; |
---|
25 | using namespace mycpgplot; |
---|
26 | using namespace Statistics; |
---|
27 | using namespace PixelInfo; |
---|
28 | |
---|
29 | #ifdef TEST_DEBUG |
---|
30 | const bool TESTING=true; |
---|
31 | #else |
---|
32 | const bool TESTING=false; |
---|
33 | #endif |
---|
34 | |
---|
35 | /****************************************************************/ |
---|
36 | /////////////////////////////////////////////////// |
---|
37 | //// Functions for DataArray class: |
---|
38 | /////////////////////////////////////////////////// |
---|
39 | |
---|
40 | DataArray::DataArray(){ |
---|
41 | /** |
---|
42 | * Fundamental constructor for DataArray. |
---|
43 | * Number of dimensions and pixels are set to 0. Nothing else allocated. |
---|
44 | */ |
---|
45 | this->numDim=0; |
---|
46 | this->numPixels=0; |
---|
47 | }; |
---|
48 | //-------------------------------------------------------------------- |
---|
49 | |
---|
50 | DataArray::DataArray(short int nDim){ |
---|
51 | /** |
---|
52 | * N-dimensional constructor for DataArray. |
---|
53 | * Number of dimensions defined, and dimension array allocated. |
---|
54 | * Number of pixels are set to 0. |
---|
55 | * \param nDim Number of dimensions. |
---|
56 | */ |
---|
57 | if(nDim>0) this->axisDim = new long[nDim]; |
---|
58 | this->numDim=nDim; |
---|
59 | this->numPixels=0; |
---|
60 | }; |
---|
61 | //-------------------------------------------------------------------- |
---|
62 | |
---|
63 | DataArray::DataArray(short int nDim, long size){ |
---|
64 | /** |
---|
65 | * N-dimensional constructor for DataArray. |
---|
66 | * Number of dimensions and number of pixels defined. |
---|
67 | * Arrays allocated based on these values. |
---|
68 | * \param nDim Number of dimensions. |
---|
69 | * \param size Number of pixels. |
---|
70 | * |
---|
71 | * Note that we can assign values to the dimension array. |
---|
72 | */ |
---|
73 | |
---|
74 | if(size<0) |
---|
75 | duchampError("DataArray(nDim,size)", |
---|
76 | "Negative size -- could not define DataArray"); |
---|
77 | else if(nDim<0) |
---|
78 | duchampError("DataArray(nDim,size)", |
---|
79 | "Negative number of dimensions: could not define DataArray"); |
---|
80 | else { |
---|
81 | if(size>0) this->array = new float[size]; |
---|
82 | this->numPixels = size; |
---|
83 | if(nDim>0) this->axisDim = new long[nDim]; |
---|
84 | this->numDim = nDim; |
---|
85 | } |
---|
86 | } |
---|
87 | //-------------------------------------------------------------------- |
---|
88 | |
---|
89 | DataArray::DataArray(short int nDim, long *dimensions) |
---|
90 | { |
---|
91 | /** |
---|
92 | * Most robust constructor for DataArray. |
---|
93 | * Number and sizes of dimensions are defined, and hence the number of |
---|
94 | * pixels. Arrays allocated based on these values. |
---|
95 | * \param nDim Number of dimensions. |
---|
96 | * \param dimensions Array giving sizes of dimensions. |
---|
97 | */ |
---|
98 | if(nDim<0) |
---|
99 | duchampError("DataArray(nDim,dimArray)", |
---|
100 | "Negative number of dimensions: could not define DataArray"); |
---|
101 | else { |
---|
102 | int size = dimensions[0]; |
---|
103 | for(int i=1;i<nDim;i++) size *= dimensions[i]; |
---|
104 | if(size<0) |
---|
105 | duchampError("DataArray(nDim,dimArray)", |
---|
106 | "Negative size: could not define DataArray"); |
---|
107 | else{ |
---|
108 | this->numPixels = size; |
---|
109 | if(size>0) this->array = new float[size]; |
---|
110 | this->numDim=nDim; |
---|
111 | if(nDim>0) this->axisDim = new long[nDim]; |
---|
112 | for(int i=0;i<nDim;i++) this->axisDim[i] = dimensions[i]; |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | //-------------------------------------------------------------------- |
---|
117 | |
---|
118 | DataArray::~DataArray() |
---|
119 | { |
---|
120 | /** |
---|
121 | * Destructor -- arrays deleted if they have been allocated, and the |
---|
122 | * object list is cleared. |
---|
123 | */ |
---|
124 | if(this->numPixels>0) delete [] this->array; |
---|
125 | if(this->numDim>0) delete [] this->axisDim; |
---|
126 | this->objectList.clear(); |
---|
127 | } |
---|
128 | //-------------------------------------------------------------------- |
---|
129 | //-------------------------------------------------------------------- |
---|
130 | |
---|
131 | void DataArray::getDim(long &x, long &y, long &z){ |
---|
132 | /** |
---|
133 | * The sizes of the first three dimensions (if they exist) are returned. |
---|
134 | * \param x The first dimension. Defaults to 0 if numDim \f$\le\f$ 0. |
---|
135 | * \param y The second dimension. Defaults to 1 if numDim \f$\le\f$ 1. |
---|
136 | * \param z The third dimension. Defaults to 1 if numDim \f$\le\f$ 2. |
---|
137 | */ |
---|
138 | if(this->numDim>0) x=this->axisDim[0]; |
---|
139 | else x=0; |
---|
140 | if(this->numDim>1) y=this->axisDim[1]; |
---|
141 | else y=1; |
---|
142 | if(this->numDim>2) z=this->axisDim[2]; |
---|
143 | else z=1; |
---|
144 | } |
---|
145 | //-------------------------------------------------------------------- |
---|
146 | |
---|
147 | void DataArray::getDimArray(long *output){ |
---|
148 | /** |
---|
149 | * The axisDim array is written to output. This needs to be defined |
---|
150 | * beforehand: no checking is done on the memory. |
---|
151 | * \param output The array that is written to. |
---|
152 | */ |
---|
153 | for(int i=0;i<this->numDim;i++) output[i] = this->axisDim[i]; |
---|
154 | } |
---|
155 | //-------------------------------------------------------------------- |
---|
156 | |
---|
157 | void DataArray::getArray(float *output){ |
---|
158 | /** |
---|
159 | * The pixel value array is written to output. This needs to be defined |
---|
160 | * beforehand: no checking is done on the memory. |
---|
161 | * \param output The array that is written to. |
---|
162 | */ |
---|
163 | for(int i=0;i<this->numPixels;i++) output[i] = this->array[i]; |
---|
164 | } |
---|
165 | //-------------------------------------------------------------------- |
---|
166 | |
---|
167 | void DataArray::saveArray(float *input, long size){ |
---|
168 | /** |
---|
169 | * Saves the array in input to the pixel array DataArray::array. |
---|
170 | * The size of the array given must be the same as the current number of |
---|
171 | * pixels, else an error message is returned and nothing is done. |
---|
172 | * \param input The array of values to be saved. |
---|
173 | * \param size The size of input. |
---|
174 | */ |
---|
175 | if(size != this->numPixels) |
---|
176 | duchampError("DataArray::saveArray", |
---|
177 | "Input array different size to existing array. Cannot save."); |
---|
178 | else { |
---|
179 | if(this->numPixels>0) delete [] this->array; |
---|
180 | this->numPixels = size; |
---|
181 | this->array = new float[size]; |
---|
182 | for(int i=0;i<size;i++) this->array[i] = input[i]; |
---|
183 | } |
---|
184 | } |
---|
185 | //-------------------------------------------------------------------- |
---|
186 | |
---|
187 | void DataArray::addObject(Detection object){ |
---|
188 | /** |
---|
189 | * \param object The object to be added to the object list. |
---|
190 | */ |
---|
191 | // objectList is a vector, so just use push_back() |
---|
192 | this->objectList.push_back(object); |
---|
193 | } |
---|
194 | //-------------------------------------------------------------------- |
---|
195 | |
---|
196 | void DataArray::addObjectList(std::vector <Detection> newlist) { |
---|
197 | /** |
---|
198 | * \param newlist The list of objects to be added to the object list. |
---|
199 | */ |
---|
200 | for(int i=0;i<newlist.size();i++) this->objectList.push_back(newlist[i]); |
---|
201 | } |
---|
202 | //-------------------------------------------------------------------- |
---|
203 | |
---|
204 | // void DataArray::addObjectOffsets(){ |
---|
205 | // /** |
---|
206 | // * Add the pixel offsets (that is, offsets from the corner of the cube to the |
---|
207 | // * corner of the utilised part) that are stored in the Param set to the |
---|
208 | // * coordinate values of each object in the object list. |
---|
209 | // */ |
---|
210 | // for(int i=0;i<this->objectList.size();i++){ |
---|
211 | // for(int p=0;p<this->objectList[i].getSize();p++){ |
---|
212 | // this->objectList[i].setX(p,this->objectList[i].getX(p)+ |
---|
213 | // this->par.getXOffset()); |
---|
214 | // this->objectList[i].setY(p,this->objectList[i].getY(p)+ |
---|
215 | // this->par.getYOffset()); |
---|
216 | // this->objectList[i].setZ(p,this->objectList[i].getZ(p)+ |
---|
217 | // this->par.getZOffset()); |
---|
218 | // } |
---|
219 | // } |
---|
220 | // } |
---|
221 | // //-------------------------------------------------------------------- |
---|
222 | |
---|
223 | bool DataArray::isDetection(float value){ |
---|
224 | /** |
---|
225 | * Is a given value a detection, based on the statistics in the |
---|
226 | * DataArray's StatsContainer? |
---|
227 | * \param value The pixel value to test. |
---|
228 | */ |
---|
229 | if(par.isBlank(value)) return false; |
---|
230 | else return Stats.isDetection(value); |
---|
231 | }; |
---|
232 | //-------------------------------------------------------------------- |
---|
233 | |
---|
234 | bool DataArray::isDetection(long voxel){ |
---|
235 | /** |
---|
236 | * Is a given pixel a detection, based on the statistics in the |
---|
237 | * DataArray's StatsContainer? |
---|
238 | * If the pixel lies outside the valid range for the data array, return false. |
---|
239 | * \param voxel Location of the DataArray's pixel to be tested. |
---|
240 | */ |
---|
241 | if((voxel<0)||(voxel>this->numPixels)) return false; |
---|
242 | else if(par.isBlank(this->array[voxel])) return false; |
---|
243 | else return Stats.isDetection(this->array[voxel]); |
---|
244 | }; |
---|
245 | //-------------------------------------------------------------------- |
---|
246 | |
---|
247 | std::ostream& operator<< ( std::ostream& theStream, DataArray &array) |
---|
248 | { |
---|
249 | /** |
---|
250 | * A way to print out the pixel coordinates & flux values of the |
---|
251 | * list of detected objects belonging to the DataArray. |
---|
252 | * These are formatted nicely according to the << operator for Detection, |
---|
253 | * with a line indicating the number of detections at the start. |
---|
254 | * \param theStream The ostream object to which the output should be sent. |
---|
255 | * \param array The DataArray containing the list of objects. |
---|
256 | */ |
---|
257 | for(int i=0;i<array.numDim;i++){ |
---|
258 | if(i>0) theStream<<"x"; |
---|
259 | theStream<<array.axisDim[i]; |
---|
260 | } |
---|
261 | theStream<<std::endl; |
---|
262 | theStream<<array.objectList.size()<<" detections:\n--------------\n"; |
---|
263 | for(int i=0;i<array.objectList.size();i++){ |
---|
264 | theStream << "Detection #" << array.objectList[i].getID()<<std::endl; |
---|
265 | Detection *obj = new Detection; |
---|
266 | *obj = array.objectList[i]; |
---|
267 | obj->addOffsets(); |
---|
268 | theStream<<*obj; |
---|
269 | delete obj; |
---|
270 | } |
---|
271 | theStream<<"--------------\n"; |
---|
272 | return theStream; |
---|
273 | } |
---|
274 | |
---|
275 | /****************************************************************/ |
---|
276 | ///////////////////////////////////////////////////////////// |
---|
277 | //// Functions for Cube class |
---|
278 | ///////////////////////////////////////////////////////////// |
---|
279 | |
---|
280 | Cube::Cube(){ |
---|
281 | /** |
---|
282 | * Basic Constructor for Cube class. |
---|
283 | * numDim set to 3, but numPixels to 0 and all bool flags to false. |
---|
284 | * No allocation done. |
---|
285 | */ |
---|
286 | numPixels=0; numDim=3; |
---|
287 | reconExists = false; reconAllocated = false; baselineAllocated = false; |
---|
288 | }; |
---|
289 | //-------------------------------------------------------------------- |
---|
290 | |
---|
291 | Cube::Cube(long size){ |
---|
292 | /** |
---|
293 | * Alternative Cube constructor, where size is given but not individual |
---|
294 | * dimensions. Arrays are allocated as appropriate (according to the |
---|
295 | * relevant flags in Param set), but the Cube::axisDim array is not. |
---|
296 | */ |
---|
297 | this->reconAllocated = false; |
---|
298 | this->baselineAllocated = false; |
---|
299 | this->numPixels = this->numDim = 0; |
---|
300 | if(size<0) |
---|
301 | duchampError("Cube(size)","Negative size -- could not define Cube"); |
---|
302 | else{ |
---|
303 | if(size>0){ |
---|
304 | this->array = new float[size]; |
---|
305 | if(this->par.getFlagATrous()||this->par.getFlagSmooth()){ |
---|
306 | this->recon = new float[size]; |
---|
307 | this->reconAllocated = true; |
---|
308 | } |
---|
309 | if(this->par.getFlagBaseline()){ |
---|
310 | this->baseline = new float[size]; |
---|
311 | this->baselineAllocated = true; |
---|
312 | } |
---|
313 | } |
---|
314 | this->numPixels = size; |
---|
315 | this->axisDim = new long[2]; |
---|
316 | this->numDim = 3; |
---|
317 | this->reconExists = false; |
---|
318 | } |
---|
319 | } |
---|
320 | //-------------------------------------------------------------------- |
---|
321 | |
---|
322 | Cube::Cube(long *dimensions){ |
---|
323 | /** |
---|
324 | * Alternative Cube constructor, where sizes of dimensions are given. |
---|
325 | * Arrays are allocated as appropriate (according to the |
---|
326 | * relevant flags in Param set), as is the Cube::axisDim array. |
---|
327 | */ |
---|
328 | int size = dimensions[0] * dimensions[1] * dimensions[2]; |
---|
329 | int imsize = dimensions[0] * dimensions[1]; |
---|
330 | this->reconAllocated = false; |
---|
331 | this->baselineAllocated = false; |
---|
332 | this->numPixels = this->numDim = 0; |
---|
333 | if((size<0) || (imsize<0) ) |
---|
334 | duchampError("Cube(dimArray)","Negative size -- could not define Cube"); |
---|
335 | else{ |
---|
336 | this->numPixels = size; |
---|
337 | if(size>0){ |
---|
338 | this->array = new float[size]; |
---|
339 | this->detectMap = new short[imsize]; |
---|
340 | if(this->par.getFlagATrous()||this->par.getFlagSmooth()){ |
---|
341 | this->recon = new float[size]; |
---|
342 | this->reconAllocated = true; |
---|
343 | } |
---|
344 | if(this->par.getFlagBaseline()){ |
---|
345 | this->baseline = new float[size]; |
---|
346 | this->baselineAllocated = true; |
---|
347 | } |
---|
348 | } |
---|
349 | this->numDim = 3; |
---|
350 | this->axisDim = new long[3]; |
---|
351 | for(int i=0;i<3 ;i++) this->axisDim[i] = dimensions[i]; |
---|
352 | for(int i=0;i<imsize;i++) this->detectMap[i] = 0; |
---|
353 | this->reconExists = false; |
---|
354 | } |
---|
355 | } |
---|
356 | //-------------------------------------------------------------------- |
---|
357 | |
---|
358 | Cube::~Cube() |
---|
359 | { |
---|
360 | /** |
---|
361 | * The destructor deletes the memory allocated for Cube::detectMap, and, |
---|
362 | * if these have been allocated, Cube::recon and Cube::baseline. |
---|
363 | */ |
---|
364 | delete [] this->detectMap; |
---|
365 | if(this->reconAllocated) delete [] this->recon; |
---|
366 | if(this->baselineAllocated) delete [] this->baseline; |
---|
367 | } |
---|
368 | //-------------------------------------------------------------------- |
---|
369 | |
---|
370 | void Cube::initialiseCube(long *dimensions) |
---|
371 | { |
---|
372 | /** |
---|
373 | * This function will set the sizes of all arrays that will be used by Cube. |
---|
374 | * It will also define the values of the axis dimensions: this will be done |
---|
375 | * using the WCS in the FitsHeader class, so the WCS needs to be good and |
---|
376 | * have three axes. If this is not the case, the axes are assumed to be |
---|
377 | * ordered in the sense of lng,lat,spc. |
---|
378 | * |
---|
379 | * \param dimensions An array of values giving the dimensions (sizes) for |
---|
380 | * all axes. |
---|
381 | */ |
---|
382 | |
---|
383 | int lng,lat,spc,size,imsize; |
---|
384 | |
---|
385 | if(this->head.isWCS() && (this->head.getNumAxes()>=3)){ |
---|
386 | // if there is a WCS and there is at least 3 axes |
---|
387 | lng = this->head.getWCS()->lng; |
---|
388 | lat = this->head.getWCS()->lat; |
---|
389 | spc = this->head.getWCS()->spec; |
---|
390 | } |
---|
391 | else{ |
---|
392 | // just take dimensions[] at face value |
---|
393 | lng = 0; |
---|
394 | lat = 1; |
---|
395 | spc = 2; |
---|
396 | } |
---|
397 | |
---|
398 | size = dimensions[lng]; |
---|
399 | if(this->head.getNumAxes()>1) size *= dimensions[lat]; |
---|
400 | if(this->head.getNumAxes()>2) size *= dimensions[spc]; |
---|
401 | imsize = dimensions[lng]; |
---|
402 | if(this->head.getNumAxes()>1) imsize *= dimensions[lat]; |
---|
403 | |
---|
404 | this->reconAllocated = false; |
---|
405 | this->baselineAllocated = false; |
---|
406 | |
---|
407 | if((size<0) || (imsize<0) ) |
---|
408 | duchampError("Cube::initialiseCube(dimArray)", |
---|
409 | "Negative size -- could not define Cube.\n"); |
---|
410 | else{ |
---|
411 | this->numPixels = size; |
---|
412 | if(size>0){ |
---|
413 | this->array = new float[size]; |
---|
414 | this->detectMap = new short[imsize]; |
---|
415 | if(this->par.getFlagATrous() || this->par.getFlagSmooth()){ |
---|
416 | this->recon = new float[size]; |
---|
417 | this->reconAllocated = true; |
---|
418 | } |
---|
419 | if(this->par.getFlagBaseline()){ |
---|
420 | this->baseline = new float[size]; |
---|
421 | this->baselineAllocated = true; |
---|
422 | } |
---|
423 | } |
---|
424 | this->numDim = 3; |
---|
425 | this->axisDim = new long[this->numDim]; |
---|
426 | this->axisDim[0] = dimensions[lng]; |
---|
427 | if(this->head.getNumAxes()>1) this->axisDim[1] = dimensions[lat]; |
---|
428 | else this->axisDim[1] = 1; |
---|
429 | if(this->head.getNumAxes()>2) this->axisDim[2] = dimensions[spc]; |
---|
430 | else this->axisDim[2] = 1; |
---|
431 | for(int i=0;i<imsize;i++) this->detectMap[i] = 0; |
---|
432 | this->reconExists = false; |
---|
433 | } |
---|
434 | } |
---|
435 | //-------------------------------------------------------------------- |
---|
436 | |
---|
437 | int Cube::getCube(){ |
---|
438 | /** |
---|
439 | * A front-end to the Cube::getCube() function, that does |
---|
440 | * subsection checks. |
---|
441 | * Assumes the Param is set up properly. |
---|
442 | */ |
---|
443 | std::string fname = par.getImageFile(); |
---|
444 | if(par.getFlagSubsection()) fname+=par.getSubsection(); |
---|
445 | return getCube(fname); |
---|
446 | }; |
---|
447 | //-------------------------------------------------------------------- |
---|
448 | |
---|
449 | void Cube::saveArray(float *input, long size){ |
---|
450 | if(size != this->numPixels){ |
---|
451 | std::stringstream errmsg; |
---|
452 | errmsg << "Input array different size to existing array (" |
---|
453 | << size << " cf. " << this->numPixels << "). Cannot save.\n"; |
---|
454 | duchampError("Cube::saveArray",errmsg.str()); |
---|
455 | } |
---|
456 | else { |
---|
457 | if(this->numPixels>0) delete [] array; |
---|
458 | this->numPixels = size; |
---|
459 | this->array = new float[size]; |
---|
460 | for(int i=0;i<size;i++) this->array[i] = input[i]; |
---|
461 | } |
---|
462 | } |
---|
463 | //-------------------------------------------------------------------- |
---|
464 | |
---|
465 | void Cube::saveRecon(float *input, long size){ |
---|
466 | /** |
---|
467 | * Saves the array in input to the reconstructed array Cube::recon |
---|
468 | * The size of the array given must be the same as the current number of |
---|
469 | * pixels, else an error message is returned and nothing is done. |
---|
470 | * If the recon array has already been allocated, it is deleted first, and |
---|
471 | * then the space is allocated. |
---|
472 | * Afterwards, the appropriate flags are set. |
---|
473 | * \param input The array of values to be saved. |
---|
474 | * \param size The size of input. |
---|
475 | */ |
---|
476 | if(size != this->numPixels){ |
---|
477 | std::stringstream errmsg; |
---|
478 | errmsg << "Input array different size to existing array (" |
---|
479 | << size << " cf. " << this->numPixels << "). Cannot save.\n"; |
---|
480 | duchampError("Cube::saveRecon",errmsg.str()); |
---|
481 | } |
---|
482 | else { |
---|
483 | if(this->numPixels>0){ |
---|
484 | if(this->reconAllocated) delete [] this->recon; |
---|
485 | this->numPixels = size; |
---|
486 | this->recon = new float[size]; |
---|
487 | this->reconAllocated = true; |
---|
488 | for(int i=0;i<size;i++) this->recon[i] = input[i]; |
---|
489 | this->reconExists = true; |
---|
490 | } |
---|
491 | } |
---|
492 | } |
---|
493 | //-------------------------------------------------------------------- |
---|
494 | |
---|
495 | void Cube::getRecon(float *output){ |
---|
496 | /** |
---|
497 | * The reconstructed array is written to output. The output array needs to |
---|
498 | * be defined beforehand: no checking is done on the memory. |
---|
499 | * \param output The array that is written to. |
---|
500 | */ |
---|
501 | // Need check for change in number of pixels! |
---|
502 | for(int i=0;i<this->numPixels;i++){ |
---|
503 | if(this->reconExists) output[i] = this->recon[i]; |
---|
504 | else output[i] = 0.; |
---|
505 | } |
---|
506 | } |
---|
507 | //-------------------------------------------------------------------- |
---|
508 | |
---|
509 | void Cube::removeMW() |
---|
510 | { |
---|
511 | /** |
---|
512 | * The channels corresponding to the Milky Way range (as given by the Param |
---|
513 | * set) are all set to 0 in the pixel array. |
---|
514 | * Only done if the appropriate flag is set, and the pixels are not BLANK. |
---|
515 | * \deprecated |
---|
516 | */ |
---|
517 | if(this->par.getFlagMW()){ |
---|
518 | for(int pix=0;pix<this->axisDim[0]*this->axisDim[1];pix++){ |
---|
519 | for(int z=0;z<this->axisDim[2];z++){ |
---|
520 | int pos = z*this->axisDim[0]*this->axisDim[1] + pix; |
---|
521 | if(!this->isBlank(pos) && this->par.isInMW(z)) this->array[pos]=0.; |
---|
522 | } |
---|
523 | } |
---|
524 | } |
---|
525 | } |
---|
526 | //-------------------------------------------------------------------- |
---|
527 | |
---|
528 | void Cube::setCubeStatsOld() |
---|
529 | { |
---|
530 | /** |
---|
531 | * \deprecated |
---|
532 | * |
---|
533 | * Calculates the full statistics for the cube: mean, rms, median, madfm. |
---|
534 | * Only do this if the threshold has not been defined (ie. is still 0., |
---|
535 | * its default). |
---|
536 | * Also work out the threshold and store it in the Param set. |
---|
537 | * |
---|
538 | * For the stats calculations, we ignore BLANKs and MW channels. |
---|
539 | */ |
---|
540 | |
---|
541 | if(!this->par.getFlagFDR() && this->par.getFlagUserThreshold() ){ |
---|
542 | // if the user has defined a threshold, set this in the StatsContainer |
---|
543 | this->Stats.setThreshold( this->par.getThreshold() ); |
---|
544 | } |
---|
545 | else{ |
---|
546 | // only work out the mean etc if we need to. |
---|
547 | // the only reason we don't is if the user has specified a threshold. |
---|
548 | |
---|
549 | std::cout << "Calculating the cube statistics... " << std::flush; |
---|
550 | |
---|
551 | // get number of good pixels; |
---|
552 | int goodSize = 0; |
---|
553 | for(int p=0;p<this->axisDim[0]*this->axisDim[1];p++){ |
---|
554 | for(int z=0;z<this->axisDim[2];z++){ |
---|
555 | int vox = z * this->axisDim[0] * this->axisDim[1] + p; |
---|
556 | if(!this->isBlank(vox) && !this->par.isInMW(z)) goodSize++; |
---|
557 | } |
---|
558 | } |
---|
559 | |
---|
560 | float *tempArray = new float[goodSize]; |
---|
561 | |
---|
562 | goodSize=0; |
---|
563 | for(int p=0;p<this->axisDim[0]*this->axisDim[1];p++){ |
---|
564 | for(int z=0;z<this->axisDim[2];z++){ |
---|
565 | int vox = z * this->axisDim[0] * this->axisDim[1] + p; |
---|
566 | if(!this->isBlank(vox) && !this->par.isInMW(z)) |
---|
567 | tempArray[goodSize++] = this->array[vox]; |
---|
568 | } |
---|
569 | } |
---|
570 | if(!this->reconExists){ |
---|
571 | // if there's no recon array, calculate everything from orig array |
---|
572 | this->Stats.calculate(tempArray,goodSize); |
---|
573 | } |
---|
574 | else{ |
---|
575 | // just get mean & median from orig array, and rms & madfm from recon |
---|
576 | StatsContainer<float> origStats,reconStats; |
---|
577 | origStats.calculate(tempArray,goodSize); |
---|
578 | goodSize=0; |
---|
579 | for(int p=0;p<this->axisDim[0]*this->axisDim[1];p++){ |
---|
580 | for(int z=0;z<this->axisDim[2];z++){ |
---|
581 | int vox = z * this->axisDim[0] * this->axisDim[1] + p; |
---|
582 | if(!this->isBlank(vox) && !this->par.isInMW(z)) |
---|
583 | tempArray[goodSize++] = this->array[vox] - this->recon[vox]; |
---|
584 | } |
---|
585 | } |
---|
586 | reconStats.calculate(tempArray,goodSize); |
---|
587 | |
---|
588 | // Get the "middle" estimators from the original array. |
---|
589 | this->Stats.setMean(origStats.getMean()); |
---|
590 | this->Stats.setMedian(origStats.getMedian()); |
---|
591 | // Get the "spread" estimators from the residual (orig-recon) array |
---|
592 | this->Stats.setStddev(reconStats.getStddev()); |
---|
593 | this->Stats.setMadfm(reconStats.getMadfm()); |
---|
594 | } |
---|
595 | |
---|
596 | delete [] tempArray; |
---|
597 | |
---|
598 | this->Stats.setUseFDR( this->par.getFlagFDR() ); |
---|
599 | // If the FDR method has been requested |
---|
600 | if(this->par.getFlagFDR()) this->setupFDR(); |
---|
601 | else{ |
---|
602 | // otherwise, calculate one based on the requested SNR cut level, and |
---|
603 | // then set the threshold parameter in the Par set. |
---|
604 | this->Stats.setThresholdSNR( this->par.getCut() ); |
---|
605 | this->par.setThreshold( this->Stats.getThreshold() ); |
---|
606 | } |
---|
607 | |
---|
608 | |
---|
609 | } |
---|
610 | std::cout << "Using "; |
---|
611 | if(this->par.getFlagFDR()) std::cout << "effective "; |
---|
612 | std::cout << "flux threshold of: "; |
---|
613 | float thresh = this->Stats.getThreshold(); |
---|
614 | if(this->par.getFlagNegative()) thresh *= -1.; |
---|
615 | std::cout << thresh << std::endl; |
---|
616 | |
---|
617 | } |
---|
618 | //-------------------------------------------------------------------- |
---|
619 | |
---|
620 | void Cube::setCubeStats() |
---|
621 | { |
---|
622 | /** |
---|
623 | * Calculates the full statistics for the cube: |
---|
624 | * mean, rms, median, madfm |
---|
625 | * Only do this if the threshold has not been defined (ie. is still 0., |
---|
626 | * its default). |
---|
627 | * Also work out the threshold and store it in the par set. |
---|
628 | * |
---|
629 | * Different from Cube::setCubeStatsOld() as it doesn't use the |
---|
630 | * getStats functions but has own versions of them hardcoded to |
---|
631 | * ignore BLANKs and MW channels. This saves on memory usage -- necessary |
---|
632 | * for dealing with very big files. |
---|
633 | */ |
---|
634 | |
---|
635 | if(!this->par.getFlagFDR() && this->par.getFlagUserThreshold() ){ |
---|
636 | // if the user has defined a threshold, set this in the StatsContainer |
---|
637 | this->Stats.setThreshold( this->par.getThreshold() ); |
---|
638 | } |
---|
639 | else{ |
---|
640 | // only work out the stats if we need to. |
---|
641 | // the only reason we don't is if the user has specified a threshold. |
---|
642 | |
---|
643 | if(this->par.isVerbose()) |
---|
644 | std::cout << "Calculating the cube statistics... " << std::flush; |
---|
645 | |
---|
646 | long xysize = this->axisDim[0]*this->axisDim[1]; |
---|
647 | |
---|
648 | // get number of good pixels; |
---|
649 | int vox,goodSize = 0; |
---|
650 | for(int p=0;p<xysize;p++){ |
---|
651 | for(int z=0;z<this->axisDim[2];z++){ |
---|
652 | vox = z*xysize+p; |
---|
653 | if(!this->isBlank(vox) && !this->par.isInMW(z)) goodSize++; |
---|
654 | } |
---|
655 | } |
---|
656 | |
---|
657 | float *tempArray = new float[goodSize]; |
---|
658 | |
---|
659 | goodSize=0; |
---|
660 | for(int x=0;x<this->axisDim[0];x++){ |
---|
661 | for(int y=0;y<this->axisDim[1];y++){ |
---|
662 | for(int z=0;z<this->axisDim[2];z++){ |
---|
663 | vox = z * xysize + y*this->axisDim[0] + x; |
---|
664 | if(!this->isBlank(vox) && !this->par.isInMW(z) && |
---|
665 | this->par.isStatOK(x,y,z)){ |
---|
666 | tempArray[goodSize] = this->array[vox]; |
---|
667 | goodSize++; |
---|
668 | } |
---|
669 | } |
---|
670 | } |
---|
671 | } |
---|
672 | |
---|
673 | float mean,median,stddev,madfm; |
---|
674 | mean = tempArray[0]; |
---|
675 | for(int i=1;i<goodSize;i++) mean += tempArray[i]; |
---|
676 | mean /= float(goodSize); |
---|
677 | mean = findMean(tempArray,goodSize); |
---|
678 | this->Stats.setMean(mean); |
---|
679 | |
---|
680 | std::sort(tempArray,tempArray+goodSize); |
---|
681 | if((goodSize%2)==0) |
---|
682 | median = (tempArray[goodSize/2-1] + tempArray[goodSize/2])/2; |
---|
683 | else median = tempArray[goodSize/2]; |
---|
684 | this->Stats.setMedian(median); |
---|
685 | |
---|
686 | |
---|
687 | if(!this->reconExists){ |
---|
688 | // if there's no recon array, calculate everything from orig array |
---|
689 | stddev = (tempArray[0]-mean) * (tempArray[0]-mean); |
---|
690 | for(int i=1;i<goodSize;i++) |
---|
691 | stddev += (tempArray[i]-mean)*(tempArray[i]-mean); |
---|
692 | stddev = sqrt(stddev/float(goodSize-1)); |
---|
693 | this->Stats.setStddev(stddev); |
---|
694 | |
---|
695 | for(int i=0;i<goodSize;i++)// tempArray[i] = absval(tempArray[i]-median); |
---|
696 | { |
---|
697 | if(tempArray[i]>median) tempArray[i] -= median; |
---|
698 | else tempArray[i] = median - tempArray[i]; |
---|
699 | } |
---|
700 | std::sort(tempArray,tempArray+goodSize); |
---|
701 | if((goodSize%2)==0) |
---|
702 | madfm = (tempArray[goodSize/2-1]+tempArray[goodSize/2])/2; |
---|
703 | else madfm = tempArray[goodSize/2]; |
---|
704 | this->Stats.setMadfm(madfm); |
---|
705 | } |
---|
706 | else{ |
---|
707 | // just get mean & median from orig array, and rms & madfm from residual |
---|
708 | // recompute array values to be residuals & then find stddev & madfm |
---|
709 | |
---|
710 | goodSize = 0; |
---|
711 | for(int p=0;p<xysize;p++){ |
---|
712 | for(int z=0;z<this->axisDim[2];z++){ |
---|
713 | vox = z * xysize + p; |
---|
714 | if(!this->isBlank(vox) && !this->par.isInMW(z)){ |
---|
715 | tempArray[goodSize] = this->array[vox] - this->recon[vox]; |
---|
716 | goodSize++; |
---|
717 | } |
---|
718 | } |
---|
719 | } |
---|
720 | mean = tempArray[0]; |
---|
721 | for(int i=1;i<goodSize;i++) mean += tempArray[i]; |
---|
722 | mean /= float(goodSize); |
---|
723 | stddev = (tempArray[0]-mean) * (tempArray[0]-mean); |
---|
724 | for(int i=1;i<goodSize;i++) |
---|
725 | stddev += (tempArray[i]-mean)*(tempArray[i]-mean); |
---|
726 | stddev = sqrt(stddev/float(goodSize-1)); |
---|
727 | this->Stats.setStddev(stddev); |
---|
728 | |
---|
729 | std::sort(tempArray,tempArray+goodSize); |
---|
730 | if((goodSize%2)==0) |
---|
731 | median = (tempArray[goodSize/2-1] + tempArray[goodSize/2])/2; |
---|
732 | else median = tempArray[goodSize/2]; |
---|
733 | for(int i=0;i<goodSize;i++){ |
---|
734 | if(tempArray[i]>median) tempArray[i] = tempArray[i]-median; |
---|
735 | else tempArray[i] = median - tempArray[i]; |
---|
736 | } |
---|
737 | std::sort(tempArray,tempArray+goodSize); |
---|
738 | if((goodSize%2)==0) |
---|
739 | madfm = (tempArray[goodSize/2-1] + tempArray[goodSize/2])/2; |
---|
740 | else madfm = tempArray[goodSize/2]; |
---|
741 | this->Stats.setMadfm(madfm); |
---|
742 | } |
---|
743 | |
---|
744 | delete [] tempArray; |
---|
745 | |
---|
746 | this->Stats.setUseFDR( this->par.getFlagFDR() ); |
---|
747 | // If the FDR method has been requested |
---|
748 | if(this->par.getFlagFDR()) this->setupFDR(); |
---|
749 | else{ |
---|
750 | // otherwise, calculate threshold based on the requested SNR cut level, |
---|
751 | // and then set the threshold parameter in the Par set. |
---|
752 | this->Stats.setThresholdSNR( this->par.getCut() ); |
---|
753 | this->par.setThreshold( this->Stats.getThreshold() ); |
---|
754 | } |
---|
755 | |
---|
756 | } |
---|
757 | |
---|
758 | if(this->par.isVerbose()){ |
---|
759 | std::cout << "Using "; |
---|
760 | if(this->par.getFlagFDR()) std::cout << "effective "; |
---|
761 | std::cout << "flux threshold of: "; |
---|
762 | float thresh = this->Stats.getThreshold(); |
---|
763 | if(this->par.getFlagNegative()) thresh *= -1.; |
---|
764 | std::cout << thresh << std::endl; |
---|
765 | } |
---|
766 | |
---|
767 | } |
---|
768 | //-------------------------------------------------------------------- |
---|
769 | |
---|
770 | void Cube::setupFDR() |
---|
771 | { |
---|
772 | /** |
---|
773 | * Determines the critical Probability value for the False |
---|
774 | * Discovery Rate detection routine. All pixels with Prob less |
---|
775 | * than this value will be considered detections. |
---|
776 | * |
---|
777 | * The Prob here is the probability, assuming a Normal |
---|
778 | * distribution, of obtaining a value as high or higher than the |
---|
779 | * pixel value (ie. only the positive tail of the PDF). |
---|
780 | * |
---|
781 | * The probabilities are calculated using the |
---|
782 | * StatsContainer::getPValue(), which calculates the z-statistic, |
---|
783 | * and then the probability via |
---|
784 | * \f$0.5\operatorname{erfc}(z/\sqrt{2})\f$ -- giving the positive |
---|
785 | * tail probability. |
---|
786 | */ |
---|
787 | |
---|
788 | // first calculate p-value for each pixel -- assume Gaussian for now. |
---|
789 | |
---|
790 | float *orderedP = new float[this->numPixels]; |
---|
791 | int count = 0; |
---|
792 | for(int x=0;x<this->axisDim[0];x++){ |
---|
793 | for(int y=0;y<this->axisDim[1];y++){ |
---|
794 | for(int z=0;z<this->axisDim[2];z++){ |
---|
795 | int pix = z * this->axisDim[0]*this->axisDim[1] + |
---|
796 | y*this->axisDim[0] + x; |
---|
797 | |
---|
798 | if(!(this->par.isBlank(this->array[pix])) && !this->par.isInMW(z)){ |
---|
799 | // only look at non-blank, valid pixels |
---|
800 | orderedP[count++] = this->Stats.getPValue(this->array[pix]); |
---|
801 | } |
---|
802 | } |
---|
803 | } |
---|
804 | } |
---|
805 | |
---|
806 | // now order them |
---|
807 | std::stable_sort(orderedP,orderedP+count); |
---|
808 | |
---|
809 | // now find the maximum P value. |
---|
810 | int max = 0; |
---|
811 | float cN = 0.; |
---|
812 | int numVox = int(ceil(this->par.getBeamSize())) * 2; |
---|
813 | // why beamSize*2? we are doing this in 3D, so spectrally assume just the |
---|
814 | // neighbouring channels are correlated, but spatially all those within |
---|
815 | // the beam, so total number of voxels is 2*beamSize |
---|
816 | for(int psfCtr=1;psfCtr<=numVox;psfCtr++) cN += 1./float(psfCtr); |
---|
817 | |
---|
818 | double slope = this->par.getAlpha()/cN; |
---|
819 | for(int loopCtr=0;loopCtr<count;loopCtr++) { |
---|
820 | if( orderedP[loopCtr] < (slope * double(loopCtr+1)/ double(count)) ){ |
---|
821 | max = loopCtr; |
---|
822 | } |
---|
823 | } |
---|
824 | |
---|
825 | this->Stats.setPThreshold( orderedP[max] ); |
---|
826 | |
---|
827 | |
---|
828 | // Find real value of the P threshold by finding the inverse of the |
---|
829 | // error function -- root finding with brute force technique |
---|
830 | // (relatively slow, but we only do it once). |
---|
831 | double zStat = 0.; |
---|
832 | double deltaZ = 0.1; |
---|
833 | double tolerance = 1.e-6; |
---|
834 | double initial = 0.5 * erfc(zStat/M_SQRT2) - this->Stats.getPThreshold(); |
---|
835 | do{ |
---|
836 | zStat+=deltaZ; |
---|
837 | double current = 0.5 * erfc(zStat/M_SQRT2) - this->Stats.getPThreshold(); |
---|
838 | if((initial*current)<0.){ |
---|
839 | zStat-=deltaZ; |
---|
840 | deltaZ/=2.; |
---|
841 | } |
---|
842 | }while(deltaZ>tolerance); |
---|
843 | this->Stats.setThreshold( zStat*this->Stats.getSpread() + |
---|
844 | this->Stats.getMiddle() ); |
---|
845 | |
---|
846 | /////////////////////////// |
---|
847 | if(TESTING){ |
---|
848 | std::stringstream ss; |
---|
849 | float *xplot = new float[2*max]; |
---|
850 | for(int i=0;i<2*max;i++) xplot[i]=float(i)/float(count); |
---|
851 | cpgopen("latestFDR.ps/vcps"); |
---|
852 | cpgpap(8.,1.); |
---|
853 | cpgslw(3); |
---|
854 | cpgenv(0,float(2*max)/float(count),0,orderedP[2*max],0,0); |
---|
855 | cpglab("i/N (index)", "p-value",""); |
---|
856 | cpgpt(2*max,xplot,orderedP,DOT); |
---|
857 | |
---|
858 | ss.str(""); |
---|
859 | ss << "\\gm = " << this->Stats.getMiddle(); |
---|
860 | cpgtext(max/(4.*count),0.9*orderedP[2*max],ss.str().c_str()); |
---|
861 | ss.str(""); |
---|
862 | ss << "\\gs = " << this->Stats.getSpread(); |
---|
863 | cpgtext(max/(4.*count),0.85*orderedP[2*max],ss.str().c_str()); |
---|
864 | ss.str(""); |
---|
865 | ss << "Slope = " << slope; |
---|
866 | cpgtext(max/(4.*count),0.8*orderedP[2*max],ss.str().c_str()); |
---|
867 | ss.str(""); |
---|
868 | ss << "Alpha = " << this->par.getAlpha(); |
---|
869 | cpgtext(max/(4.*count),0.75*orderedP[2*max],ss.str().c_str()); |
---|
870 | ss.str(""); |
---|
871 | ss << "c\\dN\\u = " << cN; |
---|
872 | cpgtext(max/(4.*count),0.7*orderedP[2*max],ss.str().c_str()); |
---|
873 | ss.str(""); |
---|
874 | ss << "max = "<<max << " (out of " << count << ")"; |
---|
875 | cpgtext(max/(4.*count),0.65*orderedP[2*max],ss.str().c_str()); |
---|
876 | ss.str(""); |
---|
877 | ss << "Threshold = "<<zStat*this->Stats.getSpread()+this->Stats.getMiddle(); |
---|
878 | cpgtext(max/(4.*count),0.6*orderedP[2*max],ss.str().c_str()); |
---|
879 | |
---|
880 | cpgslw(1); |
---|
881 | cpgsci(RED); |
---|
882 | cpgmove(0,0); |
---|
883 | cpgdraw(1,slope); |
---|
884 | cpgsci(BLUE); |
---|
885 | cpgsls(DOTTED); |
---|
886 | cpgmove(0,orderedP[max]); |
---|
887 | cpgdraw(2*max/float(count),orderedP[max]); |
---|
888 | cpgmove(max/float(count),0); |
---|
889 | cpgdraw(max/float(count),orderedP[2*max]); |
---|
890 | cpgsci(GREEN); |
---|
891 | cpgsls(SOLID); |
---|
892 | for(int i=1;i<=10;i++) { |
---|
893 | ss.str(""); |
---|
894 | ss << float(i)/2. << "\\gs"; |
---|
895 | float prob = 0.5*erfc((float(i)/2.)/M_SQRT2); |
---|
896 | cpgtick(0, 0, 0, orderedP[2*max], |
---|
897 | prob/orderedP[2*max], |
---|
898 | 0, 1, 1.5, 90., ss.str().c_str()); |
---|
899 | } |
---|
900 | cpgend(); |
---|
901 | delete [] xplot; |
---|
902 | } |
---|
903 | delete [] orderedP; |
---|
904 | |
---|
905 | } |
---|
906 | //-------------------------------------------------------------------- |
---|
907 | |
---|
908 | bool Cube::isDetection(long x, long y, long z) |
---|
909 | { |
---|
910 | /** |
---|
911 | * Is a given voxel at position (x,y,z) a detection, based on the statistics |
---|
912 | * in the Cube's StatsContainer? |
---|
913 | * If the pixel lies outside the valid range for the data array, |
---|
914 | * return false. |
---|
915 | * \param x X-value of the Cube's voxel to be tested. |
---|
916 | * \param y Y-value of the Cube's voxel to be tested. |
---|
917 | * \param z Z-value of the Cube's voxel to be tested. |
---|
918 | */ |
---|
919 | long voxel = z*axisDim[0]*axisDim[1] + y*axisDim[0] + x; |
---|
920 | return DataArray::isDetection(array[voxel]); |
---|
921 | }; |
---|
922 | //-------------------------------------------------------------------- |
---|
923 | |
---|
924 | void Cube::calcObjectWCSparams() |
---|
925 | { |
---|
926 | /** |
---|
927 | * A function that calculates the WCS parameters for each object in the |
---|
928 | * Cube's list of detections. |
---|
929 | * Each object gets an ID number assigned to it (which is simply its order |
---|
930 | * in the list), and if the WCS is good, the WCS paramters are calculated. |
---|
931 | */ |
---|
932 | |
---|
933 | for(int i=0;i<this->objectList.size();i++){ |
---|
934 | this->objectList[i].setID(i+1); |
---|
935 | this->objectList[i].setCentreType(this->par.getPixelCentre()); |
---|
936 | this->objectList[i].calcFluxes(this->array,this->axisDim); |
---|
937 | this->objectList[i].calcWCSparams(this->array,this->axisDim,this->head); |
---|
938 | |
---|
939 | if(this->par.getFlagUserThreshold()) |
---|
940 | this->objectList[i].setPeakSNR( this->objectList[i].getPeakFlux() / this->Stats.getThreshold() ); |
---|
941 | else |
---|
942 | this->objectList[i].setPeakSNR( (this->objectList[i].getPeakFlux() - this->Stats.getMiddle()) / this->Stats.getSpread() ); |
---|
943 | |
---|
944 | } |
---|
945 | |
---|
946 | if(!this->head.isWCS()){ |
---|
947 | // if the WCS is bad, set the object names to Obj01 etc |
---|
948 | int numspaces = int(log10(this->objectList.size())) + 1; |
---|
949 | std::stringstream ss; |
---|
950 | for(int i=0;i<this->objectList.size();i++){ |
---|
951 | ss.str(""); |
---|
952 | ss << "Obj" << std::setfill('0') << std::setw(numspaces) << i+1; |
---|
953 | this->objectList[i].setName(ss.str()); |
---|
954 | } |
---|
955 | } |
---|
956 | |
---|
957 | } |
---|
958 | //-------------------------------------------------------------------- |
---|
959 | |
---|
960 | void Cube::updateDetectMap() |
---|
961 | { |
---|
962 | /** |
---|
963 | * A function that, for each detected object in the cube's list, increments |
---|
964 | * the cube's detection map by the required amount at each pixel. |
---|
965 | */ |
---|
966 | |
---|
967 | Scan temp; |
---|
968 | for(int obj=0;obj<this->objectList.size();obj++){ |
---|
969 | long numZ=this->objectList[obj].pixels().getNumChanMap(); |
---|
970 | for(int iz=0;iz<numZ;iz++){ // for each channel map |
---|
971 | Object2D chanmap = this->objectList[obj].pixels().getChanMap(iz).getObject(); |
---|
972 | for(int iscan=0;iscan<chanmap.getNumScan();iscan++){ |
---|
973 | temp = chanmap.getScan(iscan); |
---|
974 | for(int x=temp.getX(); x <= temp.getXmax(); x++) |
---|
975 | this->detectMap[temp.getY()*this->axisDim[0] + x]++; |
---|
976 | } // end of loop over scans |
---|
977 | } // end of loop over channel maps |
---|
978 | } // end of loop over objects. |
---|
979 | |
---|
980 | } |
---|
981 | //-------------------------------------------------------------------- |
---|
982 | |
---|
983 | void Cube::updateDetectMap(Detection obj) |
---|
984 | { |
---|
985 | /** |
---|
986 | * A function that, for the given object, increments the cube's |
---|
987 | * detection map by the required amount at each pixel. |
---|
988 | * |
---|
989 | * \param obj A Detection object that is being incorporated into the map. |
---|
990 | */ |
---|
991 | |
---|
992 | Scan temp; |
---|
993 | long numZ=obj.pixels().getNumChanMap(); |
---|
994 | for(int iz=0;iz<numZ;iz++){ // for each channel map |
---|
995 | Object2D chanmap = obj.pixels().getChanMap(iz).getObject(); |
---|
996 | for(int iscan=0;iscan<chanmap.getNumScan();iscan++){ |
---|
997 | temp = chanmap.getScan(iscan); |
---|
998 | for(int x=temp.getX(); x <= temp.getXmax(); x++) |
---|
999 | this->detectMap[temp.getY()*this->axisDim[0] + x]++; |
---|
1000 | } // end of loop over scans |
---|
1001 | } // end of loop over channel maps |
---|
1002 | |
---|
1003 | } |
---|
1004 | //-------------------------------------------------------------------- |
---|
1005 | |
---|
1006 | float Cube::enclosedFlux(Detection obj) |
---|
1007 | { |
---|
1008 | /** |
---|
1009 | * A function to calculate the flux enclosed by the range |
---|
1010 | * of pixels detected in the object obj (not necessarily all |
---|
1011 | * pixels will have been detected). |
---|
1012 | * |
---|
1013 | * \param obj The Detection under consideration. |
---|
1014 | */ |
---|
1015 | obj.calcFluxes(this->array, this->axisDim); |
---|
1016 | int xsize = obj.getXmax()-obj.getXmin()+1; |
---|
1017 | int ysize = obj.getYmax()-obj.getYmin()+1; |
---|
1018 | int zsize = obj.getZmax()-obj.getZmin()+1; |
---|
1019 | std::vector <float> fluxArray(xsize*ysize*zsize,0.); |
---|
1020 | for(int x=0;x<xsize;x++){ |
---|
1021 | for(int y=0;y<ysize;y++){ |
---|
1022 | for(int z=0;z<zsize;z++){ |
---|
1023 | fluxArray[x+y*xsize+z*ysize*xsize] = |
---|
1024 | this->getPixValue(x+obj.getXmin(), |
---|
1025 | y+obj.getYmin(), |
---|
1026 | z+obj.getZmin()); |
---|
1027 | if(this->par.getFlagNegative()) |
---|
1028 | fluxArray[x+y*xsize+z*ysize*xsize] *= -1.; |
---|
1029 | } |
---|
1030 | } |
---|
1031 | } |
---|
1032 | float sum = 0.; |
---|
1033 | for(int i=0;i<fluxArray.size();i++) |
---|
1034 | if(!this->par.isBlank(fluxArray[i])) sum+=fluxArray[i]; |
---|
1035 | return sum; |
---|
1036 | } |
---|
1037 | //-------------------------------------------------------------------- |
---|
1038 | |
---|
1039 | void Cube::setupColumns() |
---|
1040 | { |
---|
1041 | /** |
---|
1042 | * A front-end to the two setup routines in columns.cc. |
---|
1043 | * This first calculates the WCS parameters for all objects, then |
---|
1044 | * sets up the columns (calculates their widths and precisions and so on). |
---|
1045 | * The precisions are also stored in each Detection object. |
---|
1046 | */ |
---|
1047 | this->calcObjectWCSparams(); |
---|
1048 | // need this as the colSet functions use vel, RA, Dec, etc... |
---|
1049 | |
---|
1050 | this->fullCols.clear(); |
---|
1051 | this->fullCols = getFullColSet(this->objectList, this->head); |
---|
1052 | |
---|
1053 | this->logCols.clear(); |
---|
1054 | this->logCols = getLogColSet(this->objectList, this->head); |
---|
1055 | |
---|
1056 | int vel,fpeak,fint,pos,xyz,snr; |
---|
1057 | vel = fullCols[VEL].getPrecision(); |
---|
1058 | fpeak = fullCols[FPEAK].getPrecision(); |
---|
1059 | snr = fullCols[SNRPEAK].getPrecision(); |
---|
1060 | xyz = fullCols[X].getPrecision(); |
---|
1061 | xyz = std::max(xyz, fullCols[Y].getPrecision()); |
---|
1062 | xyz = std::max(xyz, fullCols[Z].getPrecision()); |
---|
1063 | if(this->head.isWCS()) fint = fullCols[FINT].getPrecision(); |
---|
1064 | else fint = fullCols[FTOT].getPrecision(); |
---|
1065 | pos = fullCols[WRA].getPrecision(); |
---|
1066 | pos = std::max(pos, fullCols[WDEC].getPrecision()); |
---|
1067 | |
---|
1068 | for(int obj=0;obj<this->objectList.size();obj++){ |
---|
1069 | this->objectList[obj].setVelPrec(vel); |
---|
1070 | this->objectList[obj].setFpeakPrec(fpeak); |
---|
1071 | this->objectList[obj].setXYZPrec(xyz); |
---|
1072 | this->objectList[obj].setPosPrec(pos); |
---|
1073 | this->objectList[obj].setFintPrec(fint); |
---|
1074 | this->objectList[obj].setSNRPrec(snr); |
---|
1075 | } |
---|
1076 | |
---|
1077 | } |
---|
1078 | //-------------------------------------------------------------------- |
---|
1079 | |
---|
1080 | bool Cube::objAtSpatialEdge(Detection obj) |
---|
1081 | { |
---|
1082 | /** |
---|
1083 | * A function to test whether the object obj |
---|
1084 | * lies at the edge of the cube's spatial field -- |
---|
1085 | * either at the boundary, or next to BLANKs. |
---|
1086 | * |
---|
1087 | * /param obj The Detection under consideration. |
---|
1088 | */ |
---|
1089 | |
---|
1090 | bool atEdge = false; |
---|
1091 | |
---|
1092 | int pix = 0; |
---|
1093 | std::vector<Voxel> voxlist = obj.pixels().getPixelSet(); |
---|
1094 | while(!atEdge && pix<voxlist.size()){ |
---|
1095 | // loop over each pixel in the object, until we find an edge pixel. |
---|
1096 | for(int dx=-1;dx<=1;dx+=2){ |
---|
1097 | if( ((voxlist[pix].getX()+dx)<0) || |
---|
1098 | ((voxlist[pix].getX()+dx)>=this->axisDim[0]) ) |
---|
1099 | atEdge = true; |
---|
1100 | else if(this->isBlank(voxlist[pix].getX()+dx, |
---|
1101 | voxlist[pix].getY(), |
---|
1102 | voxlist[pix].getZ())) |
---|
1103 | atEdge = true; |
---|
1104 | } |
---|
1105 | for(int dy=-1;dy<=1;dy+=2){ |
---|
1106 | if( ((voxlist[pix].getY()+dy)<0) || |
---|
1107 | ((voxlist[pix].getY()+dy)>=this->axisDim[1]) ) |
---|
1108 | atEdge = true; |
---|
1109 | else if(this->isBlank(voxlist[pix].getX(), |
---|
1110 | voxlist[pix].getY()+dy, |
---|
1111 | voxlist[pix].getZ())) |
---|
1112 | atEdge = true; |
---|
1113 | } |
---|
1114 | pix++; |
---|
1115 | } |
---|
1116 | |
---|
1117 | return atEdge; |
---|
1118 | } |
---|
1119 | //-------------------------------------------------------------------- |
---|
1120 | |
---|
1121 | bool Cube::objAtSpectralEdge(Detection obj) |
---|
1122 | { |
---|
1123 | /** |
---|
1124 | * A function to test whether the object obj |
---|
1125 | * lies at the edge of the cube's spectral extent -- |
---|
1126 | * either at the boundary, or next to BLANKs. |
---|
1127 | * |
---|
1128 | * /param obj The Detection under consideration. |
---|
1129 | */ |
---|
1130 | |
---|
1131 | bool atEdge = false; |
---|
1132 | |
---|
1133 | int pix = 0; |
---|
1134 | std::vector<Voxel> voxlist = obj.pixels().getPixelSet(); |
---|
1135 | while(!atEdge && pix<voxlist.size()){ |
---|
1136 | // loop over each pixel in the object, until we find an edge pixel. |
---|
1137 | for(int dz=-1;dz<=1;dz+=2){ |
---|
1138 | if( ((voxlist[pix].getZ()+dz)<0) || |
---|
1139 | ((voxlist[pix].getZ()+dz)>=this->axisDim[2])) |
---|
1140 | atEdge = true; |
---|
1141 | else if(this->isBlank(voxlist[pix].getX(), |
---|
1142 | voxlist[pix].getY(), |
---|
1143 | voxlist[pix].getZ()+dz)) |
---|
1144 | atEdge = true; |
---|
1145 | } |
---|
1146 | pix++; |
---|
1147 | } |
---|
1148 | |
---|
1149 | return atEdge; |
---|
1150 | } |
---|
1151 | //-------------------------------------------------------------------- |
---|
1152 | |
---|
1153 | void Cube::setObjectFlags() |
---|
1154 | { |
---|
1155 | /** |
---|
1156 | * A function to set any warning flags for all the detected objects |
---|
1157 | * associated with the cube. |
---|
1158 | * Flags to be looked for: |
---|
1159 | * <ul><li> Negative enclosed flux (N) |
---|
1160 | * <li> Detection at edge of field (spatially) (E) |
---|
1161 | * <li> Detection at edge of spectral region (S) |
---|
1162 | * </ul> |
---|
1163 | */ |
---|
1164 | |
---|
1165 | for(int i=0;i<this->objectList.size();i++){ |
---|
1166 | |
---|
1167 | if( this->enclosedFlux(this->objectList[i]) < 0. ) |
---|
1168 | this->objectList[i].addToFlagText("N"); |
---|
1169 | |
---|
1170 | if( this->objAtSpatialEdge(this->objectList[i]) ) |
---|
1171 | this->objectList[i].addToFlagText("E"); |
---|
1172 | |
---|
1173 | if( this->objAtSpectralEdge(this->objectList[i]) && |
---|
1174 | (this->axisDim[2] > 2)) |
---|
1175 | this->objectList[i].addToFlagText("S"); |
---|
1176 | |
---|
1177 | } |
---|
1178 | |
---|
1179 | } |
---|
1180 | //-------------------------------------------------------------------- |
---|
1181 | |
---|
1182 | |
---|
1183 | |
---|
1184 | /****************************************************************/ |
---|
1185 | ///////////////////////////////////////////////////////////// |
---|
1186 | //// Functions for Image class |
---|
1187 | ///////////////////////////////////////////////////////////// |
---|
1188 | |
---|
1189 | Image::Image(long size){ |
---|
1190 | // need error handling in case size<0 !!! |
---|
1191 | this->numPixels = this->numDim = 0; |
---|
1192 | if(size<0) |
---|
1193 | duchampError("Image(size)","Negative size -- could not define Image"); |
---|
1194 | else{ |
---|
1195 | if(size>0) this->array = new float[size]; |
---|
1196 | this->numPixels = size; |
---|
1197 | this->axisDim = new long[2]; |
---|
1198 | this->numDim = 2; |
---|
1199 | } |
---|
1200 | } |
---|
1201 | //-------------------------------------------------------------------- |
---|
1202 | |
---|
1203 | Image::Image(long *dimensions){ |
---|
1204 | this->numPixels = this->numDim = 0; |
---|
1205 | int size = dimensions[0] * dimensions[1]; |
---|
1206 | if(size<0) |
---|
1207 | duchampError("Image(dimArray)","Negative size -- could not define Image"); |
---|
1208 | else{ |
---|
1209 | this->numPixels = size; |
---|
1210 | if(size>0) this->array = new float[size]; |
---|
1211 | this->numDim=2; |
---|
1212 | this->axisDim = new long[2]; |
---|
1213 | for(int i=0;i<2;i++) this->axisDim[i] = dimensions[i]; |
---|
1214 | } |
---|
1215 | } |
---|
1216 | //-------------------------------------------------------------------- |
---|
1217 | //-------------------------------------------------------------------- |
---|
1218 | |
---|
1219 | void Image::saveArray(float *input, long size) |
---|
1220 | { |
---|
1221 | /** |
---|
1222 | * Saves the array in input to the pixel array Image::array. |
---|
1223 | * The size of the array given must be the same as the current number of |
---|
1224 | * pixels, else an error message is returned and nothing is done. |
---|
1225 | * \param input The array of values to be saved. |
---|
1226 | * \param size The size of input. |
---|
1227 | */ |
---|
1228 | if(size != this->numPixels) |
---|
1229 | duchampError("Image::saveArray", |
---|
1230 | "Input array different size to existing array. Cannot save."); |
---|
1231 | else { |
---|
1232 | if(this->numPixels>0) delete [] array; |
---|
1233 | this->numPixels = size; |
---|
1234 | if(this->numPixels>0) this->array = new float[size]; |
---|
1235 | for(int i=0;i<size;i++) this->array[i] = input[i]; |
---|
1236 | } |
---|
1237 | } |
---|
1238 | //-------------------------------------------------------------------- |
---|
1239 | |
---|
1240 | void Image::extractSpectrum(float *Array, long *dim, long pixel) |
---|
1241 | { |
---|
1242 | /** |
---|
1243 | * A function to extract a 1-D spectrum from a 3-D array. |
---|
1244 | * The array is assumed to be 3-D with the third dimension the spectral one. |
---|
1245 | * The spectrum extracted is the one lying in the spatial pixel referenced |
---|
1246 | * by the third argument. |
---|
1247 | * The extracted spectrum is stored in the pixel array Image::array. |
---|
1248 | * \param Array The array containing the pixel values, from which |
---|
1249 | * the spectrum is extracted. |
---|
1250 | * \param dim The array of dimension values. |
---|
1251 | * \param pixel The spatial pixel that contains the desired spectrum. |
---|
1252 | */ |
---|
1253 | if((pixel<0)||(pixel>=dim[0]*dim[1])) |
---|
1254 | duchampError("Image::extractSpectrum", |
---|
1255 | "Requested spatial pixel outside allowed range. Cannot save."); |
---|
1256 | else if(dim[2] != this->numPixels) |
---|
1257 | duchampError("Image::extractSpectrum", |
---|
1258 | "Input array different size to existing array. Cannot save."); |
---|
1259 | else { |
---|
1260 | if(this->numPixels>0) delete [] array; |
---|
1261 | this->numPixels = dim[2]; |
---|
1262 | if(this->numPixels>0) this->array = new float[dim[2]]; |
---|
1263 | for(int z=0;z<dim[2];z++) this->array[z] = Array[z*dim[0]*dim[1] + pixel]; |
---|
1264 | } |
---|
1265 | } |
---|
1266 | //-------------------------------------------------------------------- |
---|
1267 | |
---|
1268 | void Image::extractSpectrum(Cube &cube, long pixel) |
---|
1269 | { |
---|
1270 | /** |
---|
1271 | * A function to extract a 1-D spectrum from a Cube class |
---|
1272 | * The spectrum extracted is the one lying in the spatial pixel referenced |
---|
1273 | * by the second argument. |
---|
1274 | * The extracted spectrum is stored in the pixel array Image::array. |
---|
1275 | * \param cube The Cube containing the pixel values, from which the spectrum is extracted. |
---|
1276 | * \param pixel The spatial pixel that contains the desired spectrum. |
---|
1277 | */ |
---|
1278 | long zdim = cube.getDimZ(); |
---|
1279 | long spatSize = cube.getDimX()*cube.getDimY(); |
---|
1280 | if((pixel<0)||(pixel>=spatSize)) |
---|
1281 | duchampError("Image::extractSpectrum", |
---|
1282 | "Requested spatial pixel outside allowed range. Cannot save."); |
---|
1283 | else if(zdim != this->numPixels) |
---|
1284 | duchampError("Image::extractSpectrum", |
---|
1285 | "Input array different size to existing array. Cannot save."); |
---|
1286 | else { |
---|
1287 | if(this->numPixels>0) delete [] array; |
---|
1288 | this->numPixels = zdim; |
---|
1289 | if(this->numPixels>0) this->array = new float[zdim]; |
---|
1290 | for(int z=0;z<zdim;z++) |
---|
1291 | this->array[z] = cube.getPixValue(z*spatSize + pixel); |
---|
1292 | } |
---|
1293 | } |
---|
1294 | //-------------------------------------------------------------------- |
---|
1295 | |
---|
1296 | void Image::extractImage(float *Array, long *dim, long channel) |
---|
1297 | { |
---|
1298 | /** |
---|
1299 | * A function to extract a 2-D image from a 3-D array. |
---|
1300 | * The array is assumed to be 3-D with the third dimension the spectral one. |
---|
1301 | * The dimensions of the array are in the dim[] array. |
---|
1302 | * The image extracted is the one lying in the channel referenced |
---|
1303 | * by the third argument. |
---|
1304 | * The extracted image is stored in the pixel array Image::array. |
---|
1305 | * \param Array The array containing the pixel values, from which the image is extracted. |
---|
1306 | * \param dim The array of dimension values. |
---|
1307 | * \param channel The spectral channel that contains the desired image. |
---|
1308 | */ |
---|
1309 | |
---|
1310 | long spatSize = dim[0]*dim[1]; |
---|
1311 | if((channel<0)||(channel>=dim[2])) |
---|
1312 | duchampError("Image::extractImage", |
---|
1313 | "Requested channel outside allowed range. Cannot save."); |
---|
1314 | else if(spatSize != this->numPixels) |
---|
1315 | duchampError("Image::extractImage", |
---|
1316 | "Input array different size to existing array. Cannot save."); |
---|
1317 | else { |
---|
1318 | if(this->numPixels>0) delete [] array; |
---|
1319 | this->numPixels = spatSize; |
---|
1320 | if(this->numPixels>0) this->array = new float[spatSize]; |
---|
1321 | for(int npix=0; npix<spatSize; npix++) |
---|
1322 | this->array[npix] = Array[channel*spatSize + npix]; |
---|
1323 | } |
---|
1324 | } |
---|
1325 | //-------------------------------------------------------------------- |
---|
1326 | |
---|
1327 | void Image::extractImage(Cube &cube, long channel) |
---|
1328 | { |
---|
1329 | /** |
---|
1330 | * A function to extract a 2-D image from Cube class. |
---|
1331 | * The image extracted is the one lying in the channel referenced |
---|
1332 | * by the second argument. |
---|
1333 | * The extracted image is stored in the pixel array Image::array. |
---|
1334 | * \param cube The Cube containing the pixel values, from which the image is extracted. |
---|
1335 | * \param channel The spectral channel that contains the desired image. |
---|
1336 | */ |
---|
1337 | long spatSize = cube.getDimX()*cube.getDimY(); |
---|
1338 | if((channel<0)||(channel>=cube.getDimZ())) |
---|
1339 | duchampError("Image::extractImage", |
---|
1340 | "Requested channel outside allowed range. Cannot save."); |
---|
1341 | else if(spatSize != this->numPixels) |
---|
1342 | duchampError("Image::extractImage", |
---|
1343 | "Input array different size to existing array. Cannot save."); |
---|
1344 | else { |
---|
1345 | if(this->numPixels>0) delete [] array; |
---|
1346 | this->numPixels = spatSize; |
---|
1347 | if(this->numPixels>0) this->array = new float[spatSize]; |
---|
1348 | for(int npix=0; npix<spatSize; npix++) |
---|
1349 | this->array[npix] = cube.getPixValue(channel*spatSize + npix); |
---|
1350 | } |
---|
1351 | } |
---|
1352 | //-------------------------------------------------------------------- |
---|
1353 | |
---|
1354 | void Image::removeMW() |
---|
1355 | { |
---|
1356 | /** |
---|
1357 | * A function to remove the Milky Way range of channels from a 1-D spectrum. |
---|
1358 | * The array in this Image is assumed to be 1-D, with only the first axisDim |
---|
1359 | * equal to 1. |
---|
1360 | * The values of the MW channels are set to 0, unless they are BLANK. |
---|
1361 | */ |
---|
1362 | if(this->par.getFlagMW() && (this->axisDim[1]==1) ){ |
---|
1363 | for(int z=0;z<this->axisDim[0];z++){ |
---|
1364 | if(!this->isBlank(z) && this->par.isInMW(z)) this->array[z]=0.; |
---|
1365 | } |
---|
1366 | } |
---|
1367 | } |
---|
1368 | |
---|