Repository: yt-project/unyt

Issue:
hdf5 reading for unyt.quantity.
Hi I believe that there is a bug whenever I try to read a `unyt.unyt_quantity ` that was stored in a hdf5 file using `write_hdf5`.  Basically I think it treats the quantity as a non-zero dimensional array, and uses ellipses to get the data.  I have tried to use `unyt.unyt_quantity.from_hdf5()` and `unyt.unyt_array.from_hdf5()`. 


```
----> 1 unyt.unyt_quantity.from_hdf5("...")
File (.../python3.9/site-packages/unyt/array.py:1557), in unyt_array.from_hdf5(cls, filename, dataset_name, group_name)
   1555     g = f
  1555     g = f
   1556 dataset = g[dataset_name]
-> 1557 data = dataset[:]
   1558 units = dataset.attrs.get("units", "")
   1559 unit_lut = default_unit_symbol_lut.copy()

File (objects.pyx:54), in h5py._objects.with_phil.wrapper()

File (objects.pyx:55), in h5py._objects.with_phil.wrapper()

File(..../h5py/_hl/dataset.py:817), in Dataset.__getitem__(self, args, new_dtype)
    815 if self.shape == ():
    816     fspace = self.id.get_space()
--> 817     selection = sel2.select_read(fspace, args)
    818     if selection.mshape is None:
    819         arr = numpy.zeros((), dtype=new_dtype)

File (../h5py/_hl/selections2.py:101), in select_read(fspace, args)
     96 """ Top-level dispatch function for reading.
     97 
     98 At the moment, only supports reading from scalar datasets.
     99 """
    100 if fspace.shape == ():
--> 101     return ScalarReadSelection(fspace, args)
    103 raise NotImplementedError()

File [.../h5py/_hl/selections2.py:86), in ScalarReadSelection.__init__(self, fspace, args)
     84     self.mshape = ()
     85 else:
---> 86     raise ValueError("Illegal slicing argument for scalar dataspace")
     88 self.mspace = h5s.create(h5s.SCALAR)
     89 self.fspace = fspace

ValueError: Illegal slicing argument for scalar dataspace
```

Discussion:

Fix (patch):
diff --git a/unyt/array.py b/unyt/array.py
index 8970312..476ccc4 100644
--- a/unyt/array.py
+++ b/unyt/array.py
@@ -1562,7 +1562,7 @@ class unyt_array(np.ndarray):
         else:
             g = f
         dataset = g[dataset_name]
-        data = dataset[:]
+        data = dataset[...]
         units = dataset.attrs.get("units", "")
         unit_lut = default_unit_symbol_lut.copy()
         unit_lut_load = pickle.loads(dataset.attrs["unit_registry"].tobytes())


Test changes:
diff --git a/unyt/tests/test_unyt_array.py b/unyt/tests/test_unyt_array.py
index de0d1a3..5a20685 100644
--- a/unyt/tests/test_unyt_array.py
+++ b/unyt/tests/test_unyt_array.py
@@ -1587,6 +1587,25 @@ def test_h5_io():
     shutil.rmtree(tmpdir)
 
 
+def test_h5_quantity_roundtrip(tmp_path):
+    # regression test for https://github.com/yt-project/unyt/issues/559
+    pytest.importorskip("h5py")
+
+    q1 = 5 * cm
+    savefile = tmp_path / "savefile.hdf5"
+    q1.write_hdf5(
+        savefile,
+        dataset_name="test_ds",
+        group_name="test_group",
+    )
+    q2 = unyt_quantity.from_hdf5(
+        savefile,
+        dataset_name="test_ds",
+        group_name="test_group",
+    )
+    assert_array_equal_units(q2, q1)
+
+
 def test_equivalencies():
     import unyt as u