    Program g01ddfe

!     G01DDF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: g01ddf, m01caf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: pw, w
      Integer                          :: ifail, j, n, pn
      Logical                          :: calwts
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:), x(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: allocated
!     .. Executable Statements ..
      Write (nout,*) 'G01DDF Example Program Results'
      Write (nout,*)

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

      pn = 0
      j = 0
d_lp: Do
!       Read in the problem size
        Read (nin,*,Iostat=ifail) n
        If (ifail/=0) Then
          Exit d_lp
        End If

        If (pn/=n) Then
          If (allocated(x)) Then
            Deallocate (x)
            Deallocate (a)
          End If
          Allocate (a(n),x(n))

!         Need to re-calculate the weights
          calwts = .True.
        Else
!         Use the previously calculated weights
          calwts = .False.
        End If
        pn = n

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

!       Sort the data
        ifail = 0
        Call m01caf(x,1,n,'A',ifail)

!       Calculate the test statistic
        ifail = 0
        Call g01ddf(x,n,calwts,a,w,pw,ifail)

        j = j + 1

!       Display results
        Write (nout,99999) 'For sample number ', j, &
          ', value of W statistic = ', w
        Write (nout,99998) '                     Significance level is ', pw
        Write (nout,*)
      End Do d_lp

99999 Format (1X,A,I1,A,F7.4)
99998 Format (1X,A,F8.4)
    End Program g01ddfe
