!   D03MAF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE d03mafe_mod

!      D03MAF Example Program Module:
!             Parameters and User-defined Routines

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER       :: rad = 6.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: xmid = 7.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: ymid = 7.0_nag_wp
       INTEGER, PARAMETER                  :: nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: isin
       FUNCTION isin(x,y)
!         Circular domain

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          INTEGER                             :: isin
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x, y
!         .. Executable Statements ..
          IF ((x-xmid)**2+(y-ymid)**2<=rad**2) THEN
             isin = 1
          ELSE
             isin = 0
          END IF
          RETURN
!dec$ attributes offload : mic :: isin
       END FUNCTION isin
    END MODULE d03mafe_mod

    PROGRAM d03mafe

!      D03MAF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d03maf, nag_wp
       USE d03mafe_mod, ONLY : isin, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: h
       INTEGER                             :: i, ifail, m, n, nb, npts,        &
                                              sddist, sdindx
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: dist(:,:), places(:,:)
       INTEGER, ALLOCATABLE                :: indx(:,:)
!      .. Executable Statements ..
       WRITE (nout,*) 'D03MAF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) sddist, sdindx
       ALLOCATE (dist(4,sddist),places(2,sdindx),indx(4,sdindx))

       READ (nin,*) h
       READ (nin,*) m, n, nb

!      ifail: behaviour on error exit   
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft  
       ifail = 0
       CALL d03maf(h,m,n,nb,npts,places,indx,sdindx,isin,dist,sddist,ifail)

       WRITE (nout,*) '  I    X(I)      Y(I)'
       DO i = 1, npts
          WRITE (nout,99999) i, places(1,i), places(2,i)
       END DO
       WRITE (nout,*)
       WRITE (nout,*) 'INDX'
       WRITE (nout,99998) (indx(1:4,i),i=1,npts)

99999  FORMAT (1X,I3,2F10.6)
99998  FORMAT (1X,4I5)
    END PROGRAM d03mafe
