module rad_kd_utils

  use vtype_module

  implicit none

  private

  public :: InqPFIntGQNum
  public :: RadKDUtilsCalcTendency

contains

  !----------------------------------------------------------------------------

  function InqPFIntGQNum( DelWaveNum ) result( GQNum )


    real(DP), intent(in) :: DelWaveNum

    integer :: GQNum

    real(DP), parameter :: DelWaveNumPerPoint = 10.0d0


    GQNum = int( DelWaveNum / DelWaveNumPerPoint ) + 1


  end function InqPFIntGQNum

  !----------------------------------------------------------------------------

  subroutine RadKDUtilsCalcTendency( &
    & Grav, Cp, &
    & kmax, r_Press, &
    & r_RadUwFlux, r_RadDwFlux, &
    & z_DTempDt &
    & )

    real(DP)    , intent(in ) :: Grav
    real(DP)    , intent(in ) :: Cp
    integer     , intent(in ) :: kmax
    real(DP)    , intent(in ) :: r_Press(0:kmax)
    real(DP)    , intent(in ) :: r_RadUwFlux(0:kmax)
    real(DP)    , intent(in ) :: r_RadDwFlux(0:kmax)
    real(DP)    , intent(out) :: z_DTempDt  (1:kmax)


    ! Local variables
    !
    real(DP) :: r_RadNetFlux(0:kmax)
    integer  :: k


    r_RadNetFlux = r_RadUwFlux - r_RadDwFlux

    do k = 1, kmax
      z_DTempDt(k) = &
        &   ( r_RadNetFlux(k-1) - r_RadNetFlux(k) ) &
        & / ( r_Press     (k-1) - r_Press     (k) ) &
        & * Grav / Cp
    end do


  end subroutine RadKDUtilsCalcTendency

  !----------------------------------------------------------------------------

end module rad_kd_utils
