    PROGRAM e01sgfe

!      E01SGF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : e01sgf, e01shf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, liq, lrq, m, n, nq, nw
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: f(:), q(:), qx(:), qy(:), rq(:),     &
                                          u(:), v(:), x(:), y(:)
       INTEGER, ALLOCATABLE            :: iq(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'E01SGF Example Program Results'

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

!      Input the number of data points

       READ (nin,*) m
       liq = 2*m + 1
       lrq = 6*m + 5
       ALLOCATE (x(m),y(m),f(m),iq(liq),rq(lrq))

       DO i = 1, m
          READ (nin,*) x(i), y(i), f(i)
       END DO

!      Generate the interpolant.

       nq = 0
       nw = 0

       ifail = 0
       CALL e01sgf(m,x,y,f,nw,nq,iq,liq,rq,lrq,ifail)

!      Input the number of evaluation points.

       READ (nin,*) n
       ALLOCATE (u(n),v(n),q(n),qx(n),qy(n))

       DO i = 1, n
          READ (nin,*) u(i), v(i)
       END DO

!      Evaluate the interpolant using E01SHF.

       ifail = 0
       CALL e01shf(m,x,y,f,iq,liq,rq,lrq,n,u,v,q,qx,qy,ifail)

       WRITE (nout,*)
       WRITE (nout,*) '     I      U(I)      V(I)      Q(I)'

       DO i = 1, n
          WRITE (nout,99999) i, u(i), v(i), q(i)
       END DO

99999  FORMAT (1X,I6,3F10.2)
    END PROGRAM e01sgfe
