    Program g13cafe

!     G13CAF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: g13caf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: px
      Integer                          :: i, ic, ifail, iw, kc, l, lg, lxg,    &
                                          mtx, mw, nc, ng, nx, nxg
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: c(:), xg(:)
      Real (Kind=nag_wp)               :: stats(4)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max
!     .. Executable Statements ..
      Write (nout,*) 'G13CAF Example Program Results'
      Write (nout,*)

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

!     Read in the problem size
      Read (nin,*) nx, nc

!     Read in smoothing parameters
      Read (nin,*) mtx, ic, px, iw, mw, l, lg
      If (ic==0) Then
        Read (nin,*) kc
      End If

      If (ic==0) Then
        nxg = max(kc,l)
      Else
        nxg = l
      End If
      lxg = max(nxg,nx)
      Allocate (xg(lxg),c(nc))

!     Read in the data
      Read (nin,*) xg(1:nx)

!     Calculate smoothed spectrum
      ifail = -1
      Call g13caf(nx,mtx,px,iw,mw,ic,nc,c,kc,l,lg,nxg,xg,ng,stats,ifail)
      If (ifail/=0) Then
        If (ifail<4) Then
          Go To 100
        End If
      End If

!     Display results
      Write (nout,*) 'Covariances'
      Write (nout,99999) c(1:nc)
      Write (nout,*)
      Write (nout,99998) 'Degrees of freedom =', stats(1), &
        '      Bandwidth =', stats(4)
      Write (nout,*)
      Write (nout,99997) '95 percent confidence limits -     Lower =', &
        stats(2), '  Upper =', stats(3)
      Write (nout,*)
      Write (nout,*) &
        '      Spectrum       Spectrum      Spectrum       Spectrum'
      Write (nout,*) &
        '      estimate       estimate      estimate       estimate'
      Write (nout,99996)(i,xg(i),i=1,ng)

100   Continue

99999 Format (1X,6F11.4)
99998 Format (1X,A,F4.1,A,F7.4)
99997 Format (1X,A,F7.4,A,F7.4)
99996 Format (1X,I4,F10.4,I5,F10.4,I5,F10.4,I5,F10.4)
    End Program g13cafe
