SUBROUTINE gpscalc_refrac(K_NLEV,  &
 & pres, &
 & P_TEMP, &
 & P_Q,    &
 & PZ_DRY,    &
 & PZ_WET,   & 
 & P_REFRAC)  

!**** *gpscalc_refrac* - part of the GPSRO observation operator.

!     S. Healy             ECMWF          2004/10/26
!     Based on a EUMETSAT GRAS-SAF routine that was Dr'd automatically by the CY27 merge.  

!     PURPOSE
!     -------
!     It calculates the refractivity values on the model levels :

!**   INTERFACE
!     ---------

!     CALL gpscalc_refrac(K_NLEV,pres,P_TEMP,P_Q,P_REFRAC)  
!     WHERE K_NLEV   =  number of full model levels
!           pres     =  pressure on the full model levels
!           P_TEMP   =  temperature on the full model levels
!           P_Q      =  specific humidity on full model levels
!           PZ_DRY   = inverse of compressibility of dry air
!           PZ_WET   = inverse of compressibilty if water vapour
!           P_REFRAC =  refractivity on full model levels 

!     gpscalc_refrac is called from gpsro_op and gpsro_ad

!**   METHOD
!     ------
!     1) calculate the dry and wey refractivity terms and add them. 

!**   EXTERNALS
!     ---------

!**   MODIFICATIONS
!     -------------
!     Sean Healy      2004/10/26 : Original
!     Sean Healy      2008/01/26 : Use Rueger refractivity coefficients
!     -------------------------------------------------------------------------

               
USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMANCS   ,ONLY : RMDI
USE YOMCST    ,ONLY : RMV, RMD

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: K_NLEV 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRES(K_NLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_TEMP(K_NLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_Q(K_NLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PZ_DRY(K_NLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PZ_WET(K_NLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: P_REFRAC(K_NLEV) 
! local variables

REAL(KIND=JPRB), PARAMETER :: z_aval = 0.77643_JPRB
REAL(KIND=JPRB), PARAMETER :: z_bval = 3.75463E3_JPRB
REAL(KIND=JPRB), PARAMETER :: z_cval = 0.712952_JPRB


INTEGER(KIND=JPIM) :: i                ! counter
REAL(KIND=JPRB)    :: Z_NDRY,Z_NWET    ! dry and wet refractivity
REAL(KIND=JPRB)    :: Z_EPS            ! ratio of water vapour/ dry molecular weights   
REAL(KIND=JPRB)    :: Z_PW             ! water vapour pressure 
REAL(KIND=JPRB)    :: Z_PD 
REAL(KIND=JPRB) :: ZHOOK_HANDLE

! set the ratio of molecular weights (vapour/dry)

IF (LHOOK) CALL DR_HOOK('GPSCALC_REFRAC',0,ZHOOK_HANDLE)
Z_EPS = rmv/rmd

! initialise the refractivity to missing

P_REFRAC(:)  = RMDI

DO i = 1, K_NLEV

  Z_PW = pres(i)*P_Q(i)/(Z_EPS+(1.0_JPRB-Z_EPS)*P_Q(i))

  Z_PD = pres(i) - Z_PW

  Z_NDRY = z_aval*Z_PD/P_TEMP(i)
         
  Z_NWET = Z_PW/P_TEMP(i)*(z_bval/P_TEMP(i)+ z_cval)  
   
  P_REFRAC(i) = Z_NDRY*PZ_DRY(i) + Z_NWET*PZ_WET(i)
  
  IF (P_REFRAC(i) < 0.0_JPRB) THEN
  
     write (6,*) 'gps-refrac',i,p_refrac(i),Z_NDRY,Z_NWET,PZ_DRY(i),PZ_WET(i)
  
  ENDIF

ENDDO

IF (LHOOK) CALL DR_HOOK('GPSCALC_REFRAC',1,ZHOOK_HANDLE)

END SUBROUTINE gpscalc_refrac
