!   C06FUF Example Program Text
!   Mark 24 Release. NAG Copyright 2012.

    Module c06fufe_mod

!     C06FUF Example Program Module:
!            Parameters and User-defined Routines

!     .. Use Statements ..
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter                   :: nin = 5, nout = 6
    Contains
      Subroutine readxy(nin,x,y,n1,n2)
!       Read 2-dimensional complex data

!       .. Scalar Arguments ..
        Integer, Intent (In)                 :: n1, n2, nin
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Out)     :: x(n1,n2), y(n1,n2)
!       .. Local Scalars ..
        Integer                              :: i, j
!       .. Executable Statements ..
        Do i = 1, n1
          Read (nin,*)(x(i,j),j=1,n2)
          Read (nin,*)(y(i,j),j=1,n2)
        End Do
        Return
      End Subroutine readxy

      Subroutine writxy(nout,x,y,n1,n2)
!       Print 2-dimensional complex data

!       .. Scalar Arguments ..
        Integer, Intent (In)                 :: n1, n2, nout
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (In)      :: x(n1,n2), y(n1,n2)
!       .. Local Scalars ..
        Integer                              :: i, j
!       .. Executable Statements ..
        Do i = 1, n1
          Write (nout,*)
          Write (nout,99999) 'Real ', (x(i,j),j=1,n2)
          Write (nout,99999) 'Imag ', (y(i,j),j=1,n2)
        End Do
        Return

99999   Format (1X,A,7F10.3/(6X,7F10.3))
      End Subroutine writxy
    End Module c06fufe_mod

    Program c06fufe

!     C06FUF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: c06fuf, c06gcf, nag_wp
      Use c06fufe_mod, Only: nin, nout, readxy, writxy
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Integer                              :: ieof, ifail, m, n, n_bi
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable      :: trigm(:), trign(:), work(:),     &
                                              x(:), y(:)
!     .. Executable Statements ..
      Write (nout,*) 'C06FUF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
loop: Do
        Read (nin,*,Iostat=ieof) m, n
        If (ieof<0) Exit loop

        Allocate (trigm(2*m),trign(2*n),work(2*m*n),x(m*n),y(m*n))
        Call readxy(nin,x,y,m,n)
        Write (nout,*)
        Write (nout,*) 'Original data values'
        Call writxy(nout,x,y,m,n)

!       ifail: behaviour on error exit   
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
!       Compute transform
        Call c06fuf(m,n,x,y,'Initial',trigm,trign,work,ifail)

        Write (nout,*)
        Write (nout,*) 'Components of discrete Fourier transform'
        Call writxy(nout,x,y,m,n)

!       Compute inverse transform
        n_bi = m*n
        Call c06gcf(y,n_bi,ifail)
        Call c06fuf(m,n,x,y,'Subsequent',trigm,trign,work,ifail)
        Call c06gcf(y,n_bi,ifail)

        Write (nout,*)
        Write (nout,*) 'Original sequence as restored by inverse transform'
        Call writxy(nout,x,y,m,n)
        Deallocate (trigm,trign,work,x,y)
      End Do loop

    End Program c06fufe
