Line data Source code
1 : //# FlagAgentClipping.cc: This file contains the implementation of the FlagAgentClipping class.
2 : //#
3 : //# CASA - Common Astronomy Software Applications (http://casa.nrao.edu/)
4 : //# Copyright (C) Associated Universities, Inc. Washington DC, USA 2011, All rights reserved.
5 : //# Copyright (C) European Southern Observatory, 2011, All rights reserved.
6 : //#
7 : //# This library is free software; you can redistribute it and/or
8 : //# modify it under the terms of the GNU Lesser General Public
9 : //# License as published by the Free software Foundation; either
10 : //# version 2.1 of the License, or (at your option) any later version.
11 : //#
12 : //# This library is distributed in the hope that it will be useful,
13 : //# but WITHOUT ANY WARRANTY, without even the implied warranty of
14 : //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : //# Lesser General Public License for more details.
16 : //#
17 : //# You should have received a copy of the GNU Lesser General Public
18 : //# License along with this library; if not, write to the Free Software
19 : //# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 : //# MA 02111-1307 USA
21 : //# $Id: $
22 :
23 : #include <flagging/Flagging/FlagAgentClipping.h>
24 :
25 : using namespace casacore;
26 : namespace casa { //# NAMESPACE CASA - BEGIN
27 :
28 89 : FlagAgentClipping::FlagAgentClipping(FlagDataHandler *dh, Record config, Bool writePrivateFlagCube, Bool flag):
29 89 : FlagAgentBase(dh,config,IN_ROWS,writePrivateFlagCube,flag)
30 : {
31 89 : setAgentParameters(config);
32 :
33 : // Request loading polarization map to FlagDataHandler
34 89 : flagDataHandler_p->setMapPolarizations(true);
35 89 : }
36 :
37 178 : FlagAgentClipping::~FlagAgentClipping()
38 : {
39 : // Compiler automagically calls FlagAgentBase::~FlagAgentBase()
40 178 : }
41 :
42 : void
43 89 : FlagAgentClipping::setAgentParameters(Record config)
44 : {
45 89 : logger_p->origin(LogOrigin(agentName_p,__FUNCTION__,WHERE));
46 :
47 : int exists;
48 :
49 : // First check basic configuration to see what modules are plugged in the check
50 89 : exists = config.fieldNumber ("clipzeros");
51 89 : if (exists >= 0)
52 : {
53 82 : if( config.type(exists) != TpBool )
54 : {
55 0 : throw( AipsError ( "Parameter 'clipzeros' must be of type 'bool'" ) );
56 : }
57 :
58 82 : clipzeros_p = config.asBool("clipzeros");
59 : }
60 : else
61 : {
62 7 : clipzeros_p = false;
63 : }
64 :
65 89 : *logger_p << logLevel_p << " clipzeros is " << clipzeros_p << LogIO::POST;
66 :
67 :
68 89 : exists = config.fieldNumber ("clipoutside");
69 89 : if (exists >= 0)
70 : {
71 74 : if( config.type(exists) != TpBool )
72 : {
73 0 : throw( AipsError ( "Parameter 'clipoutside' must be of type 'bool'" ) );
74 : }
75 :
76 74 : clipoutside_p = config.asBool("clipoutside");
77 : }
78 : else
79 : {
80 15 : clipoutside_p = true;
81 : }
82 :
83 89 : *logger_p << logLevel_p << " clipoutside is " << clipoutside_p << LogIO::POST;
84 :
85 89 : String datacol = "";
86 89 : weightcol_p = false;
87 89 : exists = config.fieldNumber ("datacolumn");
88 89 : if (exists >= 0)
89 : {
90 89 : datacol = config.asString("datacolumn");
91 : }
92 :
93 89 : exists = config.fieldNumber ("clipminmax");
94 89 : if (exists >= 0)
95 : {
96 74 : if( config.type(exists) != TpArrayDouble && config.type(exists) != TpArrayFloat && config.type(exists) != TpArrayInt )
97 : {
98 0 : throw( AipsError ( "Parameter 'clipminmax' must be of type 'array double' : [minval,maxval]" ) );
99 : }
100 :
101 74 : Array<Double> cliprange = config.asArrayDouble("clipminmax");
102 74 : if (cliprange.size() == 2)
103 : {
104 74 : Bool deleteIt = false;
105 74 : clipmin_p = cliprange.getStorage(deleteIt)[0];
106 74 : clipmax_p = cliprange.getStorage(deleteIt)[1];
107 74 : *logger_p << logLevel_p << " clipmin is " << clipmin_p << LogIO::POST;
108 74 : *logger_p << logLevel_p << " clipmax is " << clipmax_p << LogIO::POST;
109 :
110 74 : clipminmax_p = true;
111 :
112 : // for the moment, treat WEIGHT as if it was WEIGHT_SPECTRUM,
113 : // so the clipminmax given by the user is divided by the
114 : // number of channels inside preProcessBuffer()
115 74 : if (datacol.compare("WEIGHT") == 0)
116 : {
117 5 : weightcol_p = true;
118 5 : original_clipmin_p = clipmin_p;
119 5 : original_clipmax_p = clipmax_p;
120 : }
121 : }
122 : else
123 : {
124 0 : clipminmax_p = false;
125 0 : *logger_p << logLevel_p << " clipminmax range not provided" << LogIO::POST;
126 : }
127 :
128 74 : }
129 : else
130 : {
131 15 : clipminmax_p = false;
132 15 : *logger_p << logLevel_p << " clipminmax range not provided" << LogIO::POST;
133 : }
134 :
135 :
136 : // Then, point to the function covering for all the modules selected
137 89 : if (clipminmax_p)
138 : {
139 74 : if (clipoutside_p)
140 : {
141 68 : if (clipzeros_p)
142 : {
143 17 : checkVis_p = &FlagAgentClipping::checkVisForClipOutsideAndZeros;
144 17 : *logger_p << logLevel_p << " Clipping outside [" << clipmin_p << "," << clipmax_p << "], plus NaNs and zeros" << LogIO::POST;
145 : }
146 : else
147 : {
148 51 : checkVis_p = &FlagAgentClipping::checkVisForClipOutside;
149 51 : *logger_p << logLevel_p << " Clipping outside [" << clipmin_p << "," << clipmax_p << "], plus NaNs" << LogIO::POST;
150 : }
151 : }
152 : else
153 : {
154 6 : if (clipzeros_p)
155 : {
156 0 : checkVis_p = &FlagAgentClipping::checkVisForClipInsideAndZeros;
157 0 : *logger_p << logLevel_p << " Clipping inside [" << clipmin_p << "," << clipmax_p << "], plus NaNs and zeros" << LogIO::POST;
158 : }
159 : else
160 : {
161 6 : checkVis_p = &FlagAgentClipping::checkVisForClipInside;
162 6 : *logger_p << logLevel_p << " Clipping inside [" << clipmin_p << "," << clipmax_p << "], plus NaNs" << LogIO::POST;
163 : }
164 : }
165 : }
166 : else
167 : {
168 15 : if (clipzeros_p)
169 : {
170 11 : checkVis_p = &FlagAgentClipping::checkVisForNaNsAndZeros;
171 11 : *logger_p << logLevel_p << " Clipping range not provided, clipping NaNs and zeros" << LogIO::POST;
172 : }
173 : else
174 : {
175 4 : checkVis_p = &FlagAgentClipping::checkVisForNaNs;
176 4 : *logger_p << logLevel_p << " Clipping range not provided, clipping only NaNs" << LogIO::POST;
177 : }
178 : }
179 :
180 178 : return;
181 89 : }
182 :
183 : // jagonzal: With the new WEIGHT/SIGMA convention it is not longer
184 : // necessary divide the clipping range between the number of channels
185 : /*
186 : void
187 : FlagAgentClipping::preProcessBuffer(const vi::VisBuffer2 &visBuffer)
188 : {
189 :
190 : logger_p->origin(LogOrigin(agentName_p,__FUNCTION__,WHERE));
191 :
192 : // Only done if datacolumn is WEIGHT
193 : if (weightcol_p)
194 : {
195 : Int nChannels;
196 : nChannels = visBuffer.nChannels();
197 : clipmin_p = original_clipmin_p/nChannels;
198 : clipmax_p = original_clipmax_p/nChannels;
199 : }
200 : *logger_p << LogIO::DEBUG1 << " clipmin is " << clipmin_p << LogIO::POST;
201 : *logger_p << LogIO::DEBUG1 << " clipmax is " << clipmax_p << LogIO::POST;
202 :
203 : }
204 : */
205 :
206 : bool
207 547992 : FlagAgentClipping::computeInRowFlags(const vi::VisBuffer2 &/*visBuffer*/, VisMapper &visibilities,
208 : FlagMapper &flags, uInt row)
209 : {
210 : // Get flag cube size
211 : Float visExpression;
212 : Int nPols,nChannels,nTimesteps;
213 547992 : visibilities.shape(nPols, nChannels, nTimesteps);
214 :
215 31321976 : for (uInt chan_i=0;chan_i<(uInt) nChannels;chan_i++)
216 : {
217 104836113 : for (uInt pol_i=0;pol_i<(uInt) nPols;pol_i++)
218 : {
219 74062129 : visExpression = visibilities(pol_i,chan_i,row);
220 74062129 : if ((*this.*checkVis_p)(visExpression))
221 : {
222 4180206 : flags.applyFlag(pol_i,chan_i,row);
223 4180206 : visBufferFlags_p += 1;
224 : }
225 : }
226 : }
227 :
228 547992 : return false;
229 : }
230 :
231 : bool
232 21255209 : FlagAgentClipping::checkVisForClipOutside(Float visExpression)
233 : {
234 21255209 : if ((visExpression > clipmax_p) or (visExpression < clipmin_p))
235 : {
236 2636153 : return true;
237 : }
238 : else
239 : {
240 18619056 : return isNaN(visExpression);
241 : }
242 : }
243 :
244 : bool
245 14271452 : FlagAgentClipping::checkVisForClipInside(Float visExpression)
246 : {
247 14271452 : if ((visExpression <= clipmax_p) and (visExpression >= clipmin_p))
248 : {
249 2091 : return true;
250 : }
251 : else
252 : {
253 14269361 : return isNaN(visExpression);
254 : }
255 : }
256 :
257 : bool
258 4658812 : FlagAgentClipping::checkVisForClipOutsideAndZeros(Float visExpression)
259 : {
260 4658812 : if ((visExpression > clipmax_p) or (visExpression < clipmin_p))
261 : {
262 518359 : return true;
263 : }
264 : else
265 : {
266 4140453 : return isNaNOrZero(visExpression);
267 : }
268 : }
269 :
270 : bool
271 0 : FlagAgentClipping::checkVisForClipInsideAndZeros(Float visExpression)
272 : {
273 0 : if ((visExpression <= clipmax_p) and (visExpression >= clipmin_p))
274 : {
275 0 : return true;
276 : }
277 : else
278 : {
279 0 : return isNaNOrZero(visExpression);
280 : }
281 : }
282 :
283 : bool
284 10175232 : FlagAgentClipping::checkVisForNaNs(Float visExpression)
285 : {
286 10175232 : return isNaN(visExpression);
287 : }
288 :
289 : bool
290 23701424 : FlagAgentClipping::checkVisForNaNsAndZeros(Float visExpression)
291 : {
292 23701424 : return isNaNOrZero(visExpression);
293 : }
294 :
295 :
296 :
297 : } //# NAMESPACE CASA - END
298 :
299 :
|