Reading an array of unknown length from an HDF file in Fortran

I want to read an array of dimension one of arbitrary size from an hdf file. I'm working off of the "Read / Write to External Dataset" example here, but since I don't know the array dimensions a priori, I need to call a few extra subroutines.

The dimension of the dataspace can be found, since printing out m gives the correct value, but the data itself can't be read.

The test array I'm trying to read looks like:

HDF5 "test_1d.h5" {
GROUP "/" {
   DATASET "sample_array" {
      DATATYPE  H5T_IEEE_F64LE
      DATASPACE  SIMPLE { ( 5 ) / ( 5 ) }
      DATA {
      (0): 1, 4, 2, 8, 6
      }
   }
}
}

The program:

program hdf_read_test

use hdf5
implicit none

     integer                                      :: m,hdferror
     character(len=1024)                          :: fname,dsetname
     double precision, dimension(:), allocatable  :: X

     integer(hid_t)                               :: file_id,dset_id,dspace_id
     integer(hsize_t), dimension(1)               :: dims,maxdims

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

     ! Specify filename/dataset name.
     fname = "test_1d.h5"
     dsetname = "sample_array"

     call h5fopen_f(fname, H5F_ACC_RDONLY_F, file_id, hdferror)
     call h5dopen_f(file_id, dsetname, dset_id, hdferror)

     ! ---------------
     ! Figure out the size of the array.

     ! Get the dataspace ID
     call h5dget_space_f(dset_id,dspace_id,hdferror)

     ! Getting dims from dataspace
     call h5sget_simple_extent_dims_f(dspace_id, dims, maxdims, hdferror)   

     ! Allocate memory for the array.
     m = dims(1)
     allocate(X(m))

     ! ----------------
     ! Read the array.
     call h5dread_f(dset_id, H5T_IEEE_F64LE, X, dims, hdferror, H5S_ALL_F, dspace_id)

     ! Check the values.
     write(*,*)
     write(*,*) "Array size: ",m
     write(*,*) "Array elements: ",X
     write(*,*)

     ! -----------------
     ! Clean up.
     deallocate(X)

     call h5sclose_f(dspace_id, hdferror)
     call h5dclose_f(dset_id, hdferror)
     call h5fclose_f(file_id, hdferror)
     call h5close_f(hdferror)

end program hdf_read_test

Compiled via

h5fc -o hdf_read_test hdf_read_test.f90

produces the output:

HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 140545957812032:
  #000: ../../../src/H5Dio.c line 182 in H5Dread(): can't read data
    major: Dataset
    minor: Read failed
  #001: ../../../src/H5Dio.c line 438 in H5D__read(): unable to set up type info
    major: Dataset
    minor: Unable to initialize object
  #002: ../../../src/H5Dio.c line 914 in H5D__typeinfo_init(): not a datatype
    major: Invalid arguments to routine
    minor: Inappropriate type
HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 140545957812032:
  #000: ../../../src/H5T.c line 1761 in H5Tclose(): not a datatype
    major: Invalid arguments to routine
    minor: Inappropriate type

Thanks.

Answers


First off, your error is that with the fortran API you need call h5open_f(hdferror) to intialize the HDF library, before calling h5fopen_f.

Side notes.

  • I would be checking the value of hdferror after every call to a HDF.
  • The call h5sget_simple_extent_dims_f() returns the rank in the last argument (hdferror) or -1 on failure.

Need Your Help

CFileImageLoader(LPCTSTR lpszFileName);

c++ windows mfc

I have a class which expects a LPCTSTR.

Is there a way to cache Spring <mvc:resources> files in memory?

java spring caching resources static-files

Suppose I have bound all urls to Spring dispatcher servlet, and set up some .css and .js directories with &lt;mvc:resources&gt; in mvc Spring namespace.