!
! Example of creating a dataset which uses the Fletcher32 checksum
! algorithm for error detection. 

     PROGRAM DSETCKSUM

     USE HDF5 ! This module contains all necessary modules 
        
     IMPLICIT NONE

     CHARACTER(LEN=10), PARAMETER :: filename = "dcksumf.h5"
     CHARACTER(LEN=4), PARAMETER :: dsetname = "dset-checksum"

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

     INTEGER     ::   error ! Error flag
     INTEGER     ::  i, j

     INTEGER, DIMENSION(4,6) :: dset_data, data_out ! Data buffers
     INTEGER(HSIZE_T), DIMENSION(1)  :: data_dims
     INTEGER(HSIZE_T), DIMENSION(2) :: chunk_dims = (/2,3/)
     INTEGER :: rank_chunk = 2
     INTEGER :: edc
     INTEGER, DIMENSION(1) :: cd_values (0) 


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

     INTEGER     ::   error ! Error flag

     ! Initialize FORTRAN predefined datatypes.
     !
     CALL h5open_f (error)

     ! Create a new file 
     ! 
     CALL h5fcreate_f (filename, H5F_ACC_TRUNC_F, file_id, error, &
                       H5P_DEFAULT_F, H5P_DEFAULT_F)

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

     ! Set Dataset Creation Property List to create chunked dataset
     ! and to use checksum filter.
     !
     CALL h5pcreate_f (H5P_DATASET_CREATE_F, dcpl, error) 
     CALL h5pset_chunk_f (dcpl, rank_chunk, chunk_dims, error)
     CALL h5pset_filter_f (dcpl, H5Z_FILTER_FLETCHER32_F, 0, 0, cd_values, error)

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

     ! Initialize the dset_data array.
     !
     do i = 1, 4
          do j = 1, 6
               dset_data(i,j) = (i-1)*6 + j;
          end do
     end do

     ! Write the dataset.
     !
     data_dims(1) = 4
     data_dims(2) = 6
     CALL h5dwrite_f(dset_id, H5T_NATIVE_INTEGER, dset_data, data_dims, error)

     ! End access to the dataset, dataspace, property list, and file.
     ! 
     CALL h5pclose_f (dcpl, error)
     CALL h5dclose_f (dset_id, error)
     CALL h5sclose_f (dspace_id, error)
     CALL h5fclose_f (file_id, error)

     ! Reopen file
    
     CALL h5fopen_f (filename, H5F_ACC_RDWR_F, file_id, error)
     CALL h5dopen_f(file_id, dsetname, dset_id, error)

     CALL h5pcreate_f (H5P_DATASET_XFER_F, dxpl, error)
     CALL h5pget_edc_check_f (dxpl, edc, error) 
     if (edc .eq. 1)  print *, "Checksum is enabled." 
     if (edc .eq. 0)  print *, "Checksum is disabled."
    
     CALL h5dread_f (dset_id, H5T_NATIVE_INTEGER, data_out, data_dims, &
                      error, H5P_DEFAULT_F, H5P_DEFAULT_F, dxpl)
 
     CALL h5pclose_f (dxpl, error)
     CALL h5dclose_f (dset_id, error)
     CALL h5fclose_f (file_id, error)

     ! Close FORTRAN predefined datatypes.
     !
     CALL h5close_f (error)

     END PROGRAM DSETCKSUM
     
 
