subroutine trxpl_fwr2d_cdf(tpath,ztime0,ztavg)

  !  write NetCDF file containing data for FWR2D, namely:
  ! 
  !   nR, nZ, & R & Z grids: Rr, Zz, ...
  ! 
  !   ne(R,Z)[m**-3], Te(R,Z)[KeV], |B|(R,Z)[T], and derived quantities:
  !   omega0(R,Z), omegaxl(R,Z), omegaxr(R,Z) defined as follows:
  !
  !   omega0 = sqrt( (ne*ee**2) / (eps0*me*sqrt(1+5*Te/511)) ) = omega[pe]
  !   omega[ce] = (ee*|B|) / (me*sqrt(1+5*Te/511))
  !   omega_xl = sqrt(omega[ce]**2/4 + omega0**2) - omega[ce]/2
  !   omega_xr = sqrt(omega[ce]**2/4 + omega0**2) + omega[ce]/2
  !
  !     where ee = electron charge = 1.6022e-19 Coulombs
  !           me = electron mass   = 9.1099e-31 kg
  !           eps0 = vac. permittivity = 8.8542e-12 F/m
  !

  implicit NONE

  character*(*), intent(in) :: tpath    ! TRANSP run specification
  real*8, intent(in) :: ztime0          ! time of interest
  real*8, intent(in) :: ztavg           ! +/- delta(t) time averaging

  include 'trxpl.blk'  !... to communicate with trxpl_add_profile

  real*8 Rminx,Rmaxx,Zminx,Zmaxx      ! limits of xplasma grid
  real*8 Rminpl,Rmaxpl,Zminpl,Zmaxpl  ! limits of plasma boundary
  real*8 Rmin,Rmax,Zmin,Zmax          ! limits of grid for FWR2D

  real*8 dxtend                       ! amount of extension of grid beyond
                                      ! plasma bdy, subject to conditions

  real*8 lamda_ne,lamda_Te            ! scrape-off thicknesses, for
                                      ! ne & Te, respectively.

  integer :: nR,nZ                    ! gridsizes
  integer :: nbdy                     ! bdy contour size

  real*8, dimension(:), allocatable :: Rr,Zz  ! grids

  real*8, dimension(:), allocatable :: Rbdy,Zbdy    ! plasma bdy sequence

  !  (R,Z) variables, as described above:

  real*8, dimension(:,:), allocatable :: ne,te,bb,bpol,ti,qrz
  real*8, dimension(:,:), allocatable :: omega_pe,omega_xl,omega_xr
  real*8, dimension(:,:), allocatable :: omega_ce

  !  constants, as described above, and...

  real*8, parameter :: ZERO = 0.0d0
  real*8, parameter :: ONE = 1.0d0
  real*8, parameter :: ee = 1.6022d-19   
  real*8, parameter :: me = 9.1099d-31
  real*8, parameter :: eps0 = 8.8542d-12
  real*8, parameter :: c2pi = 6.2831853071795862D+00

  !  additional items

  integer, parameter :: igmin = 10      ! sanity limits on gridsizes
  integer, parameter :: igmax = 2000

  integer ierr,lnout
  logical lxout,lchar,lyes
  character*120 zfile
  character*35 tlabel
  character*10 zname

  real*8 zrhomin,zrhomax
  real*8 zne_bdy,zte_bdy,zti_bdy

  real*8 ztite
  integer :: idq

  real*8 dens_0,dens_a,gamma_dens
  real*8 temp_0,temp_a,gamma_temp

  real*8 r_mag_axis,z_mag_axis

  integer ingen

  !-------------------------------------------------------------------

  tlabel=' '
  write(tlabel,999) ztime0,ztavg
