    PROGRAM g01abfe

!      G01ABF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g01abf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, iwt, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp)              :: res(13)
       REAL (KIND=nag_wp), ALLOCATABLE :: wt(:), wtin(:), x1(:), x2(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G01ABF Example Program Results'
       WRITE (nout,*)

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

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

       ALLOCATE (wt(n),wtin(n),x1(n),x2(n))

!      Read in data
       READ (nin,*) (x1(i),x2(i),i=1,n)
       IF (iwt==1) THEN
          READ (nin,*) wtin(1:n)
          wt(1:n) = wtin(1:n)
       END IF

!      Display data
       WRITE (nout,99999) 'Number of cases', n
       WRITE (nout,*) 'Data as input -'
       WRITE (nout,*) '         Var  1     Var  2     Var  1   &
          &  Var  2     Var  1     Var  2'

       WRITE (nout,99995) (x1(i),x2(i),i=1,n)
       IF (iwt==1) THEN
          WRITE (nout,*) 'Weights as input -'
          WRITE (nout,99994) wtin(1:n)
       END IF
       WRITE (nout,*)

!      Calculate summary statistics
       ifail = -1
       CALL g01abf(n,x1,x2,iwt,wt,res,ifail)
       IF (ifail/=0) THEN
          IF (ifail/=2) THEN
             GO TO 20
          END IF
       END IF

!      Display results
       WRITE (nout,99999) 'No. of valid cases', iwt
       WRITE (nout,99993) 'Variable 1', 'Variable 2'
       WRITE (nout,99998) 'Mean    ', res(1), res(2)
       WRITE (nout,99997) 'Corr SSP', res(5), res(6), res(7)
       WRITE (nout,99998) 'Minimum ', res(9), res(11)
       WRITE (nout,99998) 'Maximum ', res(10), res(12)
       WRITE (nout,99998) 'Sum of weights         ', res(13)
       IF (ifail==0) THEN
          WRITE (nout,99998) 'Std devn', res(3), res(4)
          WRITE (nout,99996) 'Correln ', res(8)
       ELSE
          WRITE (nout,*) 'Std devn and Correln not defined'
       END IF

20     CONTINUE

99999  FORMAT (1X,A,I5)
99998  FORMAT (1X,A,F15.1,F30.1)
99997  FORMAT (1X,A,3E15.5)
99996  FORMAT (1X,A,F30.4)
99995  FORMAT (5X,6F11.1)
99994  FORMAT (13X,F9.3)
99993  FORMAT (13X,A,20X,A)
    END PROGRAM g01abfe
