    PROGRAM g04bcfe

!      G04BCF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g04bcf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: ldtabl = 6, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: gmean, tol
       INTEGER                         :: ifail, irdf, ldc, lit, n, ncol,      &
                                          nrep, nrow, nt
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: c(:,:), cmean(:), ef(:), r(:),       &
                                          rmean(:), rpmean(:), tmean(:),       &
                                          wk(:), y(:)
       REAL (KIND=nag_wp)              :: tabl(ldtabl,5)
       INTEGER, ALLOCATABLE            :: irep(:), it(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G04BCF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       READ (nin,*) nrep, nrow, ncol, nt

       n = nrep*nrow*ncol
       IF (nt>1) THEN
          lit = n
       ELSE
          lit = 1
       END IF
       ldc = nt
       ALLOCATE (irep(nt),y(n),tmean(nt),it(lit),c(ldc,nt),rpmean(nrep), &
          rmean(nrep*nrow),cmean(nrep*ncol),r(n),ef(nt),wk(3*nt))

!      Read in the data
       READ (nin,*) y(1:n)
       IF (nt>1) THEN
          READ (nin,*) it(1:n)
       END IF

!      Use default tolerance
       tol = 0.0E0_nag_wp

!      Use standard degrees of freedom
       irdf = 0

!      Calculate the ANOVA table
       ifail = 0
       CALL g04bcf(nrep,nrow,ncol,y,nt,it,gmean,tmean,tabl,ldtabl,c,ldc,irep, &
          rpmean,rmean,cmean,r,ef,tol,irdf,wk,ifail)

!      Display results
       WRITE (nout,*) ' ANOVA TABLE'
       WRITE (nout,*)
       IF (nrep>1) THEN
          WRITE (nout,99998) ' Reps        ', tabl(1,1:5)
       END IF
       WRITE (nout,99998) ' Rows        ', tabl(2,1:5)
       WRITE (nout,99998) ' Columns     ', tabl(3,1:5)
       WRITE (nout,99998) ' Treatments  ', tabl(4,1:5)
       WRITE (nout,99998) ' Residual    ', tabl(5,1:3)
       WRITE (nout,99998) ' Total       ', tabl(6,1:2)
       WRITE (nout,*)
       WRITE (nout,*) ' Treatment means'
       WRITE (nout,*)
       WRITE (nout,99999) tmean(1:nt)
       WRITE (nout,*)
       WRITE (nout,99997) ' S.E. of difference (orthogonal design) = ', c(2,1)

99999  FORMAT (10F10.4)
99998  FORMAT (A,F3.0,2X,3(F10.4,2X),F8.4)
99997  FORMAT (A,F10.4)
    END PROGRAM g04bcfe
