Skip to content

Commit 54f6008

Browse files
committed
improve file open logic
1 parent 0ce1e6c commit 54f6008

File tree

11 files changed

+111
-81
lines changed

11 files changed

+111
-81
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ endif()
77

88
project(h5fortran
99
LANGUAGES C Fortran
10-
VERSION 4.11.0
10+
VERSION 4.12.0
1111
)
1212

1313
include(CTest)

cmake/check_hdf5.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ program main
1717
print '(a,i0)', "H5F_ACC_TRUNC_F = ", H5F_ACC_TRUNC_F
1818
print '(a,i0)', "H5F_ACC_RDWR_F = ", H5F_ACC_RDWR_F
1919

20-
if(H5F_ACC_RDONLY_F == H5F_ACC_TRUNC_F .or. H5F_ACC_RDONLY_F == H5F_ACC_RDWR_F) then
20+
if(any(H5F_ACC_RDONLY_F == [H5F_ACC_TRUNC_F, H5F_ACC_RDWR_F])) then
2121
error stop "H5F_ACC_RDONLY, H5F_ACC_TRUNC, H5F_ACC_RDWR are not all distinct"
2222
endif
2323

example/CMakeLists.txt

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.22...3.27)
1+
cmake_minimum_required(VERSION 3.22...4.1)
22

33
project(h5fortranExample
44
LANGUAGES C CXX Fortran)
@@ -11,21 +11,13 @@ find_package(h5fortran CONFIG REQUIRED)
1111
add_library(fortran_interface fortran_interface.f90)
1212
target_link_libraries(fortran_interface PRIVATE h5fortran::h5fortran)
1313

14-
add_executable(ex_fcn ex_fcn.f90)
15-
target_link_libraries(ex_fcn PRIVATE h5fortran::h5fortran)
16-
add_test(NAME Fortran_fcn COMMAND ex_fcn)
14+
foreach(t IN ITEMS ex_fcn ex_oo ex_oo3d char_repeat_read vtk_write)
1715

18-
add_executable(ex_oo ex_oo.f90)
19-
target_link_libraries(ex_oo PRIVATE h5fortran::h5fortran)
20-
add_test(NAME Fortran_oo COMMAND ex_oo)
16+
add_executable(${t} ${t}.f90)
17+
target_link_libraries(${t} PRIVATE h5fortran::h5fortran)
18+
add_test(NAME Fortran_${t} COMMAND ${t})
2119

22-
add_executable(repeat_char_read char_repeat_read.f90)
23-
target_link_libraries(repeat_char_read PRIVATE h5fortran::h5fortran)
24-
25-
## VTK HDF5 write example
26-
add_executable(vtk_write vtk_write.f90)
27-
target_link_libraries(vtk_write PRIVATE h5fortran::h5fortran)
28-
add_test(NAME VTK COMMAND vtk_write ${CMAKE_CURRENT_BINARY_DIR}/vtk.hdf)
20+
endforeach()
2921

3022
## C, C++ examples
3123

