[297] | 1 | //#--------------------------------------------------------------------------- |
---|
[881] | 2 | //# STLineFinder.h: A class for automated spectral line search |
---|
[297] | 3 | //#--------------------------------------------------------------------------- |
---|
| 4 | //# Copyright (C) 2004 |
---|
| 5 | //# ATNF |
---|
| 6 | //# |
---|
| 7 | //# This program is free software; you can redistribute it and/or modify it |
---|
| 8 | //# under the terms of the GNU General Public License as published by the Free |
---|
| 9 | //# Software Foundation; either version 2 of the License, or (at your option) |
---|
| 10 | //# any later version. |
---|
| 11 | //# |
---|
| 12 | //# This program is distributed in the hope that it will be useful, but |
---|
| 13 | //# WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
| 15 | //# Public License for more details. |
---|
| 16 | //# |
---|
| 17 | //# You should have received a copy of the GNU General Public License along |
---|
| 18 | //# with this program; if not, write to the Free Software Foundation, Inc., |
---|
| 19 | //# 675 Massachusetts Ave, Cambridge, MA 02139, USA. |
---|
| 20 | //# |
---|
| 21 | //# Correspondence concerning this software should be addressed as follows: |
---|
| 22 | //# Internet email: Malte.Marquarding@csiro.au |
---|
| 23 | //# Postal address: Malte Marquarding, |
---|
| 24 | //# Australia Telescope National Facility, |
---|
| 25 | //# P.O. Box 76, |
---|
| 26 | //# Epping, NSW, 2121, |
---|
| 27 | //# AUSTRALIA |
---|
| 28 | //# |
---|
[890] | 29 | //# $Id: STLineFinder.h 1757 2010-06-09 09:03:06Z KanaSugimoto $ |
---|
[297] | 30 | //#--------------------------------------------------------------------------- |
---|
[881] | 31 | #ifndef STLINEFINDER_H |
---|
| 32 | #define STLINEFINDER_H |
---|
[297] | 33 | |
---|
| 34 | // STL |
---|
| 35 | #include <vector> |
---|
| 36 | #include <list> |
---|
| 37 | #include <utility> |
---|
| 38 | #include <exception> |
---|
| 39 | |
---|
| 40 | // AIPS++ |
---|
| 41 | #include <casa/aips.h> |
---|
| 42 | #include <casa/Exceptions/Error.h> |
---|
| 43 | #include <casa/Arrays/Vector.h> |
---|
| 44 | #include <casa/Utilities/Assert.h> |
---|
| 45 | #include <casa/Utilities/CountedPtr.h> |
---|
| 46 | |
---|
| 47 | // ASAP |
---|
[881] | 48 | #include "ScantableWrapper.h" |
---|
| 49 | #include "Scantable.h" |
---|
[297] | 50 | |
---|
| 51 | namespace asap { |
---|
| 52 | |
---|
[352] | 53 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 54 | // |
---|
| 55 | // LFLineListOperations - a class incapsulating operations with line lists |
---|
| 56 | // The LF prefix stands for Line Finder |
---|
| 57 | // |
---|
[297] | 58 | |
---|
[881] | 59 | struct LFLineListOperations { |
---|
[331] | 60 | // concatenate two lists preserving the order. If two lines appear to |
---|
[881] | 61 | // be adjacent or have a non-void intersection, they are joined into |
---|
[343] | 62 | // the new line |
---|
[344] | 63 | static void addNewSearchResult(const std::list<std::pair<int, int> > |
---|
| 64 | &newlines, std::list<std::pair<int, int> > &lines_list) |
---|
[331] | 65 | throw(casa::AipsError); |
---|
[344] | 66 | |
---|
| 67 | // extend all line ranges to the point where a value stored in the |
---|
| 68 | // specified vector changes (e.g. value-mean change its sign) |
---|
| 69 | // This operation is necessary to include line wings, which are below |
---|
| 70 | // the detection threshold. If lines becomes adjacent, they are |
---|
| 71 | // merged together. Any masked channel stops the extension |
---|
[352] | 72 | static void searchForWings(std::list<std::pair<int, int> > &newlines, |
---|
| 73 | const casa::Vector<casa::Int> &signs, |
---|
| 74 | const casa::Vector<casa::Bool> &mask, |
---|
| 75 | const std::pair<int,int> &edge) |
---|
[344] | 76 | throw(casa::AipsError); |
---|
[352] | 77 | protected: |
---|
[881] | 78 | |
---|
[343] | 79 | // An auxiliary object function to test whether two lines have a non-void |
---|
| 80 | // intersection |
---|
| 81 | class IntersectsWith : public std::unary_function<pair<int,int>, bool> { |
---|
| 82 | std::pair<int,int> line1; // range of the first line |
---|
| 83 | // start channel and stop+1 |
---|
| 84 | public: |
---|
[1353] | 85 | explicit IntersectsWith(const std::pair<int,int> &in_line1); |
---|
[343] | 86 | // return true if line2 intersects with line1 with at least one |
---|
| 87 | // common channel, and false otherwise |
---|
| 88 | bool operator()(const std::pair<int,int> &line2) const throw(); |
---|
| 89 | }; |
---|
| 90 | |
---|
| 91 | // An auxiliary object function to build a union of several lines |
---|
| 92 | // to account for a possibility of merging the nearby lines |
---|
| 93 | class BuildUnion { |
---|
| 94 | std::pair<int,int> temp_line; // range of the first line |
---|
| 95 | // start channel and stop+1 |
---|
| 96 | public: |
---|
[1353] | 97 | explicit BuildUnion(const std::pair<int,int> &line1); |
---|
[343] | 98 | // update temp_line with a union of temp_line and new_line |
---|
| 99 | // provided there is no gap between the lines |
---|
| 100 | void operator()(const std::pair<int,int> &new_line) throw(); |
---|
| 101 | // return the result (temp_line) |
---|
| 102 | const std::pair<int,int>& result() const throw(); |
---|
| 103 | }; |
---|
[881] | 104 | |
---|
[343] | 105 | // An auxiliary object function to test whether a specified line |
---|
| 106 | // is at lower spectral channels (to preserve the order in the line list) |
---|
| 107 | class LaterThan : public std::unary_function<pair<int,int>, bool> { |
---|
| 108 | std::pair<int,int> line1; // range of the first line |
---|
| 109 | // start channel and stop+1 |
---|
| 110 | public: |
---|
[1353] | 111 | explicit LaterThan(const std::pair<int,int> &in_line1); |
---|
[343] | 112 | |
---|
| 113 | // return true if line2 should be placed later than line1 |
---|
| 114 | // in the ordered list (so, it is at greater channel numbers) |
---|
| 115 | bool operator()(const std::pair<int,int> &line2) const throw(); |
---|
[881] | 116 | }; |
---|
| 117 | |
---|
| 118 | |
---|
[352] | 119 | }; |
---|
| 120 | |
---|
| 121 | // |
---|
| 122 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 123 | |
---|
| 124 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 125 | // |
---|
[881] | 126 | // STLineFinder - a class for automated spectral line search |
---|
[352] | 127 | // |
---|
| 128 | // |
---|
| 129 | |
---|
[881] | 130 | struct STLineFinder : protected LFLineListOperations { |
---|
| 131 | STLineFinder() throw(); |
---|
| 132 | virtual ~STLineFinder() throw(casa::AipsError); |
---|
[352] | 133 | |
---|
[369] | 134 | // set the parameters controlling algorithm |
---|
| 135 | // in_threshold a single channel threshold default is sqrt(3), which |
---|
| 136 | // means together with 3 minimum channels at least 3 sigma |
---|
| 137 | // detection criterion |
---|
| 138 | // For bad baseline shape, in_threshold may need to be |
---|
| 139 | // increased |
---|
| 140 | // in_min_nchan minimum number of channels above the threshold to report |
---|
| 141 | // a detection, default is 3 |
---|
| 142 | // in_avg_limit perform the averaging of no more than in_avg_limit |
---|
| 143 | // adjacent channels to search for broad lines |
---|
[881] | 144 | // Default is 8, but for a bad baseline shape this |
---|
[369] | 145 | // parameter should be decreased (may be even down to a |
---|
| 146 | // minimum of 1 to disable this option) to avoid |
---|
| 147 | // confusing of baseline undulations with a real line. |
---|
[881] | 148 | // Setting a very large value doesn't usually provide |
---|
| 149 | // valid detections. |
---|
[369] | 150 | // in_box_size the box size for running mean calculation. Default is |
---|
| 151 | // 1./5. of the whole spectrum size |
---|
[1757] | 152 | // in_noise_box the box size for off-line noise estimation (if working with |
---|
| 153 | // local noise. Negative value means use global noise estimate |
---|
| 154 | // Default is -1 (i.e. estimate using the whole spectrum) |
---|
| 155 | // in_median true if median statistics is used as opposed to average of |
---|
| 156 | // the lowest 80% of deviations (default) |
---|
[369] | 157 | void setOptions(const casa::Float &in_threshold=sqrt(3.), |
---|
| 158 | const casa::Int &in_min_nchan=3, |
---|
| 159 | const casa::Int &in_avg_limit=8, |
---|
[1757] | 160 | const casa::Float &in_box_size=0.2, |
---|
| 161 | const casa::Float &in_noise_box=-1., |
---|
| 162 | const casa::Bool &in_median = casa::False) throw(); |
---|
[369] | 163 | |
---|
[907] | 164 | // set the scan to work with (in_scan parameter) |
---|
| 165 | void setScan(const ScantableWrapper &in_scan) throw(casa::AipsError); |
---|
| 166 | |
---|
| 167 | // search for spectral lines in a row specified by whichRow |
---|
| 168 | // in_mask and in_edge parameters control channel rejection |
---|
| 169 | // if in_edge has zero length, all channels chosen by mask will be used |
---|
[352] | 170 | // if in_edge has one element only, it represents the number of |
---|
| 171 | // channels to drop from both sides of the spectrum |
---|
| 172 | // in_edge is introduced for convinience, although all functionality |
---|
[881] | 173 | // can be achieved using a spectrum mask only |
---|
| 174 | // Number of lines found is returned |
---|
[907] | 175 | int findLines(const std::vector<bool> &in_mask, |
---|
| 176 | const std::vector<int> &in_edge = std::vector<int>(), |
---|
| 177 | const casa::uInt &whichRow = 0) throw(casa::AipsError); |
---|
[352] | 178 | |
---|
| 179 | // get the mask to mask out all lines that have been found (default) |
---|
| 180 | // if invert=true, only channels belong to lines will be unmasked |
---|
| 181 | // Note: all channels originally masked by the input mask (in_mask |
---|
| 182 | // in setScan) or dropped out by the edge parameter (in_edge |
---|
| 183 | // in setScan) are still excluded regardless on the invert option |
---|
| 184 | std::vector<bool> getMask(bool invert=false) const throw(casa::AipsError); |
---|
| 185 | |
---|
[370] | 186 | // get range for all lines found. The same units as used in the scan |
---|
[881] | 187 | // will be returned (e.g. velocity instead of channels). |
---|
[370] | 188 | std::vector<double> getLineRanges() const throw(casa::AipsError); |
---|
| 189 | // The same as getLineRanges, but channels are always used to specify |
---|
| 190 | // the range |
---|
| 191 | std::vector<int> getLineRangesInChannels() const throw(casa::AipsError); |
---|
[368] | 192 | protected: |
---|
| 193 | // auxiliary function to average adjacent channels and update the mask |
---|
| 194 | // if at least one channel involved in summation is masked, all |
---|
| 195 | // output channels will be masked. This function works with the |
---|
| 196 | // spectrum and edge fields of this class, but updates the mask |
---|
| 197 | // array specified, rather than the field of this class |
---|
| 198 | // boxsize - a number of adjacent channels to average |
---|
| 199 | void averageAdjacentChannels(casa::Vector<casa::Bool> &mask2update, |
---|
| 200 | const casa::Int &boxsize) |
---|
| 201 | throw(casa::AipsError); |
---|
[369] | 202 | |
---|
| 203 | // auxiliary function to fit and subtract a polynomial from the current |
---|
[890] | 204 | // spectrum. It uses the Fitter class. This action is required before |
---|
[369] | 205 | // reducing the spectral resolution if the baseline shape is bad |
---|
| 206 | void subtractBaseline(const casa::Vector<casa::Bool> &temp_mask, |
---|
| 207 | const casa::Int &order) throw(casa::AipsError); |
---|
[881] | 208 | |
---|
[368] | 209 | // an auxiliary function to remove all lines from the list, except the |
---|
| 210 | // strongest one (by absolute value). If the lines removed are real, |
---|
[881] | 211 | // they will be find again at the next iteration. This approach |
---|
| 212 | // increases the number of iterations required, but is able to remove |
---|
[368] | 213 | // the sidelobes likely to occur near strong lines. |
---|
| 214 | // Later a better criterion may be implemented, e.g. |
---|
| 215 | // taking into consideration the brightness of different lines. Now |
---|
[881] | 216 | // use the simplest solution |
---|
[368] | 217 | // temp_mask - mask to work with (may be different from original mask as |
---|
| 218 | // the lines previously found may be masked) |
---|
| 219 | // lines2update - a list of lines to work with |
---|
| 220 | // nothing will be done if it is empty |
---|
| 221 | // max_box_nchan - channels in the running box for baseline filtering |
---|
| 222 | void keepStrongestOnly(const casa::Vector<casa::Bool> &temp_mask, |
---|
| 223 | std::list<std::pair<int, int> > &lines2update, |
---|
| 224 | int max_box_nchan) |
---|
| 225 | throw (casa::AipsError); |
---|
[297] | 226 | private: |
---|
[881] | 227 | casa::CountedConstPtr<Scantable> scan; // the scan to work with |
---|
[297] | 228 | casa::Vector<casa::Bool> mask; // associated mask |
---|
| 229 | std::pair<int,int> edge; // start and stop+1 channels |
---|
| 230 | // to work with |
---|
[881] | 231 | casa::Float threshold; // detection threshold - the |
---|
[297] | 232 | // minimal signal to noise ratio |
---|
| 233 | casa::Double box_size; // size of the box for running |
---|
| 234 | // mean calculations, specified as |
---|
| 235 | // a fraction of the whole spectrum |
---|
| 236 | int min_nchan; // A minimum number of consequtive |
---|
| 237 | // channels, which should satisfy |
---|
| 238 | // the detection criterion, to be |
---|
| 239 | // a detection |
---|
[369] | 240 | casa::Int avg_limit; // perform the averaging of no |
---|
| 241 | // more than in_avg_limit |
---|
| 242 | // adjacent channels to search |
---|
| 243 | // for broad lines. see setOptions |
---|
[370] | 244 | casa::uInt last_row_used; // the Row number specified |
---|
| 245 | // during the last findLines call |
---|
[297] | 246 | std::list<std::pair<int, int> > lines; // container of start and stop+1 |
---|
| 247 | // channels of the spectral lines |
---|
| 248 | // a buffer for the spectrum |
---|
| 249 | mutable casa::Vector<casa::Float> spectrum; |
---|
[1757] | 250 | |
---|
| 251 | // the box size for off-line noise estimation (if working with |
---|
| 252 | // local noise. Negative value means use global noise estimate |
---|
| 253 | // Default is -1 (i.e. estimate using the whole spectrum) |
---|
| 254 | casa::Float itsNoiseBox; |
---|
| 255 | |
---|
| 256 | // true if median statistics is used as opposed to average of |
---|
| 257 | // the lowest 80% of deviations (default) |
---|
| 258 | casa::Bool itsUseMedian; |
---|
[352] | 259 | }; |
---|
[297] | 260 | |
---|
[352] | 261 | // |
---|
| 262 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 263 | |
---|
[297] | 264 | } // namespace asap |
---|
[881] | 265 | #endif // #ifndef STLINEFINDER_H |
---|