[HDF5 Table with Fortran API]

Dear all,

I would like to set up a simple HDF5 table using Fortran API. Please see below the code that I have written so far. Unfortunately, I’m not sure if it is appropriate to calculate the typeSize with sizeof(species(1)). If possible, would you also check my fieldOffset calculation. At the moment the code crashes with the following error message:

HDF5-DIAG: Error detected in HDF5 (1.10.5) thread 0:
#000: ../../src/H5Tcompound.c line 350 in H5Tinsert(): not a datatype
major: Invalid arguments to routine
minor: Inappropriate type

Minimal working example:

  PROGRAM H5_TABLE

  USE H5TB

  IMPLICIT NONE

  ! Specie properties
  type specie
    real    :: amass  ! mass
    real    :: vvv    ! interaction length
    real    :: chge   ! charge
    integer :: lfrzn  ! frozen property
  end type specie

  type(specie), allocatable :: species(:)

  integer(hsize_t), parameter :: nFields = 4, nRecords = 3

  character(len=64) :: fileName, tableName, dataSetName, fieldNames(nFields)
  integer(hid_t)    :: fileID, fieldTypes(nFields)
  integer(hsize_t)  :: chunkSize
  integer(size_t)   :: typeSize, fieldOffset(nFields)

  integer :: compress, info

  allocate(species(nRecords), stat=info)

  fileName    = "data.h5"
  tableName   = "Species"
  dataSetName = tableName

  typeSize = sizeof(species(1))

  fieldNames = ["Mass    ","I-length","Charge  ","Frozen  "]

  print *, "size(amass):", sizeof(species(1)%amass)
  print *, "size(vvv):",   sizeof(species(1)%vvv)
  print *, "size(chge):",  sizeof(species(1)%chge)
  print *, "size(lfrzn):", sizeof(species(1)%lfrzn)

  fieldOffset(1) = 0
  fieldOffset(2) = fieldOffset(1) + sizeof(species(1)%amass)
  fieldOffset(3) = fieldOffset(2) + sizeof(species(1)%vvv)
  fieldOffset(4) = fieldOffset(3) + sizeof(species(1)%chge)

  print *, "offset(amass):", fieldOffset(1)
  print *, "offset(vvv):",   fieldOffset(2)
  print *, "offset(chge):",  fieldOffset(3)
  print *, "offset(lfrzn):", fieldOffset(4)

  fieldTypes = [H5T_NATIVE_REAL, H5T_NATIVE_REAL, &
                H5T_NATIVE_REAL, H5T_STD_I32BE]

  chunkSize = 4
  compress  = 1

  ! Initialise Fortran interface
  call H5open_f(info)

  ! Create new file (with default properties)
  call H5Fcreate_f(fileName, H5F_ACC_TRUNC_F, fileID, info)

!-----------------------------------------------------------------------

  call H5TBmake_table_f(tableName, fileID, dataSetName, nFields,  &
         nRecords, typeSize, fieldNames, fieldOffset, fieldTypes, &
         chunkSize, compress, info)

!-----------------------------------------------------------------------

  ! Close file
  call h5fclose_f(fileID, info)

  ! Close Fortran interface
  call h5close_f(info)

  deallocate(species, stat=info)

  END PROGRAM H5_TABLE

! @eof h5_table.f90