[301] | 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 | // ----------------------------------------------------------------------- |
---|
[571] | 28 | #include <iostream> |
---|
| 29 | #include <sstream> |
---|
[3] | 30 | #include <vector> |
---|
[212] | 31 | #include <algorithm> |
---|
[954] | 32 | #include <map> |
---|
[571] | 33 | #include <duchamp/duchamp.hh> |
---|
| 34 | #include <duchamp/param.hh> |
---|
[393] | 35 | #include <duchamp/Detection/detection.hh> |
---|
[3] | 36 | |
---|
| 37 | |
---|
[212] | 38 | |
---|
[378] | 39 | namespace duchamp |
---|
[3] | 40 | { |
---|
| 41 | |
---|
[571] | 42 | void SortByZ(std::vector <Detection> &inputList) |
---|
[378] | 43 | { |
---|
[528] | 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. |
---|
[378] | 52 | |
---|
[954] | 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 | } |
---|
[3] | 61 | |
---|
[954] | 62 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
| 63 | sorted.push_back(inputList[comp->second]); |
---|
[3] | 64 | |
---|
[378] | 65 | inputList.clear(); |
---|
[954] | 66 | for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det ); |
---|
[378] | 67 | sorted.clear(); |
---|
[954] | 68 | |
---|
[378] | 69 | } |
---|
[3] | 70 | |
---|
[378] | 71 | //====================================================================== |
---|
[301] | 72 | |
---|
[571] | 73 | void SortByVel(std::vector <Detection> &inputList) |
---|
[378] | 74 | { |
---|
[528] | 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. |
---|
[3] | 89 | |
---|
[378] | 90 | bool isGood = true; |
---|
[623] | 91 | for(size_t i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS(); |
---|
[3] | 92 | |
---|
[378] | 93 | if(isGood){ |
---|
[3] | 94 | |
---|
[954] | 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 | } |
---|
[3] | 103 | |
---|
[954] | 104 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
| 105 | sorted.push_back(inputList[comp->second]); |
---|
[3] | 106 | |
---|
[378] | 107 | inputList.clear(); |
---|
[954] | 108 | for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det ); |
---|
[378] | 109 | sorted.clear(); |
---|
[954] | 110 | |
---|
| 111 | |
---|
[378] | 112 | } |
---|
[3] | 113 | |
---|
[378] | 114 | } |
---|
| 115 | |
---|
[571] | 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 | /// |
---|
[955] | 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. |
---|
[571] | 128 | /// |
---|
| 129 | /// \param inputList List of Detections to be sorted. |
---|
| 130 | /// \param parameter The name of the parameter to be sorted |
---|
[955] | 131 | /// on. Options are listed in the param.hh file |
---|
[571] | 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 | |
---|
[955] | 136 | bool OK = false, reverseSort=(parameter[0]=='-'); |
---|
| 137 | std::string checkParam; |
---|
| 138 | if(reverseSort) checkParam = parameter.substr(1); |
---|
| 139 | else checkParam = parameter; |
---|
[571] | 140 | for(int i=0;i<numSortingParamOptions;i++) |
---|
[955] | 141 | OK = OK || (checkParam == sortingParamOptions[i]); |
---|
[571] | 142 | if(!OK){ |
---|
[913] | 143 | DUCHAMPERROR("SortDetections", "Invalid sorting parameter: " << parameter << " -- Not doing any sorting."); |
---|
[571] | 144 | return; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | bool isGood = true; |
---|
[955] | 148 | if(checkParam!="zvalue" && checkParam!="pflux" && checkParam!="snr" && checkParam!="xvalue" && checkParam!="yvalue"){ |
---|
[623] | 149 | for(size_t i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS(); |
---|
[571] | 150 | } |
---|
| 151 | |
---|
| 152 | if(isGood){ |
---|
| 153 | |
---|
[954] | 154 | std::vector<Detection> sorted; |
---|
| 155 | std::vector<Detection>::iterator det; |
---|
| 156 | |
---|
[955] | 157 | if(checkParam=="xvalue" || checkParam=="yvalue" || checkParam=="zvalue" || checkParam=="iflux" || checkParam=="pflux" || checkParam=="snr"){ |
---|
[954] | 158 | |
---|
| 159 | std::multimap<float, size_t> complist; |
---|
| 160 | std::multimap<float, size_t>::iterator comp; |
---|
| 161 | size_t ct=0; |
---|
[955] | 162 | float reverse = reverseSort ? -1. : 1.; |
---|
[954] | 163 | for (det=inputList.begin();det<inputList.end();det++){ |
---|
[955] | 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++)); |
---|
[954] | 170 | } |
---|
| 171 | |
---|
| 172 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
| 173 | sorted.push_back(inputList[comp->second]); |
---|
| 174 | |
---|
[571] | 175 | } |
---|
[955] | 176 | else if(checkParam=="ra" || checkParam=="dec" || checkParam=="vel" || checkParam=="w50"){ |
---|
[571] | 177 | |
---|
[954] | 178 | std::multimap<double, size_t> complist; |
---|
| 179 | std::multimap<double, size_t>::iterator comp; |
---|
| 180 | size_t ct=0; |
---|
[955] | 181 | double reverse = reverseSort ? -1. : 1.; |
---|
[954] | 182 | for (det=inputList.begin();det<inputList.end();det++){ |
---|
[955] | 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++)); |
---|
[954] | 187 | } |
---|
| 188 | |
---|
| 189 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
| 190 | sorted.push_back(inputList[comp->second]); |
---|
| 191 | |
---|
| 192 | } |
---|
[571] | 193 | |
---|
| 194 | inputList.clear(); |
---|
[954] | 195 | for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det ); |
---|
[571] | 196 | sorted.clear(); |
---|
[954] | 197 | |
---|
[571] | 198 | } |
---|
| 199 | |
---|
| 200 | } |
---|
| 201 | |
---|
[378] | 202 | } |
---|