forked from pseudospectators/mpi2vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_hdf2xmf_2d.f90
414 lines (334 loc) · 14.4 KB
/
convert_hdf2xmf_2d.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
program hdf2xml
implicit none
integer, parameter :: pr = 8
integer :: nx, ny, nscal,nvec,ivec,ndt,idt,io_error=0,iscal
real (kind=pr) :: time, xl, yl
character (len=5) :: basefilename
character (len=80) :: ofilename
character prefixes_scalar(100)*80 ! maximal 100 prefixes à 80 chars length
character prefixes_vector(100)*80 ! maximal 100 prefixes à 80 chars length
character timesteps(10000)*80 ! maximal 10000 time steps à 80 chars length
LOGICAL :: file_exists, scalars_exist, vectors_exist
write (*,*) "-------------------------"
write (*,*) " Fortran XMF generator"
write (*,*) " 2D VERSION"
write (*,*) "-------------------------"
call get_command_argument(1, ofilename)
write(*,*) "writing XMF to "//trim(adjustl(ofilename))
!---------------------------------------------------------------------
! read in LIST OF scalar prefixes. note it is mandatory to follow
! file naming convention. for a file mask_00010.h5 "mask" is in
! this file
! ---------------------------------------------------------------------
INQUIRE(FILE='prefixes_scalar.in', EXIST=scalars_exist)
if(scalars_exist) then
io_error=0
open (15, file='prefixes_scalar.in', action='read', status='old' )
nscal = 1
write (*,'(A)',advance='no') "scalars = "
do while (io_error==0)
read (15,'(A)',iostat=io_error) prefixes_scalar(nscal)
if (io_error ==0) then ! pay a little attention here, else we go one too far
write (*,'(A,1x)',advance='no') trim(adjustl(prefixes_scalar(nscal)))
nscal = nscal+1
endif
enddo
nscal = nscal-1
write(*,*) " "
close (15)
endif
! now we know how many scalars we have and what their names are
!---------------------------------------------------------------------
! read in LIST OF vector prefixes. note vectors have 3 components
! and one basename. this file contains the basename only i.e. you
! save usx_00010.h5 usy_00010.h5 usz_00010.h5 the basename is "us"
! note it is mandatory to follow file naming convention.
! ---------------------------------------------------------------------
INQUIRE(FILE='prefixes_vector.in', EXIST=vectors_exist)
if(vectors_exist) then
io_error=0
open (15, file='prefixes_vector.in', action='read', status='old' )
nvec = 1
write (*,'(A)',advance='no') "vectors = "
do while (io_error==0)
read (15,'(A)',iostat=io_error) prefixes_vector(nvec)
if (io_error ==0) then ! pay a little attention here, else we go one too far
write (*,'(A,1x)',advance='no') trim(adjustl(prefixes_vector(nvec)))
nvec = nvec+1
endif
enddo
nvec = nvec-1
write(*,*) " "
close (15)
endif
! now we know how many vectors we have and what their names are
if((vectors_exist .eqv. .false.) .and. (scalars_exist .eqv. .false.)) then
write(*,*) "Neither scalars nor vectors found; unable to proceed."
stop
endif
!--------------------------------------------------
! Read in LIST OF different time steps, generated externally
! file looks like
! 00010
! 00015
! 00020 etc
!--------------------------------------------------
INQUIRE(FILE='timesteps.in', EXIST=file_exists)
if(file_exists) then
io_error=0
open (15, file='timesteps.in', action='read', status='old' )
ndt = 1
write (*,'(A)',advance='no') "time steps= "
do while (io_error==0)
read (15,'(A)',iostat=io_error) timesteps(ndt)
if (io_error ==0) then ! pay a little attention here, else we
! go one too far
write (*,'(A,1x)',advance='no') trim(adjustl(timesteps(ndt)))
ndt = ndt+1
endif
enddo
ndt = ndt-1
write(*,*) " "
close (15)
else
write(*,*) "timesteps.in does not exist; unable to proceed."
stop
endif
! Now we know how many vectors we have and what their names are
!-----------------------------------------------------------------------------
!-----------------------------------------------------------------------------
! Create the main *.xml file
!-----------------------------------------------------------------------------
!-----------------------------------------------------------------------------
open (14, file=ofilename, status='replace')
! Read the resolution so that we can call BeginFile.
if(scalars_exist) then
call Fetch_attributes (trim(adjustl(prefixes_scalar(1)))//"_"//trim(adjustl(timesteps(1)))//".h5",&
trim(adjustl(prefixes_scalar(1))), nx,ny,xl,yl,time)
endif
if(vectors_exist) then
call Fetch_attributes(trim(adjustl(prefixes_vector(1)))//"x_"//trim(adjustl(timesteps(1)))//".h5",&
trim(adjustl(prefixes_vector(1)))//"x", nx,ny,xl,yl,time)
endif
! File header, requires resolution
call BeginFile(nx,ny)
! begin loops over time steps
do idt = 1, ndt
! Get the time from any of these files
if(scalars_exist) then
call Fetch_attributes (trim(adjustl(prefixes_scalar(1)))//"_"//trim(adjustl(timesteps(idt)))//".h5",&
trim(adjustl(prefixes_scalar(1))), nx,ny,xl,yl,time)
endif
if(vectors_exist) then
call Fetch_attributes (trim(adjustl(prefixes_vector(1)))//"x_"//trim(adjustl(timesteps(idt)))//".h5",&
trim(adjustl(prefixes_vector(1)))//"x", nx,ny,xl,yl,time)
endif
! Time step header
call BeginTimeStep(nx,ny,xl,yl,time)
! Scalars
if(scalars_exist) then
do iscal = 1, nscal
call WriteScalar(trim(adjustl(timesteps(idt))),&
trim(adjustl(prefixes_scalar(iscal))),&
nx,ny,xl,yl,time)
end do
endif
! Vectors
if(vectors_exist) then
do ivec = 1, nvec
call WriteVector(trim(adjustl(timesteps(idt))),&
trim(adjustl(prefixes_vector(ivec))),&
nx,ny,xl,yl,time)
enddo
endif
! Time step footer
call EndTimeStep()
enddo
! file footer
call EndFile()
close (14)
end program hdf2xml
subroutine BeginFile(nx,ny)
implicit none
integer, parameter :: pr = 8
integer, intent (in) :: nx, ny
character (len=128) :: nxyz_str
! NB: indices output in z,y,x order. (C vs Fortran ordering?)
write (nxyz_str,'(2(i4.4,1x))') ny, nx
write (*,'("Resolution: ",2(i4,1x) )') nx, ny
write (14,'(A)') '<?xml version="1.0" ?>'
write (14,'(A)') '<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" ['
write (14,'(A)') '<!ENTITY nxnynz "'//trim(adjustl(nxyz_str))//'">'
! write also half the resolution to XMF file since we might use it
! if striding is active!
write (nxyz_str,'(2(i4.4,1x))') ny/2, nx/2
write (14,'(A)') '<!ENTITY subnxnynz "'//trim(adjustl(nxyz_str))//'">'
write (14,'(A)') ']>'
write (14,'(A)') '<Xdmf Version="2.0"> '
write (14,'(A)') '<Domain> '
write (14,'(A)') '<Grid Name="Box" GridType="Collection" CollectionType="Temporal">'
end subroutine BeginFile
subroutine EndFile()
implicit none
write (14,'(A)') '</Grid>'
write (14,'(A)') '</Domain>'
write (14,'(A)') '</Xdmf> '
end subroutine EndFile
subroutine BeginTimeStep( nx, ny, xl, yl , time )
implicit none
integer, parameter :: pr = 8
integer, intent (in) :: nx, ny
real (kind=pr), intent(in) :: xl,yl, time
character (len=128) :: time_string
write (time_string,'(es15.8)') time
write (14,'(A)') '<!-- beginning time step --> '
write (14,'(A)') '<Grid Name="FLUSI_cartesian_grid" GridType="Uniform">'
write (14,'(A)') ' <Time Value="'//trim(adjustl(time_string))//'" />'
write (14,'(A)') ' <Topology TopologyType="2DCoRectMesh" Dimensions="&nxnynz;" />'
write (14,'(A)') ' '
write (14,'(A)') ' <Geometry GeometryType="Origin_DxDy">'
write (14,'(A)') ' <DataItem Dimensions="2" NumberType="Float" Format="XML">'
write (14,'(A)') ' 0 0'
write (14,'(A)') ' </DataItem>'
write (14,'(A)') ' <DataItem Dimensions="2" NumberType="Float" Format="XML">'
! NB: indices output in z,y,x order. (C vs Fortran ordering?)
write (14,'(4x,2(es15.8,1x))') yl/dble(ny), xl/dble(nx)
write (14,'(A)') ' </DataItem>'
write (14,'(A)') ' </Geometry>'
end subroutine BeginTimeStep
subroutine EndTimeStep ( )
implicit none
write (14,*) '</Grid>'
end subroutine EndTimeStep
subroutine WriteVector( basefilename, prefix, nx, ny, xl, yl , time )
implicit none
integer, parameter :: pr = 8
integer, intent (in) :: nx, ny
real (kind=pr), intent(in) :: xl,yl, time
character (len=*), intent (in) :: basefilename, prefix
! I DID NOT FIGURE OUT HOW I CAN DEFINE A 2D vector....
! write (14,'(A)') ' '
! write (14,'(A)') ' <!--Vector-->'
! write (14,'(A)') ' <Attribute Name="'//prefix//'" AttributeType="Vector" Center="Node">'
! write (14,'(A)') ' <DataItem ItemType="Function" Function="JOIN($0, $1)" Dimensions="&nxnynz; 2" NumberType="Float">'
! write (14,'(A)') ' <DataItem Dimensions="&nxnynz;" NumberType="Float" Format="HDF">'
! write (14,'(A)') ' '//prefix//'x_'//basefilename//'.h5:/'//prefix//'x'
! write (14,'(A)') ' </DataItem>'
! write (14,'(A)') ' '
! write (14,'(A)') ' <DataItem Dimensions="&nxnynz;" NumberType="Float" Format="HDF">'
! write (14,'(A)') ' '//prefix//'y_'//basefilename//'.h5:/'//prefix//'y'
! write (14,'(A)') ' </DataItem>'
! write (14,'(A)') ' </DataItem>'
! write (14,'(A)') ' </Attribute> '
write (14,'(A)') ' '
write (14,'(A)') ' <!--Scalar-->'
write (14,'(A)') ' <Attribute Name="'//prefix//'x" AttributeType="Scalar" Center="Node">'
write (14,'(A)') ' <DataItem Dimensions="&nxnynz;" NumberType="Float" Format="HDF">'
write (14,'(A)') ' '//prefix//'x_'//basefilename//'.h5:/'//prefix//'x'
write (14,'(A)') ' </DataItem>'
write (14,'(A)') ' </Attribute>'
write (14,'(A)') ' '
write (14,'(A)') ' <!--Scalar-->'
write (14,'(A)') ' <Attribute Name="'//prefix//'y" AttributeType="Scalar" Center="Node">'
write (14,'(A)') ' <DataItem Dimensions="&nxnynz;" NumberType="Float" Format="HDF">'
write (14,'(A)') ' '//prefix//'y_'//basefilename//'.h5:/'//prefix//'y'
write (14,'(A)') ' </DataItem>'
write (14,'(A)') ' </Attribute>'
end subroutine WriteVector
subroutine WriteScalar ( basefilename, prefix, nx, ny, xl, yl, time )
implicit none
integer, parameter :: pr = 8
integer, intent (in) :: nx, ny
real (kind=pr), intent(in) :: xl,yl, time
character (len=*), intent (in) :: basefilename, prefix
write (14,'(A)') ' '
write (14,'(A)') ' <!--Scalar-->'
write (14,'(A)') ' <Attribute Name="'//prefix//'" AttributeType="Scalar" Center="Node">'
write (14,'(A)') ' <DataItem Dimensions="&nxnynz;" NumberType="Float" Format="HDF">'
write (14,'(A)') ' '//prefix//'_'//basefilename//'.h5:/'//prefix
write (14,'(A)') ' </DataItem>'
write (14,'(A)') ' </Attribute>'
end subroutine WriteScalar
!----------------------------------------------------
! This routine fetches the resolution, the domain size and the time
! form a *.h5 file
!----------------------------------------------------
! filename: a *.h5 file to read from.
! dsetname: a dataset inside the file. in our system, we only have one per file
! and this matches the prefix: mask_00010.h5 --> dsetname = "mask"
! note:
! the file must contain the dataset
! but especially the attributes "nxyz", "time", "domain_size"
!----------------------------------------------------
subroutine Fetch_attributes( filename, dsetname, nx, ny, xl, yl , time )
use hdf5
implicit none
integer, parameter :: pr = 8
integer, intent (out) :: nx, ny
real (kind=pr), intent(out) :: xl,yl, time
character(len=*) :: filename ! file name
character(len=*) :: dsetname ! dataset name
character(len=4) :: aname ! attribute name
character(len=11) :: aname2
integer(hid_t) :: file_id ! file identifier
integer(hid_t) :: dset_id ! dataset identifier
integer(hid_t) :: attr_id ! attribute identifier
integer(hid_t) :: aspace_id ! attribute dataspace identifier
integer(hid_t) :: atype_id ! attribute dataspace identifier
integer(hsize_t), dimension(1) :: adims = (/1/) ! attribute dimension
integer :: arank = 1 ! attribure rank
integer(size_t) :: attrlen ! length of the attribute string
real (kind=pr) :: attr_data ! attribute data
real (kind=pr), dimension (1:2) :: attr_data2
integer, dimension (1:2) :: attr_data3
integer :: error ! error flag
integer(hsize_t), dimension(1) :: data_dims
! Initialize FORTRAN interface.
CALL h5open_f(error)
! Open an existing file.
CALL h5fopen_f (filename, H5F_ACC_RDWR_F, file_id, error)
! Open an existing dataset.
CALL h5dopen_f(file_id, dsetname, dset_id, error)
!!!!!!!!!!!!!!!!!!!!!!!!!!
! open attribute (time)
!!!!!!!!!!!!!!!!!!!!!!!!!!
aname = "time"
CALL h5aopen_f(dset_id, aname, attr_id, error)
! Get dataspace and read
CALL h5aget_space_f(attr_id, aspace_id, error)
data_dims(1) = 1
CALL h5aread_f( attr_id, H5T_NATIVE_DOUBLE, attr_data, data_dims, error)
time = attr_data
CALL h5aclose_f(attr_id, error) ! Close the attribute.
CALL h5sclose_f(aspace_id, error) ! Terminate access to the data space.
!!!!!!!!!!!!!!!!!!!!!!!!!!
! open attribute (domain_length)
!!!!!!!!!!!!!!!!!!!!!!!!!!
aname2 = "domain_size"
CALL h5aopen_f(dset_id, aname2, attr_id, error)
! Get dataspace and read
CALL h5aget_space_f(attr_id, aspace_id, error)
data_dims(1) = 2
CALL h5aread_f( attr_id, H5T_NATIVE_DOUBLE, attr_data2, data_dims, error)
xl = attr_data2(1)
yl = attr_data2(2)
CALL h5aclose_f(attr_id, error) ! Close the attribute.
CALL h5sclose_f(aspace_id, error) ! Terminate access to the data space.
!!!!!!!!!!!!!!!!!!!!!!!!!!
! open attribute (sizes)
!!!!!!!!!!!!!!!!!!!!!!!!!!
aname = "nxyz"
CALL h5aopen_f(dset_id, aname, attr_id, error)
! Get dataspace and read
CALL h5aget_space_f(attr_id, aspace_id, error)
data_dims(1) = 2
CALL h5aread_f( attr_id, H5T_NATIVE_INTEGER, attr_data3, data_dims, error)
nx = attr_data3(1)
ny = attr_data3(2)
CALL h5aclose_f(attr_id, error) ! Close the attribute.
CALL h5sclose_f(aspace_id, error) ! Terminate access to the data space.
CALL h5dclose_f(dset_id, error) ! End access to the dataset and release resources used by it.
CALL h5fclose_f(file_id, error) ! Close the file.
CALL h5close_f(error) ! Close FORTRAN interface.
! write (*,'("time=",es12.4," domain=",3(es12.4,1x),2x,3(i3,1x))') time, xl,yl,zl, nx, ny, nz
end subroutine Fetch_attributes