1
votes

A string array is defined using fortran language:

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

Now I want to save this array into a dataset using h5ltmake_dataset_string_f() interface illustrated by HDF5 API reference documentation

Fortran90 Interface: h5ltmake_dataset_string_f

subroutine h5ltmake_dataset_string_f(loc_id,dset_name,buf,errcode )

    implicit none

integer(HID_T), intent(IN) :: loc_id ! file or group identifier

character(LEN=*), intent(IN) :: dset_name ! name of the dataset

character(LEN=*), intent(IN), dimension(:) :: buf ! data buffer

integer :: errcode ! error code

end subroutine h5ltmake_dataset_string_f

My calling code below

h5ltmake_dataset_string_f(group_id, dset_name, strings, error)

and group_id, dset_name and error have been defined before subroutine call.

But when I compile and build this code, compile error occurs says

h5ltmake_dataset_string_f(): the actual arguments and dummy arguments have been violated!

So how to write the strings into one dataset?

2

2 Answers

0
votes

Use API h5dwrite_f(...), not h5ltmake_dataset_f() to write string array.

0
votes

Although the issue was opened several years ago, here goes a solution to write an array of strings ("hello", "world", "Hello", "HDF5", "fortran") into an HDF5 dataset in Fortran using HDFql (http://www.hdfql.com). Posting this in case others find themselves struggling with HDF5 low-level details when performing this type of operation:

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