    PROGRAM g01arfe

!      G01ARF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g01arf, nag_wp, x04abf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: iset = 1, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: unit, unit1, unit2
       INTEGER                         :: i, ifail, ldplot, lines, n, nstepx,  &
                                          nstepy, outchn
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: sorty(:), y(:)
       INTEGER, ALLOCATABLE            :: iwork(:)
       CHARACTER (1), ALLOCATABLE      :: plot(:,:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'G01ARF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

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

!      Read in the problem size
       READ (nin,*) n, unit, nstepx, nstepy

!      Make sure there is more than the minimum number of lines available
!      for the plot
       ldplot = max(100,nstepy)

       ALLOCATE (y(n),iwork(n),sorty(n),plot(ldplot,nstepx))

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

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

!      Produce and display the plot
       unit1 = unit
       ifail = 0
       CALL g01arf('Fences','Print',n,y,nstepx,nstepy,unit1,plot,ldplot,lines, &
          sorty,iwork,ifail)

       WRITE (nout,*)
       FLUSH (nout)

!      Produce the plot, without printing it
       unit2 = unit
       ifail = 0
       CALL g01arf('Extremes','Noprint',n,y,nstepx,nstepy,unit2,plot,ldplot, &
          lines,sorty,iwork,ifail)

!      Display the plot
       DO i = 1, lines
          WRITE (nout,99999) plot(i,1:nstepx)
       END DO

99999  FORMAT (1X,132A)
    END PROGRAM g01arfe
