subroutine plot_fp_sv(n1,n2,evec1,fvec1)

  ! plot SV data from fpreact and adas
  use iso_c_binding, only: fp => c_double
  use physconst_mod, only: zero, one, zm3tocm3
  use fpreact_parameters
  use adas_mod, only : adas_zstop_sigvz,adas_btsigv,adas_bms
  implicit NONE

  ! data buffers -- uninitialized!

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

  real(fp) :: evec1(n1)             ! E-vector for f(E) plot
  real(fp) :: fvec1(n1),fvec(n1)             ! for f(E) plot
  real(fp), dimension(:), allocatable :: evec2, tvec2,rhvec2   ! E-vector, T-vector for f(E,T) plot
  real(fp), dimension(:,:,:), allocatable :: farr       ! real8for f(E,T) plot
  real(fp), dimension(:,:), allocatable :: wkvec   !work vector
  !----------------------------------
  integer luntrm,lnout
  logical lxout,lchar

  integer izneut,izimp,ihandle,idum1,idum2,ierr,i,ii
  integer, dimension(1) :: izimp_ar
  real(fp) erange1,erange2,trange1,trange2
  real(fp) :: targ_density1,targ_density2,targ_temp1,targ_temp2
  real(fp), dimension(:), allocatable :: conc       ! for impurity concentration from 0 to 1
  real(fp) :: targdensity(1)  ! for electron (target) density
  real(fp), dimension(1) :: targtemp  ! for target temperature
  real(fp), dimension(1) :: beam_energy ! for beam_energy
  real*4 :: r4_beam_energy !do not change real*4 to real(fp)
  character*60 zlbl1,zlbl2,zlbl3
  character*10 zbzi
  real(fp) zamu !impurit mass [amu]
  real(fp) zeova,zte,ztiova,zne,zz,zzf,ztxova
  character*140  :: adas_tables_file
  character*240 :: filename
  character*9 :: cizneut, cizion                !character izneut and izion

  luntrm=lnout(0)
  !----------------------------------
  if(lxout(0)) write(luntrm,*) &
       ' A -- ADAS [sigma*v](E/A,T/A,Ne) impurity stopping rate coefficient' 
  if(lxout(0)) write(luntrm,*) &
       ' P -- PREACT [sigma*v](E/A) impurity stopping rate coefficient' 
  if(lxout(0)) write(luntrm,*) &
       ' C -- COMPARE ADAS and PREACT impurity stopping rate coefficient' 
  if(lxout(0)) write(luntrm,*) &
       ' X -- ADAS/Excited state [sigma*v](E/A,T/A,Ne) impurity stopping rate coefficient' 
  if(lxout(0)) write(luntrm,*) &
       ' hit "," to exit' 
  call uread('select option:$')

if(lchar('P')) then !PREACT DATA
  zlbl1='preact/fpreact "SV" database'
  zlbl2='[sigma*v](E/A) impurity stopping rate coefficient'

  luntrm=lnout(0)
  write(luntrm,1001)
1001 format(/ &
         'plot of PREACT impurity-stopping of selected neutral specie as a'/ &
         'function of interaction energy-per-nucleon (E/A)'/ &
         ' hit "," to exit'/)

  izimp=6
  do
     izneut=0
     call ureadi('select neutral specie: 1 for H, 2 for He, 0 -- to exit:$',izneut)
     if(izneut.eq.0) exit
     izneut=max(1,min(2,izneut))

     call ureadi('select impurity specie: 6 for C+6, 8 for O+8:$',izimp)
     if((izimp.ne.6).and.(izimp.ne.8)) then
        call uermsi('Must select 6 or 8 for Zimp',izimp,0)
        cycle
     endif

     call zstop_handle(izneut,izimp,ihandle)
     call sv_coldtarget_erange(ihandle,erange1,erange2,idum1,idum2)
     call plot_fp_mkvec(erange1,erange2,n1,evec1)

     call zstop_sigv(izneut,izimp,evec1,n1,fvec1)

     zlbl3='stopping  impact, vs. Erel/A'
     if(izimp.eq.6) then
        if(izneut.eq.fpreact_Z_H) then
           zlbl3='stopping of H0 by C+6 impact, vs. Erel/A'
        else
           zlbl3='stopping of He0 by C+6 impact, vs. Erel/A'
        endif
     else if(izimp.eq.8) then
        if(izneut.eq.fpreact_Z_H) then
           zlbl3='stopping of H0 by O+8 impact, vs. Erel/A'
        else
           zlbl3='stopping of He0 by O+8 impact, vs. Erel/A'
        endif
     endif

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

  enddo
