LCOV - code coverage report
Current view: top level - src - hdf5_wrapper.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 60.2 % 251 151
Test Date: 2026-11-24 01:22:08 Functions: 72.2 % 18 13

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief A wrapper around the HDF5 Fortran API
      10              : !> \par History
      11              : !>      04.2023 created [SB]
      12              : !> \author Stefano Battaglia
      13              : ! **************************************************************************************************
      14              : MODULE hdf5_wrapper
      15              : 
      16              : #ifdef __HDF5
      17              :    USE hdf5, ONLY: &
      18              :       h5aclose_f, h5acreate_f, h5aopen_f, h5aread_f, h5awrite_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
      25              : #endif
      26              :    USE iso_c_binding, ONLY: C_LOC, &
      27              :                             C_PTR
      28              :    USE cp_log_handling, ONLY: cp_logger_get_default_io_unit
      29              :    USE kinds, ONLY: dp
      30              : #include "./base/base_uses.f90"
      31              : 
      32              :    IMPLICIT NONE
      33              : 
      34              :    PRIVATE
      35              : 
      36              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'hdf5_wrapper'
      37              : #ifdef __HDF5
      38              :    INTEGER, PARAMETER, PUBLIC           :: hdf5_id = hid_t
      39              : 
      40              :    PUBLIC :: h5aread_double_scalar, h5awrite_boolean, h5awrite_double_scalar, h5awrite_double_simple
      41              :    PUBLIC :: h5awrite_fixlen_string, h5awrite_integer_scalar, h5awrite_integer_simple
      42              :    PUBLIC :: h5awrite_string_simple, h5dread_double_simple, h5dwrite_double_simple, h5fclose
      43              :    PUBLIC :: h5fcreate, h5fopen, h5gclose, h5gcreate, h5gopen, h5open
      44              : #endif
      45              : 
      46              : CONTAINS
      47              : 
      48              : #ifdef __HDF5
      49              : ! **************************************************************************************************
      50              : !> \brief Initialize the HDF5 Fortran API. Keep it initialized for other users (e.g. openPMD).
      51              : ! **************************************************************************************************
      52            3 :    SUBROUTINE h5open()
      53              :       INTEGER                                            :: error
      54              : 
      55            3 :       CALL h5open_f(error)
      56            3 :       IF (error < 0) CPABORT('ERROR: failed to initialize HDF5 interface')
      57              : 
      58            3 :    END SUBROUTINE h5open
      59              : 
      60              : ! **************************************************************************************************
      61              : !> \brief Create a HDF5 file
      62              : !> \param filename the name of the hdf5 file
      63              : !> \param file_id the file id of the hdf5 file
      64              : ! **************************************************************************************************
      65            3 :    SUBROUTINE h5fcreate(filename, file_id)
      66              :       CHARACTER(LEN=*), INTENT(IN)                       :: filename
      67              :       INTEGER(KIND=hid_t), INTENT(OUT)                   :: file_id
      68              : 
      69              :       INTEGER                                            :: error
      70              : 
      71            3 :       CALL h5fcreate_f(filename, h5f_acc_trunc_f, file_id, error)
      72              : 
      73            3 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 file')
      74              : 
      75            3 :    END SUBROUTINE h5fcreate
      76              : 
      77              : ! **************************************************************************************************
      78              : !> \brief Open a HDF5 file
      79              : !> \param filename the name of the hdf5 file
      80              : !> \param file_id the file id of the hdf5 file
      81              : ! **************************************************************************************************
      82            0 :    SUBROUTINE h5fopen(filename, file_id)
      83              :       CHARACTER(LEN=*), INTENT(IN)                       :: filename
      84              :       INTEGER(KIND=hid_t), INTENT(OUT)                   :: file_id
      85              : 
      86              :       INTEGER                                            :: error
      87              : 
      88            0 :       CALL h5fopen_f(filename, h5f_acc_rdonly_f, file_id, error)
      89              : 
      90            0 :       IF (error < 0) CPABORT('ERROR: failed to open HDF5 file')
      91              : 
      92            0 :    END SUBROUTINE h5fopen
      93              : 
      94              : ! **************************************************************************************************
      95              : !> \brief Close a HDF5 file
      96              : !> \param file_id the file id of the hdf5 file
      97              : ! **************************************************************************************************
      98            3 :    SUBROUTINE h5fclose(file_id)
      99              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: file_id
     100              : 
     101              :       INTEGER                                            :: error
     102              : 
     103            3 :       CALL h5fclose_f(file_id, error)
     104            3 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 file')
     105              : 
     106            3 :    END SUBROUTINE h5fclose
     107              : 
     108              : ! **************************************************************************************************
     109              : !> \brief Create a HDF5 group
     110              : !> \param loc_id file or group identifier
     111              : !> \param name name of the group
     112              : !> \param grp_id group identifier
     113              : ! **************************************************************************************************
     114           18 :    SUBROUTINE h5gcreate(loc_id, name, grp_id)
     115              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     116              :       CHARACTER(LEN=*), INTENT(IN)                       :: name
     117              :       INTEGER(KIND=hid_t), INTENT(OUT)                   :: grp_id
     118              : 
     119              :       INTEGER                                            :: error
     120              : 
     121           18 :       CALL h5gcreate_f(loc_id, name, grp_id, error)
     122           18 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 group')
     123              : 
     124           18 :    END SUBROUTINE h5gcreate
     125              : 
     126              : ! **************************************************************************************************
     127              : !> \brief Open a HDF5 group
     128              : !> \param loc_id file or group identifier
     129              : !> \param name name of the group
     130              : !> \param grp_id group identifier
     131              : ! **************************************************************************************************
     132            0 :    SUBROUTINE h5gopen(loc_id, name, grp_id)
     133              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     134              :       CHARACTER(LEN=*), INTENT(IN)                       :: name
     135              :       INTEGER(KIND=hid_t), INTENT(OUT)                   :: grp_id
     136              : 
     137              :       INTEGER                                            :: error
     138              : 
     139            0 :       CALL h5gopen_f(loc_id, name, grp_id, error)
     140            0 :       IF (error < 0) CPABORT('ERROR: failed to open HDF5 group')
     141              : 
     142            0 :    END SUBROUTINE h5gopen
     143              : 
     144              : ! **************************************************************************************************
     145              : !> \brief Close a HDF5 group
     146              : !> \param grp_id group identifier
     147              : ! **************************************************************************************************
     148           18 :    SUBROUTINE h5gclose(grp_id)
     149              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: grp_id
     150              : 
     151              :       INTEGER                                            :: error
     152              : 
     153           18 :       CALL h5gclose_f(grp_id, error)
     154           18 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 group')
     155              : 
     156           18 :    END SUBROUTINE h5gclose
     157              : 
     158              : ! **************************************************************************************************
     159              : !> \brief Write a variable-length string attribute
     160              : !> \param loc_id either file id or group id
     161              : !> \param attr_name the name of the attribute
     162              : !> \param attr_data the attribute data, i.e. the string to write
     163              : ! **************************************************************************************************
     164            0 :    SUBROUTINE h5awrite_varlen_string(loc_id, attr_name, attr_data)
     165              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     166              :       CHARACTER(LEN=*), INTENT(IN)                       :: attr_name
     167              :       CHARACTER(LEN=*), INTENT(IN), TARGET               :: attr_data
     168              : 
     169              :       INTEGER                                            :: error, output_unit
     170              :       INTEGER(KIND=hid_t)                                :: attr_id, space_id, type_id
     171              :       TYPE(c_ptr)                                        :: buffer
     172              :       TYPE(c_ptr), TARGET                                :: in_between_ptr
     173              : 
     174            0 :       output_unit = cp_logger_get_default_io_unit()
     175              : 
     176              :       ! create a scalar dataspace
     177            0 :       CALL h5screate_f(h5s_scalar_f, space_id, error)
     178            0 :       IF (error < 0) THEN
     179              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     180            0 :             ' ERROR: failed to create HDF5 dataspace'
     181            0 :          RETURN
     182              :       END IF
     183              : 
     184              :       ! create a variable-length string type
     185            0 :       CALL h5tcopy_f(h5t_string, type_id, error)
     186            0 :       CALL h5tset_cset_f(type_id, h5t_cset_utf8_f, error)
     187            0 :       CALL h5tset_strpad_f(type_id, h5t_str_nullpad_f, error)
     188              : 
     189              :       ! create the attribute
     190            0 :       CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
     191            0 :       IF (error < 0) THEN
     192              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     193            0 :             ' ERROR: failed to create HDF5 attribute'
     194            0 :          RETURN
     195              :       END IF
     196              : 
     197              :       ! weird in-between pointer needed for variable-length
     198              :       ! string to a scalar dataspace
     199            0 :       in_between_ptr = C_LOC(attr_data)
     200              :       ! the actual pointer to be passed
     201            0 :       buffer = C_LOC(in_between_ptr)
     202              : 
     203              :       ! write the string attribute to file
     204            0 :       CALL h5awrite_f(attr_id, type_id, buffer, error)
     205            0 :       IF (error < 0) THEN
     206              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     207            0 :             ' ERROR: failed to write HDF5 attribute'
     208            0 :          RETURN
     209              :       END IF
     210              : 
     211              :       ! close attribute
     212            0 :       CALL h5aclose_f(attr_id, error)
     213            0 :       IF (error < 0) THEN
     214              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     215            0 :             ' ERROR: failed to close HDF5 attribute'
     216            0 :          RETURN
     217              :       END IF
     218              : 
     219              :       ! close dataspace
     220            0 :       CALL h5sclose_f(space_id, error)
     221            0 :       IF (error < 0) THEN
     222              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     223            0 :             ' ERROR: failed to close HDF5 dataspace'
     224            0 :          RETURN
     225              :       END IF
     226              : 
     227              :       ! close datatype
     228            0 :       CALL h5tclose_f(type_id, error)
     229            0 :       IF (error < 0) THEN
     230              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     231            0 :             ' ERROR: failed to close HDF5 datatype'
     232            0 :          RETURN
     233              :       END IF
     234              : 
     235              :    END SUBROUTINE h5awrite_varlen_string
     236              : 
     237              : ! **************************************************************************************************
     238              : !> \brief Write a fixed-length string attribute
     239              : !> \param loc_id either file id or group id
     240              : !> \param attr_name the name of the attribute
     241              : !> \param attr_data the attribute data, i.e. the string to write
     242              : ! **************************************************************************************************
     243          243 :    SUBROUTINE h5awrite_fixlen_string(loc_id, attr_name, attr_data)
     244              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     245              :       CHARACTER(LEN=*), INTENT(IN)                       :: attr_name
     246              :       CHARACTER(LEN=*), INTENT(IN), TARGET               :: attr_data
     247              : 
     248              :       INTEGER                                            :: error, output_unit
     249              :       INTEGER(KIND=hid_t)                                :: attr_id, space_id, type_id
     250              :       TYPE(c_ptr)                                        :: buffer
     251              : 
     252           27 :       output_unit = cp_logger_get_default_io_unit()
     253              : 
     254              :       ! create a scalar dataspace
     255           27 :       CALL h5screate_f(h5s_scalar_f, space_id, error)
     256           27 :       IF (error < 0) THEN
     257              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     258            0 :             ' ERROR: failed to create HDF5 dataspace'
     259            0 :          RETURN
     260              :       END IF
     261              : 
     262              :       ! create a fixed-length string datatype
     263           27 :       CALL h5tcopy_f(h5t_c_s1, type_id, error)
     264           27 :       CALL h5tset_cset_f(type_id, h5t_cset_utf8_f, error)
     265           27 :       CALL h5tset_size_f(type_id, LEN(attr_data, size_t), error)
     266              : 
     267              :       ! create the attribute
     268           27 :       CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
     269           27 :       IF (error < 0) THEN
     270              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     271            0 :             ' ERROR: failed to create HDF5 attribute'
     272            0 :          RETURN
     273              :       END IF
     274              : 
     275              :       ! the actual pointer to be passed
     276           27 :       buffer = C_LOC(attr_data)
     277              : 
     278              :       ! write the string attribute to file
     279           27 :       CALL h5awrite_f(attr_id, type_id, buffer, error)
     280           27 :       IF (error < 0) THEN
     281              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     282            0 :             ' ERROR: failed to write HDF5 attribute'
     283            0 :          RETURN
     284              :       END IF
     285              : 
     286              :       ! close attribute
     287           27 :       CALL h5aclose_f(attr_id, error)
     288           27 :       IF (error < 0) THEN
     289              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     290            0 :             ' ERROR: failed to close HDF5 attribute'
     291            0 :          RETURN
     292              :       END IF
     293              : 
     294              :       ! close dataspace
     295           27 :       CALL h5sclose_f(space_id, error)
     296           27 :       IF (error < 0) THEN
     297              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     298            0 :             ' ERROR: failed to close HDF5 dataspace'
     299            0 :          RETURN
     300              :       END IF
     301              : 
     302              :       ! close datatype
     303           27 :       CALL h5tclose_f(type_id, error)
     304           27 :       IF (error < 0) THEN
     305              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     306            0 :             ' ERROR: failed to close HDF5 datatype'
     307            0 :          RETURN
     308              :       END IF
     309              : 
     310              :    END SUBROUTINE h5awrite_fixlen_string
     311              : 
     312              : ! **************************************************************************************************
     313              : !> \brief Write a boolean attribute
     314              : !> \param loc_id either file id or group id
     315              : !> \param attr_name the name of the attribute
     316              : !> \param attr_data the attribute data, i.e. the logical to write (.true. or .false.)
     317              : ! **************************************************************************************************
     318           21 :    SUBROUTINE h5awrite_boolean(loc_id, attr_name, attr_data)
     319              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     320              :       CHARACTER(LEN=*), INTENT(IN)                       :: attr_name
     321              :       LOGICAL, INTENT(IN)                                :: attr_data
     322              : 
     323              :       INTEGER                                            :: error, output_unit
     324              :       INTEGER(KIND=hid_t)                                :: attr_id, space_id, type_id
     325              :       INTEGER, TARGET                                    :: attr_data_to_int
     326              :       TYPE(c_ptr)                                        :: buffer
     327              : 
     328            3 :       output_unit = cp_logger_get_default_io_unit()
     329              : 
     330              :       ! 8-bit integers in enum bool_type
     331              : 
     332              :       ! create a scalar dataspace
     333            3 :       CALL h5screate_f(h5s_scalar_f, space_id, error)
     334            3 :       IF (error < 0) THEN
     335              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     336            0 :             ' ERROR: failed to create HDF5 dataspace'
     337            0 :          RETURN
     338              :       END IF
     339              : 
     340              :       ! create the datatype
     341            3 :       CALL h5tcreate_f(h5t_enum_f, INT(1, size_t), type_id, error)
     342            3 :       CALL h5tenum_insert_f(type_id, "FALSE", 0, error)
     343            3 :       CALL h5tenum_insert_f(type_id, "TRUE", 1, error)
     344              : 
     345            3 :       IF (attr_data) THEN
     346            3 :          attr_data_to_int = 1
     347              :       ELSE
     348            0 :          attr_data_to_int = 0
     349              :       END IF
     350              :       ! the C pointer to the actual data
     351            3 :       buffer = C_LOC(attr_data_to_int)
     352              : 
     353              :       ! create the attribute
     354            3 :       CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
     355            3 :       IF (error < 0) THEN
     356              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     357            0 :             ' ERROR: failed to create HDF5 attribute'
     358            0 :          RETURN
     359              :       END IF
     360              : 
     361              :       ! write the string attribute to file
     362            3 :       CALL h5awrite_f(attr_id, type_id, buffer, error)
     363            3 :       IF (error < 0) THEN
     364              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     365            0 :             ' ERROR: failed to write HDF5 attribute'
     366            0 :          RETURN
     367              :       END IF
     368              : 
     369              :       ! close attribute
     370            3 :       CALL h5aclose_f(attr_id, error)
     371            3 :       IF (error < 0) THEN
     372              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     373            0 :             ' ERROR: failed to close HDF5 attribute'
     374            0 :          RETURN
     375              :       END IF
     376              : 
     377              :       ! close dataspace
     378            3 :       CALL h5sclose_f(space_id, error)
     379            3 :       IF (error < 0) THEN
     380              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     381            0 :             ' ERROR: failed to close HDF5 dataspace'
     382            0 :          RETURN
     383              :       END IF
     384              : 
     385              :       ! close datatype
     386            3 :       CALL h5tclose_f(type_id, error)
     387            3 :       IF (error < 0) THEN
     388              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     389            0 :             ' ERROR: failed to close HDF5 datatype'
     390            0 :          RETURN
     391              :       END IF
     392              : 
     393              :    END SUBROUTINE h5awrite_boolean
     394              : 
     395              : ! **************************************************************************************************
     396              : !> \brief Write a (scalar) integer attribute
     397              : !> \param loc_id either file id or group id
     398              : !> \param attr_name the name of the attribute
     399              : !> \param attr_data the attribute data, i.e. the integer to write
     400              : ! **************************************************************************************************
     401          180 :    SUBROUTINE h5awrite_integer_scalar(loc_id, attr_name, attr_data)
     402              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     403              :       CHARACTER(LEN=*), INTENT(IN)                       :: attr_name
     404              :       INTEGER, INTENT(IN), TARGET                        :: attr_data
     405              : 
     406              :       INTEGER                                            :: error, output_unit
     407              :       INTEGER(KIND=hid_t)                                :: attr_id, space_id, type_id
     408              :       TYPE(c_ptr)                                        :: buffer
     409              : 
     410           30 :       output_unit = cp_logger_get_default_io_unit()
     411              : 
     412              :       ! create a scalar dataspace
     413           30 :       CALL h5screate_f(h5s_scalar_f, space_id, error)
     414           30 :       IF (error < 0) THEN
     415              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     416            0 :             ' ERROR: failed to create HDF5 dataspace'
     417            0 :          RETURN
     418              :       END IF
     419              : 
     420              :       ! the C pointer to the actual data
     421           30 :       buffer = C_LOC(attr_data)
     422              : 
     423              :       ! set the type of data
     424           30 :       type_id = h5t_native_integer
     425              : 
     426              :       ! create the attribute
     427           30 :       CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
     428           30 :       IF (error < 0) THEN
     429              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     430            0 :             ' ERROR: failed to create HDF5 attribute'
     431            0 :          RETURN
     432              :       END IF
     433              : 
     434              :       ! write the string attribute to file
     435           30 :       CALL h5awrite_f(attr_id, type_id, buffer, error)
     436           30 :       IF (error < 0) THEN
     437              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     438            0 :             ' ERROR: failed to write HDF5 attribute'
     439            0 :          RETURN
     440              :       END IF
     441              : 
     442              :       ! close attribute
     443           30 :       CALL h5aclose_f(attr_id, error)
     444           30 :       IF (error < 0) THEN
     445              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     446            0 :             ' ERROR: failed to close HDF5 attribute'
     447            0 :          RETURN
     448              :       END IF
     449              : 
     450              :       ! close dataspace
     451           30 :       CALL h5sclose_f(space_id, error)
     452           30 :       IF (error < 0) THEN
     453              :          WRITE (UNIT=output_unit, FMT="(/,T5,A,/)") &
     454            0 :             ' ERROR: failed to close HDF5 dataspace'
     455            0 :          RETURN
     456              :       END IF
     457              : 
     458              :    END SUBROUTINE h5awrite_integer_scalar
     459              : 
     460              : ! **************************************************************************************************
     461              : !> \brief Write a (scalar) double precision attribute
     462              : !> \param loc_id either file id or group id
     463              : !> \param attr_name the name of the attribute
     464              : !> \param attr_data the attribute data, i.e. the double to write
     465              : ! **************************************************************************************************
     466           95 :    SUBROUTINE h5awrite_double_scalar(loc_id, attr_name, attr_data)
     467              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     468              :       CHARACTER(LEN=*), INTENT(IN)                       :: attr_name
     469              :       REAL(KIND=dp), INTENT(IN), TARGET                  :: attr_data
     470              : 
     471              :       INTEGER                                            :: error
     472              :       INTEGER(KIND=hid_t)                                :: attr_id, space_id, type_id
     473              :       TYPE(c_ptr)                                        :: buffer
     474              : 
     475              :       ! create a scalar dataspace
     476           19 :       CALL h5screate_f(h5s_scalar_f, space_id, error)
     477           19 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 dataspace')
     478              : 
     479              :       ! the C pointer to the actual data
     480           19 :       buffer = C_LOC(attr_data)
     481              : 
     482              :       ! set the type of data
     483           19 :       type_id = h5t_native_double
     484              : 
     485              :       ! create the attribute
     486           19 :       CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
     487           19 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 attribute')
     488              : 
     489              :       ! write the string attribute to file
     490           19 :       CALL h5awrite_f(attr_id, type_id, buffer, error)
     491           19 :       IF (error < 0) CPABORT('ERROR: failed to write HDF5 attribute')
     492              : 
     493              :       ! close attribute
     494           19 :       CALL h5aclose_f(attr_id, error)
     495           19 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 attribute')
     496              : 
     497              :       ! close dataspace
     498           19 :       CALL h5sclose_f(space_id, error)
     499           19 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 dataspace')
     500              : 
     501           19 :    END SUBROUTINE h5awrite_double_scalar
     502              : 
     503              : ! **************************************************************************************************
     504              : !> \brief Write an array of fixed-length string attribute
     505              : !> \param loc_id either file id or group id
     506              : !> \param attr_name the name of the attribute
     507              : !> \param attr_data the attribute data, i.e. the array of strings
     508              : ! **************************************************************************************************
     509           21 :    SUBROUTINE h5awrite_string_simple(loc_id, attr_name, attr_data)
     510              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     511              :       CHARACTER(LEN=*), INTENT(IN)                       :: attr_name
     512              :       CHARACTER(LEN=*), DIMENSION(:), INTENT(IN), TARGET :: attr_data
     513              : 
     514              :       INTEGER                                            :: error
     515              :       INTEGER(KIND=hid_t)                                :: attr_id, space_id, type_id
     516              :       INTEGER(KIND=hsize_t), DIMENSION(2)                :: dims
     517              :       TYPE(c_ptr)                                        :: buffer
     518              : 
     519            3 :       dims(1) = LEN(attr_data(1), kind=hsize_t) ! length of a string entry
     520            3 :       dims(2) = SIZE(attr_data, kind=hsize_t)   ! length of array of strings
     521              : 
     522              :       ! create a fixed-length string datatype
     523            3 :       CALL h5tcopy_f(h5t_c_s1, type_id, error)
     524            3 :       CALL h5tset_cset_f(type_id, h5t_cset_utf8_f, error)
     525            3 :       CALL h5tset_size_f(type_id, INT(dims(1), size_t), error)
     526              : 
     527              :       ! create a simple dataspace
     528            3 :       CALL h5screate_simple_f(1, dims(2:2), space_id, error)
     529            3 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 dataspace')
     530              : 
     531              :       ! create the atrtibute
     532            3 :       CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
     533            3 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 attribute')
     534              : 
     535              :       ! the actual pointer to be passed
     536            3 :       buffer = C_LOC(attr_data(1))
     537              : 
     538              :       ! write the string array attribute to file
     539            3 :       CALL h5awrite_f(attr_id, type_id, buffer, error)
     540            3 :       IF (error < 0) CPABORT('ERROR: failed to write HDF5 attribute')
     541              : 
     542              :       ! close attribute
     543            3 :       CALL h5aclose_f(attr_id, error)
     544            3 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 attribute')
     545              : 
     546              :       ! close dataspace
     547            3 :       CALL h5sclose_f(space_id, error)
     548            3 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 dataspace')
     549              : 
     550              :       ! close datatype
     551            3 :       CALL h5tclose_f(type_id, error)
     552            3 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 datatype')
     553              : 
     554            3 :    END SUBROUTINE h5awrite_string_simple
     555              : 
     556              : ! **************************************************************************************************
     557              : !> \brief Write an array of doubles attribute
     558              : !> \param loc_id either file id or group id
     559              : !> \param attr_name the name of the attribute
     560              : !> \param attr_data the attribute data, i.e. the array of doubles
     561              : ! **************************************************************************************************
     562           30 :    SUBROUTINE h5awrite_double_simple(loc_id, attr_name, attr_data)
     563              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     564              :       CHARACTER(LEN=*), INTENT(IN)                       :: attr_name
     565              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN), TARGET    :: attr_data
     566              : 
     567              :       INTEGER                                            :: error
     568              :       INTEGER(KIND=hid_t)                                :: attr_id, space_id, type_id
     569              :       INTEGER(KIND=hsize_t), DIMENSION(1)                :: dims
     570              :       TYPE(c_ptr)                                        :: buffer
     571              : 
     572            6 :       dims(1) = SIZE(attr_data, kind=hsize_t)   ! length of array of strings
     573              : 
     574              :       ! set the type of data
     575            6 :       type_id = h5t_native_double
     576              : 
     577              :       ! create a simple dataspace
     578            6 :       CALL h5screate_simple_f(1, dims, space_id, error)
     579            6 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 dataspace')
     580              : 
     581              :       ! create the atrtibute
     582            6 :       CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
     583            6 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 attribute')
     584              : 
     585              :       ! the actual pointer to be passed
     586            6 :       buffer = C_LOC(attr_data(1))
     587              : 
     588              :       ! write the string array attribute to file
     589            6 :       CALL h5awrite_f(attr_id, type_id, buffer, error)
     590            6 :       IF (error < 0) CPABORT('ERROR: failed to write HDF5 attribute')
     591              : 
     592              :       ! close attribute
     593            6 :       CALL h5aclose_f(attr_id, error)
     594            6 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 attribute')
     595              : 
     596              :       ! close dataspace
     597            6 :       CALL h5sclose_f(space_id, error)
     598            6 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 dataspace')
     599              : 
     600            6 :    END SUBROUTINE h5awrite_double_simple
     601              : 
     602              : ! **************************************************************************************************
     603              : !> \brief Write an array of integers attribute
     604              : !> \param loc_id either file id or group id
     605              : !> \param attr_name the name of the attribute
     606              : !> \param attr_data the attribute data, i.e. the array of integers
     607              : ! **************************************************************************************************
     608           15 :    SUBROUTINE h5awrite_integer_simple(loc_id, attr_name, attr_data)
     609              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     610              :       CHARACTER(LEN=*), INTENT(IN)                       :: attr_name
     611              :       INTEGER, DIMENSION(:), INTENT(IN), TARGET          :: attr_data
     612              : 
     613              :       INTEGER                                            :: error
     614              :       INTEGER(KIND=hid_t)                                :: attr_id, space_id, type_id
     615              :       INTEGER(KIND=hsize_t), DIMENSION(1)                :: dims
     616              :       TYPE(c_ptr)                                        :: buffer
     617              : 
     618            3 :       dims(1) = SIZE(attr_data, kind=hsize_t)   ! length of array of strings
     619              : 
     620              :       ! set the type of data
     621            3 :       type_id = h5t_native_integer
     622              : 
     623              :       ! create a simple dataspace
     624            3 :       CALL h5screate_simple_f(1, dims, space_id, error)
     625            3 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 dataspace')
     626              : 
     627              :       ! create the atrtibute
     628            3 :       CALL h5acreate_f(loc_id, attr_name, type_id, space_id, attr_id, error)
     629            3 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 attribute')
     630              : 
     631              :       ! the actual pointer to be passed
     632            3 :       buffer = C_LOC(attr_data(1))
     633              : 
     634              :       ! write the string array attribute to file
     635            3 :       CALL h5awrite_f(attr_id, type_id, buffer, error)
     636            3 :       IF (error < 0) CPABORT('ERROR: failed to write HDF5 attribute')
     637              : 
     638              :       ! close attribute
     639            3 :       CALL h5aclose_f(attr_id, error)
     640            3 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 attribute')
     641              : 
     642              :       ! close dataspace
     643            3 :       CALL h5sclose_f(space_id, error)
     644            3 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 dataspace')
     645              : 
     646            3 :    END SUBROUTINE h5awrite_integer_simple
     647              : 
     648              : ! **************************************************************************************************
     649              : !> \brief Write a dataset containing an array of doubles
     650              : !> \param loc_id either file id or group id
     651              : !> \param dset_name the name of the dataset
     652              : !> \param dset_data the dataset data, i.e. the array of doubles
     653              : ! **************************************************************************************************
     654           90 :    SUBROUTINE h5dwrite_double_simple(loc_id, dset_name, dset_data)
     655              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     656              :       CHARACTER(LEN=*), INTENT(IN)                       :: dset_name
     657              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN), TARGET    :: dset_data
     658              : 
     659              :       INTEGER                                            :: error
     660              :       INTEGER(KIND=hid_t)                                :: dset_id, space_id, type_id
     661              :       INTEGER(KIND=hsize_t), DIMENSION(1)                :: dims
     662              :       TYPE(c_ptr)                                        :: buffer
     663              : 
     664           18 :       dims(1) = SIZE(dset_data, kind=hsize_t)   ! length of array
     665              : 
     666              :       ! set the type of data
     667           18 :       type_id = h5t_native_double
     668              : 
     669              :       ! create a simple dataspace
     670           18 :       CALL h5screate_simple_f(1, dims, space_id, error)
     671           18 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 dataspace')
     672              : 
     673              :       ! create the dataset
     674           18 :       CALL h5dcreate_f(loc_id, dset_name, type_id, space_id, dset_id, error)
     675           18 :       IF (error < 0) CPABORT('ERROR: failed to create HDF5 dataset')
     676              : 
     677              :       ! the actual pointer to be passed
     678           18 :       buffer = C_LOC(dset_data(1))
     679              : 
     680              :       ! write the string array attribute to file
     681           18 :       CALL h5dwrite_f(dset_id, type_id, buffer, error)
     682           18 :       IF (error < 0) CPABORT('ERROR: failed to write HDF5 dataset')
     683              : 
     684              :       ! close dataset
     685           18 :       CALL h5dclose_f(dset_id, error)
     686           18 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 dataset')
     687              : 
     688              :       ! close dataspace
     689           18 :       CALL h5sclose_f(space_id, error)
     690           18 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 dataspace')
     691              : 
     692           18 :    END SUBROUTINE h5dwrite_double_simple
     693              : 
     694              : ! **************************************************************************************************
     695              : !> \brief Read a dataset containing an array of doubles
     696              : !> \param loc_id either file id or group id
     697              : !> \param dset_name the name of the dataset
     698              : !> \param dset_data where the read dataset data will be written
     699              : ! **************************************************************************************************
     700            0 :    SUBROUTINE h5dread_double_simple(loc_id, dset_name, dset_data)
     701              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     702              :       CHARACTER(LEN=*), INTENT(IN)                       :: dset_name
     703              :       REAL(KIND=dp), DIMENSION(:), INTENT(OUT)           :: dset_data
     704              : 
     705              :       INTEGER                                            :: error
     706              :       INTEGER(KIND=hid_t)                                :: dset_id, npoints, space_id, type_id
     707              :       INTEGER(KIND=hsize_t), DIMENSION(1)                :: dims
     708              : 
     709            0 :       dims(1) = SIZE(dset_data, kind=hsize_t)   ! length of array
     710              : 
     711              :       ! set the type of data
     712            0 :       type_id = h5t_native_double
     713              : 
     714              :       ! open the dataset
     715            0 :       CALL h5dopen_f(loc_id, dset_name, dset_id, error)
     716            0 :       IF (error < 0) CPABORT('ERROR: failed to open HDF5 dataset')
     717              : 
     718              :       ! get information on the dataspace
     719            0 :       CALL h5dget_space_f(dset_id, space_id, error)
     720            0 :       IF (error < 0) CPABORT('ERROR: failed to fetch HDF5 dataspace info')
     721              : 
     722              :       ! get dataspace dims
     723            0 :       CALL h5sget_simple_extent_npoints_f(space_id, npoints, error)
     724            0 :       IF (error < 0) CPABORT('ERROR: failed to fetch HDF5 dataspace dimension')
     725              : 
     726              :       ! read the data
     727            0 :       CALL h5dread_f(dset_id, type_id, dset_data, dims, error)
     728            0 :       IF (error < 0) CPABORT('ERROR: failed to read HDF5 dataset')
     729              : 
     730              :       ! close dataset
     731            0 :       CALL h5dclose_f(dset_id, error)
     732            0 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 dataset')
     733              : 
     734              :       ! close dataspace
     735            0 :       CALL h5sclose_f(space_id, error)
     736            0 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 dataspace')
     737              : 
     738            0 :    END SUBROUTINE h5dread_double_simple
     739              : 
     740              : ! **************************************************************************************************
     741              : !> \brief Read an attribute containing a scalar double
     742              : !> \param loc_id either file id or group id
     743              : !> \param attr_name ...
     744              : !> \param attr_data ...
     745              : ! **************************************************************************************************
     746            0 :    SUBROUTINE h5aread_double_scalar(loc_id, attr_name, attr_data)
     747              :       INTEGER(KIND=hid_t), INTENT(IN)                    :: loc_id
     748              :       CHARACTER(LEN=*), INTENT(IN)                       :: attr_name
     749              :       REAL(KIND=dp), INTENT(OUT), TARGET                 :: attr_data
     750              : 
     751              :       INTEGER                                            :: error
     752              :       INTEGER(KIND=hid_t)                                :: attr_id, type_id
     753              :       TYPE(c_ptr)                                        :: buffer
     754              : 
     755              :       ! set the type of data
     756            0 :       type_id = h5t_native_double
     757              : 
     758              :       ! open the attribute
     759            0 :       CALL h5aopen_f(loc_id, attr_name, attr_id, error)
     760            0 :       IF (error < 0) CPABORT('ERROR: failed to open HDF5 attribute')
     761              : 
     762            0 :       buffer = C_LOC(attr_data)
     763              :       ! read the data
     764            0 :       CALL h5aread_f(attr_id, type_id, buffer, error)
     765            0 :       IF (error < 0) CPABORT('ERROR: failed to read HDF5 attribute')
     766              : 
     767              :       ! close the attribute
     768            0 :       CALL h5aclose_f(attr_id, error)
     769            0 :       IF (error < 0) CPABORT('ERROR: failed to close HDF5 attribute')
     770              : 
     771            0 :    END SUBROUTINE h5aread_double_scalar
     772              : 
     773              : #endif
     774              : 
     775              : END MODULE hdf5_wrapper
        

Generated by: LCOV version 2.0-1