Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --cyclic option to regrid cli and services #108

Merged
merged 5 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ History

0.x.x (xxxx-xx-xx)
------------------
* Add ``--cyclic`` option to regrid cli and services. (PR #108, @brews)
* Add ``papermill``, ``intake-esm`` to Docker environment. (PR #106, @brews)


Expand Down
6 changes: 5 additions & 1 deletion dodola/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ def rechunk(x, chunk, out):
help="Local path to existing regrid weights file",
)
@click.option("--astype", "-t", default=None, help="Type to recast output to")
def regrid(x, out, method, domain_file, weightspath, astype):
@click.option(
"--cyclic", default=None, help="Add wrap-around values to dim before regridding"
)
def regrid(x, out, method, domain_file, weightspath, astype, cyclic):
"""Regrid a target climate dataset

Note, the weightspath only accepts paths to NetCDF files on the local disk. See
Expand All @@ -248,6 +251,7 @@ def regrid(x, out, method, domain_file, weightspath, astype):
domain_file=domain_file,
weights_path=weightspath,
astype=astype,
add_cyclic=cyclic,
)


Expand Down
23 changes: 22 additions & 1 deletion dodola/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,22 @@ def build_xesmf_weights_file(x, domain, method, filename=None):
return str(out.filename)


def xesmf_regrid(x, domain, method, weights_path=None, astype=None):
def _add_cyclic(ds, dim):
"""
Adds wrap-around, appending first value to end of data for named dimension.

Basically an xarray version of ``cartopy.util.add_cyclic_point()``.
"""
return ds.map(
lambda x, d: xr.concat([x, x.isel({d: 0})], dim=d),
keep_attrs=True,
d=str(dim),
)


def xesmf_regrid(x, domain, method, weights_path=None, astype=None, add_cyclic=None):
"""
Regrid a Dataset.

Parameters
----------
Expand All @@ -263,11 +277,18 @@ def xesmf_regrid(x, domain, method, weights_path=None, astype=None):
Local path to netCDF file of pre-calculated XESMF regridding weights.
astype : str, numpy.dtype, or None, optional
Typecode or data-type to which the regridded output is cast.
add_cyclic : str, or None, optional
Add cyclic point (aka wrap-around pixel) to given dimension before
regridding. Useful for avoiding dateline artifacts along longitude
in global datasets.

Returns
-------
xr.Dataset
"""
if add_cyclic:
x = _add_cyclic(x, add_cyclic)

regridder = xe.Regridder(
x,
domain,
Expand Down
10 changes: 8 additions & 2 deletions dodola/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ def rechunk(x, target_chunks, out):


@log_service
def regrid(x, out, method, domain_file, weights_path=None, astype=None):
def regrid(
x, out, method, domain_file, weights_path=None, astype=None, add_cyclic=None
):
"""Regrid climate data

Parameters
Expand All @@ -279,9 +281,12 @@ def regrid(x, out, method, domain_file, weights_path=None, astype=None):
Local file path name to write regridding weights file to.
astype : str, numpy.dtype, or None, optional
Typecode or data-type to which the regridded output is cast.
add_cyclic : str, or None, optional
Add cyclic (aka wrap-around values) to dimension before regridding.
Useful for avoiding dateline artifacts along longitude in global
datasets.
"""
ds = storage.read(x)

ds_domain = storage.read(domain_file)

regridded_ds = xesmf_regrid(
Expand All @@ -290,6 +295,7 @@ def regrid(x, out, method, domain_file, weights_path=None, astype=None):
method=method,
weights_path=weights_path,
astype=astype,
add_cyclic=add_cyclic,
)

storage.write(out, regridded_ds)
Expand Down
14 changes: 14 additions & 0 deletions dodola/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dodola.core import (
train_quantiledeltamapping,
adjust_quantiledeltamapping_year,
_add_cyclic,
)


Expand Down Expand Up @@ -181,3 +182,16 @@ def test_adjust_quantiledeltamapping_year_output_time():
assert max(adjusted_ds["time"].values) == cftime.DatetimeNoLeap(
2088, 12, 31, 0, 0, 0, 0
)


def test_add_cyclic():
"""Test _add_cyclic adds wraparound values"""
in_da = xr.DataArray(
np.ones([5, 6]) * np.arange(6),
coords=[np.arange(5) + 1, np.arange(6) + 1],
dims=["lat", "lon"],
)
out_ds = _add_cyclic(ds=in_da.to_dataset(name="fakevariable"), dim="lon")
assert all(
out_ds["fakevariable"].isel(lon=0) == out_ds["fakevariable"].isel(lon=-1)
)