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

    MODULE c06fxfe_mod

!      C06FXF 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,n3)
!         Read 3-dimensional complex data

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: n1, n2, n3, nin
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: x(n1,n2,n3), y(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)
                READ (nin,*) (y(i,j,k),k=1,n3)
             END DO
          END DO
          RETURN
!dec$ attributes offload : mic :: readxy
       END SUBROUTINE readxy

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

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: n1, n2, n3, nout
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x(n1,n2,n3), y(n1,n2,n3)
!         .. Local Scalars ..
          INTEGER                             :: i, j, k
!         .. Executable Statements ..
          DO i = 1, n1
             WRITE (nout,*)
             WRITE (nout,99998) 'z(i,j,k) for i =', i
             DO j = 1, n2
                WRITE (nout,*)
                WRITE (nout,99999) 'Real ', (x(i,j,k),k=1,n3)
                WRITE (nout,99999) 'Imag ', (y(i,j,k),k=1,n3)
             END DO
          END DO
          RETURN

99999     FORMAT (1X,A,7F10.3/(6X,7F10.3))
99998     FORMAT (1X,A,I6)
!dec$ attributes offload : mic :: writxy
       END SUBROUTINE writxy
    END MODULE c06fxfe_mod

    PROGRAM c06fxfe

!      C06FXF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : c06fxf, c06gcf, nag_wp
       USE c06fxfe_mod, ONLY : nin, nout, readxy, writxy
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       INTEGER                             :: ieof, ifail, n, n1, n2, n3
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: trign1(:), trign2(:), trign3(:), &
                                              work(:), x(:), y(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'C06FXF Example Program Results'
!      Skip heading in data file
       READ (nin,*)

LOOP:  DO
          READ (nin,*,IOSTAT=ieof) n1, n2, n3
          IF (ieof<0) EXIT LOOP

          n = n1*n2*n3
          ALLOCATE (trign1(2*n1),trign2(2*n2),trign3(2*n3),work(2*n),x(n), &
             y(n))

          CALL readxy(nin,x,y,n1,n2,n3)
          WRITE (nout,*)
          WRITE (nout,*) 'Original data values'
          CALL writxy(nout,x,y,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 c06fxf(n1,n2,n3,x,y,'Initial',trign1,trign2,trign3,work,ifail)

          WRITE (nout,*)
          WRITE (nout,*) 'Components of discrete Fourier transform'
          CALL writxy(nout,x,y,n1,n2,n3)

!         -- Compute inverse transform
          CALL c06gcf(y,n,ifail)
          CALL c06fxf(n1,n2,n3,x,y,'Subsequent',trign1,trign2,trign3,work, &
             ifail)
          CALL c06gcf(y,n,ifail)

          WRITE (nout,*)
          WRITE (nout,*) 'Original sequence as restored by inverse transform'
          CALL writxy(nout,x,y,n1,n2,n3)
          DEALLOCATE (trign1,trign2,trign3,work,x,y)
       END DO LOOP

    END PROGRAM c06fxfe
