Hello,
I would like to create a UDF (and later a UDP) that iterates through a stack of SAR images to calculate change detection between each date. For instance date d would be compared with d+1, then d+1 with d+2, … and last date should be excluded as it doesn’t have a d+1.
To this purpose I developed a function called ‘detect_changes’ that performs something similar to coherence calculation because it computes means on a sliding window between 2 dates. I tested my code locally with S1_GRD images and it works properly. The code takes as inputs two numpy arrays, I am trying to reproduce that in a UDF.
Here is the code I use for the UDF:
def apply_datacube(cube: XarrayDataCube, context: dict) → XarrayDataCube:
# Parameters from context
filter_size = context.get(“filter_size”, (3, 3))
contamination = context.get(“contamination”, 0.02)
# Extracting data arrays
data = cube.get_array()
times = data.coords[“t”].values # Get dates
change_maps =
for i in range(len(times) - 1):
first_image = np.abs(data.sel(t=times[i]).values)
second_image = np.abs(data.sel(t=times[i + 1]).values)
# Detect changes between consecutive time steps
change_map = detect_changes(first_image, second_image, filter_size, contamination)
change_maps.append(xr.DataArray(change_map, dims=[“x”, “y”], coords={“x”: data.x, “y”: data.y, “t”: times[i + 1]}))
# Combine all change maps into a single xarray.DataArray
result_array = xr.concat(change_maps, dim=“t”)
# Return the result as a new XarrayDataCube
return XarrayDataCube(result_array)
The error that I get :
File “/opt/openeo/lib/python3.8/site-packages/openeo/udf/run_code.py”, line 180, in run_udf_code\n result_cube = func(cube=data.get_datacube_list()[0], context=data.user_context)\n File “”, line 60, in apply_datacube\n File “/opt/openeo/lib/python3.8/site-packages/xarray/core/coordinates.py”, line 283, in getitem\n return self._data._getitem_coord(key)\n File “/opt/openeo/lib/python3.8/site-packages/xarray/core/dataarray.py”, line 690, in _getitem_coord\n _, key, var = _get_virtual_variable(\n File “/opt/openeo/lib/python3.8/site-packages/xarray/core/dataset.py”, line 171, in _get_virtual_variable\n ref_var = variables[ref_name]\nKeyError: 't''}]
I don t get the structure of the XarrayDataCube and its arrays. I don’t get at all if I should apply my UDF to each array like I tried to do or directly on the whole datacube.
Job ID : j-24112647e9eb45269e6e98aa978ba4d9
Could someone support on this please ?
I would be very grateful
BEHLOUL Selim