    PROGRAM g11bafe

!      G11BAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g11baf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, j, k, lauxt, ldf, lwt,     &
                                          maxt, n, ncells, ncol, ndim, nfac,   &
                                          nrow
       CHARACTER (1)                   :: stat, weight
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: auxt(:), table(:), wt(:), y(:)
       INTEGER, ALLOCATABLE            :: icount(:), idim(:), ifac(:,:),       &
                                          isf(:), iwk(:), lfac(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G11BAF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       READ (nin,*) stat, weight, n, nfac

       IF (weight=='W' .OR. weight=='w' .OR. weight=='V' .OR. weight=='v') &
             THEN
          lwt = n
       ELSE
          lwt = 0
       END IF
       ldf = n
       ALLOCATE (isf(nfac),lfac(nfac),ifac(ldf,nfac),y(n),wt(lwt),idim(nfac), &
          iwk(2*nfac))

!      Read in data
       IF (lwt>0) THEN
          READ (nin,*) (ifac(i,1:nfac),y(i),wt(i),i=1,n)
       ELSE
          READ (nin,*) (ifac(i,1:nfac),y(i),i=1,n)
       END IF
       READ (nin,*) lfac(1:nfac)
       READ (nin,*) isf(1:nfac)

!      Calculate MAXT
       maxt = 1
       DO i = 1, nfac
          IF (isf(i)>0) THEN
             maxt = maxt*lfac(i)
          END IF
       END DO

       SELECT CASE (stat)
       CASE ('A','a')
          lauxt = maxt
       CASE ('V','v')
          lauxt = 2*maxt
       CASE DEFAULT
          lauxt = 0
       END SELECT

       ALLOCATE (table(maxt),icount(maxt),auxt(lauxt))

!      Compute table
       ifail = 0
       CALL g11baf(stat,'I',weight,n,nfac,isf,lfac,ifac,ldf,y,wt,table,maxt, &
          ncells,ndim,idim,icount,auxt,iwk,ifail)

!      Display results
       WRITE (nout,*) ' TABLE'
       WRITE (nout,*)
       ncol = idim(ndim)
       nrow = ncells/ncol
       k = 1
       DO i = 1, nrow
          WRITE (nout,99999) (table(j),'(',icount(j),')',j=k,k+ncol-1)
          k = k + ncol
       END DO

99999  FORMAT (1X,6(F8.2,A,I2,A))
    END PROGRAM g11bafe
