subroutine nbfi_numfast(inum)

  !  return the number of fast species known to nubeam

  use nbi_com
  implicit none

  integer, intent(out) :: inum

  integer :: ierr

  call nbi_chk_inputs(ierr)  ! this assures labeling info is defined

  inum = nsfast

end subroutine nbfi_numfast

subroutine nbfi_lbl(lbls,idim,igot)

  !  return a label for each known fast specie, labels have this form...

  !  <id> will be H or D or T or He3 or He4 or (rarely) something from
  !  higher on the periodic table of elements

  !  <id>_MCBEAM  -- monte carlo beam specie
  !  <id>_MCFUSN  -- monte carlo fusion product specie
  !  <id>_FPBEAM  -- fokker planck beam specie (not computed by mc code)
  !  <id>_FPRFI   -- fokker planck rf minority ion specie (not by mc code)

  !  igot gives the number of labels set; igot=-1 if idim is too small

  use nbi_com

  implicit none

  integer, intent(in) :: idim    ! dimension of lbls character string array
  character*(*), dimension(idim), intent(out) :: lbls   ! label strings
  integer, intent(out) :: igot   ! number of label strings returned

  !---------------------------------------------
  !  local...

  character*10 zid
  integer idlen,i,iz,ia
  integer :: ierr,igot_h

  !---------------------------------------------

  call nbi_chk_inputs(ierr)  ! this assures labeling info is defined

  if(nsfast.gt.idim) then
     igot=-1
  else
     igot=nsfast
     igot_h=1
     do i=1,igot

        iz=xzbeams(i)+0.5
        ia=abeams(i)+0.5
        call namels(iz,ia,zid)
        idlen=len_trim(zid)

        if(i.gt.nsbeam) then
           lbls(i)=zid(1:idlen)//'_FPRFI'
        else if(nlfbmfpp(i)) then
           lbls(i)=zid(1:idlen)//'_FPBEAM'
        else if(nlfprod(i)) then
           if(zid(1:idlen).eq."H") then
              if(igot_h.eq.2) then
                 lbls(i)=zid(1:idlen)//'14_MCFUSN'
              elseif(igot_h.eq.1) then
                 lbls(i)=zid(1:idlen)//'_MCFUSN'
                 igot_h=igot_h+1
              else
                 igot=-1 ! this is an error: it should not be more that two protons
                 exit
              endif
           else
              lbls(i)=zid(1:idlen)//'_MCFUSN'
           endif
        else
           lbls(i)=zid(1:idlen)//'_MCBEAM'
        endif

     enddo
  endif

end subroutine nbfi_lbl

integer function index_nbfi(iz,ia,itype)

  use nbi_com
  implicit none

  ! return the index to the indicated fast ion specie
  ! this is the index used by nubeam to keep track of multiple
  ! fast ion species

  ! *** warning: index_nbfi returns 0 if no matching fast ion specie
  !     is found ***

  integer, intent(in) :: iz    ! z of fast specie
  integer, intent(in) :: ia    ! a of fast specie

  integer, intent(in) :: itype ! type of fast specie:

  !  itype=1  --  beam specie -- monte carlo slowing down
  !  itype=2  --  beam specie -- fpp slowing down
  !  itype=3  --  fusion product specie -- monte carlo slowing down
  !      (itype=4, fusion product, fpp slowing down -- not supported)
  !      (itype=5, rf tail specie, monte carlo slowing down -- not supported)
  !  itype=6  --  rf tail specie -- fpp slowing down

  !--------------------------------

  integer is,iia,iiz,iok
  integer :: ierr
  integer, save :: igot_h=1

  !--------------------------------

  call nbi_chk_inputs(ierr)  ! this assures labeling info is defined

  index_nbfi=0

  if(itype.eq.6) then

     ! look for rf tail specie

     do is=nsbeam+1,nsfast
        iia=amini(is-nsbeam)+0.5
        iiz=xzmini(is-nsbeam)+0.5
        if((ia.eq.iia).and.(iz.eq.iiz)) then
           index_nbfi=is
           exit
        endif
     enddo

  else

     ! look for beam or fusion product specie

     do is=1,nsbeam
        iia=abeams(is)+0.5
        iiz=xzbeams(is)+0.5
        iok=0
        if(itype.eq.1) then
           if((.not.nlfbmfpp(is)).and.(.not.nlfprod(is))) iok=1
        else if(itype.eq.2) then
           if(nlfbmfpp(is).and.(.not.nlfprod(is))) iok=1
        else if(itype.eq.3) then
           if((.not.nlfbmfpp(is)).and.nlfprod(is)) iok=1
        else if(itype.eq.4) then
           ! these do not occur as of 3/4/2002...(dmc)
           if(nlfbmfpp(is).and.nlfprod(is)) iok=1
        endif
        if(iok.eq.0) then
           cycle         ! wrong itype
        else
           if((ia.eq.iia).and.(iz.eq.iiz)) then
              index_nbfi=is
              if(itype.eq.3.and.ia.eq.1.and.iz.eq.1.and.(nlfsp.and.nlfsp14)) then
                 !fusion proton (3mev) and proton(14.7mev) are in this run
                 if(igot_h.eq.1.or.igot_h.eq.3) then
                    igot_h=igot_h+1
                    if(igot_h.eq.4) igot_h=1
                    exit
                 else
                    igot_h=igot_h+1
                    cycle
                 endif
              endif
              exit
           endif
        endif
     enddo

  endif

end function index_nbfi
