    PROGRAM c06fbfe

!      C06FBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : c06faf, c06fbf, c06gbf, c06gsf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: ieof, ifail, j, m, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: u(:), v(:), work(:), x(:), xx(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'C06FBF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
LOOP:  DO
          READ (nin,*,IOSTAT=ieof) n
          IF (ieof<0) EXIT LOOP
          ALLOCATE (u(0:n-1),v(0:n-1),x(0:n-1),xx(0:n-1),work(n))
          READ (nin,*) x(0:n-1)
          xx(0:n-1) = x(0:n-1)

!         Convert x to separated real and imaginary parts for printing.
          ifail = 0
          m = 1
          CALL c06gsf(m,n,x,u,v,ifail)

!         ifail: behaviour on error exit   
!                =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
          ifail = 0
          CALL c06fbf(x,n,work,ifail)

          WRITE (nout,*)
          WRITE (nout,*) &
             'Original sequence and corresponding complex sequence'
          WRITE (nout,*)
          WRITE (nout,*) '         Data           Real      Imag'
          WRITE (nout,*)
          WRITE (nout,99999) (j,xx(j),'     ',u(j),v(j),j=0,n-1)
          WRITE (nout,*)
          WRITE (nout,*) 'Components of discrete Fourier transform'
          WRITE (nout,*)
          WRITE (nout,99998) (j,x(j),j=0,n-1)

          CALL c06faf(x,n,work,ifail)
          CALL c06gbf(x,n,ifail)

          WRITE (nout,*)
          WRITE (nout,*) 'Original sequence as restored by inverse transform'
          WRITE (nout,*)
          WRITE (nout,*) '        Original  Restored'
          WRITE (nout,*)
          WRITE (nout,99997) (j,xx(j),x(j),j=0,n-1)
          DEALLOCATE (u,v,x,xx,work)
       END DO LOOP

99999  FORMAT (1X,I5,F10.5,A,2F10.5)
99998  FORMAT (1X,I5,F10.5)
99997  FORMAT (1X,I5,2F10.5)
    END PROGRAM c06fbfe
