22import  xarray 
33
44from  openeo .metadata  import  CollectionMetadata 
5- from  openeo .udf  import  XarrayDataCube 
65from  openeo .udf .debug  import  inspect 
76
87
98def  apply_metadata (input_metadata : CollectionMetadata , context : dict ) ->  CollectionMetadata :
10-     xstep  =  input_metadata .get ("x" , "step" )
11-     ystep  =  input_metadata .get ("y" , "step" )
12-     new_metadata  =  {
13-         "x" : {"type" : "spatial" , "axis" : "x" , "step" : xstep  /  2.0 , "reference_system" : 4326 },
14-         "y" : {"type" : "spatial" , "axis" : "y" , "step" : ystep  /  2.0 , "reference_system" : 4326 },
15-         "t" : {"type" : "temporal" },
16-     }
17-     return  CollectionMetadata (new_metadata )
9+     res =  [ d .step  /  2.0  for  d  in  input_metadata .spatial_dimensions ]
10+     return  input_metadata .resample_spatial (resolution = res )
1811
1912
2013def  fancy_upsample_function (array : np .array , factor : int  =  2 ) ->  np .array :
2114    assert  array .ndim  ==  3 
2215    return  array .repeat (factor , axis = - 1 ).repeat (factor , axis = - 2 )
2316
2417
25- def  apply_datacube (cube : XarrayDataCube , context : dict ) ->  XarrayDataCube :
26-     cubearray : xarray .DataArray  =  cube .get_array (). copy () +  60 
18+ def  apply_datacube (cube : xarray . DataArray , context : dict ) ->  xarray . DataArray :
19+     cubearray : xarray .DataArray  =  cube .copy () +  60 
2720
2821    # We make prediction and transform numpy array back to datacube 
2922
3023    # Pixel size of the original image 
31-     init_pixel_size_x  =  cubearray .coords ["x" ][- 1 ] -  cubearray .coords ["x" ][- 2 ]
32-     init_pixel_size_y  =  cubearray .coords ["y" ][- 1 ] -  cubearray .coords ["y" ][- 2 ]
24+     init_pixel_size_x  =  cubearray .coords ["x" ][- 1 ]. item ()  -  cubearray .coords ["x" ][- 2 ]. item () 
25+     init_pixel_size_y  =  cubearray .coords ["y" ][- 1 ]. item ()  -  cubearray .coords ["y" ][- 2 ]. item () 
3326
3427    if  cubearray .data .ndim  ==  4  and  cubearray .data .shape [0 ] ==  1 :
3528        cubearray  =  cubearray [0 ]
3629    predicted_array  =  fancy_upsample_function (cubearray .data , 2 )
37-     inspect (data = predicted_array , message = "predicted array" )
30+     inspect (data = predicted_array , message = f "predicted array" )
3831    coord_x  =  np .linspace (
39-         start = cube .get_array (). coords ["x" ]. min (),
40-         stop = cube .get_array (). coords ["x" ]. max () +  init_pixel_size_x ,
32+         start = cube .coords ["x" ][ 0 ]. item (),
33+         stop = cube .coords ["x" ][ - 1 ]. item () +  init_pixel_size_x ,
4134        num = predicted_array .shape [- 2 ],
4235        endpoint = False ,
4336    )
4437    coord_y  =  np .linspace (
45-         start = cube .get_array (). coords ["y" ].min (),
46-         stop = cube .get_array (). coords ["y" ].max () +  init_pixel_size_y ,
38+         start = cube .coords ["y" ].min (). item (),
39+         stop = cube .coords ["y" ].max (). item () +  init_pixel_size_y ,
4740        num = predicted_array .shape [- 1 ],
4841        endpoint = False ,
4942    )
@@ -53,4 +46,4 @@ def apply_datacube(cube: XarrayDataCube, context: dict) -> XarrayDataCube:
5346        coords = dict (x = coord_x , y = coord_y ),
5447    )
5548
56-     return  XarrayDataCube ( predicted_cube ) 
49+     return  predicted_cube 
0 commit comments