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

    Module c06pyfe_mod

!     C06PYF 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 readx(nin,x,n1,n2,n3)
!       Read 3-dimensional real data

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

      Subroutine writx(nout,x,n1,n2,n3)
!       Print 3-dimensional real data

!       .. Scalar Arguments ..
        Integer, Intent (In)                 :: n1, n2, n3, nout
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (In)      :: x(n1,n2,n3)
!       .. Local Scalars ..
        Integer                              :: i, j, k
!       .. Executable Statements ..
        Do i = 1, n1
          Write (nout,*)
          Write (nout,99998) 'x(i,j,k) for i =', i
          Do j = 1, n2
            Write (nout,*)
            Write (nout,99999) 'Real ', (x(i,j,k),k=1,n3)
          End Do
        End Do
        Return

99999   Format (1X,A,4F10.3)
99998   Format (1X,A,I6)
      End Subroutine writx

      Subroutine writy(nout,y,n1,n2,n3)
!       Print 3-dimensional complex data

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

99999   Format (1X,A,4F10.3)
99998   Format (1X,A,I6)
      End Subroutine writy
    End Module c06pyfe_mod

    Program c06pyfe

!     C06PYF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: c06pyf, c06pzf, nag_wp
      Use c06pyfe_mod, Only: nin, nout, readx, writx, writy
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Integer                              :: ieof, ifail, n1, n2, n3
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable   :: y(:)
      Real (Kind=nag_wp), Allocatable      :: x(:)
!     .. Executable Statements ..
      Write (nout,*) 'C06PYF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
loop: Do
        Read (nin,*,Iostat=ieof) n1, n2, n3
        If (ieof<0) Exit loop

        Allocate (x(n1*n2*n3),y((n1/2+1)*n2*n3))

        Call readx(nin,x,n1,n2,n3)
        Write (nout,*)
        Write (nout,*) 'Original data values'
        Call writx(nout,x,n1,n2,n3)

!       ifail: behaviour on error exit   
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
!       -- Compute transform
        Call c06pyf(n1,n2,n3,x,y,ifail)

        Write (nout,*)
        Write (nout,*) 'Components of discrete Fourier transform'
        Call writy(nout,y,n1/2+1,n2,n3)

!       -- Compute inverse transform
        x = 0._nag_wp
        Call c06pzf(n1,n2,n3,y,x,ifail)

        Write (nout,*)
        Write (nout,*) 'Original sequence as restored by inverse transform'
        Call writx(nout,x,n1,n2,n3)
        Deallocate (x,y)

      End Do loop

    End Program c06pyfe
