18 h5aclose_f, h5acreate_f, h5aopen_f, h5aread_f, h5awrite_f, h5close_f, h5dclose_f, &
19 h5dcreate_f, h5dget_space_f, h5dopen_f, h5dread_f, h5dwrite_f, h5f_acc_rdonly_f, h5f_acc_rdwr_f, &
20 h5f_acc_trunc_f, h5fclose_f, h5fcreate_f, h5fopen_f, h5gclose_f, h5gcreate_f, h5gopen_f, &
21 h5open_f, h5s_scalar_f, h5sclose_f, h5screate_f, h5screate_simple_f, &
22 h5sget_simple_extent_npoints_f, h5t_c_s1, h5t_cset_utf8_f, h5t_enum_f, h5t_native_double, &
23 h5t_native_integer, h5t_str_nullpad_f, h5t_string, h5tclose_f, h5tcopy_f, h5tcreate_f, &
24 h5tenum_insert_f, h5tset_cset_f, h5tset_size_f, h5tset_strpad_f, hid_t, hsize_t, size_t
25#if defined(__parallel)
26 USE hdf5,
ONLY: h5pcreate_f, h5pclose_f, h5p_file_access_f, h5p_default_f, h5pset_fapl_mpio_f
29 USE iso_c_binding,
ONLY: c_loc, &
34#include "./base/base_uses.f90"
40 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'hdf5_wrapper'
42 INTEGER,
PARAMETER,
PUBLIC :: hdf5_id = hid_t
44 PUBLIC :: h5aread_double_scalar, h5awrite_boolean, h5awrite_double_scalar, h5awrite_double_simple
45 PUBLIC :: h5awrite_fixlen_string, h5awrite_integer_scalar, h5awrite_integer_simple
46 PUBLIC :: h5awrite_string_simple, h5close, h5dread_double_simple, h5dwrite_double_simple, h5fclose
47 PUBLIC :: h5fcreate, h5fopen, h5gclose, h5gcreate, h5gopen, h5open
60 IF (error < 0) cpabort(
'ERROR: failed to initialize HDF5 interface')
71 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 interface')
73 END SUBROUTINE h5close
80 SUBROUTINE h5fcreate(filename, file_id)
81 CHARACTER(LEN=*),
INTENT(IN) :: filename
82 INTEGER(KIND=hid_t),
INTENT(OUT) :: file_id
85#if defined(__parallel)
86 INTEGER(KIND=hid_t) :: plist_id
89#if defined(__parallel)
90 CALL h5pcreate_f(h5p_file_access_f, plist_id, error)
92 CALL h5fcreate_f(filename, h5f_acc_trunc_f, file_id, error, access_prp=plist_id)
93 CALL h5pclose_f(plist_id, error)
95 CALL h5fcreate_f(filename, h5f_acc_trunc_f, file_id, error)
98 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 file')
100 END SUBROUTINE h5fcreate
107 SUBROUTINE h5fopen(filename, file_id)
108 CHARACTER(LEN=*),
INTENT(IN) :: filename
109 INTEGER(KIND=hid_t),
INTENT(OUT) :: file_id
112#if defined(__parallel)
113 INTEGER(KIND=hid_t) :: plist_id
116#if defined(__parallel)
117 CALL h5pcreate_f(h5p_file_access_f, plist_id, error)
119 CALL h5fopen_f(filename, h5f_acc_rdwr_f, file_id, error, access_prp=plist_id)
120 CALL h5pclose_f(plist_id, error)
122 CALL h5fopen_f(filename, h5f_acc_rdwr_f, file_id, error)
125 IF (error < 0) cpabort(
'ERROR: failed to open HDF5 file')
127 END SUBROUTINE h5fopen
133 SUBROUTINE h5fclose(file_id)
134 INTEGER(KIND=hid_t),
INTENT(IN) :: file_id
138 CALL h5fclose_f(file_id, error)
139 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 file')
141 END SUBROUTINE h5fclose
149 SUBROUTINE h5gcreate(loc_id, name, grp_id)
150 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
151 CHARACTER(LEN=*),
INTENT(IN) :: name
152 INTEGER(KIND=hid_t),
INTENT(OUT) :: grp_id
156 CALL h5gcreate_f(loc_id, name, grp_id, error)
157 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 group')
159 END SUBROUTINE h5gcreate
167 SUBROUTINE h5gopen(loc_id, name, grp_id)
168 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
169 CHARACTER(LEN=*),
INTENT(IN) :: name
170 INTEGER(KIND=hid_t),
INTENT(OUT) :: grp_id
174 CALL h5gopen_f(loc_id, name, grp_id, error)
175 IF (error < 0) cpabort(
'ERROR: failed to open HDF5 group')
177 END SUBROUTINE h5gopen
183 SUBROUTINE h5gclose(grp_id)
184 INTEGER(KIND=hid_t),
INTENT(IN) :: grp_id
188 CALL h5gclose_f(grp_id, error)
189 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 group')
191 END SUBROUTINE h5gclose
199 SUBROUTINE h5awrite_varlen_string(loc_id, attr_name, attr_data)
200 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
201 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
202 CHARACTER(LEN=*),
INTENT(IN),
TARGET :: attr_data
204 INTEGER :: error, output_unit
205 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
206 TYPE(c_ptr) :: buffer
207 TYPE(c_ptr),
TARGET :: in_between_ptr
212 CALL h5screate_f(h5s_scalar_f, space_id, error)
214 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
215 ' ERROR: failed to create HDF5 dataspace'
220 CALL h5tcopy_f(h5t_string, type_id, error)
221 CALL h5tset_cset_f(type_id, h5t_cset_utf8_f, error)
222 CALL h5tset_strpad_f(type_id, h5t_str_nullpad_f, error)
225 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
227 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
228 ' ERROR: failed to create HDF5 attribute'
234 in_between_ptr = c_loc(attr_data)
236 buffer = c_loc(in_between_ptr)
239 CALL h5awrite_f(attr_id, type_id, buffer, error)
241 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
242 ' ERROR: failed to write HDF5 attribute'
247 CALL h5aclose_f(attr_id, error)
249 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
250 ' ERROR: failed to close HDF5 attribute'
255 CALL h5sclose_f(space_id, error)
257 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
258 ' ERROR: failed to close HDF5 dataspace'
263 CALL h5tclose_f(type_id, error)
265 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
266 ' ERROR: failed to close HDF5 datatype'
270 END SUBROUTINE h5awrite_varlen_string
278 SUBROUTINE h5awrite_fixlen_string(loc_id, attr_name, attr_data)
279 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
280 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
281 CHARACTER(LEN=*),
INTENT(IN),
TARGET :: attr_data
283 INTEGER :: error, output_unit
284 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
285 TYPE(c_ptr) :: buffer
290 CALL h5screate_f(h5s_scalar_f, space_id, error)
292 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
293 ' ERROR: failed to create HDF5 dataspace'
298 CALL h5tcopy_f(h5t_c_s1, type_id, error)
299 CALL h5tset_cset_f(type_id, h5t_cset_utf8_f, error)
300 CALL h5tset_size_f(type_id, len(attr_data, size_t), error)
303 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
305 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
306 ' ERROR: failed to create HDF5 attribute'
311 buffer = c_loc(attr_data)
314 CALL h5awrite_f(attr_id, type_id, buffer, error)
316 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
317 ' ERROR: failed to write HDF5 attribute'
322 CALL h5aclose_f(attr_id, error)
324 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
325 ' ERROR: failed to close HDF5 attribute'
330 CALL h5sclose_f(space_id, error)
332 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
333 ' ERROR: failed to close HDF5 dataspace'
338 CALL h5tclose_f(type_id, error)
340 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
341 ' ERROR: failed to close HDF5 datatype'
345 END SUBROUTINE h5awrite_fixlen_string
353 SUBROUTINE h5awrite_boolean(loc_id, attr_name, attr_data)
354 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
355 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
356 LOGICAL,
INTENT(IN) :: attr_data
358 INTEGER :: error, output_unit
359 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
360 INTEGER,
TARGET :: attr_data_to_int
361 TYPE(c_ptr) :: buffer
368 CALL h5screate_f(h5s_scalar_f, space_id, error)
370 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
371 ' ERROR: failed to create HDF5 dataspace'
376 CALL h5tcreate_f(h5t_enum_f, int(1, size_t), type_id, error)
377 CALL h5tenum_insert_f(type_id,
"FALSE", 0, error)
378 CALL h5tenum_insert_f(type_id,
"TRUE", 1, error)
386 buffer = c_loc(attr_data_to_int)
389 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
391 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
392 ' ERROR: failed to create HDF5 attribute'
397 CALL h5awrite_f(attr_id, type_id, buffer, error)
399 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
400 ' ERROR: failed to write HDF5 attribute'
405 CALL h5aclose_f(attr_id, error)
407 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
408 ' ERROR: failed to close HDF5 attribute'
413 CALL h5sclose_f(space_id, error)
415 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
416 ' ERROR: failed to close HDF5 dataspace'
421 CALL h5tclose_f(type_id, error)
423 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
424 ' ERROR: failed to close HDF5 datatype'
428 END SUBROUTINE h5awrite_boolean
436 SUBROUTINE h5awrite_integer_scalar(loc_id, attr_name, attr_data)
437 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
438 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
439 INTEGER,
INTENT(IN),
TARGET :: attr_data
441 INTEGER :: error, output_unit
442 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
443 TYPE(c_ptr) :: buffer
448 CALL h5screate_f(h5s_scalar_f, space_id, error)
450 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
451 ' ERROR: failed to create HDF5 dataspace'
456 buffer = c_loc(attr_data)
459 type_id = h5t_native_integer
462 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
464 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
465 ' ERROR: failed to create HDF5 attribute'
470 CALL h5awrite_f(attr_id, type_id, buffer, error)
472 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
473 ' ERROR: failed to write HDF5 attribute'
478 CALL h5aclose_f(attr_id, error)
480 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
481 ' ERROR: failed to close HDF5 attribute'
486 CALL h5sclose_f(space_id, error)
488 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
489 ' ERROR: failed to close HDF5 dataspace'
493 END SUBROUTINE h5awrite_integer_scalar
501 SUBROUTINE h5awrite_double_scalar(loc_id, attr_name, attr_data)
502 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
503 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
504 REAL(KIND=
dp),
INTENT(IN),
TARGET :: attr_data
507 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
508 TYPE(c_ptr) :: buffer
511 CALL h5screate_f(h5s_scalar_f, space_id, error)
512 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataspace')
515 buffer = c_loc(attr_data)
518 type_id = h5t_native_double
521 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
522 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 attribute')
525 CALL h5awrite_f(attr_id, type_id, buffer, error)
526 IF (error < 0) cpabort(
'ERROR: failed to write HDF5 attribute')
529 CALL h5aclose_f(attr_id, error)
530 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 attribute')
533 CALL h5sclose_f(space_id, error)
534 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
536 END SUBROUTINE h5awrite_double_scalar
544 SUBROUTINE h5awrite_string_simple(loc_id, attr_name, attr_data)
545 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
546 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
547 CHARACTER(LEN=*),
DIMENSION(:),
INTENT(IN),
TARGET :: attr_data
550 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
551 INTEGER(KIND=hsize_t),
DIMENSION(2) :: dims
552 TYPE(c_ptr) :: buffer
554 dims(1) = len(attr_data(1), kind=hsize_t)
555 dims(2) =
SIZE(attr_data, kind=hsize_t)
558 CALL h5tcopy_f(h5t_c_s1, type_id, error)
559 CALL h5tset_cset_f(type_id, h5t_cset_utf8_f, error)
560 CALL h5tset_size_f(type_id, int(dims(1), size_t), error)
563 CALL h5screate_simple_f(1, dims(2:2), space_id, error)
564 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataspace')
567 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
568 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 attribute')
571 buffer = c_loc(attr_data(1))
574 CALL h5awrite_f(attr_id, type_id, buffer, error)
575 IF (error < 0) cpabort(
'ERROR: failed to write HDF5 attribute')
578 CALL h5aclose_f(attr_id, error)
579 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 attribute')
582 CALL h5sclose_f(space_id, error)
583 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
586 CALL h5tclose_f(type_id, error)
587 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 datatype')
589 END SUBROUTINE h5awrite_string_simple
597 SUBROUTINE h5awrite_double_simple(loc_id, attr_name, attr_data)
598 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
599 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
600 REAL(KIND=
dp),
DIMENSION(:),
INTENT(IN),
TARGET :: attr_data
603 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
604 INTEGER(KIND=hsize_t),
DIMENSION(1) :: dims
605 TYPE(c_ptr) :: buffer
607 dims(1) =
SIZE(attr_data, kind=hsize_t)
610 type_id = h5t_native_double
613 CALL h5screate_simple_f(1, dims, space_id, error)
614 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataspace')
617 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
618 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 attribute')
621 buffer = c_loc(attr_data(1))
624 CALL h5awrite_f(attr_id, type_id, buffer, error)
625 IF (error < 0) cpabort(
'ERROR: failed to write HDF5 attribute')
628 CALL h5aclose_f(attr_id, error)
629 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 attribute')
632 CALL h5sclose_f(space_id, error)
633 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
635 END SUBROUTINE h5awrite_double_simple
643 SUBROUTINE h5awrite_integer_simple(loc_id, attr_name, attr_data)
644 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
645 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
646 INTEGER,
DIMENSION(:),
INTENT(IN),
TARGET :: attr_data
649 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
650 INTEGER(KIND=hsize_t),
DIMENSION(1) :: dims
651 TYPE(c_ptr) :: buffer
653 dims(1) =
SIZE(attr_data, kind=hsize_t)
656 type_id = h5t_native_integer
659 CALL h5screate_simple_f(1, dims, space_id, error)
660 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataspace')
663 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
664 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 attribute')
667 buffer = c_loc(attr_data(1))
670 CALL h5awrite_f(attr_id, type_id, buffer, error)
671 IF (error < 0) cpabort(
'ERROR: failed to write HDF5 attribute')
674 CALL h5aclose_f(attr_id, error)
675 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 attribute')
678 CALL h5sclose_f(space_id, error)
679 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
681 END SUBROUTINE h5awrite_integer_simple
689 SUBROUTINE h5dwrite_double_simple(loc_id, dset_name, dset_data)
690 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
691 CHARACTER(LEN=*),
INTENT(IN) :: dset_name
692 REAL(KIND=
dp),
DIMENSION(:),
INTENT(IN),
TARGET :: dset_data
695 INTEGER(KIND=hid_t) :: dset_id, space_id, type_id
696 INTEGER(KIND=hsize_t),
DIMENSION(1) :: dims
697 TYPE(c_ptr) :: buffer
699 dims(1) =
SIZE(dset_data, kind=hsize_t)
702 type_id = h5t_native_double
705 CALL h5screate_simple_f(1, dims, space_id, error)
706 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataspace')
709 CALL h5dcreate_f(loc_id, dset_name, type_id, space_id, dset_id, error)
710 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataset')
713 buffer = c_loc(dset_data(1))
716 CALL h5dwrite_f(dset_id, type_id, buffer, error)
717 IF (error < 0) cpabort(
'ERROR: failed to write HDF5 dataset')
720 CALL h5dclose_f(dset_id, error)
721 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataset')
724 CALL h5sclose_f(space_id, error)
725 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
727 END SUBROUTINE h5dwrite_double_simple
735 SUBROUTINE h5dread_double_simple(loc_id, dset_name, dset_data)
736 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
737 CHARACTER(LEN=*),
INTENT(IN) :: dset_name
738 REAL(KIND=
dp),
DIMENSION(:),
INTENT(OUT) :: dset_data
741 INTEGER(KIND=hid_t) :: dset_id, npoints, space_id, type_id
742 INTEGER(KIND=hsize_t),
DIMENSION(1) :: dims
744 dims(1) =
SIZE(dset_data, kind=hsize_t)
747 type_id = h5t_native_double
750 CALL h5dopen_f(loc_id, dset_name, dset_id, error)
751 IF (error < 0) cpabort(
'ERROR: failed to open HDF5 dataset')
754 CALL h5dget_space_f(dset_id, space_id, error)
755 IF (error < 0) cpabort(
'ERROR: failed to fetch HDF5 dataspace info')
758 CALL h5sget_simple_extent_npoints_f(space_id, npoints, error)
759 IF (error < 0) cpabort(
'ERROR: failed to fetch HDF5 dataspace dimension')
762 CALL h5dread_f(dset_id, type_id, dset_data, dims, error)
763 IF (error < 0) cpabort(
'ERROR: failed to read HDF5 dataset')
766 CALL h5dclose_f(dset_id, error)
767 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataset')
770 CALL h5sclose_f(space_id, error)
771 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
773 END SUBROUTINE h5dread_double_simple
781 SUBROUTINE h5aread_double_scalar(loc_id, attr_name, attr_data)
782 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
783 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
784 REAL(KIND=
dp),
INTENT(OUT),
TARGET :: attr_data
787 INTEGER(KIND=hid_t) :: attr_id, type_id
788 TYPE(c_ptr) :: buffer
791 type_id = h5t_native_double
794 CALL h5aopen_f(loc_id, attr_name, attr_id, error)
795 IF (error < 0) cpabort(
'ERROR: failed to open HDF5 attribute')
797 buffer = c_loc(attr_data)
799 CALL h5aread_f(attr_id, type_id, buffer, error)
800 IF (error < 0) cpabort(
'ERROR: failed to read HDF5 attribute')
803 CALL h5aclose_f(attr_id, error)
804 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 attribute')
806 END SUBROUTINE h5aread_double_scalar
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
A wrapper around the HDF5 Fortran API.
Defines the basic variable types.
integer, parameter, public dp
Interface to the message passing library MPI.
type(mp_comm_type), parameter, public mp_comm_world
type(mp_info_type), parameter, public mp_info_null