diff --git a/src/HDF5_utilities.f90 b/src/HDF5_utilities.f90 index c04694265..2a05f101c 100644 --- a/src/HDF5_utilities.f90 +++ b/src/HDF5_utilities.f90 @@ -82,7 +82,8 @@ module HDF5_utilities HDF5_addGroup, & HDF5_read, & HDF5_write, & - HDF5_setLink + HDF5_setLink, & + HDF5_objectExists contains subroutine HDF5_utilities_init @@ -241,18 +242,46 @@ end function HDF5_openGroup !-------------------------------------------------------------------------------------------------- !> @brief close a group !-------------------------------------------------------------------------------------------------- -subroutine HDF5_closeGroup(ID) +subroutine HDF5_closeGroup(group_id) implicit none - integer(HID_T), intent(in) :: ID + integer(HID_T), intent(in) :: group_id integer(HDF5_ERR_TYPE) :: hdferr - call h5gclose_f(ID, hdferr) - if (hdferr < 0) call IO_error(1_pInt,ext_msg = 'HDF5_closeGroup: h5gclose_f (el is ID)', el = int(ID,pInt)) + call h5gclose_f(group_id, hdferr) + if (hdferr < 0) call IO_error(1_pInt,ext_msg = 'HDF5_closeGroup: h5gclose_f (el is ID)', el = int(group_id,pInt)) end subroutine HDF5_closeGroup +!-------------------------------------------------------------------------------------------------- +!> @brief check whether a group or a dataset exists +!-------------------------------------------------------------------------------------------------- +logical function HDF5_objectExists(loc_id,path) + + implicit none + integer(HID_T), intent(in) :: loc_id + character(len=*), intent(in), optional :: path + integer(HDF5_ERR_TYPE) :: hdferr + character(len=256) :: p + + if (present(path)) then + p = trim(path) + else + p = '.' + endif + + call h5lexists_f(loc_id, p, HDF5_objectExists, hdferr) + if (hdferr < 0) call IO_error(1_pInt,ext_msg = 'HDF5_objectExists: h5oexists_by_name_f') + + if(HDF5_objectExists) then + call h5oexists_by_name_f(loc_id, p, HDF5_objectExists, hdferr) + if (hdferr < 0) call IO_error(1_pInt,ext_msg = 'HDF5_objectExists: h5oexists_by_name_f') + endif + +end function HDF5_objectExists + + !-------------------------------------------------------------------------------------------------- !> @brief adds a string attribute to the path given relative to the location !-------------------------------------------------------------------------------------------------- diff --git a/src/results.f90 b/src/results.f90 index 718a5dbd9..d38178993 100644 --- a/src/results.f90 +++ b/src/results.f90 @@ -162,7 +162,8 @@ subroutine results_writeVectorDataset(group,dataset,label,SIunit) groupHandle = results_openGroup(group) call HDF5_write(groupHandle,dataset,label) - call HDF5_addAttribute(groupHandle,'Unit',SIunit,label) + if (HDF5_objectExists(groupHandle,label)) & + call HDF5_addAttribute(groupHandle,'Unit',SIunit,label) call HDF5_closeGroup(groupHandle) end subroutine results_writeVectorDataset