subroutine plot_fpreact

  ! main routine for uread/sglib `fpreact' data plotting tool.

  ! this routine sets up some temporary storage buffers, with
  ! user controllable sizing, and then calls a plotting routine for
  ! each type of atomic or nuclear table as requested by the user

  !  Mods
  !     20-Apr-2009  jim.conboy@jet.uk   to build Sat Jan 24 04:30:36 EST 2009
  !                  BUGFIX - wkvec is rank 2 !
  !     ----------------------------------------------------------------------

  use iso_c_binding, only: fp => c_double
  implicit NONE

  real(fp), dimension(:), allocatable :: &
       evec1, & ! Erel or Erel/A vector for 1d plots
       tvec1, & ! Te or Ti or Ti/Ai vector for 1d plots
       evec2, & ! Erel or Erel/A vector for 2d plots
       tvec2, & ! Ti or Ti/Ai vector for 2d plots
       fvec1    ! result vector for 1d plots
  real(fp), dimension(:,:), allocatable :: &
       wkvec    ! work vector

  real(fp), dimension(:,:), allocatable :: &
       farr    ! result array for 2d plots

  integer :: n1=501
  integer :: n2=201

  logical lxout,lchar
  integer luntrm,lnout

  save n1,n2

  luntrm=lnout(0)

  do

     if(allocated(evec1)) deallocate( &
          evec1,tvec1,evec2,tvec2,fvec1,wkvec,farr)

     allocate(evec1(n1),tvec1(n1),evec2(n2),tvec2(n2), &
          fvec1(n1),wkvec(max(n1,n2),2),farr(n2,n2))

     if(lxout(0)) write(luntrm,1001) n1,n2
1001 format(/ &
          ' 1d tables are dimensioned (N1), N1=',i7/ &
          ' 2d tables are dimensioned (N2,N2), N2=',i6 // &
          ' options:'/ &
          '  N -- change table dimensioning'/ &
          '  C -- plot charge exchange (cx) rate data'/ &
          '  I -- plot ion impact ionization (ii) rate data'/ &
          '  R -- plot impurity radiation and related data'/ &
          '  F -- plot fusion (fs) rate data'/ &
          '  E -- plot electron impact ionization (ei) rate data'/ &
          '  S -- plot impurity impact ionization (sv) rate data'/ &
          '  Q -- quit'/)

     call uread('plot_fpreact: select option:$')

     if(lchar('N')) then
        call ureadi('enter N1, in range [10:1000001]:$',n1)
        n1=max(10,min(1000001,n1))
        call ureadi('enter N2, in range [10:5001]:$',n2)
        n2=max(10,min(5001,n2))
     else if(lchar('C')) then
        call plot_fp_cx(n1,n2,evec1,fvec1,evec2,tvec2,wkvec,farr)
     else if(lchar('I')) then
        call plot_fp_ii(n1,n2,evec1,fvec1,evec2,tvec2,wkvec,farr)
     else if(lchar('R')) then
        call plot_fp_ce(n1,n2) !coronal equilibrium
     else if(lchar('F')) then
        call plot_fp_fs(n1,n2,evec1,tvec1,fvec1,evec2,tvec2,wkvec,farr)
     else if(lchar('E')) then
        call plot_fp_ei(n1,tvec1,fvec1)
     else if(lchar('S')) then
        call plot_fp_sv(n1,n2,evec1,fvec1)
     else if(lchar('Q')) then
        exit
     else
        call uermsg(' %plot_fpreact: unrecognized option',' ',0)
     endif

  enddo

  if(allocated(evec1)) deallocate( &
       evec1,tvec1,evec2,tvec2,fvec1,wkvec,farr)

end subroutine plot_fpreact
