subroutine h5write(nvars, nstanzas, drows, dcols, ep_data, ms_data, ep_diet)

use statevartypesEcopath_mod
use iso_c_binding
use hdf5

implicit none

! This is the name of the data file we will read. 
character (len = 9), parameter :: filename  = "ep_res.h5"
character (len = 7), parameter :: dsetname0 = "ep_data"     ! name of the Ecopath base dataset
character (len = 7), parameter :: dsetname1 = "ms_data"     ! name of the Ecopath multistanza dataset

! variables inherited from Ecopath model
integer(8), intent(in)                 :: nvars, nstanzas, drows, dcols
type(ecopath_data), intent(in), target :: ep_data
type(multi_stanza), intent(in), target :: ms_data(nstanzas)
real(4), intent(in), target            :: ep_diet(drows, dcols)

! in-subroutine variable declarations for ep_data
integer(8), parameter             :: ep_dim0   = 52         ! dimension of ep_data
integer                           :: hdferr                 ! variable to handle errors
integer(hid_t)                    :: file_id, plist_id      ! file identifier of the output file
integer(hid_t)                    :: dset_id                ! dataset identifier
integer(hid_t)                    :: dtype_id, dt1_id       ! dataset type identifier
integer(4)                        :: dspace_id              ! dataspace identifier
integer(hid_t)                    :: memtype
integer(8)                        :: type_size, offset
integer(8)                        :: type_sizei, type_sizer
integer(hsize_t), DIMENSION(1)    :: ep_dims = (/ep_dim0/)
type(c_ptr)                       :: f_ptr

   ! initialize FORTRAN interface
   call h5open_f(hdferr)

   call h5pcreate_f(H5P_DATASET_XFER_F, plist_id, hdferr)
   call h5pset_preserve_f(plist_id, .TRUE., hdferr)

   ! create file, if it already exists overwrite (H%F_ACC_TRUNC_F)
   call h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, hdferr)
  
   ! get size of each member in the compound datatype
   call h5tget_size_f(H5T_NATIVE_INTEGER, type_sizei, hdferr)
   call h5tget_size_f(H5T_NATIVE_REAL, type_sizer, hdferr)
   type_size = (19 * type_sizer) + (5 *  type_sizei)

   ! create the compound datatype for memory
   call h5tcreate_f(H5T_COMPOUND_F, type_size, memtype, hdferr)

   ! insert members
   offset = 0
   call h5tinsert_f(memtype, "biomass", offset, H5T_NATIVE_REAL, hdferr)
   
   offset = offset + type_sizer
   call h5tinsert_f(memtype, "pob", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "qob", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "ee", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "poq", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "unass_q", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "detritus_import", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "immig", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "emig", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "emig_rate", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "ba", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "ba_rate", offset, H5T_NATIVE_REAL, hdferr) 

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "landings", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizei
   call h5tinsert_f(memtype, "org_type", offset, H5T_NATIVE_INTEGER, hdferr)

   offset = offset + type_sizei
   call h5tinsert_f(memtype, "stanza", offset, H5T_NATIVE_INTEGER, hdferr)

   offset = offset + type_sizei
   call h5tinsert_f(memtype, "stanza_no", offset, H5T_NATIVE_INTEGER, hdferr)

   offset = offset + type_sizei
   call h5tinsert_f(memtype, "age_start", offset, H5T_NATIVE_INTEGER, hdferr)

   offset = offset + type_sizei
   call h5tinsert_f(memtype, "leading", offset, H5T_NATIVE_INTEGER, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "production", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "consumption", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "respiration", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "assimilation", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "eatenof", offset, H5T_NATIVE_REAL, hdferr)

   offset = offset + type_sizer
   call h5tinsert_f(memtype, "eatenby", offset, H5T_NATIVE_REAL, hdferr)


   call h5screate_simple_f(1, ep_dims, dspace_id, hdferr)
   call h5dcreate_f(file_id, dsetname0, memtype, dspace_id, dset_id, hdferr)
   
   f_ptr = C_LOC(ep_data)
   call h5dwrite_f(dset_id, memtype, f_ptr, hdferr)
   

end subroutine h5write

