Skip to content

Commit

Permalink
make hdf_shape_check top-level procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Feb 24, 2020
1 parent fa8a909 commit 78228c4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 54 deletions.
47 changes: 39 additions & 8 deletions src/interface.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ module h5fortran
use hdf5, only : HID_T, SIZE_T, HSIZE_T, H5F_ACC_RDONLY_F, H5F_ACC_RDWR_F, H5F_ACC_TRUNC_F, &
h5open_f, h5close_f, h5gcreate_f, h5gclose_f, h5fopen_f, h5fcreate_f, h5fclose_f, h5lexists_f, &
h5get_libversion_f, h5eset_auto_f
use h5lt, only : h5ltget_dataset_ndims_f, h5ltget_dataset_info_f

use string_utils, only : toLower, strip_trailing_null, truncate_string_null

implicit none
private
public :: hdf5_file, toLower, hsize_t, strip_trailing_null, truncate_string_null, check, h5write, h5read
public :: hdf5_file, toLower, hdf_shape_check, hsize_t, strip_trailing_null, truncate_string_null, &
check, h5write, h5read


!> main type
Expand Down Expand Up @@ -159,13 +161,6 @@ module subroutine lt7read(filename, dname, value, ierr)
end subroutine lt7read


module subroutine hdf_setup_read(self, dname, dims, ierr)
class(hdf5_file), intent(in) :: self
character(*), intent(in) :: dname
integer(HSIZE_T), intent(in) :: dims(:)
integer, intent(out) :: ierr
end subroutine hdf_setup_read

module subroutine hdf_setup_write(self, dname, dtype, dims, sid, did, ierr, chunk_size)
class(hdf5_file), intent(inout) :: self
character(*), intent(in) :: dname
Expand Down Expand Up @@ -505,4 +500,40 @@ logical function check(ierr, msg)
end function check


subroutine hdf_shape_check(self, dname, dims, ierr)
class(hdf5_file), intent(in) :: self
character(*), intent(in) :: dname
integer(HSIZE_T), intent(in) :: dims(:)
integer, intent(out) :: ierr

integer(SIZE_T) :: dsize
integer(HSIZE_T) :: ddims(size(dims))
integer :: dtype, drank

if (.not.self%exist(dname, ierr)) return

