diff --git a/src/eopf_geozarr/conversion/utils.py b/src/eopf_geozarr/conversion/utils.py index 845c2044..043ff56a 100644 --- a/src/eopf_geozarr/conversion/utils.py +++ b/src/eopf_geozarr/conversion/utils.py @@ -3,7 +3,16 @@ from __future__ import annotations from collections.abc import Mapping -from typing import TYPE_CHECKING, Any, Protocol, TypedDict, TypeGuard, cast, runtime_checkable +from typing import ( + TYPE_CHECKING, + Any, + Literal, + Protocol, + TypedDict, + TypeGuard, + cast, + runtime_checkable, +) import numpy as np import structlog @@ -78,14 +87,7 @@ def _bbox_from_coordinates(dataset: xr.Dataset) -> list[float]: along an axis, because the pixel size is then unknown. """ if not _has_regular_xy_grid(dataset): - x_coords = dataset.coords["x"].values - y_coords = dataset.coords["y"].values - return [ - float(x_coords.min()), - float(y_coords.min()), - float(x_coords.max()), - float(y_coords.max()), - ] + return _coordinate_extent(dataset) pixel_size_x, _, x_min, _, neg_pixel_size_y, y_max = _transform_from_coordinates(dataset) x_max = x_min + dataset.sizes["x"] * pixel_size_x @@ -378,10 +380,36 @@ def get_subroots(groups: tuple[str, ...]) -> set[str] | None: return subroots or None +def _node_transform_from_coordinates(dataset: xr.Dataset) -> GeoTransform: + """Affine transform from array indices to the x/y node coordinates. + + For a node-registered grid, index `(0, 0)` is at `(x[0], y[0])`: the coordinates + are not moved by a half cell. Requires `_has_regular_xy_grid(dataset)`. + """ + x_coords = dataset.coords["x"].values + y_coords = dataset.coords["y"].values + step_x = float(x_coords[1] - x_coords[0]) + step_y = float(y_coords[1] - y_coords[0]) + return (step_x, 0.0, float(x_coords[0]), 0.0, step_y, float(y_coords[0])) + + +def _coordinate_extent(dataset: xr.Dataset) -> list[float]: + """Extent ``[xmin, ymin, xmax, ymax]`` of the x/y coordinate values.""" + x_coords = dataset.coords["x"].values + y_coords = dataset.coords["y"].values + return [ + float(x_coords.min()), + float(y_coords.min()), + float(x_coords.max()), + float(y_coords.max()), + ] + + def write_dataset_geo_metadata( dataset: xr.Dataset, grid_mapping_var_name: str = "spatial_ref", crs: CRS | None = None, + registration: Literal["pixel", "node"] = "pixel", ) -> xr.Dataset: """ Return a copy of the dataset with geographic metadata added; `dataset` is not modified. @@ -391,6 +419,11 @@ def write_dataset_geo_metadata( dataset: Dataset to add metadata to grid_mapping_var_name: Name for grid mapping variable crs: Coordinate Reference System to use (if None, attempts to detect from dataset) + registration: `spatial:registration` of the grid. With `"pixel"`, the x/y + coordinates are cell centres, and `spatial:bbox` and `spatial:transform` + refer to the outer cell edges. With `"node"`, the values are at the x/y + coordinates, and `spatial:bbox` and `spatial:transform` refer to these + coordinates (e.g. the Sentinel-2 sun and viewing angle grids). Returns: The dataset with the metadata, or an unchanged copy if no CRS is found. @@ -416,7 +449,14 @@ def write_dataset_geo_metadata( dataset.rio.write_grid_mapping(grid_mapping_var_name, inplace=True) dataset.attrs["grid_mapping"] = grid_mapping_var_name + has_xy_dims = "x" in dataset.dims and "y" in dataset.dims for var in dataset.data_vars.values(): + # A variable without the x/y dimensions (e.g. a mean angle per band) + # is not on the grid, so it gets no grid mapping. + if has_xy_dims and not {"x", "y"} <= set(var.dims): + var.attrs.pop("grid_mapping", None) + var.encoding.pop("grid_mapping", None) + continue var.rio.write_grid_mapping(grid_mapping_var_name, inplace=True) var.attrs["grid_mapping"] = grid_mapping_var_name @@ -427,25 +467,36 @@ def write_dataset_geo_metadata( # Assemble spatial convention data spatial_data: spatial_cm.SpatialAttrs = { "spatial:dimensions": ["y", "x"], # Required field - "spatial:registration": "pixel", # Default registration type + "spatial:registration": registration, } # Calculate and add spatial bbox if coordinates are available if "x" in dataset.coords and "y" in dataset.coords: - spatial_data["spatial:bbox"] = _bbox_from_coordinates(dataset) + if registration == "node": + spatial_data["spatial:bbox"] = _coordinate_extent(dataset) + if _has_regular_xy_grid(dataset): + spatial_data["spatial:transform"] = list( + _node_transform_from_coordinates(dataset) + ) + else: + spatial_data["spatial:bbox"] = _bbox_from_coordinates(dataset) - if _has_spatial_transform(dataset): - spatial_transform = _preferred_spatial_transform(dataset) - # Only add spatial:transform if we have valid transform data (not all zeros) - if not all(t == 0 for t in spatial_transform): - spatial_data["spatial:transform"] = list(spatial_transform) + if _has_spatial_transform(dataset): + spatial_transform = _preferred_spatial_transform(dataset) + # Only add spatial:transform if we have valid transform data (not all zeros) + if not all(t == 0 for t in spatial_transform): + spatial_data["spatial:transform"] = list(spatial_transform) - # Add spatial shape if data variables exist + # Add spatial shape if data variables exist. Take it from the x/y + # dimensions: the first variable is not always on the grid. if dataset.data_vars: - first_var = next(iter(dataset.data_vars.values())) - if first_var.ndim >= 2: - height, width = first_var.shape[-2:] - spatial_data["spatial:shape"] = [height, width] + if has_xy_dims: + spatial_data["spatial:shape"] = [dataset.sizes["y"], dataset.sizes["x"]] + else: + first_var = next(iter(dataset.data_vars.values())) + if first_var.ndim >= 2: + height, width = first_var.shape[-2:] + spatial_data["spatial:shape"] = [height, width] # Build validated spatial + proj convention attrs (data + CMOs) via zarr-cm dataset.attrs.update(build_convention_attrs(spatial=spatial_data, crs=crs)) @@ -462,6 +513,7 @@ def stream_write_dataset( add_geo_metadata: bool = False, chunk_and_shard_coords: bool = True, crs: CRS | None = None, + registration: Literal["pixel", "node"] = "pixel", ) -> xr.Dataset: """ Stream write a lazy dataset with advanced chunking and sharding. @@ -474,6 +526,8 @@ def stream_write_dataset( dataset_path: Output path for dataset encoding: Encoding dictionary for variables enable_sharding: Enable Zarr v3 sharding + registration: `spatial:registration` of the grid, used with + `add_geo_metadata` (see `write_dataset_geo_metadata`) Returns: Written dataset @@ -504,7 +558,7 @@ def stream_write_dataset( ) if add_geo_metadata: - dataset = write_dataset_geo_metadata(dataset, crs=crs) + dataset = write_dataset_geo_metadata(dataset, crs=crs, registration=registration) # Sanitize NaN values in dataset attributes before writing dataset = fs_utils.sanitize_dataset_attributes(dataset) diff --git a/src/eopf_geozarr/s2_optimization/s2_multiscale.py b/src/eopf_geozarr/s2_optimization/s2_multiscale.py index f4380490..e4cbab47 100644 --- a/src/eopf_geozarr/s2_optimization/s2_multiscale.py +++ b/src/eopf_geozarr/s2_optimization/s2_multiscale.py @@ -145,6 +145,9 @@ def applies_to(self, s2_type: S2Type | None) -> bool: ), ) +GEOMETRY_PATH = "/conditions/geometry" +"""Group of the sun and viewing angle grids (node-registered, in the tile CRS).""" + MultiscalesFlavor = Literal["experimental_multiscales_convention"] @@ -681,7 +684,11 @@ def create_multiscale_from_datatree( for name in scalar_coords: encoding.pop(str(name), None) - add_geo_metadata = any( + # The sun and viewing angle grids in `/conditions/geometry` are in the tile CRS. + # Their values are at the grid nodes: `(x[0], y[0])` is the upper-left corner + # of the tile, not the centre of a cell. + is_geometry_group = group_path == GEOMETRY_PATH + add_geo_metadata = is_geometry_group or any( m in group_path for m in ("/measurements/", "/quality/", "/conditions/mask") ) ds_out = utils.stream_write_dataset( @@ -691,6 +698,7 @@ def create_multiscale_from_datatree( encoding=encoding, add_geo_metadata=add_geo_metadata, crs=crs, + registration="node" if is_geometry_group else "pixel", ) processed_groups[group_path] = ds_out diff --git a/tests/_test_data/optimized_geozarr_examples/S2A_MSIL2A_20251008T100041_N0511_R122_T32TQM_20251008T122613.json b/tests/_test_data/optimized_geozarr_examples/S2A_MSIL2A_20251008T100041_N0511_R122_T32TQM_20251008T122613.json index 03c05fd2..c98de997 100644 --- a/tests/_test_data/optimized_geozarr_examples/S2A_MSIL2A_20251008T100041_N0511_R122_T32TQM_20251008T122613.json +++ b/tests/_test_data/optimized_geozarr_examples/S2A_MSIL2A_20251008T100041_N0511_R122_T32TQM_20251008T122613.json @@ -5,7 +5,49 @@ "attributes": {}, "members": { "geometry": { - "attributes": {}, + "attributes": { + "grid_mapping": "spatial_ref", + "proj:code": "EPSG:32632", + "spatial:bbox": [ + 302386.95652173914, + 4892626.956521739, + 407413.04347826086, + 4997653.043478261 + ], + "spatial:dimensions": [ + "y", + "x" + ], + "spatial:registration": "node", + "spatial:shape": [ + 23, + 23 + ], + "spatial:transform": [ + 4773.913043478271, + 0.0, + 302386.95652173914, + 0.0, + -4773.913043478504, + 4997653.043478261 + ], + "zarr_conventions": [ + { + "description": "Spatial coordinate information", + "name": "spatial", + "schema_url": "https://raw.githubusercontent.com/zarr-conventions/spatial/refs/tags/v0.1/schema.json", + "spec_url": "https://github.com/zarr-conventions/spatial/blob/v0.1/README.md", + "uuid": "689b58e2-cf7b-45e0-9fff-9cfc0883d6b4" + }, + { + "description": "Coordinate reference system information for geospatial data", + "name": "proj", + "schema_url": "https://raw.githubusercontent.com/zarr-conventions/proj/refs/tags/v0.1/schema.json", + "spec_url": "https://github.com/zarr-conventions/proj/blob/v0.1/README.md", + "uuid": "f17cb550-5864-4468-aeb7-f3180cfb622f" + } + ] + }, "members": { "angle": { "attributes": {}, @@ -271,9 +313,65 @@ "storage_transformers": [], "zarr_format": 3 }, + "spatial_ref": { + "attributes": { + "crs_wkt": "PROJCS[\"WGS 84 / UTM zone 32N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",9],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"32632\"]]", + "false_easting": 500000.0, + "false_northing": 0.0, + "geographic_crs_name": "WGS 84", + "grid_mapping_name": "transverse_mercator", + "horizontal_datum_name": "World Geodetic System 1984", + "inverse_flattening": 298.257223563, + "latitude_of_projection_origin": 0.0, + "longitude_of_central_meridian": 9.0, + "longitude_of_prime_meridian": 0.0, + "prime_meridian_name": "Greenwich", + "projected_crs_name": "WGS 84 / UTM zone 32N", + "reference_ellipsoid_name": "WGS 84", + "scale_factor_at_central_meridian": 0.9996, + "semi_major_axis": 6378137.0, + "semi_minor_axis": 6356752.314245179, + "spatial_ref": "PROJCS[\"WGS 84 / UTM zone 32N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",9],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"32632\"]]" + }, + "chunk_grid": { + "configuration": { + "chunk_shape": [] + }, + "name": "regular" + }, + "chunk_key_encoding": { + "configuration": { + "separator": "/" + }, + "name": "default" + }, + "codecs": [ + { + "configuration": { + "endian": "little" + }, + "name": "bytes" + }, + { + "configuration": { + "checksum": false, + "level": 0 + }, + "name": "zstd" + } + ], + "data_type": "int64", + "dimension_names": null, + "fill_value": 0, + "node_type": "array", + "shape": [], + "storage_transformers": [], + "zarr_format": 3 + }, "sun_angles": { "attributes": { - "_FillValue": "AAAAAAAA+H8=" + "_FillValue": "AAAAAAAA+H8=", + "grid_mapping": "spatial_ref" }, "chunk_grid": { "configuration": { @@ -351,7 +449,8 @@ }, "viewing_incidence_angles": { "attributes": { - "_FillValue": "AAAAAAAA+H8=" + "_FillValue": "AAAAAAAA+H8=", + "grid_mapping": "spatial_ref" }, "chunk_grid": { "configuration": { diff --git a/tests/_test_data/optimized_geozarr_examples/S2B_MSIL1C_20250113T103309_N0511_R108_T32TLQ_20250113T122458.json b/tests/_test_data/optimized_geozarr_examples/S2B_MSIL1C_20250113T103309_N0511_R108_T32TLQ_20250113T122458.json index aa1ecf49..fdc6dcda 100644 --- a/tests/_test_data/optimized_geozarr_examples/S2B_MSIL1C_20250113T103309_N0511_R108_T32TLQ_20250113T122458.json +++ b/tests/_test_data/optimized_geozarr_examples/S2B_MSIL1C_20250113T103309_N0511_R108_T32TLQ_20250113T122458.json @@ -5,7 +5,49 @@ "attributes": {}, "members": { "geometry": { - "attributes": {}, + "attributes": { + "grid_mapping": "spatial_ref", + "proj:code": "EPSG:32632", + "spatial:bbox": [ + 302386.95652173914, + 4892626.956521739, + 407413.04347826086, + 4997653.043478261 + ], + "spatial:dimensions": [ + "y", + "x" + ], + "spatial:registration": "node", + "spatial:shape": [ + 23, + 23 + ], + "spatial:transform": [ + 4773.913043478271, + 0.0, + 302386.95652173914, + 0.0, + -4773.913043478504, + 4997653.043478261 + ], + "zarr_conventions": [ + { + "description": "Spatial coordinate information", + "name": "spatial", + "schema_url": "https://raw.githubusercontent.com/zarr-conventions/spatial/refs/tags/v0.1/schema.json", + "spec_url": "https://github.com/zarr-conventions/spatial/blob/v0.1/README.md", + "uuid": "689b58e2-cf7b-45e0-9fff-9cfc0883d6b4" + }, + { + "description": "Coordinate reference system information for geospatial data", + "name": "proj", + "schema_url": "https://raw.githubusercontent.com/zarr-conventions/proj/refs/tags/v0.1/schema.json", + "spec_url": "https://github.com/zarr-conventions/proj/blob/v0.1/README.md", + "uuid": "f17cb550-5864-4468-aeb7-f3180cfb622f" + } + ] + }, "members": { "angle": { "attributes": {}, @@ -271,9 +313,65 @@ "storage_transformers": [], "zarr_format": 3 }, + "spatial_ref": { + "attributes": { + "crs_wkt": "PROJCS[\"WGS 84 / UTM zone 32N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",9],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"32632\"]]", + "false_easting": 500000.0, + "false_northing": 0.0, + "geographic_crs_name": "WGS 84", + "grid_mapping_name": "transverse_mercator", + "horizontal_datum_name": "World Geodetic System 1984", + "inverse_flattening": 298.257223563, + "latitude_of_projection_origin": 0.0, + "longitude_of_central_meridian": 9.0, + "longitude_of_prime_meridian": 0.0, + "prime_meridian_name": "Greenwich", + "projected_crs_name": "WGS 84 / UTM zone 32N", + "reference_ellipsoid_name": "WGS 84", + "scale_factor_at_central_meridian": 0.9996, + "semi_major_axis": 6378137.0, + "semi_minor_axis": 6356752.314245179, + "spatial_ref": "PROJCS[\"WGS 84 / UTM zone 32N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",9],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"32632\"]]" + }, + "chunk_grid": { + "configuration": { + "chunk_shape": [] + }, + "name": "regular" + }, + "chunk_key_encoding": { + "configuration": { + "separator": "/" + }, + "name": "default" + }, + "codecs": [ + { + "configuration": { + "endian": "little" + }, + "name": "bytes" + }, + { + "configuration": { + "checksum": false, + "level": 0 + }, + "name": "zstd" + } + ], + "data_type": "int64", + "dimension_names": null, + "fill_value": 0, + "node_type": "array", + "shape": [], + "storage_transformers": [], + "zarr_format": 3 + }, "sun_angles": { "attributes": { - "_FillValue": "AAAAAAAA+H8=" + "_FillValue": "AAAAAAAA+H8=", + "grid_mapping": "spatial_ref" }, "chunk_grid": { "configuration": { @@ -351,7 +449,8 @@ }, "viewing_incidence_angles": { "attributes": { - "_FillValue": "AAAAAAAA+H8=" + "_FillValue": "AAAAAAAA+H8=", + "grid_mapping": "spatial_ref" }, "chunk_grid": { "configuration": { diff --git a/tests/_test_data/optimized_geozarr_examples/S2C_MSIL2A_20250811T112131_N0511_R037_T29TPF_20250811T152216.json b/tests/_test_data/optimized_geozarr_examples/S2C_MSIL2A_20250811T112131_N0511_R037_T29TPF_20250811T152216.json index 6a95ac57..bd4d8443 100644 --- a/tests/_test_data/optimized_geozarr_examples/S2C_MSIL2A_20250811T112131_N0511_R037_T29TPF_20250811T152216.json +++ b/tests/_test_data/optimized_geozarr_examples/S2C_MSIL2A_20250811T112131_N0511_R037_T29TPF_20250811T152216.json @@ -5,7 +5,49 @@ "attributes": {}, "members": { "geometry": { - "attributes": {}, + "attributes": { + "grid_mapping": "spatial_ref", + "proj:code": "EPSG:32632", + "spatial:bbox": [ + 302386.95652173914, + 4892626.956521739, + 407413.04347826086, + 4997653.043478261 + ], + "spatial:dimensions": [ + "y", + "x" + ], + "spatial:registration": "node", + "spatial:shape": [ + 23, + 23 + ], + "spatial:transform": [ + 4773.913043478271, + 0.0, + 302386.95652173914, + 0.0, + -4773.913043478504, + 4997653.043478261 + ], + "zarr_conventions": [ + { + "description": "Spatial coordinate information", + "name": "spatial", + "schema_url": "https://raw.githubusercontent.com/zarr-conventions/spatial/refs/tags/v0.1/schema.json", + "spec_url": "https://github.com/zarr-conventions/spatial/blob/v0.1/README.md", + "uuid": "689b58e2-cf7b-45e0-9fff-9cfc0883d6b4" + }, + { + "description": "Coordinate reference system information for geospatial data", + "name": "proj", + "schema_url": "https://raw.githubusercontent.com/zarr-conventions/proj/refs/tags/v0.1/schema.json", + "spec_url": "https://github.com/zarr-conventions/proj/blob/v0.1/README.md", + "uuid": "f17cb550-5864-4468-aeb7-f3180cfb622f" + } + ] + }, "members": { "angle": { "attributes": {}, @@ -271,9 +313,65 @@ "storage_transformers": [], "zarr_format": 3 }, + "spatial_ref": { + "attributes": { + "crs_wkt": "PROJCS[\"WGS 84 / UTM zone 32N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",9],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"32632\"]]", + "false_easting": 500000.0, + "false_northing": 0.0, + "geographic_crs_name": "WGS 84", + "grid_mapping_name": "transverse_mercator", + "horizontal_datum_name": "World Geodetic System 1984", + "inverse_flattening": 298.257223563, + "latitude_of_projection_origin": 0.0, + "longitude_of_central_meridian": 9.0, + "longitude_of_prime_meridian": 0.0, + "prime_meridian_name": "Greenwich", + "projected_crs_name": "WGS 84 / UTM zone 32N", + "reference_ellipsoid_name": "WGS 84", + "scale_factor_at_central_meridian": 0.9996, + "semi_major_axis": 6378137.0, + "semi_minor_axis": 6356752.314245179, + "spatial_ref": "PROJCS[\"WGS 84 / UTM zone 32N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",9],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"32632\"]]" + }, + "chunk_grid": { + "configuration": { + "chunk_shape": [] + }, + "name": "regular" + }, + "chunk_key_encoding": { + "configuration": { + "separator": "/" + }, + "name": "default" + }, + "codecs": [ + { + "configuration": { + "endian": "little" + }, + "name": "bytes" + }, + { + "configuration": { + "checksum": false, + "level": 0 + }, + "name": "zstd" + } + ], + "data_type": "int64", + "dimension_names": null, + "fill_value": 0, + "node_type": "array", + "shape": [], + "storage_transformers": [], + "zarr_format": 3 + }, "sun_angles": { "attributes": { - "_FillValue": "AAAAAAAA+H8=" + "_FillValue": "AAAAAAAA+H8=", + "grid_mapping": "spatial_ref" }, "chunk_grid": { "configuration": { @@ -351,7 +449,8 @@ }, "viewing_incidence_angles": { "attributes": { - "_FillValue": "AAAAAAAA+H8=" + "_FillValue": "AAAAAAAA+H8=", + "grid_mapping": "spatial_ref" }, "chunk_grid": { "configuration": { diff --git a/tests/test_s2_multiscale.py b/tests/test_s2_multiscale.py index 056439bb..851c8009 100644 --- a/tests/test_s2_multiscale.py +++ b/tests/test_s2_multiscale.py @@ -7,7 +7,7 @@ import pathlib from collections.abc import Mapping, Sequence from itertools import pairwise -from typing import Any +from typing import Any, cast import numpy as np import pytest @@ -804,6 +804,89 @@ def test_create_multiscale_reuses_native_aux_levels(tmp_path: pathlib.Path) -> N np.testing.assert_array_equal(ds[var_name].values, expected) +def _convention_names(attrs: Mapping[str, Any]) -> set[str]: + """Names of the conventions declared in `zarr_conventions`.""" + return {c["name"] for c in cast("list[dict[str, Any]]", attrs["zarr_conventions"])} + + +def test_create_multiscale_declares_crs_on_conditions_groups(tmp_path: pathlib.Path) -> None: + """`conditions/geometry` and `conditions/mask/detector_footprint/r*` declare the tile CRS. + + The angle grids are node-registered: `(x[0], y[0])` is the upper-left corner of + the tile, so `spatial:transform` starts there and is not moved by a half cell. + Arrays without the x/y dimensions get no grid mapping. + See EOPF-Explorer/data-model#346. + """ + dt_input = _make_minimal_s2_datatree(raw=False) + rng = np.random.default_rng(2) + + # 23 x 23 nodes with a step of 5000 m, from the upper-left corner of the tile. + size, step, ulx, uly = 23, 5000.0, 600000.0, 7800000.0 + x = ulx + step * np.arange(size) + y = uly - step * np.arange(size) + dt_input["conditions/geometry"] = xr.DataTree( + xr.Dataset( + { + "mean_sun_angles": (["angle"], rng.uniform(0, 180, 2)), + "sun_angles": (["angle", "y", "x"], rng.uniform(0, 180, (2, size, size))), + "mean_viewing_incidence_angles": (["band", "angle"], rng.uniform(0, 180, (3, 2))), + "viewing_incidence_angles": ( + ["band", "detector", "angle", "y", "x"], + rng.uniform(0, 180, (3, 2, 2, size, size)), + ), + }, + coords={ + "x": x, + "y": y, + "angle": ["zenith", "azimuth"], + "band": ["b01", "b02", "b03"], + "detector": [1, 2], + }, + ) + ) + r60m_coords = dt_input["measurements/reflectance/r60m"].to_dataset().coords + footprint = xr.DataArray( + rng.integers(0, 3, size=(20, 20), dtype="uint8"), dims=["y", "x"], coords=r60m_coords + ) + dt_input["conditions/mask/detector_footprint/r60m"] = xr.DataTree( + xr.Dataset({"b01": footprint}) + ) + output_path = str(tmp_path / "output.zarr") + + with capture_logs(): + create_multiscale_from_datatree( + dt_input, + output_group=zarr.create_group(output_path), + enable_sharding=False, + spatial_chunk=_BEHAVIOR_SPATIAL_CHUNK, + crs=CRS.from_epsg(32636), + ) + + geometry = zarr.open_group(output_path, path="conditions/geometry", mode="r") + attrs = geometry.attrs.asdict() + assert _convention_names(attrs) == {"spatial", "proj"} + assert attrs["proj:code"] == "EPSG:32636" + assert attrs["spatial:dimensions"] == ["y", "x"] + assert attrs["spatial:registration"] == "node" + assert attrs["spatial:shape"] == [size, size] + assert attrs["spatial:transform"] == [step, 0.0, ulx, 0.0, -step, uly] + assert attrs["spatial:bbox"] == [ulx, uly - step * (size - 1), ulx + step * (size - 1), uly] + + assert geometry["sun_angles"].attrs["grid_mapping"] == "spatial_ref" + assert geometry["viewing_incidence_angles"].attrs["grid_mapping"] == "spatial_ref" + assert "grid_mapping" not in geometry["mean_sun_angles"].attrs + assert "grid_mapping" not in geometry["mean_viewing_incidence_angles"].attrs + + detector_footprint = zarr.open_group( + output_path, path="conditions/mask/detector_footprint/r60m", mode="r" + ).attrs.asdict() + assert _convention_names(detector_footprint) == {"spatial", "proj"} + assert detector_footprint["proj:code"] == "EPSG:32636" + assert detector_footprint["spatial:registration"] == "pixel" + assert detector_footprint["spatial:shape"] == [20, 20] + assert "spatial:transform" in detector_footprint + + def test_create_multiscale_builds_atmosphere_pyramid(tmp_path: pathlib.Path) -> None: """`quality/atmosphere` (aot, wvp) gets a pyramid like the reflectance bands.