SUBROUTINE FTLFTW
*-- Author : I.O. Skillicorn
      SUBROUTINE FTLFTW(XS,YS,WS,L,KEY,KK,AS,BS,ES,DAS,DBS,COVS)
C     MOD TO VECTORIZE                                                                                        
      DIMENSION XS(100),YS(100),WS(100)                                 
      DOUBLE PRECISION X(100),Y(100),W(100)                             
      DOUBLE PRECISION W2, W2X, W2Y, W2XY, W2X2, W2Y2                   
      DOUBLE PRECISION A,B,D,E                                          
      DOUBLE PRECISION DA, DB, COV                                      
                                                                        
C     TO PERFORM A WEIGHTED STRAIGHT LINE FIT  Y=AX+B                                                         
C     CERN LIBRARY ROUTINE MOD TO INCLUDE FIT TO Y=AX                                                         
C     CALCULATE SUMS   WEIGHT =1./ERROR                                                                       
      IF(L.LE.1)GOTO1                                                   
      W2=0.0D0                                                          
      W2X=0.0D0                                                         
      W2Y=0.0D0                                                         
      W2XY=0.0D0                                                        
      W2X2=0.0D0                                                        
      W2Y2=0.0D0                                                        
      ICNT=0                                                            
      DO 2 J=1,L                                                        
      W(J) = DBLE(WS(J))                                                
      X(J) = DBLE(XS(J))                                                
      Y(J) = DBLE(YS(J))                                                
      W2=W2+W(J)*W(J)                                                   
      W2X=W2X+W(J)*W(J)*X(J)                                            
      W2X2=W2X2+W(J)*W(J)*X(J)*X(J)                                     
      W2XY=W2XY+W(J)*W(J)*X(J)*Y(J)                                     
      W2Y=W2Y+W(J)*W(J)*Y(J)                                            
      W2Y2=W2Y2+W(J)*W(J)*Y(J)*Y(J)                                     
      ICNT=ICNT+1                                                       
 2    CONTINUE                                                          
C                                                                                                             
C     FIT PARAMETERS                                                                                          
      IF(KK.EQ.2)THEN                                                   
      A=(W2XY-W2X*W2Y/W2)/(W2X2-W2X**2/W2)                              
      B=(W2Y-A*W2X)/W2                                                  
      D=W2X2*W2-W2X*W2X                                                 
C     ADDED 24/3/88                                                                                           
      IF(ABS(D).LT.1.0D-6)GOTO1                                         
C                                                                                                             
      COV=-W2X/D                                                        
      IF(ICNT.LE.2)GOTO3                                                
      E=(W2Y2-W2Y**2/W2-(W2XY-W2X*W2Y/W2)**2/(W2X2-W2X**2/W2))          
     1 /DFLOAT(ICNT-2)                                                  
      DA=DSQRT(W2/D)                                                    
      DB=DSQRT(W2X2/D)                                                  
      ELSE                                                              
      A=W2XY/W2X2                                                       
      DA=1./SQRT(W2X2)                                                  
      B=0.                                                              
      DB=0.                                                             
      E=(W2Y2-W2XY**2/W2X2)/FLOAT(ICNT-1)                               
      ENDIF                                                             
      GOTO4                                                             
C                                                                                                             
C     INSUFFICIENT POINTS                                                                                     
 1    A=0.0D0                                                           
      B=0.0D0                                                           
      DA=0.0D0                                                          
      DB=0.0D0                                                          
 3    E=0.0D0                                                           
 4    CONTINUE                                                          
      AS = A                                                            
      BS = B                                                            
      ES = E                                                            
      DAS = DA                                                          
      DBS = DB                                                          
      COVS = COV                                                        
      RETURN                                                            
      END                                                               
*                                                                                                             
*                                                                                                             
C new version imported from IOS fortran 6/6/91                                                                
*