Skip to content

Commit f913e70

Browse files
committed
Rationalise handling of set_auto_chartostring in threadsafe/encoded netcdf wrappers.
1 parent 35f895c commit f913e70

3 files changed

Lines changed: 25 additions & 20 deletions

File tree

lib/iris/fileformats/cf.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,12 +1344,9 @@ def __init__(self, file_source, warn=False, monotonic=False):
13441344
self._with_ugrid = False
13451345

13461346
# Read the variables in the dataset only once to reduce runtime.
1347-
# Turn off *any* automatic decoding in the underlying netCDF4 dataset
13481347
ds = self._dataset
1349-
if isinstance(ds, _thread_safe_nc.DatasetWrapper):
1350-
ds._contained_instance.set_auto_chartostring(False)
1351-
else:
1352-
ds.set_auto_chartostring(False)
1348+
# Turn off *any* automatic decoding in the underlying netCDF4 dataset.
1349+
ds.set_auto_chartostring(False)
13531350
variables = self._dataset.variables
13541351
self._translate(variables)
13551352
self._build_cf_groups(variables)

lib/iris/fileformats/netcdf/_bytecoding_datasets.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,15 @@ def _identify_encoding(encoding, var_name: str, writing: bool = False) -> str:
327327
return result
328328

329329

330-
class EncodedVariable(VariableWrapper):
330+
class Mixin_Block_AutoChartostring:
331+
def set_auto_chartostring(self, onoff: bool):
332+
# Silently allow a call to "turn it off", but not on.
333+
if onoff:
334+
msg = "auto_chartostring is not supported by Iris 'EncodedVariable' type."
335+
raise TypeError(msg)
336+
337+
338+
class EncodedVariable(VariableWrapper, Mixin_Block_AutoChartostring):
331339
"""A variable wrapper that translates variable data according to byte encodings."""
332340

333341
def __init__(self, *args, **kwargs):
@@ -380,26 +388,18 @@ def __setitem__(self, keys, data):
380388
data = encoding_spec.encode_strings_as_bytearray(data)
381389
super().__setitem__(keys, data)
382390

383-
def set_auto_chartostring(self, onoff: bool):
384-
msg = "auto_chartostring is not supported by Iris 'EncodedVariable' type."
385-
raise TypeError(msg)
386-
387391

388-
class EncodedGroup(GroupWrapper):
392+
class EncodedGroup(GroupWrapper, Mixin_Block_AutoChartostring):
389393
"""A specialised GroupWrapper whose variables are EncodedVariables."""
390394

391395
VAR_WRAPPER_CLS = EncodedVariable
392396
GRP_WRAPPER_CLS: Any | None = None
393397

394-
def set_auto_chartostring(self, onoff: bool):
395-
msg = "auto_chartostring is not supported by Iris 'EncodedGroup' type."
396-
raise TypeError(msg)
397-
398398

399399
EncodedGroup.GRP_WRAPPER_CLS = EncodedGroup
400400

401401

402-
class EncodedDataset(DatasetWrapper):
402+
class EncodedDataset(DatasetWrapper, Mixin_Block_AutoChartostring):
403403
"""A specialised DatasetWrapper.
404404
405405
Its groups are EncodedGroups and variables are EncodedVariables.
@@ -408,10 +408,6 @@ class EncodedDataset(DatasetWrapper):
408408
VAR_WRAPPER_CLS = EncodedVariable
409409
GRP_WRAPPER_CLS = EncodedGroup
410410

411-
def set_auto_chartostring(self, onoff: bool):
412-
msg = "auto_chartostring is not supported by Iris 'EncodedGroup' type."
413-
raise TypeError(msg)
414-
415411

416412
class EncodedNetCDFDataProxy(NetCDFDataProxy):
417413
__slots__ = NetCDFDataProxy.__slots__ + ("encoding_details",)

lib/iris/fileformats/netcdf/_thread_safe_nc.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ def get_dims(self, *args, **kwargs) -> typing.Tuple[DimensionWrapper]:
149149
dimensions_ = list(self._contained_instance.get_dims(*args, **kwargs))
150150
return tuple([DimensionWrapper.from_existing(d) for d in dimensions_])
151151

152+
def set_auto_chartostring(self, onoff: bool):
153+
with _GLOBAL_NETCDF4_LOCK:
154+
self._contained_instance.set_auto_chartostring(onoff)
155+
152156

153157
class GroupWrapper(_ThreadSafeWrapper):
154158
"""Accessor for a netCDF4.Group, always acquiring _GLOBAL_NETCDF4_LOCK.
@@ -287,6 +291,10 @@ def createGroup(self, *args, **kwargs):
287291
new_group = self._contained_instance.createGroup(*args, **kwargs)
288292
return self.GRP_WRAPPER_CLS.from_existing(new_group)
289293

294+
def set_auto_chartostring(self, onoff: bool):
295+
with _GLOBAL_NETCDF4_LOCK:
296+
self._contained_instance.set_auto_chartostring(onoff)
297+
290298

291299
GroupWrapper.GRP_WRAPPER_CLS = GroupWrapper
292300

@@ -314,6 +322,10 @@ def fromcdl(cls, *args, **kwargs):
314322
instance = cls.CONTAINED_CLASS.fromcdl(*args, **kwargs)
315323
return cls.from_existing(instance)
316324

325+
def set_auto_chartostring(self, onoff: bool):
326+
with _GLOBAL_NETCDF4_LOCK:
327+
self._contained_instance.set_auto_chartostring(onoff)
328+
317329

318330
class NetCDFDataProxy:
319331
"""A reference to the data payload of a single NetCDF file variable."""

0 commit comments

Comments
 (0)