SUBROUTINE FKNORM
*-- Author : S.Burke / J.V. Morris
      SUBROUTINE FKNORM(S,IERR)
*-----------------------------------------Updates 27/07/93-------                                             
**: FKNORM 30907 SB. Error 17 now tan(theta)>5.                                                               
**********************************************************************                                        
*                                                                    *                                        
* Normalise the theta and phi components of the state vector S       *                                        
*                                                                    *                                        
* ERROR CONDITIONS;                                                  *                                        
*       IERR =   0 ; normal termination                              *                                        
*       IERR = 116 ; tan(theta) > 10**6: reset to 10**6              *                                        
*                  | x or y > 10**4: reset to 10**4                  *                                        
*       IERR =  17 ; tan(theta) > 50                                 *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
      IMPLICIT DOUBLE PRECISION (A-H,O-Z)                               
                                                                        
      PARAMETER (IUTIL=1,IROUT=4)                                       
*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 S(5)                                                    
                                                                        
*KEEP,FKPIDP.                                                                                                 
      DOUBLE PRECISION PI,TWOPI,PIBY2                                   
      PARAMETER (PI=3.141592653589793238)                               
      PARAMETER (TWOPI=PI*2.0D0,PIBY2=PI/2.0D0)                         
*KEND.                                                                                                        
                                                                        
**********************************************************************                                        
                                                                        
      IERR = 0                                                          
                                                                        
      IF (S(4).LT.0.D0) THEN                                            
         S(4) = DABS(S(4))                                              
         S(5) = S(5) + PI                                               
      ENDIF                                                             
                                                                        
      IF (S(4).GT.1.D6) THEN                                            
         CALL FKERR(IUTIL,IROUT,IFATAL,ITHGP2,IERR)
         S(4) = 1.D6                                                    
      ELSEIF (S(4).GT.50.D0) THEN                                       
         CALL FKERR(IUTIL,IROUT,IWARN,ITHG1,IERR)
      ENDIF                                                             
                                                                        
      IF (DABS(S(5)).GT.TWOPI) S(5) = DMOD(S(5),TWOPI)                  
      IF (S(5).LT.0.D0) S(5) = S(5) + TWOPI                             
                                                                        
      IF (ABS(S(1)).GT.1.D4) THEN                                       
         CALL FKERR(IUTIL,IROUT,IFATAL,ITHGP2,IERR)
         S(1) = SIGN(1.D4,S(1))                                         
      ENDIF                                                             
                                                                        
      IF (ABS(S(2)).GT.1.D4) THEN                                       
         CALL FKERR(IUTIL,IROUT,IFATAL,ITHGP2,IERR)
         S(2) = SIGN(1.D4,S(2))                                         
      ENDIF                                                             
                                                                        
      RETURN                                                            
      END                                                               
*