1 | //#--------------------------------------------------------------------------- |
---|
2 | //# SDLineFinder.h: A class for automated spectral line search |
---|
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 | //# |
---|
29 | //# $Id: |
---|
30 | //#--------------------------------------------------------------------------- |
---|
31 | #ifndef SDLINEFINDER_H |
---|
32 | #define SDLINEFINDER_H |
---|
33 | |
---|
34 | // STL |
---|
35 | #include <vector> |
---|
36 | #include <list> |
---|
37 | #include <utility> |
---|
38 | #include <exception> |
---|
39 | |
---|
40 | // boost |
---|
41 | #include <boost/python.hpp> |
---|
42 | |
---|
43 | // AIPS++ |
---|
44 | #include <casa/aips.h> |
---|
45 | #include <casa/Exceptions/Error.h> |
---|
46 | #include <casa/Arrays/Vector.h> |
---|
47 | #include <casa/Utilities/Assert.h> |
---|
48 | #include <casa/Utilities/CountedPtr.h> |
---|
49 | |
---|
50 | // ASAP |
---|
51 | #include "SDMemTableWrapper.h" |
---|
52 | #include "SDMemTable.h" |
---|
53 | |
---|
54 | namespace asap { |
---|
55 | |
---|
56 | // SDLineFinder - a class for automated spectral line search |
---|
57 | struct SDLineFinder { |
---|
58 | SDLineFinder() throw(); |
---|
59 | virtual ~SDLineFinder() throw(casa::AipsError); |
---|
60 | |
---|
61 | // set the scan to work with (in_scan parameter), associated mask (in_mask |
---|
62 | // parameter) and the edge channel rejection (in_edge parameter) |
---|
63 | // if in_edge has zero length, all channels chosen by mask will be used |
---|
64 | // if in_edge has one element only, it represents the number of |
---|
65 | // channels to drop from both sides of the spectrum |
---|
66 | // in_edge is introduced for convinience, although all functionality |
---|
67 | // can be achieved using a spectrum mask only |
---|
68 | void setScan(const SDMemTableWrapper &in_scan, |
---|
69 | const std::vector<bool> &in_mask, |
---|
70 | const boost::python::tuple &in_edge) throw(casa::AipsError); |
---|
71 | |
---|
72 | // search for spectral lines. Number of lines found is returned |
---|
73 | int findLines() throw(casa::AipsError); |
---|
74 | |
---|
75 | // get the mask to mask out all lines that have been found (default) |
---|
76 | // if invert=true, only channels belong to lines will be unmasked |
---|
77 | // Note: all channels originally masked by the input mask (in_mask |
---|
78 | // in setScan) or dropped out by the edge parameter (in_edge |
---|
79 | // in setScan) are still excluded regardless on the invert option |
---|
80 | std::vector<bool> getMask(bool invert=false) const throw(casa::AipsError); |
---|
81 | |
---|
82 | // get range for all lines found. If defunits is true (default), the |
---|
83 | // same units as used in the scan will be returned (e.g. velocity |
---|
84 | // instead of channels). If defunits is false, channels will be returned |
---|
85 | std::vector<int> getLineRanges(bool defunits=true) |
---|
86 | const throw(casa::AipsError); |
---|
87 | |
---|
88 | protected: |
---|
89 | // supplementary function to control running mean calculations. |
---|
90 | // It adds a specified channel to the running mean box and |
---|
91 | // removes (ch-maxboxnchan+1)'th channel from there |
---|
92 | // Channels, for which the mask is false or index is beyond the |
---|
93 | // allowed range, are ignored |
---|
94 | void advanceRunningBox(int ch) throw(casa::AipsError); |
---|
95 | |
---|
96 | |
---|
97 | // test a channel against current running mean & rms |
---|
98 | // if channel specified is masked out or beyond the allowed indexes, |
---|
99 | // false is returned |
---|
100 | casa::Bool testChannel(int ch) throw(std::exception, casa::AipsError); |
---|
101 | |
---|
102 | // process a channel: update curline and is_detected before and |
---|
103 | // add a new line to the list, if necessary using processCurLine() |
---|
104 | void processChannel(int ch) throw(casa::AipsError); |
---|
105 | |
---|
106 | // process the interval of channels stored in curline |
---|
107 | // if it satisfies the criterion, add this interval as a new line |
---|
108 | void processCurLine() throw(casa::AipsError); |
---|
109 | |
---|
110 | private: |
---|
111 | casa::CountedConstPtr<SDMemTable> scan; // the scan to work with |
---|
112 | casa::Vector<casa::Bool> mask; // associated mask |
---|
113 | std::pair<int,int> edge; // start and stop+1 channels |
---|
114 | // to work with |
---|
115 | casa::Float threshold; // detection threshold - the |
---|
116 | // minimal signal to noise ratio |
---|
117 | casa::Double box_size; // size of the box for running |
---|
118 | // mean calculations, specified as |
---|
119 | // a fraction of the whole spectrum |
---|
120 | int min_nchan; // A minimum number of consequtive |
---|
121 | // channels, which should satisfy |
---|
122 | // the detection criterion, to be |
---|
123 | // a detection |
---|
124 | std::list<std::pair<int, int> > lines; // container of start and stop+1 |
---|
125 | // channels of the spectral lines |
---|
126 | // statistics for running mean filtering |
---|
127 | casa::Float sum; // sum of fluxes |
---|
128 | casa::Float sumsq; // sum of squares of fluxes |
---|
129 | int box_chan_cntr; // actual number of channels in the box |
---|
130 | int max_box_nchan; // maximum allowed number of channels in the box |
---|
131 | // (calculated from boxsize and actual spectrum size) |
---|
132 | // a buffer for the spectrum |
---|
133 | mutable casa::Vector<casa::Float> spectrum; |
---|
134 | |
---|
135 | // temporary line edge channels and flag, which is True if the line |
---|
136 | // was detected in the previous channels. |
---|
137 | std::pair<int,int> cur_line; |
---|
138 | casa::Bool is_detected_before; |
---|
139 | }; |
---|
140 | } // namespace asap |
---|
141 | #endif // #ifndef SDLINEFINDER_H |
---|