pylawr.transform.temporal#
This class can be used to extrapolate a field based on two previous fields using template matching. |
|
Noise filter that dynamically determines an appropriate spatially independent noise threshold to subtract from the reflectivity field. |
pylawr.transform.temporal.Extrapolator#
- class pylawr.transform.temporal.Extrapolator(cut_percentage=0.15, correlation_threshold=0.5, max_timediff=600.0)[source]#
Bases:
Transformer,MemoryMixinThis class can be used to extrapolate a field based on two previous fields using template matching. The template matching finds similar areas between two fields. Based on the distance between similar pixels a vector of pixel movement between to time steps is calculated. With the vector the current field can be shifted to a field of a following time step.
- time#
timestep of fitted vector
- Type
numpy.datetime64
- correlation_threshold#
Minimal threshold of maximal correlation of match template between data arrays. Is the correlation lower than threshold, the vector of movement is set to zero.
- Type
- _abc_impl = <_abc._abc_data object>#
- static _check_grids(grid_now, grid_pre)[source]#
Checks if the grids of the arrays are equal and child of
pylawr.grid.cartesian.CartesianGrid
- _determine_timedelta(time, time_pre)[source]#
Get the timedelta between two data arrays.
- Parameters
time (
numpy.datetime64) – actual timestep.time_pre (
numpy.datetime64) – previous timestep.
- Returns
timedelta – Timedelta in seconds.
- Return type
numpy.timedelat64(seconds)
- calc_match_matrix(array, array_pre)[source]#
Calculates the correlation matrix based on match template between two arrays
- Parameters
array (
xarray.DataArray) – Data of analogous actual timestep.array_pre (
xarray.DataArray) – Data of analogous previous timestep.
- Returns
Correlation matrix
- Return type
numpy.ndarray(float)
- fit(array, array_pre, grid=None, *args, **kwargs)[source]#
Fits a vector of movement between to arrays, of e.g. reflectivity, based on match template.
- Parameters
array (
xarray.DataArray) – Data of analogous actual timestep.array_pre (
xarray.DataArray) – Data of analogous previous timestep.grid (
pylawr.grid.cartesian.CartesianGridor None, optional) – To fit the arrays they need to have the same grid specifications. If no grid is given, the grids from given arrays are used.
- property fitted#
- property time#
- to_xarray()[source]#
Serialize this filter’s parameters to an
xarray.Dataset- Returns
the filter’s parameters as dataset
- Return type
- transform(array, grid=None, time=None, *args, **kwargs)[source]#
Extrapolate the given given
arraywith a cartesiangridaccording to the fitted movement vector.- Parameters
array (
xarray.DataArray) – The array to operate ongrid (child of
pylawr.grid.base.BaseGridor None) – To extrapolate the array needs to have apylawr.grid.cartesian.CartesianGrid.time (
numpy.datetime64) – next time step to extrapolate toargs (sequence) – Further positional arguments
kwargs (dict) – Further keyword arguments
- Returns
The transformed array
- Return type
- property vector#
pylawr.transform.temporal.NoiseRemover#
- class pylawr.transform.temporal.NoiseRemover(noise_percentile=10, max_noise_percentile_tendency=0.006666666666666667, max_time_constant_noiselevel=10800, noiselevel_multiplier=1.03, remembrance_time=300, noiselevel=8e-07)[source]#
Bases:
Transformer,MemoryMixinNoise filter that dynamically determines an appropriate spatially independent noise threshold to subtract from the reflectivity field.
Assumptions:
reflectivity data is the raw measurement, i.e. no range dependency correction etc. has happened yet
- noise_percentile#
percentile of all sorted values to use for determination of a noise level. Defaults to
10which means \(10\%\).- Type
- remembrance_time#
time in seconds to do a moving average of older values. Defaults to 300 seconds, 5 minutes.
- Type
- max_noise_percentile_tendency#
maximum relative tendency per time of the actual
noise_percentilevalue. Defaults to \(\frac{20\%}{30s}\). If a higher relative tendency occurs, the previousnoiselevelwill be used for filtering.- Type
- max_time_constant_noiselevel#
maximum time in seconds where the
noiselevelcan be constant before it is reset to the default. Defaults to three hours.- Type
- noiselevel_constant_since#
the time where
noiselevellast changed- Type
- noiselevel_multiplier#
the noiselevel is multiplied with this constant value. Defaults to 1.03.
- Type
- times#
recorded threshold times
- thresholds#
recorded thresholds
- Type
- _abc_impl = <_abc._abc_data object>#
- _determine_noiselevel_from_array(array)[source]#
Calculate the noise level from given array. At the moment, the noise level is the noise_percentile-th percentile of the given array.
- Parameters
array (xarray.DataArray) – This array is used to calculate the noise level.
- Returns
noiselevel – The calculated noise level, based on the given array.
- Return type
- _get_last_changed_time()[source]#
Get the index, where the noise levels changed the last time.
- Returns
time_obj – The time of the last changed value within the thresholds. If the index is np.nan, then no noise level was saved or the noise level was saved without setting the appropriate time.
- Return type
numpy.datetime64 or numpy.nan
- static _get_mean_noise(noiselevels)[source]#
Calculate the mean noise level from given noise levels. At the moment this is only the nan median of the noise levels. In the future it will be possible to calculate the mean noise with a weighted mean.
- Parameters
noiselevels – The mean from these noise levels is calculated.
- Returns
noise_level – The calculated noise level.
- Return type
- _get_old_noise_level(time_obj)[source]#
Get the noise level from the old thresholds, which is the nearest to the given time object.
- Parameters
time_obj (numpy.datetime64) – The nearest noise level to this time object will be returned.
- Returns
old_noiselevel – The old_noiselevel is either determined from nearest old noise level or if not fitted it is the default value.
- Return type
- _get_remembrance_interval(time_obj)[source]#
Calculate the remembrance interval based on given time object and set remembrance time.
- Parameters
time_obj (datetime-like) – The remembrance interval is calculated based on this time object.
- Returns
start_date (pandas.datetime) – The start date of the remembrance interval. This is calculated based on \(time_obj-remembrance_time\)
end_date (pandas.datetime) – The end date of the remembrance interval. Basically, this is the given time object as pandas-datetime.
- _is_rainy_field(array)[source]#
Check if RadarField has more rain than a threshold. The threshold is calculated as \(100 - noise_percentile\).
- Parameters
array (xarray.DataArray) – This array is used to check if the field is rainy or not. The array is converted into reflectivity and all reflectivity above 0 dBZ will be set to rain.
- Returns
is_rainy – If the rain fraction is larger than the threshold, this will be True, else False.
- Return type
- _prune_thresholds(time_obj)[source]#
Prune the thresholds based on the given time object and set remembrance time.
- Parameters
time_obj (numpy.datetime64) – This time object will be used as base time for the pruning process.
- _save_noiselevel(noiselevel, time_obj)[source]#
Save the given noiselevel and time object pair to the attributes of this filter.
- Parameters
time_obj (numpy.datetime64) – The valid time of the given noise level.
- fit(array, time_obj=None)[source]#
Given a reflectivity array, determine the
noiselevelfor use intransform- Parameters
array (array-like) – The linear reflectivity \(Z\)
time_obj (datetime.datetime or numpy.datetime64) – The time
arraywas recorded. Defaults toarray.timeif available, otherwisedatetime.datetime.now.
- property fitted#
- get_noiselevel(time_obj=None)[source]#
Determine an appropriate noiselevel from the available data. If
timeis given, interpolate an appropriate noiselevel fromthresholds. If that is impossible, use the value ofnoiselevel.- Parameters
time_obj (datetime.datetime or numpy.datetime64, optional) – The time used to determine the noiselevel. If None the value of the last`noiselevel` is returned.
- Returns
the noiselevel to use
- Return type
- property noiselevel#
- property noiselevel_constant_since#
Get the index, where the noise levels changed the last time.
- Returns
time_obj – The time of the last changed value within the thresholds. If the index is np.nan, then no noise level was saved or the noise level was saved without setting the appropriate time.
- Return type
numpy.datetime64 or numpy.nan
- property thresholds#
- property times#
- to_xarray()[source]#
Convert parameters to a dataset for easy serialisation
- Returns
parameters as dataset
- Return type
- transform(array, time_obj=None, replace_negative=0.0, *args, **kwargs)[source]#
Subtract an appropriate noiselevel (
get_noiselevel) from the given linear reflectivityarray, optionally replacing (unphysical) negative results.- Parameters
array (array-like) – The linear reflectivity array to remove the noise level from
time_obj (datetime.datetime or numpy.datetime64, optional) – The time used to determine the noiselevel to use. Defaults to
array.timeif available. If unavailable, the value ofnoiselevelis used for transforming.replace_negative (float, optional) – The value to replace negative values in the resulting array with. Defaults to \(0.\,\mathrm{mm^6m^{-3}}\). Set to
Noneto prevent replacing.
- Returns
The array with linear reflectivity and the noise level is removed.
- Return type
array-like
- property xr_thresholds#
Get the thresholds as xarray.DataArray.
- Returns
thres – The thresholds as xarray.DataArray with times as axis.
- Return type