wrhdf_2d

Contents

wrhdf_2d#

wrhdf_2d(hdf_filename, x, y, f, **kwargs)[source]#

Write a 2D PSI-style HDF5 or HDF4 file.

The data in the HDF file will appear as X,Y in Fortran order.

Each dimension requires a 1D “scale” associated with it that describes the rectilinear grid coordinates in each dimension.

Parameters:
hdf_filenamePathLike

The path to the 2D HDF5 (.h5) or HDF4 (.hdf) file to write.

xnp.ndarray

1D array of scales in the X dimension.

ynp.ndarray

1D array of scales in the Y dimension.

fnp.ndarray

2D data array, C-ordered with shape (ny, nx) in Python.

**kwargs

Additional keyword arguments passed through to the underlying write_hdf_data() routine, specifically:

  • dataset_idstr | None

    The identifier of the dataset to write. If None, a default dataset is used ('Data-Set-2' for HDF4 and 'Data' for HDF5).

  • sync_dtype: bool

    If True, the data type of the scales will be matched to that of the data array.

Omitting these will yield the same behavior as the legacy routines, i.e. writing to the default PSI dataset IDs for HDF4/HDF5 files and synchronizing datatypes between the dataset and scales.

Raises:
ValueError

If the file does not have a .hdf or .h5 extension.

KeyError

If, for HDF4 files, the data or scale dtype is not supported by pyhdf. See write_hdf_data() for the full dtype support table.

See also

write_hdf_data

Generic HDF data writing routine.

Notes

This routine is provided for backward compatibility with existing PSI codes that expect this API signature. The underlying implementation dispatches to write_hdf_data(), but the argument order and default behavior (sync dtype, default dataset IDs) are preserved.

Warning

When called with its default arguments this routine writes to the default PSI dataset IDs for HDF4/HDF5 files and synchronizes dtypes between the dataset and scales. This behavior is required for interoperability with certain Fortran-based PSI tools.

Examples

>>> import tempfile, numpy as np
>>> from pathlib import Path
>>> from psi_io import wrhdf_2d, rdhdf_2d
>>> x = np.linspace(0.0, 2*np.pi, 64, dtype=np.float32)
>>> y = np.linspace(0.0, np.pi, 32, dtype=np.float32)
>>> f = np.outer(np.sin(x), np.cos(y)).astype(np.float32)
>>> with tempfile.TemporaryDirectory() as d:
...     wrhdf_2d(Path(d) / "out.h5", x, y, f)
...     x2, y2, f2 = rdhdf_2d(Path(d) / "out.h5")
...     np.array_equal(f, f2)
True