Skip to content

Commit 0268b26

Browse files
committed
Use reshape to transpose n-dim HDF5 arrays
1 parent eb70deb commit 0268b26

File tree

2 files changed

+14
-38
lines changed

2 files changed

+14
-38
lines changed

src/nf/io/nf_io_hdf5.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module subroutine get_hdf5_dataset_real32_1d(filename, object_name, values)
3131
!! HDF5 file name
3232
character(*), intent(in) :: object_name
3333
!! Object (dataset) name
34-
real(real32), allocatable, intent(in out) :: values(:)
34+
real(real32), allocatable, intent(out) :: values(:)
3535
!! Array to store the dataset values into
3636
end subroutine get_hdf5_dataset_real32_1d
3737

@@ -41,7 +41,7 @@ module subroutine get_hdf5_dataset_real32_2d(filename, object_name, values)
4141
!! HDF5 file name
4242
character(*), intent(in) :: object_name
4343
!! Object (dataset) name
44-
real(real32), allocatable, intent(in out) :: values(:,:)
44+
real(real32), allocatable, intent(out) :: values(:,:)
4545
!! Array to store the dataset values into
4646
end subroutine get_hdf5_dataset_real32_2d
4747

@@ -51,7 +51,7 @@ module subroutine get_hdf5_dataset_real32_4d(filename, object_name, values)
5151
!! HDF5 file name
5252
character(*), intent(in) :: object_name
5353
!! Object (dataset) name
54-
real(real32), allocatable, intent(in out) :: values(:,:,:,:)
54+
real(real32), allocatable, intent(out) :: values(:,:,:,:)
5555
!! Array to store the dataset values into
5656
end subroutine get_hdf5_dataset_real32_4d
5757

src/nf/io/nf_io_hdf5_submodule.f90

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
submodule(nf_io_hdf5) nf_io_hdf5_submodule
22

33
use iso_fortran_env, only: int64, real32, stderr => error_unit
4+
use functional, only: reverse
45
use h5fortran, only: hdf5_file
56
use hdf5, only: H5F_ACC_RDONLY_F, HID_T, &
67
h5aget_type_f, h5aopen_by_name_f, h5aread_f, &
@@ -50,7 +51,7 @@ module subroutine get_hdf5_dataset_real32_1d(filename, object_name, values)
5051

5152
character(*), intent(in) :: filename
5253
character(*), intent(in) :: object_name
53-
real(real32), allocatable, intent(in out) :: values(:)
54+
real(real32), allocatable, intent(out) :: values(:)
5455

5556
type(hdf5_file) :: f
5657
integer(int64), allocatable :: dims(:)
@@ -78,23 +79,15 @@ module subroutine get_hdf5_dataset_real32_2d(filename, object_name, values)
7879

7980
character(*), intent(in) :: filename
8081
character(*), intent(in) :: object_name
81-
real(real32), allocatable, intent(in out) :: values(:,:)
82+
real(real32), allocatable, intent(out) :: values(:,:)
8283

8384
type(hdf5_file) :: f
8485
integer(int64), allocatable :: dims(:)
8586

8687
call f % open(filename, 'r')
8788
call f % shape(object_name, dims)
8889

89-
! If values is already allocated, re-allocate only if incorrect shape
90-
if (allocated(values)) then
91-
if (.not. all(shape(values) == dims)) then
92-
deallocate(values)
93-
allocate(values(dims(1), dims(2)))
94-
end if
95-
else
96-
allocate(values(dims(1), dims(2)))
97-
end if
90+
allocate(values(dims(1), dims(2)))
9891

9992
call f % read(object_name, values)
10093
call f % close()
@@ -109,43 +102,26 @@ module subroutine get_hdf5_dataset_real32_4d(filename, object_name, values)
109102

110103
character(*), intent(in) :: filename
111104
character(*), intent(in) :: object_name
112-
real(real32), allocatable, intent(in out) :: values(:,:,:,:)
105+
real(real32), allocatable, intent(out) :: values(:,:,:,:)
113106

114107
type(hdf5_file) :: f
115108
integer(int64), allocatable :: dims(:)
116109

117110
call f % open(filename, 'r')
118111
call f % shape(object_name, dims)
119112

120-
! If values is already allocated, re-allocate only if incorrect shape
121-
if (allocated(values)) then
122-
if (.not. all(shape(values) == dims)) then
123-
deallocate(values)
124-
allocate(values(dims(1), dims(2), dims(3), dims(4)))
125-
end if
126-
else
127-
allocate(values(dims(1), dims(2), dims(3), dims(4)))
128-
end if
113+
allocate(values(dims(1), dims(2), dims(3), dims(4)))
129114

130115
call f % read(object_name, values)
131116
call f % close()
132117

133118
! Transpose the array to get from C to Fortran order
134-
values = reverse_dim_order(values)
119+
values = reshape( &
120+
values, &
121+
shape=[dims(4), dims(3), dims(2), dims(1)], &
122+
order=[4, 3, 2, 1] &
123+
)
135124

136125
end subroutine get_hdf5_dataset_real32_4d
137126

138-
139-
pure function reverse_dim_order(x) result(res)
140-
real, intent(in) :: x(:,:,:,:)
141-
real, allocatable :: res(:,:,:,:)
142-
integer :: dims(4)
143-
integer :: i, j, k, l
144-
dims = shape(x)
145-
allocate(res(dims(4), dims(3), dims(2), dims(1)))
146-
do concurrent(i = 1:dims(1), j = 1:dims(2), k = 1:dims(3), l = 1:dims(4))
147-
res(l,k,j,i) = x(i,j,k,l)
148-
end do
149-
end function reverse_dim_order
150-
151127
end submodule nf_io_hdf5_submodule

0 commit comments

Comments
 (0)