2012-12-14 140 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解決了我的問題。