!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer, 
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms 
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer 
!version 3 or (at your option) any later version that should be found at: 
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html 
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software; 
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec), 
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
!   #################################################################

    SUBROUTINE SURFACE_CD(PRI, PZREF, PUREF, PZ0EFF, PZ0H,   &,4
                             PCD, PCDN)
!   #################################################################
!
!!****  *SURFACE_CD*  
!!
!!    PURPOSE
!!    -------
!
!     Computes the drag coefficients for momentum near the ground
!         
!     
!!**  METHOD
!!    ------
!
!
!
!    1 and 2 : computation of relative humidity near the ground
!
!    3 : richardson number
!
!    4 : the aerodynamical resistance for heat transfers is deduced
!
!    5 : the drag coefficient for momentum ZCD is computed
!
!
!!    EXTERNAL
!!    --------
!!
!!
!!    IMPLICIT ARGUMENTS
!!    ------------------
!!
!!    MODD_CST
!!    MODD_GROUND_PAR
!!
!!      
!!    REFERENCE
!!    ---------
!!
!!      
!!    AUTHOR
!!    ------
!!
!!	V. Masson           * Meteo-France *
!!
!!    MODIFICATIONS
!!    -------------
!!      Original    20/01/98 
!!                  02/04/01 (P Jabouille) limitation of Z0 with 0.5 PUREF
!-------------------------------------------------------------------------------
!
!*       0.     DECLARATIONS
!               ------------
!
USE MODD_CSTS,ONLY : XKARMAN
!
USE MODE_THERMOS
!
IMPLICIT NONE
!
!*      0.1    declarations of arguments
!
!
REAL, DIMENSION(:), INTENT(IN)    :: PRI      ! Richardson number
REAL, DIMENSION(:), INTENT(IN)    :: PZREF    ! reference height of the first
                                              ! atmospheric level
REAL, DIMENSION(:), INTENT(IN)    :: PUREF    ! reference height of the wind
!                                             ! NOTE this is different from ZZREF
!                                             ! ONLY in stand-alone/forced mode,
!                                             ! NOT when coupled to a model (MesoNH)
REAL, DIMENSION(:), INTENT(IN)    :: PZ0EFF   ! roughness length for momentum
                                              ! with subgrid-scale orography
REAL, DIMENSION(:), INTENT(IN)    :: PZ0H     ! roughness length for heat
!
REAL, DIMENSION(:), INTENT(OUT)   :: PCD      ! drag coefficient for momentum
REAL, DIMENSION(:), INTENT(OUT)   :: PCDN     ! neutral drag coefficient for momentum
!
!*      0.2    declarations of local variables
!
!
REAL, DIMENSION(SIZE(PRI)) :: ZZ0EFF, ZZ0H, ZMU,     &
                              ZCMSTAR, ZPM, ZCM, ZFM
!-------------------------------------------------------------------------------
!
!*       1.     Drag coefficient for momentum transfers
!               ---------------------------------------
!

!
ZZ0EFF(:) = MIN(PZ0EFF(:),PUREF(:)*0.5)
ZZ0H  (:) = MIN(ZZ0EFF(:),PZ0H(:))
!
ZMU(:) = LOG( MIN(ZZ0EFF(:)/ZZ0H(:),200.) )
!
PCDN(:) = (XKARMAN/LOG(PUREF(:)/ZZ0EFF(:)))**2

ZCMSTAR(:) = CMSTAR(ZMU(:))
ZPM(:)     = PM(ZMU(:))
!
ZCM(:) = 10.*ZCMSTAR(:)*PCDN(:)*( PUREF(:)/ZZ0EFF(:) )**ZPM(:)
!
WHERE( PRI > 0.0 )
  ZFM(:) = 1. + 10.*PRI(:) / SQRT( 1.+5.*PRI(:) )
  ZFM(:) = 1. / ZFM(:)
ELSEWHERE
  ZFM(:) = 1. - 10.*PRI(:) / ( 1.+ZCM(:)*SQRT(-PRI(:)) )
END WHERE 
!
PCD(:) = PCDN(:)*ZFM(:)
!
!-------------------------------------------------------------------------------
!
!

!
CONTAINS
!
!                                              functions used in the calculation
!                                              the terms Cm
!  

  FUNCTION CMSTAR(X) 3
  REAL, DIMENSION(:), INTENT(IN)     :: X
  REAL, DIMENSION(SIZE(X))           :: CMSTAR
  !
  CMSTAR = 6.8741 + 2.6933*X - 0.3601*X*X + 0.0154*X*X*X
  !
  END FUNCTION CMSTAR
  !
  !

  FUNCTION PM(X) 3
  REAL, DIMENSION(:), INTENT(IN)     :: X
  REAL, DIMENSION(SIZE(X))           :: PM
  !
  PM = 0.5233 - 0.0815*X + 0.0135*X*X - 0.0010*X*X*X
  !
  END FUNCTION PM
                               
!
!-------------------------------------------------------------------------------
!
!
END SUBROUTINE SURFACE_CD