module rad_rte_two_stream_app_wrapper

  use dc_types

  implicit none

  private

  public :: RadRTETwoStreamAppLWWrapper
  public :: RadRTETwoStreamAppLWWrapperInit


  character(128), save :: ModuleName = 'rad_rte_two_stream_app_wrapper'

  logical, save :: FlagInited = .false.

contains

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

  subroutine RadRTETwoStreamAppLWWrapper(      &
    & z_SSA, z_AF,                             & ! (in)
    & r_OptDep,                                & ! (in)
    & SurfAlbedo,                              & ! (in)
    & r_PFInted, SurfPFInted, SurfDPFDTInted,  & ! (in)
    & r_RadUwFlux, r_RadDwFlux,                & ! (out)
    & ra_DelRadUwFlux, ra_DelRadDwFlux         & ! (out)
    & )

    ! USE statements
    !
    use gridset, only : imax, jmax, kmax

    use rad_rte_two_stream_app, only : RadRTETwoStreamAppLW


    real(DP), intent(in ) :: z_SSA          (1:kmax)
    real(DP), intent(in ) :: z_AF           (1:kmax)
    real(DP), intent(in ) :: r_OptDep       (0:kmax)
    real(DP), intent(in ) :: SurfAlbedo
    real(DP), intent(in ) :: r_PFInted      (0:kmax)
    real(DP), intent(in ) :: SurfPFInted
    real(DP), intent(in ) :: SurfDPFDTInted
    real(DP), intent(out) :: r_RadUwFlux    (0:kmax)
    real(DP), intent(out) :: r_RadDwFlux    (0:kmax)
    real(DP), intent(out) :: ra_DelRadUwFlux(0:kmax, 0:1)
    real(DP), intent(out) :: ra_DelRadDwFlux(0:kmax, 0:1)


    ! Local variables
    !
    real(DP) :: xyz_SSA          (0:imax-1, 1:jmax, 1:kmax)
    real(DP) :: xyz_AF           (0:imax-1, 1:jmax, 1:kmax)
    real(DP) :: xyr_OptDep       (0:imax-1, 1:jmax, 0:kmax)
    real(DP) :: xy_SurfAlbedo    (0:imax-1, 1:jmax)
    real(DP) :: xyr_PFInted      (0:imax-1, 1:jmax, 0:kmax)
    real(DP) :: xy_SurfPFInted   (0:imax-1, 1:jmax)
    real(DP) :: xy_SurfDPFDTInted(0:imax-1, 1:jmax)
    real(DP) :: xyr_RadUwFlux    (0:imax-1, 1:jmax, 0:kmax)
    real(DP) :: xyr_RadDwFlux    (0:imax-1, 1:jmax, 0:kmax)
    real(DP) :: xyra_DelRadUwFlux(0:imax-1, 1:jmax, 0:kmax, 0:1)
    real(DP) :: xyra_DelRadDwFlux(0:imax-1, 1:jmax, 0:kmax, 0:1)

    integer :: k


    if ( .not. FlagInited ) then
      write( 6, * ) trim( ModuleName ), " is not initialized."
      stop
    end if


    do k = 1, kmax
      xyz_SSA    (:,:,k) = z_SSA    (k)
      xyz_AF     (:,:,k) = z_AF     (k)
    end do
    do k = 0, kmax
      xyr_OptDep (:,:,k) = r_OptDep (k)
      xyr_PFInted(:,:,k) = r_PFInted(k)
    end do
    xy_SurfAlbedo     = SurfAlbedo
    xy_SurfPFInted    = SurfPFInted
    xy_SurfDPFDTInted = SurfDPFDTInted

    call RadRTETwoStreamAppLW(                     &
      & xyz_SSA, xyz_AF,                                 & ! (in)
      & xyr_OptDep,                                      & ! (in)
      & xy_SurfAlbedo,                                   & ! (in)
      & xyr_PFInted, xy_SurfPFInted, xy_SurfDPFDTInted,  & ! (in)
      & xyr_RadUwFlux, xyr_RadDwFlux,                    & ! (out)
      & xyra_DelRadUwFlux, xyra_DelRadDwFlux             & ! (out)
      & )

    r_RadUwFlux     = xyr_RadUwFlux    (0,1,:)
    r_RadDwFlux     = xyr_RadDwFlux    (0,1,:)
    ra_DelRadUwFlux = xyra_DelRadUwFlux(0,1,:,:)
    ra_DelRadDwFlux = xyra_DelRadDwFlux(0,1,:,:)


  end subroutine RadRTETwoStreamAppLWWrapper

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

  subroutine RadRTETwoStreamAppLWWrapperInit

    use rad_rte_two_stream_app, only : RadRTETwoStreamAppInit


    if ( FlagInited ) return


    call RadRTETwoStreamAppInit


    FlagInited = .true.

  end subroutine RadRTETwoStreamAppLWWrapperInit

end module rad_rte_two_stream_app_wrapper

