I have two xarray datasets, each with their own rotated pole crs. I need to interpolate a variable from one dataset and add it to the other.
Each data set has dims/coords of rlat/rlon, and coords of lat/lon that are referenced to (rlat,rlon).
ds1 = xr.Dataset(
data_vars=dict{var1=(["rlat", "rlon"], var1),var2=(["rlat", "rlon"], var2)),
coords=dict(rlat=rlat,rlon=rlon,
lon=(["rlat", "rlon"], lon),lat=(["rlat", "rlon"], lat))
)
ds2 = xr.Dataset(
data_vars=dict{var2=(["rlat", "rlon"], var2)),
coords=dict(rlat=rlat,rlon=rlon,
lon=(["rlat", "rlon"], lon),lat=(["rlat", "rlon"], lat))
)
So I can do this by flattening the lons, lats, and var, and using np.griddata
to interpolate var1
between the two.
ds1_lon = ds1.lon.values.flatten()
ds1_lat = ds1.lat.values.flatten()
ds2_lon = ds2.lon.values.flatten()
ds2_lat = ds2.lat.values.flatten()
ds1_var1 = ds1[var1].values.flatten()
ds_var1_interp = griddata((ds1_lon, ds1_lat), ds_var1, (ds2_lon, ds2_lat), method='nearest')
ds1_var1_interp = ds_var_interp.reshape(ds2.lon.shape)
ds2[var1] = xr.DataArray(ds1_var1_interp, dims=["rlat", "rlon"], coords={"rlat": ds2.rlat,
"rlon": ds2.rlon})
But there has go to be a better way? Especially for situations where I don’t already have lat/lon in the same coordinate system.
I have the crs for each, for example:
ds1_proj4 = '-m 57.295779506 +proj=ob_tran +o_proj=latlon +o_lat_p=-185.0 +lon_0=20.0'
ds2_proj4 = '-m 57.295779506 +proj=ob_tran +o_proj=latlon +o_lat_p=-180.0 +lon_0=10.0'
But I’ve tried various combinations of pyproj.Transform, rioxarray (only takes x/y), etc, to no avail.
Is there an efficient way to transform a variable in an xarray from one coordinate system to another and add the transformed variable to the desired xarray dataset?