    Program c06frfe

!     C06FRF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: c06frf, c06gcf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
      Character (7), Parameter         :: init_first = 'Initial'
      Character (10), Parameter        :: init_repeat = 'Subsequent'
!     .. Local Scalars ..
      Integer                          :: i, ieof, ifail, lwork, m, mn, n, n2
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: trig(:), work(:), x(:,:), y(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'C06FRF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
loop: Do
        Read (nin,*,Iostat=ieof) m, n
        mn = m*n
        n2 = 2*n
        lwork = 2*mn
        If (ieof<0) Exit loop

        Allocate (trig(n2),x(m,n),y(m,n),work(lwork))
        Do i = 1, m
          Read (nin,*) x(i,1:n)
          Read (nin,*) y(i,1:n)
        End Do
        Write (nout,*)
        Write (nout,*) 'Original data values'
        Do i = 1, m
          Write (nout,*)
          Write (nout,99999) 'Real ', x(i,1:n)
          Write (nout,99999) 'Imag ', y(i,1:n)
        End Do

!       ifail: behaviour on error exit   
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call c06frf(m,n,x,y,init_first,trig,work,ifail)

        Write (nout,*)
        Write (nout,*) 'Discrete Fourier transforms'
        Do i = 1, m
          Write (nout,*)
          Write (nout,99999) 'Real ', x(i,1:n)
          Write (nout,99999) 'Imag ', y(i,1:n)
        End Do

        Call c06gcf(y,mn,ifail)
        ifail = 0
        Call c06frf(m,n,x,y,init_repeat,trig,work,ifail)
        Call c06gcf(y,mn,ifail)

        Write (nout,*)
        Write (nout,*) 'Original data as restored by inverse transform'
        Do i = 1, m
          Write (nout,*)
          Write (nout,99999) 'Real ', x(i,1:n)
          Write (nout,99999) 'Imag ', y(i,1:n)
        End Do
        Deallocate (trig,x,y,work)
      End Do loop

99999 Format (1X,A,6F10.4)
    End Program c06frfe
