        SUBROUTINE ZPHAFN(X,A,Y,DERIV,NA)
*
*  Module number: 13.4.3
*
*  Module name: ZPHAFN
*
*  Keyphrase:
*  ----------
*       Compute pha fit function value
*  Description:
*  ------------
C
C ROUTINE TO EVALUTE THE DERIVATIVES OF FUNCTION FOR LEAST-SQUARES SEARCH
C       WITH FORM OF A GAUSSIAN AND A QUADRATIC POLYNOMIAL.
C       Y = A(1)*EXP(-Z*82/2) + A(4)+A(5)*X
C       WHERE Z = (X-A(2))/A(3)
C
C
*
*  FORTRAN name: zphafn.for
*
*  Keywords of accessed files and tables:
*  --------------------------------------
*       none
*
*  Subroutines Called:
*  -------------------
*       none
*
*  History:
*  --------
*  Version      Date        Author          Description
*     1       Mar 88       D. Lindler   Designed and coded
*-------------------------------------------------------------------------------
C INPUT PARAMETERS
C
C       X - X VALUE
C       A - ARRAY OF PARAMETERS
C
C OUTPUT PARAMETERS
C
C       DERIV - DERIVATIVES OF FUNCTION
C
        INTEGER NA
        DOUBLE PRECISION X,A(NA),Y,DERIV(*)
        INTEGER I
        DOUBLE PRECISION TEMP,Z,Z2
C-----------------------------------------------------------------------
        DO 1 I=1,3
1               DERIV(I)=0.0
C
C SET UP BASELINE STEP FUNCTION
C
	ILO = A(2)-ABS(A(3))
	IHI = A(2)+ABS(A(3))
	IF (X.LE.ILO) THEN
	   Y = A(4) 
           DERIV(4) = 1.
           DERIV(5) = 0.
	ELSE IF (X.GE.IHI) THEN
	   Y = A(5) 
           DERIV(4) = 0.
           DERIV(5) = 1.
	ELSE 
	   Y = A(4)+(A(5)-A(4))/(IHI-ILO)*(X-ILO) 
           DERIV(4) = 1.-(X-ILO)/(IHI-ILO) 
           DERIV(5) = (X-ILO)/(IHI-ILO) 
	ENDIF
C
C LINEAR BASELINE
C
C        Y = A(4) + A(5)*X
C        DERIV(4) = 1.
C        DERIV(5) = X
C
C NOW ADD THE GAUSSIAN
C
        IF(A(3).GT.1.0E-10)THEN
                Z = (X - A(2))/A(3)
                Z2=Z**2
                IF(Z2.LT.50.0)THEN
                        TEMP=EXP(-Z2/2.0)
                        Y = Y + A(1)*TEMP
                        DERIV(1) = TEMP
                        DERIV(2) = A(1)*DERIV(1)*Z/A(3)
                        DERIV(3) = DERIV(2)*Z
                ENDIF
        ENDIF
        RETURN
        END
