Commit c5750cc9 authored by Lars Bärring's avatar Lars Bärring Committed by Klaus Zimmermann
Browse files

Index function percentile (closes #80)

parent 1c899f47
......@@ -123,6 +123,13 @@ index_functions:
reducer:
kind: reducer
percentile:
description: |
Calculates percentiles, default method is "linear" option of numpy and dask.
parameters:
percentiles:
kind: quantity
temperature_sum:
description: |
Calculates the temperature sum above/below a threshold. First, the threshold
......
......@@ -8,6 +8,7 @@ from .index_functions import ( # noqa: F401
ExtremeTemperatureRange,
FirstOccurrence,
LastOccurrence,
Percentile,
SpellLength,
Statistics,
ThresholdedStatistics,
......
......@@ -286,6 +286,39 @@ class ThresholdedStatistics(ThresholdMixin, ReducerMixin, IndexFunction):
return res.astype('float32')
class Percentile(IndexFunction):
def __init__(self, percentiles, interpolation='linear'):
super().__init__(units=Unit('days'))
points = percentiles.points
assert np.all(points > 0)
assert np.all(points < 100)
self.percentiles = percentiles
self.interpolation = interpolation
self.units = '%'
def prepare(self, input_cubes):
super().prepare(input_cubes)
ref_cube = next(iter(input_cubes.values()))
self.standard_name = ref_cube.standard_name
self.units = ref_cube.units
def call_func(self, data, axis, **kwargs):
axis = normalize_axis(axis, data.ndim)
res = np.percentile(data, q=self.percentiles, axis=axis,
interpolation=self.interpolation)
return res.astype('float32')
def lazy_func(self, data, axis, **kwargs):
axis = normalize_axis(axis, data.ndim)
def percentile(arr):
return np.percentile(arr,
q=self.percentiles.points,
interpolation=self.interpolation)
res = da.apply_along_axis(percentile, axis=axis, arr=data).squeeze()
return res.astype('float32')
class TemperatureSum(ThresholdMixin, IndexFunction):
def __init__(self, threshold, condition):
super().__init__(threshold, condition, units=Unit('days'))
......
......@@ -58,6 +58,7 @@ setuptools.setup(
'last_occurrence=climix.index_functions:LastOccurrence',
'spell_length=climix.index_functions:SpellLength',
'statistics=climix.index_functions:Statistics',
'percentile=climix.index_functions:Percentile',
'thresholded_statistics=climix.index_functions:ThresholdedStatistics',
'temperature_sum=climix.index_functions:TemperatureSum',
'diurnal_temperature_range=climix.index_functions:DiurnalTemperatureRange',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment