    PROGRAM g04dbfe

!      G04DBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g04bbf, g04dbf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: clevel, gmean, rdf, tol
       INTEGER                         :: i, iblock, ifail, ij, irdf, j, ldc,  &
                                          lit, n, nt
       CHARACTER (1)                   :: typ
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: bmean(:), c(:,:), cil(:), ciu(:),    &
                                          ef(:), r(:), tmean(:), wk(:), y(:)
       REAL (KIND=nag_wp)              :: table(4,5)
       INTEGER, ALLOCATABLE            :: irep(:), isig(:), it(:)
       CHARACTER (1)                   :: star(2)
!      .. Intrinsic Functions ..
       INTRINSIC                          abs
!      .. Executable Statements ..
       WRITE (nout,*) 'G04DBF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       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),cil(nt*(nt-1)/2),ciu(nt*(nt- &
          1)/2),isig(nt*(nt-1)/2))

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

!      Read in the type of level for the CIs
       READ (nin,*) typ, clevel

!      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,table,4,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    ', table(1,1:5)
       END IF
       WRITE (nout,99998) ' Treatments', table(2,1:5)
       WRITE (nout,99998) ' Residual  ', table(3,1:3)
       WRITE (nout,99998) ' Total     ', table(4,1:2)
       WRITE (nout,*)
       WRITE (nout,*) ' Treatment means'
       WRITE (nout,*)
       WRITE (nout,99999) tmean(1:nt)
       WRITE (nout,*)

!      Extract the residual degrees of freedom
       rdf = table(3,1)

!      Calculate simultaneous CIs
       ifail = 0
       CALL g04dbf(typ,nt,tmean,rdf,c,ldc,clevel,cil,ciu,isig,ifail)

!      Display results from G04DBF
       WRITE (nout,*) ' Simultaneous Confidence Intervals'
       WRITE (nout,*)
       star(2) = '*'
       star(1) = ' '
       ij = 0
       DO i = 1, nt
          DO j = 1, i - 1
             ij = ij + 1
             WRITE (nout,99997) i, j, cil(ij), ciu(ij), star(isig(ij)+1)
          END DO
       END DO

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