!> check for matching rank, else bad reads can occur--doesn't always crash without this check
call h5ltget_dataset_ndims_f(self%lid, dname, drank, ierr)
if (check(ierr, 'ERROR: get_dataset_ndim ' // dname // ' read ' // self%filename)) return

if (drank /= size(dims)) then
write(stderr,'(A,I6,A,I6)') 'ERROR: rank mismatch ' // dname // ' = ',drank,' variable rank =', size(dims)
ierr = -1
return
endif

!> check for matching size, else bad reads can occur.

call h5ltget_dataset_info_f(self%lid, dname, ddims, dtype, dsize, ierr)
if (check(ierr, 'ERROR: get_dataset_info ' // dname // ' read ' // self%filename)) return

if(.not. all(dims == ddims)) then
write(stderr,*) 'ERROR: shape mismatch ' // dname // ' = ',ddims,' variable shape =', dims
ierr = -1
return
endif

end subroutine hdf_shape_check


end module h5fortran
40 changes: 1 addition & 39 deletions src/read.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,14 @@
!! This submodule is for reading HDF5 via submodules
use hdf5, only : h5dopen_f, h5dread_f, h5dclose_f, h5dget_create_plist_f, &
h5pget_layout_f, H5D_CONTIGUOUS_F, H5D_CHUNKED_F
use H5LT, only : h5ltget_dataset_info_f, &
h5ltread_dataset_f, h5ltread_dataset_int_f, h5ltread_dataset_float_f, h5ltread_dataset_double_f,&
h5ltget_dataset_ndims_f, &
use H5LT, only : h5ltread_dataset_f, h5ltread_dataset_int_f, h5ltread_dataset_float_f, h5ltread_dataset_double_f,&
h5ltread_dataset_string_f, h5ltpath_valid_f

implicit none

contains


module procedure hdf_setup_read
! module subroutine hdf_setup_read(self, dname, dims, ierr)
! class(hdf5_file), intent(in) :: self
! character(*), intent(in) :: dname
! integer(hsize_t), intent(in) :: dims(:)
! integer, intent(out) :: ierr
integer(SIZE_T) :: dsize
integer(HSIZE_T) :: ddims(size(dims))
integer :: dtype, drank

if (.not.self%exist(dname, ierr)) return

!> check for matching rank, else bad reads can occur--doesn't always crash without this check
call h5ltget_dataset_ndims_f(self%lid, dname, drank, ierr)
if (check(ierr, 'ERROR: get_dataset_ndim ' // dname // ' read ' // self%filename)) return

if (drank /= size(dims)) then
write(stderr,'(A,I6,A,I6)') 'ERROR: rank mismatch ' // dname // ' = ',drank,' variable rank =', size(dims)
ierr = -1
return
endif

!> check for matching size, else bad reads can occur.

call h5ltget_dataset_info_f(self%lid, dname, ddims, dtype, dsize, ierr)
if (check(ierr, 'ERROR: get_dataset_info ' // dname // ' read ' // self%filename)) return

if(.not. all(dims == ddims)) then
write(stderr,*) 'ERROR: shape mismatch ' // dname // ' = ',ddims,' variable shape =', dims
ierr = -1
return
endif

end procedure hdf_setup_read


module procedure hdf_get_shape
!! must get dims before info, as "dims" must be allocated or segfault occurs.
integer(SIZE_T) :: dsize
Expand Down
14 changes: 7 additions & 7 deletions src/reader.f90
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
integer(HSIZE_T) :: dims(rank(value))
dims = shape(value)

call hdf_setup_read(self, dname, dims, ierr)
call hdf_shape_check(self, dname, dims, ierr)
if (ierr /= 0) return

select type (value)
Expand Down Expand Up @@ -84,7 +84,7 @@
integer(HSIZE_T) :: dims(rank(value))
dims = shape(value)

call hdf_setup_read(self, dname, dims, ierr)
call hdf_shape_check(self, dname, dims, ierr)
if (ierr /= 0) return

select type (value)
Expand Down Expand Up @@ -121,7 +121,7 @@
integer(HSIZE_T) :: dims(rank(value))
dims = shape(value)

call hdf_setup_read(self, dname, dims, ierr)
call hdf_shape_check(self, dname, dims, ierr)
if (ierr /= 0) return

select type (value)
Expand Down Expand Up @@ -158,7 +158,7 @@
integer(HSIZE_T) :: dims(rank(value))
dims = shape(value)

call hdf_setup_read(self, dname, dims, ierr)
call hdf_shape_check(self, dname, dims, ierr)
if (ierr /= 0) return

select type (value)
Expand Down Expand Up @@ -195,7 +195,7 @@
integer(HSIZE_T) :: dims(rank(value))
dims = shape(value)

call hdf_setup_read(self, dname, dims, ierr)
call hdf_shape_check(self, dname, dims, ierr)
if (ierr /= 0) return

select type (value)
Expand Down Expand Up @@ -232,7 +232,7 @@
integer(HSIZE_T) :: dims(rank(value))
dims = shape(value)

call hdf_setup_read(self, dname, dims, ierr)
call hdf_shape_check(self, dname, dims, ierr)
if (ierr /= 0) return

select type (value)
Expand Down Expand Up @@ -269,7 +269,7 @@
integer(HSIZE_T) :: dims(rank(value))
dims = shape(value)

call hdf_setup_read(self, dname, dims, ierr)
call hdf_shape_check(self, dname, dims, ierr)
if (ierr /= 0) return

select type (value)
Expand Down

0 comments on commit 78228c4

Please sign in to comment.