
C	help routine for modinvit
C	Sven Ivansson 1992




	SUBROUTINE FWYSMAX (IDMAX,ICMAX, DYSMAX,YSMAX)
C	to do "final scaling"

	USE INMODELa
	USE FWYSa

	IMPLICIT NONE

	INCLUDE '../RPPARAMETER.INC'
C       nol <= noldim

	INTEGER IDMAX,ICMAX
	REAL*8 DYSMAX
	COMPLEX*16 YSMAX

	INTEGER ID, ID1,ID2
	REAL*8 SLASK, SLASK1,SLASK2,SLASK3,SLASK4

	IDMAX = 0
	ICMAX = 0
	DYSMAX = 0.0D0
	IF (NSW.GT.0) THEN
	  IF ( NSS.GT.0 .OR. ISOLID(0).NE.0 ) THEN
	    DO ID = 0, NSS
	      SLASK1 = DREAL(YS1FW(ID))*DREAL(YS1FW(ID)) + 
     $	      DIMAG(YS1FW(ID))*DIMAG(YS1FW(ID))
	      SLASK2 = DREAL(YS2FW(ID))*DREAL(YS2FW(ID)) + 
     $	      DIMAG(YS2FW(ID))*DIMAG(YS2FW(ID))
	      SLASK3 = DREAL(YS3FW(ID))*DREAL(YS3FW(ID)) + 
     $	      DIMAG(YS3FW(ID))*DIMAG(YS3FW(ID))
	      SLASK4 = DREAL(YS4FW(ID))*DREAL(YS4FW(ID)) + 
     $	      DIMAG(YS4FW(ID))*DIMAG(YS4FW(ID))
	      SLASK = DMAX1(SLASK1,SLASK2,SLASK3,SLASK4)
	      IF (SLASK.GT.DYSMAX) THEN
	        DYSMAX = SLASK
	        IDMAX = ID
	        IF (SLASK1.GE.SLASK2) THEN
	          SLASK = SLASK1
	          ICMAX = 1
	        ELSE
	          SLASK = SLASK2
	          ICMAX = 2
	        END IF
	        IF (SLASK3.GT.SLASK) THEN
	          SLASK = SLASK3
	          ICMAX = 3
	        END IF
	        IF (SLASK4.GT.SLASK) THEN
	          ICMAX = 4
	        END IF
	      END IF
	    END DO
	    ID1 = NSS+1
	  ELSE
	    ID1 = 0
	  END IF
	ELSE
	  ID1 = 1
	END IF
	IF ( (NSW+1).LT.NOL .OR. ISOLID(NOL).NE.0 ) THEN
	  DO ID = NSW, NOL-1
	    SLASK1 = DREAL(YS1FW(ID))*DREAL(YS1FW(ID)) + 
     $	    DIMAG(YS1FW(ID))*DIMAG(YS1FW(ID))
	    SLASK2 = DREAL(YS2FW(ID))*DREAL(YS2FW(ID)) + 
     $	    DIMAG(YS2FW(ID))*DIMAG(YS2FW(ID))
	    SLASK3 = DREAL(YS3FW(ID))*DREAL(YS3FW(ID)) + 
     $	    DIMAG(YS3FW(ID))*DIMAG(YS3FW(ID))
	    SLASK4 = DREAL(YS4FW(ID))*DREAL(YS4FW(ID)) + 
     $	    DIMAG(YS4FW(ID))*DIMAG(YS4FW(ID))
	    SLASK = DMAX1(SLASK1,SLASK2,SLASK3,SLASK4)
	    IF (SLASK.GT.DYSMAX) THEN
	      DYSMAX = SLASK
	      IDMAX = ID
	      IF (SLASK1.GE.SLASK2) THEN
	        SLASK = SLASK1
	        ICMAX = 1
	      ELSE
	        SLASK = SLASK2
	        ICMAX = 2
	      END IF
	      IF (SLASK3.GT.SLASK) THEN
	        SLASK = SLASK3
	        ICMAX = 3
	      END IF
	      IF (SLASK4.GT.SLASK) THEN
	        ICMAX = 4
	      END IF
	    END IF
	  END DO
	  ID2 = NSW-1
	ELSE
	  ID2 = NSW
	END IF
	IF (ID1.LE.ID2) THEN
	  DO ID = ID1, ID2
	    SLASK2 = DREAL(YS2FW(ID))*DREAL(YS2FW(ID)) + 
     $	    DIMAG(YS2FW(ID))*DIMAG(YS2FW(ID))
	    SLASK4 = DREAL(YS4FW(ID))*DREAL(YS4FW(ID)) + 
     $	    DIMAG(YS4FW(ID))*DIMAG(YS4FW(ID))
	    SLASK = DMAX1(SLASK2,SLASK4)
	    IF (SLASK.GT.DYSMAX) THEN
	      DYSMAX = SLASK
	      IDMAX = ID
	      IF (SLASK2.GE.SLASK4) THEN
	        ICMAX = 2
	      ELSE
	        ICMAX = 4
	      END IF
	    END IF
	  END DO
	END IF

	IF (DYSMAX.GT.0.0D0) THEN
	  DYSMAX = DSQRT(DYSMAX)
	  IF (ICMAX.EQ.1) THEN
	    YSMAX = YS1FW(IDMAX)
	  ELSE IF (ICMAX.EQ.2) THEN
	    YSMAX = YS2FW(IDMAX)
	  ELSE IF (ICMAX.EQ.3) THEN
	    YSMAX = YS3FW(IDMAX)
	  ELSE IF (ICMAX.EQ.4) THEN
	    YSMAX = YS4FW(IDMAX)
	  ELSE
	    write(*,*) dysmax,icmax,idmax
	    STOP 3416
	  END IF
	ELSE
	  YSMAX = (0.0D0,0.0D0)
	END IF

	RETURN
	END
