module wei_sll
  !! WEI slowness operator
  !! for FK-FX downward continuation
  !! includes salt logic
  use lloyd_mod
  use wei_wavespace_mod
  use wei_slow_mod
  !---------------
  implicit none
  integer, private, save  :: nref
  type(sep3d), private,save :: schoice

contains
!!$
!!$=head1 NAME
!!$
!!$ weisll_init - Initialize multiple  slowness  lloyd operator
!!$
!!$=head1 SYNOPSIS
!!$
!!$ ierr=weisll_init()
!!$
!!$=head1 DESCRIPTION
!!$
!!$ Initialize multiple slowness operator lloyd
!!$
!!$=head1 SEE ALSO
!!$
!!$L<weisloN_init> L<weislo1> L<weislsN>
!!$
!!$=head1 LIBRARY
!!$
!!$ sepweif90
!!$
!!$=cut
!!$

  !----------------------------------------------------------------
  !! N reference slownesses
  integer function weisll_init() result(st)
    integer :: er,i
#ifdef DEBUG
    character(len=128), parameter :: unit='weisll_init'
    if(my_debug()) call in(unit)
#endif

    call from_param("nref",nref,1)
    call init_lloyd()


#ifdef DEBUG
    if(my_debug()) call out(unit)
#endif
    st=0
  end function weisll_init
!!$
!!$=head1 NAME
!!$
!!$ weisll - Multiple  slowness operator lloyd vel/find
!!$
!!$=head1 SYNOPSIS
!!$
!!$ ierr=weisll()
!!$
!!$=head1 DESCRIPTION
!!$
!!$  multiple slowness operator lloyd
!!$
!!$=head1 SEE ALSO
!!$
!!$L<weisloN_init> L<weislo1> L<weislsN_init>
!!$
!!$=head1 LIBRARY
!!$
!!$ sepweif90
!!$
!!$=cut
!!$
  !----------------------------------------------------------------  
  !! N reference slownesses
  integer function weisll() result(st)
    integer :: izs,ifk,i4,i3,i2,i1
    real    :: ds, so, sz
    real    :: sl,su
    real    :: smin,smax
    integer :: imx, imy
    real    :: ssalt_true
    real    :: ssalt_mean,ssalt_sum
    real    :: slocal
    real, pointer :: slow(:,:,:)
    real, pointer :: bound(:,:),slice(:),ref_slow(:,:)
    integer, pointer :: nrefs(:)
    integer :: ins,ndo,ierr,n
    type(sep3d) :: wsep

#ifdef DEBUG
    character(len=128), parameter :: unit='weisll'
    if(my_debug()) call in(unit)
#endif

   call grab_current_slow_block(slow)
   allocate(bound(nref,size(slow,3)),slice(size(slow,1)*size(slow,2)))
   allocate(ref_slow(nref,size(slow,3)),nrefs(size(slow,3)))
   call add_mem(size(bound)+size(slice)+size(ref_slow)+size(nrefs))
   do izs=1,size(slow,3) 
     slice=reshape(slow,(/size(slow)/))
     nrefs(izs)=lloyd_go(slice,ref_slow(:,izs),nref,bound(:,izs))
   end do
   call store_slowness_refs(nrefs,ref_slow,bound)
   call del_mem(size(bound)+size(ref_slow)+size(slice)+size(nrefs))
   deallocate(bound,ref_slow,slice,nrefs)
#ifdef DEBUG
    if(my_debug()) call out(unit)
#endif
    st=0
  end function weisll
end module wei_sll
