SUBROUTINE FPPJ23
*-- Author :    I. O. Skillicorn      16/11/92
      SUBROUTINE FPPJ23
c   fpphit called                                                       
**: FPPJ23 40000 IS. New linking code.                                                                        
**----------------------------------------------------------------------                                      
**: FPPJ23 30907 RP. Farm changes.                                                                            
**----------------------------------------------------------------------                                      
C     JOIN 2 PLANAR MODULES  - 2+3                                                                            
       COMMON/FGMIOS/                                                   
*    PLANAR GEOMETRY                                                                                          
     + ZPP(140),C(140),S(140),ITYPE(140),WZERO(140),WSPACE,             
*                                                                                                             
*    RADIAL GEOMETRY                                                                                          
     + ZP(36),PHW(36),WS(36)                                            
                                                                        
*KEEP,FPJPAR.                                                                                                 
      COMMON/FPJPAR/                                                    
     +      RRCUT1, RRCUT2, RRCUT3,                                     
     +      PLCC3, PLCC12, PLCC23, PLCC13                               
*KEND.                                                                                                        
                                                                        
      COMMON/FTPS3/NS(3),SPAR(4,50,3),IPT(12,50,3),IPLA(12,50,3),       
     1  SGN(12,50,3),YYS(12,50,3),YYF(12,50,3)                          
      COMMON/FTPLNK/LINK,CHIL(100),LNK(3,100),IUS(100,3)                
      COMMON/FTPPBK/NPP,IPP(36,100),CHPP(100),LP(3,100)                 
      COMMON/FPPFIT/PSSS(100),PISS(100),RSSS(100),RISS(100)             
                                                                        
      COMMON/FKLOC/KLOC(100)                                            
      COMMON/FEVSAT/IEVSAT                                              
      COMMON/fsegtp/iseg(100,3)                                         
                                                                        
      DIMENSION PSS(100),PIS(100),RSS(100),RIS(100)                     
      DIMENSION PPP(50) ,TH(50),XX(50),YY(50),ZZ(50),WP(50),WPP(50)     
      DIMENSION CH(100)                                                 
      PI2=6.283185307                                                   
*     RRCUT=10.0                                                                                              
      LINK=0                                                            
      LINKO=LINK                                                        
      DO 200 J=1,NS(2)                                                  
      IF(IUS(J,2).NE.0)GOTO200                                          
      Z2=ZPP(18)                                                        
*     X2=SPAR(3,J,2)*Z2+SPAR(4,J,2)                                                                           
*     Y2=SPAR(1,J,2)*Z2+SPAR(2,J,2)                                                                           
      DO 300 K=1,NS(3)                                                  
      IF(IUS(K,3).NE.0)GOTO300                                          
      Z3=ZPP(30)                                                        
*     X3=SPAR(3,K,3)*Z3+SPAR(4,K,3)                                                                           
*     Y3=SPAR(1,K,3)*Z3+SPAR(2,K,3)                                                                           
      ZM=0.5*(Z2+Z3)                                                    
      X2M=SPAR(3,J,2)*ZM+SPAR(4,J,2)                                    
      Y2M=SPAR(1,J,2)*ZM+SPAR(2,J,2)                                    
      X3M=SPAR(3,K,3)*ZM+SPAR(4,K,3)                                    
      Y3M=SPAR(1,K,3)*ZM+SPAR(2,K,3)                                    
      RR=    ((X2M-X3M)**2+(Y2M-Y3M)**2)                                
      CALL SHS(902,0,RR)                                                                               
      RRCUT=RRCUT1                                                      
      If(   iseg(j,2).gt.1                                              
     + .or. iseg(k,3).gt.1  )RRCUT=RRCUT2                               
      If(   iseg(j,2).gt.2                                              
     + .or. iseg(k,3).gt.2  )RRCUT=RRCUT3                               
      IF(RR.GT.RRCUT)GOTO300                                            
      call fpphit(2,3,j,k,iflag)                                        
      if(iflag.eq.1)goto300                                             
      IC=0                                                              
      DO 400 L=2,3                                                      
