module OW_shaper

 use OW_type
 use OW_parms_areal

implicit none

contains

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

temp=0
do j=1,sou%w%n
   do k=1,kym%n-2*(padtracesy+tapertry)
      do i=1,kxm%n-2*(padtracesx+tapertrx)
         temp(j)=temp(j)+sum(abs(GGS(i,k,max(1,j-10):min(j+10,sou%w%n))))/(min(j+10,sou%w%n)-max(1,j-10)+1)
      end do
   end do
end do
temp=temp/(kxm%n-2*(padtracesx+tapertrx))/(kym%n-2*(padtracesy+tapertry))
do k=1,kym%n-2*(padtracesy+tapertry)
   do i=1,kxm%n-2*(padtracesx+tapertrx)
      if (sum(abs(temp)**2+.01*maxval(abs(temp))) > 0) then
         GGS(i,k,:)=GGS(i,k,:)*abs(source)*abs(temp)/(abs(temp)**2+.01*maxval(abs(temp)))
      end if
   end do
end do
end subroutine

end module
