Skip to content

Commit e71d379

Browse files
BrianMichelltasansal
authored andcommitted
Add base safety for grid override template mutation
1 parent 6a17b16 commit e71d379

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/mdio/builder/templates/base.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ def build_dataset(
9292
self._builder = MDIODatasetBuilder(name=name, attributes=attributes)
9393
self._add_dimensions()
9494
self._add_coordinates()
95-
# Ensure any coordinates declared on the template but not added by subclass overrides
96-
# are materialized with generic defaults. This keeps templates override-agnostic while
97-
# allowing runtime-augmented coordinate lists to be respected.
95+
# Ensure any coordinates declared on the template but not added by _add_coordinates
96+
# are materialized with generic defaults. This handles coordinates added by grid overrides.
9897
for coord_name in self.coordinate_names:
9998
try:
10099
self._builder.add_coordinate(
@@ -104,7 +103,7 @@ def build_dataset(
104103
compressor=compressors.Blosc(cname=compressors.BloscCname.zstd),
105104
metadata=CoordinateMetadata(units_v1=self.get_unit_by_key(coord_name)),
106105
)
107-
except ValueError as exc: # coordinate may already exist from subclass override
106+
except ValueError as exc: # coordinate may already exist
108107
if "same name twice" not in str(exc):
109108
raise
110109
self._add_variables()
@@ -253,14 +252,21 @@ def _add_coordinates(self) -> None:
253252
)
254253

255254
# Add non-dimension coordinates
255+
# Note: coordinate_names may be modified at runtime by grid overrides,
256+
# so we need to handle dynamic additions gracefully
256257
for name in self.coordinate_names:
257-
self._builder.add_coordinate(
258-
name=name,
259-
dimensions=self.spatial_dimension_names,
260-
data_type=ScalarType.FLOAT64,
261-
compressor=compressors.Blosc(cname=compressors.BloscCname.zstd),
262-
metadata=CoordinateMetadata(units_v1=self.get_unit_by_key(name)),
263-
)
258+
try:
259+
self._builder.add_coordinate(
260+
name=name,
261+
dimensions=self.spatial_dimension_names,
262+
data_type=ScalarType.FLOAT64,
263+
compressor=compressors.Blosc(cname=compressors.BloscCname.zstd),
264+
metadata=CoordinateMetadata(units_v1=self.get_unit_by_key(name)),
265+
)
266+
except ValueError as exc:
267+
# Coordinate may already exist from subclass override
268+
if "same name twice" not in str(exc):
269+
raise
264270

265271
def _add_trace_mask(self) -> None:
266272
"""Add trace mask variables."""

0 commit comments

Comments
 (0)