grids.base.GridBase.from_hdf5#

abstract GridBase.from_hdf5(filename: str, group_name: str | None = None, **kwargs)[source]#

Load a grid instance from an HDF5 file.

This method reconstructs a grid from serialized data stored in an HDF5 file. It expects the file to contain grid metadata, coordinate arrays, ghost zone definitions, and a serialized coordinate system.

Parameters:
  • filename (str) – Path to the source HDF5 file.

  • group_name (str, optional) – If specified, loads data from this group within the file. If None, loads from the root group.

  • **kwargs – Additional keyword arguments passed to the loading logic.

Returns:

An instance of a concrete grid class.

Return type:

GridBase

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:

@classmethod
def from_hdf5(cls, filename: str, group_name: Optional[str] = None,**kwargs):
    # Ensure existence of the hdf5 file before attempting to
    # load data from it.
    import h5py
    filename = Path(filename)
    if not filename.exists():
        raise IOError(f"HDF5 file '{filename}' does not exist.")

    # Open the hdf5 file in read mode and start parsing the
    # data from it.
    with h5py.File(filename, "r") as f:
        # Navigate to the appropriate group
        group = f[group_name] if group_name else f

        # ====================== #
        # Subclass Logic         #
        # ====================== #
        # This is where subclasses should save relevant
        # data either in Datasets or in group.attrs

        # Load the units.
        if 'units' in group.keys():
            units = cls._deserialize_unit_system(group['units'])
        else:
            units = None

    # Load the coordinate system object
    coordinate_system = cls._load_coordinate_system(filename,group_name=group_name)

    return cls(
        *args,
        **kwargs
    )