!+ Subroutine to calculate specific humidities from Gill's formula.
!
! Method:
!	Straightforward.
!
! Current Owner of Code: J. M. Edwards
!
! History:
!	Version		Date			Comment
!	1.0		12-04-95		First Version under RCS
!						(J. M. Edwards)
!
! Description of Code:
!   FORTRAN 77  with extensions listed in documentation.
!
!- ---------------------------------------------------------------------
      SUBROUTINE QSAT_GILL(SAT_SPEC_HUM, T, P, N_PROFILE, N_LAYER,
     &                      L_LAYER, NPD_PROFILE, NPD_LAYER)
!
!
      IMPLICIT NONE
!
!
!     THIS ROUTINE COMPUTES THE SATURATED SPECIFIC HUMIDITY
!     AT TEMPERATURE T AND PRESSURE P,
!     USING THE FORMULAE GIVEN IN APPENDIX FOUR OF ADRIAN GILL'S BOOK.
!
!     NOTE THAT THE FORMULAE WORK WITH PRESSURES IN HECTOPASCALS AND
!     TEMPERATURES IN DEGREES CELSIUS. THE CONVERSIONS ARE MADE
!     INSIDE THIS ROUTINE AND SHOULD HAVE NO IMPACT ON THE REST
!     OF THE CODE.
!
!     THIS ROUTINE WAS PERPETRATED BY D L ROBERTS (12/8/93).
!
!     MODIFIED TO COPE WITH VERY LOW PRESSURES BY DLR (27/10/93).
!
      INTEGER  !, INTENT(IN)
     &    N_PROFILE
!               NUMBER OF PROFILES.
     &  , N_LAYER
!               NUMBER OF LAYERS.
     &  , NPD_PROFILE
!               MAX NUMBER OF PROFILES.
     &  , NPD_LAYER
!               MAX NUMBER OF LAYERS.
!
      REAL*8 !, INTENT(IN)
     &    P(NPD_PROFILE, 0: NPD_LAYER)
!               PRESSURE IN PASCALS
     &  , T(NPD_PROFILE, 0: NPD_LAYER)
!               TEMPERATURE IN KELVIN
!
      REAL*8 !, INTENT(OUT)
     &    SAT_SPEC_HUM(NPD_PROFILE, 0: NPD_LAYER)
!          SATURATED SPECIFIC HUMIDITY AT T AND P.
!
      LOGICAL  !, INTENT(IN)
     &    L_LAYER
!               TRUE=>LAYERS, FALSE=>LEVELS.
!
!     LOCAL VARIABLES.
!
      INTEGER NL,I,L
!
      REAL*8 PRESS ! PRESSURE IN HECTOPASCALS.
      REAL*8 TEMP ! TEMPERATURE IN CELSIUS.
      REAL*8 A ! A TEMPORARY HOLDING VARIABLE.
      REAL*8 EW ! SATURATION VAPOUR PRESSURE OF PURE WATER VAPOUR.
      REAL*8 EWDASH ! SAT VAP PRESSURE OF WATER VAPOUR IN AIR.
      REAL*8 FW ! THE RATIO BETWEEN EWDASH AND EW.
      REAL*8 ZERO_DEGC ! KELVIN EQUIVALENT OF ZERO CELSIUS.
!
!     THE VALUE ASSIGNED IS THAT USED IN V3.1 OF THE UNIFIED MODEL.
!
      PARAMETER( ZERO_DEGC=273.15D+00 )
!
      REAL*8 EPSILON ! THE RATIO OF THE MOLECULAR MASS OF WATER
!                            TO THAT OF DRY AIR.
!     THE VALUE ASSIGNED IS THAT USED IN V3.1 OF THE UNIFIED MODEL.
!
      PARAMETER( EPSILON=0.62198D+00 )
!
      REAL*8 ETA ! ONE MINUS EPSILON.
      PARAMETER( ETA = 1.0D+00 - EPSILON )
!
!     SET NL ACCORDING TO WHETHER WE HAVE LEVELS OR LAYERS.
!
      IF (L_LAYER) THEN
        NL = 1
      ELSE
        NL = 0
      ENDIF
!
!     LOOP OVER ALL POINTS.
!     THESE LOOPS ARE NOT INDENTED, IN ORDER TO MAKE THE
!     EQUATIONS EASIER TO READ BY KEEPING THEM TO ONE LINE.
!
      DO I = NL,N_LAYER
      DO L = 1,N_PROFILE
!
!     CONVERT TO LOCAL UNITS FOR TEMPERATURE AND PRESSURE.
!
      TEMP = T(L,I) - ZERO_DEGC
      PRESS = P(L,I)*0.01D+00
!
!     EQUATION (A4.7) OF GILL'S BOOK.
!
      FW = 1.0D+00 + 1.0D-06*PRESS*( 4.5D+00 + 6.0D-04*TEMP*TEMP )
!
!     EQUATION (A4.5) OF GILL.
!
      A = ( 7.859D-01 + 3.477D-02*TEMP )/( 1.0D+00 + 4.12D-03*TEMP )
      EW = 1.0D+01**A
!
!     EQUATION (A4.6) OF GILL.
!
      EWDASH = FW*EW
!
!     THE NEXT EQUATION IS A REARRANGEMENT OF GILL'S (A4.3),
!     WITH W SUBSCRIPTS ADDED BECAUSE WE REQUIRE SATURATION.
!
!     NOTE THAT AT VERY LOW PRESSURES A FIX HAS TO BE APPLIED,
!     TO AVOID A SINGULARITY.
!
      IF (PRESS .GT. EWDASH) THEN
        SAT_SPEC_HUM(L,I) = (EPSILON*EWDASH)/(PRESS-EWDASH*ETA)
      ELSE
        SAT_SPEC_HUM(L,I) = 1.0D+00
      ENDIF
!
!     END OF THE DOUBLE LOOP.
!
      ENDDO
      ENDDO
!
      RETURN
      END
