Skip to content

Instantly share code, notes, and snippets.

@rohithteja
Last active February 21, 2022 15:28
Show Gist options
  • Select an option

  • Save rohithteja/fe8979a70a3543732717ad5529b9fc47 to your computer and use it in GitHub Desktop.

Select an option

Save rohithteja/fe8979a70a3543732717ad5529b9fc47 to your computer and use it in GitHub Desktop.
interpolate netcdf
import xarray as xr
ds = xr.open_dataset('2mtemp_2021.nc')
ds = ds.resample(time='M').mean() #resample monthly
#bounds
min_lon = -128
min_lat = 19
max_lon = -67
max_lat = 50
R = 0.05 #resolution
#create dummy dataset with the required resolution and bounds
lats = np.flip(np.arange(min_lat,max_lat,R)).astype('float32')
lons = np.arange(min_lon,max_lon,R).astype('float32')
ds_out = xr.Dataset({'latitude': (['latitude'],lats),
'longitude': (['longitude'],lons),})
#interpolate and assign
ds_interp = ds.interp_like(other=ds_out)
ds_interp = ds_interp.assign_coords({'latitude': (['latitude'],lats),
'longitude': (['longitude'],lons),})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment