    PROGRAM g13agfe

!      G13AGF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g13agf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: c
       INTEGER                         :: ifail, npar, nst, nuv, nwa
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: anexr(:), anx(:), par(:), st(:), wa(:)
       INTEGER                         :: mr(7)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'G13AGF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size etc
       READ (nin,*) nuv, 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)
       nst = mr(4)*mr(7) + mr(5)*mr(7) + mr(2) + mr(3) + &
          max(mr(1),mr(6)*mr(7))
       nwa = 4*npar + 3*nst

       ALLOCATE (st(nst),anx(nuv),anexr(nuv),wa(nwa),par(npar))

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

!      Read in state set from G13AEF, G13AFF or previous call to G13AGF
       READ (nin,*) st(1:nst)

!      Read in new observations to update state set
       READ (nin,*) anx(1:nuv)

!      Update state set
       ifail = 0
       CALL g13agf(st,nst,mr,par,npar,c,anx,nuv,anexr,wa,nwa,ifail)

!      Display results
       WRITE (nout,*) 'The updated state set array now holds the values'
       WRITE (nout,99999) st(1:nst)
       WRITE (nout,*)
       WRITE (nout,99998) 'The residuals corresponding to the', nuv
       WRITE (nout,*) 'values used to update the system are'
       WRITE (nout,99999) anexr(1:nuv)

99999  FORMAT (1X,8F8.4)
99998  FORMAT (1X,A,I3,A)
    END PROGRAM g13agfe
