#include "fpreproc/f77_dcomplx.h"
!------------------------------------------------------------------------------
!
!  SIGPREP_ADAS_Z -- SET ARGUMENTS FOR HARRY'S SIGMA ROUTINE FROM SIGTAB
!               REACTION NUMBER, SET KEY FOR TYPE OF INTEGRATION
!
SUBROUTINE SIGPREP_ADAS_Z(IREACT,iz1,iz2,ikey)
!
  use iso_c_binding, only: fp => c_double
  use sigtabl_mod     
  IMPLICIT NONE
!
!  29/06/09
!  This routine can be used to converte from ADAS reaction names to 
!  THE
!  CONVENTION USED IN HARRY TOWNER'S FUSION CROSS SECTION ROUTINES
!
! and set key for the
! KEY=1 FOR THERMONUCLEAR <SIGMA*V>
! key  = 2  for FOR BEAM TARGET <sig*v>
! key  = 3 for <E/A>
! key = 4 for <fd>
! key = 5  TRANSP BEAM BEAM GYRO INTEGRALS  See: preact/comptabl_z.for
!     react    : reaction type (
!                               1 = single ion impact ionisation (e.g. H +0 + H +1 => H +1 + H +1 + e)
!                               2 = full   ion impact ionisation (e.g. He+0 + H +1 => He+2 + H +1 + 2e)
!                               3 = single charge exchang        (e.g. He+0 + H +1 => He+1 + H +0)
!                               4 = full   charge exchang        (e.g. He+0 + He+2 => He+2 + He+0)
!                              )
! KREACT MUST be consistent with calls in sigsub/sigall_z.for
!============
! idecl:  explicitize implicit INTEGER declarations:
  INTEGER ireact !1 and 2 ='II', 3and 4 = 'CX'
  integer :: iZ1, iZ2 !     iZ1       : atomic charge of primary   particle {1, 2}
                          !     iZ2       : atomic charge of secondary particle {1, ..., 10}
  INTEGER ikey
