    PROGRAM g01aafe

!      G01AAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g01aaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: s2, s3, s4, wtsum, xmax, xmean, xmin
       INTEGER                         :: ifail, iwt, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: wt(:), wtin(:), x(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G01AAF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       READ (nin,*) n, iwt

       ALLOCATE (wt(n),wtin(n),x(n))

!      Read in data
       READ (nin,*) x(1:n)
       IF (iwt==1) THEN
          READ (nin,*) wtin(1:n)
          wt(1:n) = wtin(1:n)
       END IF

!      Display data
       WRITE (nout,99999) 'Number of cases ', n
       WRITE (nout,*) 'Data as input -'
       WRITE (nout,99998) x(1:n)
       IF (iwt==1) THEN
          WRITE (nout,*) 'Weights as input -'
          WRITE (nout,99998) wtin(1:n)
       END IF
       WRITE (nout,*)

!      Calculate summary statistics
       ifail = -1
       CALL g01aaf(n,x,iwt,wt,xmean,s2,s3,s4,xmin,xmax,wtsum,ifail)
       IF (ifail/=0) THEN
          IF (ifail/=2) THEN
             GO TO 20
          END IF
       END IF

!      Display results
       WRITE (nout,99999) 'No. of valid cases    ', iwt
       WRITE (nout,99997) 'Mean          ', xmean
       WRITE (nout,99997) 'Minimum       ', xmin
       WRITE (nout,99997) 'Maximum       ', xmax
       WRITE (nout,99997) 'Sum of weights', wtsum

       IF (ifail==0) THEN
          WRITE (nout,99997) 'Std devn      ', s2
          WRITE (nout,99997) 'Skewness      ', s3
          WRITE (nout,99997) 'Kurtosis      ', s4
       ELSE
          WRITE (nout,*) 'Std devn and coeffts of skewness'
          WRITE (nout,*) 'and kurtosis not defined'
       END IF

20     CONTINUE

99999  FORMAT (1X,A,I5)
99998  FORMAT (1X,5F12.1)
99997  FORMAT (1X,A,F13.1)
    END PROGRAM g01aafe
