C   13/10/83 310222221  MEMBER NAME  MUENDP   (JADEMUS)     FORTRAN
C
C LAST CHANGE 22.00 22/10/83 HUGH MCCANN .
C             10.00 14/10/83 HUGH MCCANN .
C
      SUBROUTINE MUENDP(JCEPT,IRONRG,FACSTP)
C
C        THIS ROUTINE ENSURES THAT TRACKS 'SEE' THE REAL THICKNESS
C        OF THE YOKE END PLATES. THEY ARE SET TO BE 340 MM THICK IN
C        MUREG, BUT IN FACT THE THICKNESS VARIES WITH THE DISTANCE
C        FROM THE BEAM LINE AS FOLLOWS :
C        (A)
C                   RADIUS < INNER RADIUS OF COIL , T = 340 MM
C                             ( RCOILI = 968 MM )
C            THIS COINCIDES WITH THE DEFINITION OF'END PLUG' IN MUFFLE
C            AND USES THE SAME FICTITIOUS THICKNESS AS IS USED THERE.
C            ( SEE BLOCK DATA FOR CMUFFL ).
C
C        (B)
C        RCOILI <   RADIUS <   END PLUG COLLAR OUTER RADIUS , T = 290 ;
C                               (  FLOAT(IREP4) = 1400. MM )
C           THIS IS ALSO A FICTITOUS THICKNESS, FOR THE SAME REASON AS
C           ABOVE. THE REAL THICKNESS IS 340 MM. THE POSSIBILITY THAT
C           THE TRACK MIGHT LEAVE THE IRON THRO' THE CYLINDER
C           X**2+Y**2=(COLLAR OUTER RADIUS) IS ALSO TAKEN INTO ACCOUNT.
C
C        (C)
C        1400.  <   RADIUS   , T = 210 MM  .  AT LAST , A REAL THICKNESS
C                                             VIZ. ACTUAL YOKE END PLATE
C
C
      IMPLICIT INTEGER*2 (H)
C
#include "cmuffl.for"
#include "cmufwork.for"
#include "cmureg.for"
#include "cmucalib.for"
      DATA THICK/290./
      COLLAR=FLOAT(IREP4)
      ZLIMEP=FLOAT(IZOEP5)
C
      FACSTP=1.
      ZENTER=ZRLO(IRONRG)
      IF(HRFACE(IRONRG).EQ.5)  ZENTER=ABS(ZRHI(IRONRG))
C
C       IF THE CURRENT STEP BRINGS THE TOTAL Z TRAVERSAL OF THE END
C       PLATE TO LESS THAN THE SMALLEST THICKNESS ( THE OUTER LIMIT OF
C       WHICH IS ZLIMEP ) , THEN NO MODIFICATION OF THE STEP
C       IS NEEDED.  I.E. GO TO 100 WITH FACSTP = 1.   .
C
      IF(  ABS(CINT(3,JCEPT)).LT.ZLIMEP  )   GO TO 100
C
C              CHECK THAT THE STARTING POINT OF THE STEP IS IN IRON.
           RSTART=SQRT( X**2 + Y**2 )
           IF( RSTART.LT.RCOILI )  GO TO 20
               IF( RSTART.GT.COLLAR )  GO TO 10
C                    IN THE COLLAR REGION.
                   IF( ABS(Z).LT.(ZENTER+THICK) )  GO TO 20
                       FACSTP=0.
                       GO TO 100
C                   BARE END PLATE REGION.
   10          IF( ABS(Z).LT.ZLIMEP )  GO TO 20
                   FACSTP=0.
                   GO TO 100
C
C               THE STARTING POINT IS IN IRON.  NOW CHECK END POINT.
   20      REND=SQRT(   CINT(1,JCEPT)**2  +  CINT(2,JCEPT)**2   )
           IF( REND.LT.RCOILI )  GO TO 100
C                 DISTANCE ALONG TRACK FROM CURRENT POSITION (X,Y,Z)
C                 TO EXIT FROM THE IRON IS ESTEP.
C                 IF DSTEP </= ESTEP , NO MODIFICATION NEEDED.
               IF( REND.GT.COLLAR )  GO TO 40
                  ESTEP= (ZENTER+THICK-ABS(Z))/ABS(DCZ)
                  GO TO 80
   40          ESTEP= (ZLIMEP-ABS(Z))/ABS(DCZ)
C                 IT IS POSSIBLE THAT THE TRACK WILL ACTUALLY LEAVE
C                 THE IRON THRO' THE CYLINDER X**2 + Y**2 = COLLAR.
               CSTEP=COLLAR/SQRT(1.00001-DCZ**2) - SQRT(X**2+Y**2+Z**2)
               IF(CSTEP.GT.ESTEP)  ESTEP=CSTEP
C
   80          IF( ESTEP.LT.DSTEP )  FACSTP=ESTEP/DSTEP
C
  100 RETURN
      END
