Skip to content

Commit c0dff8f

Browse files
committed
h5read, h5write easy I/O
1 parent b2f1ad2 commit c0dff8f

File tree

8 files changed

+666
-17
lines changed

8 files changed

+666
-17
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if(NOT CMAKE_BUILD_TYPE)
44
endif()
55
project(hdf5iface
66
LANGUAGES Fortran
7-
VERSION 2.4.4
7+
VERSION 2.5.0
88
DESCRIPTION "thin, light, easy HDF5 Fortran interface"
99
HOMEPAGE_URL https://github.com/scivision/h5fortran)
1010
enable_testing()

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@
55
[![Actions Status](https://github.com/scivision/h5fortran/workflows/ci_linux/badge.svg)](https://github.com/scivision/h5fortran/actions)
66
[![Actions Status](https://github.com/scivision/h5fortran/workflows/ci_mac/badge.svg)](https://github.com/scivision/h5fortran/actions)
77

8-
Straightforward single-module access to HDF5.
9-
For NetCDF4 see [nc4fortran](https://github.com/scivision/nc4fortran/).
10-
Designed for easy use as a Meson "subproject" or CMake "ExternalProject / FetchContent" using **static** or **shared** linking.
8+
Simple, robust, thin HDF5 polymorphic read/write interface.
9+
Reading or writing {real64,real32,int64,int32} from scalar to 7d is as simple as
10+
11+
```fortran
12+
use h5fortran
13+
14+
call h5save('foo.h5', '/x', x)
15+
16+
call h5read('bar.h5', '/y', y)
17+
```
18+
19+
* For NetCDF4 see [nc4fortran](https://github.com/scivision/nc4fortran/).
20+
* Designed for easy use as a Meson "subproject" or CMake "ExternalProject / FetchContent" using **static** or **shared** linking.
21+
1122
Uses Fortran 2008 `submodule` for clean template structure.
1223
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.
1324
In distinction from other high-level HDF5 interfaces, h5fortran works to deduplicate code, using polymorphism wherever feasible and extensive test suite.

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
project('h5fortran', 'fortran',
22
meson_version : '>=0.52.0',
3-
version : '2.4.4',
3+
version : '2.5.0',
44
default_options : ['default_library=static', 'buildtype=release', 'warning_level=3'])
55

66
subdir('meson')

src/hdf5_interface.f90

Lines changed: 114 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module h5fortran
99
use string_utils, only : toLower, strip_trailing_null, truncate_string_null
1010

1111
implicit none
12+
private
13+
public :: hdf5_file, toLower, hsize_t, strip_trailing_null, truncate_string_null, check, h5write, h5read
14+
1215

1316
!> main type
1417
type :: hdf5_file
@@ -47,10 +50,115 @@ module h5fortran
4750

4851
end type hdf5_file
4952

53+
interface h5write
54+
procedure lt0write, lt1write, lt2write, lt3write, lt4write, lt5write, lt6write, lt7write
55+
end interface h5write
56+
57+
interface h5read
58+
procedure lt0read, lt1read, lt2read, lt3read, lt4read, lt5read, lt6read, lt7read
59+
end interface h5read
60+
5061

5162
!> Submodules
5263
interface
5364

65+
module subroutine lt0write(filename, dname, value, ierr)
66+
character(*), intent(in) :: filename, dname
67+
class(*), intent(in) :: value
68+
integer, intent(out), optional :: ierr
69+
end subroutine lt0write
70+
71+
module subroutine lt1write(filename, dname, value, ierr)
72+
character(*), intent(in) :: filename, dname
73+
class(*), intent(in) :: value(:)
74+
integer, intent(out), optional :: ierr
75+
end subroutine lt1write
76+
77+
module subroutine lt2write(filename, dname, value, ierr)
78+
character(*), intent(in) :: filename, dname
79+
class(*), intent(in) :: value(:,:)
80+
integer, intent(out), optional :: ierr
81+
end subroutine lt2write
82+
83+
module subroutine lt3write(filename, dname, value, ierr)
84+
character(*), intent(in) :: filename, dname
85+
class(*), intent(in) :: value(:,:,:)
86+
integer, intent(out), optional :: ierr
87+
end subroutine lt3write
88+
89+
module subroutine lt4write(filename, dname, value, ierr)
90+
character(*), intent(in) :: filename, dname
91+
class(*), intent(in) :: value(:,:,:,:)
92+
integer, intent(out), optional :: ierr
93+
end subroutine lt4write
94+
95+
module subroutine lt5write(filename, dname, value, ierr)
96+
character(*), intent(in) :: filename, dname
97+
class(*), intent(in) :: value(:,:,:,:,:)
98+
integer, intent(out), optional :: ierr
99+
end subroutine lt5write
100+
101+
module subroutine lt6write(filename, dname, value, ierr)
102+
character(*), intent(in) :: filename, dname
103+
class(*), intent(in) :: value(:,:,:,:,:,:)
104+
integer, intent(out), optional :: ierr
105+
end subroutine lt6write
106+
107+
module subroutine lt7write(filename, dname, value, ierr)
108+
character(*), intent(in) :: filename, dname
109+
class(*), intent(in) :: value(:,:,:,:,:,:,:)
110+
integer, intent(out), optional :: ierr
111+
end subroutine lt7write
112+
113+
module subroutine lt0read(filename, dname, value, ierr)
114+
character(*), intent(in) :: filename, dname
115+
class(*), intent(out) :: value
116+
integer, intent(out), optional :: ierr
117+
end subroutine lt0read
118+
119+
module subroutine lt1read(filename, dname, value, ierr)
120+
character(*), intent(in) :: filename, dname
121+
class(*), intent(out) :: value(:)
122+
integer, intent(out), optional :: ierr
123+
end subroutine lt1read
124+
125+
module subroutine lt2read(filename, dname, value, ierr)
126+
character(*), intent(in) :: filename, dname
127+
class(*), intent(out) :: value(:,:)
128+
integer, intent(out), optional :: ierr
129+
end subroutine lt2read
130+
131+
module subroutine lt3read(filename, dname, value, ierr)
132+
character(*), intent(in) :: filename, dname
133+
class(*), intent(out) :: value(:,:,:)
134+
integer, intent(out), optional :: ierr
135+
end subroutine lt3read
136+
137+
module subroutine lt4read(filename, dname, value, ierr)
138+
character(*), intent(in) :: filename, dname
139+
class(*), intent(out) :: value(:,:,:,:)
140+
integer, intent(out), optional :: ierr
141+
end subroutine lt4read
142+
143+
module subroutine lt5read(filename, dname, value, ierr)
144+
character(*), intent(in) :: filename, dname
145+
class(*), intent(out) :: value(:,:,:,:,:)
146+
integer, intent(out), optional :: ierr
147+
end subroutine lt5read
148+
149+
module subroutine lt6read(filename, dname, value, ierr)
150+
character(*), intent(in) :: filename, dname
151+
class(*), intent(out) :: value(:,:,:,:,:,:)
152+
integer, intent(out), optional :: ierr
153+
end subroutine lt6read
154+
155+
module subroutine lt7read(filename, dname, value, ierr)
156+
character(*), intent(in) :: filename, dname
157+
class(*), intent(out) :: value(:,:,:,:,:,:,:)
158+
integer, intent(out), optional :: ierr
159+
end subroutine lt7read
160+
161+
54162
module subroutine hdf_setup_read(self, dname, dims, ierr)
55163
class(hdf5_file), intent(in) :: self
56164
character(*), intent(in) :: dname
@@ -240,12 +348,6 @@ end subroutine writeattr
240348

241349
end interface
242350

243-
integer, parameter :: ENOENT = 2, EIO = 5
244-
245-
private
246-
public :: hdf5_file, toLower, hsize_t, strip_trailing_null, truncate_string_null, check
247-
248-
249351
contains
250352

251353

@@ -310,17 +412,19 @@ subroutine hdf_initialize(self,filename,ierr, status,action,comp_lvl,chunk_size,
310412
inquire(file=filename, exist=exists)
311413
if (.not.exists) then
312414
write(stderr,*) 'ERROR: ' // filename // ' does not exist.'
313-
ierr = ENOENT
415+
ierr = -1
416+
return
314417
endif
315418
call h5fopen_f(filename,H5F_ACC_RDONLY_F,self%lid,ierr)
316419
case('write','readwrite','w','rw', 'r+', 'append', 'a')
317420
inquire(file=filename, exist=exists)
318421
if(lstatus == 'unknown' .and. .not.exists) then
319-
call h5fopen_f(filename, H5F_ACC_TRUNC_F, self%lid,ierr)
422+
call h5fcreate_f(filename, H5F_ACC_TRUNC_F, self%lid, ierr)
423+
if (check(ierr, 'ERROR: ' // filename // ' could not be created')) return
320424
else
321-
call h5fopen_f(filename, H5F_ACC_RDWR_F, self%lid,ierr)
425+
call h5fopen_f(filename, H5F_ACC_RDWR_F, self%lid, ierr)
426+
if (check(ierr, 'ERROR: ' // filename // ' could not be opened in read/write')) return
322427
endif
323-
if (check(ierr, 'ERROR: ' // filename // ' could not be opened')) return
324428
case default
325429
write(stderr,*) 'Unsupported action -> ' // laction
326430
ierr = 128

0 commit comments

Comments
 (0)