SUBROUTINE FKETOI
*-- Author : S.Burke
      SUBROUTINE FKETOI(EXT,SINT,CINT)
**********************************************************************                                        
*                                                                    *                                        
* Convert an `external' state vector to an `internal' vector         *                                        
* and covariance.                                                    *                                        
*                                                                    *                                        
* External: 1/r, phi, theta, x, y, z, IPTYPE, covariance             *                                        
* Internal: x, y, q/p, tan(theta), phi                               *                                        
*                                                                    *                                        
* The `external' covariance is packed with COVCP.                    *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
*KEEP,FKPIDP.                                                                                                 
      DOUBLE PRECISION PI,TWOPI,PIBY2                                   
      PARAMETER (PI=3.141592653589793238)                               
      PARAMETER (TWOPI=PI*2.0D0,PIBY2=PI/2.0D0)                         
*KEND.                                                                                                        
                                                                        
      PARAMETER (BCONV=-0.0002998)                                      
                                                                        
      DOUBLE PRECISION SINT(5),CINT(5,5),SEC2TH,DIFF1,DIFF3             
      DIMENSION EXT(16),B(3),COV(15)                                    
                                                                        
**********************************************************************                                        
                                                                        
* Get the field                                                                                               
      CALL GUFLD(EXT(4),B)                                                                             
                                                                        
* Trap zero field problems                                                                                    
      IF (ABS(B(3)).LT.1.0E-2) B(3) = SIGN(12.0,B(3))                   
                                                                        
* Transform the state vector                                                                                  
      SINT(1) = EXT(4)                                                  
      SINT(2) = EXT(5)                                                  
      SINT(3) = EXT(1)*DSIN(DBLE(EXT(3)))/(BCONV*B(3))                  
      SINT(4) = DTAN(DBLE(EXT(3)))                                      
      SINT(5) = EXT(2)                                                  
      IF (SINT(5).LT.0.D0) SINT(5) = SINT(5) + TWOPI                    
                                                                        
* Trap more zero-field problems                                                                               
      IF (ABS(SINT(3)).GT.1.0D4) SINT(3) = SIGN(1.0D4,SINT(3))          
                                                                        
* Differential coefficients                                                                                   
      SEC2TH = (1.D0 + SINT(4)*SINT(4))                                 
      IF (EXT(1).NE.0.) THEN                                            
         DIFF1 = SINT(3)/EXT(1)                                         
      ELSE                                                              
         DIFF1 = DSIN(DBLE(EXT(3)))/(BCONV*B(3))                        
      ENDIF                                                             
      DIFF3 = SINT(3)*SINT(4)                                           
                                                                        
* Unpack the covariance ...                                                                                   
      CALL COVCP(5,COV,EXT(8),2)                                                                       
                                                                        
* ... and transform it                                                                                        
      CINT(1,1) = COV(10)                                               
                                                                        
      CINT(2,1) = COV(14)                                               
      CINT(2,2) = COV(15)                                               
                                                                        
      CINT(3,1) = COV(7) *DIFF1 + COV(9) *DIFF3                         
      CINT(3,2) = COV(11)*DIFF1 + COV(13)*DIFF3                         
      CINT(3,3) = COV(1)*DIFF1*DIFF1 + 2.D0*COV(4)*DIFF1*DIFF3          
     &          + COV(6)*DIFF3*DIFF3                                    
                                                                        
* Trap more zero-field problems                                                                               
      IF (CINT(3,3).GT.1.0D6) CINT(3,3) = 1.0D6                         
                                                                        
      CINT(4,1) = COV(9)*SEC2TH                                         
      CINT(4,2) = COV(13)                                               
      CINT(4,3) = (COV(4)*DIFF1 + COV(6)*DIFF3)*SEC2TH                  
      CINT(4,4) = COV(6)*SEC2TH*SEC2TH                                  
                                                                        
      CINT(5,1) = COV(8)                                                
      CINT(5,2) = COV(12)                                               
      CINT(5,3) = COV(2)*DIFF1 + COV(5)*DIFF3                           
      CINT(5,4) = COV(5)*SEC2TH                                         
      CINT(5,5) = COV(3)                                                
                                                                        
      RETURN                                                            
      END                                                               
*