Error in reading real attributes


#1

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.


#2

I can’t help you w/ Fortran, but an MWE would drastically increase your chances of someone replying. (My apologies, if your post contains an MWE, and I didn’t recognize it.) G.


#3

I don’t see any issues with the code provided. Have you checked to make sure no errors occurred? Can you use h5dump on the file to check it looks correct?

In case you are not aware, examples by API can be found here:

https://support.hdfgroup.org/HDF5/examples/api-fortran.html


#4

Thank you very much for taking the time for responding to the question. I managed to rectify the problem. It was related to the initialization of the hdf environment. I forgot to initialize the environment before using the hdf routines. However, it’s quite strange that h5fopenf didn’t throw any error while opening the file. It should have returned a negative error when the hdf environment is not initialized.
Thanks and Regards,
Umair