      subroutine StomatalConductance(pco2s,rehulfsurf,gammas,
     &    pvapordef_s,rayDzero,assim_net,istommodel,
     &    stomintercept,stomslope,gswmod)
      implicit none

!=====================Inputs===================================
! pvapordef_s:  water vapor partial pressure deficit at the leaf surface [Pa]
! istommodel:   which stomatal conductance model to use
!                    1 = Ball - Berry Model
!                    2 = Ray Leuning model using leaf surface CO2 concentration
!                    3 = Belinda E. Medlyn model
!                    4 = Dewar model
! stomintercept: Interception in the Ball - Berry model or the Leuning version [mol H2O m-2 s-1]
! stomslope:     Slope in the Ball - Berry model or the leuning version [--]
! rayDzero:      D0 in the Ray Leuning modified Ball - Berry Model [Pa]
!
! assim_net:     net rate of CO2 uptake per unit leaf area
!                     calculated from the biochemical model[umol m-2 s-1]
! gammas:        CO2 compensation point (ppm)      
! pco2s:         CO2 concentration at the leaf surface or internal CO2 (ppm)
! rehulfsurf:   relative humidity at the leaf surface [0-1]

      integer istommodel
      double precision pco2s,rehulfsurf,gammas,
     &    pvapordef_s,rayDzero,stomintercept,
     &    stomslope,assim_net

!=====================Outputs=================================
! gswmod:       stomatal conductance for water vapor calculated 
!                 from stomatal conductance model [mol m-2 s-1]
      double precision gswmod

      if(istommodel.eq.1)then
!         Ball - Berry model        
        gswmod=stomintercept+stomslope*assim_net*
     &         rehulfsurf/pco2s
        gswmod=dmax1(gswmod,stomintercept)
      endif
      if(istommodel.eq.2)then
!       Ray Leuning model using leaf surface CO2 or internal CO2
        gswmod=stomintercept+stomslope*assim_net/
     &    ((pco2s-gammas)*(1.0d0+pvapordef_s/rayDzero))
        gswmod=dmax1(gswmod,stomintercept)
      endif
      if(istommodel.eq.3)then
!Belinda Medlyn model
        gswmod=stomintercept+(1.0d0+stomslope/dsqrt(1.0d-3*pvapordef_s))
     &*assim_net/pco2s
        gswmod=dmax1(gswmod,stomintercept)
      endif
      if(istommodel.eq.4)then
!       Dewar model
        gswmod=(stomintercept+stomslope*assim_net)/
     &    (pco2s*(1.0d0+pvapordef_s/rayDzero))
        gswmod=dmax1(gswmod,stomintercept)
      endif
      
      return
      end subroutine StomatalConductance

      subroutine Der_StomatalConductance(pco2s,rehulfsurf,
     &    gammas,pvapordef_s,rayDzero,assim_net,istommodel,
     &    stomintercept,stomslope,derivb,derivslope,derivd0)
      implicit none

!=====================Inputs===================================
! pvapordef_s:  water vapor partial pressure deficit at the leaf surface [Pa]
! istommodel:   which stomatal conductance model to use
!                    1 = Ball - Berry Model
!                    2 = Ray Leuning model using leaf surface CO2 concentration
!                    3 = Belinda Medlyn model
!                    4 = Dewar model
! stomintercept: Interception in the Ball - Berry model or the Leuning version [mol H2O m-2 s-1]
! stomslope:     Slope in the Ball - Berry model or the leuning version [--]
! rayDzero:      D0 in the Ray Leuning modified Ball - Berry Model [Pa]
!
! assim_net:     net rate of CO2 uptake per unit leaf area
!                     calculated from the biochemical model[umol m-2 s-1]
! gammas:        CO2 compensation point (ppm)      
! pco2s:         CO2 concentration at the leaf surface or internal CO2 (ppm)
! rehulfsurf:   relative humidity at the leaf surface [0-1]

      integer istommodel
      double precision pco2s,rehulfsurf,gammas,
     &    pvapordef_s,rayDzero,stomintercept,
     &    stomslope,assim_net

!=====================Outputs=================================
! gswmod:       stomatal conductance for water vapor calculated 
!                 from stomatal conductance model [mol m-2 s-1]
      double precision derivb,derivslope,derivd0
      
      derivb=1.0d0
      if(istommodel.eq.1)then
!         Ball - Berry model
        derivslope=assim_net*rehulfsurf/pco2s
      endif
      if(istommodel.eq.2)then
!       Ray Leuning model using leaf surface CO2
        derivslope=assim_net/
     &    ((pco2s-gammas)*(1.0d0+pvapordef_s/rayDzero))
        derivd0=(stomslope*assim_net/(pco2s-gammas))*
     &          pvapordef_s/((rayDzero+pvapordef_s)**2.0d0)
      endif
      if(istommodel.eq.3)then
!       Belinda E. Medlyn model
        derivslope=assim_net/(pco2s*dsqrt(1.0d-3*pvapordef_s))
      endif
      if(istommodel.eq.4)then
!       Ray Leuning model using leaf surface partial pressure
        derivb=1.0d0/(pco2s*(1.0d0+pvapordef_s/rayDzero))
        derivslope=assim_net/
     &    (pco2s*(1.0d0+pvapordef_s/rayDzero))
        derivd0=((stomintercept+stomslope*assim_net)/pco2s)*
     &          pvapordef_s/((rayDzero+pvapordef_s)**2.0d0)
      endif
      return
      end subroutine Der_StomatalConductance
