!!.. read nzs depth step velocity or slowness
module wei_vel_mod

  implicit none
  logical, private :: lloyd
  integer, private :: nref
contains

!=====================================================================
!
  integer function wei_vel_init(lloyd_in, nref_in) result(stat)
    logical :: lloyd_in
    integer :: nref_in
    lloyd = lloyd_in
    nref  = nref_in
    stat  = 0
  end function
!=====================================================================
!
  integer function wei_vel_oper(vmod_slice, vref_slice) result(stat)
    real, dimension(:,:), pointer :: vmod_slice
    real, dimension(:)  , pointer :: vref_slice
    stat = 1
    call wei_get_vref_slice(vmod_slice, vref_slice)
    stat = 0
  end function
!======================================================================
  integer function wei_vel_clos() result(stat)
    lloyd = .false.
    nref  = 0
    stat  = 0
  end function
!======================================================================
!.. select the reference velocity for the current depth step
!.. Lloyd's method will be used
!----------------------------------------------------------------------
  subroutine wei_get_vref_slice(vmod_slice, vref_slice)
    real, dimension(:,:), pointer :: vmod_slice
    real, dimension(:)  , pointer :: vref_slice
    integer                       :: iref
    real                          :: vmax, vmin, dvel

    if(nref > size(vref_slice)) call seperr("ERROR: nref larger than the size of vref_slice(:)")

    if (lloyd) then
      write(0,*)"Currently not available..."
      STOP
    else
      vref_slice=0.0
      if(nref==1)then
         vref_slice(1)=sum(vmod_slice)/size(vmod_slice)
      else
         vmax=maxval(vmod_slice)
         vmin=minval(vmod_slice)
         dvel=(vmax-vmin)/(nref-1)
         do iref=1, nref
            vref_slice(iref)=vmin+(iref-1)*dvel
         end do
      end if
    end if
  end subroutine

end module
