      program linfit_main

      implicit none

      integer*4 jds(100)
      real*4 x(100), y(100), dy(100), w(100)
      real*4 xx, yy, dyy, chisq
      integer*4 i, ids, npts
      real*4 Rise, Run, RR
      real*4 p(2)
      character*24 fname

      open (7, file='ANSWER')
      rewind (7)
      write (7,*) ' '
      write (7,*) '      LINEAR MINIMUM-CHISQUARED FITTING'
      write (7,*) '      ================================='
      write (*,*) ' '
      write (*,*) '      LINEAR MINIMUM-CHISQUARED FITTING'
      write (*,*) '      ================================='
      write (*,*) ' '

      write (*,1111) 
 1111 format('Enter the name of the file containing the data: ',$)
      read (*,*) fname

      open (2, file=fname)

      write (7,*) ' '
      write (7,*) 'READING IN FILE ',fname,':'
      write (7,*) ' '
      write (*,*) ' '
      write (*,*) 'READING IN FILE ',fname,':'
      write (*,*) ' '

      do i=1,10
         read (2,*,end=1000) ids, xx, yy, dyy
         write (7,*) ids, xx, yy, dyy
         write (*,*) ids, xx, yy, dyy
         jds(i) = ids
         x(i) = xx
         y(i) = yy
         dy(i) = dyy
         w(i) = 1/(dyy*dyy)
      end do

 1000 npts = i - 1
      write (7,*) ' '
      write (7,*) 'Read ',npts,' rows of data.'
      write (7,*) ' '
      write (*,*) ' '
      write (*,*) 'Read ',npts,' rows of data.'
      write (*,*) ' '

      Rise = y(npts) - y(1)
      Run = x(npts) - x(1)
      RR = Rise/Run
C      write (*,*) "Rise/Run = ",RR

C------------------------------------------
      call linfit (npts, x, y, w, p, chisq )
C------------------------------------------

      write (*,*) ' '
      write (*,*) '      BEST FIT:'
      write (*,*) ' '
      write (*,*) 'Chi squared = ',chisq
      write (*,*) ' '
      write (*,*) 'Intercept p(1) = ',p(1)
      write (*,*) '   Slope p(2) = ',p(2)
      write (*,*) ' '
      write (*,*) '(Compare Rise/Run = ',RR,')'
      write (*,*) ' '

      write (7,*) ' '
      write (7,*) '      BEST FIT:'
      write (7,*) ' '
      write (7,*) 'Chi squared = ',chisq
      write (7,*) ' '
      write (7,*) 'Intercept p(1) = ',p(1)
      write (7,*) '    Slope p(2) = ',p(2)
      write (7,*) ' '
      write (7,*) '(Compare Rise/Run = ',RR,')'
      write (7,*) ' '

      close (7)

      stop
      end
