SUBROUTINE mindex_nbfi(iz,ia,itype,inubeam)

use nbi_com
implicit NONE

! Return the label of the given fast ion specie
! 0 <-> fast ion specie is not matched

integer, intent(in) :: iz       ! Z of fast specie
integer, intent(in) :: ia       ! A of fast specie
integer, intent(in) :: itype    ! type of fast specie:
integer, intent(out) :: inubeam ! 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

inubeam=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
      inubeam=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
          inubeam=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 SUBROUTINE mindex_nbfi

