    PROGRAM g10acfe

!      G10ACF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g10acf, g10zaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: crit, df, rho, rss, tol, u
       INTEGER                         :: i, ifail, ldc, lwk, lwt, maxcal, n,  &
                                          nord
       CHARACTER (1)                   :: method, weight
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: c(:,:), h(:), res(:), wk(:), wt(:),  &
                                          wwt(:), x(:), xord(:), y(:),         &
                                          yhat(:), yord(:)
       INTEGER, ALLOCATABLE            :: iwrk(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G10ACF Example Program Results'
       WRITE (nout,*)

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

       IF (weight=='W' .OR. weight=='w') THEN
          lwt = n
       ELSE
          lwt = 0
       END IF
       ldc = n - 1
       lwk = 7*(n+2)
       ALLOCATE (x(n),y(n),wt(lwt),xord(n),yord(n),wwt(n),yhat(n),c(ldc,3), &
          res(n),h(n),wk(lwk),iwrk(n))

!      Read in data
       IF (lwt>0) THEN
          READ (nin,*) (x(i),y(i),wt(i),i=1,n)
       ELSE
          READ (nin,*) (x(i),y(i),i=1,n)
       END IF

!      Read in control parameters
       READ (nin,*) u, tol, maxcal, crit

!      Sort data, removing ties and weighting accordingly
       ifail = 0
       CALL g10zaf(weight,n,x,y,wt,nord,xord,yord,wwt,rss,iwrk,ifail)

!      Fit cubic spline
       ifail = 0
       CALL g10acf(method,'W',nord,xord,yord,wwt,yhat,c,ldc,rss,df,res,h,crit, &
          rho,u,tol,maxcal,wk,ifail)

!      Display results
       WRITE (nout,99999) 'Residual sum of squares = ', rss
       WRITE (nout,99999) 'Degrees of freedom = ', df
       WRITE (nout,99999) 'RHO = ', rho
       WRITE (nout,*)
       WRITE (nout,*) '     Input data                Output results'
       WRITE (nout,*) '   I    X       Y            YHAT      H'
       WRITE (nout,99998) (i,xord(i),yord(i),yhat(i),h(i),i=1,nord)

99999  FORMAT (1X,A,F10.2)
99998  FORMAT (I4,2F8.3,6X,2F8.3)
    END PROGRAM g10acfe
