    PROGRAM g07gafe

!      G07GAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g07gaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: mean, var
       INTEGER                         :: i, ifail, ldiff, n, niout, p
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: diff(:), llamb(:), y(:)
       INTEGER, ALLOCATABLE            :: iout(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G07GAF Example Program Results'
       WRITE (nout,*)

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

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

       ALLOCATE (y(n),iout(n),diff(ldiff),llamb(ldiff))

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

!      Let routine calculate mean and variance
       mean = 0.0E0_nag_wp
       var = 0.0E0_nag_wp

!      Get a list of potential outliers
       ifail = 0
       CALL g07gaf(n,p,y,mean,var,iout,niout,ldiff,diff,llamb,ifail)

!      Display results
       WRITE (nout,*) 'Number of potential outliers:', niout
       IF (ldiff>0) THEN
          WRITE (nout,*) '  No.  Index    Value       Diff    ln(lambda^2)'
       ELSE
          WRITE (nout,*) '  No.  Index    Value'
       END IF
       DO i = 1, niout
          IF (i>ldiff) THEN
             WRITE (nout,99999) i, iout(i), y(iout(i))
          ELSE
             WRITE (nout,99998) i, iout(i), y(iout(i)), diff(i), llamb(i)
          END IF
       END DO

99999  FORMAT (1X,I4,2X,I4,1X,F10.2)
99998  FORMAT (1X,I4,2X,I4,3(1X,F10.2))
    END PROGRAM g07gafe
