37
37
class GeometryNames :
38
38
"""Helper class to ease handling of all the variable names needed for CF geometries."""
39
39
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
+ ):
41
46
self .container_name : str = GEOMETRY_CONTAINER_NAME + suffix
42
47
self .node_dim : str = "node" + suffix
43
48
self .node_count : str = "node_count" + suffix
@@ -51,24 +56,29 @@ def __init__(self, suffix: str = "", grid_mapping: str | None = None):
51
56
self .attrs_x : dict [str , str ] = {}
52
57
self .attrs_y : dict [str , str ] = {}
53
58
59
+ gmattr = {"grid_mapping" : grid_mapping } if grid_mapping else {}
54
60
# 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" ]:
56
62
# Special case for longitude_latitude type grid mappings
57
63
self .coordinates_x = "lon"
58
64
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" :
63
73
self .attrs_x = dict (
64
- units = "degrees_east" , standard_name = "grid_longitude"
74
+ units = "degrees_east" , standard_name = "grid_longitude" , ** gmattr
65
75
)
66
76
self .attrs_y = dict (
67
- units = "degrees_north" , standard_name = "grid_latitude"
77
+ units = "degrees_north" , standard_name = "grid_latitude" , ** gmattr
68
78
)
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 )
72
82
73
83
@property
74
84
def geometry_container_attrs (self ) -> dict [str , str ]:
@@ -429,6 +439,8 @@ def shapely_to_cf(
429
439
geom .item ().geom_type if isinstance (geom , xr .DataArray ) else geom .geom_type
430
440
for geom in geometries
431
441
}
442
+
443
+ grid_mapping_varname = None
432
444
if (
433
445
grid_mapping is None
434
446
and isinstance (geometries , xr .DataArray )
@@ -439,7 +451,9 @@ def shapely_to_cf(
439
451
"grid_mapping_name"
440
452
]
441
453
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
+ )
443
457
444
458
if types .issubset ({"Point" , "MultiPoint" }):
445
459
ds = points_to_cf (geometries , names = names )
@@ -452,9 +466,6 @@ def shapely_to_cf(
452
466
f"Mixed geometry types are not supported in CF-compliant datasets. Got { types } "
453
467
)
454
468
455
- # for name_ in ["x", "y", "crd_x", "crd_y"]:
456
- # ds[name_].attrs["grid_mapping"] = grid_mapping_varname
457
-
458
469
return ds
459
470
460
471
0 commit comments