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 | #ifndef STATS_H |
---|
21 | #include <Utils/Statistics.hh> |
---|
22 | #endif |
---|
23 | |
---|
24 | using std::string; |
---|
25 | using std::vector; |
---|
26 | using namespace Column; |
---|
27 | using namespace Statistics; |
---|
28 | |
---|
29 | /****************************************************************/ |
---|
30 | ///////////////////////////////////////////////////////////// |
---|
31 | //// Definition of an n-dimensional data array: |
---|
32 | //// array of pixel values, size & dimensions |
---|
33 | //// array of Detection objects |
---|
34 | ///////////////////////////////////////////////////////////// |
---|
35 | |
---|
36 | |
---|
37 | class DataArray |
---|
38 | { |
---|
39 | public: |
---|
40 | DataArray(){numDim=0; numPixels=0;}; |
---|
41 | DataArray(short int nDim){numDim=nDim; numPixels=0;}; |
---|
42 | DataArray(short int nDim, long size); |
---|
43 | DataArray(short int nDim, long *dimensions); |
---|
44 | virtual ~DataArray(); |
---|
45 | // Size and Dimension related |
---|
46 | long getDimX(){if(numDim>0) return axisDim[0];else return 0;}; |
---|
47 | long getDimY(){if(numDim>1) return axisDim[1];else return 1;}; |
---|
48 | long getDimZ(){if(numDim>2) return axisDim[2];else return 1;}; |
---|
49 | void getDim(long &x, long &y, long &z); |
---|
50 | long getSize(){return numPixels;}; |
---|
51 | short int getNumDim(){return numDim;}; |
---|
52 | // Related to the various arrays |
---|
53 | void getDimArray(long *output); |
---|
54 | void getArray(float *output); |
---|
55 | virtual void saveArray(float *input, long size); |
---|
56 | float getPixValue(long pos){return array[pos];}; |
---|
57 | void setPixValue(long pos, float f){array[pos] = f;}; |
---|
58 | // Related to the object lists |
---|
59 | Detection getObject(long number){return objectList[number];}; |
---|
60 | void addObject(Detection object); |
---|
61 | // adds a single detection to the object list |
---|
62 | vector <Detection> getObjectList(){return objectList;}; |
---|
63 | void addObjectList(vector <Detection> newlist); |
---|
64 | // adds all objects in a detection list to the object list |
---|
65 | void addObjectOffsets(); |
---|
66 | long getNumObj(){return objectList.size();}; |
---|
67 | void clearDetectionList(){this->objectList.clear();}; |
---|
68 | // Parameter list related. |
---|
69 | int readParam(string paramfile){ |
---|
70 | return par.readParams(paramfile);}; |
---|
71 | void showParam(std::ostream &stream){stream << par;}; |
---|
72 | Param getParam(){return par;}; |
---|
73 | void saveParam(Param newpar){par = newpar;}; |
---|
74 | Param& pars(){Param &rpar = par; return rpar;}; |
---|
75 | bool isBlank(int vox){return par.isBlank(array[vox]);}; |
---|
76 | // Statistics |
---|
77 | StatsContainer<float> getStats(){return Stats;}; |
---|
78 | StatsContainer<float>& stats(){ |
---|
79 | StatsContainer<float> &rstats = Stats; |
---|
80 | return rstats; |
---|
81 | }; |
---|
82 | void saveStats(StatsContainer<float> newStats){ |
---|
83 | Stats = newStats; |
---|
84 | }; |
---|
85 | |
---|
86 | bool isDetection(float value){ |
---|
87 | if(par.isBlank(value)) return false; |
---|
88 | else return Stats.isDetection(value); |
---|
89 | }; |
---|
90 | |
---|
91 | friend std::ostream& operator<< ( std::ostream& theStream, DataArray &array); |
---|
92 | |
---|
93 | protected: |
---|
94 | short int numDim; // number of dimensions. |
---|
95 | long *axisDim; // array of dimensions of cube |
---|
96 | // (ie. how large in each direction). |
---|
97 | long numPixels; // total number of pixels in cube |
---|
98 | float *array; // array of data |
---|
99 | vector <Detection> objectList; // the list of detected objects |
---|
100 | Param par; // a parameter list. |
---|
101 | StatsContainer<float> Stats; // The statistics for the dataArray |
---|
102 | }; |
---|
103 | |
---|
104 | |
---|
105 | /****************************************************************/ |
---|
106 | ///////////////////////////////////////////////////////////// |
---|
107 | //// Definition of an image object (2D): |
---|
108 | //// a DataArray object |
---|
109 | //// arrays for: probability values (for FDR) |
---|
110 | //// mask image to indicate location of objects |
---|
111 | //// detected objects |
---|
112 | //// statistics information |
---|
113 | ///////////////////////////////////////////////////////////// |
---|
114 | |
---|
115 | class Cube; |
---|
116 | class Image : public DataArray |
---|
117 | { |
---|
118 | public: |
---|
119 | Image(){numPixels=0; numDim=2;}; |
---|
120 | Image(long nPix); |
---|
121 | Image(long *dimensions); |
---|
122 | virtual ~Image(){}; |
---|
123 | |
---|
124 | // Defining the array |
---|
125 | void saveArray(float *input, long size); |
---|
126 | void extractSpectrum(float *Array, long *dim, long pixel); |
---|
127 | void extractImage(float *Array, long *dim, long channel); |
---|
128 | void extractSpectrum(Cube &cube, long pixel); |
---|
129 | void extractImage(Cube &cube, long channel); |
---|
130 | // Accessing the data. |
---|
131 | float getPixValue(long x, long y){return array[y*axisDim[0] + x];}; |
---|
132 | float getPixValue(long pos){return array[pos];}; |
---|
133 | // the next few should have checks against array overflow... |
---|
134 | void setPixValue(long x, long y, float f){array[y*axisDim[0] + x] = f;}; |
---|
135 | void setPixValue(long pos, float f){array[pos] = f;}; |
---|
136 | bool isBlank(int vox){return par.isBlank(array[vox]);}; |
---|
137 | bool isBlank(long x,long y){return par.isBlank(array[y*axisDim[0]+x]);}; |
---|
138 | |
---|
139 | // Detection-related |
---|
140 | void lutz_detect(); // in Detection/lutz_detect.cc |
---|
141 | void spectrumDetect(); // in Detection/spectrumDetect.cc |
---|
142 | int getMinSize(){return minSize;}; |
---|
143 | void setMinSize(int i){minSize=i;}; |
---|
144 | // the rest are in Detection/thresholding_functions.cc |
---|
145 | bool isDetection(long x, long y){ |
---|
146 | long voxel = y*axisDim[0] + x; |
---|
147 | if(isBlank(x,y)) return false; |
---|
148 | else return Stats.isDetection(array[voxel]); |
---|
149 | }; |
---|
150 | |
---|
151 | void removeMW(); |
---|
152 | |
---|
153 | private: |
---|
154 | int minSize; // the minimum number of pixels for a detection |
---|
155 | // to be accepted. |
---|
156 | }; |
---|
157 | |
---|
158 | /****************************************************************/ |
---|
159 | ///////////////////////////////////////////////////////////// |
---|
160 | //// Definition of an data-cube object (3D): |
---|
161 | //// a DataArray object limited to dim=3 |
---|
162 | ///////////////////////////////////////////////////////////// |
---|
163 | |
---|
164 | class Cube : public DataArray |
---|
165 | { |
---|
166 | public: |
---|
167 | Cube(){ |
---|
168 | numPixels=0; numDim=3; |
---|
169 | reconExists = false; reconAllocated = false; baselineAllocated = false; |
---|
170 | }; |
---|
171 | Cube(long nPix); |
---|
172 | Cube(long *dimensions); |
---|
173 | virtual ~Cube(); |
---|
174 | |
---|
175 | // additional accessor functions |
---|
176 | // -- in Cubes/cubes.cc unless otherwise specified. |
---|
177 | |
---|
178 | int getCube(string fname); |
---|
179 | int getCube(){ |
---|
180 | // a front-end to the getCube function, that does subsection checks |
---|
181 | // assumes the Param is setup properly |
---|
182 | string fname = par.getImageFile(); |
---|
183 | if(par.getFlagSubsection()) fname+=par.getSubsection(); |
---|
184 | return getCube(fname); |
---|
185 | }; |
---|
186 | int getFITSdata(string fname); // in FitsIO/dataIO.cc |
---|
187 | void initialiseCube(long *dimensions); // in cubes.cc |
---|
188 | int getopts(int argc, char ** argv); // in cubes.cc |
---|
189 | void readSavedArrays(); // in cubes.cc |
---|
190 | void saveSmoothedCube(); // in saveImage.cc |
---|
191 | void saveReconstructedCube(); // in saveImage.cc |
---|
192 | int readReconCube(); // in readRecon.cc |
---|
193 | int readSmoothCube(); // in readSmooth.cc |
---|
194 | |
---|
195 | bool isBlank(int vox){return par.isBlank(array[vox]);}; |
---|
196 | bool isBlank(long x, long y, long z){ |
---|
197 | return par.isBlank(array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x]);}; |
---|
198 | float getPixValue(long pos){return array[pos];}; |
---|
199 | float getPixValue(long x, long y, long z){ |
---|
200 | return array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x];}; |
---|
201 | short getDetectMapValue(long pos){return detectMap[pos];}; |
---|
202 | short getDetectMapValue(long x, long y){return detectMap[y*axisDim[0]+x];}; |
---|
203 | bool isRecon(){return reconExists;}; |
---|
204 | float getReconValue(long pos){return recon[pos];}; |
---|
205 | float getReconValue(long x, long y, long z){ |
---|
206 | return recon[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x];}; |
---|
207 | float getBaselineValue(long pos){return baseline[pos];}; |
---|
208 | float getBaselineValue(long x, long y, long z){ |
---|
209 | return baseline[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x];}; |
---|
210 | // these should have checks against array overflow... |
---|
211 | void setPixValue(long pos, float f){array[pos] = f;}; |
---|
212 | void setPixValue(long x, long y, long z, float f){ |
---|
213 | array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x] = f;}; |
---|
214 | void setDetectMapValue(long pos, short f){detectMap[pos] = f;}; |
---|
215 | void setDetectMapValue(long x, long y, short f){ |
---|
216 | detectMap[y*axisDim[0] + x] = f;}; |
---|
217 | void setReconValue(long pos, float f){recon[pos] = f;}; |
---|
218 | void setReconValue(long x, long y, long z, float f){ |
---|
219 | recon[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x] = f;}; |
---|
220 | void setReconFlag(bool f){reconExists = f;}; |
---|
221 | void saveArray(float *input, long size); |
---|
222 | void saveRecon(float *input, long size); |
---|
223 | void getRecon(float *output); |
---|
224 | |
---|
225 | vector<Col> getLogCols(){return logCols;}; |
---|
226 | void setLogCols(vector<Col> C){logCols=C;}; |
---|
227 | vector<Col> getFullCols(){return fullCols;}; |
---|
228 | void setFullCols(vector<Col> C){fullCols=C;}; |
---|
229 | |
---|
230 | // Statistics for cube |
---|
231 | void setCubeStatsOld(); // in Cubes/cubes.cc |
---|
232 | void setCubeStats(); // in Cubes/cubes.cc |
---|
233 | int setupFDR(); |
---|
234 | bool isDetection(long x, long y, long z){ |
---|
235 | long voxel = z*axisDim[0]*axisDim[1] + y*axisDim[0] + x; |
---|
236 | return DataArray::isDetection(array[voxel]); |
---|
237 | }; |
---|
238 | |
---|
239 | // Functions that act on the cube |
---|
240 | void removeMW(); // in Cubes/cubes.cc |
---|
241 | void trimCube(); // in Cubes/trimImage.cc |
---|
242 | void unTrimCube(); // in Cubes/trimImage.cc |
---|
243 | void removeBaseline(); // in ATrous/baselineSubtract.cc |
---|
244 | void replaceBaseline(); // in ATrous/baselineSubtract.cc |
---|
245 | void invert(); // in Cubes/invertCube.cc |
---|
246 | void reInvert(); // in Cubes/invertCube.cc |
---|
247 | |
---|
248 | // Reconstruction and Searching functions |
---|
249 | void ReconSearch(); // in ATrous/ReconSearch.cc |
---|
250 | void ReconCube(); // in ATrous/ReconSearch.cc |
---|
251 | void ReconCube1D(); // in ATrous/ReconSearch.cc |
---|
252 | void ReconCube2D(); // in ATrous/ReconSearch.cc |
---|
253 | void ReconCube3D(); // in ATrous/ReconSearch.cc |
---|
254 | void CubicSearch(); // in Cubes/CubicSearch.cc |
---|
255 | void SmoothSearch(); // in Cubes/smoothCube.cc |
---|
256 | void SmoothCube(); // in Cubes/smoothCube.cc |
---|
257 | |
---|
258 | // Dealing with the WCS |
---|
259 | FitsHeader getHead(){return head;}; |
---|
260 | void setHead(FitsHeader F){head=F;}; |
---|
261 | FitsHeader& header(){FitsHeader &h = head; return h;}; |
---|
262 | int wcsToPix(const double *world, double *pix){ |
---|
263 | return this->head.wcsToPix(world,pix);} |
---|
264 | int wcsToPix(const double *world, double *pix, const int npts){ |
---|
265 | return this->head.wcsToPix(world,pix,npts);} |
---|
266 | int pixToWCS(const double *pix, double *world){ |
---|
267 | return this->head.pixToWCS(pix,world);} |
---|
268 | int pixToWCS(const double *pix, double *world, const int npts){ |
---|
269 | return this->head.pixToWCS(pix,world,npts);} |
---|
270 | |
---|
271 | // Dealing with the detections |
---|
272 | void ObjectMerger(); // in Cubes/Merger.cc |
---|
273 | void calcObjectWCSparams(); |
---|
274 | void sortDetections(); |
---|
275 | void updateDetectMap(); |
---|
276 | void updateDetectMap(Detection obj); |
---|
277 | void setObjectFlags(); |
---|
278 | float enclosedFlux(Detection obj); |
---|
279 | bool objAtSpatialEdge(Detection obj); |
---|
280 | bool objAtSpectralEdge(Detection obj); |
---|
281 | |
---|
282 | // Text outputting of detected objects. |
---|
283 | // the next five in Cubes/detectionIO.cc |
---|
284 | void outputDetectionsKarma(std::ostream &stream); |
---|
285 | void outputDetectionsVOTable(std::ostream &stream); |
---|
286 | void outputDetectionList(); |
---|
287 | void logDetectionList(); |
---|
288 | void logDetection(Detection obj, int counter); |
---|
289 | void setupColumns(); // in Cubes/cubes.cc |
---|
290 | |
---|
291 | // Graphical plotting of the cube and the detections. |
---|
292 | void plotBlankEdges(); // in Cubes/cubes.cc |
---|
293 | // the next three in Cubes/plotting.cc |
---|
294 | void plotDetectionMap(string pgDestination); |
---|
295 | void plotMomentMap(string pgDestination); |
---|
296 | void plotMomentMap(vector<string> pgDestination); |
---|
297 | void plotWCSaxes(); |
---|
298 | // the next two in Cubes/outputSpectra.cc |
---|
299 | void outputSpectra(); |
---|
300 | void plotSpectrum(Detection obj,Plot::SpectralPlot &plot); |
---|
301 | // the next three in Cubes/drawMomentCutout.cc |
---|
302 | void drawMomentCutout(Detection &object); |
---|
303 | void drawScale(float xstart,float ystart,float channel); |
---|
304 | void drawFieldEdge(); |
---|
305 | |
---|
306 | private: |
---|
307 | float *recon; // reconstructed array -- used when doing a trous |
---|
308 | // reconstruction. |
---|
309 | bool reconExists; // flag saying whether there is a reconstruction |
---|
310 | short *detectMap; // "moment map" -- x,y locations of detected pixels |
---|
311 | float *baseline; // array of spectral baseline values. |
---|
312 | |
---|
313 | bool reconAllocated; // have we allocated memory for the recon array? |
---|
314 | bool baselineAllocated;// have we allocated memory for the baseline array? |
---|
315 | |
---|
316 | FitsHeader head; // the WCS and other header information. |
---|
317 | vector<Col> fullCols; // the list of all columns as printed in the |
---|
318 | // results file |
---|
319 | vector<Col> logCols; // the list of columns as printed in the log file |
---|
320 | |
---|
321 | }; |
---|
322 | |
---|
323 | /****************************************************************/ |
---|
324 | ////////////////////////////////////////////////////// |
---|
325 | // Prototypes for functions that use above classes |
---|
326 | ////////////////////////////////////////////////////// |
---|
327 | |
---|
328 | void findSources(Image &image); |
---|
329 | void findSources(Image &image, float mean, float sigma); |
---|
330 | |
---|
331 | vector <Detection> searchReconArray(long *dim, float *originalArray, |
---|
332 | float *reconArray, Param &par, |
---|
333 | StatsContainer<float> &stats); |
---|
334 | vector <Detection> search3DArray(long *dim, float *Array, Param &par, |
---|
335 | StatsContainer<float> &stats); |
---|
336 | |
---|
337 | void growObject(Detection &object, Cube &cube); |
---|
338 | |
---|
339 | #endif |
---|