source: branches/NewStructure/src/Detection/detection.hh @ 1441

Last change on this file since 1441 was 1410, checked in by MatthewWhiting, 10 years ago

Results of merging Detections with trunk

File size: 17.0 KB
Line 
1// -----------------------------------------------------------------------
2// detection.hh: Definition of the Detection class.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#ifndef DETECTION_H
29#define DETECTION_H
30
31#include <iostream>
32#include <string>
33#include <vector>
34#include <duchamp/param.hh>
35#include <duchamp/PixelMap/Voxel.hh>
36#include <duchamp/PixelMap/Object3D.hh>
37#include <duchamp/Outputs/columns.hh>
38#include <duchamp/Outputs/CatalogueSpecification.hh>
39
40using namespace PixelInfo;
41
42namespace duchamp
43{
44
45
46  /// Class to represent a contiguous set of detected voxels.
47  ///  This is a detected object, which features:
48  ///   a vector of voxels, average and centroid positions, total & peak fluxes,
49  ///   the possibility of WCS-calculated parameters (RA, Dec, velocity,
50  ///     related widths).
51  ///  Also many functions with which to manipulate the Detections.
52
53  class Detection : public Object3D
54  {
55  public:
56    Detection();
57    Detection(const Object3D& o);
58    Detection(const Detection& d);
59    Detection& operator= (const Detection& d);
60    virtual ~Detection(){};
61    void defaultDetection();
62    //------------------------------
63    // These are functions in detection.cc.
64    //
65
66    friend Detection operator+ (Detection &lhs, Detection &rhs);
67    void addDetection(Detection &other);
68
69    bool canMerge(Detection &other, Param &par);
70    bool isNear(Detection &other, Param &par);
71    bool isClose(Detection &other, Param &par);
72
73    /// @brief Test whether voxel lists match
74    bool voxelListsMatch(std::vector<PixelInfo::Voxel> voxelList);
75
76    /// @brief Test whether a voxel list contains all detected voxels
77    bool voxelListCovered(std::vector<PixelInfo::Voxel> voxelList);
78
79    /// @brief Calculate flux-related parameters of the Detection.
80    void   calcFluxes(float *fluxArray, size_t *dim);
81    /// @brief Calculate flux-related parameters of the Detection.
82    void   calcFluxes(std::vector<PixelInfo::Voxel> voxelList);
83    void   calcFluxes(std::map<Voxel,float> &voxelMap);
84
85      /// @brief Invert the source's flux values
86      void invert();
87
88    /// @brief Calculate parameters related to the World Coordinate System.
89    //    void   calcWCSparams(float *fluxArray, long *dim, FitsHeader &head);
90    void   calcWCSparams(FitsHeader &head);
91
92    /// @brief Calculate the integrated flux over the entire Detection.
93    void   calcIntegFlux(float *fluxArray, size_t *dim, FitsHeader &head, Param &par);
94    /// @brief Calculate the integrated flux over the entire Detection.
95    void   calcIntegFlux(size_t zdim, std::vector<PixelInfo::Voxel> voxelList, FitsHeader &head);
96    void   calcIntegFlux(size_t zdim, std::map<Voxel,float> voxelMap, FitsHeader &head);
97
98    /// @brief Calculate the 20%-/50%-peak-flux widths in a general fashion
99    void calcVelWidths(size_t zdim, float *intSpec, FitsHeader &head);
100    /// @brief Calculate the 20%/50% peak flux widths
101    void calcVelWidths(float *fluxArray, size_t *dim, FitsHeader &head, Param &par);
102    /// @brief Calculate the 20%/50% peak flux widths
103    void calcVelWidths(size_t zdim, std::vector<PixelInfo::Voxel> voxelList, FitsHeader &head);
104    void calcVelWidths(size_t zdim, std::map<Voxel,float> voxelMap, FitsHeader &head);
105
106    /// @brief Calculate the spatial (moment-0) shape
107    void findShape(const float *momentMap, const size_t *dim, FitsHeader &head);
108
109    /// @brief Set the values of the axis offsets from the cube.
110    void   setOffsets(Param &par);
111
112      using Object3D::addOffsets;  //tell the compiler we want both the addOffsets from Object3D *and* Detection
113
114    /// @brief Add the offset values to the pixel locations
115   void   addOffsets(size_t xoff, size_t yoff, size_t zoff){
116       Object3D::addOffsets(xoff,yoff,zoff);
117       xpeak+=xoff; ypeak+=yoff; zpeak+=zoff;
118       xCentroid+=xoff; yCentroid+=yoff; zCentroid+=zoff;
119    };
120
121      void   addOffsets(){ addOffsets(xSubOffset, ySubOffset, zSubOffset);};
122      void   removeOffsets(size_t xoff, size_t yoff, size_t zoff){ addOffsets(-xoff, -yoff, -zoff);};
123      void   removeOffsets(){ addOffsets(-xSubOffset, -ySubOffset, -zSubOffset);};
124      void   addOffsets(Param &par){setOffsets(par); addOffsets();};
125
126    //
127    //---------------------------------
128    // Text Output -- all in Detection/outputDetection.cc
129    //
130    /// @brief The spectral output label that contains info on the WCS position & velocity.
131    std::string outputLabelWCS(); 
132
133    /// @brief The spectral output label that contains info on the pixel location.
134    std::string outputLabelPix();
135
136    /// @brief The spectral output label that contains info on fluxes of the Detection.
137    std::string outputLabelFluxes();
138
139    /// @brief The spectral output label that contains info on widths of the Detection.
140    std::string outputLabelWidths(FitsHeader &head);
141
142    /// @brief Print all required values for the Detection to a table.
143    void printTableRow(std::ostream &stream, Catalogues::CatalogueSpecification columns, Catalogues::DESTINATION tableType);
144
145    /// @brief Print a particular value for the Detection to a table.
146    void printTableEntry(std::ostream &stream, Catalogues::Column column);
147
148    //----------------------------------
149    // For plotting routines... in Cubes/drawMomentCutout.cc
150    //
151    /// @brief Draw spatial borders for a particular Detection.
152    void   drawBorders(int xoffset, int yoffset);
153    //
154   
155    //
156    //----------------------------------
157    // Basic functions
158    //
159
160    /// @brief Add a single voxel to the pixel list.
161    void   addPixel(long x, long y, long z){Object3D::addPixel(x,y,z);};
162   
163    /// @brief Add a single voxel to the pixel list.
164    void   addPixel(PixelInfo::Voxel point){
165      /// @brief This one adds the pixel to the pixelArray, and updates the fluxes according to the Voxel's flux information
166      Object3D::addPixel(point.getX(),point.getY(),point.getZ());
167      totalFlux += point.getF();
168      if(point.getF()>peakFlux){
169        peakFlux = point.getF();
170        xpeak = point.getX(); ypeak = point.getY(); zpeak = point.getZ();
171      }
172    };
173
174    /// @brief How many channels does the Detection have?
175    size_t   getNumChannels(){return Object3D::getNumDistinctZ();};
176
177    /// @brief Is there at least the acceptable minimum number of channels in the Detection? 
178    bool   hasEnoughChannels(int minNumber);
179 
180    //-----------------------------------
181    // Basic accessor functions for private members follow...
182    //
183    long        getXOffset(){return xSubOffset;};
184    void        setXOffset(long o){xSubOffset = o;};
185    long        getYOffset(){return ySubOffset;};
186    void        setYOffset(long o){ySubOffset = o;};
187    long        getZOffset(){return zSubOffset;};
188    void        setZOffset(long o){zSubOffset = o;};
189    //       
190    bool        hasParams(){return haveParams;};
191    //
192    float       getXcentre();
193    float       getYcentre();
194    float       getZcentre();
195    float       getTotalFlux(){return totalFlux;};
196    void        setTotalFlux(float f){totalFlux=f;};
197    float       getTotalFluxError(){return eTotalFlux;};
198    void        setTotalFluxError(float f){eTotalFlux=f;};
199    double      getIntegFlux(){return intFlux;};
200    void        setIntegFlux(double f){intFlux=f;};
201    double      getIntegFluxError(){return eIntFlux;};
202    void        setIntegFluxError(double d){eIntFlux=d;};
203    float       getPeakFlux(){return peakFlux;};
204    void        setPeakFlux(float f){peakFlux=f;};
205    long        getXPeak(){return xpeak;};
206    long        getYPeak(){return ypeak;};
207    long        getZPeak(){return zpeak;};
208    float       getPeakSNR(){return peakSNR;};
209    void        setPeakSNR(float f){peakSNR = f;};
210    float       getXCentroid(){return xCentroid;};
211    float       getYCentroid(){return yCentroid;};
212    float       getZCentroid(){return zCentroid;};
213    std::string getCentreType(){return centreType;};
214    void        setCentreType(std::string s){centreType=s;};
215    bool        isNegative(){return negSource;};
216    void        setNegative(bool f){negSource = f;};
217    std::string getFlagText(){return flagText;};
218    void        setFlagText(std::string s){flagText = s;};
219    void        addToFlagText(std::string s){flagText += s;};
220    //       
221    /// @brief Is the WCS good enough to be used?
222    /// \return Detection::flagWCS =  True/False
223    bool        isWCS(){return flagWCS;};
224    bool        isSpecOK(){return specOK;};
225    void        setSpecOK(bool b){specOK=b;};
226    std::string getName(){return name;};
227    void        setName(std::string s){name=s;};
228    std::string getRAs(){return raS;};
229    std::string getDecs(){return decS;};
230    double      getRA(){return ra;};
231    double      getDec(){return dec;};
232    double      getRAWidth(){return raWidth;};
233    double      getDecWidth(){return decWidth;};
234    double      getMajorAxis(){return majorAxis;};
235    double      getMinorAxis(){return minorAxis;};
236    double      getPositionAngle(){return posang;};
237    double      getVel(){return vel;};
238    double      getVelWidth(){return velWidth;};
239    double      getVelMin(){return velMin;};
240    double      getVelMax(){return velMax;};
241    double      getW20(){return w20;};
242    double      getV20Min(){return v20min;};
243    double      getV20Max(){return v20max;};
244    double      getW50(){return w50;};
245    double      getV50Min(){return v50min;};
246    double      getV50Max(){return v50max;};
247    int         getID(){return id;};
248    void        setID(int i){id = i;};
249    //
250    int         getPosPrec(){return posPrec;};
251    void        setPosPrec(int i){posPrec=i;};
252    int         getXYZPrec(){return xyzPrec;};
253    void        setXYZPrec(int i){xyzPrec=i;};
254    int         getFintPrec(){return fintPrec;};
255    void        setFintPrec(int i){fintPrec=i;};
256    int         getFpeakPrec(){return fpeakPrec;};
257    void        setFpeakPrec(int i){fpeakPrec=i;};
258    int         getVelPrec(){return velPrec;};
259    void        setVelPrec(int i){velPrec=i;};
260    int         getSNRPrec(){return snrPrec;};
261    void        setSNRPrec(int i){snrPrec=i;};
262    //
263  protected:
264    // Subsection offsets
265    long           xSubOffset;     ///< The x-offset, from subsectioned cube
266    long           ySubOffset;     ///< The y-offset, from subsectioned cube
267    long           zSubOffset;     ///< The z-offset, from subsectioned cube
268    bool           haveParams;     ///< Have the parameters been calculated?
269    // Flux related
270    float          totalFlux;      ///< sum of the fluxes of all the pixels
271    float          eTotalFlux;     ///< error on the total flux
272    double         intFlux;        ///< integrated flux : involves integration over velocity.
273    double         eIntFlux;       ///< error on the integrated flux
274    float          peakFlux;       ///< maximum flux over all the pixels
275    long           xpeak;          ///< x-pixel location of peak flux
276    long           ypeak;          ///< y-pixel location of peak flux
277    long           zpeak;          ///< z-pixel location of peak flux
278    float          peakSNR;        ///< signal-to-noise ratio at peak
279    float          xCentroid;      ///< x-pixel location of centroid
280    float          yCentroid;      ///< y-pixel location of centroid
281    float          zCentroid;      ///< z-pixel location of centroid
282    std::string    centreType;     ///< which type of pixel centre to report: "average", "centroid", or "peak" (flux)
283    bool           negSource;      ///< is the source a negative feature?
284    std::string    flagText;       ///< any warning flags about the quality of the detection.
285    // WCS related
286    int            id;             ///< ID -- generally number in list
287    std::string    name;           ///< IAU-style name (based on position)
288    bool           flagWCS;        ///< A flag indicating whether the WCS parameters have been set.
289    std::string    raS;            ///< Right Ascension (or Longitude) of pixel centre in form 12:34:23
290    std::string    decS;           ///< Declination (or Latitude) of pixel centre, in form -12:23:34
291    double         ra;             ///< Central Right Ascension in degrees
292    double         dec;            ///< Central Declination in degrees
293    double         raWidth;        ///< Width of detection in RA direction in arcmin
294    double         decWidth;       ///< Width of detection in Dec direction in arcmin
295    double         majorAxis;      ///< Major axis length in arcmin
296    double         minorAxis;      ///< Minor axis length in arcmin
297    double         posang;         ///< Position angle of the major axis, in degrees
298    bool           specOK;         ///< Is the spectral dimension valid?
299    std::string    specUnits;      ///< Units of the spectral dimension
300    std::string    specType;       ///< WCS ctype code for the spectral dimension
301    std::string    fluxUnits;      ///< Units of flux
302    std::string    intFluxUnits;   ///< Units of integrated flux
303    std::string    lngtype;        ///< Type of longitude axis (RA/GLON)
304    std::string    lattype;        ///< Type of latitude axis (DEC/GLAT)
305    double         vel;            ///< Central velocity (from zCentre)
306    double         velWidth;       ///< Full velocity width
307    double         velMin;         ///< Minimum velocity
308    double         velMax;         ///< Maximum velocity
309    double         v20min;         ///< Minimum velocity at 20% of peak flux
310    double         v20max;         ///< Maximum velocity at 20% of peak flux
311    double         w20;            ///< Velocity width at 20% of peak flux 
312    double         v50min;         ///< Minimum velocity at 50% of peak flux
313    double         v50max;         ///< Maximum velocity at 50% of peak flux
314    double         w50;            ///< Velocity width at 50% of peak flux 
315    /// @brief  The next six are the precision of values printed in the headers of the spectral plots
316    /// @name
317    /// @{
318    int            posPrec;        ///< Precision of WCS positional values
319    int            xyzPrec;        ///< Precision of pixel positional values
320    int            fintPrec;       ///< Precision of F_int/F_tot values
321    int            fpeakPrec;      ///< Precision of F_peak values
322    int            velPrec;        ///< Precision of velocity values.
323    int            snrPrec;        ///< Precision of S/N_max values.
324    /// @}
325  };
326
327  //==========================================================================
328
329  //////////////////////////////////////////////////////
330  // Prototypes for functions that use above classes
331  //////////////////////////////////////////////////////
332
333  //----------------
334  // These are in sorting.cc
335  //
336  /// @brief Sort a list of Detections by Z-pixel value.
337  void SortByZ(std::vector <Detection> &inputList);
338
339  /// @brief Sort a list of Detections by Velocity.
340  void SortByVel(std::vector <Detection> &inputList);
341
342  /// @brief Sort a list of Detections by a nominated parameter
343  void SortDetections(std::vector <Detection> &inputList, std::string parameter);
344
345  //----------------
346  // This is in areClose.cc
347  //
348  // /// @brief Determine whether two objects are close according to set parameters.
349  // bool areClose(Detection &object1, Detection &object2, Param &par);
350
351  //----------------
352  // This is in mergeIntoList.cc
353  //
354  /// @brief Add an object into a list, combining with adjacent objects if need be.
355  void mergeIntoList(Detection &object, std::vector <Detection> &objList,
356                     Param &par);
357
358  //----------------
359  // These are in Cubes/Merger.cc
360  //
361  /// @brief Merge a list of Detections so that all adjacent voxels are in the same Detection.
362  void mergeList(std::vector<Detection> &objList, Param &par);   
363  /// @brief Culls a list of Detections that do not meet minimum requirements.
364  void finaliseList(std::vector<Detection> &objList, Param &par);
365  /// @brief Manage both the merging and the cleaning up of the list.
366  void ObjectMerger(std::vector<Detection> &objList, Param &par);
367
368  // /// @brief Print the header information to a particular table
369  // void outputTableHeader(std::ostream &stream, Catalogues::CatalogueSpecification &columns, Catalogues::DESTINATION tableType, bool flagWCS);
370
371}
372
373#endif
Note: See TracBrowser for help on using the repository browser.