Skip to content

Commit

Permalink
Merge and upd restrict_by_wl
Browse files Browse the repository at this point in the history
  • Loading branch information
aulemahal committed Oct 9, 2023
2 parents 5fda0d5 + 1f3de06 commit 3f42c95
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .cruft.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"project_slug": "xscen",
"project_short_description": "A climate change scenario-building analysis framework, built with xclim/xarray.",
"pypi_username": "RondeauG",
"version": "0.7.12-beta",
"version": "0.7.13-beta",
"use_pytest": "y",
"use_black": "y",
"add_pyup_badge": "n",
Expand Down
3 changes: 2 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ New features and enhancements
* ``xs.spatial_mean`` with ``method='xESMF'`` will also automatically segmentize polygons (down to a 1° resolution) to ensure a correct average (:pull:`260`).
* Added documentation for `require_all_on` in `search_data_catalogs`. (:pull:`263`).
* ``xs.save_to_table`` and ``xs.io.to_table`` to transform datasets and arrays to DataFrames, but with support for multi-columns, multi-sheets and localized table of content generation.
* Added annual global tas timeseries for CMIP6's models CMCC-ESM2 (ssp245, ssp370, ssp585), EC-Earth3-CC (ssp245, ssp585), KACE-1-0-G (ssp245, ssp370, ssp585) and TaiESM1 (ssp245, ssp370). (:issue:`268`, :pull:`270`).
* Better ``xs.extract.resample`` : support for weighted resampling operations when starting with frequencies coarser than daily and missing timesteps/values handling. (:issue:`80`, :issue:`93`, :pull:`265`).
* Added annual global tas timeseries for CMIP6's models CMCC-ESM2 (ssp245, ssp370, ssp585), EC-Earth3-CC (ssp245, ssp585), KACE-1-0-G (ssp245, ssp370, ssp585) and TaiESM1 (ssp245, ssp370). Moved global tas database to a netCDF file. (:issue:`268`, :pull:`270`).

Breaking changes
^^^^^^^^^^^^^^^^
Expand Down
8 changes: 4 additions & 4 deletions docs/goodtoknow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ There are many ways to open data in xscen workflows. The list below tries to mak
Which function to use when resampling data
------------------------------------------

:extract_dataset: :py:func:`~xscen.extract.extract_dataset` has resampling capabilities to provide daily data from finer sources.
:extract_dataset: :py:func:`~xscen.extract.extract_dataset`'s resampling capabilities are meant to provide daily data from finer sources.

:xclim indicators: Through :py:func:`~xscen.indicators.compute_indicators`, xscen workflows can easily use `xclim indicators <https://xclim.readthedocs.io/en/stable/indicators.html>`_
to go from daily data to coarser (monthly, seasonal, annual).
:resample: :py:func`xscen.extract.resample` extends xarray's `resample` methods with support for weighted resampling when starting from data coarser than daily and for handling of missing timesteps or values.

What is currently not covered by either `xscen` or `xclim` is a method to resample from data coarser than daily, where the base period is non-uniform (ex: resampling from monthly to annual data, taking into account the number of days per month).
:xclim indicators: Through :py:func:`~xscen.indicators.compute_indicators`, xscen workflows can easily use `xclim indicators <https://xclim.readthedocs.io/en/stable/indicators.html>`_
to go from daily data to coarser (monthly, seasonal, annual), with missing values handling. This option will add more metadata than the two firsts.


Metadata translation
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.7.12-beta
current_version = 0.7.13-beta
commit = True
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+).(?P<patch>\d+)(\-(?P<release>[a-z]+))?
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ def run(self):
test_suite="tests",
extras_require={"dev": dev_requirements},
url="https://github.com/Ouranosinc/xscen",
version="0.7.12-beta",
version="0.7.13-beta",
zip_safe=False,
)
81 changes: 81 additions & 0 deletions tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import pandas as pd
import pytest
import xarray as xr
from conftest import notebooks
from xclim.testing.helpers import test_timeseries as timeseries

Expand Down Expand Up @@ -413,3 +414,83 @@ def test_outofrange(self):

def test_none(self):
assert xs.subset_warming_level(TestSubsetWarmingLevel.ds, wl=20) is None


class TestResample:
@pytest.mark.parametrize(
"infrq,meth,outfrq,exp",
[
["MS", "mean", "QS-DEC", [0.47457627, 3, 6.01086957, 9, 11.96666667]],
[
"QS-DEC",
"mean",
"YS",
[1.49041096, 5.49041096, 9.49453552, 13.49041096, 17.49041096],
],
["MS", "std", "2YS", [6.92009239, 6.91557206]],
[
"QS",
"median",
"YS",
[1.516437, 5.516437, 9.516437, 13.51092864, 17.516437],
],
],
)
def test_weighted(self, infrq, meth, outfrq, exp):
da = timeseries(
np.arange(48),
variable="tas",
start="2001-01-01",
freq=infrq,
)
out = xs.extract.resample(da, outfrq, method=meth)
np.testing.assert_allclose(out.isel(time=slice(0, 5)), exp)

def test_weighted_wind(self):
uas = timeseries(
np.arange(48),
variable="uas",
start="2001-01-01",
freq="MS",
)
vas = timeseries(
np.arange(48),
variable="vas",
start="2001-01-01",
freq="MS",
)
ds = xr.merge([uas, vas])
out = xs.extract.resample(ds.uas, "YS", method="wind_direction", ds=ds)
np.testing.assert_allclose(out, [5.5260274, 17.5260274, 29.5260274, 41.5136612])

def test_missing(self):
da = timeseries(
np.arange(48),
variable="tas",
start="2001-01-01",
freq="MS",
)
out = xs.extract.resample(da, "QS-DEC", method="mean", missing="drop")
assert out.size == 15

out = xs.extract.resample(da, "QS-DEC", method="mean", missing="mask")
assert out.isel(time=0).isnull().all()

def test_missing_xclim(self):
arr = np.arange(48).astype(float)
arr[0] = np.nan
arr[40:] = np.nan
da = timeseries(
arr,
variable="tas",
start="2001-01-01",
freq="MS",
)
out = xs.extract.resample(da, "YS", method="mean", missing={"method": "any"})
assert out.isel(time=0).isnull().all()

out = xs.extract.resample(
da, "YS", method="mean", missing={"method": "pct", "tolerance": 0.6}
)
assert out.isel(time=0).notnull().all()
assert out.isel(time=-1).isnull().all()
2 changes: 1 addition & 1 deletion tests/test_xscen.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ def test_package_metadata(self):
contents = f.read()
assert """Gabriel Rondeau-Genesse""" in contents
assert '__email__ = "[email protected]"' in contents
assert '__version__ = "0.7.12-beta"' in contents
assert '__version__ = "0.7.13-beta"' in contents
2 changes: 1 addition & 1 deletion xscen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

__author__ = """Gabriel Rondeau-Genesse"""
__email__ = "[email protected]"
__version__ = "0.7.12-beta"
__version__ = "0.7.13-beta"


# monkeypatch so that warnings.warn() doesn't mention itself
Expand Down
Loading

0 comments on commit 3f42c95

Please sign in to comment.