MCPcopy Create free account
hub / github.com/pydata/xarray / _build_and_get_enum

Function _build_and_get_enum

xarray/backends/netCDF4_.py:337–367  ·  view source on GitHub ↗

Add or get the netCDF4 Enum based on the dtype in encoding. The return type should be ``netCDF4.EnumType``, but we avoid importing netCDF4 globally for performances.

(
    store, var_name: str, dtype: np.dtype, enum_name: str, enum_dict: dict[str, int]
)

Source from the content-addressed store, hash-verified

335
336
337def _build_and_get_enum(
338 store, var_name: str, dtype: np.dtype, enum_name: str, enum_dict: dict[str, int]
339) -> ncEnumType | h5EnumType:
340 """
341 Add or get the netCDF4 Enum based on the dtype in encoding.
342 The return type should be ``netCDF4.EnumType``,
343 but we avoid importing netCDF4 globally for performances.
344 """
345 if enum_name not in store.ds.enumtypes:
346 create_func = (
347 store.ds.createEnumType
348 if isinstance(store, NetCDF4DataStore)
349 else store.ds.create_enumtype
350 )
351 return create_func(
352 dtype,
353 enum_name,
354 enum_dict,
355 )
356 datatype = store.ds.enumtypes[enum_name]
357 if datatype.enum_dict != enum_dict:
358 error_msg = (
359 f"Cannot save variable `{var_name}` because an enum"
360 f" `{enum_name}` already exists in the Dataset but has"
361 " a different definition. To fix this error, make sure"
362 " all variables have a uniquely named enum in their"
363 " `encoding['dtype'].metadata` or, if they should share"
364 " the same enum type, make sure the enums are identical."
365 )
366 raise ValueError(error_msg)
367 return datatype
368
369
370@dataclass

Callers 2

prepare_variableMethod · 0.90
prepare_variableMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…