grids.base.GridBase.to_hdf5#
- abstract GridBase.to_hdf5(filename: str, group_name: str | None = None, overwrite: bool = False, **kwargs)[source]#
Save the grid to an HDF5 file.
This method serializes the grid structure, including geometry metadata, coordinate information, and ghost zone configuration, into an HDF5 file. It may also delegate storage of the coordinate system and any subclass-specific attributes.
- Parameters:
filename (
str
) – Path to the target HDF5 file.group_name (
str
, optional) – If specified, the grid is saved inside this group within the file. If None, the data is written to the root group.overwrite (
bool
, defaultFalse
) – Whether to overwrite existing file or group.**kwargs – Additional keyword arguments passed to the saving logic. In many cases, there are no kwargs implemented; however, these can be used in specific subclasses.
- Raises:
NotImplementedError – This method must be implemented by all concrete grid subclasses.
Notes
For all subclasses of
GridBase
, this method should be overwritten to ensure that all of the necessary metadata is saved / loaded to / from HDF5 properly. There are various helper methods for setting up the HDF5 group object, saving unit systems, and saving coordinate systems. In general, a subclass implementation should look like the following:def to_hdf5( self, filename: str, group_name: Optional[str] = None, overwrite: bool = False **kwargs ): import h5py # Start by ensuring the file exists in the proper place and # gets wiped if that is necessary. # The _build_hdf5_stub method does this automatically. self._build_hdf5_stub(filename,group_name=group_name, overwrite=overwrite) # We now proceed with opening the file and writing the data. with h5py.File(filename, "r+") as f: # Use the _parse_hdf5_group method to parse the # correct group / file object into which data should # be placed. group = self._parse_hdf5_group(f, group_name=group_name, overwrite=overwrite) # ====================== # # Subclass Logic # # ====================== # # This is where subclasses should save relevant # data either in Datasets or in group.attrs # Finish off by saving the unit # system (using the _serialize_unit_system method) # and the coordinate system (_save_coordinate_system). self._serialize_unit_system(group.require_group('units')) # Finally, the coordinate system itself must now be saved to disk. We leave # the context manager to ensure that everything flushes correctly - the coordinate # system's logic for saving opens the file separately. self._save_coordinate_system(filename, group_name=group_name, overwrite=overwrite)