    PROGRAM g13ajfe

!      G13AJF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g13ajf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: c, rms
       INTEGER                         :: i, ifail, ifv, ist, iw, kfc, nfv,    &
                                          npar, nst, nx, pp, qp
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: fsd(:), fva(:), par(:), st(:), w(:), &
                                          x(:)
       INTEGER                         :: isf(4), mr(7)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'G13AJF Example Program Results'
       WRITE (nout,*)

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

!      Read in problem size etc
       READ (nin,*) nx, nfv, kfc, c

!      Read in the orders
       READ (nin,*) mr(1:7)

!      Calculate NPAR and various array lengths
       npar = mr(1) + mr(3) + mr(4) + mr(6)
       ist = mr(4) + mr(7) + mr(2) + mr(5) + mr(3) + max(mr(1),mr(6)*mr(7))
       ifv = max(1,nfv)
       qp = mr(6)*mr(7) + mr(3)
       pp = mr(4)*mr(7) + mr(1)
       iw = 6*nx + 5*npar + qp*(qp+11) + 3*pp + 7
       ALLOCATE (par(npar),x(nx),st(ist),fva(ifv),fsd(ifv),w(iw))

!      Read in data
       READ (nin,*) x(1:nx)

!      Read in parameter estimates
       READ (nin,*) par(1:npar)

       ifail = 0
       CALL g13ajf(mr,par,npar,c,kfc,x,nx,rms,st,ist,nst,nfv,fva,fsd,ifv,isf, &
          w,iw,ifail)

!      Display results
       WRITE (nout,99999) 'The residual mean square is ', rms
       WRITE (nout,*)
       WRITE (nout,99998) 'The state set consists of ', nst, ' values'
       WRITE (nout,99997) st(1:nst)
       WRITE (nout,*)
       WRITE (nout,99996) 'The ', nfv, &
          ' forecast values and standard errors are -'
       WRITE (nout,99995) (fva(i),fsd(i),i=1,nfv)

99999  FORMAT (1X,A,F9.2)
99998  FORMAT (1X,A,I1,A)
99997  FORMAT (1X,4F11.4)
99996  FORMAT (1X,A,I2,A)
99995  FORMAT (10X,2F10.2)
    END PROGRAM g13ajfe
