Creating dataset with Fortran


Hello everyone,

I’m a beginner with HDF5 and I’m having trouble creating my first dataset with Fortran. I’m using the code below from Learning the Basics:


USE HDF5 ! This module contains all necessary modules


CHARACTER(LEN=8), PARAMETER :: filename = “dsetf.h5” ! File name
CHARACTER(LEN=4), PARAMETER :: dsetname = “dset” ! Dataset name

INTEGER(HID_T) :: file_id ! File identifier
INTEGER(HID_T) :: dset_id ! Dataset identifier
INTEGER(HID_T) :: dspace_id ! Dataspace identifier

INTEGER(HSIZE_T), DIMENSION(2) :: dims = (/4,6/) ! Dataset dimensions
INTEGER :: rank = 2 ! Dataset rank

INTEGER :: error ! Error flag

! Initialize FORTRAN interface.
CALL h5open_f(error)

! Create a new file using default properties.
CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error)

! Create the dataspace.
CALL h5screate_simple_f(rank, dims, dspace_id, error)

! Create the dataset with default properties.
CALL h5dcreate_f(file_id, dsetname, H5T_NATIVE_INTEGER, dspace_id, &
dset_id, error)

! End access to the dataset and release resources used by it.
CALL h5dclose_f(dset_id, error)

! Terminate access to the data space.
CALL h5sclose_f(dspace_id, error)

! Close the file.
CALL h5fclose_f(file_id, error)

! Close FORTRAN interface.
CALL h5close_f(error)


I can create a file, but when I try to create the dataset I get the error below:

HDF5-DIAG: Error detected in HDF5 (1.8.22) thread 0:
#000: C:\autotest\hdf518-StdRelease-dist-10vs16\build\hdfsrc\src\H5D.c line 150 in H5Dcreate2(): not a datatype ID
major: Invalid arguments to routine
minor: Inappropriate type
HDF5-DIAG: Error detected in HDF5 (1.8.22) thread 0:
#000: C:\autotest\hdf518-StdRelease-dist-10vs16\build\hdfsrc\src\H5D.c line 385 in H5Dclose(): not a dataset
major: Invalid arguments to routine
minor: Inappropriate type

Could someone give me a clue?

Thank you :slight_smile:



Try to remove the & when passing variable dset_id to function h5dcreate_f and see if the issue goes away.

That said, you may want to try HDFql as it greatly simplifies how HDF5 files are handled in Fortran (as well as in C, C++, Java, C#, Python and R). As an example, the creation of a dataset in Fortran could be done as follows:


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

    ! declare variable
    INTEGER :: state

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

    ! create a dataset named "dset" of data type int of two dimensions (size 4x6)
    state = hdfql_execute("CREATE DATASET dset AS INT(4, 6)");


Hope it helps!


I don’t see anything wrong with the program, so maybe it is an incompatibility issue with your HDF5 install on windows. You might want to add error checks to see if APIs succeed and the file_id and dspace_id look reasonable. Can you print H5T_NATIVE_INTEGER?


Thank you, but removing the & didn’t fix the problem…
I’ll take a look at HDFql :slight_smile:


Thanks for the reply! Using the code below I get 0 printed on the console:

USE HDF5! This module contains all necessary modules

Some details of my environment:

  • HDF 1.8.22
  • Microsoft Visual Studio 2019
  • Intel Fortran Compiler 2021 beta
  • Some configurations:
  • Poject > Properties > Fortran > General > Additional Include Directories: C:\Program Files\HDF_Group\HDF5\1.8.22\include
  • Poject > Properties > Linker > General > Additional Library Directories: C:\Program Files\HDF_Group\HDF5\1.8.22\lib
  • Poject > Properties > Linker > Input: hdf5.lib hdf5_tools.lib libhdf5_hl_cpp.lib hdf5_cpp.lib libaec.lib libhdf5_hl_f90cstub.lib hdf5_f90cstub.lib libhdf5.lib libhdf5lib_hldhd. lib hdf5_hl_cpp.lib libhdf5_f90cstub.lib libzlib.lib hdf5_hl_f90cstub.lib libhdf5_fortran.lib hdf5_hl_fortran.lib libhdf5_hl.lib

Thank you very much :slight_smile:


Did you compile your own HDF5 1.8.22? The HDFGroup’s binaries were compiled with Ifort 2019. Maybe this could be the problem.


You didn’t initialize (i.e. open) the interface before printing the type (you didn’t open it at all in this test program, in fact).


Another easy to use object-oriented or functional Fortran HDF5 interface is h5fortran

use h5fortran

call h5write("/x", x) 
call h5read("/y", y)

works for scalar through 7D for real32,real64,int32,int64 and character scalars.


Thanks for the answer! I checked the Release Notes, and as I’m using HDFGroup’s binaries + Ifort 2021, this could really be a problem. However I could not find free older versions of Intel Fortran to download …


Thanks for the tip! :slight_smile:


As there seems to be an issue with h5open_f not setting the parameters from C correctly, I would suggest compiling HDF5 from source with ifort 2021 instead of using the binaries.