I've attached a modified version of h5ex_t_vlstring_F03.f90 to use attributes 
instead. This example assumes you have a F2003 compiler and you used 
--enable-fortran2003 when you built hdf5. Hope this helps.

Scot


________________________________________
From: Hdf-forum <[email protected]> on behalf of Pierre de 
Buyl <[email protected]>
Sent: Wednesday, February 05, 2014 8:59 AM
To: [email protected]
Subject: [Hdf-forum] VL string attribute in Fortran

Dear hdf community,

I would like to know whether the absence of a "Read / Write Variable Length
String Datatype (Attribute)" is due to a technical problem. The example page [1]
does not provide such an example.

In the context of the H5MD file format [2], we are using (currently) VL string
attributes. If it turns out that we cannot handle those in Fortran, we could
switch to fixed-length strings.

Regards,

Pierre de Buyl

[1] http://www.hdfgroup.org/HDF5/examples/api18-fortran.html
[2] H5MD : HDF5 for molecular data http://nongnu.org/h5md/



_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
!************************************************************
!
!  This example shows how to read and write variable-length
!  string attributes to a dataset using h5aread_f and
!  h5awrite_f, and F2003 intrinsics C_LOC and C_F_POINTER.  
!  The program first writes variable-length strings attribute
!  Next, it reopens the file, reads back the data, and outputs it to 
!  the screen.
!
!  This file is intended for use with HDF5 Library version 1.8
!  and --enable-fortran2003
!
!************************************************************

PROGRAM main

  USE HDF5
  USE ISO_C_BINDING
  
  IMPLICIT NONE

  CHARACTER(LEN=20), PARAMETER :: filename = "h5ex_vlstring_F03.h5"
  CHARACTER(LEN=3) , PARAMETER :: dataset  = "DS1"
  CHARACTER(LEN=2) , PARAMETER :: attribute = "A1"

  INTEGER(HSIZE_T), PARAMETER :: dim0 = 4
  INTEGER(HID_T)  :: file, filetype, space, attr ! Handles
  INTEGER :: hdferr
  INTEGER(HSIZE_T), DIMENSION(1:1) :: dims = (/dim0/)
  
  TYPE(C_PTR), DIMENSION(1:dim0), TARGET :: wdata
  CHARACTER(len=8, KIND=c_char), DIMENSION(1), TARGET  :: A = "Parting"//C_NULL_CHAR
  CHARACTER(len=8, KIND=c_char), DIMENSION(1), TARGET  :: B = "is_such"//C_NULL_CHAR
  CHARACTER(len=6, KIND=c_char), DIMENSION(1), TARGET  :: C = "sweet"//C_NULL_CHAR
  CHARACTER(len=8, KIND=c_char), DIMENSION(1), TARGET  :: D = "sorrow."//C_NULL_CHAR
  TYPE(C_PTR), DIMENSION(:), ALLOCATABLE, TARGET :: rdata ! Read buffer
  CHARACTER(len = 8, kind=c_char),  POINTER :: data ! A pointer to a Fortran string
  TYPE(C_PTR) :: f_ptr
  INTEGER :: i, len

  ! Initialize array of C pointers
  wdata(1) = C_LOC(A(1))     
  wdata(2) = C_LOC(B(1))     
  wdata(3) = C_LOC(C(1))     
  wdata(4) = C_LOC(D(1))     
  !
  ! Initialize FORTRAN interface.
  !
  CALL h5open_f(hdferr)
  !
  ! Create a new file using the default properties.
  !
  CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file, hdferr)
  !
  ! Create file and memory datatypes.  For this example we will save
  ! the strings as C variable length strings, H5T_STRING is defined
  ! as a variable length string.
  !
  CALL H5Tcopy_f(H5T_STRING, filetype, hdferr)
  !
  ! Create dataspace.
  !
  CALL h5screate_simple_f(1, dims, space, hdferr)
  !
  ! Create the attribute and write the variable-length string data to
  ! it.
  !

  CALL h5acreate_f(file, attribute, filetype, space, attr, hdferr)	

  f_ptr = C_LOC(wdata(1))
  CALL h5awrite_f(attr, filetype, f_ptr, hdferr )
  !
  ! Close and release resources.
  !
  CALL h5aclose_f(attr , hdferr)
  CALL h5sclose_f(space, hdferr)
  CALL H5Tclose_f(filetype, hdferr)
  CALL h5fclose_f(file, hdferr)
  !
  ! Now we begin the read section of this example.
  !
  !
  ! Open file and attribute.
  !
  CALL h5fopen_f(filename, H5F_ACC_RDONLY_F, file, hdferr)
  CALL h5aopen_f(file, attribute, attr, hdferr)

  ALLOCATE(rdata(1:dims(1)))
  !
  ! Read attribute.
  !
  f_ptr = C_LOC(rdata(1))
  CALL h5aread_f(attr, H5T_STRING, f_ptr, hdferr)
  !
  ! Output  to the screen.
  !
  DO i = 1, dims(1)
     CALL C_F_POINTER(rdata(i), data)
     len = 0
     DO
        IF(DATA(len+1:len+1).EQ.C_NULL_CHAR.OR.len.GE.8) EXIT
        len = len + 1
     ENDDO
     WRITE(*,'(A,"(",I0,"): ",A)') attribute, i, data(1:len)
  END DO

  DEALLOCATE(rdata)
  
  CALL h5aclose_f(attr , hdferr)
  CALL h5fclose_f(file , hdferr)

END PROGRAM main
_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org

Reply via email to