!-------------------------------------- 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 LQTOES_AD(delLQ,delTT,delP0,delES,LQ_g,PRES_g,dPdPsfc) 3,8
!
! Purpose: ADJOINT VERSION
!          to calculate the dew point depression from specific
!          humidity, temperature and pressure.  No ice phase
!          is permitted and the pressure vector is given.
!
      use MathPhysConstants_mod
      use physicsFunctions_mod
      IMPLICIT NONE
      REAL*8, intent(inout) :: delLQ,delTT,delP0
      REAL*8, intent(in)  :: delES,LQ_g,PRES_g,dPdPsfc
      REAL*8 ZE,ZTD,dTDdE,ZQBRANCH,HU_g
      REAL*8 dESdLQ,dESdTT,dESdP0

      dESdTT = 1.0d0
*
* Forward calculations of saturation vapour pressure and dewpoint temperature
* and adjoint of vapour pressure from adjoint of dewpoint temperature
*
      HU_g = exp(LQ_g)
      ZE = FOEFQ8(HU_g, PRES_g)

      ZTD=FOTW8(ZE)
      dTDdE=FODTW8(ZTD,ZE)
*
* adjoint of temp. specific humidity and surface pressure due to changes in vapour pressure
*
      ZQBRANCH = FQBRANCH(HU_g)
      dESdLQ = - ZQBRANCH*FOEFQA(1.0d0,dTDdE,HU_g,PRES_g)

      dESdP0 = - ZQBRANCH*FOEFQPSA(1.0d0,dTDdE,HU_g,dPdPsfc)-
     &           (1.D0-ZQBRANCH)*(dTDdE*dPdPsfc)

      ! TLM: delES =  dESdLQ*delLQ + dESdP0*delP0 + dESdTT*delTT
      ! ADJOINT:
      delLQ = delLQ + dESdLQ*delES
      delP0 = delP0 + dESdP0*delES
      delTT = delTT + dESdTT*delES

      return
      END SUBROUTINE LQTOES_AD