!-------------------------------------- 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 mhuaesgd(pesg,phug,pttg,ppsg,ppt,kni,knj,knk) 2
#if defined (DOC)
***   S/R mhuaesgd  -  CALCULATE ES=T-TD FROM T AND Q.  Grid-point version.
*                      NO ICE PHASE....
*
*Author
*          L. Fillion  -ARMA/AES - 11 dec 98
*
*Revision
*          Y.J. Rochon and Cecilien Charette - SMC - Sept 2004
*            - Use of new function FOTW8 FOEFQ8(in fintern8.cdk)
*              Follow WMO convention on T-Td vs HU relationship)
*
*Arguments:
*          - Output -
*     zesg  : Grid-point analysis increment of (T-Td)
*
*          - Input -
*     phug  : Grid-point basic-state specific-humidity. (Kg/Kg)
*     pttg  : Grid-point basic-state Temperature (K)
*     ppsg  : Grid-point basic-state Surface-pressure (Pa).
*     ppt   : Grid-point top pressure level of the analysis domain.
*     kni   : X-dimension of input grid-point fields.
*     knj   : Y-dimension of input grid-point fields.
*     knk   : Vertical-dimension of input grid-point fields.
*
*    -------------------
*
*Object:   For postprocessing in variational analysis:
*          calculate the dew point depression from specific
*          humidity, temperature and pressure.  No ice phase
*          is permitted.
*
#endif
      IMPLICIT NONE
      integer kni,knj,knk
      real*8 pesg(kni,knk,knj),phug(kni,knk,knj),pttg(kni,knk,knj)
      real*8 ppsg(kni,knj),ppt(kni,knj)
*
*IMPLICITS
#include "pardim.cdk"
#include "comdim.cdk"
#include "comgd0.cdk"
#include "comgem.cdk"
*
      INTEGER ji,jj,jlev,jij
      REAL*8 ZE, ZTD, ZPRES
      INTEGER IERR,ILEN
      EXTERNAL HPDEALLC, HPALLOC
      real*8 zpresa,zpresb
C
*
#include "comphy.cdk"
#include "dinternv.cdk"
#include "dintern8.cdk"
#include "fintern8.cdk"
#include "finternv.cdk"
*
**
      do jlev=1,knk
      do ji=1,kni
      do jj=1,knj
        jij=(ji-1)*knj+jj
        zpresb = ((vhybinc(jlev) - rptopinc/rprefinc)
     &           /(1.0-rptopinc/rprefinc))**rcoefinc
        zpresa = rprefinc * (vhybinc(jlev)-zpresb)
        zpres  = zpresa + zpresb*ppsg(ji,jj)
c
c        zpres = ppt(ji,jj) + vlev(jlev)*(ppsg(ji,jj)-ppt(ji,jj))
c
c       get the saturated vapor pressure from q (specific humidity)
c
        ZE = FOEFQ8(phug(ji,jlev,jj),zpres)
c
c       now the dewpoint temperature
c
        ZTD=FOTW8(ZE)
c
c       finally the dewpoint depression
c
        pesg(ji,jlev,jj) = pttg(ji,jlev,jj) - ZTD
      enddo
      enddo
      enddo
c
      return
      end