【问题标题】:How to use HDF5 tables in Fortran如何在 Fortran 中使用 HDF5 表
【发布时间】:2012-12-02 00:09:27
【问题描述】:

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

我的部分问题是如何处理它需要的偏移量和大小。使用 gfortran,我也许可以使用 sizeofloc,但这些是 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;现在我明白作者为什么使用转移了。

标签: fortran hdf5


【解决方案1】:

原来这是一个非常简单的问题。

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

【讨论】:

    猜你喜欢
    • 2013-12-01
    • 2018-02-21
    • 2020-01-29
    • 2021-06-04
    • 2018-05-21
    • 2016-05-20
    • 2019-11-15
    • 2018-10-22
    • 2013-02-26
    相关资源
    最近更新 更多