Skip to content

Instantly share code, notes, and snippets.

@rileyhales
Created March 22, 2024 18:18
Show Gist options
  • Save rileyhales/fe588578ff803e3609964c5d19fa9220 to your computer and use it in GitHub Desktop.
Save rileyhales/fe588578ff803e3609964c5d19fa9220 to your computer and use it in GitHub Desktop.
import xarray as xr
import numpy as np
with xr.open_dataset('./2023era5/2023_11_era5_hourly.nc') as ds:
# find the time steps where the runoff is not nan when expver=1
a = ds.ro.sel(latitude=0, longitude=0, expver=1)
expver1_timesteps = a.time[~np.isnan(a)]
# find the time steps where the runoff is not nan when expver=5
b = ds.ro.sel(latitude=0, longitude=0, expver=5)
expver5_timesteps = b.time[~np.isnan(b)]
# assert that the two timesteps combined are the same as the original
assert len(ds.time) == len(expver1_timesteps) + len(expver5_timesteps)
# combine the two
xr.concat([
ds.sel(expver=1, time=expver1_timesteps.values).drop_vars('expver'),
ds.sel(expver=5, time=expver5_timesteps.values).drop_vars('expver')
], dim='time').to_netcdf('./2023era5/2023_11_era5_hourly_noexpver.nc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment