FUNCTION FKCHXY
*-- Author : S.Burke
      DOUBLE PRECISION FUNCTION FKCHXY(XY,CXY,SVEC,CVEC,IERR)
*-----------------------------------------Updates 27/07/93-------                                             
**: FKCHXY 30907 SB. Function now (correctly) double precision.                                               
**********************************************************************                                        
*                                                                    *                                        
* Calculate (filtered/smoothed) chi-squared for an x-y measurement   *                                        
*                                                                    *                                        
* ERROR CONDITIONS;                                                  *                                        
*       IERR =   0 ; normal termination                              *                                        
*   ->  IERR = 112 ; CRES not positive definite                      *                                        
*                                                                    *                                        
*   ->  Fatal error                                                  *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
      IMPLICIT DOUBLE PRECISION (A-H,O-Z)                               
                                                                        
      PARAMETER (IUTIL=1,IROUT=10)                                      
*KEEP,FKECODE.                                                                                                
      PARAMETER (IWARN=0,IFATAL=1,IFPRO=2,IFFLT=3,IFSMO=4,IFPRS=5,      
     &           IFPAS=6,IFPAF=7)                                       
      PARAMETER (IINF1=1,IINF2=2,IINF3=3,IINV=4,IDONE=5)                
      PARAMETER (IICV=6,IMCV=7,IOCV=11,IRCV=12,IOVCV=13,                
     &           ITHGP2=16,ITHG1=17)                                    
      PARAMETER (IFREE=20,IFREE1=30,IFREE2=40,IFREE3=50)                
*KEND.                                                                                                        
                                                                        
      DIMENSION XY(2),CXY(2,2),SVEC(5),CVEC(5,5)                        
                                                                        
**********************************************************************                                        
                                                                        
      IERR=0                                                            
                                                                        
* Calculate the residuals and covariance ...                                                                  
      R1  = XY(1) - SVEC(1)                                             
      R2  = XY(2) - SVEC(2)                                             
      C11 = CXY(1,1) - CVEC(1,1)                                        
      C21 = CXY(2,1) - CVEC(2,1)                                        
      C22 = CXY(2,2) - CVEC(2,2)                                        
                                                                        
* ... and the chi-squared                                                                                     
      DET = C11*C22 - C21*C21                                           
      IF (C11.LE.0.D0 .OR. DET.LE.0.D0) THEN                            
         FKCHXY = 0.D0                                                  
         CALL FKERR(IUTIL,IROUT,IFATAL,IRCV,IERR)
         RETURN                                                         
      ENDIF                                                             
                                                                        
      FKCHXY = (R1*(C22*R1 - 2*C21*R2) + C11*R2*R2)/DET                 
                                                                        
      RETURN                                                            
      END                                                               
*