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

    Module c06fffe_mod

!     C06FFF 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 c06fffe_mod

    Program c06fffe

!     C06FFF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: c06fff, c06gcf, nag_wp
      Use c06fffe_mod, Only: nin, nout, readxy, writxy
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Integer                              :: ieof, ifail, l, lwork, n, ndim
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable      :: work(:), x(:), y(:)
      Integer, Allocatable                 :: nd(:)
!     .. Intrinsic Procedures ..
      Intrinsic                            :: product
!     .. Executable Statements ..
      Write (nout,*) 'C06FFF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
loop: Do
        Read (nin,*,Iostat=ieof) ndim
        If (ieof<0) Exit loop

        Allocate (nd(ndim))

        Read (nin,*) nd(1:ndim), l

        n = product(nd(1:ndim))
        lwork = 3*nd(l)
        Allocate (x(n),y(n),work(lwork))

        Call readxy(nin,x,y,nd(1),nd(2))

        Write (nout,*)
        Write (nout,*) 'Original data'
        Call writxy(nout,x,y,nd(1),nd(2))

!       ifail: behaviour on error exit   
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft

!       Compute transform
        ifail = 0
        Call c06fff(ndim,l,nd,n,x,y,work,lwork,ifail)

        Write (nout,*)
        Write (nout,99999) 'Discrete Fourier transform of variable ', l
        Call writxy(nout,x,y,nd(1),nd(2))

!       Compute inverse transform
        Call c06gcf(y,n,ifail)
        Call c06fff(ndim,l,nd,n,x,y,work,lwork,ifail)
        Call c06gcf(y,n,ifail)

        Write (nout,*)
        Write (nout,*) 'Original sequence as restored by inverse transform'
        Call writxy(nout,x,y,nd(1),nd(2))
        Deallocate (x,y,work,nd)
      End Do loop

99999 Format (1X,A,I1)
    End Program c06fffe
