module OW_sll
  !! Lloyds Algorithm slowness operator
  !! for FK-FX downward continuation
  !! includes salt logic ??
  use OW_type
  use OW_lloyd

!---------------
  implicit none

  real, dimension(:,:), pointer, private :: sx
  real, dimension(:), pointer, private :: bound,sref,s_slice

contains
!----------------------------------------------------------------
  !! N reference slownesses
  integer function sll_init() result(st)
    integer :: er
    real::a

    allocate(bound(nref),sref(nref))
    allocate(sx(img%xm%n))
    call init_lloyd()
    st=OK
  end function sll_init


  !----------------------------------------------------------------  
  !! N reference slownesses
  integer function sll() result(st)
    integer :: izs,ifk
    real    :: ds, so, sz
    real    :: sl,su
    real    :: smin,smax
    integer :: imx, imy, i1,i2
    real    :: ssalt_true
    real    :: ssalt_mean,ssalt_sum
    real    :: slocal
    integer :: ins,ndo,ierr

    slo%nrs = nref
    do i1=1,img%xm%n
          sx(i1) =1/vel(i1)
    end do
    call lloyd_setup()
    st=OK
  end function sll
  !----------------------------------------------------------------
  subroutine lloyd_setup()
    integer :: i1,i2

    nnref=lloyd_go(sx,sref,nref,bound)
    slo%sz(izs,1:size(sref)) =sref
    slo%bound(izs,1:size(bound))= bound

    do  i2=1,size(sxy,2)
    do  i1=1,size(sxy,1)
       slo%si(i1,i2,izs)=1
       do while(sxy(i1,i2) > bound(slo%si(i1,i2,izs)))
          slo%si(i1,i2,izs)=slo%si(i1,i2,izs)+1
       end do
    end do
    end do
  end subroutine lloyd_setup
!----------------------------------------------------------------
end module OW_sll
