我想从 hdf 文件中读取任意大小的一维数组。我正在处理“读取/写入外部数据集”示例here,但由于我不知道数组维度的先验,我需要调用一些额外的子例程。
可以找到数据空间的维度,因为打印出 m 给出了正确的值,但数据本身无法读取。
我正在尝试读取的测试数组如下所示:
HDF5 "test_1d.h5" {
GROUP "/" {
DATASET "sample_array" {
DATATYPE H5T_IEEE_F64LE
DATASPACE SIMPLE { ( 5 ) / ( 5 ) }
DATA {
(0): 1, 4, 2, 8, 6
}
}
}
}
该程序:
program hdf_read_test
use hdf5
implicit none
integer :: m,hdferror
character(len=1024) :: fname,dsetname
double precision, dimension(:), allocatable :: X
integer(hid_t) :: file_id,dset_id,dspace_id
integer(hsize_t), dimension(1) :: dims,maxdims
! ------------------------------------------
! Specify filename/dataset name.
fname = "test_1d.h5"
dsetname = "sample_array"
call h5fopen_f(fname, H5F_ACC_RDONLY_F, file_id, hdferror)
call h5dopen_f(file_id, dsetname, dset_id, hdferror)
! ---------------
! Figure out the size of the array.
! Get the dataspace ID
call h5dget_space_f(dset_id,dspace_id,hdferror)
! Getting dims from dataspace
call h5sget_simple_extent_dims_f(dspace_id, dims, maxdims, hdferror)
! Allocate memory for the array.
m = dims(1)
allocate(X(m))
! ----------------
! Read the array.
call h5dread_f(dset_id, H5T_IEEE_F64LE, X, dims, hdferror, H5S_ALL_F, dspace_id)
! Check the values.
write(*,*)
write(*,*) "Array size: ",m
write(*,*) "Array elements: ",X
write(*,*)
! -----------------
! Clean up.
deallocate(X)
call h5sclose_f(dspace_id, hdferror)
call h5dclose_f(dset_id, hdferror)
call h5fclose_f(file_id, hdferror)
call h5close_f(hdferror)
end program hdf_read_test
编译通过
h5fc -o hdf_read_test hdf_read_test.f90
产生输出:
HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 140545957812032:
#000: ../../../src/H5Dio.c line 182 in H5Dread(): can't read data
major: Dataset
minor: Read failed
#001: ../../../src/H5Dio.c line 438 in H5D__read(): unable to set up type info
major: Dataset
minor: Unable to initialize object
#002: ../../../src/H5Dio.c line 914 in H5D__typeinfo_init(): not a datatype
major: Invalid arguments to routine
minor: Inappropriate type
HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 140545957812032:
#000: ../../../src/H5T.c line 1761 in H5Tclose(): not a datatype
major: Invalid arguments to routine
minor: Inappropriate type
谢谢。