module leap_frog
  use CalcParameter
  implicit none
contains
  subroutine leap_frog_2D_filter( &
       & Gdata, Gdata_b1, Gdata_b2, Gdata_dt, &
       & grd_x, grd_z, step)
    real(8), intent(inout) :: Gdata(-1:,0:),Gdata_b1(-1:,0:),Gdata_b2(-1:,0:)
    real(8), intent(in)    :: Gdata_dt(:,:)
    ! Runge Kutta Gill のステップ数(step=1,2,3,4)
    integer, intent(in)    :: grd_x, grd_z, step
    ! Runge Kutta Gill の変数
    !    real(8) :: Gdata_b2(grd_x,grd_z)
    integer :: i, j
    if ( step == 1 ) then 
       do j = 1, grd_z
          do i=1, grd_x
             Gdata_b2(i,j) = Gdata(i,j)
             Gdata(i,j) = Gdata(i,j)+Gdata_dt(i,j)*dt
          end do
       end do
    else
       do j = 1, grd_z
          do i=1, grd_x
             Gdata_b1(i,j) = Gdata(i,j)
             Gdata(i,j) = Gdata_b2(i,j)+Gdata_dt(i,j)*dble(2)*dt
             Gdata_b2(i,j) = Gdata_b1(i,j) &
                  &     + TfilCoef*(Gdata(i,j) &
                  &     - 2.0D0*Gdata_b1(i,j) + Gdata_b2(i,j))
          end do
       end do
    end if
  end subroutine leap_frog_2D_filter
  
  subroutine leap_frog_2D(Gdata, Gdata_b1, Gdata_dt, grd_x, grd_z, step)
    real(8), intent(inout) :: Gdata(-1:,0:),Gdata_b1(-1:,0:)
    real(8), intent(in)    :: Gdata_dt(:,:)
    ! Runge Kutta Gill のステップ数(step=1,2,3,4)
    integer, intent(in)    :: grd_x, grd_z, step
    ! Runge Kutta Gill の変数
    real(8) :: Gdata_b2(grd_x,grd_z)
!    real(8),parameter :: TfilCoef = 0.6d+0
    integer :: i, j
    if ( mod(step,5) == 1 ) then 
       do j = 1, grd_z
          do i=1, grd_x
             Gdata_b1(i,j) = Gdata(i,j)
             Gdata(i,j) = Gdata(i,j)+Gdata_dt(i,j)*dt
          end do
       end do
    else
       do j = 1, grd_z
          do i=1, grd_x
             Gdata_b2(i,j) = Gdata(i,j)
             Gdata(i,j) = Gdata_b1(i,j)+Gdata_dt(i,j)*dble(2)*dt
             Gdata_b1(i,j) =Gdata_b2(i,j)
          end do
       end do
    end if
  end subroutine leap_frog_2D

end module LEAP_FROG
