module OWvelo_areal
!--------------------------------------------------
! Compute vref
!

use OW_type
use OW_parms_areal

implicit none

contains

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

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

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

vref=0
if (nref .eq. 1) then
   vref(nref)=sum(vel)/kxm%n
   minv=vref(nref)
   maxv=minv
else
   minv=minval(vel)
   if (maxval(vel) >= vsal) then
       maxv=maxval(vel,1,vel < vsal)
       dvel=(maxv-minv)/(nref-2) 
       do iref=1,nref-1
          vref(iref)=minv+(iref-1)*dvel
       end do 
       vref(nref)=maxval(vel)
   else
          maxv=maxval(vel)
          dvel=(maxv-minv)/(nref-1) 
          do iref=1,nref
             vref(iref)=minv+(iref-1)*dvel
          end do 
   end if 
end if

end subroutine owvref

end module
