    PROGRAM nag_spline_2d_ex02

      ! Example Program Text for nag_spline_2d
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nag_spline_2d, ONLY : nag_spline_2d_comm_wp => nag_spline_2d_comm_dp &
       , nag_spline_2d_lsq_fit, nag_spline_2d_eval, nag_deallocate
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, m, p, q
      TYPE (nag_spline_2d_comm_wp) :: spline
      ! .. Local Arrays ..
      REAL (wp), ALLOCATABLE :: f(:), lambda(:), mu(:), s(:), wt(:), x(:), &
       y(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_spline_2d_ex02'

      READ (nag_std_in,*)          ! Skip heading in data file
      READ (nag_std_in,*) m
      READ (nag_std_in,*) p, q

      ALLOCATE (x(m),y(m),f(m),wt(m),s(m),lambda(p-8), &
       mu(q-8))                    ! Allocate storage

      READ (nag_std_in,*) x
      READ (nag_std_in,*) y
      READ (nag_std_in,*) f
      READ (nag_std_in,*) wt
      IF (p>8) READ (nag_std_in,*) lambda
      IF (q>8) READ (nag_std_in,*) mu

      ! Fit bicubic spline to data points.

      CALL nag_spline_2d_lsq_fit(x,y,f,lambda,mu,spline,wt=wt, &
       thresh=1.0E-6_wp)

      ! Evaluate spline at the data points and compare with data
      ! values.

      CALL nag_spline_2d_eval(spline,x,y,s)

      WRITE (nag_std_out,*) '       x          y        Data       Fit'
      DO i = 1, m
        WRITE (nag_std_out,'(4(1X,F10.4))') x(i), y(i), f(i), s(i)
      END DO

      DEALLOCATE (x,y,f,wt,s,lambda,mu) ! Deallocate storage

      CALL nag_deallocate(spline)  ! Free structure allocated by NAG fl90

    END PROGRAM nag_spline_2d_ex02
