Commit a94c7b72 authored by Klaus Zimmermann's avatar Klaus Zimmermann
Browse files

Add count_level_crossings index function (closes #151)

parent 4c408e07
......@@ -6,6 +6,19 @@ drop:
- history
index_functions:
count_level_crossings:
description: |
This index function takes two inputs, low_data and high_data, together
with one parameter, the threshold.
It calculates the number of times low_data is below threshold while
high_data is above threshold.
First, the threshold is transformed to the same standard_name and units as
the input data, then the thresholding is performed, and finally, the number
of occurrences is counted.
parameters:
threshold:
kind: quantity
count_occurrences:
description: |
Calculates the number of times some condition is met.
......
# -*- coding: utf-8 -*-
from .index_functions import ( # noqa: F401
CountLevelCrossings,
CountOccurrences,
FirstOccurrence,
LastOccurrence,
......
......@@ -9,6 +9,34 @@ import numpy as np
from .support import (normalize_axis,
IndexFunction,
ThresholdMixin, ReducerMixin)
from ..util import change_units
class CountLevelCrossings(IndexFunction):
def __init__(self, threshold):
super().__init__(units=Unit('days'))
self.threshold = threshold
self.extra_coords.append(threshold.copy())
def prepare(self, input_cubes):
props = {(cube.dtype, cube.units, cube.standard_name)
for cube in input_cubes}
assert len(props) == 1
dtype, units, standard_name = props.pop()
threshold = self.threshold
threshold.points = threshold.points.astype(dtype)
if threshold.has_bounds():
threshold.bounds = threshold.bounds.astype(dtype)
change_units(threshold, units, standard_name)
super().prepare(input_cubes)
def call_func(self, data, axis, **kwargs):
cond = da.logical_and(data['low_data'] < self.threshold.points,
self.threshold.points < data['high_data'])
res = np.count_nonzero(cond, axis=axis)
return res.astype('float32')
lazy_func = call_func
class CountOccurrences(ThresholdMixin, IndexFunction):
......
......@@ -51,6 +51,7 @@ setuptools.setup(
'climix-editor=climix.editor:main [editor]',
],
'climix.index_functions': [
'count_level_crossings=climix.index_functions:CountLevelCrossings',
'count_occurrences=climix.index_functions:CountOccurrences',
'first_occurrence=climix.index_functions:FirstOccurrence',
'last_occurrence=climix.index_functions:LastOccurrence',
......
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