-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdf.f90
executable file
·255 lines (172 loc) · 7.01 KB
/
hdf.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! !
! FILE: HdfRoutines.F90 !
! CONTAINS: subroutine hdf_read_serial_1d !
! !
! PURPOSE: I/O routines. !
! !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine HdfStart
use hdf5
implicit none
integer :: hdf_error
call h5open_f(hdf_error)
end subroutine HdfStart
!====================================================================
subroutine HdfClose
use hdf5
implicit none
integer :: hdf_error
call h5close_f(hdf_error)
end subroutine HdfClose
!====================================================================
subroutine HdfSerialWriteReal1D(dsetname,filename,var,sz)
use hdf5
implicit none
character*50,intent(in) :: dsetname,filename
integer, intent(in) :: sz
real, dimension(1:sz), intent(in) :: var
integer(HID_T) :: file_id
integer(HID_T) :: dset, filespace
integer :: hdf_error
integer(HSIZE_T) :: dims(1)
logical :: dsetexists
call h5fopen_f(filename, H5F_ACC_RDWR_F, file_id, hdf_error)
dims(1)=sz
call h5screate_simple_f(1, dims, filespace, hdf_error)
call h5lexists_f(file_id,dsetname,dsetexists,hdf_error)
if(dsetexists) call h5ldelete_f(file_id,dsetname,hdf_error)
call h5dcreate_f(file_id, dsetname, H5T_NATIVE_DOUBLE, &
& filespace, dset, hdf_error)
call h5dwrite_f(dset, H5T_NATIVE_DOUBLE, &
& var(1:sz), dims, hdf_error)
call h5dclose_f(dset, hdf_error)
call h5sclose_f(filespace, hdf_error)
call h5fclose_f(file_id, hdf_error)
end subroutine HdfSerialWriteReal1D
!====================================================================
subroutine HdfWriteSerialReal2D(filename,dsetname,ndx,ndy,var)
use hdf5
implicit none
character*50,intent(in) :: dsetname,filename
integer, intent(in) :: ndx,ndy
real :: var(ndx,ndy)
integer(HID_T) :: file_id
integer(HID_T) :: filespace
integer(HID_T) :: dset
integer :: hdf_error, ndims
integer(HSIZE_T) :: dims(2)
!RO Set offsets and element counts
ndims = 2
dims(1)=ndx
dims(2)=ndy
call h5screate_simple_f(ndims, dims, filespace, hdf_error)
call h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, hdf_error)
call h5dcreate_f(file_id, dsetname, H5T_NATIVE_DOUBLE, &
filespace, dset, hdf_error)
call h5dwrite_f(dset, H5T_NATIVE_DOUBLE,var, dims, hdf_error)
call h5dclose_f(dset, hdf_error)
call h5sclose_f(filespace, hdf_error)
call h5fclose_f(file_id, hdf_error)
end subroutine HdfWriteSerialReal2D
!====================================================================
subroutine HdfSerialWriteReal2D(dsetname,filename,var,sx,sy)
use hdf5
implicit none
character*50,intent(in) :: dsetname,filename
integer, intent(in) :: sx,sy
real, dimension(sx,sy), intent(in) :: var
integer(HID_T) :: file_id
integer(HID_T) :: dset, filespace
integer :: hdf_error,ndims
integer(HSIZE_T) :: dims(2)
logical :: dsetexists
call h5fopen_f(filename, H5F_ACC_RDWR_F, file_id, hdf_error)
ndims = 2
dims(1)=sx
dims(2)=sy
call h5screate_simple_f(ndims, dims, &
& filespace, hdf_error)
call h5lexists_f(file_id,dsetname,dsetexists,hdf_error)
if(dsetexists) call h5ldelete_f(file_id,dsetname,hdf_error)
call h5dcreate_f(file_id, dsetname, H5T_NATIVE_DOUBLE, &
& filespace, dset, hdf_error)
call h5dwrite_f(dset, H5T_NATIVE_DOUBLE, &
& var(1:sx,1:sy), dims, hdf_error)
call h5dclose_f(dset, hdf_error)
call h5sclose_f(filespace, hdf_error)
call h5fclose_f(file_id, hdf_error)
end subroutine HdfSerialWriteReal2D
!====================================================================
subroutine HdfCreateBlankFile(filename)
use hdf5
implicit none
character*50,intent(in) :: filename
integer(HID_T) :: file_id
integer :: hdf_error
call h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, hdf_error)
call h5fclose_f(file_id, hdf_error)
end subroutine HdfCreateBlankFile
!====================================================================
subroutine HdfSerialReadReal1D(dsetname,filename,var,sz)
use hdf5
implicit none
character*30,intent(in) :: dsetname,filename
integer, intent(in) :: sz
real, dimension(sz), intent(out) :: var
integer(HID_T) :: file_id
integer(HID_T) :: dset
integer :: hdf_error
integer(HSIZE_T) :: dims(1)
call h5fopen_f(filename, H5F_ACC_RDWR_F, file_id, hdf_error)
dims(1)=sz
call h5dopen_f(file_id, dsetname, dset, hdf_error)
call h5dread_f(dset, H5T_NATIVE_DOUBLE, &
& var, dims, hdf_error)
call h5dclose_f(dset, hdf_error)
call h5fclose_f(file_id, hdf_error)
end subroutine HdfSerialReadReal1D
!====================================================================
subroutine HdfSerialReadReal2D(dsetname,filename,var,sx,sy)
use hdf5
implicit none
character*30,intent(in) :: dsetname,filename
integer, intent(in) :: sx,sy
real, dimension(sx,sy), intent(out) :: var
integer(HID_T) :: file_id
integer(HID_T) :: dset
integer :: hdf_error
integer(HSIZE_T) :: dims(2)
call h5fopen_f(filename, H5F_ACC_RDWR_F, file_id, hdf_error)
dims(1)=sx
dims(2)=sy
call h5dopen_f(file_id, dsetname, dset, hdf_error)
call h5dread_f(dset, H5T_NATIVE_DOUBLE, &
& var, dims, hdf_error)
call h5dclose_f(dset, hdf_error)
call h5fclose_f(file_id, hdf_error)
end subroutine HdfSerialReadReal2D
subroutine HdfWriteSerialInt2D(filename,dsetname,ndx,ndy,var)
use hdf5
implicit none
character*50,intent(in) :: dsetname,filename
integer, intent(in) :: ndx,ndy
integer :: var(ndx,ndy)
integer(HID_T) :: file_id
integer(HID_T) :: filespace
integer(HID_T) :: dset
integer :: hdf_error, ndims
integer(HSIZE_T) :: dims(2)
!RO Set offsets and element counts
ndims = 2
dims(1)=ndx
dims(2)=ndy
call h5screate_simple_f(ndims, dims, filespace, hdf_error)
call h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, hdf_error)
call h5dcreate_f(file_id, dsetname, H5T_NATIVE_INTEGER, &
filespace, dset, hdf_error)
call h5dwrite_f(dset, H5T_NATIVE_INTEGER,var, dims, hdf_error)
call h5dclose_f(dset, hdf_error)
call h5sclose_f(filespace, hdf_error)
call h5fclose_f(file_id, hdf_error)
end subroutine HdfWriteSerialInt2D