Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option to write metadata to hdf5 file #3257

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion openmc/data/neutron.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,13 @@ def get_reaction_components(self, mt):
else:
return [mt] if mt in self else []

def export_to_hdf5(self, path, mode='a', libver='earliest'):
def export_to_hdf5(
self,
path: cv.PathLike,
mode: str = 'a',
libver: str = 'earliest',
metadata: str | None = None
):
"""Export incident neutron data to an HDF5 file.

Parameters
Expand All @@ -364,6 +370,8 @@ def export_to_hdf5(self, path, mode='a', libver='earliest'):
libver : {'earliest', 'latest'}
Compatibility mode for the HDF5 file. 'latest' will produce files
that are less backwards compatible but have performance benefits.
metadata : Optional str
A string of metadata to include in the HDF5 file as an attribute.

"""
# If data come from ENDF, don't allow exporting to HDF5
Expand All @@ -375,6 +383,8 @@ def export_to_hdf5(self, path, mode='a', libver='earliest'):
with h5py.File(str(path), mode, libver=libver) as f:
f.attrs['filetype'] = np.bytes_('data_neutron')
f.attrs['version'] = np.array(HDF5_VERSION)
if metadata is not None:
f.attrs['metadata'] = np.bytes_(metadata)

# Write basic data
g = f.create_group(self.name)
Expand Down
12 changes: 11 additions & 1 deletion openmc/data/photon.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,13 @@ def from_hdf5(cls, group_or_filename):

return data

def export_to_hdf5(self, path, mode='a', libver='earliest'):
def export_to_hdf5(
self,
path: cv.PathLike,
mode: str = 'a',
libver: str = 'earliest',
metadata: str | None = None
):
"""Export incident photon data to an HDF5 file.

Parameters
Expand All @@ -762,13 +768,17 @@ def export_to_hdf5(self, path, mode='a', libver='earliest'):
libver : {'earliest', 'latest'}
Compatibility mode for the HDF5 file. 'latest' will produce files
that are less backwards compatible but have performance benefits.
metadata : Optional str
A string of metadata to include in the HDF5 file as an attribute.

"""
with h5py.File(str(path), mode, libver=libver) as f:
# Write filetype and version
f.attrs['filetype'] = np.bytes_('data_photon')
if 'version' not in f.attrs:
f.attrs['version'] = np.array(HDF5_VERSION)
if metadata is not None:
f.attrs['metadata'] = np.bytes_(metadata)

group = f.create_group(self.name)
group.attrs['Z'] = Z = self.atomic_number
Expand Down
Loading