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
if (decon) then
   do i=padtraces+tapertr+1,kxm%n-(padtraces+tapertr)
      do j=1,sou%w%n
         temp(j)=temp(j)+sum(abs(GGS(i,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
   temp=temp/kxm%n
else
   temp=1
end if
phase = cmplx(cos(pi/4.), sin(pi/4.)) * sqrt(sou%w%all)
!do i=padtraces+tapertr+1,kxm%n-(padtraces+tapertr)
!   GGS(i,:)=GGS(i,:)*phase*sou%w%all**2
!end do
do i=padtraces+tapertr+1,kxm%n-(padtraces+tapertr)
   if (sum(abs(temp)**2+white*maxval(abs(temp))) > 0) then
      GGS(i,:)=GGS(i,:)*abs(source)*abs(temp)/(abs(temp)**2+white*maxval(abs(temp)))!*sign
   end if
end do
if (bandpass) then
   temp1=0
   do j=1,sou%w%n/2
      temp1(j)=temp(2*j)
   end do
   do i=padtraces+tapertr+1,kxm%n-(padtraces+tapertr)
      GGS(i,:)=GGS(i,:)*temp1
   end do
end if
end subroutine
end module
