      subroutine trxpl_mkgs2(tpath,ztime0,ztavg,ierr)
c
c  make GS2 namelists -- information on plasma species
c
c  also, extensive plotting of GS2-related profiles:  temperatures,
c    densities, derivatives, with optional normalization to "reference"
c    temperatures and densities.
c
c  profiles can be smoothed & GS2 namelists written at multiple spatial
c    locations.
c
c  ion species can be "aggregated" -- one species slot representing a
c    sum of several actual ion species (summed density, and <T>,<Z>,<A>
c    done so as to preserve Zeff and quasineutrality contribution).
c
c----------------------------------------------
c
      use trxpl_gs2_module
      use periodic_table_mod
c
      implicit NONE
c
      character*(*), intent(in) :: tpath    ! TRANSP run specification
      real*8, intent(in) :: ztime0          ! time of interest
      real*8, intent(in) :: ztavg           ! +/- delta(t) time averaging
      integer, intent(out) :: ierr          ! completion code, 0=OK
c
c-----------------------------------------------
      real*8 zrhomin,zrhomax,zrmin,zrmax,zzmin,zzmax,zmpme,zminm
c
      integer :: id,idz,id_g=0
      integer i,j,k,ierimp
      integer ifirst
      integer idum1,idum2,idum3,idum4
c
      integer isize,jsize
      character*10, dimension(:), allocatable :: zimplist,zimplabl
      integer, dimension(:), allocatable :: izim,icim
      real, dimension(:), allocatable :: aaim
c
      integer iansr
      logical lxout,lchar
      integer lnout,idcod
c
c  TRANSP multigraph information
c
      integer istype,imulti
      character*10 zname
      character*32 zunits
      character*64 zlabel
      integer infuns
      integer isigns(max_species)
      integer ias(max_species)
      integer izs(max_species)
      character*10 zmembs(max_species)
c
c
      integer nrho,nintrp,inper,ispecie,iert,istat
      real*8, dimension(:), allocatable :: rho_grid,rho_intrp
      real*8, dimension(:), allocatable :: data_grid,data_intrp
      real*8, dimension(:), allocatable :: deriv_grid,deriv_intrp
      real*8, dimension(:), allocatable :: zsum1,zsum2,zsum3,zsum4,
     >   zsum5,zsum6,zsum7
c
      real*8 zrat,zfac
c
      character*30 wlabel,wunits
      character*70 tlabel
c
c  multiplot labels & work arrays
c
      character*60 mnlab(3)
      character*17 kylab(5)
      integer, parameter :: max_mp=15
      character*10 lbfun(max_mp)
      integer iself(max_mp),nc(max_mp)
      real*8 zdum(max_mp),zconv
      integer infmp,idmp,iwant
      real*8, dimension(:,:), allocatable :: mprho,mpfun
c
      logical plotn,ideriv,nnorm
c-----------------------------------------------
c
      zmpme = 1.8362d3  ! mp/me, proton/electron mass ratio
      zminm = 1.0d-4    ! min temperature & density (instead of zero)
c
      tlabel=' '
      write(tlabel,999) ztime0,ztavg
 999  format(' time of interest = ',1pe11.4,' +/- ',1pe11.4,' seconds.')
c
      if((tpath.ne.transp_path).or.(ztime0.ne.transp_time).or.
     >   (ztavg.ne.transp_delt)) then
 
         transp_path = tpath
         transp_time = ztime0
         transp_delt = ztavg

         mpath0 = ' '
         mpath = ' '
         opath0 = ' '
         opath = ' '

         ofile_prefix='trgs2'
         ofile_suffix='naml.dat'

         username = ' '
         hostname = ' '

         call sget_user(username)
         call sget_host(hostname)
 
         num_species = 0

         tref_lbl='Te'
         num_reft = 1        ! Tref=Te by default, but Tref=Ti can be selected.
         num_refn = 1        ! nref=ne always!

         Tref=0
         nref=0

         tmjtx = .FALSE.     ! by default use same Ti for bulk & impurities
 
         mass_ref = 0.0d0
c
c  get minor radius from equilibrium
c
         call eq_rholim(zrhomin,zrhomax)
         call eq_glimrz(zrhomax,zrmin,zrmax,zzmin,zzmax,ierr)
         if(ierr.ne.0) then
            call uermsg('?trxpl_mkgs2:  eq_glimrz error during setup.',
     >         ' ',0)
            transp_path=' '
            return
         endif
c
         aminor = (zrmax-zrmin)/2.0d0
         R0 = (zrmax+zrmin)/2.0d0
c
         call eq_gfnum('g',id_g)
         call eq_rgetf(1,zrhomax,id_g,0,b_t0,ierr)  ! vacuum R*Bt
         if(ierr.ne.0)then
            call uermsg('?trxpl_mkgs2:  eq_rgetf("G") error.',
     >         ' ',0)
            transp_path=' '
            return
         endif
c
         b_t0=b_t0/R0                   ! reference B field, Tesla
