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

Thresholded statistics (closes #68)

parent 58be214d
......@@ -37,6 +37,23 @@ index_functions:
reducer:
kind: reducer
thresholded_statistics:
description: |
Calculates a simple statistic of the data for which some condition is met.
First, the threshold is transformed to the same standard_name and units as
the input data.
Then the thresholding is performed as condition(data, threshold), ie
if condition is <, data < threshold.
Finally, the statistic is calculated for those data values that fulfill
the condition.
parameters:
threshold:
kind: quantity
condition:
kind: operator
reducer:
kind: reducer
indices:
cdd:
reference: ETCCDI
......
......@@ -59,3 +59,20 @@ class SpellLength:
else:
res = self.reducer(candidates)
return float(res)
class ThresholdedStatistics:
def __init__(self, threshold, condition, reducer):
self.threshold = threshold
self.condition = condition
self.reducer = reducer
def prepare(self, input_cube):
change_units(self.threshold, input_cube.units, input_cube.standard_name)
self.standard_name = input_cube.standard_name
self.units = input_cube.units
def __call__(self, raw_data):
comb = self.condition(raw_data, self.threshold.data)
res = self.reducer(raw_data[comb])
return float(res)
......@@ -51,6 +51,7 @@ setuptools.setup(
'climix.index_functions': [
'count_occurrences=climix.index_functions:CountOccurences',
'spell_length=climix.index_functions:SpellLength',
'thresholded_statistics=climix.index_functions:ThresholdedStatistics',
],
},
project_urls={
......
Supports Markdown
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