From 18125b404e97db617febc73a1ffb362ea655a15d Mon Sep 17 00:00:00 2001 From: Michael Sumner Date: Wed, 13 May 2026 02:00:25 +0000 Subject: [PATCH 1/2] fix affine determination for ascending y coordinate --- src/rasterix/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rasterix/utils.py b/src/rasterix/utils.py index 87acbc4..1542a95 100644 --- a/src/rasterix/utils.py +++ b/src/rasterix/utils.py @@ -127,8 +127,9 @@ def get_affine( dx = (x[1] - x[0]).item() dy = (y[1] - y[0]).item() return Affine.translation( - x[0].item() - dx / 2, (y[0] if dy < 0 else y[-1]).item() - dy / 2 - ) * Affine.scale(dx, dy) + x[0].item() - dx/2, + y[0].item() - dy/2 +) * Affine.scale(dx, dy) _ZarrConventionRegistration = TypedDict("_ZarrConventionRegistration", {"proj:": str}) From 6bcd7595a088a223c18606dfbf4ba25b0bdbd1a8 Mon Sep 17 00:00:00 2001 From: Michael Sumner Date: Wed, 13 May 2026 14:45:32 +0000 Subject: [PATCH 2/2] unit test for y ascending --- tests/test_raster_index.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/test_raster_index.py b/tests/test_raster_index.py index 9443faf..44de5d5 100644 --- a/tests/test_raster_index.py +++ b/tests/test_raster_index.py @@ -944,3 +944,26 @@ def test_tolerance_in_concat(): with set_options(transform_rtol=1e-9): _assert_transforms_are_compatible(a1, a2) # passes with 1e-9 + + +def test_get_affine_preserves_ascending_y(): + nx, ny = 4, 3 + lon = np.array([-179.5, -89.5, 0.5, 89.5]) # ascending + lat_asc = np.array([-60.0, 0.0, 60.0]) # ascending + lat_desc = lat_asc[::-1] # descending + + ds_asc = xr.Dataset( + {"foo": (("lat", "lon"), np.zeros((ny, nx)))}, + coords={"lat": lat_asc, "lon": lon}, + ) + ds_desc = xr.Dataset( + {"foo": (("lat", "lon"), np.zeros((ny, nx)))}, + coords={"lat": lat_desc, "lon": lon}, + ) + + out_asc = assign_index(ds_asc, x_dim="lon", y_dim="lat") + out_desc = assign_index(ds_desc, x_dim="lon", y_dim="lat") + + # The materialized coords should round-trip the input direction + np.testing.assert_allclose(out_asc.lat.values, lat_asc) + np.testing.assert_allclose(out_desc.lat.values, lat_desc)