elseif(lchar('A')) then !ADAS DATA
   zlbl1='ADAS "SV" database'
   zlbl2='[sigma*v](E/A) impurity stopping rate coefficient'
   zlbl3='stopping  impact, vs. Erel/A'
  do
     izneut=0
     call ureadi('select neutral specie: 1 for H, 2 for He, 0 -- to exit:$',izneut)
     if(izneut.eq.0) exit
     izneut=max(1,min(2,izneut))
     
     if(izneut.eq.1) then
        call ureadi('select impurity specie: from 1 for H+1 to 10 Ne+10 or more:$',izimp)
     elseif(izneut.eq.2) then
        call ureadi('select impurity specie: 1 - H+1, 2 - HE++, 4 - Be+4, 6 - C+6, 8 - O+8 :$',izimp)
     endif
     
     erange1=1.0E-3_fp
     erange2=25000._fp
     call plot_fp_mkvec(erange1,erange2,n1,evec1)
     fvec1=zero
     
     call adas_zstop_sigvz(izneut,dble(izimp),evec1,n1,fvec1,ierr)
     
     call gaxspc(3) ! log-log
     call r8_grafx1(evec1,fvec1,n1,'KeV','m**3/sec',zlbl1,zlbl2,zlbl3)
     call gaxspc(1) ! restore default
     
  enddo
elseif(lchar('C')) then !ADAS vs PREACT  DATA
   zlbl1='PREACT/ADAS "SV" database'
   zlbl2='[sigma*v](E/A) impurity stopping rate coefficient'
   zlbl3='stopping  impact, vs. Erel/A'
   izimp=6
   do
      izneut=0
     call ureadi('select neutral specie: 1 for H, 2 for He, 0 -- to exit:$',izneut)
     if(izneut.eq.0) exit
     izneut=max(1,min(2,izneut))
     
     if(izneut.eq.1) then
        call ureadi('select impurity specie: from 1 for H+1 to 10 Ne+10:$',izimp)
        if((izimp.gt.10).or.(izimp.lt.1)) then
           call uermsi('Must select from 1 to 10 for Zimp',izimp,0)
           cycle
        endif
     elseif(izneut.eq.2) then
        call ureadi('select impurity specie: 1 - H+1, 2 - HE++, 4 - Be+4, 6 - C+6, 8 - O+8 :$',izimp)
        if((izimp.ne.1).and.(izimp.ne.2).and.(izimp.ne.4).and.(izimp.ne.6).and.(izimp.ne.8)) then
           call uermsi('Must select: 1 - H+1, 2 - HE++, 4 - Be+4, 6 - C+6, 8 - O+8 ',izimp,0)
           cycle
        endif
     endif
      
      call zstop_handle(izneut,izimp,ihandle)
      call sv_coldtarget_erange(ihandle,erange1,erange2,idum1,idum2)
      call plot_fp_mkvec(erange1,erange2,n1,evec1)
      
      if(izimp.le.6) then 
         call zstop_sigv(izneut,6,evec1,n1,fvec1)
         fvec1=fvec1*izimp/6
      elseif(izimp.ge.8) then 
         call zstop_sigv(izneut,8,evec1,n1,fvec1)
         fvec1=fvec1*izimp/8
      else
         call zstop_sigv(izneut,6,evec1,n1,fvec)
         fvec=fvec*(8-izimp)/2
         call zstop_sigv(izneut,8,evec1,n1,fvec1)
         fvec1=fvec1*(izimp-6)/2
         fvec1=fvec1+fvec
      endif
      fvec=0.0_fp
      call adas_zstop_sigvz(izneut,dble(izimp),evec1,n1,fvec,ierr)
      
      call gaxspc(3) ! log-log
      call r8_grafx2(evec1,fvec1,fvec,n1,'KeV','m**3/sec',zlbl1,zlbl2,zlbl3)
      call gaxspc(1) ! restore default
   enddo

