Skip to content

Commit 8b1a241

Browse files
committed
add h5exist() convenient method
1 parent 847752e commit 8b1a241

File tree

6 files changed

+42
-14
lines changed

6 files changed

+42
-14
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(h5fortran
66
LANGUAGES C Fortran
7-
VERSION 2.9.2
7+
VERSION 2.9.3
88
DESCRIPTION "thin, light object-oriented HDF5 Fortran interface"
99
HOMEPAGE_URL https://github.com/scivision/h5fortran)
1010
enable_testing()

Examples.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,16 @@ Currently, data is written contiguous if not compressed and is only chunked if c
5959

6060
## check if a variable exists
6161

62-
the logical method %exists() checks if a dataset (variable) exists in the initialized HDF5 file.
62+
the logical method %exist() checks if a dataset (variable) exists in the initialized HDF5 file.
6363

6464
```fortran
65-
exists = h5f%exists("/foo")
65+
exists = h5f%exist("/foo")
66+
```
67+
68+
A convenience method that checks existance of a dataset without creating the h5 object manually is:
69+
70+
```fortran
71+
exists = h5exist("my.h5", "/foo")
6672
```
6773

6874
## Read scalar, 3-D array of unknown size

src/interface.f90

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module h5fortran
1919
implicit none (type, external)
2020
private
2121
public :: hdf5_file, toLower, hdf_shape_check, hdf_get_slice, hdf_wrapup, hsize_t, strip_trailing_null, truncate_string_null, &
22-
check, h5write, h5read
22+
check, h5write, h5read, h5exist
2323

2424
!> Workaround for Intel 19.1 / 2020 bug with /stand:f18
2525
!> error #6410: This name has not been declared as an array or a function. [RANK]
@@ -75,6 +75,11 @@ module h5fortran
7575
!> Submodules
7676
interface
7777

78+
module logical function h5exist(filename, dname)
79+
character(*), intent(in) :: filename, dname
80+
end function h5exist
81+
82+
7883
module subroutine lt0write(filename, dname, value, ierr)
7984
character(*), intent(in) :: filename, dname
8085
class(*), intent(in) :: value

src/reader_lt.f90

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44

55
contains
66

7+
module procedure h5exist
8+
9+
type(hdf5_file) :: h
10+
11+
call h%initialize(filename, status='old', action='r')
12+
h5exist = h%exist(dname)
13+
call h%finalize()
14+
15+
end procedure h5exist
16+
17+
718
module procedure lt0read
819
type(hdf5_file) :: h
920
integer :: ier
1021

11-
call h%initialize(filename, ier, status='old')
22+
call h%initialize(filename, ier, status='old', action='r')
1223

1324
if (ier == 0) then
1425
select type (value)
@@ -41,7 +52,7 @@
4152
type(hdf5_file) :: h
4253
integer :: ier
4354

44-
call h%initialize(filename, ier, status='old')
55+
call h%initialize(filename, ier, status='old', action='r')
4556

4657
if (ier == 0) then
4758
select type (value)
@@ -72,7 +83,7 @@
7283
type(hdf5_file) :: h
7384
integer :: ier
7485

75-
call h%initialize(filename, ier, status='old')
86+
call h%initialize(filename, ier, status='old', action='r')
7687

7788
if (ier == 0) then
7889
select type (value)
@@ -103,7 +114,7 @@
103114
type(hdf5_file) :: h
104115
integer :: ier
105116

106-
call h%initialize(filename, ier, status='old')
117+
call h%initialize(filename, ier, status='old', action='r')
107118

108119
if (ier == 0) then
109120
select type (value)
@@ -134,7 +145,7 @@
134145
type(hdf5_file) :: h
135146
integer :: ier
136147

137-
call h%initialize(filename, ier, status='old')
148+
call h%initialize(filename, ier, status='old', action='r')
138149

139150
if (ier == 0) then
140151
select type (value)
@@ -165,7 +176,7 @@
165176
type(hdf5_file) :: h
166177
integer :: ier
167178

168-
call h%initialize(filename, ier, status='old')
179+
call h%initialize(filename, ier, status='old', action='r')
169180

170181
if (ier == 0) then
171182
select type (value)
@@ -196,7 +207,7 @@
196207
type(hdf5_file) :: h
197208
integer :: ier
198209

199-
call h%initialize(filename, ier, status='old')
210+
call h%initialize(filename, ier, status='old', action='r')
200211

201212
if (ier == 0) then
202213
select type (value)
@@ -227,7 +238,7 @@
227238
type(hdf5_file) :: h
228239
integer :: ier
229240

230-
call h%initialize(filename, ier, status='old')
241+
call h%initialize(filename, ier, status='old', action='r')
231242

232243
if (ier == 0) then
233244
select type (value)
@@ -253,4 +264,4 @@
253264

254265
end procedure lt7read
255266

256-
end submodule reader_lt
267+
end submodule reader_lt

src/tests/test_exist.f90

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
program test_exist
22
!! test "exist" variable
33
use, intrinsic :: iso_fortran_env, only : stderr=>error_unit
4-
use h5fortran, only: hdf5_file, h5write
4+
use h5fortran, only: hdf5_file, h5write, h5exist
55

66
implicit none (type, external)
77

@@ -25,4 +25,9 @@ program test_exist
2525

2626
if (h%exist('/foo')) error stop 'foo not exist'
2727

28+
call h%finalize()
29+
30+
if (.not. h5exist(path, '/x')) error stop 'x exists'
31+
if (h5exist(path, '/foo')) error stop 'foo not exist'
32+
2833
end program

src/tests/test_lt.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module test_lt
66

77
contains
88

9+
910
subroutine test_readwrite_lt(path)
1011

1112
character(*), intent(in) :: path

0 commit comments

Comments
 (0)