!=======================================
! 2D cumulus model - kaminari
!  - subroutine boundary
!
! Author    : TAKAHASHI Koko
! Date      : 2003/12/04 新規作成
! Note      : 境界条件と境界より外の配列の値
!             上下境界: すべりなし条件
!             側面境界: 周期境界条件
!
!=======================================
subroutine boundary(im,km,                  &
     &              u,w,pi,ptemp)

  implicit none

  integer(8), intent(in) :: im, km

  real(8), intent(inout) :: u(-2:im+2,-2:km+2)
  real(8), intent(inout) :: w(-2:im+2,-2:km+3)

  real(8), intent(inout) :: pi(-2:im+2,-2:km+2)
  real(8), intent(inout) :: ptemp(-2:im+2,-2:km+2)

  ! 上下境界
  !---  k = 0 : 下部境界
  w(0:im,0) = 0.0d0

  !---  k = km : 上部境界
  w(0:im,km+1) = 0.0d0

  ! 境界計算用の配列
  !---  k = -1 : 下部境界
  u(0:im,-1) = u(0:im,0)
  w(0:im,-1) = w(0:im,0)
  pi(0:im,-1) = pi(0:im,0)
  ptemp(0:im,-1) = ptemp(0:im,0)

  !---  k = -2 : 下部境界
  u(0:im,-2) = u(0:im,0)
  w(0:im,-2) = w(0:im,0)
  pi(0:im,-2) = pi(0:im,0)
  ptemp(0:im,-2) = ptemp(0:im,0)

  !---  k = km+1 : 上部境界
  u(0:im,km+1) = u(0:im,km)
  w(0:im,km+2) = w(0:im,km+1)
  pi(0:im,km+1) = pi(0:im,km)
  ptemp(0:im,km+1) = ptemp(0:im,km)

  !---  k = km+2 : 上部境界
  u(0:im,km+2) = u(0:im,km)
  w(0:im,km+3) = w(0:im,km+1)
  pi(0:im,km+2) = pi(0:im,km)
  ptemp(0:im,km+2) = ptemp(0:im,km)

  ! 境界計算用の配列
  !---  i = im+1 : 右端側面境界
  u(im+1,-2:km+2) = u(0,-2:km+2)
  w(im+1,-2:km+3) = w(0,-2:km+3)
  pi(im+1,-2:km+2) = pi(0,-2:km+2)
  ptemp(im+1,-2:km+2) = ptemp(0,-2:km+2)

  !---  i = im+2 : 右端側面境界
  u(im+2,-2:km+2) = u(1,-2:km+2)
  w(im+2,-2:km+3) = w(1,-2:km+3)
  pi(im+2,-2:km+2) = pi(1,-2:km+2)
  ptemp(im+2,-2:km+2) = ptemp(1,-2:km+2)

  !---  i = -1 : 左端側面境界
  u(-1,-2:km+2) = u(im,-2:km+2)
  w(-1,-2:km+3) = w(im,-2:km+3)
  pi(-1,-2:km+2) = pi(im,-2:km+2)
  ptemp(-1,-2:km+2) = ptemp(im,-2:km+2)

  !---  i = -2 : 左端側面境界
  u(-2,-2:km+2) = u(im-1,-2:km+2)
  w(-2,-2:km+3) = w(im-1,-2:km+3)
  pi(-2,-2:km+2) = pi(im-1,-2:km+2)
  ptemp(-2,-2:km+2) = ptemp(im-1,-2:km+2)

end subroutine boundary