Currently, I have something similar to this, where the input_lat
is transformed to new_lat
(here, +0.25, but in real use case, it’s indeterministic).
Since xarray_ufunc
doesn’t return a dataset with actual coordinates values, I had to return a second output to retain new_lat to re-assign the values, but this second output is shaped time, lat, lon
so I have to ds["lat"] = new_lat.isel(lon=0, time=0).values
, which I think is inefficient; I simply need it to be shaped lat
.
Any ideas on how I can modify this to make it more efficient?
import xarray as xr
import numpy as np
air = xr.tutorial.open_dataset("air_temperature")["air"]
input_lat = np.arange(20, 45)
def interp1d_np(data, base_lat, input_lat):
new_lat = input_lat + 0.25
return np.interp(input_lat, base_lat, data), new_lat
ds, new_lat = xr.apply_ufunc(
interp1d_np, # first the function
air,
air.lat, # as above
input_lat, # as above
input_core_dims=[["lat"], ["lat"], ["lat"]], # list with one entry per arg
output_core_dims=[["lat"], ["lat"]], # returned data has one dimension
exclude_dims=set(("lat",)), # dimensions allowed to change size. Must be a set!
vectorize=True, # loop over non-core dims
)
new_lat = new_lat.isel(lon=0, time=0).values
ds["lat"] = new_lat