Skip to content

Commit 860cc83

Browse files
committed
Properly unpack 0D data when reading an IDS from a netCDF file
0D IDS Data is expected in the native python data types (int, float, complex, string). Before this fix, 0D numerical data would be stored as numpy.int32, numpy.float64 or numpy.complex128 when reading from a netCDF file. Fixes #89
1 parent 72d7ea3 commit 860cc83

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

imas/backends/netcdf/nc2ids.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ def run(self, lazy: bool) -> None:
157157
for index, node in indexed_tree_iter(self.ids, target_metadata):
158158
value = data[index]
159159
if value != getattr(var, "_FillValue", None):
160+
if isinstance(value, np.generic):
161+
value = value.item()
160162
# NOTE: bypassing IDSPrimitive.value.setter logic
161163
node._IDSPrimitive__value = value
162164

@@ -166,10 +168,16 @@ def run(self, lazy: bool) -> None:
166168
# here, we'll let IDSPrimitive.value.setter take care of it:
167169
self.ids[target_metadata.path].value = data
168170

169-
else:
171+
# We need to unpack 0D ints, floats and complex numbers. For better
172+
# performance this check is done outside the for-loop:
173+
elif metadata.ndim or metadata.data_type is IDSDataType.STR:
170174
for index, node in indexed_tree_iter(self.ids, target_metadata):
171175
# NOTE: bypassing IDSPrimitive.value.setter logic
172176
node._IDSPrimitive__value = data[index]
177+
else:
178+
for index, node in indexed_tree_iter(self.ids, target_metadata):
179+
# NOTE: bypassing IDSPrimitive.value.setter logic
180+
node._IDSPrimitive__value = data[index].item() # Unpack 0D value
173181

174182
def validate_variables(self) -> None:
175183
"""Validate that all variables in the netCDF Group exist and match the DD."""
@@ -365,7 +373,7 @@ def get_child(self, child):
365373
value = var[self.index]
366374

367375
if value is not None:
368-
if isinstance(value, np.ndarray):
376+
if isinstance(value, (np.ndarray, np.generic)):
369377
if value.ndim == 0: # Unpack 0D numpy arrays:
370378
value = value.item()
371379
else:

0 commit comments

Comments
 (0)