    PROGRAM g13ddfe

!      G13DDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g13ddf, nag_wp, x04abf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: iset = 1, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: cgetol, rlogl
       INTEGER                         :: i, ifail, ip, iprint, iq, ishow, k,  &
                                          kmax, ldcm, maxcal, n, nadv, niter,  &
                                          npar
       LOGICAL                         :: exact, mean
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: cm(:,:), g(:), par(:), qq(:,:),      &
                                          v(:,:), w(:,:)
       LOGICAL, ALLOCATABLE            :: parhld(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G13DDF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

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

!      Read in problem size
       READ (nin,*) k, ip, iq, n, mean

!      Calculate NPAR
       npar = (ip+iq)*k*k
       IF (mean) THEN
          npar = npar + k
       END IF

       ldcm = npar
       kmax = k
       ALLOCATE (par(npar),qq(kmax,k),w(kmax,n),v(kmax,n),g(npar), &
          cm(ldcm,npar),parhld(npar))

!      Read in series
       READ (nin,*) (w(i,1:n),i=1,k)

!      Read in control parameters
       READ (nin,*) iprint, cgetol, maxcal, ishow

!      Read in exact likelihood flag
       READ (nin,*) exact

!      Read in initial parameter estimates and free parameter flags
       READ (nin,*) par(1:npar)
       READ (nin,*) parhld(1:npar)

!      Read in initial values for covariance matrix QQ
       READ (nin,*) (qq(i,1:i),i=1,k)

!      Set the advisory channel to NOUT for monitoring information
       IF (iprint>=0 .OR. ishow/=0) THEN
          nadv = nout
          CALL x04abf(iset,nadv)
       END IF

!      Fit a VARMA model
       ifail = 0
       CALL g13ddf(k,n,ip,iq,mean,par,npar,qq,kmax,w,parhld,exact,iprint, &
          cgetol,maxcal,ishow,niter,rlogl,v,g,cm,ldcm,ifail)

    END PROGRAM g13ddfe
