program WEIref_main
  use sep
  use mpi_sep 
  use wei_util 
  use wei_par 
  use wei_pro 
  use wei_tag 
  use wei_ref 
  use weiop_sll
  use weiop_slo
  use weiop_sls

  implicit none
  integer :: stat 
  integer :: mpierr 
  integer :: method
  !----------------------------------------------------------------
  call MPI_INIT(mpierr)
  call sep_init()
  !----------------------------------------------------------------
  !! init params for MPI and OMP
  call weipar_init()
  wpar%nmpi=1
  wpar%impi=0
  call weipar_report()
  !----------------------------------------------------------------
  if(wpar%nmpi>0) then
     if(wpar%impi==0) then
        call MPI_SEP_SEND_ARGS(wpar%nmpi,1,0)
     else
        call MPI_SEP_RECEIVE_ARGS()
     end if
  end if
  !----------------------------------------------------------------
  if(wpar%impi==0) call from_param("method",method,0)
  if(wpar%nmpi >0) call MPI_BCAST(method, 1,MPI_INTEGER,0,MPI_COMM_WORLD,mpierr)
  !----------------------------------------------------------------  
  call weitag_init()
  call weipro_init()

  select case(method)
  case(2) !! lloyd
     call weiref_init(SLin=weiop_sllN_init)
  case(1) !! even w/ salt
     call weiref_init(SLin=weiop_slsN_init)
  case default
     call weiref_init(SLin=weiop_sloN_init)
  end select

  call weipro_report()
  if(wpro%verb) call weimessage("Start selector")

  select case(method)
  case(2) !! lloyd
     stat = weiref(SLop=weiop_sllN)
  case(1) !! even w/ salt
     stat = weiref(SLop=weiop_slsN)
  case(0)
     stat = weiref(SLop=weiop_sloN)
  end select

  if(wpro%verb) call weimessage("OK")
  !----------------------------------------------------------------  
  !! shutdown MPI
  call MPI_FINALIZE(mpierr) 
  !----------------------------------------------------------------
  call exit(0)
end program WEIref_main