@@ -45,11 +37,15 @@ add_test(NAME CPP_fcn COMMAND cpp_fcn)
4537
get_property(test_names DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY TESTS)
4638
set_property(TEST ${test_names} PROPERTY WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
4739

40+
if(NOT DEFINED LINUX AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
41+
set(LINUX true)
42+
endif()
43+
4844
if(WIN32)
4945
set_property(TEST ${test_names} PROPERTY
5046
ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:${ZLIB_INCLUDE_DIRS}/../bin;PATH=path_list_prepend:${h5fortran_DIR}/../bin"
5147
)
52-
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
48+
elseif(LINUX)
5349
set_property(TEST ${test_names} PROPERTY
5450
ENVIRONMENT_MODIFICATION "LD_LIBRARY_PATH=path_list_prepend:${ZLIB_INCLUDE_DIRS}/../lib;LD_LIBRARY_PATH=path_list_prepend:${h5fortran_DIR}/../lib"
5551
)

example/ex_oo3d.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
program demo
2+
3+
use h5fortran, only : hdf5_file
4+
5+
implicit none
6+
7+
character(len=*), parameter :: h5file = 'test_oo3d.h5'
8+
9+
type(hdf5_file) :: h5f
10+
11+
real, allocatable :: v3(:, :, :)
12+
13+
allocate(v3(2, 4, 3))
14+
15+
v3 = 0.
16+
17+
call h5f % open(h5file, action='rw', debug=.true.)
18+
call h5f % write('/value1', 123.)
19+
call h5f % close()
20+
21+
!> must always specify action= even if reopening previous file
22+
call h5f % open(h5file, action='rw', comp_lvl = 1, debug=.true.)
23+
call h5f % write('/value2', v3)
24+
call h5f % close()
25+
26+
end program

example/vtk_write.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ program vtk_write
2626
real, allocatable :: Iterations(:,:,:), IterationsGradient(:,:,:,:)
2727

2828
call get_command_argument(1, filename, status=ierr)
29-
if(ierr /= 0) error stop "please give filename to write"
29+
if(ierr /= 0) filename = "vtk_hdf_example.h5"
3030

3131
call h % open(filename, 'w')
3232

fpm.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "h5fortran"
22
description = "Lightweight object-oriented HDF5 interface"
33
categories = "io"
4-
version = "4.11.0"
4+
version = "4.12.0"
55

66
[build]
77
auto-tests = false

src/attr_read.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ call get_obj_class(self, obj_name // ":" // attr_name, attr_id, attr_class)
2222

2323
!> cast the dataset read from disk to the variable type presented by user h5f%read("/my_dataset", x, "y")
2424
!! select case doesn't allow H5T_*
25-
if(attr_class == H5T_FLOAT_F .OR. attr_class == H5T_INTEGER_F) then
25+
if(any(attr_class == [H5T_FLOAT_F, H5T_INTEGER_F])) then
2626
select type(A)
2727
type is (real(real64))
2828
call H5Aread_f(attr_id, H5T_NATIVE_DOUBLE, A, attr_dims, ier)

src/interface.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ module h5fortran
1717
!> main type
1818
type :: hdf5_file
1919

20-
character(:), allocatable :: filename
20+
character(:), allocatable :: filename, last_error
2121
integer(HID_T) :: file_id = 0 !< sentinel value to avoid uninitialized variable lint
22+
integer :: file_mode = -1
2223

2324
logical :: debug = .false.
2425
logical :: fletcher32 = .false.

src/read_scalar.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
!> We only cast when needed to save memory.
4040
!! select case doesn't allow H5T_*
4141
!! https://support.hdfgroup.org/HDF5/doc/UG/HDF5_Users_Guide-Responsive%20HTML5/index.html#t=HDF5_Users_Guide%2FDatatypes%2FHDF5_Datatypes.htm%23TOC_6_10_Data_Transferbc-26&rhtocid=6.5_2
42-
if(dclass == H5T_FLOAT_F .OR. dclass == H5T_INTEGER_F) then
42+
if(any(dclass == [H5T_FLOAT_F, H5T_INTEGER_F])) then
4343
select type(A)
4444
type is (real(real64))
4545
call H5Dread_f(dset_id, H5T_NATIVE_DOUBLE, A, dims, ier, mem_space_id, file_space_id)

src/reader.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ call get_obj_class(self, dname, dset_id, dclass)
2525

2626
!> casting is handled by HDF5 library internally
2727
!! select case doesn't allow H5T_*
28-
if(dclass == H5T_FLOAT_F .OR. dclass == H5T_INTEGER_F) then
28+
if(any(dclass == [H5T_FLOAT_F, H5T_INTEGER_F])) then
2929
select type(A)
3030
type is (real(real64))
3131
call h5dread_f(dset_id, H5T_NATIVE_DOUBLE, A, dims, ier, mem_space_id, file_space_id, xfer_id)

0 commit comments

Comments
 (0)