module OWvel
!--------------------------------------------------
! Compute vref
!

use OW_type
use OW_parms

implicit none

contains

subroutine owvref(nref,vel,vref,minv,maxv)

integer				:: nref,iref
real				:: vel(:), vref(:)
real				:: minv,maxv,dvel

vref=0
if (nref .eq. 1) then
   vref(nref)=sum(vel)/img%xm%n
   minv=vref(nref)
   maxv=minv
else
   minv=minval(vel)
   if (maxval(vel).ge.vsal) then
      maxv=maxval(vel,mask = vel.lt.vsal)
   else
      maxv=maxval(vel)
   end if
   dvel=(maxv-minv)/(nref-1) 
   do iref=1,nref
      vref(iref)=minv+(iref-1)*dvel
   end do 
end if

end subroutine owvref

end module
