!c Description:
!c  断熱温度減率の計算
!c
!c Current Code Owner: 
!c   sugiyama@gfd-dennou.org
!c
!c Copyright (C) SUGIYAMA Ko-ichiro, 2003, All rights reserved

module interface_lapserate
  interface

     subroutine lapserate(temp, pres, grav, molwt, dtdz)
       real(8), intent(in)    :: temp(:)
       real(8), intent(in)    :: pres(:)
       real(8), intent(in)    :: grav
       real(8), intent(in)    :: molwt(:)
       real(8), intent(out)   :: dtdz(:)
     end subroutine lapserate
     
  end interface
end module interface_lapserate



subroutine lapserate(temp, pres, grav, molwt, dtdz)
  use prm_phys, only: gasr

  implicit none
  real(8), intent(in)    :: temp(:)
  real(8), intent(in)    :: pres(:)
  real(8), intent(in)    :: grav
  real(8), intent(in)    :: molwt(:)
  real(8), intent(out)   :: dtdz(:)
  real(8), allocatable   :: dt(:)
  real(8), allocatable   :: dp(:)
  integer                :: step
  integer                :: num
  
  
  !配列のサイズの取得
  step = size(temp, 1)
  
  !配列の定義
  allocate(dt(step), dp(step))
  
  !温度差の計算
  do num = 1, step - 1
     dt(num) = temp(num + 1) - temp(num)
  end do
  dt(step) = dt(step - 1)
  
  !圧力差の計算
  do num = 1, step - 1
     dp(num) = pres(num + 1) - pres(num)
  end do
  dp(step) = dp(step - 1)
  
  !断熱減率の計算
  dtdz = - dt * pres * grav * molwt / ( dp * gasr * temp )
  
  !配列の解放
  deallocate(dt, dp)
  
end subroutine lapserate
