!   C06PVF Example Program Text
!   Mark 26.1 Release. NAG Copyright 2016.

    Module c06pvfe_mod

!     C06PVF 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, writy
!     .. Parameters ..
      Integer, Parameter, Public       :: nin = 5, nout = 6
    Contains
      Subroutine readx(nin,x,n1,n2)
!       Read 2-dimensional real data

!       .. Scalar Arguments ..
        Integer, Intent (In)           :: n1, n2, nin
!       .. Array Arguments ..
        Real (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 real data

!       .. Scalar Arguments ..
        Integer, Intent (In)           :: n1, n2, nout
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (In) :: x(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)
        End Do
        Return

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

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

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

99999   Format (1X,A,7F10.3,/,(6X,7F10.3))
      End Subroutine writy
    End Module c06pvfe_mod

    Program c06pvfe

!     C06PVF Example Main Program

!     .. Use Statements ..
      Use c06pvfe_mod, Only: nin, nout, readx, writx, writy
      Use nag_library, Only: c06pvf, c06pwf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Integer                          :: ieof, ifail, m, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: y(:)
      Real (Kind=nag_wp), Allocatable  :: x(:)
!     .. Executable Statements ..
      Write (nout,*) 'C06PVF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
loop: Do
        Read (nin,*,Iostat=ieof) m, n
        If (ieof<0) Then
          Exit loop
        End If
        Allocate (x(m*n),y((m/2+1)*n))

        Call readx(nin,x,m,n)
        Write (nout,*)
        Write (nout,*) 'Original data values'
        Call writx(nout,x,m,n)

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
!       -- Compute transform
        Call c06pvf(m,n,x,y,ifail)

        Write (nout,*)
        Write (nout,*) 'Components of discrete Fourier transform'
        Call writy(nout,y,m/2+1,n)

!       -- Compute inverse transform
        x = 0._nag_wp
        Call c06pwf(m,n,y,x,ifail)

        Write (nout,*)
        Write (nout,*) 'Original sequence as restored by inverse transform'
        Call writx(nout,x,m,n)
        Deallocate (x,y)

      End Do loop

    End Program c06pvfe