program H5PartTest
  implicit none

#ifdef PARALLEL_IO
  include 'mpif.h'
#endif
  include 'H5PartF.h'

#ifdef PARALLEL_IO
  integer :: comm, ierr, i, rank
  integer*8 :: file_id, status, npoints
#endif
  real*8, allocatable :: x(:)

  rank = 0
#ifdef PARALLEL_IO
  call MPI_INIT(ierr)
  comm = MPI_COMM_WORLD
  call MPI_COMM_RANK(comm,rank,ierr)
#endif

  ! this enables level 3 ("info") messages to be
  ! printed by the H5Part library
  ! (3_8 is the literal for an integer*8 with value 3)
  status = h5pt_set_verbosity_level (9_8)

  ! open the a file called 'test.h5' in parallel for writing
#ifdef PARALLEL_IO
  file_id = h5pt_openw_par ('test.h5', comm)
#else
  file_id = h5pt_openw ('test.h5')
#endif

  ! in the Fortran API, steps start at 1
  status = h5pt_setstep (file_id, 1_8)

  ! set the size of the data array
  select case (rank) 
     case(0) 
        npoints = 1
     case (1)
        npoints = 0
     case (2)
        npoints = 0
     case default
        npoints = 0
     end select

     status = h5pt_setnpoints (file_id, npoints)
     if (status /= 0) then 
        write (*, '(a, a, i0, a)') __FILE__, ':', __LINE__, ": cannot set number of points"
        stop 2
     endif

    ! create data
  allocate(  x(npoints) )  
  do i=1, npoints
     x(i)  = 0.0 + real(i)
  enddo
  ! write the data

  status = h5pt_writedata_r8 (file_id, "x", x)
  if (status /= 0) then 
     write (*, '(a, a, i0, a)') __FILE__, ':', __LINE__, ": cannot write data set"
     stop 2
  endif

  ! close the file
  status = h5pt_close (file_id)

#ifdef PARALLEL_IO
  call MPI_FINALIZE(ierr)
#endif

end program H5PartTest

