在Fortran中从HDF文件读取长度未知的数组
我想从hdf文件中读取任意大小的一维数组。我正在处理“读取/写入外部数据集”的示例,但由于我事先不知道数组的维数,因此需要调用一些额外的子例程 可以找到数据空间的维度,因为打印出的m给出了正确的值,但数据本身无法读取 我试图读取的测试数组如下所示:在Fortran中从HDF文件读取长度未知的数组,fortran,gfortran,hdf5,hdf,Fortran,Gfortran,Hdf5,Hdf,我想从hdf文件中读取任意大小的一维数组。我正在处理“读取/写入外部数据集”的示例,但由于我事先不知道数组的维数,因此需要调用一些额外的子例程 可以找到数据空间的维度,因为打印出的m给出了正确的值,但数据本身无法读取 我试图读取的测试数组如下所示: HDF5 "test_1d.h5" { GROUP "/" { DATASET "sample_array" { DATATYPE H5T_IEEE_F64LE DATASPACE SIMPLE { ( 5 ) /
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
谢谢。首先,您的错误是,在调用
h5fopen\u f
之前,使用fortran API需要调用h5open\u f(hdferor)
初始化HDF库
旁注
- 每次调用HDF后,我都会检查
的值hdferor
- 调用
在失败时返回最后一个参数(h5sget\u simple\u extent\u dims\u f()
)中的秩或-1hdferor
h5fopen\u f
之前,您需要调用h5open\u f(hdferor)
初始化HDF库
旁注
- 每次调用HDF后,我都会检查
的值hdferor
- 调用
在失败时返回最后一个参数(h5sget\u simple\u extent\u dims\u f()
)中的秩或-1hdferor
h5open\u f
来初始化库。这就是问题所在。我的眼睛在捉弄我,将h5open_f和h5fopen_f结合起来。我没有看到调用h5open_f
来初始化库。这就是问题所在。我的眼睛在捉弄我,结合了H5开放式和H5开放式。就是这样,谢谢。我确实检查了这个值,我只是删除了它们作为一个简单的例子。就是这样,谢谢。我确实检查了这个值,我只是删除了它们,作为一个简单的例子。