c      fit
c
       program  fit
       implicit none
c
       Integer i1(5), i2(5), i3(5), i, j, k, jmin
       character*30  fname
       real*8  lambda, pi, alat, energy
       real*8  tth(5), tths
       real*8  sigmamin
       REAL*8  ap(60), dd(60)
       real*8  sum, sumap, dif, dq
       real*8  rem, sigma, sigma2, vol
       real*8  rmin, alatmin, zero_min
c
        lambda=0.516598627d0
        pi=3.1415926535d0
        alat= 5.411102d0
c       tths: sin(th)
c       dd: (2sin(th)/lambda)^2
c
        sigmamin=1000.
c
C ***** WRITE THE SUM DATA ON FILE (ASCII CODE) *****
c       write(6,'(''   INPUT/ out FILE NAME/X:#######.###'')')
c        read(5,'(A30)') fname
c        write(*,*) fname
c        open(1,file=fname,status='old',ERR=999)

        DO 100 I=1, 5
         READ(1,*) i1(i), i2(i), i3(i), tth(i)
  100  CONTINUE

c       close(1)
c
        DO 200 I=1, 5
         tths = sin( tth(i) /2.0d0/180.d0*pi )
         ap(i)= real(i1(i)*i1(i) +i2(i)*i2(i) + i3(i)*i3(i))
         dd(i)= 2.0d0 * alat * tths
         dd(i)= dd(i)*dd(i)
  200  CONTINUE
c      Initialize
          sum = 0.
          sumap = 0.
       DO 300 I=1, 5
          sumap = sumap + ap(i)*ap(i)
          sum = sum + dd(i)*ap(i)
  300  CONTINUE
       lambda =sum/sumap
       lambda=sqrt(lambda)
       energy=12.398/lambda
c
       rem=0.
       DO 400 I=1, 5
          dq =  ap(i) /alat/ alat
          dif = dq - dd(i)
c         print *, dif
          rem = rem + dif*dif
  400  CONTINUE
c
          sigma2 = rem/sumap/4
          sigma = sigma2* alat*alat*alat*alat*alat*alat/4.0
          sigma = sqrt(sigma)
c          print 601, zero, sigma
c           print 600, j, lambda, sigma
c      print *, ' kmin=',kmin,' jmin=',jmin, ' lat= ',alatmin
       print *, lambda, energy
c       jmin, alatmin, rmin, sigmamin
       stop
c
 600   format(1x,E15.8,3(1x,E15.8))
 601   format(3(1x,E15.8))
 999   write(*,*) fname, ' File cannot be opened! '
       END
