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

    Module c06fjfe_mod

!     C06FJF 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                               :: readxy, writxy
!     .. Parameters ..
      Integer, Parameter, Public           :: 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 c06fjfe_mod

    Program c06fjfe

!     C06FJF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: c06fjf, c06gcf, nag_wp
      Use c06fjfe_mod, Only: nin, nout, readxy, writxy
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Integer                              :: ieof, ifail, lwork, n, ndim
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable      :: work(:), x(:), y(:)
      Integer, Allocatable                 :: nd(:)
!     .. Intrinsic Procedures ..
      Intrinsic                            :: maxval, product
!     .. Executable Statements ..
      Write (nout,*) 'C06FJF 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 = 3*maxval(nd(1:ndim))
        Allocate (x(n),y(n),work(lwork))

        Call readxy(nin,x,y,nd(1),nd(2))
        Write (nout,*)
        Write (nout,*) 'Original data values'
        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
        ifail = 0
!       Compute transform
        Call c06fjf(ndim,nd,n,x,y,work,lwork,ifail)

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

!       Compute inverse transform
        Call c06gcf(y,n,ifail)
        Call c06fjf(ndim,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

    End Program c06fjfe