Skip to content

Commit 995aced

Browse files
committed
Add function to read an HDF5 varying-length string attribute
1 parent 99c4f2d commit 995aced

File tree

4 files changed

+80
-12
lines changed

4 files changed

+80
-12
lines changed

src/nf/nf_io_hdf5.f90

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module nf_io_hdf5
2+
3+
!! This module provides convenience functions to read HDF5 files.
4+
5+
implicit none
6+
7+
private
8+
public :: get_h5_attribute_string
9+
10+
interface
11+
12+
module function get_h5_attribute_string( &
13+
filename, object_name, attribute_name) result(res)
14+
character(*), intent(in) :: filename
15+
!! HDF5 file name
16+
character(*), intent(in) :: object_name
17+
!! Object (group, dataset) name
18+
character(*), intent(in) :: attribute_name
19+
!! Name of the attribute to read
20+
character(:), allocatable :: res
21+
end function get_h5_attribute_string
22+
23+
end interface
24+
25+
end module nf_io_hdf5

src/nf/nf_io_hdf5_submodule.f90

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
submodule(nf_io_hdf5) nf_io_hdf5_submodule
2+
3+
use hdf5, only: H5F_ACC_RDONLY_F, HID_T, &
4+
h5aget_type_f, h5aopen_by_name_f, h5aread_f, &
5+
h5fclose_f, h5fopen_f
6+
use iso_c_binding, only: c_char, c_f_pointer, c_loc, c_null_char, c_ptr
7+
8+
implicit none
9+
10+
contains
11+
12+
module function get_h5_attribute_string( &
13+
filename, object_name, attribute_name) result(res)
14+
15+
character(*), intent(in) :: filename
16+
character(*), intent(in) :: object_name
17+
character(*), intent(in) :: attribute_name
18+
character(:), allocatable :: res
19+
20+
! Make sufficiently large to hold most attributes
21+
integer, parameter :: BUFLEN = 10000
22+
23+
type(c_ptr) :: f_ptr
24+
type(c_ptr), target :: buffer
25+
character(len=BUFLEN, kind=c_char), pointer :: string => null()
26+
integer(HID_T) :: fid, aid, atype
27+
integer :: hdferr
28+
29+
! Open the file and get the type of the attribute
30+
call h5fopen_f(filename, H5F_ACC_RDONLY_F, fid, hdferr)
31+
call h5aopen_by_name_f(fid, object_name, attribute_name, aid, hdferr)
32+
call h5aget_type_f(aid, atype, hdferr)
33+
34+
! Read the data
35+
f_ptr = c_loc(buffer)
36+
call h5aread_f(aid, atype, f_ptr, hdferr)
37+
call c_f_pointer(buffer, string)
38+
39+
! Close the file
40+
call h5fclose_f(fid, hdferr)
41+
42+
res = string(:index(string, c_null_char))
43+
44+
end function get_h5_attribute_string
45+
46+
end submodule nf_io_hdf5_submodule

test/test_io_h5.f90

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/test_io_hdf5.f90

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
program test_io_hdf5
2+
3+
use nf_io_hdf5, only: get_h5_attribute_string
4+
implicit none
5+
6+
print *, get_h5_attribute_string('test/data/mnist_dense.h5', '.', 'model_config')
7+
print *, get_h5_attribute_string('test/data/mnist_dense.h5', 'model_weights', 'layer_names')
8+
9+
end program test_io_hdf5

0 commit comments

Comments
 (0)