    PROGRAM c09eafe

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : c09abf, c09eaf, c09ebf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, lda, ldb, m, n, nf, nwcm,  &
                                          nwcn, nwct, nwl
       CHARACTER (12)                  :: mode, wavnam, wtrans
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), ca(:,:), cd(:,:),    &
                                          ch(:,:), cv(:,:)
       INTEGER                         :: icomm(180)
!      .. Executable Statements ..
       WRITE (nout,*) 'C09EAF Example Program Results'

!      Skip heading in data file
       READ (nin,*)
!      Read problem parameters.
       READ (nin,*) m, n
       READ (nin,*) wavnam, mode
       WRITE (nout,99999) wavnam, mode

       lda = m
       ldb = m
       ALLOCATE (a(lda,n),b(ldb,n))

!      Read data array
       DO i = 1, m
          READ (nin,*) a(i,1:n)
       END DO

       WRITE (nout,99998) 'Input Data                    A'
       DO i = 1, m
          WRITE (nout,99997) a(i,1:n)
       END DO

!      Query wavelet filter dimensions
       wtrans = 'Single Level'

!      ifail: behaviour on error exit   
!      =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL c09abf(wavnam,wtrans,mode,m,n,nwl,nf,nwct,nwcn,icomm,ifail)
       nwcm = nwct/(4*nwcn)

       ALLOCATE (ca(nwcm,nwcn),cd(nwcm,nwcn),cv(nwcm,nwcn),ch(nwcm,nwcn))

       ifail = 0
       CALL c09eaf(m,n,a,lda,ca,nwcm,ch,nwcm,cv,nwcm,cd,nwcm,icomm,ifail)

       WRITE (nout,99998) 'Approximation coefficients   CA'
       DO i = 1, nwcm
          WRITE (nout,99997) ca(i,1:nwcn)
       END DO
       WRITE (nout,99998) 'Diagonal coefficients        CD'
       DO i = 1, nwcm
          WRITE (nout,99997) cd(i,1:nwcn)
       END DO
       WRITE (nout,99998) 'Horizontal coefficients      CH'
       DO i = 1, nwcm
          WRITE (nout,99997) ch(i,1:nwcn)
       END DO
       WRITE (nout,99998) 'Vertical coefficients        CV'
       DO i = 1, nwcm
          WRITE (nout,99997) cv(i,1:nwcn)
       END DO

       ifail = 0
       CALL c09ebf(m,n,ca,nwcm,ch,nwcm,cv,nwcm,cd,nwcm,b,ldb,icomm,ifail)

       WRITE (nout,99998) 'Reconstruction                B'
       DO i = 1, m
          WRITE (nout,99997) b(i,1:n)
       END DO

99999  FORMAT (/1X,'DWT ::'/1X,'       Wavelet : ',A/1X,'       End mode: ',A)
99998  FORMAT (/1X,A,' : ')
99997  FORMAT (1X,8(F8.4,1X):)
    END PROGRAM c09eafe
