    Program g05rcfe

!     G05RCF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: g05kff, g05rcf, nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: lseed = 1, nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: df, genid, i, ifail, ldc, ldx, lr,   &
                                          lstate, m, mode, n, subid
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: c(:,:), r(:), x(:,:)
      Integer                          :: seed(lseed)
      Integer, Allocatable             :: state(:)
!     .. Executable Statements ..
      Write (nout,*) 'G05RCF Example Program Results'
      Write (nout,*)
      Flush (nout)

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

!     Read in the base generator information and seed
      Read (nin,*) genid, subid, seed(1)

!     Initial call to initialiser to get size of STATE array
      lstate = 0
      Allocate (state(lstate))
      ifail = 0
      Call g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!     Reallocate STATE
      Deallocate (state)
      Allocate (state(lstate))

!     Initialize the generator to a repeatable sequence
      ifail = 0
      Call g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!     Read in sample size and number of dimensions
      Read (nin,*) n, m

      ldc = m
      ldx = n
      lr = m*(m+1) + 2
      Allocate (c(ldc,m),x(ldx,m),r(lr))

!     Read in degrees of freedom
      Read (nin,*) df

!     Read in upper triangle portion of the covariance matrix
      Do i = 1, m
        Read (nin,*) c(i,i:m)
      End Do

!     Using a single call to G05RCF, so set up reference vector
!     and generate values in one go
      mode = 2

!     Generate variates
      ifail = 0
      Call g05rcf(mode,n,df,m,c,ldc,r,lr,state,x,ldx,ifail)

!     Display the variates
      ifail = 0
      Call x04caf('General',' ',n,m,x,ldx,'Variates',ifail)

    End Program g05rcfe
