Skip to content

Commit 2d26092

Browse files
maarten-icolivhoenen
authored andcommitted
Ensure that 0D numpy arrays are unpacked when filling lazy loaded IDS from netCDF files
```python >>> entry = imas.DBEntry("ascii.nc", "r") >>> eq = entry.get("equilibrium", autoconvert=False, lazy=True) >>> # Previous behaviour: >>> eq.ids_properties.homogeneous_time <IDSInt0D (IDS:equilibrium, ids_properties/homogeneous_time, INT_0D)> ndarray(array(1, dtype=int32)) >>> # New behaviour, matches with non-lazy IDS: >>> eq.ids_properties.homogeneous_time <IDSInt0D (IDS:equilibrium, ids_properties/homogeneous_time, INT_0D)> int(1) ```
1 parent 4c50b0f commit 2d26092

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

imas/backends/netcdf/nc2ids.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,12 @@ def get_child(self, child):
366366

367367
if value is not None:
368368
if isinstance(value, np.ndarray):
369-
# Convert the numpy array to a read-only view
370-
value = value.view()
371-
value.flags.writeable = False
369+
if value.ndim == 0: # Unpack 0D numpy arrays:
370+
value = value.item()
371+
else:
372+
# Convert the numpy array to a read-only view
373+
value = value.view()
374+
value.flags.writeable = False
372375
# NOTE: bypassing IDSPrimitive.value.setter logic
373376
child._IDSPrimitive__value = value
374377

0 commit comments

Comments
 (0)