999 format('t = ',1pd11.4,' +/- ',1pd11.4,' s.')
  ztite =1.0

  !-------------------------------------------------------------------
  !  xplasma has already been set up
  !-------------------------------------------------------------------

  !  get plasma & grid R,Z limits

  call eq_rholim(zrhomin,zrhomax)  ! axis & bdy rho

  call eq_rzlims(Rminx,Rmaxx,Zminx,Zmaxx,ierr)  ! xplasma grid limits
  if(ierr.ne.0) call errmsg_exit('?trxpl_fwr2d: eq_rzlims error.')

  call eq_glimrz(zrhomax,Rminpl,Rmaxpl,Zminpl,Zmaxpl,ierr)  ! plasma bdy limits
  if(ierr.ne.0) call errmsg_exit('?trxpl_fwr2d: eq_glimrz error.')

  !--------------------------------
  !  set default controls

  lamda_ne = 0.01d0   ! scrape-off thickness, m
  lamda_te = 0.01d0   ! scrape-off thickness, m
  dxtend = 0.05d0     ! grid extension beyond plasma boundary, m
  nR = 101            ! number of R grid points
  nZ = 101            ! number of Z grid points

  !--------------------------------
  !  default for analytic profiles
  !  these are only used if tpath(1:7)='GENXPL:'; otherwise TRANSP data
  !  is expected to be available.

  dens_0=6.0d19
  dens_a=ZERO
  gamma_dens=ONE

  temp_0=10.0d0
  temp_a=ZERO
  gamma_temp=3.0d0

  !--------------------------------
  !  user specification of controls

  do

     Rmin=max(Rminx,Rminpl/2,Rminpl-dxtend)
     Rmax=min(Rmaxx,Rmaxpl+dxtend)
     Zmin=max(Zminx,Zminpl-dxtend)
     Zmax=min(Zmaxx,Zmaxpl+dxtend)

     if(lxout(0)) then
        write(lnout(0),1001) nR,Rmin,Rmax, &
             nZ,Zmin,Zmax,dxtend, &
             lamda_ne,lamda_Te
1001    format(/' FWR2D grid (meters):'/ &
             5x,i4,' R points with Rmin=',1pd11.4,' & Rmax=',1pd11.4/ &
             5x,i4,' Z points with Zmin=',1pd11.4,' & Zmax=',1pd11.4/ &
             ' based on nominal extension of ',1pd11.4,' m beyond plasma bdy.'/ &
             ' scrape-off distance for electron density (m):     ',1pd11.4/ &
             ' scrape-off distance for electron temperature (m): ',1pd11.4/ &
             /'trxpl_fwr2d options:'/ &
             ' "R" -- change number of R grid points'/ &
             ' "Z" -- change number of Z grid points'/ &
             ' "X" -- change extension distance beyond plasma boundary'/ &
             '        (extension constrained by xplasma grid limits)'/ &
             ' "D" -- change scrape-off width (m) for density'/ &
             ' "T" -- change scrape-off width (m) for temperature'/ &
             '        ("D" & "T" control extrapolation of data beyond plasma bdy).'/ &
             ' "A" -- accept parameters and proceed'/ &
             ' "Q" -- quit (without writing data).'/)
        if(tpath(1:7).eq.'GENXPL:') then
           write(lnout(0),1002) 'density','n','n','n','n','n', &
                'm**-3','n',dens_0,'n',dens_a,'n',gamma_dens
           write(lnout(0),1002) 'temperature','T','T','T','T','T', &
                'KeV','T',temp_0,'T',temp_a,'T',gamma_temp
1002       format(1x,a,':',t15,a,'(x) = (',a,'_0 - ',a,'_a)*(1-x**2)', &
                '**gamma_',a,' + ',a,'_a (',a,'); x = r/a and'/ &
                '    ',a,'_0 = ',1pd11.4,'  ',a,'_a = ',1pd11.4, &
                '  gamma_',a,' = ',1pd11.4)
           write(lnout(0),1003) ztite
