    PROGRAM c09abfe

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : c09abf, c09ecf, c09edf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, lda, ldb, lenc, m, n, nf,  &
                                          nwcn, nwct, nwl
       CHARACTER (10)                  :: mode, wavnam, wtrans
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), c(:)
       INTEGER, ALLOCATABLE            :: dwtlevm(:), dwtlevn(:)
       INTEGER                         :: icomm(180)
!      .. Executable Statements ..
       WRITE (nout,*) 'C09ABF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
!      Read problem parameters
       READ (nin,*) m, n
       lda = m
       ldb = m
       READ (nin,*) wavnam, mode
       ALLOCATE (a(lda,n),b(ldb,n))

       WRITE (nout,99999) wavnam, mode, m, n

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

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

!      Query wavelet filter dimensions
!      For Multi-Resolution Analysis, decomposition, wtrans = 'M'
       wtrans = 'Multilevel'

!      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)

       lenc = nwct
       ALLOCATE (c(lenc),dwtlevm(nwl),dwtlevn(nwl))

!      Perform Discrete Wavelet transform
       ifail = 0
       CALL c09ecf(m,n,a,lda,lenc,c,nwl,dwtlevm,dwtlevn,icomm,ifail)

       WRITE (nout,*)
       WRITE (nout,99997) nf
       WRITE (nout,99996) nwl
       WRITE (nout,99995)
       WRITE (nout,99994) dwtlevm(1:nwl)
       WRITE (nout,99993)
       WRITE (nout,99994) dwtlevn(1:nwl)
       WRITE (nout,99992) nwct
       WRITE (nout,*)
       WRITE (nout,99991)
       WRITE (nout,99998) c(1:nwct)

!      Reconstruct original data
       ifail = 0
       CALL c09edf(nwl,lenc,c,m,n,b,ldb,icomm,ifail)

       WRITE (nout,*)
       WRITE (nout,99990)
       DO i = 1, m
          WRITE (nout,99998) b(i,1:n)
       END DO

99999  FORMAT (1X,' Parameters read from file :: '/'     Wavelet : ',A10, &
          ' End mode : ',A10,' M = ',I10,' N = ',I10)
99998  FORMAT (8(F8.4,1X):)
99997  FORMAT (1X,' Length of wavelet filter :             ',I10)
99996  FORMAT (1X,' Number of Levels :                     ',I10)
99995  FORMAT (1X, &
          ' Number of coefficients in first dimension for each level : ')
99994  FORMAT (16X,8(I8,1X):)
99993  FORMAT (1X, &
          ' Number of coefficients in second dimension for each level : ')
99992  FORMAT (1X,' Total number of wavelet coefficients : ',I10)
99991  FORMAT (1X,' Wavelet coefficients C : ')
99990  FORMAT (1X,' Reconstruction       B : ')
    END PROGRAM c09abfe
