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
    real(DP) :: DelWaveNumPerPoint = 10.0d0

    DelWaveNumPerPoint = 1.0d0 / ( 2.897d-3 / 300.0 ) / 1.0d4

    write( 6, * ) DelWaveNumPerPoint

    GQNum = int( DelWaveNum / DelWaveNumPerPoint ) + 1

!!$    GQNum = min( GQNum, 1000 )
!!$    GQNum = min( GQNum, 10000 )


  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
