source: tags/release-1.2.2/src/Detection/sorting.cc @ 1455

Last change on this file since 1455 was 989, checked in by MatthewWhiting, 12 years ago

Removing the old Pair class - no longer used.

File size: 7.7 KB
Line 
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 <map>
33#include <duchamp/duchamp.hh>
34#include <duchamp/param.hh>
35#include <duchamp/Detection/detection.hh>
36
37
38
39namespace duchamp
40{
41
42  void SortByZ(std::vector <Detection> &inputList)
43  {
44    /// A Function that takes a list of Detections and sorts them in
45    /// order of increasing z-pixel value.  Upon return, the inputList
46    /// is sorted.
47    ///
48    /// We use the std::stable_sort function, so that the order of
49    /// objects with the same z-value is preserved.
50    /// \param inputList List of Detections to be sorted.
51    /// \return The inputList is returned with the elements sorted.
52
53    std::multimap<float, size_t> complist;
54    std::vector<Detection> sorted;
55    std::multimap<float, size_t>::iterator comp;
56    std::vector<Detection>::iterator det;
57    size_t ct=0;
58    for (det=inputList.begin();det<inputList.end();det++){
59      complist.insert(std::pair<float, size_t>(det->getZcentre(), ct++));
60    }
61
62    for (comp = complist.begin(); comp != complist.end(); comp++)
63      sorted.push_back(inputList[comp->second]);
64
65    inputList.clear();
66    for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det );
67    sorted.clear();
68         
69  }
70
71  //======================================================================
72
73  void SortByVel(std::vector <Detection> &inputList)
74  {
75    /// @details
76    /// A Function that takes a list of Detections and sorts them in
77    ///  order of increasing velocity.
78    /// Every member of the vector needs to have WCS defined, (and if so,
79    ///   then vel is assumed to be defined for all), otherwise no sorting
80    ///   is done.
81    ///
82    /// We use the std::stable_sort function, so that the order of
83    /// objects with the same z-value is preserved.
84    ///
85    /// \param inputList List of Detections to be sorted.
86    /// \return The inputList is returned with the elements sorted,
87    /// unless the WCS is not good for at least one element, in which
88    /// case it is returned unaltered.
89
90    bool isGood = true;
91    for(size_t i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS();
92
93    if(isGood){
94
95      std::multimap<double, size_t> complist;
96      std::vector<Detection> sorted;
97      std::multimap<double, size_t>::iterator comp;
98      std::vector<Detection>::iterator det;
99      size_t ct=0;
100      for (det=inputList.begin();det<inputList.end();det++){
101        complist.insert(std::pair<double, size_t>(det->getVel(), ct++));
102      }
103
104      for (comp = complist.begin(); comp != complist.end(); comp++)
105        sorted.push_back(inputList[comp->second]);
106
107      inputList.clear();
108      for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det );
109      sorted.clear();
110
111 
112    }
113
114  } 
115
116  //======================================================================
117
118  void SortDetections(std::vector <Detection> &inputList, std::string parameter)
119  {
120    /// @details
121    /// A Function that takes a list of Detections and sorts them in
122    ///  order of increasing value of the parameter given.
123    ///
124    /// For parameters that need the WCS (iflux, vel, ra, dec, w50), a
125    /// check is made that the WCS is valid, using
126    /// Detection::isWCS(). If it is not, the list is returned
127    /// unsorted.
128    ///
129    /// \param inputList List of Detections to be sorted.
130    /// \param parameter The name of the parameter to be sorted
131    ///        on. Options are listed in the param.hh file
132    /// \return The inputList is returned with the elements sorted,
133    ///         unless the WCS is not good for at least one element,
134    ///         in which case it is returned unaltered.
135
136    bool OK = false, reverseSort=(parameter[0]=='-');
137    std::string checkParam;
138    if(reverseSort) checkParam = parameter.substr(1);
139    else checkParam = parameter;
140    for(int i=0;i<numSortingParamOptions;i++)
141      OK = OK || (checkParam == sortingParamOptions[i]);
142    if(!OK){
143      DUCHAMPERROR("SortDetections", "Invalid sorting parameter: " << parameter << " -- Not doing any sorting.");
144      return;
145    }
146
147    bool isGood = true;
148    if(checkParam!="zvalue" && checkParam!="pflux" && checkParam!="snr" && checkParam!="xvalue" && checkParam!="yvalue"){
149      for(size_t i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS();
150    }
151
152    if(isGood){
153
154      std::vector<Detection> sorted;
155      std::vector<Detection>::iterator det;
156
157      if(checkParam=="xvalue" || checkParam=="yvalue" || checkParam=="zvalue" || checkParam=="iflux" || checkParam=="pflux" || checkParam=="snr"){
158
159        std::multimap<float, size_t> complist;
160        std::multimap<float, size_t>::iterator comp;
161        size_t ct=0;
162        float reverse = reverseSort ? -1. : 1.;
163        for (det=inputList.begin();det<inputList.end();det++){
164          if(checkParam=="xvalue")      complist.insert(std::pair<float, size_t>(reverse*det->getXcentre(),   ct++));
165          else if(checkParam=="yvalue") complist.insert(std::pair<float, size_t>(reverse*det->getYcentre(),   ct++));
166          else if(checkParam=="zvalue") complist.insert(std::pair<float, size_t>(reverse*det->getZcentre(),   ct++));
167          else if(checkParam=="iflux")  complist.insert(std::pair<float, size_t>(reverse*det->getIntegFlux(), ct++));
168          else if(checkParam=="pflux")  complist.insert(std::pair<float, size_t>(reverse*det->getPeakFlux(),  ct++));
169          else if(checkParam=="snr")    complist.insert(std::pair<float, size_t>(reverse*det->getPeakSNR(),   ct++));
170        }
171       
172        for (comp = complist.begin(); comp != complist.end(); comp++)
173          sorted.push_back(inputList[comp->second]);
174       
175      }
176      else if(checkParam=="ra" || checkParam=="dec" || checkParam=="vel" || checkParam=="w50"){
177
178        std::multimap<double, size_t> complist;
179        std::multimap<double, size_t>::iterator comp;
180        size_t ct=0;
181        double reverse = reverseSort ? -1. : 1.;
182        for (det=inputList.begin();det<inputList.end();det++){
183          if(checkParam=="ra")       complist.insert(std::pair<double, size_t>(reverse*det->getRA(),  ct++));
184          else if(checkParam=="dec") complist.insert(std::pair<double, size_t>(reverse*det->getDec(), ct++));
185          else if(checkParam=="vel") complist.insert(std::pair<double, size_t>(reverse*det->getVel(), ct++));
186          else if(checkParam=="w50") complist.insert(std::pair<double, size_t>(reverse*det->getW50(), ct++));
187        }
188       
189        for (comp = complist.begin(); comp != complist.end(); comp++)
190          sorted.push_back(inputList[comp->second]);
191       
192      }
193
194      inputList.clear();
195      for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det );
196      sorted.clear();
197 
198    }
199
200  } 
201
202}
Note: See TracBrowser for help on using the repository browser.