Skip to content

Instantly share code, notes, and snippets.

@mathause
Last active November 15, 2022 14:23
Show Gist options
  • Save mathause/f545a3f42c00cbe98f559f9220e5e5bd to your computer and use it in GitHub Desktop.
Save mathause/f545a3f42c00cbe98f559f9220e5e5bd to your computer and use it in GitHub Desktop.
roll coords
def lon_to_180(ds, lon_coords="lon"):
# https://git.iac.ethz.ch/utility_functions/utility_functions_python/-/blob/master/xarray.py#L149
lon_orig = ds[lon_coords]
if lon_orig.ndim != 1:
raise ValueError("Currently only handels 1D grids")
with xr.set_options(keep_attrs=True):
lon = ((lon_orig + 180) % 360) - 180
ds = ds.assign_coords({lon_coords: lon})
return ds.sortby(ds[lon_coords])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment