Skip to content

Commit 8b17871

Browse files
committed
Some cleanup
1 parent 8e55a3f commit 8b17871

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

cf_xarray/geometry.py

+26-15
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@
3737
class GeometryNames:
3838
"""Helper class to ease handling of all the variable names needed for CF geometries."""
3939

40-
def __init__(self, suffix: str = "", grid_mapping: str | None = None):
40+
def __init__(
41+
self,
42+
suffix: str = "",
43+
grid_mapping_name: str | None = None,
44+
grid_mapping: str | None = None,
45+
):
4146
self.container_name: str = GEOMETRY_CONTAINER_NAME + suffix
4247
self.node_dim: str = "node" + suffix
4348
self.node_count: str = "node_count" + suffix
@@ -51,24 +56,29 @@ def __init__(self, suffix: str = "", grid_mapping: str | None = None):
5156
self.attrs_x: dict[str, str] = {}
5257
self.attrs_y: dict[str, str] = {}
5358

59+
gmattr = {"grid_mapping": grid_mapping} if grid_mapping else {}
5460
# Special treatment of selected grid mappings
55-
if grid_mapping in ["latitude_longitude", "rotated_latitude_longitude"]:
61+
if grid_mapping_name in ["latitude_longitude", "rotated_latitude_longitude"]:
5662
# Special case for longitude_latitude type grid mappings
5763
self.coordinates_x = "lon"
5864
self.coordinates_y = "lat"
59-
if grid_mapping == "latitude_longitude":
60-
self.attrs_x = dict(units="degrees_east", standard_name="longitude")
61-
self.attrs_y = dict(units="degrees_north", standard_name="latitude")
62-
elif grid_mapping == "rotated_latitude_longitude":
65+
if grid_mapping_name == "latitude_longitude":
66+
self.attrs_x = dict(
67+
units="degrees_east", standard_name="longitude", **gmattr
68+
)
69+
self.attrs_y = dict(
70+
units="degrees_north", standard_name="latitude", **gmattr
71+
)
72+
elif grid_mapping_name == "rotated_latitude_longitude":
6373
self.attrs_x = dict(
64-
units="degrees_east", standard_name="grid_longitude"
74+
units="degrees_east", standard_name="grid_longitude", **gmattr
6575
)
6676
self.attrs_y = dict(
67-
units="degrees_north", standard_name="grid_latitude"
77+
units="degrees_north", standard_name="grid_latitude", **gmattr
6878
)
69-
elif grid_mapping is not None:
70-
self.attrs_x = dict(standard_name="projection_x_coordinate")
71-
self.attrs_y = dict(standard_name="projection_y_coordinate")
79+
elif grid_mapping_name is not None:
80+
self.attrs_x = dict(standard_name="projection_x_coordinate", **gmattr)
81+
self.attrs_y = dict(standard_name="projection_y_coordinate", **gmattr)
7282

7383
@property
7484
def geometry_container_attrs(self) -> dict[str, str]:
@@ -429,6 +439,8 @@ def shapely_to_cf(
429439
geom.item().geom_type if isinstance(geom, xr.DataArray) else geom.geom_type
430440
for geom in geometries
431441
}
442+
443+
grid_mapping_varname = None
432444
if (
433445
grid_mapping is None
434446
and isinstance(geometries, xr.DataArray)
@@ -439,7 +451,9 @@ def shapely_to_cf(
439451
"grid_mapping_name"
440452
]
441453

442-
names = GeometryNames(suffix=suffix, grid_mapping=grid_mapping)
454+
names = GeometryNames(
455+
suffix=suffix, grid_mapping_name=grid_mapping, grid_mapping=grid_mapping_varname
456+
)
443457

444458
if types.issubset({"Point", "MultiPoint"}):
445459
ds = points_to_cf(geometries, names=names)
@@ -452,9 +466,6 @@ def shapely_to_cf(
452466
f"Mixed geometry types are not supported in CF-compliant datasets. Got {types}"
453467
)
454468

455-
# for name_ in ["x", "y", "crd_x", "crd_y"]:
456-
# ds[name_].attrs["grid_mapping"] = grid_mapping_varname
457-
458469
return ds
459470

460471

cf_xarray/tests/test_geometry.py

+2
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ def test_shapely_to_cf_errors():
360360
)
361361
assert encoded["x"].attrs["standard_name"] == "projection_x_coordinate"
362362
assert encoded["y"].attrs["standard_name"] == "projection_y_coordinate"
363+
for name in ["x", "y", "crd_x", "crd_y"]:
364+
assert "grid_mapping" not in encoded[name].attrs
363365

364366

365367
@requires_shapely

0 commit comments

Comments
 (0)