Program g08ebfe

!     G08EBF Example Program Text

!     Mark 25 Release. NAG Copyright 2014.

!     .. Use Statements ..
      Use nag_library, Only: g08ebf, nag_wp, x04eaf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: chi, df, ex, prob
      Integer                          :: i, ifail, lag, ldc, lwrk, msize, n,  &
                                          nsamp, pn
      Logical                          :: bapp
      Character (1)                    :: cl
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: wrk(:), x(:)
      Integer, Allocatable             :: ncount(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'G08EBF Example Program Results'
      Write (nout,*)
      Flush (nout)

!     Skip main heading in data file
      Read (nin,*)

!     Read in number of samples
      Read (nin,*) nsamp, msize, lag

      ldc = msize
      lwrk = 2*lag
      Allocate (ncount(ldc,msize),wrk(lwrk),x(1))

      If (nsamp==1) Then
        cl = 'S'
      Else
        cl = 'F'
      End If

      pn = 0
      bapp = .False.
      Do i = 1, nsamp
!       Skip run heading in data file
        Read (nin,*)

!       Read in sample size
        Read (nin,*) n

        If (n>pn) Then
!         Reallocate X if required
          Deallocate (x)
          Allocate (x(n))
          pn = n
        End If

!       Read in the sample
        Read (nin,*) x(1:n)

!       Process the sample
        ifail = -1
        Call g08ebf(cl,n,x,msize,lag,ncount,ldc,ex,chi,df,prob,wrk,ifail)
        If (ifail==8) Then
          bapp = .True.
        Else If (ifail/=0) Then
          Go To 100
        End If

!       Adjust CL for intermediate calls
        If (i<nsamp-1) Then
          cl = 'I'
        Else
          cl = 'L'
        End If

      End Do

!     Display results
      ifail = 0
      Call x04eaf('General',' ',msize,msize,ncount,ldc,'Count matrix',ifail)
      Write (nout,*)
      Write (nout,99999) 'Expected value = ', ex
      Write (nout,99998) 'CHISQ          = ', chi
      Write (nout,99999) 'DF             = ', df
      Write (nout,99998) 'Probability    = ', prob
      If (bapp) Then
        Write (nout,*) ' ** Note : EX <= 5.0, the chi square approximation &
          &may not be very good.'
      End If

100   Continue

99999 Format (1X,A,F8.2)
99998 Format (1X,A,F10.4)
    End Program g08ebfe