SUBROUTINE fcserf_ad(PX,PX_HAT,PERF_HAT)

!**** *fcserf_ad* - Operator routine for gps radio occultation observations.

!     S. Healy             ECMWF          2004/10/26

!     PURPOSE
!     -------
!     Adjoint of the error function calculation :

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

!    CALL gpsro_op(...)  

!     fcserf_ad is called from gpscalc_alphaad

!**   METHOD
!     ------
!     1) Abrmowitz and Stegun, Page 299, 7.1.25


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

!**   MODIFICATIONS
!     -------------
!     Sean Healy      2004/01/05 : Original
!     -------------------------------------------------------------------------


USE PARKIND1  ,ONLY : JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK



IMPLICIT NONE

REAL(KIND=JPRB), INTENT(IN)     :: PX
REAL(KIND=JPRB), INTENT(INOUT)  :: PX_HAT
REAL(KIND=JPRB), INTENT(INOUT)  :: PERF_HAT
REAL(KIND=JPRB) :: ZT,ZT_HAT 
REAL(KIND=JPRB) :: ZHOOK_HANDLE

IF (LHOOK) CALL DR_HOOK('FCSERF_AD',0,ZHOOK_HANDLE)

ZT = 0.0_JPRB

ZT_HAT = 0.0_JPRB
      
IF (ABS(PX) < 9.0_JPRB) THEN
      
   IF (PX == 0.0_JPRB) THEN 
      
       PERF_HAT = 0.0_JPRB
                   
   ELSEIF (PX > 0.0_JPRB) THEN
      
      ZT=1.0_JPRB/(1.0_JPRB+0.47047_JPRB*PX)
  
      ZT_HAT = ZT_HAT - &
       &  (0.3480242_JPRB-(0.1917596_JPRB-2.2435668_JPRB*ZT)*ZT)*EXP(-(PX*PX))*PERF_HAT
      PX_HAT = PX_HAT + &
       &  (0.3480242_JPRB-(0.0958798_JPRB-0.7478556_JPRB*ZT)*ZT)*ZT*EXP(-(PX*PX))* &
       &  2.0_JPRB*PX*PERF_HAT
      PERF_HAT = 0.0_JPRB
      
      PX_HAT = PX_HAT - ZT/(1.0_JPRB+0.47047_JPRB*PX)*0.47047_JPRB*ZT_HAT
      ZT_HAT = 0.0_JPRB 
 
   ELSE   !(PX < 0.0)!
      
      ZT=1.0_JPRB/(1.0_JPRB-0.47047_JPRB*PX)
     
      ZT_HAT = ZT_HAT + &
       &  (0.3480242-(0.1917596-2.2435668*ZT)*ZT)*EXP(-(PX*PX))*PERF_HAT
      PX_HAT = PX_HAT - &
       &  (0.3480242_JPRB-(0.0958798_JPRB-0.7478556_JPRB*ZT)*ZT)*ZT*EXP(-(PX*PX))*&
       &  2.0_JPRB*PX*PERF_HAT
      PERF_HAT = 0.0_JPRB
      
      PX_HAT = PX_HAT + ZT/(1.0_JPRB-0.47047_JPRB*PX)*0.47047_JPRB*ZT_HAT
      ZT_HAT = 0.0_JPRB

   ENDIF   
   
ELSE
  
   PERF_HAT = 0.0_JPRB
         
ENDIF 

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

      
      
END SUBROUTINE fcserf_ad
