Open-EO / openeo-python-client

Python client API for OpenEO
https://open-eo.github.io/openeo-python-client/
Apache License 2.0
156 stars 42 forks source link

spatial coordinates are lost with execute_local_udf #549

Closed soxofaan closed 8 months ago

soxofaan commented 8 months ago

from https://discuss.eodc.eu/t/how-to-export-udfdata/705

execute_local_udf does not allow access to georeferenced x/y coordinates

soxofaan commented 8 months ago

example:

import openeo
from openeo.udf import execute_local_udf

udf = openeo.UDF("""
import xarray
def apply_datacube(cube: xarray.DataArray, context: dict) -> xarray.DataArray:
    print(f"{cube.coords['t']=}")
    print(f"{cube.coords['x']=}")
    print(f"{cube.coords['y']=}")
    return cube
""")

res = execute_local_udf(udf=udf, datacube="tmp.nc")

results in

cube.coords['t']=<xarray.DataArray 't' (t: 3)>
array(['2018-09-03T00:00:00.000000000', '2018-09-05T00:00:00.000000000',
       '2018-09-08T00:00:00.000000000'], dtype='datetime64[ns]')
Coordinates:
  * t        (t) datetime64[ns] 2018-09-03 2018-09-05 2018-09-08
Attributes:
    standard_name:  t
    long_name:      t
    axis:           T
cube.coords['x']=<xarray.DataArray 'x' (x: 219)>
array([  0,   1,   2, ..., 216, 217, 218])
Dimensions without coordinates: x
cube.coords['y']=<xarray.DataArray 'y' (y: 339)>
array([  0,   1,   2, ..., 336, 337, 338])
Dimensions without coordinates: y

Note how the x and y coordinates are just non-georeferenced ranges

soxofaan commented 8 months ago

I found that the original coordinates are explicitly dropped here: https://github.com/Open-EO/openeo-python-client/blob/70b43f2c98d444f05f8c6d40170dbee13adaa5d9/openeo/udf/run_code.py#L232

Dropping the original coordinates was in the original implementation of execute_local_udf introduced in 0962e00e0347817c036294aa057327499ad3a491 but I have no idea why this was done

soxofaan commented 8 months ago

merged in eddfa77