cupy-xarray icon indicating copy to clipboard operation
cupy-xarray copied to clipboard

apply_ufunc problem

Open Ex-tranquility opened this issue 1 year ago • 4 comments

When using apply_ufunc, broadcasting vector functions on several axes will result in the following error: Implicit conversion to a NumPy array is not allowed. Please use .get() to construct a NumPy array explicitly.

Ex-tranquility avatar Jan 22 '25 11:01 Ex-tranquility

Could you share a minimal repducible example?

jacobtomlinson avatar Jan 22 '25 11:01 jacobtomlinson

ERA5 weather data is a four-dimensional dataset that includes time, altitude (atmospheric pressure layer), longitude, and latitude. When I use the function provided by METPY to calculate divergence. When using vector functions in terms of time and height, the above error will be prompted.But using numpy doesn't have this problem

file=r'C:\Users\Administrator\Desktop\ERA5.nc'
ds=xr.open_dataset(file)[['u','v']]
ds_gpu=ds.cupy.as_cupy()
dx,dy=mcalc.lat_lon_grid_deltas(ds.longitude,ds.latitude)
def divergence(u,v):
    div=mcalc.divergence(u,v,dx=dx,dy=dy)
    return div
Divergence=xr.apply_ufunc(divergence,
                                  ds_gpu['u'],
                                  ds_gpu['v'],
                                  input_core_dims=[['latitude','longitude'],['latitude','longitude']],
                                  output_core_dims=[['latitude','longitude']],
                                  vectorize=True)

Ex-tranquility avatar Jan 22 '25 11:01 Ex-tranquility

I suspect the issue is in the metpy function which might be converting to numpy. We'll need to see the whole traceback to be sure

dcherian avatar Jan 22 '25 14:01 dcherian

thank you. However, when using the decorator to test the vector function of the broadcast, apply_ufunc did not enter the broadcast function stage before jumping out of the error and forcibly terminating. The example provided on the official website of cupy-xarray using apply_ufunc does not use the input_core_dims parameter.So further testing is not possible.

Ex-tranquility avatar Jan 23 '25 02:01 Ex-tranquility