    PROGRAM g13cbfe

!      G13CBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g13cbf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: pw, px
       INTEGER                         :: i, ifail, kc, l, lg, lxg, mtx, mw,   &
                                          ng, nx
!      .. Local Arrays ..
       REAL (KIND=nag_wp)              :: stats(4)
       REAL (KIND=nag_wp), ALLOCATABLE :: xg(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'G13CBF Example Program Results'
       WRITE (nout,*)

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

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

!      Read in smoothing parameters
       READ (nin,*) mtx, px, mw, l, kc, lg
       IF (mw/=nx) THEN
          READ (nin,*) pw
       END IF

       lxg = max(kc,nx)
       ALLOCATE (xg(lxg))

!      Read in series
       READ (nin,*) xg(1:nx)

!      Calculate smooth spectrum
       ifail = -1
       CALL g13cbf(nx,mtx,px,mw,pw,l,kc,lg,xg,ng,stats,ifail)
       IF (ifail/=0) THEN
          IF (ifail<4) THEN
             GO TO 20
          END IF
       END IF

!      Display results
       IF (mw==nx) THEN
          WRITE (nout,*) 'No smoothing'
       ELSE
          WRITE (nout,99999) 'Frequency width of smoothing window = 1/', mw
       END IF
       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)
       WRITE (nout,*)

20     CONTINUE

99999  FORMAT (1X,A,I0)
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 g13cbfe
