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

    Module c06pjfe_mod

!     C06PJF 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 c06pjfe_mod

    Program c06pjfe

!     C06PJF Example Main Program

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

        Write (nout,*)
        Write (nout,*) 'Components of discrete Fourier transform'
        Call writx(nout,x,nd(1),nd(2))

!       Compute inverse transform
        Call c06pjf('B',ndim,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

    End Program c06pjfe