1 | // ----------------------------------------------------------------------- |
---|
2 | // sorting.cc: Sort the list of Detections by channel or velocity. |
---|
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 | #include <iostream> |
---|
29 | #include <sstream> |
---|
30 | #include <vector> |
---|
31 | #include <algorithm> |
---|
32 | #include <duchamp/duchamp.hh> |
---|
33 | #include <duchamp/param.hh> |
---|
34 | #include <duchamp/Detection/detection.hh> |
---|
35 | |
---|
36 | |
---|
37 | /// @brief |
---|
38 | /// A class to match things pair-wise (useful for sorting). |
---|
39 | /// |
---|
40 | /// @details This class is deigned to match two quantities to each |
---|
41 | /// other. It was devised to find a way of taking a pair of lists |
---|
42 | /// that are matched, and sorting one list while keeping the second |
---|
43 | /// matched pair-wise. |
---|
44 | /// |
---|
45 | /// The elements are currently just assumed to be floats. This could be |
---|
46 | /// extended by templating, but at this stage we don't need to... |
---|
47 | class Pair |
---|
48 | { |
---|
49 | public: |
---|
50 | Pair(){}; |
---|
51 | virtual ~Pair(){}; |
---|
52 | friend bool operator< (const Pair& lhs, const Pair& rhs){ |
---|
53 | /// A comparison operator for pairs. Compare the primary elements |
---|
54 | /// of the two pairs, using the basic < operator. |
---|
55 | return (lhs.primary < rhs.primary); |
---|
56 | }; |
---|
57 | void define(float p, float m){ |
---|
58 | /// Basic assignment function. |
---|
59 | primary=p; matching=m; |
---|
60 | }; |
---|
61 | float get1(){return primary;}; |
---|
62 | float get2(){return matching;}; |
---|
63 | private: |
---|
64 | float primary; ///< The main element -- this will be the one that |
---|
65 | ///can be compared. |
---|
66 | float matching; ///< The secondary element -- this cannot be |
---|
67 | ///compared with other objects, it just tracks the |
---|
68 | ///primary. |
---|
69 | }; |
---|
70 | |
---|
71 | //====================================================================== |
---|
72 | |
---|
73 | namespace duchamp |
---|
74 | { |
---|
75 | |
---|
76 | void SortByZ(std::vector <Detection> &inputList) |
---|
77 | { |
---|
78 | /// A Function that takes a list of Detections and sorts them in |
---|
79 | /// order of increasing z-pixel value. Upon return, the inputList |
---|
80 | /// is sorted. |
---|
81 | /// |
---|
82 | /// We use the std::stable_sort function, so that the order of |
---|
83 | /// objects with the same z-value is preserved. |
---|
84 | /// \param inputList List of Detections to be sorted. |
---|
85 | /// \return The inputList is returned with the elements sorted. |
---|
86 | |
---|
87 | long size = inputList.size(); |
---|
88 | Pair *info = new Pair[size]; |
---|
89 | |
---|
90 | for(int i=0;i<size;i++) info[i].define(inputList[i].getZcentre(), float(i)); |
---|
91 | |
---|
92 | std::stable_sort(info,info+size); |
---|
93 | |
---|
94 | std::vector <Detection> sorted; |
---|
95 | for(int i=0;i<size;i++) sorted.push_back( inputList[int(info[i].get2())] ); |
---|
96 | |
---|
97 | delete [] info; |
---|
98 | |
---|
99 | inputList.clear(); |
---|
100 | for(int i=0;i<size;i++) inputList.push_back( sorted[i] ); |
---|
101 | sorted.clear(); |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | //====================================================================== |
---|
106 | |
---|
107 | void SortByVel(std::vector <Detection> &inputList) |
---|
108 | { |
---|
109 | /// @details |
---|
110 | /// A Function that takes a list of Detections and sorts them in |
---|
111 | /// order of increasing velocity. |
---|
112 | /// Every member of the vector needs to have WCS defined, (and if so, |
---|
113 | /// then vel is assumed to be defined for all), otherwise no sorting |
---|
114 | /// is done. |
---|
115 | /// |
---|
116 | /// We use the std::stable_sort function, so that the order of |
---|
117 | /// objects with the same z-value is preserved. |
---|
118 | /// |
---|
119 | /// \param inputList List of Detections to be sorted. |
---|
120 | /// \return The inputList is returned with the elements sorted, |
---|
121 | /// unless the WCS is not good for at least one element, in which |
---|
122 | /// case it is returned unaltered. |
---|
123 | |
---|
124 | bool isGood = true; |
---|
125 | for(size_t i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS(); |
---|
126 | |
---|
127 | if(isGood){ |
---|
128 | |
---|
129 | long size = inputList.size(); |
---|
130 | Pair *info = new Pair[size]; |
---|
131 | |
---|
132 | for(int i=0;i<size;i++) info[i].define(inputList[i].getVel(), float(i)); |
---|
133 | |
---|
134 | std::stable_sort(info, info+size); |
---|
135 | |
---|
136 | std::vector <Detection> sorted; |
---|
137 | for(int i=0;i<size;i++) sorted.push_back( inputList[int(info[i].get2())] ); |
---|
138 | |
---|
139 | delete [] info; |
---|
140 | |
---|
141 | inputList.clear(); |
---|
142 | for(int i=0;i<size;i++) inputList.push_back( sorted[i] ); |
---|
143 | sorted.clear(); |
---|
144 | |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|
148 | |
---|
149 | //====================================================================== |
---|
150 | |
---|
151 | void SortDetections(std::vector <Detection> &inputList, std::string parameter) |
---|
152 | { |
---|
153 | /// @details |
---|
154 | /// A Function that takes a list of Detections and sorts them in |
---|
155 | /// order of increasing value of the parameter given. |
---|
156 | /// |
---|
157 | /// Every member of the vector needs to have WCS defined, (and if |
---|
158 | /// so, then parameter in question is assumed to be defined for |
---|
159 | /// all), otherwise no sorting is done. The exception is the |
---|
160 | /// z-value, in which case sorting is always done. |
---|
161 | /// |
---|
162 | /// We use the std::stable_sort function, so that the order of |
---|
163 | /// objects with the same parameter value is preserved. |
---|
164 | /// |
---|
165 | /// \param inputList List of Detections to be sorted. |
---|
166 | /// \param parameter The name of the parameter to be sorted |
---|
167 | /// on. Options are "zvalue", "vel", "ra", "dec", "iflux", |
---|
168 | /// "pflux". |
---|
169 | /// \return The inputList is returned with the elements sorted, |
---|
170 | /// unless the WCS is not good for at least one element, |
---|
171 | /// in which case it is returned unaltered. |
---|
172 | |
---|
173 | bool OK = false; |
---|
174 | for(int i=0;i<numSortingParamOptions;i++) |
---|
175 | OK = OK || (parameter==sortingParamOptions[i]); |
---|
176 | if(!OK){ |
---|
177 | std::stringstream errmsg; |
---|
178 | errmsg << "Invalid sorting parameter: " << parameter << "\nNot doing any sorting."; |
---|
179 | duchampError("SortDetections", errmsg.str()); |
---|
180 | return; |
---|
181 | } |
---|
182 | |
---|
183 | bool isGood = true; |
---|
184 | if(parameter!="zvalue"){ |
---|
185 | for(size_t i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS(); |
---|
186 | } |
---|
187 | |
---|
188 | if(isGood){ |
---|
189 | |
---|
190 | long size = inputList.size(); |
---|
191 | Pair *info = new Pair[size]; |
---|
192 | |
---|
193 | for(int i=0;i<size;i++){ |
---|
194 | if(parameter=="xvalue") info[i].define(inputList[i].getXcentre(), float(i)); |
---|
195 | else if(parameter=="yvalue") info[i].define(inputList[i].getYcentre(), float(i)); |
---|
196 | else if(parameter=="zvalue") info[i].define(inputList[i].getZcentre(), float(i)); |
---|
197 | else if(parameter=="ra") info[i].define(inputList[i].getRA(), float(i)); |
---|
198 | else if(parameter=="dec") info[i].define(inputList[i].getDec(), float(i)); |
---|
199 | else if(parameter=="vel") info[i].define(inputList[i].getVel(), float(i)); |
---|
200 | else if(parameter=="w50") info[i].define(inputList[i].getW50(), float(i)); |
---|
201 | else if(parameter=="iflux") info[i].define(inputList[i].getIntegFlux(), float(i)); |
---|
202 | else if(parameter=="pflux") info[i].define(inputList[i].getPeakFlux(), float(i)); |
---|
203 | else if(parameter=="snr") info[i].define(inputList[i].getPeakSNR(), float(i)); |
---|
204 | } |
---|
205 | |
---|
206 | std::stable_sort(info, info+size); |
---|
207 | |
---|
208 | std::vector <Detection> sorted; |
---|
209 | for(int i=0;i<size;i++) sorted.push_back( inputList[int(info[i].get2())] ); |
---|
210 | |
---|
211 | delete [] info; |
---|
212 | |
---|
213 | inputList.clear(); |
---|
214 | for(int i=0;i<size;i++) inputList.push_back( sorted[i] ); |
---|
215 | sorted.clear(); |
---|
216 | |
---|
217 | } |
---|
218 | |
---|
219 | } |
---|
220 | |
---|
221 | } |
---|