module wei_ref
  !! WEI slowness selector

  use wei_util
  use wei_pro
  use wei_par
  use wei_tag
  use wei_comp

  use wei_S
  !---------------
  implicit none

  type(weslow), private :: SS,SR
contains
  !----------------------------------------------------------------
  subroutine weiref_init(SLin)
    integer            :: st,er
#ifdef DEBUG
    character(len=128), parameter :: unit='weiref_init'
#endif
    interface !!-------------------------
       integer function  SLin() result(st)
       end function SLin
    end interface !!---------------------
#ifdef DEBUG
    if(wpro%dbg>0) call in(unit)
#endif

    SS%tag=wtag%S;      call weiS_init(SS)
    SR%tag="Ref_Slo.H"; call weiS_scr_init(SR)

    !! allocate slowness arrays for FK/FX ops
    call weiS_fkfx_allocate()

    st=SLin()     !! SL op init

#ifdef DEBUG
    if(wpro%dbg>0) call out(unit)
#endif
  end subroutine weiref_init
  !----------------------------------------------------------------
  integer function weiref(SLop)
    integer,external,optional      :: SLop
    integer :: izb,iwb
    integer :: st
#ifdef DEBUG
    character(len=128), parameter :: unit='weiref'
    if(wpro%dbg>0) call in(unit)
#endif

    iwb=1
    zbA: do izb=1,wpro%nzb
       write(0,*) izb
       if(wpar%impi==0 .and. wpro%verb) call weipro_followup(izb,iwb)
       call weiS_read(SS,izb)      !! read a slowness block
       st = SLop(SS)               !! setup sS, sR, s(z)

!       !! create sloref file
!       !       call weiS_write(S2,izb)
    end do zbA

  end function weiref
  !----------------------------------------------------------------
  
end module wei_ref
