    PROGRAM nag_spline_2d_ex03

      ! 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_interp, nag_spline_2d_extract, nag_deallocate
      USE nag_write_mat, ONLY : nag_write_gen_mat
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: mx, my, p, q
      TYPE (nag_spline_2d_comm_wp) :: spline
      ! .. Local Arrays ..
      REAL (wp), ALLOCATABLE :: f(:,:), x(:), y(:)
      REAL (wp), POINTER :: kappa(:,:), lambda(:), mu(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_spline_2d_ex03'

      READ (nag_std_in,*)          ! Skip heading in data file
      READ (nag_std_in,*) mx, my

      ALLOCATE (x(mx),y(my),f(mx,my)) ! Allocate storage

      READ (nag_std_in,*) x
      READ (nag_std_in,*) y
      READ (nag_std_in,*) f

      ! Generate the (x,y,f) interpolating bicubic B-spline.

      CALL nag_spline_2d_interp(x,y,f,spline)

      ! Extract the interior knots lambda, mu and the B-spline
      ! coefficients kappa from the structure.

      CALL nag_spline_2d_extract(spline,p=p,q=q,lambda=lambda,mu=mu, &
       kappa=kappa)

      WRITE (nag_std_out,'(/1X,A,I8)') &
       'Total number of knots in x-direction: p =', p
      WRITE (nag_std_out,'(/1X,A)') 'Interior knots in x-direction:'
      WRITE (nag_std_out,'(5ES15.4)') lambda
      WRITE (nag_std_out,'(/1X,A,I8)') &
       'Total number of knots in y-direction: q =', q
      WRITE (nag_std_out,'(/1X,A)') 'Interior knots in y-direction:'
      WRITE (nag_std_out,'(5ES15.4)') mu

      CALL nag_write_gen_mat(kappa,title= &
       'The matrix kappa of B-spline coefficients:')

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

      NULLIFY (lambda,mu,kappa)

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

    END PROGRAM nag_spline_2d_ex03
