    PROGRAM g13dsfe

!      G13DSF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g13ddf, g13dsf, nag_wp, x04abf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: iset = 1, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: cgetol, chi, rlogl, siglev
       INTEGER                         :: dishow, i, idf, ifail, ip, iprint,   &
                                          iq, ishow, k, k2, kmax, ldcm, ldrcm, &
                                          liw, lpar, lwork, m, maxcal, mk2, n, &
                                          nadv, niter, npar
       LOGICAL                         :: exact, mean
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: cm(:,:), g(:), par(:), qq(:,:),      &
                                          r(:,:,:), r0(:,:), rcm(:,:), v(:,:), &
                                          w(:,:), work(:)
       INTEGER, ALLOCATABLE            :: iw(:)
       LOGICAL, ALLOCATABLE            :: parhld(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'G13DSF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

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

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

!      Calculate number of parameters for the VARMA
       k2 = k*k
       lpar = (ip+iq)*k2
       npar = lpar
       IF (mean) THEN
          lpar = lpar + k
       END IF

       kmax = k
       ldcm = lpar
       mk2 = m*k2
       ldrcm = mk2
       liw = k*max(ip,iq)
       lwork = k*(n+kmax+2) + mk2*(npar+mk2+1) + 3*k2
       ALLOCATE (par(lpar),parhld(lpar),qq(kmax,k),w(kmax,n),v(kmax,n), &
          g(lpar),cm(ldcm,lpar),r0(kmax,k),rcm(ldrcm,mk2),iw(liw),work(lwork), &
          r(kmax,kmax,mk2))

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

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

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

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

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

!      Read in the ISHOW flag for G13DSF
       READ (nin,*) ishow

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

!      Fit VARMA model
       ifail = -1
       CALL g13ddf(k,n,ip,iq,mean,par,lpar,qq,kmax,w,parhld,exact,iprint, &
          cgetol,maxcal,dishow,niter,rlogl,v,g,cm,ldcm,ifail)
       IF (ifail/=0) THEN
          IF (ifail<4) THEN
             GO TO 20
          END IF
       END IF

!      Titles
       WRITE (nout,*)
       WRITE (nout,*) 'Output from G13DSF'
       WRITE (nout,*)
       FLUSH (nout)

!      Calculate and display diagnostics
       ifail = 0
       CALL g13dsf(k,n,v,kmax,ip,iq,m,par,parhld,qq,ishow,r0,r,rcm,ldrcm,chi, &
          idf,siglev,iw,liw,work,lwork,ifail)

20     CONTINUE

    END PROGRAM g13dsfe
