|
10 | 10 | import xarray as xr
|
11 | 11 |
|
12 | 12 | GEOMETRY_CONTAINER_NAME = "geometry_container"
|
| 13 | +FEATURES_DIM_NAME = "features" |
13 | 14 |
|
14 | 15 | __all__ = [
|
15 | 16 | "decode_geometries",
|
|
34 | 35 |
|
35 | 36 | @dataclass
|
36 | 37 | class GeometryNames:
|
| 38 | + """Helper class to ease handling of all the variable names needed for CF geometries.""" |
| 39 | + |
37 | 40 | def __init__(self, suffix: str = "", grid_mapping: str | None = None):
|
38 | 41 | self.container_name: str = GEOMETRY_CONTAINER_NAME + suffix
|
39 | 42 | self.node_dim: str = "node" + suffix
|
@@ -315,7 +318,7 @@ def encode_geometries(ds: xr.Dataset):
|
315 | 318 | def reshape_unique_geometries(
|
316 | 319 | ds: xr.Dataset,
|
317 | 320 | geom_var: str = "geometry",
|
318 |
| - new_dim: str = "features", |
| 321 | + new_dim: str = FEATURES_DIM_NAME, |
319 | 322 | ) -> xr.Dataset:
|
320 | 323 | """Reshape a dataset containing a geometry variable so that all unique features are
|
321 | 324 | identified along a new dimension.
|
@@ -524,7 +527,7 @@ def points_to_cf(pts: xr.DataArray | Sequence, *, names: GeometryNames | None =
|
524 | 527 | coord = pts[dim] if dim in pts.coords else None
|
525 | 528 | pts_ = pts.values.tolist()
|
526 | 529 | else:
|
527 |
| - dim = "features" |
| 530 | + dim = FEATURES_DIM_NAME |
528 | 531 | coord = None
|
529 | 532 | pts_ = pts
|
530 | 533 |
|
@@ -599,8 +602,8 @@ def cf_to_points(ds: xr.Dataset):
|
599 | 602 | node_count_name = ds[container_name].attrs.get("node_count")
|
600 | 603 | if node_count_name is None:
|
601 | 604 | # No node_count means all geometries are single points (node_count = 1)
|
602 |
| - # And if we had no coordinates, then the dimension defaults to "features" |
603 |
| - feat_dim = feat_dim or "features" |
| 605 | + # And if we had no coordinates, then the dimension defaults to FEATURES_DIM_NAME |
| 606 | + feat_dim = feat_dim or FEATURES_DIM_NAME |
604 | 607 | node_count = xr.DataArray([1] * xy.shape[0], dims=(feat_dim,))
|
605 | 608 | if feat_dim in ds.coords:
|
606 | 609 | node_count = node_count.assign_coords({feat_dim: ds[feat_dim]})
|
|
0 commit comments