    PROGRAM e02cafe

!      E02CAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : e02caf, e02cbf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: ymax
       INTEGER                         :: i, ifail, inuxp1, inuyp1, j, k, l,   &
                                          mi, mtot, n, na, nwork, r, t
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:), f(:), ff(:), nux(:), nuy(:),   &
                                          w(:), work(:), x(:), xmax(:),        &
                                          xmin(:), y(:)
       INTEGER, ALLOCATABLE            :: m(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max, sum
!      .. Executable Statements ..
       WRITE (nout,*) 'E02CAF Example Program Results'

!      Skip heading in data file
       READ (nin,*)

!      Input the number of lines Y = Y(I) on which data is given,
!      and the required degree of fit in the X and Y directions

       READ (nin,*) n, k, l
       inuxp1 = 1
       inuyp1 = 1
       na = (k+1)*(l+1)
       ALLOCATE (a(na),m(n),y(n),xmin(n),xmax(n),nux(inuxp1),nuy(inuyp1))

!      Input Y(I), the number of data points on Y = Y(I) and the
!      range of X-values on this line, for I = 1,2,...N

       DO i = 1, n
          READ (nin,*) y(i), m(i), xmin(i), xmax(i)
       END DO

       mtot = sum(m(1:n))
       nwork = 3*mtot + 2*n*(k+2) + 5*(1+max(k,l))
       ALLOCATE (x(mtot),f(mtot),w(mtot),ff(mtot),work(nwork))

!      Input the X-values and function values, F, together with
!      their weights, W.

       READ (nin,*) (x(i),f(i),w(i),i=1,mtot)

!      Evaluate the coefficients, A, of the fit to this set of data

       ifail = 0
       CALL e02caf(m,n,k,l,x,y,f,w,mtot,a,na,xmin,xmax,nux,inuxp1,nuy,inuyp1, &
          work,nwork,ifail)

       mi = 0

       WRITE (nout,*)
       WRITE (nout,*) &
          '     Data Y     Data X     Data F   Fitted F   Residual'
       WRITE (nout,*)

       DO r = 1, n
          t = mi + 1
          mi = mi + m(r)
          ymax = y(n)

          IF (n==1) THEN
             ymax = ymax + 1.0E0_nag_wp
          END IF

!         Evaluate the fitted polynomial at each of the data points
!         on the line Y = Y(R)

          ifail = 0
          CALL e02cbf(t,mi,k,l,x,xmin(r),xmax(r),y(r),y(1),ymax,ff,a,na,work, &
             nwork,ifail)

!         Output the data and fitted values on the line Y = Y(R)

          DO i = t, mi
             WRITE (nout,99999) y(r), x(i), f(i), ff(i), ff(i) - f(i)
          END DO

          WRITE (nout,*)
       END DO

!      Output the Chebyshev coefficients of the fit

       WRITE (nout,*) 'Chebyshev coefficients of the fit'
       WRITE (nout,*)

       DO j = 1, k + 1
          WRITE (nout,99998) (a(i),i=1+(j-1)*(l+1),j*(l+1))
       END DO

99999  FORMAT (3X,4F11.4,E11.2)
99998  FORMAT (1X,6F11.4)
    END PROGRAM e02cafe
