@@ -28,13 +28,15 @@ using the openEO Python Client library:
2828 import openeo
2929
3030 # Build a UDF object from an inline string with Python source code.
31- udf = openeo.UDF("""
32- import xarray
31+ udf = openeo.UDF(
32+ """
33+ import xarray
3334
34- def apply_datacube(cube: xarray.DataArray, context: dict) -> xarray.DataArray:
35- cube.values = 0.0001 * cube.values
36- return cube
37- """ )
35+ def apply_datacube(cube: xarray.DataArray, context: dict) -> xarray.DataArray:
36+ cube.values = 0.0001 * cube.values
37+ return cube
38+ """
39+ )
3840
3941 # Or load the UDF code from a separate file.
4042 # udf = openeo.UDF.from_file("udf-code.py")
@@ -173,7 +175,7 @@ In most of the examples here, we will start from an initial Sentinel2 data cube
173175 " SENTINEL2_L2A" ,
174176 spatial_extent = {" west" : 4.00 , " south" : 51.04 , " east" : 4.10 , " north" : 51.1 },
175177 temporal_extent = [" 2022-03-01" , " 2022-03-31" ],
176- bands = [" B02" , " B03" , " B04" ]
178+ bands = [" B02" , " B03" , " B04" ],
177179 )
178180
179181
@@ -203,6 +205,7 @@ The UDF code is this short script (the part that does the actual value rescaling
203205
204206 import xarray
205207
208+
206209 def apply_datacube (cube : xarray.DataArray, context : dict ) -> xarray.DataArray:
207210 cube.values = 0.0001 * cube.values
208211 return cube
@@ -244,17 +247,19 @@ The UDF-specific part is highlighted.
244247 " SENTINEL2_L2A" ,
245248 spatial_extent = {" west" : 4.00 , " south" : 51.04 , " east" : 4.10 , " north" : 51.1 },
246249 temporal_extent = [" 2022-03-01" , " 2022-03-31" ],
247- bands = [" B02" , " B03" , " B04" ]
250+ bands = [" B02" , " B03" , " B04" ],
248251 )
249252
250253 # Create a UDF object from inline source code.
251- udf = openeo.UDF("""
252- import xarray
254+ udf = openeo.UDF(
255+ """
256+ import xarray
253257
254- def apply_datacube(cube: xarray.DataArray, context: dict) -> xarray.DataArray:
255- cube.values = 0.0001 * cube.values
256- return cube
257- """ )
258+ def apply_datacube(cube: xarray.DataArray, context: dict) -> xarray.DataArray:
259+ cube.values = 0.0001 * cube.values
260+ return cube
261+ """
262+ )
258263
259264 # Pass UDF object as child process to `apply`.
260265 rescaled = s2_cube.apply(process = udf)
@@ -309,13 +314,15 @@ To invoke a UDF like this, the apply_neighborhood method is most suitable:
309314
310315.. code-block :: python
311316
312- udf_code = Path(' udf_modify_spatial.py' ).read_text()
317+ udf_code = Path(" udf_modify_spatial.py" ).read_text()
313318 cube_updated = cube.apply_neighborhood(
314- lambda data : data.run_udf(udf = udf_code, runtime = ' Python-Jep' , context = dict ()),
319+ lambda data : data.run_udf(udf = udf_code, runtime = " Python-Jep" , context = dict ()),
315320 size = [
316- {' dimension' : ' x' , ' value' : 128 , ' unit' : ' px' },
317- {' dimension' : ' y' , ' value' : 128 , ' unit' : ' px' }
318- ], overlap = [])
321+ {" dimension" : " x" , " value" : 128 , " unit" : " px" },
322+ {" dimension" : " y" , " value" : 128 , " unit" : " px" },
323+ ],
324+ overlap = [],
325+ )
319326
320327
321328
@@ -350,13 +357,17 @@ the datacube.
350357
351358.. code-block :: python
352359
353- output_cube = inputs_cube.apply_neighborhood(my_udf, size = [
354- {' dimension' : ' x' , ' value' : 112 , ' unit' : ' px' },
355- {' dimension' : ' y' , ' value' : 112 , ' unit' : ' px' }
356- ], overlap = [
357- {' dimension' : ' x' , ' value' : 8 , ' unit' : ' px' },
358- {' dimension' : ' y' , ' value' : 8 , ' unit' : ' px' }
359- ])
360+ output_cube = inputs_cube.apply_neighborhood(
361+ my_udf,
362+ size = [
363+ {" dimension" : " x" , " value" : 112 , " unit" : " px" },
364+ {" dimension" : " y" , " value" : 112 , " unit" : " px" },
365+ ],
366+ overlap = [
367+ {" dimension" : " x" , " value" : 8 , " unit" : " px" },
368+ {" dimension" : " y" , " value" : 8 , " unit" : " px" },
369+ ],
370+ )
360371
361372
362373
@@ -396,7 +407,7 @@ and apply it along a dimension:
396407
397408.. code-block :: python
398409
399- smoothing_udf = openeo.UDF .from_file(' smooth_savitzky_golay.py' )
410+ smoothing_udf = openeo.UDF .from_file(" smooth_savitzky_golay.py" )
400411 smoothed_evi = evi_cube_masked.apply_dimension(smoothing_udf, dimension = " t" )
401412
402413
@@ -630,6 +641,7 @@ For example: to discover the shape of the data cube chunk that you receive in yo
630641 from openeo.udf import inspect
631642 import xarray
632643
644+
633645 def apply_datacube (cube : xarray.DataArray, context : dict ) -> xarray.DataArray:
634646 inspect(data = [cube.shape], message = " UDF logging shape of my cube" )
635647 cube.values = 0.0001 * cube.values
0 commit comments