Lightweight HDF5 polymorphic Fortran: h5write() h5read()
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 dataset types int32, int64, real32, real64, character (string) with rank scalar (0-D) through 7-D. Variable-length datasets "H5S_UNLIMITED" can also be read.
HDF5 attributes are also supported for read/write types int32, int64, real32, real64, character (string) with rank scalar (0-D) through 7-D.
For character, datasets and attributes are supported for fixed length and variable length strings. Character padding uses spaces as this is the default for Fortran. Space-padded, null-terminated and null-padded strings are also supported on read. UTF8 characters are passed through on read and write using the Fortran default character kind.
Tested on systems with HDF5 1.8, 1.10 and 1.12 including:
Compilers known to work (tested on CI) include:
Compilers not currently working:
We welcome contributions. In general we hold to the geospace-code code of conduct.
h5fortran can be built with CMake or fpm.
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.
To save time, if not intended to use self-tests, you can skip the build of the test suite:
cmake -B build -Dh5fortran_BUILD_TESTING=off
fpm build
fpm test
fpm install
To build the HDF5 and ZLIB libraries:
cmake -S scripts -B scripts/build -DCMAKE_INSTALL_PREFIX=~/mylibs
cmake --build scripts/build
Then build h5fortran:
cmake -B build -DCMAKE_PREFIX_PATH=~/mylibs
cmake --build build
Request a specific HDF5 Git tag or commit hash like:
cmake -B build -Dhdf5_tag=hdf5-1_12_2
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.
A_real
and A_imag
We didn't use type(c_ptr)
and c_loc()
internally for datasets as we observed problems when the actual argument is sliced on read/write.
The current h5fortran impementation (Fortran select type
for H5Dwrite/H5Dread) does work with sliced actual arguments.
HDF5 Fortran 2003 features