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

    Module c06pffe_mod

!     C06PFF 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
!       .. Executable Statements ..
        Do i = 1, n1
          Write (nout,*)
          Write (nout,99999)(x(i,j),j=1,n2)
        End Do
        Return

99999   Format (1X,7(:1X,'(',F6.3,',',F6.3,')'))
      End Subroutine writx
    End Module c06pffe_mod

    Program c06pffe

!     C06PFF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: c06pff, nag_wp
      Use c06pffe_mod, Only: nin, nout, readx, writx
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Integer                              :: ieof, ifail, l, lwork, n, ndim
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable   :: work(:), x(:)
      Integer, Allocatable                 :: nd(:)
!     .. Intrinsic Procedures ..
      Intrinsic                            :: product
!     .. Executable Statements ..
      Write (nout,*) 'C06PFF 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 = n + nd(l) + 15
        Allocate (x(n),work(lwork))
        Call readx(nin,x,nd(1),nd(2))
        Write (nout,*)
        Write (nout,*) 'Original data'
        Call writx(nout,x,nd(1),nd(2))

!       ifail: behaviour on error exit   
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
!       Compute transform
        Call c06pff('F',ndim,l,nd,n,x,work,lwork,ifail)

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

!       Compute inverse transform
        Call c06pff('B',ndim,l,nd,n,x,work,lwork,ifail)

        Write (nout,*)
        Write (nout,*) 'Original sequence as restored by inverse transform'
        Call writx(nout,x,nd(1),nd(2))
        Deallocate (nd,x,work)

      End Do loop

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