	subroutine fit(qfit, xfit, khad, Du, Dd, Ds, Dc, Db, Dg)
        implicit real*8 (a-h, o-z)

	parameter (iqbin=77, ixbin=70)
        parameter (meqn=28, mxgrd=70)!actually need only 6*4, not 7*4

C	For the interpolations:
	real*8 q2bin(77), xbin(70)
	integer N, NA(2), NAq(1)
C	real A(400) ! for some reason fint is not behaving so using
		    ! linear interpolation
	real*8 Qfit, xfit, Q(1) ! data
	real*8 ffx(meqn*mxgrd)

	real*8 Fur(77, 70), Fdr(77, 70), Fsr(77, 70), Fcr(77, 70),
     1			Fbr(77, 70), Fgr(77, 70) ! rho
	real*8 Fuk(77, 70), Fdk(77, 70), Fsk(77, 70), Fck(77, 70),
     1			Fbk(77, 70), Fgk(77, 70) ! kstar
	real*8 Fuo(77, 70), Fdo(77, 70), Fso(77, 70), Fco(77, 70),
     1			Fbo(77, 70), Fgo(77, 70) ! omega
	real*8 Fup(77, 70), Fdp(77, 70), Fsp(77, 70), Fcp(77, 70),
     1			Fbp(77, 70), Fgp(77, 70) ! phi
	real*8 Fuq(77, 70), Fdq(77, 70), Fsq(77, 70),
     1       Fcq(77, 70), Fbq(77, 70), Fgq(77, 70) ! generic
	real*8 Fuq11(77, 70), Fdq11(77, 70), Fsq11(77, 70),
     1       Fcq11(77, 70), Fbq11(77, 70), Fgq11(77, 70) ! generic
	real*8 Fuq12(77, 70), Fdq12(77, 70), Fsq12(77, 70),
     1       Fcq12(77, 70), Fbq12(77, 70), Fgq12(77, 70) ! generic
	real*8 Fuq21(77, 70), Fdq21(77, 70), Fsq21(77, 70),
     1       Fcq21(77, 70), Fbq21(77, 70), Fgq21(77, 70) ! generic
	real*8 Fuq22(77, 70), Fdq22(77, 70), Fsq22(77, 70),
     1       Fcq22(77, 70), Fbq22(77, 70), Fgq22(77, 70) ! generic

 	common / bin /q2bin, xbin
	common / rhoff/  Fur, Fdr, Fsr, Fcr, Fbr, Fgr
	common / kstarff/  Fuk, Fdk, Fsk, Fck, Fbk, Fgk
	common / omegaff/  Fuo, Fdo, Fso, Fco, Fbo, Fgo
	common / phiff/  Fup, Fdp, Fsp, Fcp, Fbp, Fgp
	common / NAA/  NA

C	To use the tables to determine the fits to the data:
C	Using 1-d linear interpolation in Q2 to generate the
C	fragmentation functions ffx(x) and cubic spline 
C	to interpolate in x.

C	Suggest setting min of (x,Q2) = (10^-3, 1.6129) (charm)
C	Here mxgrd is the number of x values in the tables and meqn goes
C	over V,u,d,s,c,b,g=7 where V, valence, has not been computed
C	because observed data are for (h+hbar) hadrons.