CSB      IF(L.EQ.1)II=I                                                                                       
      IF(L.EQ.2)II=J                                                    
      IF(L.EQ.3)II=K                                                    
      DO 410 LL=1,12                                                    
      IP=(L-1)*12+LL                                                    
      ZA=ZPP(IP)                                                        
      XF=SPAR(3,II,L)*ZA+SPAR(4,II,L)                                   
      YF=SPAR(1,II,L)*ZA+SPAR(2,II,L)                                   
      IC=IC+1                                                           
      XX(IC)=ZA                                                         
      YY(IC)=ATAN2(YF,XF)                                               
      ZZ(IC)=SQRT(XF**2+YF**2)                                          
      IF(YY(IC).LT.0.0)YY(IC)=YY(IC)+PI2                                
      WP(IC)=1./(0.10/ZZ(IC))                                           
      WPP(IC)=1.0                                                       
 410  CONTINUE                                                          
 400  CONTINUE                                                          
      IF(IC .GT. 1)THEN                                                 
      DO600 JJ=2,IC                                                     
      DP = YY(JJ)-YY(JJ-1)                                              
      IF(DP.GT.0.) THEN                                                 
      IF(ABS(DP).GT.ABS(DP-PI2))YY(JJ)=YY(JJ)-PI2                       
      ELSE                                                              
      IF(ABS(DP).GT.ABS(DP+PI2))YY(JJ)=YY(JJ)+PI2                       
      ENDIF                                                             
 600  CONTINUE                                                          
      ENDIF                                                             
C     FIT PHI-Z                                                                                               
      CALL FTLFTW(XX,YY,WP,IC,0,2,PS,PI,D1,D2,D3,D4)
C     FIT R-Z                                                                                                 
      CALL FTLFTW(XX,ZZ,WPP,IC,0,2,RS,RI,D1,D2,D3,D4)
      CHIP=0.                                                           
      DO 700 JJ=1,IC                                                    
      CHIP=CHIP+(YY(JJ)-PS*XX(JJ)-PI)**2*WP(JJ)**2                      
*     DIFF=YY(JJ)-PS*XX(JJ)-PI                                                                                
*     PRINT1002,JJ,(YY(JJ)-PS*XX(JJ)-PI)**2*WP(JJ)**2,YY(JJ),DIFF                                             
 1002 FORMAT(' ',I3,F10.2,2F10.4)                                       
 700  CONTINUE                                                          
      CHIP=CHIP/24.                                                     
      CALL FPCHI(2,3,J,K,CHID)
      CALL SHS(520,0,CHIP)                                                                             
      CALL SHS(573,0,CHID)                                                                             
*     Remove Links with poor Chisq...  18/11/93                                                               
      IF(CHID.GT.PLCC23)GOTO300                                         
*                                                                                                             
      LINK=LINK+1                                                       
      IF(LINK.GT.100)LINK=100                                           
      CHIL(LINK)=CHIP                                                   
      CH(LINK)=CHID                                                     
      LNK(1,LINK)=0                                                     
      LNK(2,LINK)=J                                                     
      LNK(3,LINK)=K                                                     
      PSS(LINK)=PS                                                      
      PIS(LINK)=PI                                                      
      RSS(LINK)=RS                                                      
      RIS(LINK)=RI                                                      
C     MOMENTUM FROM ANGLE CHANGE                                                                              
*     T1=ATAN2(SPAR(1,J,2),SPAR(3,J,2))                                                                       
*     IF(T1.LT.0.)T1=T1+PI2                                                                                   
*     T2=ATAN2(SPAR(1,K,3),SPAR(3,K,3))                                                                       
*     IF(T2.LT.0.)T2=T2+PI2                                                                                   
*     TANT=(SQRT(X3**2+Y3**2)-SQRT(X2**2+Y2**2))/(Z3-Z2)                                                      
*     SL=SQRT((X2-X3)**2+(Y2-Y3)**2)                                                                          
*     THET=ATAN(TANT)                                                                                         
*     DIFF=T2-T1                                                                                              
*     IF(DIFF.LT.-3.14)DIFF=DIFF+PI2                                                                          
*     IF(DIFF.GT.3.14)DIFF=DIFF-PI2                                                                           
*     PP=1000.                                                                                                
*     IF(DIFF.NE.0.0)PP=-0.0002998*12.*SL/(DIFF*SIN(THET))                                                    
*     PPP(LINK)=PP                                                                                            
C     CALL SHS(580,0,1./PP)                                                                                   
C     CALL SHS(581,0,1./PP)                                                                                   
C     CALL SHS(582,0,PP)                                                                                      
*     II=LINK                                                                                                 
*     PRINT 1001,II,LNK(1,II),LNK(2,II),LNK(3,II),CHIL(II)                                                    
 1001 FORMAT(' PJ12I',I3,2X,3I3,F10.2)                                  
 300  CONTINUE                                                          
 200  CONTINUE                                                          
 100  CONTINUE                                                          
                                                                        
