    PROGRAM g13cefe

!      G13CEF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g13cef, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: t
       INTEGER                         :: i, ifail, j, ng
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: ca(:), calw(:), caup(:), sc(:),      &
                                          sclw(:), scup(:), xg(:), xyig(:),    &
                                          xyrg(:), yg(:)
       REAL (KIND=nag_wp)              :: stats(4)
!      .. Executable Statements ..
       WRITE (nout,*) 'G13CEF Example Program Results'
       WRITE (nout,*)

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

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

!      Read in statistics
       READ (nin,*) stats(1:4)

       ALLOCATE (xg(ng),yg(ng),xyrg(ng),xyig(ng),ca(ng),calw(ng),caup(ng), &
          sc(ng),sclw(ng),scup(ng))

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

!      Calculate cross-amplitude spectrum
       ifail = -1
       CALL g13cef(xg,yg,xyrg,xyig,ng,stats,ca,calw,caup,t,sc,sclw,scup,ifail)
       IF (ifail/=0) THEN
          IF (ifail<2) THEN
             GO TO 20
          END IF
       END IF

!      Display results
       WRITE (nout,*) '      Cross amplitude spectrum'
       WRITE (nout,*)
       WRITE (nout,*) '                    Lower     Upper'
       WRITE (nout,*) '          Value     bound     bound'
       WRITE (nout,99999) (j-1,ca(j),calw(j),caup(j),j=1,ng)
       WRITE (nout,*)
       WRITE (nout,99998) 'Squared coherency test statistic =', t
       WRITE (nout,*)
       WRITE (nout,*) '         Squared coherency'
       WRITE (nout,*)
       WRITE (nout,*) '                    Lower     Upper'
       WRITE (nout,*) '          Value     bound     bound'
       WRITE (nout,99999) (j-1,sc(j),sclw(j),scup(j),j=1,ng)

20     CONTINUE

99999  FORMAT (1X,I5,3F10.4)
99998  FORMAT (1X,A,F12.4)
    END PROGRAM g13cefe
