    PROGRAM g03dcfe

!      G03DCF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g03daf, g03dcf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: df, sig, stat
       INTEGER                         :: i, ifail, ldgmn, ldox, ldp, ldx,     &
                                          lgc, lwk, lwt, m, n, ng, nobs, nvar, &
                                          tdati
       LOGICAL                         :: atiq
       CHARACTER (1)                   :: equal, priors, typ, weight
       CHARACTER (80)                  :: fmt
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: ati(:,:), det(:), gc(:), gmn(:,:),   &
                                          ox(:,:), p(:,:), prior(:), wk(:),    &
                                          wt(:), x(:,:)
       INTEGER, ALLOCATABLE            :: iag(:), ing(:), isx(:), iwk(:), nig(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          count, max
!      .. Executable Statements ..
       WRITE (nout,*) 'G03DCF Example Program Results'
       WRITE (nout,*)

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

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

       IF (weight=='W' .OR. weight=='w') THEN
          lwt = n
       ELSE
          lwt = 0
       END IF
       ldox = n
       ALLOCATE (ox(ldox,m),ing(n),wt(lwt),isx(m))

!      Read in data
       IF (lwt>0) THEN
          READ (nin,*) (ox(i,1:m),ing(i),wt(i),i=1,n)
       ELSE
          READ (nin,*) (ox(i,1:m),ing(i),i=1,n)
       END IF

!      Read in variable inclusion flags
       READ (nin,*) isx(1:m)

!      Calculate NVAR
       nvar = count(isx(1:m)==1)

       lwk = max(n*(nvar+1),2*nvar)
       ldgmn = ng
       lgc = (ng+1)*nvar*(nvar+1)/2
       ALLOCATE (nig(ng),gmn(ldgmn,nvar),det(ng),gc(lgc),wk(lwk),iwk(ng))

!      Compute covariance matrix
       ifail = 0
       CALL g03daf(weight,n,m,ox,ldox,isx,nvar,ing,ng,wt,nig,gmn,ldgmn,det,gc, &
          stat,df,sig,wk,iwk,ifail)

!      Read in parameters controlling grouping
       READ (nin,*) typ, equal, priors, nobs, atiq

       IF (atiq) THEN
          tdati = ng
       ELSE
          tdati = 1
       END IF
       ldx = nobs
       ldp = nobs
       ALLOCATE (x(ldx,m),prior(ng),p(ldp,ng),iag(nobs),ati(ldp,tdati))

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

!      Read in priors
       IF (priors=='I' .OR. priors=='i') THEN
          READ (nin,*) prior(1:ng)
       END IF

!      Allocate observations to groups
       ifail = 0
       CALL g03dcf(typ,equal,priors,nvar,ng,nig,gmn,ldgmn,gc,det,nobs,m,isx,x, &
          ldx,prior,p,ldp,iag,atiq,ati,wk,ifail)

!      Display results
       IF (atiq) THEN
          WRITE (fmt,99999) '(2(I6,5X,', ng, 'F6.3))'
          WRITE (nout,*) '   Obs       Posterior        Allocated', &
             '     Atypicality'
          WRITE (nout,*) '             probabilities    to group      index'
          WRITE (nout,*)
          WRITE (nout,fmt) (i,p(i,1:ng),iag(i),ati(i,1:ng),i=1,nobs)
       ELSE
          WRITE (fmt,99999) '(I6,5X,', ng, 'F6.3,I6))'
          WRITE (nout,*) '   Obs       Posterior        Allocated'
          WRITE (nout,*) '             probabilities    to group '
          WRITE (nout,*)
          WRITE (nout,fmt) (i,p(i,1:ng),iag(i),i=1,nobs)
       END IF

99999  FORMAT (A,I0,A)
    END PROGRAM g03dcfe
