c--------------------------------------------------
      subroutine trxpl_mkgs2_write(tlabel)
c
c  write namelists
c
      use trxpl_gs2_module
      implicit NONE
c
      character*(*) tlabel   ! TRANSP time label
c
c----------------------
c
      real*8 zrho,zeff
      character*24 zdate
c
      character*120 zfile1,zfile2
c
      integer luni,luno
      integer ipass,ierr,istat
      integer i,j,k,ilp
c
      integer lparams,lsknobs,lzeff,lbeta,lgrid(4),lgrd2(2)
      integer iflag_cwd
c
      logical lxout
      integer lnout
c----------------------
c
      luni=88                           ! template (input) namelist
      luno=89                           ! output namelist
c
 10   continue
c
c----------------------------
c  get (or skip) template file
c
      if(lxout(0).and.(mpath0.eq.' ')) write(lnout(0),*)
     >   'enter "S" to skip, i.e. not to use a template file.'
      call trxpl_gs2_file('TEMPLATE_FILE',mpath0,mpath,zfile1)
      if(zfile1.eq.'q') return
      if(zfile1.eq.'Q') return
      if((zfile1.eq.'s').or.(zfile1.eq.'S')) then
         luni=0
      else
         ierr=1
         call ufopen(luni,zfile1,1,ierr,0)
         if(ierr.ne.0) then
            call uermsg('trxpl_mkgs2_write:  open failure: ',zfile1,0)
            go to 10
         endif
      endif
c
c------------------------------
c
c  in a loop, specify rho values and output filenames
c
      ipass=0
c
 100  continue
c
      ipass=ipass+1
      if((ipass.gt.1).and.(luni.gt.0)) then
         rewind luni
      endif
c
c  get rho value
c
 110  continue
      zrho=-99.0d0
      if(lxout(0))
     >   write(lnout(0),*) 'enter negative number to exit write loop.'
      call ureadr8('enter "rho" flux surface coordinate:$',zrho)
      if(zrho.lt.0.0d0) then
         if(luni.gt.0) close(unit=luni)
         return
      endif
c
      if(zrho.gt.1.0d0) then
         call uermsg(' ?rho must be between 0.0 and 1.0 ',' ',0)
         go to 110
      endif
c
      transp_x = zrho
c
      call trxpl_gs2_get(zrho,ierr)
      if(ierr.ne.0) then
         call uermsg('?trxpl_gs2_get data error.',' ',0)
         go to 110
      endif
c
      zeff=0.0
      do i=2,num_species
         zeff=zeff+nvec(i)*zvec(i)*zvec(i)
      enddo
      zeff=zeff/nvec(1)
c
c  get output filename
c
      call trxpl_gs2_file('OUTPUT_FILE',opath0,opath,zfile2)
      if(zfile2.eq.'q') go to 110
      if(zfile2.eq.'Q') go to 110
c
      open(unit=luno,file=zfile2,status='old',iostat=istat)
      if(istat.eq.0) then
         close(unit=luno)
         i=len_trim(zfile2)
         call frename(zfile2(1:i),zfile2(1:i)//'~',ierr)
      endif
      call ufopen(luno,zfile2,2,ierr,0)
      if(ierr.ne.0) then
         call uermsg('?trxpl_mkgs2:  open failure.',zfile2,0)
         go to 110
      endif
c
      call fdate(zdate)
c
      i=len_trim(transp_path)
      j=len_trim(tlabel)
      write(luno,1001) zdate,username,hostname,
     >   transp_path(1:i),tlabel(1:j),zrho,
     >   tref_lbl,mass_ref,aminor,tref,tref_lbl,nref,
     >   zeff_tr,zeff_calc,renorm_nz,renorm_gradnz,fast_aggr
 1001 format(/
     >   '!_trxpl: GS2 namelist'/
     >   '!_trxpl: written on ',a,' by "trxpl"'/
     >   '!_trxpl: username: ',a/
     >   '!_trxpl: hostname: ',a/
     >   '!_trxpl: TRANSP run: ',a/
     >   '!_trxpl: at: ',a/
     >   '!_trxpl: and: rho surface: ',1pe16.9,' (norm.sqrt.tor.flux)'//
     >   '!_trxpl: Tref=',a,', nref=ne, mass_ref=',1pe11.4,
     >       ' a=',1pe11.4,' meters.'/
     >   '!_trxpl:     Tref = ',1pe11.4,' KeV   (',a,')'/
     >   '!_trxpl:     nref = ',1pe11.4,' m**-3 (ne)'/
     >   '!_trxpl: Z_ref= 1.0000 ',
     >       ' Zeff(transp)=',1pe11.4,' Zeff(calc)=',1pe11.4/
     >   '!_trxpl: quasineutrality ne/sum(nZ) renormalization:',1pe11.4/
     >   '!_trxpl: quasineutrality grad(ne)/grad(sum(nZ))',
     >       ' renormalization:',1pe11.4/
     >   '!_trxpl: fast/thermal aggregation option: ',L1)
      if(luni.gt.0) then
         i=len_trim(zfile1)
         write(luno,1002) zfile1(1:i)
 1002    format('!_trxpl:  template file:  ',a)
      endif
c
      if(luni.eq.0) then
c
         call trxpl_nlist_write_all(luno,.TRUE.,.TRUE.,.TRUE.)
c
      else
         call trxpl_nlist_find(luni,lparams,lzeff,lbeta,lsknobs,lgrid,
     >        lgrd2)
         call trxpl_nlist_copy(luni,luno,lparams,lzeff,lbeta,lsknobs,
     >        lgrid,lgrd2)
      endif
c
      close(unit=luno)
      go to 100
c
      end
