Repository navigation
Replies: 1 comment
|
For a single ds["tmp"].interp_like(
ds0,
kwargs={"fill_value": (ds["tmp"].isel(z=0).item(),
ds["tmp"].isel(z=-1).item())},
)The complication with your For a Dataset, one straightforward solution is to interpolate each data variable with its own endpoint tuple and combine them again: result = xr.Dataset(
{
name: da.interp_like(
ds0,
kwargs={
"fill_value": (
da.isel(z=0).item(),
da.isel(z=-1).item(),
)
},
)
for name, da in ds.data_vars.items()
},
coords=ds0.coords,
)With the example in the question this gives constant endpoint extrapolation independently for I tested the relevant call against current xarray before posting: a two-element For large/lazy arrays, avoid |
Uh oh!
There was an error while loading. Please reload this page.
I have several datasets of ocean data from different climate models that I'm trying to interpolate to a common vertical grid using
xarray.interp_like. The depth of the surface cell isn't the same on each model, so I'm trying to interpolate in a way that mirrors the behavior of numpy.interp, i.e. filling in with the first and last array values,fp[0]andfp[-1], outside the range of the input data. But sinceinterp_likeuses the SciPy interpolator it instead fills withnp.nan. The scipy documentation has an example of how to implement the same behavior in scipy, but that syntax really only works if you do it directly in scipy rather than passing it through xarray.I've tried a few different things for the
fill_valuekwarg ininterp_likebut couldn't come up with anything that gives me the desired behavior. The documentation for interp_like mentions numpy is used in some cases, but it's not clear to me if there is a way to choose whether numpy or scipy is used.So my question is, is there some way to tell xarray
interp_liketo use the constant extrapolation for values outside the range? This seems like it should be a fairly common use case, but I couldn't find anything suggesting this is currently possibleExample input:
Output:
Desired behavior:
I'd like to interpolate the same way as numpy would, e.g. for the
tmpvariable:Extrapolation in interp_like:
If I instead use
kwargs={'fill_value': 'extrapolate'}, the values are much different from the constant fill value, since it uses linear rather than constant extrapolation:All reactions