!-------------------------------------- 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 --------------------------------------
***FONCTION SESAHU  -  PASSAGE DE ES A HU
*

      FUNCTION SESAHU(ES,TX,PX,LNPS,MODP,SWTT,SWPH) 2,8
#include "impnone.cdk"
      REAL SESAHU, ES, TX, PX, LNPS
      INTEGER MODP
      LOGICAL SWTT, SWPH
*
*Author
*          N. Brunet  (Jan91)
*
*Revision 01 - N. Brunet (May 1994) - Improved iteration for tx=tv
*
*Object
*          to calculate specific humidity from dew point
*          depression, temperature and pressure.
*
*Arguments
*
*          - Input -
* ES       dew point depression in degrees Celsius
* TX       temperature or virtual temperature in Kelvins
* PX       see MODP
* LNPS     see MODP
* MODP     pressure mode(SI units only):
*          =0; pressure level in PX
*          =1; sigma level in PX and PS(surface pressure) in LNPS
*          =2; sigma level in PX and logarithm of sigma level in
*          LNPS
*          =3; all points of pressure in LNPS(NI,*) in Pascals
*          =4; sigma level in PX and logarithm of sigma level in
*          LNPS(in millibars unless using SI units)
*          =5; logarithm of pressure level in PX(in millibars unless
*          using SI units)
* SWTT     .TRUE. to pass TT for argument
*          .FALSE. to pass TV for argument
* SWPH     .TRUE. to consider water and ice phase
*          .FALSE. to consider water phase only
*
*IMPLICITES
#include "consphy.cdk"
*MODULES
      EXTERNAL INCTPHY
*
**
*--------------------------------------------------------------------
      REAL E, TD, TP, HUS, PN
      REAL TG, EPSIL
      INTEGER ITER, NITER
*
#include "dintern.cdk"
#include "fintern.cdk"
*--------------------------------------------------------------------
#include "initcph.cdk"
*
#include "modpr1.cdk"
*
      TP = TX
      TD = TP - ES
      IF(SWPH)THEN
         E = FOEW(TD)
      ELSE
         E = FOEWA(TD)
      END IF
      HUS = FOQFE(E,PN)
      TP = FOTTV(TX,HUS)
*
      IF(SWTT)THEN
         SESAHU = HUS
         RETURN
      END IF
*
      NITER = 25
      EPSIL = 0.001
      TG = 0.5 * (TX + TP)
*
      DO 10 ITER=1,NITER
         TD = TG - ES
         IF(SWPH)THEN
            E = FOEW(TD)
         ELSE
            E = FOEWA(TD)
         END IF
         HUS = FOQFE(E,PN)
         TP = FOTTV(TX,HUS)
*
         IF(ABS(TP-TG) .LT. EPSIL)THEN
            SESAHU = HUS
            RETURN
         END IF
*
         TG = 0.5*(TP + TG)
*
10    CONTINUE
*
      SESAHU = HUS
*
      RETURN
      CONTAINS
#include "fintern90.cdk"
      END