I have been trying to pass an user-defined parameter to my UDF but unsuccessfully.
I have tried to simply pass a dictionary as context. I read the collection “FRACTIONAL SNOW COVER” and I want to binarize the datset based on a threshold of snow cover fraction.
If this is my UDF (I know that probably it could be replaced by an OpenEO process, but I want to understand how it works for future developments)
from openeo.udf import XarrayDataCube
from openeo.udf import OpenEoUdfException
import numpy as np
import xarray as xr
def apply_datacube(cube: XarrayDataCube,
context: dict) -> XarrayDataCube:
snowT = context['snowT']
array = cube.get_array()
array = xr.where((array >= snowT) & (array <= 100), array, 0)
array = xr.where(array < snowT, array, 100)
array = array.astype(np.int16)
return XarrayDataCube(array)
saved in a script called udf-binarize.py, I tried to pass the context in this way
binarize = openeo.UDF(Path('udf-binarize.py').read_text())
binarize_dict = {'snowT':20}
scf_test = scf.apply(process=binarize, context=binarize_dict)
But I get this error:
OpenEO batch job failed: UDF Exception during Spark execution: File “/opt/venv/lib64/python3.8/site-packages/openeo/udf/run_code.py”, line 180, in run_udf_code result_cube = func(cube=data.get_datacube_list()[0], context=data.user_context) File “”, line 38, in apply_datacube openeo.udf.OpenEoUdfException: Missing snowT in dictionary
I dont’ understand how to pass that parameter “snowT”, I had a look online and the only solution I found is to do like this guy
that is fixing the parameter (e.g., C=0) and then replacing it in the text later on. But is there a more elegant solution?
Also, according to the documentation here
https://open-eo.github.io/openeo-python-client/api.html#openeo.udf.run_code.execute_local_udf
if I want to run the UDF locally by using execute_local_udf, I cannot pass the context to that function, isn’t it?!
Last question, if I put a print() in my UDF for debugging , how can I visualize the output in the Web Editor?
Thanks in advance
Valentina