LCOV - code coverage report
Current view: top level - flagging/Flagging - FlagAgentClipping.cc (source / functions) Hit Total Coverage
Test: ctest_coverage.info Lines: 84 95 88.4 %
Date: 2023-11-06 10:06:49 Functions: 10 11 90.9 %

          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         106 : FlagAgentClipping::FlagAgentClipping(FlagDataHandler *dh, Record config, Bool writePrivateFlagCube, Bool flag):
      29         106 :                                                 FlagAgentBase(dh,config,IN_ROWS,writePrivateFlagCube,flag)
      30             : {
      31         106 :         setAgentParameters(config);
      32             : 
      33             :         // Request loading polarization map to FlagDataHandler
      34         106 :         flagDataHandler_p->setMapPolarizations(true);
      35         106 : }
      36             : 
      37         212 : FlagAgentClipping::~FlagAgentClipping()
      38             : {
      39             :         // Compiler automagically calls FlagAgentBase::~FlagAgentBase()
      40         212 : }
      41             : 
      42             : void
      43         106 : FlagAgentClipping::setAgentParameters(Record config)
      44             : {
      45         106 :         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         106 :         exists = config.fieldNumber ("clipzeros");
      51         106 :         if (exists >= 0)
      52             :         {
      53          92 :                 if( config.type(exists) != TpBool )
      54             :                 {
      55           0 :                         throw( AipsError ( "Parameter 'clipzeros' must be of type 'bool'" ) );
      56             :                 }
      57             : 
      58          92 :                 clipzeros_p = config.asBool("clipzeros");
      59             :         }
      60             :         else
      61             :         {
      62          14 :                 clipzeros_p = false;
      63             :         }
      64             : 
      65         106 :         *logger_p << logLevel_p << " clipzeros is " << clipzeros_p << LogIO::POST;
      66             : 
      67             : 
      68         106 :         exists = config.fieldNumber ("clipoutside");
      69         106 :         if (exists >= 0)
      70             :         {
      71          78 :                 if( config.type(exists) != TpBool )
      72             :                 {
      73           0 :                         throw( AipsError ( "Parameter 'clipoutside' must be of type 'bool'" ) );
      74             :                 }
      75             : 
      76          78 :                 clipoutside_p = config.asBool("clipoutside");
      77             :         }
      78             :         else
      79             :         {
      80          28 :                 clipoutside_p = true;
      81             :         }
      82             : 
      83         106 :         *logger_p << logLevel_p << " clipoutside is " << clipoutside_p << LogIO::POST;
      84             : 
      85         212 :         String datacol = "";
      86         106 :         weightcol_p = false;
      87         106 :         exists = config.fieldNumber ("datacolumn");
      88         106 :         if (exists >= 0)
      89             :         {
      90         106 :                 datacol = config.asString("datacolumn");
      91             :         }
      92             : 
      93         106 :         exists = config.fieldNumber ("clipminmax");
      94         106 :         if (exists >= 0)
      95             :         {
      96          80 :                 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         160 :                 Array<Double> cliprange = config.asArrayDouble("clipminmax");
     102          80 :                 if (cliprange.size() == 2)
     103             :                 {
     104          80 :                         Bool deleteIt = false;
     105          80 :                         clipmin_p = cliprange.getStorage(deleteIt)[0];
     106          80 :                         clipmax_p = cliprange.getStorage(deleteIt)[1];
     107          80 :                         *logger_p << logLevel_p << " clipmin is " << clipmin_p << LogIO::POST;
     108          80 :                         *logger_p << logLevel_p << " clipmax is " << clipmax_p << LogIO::POST;
     109             : 
     110          80 :                         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          80 :                         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             :         }
     129             :         else
     130             :         {
     131          26 :                 clipminmax_p = false;
     132          26 :                 *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         106 :         if (clipminmax_p)
     138             :         {
     139          80 :                 if (clipoutside_p)
     140             :                 {
     141          74 :                         if (clipzeros_p)
     142             :                         {
     143          18 :                                 checkVis_p = &FlagAgentClipping::checkVisForClipOutsideAndZeros;
     144          18 :                                 *logger_p << logLevel_p << " Clipping outside [" <<  clipmin_p << "," <<  clipmax_p << "], plus NaNs and zeros" << LogIO::POST;
     145             :                         }
     146             :                         else
     147             :                         {
     148          56 :                                 checkVis_p = &FlagAgentClipping::checkVisForClipOutside;
     149          56 :                                 *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          26 :                 if (clipzeros_p)
     169             :                 {
     170          20 :                         checkVis_p = &FlagAgentClipping::checkVisForNaNsAndZeros;
     171          20 :                         *logger_p << logLevel_p << " Clipping range not provided, clipping NaNs and zeros" << LogIO::POST;
     172             :                 }
     173             :                 else
     174             :                 {
     175           6 :                         checkVis_p = &FlagAgentClipping::checkVisForNaNs;
     176           6 :                         *logger_p << logLevel_p << " Clipping range not provided, clipping only NaNs" << LogIO::POST;
     177             :                 }
     178             :         }
     179             : 
     180         212 :         return;
     181             : }
     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      747830 : 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      747830 :         visibilities.shape(nPols, nChannels, nTimesteps);
     214             : 
     215    44482254 :         for (uInt chan_i=0;chan_i<(uInt) nChannels;chan_i++)
     216             :         {
     217   154446757 :                 for (uInt pol_i=0;pol_i<(uInt) nPols;pol_i++)
     218             :                 {
     219   110712333 :                         visExpression = visibilities(pol_i,chan_i,row);
     220   110712333 :                         if ((*this.*checkVis_p)(visExpression))
     221             :                         {
     222     4718183 :                                 flags.applyFlag(pol_i,chan_i,row);
     223     4718183 :                                 visBufferFlags_p += 1;
     224             :                         }
     225             :                 }
     226             :         }
     227             : 
     228      747830 :         return false;
     229             : }
     230             : 
     231             : bool
     232    24563413 : FlagAgentClipping::checkVisForClipOutside(Float visExpression)
     233             : {
     234    24563413 :         if ((visExpression >  clipmax_p) or (visExpression <  clipmin_p))
     235             :         {
     236     3160831 :                 return true;
     237             :         }
     238             :         else
     239             :         {
     240    21402582 :                 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     4908412 : FlagAgentClipping::checkVisForClipOutsideAndZeros(Float visExpression)
     259             : {
     260     4908412 :         if ((visExpression >  clipmax_p) or (visExpression <  clipmin_p))
     261             :         {
     262      518367 :                 return true;
     263             :         }
     264             :         else
     265             :         {
     266     4390045 :                 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    10365744 : FlagAgentClipping::checkVisForNaNs(Float visExpression)
     285             : {
     286    10365744 :         return isNaN(visExpression);
     287             : }
     288             : 
     289             : bool
     290    56603312 : FlagAgentClipping::checkVisForNaNsAndZeros(Float visExpression)
     291             : {
     292    56603312 :         return isNaNOrZero(visExpression);
     293             : }
     294             : 
     295             : 
     296             : 
     297             : } //# NAMESPACE CASA - END
     298             : 
     299             : 

Generated by: LCOV version 1.16