    PROGRAM g13cgfe

!      G13CGF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g13cgf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: erlw, erup, rfse
       INTEGER                         :: i, ifail, j, l, n, ng
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: er(:), rf(:), xg(:), xyig(:),        &
                                          xyrg(:), yg(:)
       REAL (KIND=nag_wp)              :: stats(4)
!      .. Executable Statements ..
       WRITE (nout,*) 'G13CGF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       READ (nin,*) ng, l, n

       ALLOCATE (xg(ng),yg(ng),xyrg(ng),xyig(ng),er(ng),rf(l))

!      Read in the statistics
       READ (nin,*) (stats(i),i=1,4)

!      Read in data
       READ (nin,*) (xg(i),yg(i),xyrg(i),xyig(i),i=1,ng)

!      Calculate noise spectrum
       ifail = -1
       CALL g13cgf(xg,yg,xyrg,xyig,ng,stats,l,n,er,erlw,erup,rf,rfse,ifail)
       IF (ifail/=0) THEN
          IF (ifail<2) THEN
             GO TO 20
          END IF
       END IF

!      Display results
       WRITE (nout,*) '           Noise spectrum'
       WRITE (nout,99999) (j-1,er(j),j=1,ng)
       WRITE (nout,*)
       WRITE (nout,*) 'Noise spectrum bounds multiplying factors'
       WRITE (nout,99998) 'Lower =', erlw, '     Upper =', erup
       WRITE (nout,*)
       WRITE (nout,*) 'Impulse response function'
       WRITE (nout,*)
       WRITE (nout,99999) (j-1,rf(j),j=1,l)
       WRITE (nout,*)
       WRITE (nout,99998) 'Impulse response function standard error =', rfse

20     CONTINUE

99999  FORMAT (1X,I5,F16.4)
99998  FORMAT (1X,A,F10.4,A,F10.4)
    END PROGRAM g13cgfe
