subroutine r8_nbi_datckrf(nonlin,nmini,amini,xzmini,nrfmini, &
     mibs,nsfast,mrfmini,abeams,xzbeams,nlfbmfpp, &
     nrfhydr,nrfdeut,nrftrit,nrfhel3,nrfhel4, &
     mrfhydr,mrfdeut,mrftrit,mrfhel3,mrfhel4, &
     ierct)
 
  use iso_c_binding, only: fp => c_double
  implicit none
 
  !  from the rf tail species list amini(1:nmini),xzmini(1:nmini),
  !  set rf specie indices
 
  integer,intent(in) :: nonlin                   ! lun for messages
 
  integer,intent(in) :: nmini
  real(fp),intent(in) :: amini(nmini),xzmini(nmini)  ! a & z of rf minorities
 
  integer,intent(out) :: nrfmini(nmini)
 
  !  nrfmini(j) --> fast specie index for specie {amini(j),xzmini(j)}
  !     amini(j).eq.abeams(nrfmini(j)) when done.
 
  integer,intent(in) :: mibs  ! max length of fast species list
 
  integer,intent(inout) :: nsfast  ! length of fast species list
  ! ... on entry: no. of beam & fusion product species
  ! ... on exit: no. of beam + fusion product + rf tail species
 
  integer,intent(out) :: mrfmini(mibs)
 
  !  j=mrfmini(isb); j.gt.0 --> this is the rf tail specie {amini(j),xzmini(j)}
  !  where isb is a fast specie index in range [1,nsfast].
 
  !  following are "inout" because some elements already set for beams, etc.
 
  real(fp),intent(inout) :: abeams(mibs),xzbeams(mibs)  ! a & z of fast species
  logical,intent(inout) :: nlfbmfpp(mibs)          ! fpp flag of fast species
 
  integer,intent(out) :: nrfhydr,nrfdeut,nrftrit,nrfhel3,nrfhel4
  !  ...indices pointing to {abeams(...),xzbeams(...)}
 
  integer,intent(out) :: mrfhydr,mrfdeut,mrftrit,mrfhel3,mrfhel4
  !  ...indices pointing to {amini(...),xzmini(...)}
 
  integer,intent(inout) :: ierct   ! error flag -- incremented on error.
 
  !$r8real_input: amini,xzmini
 
  !  messages are printed on nonlin if an error occurs
  !---------------------------
  ! local
 
  integer i,iok
 
  !---------------------------
  !
  !
  !  minority (thermal) index
  !
  mrfhydr=0
  mrfdeut=0
  mrftrit=0
  mrfhel3=0
  mrfhel4=0
 
  !
  !  minority (fast ion) index
  !
  nrfhydr=0
  nrfdeut=0
  nrftrit=0
  nrfhel3=0
  nrfhel4=0
 
  !  clear index arrays
 
  nrfmini=0
  mrfmini=0
 
  !  loop over minority species
 
  do i=1,nmini
     iok=1
     if((abs(xzmini(i)-1.0_fp).lt.0.05_fp).and.(abs(amini(i)-1.0_fp).lt.0.05_fp)) then
        nrfhydr=nsfast+1
        mrfhydr=i
        nsfast=nrfhydr
     else if((abs(xzmini(i)-1.0_fp).lt.0.05_fp).and. &
          (abs(amini(i)-2.0_fp).lt.0.05_fp)) then
        nrfdeut=nsfast+1
        mrfdeut=i
        nsfast=nrfdeut
     else if((abs(xzmini(i)-1.0_fp).lt.0.05_fp).and. &
          (abs(amini(i)-3.0_fp).lt.0.05_fp)) then
        nrftrit=nsfast+1
        mrftrit=i
        nsfast=nrftrit
     else if((abs(xzmini(i)-2.0_fp).lt.0.05_fp).and. &
          (abs(amini(i)-3.0_fp).lt.0.05_fp)) then
        nrfhel3=nsfast+1
        mrfhel3=i
        nsfast=nrfhel3
     else if((abs(xzmini(i)-2.0_fp).lt.0.05_fp).and. &
          (abs(amini(i)-4.0_fp).lt.0.05_fp)) then
        nrfhel4=nsfast+1
        mrfhel4=i
        nsfast=nrfhel4
     else
        write(nonlin,9729) i,xzmini(i),i,amini(i)
9729    format(' ?datckb:  xzmini(',i2,')=',1pe10.3, &
             ' amini(',i2,')=',1pe10.3/ &
             '          not a supported minority specie.')
        ierct=ierct+1
        iok=0
     endif
 
     if(iok.eq.1) then
        if(nsfast.gt.mibs) then
           write(nonlin,*) '?nbi_datckrf:  too many fast species, limit is:', &
                mibs
           ierct=ierct+1
           nsfast=nsfast-1
        else
           mrfmini(nsfast)=i
           nrfmini(i)=nsfast
           xzbeams(nsfast)=xzmini(i)
           abeams(nsfast)=amini(i)
           nlfbmfpp(nsfast)=.true.
        endif
     endif
  enddo
 
end subroutine r8_nbi_datckrf
