module OW_grabvel

use OW_type
use OW_parms

implicit none

real,public,save					:: owrk
contains

subroutine owgrab_vel(ixs,velint,vel,ni)
integer	                                                  :: ni
integer						:: ixs, nv
real						:: velint(:,:), vel(:,:)!,owrk

! workspace origin 
!    owrk=(ixs-1)*rec%x%d+rec%x%o+rec%h%o
    owrk=(ixs-1)*rec%x%d+rec%x%o+rec%h%o-(padtraces+tapertr)*rec%h%d
! grab velocity in the workspace       
    nv = (owrk-v%xm%o)/v%xm%d+1.5
    ni = (owrk-img%xm%o)/img%xm%d+1.5
    vel=velint(:,nv:nv+rec%h%n-1+2*(padtraces+tapertr))
    if (owrk < img%xm%o .or. owrk > img%xm%o+img%xm%d*(img%xm%n-1)) then
         call seperr('WORKING SPACE GREATER THAN ')
         write(0,*)  'THE IMAGE SPACE FOR SHOT # ',ixs
    end if
end subroutine
!==============================================================
subroutine owgrab_refl(ixs,velint,vel,ni)
integer	                                                  :: ni
integer						:: ixs, nv
real						:: velint(:,:,:), vel(:,:,:)

! workspace origin 
!    owrk=(ixs-1)*rec%x%d+rec%x%o+rec%h%o
    owrk=(ixs-1)*rec%x%d+rec%x%o+rec%h%o-(padtraces+tapertr)*rec%h%d
! grab velocity in the workspace       
    nv = (owrk-v%xm%o)/v%xm%d+1.5
    ni = (owrk-img%xm%o)/img%xm%d+1.5
    vel=velint(:,:,nv:nv+rec%h%n-1+2*(padtraces+tapertr))
    if (owrk < img%xm%o .or. owrk > img%xm%o+img%xm%d*(img%xm%n-1)) then
         call seperr('WORKING SPACE GREATER THAN ')
         write(0,*)  'THE IMAGE SPACE FOR SHOT # ',ixs
    end if
end subroutine
!==============================================================
subroutine owreflc(velint,reflectivity)
real               :: velint(:,:),reflectivity(:,:,:)
integer            :: i1,i2

do i2 = 1,v%xm%n
   reflectivity(1,1,i2)=0.
   do i1 = 2,v%z%n
      reflectivity(i1,1,i2)=(velint(i1,i2)-velint(i1-1,i2))/sum(velint(i1-1:i1,i2))
   end do
end do

end subroutine
end module
