! Module pour la derivation dans la direction 'n' d'un champ 2D represente
! sur une base d'ondelette de Daubechies. Algorithme basé sur le travail
! de Beylkin 1992, SIAM J. Numer. Anal. 6, 1716
!
! Module for computing the derivative of a 2D field represented on a
! Daubechies wavelet base. The algorithm is based on the work of Beylkin
! 1992, SIAM J. Numer. Anal. 6, 1716
!
! M. Rieutord le 11/7/2000
! 
! Encompass other wavelets in March 2001

	module deriw2d

contains

	function dx(f,n) result(value)

	use param_run
	implicit none

	real(kind(0d0)),pointer, dimension(:,:) :: f,value
	real(kind(0d0)), allocatable, dimension(:) :: r
	integer dim1,dim2,i,j,k,n,ll,L

! Calcul des coefficients de la matrice de derivation 'r'

! choose the wavelet
        if (wavelet == 'daub4') L=8
        if (wavelet == 'daub8') L=16
        if (wavelet == 'lemarie') L=16
        if (wavelet /= 'daub4' .and. wavelet /='daub8' .and. &
            wavelet /= 'lemarie') stop'in deriw2d: no wavelet supplied'

        allocate(r(-L+2:L-2))
        call coef_r(r,L)
	

	dim1=size(f,1) ; dim2=size(f,2)
	allocate(value(dim1,dim2))

	if (n ==1) then
! calcul de la derivee dans la direction '1'
         do j=1,dim2
          do i=1,dim1
           value(i,j)=0d0
           do ll=1,dim1
            if (i-ll >= -L+2 .and. i-ll <= L-2)  &
                     value(i,j)=value(i,j)+r(i-ll)*f(ll,j)
           enddo
          enddo
         enddo
!	 value=value*dim1
	else
         do i=1,dim1
          do j=1,dim2
           value(i,j)=0d0
           do ll=1,dim2
            if (j-ll >= -L+2 .and. j-ll <= L-2)  &
                     value(i,j)=value(i,j)+r(j-ll)*f(i,ll)
           enddo
          enddo
         enddo
!	 value=value*dim2
	endif
	deallocate(r)
	end function

	end module
