          PROGRAM CHUNK
          USE HDF5 ! This module contains all necessary modules 

          IMPLICIT NONE

          !
          !the dataset is stored in file "chunkf.h5"
          !
          CHARACTER(LEN=9), PARAMETER :: filename = "chunkf.h5"
          !CHARACTER(LEN=80) :: fix_filename

          !
          !dataset name is "ExtendibleArray"
          !
          CHARACTER(LEN=15), PARAMETER :: dsetname = "ExtendibleArray"

          !
          !dataset rank is 2
          !
          INTEGER :: RANK = 2

          INTEGER(HID_T) :: file_id       ! File identifier 
          INTEGER(HID_T) :: dset_id       ! Dataset identifier 
          INTEGER(HID_T) :: dataspace     ! Dataspace identifier 
          INTEGER(HID_T) :: memspace      ! memory Dataspace identifier 
          INTEGER(HID_T) :: crp_list        ! dataset creatation property identifier 

          !
          !dataset dimensions at creation time
          !
          INTEGER(HSIZE_T), DIMENSION(2) :: dims = (/3,3/)

          !
          !data dimensions 
          !
          INTEGER(HSIZE_T), DIMENSION(2) :: dims1 = (/10,3/)

          !
          !Maximum dimensions
          !
          INTEGER(HSIZE_T), DIMENSION(2) :: maxdims 

          !
          !data arrays for reading and writing 
          !
          INTEGER, DIMENSION(10,3) :: data_in, data_out

          !
          !Size of data in the file 
          !
          INTEGER(HSIZE_T), DIMENSION(2) :: size

          !
          !general purpose integer 
          !
          INTEGER :: i, j

          !
          !flag to check operation success 
          !
          INTEGER :: error 

          !
          !Variables used in reading data back
          !  
          INTEGER(HSIZE_T), DIMENSION(2) :: dimsr, maxdimsr
          INTEGER :: rankr
          INTEGER(HSIZE_T), DIMENSION(2) :: data_dims

          !
          !data initialization 
          !
          do i = 1, 10
             do j = 1, 3
                data_in(i,j) = 2
             end do
          end do

          !
          !Initialize FORTRAN predifined datatypes
          !
          CALL h5open_f(error) 

          !
          !Create a new file using default properties.
          ! 
          !CALL h5_fixname_f(filename, fix_filename, H5P_DEFAULT_F, error)
          !if (error .ne. 0) then
          !    write(*,*) "Cannot modify filename"
          !    stop
          !endif
          CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error)


          !
          !Create the data space with unlimited dimensions.
          !
          maxdims = (/H5S_UNLIMITED_F, H5S_UNLIMITED_F/)

          CALL h5screate_simple_f(RANK, dims, dataspace, error, maxdims)

          !
          !Modify dataset creation properties, i.e. enable chunking
          !
          CALL h5pcreate_f(H5P_DATASET_CREATE_F, crp_list, error)

          CALL h5pset_chunk_f(crp_list, RANK, dims1, error)

          !
          !Create a dataset with 3X3 dimensions using cparms creation propertie .
          !
          CALL h5dcreate_f(file_id, dsetname, H5T_NATIVE_INTEGER, dataspace, &
                           dset_id, error, crp_list )

          !
          !Extend the dataset. This call assures that dataset is 3 x 3.
          !
          size(1) = 3
          size(2) = 3
          CALL h5dextend_f(dset_id, size, error)


          !
          !Extend the dataset. Dataset becomes 10 x 3.
          !
          size(1)   = 10;  
          size(2)   = 3; 
          CALL h5dextend_f(dset_id, size, error)

          !
          !Write the data of size 10X3 to the extended dataset.
          !
          data_dims(1) = 10
          data_dims(2) = 3
          CALL H5dwrite_f(dset_id, H5T_NATIVE_INTEGER, data_in, data_dims, error)

          !
          !Close the dataspace for the dataset.
          !
          CALL h5sclose_f(dataspace, error)

          !
          !Close the property list.
          !
          CALL h5pclose_f(crp_list, error)
          !
          !Close the dataset.
          !
          CALL h5dclose_f(dset_id, error)

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

          !
          !read the data back
          !
          !Open the file.
          !
          CALL h5fopen_f (filename, H5F_ACC_RDONLY_F, file_id, error)

          !
          !Open the  dataset.
          !
          CALL h5dopen_f(file_id, dsetname, dset_id, error)

          !
          !Get dataset's dataspace handle.
          !
          CALL h5dget_space_f(dset_id, dataspace, error)

          !
          !Get dataspace's rank.
          !
          CALL h5sget_simple_extent_ndims_f(dataspace, rankr, error)
          IF (rankr .NE. RANK) then
              write(*,*) "dataset rank error occured"
              stop
          END IF

          !
          !Get dataspace's dimensinons.
          !
          CALL h5sget_simple_extent_dims_f(dataspace, dimsr, maxdimsr, error)
          IF ((dimsr(1) .NE. dims1(1)) .OR. (dimsr(2) .NE. dims1(2))) THEN
              write(*,*) "dataset dimensions error occured"
              stop
          END IF

          !
          !Get creation property list.
          !
          CALL h5dget_create_plist_f(dset_id, crp_list, error)


          !
          !create memory dataspace.
          !
          CALL h5screate_simple_f(rankr, dimsr, memspace, error)

          !
          !Read data 
          !
          CALL H5dread_f(dset_id, H5T_NATIVE_INTEGER, data_out, data_dims, error, &
                    memspace, dataspace)


          !
          !Compare the data.
          ! 
          do i = 1, dims1(1)
              do j = 1, dims1(2)
                  IF (data_out(i,j) .NE. data_in(i, j)) THEN 
                      write(*, *) "extend dataset test error occured"
                      write(*, *) "read value is not the same as the written values"
                  END IF
              end do    
          end do

          !
          !Close the dataspace for the dataset.
          !
          CALL h5sclose_f(dataspace, error)

          !
          !Close the memspace for the dataset.
          !
          CALL h5sclose_f(memspace, error)

          !
          !Close the property list.
          !
          CALL h5pclose_f(crp_list, error)

          !
          !Close the dataset.
          !
          CALL h5dclose_f(dset_id, error)

          !
          !Close the file.
          !
          CALL h5fclose_f(file_id, error)
 
          !Close FORTRAN predefined datatypes
          !
          CALL h5close_f(error)

          END PROGRAM CHUNK 



