module weiop_sls
  !! WEI slowness operator
  !! for FK-FX downward continuation

  use wei_util
  use wei_pro
  use wei_S
  use wei_attr
  use wei_dump
  !---------------
  implicit none

  real, dimension(:,:,:,:), pointer, private :: sxy
  real, private :: eds !! epsilon(ds)
  real, private :: ssalt
contains
  !----------------------------------------------------------------
  !! one reference slowness
  integer function weiop_sls1_init() result(st)
    integer :: er
#ifdef DEBUG
    character(len=128), parameter :: unit='weiop_sls_init'
    if(wpro%dbg>0) call in(unit)
#endif
    !! do nothing
#ifdef DEBUG
    if(wpro%dbg>0) call out(unit)
#endif
    st=weiOK
  end function weiop_sls1_init
  !----------------------------------------------------------------  
  !! one reference slowness
  integer function weiop_sls1(X) result(st)
    type(weslow), intent(inout) :: X
    integer :: izs
#ifdef DEBUG
    character(len=128), parameter :: unit='weiop_sls1'
    if(wpro%dbg>0) call in(unit)
#endif

    !! spread the slowness in its PBA
    call weiS_push(X)

    !! s(z): FKop slowness
    do izs=1,wpro%nzs
       slo%nr_s(izs) = 1 !! one reference slowness
       slo%nr_r(izs) = 1 !! one reference slowness

       slo%rs_s(izs,1) = weiS_mean(X,izs)
       slo%rs_r(izs,1) = weiS_mean(X,izs)
    end do

    slo%si_s=1.
    slo%si_r=1.

#ifdef DEBUG
    if(wpro%dbg>0) call out(unit)
#endif
    st=weiOK
  end function weiop_sls1
  !----------------------------------------------------------------
  !! N reference slownesses
  integer function weiop_slsN_init() result(st)
    integer :: er
#ifdef DEBUG
    character(len=128), parameter :: unit='weiop_slsN_init'
    if(wpro%dbg>0) call in(unit)
#endif

    allocate(sxy(wpro%amx%n,wpro%amy%n,wpro%ahx%n,wpro%ahy%n),stat=er)
    if(er/=0) call seperr('cant allocate sxy')

    eds = epsilon(1.)
    call from_param("ssalt",ssalt,0.)

#ifdef DEBUG
    if(wpro%dbg>0) call out(unit)
#endif
    st=weiOK
  end function weiop_slsN_init
  !----------------------------------------------------------------  
  !! N reference slownesses
  integer function weiop_slsN(X) result(st)
    type(weslow), intent(inout) :: X
    integer :: izs,ifk
    real    :: ds, so, sz
    real    :: sl,su
    real    :: smin,smax
#ifdef DEBUG
    character(len=128), parameter :: unit='weiop_slsN'
    if(wpro%dbg>0) call in(unit)
#endif

    !! spread the slowness in its PBAs    
    call weiS_push(X)

    !! min/max slowness in the interval
    izsloop: do izs=1,wpro%nzs
       slo%nr_s(izs) = wpro%nfk
       slo%nr_r(izs) = wpro%nfk

!!$       where(X%bin(:,:,izs)>ssalt)
!!$          smin = minval(real(X%bin(:,:,izs)))
!!$          smax = maxval(real(X%bin(:,:,izs)))
!!$       end where
       smin = weiS_min(X,izs)
       smax = weiS_max(X,izs)
       ds = (smax-smin) / wpro%nfk

       !! s(z): FKop slowness
       do ifk=1,wpro%nfk
          slo%rs_s(izs,ifk) = smin + (ifk-.5)*ds
          slo%rs_r(izs,ifk) = smin + (ifk-.5)*ds
       end do

       !! interpolation map
       slo%si_s(:,:,:,:,izs)=wpro%nfk
       slo%si_r(:,:,:,:,izs)=wpro%nfk

       !! source
       sxy(:,:,:,:) = slo%ss_s(:,:,:,:,izs)

       do ifk=1,wpro%nfk                        !! [sl...sz...su]
          sz = slo%rs_s(izs,ifk)
          sl = sz-ds/2-eds
          su = sz+ds/2
          where((sxy >= sl) .and. (sxy < su))
             slo%si_s(:,:,:,:,izs)=ifk
          end where
       end do
       slo%sw_s(:,:,:,:,izs)=slo%si_s(:,:,:,:,izs)

       !! receiver
       sxy(:,:,:,:) = slo%ss_r(:,:,:,:,izs)

       do ifk=1,wpro%nfk                        !! [sl...sz...su]
          sz = slo%rs_r(izs,ifk)
          sl = sz-ds/2-eds
          su = sz+ds/2
          where((sxy >= sl) .and. (sxy < su))
             slo%si_r(:,:,:,:,izs)=ifk
          end where
       end do
       slo%sw_r(:,:,:,:,izs)=slo%si_r(:,:,:,:,izs)

    end do izsloop

#ifdef DEBUG
    if(wpro%dbg>0) call out(unit)
#endif
    st=weiOK
  end function weiop_slsN
  !----------------------------------------------------------------
end module weiop_sls



