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, &
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
26 USE iso_c_binding,
ONLY: c_loc, &
29#include "./base/base_uses.f90"
33 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'hdf5_wrapper'
35 INTEGER,
PARAMETER,
PUBLIC :: hdf5_id = hid_t
48 IF (error < 0) cpabort(
'ERROR: failed to initialize HDF5 interface')
59 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 interface')
61 END SUBROUTINE h5close
68 SUBROUTINE h5fcreate(filename, file_id)
69 CHARACTER(LEN=*),
INTENT(IN) :: filename
70 INTEGER(KIND=hid_t),
INTENT(OUT) :: file_id
74 CALL h5fcreate_f(filename, h5f_acc_trunc_f, file_id, error)
75 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 file')
77 END SUBROUTINE h5fcreate
84 SUBROUTINE h5fopen(filename, file_id)
85 CHARACTER(LEN=*),
INTENT(IN) :: filename
86 INTEGER(KIND=hid_t),
INTENT(OUT) :: file_id
90 CALL h5fopen_f(trim(filename), h5f_acc_rdonly_f, file_id, error)
91 IF (error < 0) cpabort(
'ERROR: failed to open HDF5 file')
93 END SUBROUTINE h5fopen
99 SUBROUTINE h5fclose(file_id)
100 INTEGER(KIND=hid_t),
INTENT(IN) :: file_id
104 CALL h5fclose_f(file_id, error)
105 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 file')
107 END SUBROUTINE h5fclose
115 SUBROUTINE h5gcreate(loc_id, name, grp_id)
116 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
117 CHARACTER(LEN=*),
INTENT(IN) :: name
118 INTEGER(KIND=hid_t),
INTENT(OUT) :: grp_id
122 CALL h5gcreate_f(loc_id, name, grp_id, error)
123 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 group')
125 END SUBROUTINE h5gcreate
133 SUBROUTINE h5gopen(loc_id, name, grp_id)
134 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
135 CHARACTER(LEN=*),
INTENT(IN) :: name
136 INTEGER(KIND=hid_t),
INTENT(OUT) :: grp_id
140 CALL h5gopen_f(loc_id, name, grp_id, error)
141 IF (error < 0) cpabort(
'ERROR: failed to open HDF5 group')
143 END SUBROUTINE h5gopen
149 SUBROUTINE h5gclose(grp_id)
150 INTEGER(KIND=hid_t),
INTENT(IN) :: grp_id
154 CALL h5gclose_f(grp_id, error)
155 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 group')
157 END SUBROUTINE h5gclose
165 SUBROUTINE h5awrite_varlen_string(loc_id, attr_name, attr_data)
166 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
167 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
168 CHARACTER(LEN=*),
INTENT(IN),
TARGET :: attr_data
170 INTEGER :: error, output_unit
171 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
172 TYPE(c_ptr) :: buffer
173 TYPE(c_ptr),
TARGET :: in_between_ptr
176 CALL h5screate_f(h5s_scalar_f, space_id, error)
178 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
179 ' ERROR: failed to create HDF5 dataspace'
184 CALL h5tcopy_f(h5t_string, type_id, error)
185 CALL h5tset_cset_f(type_id, h5t_cset_utf8_f, error)
186 CALL h5tset_strpad_f(type_id, h5t_str_nullpad_f, error)
189 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
191 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
192 ' ERROR: failed to create HDF5 attribute'
198 in_between_ptr = c_loc(attr_data)
200 buffer = c_loc(in_between_ptr)
203 CALL h5awrite_f(attr_id, type_id, buffer, error)
205 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
206 ' ERROR: failed to write HDF5 attribute'
211 CALL h5aclose_f(attr_id, error)
213 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
214 ' ERROR: failed to close HDF5 attribute'
219 CALL h5sclose_f(space_id, error)
221 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
222 ' ERROR: failed to close HDF5 dataspace'
227 CALL h5tclose_f(type_id, error)
229 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
230 ' ERROR: failed to close HDF5 datatype'
234 END SUBROUTINE h5awrite_varlen_string
242 SUBROUTINE h5awrite_fixlen_string(loc_id, attr_name, attr_data)
243 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
244 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
245 CHARACTER(LEN=*),
INTENT(IN),
TARGET :: attr_data
247 INTEGER :: error, output_unit
248 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
249 TYPE(c_ptr) :: buffer
252 CALL h5screate_f(h5s_scalar_f, space_id, error)
254 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
255 ' ERROR: failed to create HDF5 dataspace'
260 CALL h5tcopy_f(h5t_c_s1, type_id, error)
261 CALL h5tset_cset_f(type_id, h5t_cset_utf8_f, error)
262 CALL h5tset_size_f(type_id, len(attr_data, size_t), error)
265 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
267 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
268 ' ERROR: failed to create HDF5 attribute'
273 buffer = c_loc(attr_data)
276 CALL h5awrite_f(attr_id, type_id, buffer, error)
278 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
279 ' ERROR: failed to write HDF5 attribute'
284 CALL h5aclose_f(attr_id, error)
286 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
287 ' ERROR: failed to close HDF5 attribute'
292 CALL h5sclose_f(space_id, error)
294 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
295 ' ERROR: failed to close HDF5 dataspace'
300 CALL h5tclose_f(type_id, error)
302 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
303 ' ERROR: failed to close HDF5 datatype'
307 END SUBROUTINE h5awrite_fixlen_string
315 SUBROUTINE h5awrite_boolean(loc_id, attr_name, attr_data)
316 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
317 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
318 LOGICAL,
INTENT(IN) :: attr_data
320 INTEGER :: error, output_unit
321 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
322 INTEGER,
TARGET :: attr_data_to_int
323 TYPE(c_ptr) :: buffer
328 CALL h5screate_f(h5s_scalar_f, space_id, error)
330 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
331 ' ERROR: failed to create HDF5 dataspace'
336 CALL h5tcreate_f(h5t_enum_f, int(1, size_t), type_id, error)
337 CALL h5tenum_insert_f(type_id,
"FALSE", 0, error)
338 CALL h5tenum_insert_f(type_id,
"TRUE", 1, error)
346 buffer = c_loc(attr_data_to_int)
349 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
351 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
352 ' ERROR: failed to create HDF5 attribute'
357 CALL h5awrite_f(attr_id, type_id, buffer, error)
359 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
360 ' ERROR: failed to write HDF5 attribute'
365 CALL h5aclose_f(attr_id, error)
367 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
368 ' ERROR: failed to close HDF5 attribute'
373 CALL h5sclose_f(space_id, error)
375 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
376 ' ERROR: failed to close HDF5 dataspace'
381 CALL h5tclose_f(type_id, error)
383 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
384 ' ERROR: failed to close HDF5 datatype'
388 END SUBROUTINE h5awrite_boolean
396 SUBROUTINE h5awrite_integer_scalar(loc_id, attr_name, attr_data)
397 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
398 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
399 INTEGER,
INTENT(IN),
TARGET :: attr_data
401 INTEGER :: error, output_unit
402 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
403 TYPE(c_ptr) :: buffer
406 CALL h5screate_f(h5s_scalar_f, space_id, error)
408 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
409 ' ERROR: failed to create HDF5 dataspace'
414 buffer = c_loc(attr_data)
417 type_id = h5t_native_integer
420 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
422 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
423 ' ERROR: failed to create HDF5 attribute'
428 CALL h5awrite_f(attr_id, type_id, buffer, error)
430 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
431 ' ERROR: failed to write HDF5 attribute'
436 CALL h5aclose_f(attr_id, error)
438 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
439 ' ERROR: failed to close HDF5 attribute'
444 CALL h5sclose_f(space_id, error)
446 WRITE (unit=output_unit, fmt=
"(/,T5,A,/)") &
447 ' ERROR: failed to close HDF5 dataspace'
451 END SUBROUTINE h5awrite_integer_scalar
459 SUBROUTINE h5awrite_double_scalar(loc_id, attr_name, attr_data)
460 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
461 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
462 REAL(KIND=
dp),
INTENT(IN),
TARGET :: attr_data
465 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
466 TYPE(c_ptr) :: buffer
469 CALL h5screate_f(h5s_scalar_f, space_id, error)
470 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataspace')
473 buffer = c_loc(attr_data)
476 type_id = h5t_native_double
479 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
480 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 attribute')
483 CALL h5awrite_f(attr_id, type_id, buffer, error)
484 IF (error < 0) cpabort(
'ERROR: failed to write HDF5 attribute')
487 CALL h5aclose_f(attr_id, error)
488 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 attribute')
491 CALL h5sclose_f(space_id, error)
492 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
494 END SUBROUTINE h5awrite_double_scalar
502 SUBROUTINE h5awrite_string_simple(loc_id, attr_name, attr_data)
503 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
504 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
505 CHARACTER(LEN=*),
DIMENSION(:),
INTENT(IN),
TARGET :: attr_data
508 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
509 INTEGER(KIND=hsize_t),
DIMENSION(2) :: dims
510 TYPE(c_ptr) :: buffer
512 dims(1) = len(attr_data(1), kind=hsize_t)
513 dims(2) =
SIZE(attr_data, kind=hsize_t)
516 CALL h5tcopy_f(h5t_c_s1, type_id, error)
517 CALL h5tset_cset_f(type_id, h5t_cset_utf8_f, error)
518 CALL h5tset_size_f(type_id, int(dims(1), size_t), error)
521 CALL h5screate_simple_f(1, dims(2:2), space_id, error)
522 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataspace')
525 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
526 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 attribute')
529 buffer = c_loc(attr_data(1))
532 CALL h5awrite_f(attr_id, type_id, buffer, error)
533 IF (error < 0) cpabort(
'ERROR: failed to write HDF5 attribute')
536 CALL h5aclose_f(attr_id, error)
537 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 attribute')
540 CALL h5sclose_f(space_id, error)
541 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
544 CALL h5tclose_f(type_id, error)
545 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 datatype')
547 END SUBROUTINE h5awrite_string_simple
555 SUBROUTINE h5awrite_double_simple(loc_id, attr_name, attr_data)
556 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
557 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
558 REAL(KIND=
dp),
DIMENSION(:),
INTENT(IN),
TARGET :: attr_data
561 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
562 INTEGER(KIND=hsize_t),
DIMENSION(1) :: dims
563 TYPE(c_ptr) :: buffer
565 dims(1) =
SIZE(attr_data, kind=hsize_t)
568 type_id = h5t_native_double
571 CALL h5screate_simple_f(1, dims, space_id, error)
572 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataspace')
575 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
576 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 attribute')
579 buffer = c_loc(attr_data(1))
582 CALL h5awrite_f(attr_id, type_id, buffer, error)
583 IF (error < 0) cpabort(
'ERROR: failed to write HDF5 attribute')
586 CALL h5aclose_f(attr_id, error)
587 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 attribute')
590 CALL h5sclose_f(space_id, error)
591 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
593 END SUBROUTINE h5awrite_double_simple
601 SUBROUTINE h5awrite_integer_simple(loc_id, attr_name, attr_data)
602 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
603 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
604 INTEGER,
DIMENSION(:),
INTENT(IN),
TARGET :: attr_data
607 INTEGER(KIND=hid_t) :: attr_id, space_id, type_id
608 INTEGER(KIND=hsize_t),
DIMENSION(1) :: dims
609 TYPE(c_ptr) :: buffer
611 dims(1) =
SIZE(attr_data, kind=hsize_t)
614 type_id = h5t_native_integer
617 CALL h5screate_simple_f(1, dims, space_id, error)
618 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataspace')
621 CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
622 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 attribute')
625 buffer = c_loc(attr_data(1))
628 CALL h5awrite_f(attr_id, type_id, buffer, error)
629 IF (error < 0) cpabort(
'ERROR: failed to write HDF5 attribute')
632 CALL h5aclose_f(attr_id, error)
633 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 attribute')
636 CALL h5sclose_f(space_id, error)
637 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
639 END SUBROUTINE h5awrite_integer_simple
647 SUBROUTINE h5dwrite_double_simple(loc_id, dset_name, dset_data)
648 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
649 CHARACTER(LEN=*),
INTENT(IN) :: dset_name
650 REAL(KIND=
dp),
DIMENSION(:),
INTENT(IN),
TARGET :: dset_data
653 INTEGER(KIND=hid_t) :: dset_id, space_id, type_id
654 INTEGER(KIND=hsize_t),
DIMENSION(1) :: dims
655 TYPE(c_ptr) :: buffer
657 dims(1) =
SIZE(dset_data, kind=hsize_t)
660 type_id = h5t_native_double
663 CALL h5screate_simple_f(1, dims, space_id, error)
664 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataspace')
667 CALL h5dcreate_f(loc_id, dset_name, type_id, space_id, dset_id, error)
668 IF (error < 0) cpabort(
'ERROR: failed to create HDF5 dataset')
671 buffer = c_loc(dset_data(1))
674 CALL h5dwrite_f(dset_id, type_id, buffer, error)
675 IF (error < 0) cpabort(
'ERROR: failed to write HDF5 dataset')
678 CALL h5dclose_f(dset_id, error)
679 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataset')
682 CALL h5sclose_f(space_id, error)
683 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
685 END SUBROUTINE h5dwrite_double_simple
693 SUBROUTINE h5dread_double_simple(loc_id, dset_name, dset_data)
694 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
695 CHARACTER(LEN=*),
INTENT(IN) :: dset_name
696 REAL(KIND=
dp),
DIMENSION(:),
INTENT(OUT) :: dset_data
699 INTEGER(KIND=hid_t) :: dset_id, npoints, space_id, type_id
700 INTEGER(KIND=hsize_t),
DIMENSION(1) :: dims
702 dims(1) =
SIZE(dset_data, kind=hsize_t)
705 type_id = h5t_native_double
708 CALL h5dopen_f(loc_id, dset_name, dset_id, error)
709 IF (error < 0) cpabort(
'ERROR: failed to open HDF5 dataset')
712 CALL h5dget_space_f(dset_id, space_id, error)
713 IF (error < 0) cpabort(
'ERROR: failed to fetch HDF5 dataspace info')
716 CALL h5sget_simple_extent_npoints_f(space_id, npoints, error)
717 IF (error < 0) cpabort(
'ERROR: failed to fetch HDF5 dataspace dimension')
720 CALL h5dread_f(dset_id, type_id, dset_data, dims, error)
721 IF (error < 0) cpabort(
'ERROR: failed to read HDF5 dataset')
724 CALL h5dclose_f(dset_id, error)
725 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataset')
728 CALL h5sclose_f(space_id, error)
729 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 dataspace')
731 END SUBROUTINE h5dread_double_simple
739 SUBROUTINE h5aread_double_scalar(loc_id, attr_name, attr_data)
740 INTEGER(KIND=hid_t),
INTENT(IN) :: loc_id
741 CHARACTER(LEN=*),
INTENT(IN) :: attr_name
742 REAL(KIND=
dp),
INTENT(OUT),
TARGET :: attr_data
745 INTEGER(KIND=hid_t) :: attr_id, type_id
746 TYPE(c_ptr) :: buffer
749 type_id = h5t_native_double
752 CALL h5aopen_f(loc_id, attr_name, attr_id, error)
753 IF (error < 0) cpabort(
'ERROR: failed to open HDF5 attribute')
755 buffer = c_loc(attr_data)
757 CALL h5aread_f(attr_id, type_id, buffer, error)
758 IF (error < 0) cpabort(
'ERROR: failed to read HDF5 attribute')
761 CALL h5aclose_f(attr_id, error)
762 IF (error < 0) cpabort(
'ERROR: failed to close HDF5 attribute')
764 END SUBROUTINE h5aread_double_scalar
A wrapper around the HDF5 Fortran API.
Defines the basic variable types.
integer, parameter, public dp