Skip to content

Commit f60ab8f

Browse files
test: drive _FillValue masking test through create_geozarr_dataset (#172)
Replace the standalone xarray write with a real converter run: build a small float DataTree, invoke create_geozarr_dataset, then reopen the output with use_zarr_fill_value_as_mask=True and assert masking on the nodata patch. Exercises the converter's _FillValue + encoding path end-to-end, per @vincentsarago's review.
1 parent 7280485 commit f60ab8f

1 file changed

Lines changed: 51 additions & 26 deletions

File tree

‎tests/test_array_attrs.py‎

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,44 +101,69 @@ def test_float_measurements_have_fill_value(snapshot: dict) -> None:
101101

102102

103103
def test_fill_value_masking_roundtrip(tmp_path: pathlib.Path) -> None:
104-
"""End-to-end: a float array written with our ``_FillValue`` convention
105-
must round-trip through xarray's ``use_zarr_fill_value_as_mask=True``.
104+
"""End-to-end converter check: float arrays produced by
105+
``create_geozarr_dataset`` must round-trip through xarray's
106+
``use_zarr_fill_value_as_mask=True`` so NaN cells come back masked.
106107
107-
Mirrors the rio-tiler reader expectation: NaN-filled cells become masked
108-
when re-opened, all other cells remain unmasked.
108+
Mirrors the rio-tiler reader expectation. Builds a minimal float
109+
DataTree, runs the real converter, then reopens the output and
110+
asserts masking semantics on a measurement band.
109111
"""
110112
import numpy as np
111113
import xarray as xr
112114

113-
# Build a small float dataset following the converter's convention:
114-
# - attrs["_FillValue"] = np.nan (xarray encodes via FillValueCoder)
115-
# - encoding["fill_value"] = "NaN"
116-
data = np.arange(50 * 50, dtype="float32").reshape(50, 50)
117-
data[0:5, 0:5] = np.nan # nodata patch
118-
da = xr.DataArray(data, dims=("y", "x"), name="band")
119-
da.attrs["_FillValue"] = np.nan
120-
121-
ds = da.to_dataset()
122-
store = tmp_path / "roundtrip.zarr"
123-
ds.to_zarr(
124-
store,
125-
zarr_format=3,
126-
encoding={"band": {"fill_value": "NaN", "chunks": (25, 25)}},
127-
consolidated=True,
115+
from eopf_geozarr.conversion import create_geozarr_dataset
116+
117+
epsg_code = 32632
118+
x_min, x_max = 600000, 605120 # 5.12 km
119+
y_min, y_max = 5090000, 5095120
120+
nx = ny = 512
121+
122+
# Float reflectance band with a nodata patch (NaN) — what the converter
123+
# would normally see after CF decoding of scale_factor/add_offset.
124+
data = np.random.default_rng(42).uniform(0.0, 1.0, size=(ny, nx)).astype("float32")
125+
data[0:32, 0:32] = np.nan
126+
127+
ds = xr.Dataset(
128+
{"b04": (["y", "x"], data, {"long_name": "Red band (B04)"})},
129+
coords={
130+
"x": np.linspace(x_min, x_max, nx, endpoint=False),
131+
"y": np.linspace(y_max, y_min, ny, endpoint=False),
132+
},
133+
).rio.write_crs(f"EPSG:{epsg_code}")
134+
135+
dt = xr.DataTree()
136+
dt["measurements"] = xr.DataTree()
137+
dt["measurements/reflectance"] = xr.DataTree()
138+
dt["measurements/reflectance/r10m"] = ds
139+
140+
output_path = tmp_path / "fill_value_roundtrip.zarr"
141+
create_geozarr_dataset(
142+
dt_input=dt,
143+
groups=["/measurements/reflectance/r10m"],
144+
output_path=str(output_path),
145+
spatial_chunk=256,
146+
min_dimension=128,
147+
max_retries=1,
128148
)
129149

150+
band_group = output_path / "measurements" / "reflectance" / "r10m"
130151
reopened = xr.open_dataset(
131-
store,
152+
band_group,
132153
engine="zarr",
133154
zarr_format=3,
134155
consolidated=True,
135156
decode_times=False,
136157
decode_coords=False,
137158
use_zarr_fill_value_as_mask=True,
138159
)
139-
masked = reopened["band"].to_masked_array()
140-
141-
assert np.ma.is_masked(masked), "NaN cells should be masked when opened with mask=True"
142-
assert masked.mask[0, 0], "nodata corner cell must be masked"
143-
assert not masked.mask[-1, -1], "valid cell must not be masked"
144-
reopened.close()
160+
try:
161+
masked = reopened["b04"].to_masked_array()
162+
assert np.ma.is_masked(masked), (
163+
"NaN cells in converter output should be masked when opened with "
164+
"use_zarr_fill_value_as_mask=True"
165+
)
166+
assert masked.mask[0, 0], "nodata corner cell must be masked"
167+
assert not masked.mask[-1, -1], "valid cell must not be masked"
168+
finally:
169+
reopened.close()

0 commit comments

Comments
 (0)