[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 3085 2015-11-25 06:35:48Z TakeshiNakazato $
|
---|
[297] | 30 | //#---------------------------------------------------------------------------
|
---|
[881] | 31 | #ifndef STLINEFINDER_H
|
---|
| 32 | #define STLINEFINDER_H
|
---|
[297] | 33 |
|
---|
[3085] | 34 | // ASAP
|
---|
| 35 | #include "ScantableWrapper.h"
|
---|
| 36 | #include "Scantable.h"
|
---|
| 37 |
|
---|
[297] | 38 | // STL
|
---|
| 39 | #include <vector>
|
---|
| 40 | #include <list>
|
---|
| 41 | #include <utility>
|
---|
| 42 | #include <exception>
|
---|
| 43 |
|
---|
| 44 | // AIPS++
|
---|
| 45 | #include <casa/aips.h>
|
---|
| 46 | #include <casa/Exceptions/Error.h>
|
---|
| 47 | #include <casa/Arrays/Vector.h>
|
---|
| 48 | #include <casa/Utilities/Assert.h>
|
---|
| 49 | #include <casa/Utilities/CountedPtr.h>
|
---|
| 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> >
|
---|
[3029] | 64 | &newlines, std::list<std::pair<int, int> > &lines_list);
|
---|
[344] | 65 |
|
---|
| 66 | // extend all line ranges to the point where a value stored in the
|
---|
| 67 | // specified vector changes (e.g. value-mean change its sign)
|
---|
| 68 | // This operation is necessary to include line wings, which are below
|
---|
| 69 | // the detection threshold. If lines becomes adjacent, they are
|
---|
| 70 | // merged together. Any masked channel stops the extension
|
---|
[352] | 71 | static void searchForWings(std::list<std::pair<int, int> > &newlines,
|
---|
| 72 | const casa::Vector<casa::Int> &signs,
|
---|
| 73 | const casa::Vector<casa::Bool> &mask,
|
---|
[3029] | 74 | const std::pair<int,int> &edge);
|
---|
[352] | 75 | protected:
|
---|
[881] | 76 |
|
---|
[343] | 77 | // An auxiliary object function to test whether two lines have a non-void
|
---|
| 78 | // intersection
|
---|
| 79 | class IntersectsWith : public std::unary_function<pair<int,int>, bool> {
|
---|
| 80 | std::pair<int,int> line1; // range of the first line
|
---|
| 81 | // start channel and stop+1
|
---|
| 82 | public:
|
---|
[1353] | 83 | explicit IntersectsWith(const std::pair<int,int> &in_line1);
|
---|
[343] | 84 | // return true if line2 intersects with line1 with at least one
|
---|
| 85 | // common channel, and false otherwise
|
---|
[3029] | 86 | bool operator()(const std::pair<int,int> &line2) const;
|
---|
[343] | 87 | };
|
---|
| 88 |
|
---|
| 89 | // An auxiliary object function to build a union of several lines
|
---|
| 90 | // to account for a possibility of merging the nearby lines
|
---|
| 91 | class BuildUnion {
|
---|
| 92 | std::pair<int,int> temp_line; // range of the first line
|
---|
| 93 | // start channel and stop+1
|
---|
| 94 | public:
|
---|
[1353] | 95 | explicit BuildUnion(const std::pair<int,int> &line1);
|
---|
[343] | 96 | // update temp_line with a union of temp_line and new_line
|
---|
| 97 | // provided there is no gap between the lines
|
---|
[3029] | 98 | void operator()(const std::pair<int,int> &new_line);
|
---|
[343] | 99 | // return the result (temp_line)
|
---|
[3029] | 100 | const std::pair<int,int>& result() const;
|
---|
[343] | 101 | };
|
---|
[881] | 102 |
|
---|
[343] | 103 | // An auxiliary object function to test whether a specified line
|
---|
| 104 | // is at lower spectral channels (to preserve the order in the line list)
|
---|
| 105 | class LaterThan : public std::unary_function<pair<int,int>, bool> {
|
---|
| 106 | std::pair<int,int> line1; // range of the first line
|
---|
| 107 | // start channel and stop+1
|
---|
| 108 | public:
|
---|
[1353] | 109 | explicit LaterThan(const std::pair<int,int> &in_line1);
|
---|
[343] | 110 |
|
---|
| 111 | // return true if line2 should be placed later than line1
|
---|
| 112 | // in the ordered list (so, it is at greater channel numbers)
|
---|
[3029] | 113 | bool operator()(const std::pair<int,int> &line2) const;
|
---|
[881] | 114 | };
|
---|
| 115 |
|
---|
| 116 |
|
---|
[352] | 117 | };
|
---|
| 118 |
|
---|
| 119 | //
|
---|
| 120 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 121 |
|
---|
| 122 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 123 | //
|
---|
[881] | 124 | // STLineFinder - a class for automated spectral line search
|
---|
[352] | 125 | //
|
---|
| 126 | //
|
---|
| 127 |
|
---|
[881] | 128 | struct STLineFinder : protected LFLineListOperations {
|
---|
[3029] | 129 | STLineFinder();
|
---|
| 130 | virtual ~STLineFinder();
|
---|
[352] | 131 |
|
---|
[369] | 132 | // set the parameters controlling algorithm
|
---|
| 133 | // in_threshold a single channel threshold default is sqrt(3), which
|
---|
| 134 | // means together with 3 minimum channels at least 3 sigma
|
---|
| 135 | // detection criterion
|
---|
| 136 | // For bad baseline shape, in_threshold may need to be
|
---|
| 137 | // increased
|
---|
| 138 | // in_min_nchan minimum number of channels above the threshold to report
|
---|
| 139 | // a detection, default is 3
|
---|
| 140 | // in_avg_limit perform the averaging of no more than in_avg_limit
|
---|
| 141 | // adjacent channels to search for broad lines
|
---|
[881] | 142 | // Default is 8, but for a bad baseline shape this
|
---|
[369] | 143 | // parameter should be decreased (may be even down to a
|
---|
| 144 | // minimum of 1 to disable this option) to avoid
|
---|
| 145 | // confusing of baseline undulations with a real line.
|
---|
[881] | 146 | // Setting a very large value doesn't usually provide
|
---|
| 147 | // valid detections.
|
---|
[369] | 148 | // in_box_size the box size for running mean calculation. Default is
|
---|
| 149 | // 1./5. of the whole spectrum size
|
---|
[1644] | 150 | // in_noise_box the box size for off-line noise estimation (if working with
|
---|
| 151 | // local noise. Negative value means use global noise estimate
|
---|
| 152 | // Default is -1 (i.e. estimate using the whole spectrum)
|
---|
| 153 | // in_median true if median statistics is used as opposed to average of
|
---|
| 154 | // the lowest 80% of deviations (default)
|
---|
[369] | 155 | void setOptions(const casa::Float &in_threshold=sqrt(3.),
|
---|
| 156 | const casa::Int &in_min_nchan=3,
|
---|
| 157 | const casa::Int &in_avg_limit=8,
|
---|
[1644] | 158 | const casa::Float &in_box_size=0.2,
|
---|
| 159 | const casa::Float &in_noise_box=-1.,
|
---|
[3029] | 160 | const casa::Bool &in_median = casa::False);
|
---|
[369] | 161 |
|
---|
[2580] | 162 | void setDetailedOptions( const casa::Int &order=9 ) ;
|
---|
| 163 |
|
---|
[907] | 164 | // set the scan to work with (in_scan parameter)
|
---|
[3029] | 165 | void setScan(const ScantableWrapper &in_scan);
|
---|
[907] | 166 |
|
---|
[2012] | 167 | // set spectrum data to work with. this is a method to allow linefinder work
|
---|
| 168 | // without setting scantable for the purpose of using linefinder inside some
|
---|
| 169 | // method in scantable class. (Dec. 22, 2010 by W.Kawasaki)
|
---|
| 170 | void setData(const std::vector<float> &in_spectrum);
|
---|
| 171 |
|
---|
[907] | 172 | // search for spectral lines in a row specified by whichRow
|
---|
| 173 | // in_mask and in_edge parameters control channel rejection
|
---|
| 174 | // if in_edge has zero length, all channels chosen by mask will be used
|
---|
[352] | 175 | // if in_edge has one element only, it represents the number of
|
---|
| 176 | // channels to drop from both sides of the spectrum
|
---|
| 177 | // in_edge is introduced for convinience, although all functionality
|
---|
[881] | 178 | // can be achieved using a spectrum mask only
|
---|
| 179 | // Number of lines found is returned
|
---|
[907] | 180 | int findLines(const std::vector<bool> &in_mask,
|
---|
| 181 | const std::vector<int> &in_edge = std::vector<int>(),
|
---|
[3029] | 182 | const casa::uInt &whichRow = 0);
|
---|
[352] | 183 |
|
---|
| 184 | // get the mask to mask out all lines that have been found (default)
|
---|
| 185 | // if invert=true, only channels belong to lines will be unmasked
|
---|
| 186 | // Note: all channels originally masked by the input mask (in_mask
|
---|
| 187 | // in setScan) or dropped out by the edge parameter (in_edge
|
---|
| 188 | // in setScan) are still excluded regardless on the invert option
|
---|
[3029] | 189 | std::vector<bool> getMask(bool invert=false) const;
|
---|
[352] | 190 |
|
---|
[370] | 191 | // get range for all lines found. The same units as used in the scan
|
---|
[881] | 192 | // will be returned (e.g. velocity instead of channels).
|
---|
[3029] | 193 | std::vector<double> getLineRanges() const;
|
---|
[370] | 194 | // The same as getLineRanges, but channels are always used to specify
|
---|
| 195 | // the range
|
---|
[3029] | 196 | std::vector<int> getLineRangesInChannels() const;
|
---|
[368] | 197 | protected:
|
---|
| 198 | // auxiliary function to average adjacent channels and update the mask
|
---|
| 199 | // if at least one channel involved in summation is masked, all
|
---|
| 200 | // output channels will be masked. This function works with the
|
---|
| 201 | // spectrum and edge fields of this class, but updates the mask
|
---|
| 202 | // array specified, rather than the field of this class
|
---|
| 203 | // boxsize - a number of adjacent channels to average
|
---|
| 204 | void averageAdjacentChannels(casa::Vector<casa::Bool> &mask2update,
|
---|
[3029] | 205 | const casa::Int &boxsize);
|
---|
[369] | 206 |
|
---|
| 207 | // auxiliary function to fit and subtract a polynomial from the current
|
---|
[890] | 208 | // spectrum. It uses the Fitter class. This action is required before
|
---|
[369] | 209 | // reducing the spectral resolution if the baseline shape is bad
|
---|
| 210 | void subtractBaseline(const casa::Vector<casa::Bool> &temp_mask,
|
---|
[3029] | 211 | const casa::Int &order);
|
---|
[881] | 212 |
|
---|
[368] | 213 | // an auxiliary function to remove all lines from the list, except the
|
---|
| 214 | // strongest one (by absolute value). If the lines removed are real,
|
---|
[881] | 215 | // they will be find again at the next iteration. This approach
|
---|
| 216 | // increases the number of iterations required, but is able to remove
|
---|
[368] | 217 | // the sidelobes likely to occur near strong lines.
|
---|
| 218 | // Later a better criterion may be implemented, e.g.
|
---|
| 219 | // taking into consideration the brightness of different lines. Now
|
---|
[881] | 220 | // use the simplest solution
|
---|
[368] | 221 | // temp_mask - mask to work with (may be different from original mask as
|
---|
| 222 | // the lines previously found may be masked)
|
---|
| 223 | // lines2update - a list of lines to work with
|
---|
| 224 | // nothing will be done if it is empty
|
---|
| 225 | // max_box_nchan - channels in the running box for baseline filtering
|
---|
| 226 | void keepStrongestOnly(const casa::Vector<casa::Bool> &temp_mask,
|
---|
| 227 | std::list<std::pair<int, int> > &lines2update,
|
---|
[3029] | 228 | int max_box_nchan);
|
---|
[297] | 229 | private:
|
---|
[2943] | 230 | casa::CountedPtr<Scantable> scan; // the scan to work with
|
---|
[297] | 231 | casa::Vector<casa::Bool> mask; // associated mask
|
---|
| 232 | std::pair<int,int> edge; // start and stop+1 channels
|
---|
| 233 | // to work with
|
---|
[881] | 234 | casa::Float threshold; // detection threshold - the
|
---|
[297] | 235 | // minimal signal to noise ratio
|
---|
| 236 | casa::Double box_size; // size of the box for running
|
---|
| 237 | // mean calculations, specified as
|
---|
| 238 | // a fraction of the whole spectrum
|
---|
| 239 | int min_nchan; // A minimum number of consequtive
|
---|
| 240 | // channels, which should satisfy
|
---|
| 241 | // the detection criterion, to be
|
---|
| 242 | // a detection
|
---|
[369] | 243 | casa::Int avg_limit; // perform the averaging of no
|
---|
| 244 | // more than in_avg_limit
|
---|
| 245 | // adjacent channels to search
|
---|
| 246 | // for broad lines. see setOptions
|
---|
[370] | 247 | casa::uInt last_row_used; // the Row number specified
|
---|
| 248 | // during the last findLines call
|
---|
[297] | 249 | std::list<std::pair<int, int> > lines; // container of start and stop+1
|
---|
| 250 | // channels of the spectral lines
|
---|
| 251 | // a buffer for the spectrum
|
---|
| 252 | mutable casa::Vector<casa::Float> spectrum;
|
---|
[1644] | 253 |
|
---|
| 254 | // the box size for off-line noise estimation (if working with
|
---|
| 255 | // local noise. Negative value means use global noise estimate
|
---|
| 256 | // Default is -1 (i.e. estimate using the whole spectrum)
|
---|
| 257 | casa::Float itsNoiseBox;
|
---|
| 258 |
|
---|
| 259 | // true if median statistics is used as opposed to average of
|
---|
| 260 | // the lowest 80% of deviations (default)
|
---|
| 261 | casa::Bool itsUseMedian;
|
---|
[2012] | 262 |
|
---|
| 263 | // true if spectra and mask data should be provided from
|
---|
| 264 | // scantable (default = true)
|
---|
| 265 | bool useScantable;
|
---|
[2580] | 266 |
|
---|
| 267 | // shared object for nominal throw
|
---|
| 268 | casa::AipsError err ;
|
---|
[352] | 269 | };
|
---|
[297] | 270 |
|
---|
[352] | 271 | //
|
---|
| 272 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 273 |
|
---|
[297] | 274 | } // namespace asap
|
---|
[881] | 275 | #endif // #ifndef STLINEFINDER_H
|
---|