subroutine plot_fp_cx(n1,n2,evec1,fvec1,evec2,tvec2,wkvec,farr)

  ! plot CX data from fpreact
  use adas_mod, only : adas_sigv,adas_btsigv
  use fpreact_parameters
  use iso_c_binding, only: fp => c_double

  implicit NONE

  ! data buffers -- uninitialized!

  integer, intent(in) :: n1,n2  ! dimension info

  real(fp) :: evec1(n1)             ! E/A-vector for f(E) plot
  real(fp) :: fvec1(n1)             ! for f(E) plot
  real(fp) :: evec2(n2),tvec2(n2)   ! E/A,T/A for f(E,T) plot
  real(fp) :: wkvec(max(n1,n2),2)
  real(fp) :: farr(n2,n2)

  !----------------------------------
  integer luntrm,lnout
  logical lxout,lchar

  integer izneut,izion,ihandle,idum1,idum2,iopt,i

  real(fp) :: erange1,erange2,trange1,trange2

  character*60 zlbl1,zlbl2,zlbl3

  character*30 flbl,flbl2
  character*15 funs
  character*10 zbzi

  real(fp), dimension(:,:), allocatable :: farr2
  real(fp), dimension(:), allocatable :: fvec1_adas
  real(fp), dimension(:), allocatable :: tevec1 ! T/A-vector for f(E) or f(E,T) plot
  integer fadas_CX,ii
  integer, parameter :: NEUTRAL=1, ION=2 ! beam type: =neutral or =ion
!----------------------------------
  luntrm=lnout(0)

  zlbl1='preact/fpreact "CX" database'
  zlbl2='[sigma*v](E/A) charge exchange rate coefficient'

  luntrm=lnout(0)
  if(lxout(0)) write(luntrm,1001)