else  if(lchar('X')) then !ADAS data/excited state impurity stopping rate coefficient
  luntrm=lnout(0)
  zlbl1='ADAS "SV-CX" database'
  izimp=6
  do
     izneut=0
     if(lxout(0)) write(luntrm,*)'   %fp_SV: ADAS data available only for H (06/23/09)'
     call ureadi('select neutral specie: 1 for H, 0 -- to exit:$',izneut)
     if(izneut.eq.0) exit
     izneut=1
     !MG     izneut=max(1,min(2,izneut))
     
     call ureadi('select impurity specie: from 1 for H+1 to 10 Ne+10:$',izimp)
     if((izimp.gt.10).and.(izimp.lt.1)) then
        call uermsi('Must select from 1 to 10 for Zimp',izimp,0)
        cycle
     endif
     write(cizneut,'(i5)') izneut
     write(cizion,'(i5)') izimp
     
!in[KeV/amu]     
     erange1=1.e-3_fp
     erange2=2.5e4_fp
![cm**-3]
     targ_density1=1.e12_fp
     targ_density2=1.e16_fp
!in[KeV/amu]     
     targ_temp1=1.1e-3_fp
     targ_temp2=1.e2_fp
     
     
     
     
     if(allocated(evec2))deallocate(evec2)
     allocate(evec2(n2))
     if(allocated(conc))deallocate(conc)
     allocate(conc(n2))
        
     call plot_fp_mkvec(erange1,erange2,n2,evec2)
     
     conc = one !only one species
     
     if(lxout(0)) write(luntrm,1002)
