"""Functions for creating gate filters (masks) which can be used it variouscorrections routines in Py-ART."""fromcopyimportdeepcopyimportnumpyasnpfrom..configimportget_field_name,get_metadatafrom..utilimporttexture_along_ray
[docs]defmoment_based_gate_filter(radar,ncp_field=None,rhv_field=None,refl_field=None,min_ncp=0.5,min_rhv=None,min_refl=-20.,max_refl=100.0):""" Create a filter which removes undesired gates based on moments. Creates a gate filter in which the following gates are excluded: * Gates where the instrument is transitioning between sweeps. * Gates where the reflectivity is outside the interval min_refl, max_refl. * Gates where the normalized coherent power is below min_ncp. * Gates where the cross correlation ratio is below min_rhi. Using the default parameter this filtering is disabled. * Gates where any of the above three fields are masked or contain invalid values (NaNs or infs). * If any of these three fields do not exist in the radar that fields filter criteria is not applied. Parameters ---------- radar : Radar Radar object from which the gate filter will be built. refl_field, ncp_field, rhv_field : str Names of the radar fields which contain the reflectivity, normalized coherent power (signal quality index) and cross correlation ratio (RhoHV) from which the gate filter will be created using the above criteria. A value of None for any of these parameters will use the default field name as defined in the Py-ART configuration file. min_ncp, min_rhv : float Minimum values for the normalized coherence power and cross correlation ratio. Gates in these fields below these limits as well as gates which are masked or contain invalid values will be excluded and not used in calculation which use the filter. A value of None will disable filtering based upon the given field including removing masked or gates with an invalid value. To disable the thresholding but retain the masked and invalid filter set the parameter to a value below the lowest value in the field. min_refl, max_refl : float Minimum and maximum values for the reflectivity. Gates outside of this interval as well as gates which are masked or contain invalid values will be excluded and not used in calculation which use this filter. A value or None for one of these parameters will disable the minimum or maximum filtering but retain the other. A value of None for both of these values will disable all filtering based upon the reflectivity including removing masked or gates with an invalid value. To disable the interval filtering but retain the masked and invalid filter set the parameters to values above and below the lowest and greatest values in the reflectivity field. Returns ------- gatefilter : :py:class:`GateFilter` A gate filter based upon the described criteria. This can be used as a gatefilter parameter to various functions in pyart.correct. """# parse the field parametersifrefl_fieldisNone:refl_field=get_field_name('reflectivity')ifncp_fieldisNone:ncp_field=get_field_name('normalized_coherent_power')ifrhv_fieldisNone:rhv_field=get_field_name('cross_correlation_ratio')# filter gates based upon field parametersgatefilter=GateFilter(radar)gatefilter.exclude_transition()if(min_ncpisnotNone)and(ncp_fieldinradar.fields):gatefilter.exclude_below(ncp_field,min_ncp)gatefilter.exclude_masked(ncp_field)gatefilter.exclude_invalid(ncp_field)if(min_rhvisnotNone)and(rhv_fieldinradar.fields):gatefilter.exclude_below(rhv_field,min_rhv)gatefilter.exclude_masked(rhv_field)gatefilter.exclude_invalid(rhv_field)ifrefl_fieldinradar.fields:ifmin_reflisnotNone:gatefilter.exclude_below(refl_field,min_refl)gatefilter.exclude_masked(refl_field)gatefilter.exclude_invalid(refl_field)ifmax_reflisnotNone:gatefilter.exclude_above(refl_field,max_refl)gatefilter.exclude_masked(refl_field)gatefilter.exclude_invalid(refl_field)returngatefilter
[docs]defmoment_and_texture_based_gate_filter(radar,zdr_field=None,rhv_field=None,phi_field=None,refl_field=None,textzdr_field=None,textrhv_field=None,textphi_field=None,textrefl_field=None,wind_size=7,max_textphi=20.,max_textrhv=0.3,max_textzdr=2.85,max_textrefl=8.,min_rhv=0.6):""" Create a filter which removes undesired gates based on texture of moments. Creates a gate filter in which the following gates are excluded: * Gates where the instrument is transitioning between sweeps. * Gates where RhoHV is below min_rhv * Gates where the PhiDP texture is above max_textphi. * Gates where the RhoHV texture is above max_textrhv. * Gates where the ZDR texture is above max_textzdr * Gates where the reflectivity texture is above max_textrefl * If any of the thresholds is not set or the field (RhoHV, ZDR, PhiDP, reflectivity) do not exist in the radar the filter is not applied. Parameters ---------- radar : Radar Radar object from which the gate filter will be built. zdr_field, rhv_field, phi_field, refl_field : str Names of the radar fields which contain the differential reflectivity, cross correlation ratio, differential phase and reflectivity from which the textures will be computed. A value of None for any of these parameters will use the default field name as defined in the Py-ART configuration file. textzdr_field, textrhv_field, textphi_field, textrefl_field : str Names of the radar fields given to the texture of the differential reflectivity, texture of the cross correlation ratio, texture of differential phase and texture of reflectivity. A value of None for any of these parameters will use the default field name as defined in the Py-ART configuration file. wind_size : int Size of the moving window used to compute the ray texture. max_textphi, max_textrhv, max_textzdr, max_textrefl : float Maximum value for the texture of the differential phase, texture of RhoHV, texture of Zdr and texture of reflectivity. Gates in these fields above these limits as well as gates which are masked or contain invalid values will be excluded and not used in calculation which use the filter. A value of None will disable filtering based upon the given field including removing masked or gates with an invalid value. To disable the thresholding but retain the masked and invalid filter set the parameter to a value above the highest value in the field. min_rhv : float Minimum value for the RhoHV. Gates below this limits as well as gates which are masked or contain invalid values will be excluded and not used in calculation which use the filter. A value of None will disable filtering based upon the given field including removing masked or gates with an invalid value. To disable the thresholding but retain the masked and invalid filter set the parameter to a value below the lowest value in the field. Returns ------- gatefilter : :py:class:`GateFilter` A gate filter based upon the described criteria. This can be used as a gatefilter parameter to various functions in pyart.correct. """# parse the field parametersifrefl_fieldisNone:refl_field=get_field_name('reflectivity')ifzdr_fieldisNone:zdr_field=get_field_name('differential_reflectivity')ifrhv_fieldisNone:rhv_field=get_field_name('cross_correlation_ratio')ifphi_fieldisNone:phi_field=get_field_name('uncorrected_differential_phase')iftextrefl_fieldisNone:textrefl_field=get_field_name('reflectivity_texture')iftextzdr_fieldisNone:textzdr_field=get_field_name('differential_reflectivity_texture')iftextrhv_fieldisNone:textrhv_field=get_field_name('cross_correlation_ratio_texture')iftextphi_fieldisNone:textphi_field=get_field_name('differential_phase_texture')# make deepcopy of input radar (we do not want to modify the original)radar_aux=deepcopy(radar)# compute the textures of the moments and add them into radar objectif(max_textphiisnotNone)and(phi_fieldinradar_aux.fields):textphi=texture_along_ray(radar_aux,phi_field,wind_size=wind_size)tphi=get_metadata(textphi_field)tphi['data']=textphiradar_aux.add_field(textphi_field,tphi)if(max_textrhvisnotNone)and(rhv_fieldinradar_aux.fields):textrho=texture_along_ray(radar_aux,rhv_field,wind_size=wind_size)trhv=get_metadata(textrhv_field)trhv['data']=textrhoradar_aux.add_field(textrhv_field,trhv)if(max_textzdrisnotNone)and(zdr_fieldinradar_aux.fields):textzdr=texture_along_ray(radar_aux,zdr_field,wind_size=wind_size)tzdr=get_metadata(textzdr_field)tzdr['data']=textzdrradar_aux.add_field(textzdr_field,tzdr)if(max_textreflisnotNone)and(refl_fieldinradar_aux.fields):textrefl=texture_along_ray(radar_aux,refl_field,wind_size=wind_size)trefl=get_metadata(textrefl_field)trefl['data']=textreflradar_aux.add_field(textrefl_field,trefl)# filter gates based upon field parametersgatefilter=GateFilter(radar_aux)gatefilter.exclude_transition()if(min_rhvisnotNone)and(rhv_fieldinradar_aux.fields):gatefilter.exclude_below(rhv_field,min_rhv)gatefilter.exclude_masked(rhv_field)gatefilter.exclude_invalid(rhv_field)if(max_textphiisnotNone)and(textphi_fieldinradar_aux.fields):gatefilter.exclude_above(textphi_field,max_textphi)gatefilter.exclude_masked(textphi_field)gatefilter.exclude_invalid(textphi_field)if(max_textrhvisnotNone)and(textrhv_fieldinradar_aux.fields):gatefilter.exclude_above(textrhv_field,max_textrhv)gatefilter.exclude_masked(textrhv_field)gatefilter.exclude_invalid(textrhv_field)if(max_textzdrisnotNone)and(textzdr_fieldinradar_aux.fields):gatefilter.exclude_above(textzdr_field,max_textzdr)gatefilter.exclude_masked(textzdr_field)gatefilter.exclude_invalid(textzdr_field)if(max_textreflisnotNone)and(textrefl_fieldinradar_aux.fields):gatefilter.exclude_above(textrefl_field,max_textrefl)gatefilter.exclude_masked(textrefl_field)gatefilter.exclude_invalid(textrefl_field)returngatefilter
[docs]deftemp_based_gate_filter(radar,temp_field=None,min_temp=0.,thickness=400.,beamwidth=None):""" Create a filter which removes undesired gates based on temperature. Used primarily to filter out the melting layer and gates above it. Parameters ---------- radar : Radar Radar object from which the gate filter will be built. temp_field : str Name of the radar field which contains the temperature. A value of None for will use the default field name as defined in the Py-ART configuration file. min_temp : float Minimum value for the temperature in degrees. Gates below this limits as well as gates which are masked or contain invalid values will be excluded and not used in calculation which use the filter. A value of None will disable filtering based upon the field including removing masked or gates with an invalid value. To disable the thresholding but retain the masked and invalid filter set the parameter to a value below the lowest value in the field. thickness : float The estimated thickness of the melting layer in m. beamwidth : float The radar antenna 3 dB beamwidth [deg]. Returns ------- gatefilter : :py:class:`GateFilter` A gate filter based upon the described criteria. This can be used as a gatefilter parameter to various functions in pyart.correct. """# parse the field parametersiftemp_fieldisNone:temp_field=get_field_name('temperature')# make deepcopy of input radar (we do not want to modify the original)radar_aux=deepcopy(radar)# filter gates based upon field parametersgatefilter=GateFilter(radar_aux)if(min_tempisnotNone)and(temp_fieldinradar_aux.fields):gatefilter.exclude_below(temp_field,min_temp)gatefilter.exclude_masked(temp_field)gatefilter.exclude_invalid(temp_field)deltar=radar.range['data'][1]-radar.range['data'][0]ifbeamwidthisnotNone:beam_rad=beamwidth*np.pi/180.ifthicknessisnotNone:temp=radar_aux.fields[temp_field]temp['data']=np.ma.masked_where(gatefilter.gate_excluded==1,temp['data'])forrayinrange(radar_aux.nrays):gate_h_ray=radar_aux.gate_altitude['data'][ray,:]# index of first excluded gateind_r=np.where(gatefilter.gate_excluded[ray,:]==1)[0]ifind_r.size>0:# some gates are excluded: find the maximum heightind_r=ind_r[0]ifbeamwidthisNone:hmax=gate_h_ray[ind_r]-thicknesselse:# consider also the radar volume# maximum altitude at the end of the volumeifind_r<radar_aux.ngates-2:hmax=((gate_h_ray[ind_r]+gate_h_ray[ind_r+1])/2.-thickness)else:hmax=gate_h_ray[ind_r]-thicknessbeam_radius=((radar.range['data'][ind_r]+deltar/2.)*beam_rad/2.)delta_h=(beam_radius*np.cos(radar.elevation['data'][ray]*np.pi/180.))hmax-=delta_hind_hmax=np.where(radar_aux.gate_altitude['data'][ray,:]>hmax)[0]ifind_hmax.size>0:ind_hmax=ind_hmax[0]temp['data'][ray,ind_hmax:]=np.ma.maskedradar_aux.add_field(temp_field,temp,replace_existing=True)gatefilter=GateFilter(radar_aux)gatefilter.exclude_masked(temp_field)returngatefilter
[docs]defiso0_based_gate_filter(radar,iso0_field=None,max_h_iso0=0.,thickness=400.,beamwidth=None):""" Create a filter which removes undesired gates based height over the iso0. Used primarily to filter out the melting layer and gates above it. Parameters ---------- radar : Radar Radar object from which the gate filter will be built. iso0_field : str Name of the radar field which contains the height relative to the iso0. A value of None for will use the default field name as defined in the Py-ART configuration file. max_h_iso0 : float Maximum height relative to the iso0 in m. Gates below this limits as well as gates which are masked or contain invalid values will be excluded and not used in calculation which use the filter. A value of None will disable filtering based upon the field including removing masked or gates with an invalid value. To disable the thresholding but retain the masked and invalid filter set the parameter to a value below the lowest value in the field. thickness : float The estimated thickness of the melting layer in m. beamwidth : float The radar antenna 3 dB beamwidth [deg]. Returns ------- gatefilter : :py:class:`GateFilter` A gate filter based upon the described criteria. This can be used as a gatefilter parameter to various functions in pyart.correct. """# parse the field parametersifiso0_fieldisNone:iso0_field=get_field_name('height_over_iso0')# make deepcopy of input radar (we do not want to modify the original)radar_aux=deepcopy(radar)# filter gates based upon field parametersgatefilter=GateFilter(radar_aux)if(max_h_iso0isnotNone)and(iso0_fieldinradar_aux.fields):gatefilter.exclude_above(iso0_field,max_h_iso0)gatefilter.exclude_masked(iso0_field)gatefilter.exclude_invalid(iso0_field)deltar=radar.range['data'][1]-radar.range['data'][0]ifbeamwidthisnotNone:beam_rad=beamwidth*np.pi/180.ifthicknessisnotNone:iso0=radar_aux.fields[iso0_field]iso0['data']=np.ma.masked_where(gatefilter.gate_excluded==1,iso0['data'])forrayinrange(radar_aux.nrays):gate_h_ray=radar_aux.gate_altitude['data'][ray,:]# index of first excluded gateind_r=np.where(gatefilter.gate_excluded[ray,:]==1)[0]ifind_r.size>0:# some gates are excluded: find the maximum heightind_r=ind_r[0]ifbeamwidthisNone:hmax=gate_h_ray[ind_r]-thicknesselse:# consider also the radar volume# maximum altitude at the end of the volumeifind_r<radar_aux.ngates-2:hmax=((gate_h_ray[ind_r]+gate_h_ray[ind_r+1])/2.-thickness)else:hmax=gate_h_ray[ind_r]-thicknessbeam_radius=((radar.range['data'][ind_r]+deltar/2.)*beam_rad/2.)delta_h=(beam_radius*np.cos(radar.elevation['data'][ray]*np.pi/180.))hmax-=delta_hind_hmax=np.where(radar_aux.gate_altitude['data'][ray,:]>hmax)[0]ifind_hmax.size>0:ind_hmax=ind_hmax[0]iso0['data'][ray,ind_hmax:]=np.ma.maskedradar_aux.add_field(iso0_field,iso0,replace_existing=True)gatefilter=GateFilter(radar_aux)gatefilter.exclude_masked(iso0_field)returngatefilter
[docs]classGateFilter(object):""" A class for building a boolean arrays for filtering gates based on a set of condition typically based on the values in the radar fields. These filter can be used in various algorithms and calculations within Py-ART. See :py:func:`pyart.correct.GateFilter.exclude_below` for method parameter details. Parameters ---------- radar : Radar Radar object from which gate filter will be build. exclude_based : bool, optional True, the default and suggested method, will begin with all gates included and then use the exclude methods to exclude gates based on conditions. False will begin with all gates excluded from which a set of gates to include should be set using the include methods. Examples -------- >>> import pyart >>> radar = pyart.io.read('radar_file.nc') >>> gatefilter = pyart.correct.GateFilter(radar) >>> gatefilter.exclude_below('reflectivity', 10) >>> gatefilter.exclude_below('normalized_coherent_power', 0.75) """def__init__(self,radar,exclude_based=True):""" initialize """self._radar=radarshape=(radar.nrays,radar.ngates)ifexclude_based:# start with all gates included, exclude gates based on a set# of rules using the exclude_ methods.self._gate_excluded=np.zeros(shape,dtype=np.bool_)else:# start with all gates excluded, include gates based on a set# of rules using the include_ methods.self._gate_excluded=np.ones(shape,dtype=np.bool_)# Implemetation is based on marking excluded gates stored in the private# _gate_excluded attribute. The gate_included attribute can be found# by taking the ones complement of gates_included.
[docs]defcopy(self):""" Return a copy of the gatefilter. """a=GateFilter(self._radar)a._gate_excluded=self._gate_excluded.copy()returna
@propertydefgate_included(self):""" Boolean array indicating if a gate should be included in a calculation. Elements marked True indicate the corresponding gate should be include. Those marked False should be excluded. This is read-only attribute, any changes to the array will NOT be reflected in gate_excluded and will be lost when the attribute is accessed again. """return~self._gate_excluded.copy()@propertydefgate_excluded(self):""" Boolean array indicating if a gate should be excluded from a calculation. Elements marked True indicate the corresponding gate should be excluded. Those marked False should be included. This is read-only attribute, any changes to the array will NOT be reflected in gate_included and will be lost when the attribute is accessed again. """returnself._gate_excluded.copy()def_get_fdata(self,field):""" Check that the field exists and retrieve field data. """self._radar.check_field_exists(field)returnself._radar.fields[field]['data']def_merge(self,marked,op,exclude_masked):""" Merge an array of marked gates with the exclude array. """# exclude masked elements in marked by replacing them with the value# of the exclude_masked flag. This does nothing if marked is a# non-masked array.ifexclude_maskednotin[True,False]:raiseValueError("exclude_masked must be 'True' or 'False'")marked=np.ma.filled(marked,exclude_masked)# merge array of marked gates with existing excluded gates# using the specified operation.ifop=='or':self._gate_excluded=np.logical_or(self._gate_excluded,marked)elifop=='and':self._gate_excluded=np.logical_and(self._gate_excluded,marked)elifop=='new':self._gate_excluded=markedelse:raiseValueError("invalid 'op' parameter: ",op)return#################### exclude methods ####################
[docs]defexclude_transition(self,trans_value=1,exclude_masked=True,op='or'):""" Exclude all gates in rays marked as in transition between sweeps. Exclude all gates in rays marked as "in transition" by the antenna_transition attribute of the radar used to construct the filter. If no antenna transition information is available no gates are excluded. Parameters ---------- trans_value : int, optional Value used in the antenna transition data to indicate that the instrument was between sweeps (in transition) during the collection of a specific ray. Typically a value of 1 is used to indicate this transition and the default can be used in these cases. exclude_masked : bool, optional True to filter masked values in antenna_transition if the data is a masked array, False to include any masked values. op : {'and', 'or', 'new'} Operation to perform when merging the existing set of excluded gates with the excluded gates from the current operation. 'and' will perform a logical AND operation, 'or' a logical OR, and 'new' will replace the existing excluded gates with the one generated here. 'or', the default for exclude methods, is typically desired when building up a set of conditions for excluding gates where the desired effect is to exclude gates which meet any of the conditions. 'and', the default for include methods, is typically desired when building up a set of conditions where the desired effect is to include gates which meet any of the conditions. Note that the 'and' method MAY results in including gates which have previously been excluded because they were masked or invalid. """marked=np.zeros_like(self._gate_excluded)ifself._radar.antenna_transitionisnotNone:transition_data=self._radar.antenna_transition['data']in_transition=transition_data==trans_valuemarked[in_transition]=Truereturnself._merge(marked,op,exclude_masked)
[docs]defexclude_below(self,field,value,exclude_masked=True,op='or',inclusive=False):""" Exclude gates where a given field is below a given value. Parameters ---------- field : str Name of field compared against the value. value : float Gates with a value below this value in the specified field will be marked for exclusion in the filter. exclude_masked : bool, optional True to filter masked values in the specified field if the data is a masked array, False to include any masked values. op : {'and', 'or', 'new'} Operation to perform when merging the existing set of excluded gates with the excluded gates from the current operation. 'and' will perform a logical AND operation, 'or' a logical OR, and 'new' will replace the existing excluded gates with the one generated here. 'or', the default for exclude methods, is typically desired when building up a set of conditions for excluding gates where the desired effect is to exclude gates which meet any of the conditions. 'and', the default for include methods, is typically desired when building up a set of conditions where the desired effect is to include gates which meet any of the conditions. Note that the 'and' method MAY results in including gates which have previously been excluded because they were masked or invalid. inclusive : bool Indicates whether the specified value should also be excluded. """ifinclusive:marked=self._get_fdata(field)<=valueelse:marked=self._get_fdata(field)<valuereturnself._merge(marked,op,exclude_masked)
[docs]defexclude_above(self,field,value,exclude_masked=True,op='or',inclusive=False):""" Exclude gates where a given field is above a given value. """ifinclusive:marked=self._get_fdata(field)>=valueelse:marked=self._get_fdata(field)>valuereturnself._merge(marked,op,exclude_masked)
[docs]defexclude_inside(self,field,v1,v2,exclude_masked=True,op='or',inclusive=True):""" Exclude gates where a given field is inside a given interval. """ifv2<v1:(v1,v2)=(v2,v1)fdata=self._get_fdata(field)ifinclusive:marked=(fdata>=v1)&(fdata<=v2)else:marked=(fdata>v1)&(fdata<v2)returnself._merge(marked,op,exclude_masked)
[docs]defexclude_outside(self,field,v1,v2,exclude_masked=True,op='or',inclusive=False):""" Exclude gates where a given field is outside a given interval. """ifv2<v1:(v1,v2)=(v2,v1)fdata=self._get_fdata(field)ifinclusive:marked=(fdata<=v1)|(fdata>=v2)else:marked=(fdata<v1)|(fdata>v2)returnself._merge(marked,op,exclude_masked)
[docs]defexclude_equal(self,field,value,exclude_masked=True,op='or'):""" Exclude gates where a given field is equal to a value. """marked=(self._get_fdata(field)==value)returnself._merge(marked,op,exclude_masked)
[docs]defexclude_not_equal(self,field,value,exclude_masked=True,op='or'):""" Exclude gates where a given field is not equal to a value. """marked=(self._get_fdata(field)!=value)returnself._merge(marked,op,exclude_masked)
[docs]defexclude_all(self):""" Exclude all gates. """self._gate_excluded=np.ones_like(self._gate_excluded)return
[docs]defexclude_none(self):""" Exclude no gates, include all gates. """self._gate_excluded=np.zeros_like(self._gate_excluded)return
[docs]defexclude_masked(self,field,exclude_masked=True,op='or'):""" Exclude gates where a given field is masked. """marked=np.ma.getmaskarray(self._get_fdata(field))returnself._merge(marked,op,exclude_masked)
[docs]defexclude_invalid(self,field,exclude_masked=True,op='or'):""" Exclude gates where an invalid value occurs in a field (NaNs or infs). """marked=~np.isfinite(self._get_fdata(field))returnself._merge(marked,op,exclude_masked)
[docs]defexclude_gates(self,mask,exclude_masked=True,op='or'):""" Exclude gates where a given mask is equal True. Parameters ---------- mask : numpy array Boolean numpy array with same shape as a field array. exclude_masked : bool, optional True to filter masked values in the specified mask if it is a masked array, False to include any masked values. op : {'and', 'or', 'new'} Operation to perform when merging the existing set of excluded gates with the excluded gates from the current operation. 'and' will perform a logical AND operation, 'or' a logical OR, and 'new' will replace the existing excluded gates with the one generated here. 'or', the default for exclude methods, is typically desired when building up a set of conditions for excluding gates where the desired effect is to exclude gates which meet any of the conditions. 'and', the default for include methods, is typically desired when building up a set of conditions where the desired effect is to include gates which meet any of the conditions. Note that the 'and' method MAY results in including gates which have previously been excluded because they were masked or invalid. """fdata=next(iter(self._radar.fields.values()))['data']ifmask.shape!=fdata.shape:raiseValueError("mask array must be the same size as a field.")marked=np.array(mask,dtype='bool')returnself._merge(marked,op,exclude_masked)
[docs]definclude_not_transition(self,trans_value=0,exclude_masked=True,op='and'):""" Include all gates in rays not marked as in transition between sweeps. Include all gates in rays not marked as "in transition" by the antenna_transition attribute of the radar used to construct the filter. If no antenna transition information is available all gates are included. Parameters ---------- trans_value : int, optional Value used in the antenna transition data to indicate that the instrument is not between sweeps (in transition) during the collection of a specific ray. Typically a value of 0 is used to indicate no transition and the default can be used in these cases. exclude_masked : bool, optional True to filter masked values in antenna_transition if the data is a masked array, False to include any masked values. op : {'and', 'or', 'new'} Operation to perform when merging the existing set of excluded gates with the excluded gates from the current operation. 'and' will perform a logical AND operation, 'or' a logical OR, and 'new' will replace the existing excluded gates with the one generated here. 'or', the default for exclude methods, is typically desired when building up a set of conditions for excluding gates where the desired effect is to exclude gates which meet any of the conditions. 'and', the default for include methods, is typically desired when building up a set of conditions where the desired effect is to include gates which meet any of the conditions. Note that the 'or' method MAY results in excluding gates which have previously been included. """ifself._radar.antenna_transitionisNone:include=np.ones_like(self._gate_excluded)# include all gateselse:include=np.zeros_like(self._gate_excluded)transition_data=self._radar.antenna_transition['data']not_in_transition=(transition_data==trans_value)include[not_in_transition]=Truereturnself._merge(~include,op,exclude_masked)
[docs]definclude_below(self,field,value,exclude_masked=True,op='and',inclusive=False):""" Include gates where a given field is below a given value. """ifinclusive:marked=self._get_fdata(field)<=valueelse:marked=self._get_fdata(field)<valueself._merge(~marked,op,exclude_masked)
[docs]definclude_above(self,field,value,exclude_masked=True,op='and',inclusive=False):""" Include gates where a given field is above a given value. """ifinclusive:marked=self._get_fdata(field)>=valueelse:marked=self._get_fdata(field)>valueself._merge(~marked,op,exclude_masked)
[docs]definclude_inside(self,field,v1,v2,exclude_masked=True,op='and',inclusive=True):""" Include gates where a given field is inside a given interval. """ifv2<v1:(v1,v2)=(v2,v1)fdata=self._get_fdata(field)ifinclusive:marked=(fdata>=v1)&(fdata<=v2)else:marked=(fdata>v1)&(fdata<v2)returnself._merge(~marked,op,exclude_masked)
[docs]definclude_outside(self,field,v1,v2,exclude_masked=True,op='and',inclusive=False):""" Include gates where a given field is outside a given interval. """ifv2<v1:(v1,v2)=(v2,v1)fdata=self._get_fdata(field)ifinclusive:marked=(fdata<=v1)|(fdata>=v2)else:marked=(fdata<v1)|(fdata>v2)returnself._merge(~marked,op,exclude_masked)
[docs]definclude_equal(self,field,value,exclude_masked=True,op='and'):""" Include gates where a given field is equal to a value. """marked=(self._get_fdata(field)==value)returnself._merge(~marked,op,exclude_masked)
[docs]definclude_not_equal(self,field,value,exclude_masked=True,op='and'):""" Include gates where a given field is not equal to a value. """marked=(self._get_fdata(field)!=value)returnself._merge(~marked,op,exclude_masked)
[docs]definclude_all(self):""" Include all gates. """self._gate_excluded=np.zeros_like(self._gate_excluded)
[docs]definclude_none(self):""" Include no gates, exclude all gates. """self._gate_excluded=np.ones_like(self._gate_excluded)
[docs]definclude_not_masked(self,field,exclude_masked=True,op='and'):""" Include gates where a given field in not masked. """marked=np.ma.getmaskarray(self._get_fdata(field))returnself._merge(marked,op,exclude_masked)
[docs]definclude_valid(self,field,exclude_masked=True,op='and'):""" Include gates where a valid value occurs in a field (not NaN or inf). """marked=np.isfinite(self._get_fdata(field))returnself._merge(~marked,op,exclude_masked)
[docs]definclude_gates(self,mask,exclude_masked=True,op='and'):""" Include gates where a given mask is equal True. Parameters ---------- mask : numpy array Boolean numpy array with same shape as a field array. exclude_masked : bool, optional True to filter masked values in the specified mask if it is a masked array, False to include any masked values. op : {'and', 'or', 'new'} Operation to perform when merging the existing set of excluded gates with the excluded gates from the current operation. 'and' will perform a logical AND operation, 'or' a logical OR, and 'new' will replace the existing excluded gates with the one generated here. 'or', the default for exclude methods, is typically desired when building up a set of conditions for excluding gates where the desired effect is to exclude gates which meet any of the conditions. 'and', the default for include methods, is typically desired when building up a set of conditions where the desired effect is to include gates which meet any of the conditions. Note that the 'or' method MAY results in excluding gates which have previously been included. """fdata=next(iter(self._radar.fields.values()))['data']ifmask.shape!=fdata.shape:raiseValueError("Mask array must be the same size as a field.")marked=~np.array(mask,dtype='bool')returnself._merge(marked,op,exclude_masked)