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