Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
climix
climix
Commits
a94c7b72
Commit
a94c7b72
authored
Feb 04, 2020
by
Klaus Zimmermann
Browse files
Add count_level_crossings index function (closes #151)
parent
4c408e07
Changes
4
Hide whitespace changes
Inline
Side-by-side
climix/etc/metadata.yml
View file @
a94c7b72
...
...
@@ -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.
...
...
climix/index_functions/__init__.py
View file @
a94c7b72
# -*- coding: utf-8 -*-
from
.index_functions
import
(
# noqa: F401
CountLevelCrossings
,
CountOccurrences
,
FirstOccurrence
,
LastOccurrence
,
...
...
climix/index_functions/index_functions.py
View file @
a94c7b72
...
...
@@ -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
):
...
...
setup.py
View file @
a94c7b72
...
...
@@ -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'
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment