c     cal betas at id 
c I changed the definition of flq as being the full quad length. L.Emery.
		implicit real*8 (a-h,o-z)
		real*4 ran

		character*4 plane
		integer*4 verbose, solutions, notConverged, nonPhysical
		real*8 betaBQ1,errorBQ1,knlBQ1,betaAQ1,errorAQ1,knlAQ1
      common /cmdLineArgs/betaBQ1,errorBQ1,knlBQ1,betaAQ1,errorAQ1,knlAQ1,plane,verbose
      integer n, argc
      character*128 argv

      n = iargc()
		if (n.eq.0) then
         call readCmdLineArgs(n, '')
		endif
			
		betaBQ1 = 0.0
		betaAQ1 = 0.0
      do i = 1, n
         call getarg( i, argv)
c     can only send one argument at a time.
			argc = 1
         call readCmdLineArgs(argc, argv)
      enddo
		if (betaAQ1.eq.0) then
			write(*,*) 'Invalid beta for AQ1.'
			stop
		endif
		if (betaBQ1.eq.0) then
			write(*,*) 'Invalid beta for BQ1.'
			stop
		endif
		if (knlBQ1.lt.0) then
			write(*,*) 'knlBQ1 must be >=0.'
			stop
		endif
		if (knlAQ1.lt.0) then
			write(*,*) 'knlAQ1 must be >=0.'
			stop
		endif
		if (plane(1:1) .eq. '') then
			write(*,*) 'No plane specified.'
			stop
		endif

		flq = .50
		fkbq1 = knlBQ1/ flq
		fkaq1 = knlAQ1/ flq
		fkbq1=dsqrt(fkbq1)
		fkaq1=dsqrt(fkaq1)
c		transport through half the quad
		x = fkbq1 * flq/ 2d0
		if ( x .ne. 0 ) then
			if (plane(1:1) .eq. 'x') then
				a11=dcosh(x)
				a12=dsinh(x)/fkbq1
				a21=a12*fkbq1**2
			else
				a11=dcos(x)
				a12=dsin(x)/fkbq1
				a21=-a12*fkbq1**2
			endif
		else
			a11 = 1d0
			a12 = flq/ 2d0
			a21 = 0d0
		endif
c		transport through half the quad
		x = fkaq1 * flq/ 2d0
		if (plane(1:1) .eq. 'x') then
			if ( x .ne. 0 ) then
				b11=dcosh(x)
				b12=dsinh(x)/fkaq1
				b21=b12*fkaq1**2
			else
				b11 = 1d0
				b12 = flq/2d0
				b21 = 0d0
			endif				
		else if ( plane(1:1) .eq. 'y' ) then
			if ( x .ne. 0 ) then
				b11=dcos(x)
				b12=dsin(x)/fkaq1
				b21=-b12*fkaq1**2
			else
				b11 = 1d0
				b12 = flq/2d0
				b21 = 0d0
			endif
		endif
c		ss = 2 * 3.363249859486100
		ss = 2 * (3.61 - flq/2d0)
c     write(6,*) ' input betax at B q1 and betax at A q1'
c     write(6,*) ' 16.8791,16.8791 for test'
c     read(5,*) b0b,b3b
c     bob=ave betax at bq1
c     b3b=ave betax at aq1
c     bo ao =betax,alphax at center of bq1
c     b1 a1 =betax,alphax at exit of bq1
c     b2 a2 =betax,alphax at entrance of aq1
c     b3,a3 =betax,alphax at center of aq1
		b0b0 = betaBQ1
		b3b0 = betaAQ1
		err0 = errorBQ1
		err3 = errorAQ1
c     FIRST GUESS FOR B1,B2
		b1=b0b0
		b2=b3b0
		b1l=b1
		is=052898
		itotal=500
		if((err0.eq.0.).and.(err3.eq.0.)) itotal=1
		bave=0.
		brms=0.
		aave=0.
		arms=0.
c if error estimation is requested, loop over 500 randoms numbers.
		solutions = 0
		notConverged = 0
		nonPhysical = 0
		do i=1,itotal
			run=0.
			do j=1,10
				fnorm=ran(is)
				run=run+fnorm
			enddo
			b0b=b0b0+err0*(run-5.)
			run=0.
			do j=1,10
				fnorm=ran(is)
				run=run+fnorm
			enddo
			b3b=b3b0+err0*(run-5.)
c			write(*,*) 'b0b,b3b ',b0b,b3b
c start of convergence loops
			itt=0
			ittLimit = 100
 10		continue
			check=b1*b2-ss**2
			check1 = check
			if(check.lt.0) then
				check1=0.
			endif
			a1=(b1-dsqrt(check1))/ss
C     SOLVE FOR A0 AS FUNCTION OF B1,A1
			a0=b1*a11*a21+a1*(1.+2.*a12*a21)+(1.+a1**2)/b1*a12*a11
C     SOLVE FOR BO AS FUNCTION OF A0,B0B
			if ( b0b**2-a12**2*(1.+a11**2)*(1.+a0**2) .lt. 0) then
				write(*,*) 'Problem with sqrt argument b0b**2-a12**2*(1.+a11**2)*(1.+a0**2)', b0b**2-a12**2*(1.+a11**2)*(1.+a0**2)
			endif
			b0=(b0b+dsqrt(b0b**2-a12**2*(1.+a11**2)*(1.+a0**2)))/(1.+a11**2)
c     SOLVE FOR B1 AS FUNCTION OF B0,A0
			b1=((b0*a11-a0*a12)**2+a12**2)/b0
			itt=itt+1
C     CHECK FOR CONVERGENCE
			diff=(b1-b1l)**2
			if((diff.lt.1.d-10).or.(itt.gt.ittLimit)) goto 20
			b1l=b1
C     SOLVE FOR A2 AS FUNCTION OF B1,A1
			a2=a1-(1.+a1**2)*ss/b1
C     SOLVE FOR A3 AS FUNCYION OF B2,A2
			a3=-b2*B11*b21+a2*(1.+2.*b12*b21)-(1.+a2**2)/b2*b12*b11
C     SOLVE FOR B3 AS FUNCTION OF B3B,A3
			if ( b3b**2-b12**2*(1.+b11**2)*(1.+a3**2) .lt. 0 ) then
				write(*,*) 'Problem with sqrt argumentb3b**2-b12**2*(1.+b11**2)*(1.+a3**2) ', b3b**2-b12**2*(1.+b11**2)*(1.+a3**2)
			endif
			b3=(b3b+dsqrt(b3b**2-b12**2*(1.+b11**2)*(1.+a3**2)))/(1.+b11**2)
C     SOLVE FOR B2 AS FUNCTION OF B3,A3
			b2=((b3*b11+a3*b12)**2+b12**2)/b3
c			write(*,*) 'b0, a0: ', b0, a0
c			write(*,*) 'b1, a1: ', b1, a1
c			write(*,*) 'b2, a2: ', b2, a2
c			write(*,*) 'b3, a3: ', b3, a3
c			write(*,*)
			goto 10
 20		continue
			if (itt. gt. ittLimit) then
				notConverged = notConverged + 1
			endif
			if (check .lt. 0.0) then
				nonphysical = nonphysical + 1
			endif
			if (itt .le. ittLimit .and. check .ge. 0.0) then
C     SOLUTION HAS CONVERGED
				solutions = solutions + 1
				bid=((b1-a1*ss/2.)**2+(ss/2.)**2)/b1
				aid=a1-(1.+a1**2)*ss/b1/2.
				bave=bave+bid
				brms=brms+bid**2
				aave=aave+aid
				arms=arms+aid**2
			endif
		enddo
		if ( solutions .eq. 0 ) then
			write(6,*) 'There were no physical solutions.'
			stop
		else
			ffg=dfloat(solutions)
			betaave=bave/ffg
			temp=brms/ffg-betaave**2
			if(temp.lt.0.) temp=0.
			betarms=dsqrt(temp)
			alphave=aave/ffg
			temp=arms/ffg-alphave**2
			if(temp.lt.0.) temp=0.
			alphrms=dsqrt(temp)
		endif
		if (nonphysical .gt. 0) then
			write(6,*) 'Non-physical solutions: ', nonphysical, '(not used in average).'
		endif
		if (notconverged .gt. 0) then
			write(6,*) 'Instances on non-convergence: ', notConverged
		endif
		write (6,*) solutions, ' valid solutions out of ', itotal, '.'
		write(6,*) 'beta: ', betaave,betarms
		write(6,*) 'alpha: ', alphave,alphrms
		stop
		end
