Skip to content

Instantly share code, notes, and snippets.

@brews
Created October 26, 2021 17:30
Show Gist options
  • Save brews/b3e9e4e97bacd3f868f339b2f626692f to your computer and use it in GitHub Desktop.
Save brews/b3e9e4e97bacd3f868f339b2f626692f to your computer and use it in GitHub Desktop.
xarray-native approach to adding a cyclic pixel (aka wrap-around pixel) to arrays in an xarray.Dataset.
import xarray as xr
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),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment