Interfaceing HDF5 files following the Standard Name Convention#

Let’s remember, that standard names are set to identify certain datasets in the HDF5 file. Those datasets have a special meaning to the user and are likely to be frequently used in the analysis of the data.

For this purpose a high-level interface HDF5StandardNameInterface is written, which facilitates the work with such HDF5 data. It scans the HDF5 file for all standard names and creates a class property for each

import h5rdmtoolbox as h5tbx

from h5rdmtoolbox.convention.standard_names import HDF5StandardNameInterface

import numpy as np

cv = h5tbx.convention.from_zenodo('https://zenodo.org/record/8357399')
cv.properties[h5tbx.File]['data_type'].make_optional()
cv.properties[h5tbx.File]['contact'].make_optional()
h5tbx.use(cv)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[1], line 7
      3 from h5rdmtoolbox.convention.standard_names import HDF5StandardNameInterface
      5 import numpy as np
----> 7 cv = h5tbx.convention.from_zenodo('https://zenodo.org/record/8357399')
      8 cv.properties[h5tbx.File]['data_type'].make_optional()
      9 cv.properties[h5tbx.File]['contact'].make_optional()

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/checkouts/v1.4.1/h5rdmtoolbox/convention/core.py:758, in from_zenodo(doi_or_recid, name, overwrite, force_download)
    755 if not filename.exists() or force_download:
    756     record = zenodo.ZenodoRecord(rec_id)
--> 758     filenames = list(record.files.keys())
    759     if name is None:
    760         matches = [file for file in filenames if pathlib.Path(file).suffix == '.yaml']

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/checkouts/v1.4.1/h5rdmtoolbox/repository/zenodo/core.py:602, in ZenodoRecord.files(self)
    595 @property
    596 def files(self) -> Dict[str, RepositoryFile]:
    597     # def _parse_download_url(filename):
    598     #     if filename is None:
    599     #         return filename
    600     #     return f"{self.rec_url}/{self.rec_id}/files/{filename}"
--> 602     is_submitted = self.submitted()
    604     def _parse_download_url(url, filename):
    605         if url is None:

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/checkouts/v1.4.1/h5rdmtoolbox/repository/zenodo/core.py:558, in ZenodoRecord.is_published(self)
    556 def is_published(self) -> bool:
    557     """Check if the deposit is published."""
--> 558     return self.json()['submitted']

KeyError: 'submitted'
with h5tbx.File() as h5:
    h5.create_dataset('x', data=[-4, 1, 3, 5, 10],
                      standard_name='x_coordinate',
                      units='m', make_scale=True)
    h5.create_dataset('y', data=[10, 11, 13],
                      standard_name='y_coordinate',
                      units='m', make_scale=True)
    h5.create_dataset('u', np.random.rand(3, 5),
                      standard_name='x_velocity',
                      units='m/s',
                      attach_scales=('y', 'x'))
    h5.create_dataset('v', np.random.rand(3, 5),
                      standard_name='y_velocity',
                      units='m/s',
                      attach_scales=('y', 'x'))
    h5.create_dataset('dudx', np.random.rand(3, 5),
                      standard_name='derivative_of_x_velocity_wrt_x_coordinate',
                      units='1/s')
h5sni = HDF5StandardNameInterface.from_hdf(h5.hdf_filename)
h5sni.standard_names
['x_coordinate',
 'y_coordinate',
 'derivative_of_x_velocity_wrt_x_coordinate',
 'y_velocity',
 'x_velocity']
h5sni.velocity.plot()
h5sni.velocity.plot.quiver()
<matplotlib.quiver.Quiver at 0x21c24e93a90>
../../../_images/4a010dfea46330febc7b0408fffdc6e1b5ae65f213e0596c47e3b6a05509e487.png