module OW_sembl

implicit none

integer, private					:: zn,gn,xn,rn,nwind
real, private					:: zd,gd,xd,rd,zo,go,xo,ro

contains

subroutine owsembl_init(zn_in,gn_in,xn_in,rn_in,zd_in,gd_in,xd_in,rd_in,zo_in,go_in,xo_in,ro_in,nwind_in)
integer						:: zn_in,gn_in,xn_in,rn_in,nwind_in
real						:: zd_in,gd_in,xd_in,rd_in,zo_in,go_in,xo_in,ro_in

zn=zn_in
gn=gn_in
xn=xn_in
rn=rn_in
zd=zd_in
gd=gd_in
xd=xd_in
rd=rd_in
zo=zo_in
go=go_in
xo=xo_in
ro=ro_in
nwind=nwind_in

end subroutine
!---------------------------------------------------------
subroutine owsembl(cig,semblance)

integer					:: iz,irho,ig,idtz,iwind
real					:: z0,rho,tg2,dtz,num,den,pi
real					:: cig(:,:),semblance(:,:)

pi=4*atan(1.)
do iz = 1,zn
   z0=(iz-1)*zd + zo
   do irho=1,rn
      rho=(irho-1)*rd+ro
      num=0;den=0
      do ig=1,gn
         tg2=tan(((ig-1)*gd+go)*pi/180.)**2
         dtz=(rho-1)*tg2*z0
         idtz=int(dtz/zd)+1.5+iz
         if ( idtz+int(nwind/2)+1 .le. zn .and. idtz-int(nwind/2)+1 .ge. 1) then
            do iwind=1,nwind
                  num=num+cig(idtz-int(nwind/2)+iwind,ig)
                  den=den+cig(idtz-int(nwind/2)+iwind,ig)**2
            end do
         else
            if ( idtz .le. zn .and. idtz .ge. 1) then
               num=num+cig(idtz,ig)
               den=den+cig(idtz,ig)**2
            end if
         end if
      end do
      if (den == 0) then
         semblance(iz,irho)=0.
      else
         semblance(iz,irho)=num**2/(gn*den)
      end if
   end do
end do

end subroutine owsembl
end module

