!   C06FUF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE c06fufe_mod

!      C06FUF 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
!dec$ attributes offload : mic :: readxy
       SUBROUTINE readxy(nin,x,y,n1,n2)
!         Read 2-dimensional complex data

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. 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
!dec$ attributes offload : mic :: readxy
       END SUBROUTINE readxy

!dec$ attributes offload : mic :: writxy
       SUBROUTINE writxy(nout,x,y,n1,n2)
!         Print 2-dimensional complex data

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. 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))
!dec$ attributes offload : mic :: writxy
       END SUBROUTINE writxy
    END MODULE c06fufe_mod

    PROGRAM c06fufe

!      C06FUF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : c06fuf, c06gcf, nag_wp
       USE c06fufe_mod, ONLY : nin, nout, readxy, writxy
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       INTEGER                             :: ieof, ifail, m, n, n_bi
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: trigm(:), trign(:), work(:),     &
                                              x(:), y(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'C06FUF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
LOOP:  DO
          READ (nin,*,IOSTAT=ieof) m, n
          IF (ieof<0) EXIT LOOP

          ALLOCATE (trigm(2*m),trign(2*n),work(2*m*n),x(m*n),y(m*n))
          CALL readxy(nin,x,y,m,n)
          WRITE (nout,*)
          WRITE (nout,*) 'Original data values'
          CALL writxy(nout,x,y,m,n)

!         ifail: behaviour on error exit   
!                =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
          ifail = 0
!         Compute transform
          CALL c06fuf(m,n,x,y,'Initial',trigm,trign,work,ifail)

          WRITE (nout,*)
          WRITE (nout,*) 'Components of discrete Fourier transform'
          CALL writxy(nout,x,y,m,n)

!         Compute inverse transform
          n_bi = m*n
          CALL c06gcf(y,n_bi,ifail)
          CALL c06fuf(m,n,x,y,'Subsequent',trigm,trign,work,ifail)
          CALL c06gcf(y,n_bi,ifail)

          WRITE (nout,*)
          WRITE (nout,*) 'Original sequence as restored by inverse transform'
          CALL writxy(nout,x,y,m,n)
          DEALLOCATE (trigm,trign,work,x,y)
       END DO LOOP

    END PROGRAM c06fufe
