!===============================================================================
!
! ildg_io_lemon.F90
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2010 Yoshifumi Nakamura
!
! This file is part of BQCD -- Berlin Quantum ChromoDynamics program
!
! BQCD is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! BQCD is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with BQCD.  If not, see <http://www.gnu.org/licenses/>.
!
!-------------------------------------------------------------------------------
# include "defs.h"

!-------------------------------------------------------------------------------
subroutine ildg_io_lemon(action, u, precision)
  use module_function_decl
  use module_lattice_io
  use module_vol
  implicit none
  include 'mpif.h'
  character(*), intent(in)        :: action
  GAUGE_FIELD_ILDG, intent(inout) :: u
  integer, intent(in)             :: precision

  integer :: x, y, z, t, ix, iy, iz, it
  integer :: coord(DIM), ierror, my, gmy, my_tmp
  INT8    :: words, words_in_u

  !                                                   8 is size of real [bytes]
  words     = SIZE_COMPLEX * NCOL * NCOL * DIM * NX * 8
  if (precision == 32) words = words / 2

  words_in_u= SIZE_COMPLEX * NCOL * NCOL * DIM * vol

  call pe2coord(my_pe(), coord)
  my=my_pe()

  if (action == "write") then
     if (precision == 64)call swap_endian8(words_in_u, u)
     if (precision == 32)call swap_endian4(words_in_u, u)
     call ildg_write_lemon(u, l, vol, precision)
  else
     call ildg_read_lemon(u, l, vol, precision)
  endif

  !!
  !! now calculate crc cksum
  !!
  do t = 0, LT - 1
     do z = 0, LZ - 1
        do y = 0, LY - 1
           do x = 0, LX - 1, NX
              it = t - nt*coord(4)
              iz = z - nz*coord(3)
              iy = y - ny*coord(2)
              ix = x - nx*coord(1)
              if ( 0<=it .and. it < nt .and. &
                   0<=iz .and. iz < nz .and. &
                   0<=iy .and. iy < ny .and. &
                   0<=ix .and. ix < nx) then
                 call cksum_add(u(1,1,1,ix,iy,iz,it),words)
                 my_tmp=my
              else
                 my_tmp=0
              endif
              call mpi_allreduce(my_tmp, gmy, 1, &
                   MPI_INTEGER4, MPI_SUM, MPI_COMM_WORLD, ierror)
              call cksum_bcast(gmy)  
              call mpi_barrier(MPI_COMM_WORLD, ierror)
           enddo
        enddo
     enddo
  enddo

  if (action == "read") then
     if (precision == 64)call swap_endian8(words_in_u, u)
     if (precision == 32)call swap_endian4(words_in_u, u)
  endif
  call mpi_barrier(MPI_COMM_WORLD, ierror)

end

!===============================================================================
