module OW_shaper

 use OW_type
 use OW_parms_areal

implicit none

contains

!===============================================
subroutine owshaper(source,GGS,sign)
 integer			:: i,j,sign
 complex			:: source(:),GGS(:,:),temp(sou%w%n), temp1(sou%w%n), phase(sou%w%n)

temp=0
   do i=1,kxm%n
      do j=1,sou%w%n
         temp(j)=temp(j)+sum(abs(GGS(max(1,j-10):min(j+10,sou%w%n),i)))/(min(j+10,sou%w%n)-max(1,j-10)+1)
      end do
   end do
   temp=temp/kxm%n
phase = cmplx(cos(pi/4.), sin(pi/4.)) * sqrt(sou%w%all)
!do i=xm%n
!   GGS(:,i)=GGS(:,i)*phase*sou%w%all**2
!end do
do i=1,kxm%n
   if (sum(abs(temp)**2+0.05*maxval(abs(temp))) > 0) then
      GGS(:,i)=GGS(:,i)*abs(source)*abs(temp)/(abs(temp)**2+0.05*maxval(abs(temp)))
   end if
end do
end subroutine
end module
