1
1
submodule(nf_io_hdf5) nf_io_hdf5_submodule
2
2
3
3
use iso_fortran_env, only: int64, real32, stderr = > error_unit
4
+ use functional, only: reverse
4
5
use h5fortran, only: hdf5_file
5
6
use hdf5, only: H5F_ACC_RDONLY_F, HID_T, &
6
7
h5aget_type_f, h5aopen_by_name_f, h5aread_f, &
@@ -50,7 +51,7 @@ module subroutine get_hdf5_dataset_real32_1d(filename, object_name, values)
50
51
51
52
character (* ), intent (in ) :: filename
52
53
character (* ), intent (in ) :: object_name
53
- real (real32), allocatable , intent (in out ) :: values(:)
54
+ real (real32), allocatable , intent (out ) :: values(:)
54
55
55
56
type (hdf5_file) :: f
56
57
integer (int64), allocatable :: dims(:)
@@ -78,23 +79,15 @@ module subroutine get_hdf5_dataset_real32_2d(filename, object_name, values)
78
79
79
80
character (* ), intent (in ) :: filename
80
81
character (* ), intent (in ) :: object_name
81
- real (real32), allocatable , intent (in out ) :: values(:,:)
82
+ real (real32), allocatable , intent (out ) :: values(:,:)
82
83
83
84
type (hdf5_file) :: f
84
85
integer (int64), allocatable :: dims(:)
85
86
86
87
call f % open (filename, ' r' )
87
88
call f % shape (object_name, dims)
88
89
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 )))
98
91
99
92
call f % read (object_name, values)
100
93
call f % close ()
@@ -109,43 +102,26 @@ module subroutine get_hdf5_dataset_real32_4d(filename, object_name, values)
109
102
110
103
character (* ), intent (in ) :: filename
111
104
character (* ), intent (in ) :: object_name
112
- real (real32), allocatable , intent (in out ) :: values(:,:,:,:)
105
+ real (real32), allocatable , intent (out ) :: values(:,:,:,:)
113
106
114
107
type (hdf5_file) :: f
115
108
integer (int64), allocatable :: dims(:)
116
109
117
110
call f % open (filename, ' r' )
118
111
call f % shape (object_name, dims)
119
112
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 )))
129
114
130
115
call f % read (object_name, values)
131
116
call f % close ()
132
117
133
118
! 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
+ )
135
124
136
125
end subroutine get_hdf5_dataset_real32_4d
137
126
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
-
151
127
end submodule nf_io_hdf5_submodule
0 commit comments