Simple, robust, thin HDF5 polymorphic Fortran read/write interface. Reading or writing {real64,real32,int32,int64} from scalar to 7d is as simple as
use h5fortran
call h5write('my.h5', '/x', x)
call h5read('my.h5', '/y', y)
For NetCDF4 see nc4fortran. h5fortran is designed for "serial" HDF5 read/write. We don't yet implement the interface for "parallel" HDF5.
h5fortran is designed for easy use using static or shared linking from your project via:
cmake --install
Uses Fortran submodule
for clean template structure.
This easy-to-use, thin object-oriented modern Fortran library abstracts away the messy parts of HDF5 so that you can read / write various types/ranks of data with a single command.
In distinction from other high-level HDF5 interfaces, h5fortran works to deduplicate code, using polymorphism wherever feasible and extensive test suite.
Polymorphic API with read/write for types int32, real32, real64 with rank scalar (0-D) through 7-D. 64-bit integers int64 are read/write from scalar through 3-D.
as well as character (string). If you need int64, we have a working example for that: src/concepts/int64.f90 that can easily be put into the h5fortran API--just make a GitHub Issue.
Tested on systems with HDF5 1.8, 1.10 and 1.12 including:
Compilers known to work include:
We welcome contributions. In general we hold to the geospace-code code of conduct.
Using CMake:
git clone https://github.com/geospace-code/h5fortran.git
cd h5fortran
cmake -B build
cmake --build build
for more details see Install.md.
For general use with non-CMake build systems, "h5fortran.pc" pkg-config file is also generated / installed.
h5fortran will automatically build the HDF5 and ZLIB libraries if needed. This is useful as many HPC have broken or ABI-incompatible HDF5 libraries installed. Building HDF5 and ZLIB takes about a minute on a typical laptop. To disable this autobuild behavior, use option:
cmake -B build -Dautobuild=off
To force building the HDF5 and ZLIB libraries, to gain better performance via optimizing for your system's CPU:
cmake -Dhdf5_external=on
cmake --build build
NOTE: If using Intel oneAPI on Windows, ensure that environment variable CC=icl as set manually in the command prompt:
set CC=icl
set FC=ifort
This is necessary to workaround techniques used by HDF5 CMake files that don't pickup the CMake set(ENV{CC})
.
Otherwise, HDF5 build failures may result due to defaulting to icl-clang.
By default we use Zlib 2.x a.k.a. zlib-ng. If you have a problem with Zlib-ng on your system, try the unmaintained Zlib 1.x by:
cmake -B build -Dzlib_legacy=on
The simplest example h5fortran usage is like:
use h5fortran
call h5write('golt.h5','/x', [1,2,3,4,5,6])
or
use h5fortran
real :: x2
if(.not. is_hdf5('golt.h5')) error stop 'golt.h5 is not an HDF5 file'
call h5read('golt.h5', '/x', x2)
For detailed examples see Examples.md.
Instead of auto-building HDF5 via H5Fortran, one may build and install the HDF5 library by:
python3 scripts/build_hdf5.py
A_real
and A_imag