如何使用Fortran API将字符串数组写入HDF5数据集?

时间:2012-12-11 13:23:23

标签: fortran hdf5

使用fortran语言定义字符串数组:

character(len = 20), dimension(5) :: strings = (/"hello", "world", "Hello", "HDF5", "fortran"/)

现在我想使用HDF5 API参考文档所示的h5ltmake_dataset_string_f()接口将此数组保存到数据集中

  

Fortran90界面: h5ltmake_dataset_string_f

     

子程序h5ltmake_dataset_string_f(loc_id,dset_name,buf,errcode)

    implicit none
     

整数(HID_T),意图(IN):: loc_id !文件或组标识符

     

字符(LEN = *),意图(IN):: dset_name !数据集的名称

     

字符(LEN = *),intent(IN),dimension(:) :: buf!数据缓冲区

     

integer :: errcode !错误代码

     

结束子程序h5ltmake_dataset_string_f

我的调用代码

h5ltmake_dataset_string_f(group_id, dset_name, strings, error)
在子程序调用之前已经定义了

和group_id,dset_name和错误。

但是当我编译和构建这个代码时,发生编译错误说

  

h5ltmake_dataset_string_f():违反了实际参数和伪参数!

那么如何将字符串写入一个数据集?

2 个答案:

答案 0 :(得分:0)

使用API​​ h5dwrite_f(...),而不是h5ltmake_dataset_f()来编写字符串数组。

答案 1 :(得分:0)

虽然这个问题几年前就已经开启了,但是这里有一个解决方案,可以将一个字符串数组(“hello”,“world”,“Hello”,“HDF5”,“fortran”)写入Fortran中的HDF5数据集HDFql(http://www.hdfql.com)。发布这个以防其他人在执行此类操作时发现自己在处理HDF5低级别细节时:

PROGRAM Example

    ! use HDFql module (make sure it can be found by the Fortran compiler)
    USE HDFql

    ! declare variables
    CHARACTER(LEN = 20), DIMENSION(5) :: strings = [CHARACTER(LEN = 20) :: "hello", "world", "Hello", "HDF5", "fortran"]
    CHARACTER :: variable_number
    INTEGER :: state

    ! create an HDF file named "example.h5" and use (i.e. open) it
    state = hdfql_execute("CREATE FILE example.h5")
    state = hdfql_execute("USE FILE example.h5")

    ! create a dataset named "my_dataset" of type char (size 20) of one dimension (size 5)
    state = hdfql_execute("CREATE DATASET my_dataset AS CHAR(5, 20)");

    ! register variable "strings" for subsequent use (by HDFql)
    state = hdfql_variable_register(strings)
    WRITE(variable_number, "(I0)") state

    ! insert (i.e. write) content of variable "strings" into dataset "my_dataset"
    state = hdfql_execute("INSERT INTO my_dataset VALUES FROM MEMORY " // variable_number)

    ! unregister variable "strings" as it is no longer used/needed (by HDFql)
    state = hdfql_variable_unregister(strings)

END PROGRAM