From 8a55556582cfe1a926bb74b28532d8e4710596b7 Mon Sep 17 00:00:00 2001 From: "Benjamin T. Schwertfeger" Date: Fri, 17 May 2024 16:25:32 +0200 Subject: [PATCH] Resolve "The behavior for data sets with different temporal resolution are not uniform" (#100) --- README.md | 4 ++++ cmethods/core.py | 16 ++++++++-------- doc/getting_started.rst | 13 +++++++------ doc/introduction.rst | 5 +++++ 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 10c7aff..b7bdb86 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,10 @@ Please refer to the official documentation for more information about these methods as well as sample scripts: https://python-cmethods.readthedocs.io/en/stable/ +## Best Practices and important Notes + +- The training data should have the same temporal resolution. + - Except for the variance scaling, all methods can be applied on stochastic and non-stochastic climate variables. Variance scaling can only be applied on non-stochastic climate variables. diff --git a/cmethods/core.py b/cmethods/core.py index eb4c2b5..a08c4a9 100644 --- a/cmethods/core.py +++ b/cmethods/core.py @@ -60,7 +60,7 @@ def apply_ufunc( 'input_core_dims must have three key-value pairs like: {"obs": "time", "simh": "time", "simp": "time"}', ) - input_core_dims = kwargs["input_core_dims"] + input_core_dims = kwargs.pop("input_core_dims") else: input_core_dims = {"obs": "time", "simh": "time", "simp": "time"} @@ -68,13 +68,13 @@ def apply_ufunc( __METHODS_FUNC__[method], obs, simh, - # Need to spoof a fake time axis since 'time' coord on full dataset is different - # than 'time' coord on training dataset. + # Need to spoof a fake time axis since 'time' coord on full dataset is + # different than 'time' coord on training dataset. simp.rename({input_core_dims["simp"]: "__t_simp__"}), dask="parallelized", vectorize=True, - # This will vectorize over the time dimension, so will submit each grid cell - # independently + # This will vectorize over the time dimension, so will submit each grid + # cell independently input_core_dims=[ [input_core_dims["obs"]], [input_core_dims["simh"]], @@ -89,9 +89,9 @@ def apply_ufunc( # Rename to proper coordinate name. result = result.rename({"__t_simp__": input_core_dims["simp"]}) - # ufunc will put the core dimension to the end (time), so want to preserve original - # order where time is commonly first. - return result.transpose(*obs.dims) + # ufunc will put the core dimension to the end (time), so want to preserve + # original order where time is commonly first. + return result.transpose(*obs.rename({input_core_dims["obs"]: input_core_dims["simp"]}).dims) def adjust( diff --git a/doc/getting_started.rst b/doc/getting_started.rst index 51d1faf..a8b8337 100644 --- a/doc/getting_started.rst +++ b/doc/getting_started.rst @@ -149,16 +149,17 @@ dimensions of ``obs`` and ``simp`` have the length, but the time dimension of import xarray as xr obs = xr.open_dataset("examples/input_data/observations.nc")["tas"] - simp = xr.open_dataset("examples/input_data/control.nc")["tas"] simh = simp.copy(deep=True)[3650:] + simp = xr.open_dataset("examples/input_data/control.nc")["tas"] bc = adjust( method="quantile_mapping", obs=obs, simh=simh.rename({"time": "t_simh"}), - simp=simh, + simp=simp, kind="+", - input_core_dims={"obs": "time", "simh": "t_simh", "simp": "time"} + input_core_dims={"obs": "time", "simh": "t_simh", "simp": "time"}, + n_quantiles=100, ) In case you are applying a scaling based technique using grouping, you have to @@ -172,15 +173,15 @@ adjust the group names accordingly to the time dimension names. import xarray as xr obs = xr.open_dataset("examples/input_data/observations.nc")["tas"] - simp = xr.open_dataset("examples/input_data/control.nc")["tas"] simh = simp.copy(deep=True)[3650:] + simp = xr.open_dataset("examples/input_data/control.nc")["tas"] bc = adjust( method="linear_scaling", obs=obs, simh=simh.rename({"time": "t_simh"}), - simp=simh, + simp=simp, kind="+", group={"obs": "time.month", "simh": "t_simh.month", "simp": "time.month"}, - input_core_dims={"obs": "time", "simh": "t_simh", "simp": "time"} + input_core_dims={"obs": "time", "simh": "t_simh", "simp": "time"}, ) diff --git a/doc/introduction.rst b/doc/introduction.rst index a207a36..99344e4 100644 --- a/doc/introduction.rst +++ b/doc/introduction.rst @@ -74,6 +74,11 @@ Please refer to the official documentation for more information about these methods as well as sample scripts: https://python-cmethods.readthedocs.io/en/stable/ +Best Practices and important Notes +---------------------------------- + +- The training data should have the same temporal resolution. + - Except for the variance scaling, all methods can be applied on stochastic and non-stochastic climate variables. Variance scaling can only be applied on non-stochastic climate variables.