C     NEW COMPARE SECTION                                                                                     
      LLL=LINK                                                          
      DO 500 LOOP=1,LLL                                                 
      CHB=100000.                                                       
      KB=0                                                              
C     SELECT BEST                                                                                             
      DO 510 K=1,LLL                                                    
      IF(CH(K).LT.0.0)GOTO 510                                          
*      IF(LNK(1,K).EQ.0)GOTO 510                                                                              
      IF(LNK(2,K).EQ.0)GOTO 510                                         
      IF(LNK(3,K).EQ.0)GOTO 510                                         
*      WRITE(*,*)' K ,CHI  ',K,CH(K),LNK(1,K),LNK(2,K),LNK(3,K)                                               
      IF(CH(K).LT.CHB)THEN                                              
      CHB=CH(K)                                                         
      KB=K                                                              
      ENDIF                                                             
 510  CONTINUE                                                          
*      WRITE(*,*)' KB,CHIB ',KB,CHB                                                                           
      IF(KB.EQ.0)GOTO 545                                               
C     COMPARE BEST WITH REMAINDER                                                                             
      DO 520 K=1,LLL                                                    
      IF(K.EQ.KB)GOTO520                                                
*      IF(LNK(1,K).EQ.0)GOTO520                                                                               
      IF(LNK(2,K).EQ.0)GOTO520                                          
      IF(LNK(3,K).EQ.0)GOTO520                                          
*      IF(LNK(1,K).EQ.LNK(1,KB))GOTO530                                                                       
      IF(LNK(2,K).EQ.LNK(2,KB))GOTO530                                  
      IF(LNK(3,K).EQ.LNK(3,KB))GOTO530                                  
      GOTO 520                                                          
C     REMOVE LINK                                                                                             
 530  LNK(1,K)=0                                                        
      LNK(2,K)=0                                                        
      LNK(3,K)=0                                                        
*      WRITE(*,*)' REMOVE ',K                                                                                 
 520  CONTINUE                                                          
C     COMPARE FINISHED , MARK BEST SEGMENT USED                                                               
      CH(KB)=-CH(KB)                                                    
 500  CONTINUE                                                          
C     RESET CHI WHEN COMPARE FINISHED                                                                         
 545  DO 540 LOOP=1,LLL                                                 
      IF(CH(LOOP).LT.0.0)CH(LOOP)=-CH(LOOP)                             
 540  CONTINUE                                                          
C                                                                                                             
C                                                                                                             
C                                                                                                             
      DO 550 I=LINKO+1,LINK                                             
      IF(LNK(2,I).EQ.0)GOTO550                                          
C     SET USED FLAG                                                                                           
C     IUS(LNK(1,I),1)=1                                                                                       
      IUS(LNK(2,I),2)=1                                                 
      IUS(LNK(3,I),3)=1                                                 
 1000 FORMAT(' PJN23',I3,1X,2(1X,12I2),F6.2,F7.2)                       
C     L1=LNK(1,I)                                                                                             
      L2=LNK(2,I)                                                       
      L3=LNK(3,I)                                                       
C     PRINT 1000,I,(IPT(III,L2,2),III=1,12)                                                                   
C    1,(IPT(III,L3,3),III=1,12)                                                                               
C    1,CHIL(I),PPP(I)                                                                                         
      NPP=NPP+1                                                         
      IF(NPP.GT.100) THEN                                               
        NPP = 100                                                       
        IEVSAT = 1                                                      
      ENDIF                                                             
      KLOC(NPP)=3                                                       
                                                                        
      LP(1,NPP)=00                                                      
      LP(2,NPP)=L2                                                      
      LP(3,NPP)=L3                                                      
      PSSS(NPP)=PSS(I)                                                  
      PISS(NPP)=PIS(I)                                                  
      RSSS(NPP)=RSS(I)                                                  
      RISS(NPP)=RIS(I)                                                  
      DO 551 KK=1,12                                                    
C     IPP(KK,NPP)=IPT(KK,L1,1)                                                                                
      IPP(KK+12,NPP)=IPT(KK,L2,2)                                       
      IPP(KK+24,NPP)=IPT(KK,L3,3)                                       
 551  CONTINUE                                                          
      CHPP(NPP)=CH(I)                                                   
      PROD=PROB(CH(I)*24.,24)                                           
      CALL SHS(577,0,PROD )                                                                            
      CALL SHS(550,0,6.001)                                                                            
      CALL SHS(550,0,10.001)                                                                           
 550  CONTINUE                                                          
                                                                        
      RETURN                                                            
      END                                                               
*