    PROGRAM g04dafe

!      G04DAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g04bbf, g04daf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: gmean, rdf, rms, tol
       INTEGER                         :: i, iblock, ifail, irdf, ldc, ldct,   &
                                          ldtabl, lit, n, nc, nt
       LOGICAL                         :: usetx
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: bmean(:), c(:,:), ct(:,:), ef(:),    &
                                          est(:), r(:), tabl(:,:), tmean(:),   &
                                          tx(:), wk(:), y(:)
       INTEGER, ALLOCATABLE            :: irep(:), it(:)
       CHARACTER (11), ALLOCATABLE     :: names(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          abs
!      .. Executable Statements ..
       WRITE (nout,*) 'G04DAF Example Program Results'
       WRITE (nout,*)

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

!      Read in problem size for G04BBF
       READ (nin,*) n, nt, iblock

       ldc = nt
       IF (nt>1) THEN
          lit = n
       ELSE
          lit = 1
       END IF
       ALLOCATE (y(n),bmean(abs(iblock)),tmean(nt),irep(nt),c(ldc,nt),r(n), &
          ef(nt),wk(3*nt),it(lit))

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

!      Don't use TX when calling G04DAF
       usetx = .FALSE.

!      Read in the number of contrasts
       READ (nin,*) nc

!      Using first 4 rows of TABL in G04BBF next NC rows in  G04DAF
       ldtabl = nc + 4

       ldct = nt
       ALLOCATE (ct(ldct,nc),est(nc),tabl(ldtabl,5),tx(nt),names(nc))

!      Read in the constrasts and their names
       DO i = 1, nc
          READ (nin,*) ct(1:nt,i)
          READ (nin,99999) names(i)
       END DO

!      Use default tolerance
       tol = 0.0E0_nag_wp

!      Use standard degrees of freedom
       irdf = 0

!      Calculate the ANOVA table
       ifail = 0
       CALL g04bbf(n,y,iblock,nt,it,gmean,bmean,tmean,tabl,ldtabl,c,ldc,irep, &
          r,ef,tol,irdf,wk,ifail)

!      Display results from G04BBF
       WRITE (nout,*) ' ANOVA table'
       WRITE (nout,*)
       WRITE (nout,*) '  Source        df         SS          MS          F', &
          '        Prob'
       WRITE (nout,*)
       IF (iblock>1) THEN
          WRITE (nout,99998) ' Blocks    ', tabl(1,1:5)
       END IF
       WRITE (nout,99998) ' Treatments', tabl(2,1:5)
       WRITE (nout,99998) ' Residual  ', tabl(3,1:3)
       WRITE (nout,99998) ' Total     ', tabl(4,1:2)
       WRITE (nout,*)

!      Extract the residual mean square and degrees of freedom from ANOVA
!      table
       rms = tabl(3,3)
       rdf = tabl(3,1)

!      Compute sums of squares for contrast
       ifail = -1
       CALL g04daf(nt,tmean,irep,rms,rdf,nc,ct,ldct,est,tabl(5,1),ldtabl,tol, &
          usetx,tx,ifail)
       IF (ifail/=0) THEN
          IF (ifail/=2) THEN
             GO TO 20
          END IF
       END IF

!      Display results from G04DAF
       WRITE (nout,*) ' Orthogonal Contrasts'
       WRITE (nout,*)
       WRITE (nout,99998) (names(i),tabl(i+4,1:5),i=1,nc)

20     CONTINUE

99999  FORMAT (A)
99998  FORMAT (A,3X,F3.0,2X,F10.1,2X,F10.1,2X,F10.3,2X,F9.4)
    END PROGRAM g04dafe