1001 format(/ &
         'plot of charge exchange of selected neutral specie as a'/ &
         'function of interaction energy-per-nucleon (E/A), or...'/ &
         'beam-target <sigma*v>(Eb/Ab,Ti/Ai) table, or...'/ &
         '<Eav> (average energy sampled from Maxwellian by cx), or...'/ &
         '<fd>=<vb.vtarg/(|vb|*vth), normalized average component of v'/ &
         'sampled in the direction of the beam; vth=sqrt(kTi/Ai).'/ &
         '(hit "," to exit)'/)

  izion=0
  do
     izneut=0
     call ureadi('select neutral (beam) specie: 1 for H, 2 for He:$',izneut)
     if(izneut.eq.0) exit
     izneut=max(1,min(2,izneut))
     izion=izneut
     if(izneut.gt.1) then
        call ureadi('select ion specie: 1 for H+, 2 for He++:$',izion)
        izion=max(1,min(izneut,izion))
     endif

     if(lxout(0)) write(luntrm,*) '"2" for <Eav>, "3" for <fd>'
     if(lxout(0)) write(luntrm,*) &
          '"4" to compare <sigma*v>(Eb/Ai,Ti/Ai) with sigma(Er)*v(Er)'
     if(lxout(0)) write(luntrm,*) 'where Er=Eb/Ai+1.5*Ti/Ai.'

     if(allocated(farr2)) deallocate(farr2)

     call uread('enter "0" for cold target plot; "1" for beam-target table:$')

     iopt=0
     if(lchar('0')) then

        ! get [sigma*v] table range
        call hatom_handle(fpreact_CX,izneut,izion,ihandle)
        call cx_coldtarget_erange(ihandle,erange1,erange2,idum1,idum2)

        call plot_fp_mkvec(erange1,erange2,n1,evec1)
        if(lxout(0)) write(luntrm,*) &
             ' A -- ADAS [sigma*v]' 
        if(lxout(0)) write(luntrm,*) &
             ' P -- PREACT [sigma*v]' 
        if(lxout(0)) write(luntrm,*) &
             ' C -- COMPARE ADAS and PREACT ' 
        if(lxout(0)) write(luntrm,*) &
             ' hit "," to exit' 
        call uread('select option:$')
        if(izneut.eq.1) then
           zlbl3='charge exchange of H+ and H0, vs. Erel/A'
        else if(izneut.eq.2) then
           if(izion.eq.1) then
              zlbl3='charge exchange of H+ and He0, vs. Erel/A'
           else
              zlbl3='charge exchange of He++ and He0, vs. Erel/A'
           endif
        endif
        if(lchar('P')) then !PREACT DATA
           zlbl1='preact/fpreact "CX" database'
           call hatom_sigv(fpreact_CX,evec1,n1,izneut,izion,fvec1,idum1)
           call gaxspc(3) ! log-log
           call r8_grafx1(evec1,fvec1,n1,'KeV','m**3/sec',zlbl1,zlbl2,zlbl3)
           call gaxspc(1) ! restore default

        elseif(lchar('A')) then !ADAS DATA
           zlbl1='ADAS "CX" database'
           fadas_CX=1
           call adas_sigv(fadas_CX,evec1,n1,izneut,dble(izion),fvec1,idum1)

           call gaxspc(3) ! log-log
           call r8_grafx1(evec1,fvec1,n1,'KeV','m**3/sec',zlbl1,zlbl2,zlbl3)
           call gaxspc(1) ! restore default

        elseif(lchar('C')) then !ADAS vs PREACT  DATA
           zlbl1='PREACT/ADAS "CX" database'
           flbl2='CX sigma*v: Er=Eb/Ab+1.5*Ti/Ai'
           if(allocated(fvec1_adas)) deallocate(fvec1_adas)
           allocate(fvec1_adas(n1))
           fvec1_adas=0.0_fp
           call hatom_sigv(fpreact_CX,evec1,n1,izneut,izion, &
                fvec1,idum1)
           fadas_CX=1
           call adas_sigv(fadas_CX,evec1,n1,izneut,dble(izion),fvec1_adas,idum1)
           write(zbzi,'(''Zb='',i1,'', Zi='',i1)') izneut,izion
           call gaxspc(3) ! log-log
           call r8_grafx2(evec1,fvec1,fvec1_adas,n1,'KeV','m**3/sec',zlbl1,zlbl2,zlbl3)
           call gaxspc(1) ! restore default

        endif

     else if(lchar('1')) then

        iopt=fpreact_CX  ! cx beam target table

     else if(lchar('2')) then

        iopt=fpreact_EAV  ! <Eav> table

     else if(lchar('3')) then

        iopt=fpreact_FD   ! <fd> table

     else if(lchar('4')) then

        iopt=fpreact_CX  ! cx beam target table
        allocate(farr2(n2,n2))
        flbl2='CX sigma*v: Er=Eb/Ab+1.5*Ti/Ai'
     else
        call uermsg('plot_fp_ii: invalid option.',' ',0)
     endif

     if(iopt.gt.0) then
        ! get table range information
        call hatom_handle(iopt,izneut,izion,ihandle)
        if(iopt.eq.fpreact_CX) then
           call cx_warmtarget_erange(ihandle,erange1,erange2, &
                idum1,idum2)
           call cx_warmtarget_trange(ihandle,trange1,trange2, &
                idum1,idum2)
           flbl='<sigma*v> charge exchange'
           funs='m**3/sec'
        else if(iopt.eq.fpreact_EAV) then
           call cx_energyavg_peramu_erange(ihandle,erange1,erange2, &
                idum1,idum2)
           call cx_energyavg_peramu_trange(ihandle,trange1,trange2, &
                idum1,idum2)
           flbl='<Ei/Ai> charge exchange'
           funs='KeV'
        else if(iopt.eq.fpreact_FD) then
           call cx_average_vdotnorm_erange(ihandle,erange1,erange2, &
                idum1,idum2)
           call cx_average_vdotnorm_trange(ihandle,trange1,trange2, &
                idum1,idum2)
           flbl='<vb.vi/(|vb|*vth)>cx'
           funs=' '
        endif
        call plot_fp_mkvec(erange1,erange2,n2,evec2)
        call plot_fp_mkvec(trange1,trange2,n2,tvec2)
        if(lxout(0)) write(luntrm,*) &
             ' A -- ADAS <sigma*v>' 
        if(lxout(0)) write(luntrm,*) &
             ' P -- PREACT <sigma*v>' 
        if(lxout(0)) write(luntrm,*) &
             ' C -- COMPARE ADAS and PREACT ' 
        if(lxout(0)) write(luntrm,*) &
             ' hit "," to exit' 
        call uread('select option:$')
        if(lchar('P'))then
           farr=0.0_fp
           zlbl1='PREACT "CX" database'
           do i=1,n2
              wkvec(1:n2,1)=tvec2(i)
              wkvec(1:n2,2)=1.5_fp*tvec2(i)+evec2
              call hatom_btsigv(iopt,fpreact_NEUTRAL_BEAM, &
                   evec2,wkvec(1:n2,1),n2,izneut,izion, &
                   farr(1:n2,i),idum1)
              if(allocated(farr2)) then
                 call hatom_sigv(fpreact_CX,wkvec(1:n2,2),n2,izneut,izion, &
                      farr2(1:n2,i),idum1)
              endif
           enddo
           write(zbzi,'(''Zb='',i1,'', Zi='',i1)') izneut,izion
           if(allocated(farr2)) then
              call r8_grf3f2(evec2,farr,farr2,tvec2,wkvec,n2,n2,n2, &
                   'beam Eb/Ab',flbl,flbl2,'target Ti/Ai', &
                   'KeV(Eb/Ab)',funs,'KeV(Ti/Ai)',zlbl1,zbzi,0)
           else
              call r8_grf3f1(evec2,farr,tvec2,wkvec,n2,n2,n2, &
                   'beam Eb/Ab',flbl,'target Ti/Ai', &
                   'KeV(Eb/Ab)',funs,'KeV(Ti/Ai)',zlbl1,zbzi,0)
           endif
        else if(lchar('A'))then
           zlbl1='ADAS "CX" database'
           fadas_CX=1
           if(allocated(tevec1)) deallocate(tevec1)
           allocate(tevec1(n2))
           farr=0.0_fp
           do i=1,n2
              wkvec(1:n2,1)=tvec2(i)
              tevec1(1:n2)=tvec2(i)
              wkvec(1:n2,2)=1.5_fp*tvec2(i)+evec2
              call adas_btsigv(fadas_CX,NEUTRAL,evec2,tevec1,n2,izneut,dble(izion),farr(1:n2,i),idum1)
