c-------------------------------------------------------------------
      subroutine trxpl_mkgs2_zef(npts,rho,n,z,zef1,zef2,nzsum1,nzsum2,
     >   esum1,esum2,zeffdata)
c
c  compute Zeff before/after smoothing.  If no smoothing, zef1=zef2
c
      use trxpl_gs2_module
c
      implicit NONE
c
      integer npts                      ! no. of pts
      real*8 rho(npts)                  ! evaluation grid
      real*8 n(npts),z(npts)            ! work arrays to store specie n & Z
      real*8 zef1(npts),zef2(npts)      ! Zeff before & after smoothing
      real*8 nzsum1(npts),nzsum2(npts)  ! 1/ne * n*Z sum before & after smooth
      real*8 esum1(npts),esum2(npts)    ! energy density (jles/m^3)
      real*8 zeffdata(npts)             ! Zeff data
c
c-----------------------
      real*8 ne(npts),nesm(npts),t(npts),tsm(npts),const
      integer j,ierr
c
c-----------------------
c
c  convert n*T, ptcl-keV/m^3, to Jles/m^3
c
      const=1.5d0*1.602d-16
c
      zef1=0.0d0
      zef2=0.0d0
c
      call eq_rgetf(npts,rho,id_norig(1),0,ne,ierr)
      if(ierr.ne.0)
     >   call errmsg_exit('?trxpl_mkgs2_zef rgetf error #0')
      if(nsmoo(1)) then
         call eq_rgetf(npts,rho,id_nsmoo(1),0,nesm,ierr)
         if(ierr.ne.0)
     >      call errmsg_exit('?trxpl_mkgs2_zef rgetf error #0A')
      else
         nesm=ne
      endif
c
      call eq_rgetf(npts,rho,id_torig(1),0,t,ierr)
      if(ierr.ne.0)
     >   call errmsg_exit('?trxpl_mkgs2_zef rgetf error #0C')
      if(tsmoo(1)) then
         call eq_rgetf(npts,rho,id_tsmoo(1),0,tsm,ierr)
         if(ierr.ne.0)
     >      call errmsg_exit('?trxpl_mkgs2_zef rgetf error #0D')
      else
         tsm=t
      endif
c
      call eq_rgetf(npts,rho,id_zeff,0,zeffdata,ierr)
      if(ierr.ne.0)
     >   call errmsg_exit('?trxpl_mkgs2_zef rgetf error #0E')
c
      t=t*Tconv(1)
      tsm=tsm*Tconv(1)
c
      nzsum1=0.0
      nzsum2=0.0
c
      esum1=ne*t*const
      esum2=nesm*tsm*const
c
      do j=1,num_species
         if(ltype(j).gt.0) then
c
c  this is an ion species
c
            if(id_zprof(j).gt.0) then
               call eq_rgetf(npts,rho,id_zprof(j),0,Z,ierr)
               if(ierr.ne.0)
     >            call errmsg_exit('?trxpl_mkgs2_zef rgetf error #9')
            else
               Z=Zplas(j)
            endif
c
            call eq_rgetf(npts,rho,id_norig(j),0,n,ierr)
            if(ierr.ne.0)
     >         call errmsg_exit('?trxpl_mkgs2_zef rgetf error #1')
c
            zef1=zef1+n*Z*Z
            nzsum1=nzsum1+n*Z
c
            call eq_rgetf(npts,rho,id_torig(j),0,t,ierr)
            if(ierr.ne.0)
     >         call errmsg_exit('?trxpl_mkgs2_zef rgetf error #1A')
            t=t*Tconv(j)
c
            esum1=esum1+n*t*const
c
            if(nsmoo(j)) then
               call eq_rgetf(npts,rho,id_nsmoo(j),0,n,ierr)
               if(ierr.ne.0)
     >            call errmsg_exit('?trxpl_mkgs2_zef rgetf error #2')
            endif
c
            if(tsmoo(j)) then
               call eq_rgetf(npts,rho,id_tsmoo(j),0,t,ierr)
               if(ierr.ne.0)
     >            call errmsg_exit('?trxpl_mkgs2_zef rgetf error #2')
               t=t*Tconv(j)
            endif
c
            zef2=zef2+n*Z*Z
            nzsum2=nzsum2+n*Z
            esum2=esum2+n*t*const
c
         endif
      enddo
c
      zef1=zef1/ne    ! Zeff = sum(ni*Zi**2)/ne
      zef2=zef2/nesm
c
      nzsum1=nzsum1/ne
      nzsum2=nzsum2/nesm
c
      write(6,*) 'trxpl_mkgs2_zef quasineutrality check:'
      write(6,*) '  (interpolation effects cause small deviations)'
      write(6,*) 'original data:  range ',minval(nzsum1),maxval(nzsum1)
      write(6,*) 'smoothed data:  range ',minval(nzsum2),maxval(nzsum2)
c
      return
      end
