    PROGRAM g10zafe

!      G10ZAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g10zaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: rss
       INTEGER                         :: i, ifail, lwt, n, nord
       CHARACTER (1)                   :: weight
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: wt(:), wtord(:), x(:), xord(:),      &
                                          y(:), yord(:)
       INTEGER, ALLOCATABLE            :: iwrk(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G10ZAF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       READ (nin,*) weight, n

       IF (weight=='W' .OR. weight=='w') THEN
          lwt = n
       ELSE
          lwt = 0
       END IF
       ALLOCATE (x(n),y(n),iwrk(n),wt(lwt),xord(n),yord(n),wtord(n))

!      Read in data
       IF (lwt>0) THEN
          READ (nin,*) (x(i),y(i),wt(i),i=1,n)
       ELSE
          READ (nin,*) (x(i),y(i),i=1,n)
       END IF

!      Reorder data
       ifail = 0
       CALL g10zaf(weight,n,x,y,wt,nord,xord,yord,wtord,rss,iwrk,ifail)

!      Display results
       WRITE (nout,99999) 'Number of distinct observations = ', nord
       WRITE (nout,99998) 'Residual sum of squares         = ', rss
       WRITE (nout,*)
       WRITE (nout,*) '             X                 Y                 WT'
       WRITE (nout,99997) (xord(i),yord(i),wtord(i),i=1,nord)

99999  FORMAT (1X,A,I6)
99998  FORMAT (1X,A,F13.5)
99997  FORMAT (5X,F13.5,5X,F13.5,5X,F13.5)
    END PROGRAM g10zafe
