    PROGRAM s22aafe

!      S22AAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, s22aaf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nlmax = 100, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: x
       INTEGER                         :: ifail, m, mode, n, nl
!      .. Local Arrays ..
       REAL (KIND=nag_wp)              :: p(0:nlmax)
!      .. Executable Statements ..
       WRITE (nout,*) 'S22AAF Example Program Results'

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

       READ (nin,*) mode, x, m, nl

       WRITE (nout,*)

       ifail = 0
       CALL s22aaf(mode,x,m,nl,p,ifail)

       WRITE (nout,*) 'MODE      X      M    NL'
       WRITE (nout,*)
       WRITE (nout,99999) mode, x, m, nl
       WRITE (nout,*)

       SELECT CASE (mode)
       CASE (1)

          IF (m==0) THEN
             WRITE (nout,*) 'Unnormalized Legendre function values'
          ELSE
             WRITE (nout,*) 'Unnormalized associated Legendre function values'
          END IF

       CASE (2)

          IF (m==0) THEN
             WRITE (nout,*) 'Normalized Legendre function values'
          ELSE
             WRITE (nout,*) 'Normalized associated Legendre function values'
          END IF

       END SELECT

       WRITE (nout,*) ' n      P(n)'

       DO n = 0, nl
          WRITE (nout,99998) n, p(n)
       END DO

99999  FORMAT (1X,I3,4X,F5.1,2I6)
99998  FORMAT (1X,I2,1X,1P,E12.4)
    END PROGRAM s22aafe