1003       format(' "P" -- set parameters for analytic profiles.'/ &
                ' "I" -- set ratio Ti/Te (to define Ti profile), current ', &
                'value: ',1pd11.4)
        endif
     endif

     call uread('trxpl_fwr2d: choose option:$')

     if(lchar('Q')) then
        return   ! do not write data

     else if(lchar('A')) then
        exit     ! proceed with parameters as selected

     else if(lchar('R')) then
        ! set nR
        if(lxout(0)) write(lnout(0),1004) igmin,igmax
        call ureadi('number of R grid points (nR):$',nR)
        nR=max(igmin,min(igmax,nR))

     else if(lchar('Z')) then
        ! set nZ
        if(lxout(0)) write(lnout(0),1004) igmin,igmax
        call ureadi('number of Z grid points (nZ):$',nZ)
        nZ=max(igmin,min(igmax,nZ))

     else if(lchar('X')) then
        ! set dxtend
        call ureadr8('grid extension beyond plasma bdy (m):$',dxtend)
        dxtend=abs(dxtend)

     else if(lchar('D')) then
        ! set lamda_ne
        call ureadr8('density scrape-off width (m):$',lamda_ne)
        lamda_ne=abs(lamda_ne)

     else if(lchar('T')) then
        ! set lamda_te
        call ureadr8('temperature scrape-off width (m):$',lamda_te)
        lamda_te=abs(lamda_te)

     else if(lchar('P')) then
        if(tpath(1:7).ne.'GENXPL:') then
           call uermsg( &
                '?trxpl_fwr2d: analytic profiles option not available.',' ',0)
        else
           call get3('m**-3','n_0',dens_0,'n_a',dens_a,'gamma_n',gamma_dens)
           call get3('KeV','T_0',temp_0,'T_a',temp_a,'gamma_T',gamma_temp)
        endif
     else if(lchar('I')) then
        if(tpath(1:7).ne.'GENXPL:') then
           call uermsg( &
                '?trxpl_fwr2d: analytic profile Ti/Te option not available.', &
                ' ',0)
        else
           call ureadr8('Ti/Te (btw 0.1 and 10.0):',ztite)
           if(ztite.lt.0.1d0) ztite=0.1d0
           if(ztite.gt.10.0d0) ztite=10.0d0
        endif
     else
        call uermsg('?trxpl_fwr2d: unrecognized option code.',' ',0)
     endif

1004    format(' minimum gridsize: ',i4,' maximum gridsize: ',i4)
  enddo

  !----------------------------------
  !  acquire temperature and density data in xplasma
  !   Hermite bicubic with scrape-off extrapolation

  if(tpath(1:7).ne.'GENXPL:') then
     ixtnd=1
     iordr=1

     zlamda=lamda_ne
     zname='NE'
     call trxpl_add_profile(zname)
     if(numpro.ne.1) return

     zlamda=lamda_te
     zname='TE'
     call trxpl_add_profile(zname)
     if(numpro.ne.2) return

     zlamda=lamda_te
     zname='TI'
     call trxpl_add_profile(zname)
     if(numpro.ne.3) return

  else
     ingen=2*max(50,nR,nZ)+1
     call gen_addprof(dens_0,dens_a,gamma_dens,'NE',lamda_ne,1)
     call gen_addprof(temp_0,temp_a,gamma_temp,'TE',lamda_te,2)
     call gen_addprof(ztite*temp_0,ztite*temp_a,gamma_temp,'TI',lamda_te,2)

  endif

  call eq_gfnum('Q',idq)
  if(idq.eq.0) call eq_gfnum('QMHD',idq)
  if(idq.gt.0) then
     zlamda=1.0d30
     call eqm_rzfun(idq,zlamda,1,ZERO,ierr)
     if(ierr.ne.0) call errmsg_exit('?trxpl_fwr2d: eqm_rzfun error.')
  else
     call errmsg_exit('?trxpl_fwr2d: no Q profile data.')
  endif

  !----------------------------------
  !  allocate local arrays

  nbdy=2*(nR+nZ)
  allocate(Rr(nR),Zz(nZ))
  allocate(Rbdy(nbdy),Zbdy(nbdy))
  allocate(ne(nR,nZ),Te(nR,nZ),bb(nR,nZ),bpol(nR,nZ),Ti(nR,nZ),qrz(nR,nZ))
  allocate(omega_pe(nR,nZ),omega_ce(nR,nZ),omega_xl(nR,nZ),omega_xr(nR,nZ))

  call calculate

  call display

  call uread(' write NetCDF file for FWR2D? (Y/N):$')

  if(lyes(0)) then
     zfile=' '
     call unixfile('trxpl_fwr2d',zfile)
     call write_cdf
  endif

  deallocate(Rr,Zz)
  deallocate(Rbdy,Zbdy)
  deallocate(ne,Te,bb,bpol,Ti,qrz,omega_pe,omega_ce,omega_xl,omega_xr)

