【发布时间】:2012-12-02 00:09:27
【问题描述】:
我想在 Fortran 中使用 HDF5 Tables,但遇到了一些麻烦。一个例子会很有用,但只有 C 例子是provided。
我的部分问题是如何处理它需要的偏移量和大小。使用 gfortran,我也许可以使用 sizeof 和 loc,但这些是 gfortran 特定的扩展。我看到较新版本的 Fortran 有 c_loc 和 c_sizeof 这可能会有所帮助,但也许它们只适用于 c 变量?
我也不知道 chunk_size 应该是多少。
FWIW,下面丑陋的代码使用 loc 和 sizeof。它编译并运行但给出错误(并且没有在 hdf5 文件中放入任何内容):
size: 8
offsets: 0 4
types: 0 0
initialized
file open
HDF5-DIAG: Error detected in HDF5 (1.8.8) thread 140636908803840:
#000: ../../../src/H5Tcompound.c line 370 in H5Tinsert(): not a datatype
major: Invalid arguments to routine
minor: Inappropriate type
table created
所以偏移量可能有意义,但类型没有意义。
任何帮助将不胜感激。谢谢。
module tdef
type blarg
integer :: arg
real :: blah
end type
end
PROGRAM H5_TABLE
use tdef
USE HDF5
use h5lt
use h5tb
IMPLICIT NONE
INTEGER(HID_T) :: file_id
INTEGER :: error
integer(HSIZE_T) :: nfields, nrecords
integer(SIZE_T) :: type_size
type(blarg) :: test
character(len=4), dimension(2) :: field_names = (/' arg', 'blah'/)
integer(SIZE_T), dimension(2) :: field_offset
integer(HID_T), dimension(2) :: field_types
integer(HSIZE_T) :: chunk_size = 1
integer :: compress = 0
nfields = 2
nrecords = 2
type_size = sizeof(test)
print *, "size:", type_size
field_offset(1) = loc(test%arg) - loc(test)
field_offset(2) = loc(test%blah) - loc(test)
print *, "offsets:", field_offset
field_types(1) = H5T_NATIVE_INTEGER
field_types(2) = H5T_NATIVE_REAL
print *, "types:", field_types
CALL h5open_f(error)
print *, "initialized"
CALL h5fcreate_f("cpt.h5", H5F_ACC_TRUNC_F, file_id, error)
print *, "file open"
call h5tbmake_table_f('Table Title', file_id, "Steps", nfields, &
nrecords, type_size, field_names, field_offset, &
field_types, chunk_size, compress, error)
print *, "table created"
CALL h5fclose_f(file_id, error)
CALL h5close_f(error)
END PROGRAM H5_TABLE
【问题讨论】:
-
偏移量看起来不错。
C_loc不会帮助你,因为它返回type(c_ptr)并且你不能用它进行指针算术(没有额外的transfer)。我玩过你的程序,但我对 HDF5 的了解不够,无法发现问题。 -
感谢您的浏览。您对 c_loc 的评论有助于我清除 this;现在我明白作者为什么使用转移了。