module OW_hadam1

use OW_type
use OW_parms
use OW_fftw

implicit none

contains

subroutine owhadamard(CHD)

integer		:: CHD(:,:)
integer,allocatable :: HD(:,:)
complex,allocatable :: tfld(:)
integer		:: i,i1,i2,j1,j2,hij

      allocate(HD(sou%w%n,sou%w%n))
      do i1=1,sou%w%n
         do j1=1,sou%w%n
             j2=j1
             j2=iand(ieor(ishft(j2,1),ieor(i1,2*sou%w%n)),j2)
             do i2=1,2*sou%w%n-1
                j2=ieor(ishft(j2,-1),iand(j2,1))
             end do
             hij=(-1)**j2
             HD(i1,j1)=hij
         end do
      end do

!      allocate(tfld(sou%w%n))

!      call owfftw(tfld)
!
!      do i=1,sou%w%n
!         tfld=0.
!         tfld=cmplx(HD(i,:),0)
!         call sfftw_execute(planF)
!         CHD(i,:)=atan(imag(tfld)*real(tfld)/(real(tfld)*real(tfld)+tiny(1.)))
!      end do
!
!      deallocate(tfld,HD)
!
!      do i=1,2*sog%xm%n*sou%w%n
!         write(2,9) CHD(i,1:512)
!         write(5,9) real(CCHD(i,:))
!         write(4,9) imag(CCHD(i,:))
!      end do
!9 format(512F10.5)

end subroutine

end module
