    PROGRAM g03dafe

!      G03DAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g03daf, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: df, sig, stat
       INTEGER                         :: i, ifail, ldgmn, ldx, lgc, lwk, lwt, &
                                          m, n, ng, nvar
       CHARACTER (1)                   :: weight
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: det(:), gc(:), gmn(:,:), wk(:),      &
                                          wt(:), x(:,:)
       INTEGER, ALLOCATABLE            :: ing(:), isx(:), iwk(:), nig(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          count
!      .. Executable Statements ..
       WRITE (nout,*) 'G03DAF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

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

!      Read in the problem size
       READ (nin,*) n, m, ng, weight

       IF (weight=='W' .OR. weight=='w') THEN
          lwt = n
       ELSE
          lwt = 0
       END IF
       ldx = n
       ALLOCATE (x(ldx,m),ing(n),wt(lwt),isx(m))

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

!      Read in variable inclusion flags
       READ (nin,*) isx(1:m)

!      Calculate NVAR
       nvar = count(isx(1:m)==1)

       ldgmn = ng
       lgc = (ng+1)*nvar*(nvar+1)/2
       lwk = n*(nvar+1)
       ALLOCATE (nig(ng),gmn(ldgmn,nvar),det(ng),gc(lgc),wk(lwk),iwk(ng))

!      Compute test statistic
       ifail = 0
       CALL g03daf(weight,n,m,x,ldx,isx,nvar,ing,ng,wt,nig,gmn,ldgmn,det,gc, &
          stat,df,sig,wk,iwk,ifail)

!      Display results
       ifail = 0
       CALL x04caf('General',' ',ng,nvar,gmn,ldgmn,'Group means',ifail)
       WRITE (nout,*)
       WRITE (nout,*) ' LOG of determinants'
       WRITE (nout,*)
       WRITE (nout,99999) det(1:ng)
       WRITE (nout,*)
       WRITE (nout,99998) ' STAT = ', stat
       WRITE (nout,99998) '   DF = ', df
       WRITE (nout,99998) '  SIG = ', sig

99999  FORMAT (1X,3F10.4)
99998  FORMAT (1X,A,F7.4)
    END PROGRAM g03dafe
