I have an xarray Dataset which will be acting as a mask to a different dataset. I'd like to create a buffer (of a configurable distance) from any nan values in the mask. I haven't seen anything that adds a buffer internally, instead of expanding the array size with padded values. Below is some reproducible code to show what I mean (the datasets I'm using have 10,000s of x/y coordinates):
import numpy as np
import xarray as xr
data = [[ 0., 1., 2., 3., nan],
[ 0., 6., 4., nan, nan],
[ 4., 3., 6., 4., nan],
[ 1., 0., 3., 4., nan]]
y = [0, 1, 2, 3]
x = [0, 1, 2, 3, 4]
test = xr.Dataset({'band': xr.DataArray(data, coords=[y, x], dims=['y', 'x'])})
I'd like to create a dataset where if I supplied a distance of 1, the above would look like this:
[[ 0., 1., 2., nan., nan],
[ 0., 6., nan., nan, nan],
[ 4., 3., 6., nan., nan],
[ 1., 0., 3., nan., nan]])
And ideally would be able to have a configurable buffer distance that could be set. I've tried to do this via downsampling the image and then upsampling the downsampled image but it was very slow and a struggle to get to work properly so thought I'd see if I'm missing a better option.