c
c  query user about desired fit order ... & other options
c
         j=0
         do
            if(lxout(0)) then
               write(lnout(0),1001) tref_lbl,tmjtx,mass_ref, 
     >            mpath(1:len_trim(mpath)),opath(1:len_trim(opath)),
     >            ofile_prefix(1:len_trim(ofile_prefix)),
     >            ofile_suffix(1:len_trim(ofile_suffix))
 1001          format(/
     >            ' ==> trxpl_mkgs2 version: 13-Jun-2005 (dmc)'/
     >            ' TRXPL_MKGS2 configuration options:'/
     >            '     currently Tref = ',a/
     >            '     flag to use TX & TMJ = ',L1/
     >            ' "I" to make Tref = Ti (bulk ion temperature)'/
     >            ' "E" to make Tref = Te (electron temperature)'/
     >            ' "S" for separation of impurity and bulk ion'/
     >            '     temperatures (use TRANSP "TX" and "TMJ" data)'/
     >            ' "A" to assume same Ti for all thermal species'/
     >            '     (use TRANSP "TI" data)'/
     >           ' "M" to set reference mass (currently: ',1pd10.3,')'//
     >            '     directory for template files: ',a/
     >            '     directory for namelist files: ',a/
     >            '     default namelist filename prefix: ',a/
     >            '     default namelist filename suffix: ',a/
     >            ' "T" to set directory path for namelist templates'/
     >          ' "N" to set directory path for namelist output files'/
     >            ' "F" to set filename default prefix & suffix:'//
     >            ' "1" to choose C1 Hermite profiles and CONTINUE...'/
     >            ' "2" to choose C2 Spline profiles and CONTINUE...'/
     >            ' "Q" to quit (immediate exit from TRXPL_MKGS2)'/)
            endif
            call uread(
     >         'trxpl_mkgs2:  enter "1" for C1 Hermite, '//
     >         'enter "2" for C2 spline profiles:$')
            if(lchar('Q')) then
               ierr=1
               transp_path=' '
               return
            else if(lchar('1')) then
               iorder=1
               exit
            else if(lchar('2')) then
               iorder=2
               exit
            else if(lchar('T')) then
               call trxpl_gs2_path('TEMPLATE_FILES',mpath0,mpath)
            else if(lchar('N')) then
               call trxpl_gs2_path('OUTPUT_NAMELISTS',opath0,opath)
            else if(lchar('F')) then
               call ureadw('trxpl_mkgs2: default prefix:$',ofile_prefix)
               call ureadw('trxpl_mkgs2: default suffix:$',ofile_suffix)
            else if(lchar('I')) then
               ! option enabled dmc 13 May 2004 -- after collisionality
               ! formula verification/correction by M. Redi
               !  (see trxpl_gs2_get.for)
               num_reft=2
               tref_lbl='Ti'
            else if(lchar('E')) then
               num_reft=1
               tref_lbl='Te'
            else if(lchar('A')) then
               tmjtx=.FALSE.
            else if(lchar('S')) then
               tmjtx=.TRUE.
            else if(lchar('M')) then
               if(lxout(0)) write(lnout(0),*) ' enter 3 for "T"...'
               j=2
               call ureadi(
     >            'trxpl_mkgs2:  reference Mass: 1 for "H",'//
     >            ' 2 for "D":$',j)
               j=max(1,min(3,j))
               mass_ref = j
            else
               call uermsg('?trxpl_mkgs2:  unrecognized option.',' ',0)
            endif
         enddo
