    PROGRAM g02bpfe

!      G02BPF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02bpf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, itype, ldrr, ldx, m, n,    &
                                          ncases
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: rr(:,:), work1(:), work2(:), x(:,:), &
                                          xmiss(:)
       INTEGER, ALLOCATABLE            :: incase(:), kworka(:), kworkb(:),     &
                                          kworkc(:), miss(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02BPF Example Program Results'
       WRITE (nout,*)

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

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

       ldrr = m
       ldx = n
       ALLOCATE (rr(ldrr,m),work1(m),work2(m),x(ldx,m),xmiss(m),incase(n), &
          kworka(n),kworkb(n),kworkc(n),miss(m))

!      Read in data
       READ (nin,*) (x(i,1:m),i=1,n)

!      Read in missing value flags
       READ (nin,*) miss(1:m)
       READ (nin,*) xmiss(1:m)

!      Display data
       WRITE (nout,99999) 'Number of variables (columns) =', m
       WRITE (nout,99999) 'Number of cases     (rows)    =', n
       WRITE (nout,*)
       WRITE (nout,*) 'Data matrix is:-'
       WRITE (nout,*)
       WRITE (nout,99998) (i,i=1,m)
       WRITE (nout,99997) (i,x(i,1:m),i=1,n)
       WRITE (nout,*)

!      Calculate correlation coefficients
       ifail = 0
       CALL g02bpf(n,m,x,ldx,miss,xmiss,itype,rr,ldrr,ncases,incase,kworka, &
          kworkb,kworkc,work1,work2,ifail)

!      Display results
       WRITE (nout,*) 'Matrix of ranks:-'
       WRITE (nout,*)
       WRITE (nout,*) &
          '(1 in the column headed In/Out indicates the case was included,'
       WRITE (nout,*) &
          ' 0 in the column headed In/Out indicates the case was omitted.)'
       WRITE (nout,*)
       WRITE (nout,99996) 'Case   In/Out', (i,i=1,m)
       WRITE (nout,99995) (i,incase(i),x(i,1:m),i=1,n)
       WRITE (nout,*)
       WRITE (nout,*) 'Matrix of rank correlation coefficients:'
       WRITE (nout,*) 'Upper triangle -- Spearman''s'
       WRITE (nout,*) 'Lower triangle -- Kendall''s tau'
       WRITE (nout,*)
       WRITE (nout,99998) (i,i=1,m)
       WRITE (nout,99997) (i,rr(i,1:m),i=1,m)
       WRITE (nout,*)
       WRITE (nout,99999) 'Number of cases actually used:', ncases

99999  FORMAT (1X,A,I5)
99998  FORMAT (1X,3I12)
99997  FORMAT (1X,I3,3F12.4)
99996  FORMAT (1X,A,I6,2I12)
99995  FORMAT (1X,I3,I7,3F12.4)
    END PROGRAM g02bpfe
