!$gpum horizontal klon
MODULE alpale_wk_mod
  PRIVATE

  LOGICAL, SAVE                                              :: first = .TRUE.
  !$OMP THREADPRIVATE(first)
  REAL, ALLOCATABLE, SAVE, DIMENSION(:)                      :: cellrad
  !$OMP THREADPRIVATE(cellrad)

  PUBLIC alpale_wk, alpale_wk_first

  CONTAINS

SUBROUTINE alpale_wk_first(cell_area)

  USE dimphy, ONLY: klon
  USE yomcst_mod_h, ONLY: rpi

  IMPLICIT NONE
  REAL, DIMENSION(klon), INTENT(IN)     :: cell_area

  IF (first) THEN
    ALLOCATE (cellrad(klon))
  !  Compute pseudo grid-cell radius cellrad, such that pi*cellrad^2=cell_area
    print *,'alpale_wk: cell_area(1) ',cell_area(1)
    cellrad(:)=sqrt(cell_area(:)/rpi)
    first = .FALSE.
  ENDIF

END SUBROUTINE alpale_wk_first

SUBROUTINE alpale_wk ( dtime, cell_area, zoccur, sigmaw, wdens, fip ,  &
                       fip_cond)

! **************************************************************
!                                                              *
! ALPALE_WK                                                    *
!                                                              *
!                                                              *
! written by   : Jean-Yves Grandpeix, 07/08/2017               *
! modified by :                                                *
! **************************************************************

  USE dimphy, ONLY: klon
  USE ioipsl_getin_p_mod, ONLY : getin_p
  USE print_control_mod, ONLY: mydebug=>debug , lunout, prt_level
  USE yomcst_mod_h, ONLY: rpi

  IMPLICIT NONE

!================================================================
! Auteur(s)   : Jean-Yves Grandpeix, 07/08/2017
! Objet : Contribution of the wake scheme to Ale and Alp
!================================================================

! Input arguments
!----------------
  REAL, INTENT(IN)                                           :: dtime
  REAL, DIMENSION(klon),    INTENT(IN)                       :: cell_area
  INTEGER, DIMENSION(klon), INTENT (IN)                      :: zoccur
  REAL, DIMENSION(klon),    INTENT(IN)                       :: sigmaw
  REAL, DIMENSION(klon),    INTENT(IN)                       :: wdens
  REAL, DIMENSION(klon),    INTENT(IN)                       :: fip
! Output arguments
!-----------------
  REAL, DIMENSION(klon), INTENT(OUT)                         :: fip_cond


! Local variables
!----------------
  INTEGER                                                    :: i
  REAL, DIMENSION(klon)                                      :: wkrad
  REAL, DIMENSION(klon)                                      :: proba_gf

!  Compute wake radius
!!  print *,'alpale_wk: sigmaw(1), wdens(1) ', sigmaw(1), wdens(1)
  DO i = 1,klon
    IF (zoccur(i) .GE. 1) THEN
      wkrad(i) = sqrt(sigmaw(i)/(rpi*wdens(i)))
    ELSE
      wkrad(i) = 0.
    ENDIF ! (zoccur(i) .GE. 1)
  ENDDO

!  Compute probability that the grid-cell is intersected by a gust front
!!  print *,'alpale_wk: wkrad(1), cellrad(1) ', wkrad(1), cellrad(1)
!!  proba_gf(:) = exp(-wdens(:)*rpi*max(wkrad(:)-cellrad(:),0.)**2) - &   ! Formules
!!                exp(-wdens(:)*rpi*(wkrad(:)+cellrad(:))**2)             ! fausses !
  proba_gf(:) = 1. - exp(-wdens(:)*rpi*((wkrad(:)+cellrad(:))**2 - &
                                        max(wkrad(:)-cellrad(:),0.)**2) )
!
  proba_gf(:) = max(proba_gf(:),1.e-3)
!  Compute Fip conditionned on the presence of some gust front within the 
!  grid-cell
!!  print *,'alpale_wk: proba_gf(1), fip(1), ', proba_gf(1), fip(1)
  fip_cond(:) = fip(:)/proba_gf(:)
!!    print *,'alpale_wk: wkrad(1), cellrad(1), proba_gf(1), fip(1), fip_cond(1) ', &
!!                        wkrad(1), cellrad(1), proba_gf(1), fip(1), fip_cond(1)

   RETURN
   END SUBROUTINE alpale_wk

END MODULE alpale_wk_mod
