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

use OW_type
use OW_parms_areal
use lloyd_mod

implicit none

contains

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

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

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
!========================================================
subroutine owvref1(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)/size(vel)
   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 owvref1

!=========================================================
!subroutine ow_lloyd (slow,vref)
!
!real                                 :: slice(kxm%n*n(2))
!allocate(bound(nref),ref_slow(nref), junk(nref,2), slice(n(1)*n(2)))
!
!notfirst=.false.
!ntot=0
!iref=0
!slice(:,1)=slow
!iref=lloyd_go(slow,ref_slow,nref,bound,err,iref)
!ntot=ntot+iref
!notfirst=.true.
!junk=-1
!junk(1:iref,1)=ref_slow
!junk(1:iref,2)=bound
!vref=junk     
!end do


!---------------------------------------------------------
end module
