    PROGRAM g04cafe

!      G04CAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g04caf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, inter, irdf, itotal, k, l, &
                                          maxt, mterm, n, nblock, nfac, ntreat
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: bmean(:), e(:), r(:), semean(:),     &
                                          table(:,:), tmean(:), y(:)
       INTEGER, ALLOCATABLE            :: imean(:), iwk(:), lfac(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G04CAF Example Program Results'
       WRITE (nout,*)

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

!      Read in problem size
       READ (nin,*) n, nblock, nfac, inter

       ALLOCATE (lfac(nfac),iwk(n+3*nfac),y(n),bmean(nblock+1),r(n))

!      Read in the number of levels for each factor
       READ (nin,*) lfac(1:nfac)

!      Read in the observations
       READ (nin,*) y(1:n)

!      Use standard degrees of freedom
       irdf = 0

!      Using call to G04CAF to calculate required values for MAXT and MTERM ...

!      Setting MAXT to zero ensures it is too small and hence the routine
!      will calculate its correct size, IMEAN needs to be at least 1 element
!      long so set MTERM to 1.
       maxt = 0
       mterm = 1

!      Dummy allocation
       ALLOCATE (tmean(maxt),e(maxt),table(mterm,5),semean(mterm), &
          imean(mterm))

!      Call the routine initially to get MTERM and MAXT
       ifail = 1
       CALL g04caf(n,y,nfac,lfac,nblock,inter,irdf,mterm,table,itotal,tmean, &
          maxt,e,imean,semean,bmean,r,iwk,ifail)
       IF (ifail/=0 .AND. ifail/=2) THEN
          WRITE (nout,99996) ' ** G04CAF exited with IFAIL = ', ifail
          GO TO 20
       END IF

!      Allocate remaining output arrays
       mterm = itotal
       maxt = imean(1)
       DEALLOCATE (tmean,e,table,semean,imean)
       ALLOCATE (tmean(maxt),e(maxt),table(mterm,5),semean(mterm), &
          imean(mterm))

!      Calculate the ANOVA table
       ifail = 0
       CALL g04caf(n,y,nfac,lfac,nblock,inter,irdf,mterm,table,itotal,tmean, &
          maxt,e,imean,semean,bmean,r,iwk,ifail)

!      Display results
       WRITE (nout,*) ' ANOVA table'
       WRITE (nout,*)
       WRITE (nout,*) '  Source      df         SS          MS          F', &
          '        Prob'
       WRITE (nout,*)
       k = 0
       IF (nblock>1) THEN
          k = k + 1
          WRITE (nout,99998) ' Blocks    ', table(1,1:5)
       END IF
       ntreat = itotal - 2 - k
       DO i = 1, ntreat
          WRITE (nout,99997) ' Effect  ', i, table(k+i,1:5)
       END DO
       WRITE (nout,99998) ' Residual  ', table(itotal-1,1:3)
       WRITE (nout,99998) ' Total     ', table(itotal,1:2)
       WRITE (nout,*)
       WRITE (nout,*) ' Treatment Means and Standard Errors'
       WRITE (nout,*)
       k = 1
       DO i = 1, ntreat
          l = imean(i)
          WRITE (nout,99996) ' Effect ', i
          WRITE (nout,*)
          WRITE (nout,99999) tmean(k:l)
          WRITE (nout,*)
          WRITE (nout,99995) ' SE of difference in means  = ', semean(i)
          WRITE (nout,*)
          k = l + 1
       END DO

20     CONTINUE

99999  FORMAT (8F10.2)
99998  FORMAT (A,3X,F3.0,2X,2(F10.0,2X),F10.3,2X,F9.4)
99997  FORMAT (A,I2,3X,F3.0,2X,2(F10.0,2X),F10.3,2X,F9.4)
99996  FORMAT (A,I5)
99995  FORMAT (A,F10.2)
    END PROGRAM g04cafe
