Skip to content

Commit 8f6e059

Browse files
resample_cube_spatial: align resampled coordinates (#306)
* add update to align resampled coordinates * run pre-commit * update resample * check spatial coordinates in resample * check spatial coordinates in resample
1 parent 9e828bb commit 8f6e059

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

openeo_processes_dask/process_implementations/cubes/resample.py

+62
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,68 @@ def resample_cube_spatial(
137137
data=data, projection=target_crs, resolution=target_resolution, method=method
138138
)
139139

140+
def align_arrays(a, b, res):
141+
desc = 1
142+
if len(a) == 1 and len(b) == 1:
143+
return a
144+
elif len(a) == 1 and len(b) > 1:
145+
if b[0] > b[1]:
146+
desc = -1
147+
elif len(a) > len(b):
148+
b0 = np.absolute(b[0] - a).argmin()
149+
blen = len(b)
150+
close = np.isclose(b, a[b0 : b0 + blen], atol=res * 0.99)
151+
if close.all():
152+
return a[b0 : b0 + blen]
153+
else:
154+
raise Exception("Coordinates could not be aligned! ")
155+
elif len(a) == len(b):
156+
if b[0] > b[1]:
157+
if a[0] < a[1]:
158+
a = np.flip(a)
159+
else:
160+
if a[0] > a[1]:
161+
a = np.flip(a)
162+
close = np.isclose(b, a, atol=res * 0.99)
163+
if close.all():
164+
return a
165+
else:
166+
raise Exception("Coordinates could not be aligned! ")
167+
else:
168+
if b[0] > b[1]:
169+
desc = -1
170+
if a[0] < a[1]:
171+
a = np.flip(a)
172+
else:
173+
if a[0] > a[1]:
174+
a = np.flip(a)
175+
a0 = np.absolute(b - a[0]).argmin()
176+
alen = len(a)
177+
close = np.isclose(a, b[a0 : a0 + alen], atol=res * 0.99)
178+
if not close.all():
179+
raise Exception("Coordinates could not be aligned! ")
180+
181+
new_b = np.arange(b[0], a[0], res * desc)
182+
new_b = np.append(new_b, a)
183+
new_b = np.append(new_b, np.flip(np.arange(b[-1], a[-1], -res * desc)))
184+
if len(b) != len(new_b):
185+
raise Exception("Coordinates could not be aligned! ")
186+
return new_b
187+
188+
x_data = resampled_data[resampled_data.openeo.x_dim[0]].values
189+
x_target = target[target.openeo.x_dim[0]].values
190+
if not (len(x_data) == len(x_target) and (x_data == x_target).all()):
191+
resampled_data[resampled_data.openeo.x_dim[0]] = align_arrays(
192+
x_target, x_data, target_resolution
193+
)
194+
195+
y_data = resampled_data[resampled_data.openeo.y_dim[0]].values
196+
y_target = target[target.openeo.y_dim[0]].values
197+
if not (len(y_data) == len(y_target) and (y_data == y_target).all()):
198+
resampled_data[resampled_data.openeo.y_dim[0]] = align_arrays(
199+
y_target, y_data, target_resolution
200+
)
201+
140202
return resampled_data
141203

142204

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "openeo-processes-dask"
3-
version = "2024.11.5"
3+
version = "2024.12.1"
44
description = "Python implementations of many OpenEO processes, dask-friendly by default."
55
authors = ["Lukas Weidenholzer <lukas.weidenholzer@eodc.eu>", "Sean Hoyal <sean.hoyal@eodc.eu>", "Valentina Hutter <valentina.hutter@eodc.eu>"]
66
maintainers = ["EODC Staff <support@eodc.eu>"]

tests/test_resample.py

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
import pytest
5+
import xarray as xr
56
from odc.geo.geobox import resolution_from_affine
67
from openeo_pg_parser_networkx.pg_schema import ParameterReference, TemporalInterval
78
from pyproj.crs import CRS
@@ -180,6 +181,8 @@ def test_resample_cube_spatial_small(
180181
)
181182

182183
assert list(output_cube.shape) == list(resampled_cube.shape)
184+
assert (output_cube["x"].values == resampled_cube["x"].values).all()
185+
assert (output_cube["y"].values == resampled_cube["y"].values).all()
183186

184187

185188
@pytest.mark.parametrize("size", [(6, 5, 30, 4)])

0 commit comments

Comments
 (0)