Replies: 1 comment
-
you could use da = xr.DataArray(
np.ones((10, 10, 4)),
coords={"bands": ["red", "green", "blue", "nir"]},
dims=("x", "y", "bands"),
)
ds = da.to_dataset(dim="bands")
(ds.red + ds.green) / (2 * (ds.blue + ds.nir)) this is a bit longer than the proposed |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have a DataArray that has three dimensions: x, y and bands. The labels in the bands array are names like
red
,blue
,nir
etc.I would like to evaluate a mathematical expression that uses those labels by doing something like:
da.eval("red-nir")
or
da.eval("(red+green)/(2*(blue+nir)")
These would return a DataArray with the resulting values.
The idea is that the user would be passing these expressions to my code, so I can't just hard code them.
Obviously the da.eval method doesn't exist at the moment - but that's representing the ideal interface.
Is there any nice way to do this at the moment?
I could probably put together some rather horrible code that iterated through the labels on the
band
axis, extracted each band into its own variable using python'seval
function with a string likebandname = da.sel(band=bandname)
, and then use python's eval function to do the final calculation - but I'm sure there must be a better way.Any thoughts?
Beta Was this translation helpful? Give feedback.
All reactions