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=padtracesy+tapertry+1,kym%n-(padtracesy+tapertry)
      do i=padtracesx+tapertrx+1,kxm%n-(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/kym%n
do k=padtracesy+tapertry+1,kym%n-(padtracesy+tapertry)
   do i=padtracesx+tapertrx+1,kxm%n-(padtracesx+tapertrx)
      if (sum(abs(temp)**2+.1*maxval(abs(temp))) > 0) then
         GGS(i,k,:)=GGS(i,k,:)*abs(source)*abs(temp)/(abs(temp)**2+.1*maxval(abs(temp)))
      end if
   end do
end do
end subroutine
end module
