!
! Calls the appropriate adpak routines for calculating ionization,
! recombination and radiation rates.  Calling all three will result
! in duplicate calls to ADDREC and ADRREC.  If this is a problem,
! then you may want to call ADERC directly.
!
!
! -------------- adp_ioniz_r8 ------------------
! return the electron ionization rate due to electron ionization
!
! adset or adreset must have been called before this subroutine
!
subroutine adp_ioniz_r8(te, ane, telion)
  use iso_c_binding, only: fp => c_double
  use adpak_mod
  implicit none
  save

  real(fp), intent(in)    :: te     ! electron temperature in KeV
  real(fp), intent(in)    :: ane    ! electron density in m-3

  real(fp), intent(out)   :: telion(*) ! ionization rate (sec-1)

  real(fp)    :: pte    ! electron temperature in KeV
  real(fp)    :: pne    ! electron density in cm-3
  integer :: jspc   ! loop variable

  !
  ! calculate rates in adpak
  !  
  pte = te
  pne = 1.e-6_fp*ane     ! convert from m-3 to cm-3
  call ADECI(PTE,PNE) ! fill RCLION, CIZLOS

  do jspc = 1, nspc
     telion(jspc) = rclion(jspc)
  end do
end subroutine adp_ioniz_r8

!
! --------------- adp_recom_r8 --------------
! return the recombination rate due to radiative and
! dielectronic recombination
!
! adset or adreset must have been called before this subroutine
!
subroutine adp_recom_r8(te, ane, telrec)
  use adpak_mod

  implicit none
  save

  real(fp), intent(in)    :: te     ! electron temperature in KeV
  real(fp), intent(in)    :: ane    ! electron density in m-3

  real(fp), intent(out)   :: telrec(*)   ! recombination rate (sec-1)

  real(fp)    :: pte    ! electron temperature in KeV
  real(fp)    :: pne    ! electron density in cm-3
  integer :: jspc   ! loop variable

  !
  ! calculate rates in adpak
  !  
  pte = te
  pne = 1.e-6_fp*ane    ! convert from m-3 to cm-3
  CALL ADDREC(PTE,PNE) ! fill RDIREC, RADDRC
  CALL ADRREC(PTE,PNE) ! fill RRAREC, RADRRC

  do jspc = 1, nspc
     telrec(jspc) = RRAREC(JSPC) + RDIREC(JSPC)
  end do
end subroutine adp_recom_r8

!
! --------------- adp_rad_r8 --------------
! return the radiation rate due to radiative and
! dielectronic recombination and collisional excitation
! and optionally brem...
!
! adset or adreset must have been called before this subroutine
!
subroutine adp_rad_r8(te, ane, ibrem, telrad)
  use adpak_mod

  implicit none
  save

  real(fp), intent(in)    :: te     ! electron temperature in KeV
  real(fp), intent(in)    :: ane    ! electron density in m-3
  integer, intent(in)   :: ibrem  ! 0-> do not include brem...
                                  ! 1-> include brem...

  real(fp), intent(out)   :: telrad(*)   ! radiation rate (joule/sec)

  real(fp)    :: pte    ! electron temperature in KeV
  real(fp)    :: pne    ! electron density in cm-3
  integer :: jspc   ! loop variable

  !
  ! calculate rates in adpak
  !  
  pte = te
  pne = 1.e-6_fp*ane      ! convert from m-3 to cm-3
  CALL ADDREC(PTE,PNE) ! fill RDIREC, RADDRC
  CALL ADRREC(PTE,PNE) ! fill RRAREC, RADRRC
  CALL ADECEX(PTE,PNE) ! fill RADCLX

  do jspc = 1, nspc
     telrad(jspc) = radrrc(jspc) + raddrc(jspc) + radclx(jspc)   
  end do

  if (ibrem/=0) then
     call ADBREM(PTE,PNE) ! fill RADBRM
     do jspc = 1, nspc
        telrad(jspc) = telrad(jspc) + radbrm(jspc)
     end do
  end if
end subroutine adp_rad_r8

!
! --------------- adp_brem_r8 --------------
! return the radiation rate due to brem...
!
! adset or adreset must have been called before this subroutine
!
subroutine adp_brem_r8(te, ane, telbrem)
  use adpak_mod

  implicit none
  save

  real(fp), intent(in)    :: te     ! electron temperature in KeV
  real(fp), intent(in)    :: ane    ! electron density in m-3

  real(fp), intent(out)   :: telbrem(*)   ! radiation rate (joule/sec)

  real(fp)    :: pte    ! electron temperature in KeV
  real(fp)    :: pne    ! electron density in cm-3
  integer :: jspc   ! loop variable

  !
  ! calculate rates in adpak
  !  
  pte = te
  pne = 1.e-6_fp*ane      ! convert from m-3 to cm-3
  call ADBREM(PTE,PNE) ! fill RADBRM
  do jspc = 1, nspc
     telbrem(jspc) = radbrm(jspc)
  end do
end subroutine adp_brem_r8

