    PROGRAM g01ddfe

!      G01DDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. 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 Functions ..
       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
