Skip to content

Commit

Permalink
Merge pull request #32 from KatharinaBuelow/newindex
Browse files Browse the repository at this point in the history
newindex
  • Loading branch information
ludwiglierhammer authored Oct 16, 2023
2 parents cb8729a + 4374f75 commit 51e309c
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Welcome to index_calculator's documentation!
usage
notebooks
indices
newproject
newindex
contributing
authors
api
Expand Down
46 changes: 46 additions & 0 deletions docs/newindex.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.. highlight:: shell

=========
New index
=========

The index calculation is based on xclim_. Index calculator only calculated indices, which are available in xclim. If you need an index, which is not included in xclim, please integrate it in xclim not here.

First, you need a new index you need to edit two files:

.. code-block:: console
$ cd index_calculator/tables
$ input_vars.json
$ metadata.json
Second:

.. code-block:: console
$ cd index_calculator/_indices.py
$ _indices.py
The easiest way is to copy an exsiting index which is similar to your new one and adjust it to your needs. The naming conventions can de found in xclim_ under indicators (not indices).

At last you need to perform a test:

.. code-block:: console
$ pip install pytest
Please edit test_indices.py and execute

.. code-block:: console
$ cd tests
$ test_indices.py (edit)
$ pytest (run)
.. _xclim: https://github.com/Ouranosinc/xclim
18 changes: 18 additions & 0 deletions docs/newproject.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.. highlight:: shell

===========
New project
===========

For adding your own project you need to change three files.


.. code-block:: console
$ cd index_calculator/tables
$ cf_conversion.json
$ projects.json
$ xcalc.json
The easiest way is to copy an exsiting project like e.g. EOBS and edit it according to your needs.
33 changes: 33 additions & 0 deletions index_calculator/_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,39 @@ def compute(self, thresh=None, **params):
return self.compute_climate_indicator(params=params, thresh=thresh)


class CMD(ClimateIndicator):
"""Number of calm days (sfcWind)."""

def __init__(self):
super().__init__()
self.thresh = 2
self.units = {"thresh": "m s-1"}
self.func = xc.atmos.calm_days

def compute(self, thresh=None, **params):
"""Calculate number of calm days.
Parameters
----------
thresh: int or string, optional
Threshold wind speed below which a day is considered
as a calm day (default: 2 m s-1).
If type of threshold is an integer the unit is set to m s-1.
Returns
-------
xarray.DataArray
Number of calm days ( fg > {thresh}).
Notes
-----
For more information on the input parameters see:
https://xclim.readthedocs.io/en/stable/api.html#xclim.indicators.atmos.calm_days
"""
return self.compute_climate_indicator(params=params, thresh=thresh)


class SQI(ClimateIndicator):
"""Number of uncomfortable sleep events (tasmin)."""

Expand Down
3 changes: 2 additions & 1 deletion index_calculator/tables/input_vars.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@
"FGn": "sfcWind",
"FX": "sfcWindmax",
"FXx": "sfcWindmax",
"FXn": "sfcWindmax"
"FXn": "sfcWindmax",
"CMD": "sfcWind"
}
6 changes: 6 additions & 0 deletions index_calculator/tables/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@
"units": "m s-1",
"description": "Minimum of daily maximum wind speed."
},
"CMD": {
"standard_name": "days_with_wind_speed_below_threshold",
"long_name": "Number of calm days (wind speed < {thresh} m s-2)",
"units": "days",
"description": "Number of days with daily wind speed below {thresh} m s-2."
},
"CW": {
"standard_name": "cold_and_wet_days",
"long_name": "Number of cold and wet days",
Expand Down
9 changes: 9 additions & 0 deletions tests/test_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,12 @@ def test_FXx():
freq="7D",
)
np.testing.assert_allclose(result, [23], rtol=1e-03)


def test_CMD():
idx_class = indices.CMD()
result = idx_class.compute(
sfcWind=sfcWind_xarray(),
freq="7D",
)
np.testing.assert_allclose(result, [1], rtol=1e-03)

0 comments on commit 51e309c

Please sign in to comment.