2012-12-14 138 views
3

我想在Fortran中使用HDF5 Tables,但我遇到了一些麻烦。一个例子会很有用,但只有C例子是provided如何在Fortran中使用HDF5表

我的问题的一部分是如何处理它需要的偏移量和大小。使用gfortran,我可以使用sizeofloc,但这些都是特定的扩展名。我发现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 
+0

补偿看起来不错。 'C_loc'不会帮助你,因为它返回'type(c_ptr)',你不能对它进行指针运算(没有额外的'transfer')。我玩过你的程序,但我不知道HDF5足以发现问题。 –

+0

感谢您的关注。您对c_loc的评论有助于为我清除[this](http://coding.derkeiler.com/Archive/Fortran/comp.lang.fortran/2009-01/msg00104.html);现在我明白为什么作者使用转移。 – lnmaurer

回答

3

事实证明,这是一个非常简单的问题。

我在使用H5T_NATIVE_INTEGER和H5T_NATIVE_REAL之后调用h5open_f。但是,直到调用h5open_f之后,这两者才会被分配适当的值。首先调用h5open_f解决了我的问题。