module OW_vel
!--------------------------------------------------
! Compute vref
!

use OW_type
use OW_param

implicit none

contains

subroutine owvref(nref,velint,vel,vref,minv,maxv,owrk,nv)

integer				:: nref,iref,ixs,iz,nv
real				:: vel(:,:),owrk
real				:: vref(:,:),minv(:),maxv(:)
real				:: dvel,velint(:,:)

!--------------------------------------------------
! Pre-compute vref
!

vref=0
vel=0
maxv=0
minv=0
do ixs=1,rec%x%n
    owrk=(ixs-1)*rec%x%d+rec%x%o+rec%h%o
    if (owrk < img%xm%o .or. owrk > img%xm%o+img%xm%d*(img%xm%n-1)) call seperr('WORKING SPACE GREATER THAN IMAGE SPACE FOR SHOT # ',ixs)
! grab velocity in the workspace       
    nv=floor((owrk-v%xm%o)/v%xm%d)+1;
    vel=0;
    vel=velint(:,nv:nv+rec%h%n-1);
!
    do iz=1,v%z%n
        if (nref .eq. 1) then
           vref(nref,iz)=sum(vel(iz,:))/rec%h%n
        else
           minv(iz)=minval(vel(iz,:))
           maxv(iz)=maxval(vel(iz,:))
           dvel=(maxv(iz)-minv(iz))/(nref-1) 
           do iref=1,nref
              vref(iref,iz)=minv(iz)+(iref-1)*dvel
           end do 
        end if
    end do 
end do 
end subroutine owvref

end module
