source: tags/release-1.0.2/src/Cubes/cubes.hh @ 1455

Last change on this file since 1455 was 117, checked in by Matthew Whiting, 18 years ago

Some bugfixes, and improved image/spectrum extraction routines:
Corrected bug that meant blank pixels weren't being seen by the
drawMomentMap function. Improved the blankpixel testing in that
function, and changed getImage so that the blank pixel info is
always stored (if it is in the fits header).
Added new functions to Image class that can read a spectrum or
channel map given a cube and a channel/pixel number.
Other minor corrections as well:
src/Utils/cpgcumul.c -- changed way _swap is declared (pointers
rather than references, which don't work in C)
src/Cubes/cubes.cc -- new extraction functions
src/Cubes/cubes.hh -- prototypes thereof
src/Cubes/drawMomentCutout.cc -- improved blank pixel handling
src/Cubes/getImage.cc -- made sure blank pixel info is always
read in.
src/param.cc -- tidied up code slightly.
src/Detection/thresholding_functions.cc -- corrected setupFDR
to remove potential for accessing outside allocated memory.

File size: 14.4 KB
Line 
1#ifndef CUBES_H
2#define CUBES_H
3
4#include <iostream>
5#include <string>
6#include <vector>
7
8#ifndef PARAM_H
9#include <param.hh>
10#endif
11#ifndef DETECTION_H
12#include <Detection/detection.hh>
13#endif
14#ifndef COLUMNS_H
15#include <Detection/columns.hh>
16#endif
17#ifndef PLOTS_H
18#include <Cubes/plots.hh>
19#endif
20
21using std::string;
22using std::vector;
23using namespace Column;
24
25/****************************************************************/
26/////////////////////////////////////////////////////////////
27//// Definition of an n-dimensional data array:
28////     array of pixel values, size & dimensions
29////     array of Detection objects
30/////////////////////////////////////////////////////////////
31
32
33class DataArray
34{
35public:
36  DataArray(){numDim=0; numPixels=0;};
37  DataArray(short int nDim){numDim=nDim; numPixels=0;};
38  DataArray(short int nDim, long size);
39  DataArray(short int nDim, long *dimensions);
40  virtual ~DataArray(){};
41  // Size and Dimension related
42  long                 getDimX(){if(numDim>0) return axisDim[0]; else return 0;};
43  long                 getDimY(){if(numDim>1) return axisDim[1]; else return 1;};
44  long                 getDimZ(){if(numDim>2) return axisDim[2]; else return 1;};
45  void                 getDim(long &x, long &y, long &z);
46  long                 getSize(){return numPixels;};
47  short int            getNumDim(){return numDim;};
48  // Related to the various arrays
49  void                 getDimArray(long *output);
50  void                 getArray(float *output);
51  virtual void         saveArray(float *input, long size);
52  float                getPixValue(long pos){return array[pos];};
53  void                 setPixValue(long pos, float f){array[pos] = f;};
54  // Related to the object lists
55  Detection            getObject(long number){return objectList[number];};
56  void                 addObject(Detection object);   // adds a single detection to the object list
57  vector <Detection>   getObjectList(){return objectList;};
58  void                 addObjectList(vector <Detection> newlist); 
59                                     // adds all objects in a detection list to the object list
60  long                 getNumObj(){return objectList.size();};
61  void                 clearDetectionList(){this->objectList.clear();};
62  // Parameter list related.
63  void                 readParam(string paramfile){par.readParams(paramfile);};
64  void                 showParam(std::ostream &stream){stream << par;};
65  Param                getParam(){return par;};
66  void                 saveParam(Param newpar){par = newpar;};
67  Param&               pars(){Param &rpar = par; return rpar;};
68  bool                 isBlank(int vox){return par.isBlank(array[vox]);};
69
70  friend std::ostream& operator<< ( std::ostream& theStream, DataArray &array);
71
72
73protected:
74  short int            numDim;     // number of dimensions.
75  long                *axisDim;    // array of dimensions of cube
76                                   //     (ie. how large in each direction).
77  long                 numPixels;  // total number of pixels in cube
78  float               *array;      // array of data
79  vector <Detection>   objectList; // the list of detected objects in the image
80  Param                par;        // a parameter list.
81};
82
83
84/****************************************************************/
85/////////////////////////////////////////////////////////////
86//// Definition of an image object (2D):
87////     a DataArray object
88////     arrays for: probability values (for FDR)
89////                 mask image to indicate location of objects
90////                 detected objects
91////     statistics information
92/////////////////////////////////////////////////////////////
93
94class Cube;
95class Image : public DataArray
96{
97public:
98  Image(){
99    numPixels=0;
100    numDim=2;};
101  Image(long nPix);
102  Image(long *dimensions);
103  virtual ~Image(){};
104
105  // Defining the array
106  void      saveArray(float *input, long size);
107  void      extractSpectrum(float *Array, long *dim, long pixel);
108  void      extractImage(float *Array, long *dim, long channel);
109  void      extractSpectrum(Cube &cube, long pixel);
110  void      extractImage(Cube &cube, long channel);
111  // Accessing the data.
112  float     getPixValue(long x, long y){return array[y*axisDim[0] + x];};
113  float     getPixValue(long pos){return array[pos];};
114  float     getPValue(long pos){return pValue[pos];};
115  float     getPValue(long x, long y){return pValue[y*axisDim[0] + x];};
116  short int getMaskValue(long pos){return mask[pos];};
117  short int getMaskValue(long x, long y){return mask[y*axisDim[0] + x];};
118  // the next few should have checks against array overflow...
119  void      setPixValue(long x, long y, float f){array[y*axisDim[0] + x] = f;};
120  void      setPixValue(long pos, float f){array[pos] = f;};
121  void      setPValue(long pos, float p){pValue[pos] = p;};
122  void      setPValue(long x, long y, float p){pValue[y*axisDim[0] + x] = p;};
123  void      setMaskValue(long pos, short int m){mask[pos] = m;};
124  void      setMaskValue(long x, long y, short int m){mask[y*axisDim[0] + x] = m;};
125  bool      isBlank(int vox){return par.isBlank(array[vox]);};
126  bool      isBlank(long x, long y){return par.isBlank(array[y*axisDim[0] + x]);};
127  // Stats-related
128  void      setStats(float m, float s, float c){mean=m; sigma=s; cutLevel=c;};
129  void      findStats(int code);
130  float     getMean(){return mean;};
131  void      setMean(float m){mean=m;};
132  float     getSigma(){return sigma;};
133  void      setSigma(float s){sigma=s;};
134  float     getCut(){return cutLevel;};
135  void      setCut(float c){cutLevel=c;};
136  float     getPCut(){return pCutLevel;};
137  void      setPCut(float p){pCutLevel = p;};
138  float     getAlpha(){return alpha;};
139  void      setAlpha(float a){alpha = a;};
140  int       getMinSize(){return minSize;};
141  void      setMinSize(int s){minSize = s;};
142
143  void      maskObject(Detection &object);
144
145  // Detection-related
146  void      lutz_detect();                  // in Detection/lutz_detect.cc
147  void      spectrumDetect();               // in Detection/spectrumDetect.cc
148  int       setupFDR();                     // in Detection/thresholding_functions.cc
149  bool      isDetection(float value);       // in Detection/thresholding_functions.cc
150  bool      isDetection(long x, long y);    // in Detection/thresholding_functions.cc
151  bool      isDetectionFDR(float pvalue);   // in Detection/thresholding_functions.cc
152
153  void      removeMW();
154 
155private:
156  float     *pValue;     // the array of p-values for each pixel
157                         //                  --> used by FDR method
158  short int *mask;       // a mask image indicating where objects are
159                         
160  float      mean;       // the mean background level of the image
161  float      sigma;      // the standard deviation of the background in the image
162  float      cutLevel;   // the limiting value (in sigmas above the mean) for
163                         //     a pixel to be called a detection.
164  float      alpha;      // used by FDR routine -- significance level
165  float      pCutLevel;  // the limiting P-value for the FDR analysis
166  int        minSize;    // the minimum number of pixels for a detection to be accepted.
167};
168
169/****************************************************************/
170/////////////////////////////////////////////////////////////
171//// Definition of an data-cube object (3D):
172////     a DataArray object limited to dim=3
173/////////////////////////////////////////////////////////////
174
175class Cube : public DataArray
176{
177public:
178  Cube(){numPixels=0; numDim=3; // flagWCS=false;
179  };
180  Cube(long nPix);
181  Cube(long *dimensions);
182  virtual ~Cube(){};            // destructor
183
184  // additional accessor functions -- in Cubes/cubes.cc unless otherwise specified.
185
186  int     getCube(string fname);
187  int     getCube(){ 
188    // a front-end to the getCube function, that does subsection checks
189    // assumes the Param is setup properly
190    string fname = this->par.getImageFile();
191    if(this->par.getFlagSubsection()){
192      this->par.parseSubsection();
193      fname+=this->par.getSubsection();
194    }
195    return this->getCube(fname);
196  };
197  void    initialiseCube(long *dimensions);
198  void    saveReconstructedCube();
199  int     readReconCube();
200
201  bool    isBlank(int vox){return par.isBlank(array[vox]);};
202  bool    isBlank(long x, long y, long z){
203    return par.isBlank(array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x]);};
204  float   getPixValue(long pos){return array[pos];};
205  float   getPixValue(long x, long y, long z){
206    return array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x];};
207  short   getDetectMapValue(long pos){return detectMap[pos];};
208  short   getDetectMapValue(long x, long y){return detectMap[y*axisDim[0] + x];};
209  bool    isRecon(){return reconExists;};
210  float   getReconValue(long pos){return recon[pos];};
211  float   getReconValue(long x, long y, long z){
212    return recon[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x];};
213  float   getBaselineValue(long pos){return baseline[pos];};
214  float   getBaselineValue(long x, long y, long z){
215    return baseline[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x];};
216  // these should have checks against array overflow...
217  void    setPixValue(long pos, float f){array[pos] = f;};
218  void    setPixValue(long x, long y, long z, float f){
219    array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x] = f;};
220  void    setDetectMapValue(long pos, short f){detectMap[pos] = f;};
221  void    setDetectMapValue(long x, long y, short f){
222    detectMap[y*axisDim[0] + x] = f;};
223  void    setReconValue(long pos, float f){recon[pos] = f;};
224  void    setReconValue(long x, long y, long z, float f){
225    recon[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x] = f;};
226  void    setReconFlag(bool f){reconExists = f;};
227  void    saveArray(float *input, long size);
228  void    saveRecon(float *input, long size);
229  void    getRecon(float *output);
230
231  // Statistics for cube
232  float   getSpecMean(int pixel){return specMean[pixel];};
233  float   getSpecSigma(int pixel){return specSigma[pixel];};
234  float   getChanMean(int channel){return chanMean[channel];};
235  float   getChanSigma(int channel){return chanSigma[channel];};
236  void    setCubeStats();    // in Cubes/cubes.cc
237
238  // Functions that act on the cube
239  void    removeMW();        // in Cubes/cubes.cc
240  void    trimCube();        // in Cubes/trimImage.cc
241  void    unTrimCube();      // in Cubes/trimImage.cc
242  void    removeBaseline();  // in ATrous/baselineSubtract.cc
243  void    replaceBaseline(); // in ATrous/baselineSubtract.cc
244  void    invert();          // in Cubes/invertCube.cc
245  void    reInvert();        // in Cubes/invertCube.cc
246
247  // Reconstruction and Searching functions
248  void    ReconSearch();     // in ATrous/ReconSearch.cc
249  void    ReconSearch1D();   // in ATrous/ReconSearch.cc
250  void    ReconSearch2D();   // in ATrous/ReconSearch.cc
251  void    ReconSearch3D();   // in ATrous/ReconSearch.cc
252  void    CubicSearch();     // in Cubes/CubicSearch.cc
253
254  // Dealing with the WCS
255  FitsHeader getHead(){return head;};
256  void       setHead(FitsHeader F){head=F;};
257  FitsHeader& header(){FitsHeader &h = head; return h;};
258  int        wcsToPix(const double *world, double *pix){
259    return this->head.wcsToPix(world,pix);}
260  int        wcsToPix(const double *world, double *pix, const int npts){
261    return this->head.wcsToPix(world,pix,npts);}
262  int        pixToWCS(const double *pix, double *world){
263    return this->head.pixToWCS(pix,world);}
264  int        pixToWCS(const double *pix, double *world, const int npts){
265    return this->head.pixToWCS(pix,world,npts);}
266 
267  // Dealing with the detections
268  void    ObjectMerger();          // in Cubes/Merger.cc
269  void    calcObjectWCSparams();
270  void    sortDetections();
271  void    updateDetectMap();
272  void    updateDetectMap(Detection obj);
273  void    setObjectFlags();
274  float   enclosedFlux(Detection obj);
275  bool    objAtEdge(Detection obj);
276 
277  // Text outputting of detected objects.
278  void    outputDetectionsKarma(std::ostream &stream);     // in Cubes/detectionIO.cc
279  void    outputDetectionsVOTable(std::ostream &stream);   // in Cubes/detectionIO.cc
280  void    outputDetectionList();                           // in Cubes/detectionIO.cc
281  void    logDetectionList();                              // in Cubes/detectionIO.cc
282  void    logDetection(Detection obj, int counter);        // in Cubes/detectionIO.cc
283  void    setupColumns();                                  // in Detection/columns.cc
284
285  // Graphical plotting of detections.
286  void    plotDetectionMap(string pgDestination);          // in Cubes/plotting.cc
287  void    plotMomentMap(string pgDestination);             // in Cubes/plotting.cc
288  void    plotWCSaxes();                                   // in Cubes/plotting.cc
289  void    outputSpectra();                                 // in Cubes/outputSpectra.cc
290  void    plotSpectrum(Detection obj,Plot::SpectralPlot &plot); // in Cubes/outputSpectra.cc
291  void    drawMomentCutout(Detection &object);             // in Cubes/drawMomentCutout.cc
292  void    drawScale(float xstart, float ystart, float channel, float scaleLength);
293                                                           // in Cubes/drawMomentCutout.cc
294
295private:
296  float  *recon;           // reconstructed array -- used when doing a trous reconstruction.
297  bool    reconExists;     // flag saying whether there is a reconstruction
298  short  *detectMap;       // "moment map" -- x,y locations of detected pixels
299  float  *baseline;        // array of spectral baseline values.
300                       
301  float  *specMean;        // array of means  for each spectrum in cube
302  float  *specSigma;       // array of sigmas for each spectrum in cube
303  float  *chanMean;        // array of means  for each channel map in cube
304  float  *chanSigma;       // array of sigmas for each channel map in cube
305       
306  FitsHeader head;         // the WCS and other header information.
307  ColSet fullColSet;       // the list of all columns as printed in the results file
308  ColSet logColSet;        // the list of columns as printed in the log file
309
310};
311
312/****************************************************************/
313//////////////////////////////////////////////////////
314// Prototypes for functions that use above classes
315//////////////////////////////////////////////////////
316
317DataArray getImage(string fname, short int maxdim);
318Image getImage(string fname);
319
320void findSources(Image &image);
321void findSources(Image &image, float mean, float sigma);
322
323vector <Detection> searchReconArray(long *dim,float *originalArray,float *reconArray, Param &par);
324vector <Detection> search3DArray(long *dim, float *Array, Param &par);
325
326void growObject(Detection &object, Image &image);
327void growObject(Detection &object, Cube &cube);
328
329#endif
Note: See TracBrowser for help on using the repository browser.