    PROGRAM g01ahfe

!      G01AHF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g01ahf, nag_wp, x04abf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: iset = 1, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: xbar, xstd
       INTEGER                         :: ifail, istand, lwork, nobs, nstepx,  &
                                          nstepy, outchn
       CHARACTER (80)                  :: title
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: work(:), x(:), xsort(:)
       INTEGER, ALLOCATABLE            :: iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          len_trim
!      .. Executable Statements ..
       WRITE (nout,*) 'G01AHF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       READ (nin,*) nobs, nstepx, nstepy, istand

       lwork = 5*nobs/2
       ALLOCATE (iwork(nobs),xsort(nobs),x(nobs),work(lwork))

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

!      Read in the title
       READ (nin,99997) title

!      Set advisory channel
       outchn = nout
       CALL x04abf(iset,outchn)

!      Display data to be plotted
       WRITE (nout,*) 'Data values to be plotted'
       WRITE (nout,99998) x(1:nobs)
       WRITE (nout,*)
       WRITE (nout,*)

!      Display title
       WRITE (nout,*)
       WRITE (nout,*) title(1:len_trim(title))
       WRITE (nout,*)
       FLUSH (nout)

!      Produce the plot
       ifail = 0
       CALL g01ahf(x,nobs,nstepx,nstepy,istand,iwork,work,lwork,xsort,xbar, &
          xstd,ifail)

!      Display additional results
       WRITE (nout,*)
       WRITE (nout,99999) 'Mean of data values = ', xbar
       WRITE (nout,99999) 'Standard deviation  = ', xstd
       WRITE (nout,*)
       WRITE (nout,*) 'Sorted standardised data values'
       WRITE (nout,99998) xsort(1:nobs)

99999  FORMAT (1X,A,F5.2)
99998  FORMAT (5X,5F7.2)
99997  FORMAT (A80)
    END PROGRAM g01ahfe
