How to pass coordinates to map, apply_ufunc, or whatever #5635
Replies: 1 comment
-
Hi there, I think what's confusing here is the way that passing a As for your question I think you should simply explicitly pass the coordinate variables you need into def innerfunc(z, x, y):
return z + x + y
def outerfunc(a):
return xr.apply_ufunc(innerfunc, a, a['x'], a['y'], input_core_dims=[['x','y'], ['x'], ['y']]) In this case this will give an error though, because innerfunc would be trying to add arrays of different shape: <ipython-input-50-538a01d37e0c> in innerfunc(z, x, y)
1 def innerfunc(z, x, y):
----> 2 return z + x + y
3
4 def outerfunc(a):
5 return xr.apply_ufunc(innerfunc, a, a['x'], a['y'], input_core_dims=[['x','y'], ['x'], ['y']])
ValueError: operands could not be broadcast together with shapes (1,4,5) (4,) Perhaps something closer to what you actually want involves broadcasting over the core dimensions first, like this def innerfunc(z, x, y):
return z + x + y
def outerfunc(a):
xx, yy = xr.broadcast(a['x'], a['y'])
return xr.apply_ufunc(innerfunc, a, xx, yy,
input_core_dims=[['x','y'], ['x','y'], ['x', 'y']],
output_core_dims=[['x', 'y']]) which should perform the addition in In [49]: outerfunc(ds)
Out[49]:
<xarray.Dataset>
Dimensions: (band: 1, x: 4, y: 5)
Coordinates:
* x (x) int64 0 1 2 3
* y (y) int64 10 11 12 13 14
* band (band) int64 1
spatial_ref <U9 'EPSG:CODE'
Data variables:
z (band, x, y) float64 10.73 9.821 10.46 ... 14.16 14.24 17.05 Does that help? |
Beta Was this translation helpful? Give feedback.
-
It looks like someone else just asked this but was shot down. I need to apply a function over a gigantic raster that is aware of the coords at each pixel it is iterating over. How do I do this?
Example trying apply_ufunc, may not be the right direction:
...
Beta Was this translation helpful? Give feedback.
All reactions