|
| 1 | +"""Tests for CRS metadata embedding in Zarr attributes.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | +import xarray as xr |
| 6 | + |
| 7 | +from eopf_geozarr.conversion.geozarr import prepare_dataset_with_crs_info |
| 8 | + |
| 9 | +EPSG_32632 = 32632 |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def utm_dataset(): |
| 14 | + """Minimal UTM dataset.""" |
| 15 | + return xr.Dataset( |
| 16 | + {"data": (["y", "x"], np.random.rand(10000, 11000))}, |
| 17 | + coords={ |
| 18 | + "x": np.arange(0, 110000, 10, dtype=np.float64), |
| 19 | + "y": np.arange(5300000, 5200000, -10, dtype=np.float64), |
| 20 | + }, |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def zarr_roundtrip(utm_dataset, tmp_path): |
| 26 | + """Dataset after Zarr write/read.""" |
| 27 | + ds = prepare_dataset_with_crs_info(utm_dataset, reference_crs=f"EPSG:{EPSG_32632}") |
| 28 | + path = tmp_path / "test.zarr" |
| 29 | + ds.drop_encoding().to_zarr(path, mode="w", zarr_format=3, consolidated=True) |
| 30 | + return xr.open_zarr(path, zarr_format=3, consolidated=True) |
| 31 | + |
| 32 | + |
| 33 | +def test_crs_attrs_embedded(utm_dataset): |
| 34 | + """CRS metadata written to attrs.""" |
| 35 | + ds = prepare_dataset_with_crs_info(utm_dataset, reference_crs=f"EPSG:{EPSG_32632}") |
| 36 | + assert ds.attrs["proj:epsg"] == EPSG_32632 |
| 37 | + assert ds.attrs["proj:code"] == f"EPSG:{EPSG_32632}" |
| 38 | + |
| 39 | + |
| 40 | +def test_crs_persists_through_zarr(zarr_roundtrip): |
| 41 | + """CRS survives Zarr cycle.""" |
| 42 | + assert zarr_roundtrip.attrs["proj:epsg"] == EPSG_32632 |
| 43 | + |
| 44 | + |
| 45 | +def test_rioxarray_reads_crs(zarr_roundtrip): |
| 46 | + """rioxarray reads CRS.""" |
| 47 | + assert zarr_roundtrip.rio.crs.to_epsg() == EPSG_32632 |
0 commit comments