Skip to content

Instantly share code, notes, and snippets.

@dennissergeev
Created June 20, 2019 14:40
Show Gist options
  • Save dennissergeev/90444bdc3e2da7e9cd3e7f865ce2a380 to your computer and use it in GitHub Desktop.
Save dennissergeev/90444bdc3e2da7e9cd3e7f865ce2a380 to your computer and use it in GitHub Desktop.
Functions for rolling xarray DataArray along its longitudinal axis
def roll_da_to0360(da, lon_name='longitude'):
"""Roll DataArray's data with longitudes from (-180, 180) to (0, 360)."""
# Roll longitudes and corresponding data
out = da.roll(longitude=da[lon_name].shape[0]//2, roll_coords=True)
# Reset western (negative) longitudes to values within (180, 360) range
out[lon_name] = out[lon_name] - (out[lon_name] // 360) * 360
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment