    PROGRAM g05pcfe

!      G05PCF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g05kbf, g05pcf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: ipmax = 2, iqmax = 2, kmax = 3
       INTEGER, PARAMETER              :: ldvar = kmax
       INTEGER, PARAMETER              :: liwork = 10, nin = 5, nmax = 100,    &
                                          nout = 6, nr = 600
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, igen, ii, ip, iq, j, k, l, &
                                          mode, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp)              :: phi(kmax*kmax*ipmax), r(nr),         &
                                          theta(kmax*kmax*iqmax),              &
                                          var(ldvar,kmax), x(ldvar,nmax),      &
                                          xmean(kmax)
       INTEGER                         :: iseed(4), iwork(liwork)
!      .. Executable Statements ..
       WRITE (nout,*) 'G05PCF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) k, ip, iq, n

       IF (k>0 .AND. k<=kmax .AND. ip>=0 .AND. ip<=ipmax .AND. iq>=0 .AND. &
             iq<=iqmax) THEN
          IF (n>0 .AND. n<=nmax) THEN
             DO l = 1, ip
                DO i = 1, k
                   ii = (l-1)*k*k + i
                   READ (nin,*) (phi(ii+k*(j-1)),j=1,k)
                END DO
             END DO
             DO l = 1, iq
                DO i = 1, k
                   ii = (l-1)*k*k + i
                   READ (nin,*) (theta(ii+k*(j-1)),j=1,k)
                END DO
             END DO
             READ (nin,*) (xmean(i),i=1,k)
             DO i = 1, k
                READ (nin,*) (var(i,j),j=1,i)
             END DO
!            Initialize the seed to a repeatable sequence
             iseed(1) = 1762543
             iseed(2) = 9324783
             iseed(3) = 4234401
             iseed(4) = 742355
!            IGEN identifies the stream.
             igen = 1
             CALL g05kbf(igen,iseed)

             mode = 2
             ifail = -1

             CALL g05pcf(mode,k,xmean,ip,phi,iq,theta,var,ldvar,n,x,igen, &
                iseed,r,nr,iwork,liwork,ifail)

             WRITE (nout,*)
             IF (ifail==0) THEN

                WRITE (nout,*) ' Realization Number 1'

                DO i = 1, k
                   WRITE (nout,99999) '  Series number ', i
                   WRITE (nout,*) '  -------------'
                   WRITE (nout,*)
                   WRITE (nout,99998) (x(i,j),j=1,n)
                END DO

                mode = 3
                ifail = -1

                CALL g05pcf(mode,k,xmean,ip,phi,iq,theta,var,ldvar,n,x,igen, &
                   iseed,r,nr,iwork,liwork,ifail)

                IF (ifail==0) THEN
                   WRITE (nout,*)
                   WRITE (nout,*)
                   WRITE (nout,*) ' Realization Number 2'

                   DO i = 1, k
                      WRITE (nout,99999) '  Series number ', i
                      WRITE (nout,*) '  -------------'
                      WRITE (nout,*)
                      WRITE (nout,99998) (x(i,j),j=1,n)
                   END DO
                END IF
             END IF

          END IF
       END IF

99999  FORMAT (/1X,A,I3)
99998  FORMAT (8(2X,F8.3))
    END PROGRAM g05pcfe