contains

  !====================================================================
  subroutine calculate

    !  compute 2d profiles wanted by fwr2d

    real*8 rhovec(nbdy),thvec(nbdy),phivec(nbdy)
    real*8 ztmp(nR),zwk(nR,7)
    integer i,j,k,iregion(nbdy)
    integer :: id_te=0,id_ne=0,id_BR=0,id_BZ=0,id_Bphi=0,id_ti,id_q,ids(7)
    real*8 nemin,nemax
    integer inemin,jnemin

    !  mag axis...

    rhovec(1)=zrhomin
    thvec(1)=ZERO
    phivec(1)=ZERO
    call eq_rzget(1,rhovec(1:1),thvec(1:1),phivec(1:1),rbdy(1:1),zbdy(1:1), &
         iregion(1:1),ierr)
    if(ierr.ne.0) call errmsg_exit('?trxpl_fwr2d: eq_rzget error @axis.')
    r_mag_axis=rbdy(1)
    z_mag_axis=zbdy(1)

    !  ids ...

    call eq_gfnum('TE',id_te); ids(1)=id_te
    call eq_gfnum('NE',id_ne); ids(2)=id_ne
    call eq_gfnum('BR',id_br); ids(3)=id_br
    call eq_gfnum('BZ',id_bz); ids(4)=id_bz
    call eq_gfnum('Bphi',id_bphi); ids(5)=id_bphi
    call eq_gfnum('TI',id_ti); ids(6)=id_ti
    call eq_gfnum('Q',id_q); ids(7)=id_q

    call eq_rgetf(1,zrhomax,id_te,0,zte_bdy,ierr)
    if(ierr.ne.0) call errmsg_exit('?trxpl_fwr2d: eq_rgetf bdy Te error.')
    zte_bdy=max(ZERO,zte_bdy)

    call eq_rgetf(1,zrhomax,id_ti,0,zti_bdy,ierr)
    if(ierr.ne.0) call errmsg_exit('?trxpl_fwr2d: eq_rgetf bdy Ti error.')
    zti_bdy=max(ZERO,zti_bdy)

    call eq_rgetf(1,zrhomax,id_ne,0,zne_bdy,ierr)
    if(ierr.ne.0) call errmsg_exit('?trxpl_fwr2d: eq_rgetf bdy ne error.')
    zne_bdy=max(ZERO,zne_bdy)

    !  plasma boundary contour

    rhovec = zrhomax
    phivec = 0
    do i=1,nbdy
       thvec(i) = (i-1)*c2pi/(nbdy-1)
    enddo

    call eq_rzget(nbdy,rhovec,thvec,phivec,rbdy,zbdy,iregion,ierr)
    if(ierr.ne.0) call errmsg_exit('?trxpl_fwr2d: eq_rzget error.')

    rbdy(nbdy)=rbdy(1)  ! guarantee exact closure of the bdy contour
    zbdy(nbdy)=zbdy(1)

    !  form R & Z grids

    do i=1,nR
       Rr(i)=(Rmin*(nR-i)+Rmax*(i-1))/(nR-1)
    enddo

    do i=1,nZ
       Zz(i)=(Zmin*(nZ-i)+Zmax*(i-1))/(nZ-1)
    enddo

    !  interpolate ne,Te,B field data ... 1 row at a time

    do i=1,nZ
       ztmp=Zz(i)
       call eq_frz(nR,Rr,ztmp,7,ids,nR,zwk,ierr)
       if(ierr.ne.0) call errmsg_exit('?trxpl_fwr2d: eq_frz error.')

       te(1:nR,i)=max(1.0d-4*zte_bdy,zwk(1:nR,1))
       ne(1:nR,i)=max(1.0d-4*zne_bdy,zwk(1:nR,2))
       bb(1:nr,i)=sqrt( zwk(1:nR,3)**2 + zwk(1:nR,4)**2 + zwk(1:nR,5)**2 )
       bpol(1:nr,i)=sqrt( zwk(1:nR,3)**2 + zwk(1:nR,4)**2 )
       ti(1:nR,i)=max(1.0d-4*zti_bdy,zwk(1:nR,6))
       qrz(1:nR,i)=zwk(1:nR,7)

    enddo

    nemin=ne(1,1)
    inemin=1
    jnemin=1
    nemax=nemin
    do j=1,nZ
       do i=1,nR
          nemax=max(nemax,ne(i,j))
          if(ne(i,j).lt.nemin) then
             inemin=i
             jnemin=j
             nemin=ne(i,j)
          endif
       enddo
    enddo

    write(lnout(0),'(A,1PD11.4)') ' minimum electron density: ',nemin
    !  write(lnout(0),'(A,1PD11.4,A,1PD11.4)') '    at R=',Rr(inemin), &
    !       ' Z=',Zz(jnemin)
    write(lnout(0),'(A,1PD11.4)') ' maximum electron density: ',nemax

    !  compute omegas ... these are NRxNZ expressions...

    omega_pe = sqrt(ne*ee*ee/(eps0*me*(1 + 5*te/511)))
    omega_ce = ee*bb/(me*(1 + 5*te/511))

    omega_xl = sqrt((omega_ce**2)/4 + omega_pe**2) - omega_ce/2
    omega_xr = sqrt((omega_ce**2)/4 + omega_pe**2) + omega_ce/2

  end subroutine calculate

  !====================================================================
  subroutine display

    real*8 wk(max(nR,nZ))

    wk = 0

    do
 
       if(lxout(0)) then
          write(lnout(0),1001) r_mag_axis,z_mag_axis
