c-------------------------------------------------------------------
      subroutine trxpl_gs2_get(zrho,ierr)
c
c  get all GS2 parameters at a single flux surface location
c
      use trxpl_gs2_module
      implicit NONE
c
      real*8, intent(in) :: zrho        ! the flux surface location (btw 0 & 1)
      integer, intent(out) :: ierr      ! completion code, 0=OK
c
c---------------------------------------------------
c
      integer idlist_n(num_species),idlist_t(num_species)
c
      integer i,j,k
c
      real*8 zminm,coulog,nufac,zsum,zderiv,ztest
c
c---------------------------------------------------
c
      zminm=1.0d-4                      ! min n & T (instead of zero)
c
      call eq_rgetf(1,zrho,id_zeff,0,zeff_tr,ierr)
      if(ierr.ne.0) return
c
      do i=1,num_species
c
         upvec(i)=0
c
         if(id_zprof(i).ne.0) then
            call eq_rgetf(1,zrho,id_zprof(i),0,zvec(i),ierr)
            if(ierr.ne.0) return
         else
            zvec(i)=zplas(i)
         endif
c
         if(id_aprof(i).ne.0) then
            call eq_rgetf(1,zrho,id_aprof(i),0,avec(i),ierr)
            if(ierr.ne.0) return
         else
            avec(i)=aplas(i)
         endif
c
         if(nsmoo(i)) then
            idlist_n(i)=id_nsmoo(i)
         else
            idlist_n(i)=id_norig(i)
         endif
c
         if(tsmoo(i)) then
            idlist_t(i)=id_tsmoo(i)
         else
            idlist_t(i)=id_torig(i)
         endif
c
      enddo
c
      call eq_rgetff(1,zrho,num_species,idlist_n,0,1,nvec,ierr)
      if(ierr.ne.0) return
c
      call eq_rgetff(1,zrho,num_species,idlist_n,1,1,npvec,ierr)
      if(ierr.ne.0) return
c
      call eq_rgetff(1,zrho,num_species,idlist_t,0,1,tvec,ierr)
      if(ierr.ne.0) return
c
      call eq_rgetff(1,zrho,num_species,idlist_t,1,1,tpvec,ierr)
      if(ierr.ne.0) return
c
      nvec(1:num_species)=max(zminm,nvec(1:num_species))
      tvec(1:num_species)=max(zminm,tvec(1:num_species))
c
      npvec(1:num_species)=-npvec(1:num_species)/nvec(1:num_species)
      tpvec(1:num_species)=-tpvec(1:num_species)/tvec(1:num_species)
c
      tvec(1:num_species)=tconv(1:num_species)*tvec(1:num_species)
c
c  dZ/drho correction...
c
      do i=1,num_species
         if(id_zprof(i).ne.0) then
            call eq_rgetf(1,zrho,id_zprof(i),1,zderiv,ierr)
            if(ierr.ne.0) return
            npvec(i)=npvec(i)-zderiv/zvec(i)
         endif
      enddo
c
c renorm for precise quasineutrality -- hold electron data fixed
c densities:
      zsum=0.0d0
      do i=2,num_species
         zsum=zsum+nvec(i)*zvec(i)
      enddo
      zsum=nvec(1)/zsum
      nvec(2:num_species)=zsum*nvec(2:num_species)
      renorm_nz=zsum
c
c also for derivatives:
c
      ztest=(nvec(1)*npvec(1))
      if(zrho.eq.0.0d0) then
         zsum=1.0d0
      else
         zsum=0.0d0
         do i=2,num_species
            zsum=zsum+nvec(i)*zvec(i)*npvec(i)
         enddo
         sumnz_grad = zsum                   ! save in module
         ne_grad = ztest                     ! save in module
         if((1.d-20*zsum*ztest).le.0.0d0) then
            zsum=1.0d0
         else
            ztest=abs(ztest)
            zsum=abs(zsum)
            if(ztest.gt.1.5d0*zsum) then
               zsum=1.5d0
            else if(ztest.lt.(2.0d0/3.0d0)*zsum) then
               zsum=(2.0d0/3.0d0)
            else
               zsum=ztest/zsum
            endif
         endif
      endif
      npvec(2:num_species)=zsum*npvec(2:num_species)
      renorm_gradnz=zsum
c
c calculate zeff
c
      zeff_calc=0.0d0
      do i=2,num_species
         zeff_calc=zeff_calc+nvec(i)*zvec(i)*zvec(i)
      enddo
      zeff_calc=zeff_calc/nvec(1)
c
c  ok... now evaluate the collisionalities
c  using the formula in the GS2 namelist documentation
c
c  **electrons are species #1
c
c  **note assumption: nref=ne.
c  ****but**** Tref=Te **or** Tref=Ti is now allowed (dmc Jan 2003).
c
c     ==>it used to be that Tref=Te was hardwired.
c
c  reference density and temperature -- normalization applied *AFTER*
c  evaluation of collisionalities
c
      nref=nvec(num_refn)
      tref=tvec(num_reft)
c
c  electron collisionality -- note select use of Tref
c
      coulog = 15.94d0 - 0.5d0 * log(nvec(1)/(1.0d19*tvec(1)*tvec(1)))
      nuvec(1) = 0.00279d0*coulog*(nvec(1)/1.0d19)/(tvec(1)**1.5)*
     >   aminor*sqrt(mass_ref/tref)
c
c  ion collisionalities
c
      do i=2,num_species
         nufac=nuvec(1)*zvec(i)*zvec(i)*
     >      sqrt(avec(1)/avec(i))*(tvec(1)/tvec(i))**1.5
         nuvec(i)=0.0d0
         do j=1,num_species
            nuvec(i)=nuvec(i)+(nvec(j)/nvec(1))*zvec(j)*zvec(j)*2.0d0/
     >         (1.0d0+sqrt((avec(i)/avec(j))*(tvec(j)/tvec(i))))
         enddo
         nuvec(i)=nuvec(i)*nufac
      enddo
c
c  compute "beta" in terms of reference n,T ...
c
      beta = 403.0d0*nref*1.0d-19*tref/
     >   1.0d5/(b_t0*b_t0)
c
c  after collisionalities evaluation... normalize densities and temperatures
c  to their reference values...
c
      nvec(1:num_species)=nvec(1:num_species)/nref ! n renormalization
      tvec(1:num_species)=tvec(1:num_species)/tref ! T renormalization
c
      return
      end
