SUBROUTINE em_field_custom(ele, param, s_rel, orb, local_ref, field, calcd, err_flag)
  USE bmad_struct
  USE bmad_interface, except_dummy => em_field_custom
  use em_field_interface

  IMPLICIT NONE

  type(ele_struct) :: ele
  type(lat_param_struct) param
  type(coord_struct) orb
  type(em_field_struct) field
  real(rp), intent(in) :: s_rel
  real(rp) t_rel 
  logical, optional :: calcd, err_flag
  logical  out_of_range,local_ref
  character(32) :: r_name = 'em_field_custom'

t_rel = orb%t 
if(ele%key == sbend$)then
  call em_field_custom_ring(ele, param, s_rel, t_rel, orb, local_ref, field, calcd, err_flag)
 else
  call em_field_custom_inj(ele, param, s_rel,t_rel, orb, local_ref,field,calcd, err_flag) 
endif

return
end

SUBROUTINE em_field_custom_ring(ele, param, s_rel, t_rel, orb, local_ref, field, calcd, err_flag)
  USE bmad_struct
  USE bmad_interface
  use em_field_interface, dummy=> em_field_custom_ring

  IMPLICIT NONE


  type(ele_struct) :: ele
  type(lat_param_struct) param
  type(coord_struct) orb
  type(em_field_struct) field
  real(rp), intent(in) :: s_rel, t_rel
  real(rp) x,y,s,t
  logical  out_of_range,local_ref
  logical, optional :: calcd, err_flag
  character(32) :: r_name = 'em_field_custom'

  x = orb%vec(1)
  y = orb%vec(3)
  s = s_rel
  t = orb%t  !t_rel            
  call fields(ele,x,y,s,t,field)

  RETURN
END SUBROUTINE em_field_custom_ring
