从 Fortran 中的 HDF 文件中读取未知长度的数组
Reading an array of unknown length from an HDF file in Fortran
我想从 hdf 文件中读取任意大小的一维数组。我正在处理 "Read / Write to External Dataset" 示例 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
谢谢。
首先,您的错误是使用 Fortran API,您 需要 调用 h5open_f(hdferror)
来初始化 HDF 库,然后再调用 h5fopen_f
.
旁注。
- 我会在每次调用 HDF 后检查
hdferror
的值。
- 调用
h5sget_simple_extent_dims_f()
returns 最后一个参数中的等级 (hdferror
) 或 -1 失败。
我想从 hdf 文件中读取任意大小的一维数组。我正在处理 "Read / Write to External Dataset" 示例 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
谢谢。
首先,您的错误是使用 Fortran API,您 需要 调用 h5open_f(hdferror)
来初始化 HDF 库,然后再调用 h5fopen_f
.
旁注。
- 我会在每次调用 HDF 后检查
hdferror
的值。 - 调用
h5sget_simple_extent_dims_f()
returns 最后一个参数中的等级 (hdferror
) 或 -1 失败。