C	For some reason fint is giving 0, so do linear interpol
C	Start the fit generation:
C	Let a1,a2,... be the tabulated values of x.
C	Let b1,b2,... be the tabulated values of y.
C	Let i and j be the subscripts for which ai≤x<ai+1, bj≤y <bj+1 .
C	Then compute:
C	t 	= 	(x-a)/(ai+1-a),
C	gj 	= 	(1-t)f(ai,bj)+t f(ai+1,bj),
C	gj+1 	= 	(1-t)f(ai,bj+1+t f(ai+1,bj+1),
C	u 	= 	(y-b)/(bj+1-b),
C	fappr 	= 	(1-u) gj+ u gj+1

C	Now interpolate for right q2 value
C	First locate q2_data in the q2bin of tables:
      	call locate(q2bin,iqbin,qfit,iiq)! binning from Numerical Recipes
	if (iiq.lt.1) print*, 'Q-data too low for table fits'
C	print*, 'q inter', iiq, qfit, q2bin(iiq), q2bin(iiq+1)
C	Now locate x_data in the xbin of tables:
      	call locate(xbin,ixbin,xfit,iix)! binning from Numerical Recipes
	if (iix.lt.1) print*, 'x-data too low for table fits'

C	Form the distances:
	tq = (qfit-q2bin(iiq))/(q2bin(iiq+1)-q2bin(iiq))
	ux = (xfit-xbin(iix))/(xbin(iix+1)-xbin(iix))
C	Use these differences to interpolate the ffs:
	if(khad.eq.1) then
	  Fuq11(iiq,iix) = Fur(iiq, iix)
	  Fdq11(iiq,iix) = Fdr(iiq, iix)
	  Fsq11(iiq,iix) = Fsr(iiq, iix)
	  Fcq11(iiq,iix) = Fcr(iiq, iix)
	  Fbq11(iiq,iix) = Fbr(iiq, iix)
	  Fgq11(iiq,iix) = Fgr(iiq, iix)
	  Fuq12(iiq,iix) = Fur(iiq, iix+1)
	  Fdq12(iiq,iix) = Fdr(iiq, iix+1)
	  Fsq12(iiq,iix) = Fsr(iiq, iix+1)
	  Fcq12(iiq,iix) = Fcr(iiq, iix+1)
	  Fbq12(iiq,iix) = Fbr(iiq, iix+1)
	  Fgq12(iiq,iix) = Fgr(iiq, iix+1)
	  Fuq21(iiq,iix) = Fur(iiq+1, iix)
	  Fdq21(iiq,iix) = Fdr(iiq+1, iix)
	  Fsq21(iiq,iix) = Fsr(iiq+1, iix)
	  Fcq21(iiq,iix) = Fcr(iiq+1, iix)
	  Fbq21(iiq,iix) = Fbr(iiq+1, iix)
	  Fgq21(iiq,iix) = Fgr(iiq+1, iix)
	  Fuq22(iiq,iix) = Fur(iiq+1, iix+1)
	  Fdq22(iiq,iix) = Fdr(iiq+1, iix+1)
	  Fsq22(iiq,iix) = Fsr(iiq+1, iix+1)
	  Fcq22(iiq,iix) = Fcr(iiq+1, iix+1)
	  Fbq22(iiq,iix) = Fbr(iiq+1, iix+1)
	  Fgq22(iiq,iix) = Fgr(iiq+1, iix+1)
C	  print*, khad, iiq, iix, Fgq22(iiq,iix), Fgr(iiq+1, iix+1)
	else if(khad.eq.2) then
	  Fuq11(iiq,iix) = Fuk(iiq, iix)
	  Fdq11(iiq,iix) = Fdk(iiq, iix)
	  Fsq11(iiq,iix) = Fsk(iiq, iix)
	  Fcq11(iiq,iix) = Fck(iiq, iix)
	  Fbq11(iiq,iix) = Fbk(iiq, iix)
	  Fgq11(iiq,iix) = Fgk(iiq, iix)
	  Fuq12(iiq,iix) = Fuk(iiq, iix+1)
	  Fdq12(iiq,iix) = Fdk(iiq, iix+1)
	  Fsq12(iiq,iix) = Fsk(iiq, iix+1)
	  Fcq12(iiq,iix) = Fck(iiq, iix+1)
	  Fbq12(iiq,iix) = Fbk(iiq, iix+1)
	  Fgq12(iiq,iix) = Fgk(iiq, iix+1)
	  Fuq21(iiq,iix) = Fuk(iiq+1, iix)
	  Fdq21(iiq,iix) = Fdk(iiq+1, iix)
	  Fsq21(iiq,iix) = Fsk(iiq+1, iix)
	  Fcq21(iiq,iix) = Fck(iiq+1, iix)
	  Fbq21(iiq,iix) = Fbk(iiq+1, iix)
	  Fgq21(iiq,iix) = Fgk(iiq+1, iix)
	  Fuq22(iiq,iix) = Fuk(iiq+1, iix+1)
	  Fdq22(iiq,iix) = Fdk(iiq+1, iix+1)
	  Fsq22(iiq,iix) = Fsk(iiq+1, iix+1)
	  Fcq22(iiq,iix) = Fck(iiq+1, iix+1)
	  Fbq22(iiq,iix) = Fbk(iiq+1, iix+1)
	  Fgq22(iiq,iix) = Fgk(iiq+1, iix+1)
	else if(khad.eq.3) then
	  Fuq11(iiq,iix) = Fuo(iiq, iix)
	  Fdq11(iiq,iix) = Fdo(iiq, iix)
	  Fsq11(iiq,iix) = Fso(iiq, iix)
	  Fcq11(iiq,iix) = Fco(iiq, iix)
	  Fbq11(iiq,iix) = Fbo(iiq, iix)
	  Fgq11(iiq,iix) = Fgo(iiq, iix)
	  Fuq12(iiq,iix) = Fuo(iiq, iix+1)
	  Fdq12(iiq,iix) = Fdo(iiq, iix+1)
	  Fsq12(iiq,iix) = Fso(iiq, iix+1)
	  Fcq12(iiq,iix) = Fco(iiq, iix+1)
	  Fbq12(iiq,iix) = Fbo(iiq, iix+1)
	  Fgq12(iiq,iix) = Fgo(iiq, iix+1)
	  Fuq21(iiq,iix) = Fuo(iiq+1, iix)
	  Fdq21(iiq,iix) = Fdo(iiq+1, iix)
	  Fsq21(iiq,iix) = Fso(iiq+1, iix)
	  Fcq21(iiq,iix) = Fco(iiq+1, iix)
	  Fbq21(iiq,iix) = Fbo(iiq+1, iix)
	  Fgq21(iiq,iix) = Fgo(iiq+1, iix)
	  Fuq22(iiq,iix) = Fuo(iiq+1, iix+1)
	  Fdq22(iiq,iix) = Fdo(iiq+1, iix+1)
	  Fsq22(iiq,iix) = Fso(iiq+1, iix+1)
	  Fcq22(iiq,iix) = Fco(iiq+1, iix+1)
	  Fbq22(iiq,iix) = Fbo(iiq+1, iix+1)
	  Fgq22(iiq,iix) = Fgo(iiq+1, iix+1)
	else if(khad.eq.4) then
	  Fuq11(iiq,iix) = Fup(iiq, iix)
	  Fdq11(iiq,iix) = Fdp(iiq, iix)
	  Fsq11(iiq,iix) = Fsp(iiq, iix)
	  Fcq11(iiq,iix) = Fcp(iiq, iix)
	  Fbq11(iiq,iix) = Fbp(iiq, iix)
	  Fgq11(iiq,iix) = Fgp(iiq, iix)
	  Fuq12(iiq,iix) = Fup(iiq, iix+1)
	  Fdq12(iiq,iix) = Fdp(iiq, iix+1)
	  Fsq12(iiq,iix) = Fsp(iiq, iix+1)
	  Fcq12(iiq,iix) = Fcp(iiq, iix+1)
	  Fbq12(iiq,iix) = Fbp(iiq, iix+1)
	  Fgq12(iiq,iix) = Fgp(iiq, iix+1)
	  Fuq21(iiq,iix) = Fup(iiq+1, iix)
	  Fdq21(iiq,iix) = Fdp(iiq+1, iix)
	  Fsq21(iiq,iix) = Fsp(iiq+1, iix)
	  Fcq21(iiq,iix) = Fcp(iiq+1, iix)
	  Fbq21(iiq,iix) = Fbp(iiq+1, iix)
	  Fgq21(iiq,iix) = Fgp(iiq+1, iix)
	  Fuq22(iiq,iix) = Fup(iiq+1, iix+1)
	  Fdq22(iiq,iix) = Fdp(iiq+1, iix+1)
	  Fsq22(iiq,iix) = Fsp(iiq+1, iix+1)
	  Fcq22(iiq,iix) = Fcp(iiq+1, iix+1)
	  Fbq22(iiq,iix) = Fbp(iiq+1, iix+1)
	  Fgq22(iiq,iix) = Fgp(iiq+1, iix+1)
	 end if

C	 Update the entries: retained the arrays in case want
C	 to return ALL x values.
	 gju 	= 	(1-tq)*Fuq11(iiq,iix)+tq*Fuq21(iiq,iix)
	 gj1u 	= 	(1-tq)*Fuq12(iiq,iix) +tq* Fuq22(iiq,iix)
	 Fuq(iiq,iix)= 	(1-ux)*gju+ ux*gj1u
	 print*, 't', tq, ux, gju,gj1u, Fuq(iiq, iix)
	 gjd 	= 	(1-tq)*Fdq11(iiq,iix)+tq*Fdq21(iiq,iix)
	 gj1d 	= 	(1-tq)*Fdq12(iiq,iix) +tq* Fdq22(iiq,iix)
	 Fdq(iiq,iix)= 	(1-ux)*gjd+ ux*gj1d
	 gjs 	= 	(1-tq)*Fsq11(iiq,iix)+tq*Fsq21(iiq,iix)
	 gj1s 	= 	(1-tq)*Fsq12(iiq,iix) +tq* Fsq22(iiq,iix)
	 Fsq(iiq,iix)= 	(1-ux)*gjs+ ux*gj1s
	 gjc 	= 	(1-tq)*Fcq11(iiq,iix)+tq*Fcq21(iiq,iix)
	 gj1c 	= 	(1-tq)*Fcq12(iiq,iix) +tq* Fcq22(iiq,iix)
	 Fcq(iiq,iix)= 	(1-ux)*gjc+ ux*gj1c
	 gjb 	= 	(1-tq)*Fbq11(iiq,iix)+tq*Fbq21(iiq,iix)
	 gj1b 	= 	(1-tq)*Fbq12(iiq,iix) +tq* Fbq22(iiq,iix)
	 Fbq(iiq,iix)= 	(1-ux)*gjb+ ux*gj1b
	 gjg 	= 	(1-tq)*Fgq11(iiq,iix)+tq*Fgq21(iiq,iix)
	 gj1g 	= 	(1-tq)*Fgq12(iiq,iix) +tq* Fgq22(iiq,iix)
	 Fgq(iiq,iix)= 	(1-ux)*gjg+ ux*gj1g

	 Du=Fuq(iiq, iix)
	 Dd=Fdq(iiq, iix)
	 Ds=Fsq(iiq, iix)
	 Dc=Fcq(iiq, iix)
	 Db=Fbq(iiq, iix)
	 Dg=Fgq(iiq, iix)
	 print*, khad, qfit, xfit, Du, Dd, Ds, Dc, Db, Dg

      return
      end

      SUBROUTINE locate(xx,n,x,j) ! binning from Numerical Recipes
      INTEGER j,n
      REAL*8 x,xx(n)
      INTEGER jl,jm,ju
      jl=0
      ju=n+1
10    if(ju-jl.gt.1)then
        jm=(ju+jl)/2
        if((xx(n).gt.xx(1)).eqv.(x.gt.xx(jm)))then
          jl=jm
        else
          ju=jm
        endif
      goto 10
      endif
      j=jl
      return
      END
C  (C) Copr. 1986-92 Numerical Recipes Software '$@%.


