Skip to content

Commit

Permalink
Resolve "The behavior for data sets with different temporal resolutio…
Browse files Browse the repository at this point in the history
…n are not uniform" (#100)
  • Loading branch information
btschwertfeger committed May 17, 2024
1 parent cdace26 commit 8a55556
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions cmethods/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ 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"}

result: XRData = xr.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"]],
Expand All @@ -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(
Expand Down
13 changes: 7 additions & 6 deletions doc/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"},
)
5 changes: 5 additions & 0 deletions doc/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8a55556

Please sign in to comment.