I created two subroutines, one to write a real attribute to a file and the other one to read from it. For some reason I’m unable to read the attribute, hdfaread-f throws an error=-1.
! Subroutine to write real attributes
subroutine write_real(file,data,dname)
implicit none
real(KIND=8), intent(in) :: data
character(*), intent(in) :: dname
type(datafile), intent(in) :: file
integer :: error
integer(HID_T) :: filespace,memspace,dset_id,attr_id,group_id,dspace_id
integer(hsize_t) :: dims(1)
dims(1) = 1
group_id = file%current_group
! Create scalar dataspace
call h5screate_f(H5S_SCALAR_F, dspace_id, error)
! Create the attribute
call h5acreate_f(group_id, dname, H5T_NATIVE_DOUBLE, dspace_id, attr_id, error)
call h5awrite_f(attr_id, H5T_NATIVE_DOUBLE, data, dims, error)
call h5aclose_f(attr_id, error)
call h5sclose_f(dspace_id, error)
end subroutine write_real
! Subroutine to read real attributes
subroutine read_real(file,data,dname)
implicit none
double precision, intent(out) :: data
character(*), intent(in) :: dname
type(datafile), intent(in) :: file
integer :: error
integer(HID_T) :: filespace,memspace,dset_id,attr_id,group_id,dspace_id
integer(hsize_t) :: dims(1)
dims(1) = 1
group_id = file%current_group
call h5aopen_f(group_id, dname, attr_id, error)
call h5aread_f(attr_id, H5T_NATIVE_DOUBLE, data,dims, error)
call h5aclose_f(attr_id, error)
end subroutine read_real
And type datafile is just a user-defined datatype:
type :: datafile
integer(kind=hid_t) :: id
integer(HID_T) :: current_group
end type datafile
Any help or direction will be much appreciated. Thank you very much.