1001      format(' (R,Z) of magnetic axis (m):',2(1x,1pe11.4)/ &
               /'display options:'/ &
               ' "N" -- ne(R,Z)'/ &
               ' "T" -- Te(R,Z)'/ &
               ' "A" -- |Bpol|(R,Z)'/ &
               ' "B" -- |B|(R,Z)'/ &
               ' "I" -- Ti(R,Z)'/ &
               ' "J" -- q(R,Z)'/ &
               ' "P" -- omega_pe(R,Z)'/ &
               ' "C" -- omega_ce(R,Z)'/ &
               ' "1" -- omega_xl(R,Z)'/ & 
               ' "2" -- omega_xr(R,Z)'/ &
               ' "X" -- xplasma debug plots.'/ &
               ' "Q" -- quit plotting menu.'/ &
               )
       endif

       call uread('trxpl_fwr2d(display): specify plotting option:$')

       if(lchar('N')) then
          call r8_grf3f1(Rr,ne,Zz,wk,nR,nZ,nR, &
               'R','electron density','Z', &
               'm','m**-3','m', tpath,tlabel,0)
          
       else if(lchar('T')) then
          call r8_grf3f1(Rr,Te,Zz,wk,nR,nZ,nR, &
               'R','electron temperature','Z', &
               'm','KeV','m', tpath,tlabel,0)
          
       else if(lchar('I')) then
          call r8_grf3f1(Rr,Ti,Zz,wk,nR,nZ,nR, &
               'R','ion temperature','Z', &
               'm','KeV','m', tpath,tlabel,0)

       else if(lchar('A')) then
          call r8_grf3f1(Rr,bpol,Zz,wk,nR,nZ,nR, &
               'R','|Bpol|','Z', &
               'm','T','m', tpath,tlabel,0)

       else if(lchar('B')) then
          call r8_grf3f1(Rr,bb,Zz,wk,nR,nZ,nR, &
               'R','|B|','Z', &
               'm','T','m', tpath,tlabel,0)

       else if(lchar('J')) then
          call r8_grf3f1(Rr,qrz,Zz,wk,nR,nZ,nR, &
               'R','q','Z', &
               'm',' ','m', tpath,tlabel,0)
          
       else if(lchar('P')) then
          call r8_grf3f1(Rr,omega_pe,Zz,wk,nR,nZ,nR, &
               'R','omega_pe','Z', &
               'm','s**-1','m', tpath,tlabel,0)
          
       else if(lchar('C')) then
          call r8_grf3f1(Rr,omega_ce,Zz,wk,nR,nZ,nR, &
               'R','omega_ce','Z', &
               'm','s**-1','m', tpath,tlabel,0)
          
       else if(lchar('1')) then
          call r8_grf3f1(Rr,omega_xl,Zz,wk,nR,nZ,nR, &
               'R','omega_xl','Z', &
               'm','s**-1','m', tpath,tlabel,0)
          
       else if(lchar('2')) then
          call r8_grf3f1(Rr,omega_xr,Zz,wk,nR,nZ,nR, &
               'R','omega_xr','Z', &
               'm','s**-1','m', tpath,tlabel,0)
          
       else if(lchar('X')) then
          call eqdbg_plot

       else if(lchar('Q')) then
          exit
       else
          call uermsg('?trxpl_fwr2d(display): unrecogized option.',' ',0)
       endif

    enddo

  end subroutine display

  !====================================================================
  subroutine get3(zunits,z0,f0,za,fa,zexp,fexp)

    character*(*), intent(in) :: zunits   ! units label

    character*(*), intent(in) :: z0       ! central value label
    real*8, intent(inout) :: f0

    character*(*), intent(in) :: za       ! edge value label
    real*8, intent(inout) :: fa

    character*(*), intent(in) :: zexp     ! exponent value label
    real*8, intent(inout) :: fexp

    !------------------------------------------

    call ureadr8(z0//' ('//zunits//'):$',f0)
    f0=max(ZERO,f0)

    call ureadr8(za//' ('//zunits//'):$',fa)
    fa=max(ZERO,fa)

    call ureadr8(zexp//' (dimensionless):$',fexp)
    fexp=max(1.0d-3,fexp)

  end subroutine get3

  !====================================================================
  subroutine gen_addprof(f0,fa,gamma,zname,lamda,ict)

    !  set up analytic profile in XPLASMA; extend to (R,Z) grid also.

    real*8,intent(in) :: f0    ! axial value of function
    real*8,intent(in) :: fa    ! edge value of function
    real*8,intent(in) :: gamma ! exponent (see formula)
    character*(*), intent(in) :: zname   ! profile name
    real*8,intent(in) :: lamda ! scrape-off width (m)
    integer, intent(in) :: ict ! fcn count; if =1, define x axis to XPLASMA

    !--------------------------
    real*8 :: xx(ingen),ff(ingen)
    character*8 :: xname = 'x_genadd'
    integer :: i,id_rho=0,id_x=0,ibc0,ibc1,id_out,ierr,iordr,iordrz
    real*8 :: zbc0,zbc1
    !--------------------------

    xx(1)=ZERO
    xx(ingen)=ONE
    do i=2,ingen-1
       xx(i)=(i-1)*xx(ingen)/(ingen-1)
    enddo

    if(ict.eq.1) then
       ! define "alternate" x axis to XPLASMA
       call eq_ganum('__RHO',id_rho)
       call eqm_uaxis(xname,id_rho,0,xx,ingen,1.0d-8,id_x,ierr)
    else
       ! fetch "alternate" x axis id
       call eq_ganum(xname,id_x)
    endif

    !  compute the profile...

    do i=1,ingen
       ff(i)=(f0-fa)*(ONE-xx(i)*xx(i))**gamma + fa
    enddo

    !  create the XPLASMA spline...

    zbc0=ZERO;  zbc1=ZERO
    ibc0=1;     ibc1=1
    if(gamma.lt.1.25d0) ibc1=0

    call eq_gfnum(zname,id_out)
    if(id_out.gt.0) then
       iordr=102  ! replacement spline
       iordrz=101 ! replacement Hermite in (R,Z) space
    else
       iordr=2    ! spline
       iordrz=1   ! Hermite in (R,Z) space
    endif

    call eqm_rhofun(iordr,id_x,zname,ff, &
         ibc0,zbc0,ibc1,zbc1, id_out, ierr)

    !  extend to (R,Z) space

    call eqm_rzfun(id_out,lamda,iordrz,ZERO,ierr)

  end subroutine gen_addprof
  !====================================================================
  subroutine write_cdf

    use ezcdf

    integer ncid,ierr,ilf

    !  write NetCDF file for FWR2D

    ilf=len_trim(zfile)

    call cdf_open(ncid,zfile(1:ilf),'w',ierr)
    if(ierr.ne.0) then
       write(lnout(0),*) ' ?trxpl_fwr2d: NetCDF open failure: ',zfile(1:ilf)

    else

       !  ** NetCDF definitions **

       !  TRANSP data source info
       call cdf_define(ncid,'TRANSP_path',tpath)
       call cdf_define(ncid,'time',ztime0)
       call cdf_define(ncid,'avg_delta_t',ztavg)
       
       !  grids
       call cdf_define(ncid,'nr',nR)
       call cdf_define(ncid,'nz',nZ)
       call cdf_define(ncid,'rr',Rr)
       call cdf_define(ncid,'zz',Zz)

       !  main data
       call cdf_define(ncid,'ne',ne)
       call cdf_define(ncid,'te',te)
       call cdf_define(ncid,'ti',ti)
       call cdf_define(ncid,'bb',bb)
       call cdf_define(ncid,'bpol',bpol)
       call cdf_define(ncid,'qrz',qrz)
       call cdf_define(ncid,'omega_pe',omega_pe)
       call cdf_define(ncid,'omega_ce',omega_ce)
       call cdf_define(ncid,'omega_xl',omega_xl)
       call cdf_define(ncid,'omega_xr',omega_xr)

       !  plasma bdy & scrape-off information
       call cdf_define(ncid,'r_mag_axis',r_mag_axis)
       call cdf_define(ncid,'z_mag_axis',z_mag_axis)
       call cdf_define(ncid,'dxtend',dxtend)
       call cdf_define(ncid,'lamda_ne',lamda_ne)
       call cdf_define(ncid,'lamda_te',lamda_te)
       call cdf_define(ncid,'nbdy',nbdy)
       call cdf_define(ncid,'rbdy',Rbdy)
       call cdf_define(ncid,'zbdy',Zbdy)

       !  ** NetCDF writes **

       !  TRANSP data source info
       call cdf_write(ncid,'TRANSP_path',tpath)
       call cdf_write(ncid,'time',ztime0)
       call cdf_write(ncid,'avg_delta_t',ztavg)

       !  grids
       call cdf_write(ncid,'nr',nR)
       call cdf_write(ncid,'nz',nZ)
       call cdf_write(ncid,'rr',Rr)
       call cdf_write(ncid,'zz',Zz)

       !  main data
       call cdf_write(ncid,'ne',ne)
       call cdf_write(ncid,'te',te)
       call cdf_write(ncid,'ti',ti)
       call cdf_write(ncid,'bb',bb)
       call cdf_write(ncid,'bpol',bpol)
       call cdf_write(ncid,'qrz',qrz)
       call cdf_write(ncid,'omega_pe',omega_pe)
       call cdf_write(ncid,'omega_ce',omega_ce)
       call cdf_write(ncid,'omega_xl',omega_xl)
       call cdf_write(ncid,'omega_xr',omega_xr)

       !  plasma bdy & scrape-off information
       call cdf_write(ncid,'r_mag_axis',r_mag_axis)
       call cdf_write(ncid,'z_mag_axis',z_mag_axis)
       call cdf_write(ncid,'dxtend',dxtend)
       call cdf_write(ncid,'lamda_ne',lamda_ne)
       call cdf_write(ncid,'lamda_te',lamda_te)
       call cdf_write(ncid,'nbdy',nbdy)
       call cdf_write(ncid,'rbdy',Rbdy)
       call cdf_write(ncid,'zbdy',Zbdy)

       !  ** all done **

       call cdf_close(ncid)

    endif

  end subroutine write_cdf

end subroutine trxpl_fwr2d_cdf
