Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/rasterix/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
23 changes: 23 additions & 0 deletions tests/test_raster_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)