!           ikey=2 ! for FOR BEAM TARGET <sig*v>
              if(allocated(farr2)) then
                 call adas_sigv(fadas_CX,wkvec(1:n2,2),n2,izneut,dble(izion),fvec1(1:n2),idum1)
                 farr2(1:n2,i)=fvec1(1:n2)
              endif
           enddo
           write(zbzi,'(''Zb='',i1,'', Zi='',i1)') izneut,izion
           if(allocated(farr2)) then
              call r8_grf3f2(evec2,farr,farr2,tvec2,wkvec,n2,n2,n2, &
                   'beam Eb/Ab',flbl,flbl2,'target Ti/Ai', &
                   'KeV(Eb/Ab)',funs,'KeV(Ti/Ai)',zlbl1,zbzi,0)
           else
              call r8_grf3f1(evec2,farr,tvec2,wkvec,n2,n2,n2, &
                   'beam Eb/Ab',flbl,'target Ti/Ai', &
                   'KeV(Eb/Ab)',funs,'KeV(Ti/Ai)',zlbl1,zbzi,0)
           endif
        else if(lchar('C'))then
           farr=0.0_fp
           zlbl1='PREACT/ADAS "CX" database'
           fadas_CX=1 !cx
           wkvec(1:n2,2)=evec2
           do i=1,n2
              wkvec(1:n2,1)=tvec2(i)
              call hatom_btsigv(iopt,fpreact_NEUTRAL_BEAM, &
                   evec2,wkvec(1:n2,1),n2,izneut,izion, &
                   farr(1:n2,i),idum1)
           enddo
           if(allocated(tevec1)) deallocate(tevec1)
           allocate(tevec1(n2))
           if(allocated(farr2)) deallocate(farr2)
           allocate(farr2(n2,n2))
           farr2=0.0_fp
           wkvec(1:n2,1)=tvec2(1:n2)
           do i=1,n2
              tevec1=tvec2(i)
              call adas_btsigv(fadas_CX,NEUTRAL,evec2,tevec1,n2,izneut,dble(izion),farr2(1:n2,i),idum1)
!           ikey=2 ! for FOR BEAM TARGET <sig*v>
           enddo
           flbl='PREACT: <sigma*v> CX'
           flbl2='ADAS: <sigma*v> CX'
           write(zbzi,'(''Zb='',i1,'', Zi='',i1)') izneut,izion
           call r8_grf3f2(evec2,farr,farr2,tvec2,wkvec,n2,n2,n2, &
                'beam Eb/Ab',flbl,flbl2,'target Ti/Ai', &
                'KeV(Eb/Ab)',funs,'KeV(Ti/Ai)',zlbl1,zbzi,0)
           

        endif
     endif

  enddo

end subroutine plot_fp_cx