1002 format(/&
          & '    enter "E" -- for FUNCTION VS. target Ti/Ai AND target Ne, fixed beam energy/amu'/&
          & '    enter "N" -- for FUNCTION VS. beam Erel/Ab AND target Ti/Ai, fixed elecron density,'/&
          & '    enter "T" -- for FUNCTION VS. beam Erel/Ab AND target Ne, fixed target temperature/amu.'/)
     call uread('  %fp_SV: ENTER OPTION: $')
     
     if(lchar('N')) then
        
        zlbl2='<sigma*v>(Erel/Ab,Ti/Ai) impurity stopping rate coefficient'
        
        if(allocated(tvec2))deallocate(tvec2)
        allocate(tvec2(n2))
        if(allocated(farr))deallocate(farr)
        allocate(farr(n2,1,n2))
        
        
           if(lxout(0)) write(luntrm,*)'   %fp_SV: electron density range [cm^-3]: ',&
                &targ_density1,targ_density2
           call ureadf('  %fp_SV: enter elecron density [cm^-3]: $',targdensity(1))
           if(targdensity(1).lt.targ_density1.or.targdensity(1).gt.targ_density2) then
              call uermsf('Must select density from: ',targ_density1,0)
              call uermsf('                      to: ',targ_density2,0)
              cycle
           endif
           
           call plot_fp_mkvec(targ_temp1,targ_temp2,n2,tvec2)
           
           if(allocated(wkvec))deallocate(wkvec)
           allocate(wkvec(n2,2))
           izimp_ar=izimp
           call adas_bms(evec2(1:n2),n2, izneut,conc(1:1),tvec2(1:n2),n2-1+1,&
                &              dble(izimp_ar),targdensity(1:1),1,&
                &              farr(1:n2,1:1,1:n2), iERR)!compute not only diagonal elements
           !        farr(1:n2,1,1:n2) !m**3/sec
           !        tvec2 !temperature, keV/amu
           !        evec2 !energy, keV/amu
           wkvec(1:n2,1)=tvec2 !temperature, keV/amu
           wkvec(1:n2,2)=evec2 !energy kev/amu
           write(zbzi,'(''Zb='',i1,'', Zi='',i1)') izneut,izimp
           !     call gaxspc(3) ! log-log
           call r8_grf3f1(evec2,farr,tvec2,wkvec,n2,n2,n2, &
                'beam Erel/Ab','Impurity stop.rate ','target Ti/Ai', &
                'KeV(Erel/Ab)','m**3/sec','KeV(Ti/Ai)',zlbl1,zbzi,0)
           !     call gaxspc(1) ! restore default
        else if(lchar('T')) then
           zlbl2='<sigma*v>(Erel/Ab, Ne) impurity stopping rate coefficient'
           if(allocated(tvec2))deallocate(tvec2)
           allocate(tvec2(n2))
           
           if(allocated(farr))deallocate(farr)
           allocate(farr(n2,n2,1))
           
           if(lxout(0)) write(luntrm,*)'   %fp_SV: target temperature range [KeV/amu]: ',&
                &targ_temp1,targ_temp2
           call ureadf('  %fp_SV: enter target temperature [KeV/amu]: $',targtemp(1))
           !       targtemp(1) ![KeV/amu]
           if(targtemp(1).lt.targ_temp1.or.targtemp(1).gt.targ_temp2) then
              call uermsf('Must select temperature from: ',targ_temp1,0)
              call uermsf('                          to: ',targ_temp2,0)
              cycle
           endif
           
           call plot_fp_mkvec(targ_density1,targ_density2,n2,tvec2)
           if(allocated(wkvec))deallocate(wkvec)
           allocate(wkvec(n2,2))
           
           izimp_ar=izimp
           do i=1,n2   
              wkvec(1:n2,1)=tvec2(i) !ne density [cm**-3]
              wkvec(1:n2,2)=evec2(i) ! energy [KeV]
           enddo
           call adas_bms(evec2,n2,izneut,conc,targtemp,1,&
                &              dble(izimp_ar),tvec2,n2,&
                &              farr, iERR)!compute not only diagonal elements
           ! farr(1:n2,1:n2,1) m**3/sec
           tvec2=tvec2*zm3tocm3 !ne density [m**-3]
           evec2=evec2 !energy, keV/amu
           wkvec(1:n2,1)=tvec2 !ne density [m**-3]
           wkvec(1:n2,2)=evec2 !energy kev/amu
           write(zbzi,'(''Zb='',i1,'', Zi='',i1)') izneut,izimp
           !     call gaxspc(3) ! log-log
           call r8_grf3f1(evec2,farr,tvec2,wkvec,n2,n2,n2, &
                'beam Erel/Ab','Impurity stop.rate','dens. Ne', &
                'KeV(Erel/Ab)','m**3/sec','1/m**3(density)',zlbl1,zbzi,0)
           !     call gaxspc(1) ! restore default
           
        else if(lchar('E')) then
           zlbl2='<sigma*v>(Ti/Ai, Ne) impurity stopping rate coefficient'
           if(allocated(rhvec2))deallocate(rhvec2)
           allocate(rhvec2(n2))
           
           if(allocated(tvec2))deallocate(tvec2)
           allocate(tvec2(n2))
           
           if(allocated(farr))deallocate(farr)
           allocate(farr(1,n2,n2))
           
           if(lxout(0)) write(luntrm,*)'   %fp_SV: beam energy range [KeV/amu]: ',&
                &erange1,erange2
           call ureadf('  %fp_SV:  beam energy range [KeV/amu]: $',r4_beam_energy)
           beam_energy(1)=r4_beam_energy*1.0_fp
           if(beam_energy(1).lt.erange1.or.beam_energy(1).gt.erange2) then
              call uermsf('Must select temperature from: ',erange1,0)
              call uermsf('                          to: ',erange2,0)
              cycle
           endif
           
           call plot_fp_mkvec(targ_temp1,targ_temp2,n2,tvec2) !temperature
           call plot_fp_mkvec(targ_density1,targ_density2,n2,rhvec2) !density
           if(allocated(wkvec))deallocate(wkvec)
           allocate(wkvec(n2,2))
           
           izimp_ar=izimp
           do ii=1,n2   
              wkvec(1:n2,2)=tvec2(ii) !temperature, KeV/amu
              do i=1,n2   
                 wkvec(1:n2,1)=rhvec2(i) !ne density [cm**-3]
              enddo
           enddo
           call adas_bms(beam_energy,1,izneut,conc,tvec2,n2,&
                &              dble(izimp_ar),rhvec2,n2,&
                &              farr,iERR)
           ! farr(1,1:n2,1:n2)  m**3/sec
           tvec2=tvec2 !temperature, keV/amu
           rhvec2=rhvec2*zm3tocm3 !ne density [m**-3]
           wkvec(1:n2,1)=rhvec2 !ne density [m**-3]
           wkvec(1:n2,2)=tvec2 !temperature,kev/amu
           write(zbzi,'(''Zb='',i1,'', Zi='',i1)') izneut,izimp
           !     call gaxspc(3) ! log-log
           call r8_grf3f1(tvec2,farr,rhvec2,wkvec,n2,n2,n2, &
                'Ti/Ai','Impurity stop.rate','dens. Ne', &
                'KeV(Ti/Ai)','m**3/sec','1/m**3(density)',zlbl1,zbzi,0)
           !     call gaxspc(1) ! restore default
           
        endif
     enddo
     
  endif

end subroutine plot_fp_sv