c
c  query user about reference mass (if it has not been set
c
         if(j.eq.0) then
            do 
               if(lxout(0)) write(lnout(0),*) ' enter 3 for "T"...'
               j=2                      ! default: "D"
               call ureadi(
     >            'trxpl_mkgs2:  reference Mass: 1 for "H",'//
     >            ' 2 for "D":$',j)
               if((j.lt.1).or.(j.gt.3)) then
                  call uermsg('?trxpl_mkgs2:  enter "1", "2" or "3".',
     >               ' ',0)
               else
                  mass_ref=j
                  exit
               endif
            enddo
         endif
c
c  clear smooth flags & non-thermal flags
c
         gsmoo=.FALSE.
         gaggr=.FALSE.
         fast_aggr=.FALSE.
         nsmoo=.FALSE.
         tsmoo=.FALSE.
         dx_smooth_n=0
         dx_smooth_t=0
c
         ltype=0
c
c  clear Z, A and profile id arrays
c
         Aplas = 0.0d0
         Zplas = 0.0d0
         id_zprof=0
         id_aprof=0
c
         id_norig=0
         id_nsmoo=0
c
         id_torig=0
         id_tsmoo=0
c
c------------------------------------
c
c  get the ion temperature
c
         call trx_prof('TI',zunits,iorder,id_ti,ierr)
         if(ierr.ne.0) then
            call uermsg('TRANSP profile access failed','TI',0)
            transp_path=' '
            return
         endif
c
         if(tmjtx) then
            call trx_prof('TX',zunits,iorder,id_tx,ierr)
            if(ierr.ne.0) then
               write(6,*) ' %warning -- reverting to use "TI" only.'
               call uermsg('TRANSP profile access failed','TX',0)
               tmjtx=.FALSE.
            endif
         endif
         if(tmjtx) then
            call trx_prof('TMJ',zunits,iorder,id_tmj,ierr)
            if(ierr.ne.0) then
               write(6,*) ' %warning -- reverting to use "TI" only.'
               call uermsg('TRANSP profile access failed','TMJ',0)
               tmjtx=.FALSE.
            endif
         endif
c
         if(.not.tmjtx) then
            id_tmj=id_ti
            id_tx=id_ti
         endif
c
c  get the electron data
c
         num_species = num_species + 1
         j = num_species
c
         Aplas(j) = 1.0d0/(mass_ref*zmpme)
         Zplas(j) = -1.0d0
         Tconv(j) = 1.0d0
c
         call trx_prof('NE',zunits,iorder,id_norig(j),ierr)
         id_ne=id_norig(j)
         if(ierr.ne.0) then
            call uermsg('TRANSP profile access failed','NE',0)
            transp_path=' '
            return
         endif
c
         call trx_prof('TE',zunits,iorder,id_torig(j),ierr)
         id_te=id_torig(j)
         if(ierr.ne.0) then
            call uermsg('TRANSP profile access failed','TE',0)
            transp_path=' '
            return
         endif
c
         ltype(j)=0
         slabel(j)='electron'
c
c  get Zeff (for comparisons)
c
         call trx_prof('ZEFFP',zunits,iorder,id_zeff,ierr)
         if(ierr.ne.0) then
            call uermsg('TRANSP profile access failed','ZEFFP',0)
            transp_path=' '
            return
         endif
c
c-------------------------------------
c
c  get the non-impurity thermal species data
c
         call rpmulti('PDENS',istype,zlabel,zunits,infuns,isigns,zmembs,
     >      ierr)
         if(ierr.ne.0) then
            call uermsg('TRANSP "PDENS" multigraph not found.',' ',0)
            transp_path=' '
            return
         endif
c
c  check status of members...
c
         call rdi_ckpdens(infuns,zmembs,ias,izs,ierr)
         if(ierr.ne.0) then
            call uermsg('TRANSP "PDENS" member: Z unknown:',
     >         zmembs(ierr),0)
            transp_path=' '
            return
         endif
c
c  setup each of the thermal ion species
c
         do i=1,infuns
            if(izs(i).gt.0) then
               num_species=num_species+1
               j=num_species
               ltype(j)=1
               if(num_reft.gt.1) num_reft=j ! be sure to get an ltype=1 specie
               id_torig(j)=id_tmj
               Aplas(j)=ias(i)/mass_ref
               Zplas(j)=izs(i)
               Tconv(j)=1.0d0
               call trx_prof(zmembs(i),zunits,iorder,id_norig(j),ierr)
               if(ierr.ne.0) then
                  call uermsg('TRANSP profile access failed',
     >               zmembs(i),0)
                  transp_path=' '
                  return
               endif
               slabel(j)=zmembs(i)(2:)
               k=len_trim(slabel(j))+2
               slabel(j)(k:)='thermal ion'
            endif
         enddo
c
c----------------------------------------
c
c  get the impurity thermal species data
c
         call rpnlist('profile:NIMP_',1,isize)
         if(isize.gt.0) then
            allocate(zimplist(isize))
            call rplist('profile:NIMP_',1,zimplist,isize,jsize,ierr)
            if(ierr.ne.0) then
               call uermsg(
     >            'TRANSP impurity profile information access error.',
     >            ' ',0)
               transp_path=' '
               deallocate(zimplist)
               return
            endif
         endif
c
c  OK:  look for impurity species.  If none are found, we will assume
c  a single impurity model (NIMP, XZIMP)
c
         jsize=0
         if(isize.gt.0) then
            allocate(aaim(isize),icim(isize),izim(isize))
            allocate(zimplabl(isize))
            do i=1,isize
               aaim(i)=0.0
               icim(i)=0
               izim(i)=0
               call uupper(zimplist(i))
               if((zimplist(i)(1:5).eq.'NIMP_').and.
     >            (index(zimplist(i)(6:10),'_').gt.0)) then
c  label has the TRANSP form for a specific impurity ion...
                  call rplabel(zimplist(i),zlabel,zunits,imulti,istype)
                  j=index(zlabel,' ')-1    ! TRANSP impurity ion code
c
c  impurity code has forms like "C+6", "O+8", "Ni+28", etc.
                  call inv_periodic_table(zlabel(1:j),.TRUE.,
     >               izim(i),aaim(i),icim(i))
                  if(izim(i).gt.0) then
                     jsize=jsize+1           ! found one...
                     zimplabl(i)=zlabel(1:j) ! save the id label
                  endif
               endif
            enddo
         endif
c
         ierimp=0
         if(jsize.eq.0) then
c  traditional single impurity model:  note Z & A can vary in time
            call rplabel('NIMP',zlabel,zunits,imulti,istype)
            if(istype.eq.1) then
               num_species=num_species+1
               j=num_species
               ltype(j)=2
               id_torig(j)=id_tx
               call trx_scal('XZIMP',zunits,Zplas(j),ierimp)
               if(ierimp.ne.0) call uermsg('XZIMP read failure',' ',0)
               if(ierimp.eq.0) then
                  call trx_scal('AIMP',zunits,Aplas(j),ierimp)
                  if(ierimp.ne.0) call uermsg('AIMP read failure',' ',0)
                  Aplas(j)=Aplas(j)/mass_ref
               endif
               Tconv(j)=1.0d0
               if(ierimp.eq.0) then
                  call trx_prof('NIMP',zunits,iorder,id_norig(j),ierimp)
                  if(ierimp.ne.0) call uermsg('NIMP read failure',' ',0)
                  slabel(j)='impurity'
                  k=len_trim(slabel(j))+2
                  slabel(j)(k:)='thermal ion'
               endif
            endif
         else
c  multi impurity model
            do i=1,isize
               if(izim(i).gt.0) then
                  num_species=num_species+1
                  j=num_species
                  ltype(j)=2
                  id_torig(j)=id_tx
                  Zplas(j)=icim(i)
                  Aplas(j)=aaim(i)/mass_ref
                  Tconv(j)=1.0d0
                  call trx_prof(zimplist(i),zunits,iorder,id_norig(j),
     >               ierimp)
                  if(ierimp.ne.0)
     >               call uermsg('read failure',zimplist(i),0)
                  slabel(j)=zimplabl(i)
                  k=len_trim(slabel(j))+2
                  slabel(j)(k:)='impurity thermal ion'
                  if(ierimp.gt.0) exit
               endif
            enddo
         endif
c
c  deallocate impurity access stuff
c
         if(isize.gt.0) then
            deallocate(zimplist)
            deallocate(zimplabl)
            deallocate(aaim,icim,izim)
         endif
c
c  report if there were errors
c
         if(ierimp.gt.0) then
            call uermsg(
     >         '?trxpl_mkgs2:  errors reading impurity ion data',' ',0)
            transp_path=' '
            return
         endif
c
c----------------------------------------
c  fast species...
c
         zrat=2.0d0/3.0d0
         call trxpl_gs2fas(3,'BDENSS','BDENS','EBEAM',zrat,ierr)
         if(ierr.ne.0) then
            call uermsg('?trxpl_mkgs2:  error reading beam species',
     >         ' ',0)
            transp_path=' '
            return
         endif
c
         zrat=1.0d0
         call trxpl_gs2fas(4,'RFDENS','NMINI','TMINI',zrat,ierr)
         if(ierr.ne.0) then
            call uermsg('?trxpl_mkgs2:  error reading RF minorities',
     >         ' ',0)
            transp_path=' '
            return
         endif
c
         zrat=2.0d0/3.0d0
         call trxpl_gs2fas(5,'FDENSS','FDENS','EFUSN',zrat,ierr)
         if(ierr.ne.0) then
            call uermsg('?trxpl_mkgs2:  error reading fusion products',
     >         ' ',0)
            transp_path=' '
            return
         endif
         write(lnout(0),*)
     >      ' (note-- "unrecognized item name" warnings referring to'
         write(lnout(0),*)
     >      ' BDENSS or RFDENS or FDENSS can be ignored; these messages'
         write(lnout(0),*)
     >  ' occur when a TRANSP run lacks a category of fast ion species)'
c
c----------------------------------------
c  all done acquiring species data
c
      endif
c
c----------------------------------------
c
c  get radial grid information
c
      call eq_xid_mag(id_ne,idum1,id_rho,idum2,idum3,idum4,ierr)
      if(id_rho.eq.0) call errmsg_exit('?trxpl_mkgs2: id_rho=0??')
      call eq_ngrid(id_rho,nrho)
c
      allocate(rho_grid(nrho),data_grid(nrho),deriv_grid(nrho))
c
      inper=10
      nintrp=inper*(nrho-1)+1
      allocate(rho_intrp(nintrp),data_intrp(nintrp),deriv_intrp(nintrp))
      allocate(zsum1(nintrp),zsum2(nintrp),zsum3(nintrp),zsum4(nintrp))
      allocate(zsum5(nintrp),zsum6(nintrp),zsum7(nintrp))
c
      call eq_grid(id_rho,rho_grid,nrho,k,ierr)
      if(ierr.ne.0) call errmsg_exit(
     >   '?trxpl_mkgs2: eq_grid ierr.ne.0??')
c
      rho_intrp(nintrp)=rho_grid(nrho)
      do i=1,nrho-1
         do j=1,inper
            k=inper*(i-1)+j
            rho_intrp(k)=
     >         ((j-1)*rho_grid(i+1)+(inper-j+1)*rho_grid(i))/inper
         enddo
      enddo
c
c  user data examination loop
c
      plotn=.true.
      ideriv=.false.
      nnorm=.false.
      ispecie=1
      zrat=Tconv(ispecie)
      ifirst=1
c
 100  continue
c
      if(plotn) then
         if(nsmoo(ispecie)) then
            id=id_nsmoo(ispecie)
         else
            id=id_norig(ispecie)
         endif
         idz=id_zprof(ispecie)
         if(.not.ideriv) then
            if(nnorm) then
               wlabel='Density * (1/ne)'
               wunits=' '
            else
               wlabel='Density'
               wunits='/m^3'
            endif
         else
            wlabel='-(1/(nZ))*(d(nZ)/drho)'
            wunits=' '
         endif
      else
         if(tsmoo(ispecie)) then
            id=id_tsmoo(ispecie)
         else
            id=id_torig(ispecie)
         endif
         idz=id_zprof(ispecie)
         if(.not.ideriv) then
            if(nnorm) then
               wlabel='Temperature * (1/'//tref_lbl//')'
               wunits=' '
            else
               wlabel='Temperature'
               wunits='keV'
            endif
         else
            wlabel='-(1/T)*(dT/drho)'
            wunits=' '
         endif
      endif
c
      if(lxout(0)) then
         write(lnout(0),*) ' '
         if(iorder.eq.1) then
            write(lnout(0),*) '   profile interpolation:  C1 Hermite.'
         else
            write(lnout(0),*) '   profile interpolation:  C2 Spline.'
         endif
c
         call trxpl_mkgs2_summ(lnout(0),' see "Z" option')
c
         write(lnout(0),*) ' '
         if(gsmoo) write(lnout(0),*)
     >      ' ** some or all profiles were smoothed **'
         write(lnout(0),*) ' plotting:  ',wlabel,' SELECTED specie = #',
     >      ispecie
         write(lnout(0),*) ' '
         write(lnout(0),1003) ifirst
 1003    format(
     >      ' ==> trxpl_mkgs2 version: 13-Jun-2005 (dmc)'/
     >      ' trxpl_mkgs2 options:'/
     >      '  <a number> -- to change SELECTED plasma specie'/
     >      '  N -- to select density'/
     >      '  T -- to select temperature'/
     >      '  F -- to select the data itself'/
     >      '  G -- to select data normalized to reference data'/
     >      '  D -- to select -(1/data)(d(data)/drho)'/
     >      '  A -- (optional) set 1st specie for multiplot (',i3,')'//
     >      '  C -- collisionality (and other) interpolated multiplots'/
     >      '       (C, M options test different evaluation methods)'/
     >      '  M -- make multiplot-- data points only, all species.'/
     >      '  P -- plot, show data points and interpolation.'/
     >      '  Z -- print/plot Z, A, or Zeff data (Edens also).'//
     >      '  R -- reduce number of ion species via aggregation'/
     >     '  S -- smooth selected data profiles (or cancel smoothing)'//
     >      '  W -- ** write GS2 namelist file'/
     >      '  Q -- quit')
      endif
      call uread('trxpl_mkgs2 option:$')
c
      if(lchar('Q')) then
         if(ierr.eq.0) then
            write(lnout(0),*) ' trxpl_mkgs2: normal exit.'
         endif
         go to 1000   ! *** for now ***
      else if(lchar('W')) then
         call trxpl_mkgs2_write(tlabel)
      else if(lchar('N')) then
         plotn=.TRUE.
      else if(lchar('T')) then
         plotn=.FALSE.
      else if(lchar('D')) then
         ideriv=.TRUE.
         nnorm=.FALSE.
      else if(lchar('F')) then
         ideriv=.FALSE.
         nnorm=.FALSE.
      else if(lchar('G')) then
         ideriv=.FALSE.
         nnorm=.TRUE.
      else if(lchar('A')) then
         call ureadi('enter new multiplot 1st specie number:$',ifirst)
         ifirst=max(1,min(num_species,ifirst))
      else if(lchar('P')) then
         call eq_rgetf(nrho,rho_grid,id,0,data_grid,ierr)
         if(ierr.ne.0) call errmsg_exit('?trxpl_mkgs2: eq_rgetf 1??')
         if(.not.plotn) data_grid=zrat*data_grid
         data_grid=max(zminm,data_grid)
         call eq_rgetf(nintrp,rho_intrp,id,0,data_intrp,ierr)
         if(ierr.ne.0) call errmsg_exit('?trxpl_mkgs2: eq_rgetf 2??')
         if(.not.plotn) data_intrp=zrat*data_intrp
         data_intrp=max(zminm,data_intrp)
c
         if(ideriv) then
            call eq_rgetf(nrho,rho_grid,id,1,deriv_grid,ierr)
            if(ierr.ne.0) call errmsg_exit('?trxpl_mkgs2: eq_rgetf 3??')
            call eq_rgetf(nintrp,rho_intrp,id,1,deriv_intrp,ierr)
            if(ierr.ne.0) call errmsg_exit('?trxpl_mkgs2: eq_rgetf 4??')
            if(plotn) then
c  density (& nZ)...
               if(idz.eq.0) then
                  data_grid=-deriv_grid/data_grid   ! Z indep. of rho
                  data_intrp=-deriv_intrp/data_intrp
               else
c  Z variation, need to compute -((1/n)*(dn/drho)+(1/Z)*(dZ/drho))
                  call eq_rgetf(nrho,rho_grid,idz,0,zsum1,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 3A??')
                  call eq_rgetf(nintrp,rho_intrp,idz,0,zsum3,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 4A??')
                  call eq_rgetf(nrho,rho_grid,idz,1,zsum2,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 3B??')
                  call eq_rgetf(nintrp,rho_intrp,idz,1,zsum4,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 4B??')
c
                  data_grid=-( (deriv_grid/data_grid) +
     >               (zsum2(1:nrho)/zsum1(1:nrho)) )
                  data_intrp=-( (deriv_intrp/data_intrp) +
     >               (zsum4/zsum3) )
               endif
c
            else
c  Temperature...
               zfac=zrat
               data_grid=-zfac*deriv_grid/data_grid
               data_intrp=-zfac*deriv_intrp/data_intrp
            endif
         endif
c
         if(nnorm) then
            if(plotn) then
               if(nsmoo(num_refn)) then
                  call eq_rgetf(nrho,rho_grid,id_nsmoo(num_refn),0,
     >               deriv_grid,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 5??')
                  call eq_rgetf(nintrp,rho_intrp,id_nsmoo(num_refn),0,
     >               deriv_intrp,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 6??')
               else
                  call eq_rgetf(nrho,rho_grid,id_norig(num_refn),0,
     >               deriv_grid,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 5A??')
                  call eq_rgetf(nintrp,rho_intrp,id_norig(num_refn),0,
     >               deriv_intrp,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 6A??')
               endif
            else
               if(tsmoo(num_reft)) then
                  call eq_rgetf(nrho,rho_grid,id_tsmoo(num_reft),0,
     >               deriv_grid,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 7??')
                  call eq_rgetf(nintrp,rho_intrp,id_tsmoo(num_reft),0,
     >               deriv_intrp,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 8??')
               else
                  call eq_rgetf(nrho,rho_grid,id_torig(num_reft),0,
     >               deriv_grid,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 7A??')
                  call eq_rgetf(nintrp,rho_intrp,id_torig(num_reft),0,
     >               deriv_intrp,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 8A??')
               endif
            endif
c
c  "deriv" arrays now contain reference parameter -- not a derivative
c
            data_grid=data_grid/deriv_grid
            data_intrp=data_intrp/deriv_intrp
         endif
c
         call r8_grafxf(rho_intrp,data_intrp,nintrp,
     >      rho_grid,data_grid,nrho,' ',wunits,tpath,tlabel,
     >      slabel(ispecie)//wlabel//' vs. rho')
c
      else if(lchar('C')) then
c
 110     continue
         mnlab(1)=tpath
         mnlab(2)=tlabel
         kylab=' '
         kylab(1)='plasma species:'
         if(lxout(0)) then
            write(lnout(0),2001)
 2001       format(' plot points evaluated as for GS2 namelist...'/
     >         ' choose one option for plot:'/
     >         '  N - density (normalized)'/
     >         '  T - temperature (normalized)'/
     >         '  F - (1/(nZ))*(d(nZ)/drho)'/
     >         '  G - (1/T)*(dT/drho)'/
     >         '  M - mass (normalized)'/
     >         '  Z - charge'/
     >         '  Y - Zeff comparison'/
     >         '  U - "uprime" (currently zero)'/
     >         '  C - collisionalities'/
     >         '  P - plot d( sum(nZ) )/drho & d(ne)/drho'/
     >         '  S - quasineutrality renormalization factors'/
     >         '  Q - quit this menu')
         endif
         call uread('GS2 eval plot option:$')
         if(lchar('Q')) then
            go to 100
         else if(lchar('N')) then
            iwant=1
            mnlab(3)='n/nref (nref=ne) vs. rho'
         else if(lchar('T')) then
            iwant=2
            mnlab(3)='T/Tref (Tref='//Tref_lbl//') vs. rho'
         else if(lchar('F')) then
            iwant=3
            mnlab(3)='-(1/(nZ))*(d(nZ)/drho) vs. rho'
         else if(lchar('G')) then
            iwant=4
            mnlab(3)='-(1/T)*(dT/drho) vs. rho'
         else if(lchar('M')) then
            iwant=5
            write(mnlab(3),'(''m/mref (mref='',f7.4,'' AMU) vs. rho'')')
     >         mass_ref
         else if(lchar('Z')) then
            iwant=6
            mnlab(3)='Z vs. rho'
         else if(lchar('U')) then
            iwant=7
            mnlab(3)='"uprime" vs. rho'
         else if(lchar('C')) then
            iwant=8
            mnlab(3)='collisionalities (GS2 formula) vs. rho'
         else if(lchar('Y')) then
            iwant=9
            mnlab(3)='Zeff (TRANSP data; computed) vs. rho'
         else if(lchar('S')) then
            iwant=10
            mnlab(3)=
     >         '(nz) and grad(nz) quasineutrality renormalizations'
         else if(lchar('P')) then
            iwant=11
            mnlab(3)=
     >         'grad(ne) and un-renormalized grad(sum(nz)) vs. rho'
         else
            call uermsg('?invalid GS2 eval plot option',' ',0)
            go to 110
         endif
c
         infmp=min(max_mp,num_species-ifirst+1)
         if(iwant.eq.9) infmp=2
         if(iwant.eq.10) infmp=2
         if(iwant.eq.11) infmp=2
c
         allocate(mprho(nintrp,infmp),mpfun(nintrp,infmp))
c
         do i=1,infmp
            j=ifirst+i-1
            nc(i)=nintrp
            iself(i)=i
            zdum(i)=j
            if(iwant.lt.9) then
               lbfun(i)=
     >            slabel(j)(1:len(lbfun(i))) ! will be truncated
            endif
         enddo
         if(iwant.eq.9) then
            lbfun(1)='Zeff_TR'
            lbfun(2)='Zeff_calc'
         endif
         if(iwant.eq.10) then
            lbfun(1)='nz_renorm'
            lbfun(2)='dnz_renorm'
         endif
         if(iwant.eq.11) then
            lbfun(1)='grad(ne)'
            lbfun(2)='grad(nz+*)'
         endif
c
         wunits=' '
         do i=1,nintrp
            call trxpl_gs2_get(rho_intrp(i),ierr)  ! see nvec, etc., in module
            if(ierr.ne.0) then
               call uermsg(' ?trxpl_gs2_get evaluation error.',' ',0)
               go to 100
            endif
c
            mprho(i,1:infmp)=rho_intrp(i)
            if(iwant.eq.1) then
               mpfun(i,1:infmp)=nvec(ifirst:ifirst+infmp-1)
            else if(iwant.eq.2) then
               mpfun(i,1:infmp)=tvec(ifirst:ifirst+infmp-1)
            else if(iwant.eq.3) then
               mpfun(i,1:infmp)=npvec(ifirst:ifirst+infmp-1)
            else if(iwant.eq.4) then
               mpfun(i,1:infmp)=tpvec(ifirst:ifirst+infmp-1)
            else if(iwant.eq.5) then
               mpfun(i,1:infmp)=avec(ifirst:ifirst+infmp-1)
            else if(iwant.eq.6) then
               mpfun(i,1:infmp)=zvec(ifirst:ifirst+infmp-1)
            else if(iwant.eq.7) then
               mpfun(i,1:infmp)=upvec(ifirst:ifirst+infmp-1)
            else if(iwant.eq.8) then
               mpfun(i,1:infmp)=nuvec(ifirst:ifirst+infmp-1)
            else if(iwant.eq.9) then
               mpfun(i,1)=zeff_tr
               mpfun(i,2)=zeff_calc
            else if(iwant.eq.10) then
               mpfun(i,1)=renorm_nz
               mpfun(i,2)=renorm_gradnz
            else if(iwant.eq.11) then
               mpfun(i,1)=ne_grad
               mpfun(i,2)=sumnz_grad
            endif
         enddo
c
         iself(1)=-iself(1)  ! signal functions not contours
         call r8_grfmm1(mprho,mpfun,nintrp,
     >      nc,infmp,zdum,infmp,iself,infmp,
     >      ' ',wunits,mnlab,kylab,lbfun)
c
         deallocate(mprho,mpfun)
         go to 110
c
      else if(lchar('M')) then
c
         mnlab(1)=tpath
         mnlab(2)=tlabel
         mnlab(3)=wlabel//' vs. rho'
         kylab=' '
         kylab(1)='plasma species:'
c
         infmp=min(max_mp,num_species-ifirst+1)
c
         allocate(mprho(nrho,infmp),mpfun(nrho,infmp))
c
         if(nnorm) then
            if(plotn) then
               if(nsmoo(num_refn)) then
                  call eq_rgetf(nrho,rho_grid,id_nsmoo(num_refn),0,
     >               deriv_grid,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 10??')
               else
                  call eq_rgetf(nrho,rho_grid,id_norig(num_refn),0,
     >               deriv_grid,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 10A??')
               endif
            else
               if(tsmoo(num_reft)) then
                  call eq_rgetf(nrho,rho_grid,id_tsmoo(num_reft),0,
     >               deriv_grid,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 11??')
               else
                  call eq_rgetf(nrho,rho_grid,id_torig(num_reft),0,
     >               deriv_grid,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 11A??')
               endif
            endif
         endif
 
         do i=1,infmp
            j=ifirst+i-1
            nc(i)=nrho
            iself(i)=i
            zdum(i)=j
            lbfun(i)=
     >         slabel(j)(1:len(lbfun(i))) ! will be truncated
            mprho(1:nrho,i)=rho_grid
            if(plotn) then
               if(nsmoo(i)) then
                  idmp=id_nsmoo(i)
               else
                  idmp=id_norig(i)
               endif
               zconv=1.0d0
            else
               if(tsmoo(i)) then
                  idmp=id_tsmoo(i)
               else
                  idmp=id_torig(i)
               endif
               zconv=Tconv(i)
            endif
            call eq_rgetf(nrho,rho_grid,idmp,0,data_grid,ierr)
            if(ierr.ne.0) call errmsg_exit(
     >         '?trxpl_mkgs2: eq_rgetf 12??')
            if(ideriv) then
               call eq_rgetf(nrho,rho_grid,idmp,1,deriv_grid,ierr)
               if(ierr.ne.0) call errmsg_exit(
     >            '?trxpl_mkgs2: eq_rgetf 13??')
               mpfun(1:nrho,i)=-deriv_grid/data_grid
               if(plotn.and.(id_zprof(i).ne.0)) then
!  dZ/drho term...
                  idmp=id_zprof(i)
                  call eq_rgetf(nrho,rho_grid,idmp,0,data_grid,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 12A??')
                  call eq_rgetf(nrho,rho_grid,idmp,1,deriv_grid,ierr)
                  if(ierr.ne.0) call errmsg_exit(
     >               '?trxpl_mkgs2: eq_rgetf 13A??')
                  mpfun(1:nrho,i)=mpfun(1:nrho,i)-deriv_grid/data_grid
               endif
            else if(nnorm) then
               mpfun(1:nrho,i)=zconv*data_grid/deriv_grid
            else
               mpfun(1:nrho,i)=zconv*data_grid
            endif
         enddo
c
         iself(1)=-iself(1)  ! signal functions not contours
         call r8_grfmm1(mprho,mpfun,nrho,
     >      nc,infmp,zdum,infmp,iself,infmp,
     >      ' ',wunits,mnlab,kylab,lbfun)
c
         deallocate(mprho,mpfun)
c
      else if(lchar('Z')) then
         if(lxout(0)) write(lnout(0),*)
     >      ' enter "1", "2", or...'
         if(lxout(0)) write(lnout(0),*)
     >      ' enter "3" for quasineutrality plot;'
         if(lxout(0)) write(lnout(0),*)
     >      ' enter "4" for energy density plot;'
         call uread('enter "1" for Zeff, "2" for Z & A information:$')
         if(lchar('1')) then
            iansr=1
         else if(lchar('2')) then
            iansr=2
         else if(lchar('3')) then
            iansr=3
         else if(lchar('4')) then
            iansr=4
         else if(lchar('Q')) then
            iansr=0
         else
            iansr=0
            call uermsg('?expected "Q" or "1" or "2" or "4"',' ',0)
         endif
c
         if((iansr.eq.1).or.(iansr.eq.3).or.(iansr.eq.4)) then
c  evaluate Zeff(s) and quasineutrality
            call trxpl_mkgs2_zef(nintrp,rho_intrp,
     >         data_intrp,deriv_intrp,zsum1,zsum2,zsum3,zsum4,
     >         zsum5,zsum6,zsum7)
            if(iansr.eq.1) then
c  Zeff
               if(gsmoo) then
                  call r8_grafx3(rho_intrp,zsum1,zsum2,zsum7,nintrp,
     >               ' ',' ',tpath,tlabel,
     >               'Zeff before/after smoothing & data, vs. rho')
               else
                  call r8_grafx2(rho_intrp,zsum1,zsum7,nintrp,
     >               ' ',' ',tpath,tlabel,
     >               'Zeff calculated & Zeff(TRANSP) vs. rho')
               endif
            else if(iansr.eq.3) then
c  quasineutrality
               if(gsmoo) then
                  call r8_grafx2(rho_intrp,zsum3,zsum4,nintrp,
     >               ' ',' ',tpath,tlabel,
     >              '(1/ne)*sum(ni*Zi) before/after smoothing, vs. rho')
               else
                  call r8_grafx1(rho_intrp,zsum3,nintrp,
     >               ' ',' ',tpath,tlabel,
     >               '(1/ne)*sum(ni*Zi) vs. rho')
               endif
            else if(iansr.eq.4) then
c  energy density
               if(gsmoo) then
                  call r8_grafx2(rho_intrp,zsum5,zsum6,nintrp,
     >               ' ','Jles/m^3',tpath,tlabel,
     >              'energy density before/after smoothing, vs. rho')
               else
                  call r8_grafx1(rho_intrp,zsum5,nintrp,
     >               ' ','Jles/m^3',tpath,tlabel,
     >               'energy density vs. rho')
               endif
            endif
 
         else if(iansr.eq.2) then
            if(id_zprof(ispecie).eq.0) then
               write(lnout(0),*) ' '
               write(lnout(0),*) tpath
               write(lnout(0),*) tlabel
               write(lnout(0),*) slabel(ispecie)
               write(lnout(0),*) '  Z = ',Zplas(ispecie),'  A = ',
     >            Aplas(ispecie)
               write(lnout(0),*) '  A normalized by reference mass:  ',
     >            mass_ref
               call upaust(0)
            else
               call eq_rgetf(nintrp,rho_intrp,id_zprof(ispecie),0,
     >            data_intrp,ierr)
               if(ierr.ne.0) call errmsg_exit(
     >            '?trxpl_mkgs2: eq_rgetf 20??')
               call eq_rgetf(nintrp,rho_intrp,id_aprof(ispecie),0,
     >            deriv_intrp,ierr)
               if(ierr.ne.0) call errmsg_exit(
     >            '?trxpl_mkgs2: eq_rgetf 21??')
               call r8_grafx2(rho_intrp,data_intrp,deriv_intrp,nintrp,
     >            ' ',' ',tpath,tlabel,
     >            slabel(ispecie)//'Z & A/Mref vs. rho')
            endif
         endif
c
      else if(lchar('R')) then
c
c  aggregate ion species
c
         call trxpl_mkgs2_aggr(nrho,rho_grid,data_grid,deriv_grid,
     >      zsum1,zsum2,data_intrp,deriv_intrp)
c
c  reset specie to electrons
c
         ispecie=1
         zrat=Tconv(1)
c
      else if(lchar('S')) then
c
c  smooth data
c
         call trxpl_mkgs2_smoo(tlabel)
c
      else
c
c  fall through:  assume a species code was entered
c
         j=idcod(iert)
         if(iert.eq.0) ispecie=j
         ispecie=max(1,min(num_species,j))
         zrat=Tconv(ispecie)
      endif
c
      go to 100
c
c  exit
c
 1000 continue
      deallocate(rho_grid,data_grid,deriv_grid,stat=istat)
      deallocate(rho_intrp,data_intrp,deriv_intrp,stat=istat)
      deallocate(zsum1,zsum2,zsum3,zsum4,zsum5,zsum6,zsum7,stat=istat)
c
      return
c
      end

c
c----------------------------------------------------------------
c
c  reset module -- when new TRANSP run is selected...
c
      subroutine trxpl_mkgs2_reset
c
      use trxpl_gs2_module
c
      call trxpl_gs2_reset
c
      return
      end
