source: trunk/src/Cubes/cubes.hh @ 222

Last change on this file since 222 was 222, checked in by Matthew Whiting, 17 years ago

Large commit, but mostly documentation-oriented.

Only non-doc-related changes are:

  • To remove two deprecated files and any declarations of the functions in them
  • To move drawBlankEdges to the Cubes/ directory
  • Some small changes to the implementation of the StatsContainer? functions.
  • Creation of Utils/devel.hh to hide functions not used in Duchamp
  • To move the trimmedHist stats functions to their own file, again to hide them.
File size: 20.2 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#ifndef STATS_H
21#include <Utils/Statistics.hh>
22#endif
23
24using std::string;
25using std::vector;
26using namespace Column;
27using 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
38class DataArray
39{
40public:
41  DataArray();  ///< Basic DataArray constructor
42  DataArray(short int nDim); ///< Basic nDim-dimensional DataArray constructor
43  DataArray(short int nDim, long size);///< Basic nDim-dimensional DataArray constructor, specifying size.
44  DataArray(short int nDim, long *dimensions); ///< Basic nDim-dimensional DataArray constructor, specifying size of dimensions.
45  virtual ~DataArray(); ///< Basic DataArray constructor
46
47  //-----------------------------------------
48  // Obvious inline accessor functions.
49  //
50  long               getDimX(){if(numDim>0) return this->axisDim[0];else return 0;};
51  long               getDimY(){if(numDim>1) return this->axisDim[1];else return 1;};
52  long               getDimZ(){if(numDim>2) return this->axisDim[2];else return 1;};
53  long               getSize(){ return this->numPixels; };
54  short int          getNumDim(){ return this->numDim; };
55  virtual float              getPixValue(long pos){ return array[pos]; };
56  virtual void               setPixValue(long pos, float f){array[pos] = f;};;
57  Detection          getObject(long number){ return objectList[number]; };
58  vector <Detection> getObjectList(){ return objectList; };
59  long               getNumObj(){ return objectList.size(); };
60
61  /** Delete all objects from the list of detections. */
62  void               clearDetectionList(){ this->objectList.clear(); };
63
64  /** Read a parameter set from file. */
65  int                readParam(string paramfile){
66    /**
67     *  Uses Param::readParams() to read parameters from a file.
68     *  \param paramfile The file to be read.
69     */
70  return par.readParams(paramfile); };
71
72  //-----------------------------------------
73  // Related to the various arrays
74  //
75  /**  Return first three dimensional axes. */
76  void               getDim(long &x, long &y, long &z);
77  /** Return array of dimensional sizes.*/
78  void               getDimArray(long *output);
79
80  /** Return pixel value array. */
81  void               getArray(float *output);
82
83  /** Save pixel value array. */
84  virtual void       saveArray(float *input, long size);
85
86  //-----------------------------------------
87  // Related to the object lists
88  //
89  /** Adds a single detection to the object list.*/
90  void               addObject(Detection object);
91
92  /** Adds all objects in a detection list to the object list. */
93  void               addObjectList(vector <Detection> newlist);   
94  /** Add pixel offsets to object coordinates. */
95  void               addObjectOffsets();
96
97  //-----------------------------------------
98  // Parameter list related.
99  //
100  /** Output the Param set.*/
101  void               showParam(std::ostream &stream){ stream << this->par; };
102  /** Return the Param set.*/
103  Param              getParam(){ return this->par; };
104  /** Save a Param set to the Cube.*/
105  void               saveParam(Param newpar){this->par = newpar;};
106  /** Provides a reference to the Param set.*/
107  Param&             pars(){ Param &rpar = this->par; return rpar; };
108  /** Is the voxel number given by vox a BLANK value?*/
109  bool               isBlank(int vox);
110
111  // --------------------------------------------
112  // Statistics
113  //
114  /**  Returns the StatsContainer. */
115  StatsContainer<float> getStats(){ return this->Stats; };
116
117  /** Provides a reference to the StatsContainer. */
118  StatsContainer<float>& stats(){ StatsContainer<float> &rstats = this->Stats;  return rstats;};
119  /** Save a StatsContainer to the Cube. */
120  void saveStats(StatsContainer<float> newStats){ this->Stats = newStats;};
121
122  /** A detection test for value. */
123  bool isDetection(float value);
124
125  /**  A detection test for pixel.  */
126  bool isDetection(long voxel);
127
128
129  /** Output operator for DataArray.*/
130  friend std::ostream& operator<< (std::ostream& theStream, DataArray &array);
131 
132
133protected:
134  short int               numDim;     ///< Number of dimensions.
135  long                   *axisDim;    ///< Array of dimensions of cube (ie. how large in each direction).
136  long                    numPixels;  ///< Total number of pixels in cube.
137  float                  *array;      ///< Array of data.
138  vector <Detection>      objectList; ///< The list of detected objects.
139  Param                   par;        ///< A parameter list.
140  StatsContainer<float>   Stats;      ///< The statistics for the DataArray.
141};
142
143
144
145//===============================================================================
146
147/**
148 * Definition of an data-cube object (3D):
149 *     a DataArray object limited to dim=3
150 */
151
152class Cube : public DataArray
153{
154public:
155  Cube();                 ///< Basic Cube constructor.
156  Cube(long nPix);        ///< Alternative Cube constructor.
157  Cube(long *dimensions); ///< Alternative Cube constructor.
158  virtual ~Cube();        ///< Basic Cube destructor.
159
160  // INLINE functions -- definitions included after class declaration.
161  /** Is the voxel number given by vox a BLANK value? */
162  bool        isBlank(int vox){ return par.isBlank(array[vox]); };
163
164  /** Is the voxel at (x,y,z) a BLANK value? */
165  bool        isBlank(long x, long y, long z){
166    return par.isBlank(array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x]); };
167
168  /** Does the Cube::recon array exist? */
169  bool        isRecon(){ return reconExists; };
170
171  float       getPixValue(long pos){ return array[pos]; };
172  float       getPixValue(long x, long y, long z){
173    return array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x]; };
174  short       getDetectMapValue(long pos){ return detectMap[pos]; };
175  short       getDetectMapValue(long x, long y){ return detectMap[y*axisDim[0]+x]; };
176  float       getReconValue(long pos){ return recon[pos]; };
177  float       getReconValue(long x, long y, long z){
178    return recon[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x];};
179  float       getBaselineValue(long pos){ return baseline[pos]; };
180  float       getBaselineValue(long x, long y, long z){
181    return baseline[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x]; };
182  void        setPixValue(long pos, float f){array[pos] = f;};;
183  void        setPixValue(long x, long y, long z, float f){
184    array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x] = f;};
185  void        setDetectMapValue(long pos, short f){ detectMap[pos] = f;};
186  void        setDetectMapValue(long x, long y, short f){ detectMap[y*axisDim[0] + x] = f;};
187  void        setReconValue(long pos, float f){recon[pos] = f;};
188  void        setReconValue(long x, long y, long z, float f){
189    recon[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x] = f; };
190  void        setReconFlag(bool f){reconExists = f;};
191
192  vector<Col> getLogCols(){return logCols;};
193  void        setLogCols(vector<Col> C){logCols=C;};
194  vector<Col> getFullCols(){return fullCols;};
195  void        setFullCols(vector<Col> C){fullCols=C;};
196
197  // additional functions -- in Cubes/cubes.cc
198  /** Allocate memory correctly, with WCS defining the correct axes. */
199  void        initialiseCube(long *dimensions);
200
201  /** Read in a FITS file, with subsection correction. */
202  int         getCube();
203
204  /** Read in command-line options. */
205  int         getopts(int argc, char ** argv);
206
207  /** Read in reconstructed & smoothed arrays from FITS files on disk. */
208  void        readSavedArrays();
209
210  /** Save an external array to the Cube's pixel array */
211  void        saveArray(float *input, long size);
212
213  /** Save an external array to the Cube's reconstructed array. */
214  void        saveRecon(float *input, long size);
215
216  /** Save reconstructed array to an external array. */
217  void        getRecon(float *output);
218
219  /** Set Milky Way channels to zero. */
220  void        removeMW();
221
222  //------------------------
223  // Statistics for cube
224  //
225
226  /** Calculate the statistics for the Cube (older version). */
227  void        setCubeStatsOld();
228
229  /** Calculate the statistics for the Cube. */
230  void        setCubeStats();
231
232  /** Set up thresholds for the False Discovery Rate routine. */
233  int         setupFDR();
234
235  /** A detection test for a given voxel. */
236  bool        isDetection(long x, long y, long z);
237
238  //-----------------------------
239  // Dealing with the detections
240  //
241
242  /** Calculate the WCS parameters for each Cube Detection. */
243  void        calcObjectWCSparams();
244
245  /** Sort the list of detections. */
246  void        sortDetections();
247
248  /** Update the map of detected pixels. */
249  void        updateDetectMap();
250
251  /** Update the map of detected pixels for a given Detection. */
252  void        updateDetectMap(Detection obj);
253
254  /** Find the flux enclosed by a Detection. */
255  float       enclosedFlux(Detection obj);
256
257  /** Set up the output column definitions for the Cube and its Detection list. */
258  void        setupColumns();
259
260  /** Is the object at the edge of the image? */
261  bool        objAtSpatialEdge(Detection obj);
262
263  /** Is the object at an end of the spectrum? */
264  bool        objAtSpectralEdge(Detection obj);
265
266  /** Set warning flags for the detections. */
267  void        setObjectFlags();
268
269  // ----------------------------
270  // Dealing with the WCS
271  //
272  FitsHeader  getHead(){ return head; };
273  void        setHead(FitsHeader F){head = F;};
274  FitsHeader& header(){ FitsHeader &h = head; return h; };
275
276  /** Convert a point from WCS to Pixel coords. */
277  int         wcsToPix(const double *world, double *pix);
278
279  /** Convert a set of points from WCS to Pixel coords. */
280  int         wcsToPix(const double *world, double *pix, const int npts);
281
282  /** Convert a point from Pixel to WCS coords. */
283  int         pixToWCS(const double *pix, double *world);
284
285  /** Convert a set of points from Pixel to WCS coords. */
286  int         pixToWCS(const double *pix, double *world, const int npts);
287
288  //-------------------------------------------
289  // FITS-I/O related functions -- not in cubes.cc
290  //
291  /** Function to read in FITS file.*/
292  int         getCube(string fname);  // in Cubes/getImage.cc
293
294  /** Function to retrieve FITS data array */
295  int         getFITSdata(string fname);   // in FitsIO/dataIO.cc
296
297  /** Save Hanning-smoothed array to disk.*/
298  void        saveSmoothedCube();       // in Cubes/saveImage.cc
299  /** Save Reconstructed array to disk. */
300  void        saveReconstructedCube();  // in Cubes/saveImage.cc
301
302  /** Read in reconstructed array from FITS file. */
303  int         readReconCube();  // in Cubes/readRecon.cc
304 
305  /** Read in Hanning-smoothed array from FITS file. */
306  int         readSmoothCube();     // in Cubes/readSmooth.cc 
307
308  //-------------------------------------
309  // Functions that act on the cube
310  //
311
312  /** Remove excess BLANK pixels from spatial edge of cube. */
313  void        trimCube();         // in Cubes/trimImage.cc
314
315  /** Replace BLANK pixels to spatial edge of cube. */
316  void        unTrimCube();       // in Cubes/trimImage.cc
317
318  /** Removes the baselines from the spectra, and stores in Cube::baseline */
319  void        removeBaseline();   // in Cubes/baseline.cc
320
321  /** Replace the baselines stored in Cube::baseline */
322  void        replaceBaseline();  // in Cubes/baseline.cc
323
324  /** Multiply all pixel values by -1. */
325  void        invert();           // in Cubes/invertCube.cc
326
327  /** Undo the inversion, and invert fluxes of all detected objects. */
328  void        reInvert();         // in Cubes/invertCube.cc
329
330  //-------------------------------------
331  // Reconstruction, Searching and Merging functions
332  //
333  // in ATrous/ReconSearch.cc
334  /** Switcher to reconstruction functions */
335  void        ReconCube();
336
337  /** Front-end to reconstruction & searching functions.*/
338  void        ReconSearch();
339  /** Performs 1-dimensional a trous reconstruction on the Cube. */
340  void        ReconCube1D();
341  /** Performs 2-dimensional a trous reconstruction on the Cube. */
342  void        ReconCube2D();
343  /** Performs 3-dimensional a trous reconstruction on the Cube. */
344  void        ReconCube3D();
345
346  // in Cubes/CubicSearch.cc
347  /** Front-end to the cubic searching routine. */
348  void        CubicSearch();
349
350  // in Cubes/smoothCube.cc
351  /** Front-end to the smoothing and searching functions. */
352  void        SmoothSearch();
353  /** A function to Hanning-smooth the cube. */
354  void        SmoothCube();
355
356  // in Cubes/Merger.cc
357  /** Merge all objects in the detection list so that only distinct ones are left. */
358  void        ObjectMerger();
359 
360  //-------------------------------------
361  // Text outputting of detected objects.
362  //   in Cubes/detectionIO.cc
363  //
364  /** Output detections to a Karma annotation file. */
365  void        outputDetectionsKarma(std::ostream &stream);
366
367  /** Output detections to a VOTable. */
368  void        outputDetectionsVOTable(std::ostream &stream);
369
370  /** Output detections to the output file and standard output. */
371  void        outputDetectionList();
372
373  /** Output detections to the log file. */
374  void        logDetectionList();
375
376  /** Output a single detection to the log file. */
377  void        logDetection(Detection obj, int counter);
378
379
380  // ----------------------------------
381  // Graphical plotting of the cube and the detections.
382  //
383  /** Draw blank edges of cube. */
384  void        plotBlankEdges();  // in cubes.cc
385
386  //  in Cubes/plotting.cc
387  /** Plot a spatial map of detections based on number of detected channels. */
388  void        plotDetectionMap(string pgDestination);
389
390  /** Plot a spatial map of detections based on 0th moment map of each object. */
391  void        plotMomentMap(string pgDestination);
392
393  /** Plot a spatial map of detections based on 0th moment map of each object to a number of PGPLOT devices. */
394  void        plotMomentMap(vector<string> pgDestination);
395
396  /** Draw WCS axes over a PGPLOT map. */
397  void        plotWCSaxes();
398
399  //  in Cubes/outputSpectra.cc
400  /** Print spectra of each detected object. */
401  void        outputSpectra();
402
403  /** Print spectrum of a single object */
404  void        plotSpectrum(Detection obj,Plot::SpectralPlot &plot);
405
406  //  in Cubes/drawMomentCutout.cc
407  /** Draw the 0th moment map for a single object. */
408  void        drawMomentCutout(Detection &object);
409
410  /** Draw a scale bar indicating angular size of the cutout. */
411  void        drawScale(float xstart,float ystart,float channel);
412
413  /** Draw a yellow line around the edge of the spatial extent of pixels. */
414  void        drawFieldEdge();
415
416private:
417  float      *recon;            ///< reconstructed array - used when doing a trous reconstruction.
418  bool        reconExists;      ///< flag saying whether there is a reconstruction
419  short      *detectMap;        ///< "moment map" - x,y locations of detected pixels
420  float      *baseline;         ///< array of spectral baseline values.
421                             
422  bool        reconAllocated;   ///< have we allocated memory for the recon array?
423  bool        baselineAllocated;///< have we allocated memory for the baseline array?
424                             
425  FitsHeader  head;             ///< the WCS and other header information.
426  vector<Col> fullCols;         ///< the list of all columns as printed in the results file
427  vector<Col> logCols;          ///< the list of columns as printed in the log file
428
429};
430
431////////////
432//// Cube inline function definitions
433////////////
434
435inline int Cube::wcsToPix(const double *world, double *pix)
436{
437  /**
438   *  Use the WCS in the FitsHeader to convert from WCS to pixel coords for
439   *   a single point.
440   *  \param world The world coordinates.
441   *  \param pix The returned pixel coordinates.
442   */
443  return this->head.wcsToPix(world,pix);
444}
445inline int Cube::wcsToPix(const double *world, double *pix, const int npts)
446{
447  /**
448   *  Use the WCS in the FitsHeader to convert from WCS to pixel coords for
449   *   a set of points.
450   *  \param world The world coordinates.
451   *  \param pix The returned pixel coordinates.
452   *  \param npts The number of points being converted.
453   */
454  return this->head.wcsToPix(world,pix,npts);
455}
456inline int Cube::pixToWCS(const double *pix, double *world)
457{
458  /**
459   *  Use the WCS in the FitsHeader to convert from pixel to WCS coords for
460   *   a single point.
461   *  \param pix The pixel coordinates.
462   *  \param world The returned world coordinates.
463   */
464  return this->head.pixToWCS(pix,world);
465}
466inline int Cube::pixToWCS(const double *pix, double *world, const int npts)
467{
468  /**
469   *  Use the WCS in the FitsHeader to convert from pixel to WCS coords for
470   *   a set of points.
471   *  \param pix The pixel coordinates.
472   *  \param world The returned world coordinates.
473   *  \param npts The number of points being converted.
474   */
475  return this->head.pixToWCS(pix,world,npts);
476}
477
478
479//===============================================================================
480
481/**
482 *  A DataArray object limited to two dimensions, and with some additional
483 *   special functions.
484 *
485 *  It is used primarily for searching a 1- or 2-D array with
486 *   lutz_detect() and spectrumDetect().
487 */
488
489class Image : public DataArray
490{
491public:
492  Image(){numPixels=0; numDim=2;};
493  Image(long nPix);
494  Image(long *dimensions);
495  virtual ~Image(){};
496
497  // Defining the array
498  /** Save an external array to the Cube's pixel array */
499  void      saveArray(float *input, long size);
500
501  /** Extract a spectrum from an array and save to the local pixel array. */
502  void      extractSpectrum(float *Array, long *dim, long pixel);
503
504  /** Extract an image from an array and save to the local pixel array. */
505  void      extractImage(float *Array, long *dim, long channel);
506
507  /** Extract a spectrum from a cube and save to the local pixel array. */
508  void      extractSpectrum(Cube &cube, long pixel);
509
510  /** Extract an image from a cube and save to the local pixel array. */
511  void      extractImage(Cube &cube, long channel);
512
513  //--------------------
514  // Accessing the data.
515  float     getPixValue(long pos){ return array[pos]; };
516  float     getPixValue(long x, long y){return array[y*axisDim[0] + x];};
517  // the next few should have checks against array overflow...
518  void      setPixValue(long pos, float f){array[pos] = f;};;
519  void      setPixValue(long x, long y, float f){array[y*axisDim[0] + x] = f;};
520  bool      isBlank(int vox){return par.isBlank(array[vox]);};
521  bool      isBlank(long x,long y){return par.isBlank(array[y*axisDim[0]+x]);};
522
523  //-----------------------
524  // Detection-related
525  /** Detect objects in a 2-D image */
526  void      lutz_detect();            // in Detection/lutz_detect.cc
527
528  /** Detect objects in a 1-D spectrum */
529  void      spectrumDetect();         // in Detection/spectrumDetect.cc
530
531  int       getMinSize(){return minSize;};
532  void      setMinSize(int i){minSize=i;};
533  // the rest are in Detection/thresholding_functions.cc
534
535  /**  A detection test for a pixel location.  */
536  bool      isDetection(long x, long y){
537    /**
538     * Test whether a pixel (x,y) is a statistically significant detection,
539     * according to the set of statistics in the local StatsContainer object.
540     */
541    long voxel = y*axisDim[0] + x;
542    if(isBlank(x,y)) return false;
543    else return Stats.isDetection(array[voxel]);
544  }; 
545
546  /** Blank out a set of channels marked as being Milky Way channels */
547  void      removeMW();
548 
549private:
550  int        minSize;    ///< the minimum number of pixels for a detection to be accepted.
551};
552
553
554/****************************************************************/
555//////////////////////////////////////////////////////
556// Prototypes for functions that use above classes
557//////////////////////////////////////////////////////
558
559/** Search a reconstructed array for significant detections. */
560vector <Detection> searchReconArray(long *dim, float *originalArray,
561                                    float *reconArray, Param &par,
562                                    StatsContainer<float> &stats);
563
564/** Search a 3-dimensional array for significant detections. */
565vector <Detection> search3DArray(long *dim, float *Array, Param &par,
566                                 StatsContainer<float> &stats);
567
568/** Grow an object to a lower threshold */
569void growObject(Detection &object, Cube &cube);
570
571/** Draw the edge of the BLANK region on a map.*/
572void drawBlankEdges(float *dataArray, int xdim, int ydim, Param &par);
573
574#endif
Note: See TracBrowser for help on using the repository browser.