source: trunk/src/SDLineFinder.h @ 344

Last change on this file since 344 was 344, checked in by vor010, 19 years ago

An algorithm to detect line wings, which are
below detection threshold, has been added

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
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
54namespace asap {
55
56// SDLineFinder  -  a class for automated spectral line search
57struct 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);
87protected:
88   // concatenate two lists preserving the order. If two lines appear to
89   // be adjacent or have a non-void intersection, they are joined into
90   // the new line
91   static void addNewSearchResult(const std::list<std::pair<int, int> >
92                  &newlines, std::list<std::pair<int, int> > &lines_list)
93                           throw(casa::AipsError);
94
95   // extend all line ranges to the point where a value stored in the
96   // specified vector changes (e.g. value-mean change its sign)
97   // This operation is necessary to include line wings, which are below
98   // the detection threshold. If lines becomes adjacent, they are
99   // merged together. Any masked channel stops the extension
100   void searchForWings(std::list<std::pair<int, int> > &newlines,
101                           const casa::Vector<casa::Int> &signs)
102                           throw(casa::AipsError);
103                           
104   // An auxiliary object function to test whether two lines have a non-void
105   // intersection
106   class IntersectsWith : public std::unary_function<pair<int,int>, bool> {
107       std::pair<int,int> line1;           // range of the first line
108                                           // start channel and stop+1
109   public:
110        IntersectsWith(const std::pair<int,int> &in_line1);
111        // return true if line2 intersects with line1 with at least one
112        // common channel, and false otherwise
113        bool operator()(const std::pair<int,int> &line2) const throw();
114   };
115
116   // An auxiliary object function to build a union of several lines
117   // to account for a possibility of merging the nearby lines
118   class BuildUnion {
119       std::pair<int,int> temp_line;       // range of the first line
120                                           // start channel and stop+1
121   public:
122        BuildUnion(const std::pair<int,int> &line1);
123        // update temp_line with a union of temp_line and new_line
124        // provided there is no gap between the lines
125        void operator()(const std::pair<int,int> &new_line) throw();
126        // return the result (temp_line)
127        const std::pair<int,int>& result() const throw();
128   };
129   
130   // An auxiliary object function to test whether a specified line
131   // is at lower spectral channels (to preserve the order in the line list)
132   class LaterThan : public std::unary_function<pair<int,int>, bool> {
133       std::pair<int,int> line1;           // range of the first line
134                                           // start channel and stop+1
135   public:
136        LaterThan(const std::pair<int,int> &in_line1);
137
138        // return true if line2 should be placed later than line1
139        // in the ordered list (so, it is at greater channel numbers)
140        bool operator()(const std::pair<int,int> &line2) const throw();
141   };
142   
143private:
144   casa::CountedConstPtr<SDMemTable> scan; // the scan to work with
145   casa::Vector<casa::Bool> mask;          // associated mask
146   std::pair<int,int> edge;                // start and stop+1 channels
147                                           // to work with
148   casa::Float threshold;                  // detection threshold - the
149                                           // minimal signal to noise ratio
150   casa::Double box_size;                  // size of the box for running
151                                           // mean calculations, specified as
152                                           // a fraction of the whole spectrum
153   int  min_nchan;                         // A minimum number of consequtive
154                                           // channels, which should satisfy
155                                           // the detection criterion, to be
156                                           // a detection
157   std::list<std::pair<int, int> > lines;  // container of start and stop+1
158                                           // channels of the spectral lines
159   // a buffer for the spectrum
160   mutable casa::Vector<casa::Float>  spectrum;
161
162};
163} // namespace asap
164#endif // #ifndef SDLINEFINDER_H
Note: See TracBrowser for help on using the repository browser.