!   C06PUF Example Program Text
!   Mark 25 Release. NAG Copyright 2014.

    Module c06pufe_mod

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

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

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

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

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

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

    Program c06pufe

!     C06PUF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: c06puf, nag_wp
      Use c06pufe_mod, Only: nin, nout, readx, writx
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Integer                              :: ieof, ifail, m, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable   :: work(:), x(:)
!     .. Executable Statements ..
      Write (nout,*) 'C06PUF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
loop: Do
        Read (nin,*,Iostat=ieof) m, n
        If (ieof<0) Exit loop
        Allocate (work(m*n+n+m+30),x(m*n))
        Call readx(nin,x,m,n)
        Write (nout,*)
        Write (nout,*) 'Original data values'
        Call writx(nout,x,m,n)

!       ifail: behaviour on error exit   
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
!       -- Compute transform
        Call c06puf('F',m,n,x,work,ifail)

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

!       -- Compute inverse transform
        Call c06puf('B',m,n,x,work,ifail)

        Write (nout,*)
        Write (nout,*) 'Original sequence as restored by inverse transform'
        Call writx(nout,x,m,n)
        Deallocate (x,work)

      End Do loop

    End Program c06pufe