!
!============
  IF(IREACT.EQ.2) THEN
     if(iZ1.eq.1) then
        if(iZ2.eq.1) then
           KREACT=-16        ! H0,H+ ii !ADAS: csIndex(2,1,1)=11. For the same reaction in PREACT KREACT=-2 
           KK=-1
        elseif(iZ2.eq.2) then
           KREACT=-17        ! He++,H0 ii !ADAS:csIndex(2,1,2)=12. For the same reaction in PREACT KREACT=-5
           KK=-1
        elseif(iZ2.eq.3) then
           KREACT=-24        ! Li+3 H0 ii !ADAS:csIndex(2,1,3)=13. 
           KK=-1
        elseif(iZ2.eq.4) then
           KREACT=-25        ! Be+4,H0 ii !ADAS:csIndex(2,1,4)=14. 
           KK=-1
        elseif(iZ2.eq.5) then
           KREACT=-26        ! B+5,H0 ii !ADAS:csIndex(2,1,5)=15. 
           KK=-1
        elseif(iZ2.eq.6) then
           KREACT=-27        ! C+6,H0 ii !ADAS:csIndex(2,1,6)=16. 
           KK=-1
        elseif(iZ2.eq.7) then
           KREACT=-28        ! N+7,H0 ii !ADAS:csIndex(2,1,7)=17. 
           KK=-1
        elseif(iZ2.eq.8) then
           KREACT=-29        ! O+8,H0 ii !ADAS:csIndex(2,1,8)=18. 
           KK=-1
        elseif(iZ2.eq.9) then
           KREACT=-30        ! F+9,H0 ii !ADAS:csIndex(2,1,9)=19. 
           KK=-1
        elseif(iZ2.eq.10) then
           KREACT=-31        ! Ne+10,H0 ii !ADAS:csIndex(2,1,10)=20. 
           KK=-1
        endif
     elseif(iZ1.eq.2) then
        if(iZ2.eq.1) then
           KREACT=-18        ! H+,He0 ii !ADAS: csIndex(2,2,1)=43. For the same reaction in PREACT KREACT=-4
           KK=-1
        elseif(iZ2.eq.2) then
           KREACT=-19        ! He++,He0 ii!ADAS: csIndex(2,2,2)=46. For the same reaction in PREACT KREACT=-7
           KK=-1
        elseif(iZ2.eq.3.or.iZ2.eq.5.or.iZ2.eq.7.or.iZ2.eq.9.or.iZ2&
             .ge.10) then
           call errmsg_exit('?sigprep_adas_z: invalid ZIMP value!')
        elseif(iZ2.eq.4) then
           KREACT=-32        ! Be+4,He0 ii!ADAS: csIndex(2,2,4)=48.
           KK=-1
        elseif(iZ2.eq.6) then
           KREACT=-33        ! C+6,He0 ii!ADAS: csIndex(2,2,6)=50.
           KK=-1
        elseif(iZ2.eq.8) then
           KREACT=-34        ! O+8,He0 ii!ADAS: csIndex(2,2,8)=52.
           KK=-1
        endif
     endif
  elseIF(IREACT.EQ.4) THEN
     if(iZ1.eq.1) then
        if(iZ2.eq.1) then
           KREACT=-20        ! H0,H+ cx !ADAS: csIndex(4,1,1)=1. For the same reaction in PREACT KREACT=-1 
           KK=-1
        elseif(iZ2.eq.2) then
           KREACT=-35        ! H0,He+2 cx !ADAS: csIndex(4,1,2)=2. 
           KK=-1
        elseif(iZ2.eq.3) then
           KREACT=-36        ! H0,Li+3 cx !ADAS: csIndex(4,1,3)=3. 
           KK=-1
        elseif(iZ2.eq.4) then
           KREACT=-37        ! H0,Be+4 cx !ADAS: csIndex(4,1,4)=4. 
           KK=-1
        elseif(iZ2.eq.5) then
           KREACT=-38        ! H0,B+5 cx !ADAS: csIndex(4,1,5)=5. 
           KK=-1
        elseif(iZ2.eq.6) then
           KREACT=-39        ! H0,C+6 cx !ADAS: csIndex(4,1,6)=6. 
           KK=-1
        elseif(iZ2.eq.7) then
           KREACT=-40        ! H0,N+7 cx !ADAS: csIndex(4,1,7)=7. 
           KK=-1
        elseif(iZ2.eq.8) then
           KREACT=-41        ! H0,O+8 cx !ADAS: csIndex(4,1,8)=8. 
           KK=-1
        elseif(iZ2.eq.9) then
           KREACT=-42        ! H0,F+9 cx !ADAS: csIndex(4,1,9)=9. 
           KK=-1
        elseif(iZ2.eq.10) then
           KREACT=-43        ! H0,Ne+10 cx !ADAS: csIndex(4,1,10)=10. 
           KK=-1
        endif
     elseif(iZ1.eq.2) then
        if(iZ2.eq.1) then
           KREACT=-21        ! H+,He0 cx !ADAS: csIndex(4,2,1)=1. For the same reaction in PREACT KREACT=-3
           KK=-1
        elseif(iZ2.eq.2) then
           KREACT=-22        ! He++,He0 cx !ADAS: csIndex(4,2,2)=20.For the same reaction in PREACT KREACT=-6
           KK=-1
        elseif(iZ2.eq.3.or.iZ2.eq.5.or.iZ2.eq.7.or.iZ2.eq.9.or.iZ2&
             .ge.10) then
           call errmsg_exit('?sigprep_adas_z: invalid ZIMP value!')
        elseif(iZ2.eq.4) then
           KREACT=-44        ! Be+4,He0 cx !ADAS: csIndex(4,2,4)=21.
           KK=-1
        elseif(iZ2.eq.6) then
           KREACT=-45        ! C+6,He0 cx !ADAS: csIndex(4,2,6)=22.
           KK=-1
        elseif(iZ2.eq.8) then
           KREACT=-46        ! O+8,He0 cx !ADAS: csIndex(4,2,8)=23.
           KK=-1
        endif
     endif
  elseIF(IREACT.EQ.1) THEN 
     if(iZ1.eq.1) then
        if(iZ2.eq.1) then
           KREACT=-16        ! H0,H+ ii !ADAS: csIndex(2,1,1)=11. For the same reaction in PREACT KREACT=-2 
           KK=-1
        elseif(iZ2.eq.2) then
           KREACT=-17        ! He++,H0 ii !ADAS:csIndex(2,1,2)=12. For the same reaction in PREACT KREACT=-5
           KK=-1
        elseif(iZ2.eq.3) then
           KREACT=-24        ! Li+3 H0 ii !ADAS:csIndex(2,1,3)=13. 
           KK=-1
        elseif(iZ2.eq.4) then
           KREACT=-25        ! Be+4,H0 ii !ADAS:csIndex(2,1,4)=14. 
           KK=-1
        elseif(iZ2.eq.5) then
           KREACT=-26        ! B+5,H0 ii !ADAS:csIndex(2,1,5)=15. 
           KK=-1
        elseif(iZ2.eq.6) then
           KREACT=-27        ! C+6,H0 ii !ADAS:csIndex(2,1,6)=16. 
           KK=-1
        elseif(iZ2.eq.7) then
           KREACT=-28        ! N+7,H0 ii !ADAS:csIndex(2,1,7)=17. 
           KK=-1
        elseif(iZ2.eq.8) then
           KREACT=-29        ! O+8,H0 ii !ADAS:csIndex(2,1,8)=18. 
           KK=-1
        elseif(iZ2.eq.9) then
           KREACT=-30        ! F+9,H0 ii !ADAS:csIndex(2,1,9)=19. 
           KK=-1
        elseif(iZ2.eq.10) then
           KREACT=-31        ! Ne+10,H0 ii !ADAS:csIndex(2,1,10)=20. 
           KK=-1
        endif
     elseif(iZ1.eq.2) then
        if(iZ2.eq.2) then
           KREACT=-23       ! He+2,He0 ii !ADAS: csIndex(1,2,2)=28. For the same reaction in PREACT KREACT=-7
           KK=-1
        elseif(iZ2.eq.1) then
           KREACT=-48       ! He+,He0 ii !ADAS: csIndex(1,2,1)=24
           KK=-1
        elseif(iZ2.eq.4) then
           KREACT=-49       ! Be+4,He0 ii !ADAS: csIndex(1,2,4)=31
           KK=-1
        elseif(iZ2.eq.6) then
           KREACT=-50       ! C+6,He0 ii !ADAS: csIndex(1,2,6)=34
           KK=-1
        elseif(iZ2.eq.8) then
           KREACT=-51       ! 0+8,He0 ii !ADAS: csIndex(1,2,8)=37
           KK=-1
        endif
     endif
  elseIF(IREACT.EQ.3) THEN 
     if(iZ1.eq.1) then
        if(iZ2.eq.1) then
           KREACT=-20        ! H0,H+ cx !ADAS: csIndex(4,1,1)=1. For the same reaction in PREACT KREACT=-1 
           KK=-1
        elseif(iZ2.eq.2) then
           KREACT=-35        ! H0,He+2 cx !ADAS: csIndex(4,1,2)=2. 
           KK=-1
        elseif(iZ2.eq.3) then
           KREACT=-36        ! H0,Li+3 cx !ADAS: csIndex(4,1,3)=3. 
           KK=-1
        elseif(iZ2.eq.4) then
           KREACT=-37        ! H0,Be+4 cx !ADAS: csIndex(4,1,4)=4. 
           KK=-1
        elseif(iZ2.eq.5) then
           KREACT=-38        ! H0,B+5 cx !ADAS: csIndex(4,1,5)=5. 
           KK=-1
        elseif(iZ2.eq.6) then
           KREACT=-39        ! H0,C+6 cx !ADAS: csIndex(4,1,6)=6. 
           KK=-1
        elseif(iZ2.eq.7) then
           KREACT=-40        ! H0,N+7 cx !ADAS: csIndex(4,1,7)=7. 
           KK=-1
        elseif(iZ2.eq.8) then
           KREACT=-41        ! H0,O+8 cx !ADAS: csIndex(4,1,8)=8. 
           KK=-1
        elseif(iZ2.eq.9) then
           KREACT=-42        ! H0,F+9 cx !ADAS: csIndex(4,1,9)=9. 
           KK=-1
        elseif(iZ2.eq.10) then
           KREACT=-43        ! H0,Ne+10 cx !ADAS: csIndex(4,1,10)=10. 
           KK=-1
        endif
     elseif(iZ1.eq.2) then
        if(iZ2.eq.2) then
           KREACT=-47       ! He+2,He0 non neutr. CX !ADAS: csIndex(3,2,2)=5
           KK=-1
        elseif(iZ2.eq.4) then
           KREACT=-52       ! Be+4,He0 non neutr. CX !ADAS: csIndex(3,2,4)=8
           KK=-1
        elseif(iZ2.eq.5) then
           KREACT=-53       ! B+5,He0 non neutr. CX !ADAS: csIndex(3,2,5)=11
           KK=-1
        elseif(iZ2.eq.6) then
           KREACT=-54       ! C+6,He0 non neutr. CX !ADAS: csIndex(3,2,6)=12
           KK=-1
        elseif(iZ2.eq.7) then
           KREACT=-55       ! N+7,He0 non neutr. CX !ADAS: csIndex(3,2,7)=15
           KK=-1
        elseif(iZ2.eq.8) then
           KREACT=-56       ! O+8,He0 non neutr. CX !ADAS: csIndex(3,2,8)=16
           KK=-1
        elseif(iZ2.eq.10) then
           KREACT=-57       ! Ne+10,He0 non neutr. CX !ADAS: csIndex(3,2,10)=19
           KK=-1
        endif
     endif
     
  elseIF(IREACT.EQ.5) THEN  ! Impact ionization = single II + full II + non-neutralaizing CX
     if(iZ1.eq.1) then
        if(iZ2.eq.1) then
           KREACT=-58        ! H0,H+ ii
           KK=-1
        elseif(iZ2.eq.2) then
           KREACT=-59       ! He++,H0 ii 
           KK=-1
        elseif(iZ2.eq.3) then
           KREACT=-60        ! Li+3 H0 ii . 
           KK=-1
        elseif(iZ2.eq.4) then
           KREACT=-61        ! Be+4,H0 ii  
           KK=-1
        elseif(iZ2.eq.5) then
           KREACT=-62        ! B+5,H0 ii  
           KK=-1
        elseif(iZ2.eq.6) then
           KREACT=-63        ! C+6,H0 ii 
           KK=-1
        elseif(iZ2.eq.7) then
           KREACT=-64        ! N+7,H0 ii  
           KK=-1
        elseif(iZ2.eq.8) then
           KREACT=-65        ! O+8,H0 ii  
           KK=-1
        elseif(iZ2.eq.9) then
           KREACT=-66        ! F+9,H0 ii  
           KK=-1
        elseif(iZ2.eq.10) then
           KREACT=-67        ! Ne+10,H0 ii  
           KK=-1
        endif
     elseif(iZ1.eq.2) then
        if(iZ2.eq.1) then
           KREACT=-68       ! He+,He0  ii 
           KK=-1
        elseif(iZ2.eq.2) then
           KREACT=-69       ! He+2,He0 ii 
           KK=-1
        elseif(iZ2.eq.4) then
           KREACT=-70       ! Be+4,He0 ii 
           KK=-1
        elseif(iZ2.eq.6) then
           KREACT=-71       ! C+6,He0 ii 
           KK=-1
        elseif(iZ2.eq.8) then
           KREACT=-72       ! 0+8,He0 ii 
           KK=-1
        endif
     endif
     
  ELSE
     call errmsg_exit('?sigprep_adas_z: invalid IREACT argument value!')
  ENDIF
  call adas_setkey_z(ikey)
  RETURN
END SUBROUTINE SIGPREP_ADAS_Z
!------------------------------------------------------------------------------
!
!  Peter Richter (prichter@princeton.edu)
!  Princeton Plasma Physics Laboratory
!  August 2000
!
!  setkey_z.for - Set variable KEY in common block 'SIGTABL_Z'.
!
!------------------------------------------------------------------------------
!
SUBROUTINE ADAS_SETKEY_Z(IKEY)
!
  use iso_c_binding, only: fp => c_double
  use sigtabl_mod, only: key
  IMPLICIT NONE
!
!============
! idecl:  explicitize implicit INTEGER declarations:
  INTEGER, intent(in) :: ikey
!============
  KEY=IKEY
!
END SUBROUTINE ADAS_SETKEY_Z
!
!------------------------------------------------------------------------------
! 30Jun2000 fgtok -s r8_precision.sub "r8con.csh conversion"

