I am trying to apply a function to an Xarray dataset, using dataset.where mask to decide where to apply it. I am not sure how to do it.
Some context: the dataset has two variables (A and B), which are two overlapping raster images (same size and coordinates). I want to run a function (defined by me), that runs a calculation on each gridcell of the raster image, using the two variables. This is not a u_func, it is just a function that takes the values of the two overlapping gridcells, does some calculations and returns a third value I want to save to a third raster image (C).
How do I do that?
I have gotten this far, but it does not work because ds.A passes the entire array to the function, not just the value of ds.A in that particular gridcell:
def my_func(x, y):
..do things
return result
ds = xr.Dataset({
"A": xarray.open_rasterio("A.tif"),
"B": xarray.open_rasterio("B.tif"),
"C": xarray.open_rasterio("C.tif"),
})
ds.C.where(ds.A > 0, my_func(ds.A, ds.B))