1 | // |
---|
2 | // C++ Implimentation: RasterEdgeDetector |
---|
3 | // |
---|
4 | // Description: |
---|
5 | // |
---|
6 | // |
---|
7 | // Author: Takeshi Nakazato <takeshi.nakazato@nao.ac.jp>, (C) 2012 |
---|
8 | // |
---|
9 | // Copyright: See COPYING file that comes with this distribution |
---|
10 | // |
---|
11 | // |
---|
12 | #include <casa/Arrays/Vector.h> |
---|
13 | #include <casa/Arrays/ArrayMath.h> |
---|
14 | #include <casa/Arrays/ArrayIO.h> |
---|
15 | #include <casa/Containers/Record.h> |
---|
16 | //#include <casa/Utilities/GenSort.h> |
---|
17 | |
---|
18 | #include "RasterEdgeDetector.h" |
---|
19 | |
---|
20 | using namespace std ; |
---|
21 | using namespace casa ; |
---|
22 | |
---|
23 | namespace asap { |
---|
24 | |
---|
25 | RasterEdgeDetector::RasterEdgeDetector() |
---|
26 | : EdgeDetector() |
---|
27 | {} |
---|
28 | |
---|
29 | RasterEdgeDetector::~RasterEdgeDetector() |
---|
30 | {} |
---|
31 | |
---|
32 | Vector<uInt> RasterEdgeDetector::detect() |
---|
33 | { |
---|
34 | initDetect() ; |
---|
35 | |
---|
36 | detectGap() ; |
---|
37 | selection() ; |
---|
38 | |
---|
39 | return off_ ; |
---|
40 | } |
---|
41 | |
---|
42 | void RasterEdgeDetector::parseOption( const Record &option ) |
---|
43 | { |
---|
44 | os_.origin(LogOrigin( "RasterEdgeDetector", "parseOption", WHERE )) ; |
---|
45 | |
---|
46 | String name = "fraction" ; |
---|
47 | if ( option.isDefined( name ) ) { |
---|
48 | if ( option.dataType( name ) == TpString ) { |
---|
49 | String fstr = option.asString( name ) ; |
---|
50 | if ( fstr == "auto" ) { |
---|
51 | fraction_ = -1.0 ; // indicates "auto" |
---|
52 | } |
---|
53 | else { |
---|
54 | // should be "xx%" format |
---|
55 | fstr.rtrim( '%' ) ; |
---|
56 | fraction_ = String::toFloat( fstr ) * 0.01 ; |
---|
57 | } |
---|
58 | } |
---|
59 | else { |
---|
60 | fraction_ = option.asFloat( name ) ; |
---|
61 | } |
---|
62 | } |
---|
63 | else { |
---|
64 | fraction_ = 0.1 ; // default is 10% |
---|
65 | } |
---|
66 | name = "npts" ; |
---|
67 | if ( option.isDefined( name ) ) { |
---|
68 | npts_ = option.asInt( name ) ; |
---|
69 | } |
---|
70 | else { |
---|
71 | npts_ = -1 ; // default is -1 (use fraction_) |
---|
72 | } |
---|
73 | |
---|
74 | os_ << "OPTION SUMMARY: " << endl |
---|
75 | << " fraction=" << fraction_ << endl |
---|
76 | << " npts=" << npts_ << LogIO::POST ; |
---|
77 | } |
---|
78 | |
---|
79 | void RasterEdgeDetector::detectGap() |
---|
80 | { |
---|
81 | os_.origin(LogOrigin( "RasterEdgeDetector", "detectGap", WHERE )) ; |
---|
82 | |
---|
83 | uInt n = time_.nelements() ; |
---|
84 | Vector<Double> tdiff( n-1 ) ; |
---|
85 | for ( uInt i = 0 ; i < n-1 ; i++ ) { |
---|
86 | tdiff[i] = time_[i+1] - time_[i] ; |
---|
87 | } |
---|
88 | Double tmed = median( tdiff, False, True, False ) ; |
---|
89 | Double threshold = tmed * 5.0 ; |
---|
90 | uInt idx = 0 ; |
---|
91 | tempuInt_[idx++] = 0 ; |
---|
92 | for ( uInt i = 0 ; i < n-1 ; i++ ) { |
---|
93 | if ( tdiff[i] > threshold ) { |
---|
94 | tempuInt_[idx++] = i+1 ; |
---|
95 | } |
---|
96 | } |
---|
97 | if ( tempuInt_[idx-1] != n ) { |
---|
98 | tempuInt_[idx++] = n ; |
---|
99 | } |
---|
100 | gaplist_ = vectorFromTempStorage( idx ) ; |
---|
101 | } |
---|
102 | |
---|
103 | void RasterEdgeDetector::selection() |
---|
104 | { |
---|
105 | os_.origin(LogOrigin( "RasterEdgeDetector", "selection", WHERE )) ; |
---|
106 | |
---|
107 | uInt n = gaplist_.nelements() - 1 ; |
---|
108 | uInt idx = 0 ; |
---|
109 | for ( uInt i = 0 ; i < n ; i++ ) { |
---|
110 | uInt start = gaplist_[i] ; |
---|
111 | uInt end = gaplist_[i+1] ; |
---|
112 | selectionPerRow( idx, start, end ) ; |
---|
113 | } |
---|
114 | off_ = vectorFromTempStorage( idx ) ; |
---|
115 | //sort( off_, Sort::Ascending, Sort::QuickSort | Sort::NoDuplicates ) ; |
---|
116 | } |
---|
117 | |
---|
118 | void RasterEdgeDetector::selectionPerRow( uInt &idx, |
---|
119 | const uInt &start, |
---|
120 | const uint &end ) |
---|
121 | { |
---|
122 | os_.origin(LogOrigin( "RasterEdgeDetector", "selectionPerRow", WHERE )) ; |
---|
123 | |
---|
124 | uInt len = end - start ; |
---|
125 | uInt noff = numOff( len ) ; |
---|
126 | |
---|
127 | if ( len > noff * 2 ) { |
---|
128 | uInt n = start + noff ; |
---|
129 | for ( uInt i = start ; i < n ; i++ ) { |
---|
130 | tempuInt_[idx++] = i ; |
---|
131 | } |
---|
132 | n = end - noff ; |
---|
133 | for ( uInt i = n ; i < end ; i++ ) { |
---|
134 | tempuInt_[idx++] = i ; |
---|
135 | } |
---|
136 | } |
---|
137 | else { |
---|
138 | // add all elements to off position list |
---|
139 | for ( uInt i = start ; i < end ; i++ ) { |
---|
140 | tempuInt_[idx++] = i ; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | uInt RasterEdgeDetector::numOff( const uInt &n ) |
---|
146 | { |
---|
147 | uInt ret = 0 ; |
---|
148 | if ( npts_ > 0 ) { |
---|
149 | ret = min( (uInt)npts_, n ) ; |
---|
150 | } |
---|
151 | else if ( fraction_ < 0 ) { |
---|
152 | ret = optimumNumber( n ) ; |
---|
153 | } |
---|
154 | else { |
---|
155 | ret = int( n * fraction_ ) ; |
---|
156 | } |
---|
157 | return ((ret > 0) ? ret : 1) ; |
---|
158 | } |
---|
159 | |
---|
160 | uInt RasterEdgeDetector::optimumNumber( const uInt &n ) |
---|
161 | { |
---|
162 | return uInt( sqrt( n + 1 ) ) - 1 ; |
---|
163 | } |
---|
164 | |
---|
165 | } // namespace asap |
---|