
! written by nbigen.py
! *** do not edit *** (use nbigen.py instead)

module nbi_pstypes

  ! Plasma State and alternate control inputs for NUBEAM
  ! Items left at default values are not transferred.

  ! Note SAVEd data.  These control routines are probably not thread safe and
  ! should be likely be executed only by a master process.
  use iso_c_binding, only: fp => c_double
  use plasma_state_mod
  use nbi_types
  use ezcdf
  use xplasma_obj_instance
  implicit NONE

  PRIVATE
  SAVE

  ! **default** marker values
  INTEGER, PARAMETER, PUBLIC :: idflt_nbips = -123456789
  real(fp), PARAMETER, PUBLIC :: r8dflt_nbips = -1.2345678901e+203_fp

  ! public data types:
  public :: nbi_ctrl_init,nbi_ctrl_update
  public :: nbi_ps_init,nbi_ps_update

  ! public methods:
  public :: nbi_ctrl_init_dflt,nbi_ctrl_update_dflt
  public :: nbi_ctrl_init_read,nbi_ctrl_update_read
  public :: nbi_ctrl_init_write,nbi_ctrl_update_write
  public :: nbi_ctrl_init_cdfwrite,nbi_ctrl_update_cdfwrite
  public :: nbi_ctrl_init_cdfread,nbi_ctrl_update_cdfread
  public :: nbi_ps_init_dflt,nbi_ps_update_dflt
  public :: nubeam_ctrl_init,nubeam_ctrl_step,nubeam_ctrl_nostep
  public :: nubeam_ctrl_step_child

  public :: inbi_logical  ! integer function: .TRUE.-> 1, .FALSE.-> 0

  ! private interfaces:
  interface trans
    module procedure trans_I0,trans_I1,trans_I2
    module procedure trans_L0,trans_L1
    module procedure trans_D0,trans_D1,trans_D2
    module procedure trans_C0
  end interface

  interface dwrite
    module procedure dwrite_I0,dwrite_I1,dwrite_I2
    module procedure dwrite_D0,dwrite_D1,dwrite_D2
    module procedure dwrite_C0
  end interface

  LOGICAL :: init_called = .FALSE.

  ! public methods for setting character data:
  public :: nubeam_set_mission
  public :: nubeam_set_workpath
 
  ! module local copies of input blocks:
 
  type(nbitype_dims) :: zdims
 
  type(nbitype_sys) :: zsys
  type(nbitype_times) :: ztimes
  type(nbitype_grid) :: zgrid
  type(nbitype_beams) :: zbeams
  type(nbitype_impurity) :: zimpurity
  type(nbitype_minority) :: zminority
  type(nbitype_powers) :: zpowers
  type(nbitype_fusion) :: zfusion
  type(nbitype_fpp) :: zfpp
  type(nbitype_num) :: znum
  type(nbitype_atomic) :: zatomic
  type(nbitype_collid) :: zcollid
  type(nbitype_flr) :: zflr
  type(nbitype_saw) :: zsaw
  type(nbitype_adif) :: zadif
  type(nbitype_ripple) :: zripple
  type(nbitype_outcon) :: zoutcon
  type(nbitype_fishbone) :: zfishbone
  type(nbitype_box) :: zbox
  type(nbitype_tube) :: ztube
  type(nbitype_misc) :: zmisc
  type(nbitype_profiles) :: zprofiles
 
  ! saved copies of character control variables
  CHARACTER*120 :: saved_mission
  CHARACTER*120 :: saved_workpath
 
  type nbi_ps_init
 
    CHARACTER*32 :: runid
    REAL(fp) :: tbm1
    REAL(fp) :: tbm2
    INTEGER :: id_mcgrid
    INTEGER :: nthets
    REAL(fp) :: ebdmax
    INTEGER :: ngmax
    real(fp) :: backz(10)  ! (1:ngmax)
    real(fp) :: aplasm(10)  ! (1:ngmax)
    INTEGER :: nbeam
    real(fp) :: xzbeama(320)  ! (1:nbeam)
    real(fp) :: abeama(320)  ! (1:nbeam)
    INTEGER :: nlco(320)  ! (1:nbeam)
    integer :: ntrace(320)  ! (1:nbeam)
    real(fp) :: rtcena(320)  ! (1:nbeam)
    real(fp) :: xlbtna(320)  ! (1:nbeam)
    real(fp) :: xybsca(320)  ! (1:nbeam)
    real(fp) :: xbzeta(320)  ! (1:nbeam)
    integer :: nbshapa(320)  ! (1:nbeam)
    real(fp) :: bmwidra(320)  ! (1:nbeam)
    real(fp) :: bmwidza(320)  ! (1:nbeam)
    real(fp) :: divra(320)  ! (1:nbeam)
    real(fp) :: divza(320)  ! (1:nbeam)
    real(fp) :: foclra(320)  ! (1:nbeam)
    real(fp) :: foclza(320)  ! (1:nbeam)
    integer :: nbapsha(320)  ! (1:nbeam)
    real(fp) :: rapedga(320)  ! (1:nbeam)
    real(fp) :: xzpedga(320)  ! (1:nbeam)
    real(fp) :: xlbapa(320)  ! (1:nbeam)
    real(fp) :: xybapa(320)  ! (1:nbeam)
    real(fp) :: xrapoffa(320)  ! (1:nbeam)
    real(fp) :: xzapoffa(320)  ! (1:nbeam)
    integer :: nbapsh2(320)  ! (1:nbeam)
    real(fp) :: rapedg2(320)  ! (1:nbeam)
    real(fp) :: xzpedg2(320)  ! (1:nbeam)
    real(fp) :: xlbapa2(320)  ! (1:nbeam)
    real(fp) :: xrapoff2(320)  ! (1:nbeam)
    real(fp) :: xzapoff2(320)  ! (1:nbeam)
    INTEGER :: nrhix
    real(fp) :: xzimpx(300)  ! (1:nrhix)
    real(fp) :: aimpx(300)  ! (1:nrhix)
    INTEGER :: nmini
    real(fp) :: xzmini(10)  ! (1:nmini)
    real(fp) :: amini(10)  ! (1:nmini)
    INTEGER :: nthrf
    real(fp) :: einjmin(320)  ! (1:nbeam)
    real(fp) :: einja(320)  ! (1:nbeam)
    real(fp) :: pinja(320)  ! (1:nbeam)
    real(fp) :: ffulla(320)  ! (1:nbeam)
    real(fp) :: fhalfa(320)  ! (1:nbeam)
    INTEGER :: nlfhe3
    INTEGER :: nlfhe4
    INTEGER :: nlfst
    INTEGER :: nlfsp
    INTEGER :: nlfsp14
    INTEGER :: nsmax
    integer :: khalo(50)  ! (1:mis)
    REAL(fp) :: dn0out
    INTEGER :: nmsigx
    INTEGER :: nmdifb
    INTEGER :: nkdifb
    INTEGER :: ndifbe
    real(fp) :: fdifbe(20)  ! (1:ndifbe)
    real(fp) :: edifbe(20)  ! (1:ndifbe)
    REAL(fp) :: x_qlim0
    INTEGER :: ndifbe_2d
    REAL(fp) :: dif2d_conv
    REAL(fp) :: dif2d_dpa
    INTEGER :: id_ripple
    INTEGER :: id_ripple2
    INTEGER :: ncoils
    INTEGER :: ncoils_2
    REAL(fp) :: plhgt
    INTEGER :: id_te
    INTEGER :: id_ti
    INTEGER :: id_tifusn
    INTEGER :: id_ne
    INTEGER :: ng
    integer :: id_nh(10)  ! (1:ngmax)
    integer :: id_nimp(300)  ! (1:nrhix)
    integer :: id_nrf(10)  ! (1:nmini)
    integer :: id_eperpmin(10)  ! (1:nmini)
    integer :: id_epllmin(10)  ! (1:nmini)
    integer :: id_nfast(15)  ! (1:nsfast)
    integer :: id_eperpfast(15)  ! (1:nsfast)
    integer :: id_epllfast(15)  ! (1:nsfast)
    INTEGER :: id_aimp
    INTEGER :: id_zimp
    INTEGER :: id_omega
    INTEGER :: id_epot
    INTEGER :: id_vpoh
    INTEGER :: id_curt
    INTEGER :: id_difb
    INTEGER :: id_velb
    INTEGER :: id_difb_bi
    INTEGER :: id_velb_bi
    INTEGER :: id_difb_fi
    INTEGER :: id_velb_fi
    integer :: id_fd0(15)  ! (1:nsfast)
    integer :: id_fdb(15)  ! (1:nsfast)
    integer :: id_fdp(15)  ! (1:nsfast)
    integer :: id_fdq(15)  ! (1:nsfast)
    integer :: id_fdr(15)  ! (1:nsfast)
    integer :: id_fds(15)  ! (1:nsfast)
    integer :: id_n0(50,10)  ! (1:nsmax,1:ngmax)
    integer :: id_t0(50,10)  ! (1:nsmax,1:ngmax)
    integer :: id_omeg0(50,10)  ! (1:nsmax,1:ngmax)
 
  end type nbi_ps_init
 
  type nbi_ps_update
 
    REAL(fp) :: tbm1
    REAL(fp) :: tbm2
    INTEGER :: id_mcgrid
    INTEGER :: nthrf
    real(fp) :: einja(320)  ! (1:nbeam)
    real(fp) :: pinja(320)  ! (1:nbeam)
    real(fp) :: ffulla(320)  ! (1:nbeam)
    real(fp) :: fhalfa(320)  ! (1:nbeam)
    REAL(fp) :: dn0out
    INTEGER :: nmdifb
    real(fp) :: fdifbe(20)  ! (1:ndifbe)
    REAL(fp) :: x_qlim0
    INTEGER :: id_ripple
    INTEGER :: id_ripple2
    REAL(fp) :: plhgt
    INTEGER :: id_te
    INTEGER :: id_ti
    INTEGER :: id_tifusn
    INTEGER :: id_ne
    INTEGER :: ng
    integer :: id_nh(10)  ! (1:ngmax)
    integer :: id_nimp(300)  ! (1:nrhix)
    integer :: id_nrf(10)  ! (1:nmini)
    integer :: id_eperpmin(10)  ! (1:nmini)
    integer :: id_epllmin(10)  ! (1:nmini)
    integer :: id_nfast(15)  ! (1:nsfast)
    integer :: id_eperpfast(15)  ! (1:nsfast)
    integer :: id_epllfast(15)  ! (1:nsfast)
    INTEGER :: id_aimp
    INTEGER :: id_zimp
    INTEGER :: id_omega
    INTEGER :: id_epot
    INTEGER :: id_vpoh
    INTEGER :: id_curt
    INTEGER :: id_difb
    INTEGER :: id_velb
    INTEGER :: id_difb_bi
    INTEGER :: id_velb_bi
    INTEGER :: id_difb_fi
    INTEGER :: id_velb_fi
    integer :: id_fd0(15)  ! (1:nsfast)
    integer :: id_fdb(15)  ! (1:nsfast)
    integer :: id_fdp(15)  ! (1:nsfast)
    integer :: id_fdq(15)  ! (1:nsfast)
    integer :: id_fdr(15)  ! (1:nsfast)
    integer :: id_fds(15)  ! (1:nsfast)
    integer :: id_n0(50,10)  ! (1:nsmax,1:ngmax)
    integer :: id_t0(50,10)  ! (1:nsmax,1:ngmax)
    integer :: id_omeg0(50,10)  ! (1:nsmax,1:ngmax)
 
  end type nbi_ps_update
 
  type nbi_ctrl_init
    SEQUENCE
 
    REAL(fp) :: xbmbnd
    real(fp) :: orbrzv_toric_Prftot0(15)  ! (1:mibs)
    REAL(fp) :: orbrzv_toric_frnm
    REAL(fp) :: xdatsf3
    REAL(fp) :: xdatsfa
    REAL(fp) :: xdatsft
    REAL(fp) :: xdatsfp
    REAL(fp) :: xdatsp3
    REAL(fp) :: plfhe3
    REAL(fp) :: plfhe4
    REAL(fp) :: plfst
    REAL(fp) :: plfsp
    REAL(fp) :: plfsp14
    REAL(fp) :: cxsplt
    REAL(fp) :: dxbsmoo
    REAL(fp) :: dxbsm_nc
    REAL(fp) :: xpand_nptcl
    REAL(fp) :: frac_depmax
    REAL(fp) :: frac_dep_lim
    REAL(fp) :: frac_depmin
    REAL(fp) :: frac_orbrr
    REAL(fp) :: AIMP
    REAL(fp) :: XZIMP
    REAL(fp) :: BI_MAX_PL
    REAL(fp) :: BI_MAX_PR
    REAL(fp) :: wghta
    REAL(fp) :: goocon
    REAL(fp) :: cxpcon
    REAL(fp) :: fppcon
    REAL(fp) :: fdtnxy
    REAL(fp) :: dtn
    REAL(fp) :: dt_acc
    REAL(fp) :: orbrzv_zzerr_con
    REAL(fp) :: xdepmod
    REAL(fp) :: xcfanbi
    REAL(fp) :: xdfanbi
    REAL(fp) :: xefanbi
    REAL(fp) :: xcfafus
    REAL(fp) :: xdfafus
    REAL(fp) :: xefafus
    REAL(fp) :: gflr_min
    REAL(fp) :: gflr_rl
    REAL(fp) :: gflr_ll
    real(fp) :: gflr_xv(2)  ! (1:2)
    real(fp) :: gflr_op(15)  ! (1:15)
    REAL(fp) :: xswfrac_allfast
    REAL(fp) :: xswfrac_beam
    REAL(fp) :: xswfrac_fusn
    REAL(fp) :: fporcelli
    real(fp) :: AEP_ASCALE(10)  ! (1:mnkaep)
    REAL(fp) :: AEP_USCALE
    real(fp) :: TAEP_FAC(10)  ! (1:mnkaep)
    REAL(fp) :: taurip
    REAL(fp) :: asrd
    REAL(fp) :: bsrd
    real(fp) :: erngfi(10,15)  ! (1:nerngfi,1:mibs)
    REAL(fp) :: fbemin
    REAL(fp) :: fbemax
    REAL(fp) :: fvpvmn
    REAL(fp) :: fvpvmx
    REAL(fp) :: fbltim
    REAL(fp) :: fshper
    REAL(fp) :: fshwid
    REAL(fp) :: tfshon
    REAL(fp) :: tfshof
    REAL(fp) :: xfishmin
    REAL(fp) :: xfishmax
    REAL(fp) :: fbtrap_depth
    real(fp) :: xninja_box(100)  ! (1:nbbox)
    real(fp) :: xninjs_box(100)  ! (1:nbbox)
    REAL(fp) :: xninj_box
    REAL(fp) :: xboxhw
    REAL(fp) :: yboxhw
    REAL(fp) :: xlbox1
    REAL(fp) :: xlbox2
    real(fp) :: Rtube(200)  ! (1:nbtube)
    real(fp) :: Ytube(200)  ! (1:nbtube)
    real(fp) :: XZETAtube(200)  ! (1:nbtube)
    real(fp) :: PHItube(200)  ! (1:nbtube)
    real(fp) :: THETAtube(200)  ! (1:nbtube)
    real(fp) :: XL1tube(200)  ! (1:nbtube)
    real(fp) :: XL2tube(200)  ! (1:nbtube)
    REAL(fp) :: RHOtube
    REAL(fp) :: edbfac
 
    INTEGER :: nonlin
    INTEGER :: nseed
    INTEGER :: nlbout
    INTEGER :: lunnbx
    INTEGER :: lunres
    INTEGER :: mrstrt
    INTEGER :: nclass
    INTEGER :: only_io
    INTEGER :: ref_namelist
    INTEGER :: quasi_check
    INTEGER :: nltest_output
    INTEGER :: BLK_MPI_BCAST_INT
    INTEGER :: BLK_MPI_BCAST_R8
    INTEGER :: nzones
    INTEGER :: nzone_fb
    INTEGER :: nlsym2b
    INTEGER :: nth0
    INTEGER :: xplasma_in_memory
    INTEGER :: nznbma
    INTEGER :: nznbme
    INTEGER :: nznbma0
    INTEGER :: nznbme0
    INTEGER :: ngyro
    INTEGER :: nlusf3
    INTEGER :: nlusfa
    INTEGER :: nlusft
    INTEGER :: nlusfp
    INTEGER :: nlusfp14
    INTEGER :: nlfatom
    INTEGER :: nlbfpp
    INTEGER :: nptcls
    INTEGER :: nptclf
    INTEGER :: nptclh
    INTEGER :: NFAST_MAP
    INTEGER :: nptcl_max
    INTEGER :: nltrk_dep0
    INTEGER :: NLDEP0_GATHER
    INTEGER :: ndep0
    INTEGER :: NDEP0_MAX
    INTEGER :: ndep_set_beam
    INTEGER :: ndep_set_ien
    INTEGER :: nbbcal
    INTEGER :: ncx0
    INTEGER :: nper_cx
    INTEGER :: LEV_NBIDEP
    INTEGER :: LEV_TTNUK
    INTEGER :: LEVMOD_HALO
    INTEGER :: NSPLT
    INTEGER :: NSPLT_GEO
    INTEGER :: NSPLT_KIN
    INTEGER :: NSPLT_GEN
    INTEGER :: nmimp
    INTEGER :: NTEST_MAX
    INTEGER :: NGOOCON_VPVBIN
    INTEGER :: NGOOCON_EBIN
    INTEGER :: NGOOCON_RBIN
    INTEGER :: nchdvp
    INTEGER :: orbrzv_option
    INTEGER :: orbrzv_rf_option
    INTEGER :: nlmcrf_on
    INTEGER :: orbrzv_nr
    INTEGER :: orbrzv_nz
    INTEGER :: nlminsv
    INTEGER :: nlebei
    INTEGER :: nsigexc
    INTEGER :: nlbbcx
    INTEGER :: nbbcx_avg
    INTEGER :: nbbcx_bb
    INTEGER :: nbfallgr
    INTEGER :: nlbeamcx
    INTEGER :: nlhvion
    INTEGER :: nlbcde
    INTEGER :: nlbcoh
    INTEGER :: nlbcpa
    INTEGER :: nlorbo
    INTEGER :: nlbflr
    INTEGER :: nlfbmflr
    INTEGER :: nlbgflr
    INTEGER :: sawflag
    INTEGER :: nlsawb
    INTEGER :: nlsawf
    INTEGER :: nmix_kdsaw
    INTEGER :: nkaep
    INTEGER :: ngradd_opt
    INTEGER :: nrip
    INTEGER :: nerngfi
    INTEGER :: nsdbgb
    INTEGER :: nlfbon
    INTEGER :: nfbon_vpvopt
    INTEGER :: nfbon_species
    INTEGER :: nbbox
    integer :: nbsbox(100)  ! (1:nbbox)
    integer :: nsample_box(100)  ! (1:nbbox)
    integer :: nbebox(100)  ! (1:nbbox)
    INTEGER :: nxbox
    INTEGER :: nybox
    INTEGER :: nlbox
    INTEGER :: ndepbox
    INTEGER :: nbtube
    integer :: nbstube(200)  ! (1:nbtube)
    integer :: nbetube(200)  ! (1:nbtube)
    integer :: LMIDtube(200)  ! (1:nbtube)
    integer :: NSEGtube(200)  ! (1:nbtube)
    INTEGER :: NDEPtube
    INTEGER :: nlcprb
    INTEGER :: nlpsirz
    INTEGER :: nmcurb
    INTEGER :: nlfdep
 
  end type nbi_ctrl_init
 
  type nbi_ctrl_update
    SEQUENCE
 
    real(fp) :: orbrzv_toric_Prftot0(15)  ! (1:mibs)
    REAL(fp) :: orbrzv_toric_frnm
    REAL(fp) :: xdatsf3
    REAL(fp) :: xdatsfa
    REAL(fp) :: xdatsft
    REAL(fp) :: xdatsfp
    REAL(fp) :: xdatsp3
    REAL(fp) :: xpand_nptcl
    REAL(fp) :: frac_dep_lim
    REAL(fp) :: AIMP
    REAL(fp) :: XZIMP
    REAL(fp) :: BI_MAX_PL
    REAL(fp) :: BI_MAX_PR
    REAL(fp) :: wghta
    real(fp) :: AEP_ASCALE(10)  ! (1:mnkaep)
    REAL(fp) :: AEP_USCALE
    real(fp) :: TAEP_FAC(10)  ! (1:mnkaep)
    REAL(fp) :: taurip
    REAL(fp) :: edbfac
 
    INTEGER :: nlbout
    INTEGER :: nltest_output
    INTEGER :: xplasma_in_memory
    INTEGER :: ngyro
    INTEGER :: nlusf3
    INTEGER :: nlusfa
    INTEGER :: nlusft
    INTEGER :: nlusfp
    INTEGER :: nlusfp14
    INTEGER :: nptcls
    INTEGER :: nptclf
    INTEGER :: nptclh
    INTEGER :: NFAST_MAP
    INTEGER :: nltrk_dep0
    INTEGER :: NLDEP0_GATHER
    INTEGER :: ndep0
    INTEGER :: NDEP0_MAX
    INTEGER :: ndep_set_beam
    INTEGER :: ndep_set_ien
    INTEGER :: ncx0
    INTEGER :: nper_cx
    INTEGER :: LEV_NBIDEP
    INTEGER :: LEV_TTNUK
    INTEGER :: LEVMOD_HALO
    INTEGER :: NSPLT
    INTEGER :: NSPLT_GEO
    INTEGER :: NSPLT_KIN
    INTEGER :: NSPLT_GEN
    INTEGER :: nmimp
    INTEGER :: NTEST_MAX
    INTEGER :: orbrzv_rf_option
    INTEGER :: nlmcrf_on
    INTEGER :: nsigexc
    INTEGER :: nlbbcx
    INTEGER :: nbbcx_avg
    INTEGER :: nbbcx_bb
    INTEGER :: nlorbo
    INTEGER :: nlbgflr
    INTEGER :: sawflag
    INTEGER :: nlsawb
    INTEGER :: nlsawf
    INTEGER :: nkaep
    INTEGER :: nlfbon
 
  end type nbi_ctrl_update
 
CONTAINS
 
 
  subroutine nbi_ps_init_dflt(blk)
    type (nbi_ps_init) :: blk
 
    blk%runid = ' '
    blk%tbm1 = r8dflt_nbips
    blk%tbm2 = r8dflt_nbips
    blk%id_mcgrid = idflt_nbips
    blk%nthets = idflt_nbips
    blk%ebdmax = r8dflt_nbips
    blk%ngmax = idflt_nbips
    blk%backz = r8dflt_nbips
    blk%aplasm = r8dflt_nbips
    blk%nbeam = idflt_nbips
    blk%xzbeama = r8dflt_nbips
    blk%abeama = r8dflt_nbips
    blk%nlco = idflt_nbips
    blk%ntrace = idflt_nbips
    blk%rtcena = r8dflt_nbips
    blk%xlbtna = r8dflt_nbips
    blk%xybsca = r8dflt_nbips
    blk%xbzeta = r8dflt_nbips
    blk%nbshapa = idflt_nbips
    blk%bmwidra = r8dflt_nbips
    blk%bmwidza = r8dflt_nbips
    blk%divra = r8dflt_nbips
    blk%divza = r8dflt_nbips
    blk%foclra = r8dflt_nbips
    blk%foclza = r8dflt_nbips
    blk%nbapsha = idflt_nbips
    blk%rapedga = r8dflt_nbips
    blk%xzpedga = r8dflt_nbips
    blk%xlbapa = r8dflt_nbips
    blk%xybapa = r8dflt_nbips
    blk%xrapoffa = r8dflt_nbips
    blk%xzapoffa = r8dflt_nbips
    blk%nbapsh2 = idflt_nbips
    blk%rapedg2 = r8dflt_nbips
    blk%xzpedg2 = r8dflt_nbips
    blk%xlbapa2 = r8dflt_nbips
    blk%xrapoff2 = r8dflt_nbips
    blk%xzapoff2 = r8dflt_nbips
    blk%nrhix = idflt_nbips
    blk%xzimpx = r8dflt_nbips
    blk%aimpx = r8dflt_nbips
    blk%nmini = idflt_nbips
    blk%xzmini = r8dflt_nbips
    blk%amini = r8dflt_nbips
    blk%nthrf = idflt_nbips
    blk%einjmin = r8dflt_nbips
    blk%einja = r8dflt_nbips
    blk%pinja = r8dflt_nbips
    blk%ffulla = r8dflt_nbips
    blk%fhalfa = r8dflt_nbips
    blk%nlfhe3 = idflt_nbips
    blk%nlfhe4 = idflt_nbips
    blk%nlfst = idflt_nbips
    blk%nlfsp = idflt_nbips
    blk%nlfsp14 = idflt_nbips
    blk%nsmax = idflt_nbips
    blk%khalo = idflt_nbips
    blk%dn0out = r8dflt_nbips
    blk%nmsigx = idflt_nbips
    blk%nmdifb = idflt_nbips
    blk%nkdifb = idflt_nbips
    blk%ndifbe = idflt_nbips
    blk%fdifbe = r8dflt_nbips
    blk%edifbe = r8dflt_nbips
    blk%x_qlim0 = r8dflt_nbips
    blk%ndifbe_2d = idflt_nbips
    blk%dif2d_conv = r8dflt_nbips
    blk%dif2d_dpa = r8dflt_nbips
    blk%id_ripple = idflt_nbips
    blk%id_ripple2 = idflt_nbips
    blk%ncoils = idflt_nbips
    blk%ncoils_2 = idflt_nbips
    blk%plhgt = r8dflt_nbips
    blk%id_te = idflt_nbips
    blk%id_ti = idflt_nbips
    blk%id_tifusn = idflt_nbips
    blk%id_ne = idflt_nbips
    blk%ng = idflt_nbips
    blk%id_nh = idflt_nbips
    blk%id_nimp = idflt_nbips
    blk%id_nrf = idflt_nbips
    blk%id_eperpmin = idflt_nbips
    blk%id_epllmin = idflt_nbips
    blk%id_nfast = idflt_nbips
    blk%id_eperpfast = idflt_nbips
    blk%id_epllfast = idflt_nbips
    blk%id_aimp = idflt_nbips
    blk%id_zimp = idflt_nbips
    blk%id_omega = idflt_nbips
    blk%id_epot = idflt_nbips
    blk%id_vpoh = idflt_nbips
    blk%id_curt = idflt_nbips
    blk%id_difb = idflt_nbips
    blk%id_velb = idflt_nbips
    blk%id_difb_bi = idflt_nbips
    blk%id_velb_bi = idflt_nbips
    blk%id_difb_fi = idflt_nbips
    blk%id_velb_fi = idflt_nbips
    blk%id_fd0 = idflt_nbips
    blk%id_fdb = idflt_nbips
    blk%id_fdp = idflt_nbips
    blk%id_fdq = idflt_nbips
    blk%id_fdr = idflt_nbips
    blk%id_fds = idflt_nbips
    blk%id_n0 = idflt_nbips
    blk%id_t0 = idflt_nbips
    blk%id_omeg0 = idflt_nbips
 
  end subroutine nbi_ps_init_dflt
 
  subroutine nbi_ps_update_dflt(blk)
    type (nbi_ps_update) :: blk
 
    blk%tbm1 = r8dflt_nbips
    blk%tbm2 = r8dflt_nbips
    blk%id_mcgrid = idflt_nbips
    blk%nthrf = idflt_nbips
    blk%einja = r8dflt_nbips
    blk%pinja = r8dflt_nbips
    blk%ffulla = r8dflt_nbips
    blk%fhalfa = r8dflt_nbips
    blk%dn0out = r8dflt_nbips
    blk%nmdifb = idflt_nbips
    blk%fdifbe = r8dflt_nbips
    blk%x_qlim0 = r8dflt_nbips
    blk%id_ripple = idflt_nbips
    blk%id_ripple2 = idflt_nbips
    blk%plhgt = r8dflt_nbips
    blk%id_te = idflt_nbips
    blk%id_ti = idflt_nbips
    blk%id_tifusn = idflt_nbips
    blk%id_ne = idflt_nbips
    blk%ng = idflt_nbips
    blk%id_nh = idflt_nbips
    blk%id_nimp = idflt_nbips
    blk%id_nrf = idflt_nbips
    blk%id_eperpmin = idflt_nbips
    blk%id_epllmin = idflt_nbips
    blk%id_nfast = idflt_nbips
    blk%id_eperpfast = idflt_nbips
    blk%id_epllfast = idflt_nbips
    blk%id_aimp = idflt_nbips
    blk%id_zimp = idflt_nbips
    blk%id_omega = idflt_nbips
    blk%id_epot = idflt_nbips
    blk%id_vpoh = idflt_nbips
    blk%id_curt = idflt_nbips
    blk%id_difb = idflt_nbips
    blk%id_velb = idflt_nbips
    blk%id_difb_bi = idflt_nbips
    blk%id_velb_bi = idflt_nbips
    blk%id_difb_fi = idflt_nbips
    blk%id_velb_fi = idflt_nbips
    blk%id_fd0 = idflt_nbips
    blk%id_fdb = idflt_nbips
    blk%id_fdp = idflt_nbips
    blk%id_fdq = idflt_nbips
    blk%id_fdr = idflt_nbips
    blk%id_fds = idflt_nbips
    blk%id_n0 = idflt_nbips
    blk%id_t0 = idflt_nbips
    blk%id_omeg0 = idflt_nbips
 
  end subroutine nbi_ps_update_dflt
 
  subroutine nbi_ctrl_init_dflt(blk)
    type (nbi_ctrl_init) :: blk
 
    blk%xbmbnd = r8dflt_nbips
    blk%orbrzv_toric_Prftot0 = r8dflt_nbips
    blk%orbrzv_toric_frnm = r8dflt_nbips
    blk%xdatsf3 = r8dflt_nbips
    blk%xdatsfa = r8dflt_nbips
    blk%xdatsft = r8dflt_nbips
    blk%xdatsfp = r8dflt_nbips
    blk%xdatsp3 = r8dflt_nbips
    blk%plfhe3 = r8dflt_nbips
    blk%plfhe4 = r8dflt_nbips
    blk%plfst = r8dflt_nbips
    blk%plfsp = r8dflt_nbips
    blk%plfsp14 = r8dflt_nbips
    blk%cxsplt = r8dflt_nbips
    blk%dxbsmoo = r8dflt_nbips
    blk%dxbsm_nc = r8dflt_nbips
    blk%xpand_nptcl = r8dflt_nbips
    blk%frac_depmax = r8dflt_nbips
    blk%frac_dep_lim = r8dflt_nbips
    blk%frac_depmin = r8dflt_nbips
    blk%frac_orbrr = r8dflt_nbips
    blk%AIMP = r8dflt_nbips
    blk%XZIMP = r8dflt_nbips
    blk%BI_MAX_PL = r8dflt_nbips
    blk%BI_MAX_PR = r8dflt_nbips
    blk%wghta = r8dflt_nbips
    blk%goocon = r8dflt_nbips
    blk%cxpcon = r8dflt_nbips
    blk%fppcon = r8dflt_nbips
    blk%fdtnxy = r8dflt_nbips
    blk%dtn = r8dflt_nbips
    blk%dt_acc = r8dflt_nbips
    blk%orbrzv_zzerr_con = r8dflt_nbips
    blk%xdepmod = r8dflt_nbips
    blk%xcfanbi = r8dflt_nbips
    blk%xdfanbi = r8dflt_nbips
    blk%xefanbi = r8dflt_nbips
    blk%xcfafus = r8dflt_nbips
    blk%xdfafus = r8dflt_nbips
    blk%xefafus = r8dflt_nbips
    blk%gflr_min = r8dflt_nbips
    blk%gflr_rl = r8dflt_nbips
    blk%gflr_ll = r8dflt_nbips
    blk%gflr_xv = r8dflt_nbips
    blk%gflr_op = r8dflt_nbips
    blk%xswfrac_allfast = r8dflt_nbips
    blk%xswfrac_beam = r8dflt_nbips
    blk%xswfrac_fusn = r8dflt_nbips
    blk%fporcelli = r8dflt_nbips
    blk%AEP_ASCALE = r8dflt_nbips
    blk%AEP_USCALE = r8dflt_nbips
    blk%TAEP_FAC = r8dflt_nbips
    blk%taurip = r8dflt_nbips
    blk%asrd = r8dflt_nbips
    blk%bsrd = r8dflt_nbips
    blk%erngfi = r8dflt_nbips
    blk%fbemin = r8dflt_nbips
    blk%fbemax = r8dflt_nbips
    blk%fvpvmn = r8dflt_nbips
    blk%fvpvmx = r8dflt_nbips
    blk%fbltim = r8dflt_nbips
    blk%fshper = r8dflt_nbips
    blk%fshwid = r8dflt_nbips
    blk%tfshon = r8dflt_nbips
    blk%tfshof = r8dflt_nbips
    blk%xfishmin = r8dflt_nbips
    blk%xfishmax = r8dflt_nbips
    blk%fbtrap_depth = r8dflt_nbips
    blk%xninja_box = r8dflt_nbips
    blk%xninjs_box = r8dflt_nbips
    blk%xninj_box = r8dflt_nbips
    blk%xboxhw = r8dflt_nbips
    blk%yboxhw = r8dflt_nbips
    blk%xlbox1 = r8dflt_nbips
    blk%xlbox2 = r8dflt_nbips
    blk%Rtube = r8dflt_nbips
    blk%Ytube = r8dflt_nbips
    blk%XZETAtube = r8dflt_nbips
    blk%PHItube = r8dflt_nbips
    blk%THETAtube = r8dflt_nbips
    blk%XL1tube = r8dflt_nbips
    blk%XL2tube = r8dflt_nbips
    blk%RHOtube = r8dflt_nbips
    blk%edbfac = r8dflt_nbips
 
    blk%nonlin = idflt_nbips
    blk%nseed = idflt_nbips
    blk%nlbout = idflt_nbips
    blk%lunnbx = idflt_nbips
    blk%lunres = idflt_nbips
    blk%mrstrt = idflt_nbips
    blk%nclass = idflt_nbips
    blk%only_io = idflt_nbips
    blk%ref_namelist = idflt_nbips
    blk%quasi_check = idflt_nbips
    blk%nltest_output = idflt_nbips
    blk%BLK_MPI_BCAST_INT = idflt_nbips
    blk%BLK_MPI_BCAST_R8 = idflt_nbips
    blk%nzones = idflt_nbips
    blk%nzone_fb = idflt_nbips
    blk%nlsym2b = idflt_nbips
    blk%nth0 = idflt_nbips
    blk%xplasma_in_memory = idflt_nbips
    blk%nznbma = idflt_nbips
    blk%nznbme = idflt_nbips
    blk%nznbma0 = idflt_nbips
    blk%nznbme0 = idflt_nbips
    blk%ngyro = idflt_nbips
    blk%nlusf3 = idflt_nbips
    blk%nlusfa = idflt_nbips
    blk%nlusft = idflt_nbips
    blk%nlusfp = idflt_nbips
    blk%nlusfp14 = idflt_nbips
    blk%nlfatom = idflt_nbips
    blk%nlbfpp = idflt_nbips
    blk%nptcls = idflt_nbips
    blk%nptclf = idflt_nbips
    blk%nptclh = idflt_nbips
    blk%NFAST_MAP = idflt_nbips
    blk%nptcl_max = idflt_nbips
    blk%nltrk_dep0 = idflt_nbips
    blk%NLDEP0_GATHER = idflt_nbips
    blk%ndep0 = idflt_nbips
    blk%NDEP0_MAX = idflt_nbips
    blk%ndep_set_beam = idflt_nbips
    blk%ndep_set_ien = idflt_nbips
    blk%nbbcal = idflt_nbips
    blk%ncx0 = idflt_nbips
    blk%nper_cx = idflt_nbips
    blk%LEV_NBIDEP = idflt_nbips
    blk%LEV_TTNUK = idflt_nbips
    blk%LEVMOD_HALO = idflt_nbips
    blk%NSPLT = idflt_nbips
    blk%NSPLT_GEO = idflt_nbips
    blk%NSPLT_KIN = idflt_nbips
    blk%NSPLT_GEN = idflt_nbips
    blk%nmimp = idflt_nbips
    blk%NTEST_MAX = idflt_nbips
    blk%NGOOCON_VPVBIN = idflt_nbips
    blk%NGOOCON_EBIN = idflt_nbips
    blk%NGOOCON_RBIN = idflt_nbips
    blk%nchdvp = idflt_nbips
    blk%orbrzv_option = idflt_nbips
    blk%orbrzv_rf_option = idflt_nbips
    blk%nlmcrf_on = idflt_nbips
    blk%orbrzv_nr = idflt_nbips
    blk%orbrzv_nz = idflt_nbips
    blk%nlminsv = idflt_nbips
    blk%nlebei = idflt_nbips
    blk%nsigexc = idflt_nbips
    blk%nlbbcx = idflt_nbips
    blk%nbbcx_avg = idflt_nbips
    blk%nbbcx_bb = idflt_nbips
    blk%nbfallgr = idflt_nbips
    blk%nlbeamcx = idflt_nbips
    blk%nlhvion = idflt_nbips
    blk%nlbcde = idflt_nbips
    blk%nlbcoh = idflt_nbips
    blk%nlbcpa = idflt_nbips
    blk%nlorbo = idflt_nbips
    blk%nlbflr = idflt_nbips
    blk%nlfbmflr = idflt_nbips
    blk%nlbgflr = idflt_nbips
    blk%sawflag = idflt_nbips
    blk%nlsawb = idflt_nbips
    blk%nlsawf = idflt_nbips
    blk%nmix_kdsaw = idflt_nbips
    blk%nkaep = idflt_nbips
    blk%ngradd_opt = idflt_nbips
    blk%nrip = idflt_nbips
    blk%nerngfi = idflt_nbips
    blk%nsdbgb = idflt_nbips
    blk%nlfbon = idflt_nbips
    blk%nfbon_vpvopt = idflt_nbips
    blk%nfbon_species = idflt_nbips
    blk%nbbox = idflt_nbips
    blk%nbsbox = idflt_nbips
    blk%nsample_box = idflt_nbips
    blk%nbebox = idflt_nbips
    blk%nxbox = idflt_nbips
    blk%nybox = idflt_nbips
    blk%nlbox = idflt_nbips
    blk%ndepbox = idflt_nbips
    blk%nbtube = idflt_nbips
    blk%nbstube = idflt_nbips
    blk%nbetube = idflt_nbips
    blk%LMIDtube = idflt_nbips
    blk%NSEGtube = idflt_nbips
    blk%NDEPtube = idflt_nbips
    blk%nlcprb = idflt_nbips
    blk%nlpsirz = idflt_nbips
    blk%nmcurb = idflt_nbips
    blk%nlfdep = idflt_nbips
 
  end subroutine nbi_ctrl_init_dflt
 
  subroutine nbi_ctrl_update_dflt(blk)
    type (nbi_ctrl_update) :: blk
 
    blk%orbrzv_toric_Prftot0 = r8dflt_nbips
    blk%orbrzv_toric_frnm = r8dflt_nbips
    blk%xdatsf3 = r8dflt_nbips
    blk%xdatsfa = r8dflt_nbips
    blk%xdatsft = r8dflt_nbips
    blk%xdatsfp = r8dflt_nbips
    blk%xdatsp3 = r8dflt_nbips
    blk%xpand_nptcl = r8dflt_nbips
    blk%frac_dep_lim = r8dflt_nbips
    blk%AIMP = r8dflt_nbips
    blk%XZIMP = r8dflt_nbips
    blk%BI_MAX_PL = r8dflt_nbips
    blk%BI_MAX_PR = r8dflt_nbips
    blk%wghta = r8dflt_nbips
    blk%AEP_ASCALE = r8dflt_nbips
    blk%AEP_USCALE = r8dflt_nbips
    blk%TAEP_FAC = r8dflt_nbips
    blk%taurip = r8dflt_nbips
    blk%edbfac = r8dflt_nbips
 
    blk%nlbout = idflt_nbips
    blk%nltest_output = idflt_nbips
    blk%xplasma_in_memory = idflt_nbips
    blk%ngyro = idflt_nbips
    blk%nlusf3 = idflt_nbips
    blk%nlusfa = idflt_nbips
    blk%nlusft = idflt_nbips
    blk%nlusfp = idflt_nbips
    blk%nlusfp14 = idflt_nbips
    blk%nptcls = idflt_nbips
    blk%nptclf = idflt_nbips
    blk%nptclh = idflt_nbips
    blk%NFAST_MAP = idflt_nbips
    blk%nltrk_dep0 = idflt_nbips
    blk%NLDEP0_GATHER = idflt_nbips
    blk%ndep0 = idflt_nbips
    blk%NDEP0_MAX = idflt_nbips
    blk%ndep_set_beam = idflt_nbips
    blk%ndep_set_ien = idflt_nbips
    blk%ncx0 = idflt_nbips
    blk%nper_cx = idflt_nbips
    blk%LEV_NBIDEP = idflt_nbips
    blk%LEV_TTNUK = idflt_nbips
    blk%LEVMOD_HALO = idflt_nbips
    blk%NSPLT = idflt_nbips
    blk%NSPLT_GEO = idflt_nbips
    blk%NSPLT_KIN = idflt_nbips
    blk%NSPLT_GEN = idflt_nbips
    blk%nmimp = idflt_nbips
    blk%NTEST_MAX = idflt_nbips
    blk%orbrzv_rf_option = idflt_nbips
    blk%nlmcrf_on = idflt_nbips
    blk%nsigexc = idflt_nbips
    blk%nlbbcx = idflt_nbips
    blk%nbbcx_avg = idflt_nbips
    blk%nbbcx_bb = idflt_nbips
    blk%nlorbo = idflt_nbips
    blk%nlbgflr = idflt_nbips
    blk%sawflag = idflt_nbips
    blk%nlsawb = idflt_nbips
    blk%nlsawf = idflt_nbips
    blk%nkaep = idflt_nbips
    blk%nlfbon = idflt_nbips
 
  end subroutine nbi_ctrl_update_dflt


  subroutine nbi_ctrl_init_cdfread(ofile,blk,ierr)
    ! read nbi_ctrl_init block in ezcdf file

    character*(*) :: ofile            ! input file
    type (nbi_ctrl_init) :: blk    ! loaded from file
    integer, intent(out) :: ierr      ! status, 0=OK

    !---------------------------
    ! local:
    integer :: icdf
    !---------------------------

    call ezcdf_open(icdf,ofile,'r',ierr)
    if(ierr.ne.0) then
       call nbi_warning( &
            ' ?nbi_ctrl_init_cdfread: ezcdf open failure, file: '// &
            trim(ofile))
       return
    endif

 
    call cdf_read(icdf,'mission',saved_mission)
    call cdf_read(icdf,'workpath',saved_workpath)
 
    call cdf_read(icdf,'xbmbnd',blk%xbmbnd)
    call cdf_read(icdf,'orbrzv_toric_Prftot0',blk%orbrzv_toric_Prftot0)
    call cdf_read(icdf,'orbrzv_toric_frnm',blk%orbrzv_toric_frnm)
    call cdf_read(icdf,'xdatsf3',blk%xdatsf3)
    call cdf_read(icdf,'xdatsfa',blk%xdatsfa)
    call cdf_read(icdf,'xdatsft',blk%xdatsft)
    call cdf_read(icdf,'xdatsfp',blk%xdatsfp)
    call cdf_read(icdf,'xdatsp3',blk%xdatsp3)
    call cdf_read(icdf,'plfhe3',blk%plfhe3)
    call cdf_read(icdf,'plfhe4',blk%plfhe4)
    call cdf_read(icdf,'plfst',blk%plfst)
    call cdf_read(icdf,'plfsp',blk%plfsp)
    call cdf_read(icdf,'plfsp14',blk%plfsp14)
    call cdf_read(icdf,'cxsplt',blk%cxsplt)
    call cdf_read(icdf,'dxbsmoo',blk%dxbsmoo)
    call cdf_read(icdf,'dxbsm_nc',blk%dxbsm_nc)
    call cdf_read(icdf,'xpand_nptcl',blk%xpand_nptcl)
    call cdf_read(icdf,'frac_depmax',blk%frac_depmax)
    call cdf_read(icdf,'frac_dep_lim',blk%frac_dep_lim)
    call cdf_read(icdf,'frac_depmin',blk%frac_depmin)
    call cdf_read(icdf,'frac_orbrr',blk%frac_orbrr)
    call cdf_read(icdf,'AIMP',blk%AIMP)
    call cdf_read(icdf,'XZIMP',blk%XZIMP)
    call cdf_read(icdf,'BI_MAX_PL',blk%BI_MAX_PL)
    call cdf_read(icdf,'BI_MAX_PR',blk%BI_MAX_PR)
    call cdf_read(icdf,'wghta',blk%wghta)
    call cdf_read(icdf,'goocon',blk%goocon)
    call cdf_read(icdf,'cxpcon',blk%cxpcon)
    call cdf_read(icdf,'fppcon',blk%fppcon)
    call cdf_read(icdf,'fdtnxy',blk%fdtnxy)
    call cdf_read(icdf,'dtn',blk%dtn)
    call cdf_read(icdf,'dt_acc',blk%dt_acc)
    call cdf_read(icdf,'orbrzv_zzerr_con',blk%orbrzv_zzerr_con)
    call cdf_read(icdf,'xdepmod',blk%xdepmod)
    call cdf_read(icdf,'xcfanbi',blk%xcfanbi)
    call cdf_read(icdf,'xdfanbi',blk%xdfanbi)
    call cdf_read(icdf,'xefanbi',blk%xefanbi)
    call cdf_read(icdf,'xcfafus',blk%xcfafus)
    call cdf_read(icdf,'xdfafus',blk%xdfafus)
    call cdf_read(icdf,'xefafus',blk%xefafus)
    call cdf_read(icdf,'gflr_min',blk%gflr_min)
    call cdf_read(icdf,'gflr_rl',blk%gflr_rl)
    call cdf_read(icdf,'gflr_ll',blk%gflr_ll)
    call cdf_read(icdf,'gflr_xv',blk%gflr_xv)
    call cdf_read(icdf,'gflr_op',blk%gflr_op)
    call cdf_read(icdf,'xswfrac_allfast',blk%xswfrac_allfast)
    call cdf_read(icdf,'xswfrac_beam',blk%xswfrac_beam)
    call cdf_read(icdf,'xswfrac_fusn',blk%xswfrac_fusn)
    call cdf_read(icdf,'fporcelli',blk%fporcelli)
    call cdf_read(icdf,'AEP_ASCALE',blk%AEP_ASCALE)
    call cdf_read(icdf,'AEP_USCALE',blk%AEP_USCALE)
    call cdf_read(icdf,'TAEP_FAC',blk%TAEP_FAC)
    call cdf_read(icdf,'taurip',blk%taurip)
    call cdf_read(icdf,'asrd',blk%asrd)
    call cdf_read(icdf,'bsrd',blk%bsrd)
    call cdf_read(icdf,'erngfi',blk%erngfi)
    call cdf_read(icdf,'fbemin',blk%fbemin)
    call cdf_read(icdf,'fbemax',blk%fbemax)
    call cdf_read(icdf,'fvpvmn',blk%fvpvmn)
    call cdf_read(icdf,'fvpvmx',blk%fvpvmx)
    call cdf_read(icdf,'fbltim',blk%fbltim)
    call cdf_read(icdf,'fshper',blk%fshper)
    call cdf_read(icdf,'fshwid',blk%fshwid)
    call cdf_read(icdf,'tfshon',blk%tfshon)
    call cdf_read(icdf,'tfshof',blk%tfshof)
    call cdf_read(icdf,'xfishmin',blk%xfishmin)
    call cdf_read(icdf,'xfishmax',blk%xfishmax)
    call cdf_read(icdf,'fbtrap_depth',blk%fbtrap_depth)
    call cdf_read(icdf,'xninja_box',blk%xninja_box)
    call cdf_read(icdf,'xninjs_box',blk%xninjs_box)
    call cdf_read(icdf,'xninj_box',blk%xninj_box)
    call cdf_read(icdf,'xboxhw',blk%xboxhw)
    call cdf_read(icdf,'yboxhw',blk%yboxhw)
    call cdf_read(icdf,'xlbox1',blk%xlbox1)
    call cdf_read(icdf,'xlbox2',blk%xlbox2)
    call cdf_read(icdf,'Rtube',blk%Rtube)
    call cdf_read(icdf,'Ytube',blk%Ytube)
    call cdf_read(icdf,'XZETAtube',blk%XZETAtube)
    call cdf_read(icdf,'PHItube',blk%PHItube)
    call cdf_read(icdf,'THETAtube',blk%THETAtube)
    call cdf_read(icdf,'XL1tube',blk%XL1tube)
    call cdf_read(icdf,'XL2tube',blk%XL2tube)
    call cdf_read(icdf,'RHOtube',blk%RHOtube)
    call cdf_read(icdf,'edbfac',blk%edbfac)
 
    call cdf_read(icdf,'nonlin',blk%nonlin)
    call cdf_read(icdf,'nseed',blk%nseed)
    call cdf_read(icdf,'nlbout',blk%nlbout)
    call cdf_read(icdf,'lunnbx',blk%lunnbx)
    call cdf_read(icdf,'lunres',blk%lunres)
    call cdf_read(icdf,'mrstrt',blk%mrstrt)
    call cdf_read(icdf,'nclass',blk%nclass)
    call cdf_read(icdf,'only_io',blk%only_io)
    call cdf_read(icdf,'ref_namelist',blk%ref_namelist)
    call cdf_read(icdf,'quasi_check',blk%quasi_check)
    call cdf_read(icdf,'nltest_output',blk%nltest_output)
    call cdf_read(icdf,'BLK_MPI_BCAST_INT',blk%BLK_MPI_BCAST_INT)
    call cdf_read(icdf,'BLK_MPI_BCAST_R8',blk%BLK_MPI_BCAST_R8)
    call cdf_read(icdf,'nzones',blk%nzones)
    call cdf_read(icdf,'nzone_fb',blk%nzone_fb)
    call cdf_read(icdf,'nlsym2b',blk%nlsym2b)
    call cdf_read(icdf,'nth0',blk%nth0)
    call cdf_read(icdf,'xplasma_in_memory',blk%xplasma_in_memory)
    call cdf_read(icdf,'nznbma',blk%nznbma)
    call cdf_read(icdf,'nznbme',blk%nznbme)
    call cdf_read(icdf,'nznbma0',blk%nznbma0)
    call cdf_read(icdf,'nznbme0',blk%nznbme0)
    call cdf_read(icdf,'ngyro',blk%ngyro)
    call cdf_read(icdf,'nlusf3',blk%nlusf3)
    call cdf_read(icdf,'nlusfa',blk%nlusfa)
    call cdf_read(icdf,'nlusft',blk%nlusft)
    call cdf_read(icdf,'nlusfp',blk%nlusfp)
    call cdf_read(icdf,'nlusfp14',blk%nlusfp14)
    call cdf_read(icdf,'nlfatom',blk%nlfatom)
    call cdf_read(icdf,'nlbfpp',blk%nlbfpp)
    call cdf_read(icdf,'nptcls',blk%nptcls)
    call cdf_read(icdf,'nptclf',blk%nptclf)
    call cdf_read(icdf,'nptclh',blk%nptclh)
    call cdf_read(icdf,'NFAST_MAP',blk%NFAST_MAP)
    call cdf_read(icdf,'nptcl_max',blk%nptcl_max)
    call cdf_read(icdf,'nltrk_dep0',blk%nltrk_dep0)
    call cdf_read(icdf,'NLDEP0_GATHER',blk%NLDEP0_GATHER)
    call cdf_read(icdf,'ndep0',blk%ndep0)
    call cdf_read(icdf,'NDEP0_MAX',blk%NDEP0_MAX)
    call cdf_read(icdf,'ndep_set_beam',blk%ndep_set_beam)
    call cdf_read(icdf,'ndep_set_ien',blk%ndep_set_ien)
    call cdf_read(icdf,'nbbcal',blk%nbbcal)
    call cdf_read(icdf,'ncx0',blk%ncx0)
    call cdf_read(icdf,'nper_cx',blk%nper_cx)
    call cdf_read(icdf,'LEV_NBIDEP',blk%LEV_NBIDEP)
    call cdf_read(icdf,'LEV_TTNUK',blk%LEV_TTNUK)
    call cdf_read(icdf,'LEVMOD_HALO',blk%LEVMOD_HALO)
    call cdf_read(icdf,'NSPLT',blk%NSPLT)
    call cdf_read(icdf,'NSPLT_GEO',blk%NSPLT_GEO)
    call cdf_read(icdf,'NSPLT_KIN',blk%NSPLT_KIN)
    call cdf_read(icdf,'NSPLT_GEN',blk%NSPLT_GEN)
    call cdf_read(icdf,'nmimp',blk%nmimp)
    call cdf_read(icdf,'NTEST_MAX',blk%NTEST_MAX)
    call cdf_read(icdf,'NGOOCON_VPVBIN',blk%NGOOCON_VPVBIN)
    call cdf_read(icdf,'NGOOCON_EBIN',blk%NGOOCON_EBIN)
    call cdf_read(icdf,'NGOOCON_RBIN',blk%NGOOCON_RBIN)
    call cdf_read(icdf,'nchdvp',blk%nchdvp)
    call cdf_read(icdf,'orbrzv_option',blk%orbrzv_option)
    call cdf_read(icdf,'orbrzv_rf_option',blk%orbrzv_rf_option)
    call cdf_read(icdf,'nlmcrf_on',blk%nlmcrf_on)
    call cdf_read(icdf,'orbrzv_nr',blk%orbrzv_nr)
    call cdf_read(icdf,'orbrzv_nz',blk%orbrzv_nz)
    call cdf_read(icdf,'nlminsv',blk%nlminsv)
    call cdf_read(icdf,'nlebei',blk%nlebei)
    call cdf_read(icdf,'nsigexc',blk%nsigexc)
    call cdf_read(icdf,'nlbbcx',blk%nlbbcx)
    call cdf_read(icdf,'nbbcx_avg',blk%nbbcx_avg)
    call cdf_read(icdf,'nbbcx_bb',blk%nbbcx_bb)
    call cdf_read(icdf,'nbfallgr',blk%nbfallgr)
    call cdf_read(icdf,'nlbeamcx',blk%nlbeamcx)
    call cdf_read(icdf,'nlhvion',blk%nlhvion)
    call cdf_read(icdf,'nlbcde',blk%nlbcde)
    call cdf_read(icdf,'nlbcoh',blk%nlbcoh)
    call cdf_read(icdf,'nlbcpa',blk%nlbcpa)
    call cdf_read(icdf,'nlorbo',blk%nlorbo)
    call cdf_read(icdf,'nlbflr',blk%nlbflr)
    call cdf_read(icdf,'nlfbmflr',blk%nlfbmflr)
    call cdf_read(icdf,'nlbgflr',blk%nlbgflr)
    call cdf_read(icdf,'sawflag',blk%sawflag)
    call cdf_read(icdf,'nlsawb',blk%nlsawb)
    call cdf_read(icdf,'nlsawf',blk%nlsawf)
    call cdf_read(icdf,'nmix_kdsaw',blk%nmix_kdsaw)
    call cdf_read(icdf,'nkaep',blk%nkaep)
    call cdf_read(icdf,'ngradd_opt',blk%ngradd_opt)
    call cdf_read(icdf,'nrip',blk%nrip)
    call cdf_read(icdf,'nerngfi',blk%nerngfi)
    call cdf_read(icdf,'nsdbgb',blk%nsdbgb)
    call cdf_read(icdf,'nlfbon',blk%nlfbon)
    call cdf_read(icdf,'nfbon_vpvopt',blk%nfbon_vpvopt)
    call cdf_read(icdf,'nfbon_species',blk%nfbon_species)
    call cdf_read(icdf,'nbbox',blk%nbbox)
    call cdf_read(icdf,'nbsbox',blk%nbsbox)
    call cdf_read(icdf,'nsample_box',blk%nsample_box)
    call cdf_read(icdf,'nbebox',blk%nbebox)
    call cdf_read(icdf,'nxbox',blk%nxbox)
    call cdf_read(icdf,'nybox',blk%nybox)
    call cdf_read(icdf,'nlbox',blk%nlbox)
    call cdf_read(icdf,'ndepbox',blk%ndepbox)
    call cdf_read(icdf,'nbtube',blk%nbtube)
    call cdf_read(icdf,'nbstube',blk%nbstube)
    call cdf_read(icdf,'nbetube',blk%nbetube)
    call cdf_read(icdf,'LMIDtube',blk%LMIDtube)
    call cdf_read(icdf,'NSEGtube',blk%NSEGtube)
    call cdf_read(icdf,'NDEPtube',blk%NDEPtube)
    call cdf_read(icdf,'nlcprb',blk%nlcprb)
    call cdf_read(icdf,'nlpsirz',blk%nlpsirz)
    call cdf_read(icdf,'nmcurb',blk%nmcurb)
    call cdf_read(icdf,'nlfdep',blk%nlfdep)


    call ezcdf_close(icdf)

  end subroutine nbi_ctrl_init_cdfread

  subroutine nbi_ctrl_update_cdfread(ofile,blk,ierr)
    ! read nbi_ctrl_update block in ezcdf file

    character*(*) :: ofile            ! input file
    type (nbi_ctrl_update) :: blk    ! loaded from file
    integer, intent(out) :: ierr      ! status, 0=OK

    !---------------------------
    ! local:
    integer :: icdf
    !---------------------------

    call ezcdf_open(icdf,ofile,'r',ierr)
    if(ierr.ne.0) then
       call nbi_warning( &
            ' ?nbi_ctrl_update_cdfread: ezcdf open failure, file: '// &
            trim(ofile))
       return
    endif

 
    call cdf_read(icdf,'orbrzv_toric_Prftot0',blk%orbrzv_toric_Prftot0)
    call cdf_read(icdf,'orbrzv_toric_frnm',blk%orbrzv_toric_frnm)
    call cdf_read(icdf,'xdatsf3',blk%xdatsf3)
    call cdf_read(icdf,'xdatsfa',blk%xdatsfa)
    call cdf_read(icdf,'xdatsft',blk%xdatsft)
    call cdf_read(icdf,'xdatsfp',blk%xdatsfp)
    call cdf_read(icdf,'xdatsp3',blk%xdatsp3)
    call cdf_read(icdf,'xpand_nptcl',blk%xpand_nptcl)
    call cdf_read(icdf,'frac_dep_lim',blk%frac_dep_lim)
    call cdf_read(icdf,'AIMP',blk%AIMP)
    call cdf_read(icdf,'XZIMP',blk%XZIMP)
    call cdf_read(icdf,'BI_MAX_PL',blk%BI_MAX_PL)
    call cdf_read(icdf,'BI_MAX_PR',blk%BI_MAX_PR)
    call cdf_read(icdf,'wghta',blk%wghta)
    call cdf_read(icdf,'AEP_ASCALE',blk%AEP_ASCALE)
    call cdf_read(icdf,'AEP_USCALE',blk%AEP_USCALE)
    call cdf_read(icdf,'TAEP_FAC',blk%TAEP_FAC)
    call cdf_read(icdf,'taurip',blk%taurip)
    call cdf_read(icdf,'edbfac',blk%edbfac)
 
    call cdf_read(icdf,'nlbout',blk%nlbout)
    call cdf_read(icdf,'nltest_output',blk%nltest_output)
    call cdf_read(icdf,'xplasma_in_memory',blk%xplasma_in_memory)
    call cdf_read(icdf,'ngyro',blk%ngyro)
    call cdf_read(icdf,'nlusf3',blk%nlusf3)
    call cdf_read(icdf,'nlusfa',blk%nlusfa)
    call cdf_read(icdf,'nlusft',blk%nlusft)
    call cdf_read(icdf,'nlusfp',blk%nlusfp)
    call cdf_read(icdf,'nlusfp14',blk%nlusfp14)
    call cdf_read(icdf,'nptcls',blk%nptcls)
    call cdf_read(icdf,'nptclf',blk%nptclf)
    call cdf_read(icdf,'nptclh',blk%nptclh)
    call cdf_read(icdf,'NFAST_MAP',blk%NFAST_MAP)
    call cdf_read(icdf,'nltrk_dep0',blk%nltrk_dep0)
    call cdf_read(icdf,'NLDEP0_GATHER',blk%NLDEP0_GATHER)
    call cdf_read(icdf,'ndep0',blk%ndep0)
    call cdf_read(icdf,'NDEP0_MAX',blk%NDEP0_MAX)
    call cdf_read(icdf,'ndep_set_beam',blk%ndep_set_beam)
    call cdf_read(icdf,'ndep_set_ien',blk%ndep_set_ien)
    call cdf_read(icdf,'ncx0',blk%ncx0)
    call cdf_read(icdf,'nper_cx',blk%nper_cx)
    call cdf_read(icdf,'LEV_NBIDEP',blk%LEV_NBIDEP)
    call cdf_read(icdf,'LEV_TTNUK',blk%LEV_TTNUK)
    call cdf_read(icdf,'LEVMOD_HALO',blk%LEVMOD_HALO)
    call cdf_read(icdf,'NSPLT',blk%NSPLT)
    call cdf_read(icdf,'NSPLT_GEO',blk%NSPLT_GEO)
    call cdf_read(icdf,'NSPLT_KIN',blk%NSPLT_KIN)
    call cdf_read(icdf,'NSPLT_GEN',blk%NSPLT_GEN)
    call cdf_read(icdf,'nmimp',blk%nmimp)
    call cdf_read(icdf,'NTEST_MAX',blk%NTEST_MAX)
    call cdf_read(icdf,'orbrzv_rf_option',blk%orbrzv_rf_option)
    call cdf_read(icdf,'nlmcrf_on',blk%nlmcrf_on)
    call cdf_read(icdf,'nsigexc',blk%nsigexc)
    call cdf_read(icdf,'nlbbcx',blk%nlbbcx)
    call cdf_read(icdf,'nbbcx_avg',blk%nbbcx_avg)
    call cdf_read(icdf,'nbbcx_bb',blk%nbbcx_bb)
    call cdf_read(icdf,'nlorbo',blk%nlorbo)
    call cdf_read(icdf,'nlbgflr',blk%nlbgflr)
    call cdf_read(icdf,'sawflag',blk%sawflag)
    call cdf_read(icdf,'nlsawb',blk%nlsawb)
    call cdf_read(icdf,'nlsawf',blk%nlsawf)
    call cdf_read(icdf,'nkaep',blk%nkaep)
    call cdf_read(icdf,'nlfbon',blk%nlfbon)


    call ezcdf_close(icdf)

  end subroutine nbi_ctrl_update_cdfread


  subroutine nbi_ctrl_init_read(ifile,blk,ierr)

    character*(*) :: ifile            ! input file
    type (nbi_ctrl_init) :: blk    ! read from file
    integer, intent(out) :: ierr      ! status, 0=OK

    !---------------------------
    ! local:
    character*40 :: tmpfilnam
    integer :: ilun_dbg = 0
    integer :: ilun_tmp,ilnb,istat
    !---------------------------
    ! namelist:
 
    CHARACTER*120 :: mission
    CHARACTER*120 :: workpath
 
    REAL(fp) :: xbmbnd
    real(fp) :: orbrzv_toric_Prftot0(15)  ! (1:mibs)
    REAL(fp) :: orbrzv_toric_frnm
    REAL(fp) :: xdatsf3
    REAL(fp) :: xdatsfa
    REAL(fp) :: xdatsft
    REAL(fp) :: xdatsfp
    REAL(fp) :: xdatsp3
    REAL(fp) :: plfhe3
    REAL(fp) :: plfhe4
    REAL(fp) :: plfst
    REAL(fp) :: plfsp
    REAL(fp) :: plfsp14
    REAL(fp) :: cxsplt
    REAL(fp) :: dxbsmoo
    REAL(fp) :: dxbsm_nc
    REAL(fp) :: xpand_nptcl
    REAL(fp) :: frac_depmax
    REAL(fp) :: frac_dep_lim
    REAL(fp) :: frac_depmin
    REAL(fp) :: frac_orbrr
    REAL(fp) :: AIMP
    REAL(fp) :: XZIMP
    REAL(fp) :: BI_MAX_PL
    REAL(fp) :: BI_MAX_PR
    REAL(fp) :: wghta
    REAL(fp) :: goocon
    REAL(fp) :: cxpcon
    REAL(fp) :: fppcon
    REAL(fp) :: fdtnxy
    REAL(fp) :: dtn
    REAL(fp) :: dt_acc
    REAL(fp) :: orbrzv_zzerr_con
    REAL(fp) :: xdepmod
    REAL(fp) :: xcfanbi
    REAL(fp) :: xdfanbi
    REAL(fp) :: xefanbi
    REAL(fp) :: xcfafus
    REAL(fp) :: xdfafus
    REAL(fp) :: xefafus
    REAL(fp) :: gflr_min
    REAL(fp) :: gflr_rl
    REAL(fp) :: gflr_ll
    real(fp) :: gflr_xv(2)  ! (1:2)
    real(fp) :: gflr_op(15)  ! (1:15)
    REAL(fp) :: xswfrac_allfast
    REAL(fp) :: xswfrac_beam
    REAL(fp) :: xswfrac_fusn
    REAL(fp) :: fporcelli
    real(fp) :: AEP_ASCALE(10)  ! (1:mnkaep)
    REAL(fp) :: AEP_USCALE
    real(fp) :: TAEP_FAC(10)  ! (1:mnkaep)
    REAL(fp) :: taurip
    REAL(fp) :: asrd
    REAL(fp) :: bsrd
    real(fp) :: erngfi(10,15)  ! (1:nerngfi,1:mibs)
    REAL(fp) :: fbemin
    REAL(fp) :: fbemax
    REAL(fp) :: fvpvmn
    REAL(fp) :: fvpvmx
    REAL(fp) :: fbltim
    REAL(fp) :: fshper
    REAL(fp) :: fshwid
    REAL(fp) :: tfshon
    REAL(fp) :: tfshof
    REAL(fp) :: xfishmin
    REAL(fp) :: xfishmax
    REAL(fp) :: fbtrap_depth
    real(fp) :: xninja_box(100)  ! (1:nbbox)
    real(fp) :: xninjs_box(100)  ! (1:nbbox)
    REAL(fp) :: xninj_box
    REAL(fp) :: xboxhw
    REAL(fp) :: yboxhw
    REAL(fp) :: xlbox1
    REAL(fp) :: xlbox2
    real(fp) :: Rtube(200)  ! (1:nbtube)
    real(fp) :: Ytube(200)  ! (1:nbtube)
    real(fp) :: XZETAtube(200)  ! (1:nbtube)
    real(fp) :: PHItube(200)  ! (1:nbtube)
    real(fp) :: THETAtube(200)  ! (1:nbtube)
    real(fp) :: XL1tube(200)  ! (1:nbtube)
    real(fp) :: XL2tube(200)  ! (1:nbtube)
    REAL(fp) :: RHOtube
    REAL(fp) :: edbfac
 
    INTEGER :: nonlin
    INTEGER :: nseed
    INTEGER :: nlbout
    INTEGER :: lunnbx
    INTEGER :: lunres
    INTEGER :: mrstrt
    INTEGER :: nclass
    INTEGER :: only_io
    INTEGER :: ref_namelist
    INTEGER :: quasi_check
    INTEGER :: nltest_output
    INTEGER :: BLK_MPI_BCAST_INT
    INTEGER :: BLK_MPI_BCAST_R8
    INTEGER :: nzones
    INTEGER :: nzone_fb
    INTEGER :: nlsym2b
    INTEGER :: nth0
    INTEGER :: xplasma_in_memory
    INTEGER :: nznbma
    INTEGER :: nznbme
    INTEGER :: nznbma0
    INTEGER :: nznbme0
    INTEGER :: ngyro
    INTEGER :: nlusf3
    INTEGER :: nlusfa
    INTEGER :: nlusft
    INTEGER :: nlusfp
    INTEGER :: nlusfp14
    INTEGER :: nlfatom
    INTEGER :: nlbfpp
    INTEGER :: nptcls
    INTEGER :: nptclf
    INTEGER :: nptclh
    INTEGER :: NFAST_MAP
    INTEGER :: nptcl_max
    INTEGER :: nltrk_dep0
    INTEGER :: NLDEP0_GATHER
    INTEGER :: ndep0
    INTEGER :: NDEP0_MAX
    INTEGER :: ndep_set_beam
    INTEGER :: ndep_set_ien
    INTEGER :: nbbcal
    INTEGER :: ncx0
    INTEGER :: nper_cx
    INTEGER :: LEV_NBIDEP
    INTEGER :: LEV_TTNUK
    INTEGER :: LEVMOD_HALO
    INTEGER :: NSPLT
    INTEGER :: NSPLT_GEO
    INTEGER :: NSPLT_KIN
    INTEGER :: NSPLT_GEN
    INTEGER :: nmimp
    INTEGER :: NTEST_MAX
    INTEGER :: NGOOCON_VPVBIN
    INTEGER :: NGOOCON_EBIN
    INTEGER :: NGOOCON_RBIN
    INTEGER :: nchdvp
    INTEGER :: orbrzv_option
    INTEGER :: orbrzv_rf_option
    INTEGER :: nlmcrf_on
    INTEGER :: orbrzv_nr
    INTEGER :: orbrzv_nz
    INTEGER :: nlminsv
    INTEGER :: nlebei
    INTEGER :: nsigexc
    INTEGER :: nlbbcx
    INTEGER :: nbbcx_avg
    INTEGER :: nbbcx_bb
    INTEGER :: nbfallgr
    INTEGER :: nlbeamcx
    INTEGER :: nlhvion
    INTEGER :: nlbcde
    INTEGER :: nlbcoh
    INTEGER :: nlbcpa
    INTEGER :: nlorbo
    INTEGER :: nlbflr
    INTEGER :: nlfbmflr
    INTEGER :: nlbgflr
    INTEGER :: sawflag
    INTEGER :: nlsawb
    INTEGER :: nlsawf
    INTEGER :: nmix_kdsaw
    INTEGER :: nkaep
    INTEGER :: ngradd_opt
    INTEGER :: nrip
    INTEGER :: nerngfi
    INTEGER :: nsdbgb
    INTEGER :: nlfbon
    INTEGER :: nfbon_vpvopt
    INTEGER :: nfbon_species
    INTEGER :: nbbox
    integer :: nbsbox(100)  ! (1:nbbox)
    integer :: nsample_box(100)  ! (1:nbbox)
    integer :: nbebox(100)  ! (1:nbbox)
    INTEGER :: nxbox
    INTEGER :: nybox
    INTEGER :: nlbox
    INTEGER :: ndepbox
    INTEGER :: nbtube
    integer :: nbstube(200)  ! (1:nbtube)
    integer :: nbetube(200)  ! (1:nbtube)
    integer :: LMIDtube(200)  ! (1:nbtube)
    integer :: NSEGtube(200)  ! (1:nbtube)
    INTEGER :: NDEPtube
    INTEGER :: nlcprb
    INTEGER :: nlpsirz
    INTEGER :: nmcurb
    INTEGER :: nlfdep
 
    namelist/NBI_INIT/ &
        mission, workpath, xbmbnd, orbrzv_toric_Prftot0,  &
        orbrzv_toric_frnm, xdatsf3, xdatsfa, xdatsft,  &
        xdatsfp, xdatsp3, plfhe3, plfhe4,  &
        plfst, plfsp, plfsp14, cxsplt,  &
        dxbsmoo, dxbsm_nc, xpand_nptcl, frac_depmax,  &
        frac_dep_lim, frac_depmin, frac_orbrr, AIMP,  &
        XZIMP, BI_MAX_PL, BI_MAX_PR, wghta,  &
        goocon, cxpcon, fppcon, fdtnxy,  &
        dtn, dt_acc, orbrzv_zzerr_con, xdepmod,  &
        xcfanbi, xdfanbi, xefanbi, xcfafus,  &
        xdfafus, xefafus, gflr_min, gflr_rl,  &
        gflr_ll, gflr_xv, gflr_op, xswfrac_allfast,  &
        xswfrac_beam, xswfrac_fusn, fporcelli, AEP_ASCALE,  &
        AEP_USCALE, TAEP_FAC, taurip, asrd,  &
        bsrd, erngfi, fbemin, fbemax,  &
        fvpvmn, fvpvmx, fbltim, fshper,  &
        fshwid, tfshon, tfshof, xfishmin,  &
        xfishmax, fbtrap_depth, xninja_box, xninjs_box,  &
        xninj_box, xboxhw, yboxhw, xlbox1,  &
        xlbox2, Rtube, Ytube, XZETAtube,  &
        PHItube, THETAtube, XL1tube, XL2tube,  &
        RHOtube, edbfac, nonlin, nseed,  &
        nlbout, lunnbx, lunres, mrstrt,  &
        nclass, only_io, ref_namelist, quasi_check,  &
        nltest_output, BLK_MPI_BCAST_INT, BLK_MPI_BCAST_R8, nzones,  &
        nzone_fb, nlsym2b, nth0, xplasma_in_memory,  &
        nznbma, nznbme, nznbma0, nznbme0,  &
        ngyro, nlusf3, nlusfa, nlusft,  &
        nlusfp, nlusfp14, nlfatom, nlbfpp,  &
        nptcls, nptclf, nptclh, NFAST_MAP,  &
        nptcl_max, nltrk_dep0, NLDEP0_GATHER, ndep0,  &
        NDEP0_MAX, ndep_set_beam, ndep_set_ien, nbbcal,  &
        ncx0, nper_cx, LEV_NBIDEP, LEV_TTNUK,  &
        LEVMOD_HALO, NSPLT, NSPLT_GEO, NSPLT_KIN,  &
        NSPLT_GEN, nmimp, NTEST_MAX, NGOOCON_VPVBIN,  &
        NGOOCON_EBIN, NGOOCON_RBIN, nchdvp, orbrzv_option,  &
        orbrzv_rf_option, nlmcrf_on, orbrzv_nr, orbrzv_nz,  &
        nlminsv, nlebei, nsigexc, nlbbcx,  &
        nbbcx_avg, nbbcx_bb, nbfallgr, nlbeamcx,  &
        nlhvion, nlbcde, nlbcoh, nlbcpa,  &
        nlorbo, nlbflr, nlfbmflr, nlbgflr,  &
        sawflag, nlsawb, nlsawf, nmix_kdsaw,  &
        nkaep, ngradd_opt, nrip, nerngfi,  &
        nsdbgb, nlfbon, nfbon_vpvopt, nfbon_species,  &
        nbbox, nbsbox, nsample_box, nbebox,  &
        nxbox, nybox, nlbox, ndepbox,  &
        nbtube, nbstube, nbetube, LMIDtube,  &
        NSEGtube, NDEPtube, nlcprb, nlpsirz,  &
        nmcurb, nlfdep 

    !---------------------------
    ! error check

    if(init_called) then
       write(ilun_dbg,*) "?nbi_ctrl_init_read: NUBEAM initialization already performed; cannot be repeated."
       ierr=1
       return
    else
       ierr=0
    endif

    ! set default values:

    mission =  "standard"
    workpath =  " "
 
    xbmbnd = r8dflt_nbips
    orbrzv_toric_Prftot0 = r8dflt_nbips
    orbrzv_toric_frnm = r8dflt_nbips
    xdatsf3 = r8dflt_nbips
    xdatsfa = r8dflt_nbips
    xdatsft = r8dflt_nbips
    xdatsfp = r8dflt_nbips
    xdatsp3 = r8dflt_nbips
    plfhe3 = r8dflt_nbips
    plfhe4 = r8dflt_nbips
    plfst = r8dflt_nbips
    plfsp = r8dflt_nbips
    plfsp14 = r8dflt_nbips
    cxsplt = r8dflt_nbips
    dxbsmoo = r8dflt_nbips
    dxbsm_nc = r8dflt_nbips
    xpand_nptcl = r8dflt_nbips
    frac_depmax = r8dflt_nbips
    frac_dep_lim = r8dflt_nbips
    frac_depmin = r8dflt_nbips
    frac_orbrr = r8dflt_nbips
    AIMP = r8dflt_nbips
    XZIMP = r8dflt_nbips
    BI_MAX_PL = r8dflt_nbips
    BI_MAX_PR = r8dflt_nbips
    wghta = r8dflt_nbips
    goocon = r8dflt_nbips
    cxpcon = r8dflt_nbips
    fppcon = r8dflt_nbips
    fdtnxy = r8dflt_nbips
    dtn = r8dflt_nbips
    dt_acc = r8dflt_nbips
    orbrzv_zzerr_con = r8dflt_nbips
    xdepmod = r8dflt_nbips
    xcfanbi = r8dflt_nbips
    xdfanbi = r8dflt_nbips
    xefanbi = r8dflt_nbips
    xcfafus = r8dflt_nbips
    xdfafus = r8dflt_nbips
    xefafus = r8dflt_nbips
    gflr_min = r8dflt_nbips
    gflr_rl = r8dflt_nbips
    gflr_ll = r8dflt_nbips
    gflr_xv = r8dflt_nbips
    gflr_op = r8dflt_nbips
    xswfrac_allfast = r8dflt_nbips
    xswfrac_beam = r8dflt_nbips
    xswfrac_fusn = r8dflt_nbips
    fporcelli = r8dflt_nbips
    AEP_ASCALE = r8dflt_nbips
    AEP_USCALE = r8dflt_nbips
    TAEP_FAC = r8dflt_nbips
    taurip = r8dflt_nbips
    asrd = r8dflt_nbips
    bsrd = r8dflt_nbips
    erngfi = r8dflt_nbips
    fbemin = r8dflt_nbips
    fbemax = r8dflt_nbips
    fvpvmn = r8dflt_nbips
    fvpvmx = r8dflt_nbips
    fbltim = r8dflt_nbips
    fshper = r8dflt_nbips
    fshwid = r8dflt_nbips
    tfshon = r8dflt_nbips
    tfshof = r8dflt_nbips
    xfishmin = r8dflt_nbips
    xfishmax = r8dflt_nbips
    fbtrap_depth = r8dflt_nbips
    xninja_box = r8dflt_nbips
    xninjs_box = r8dflt_nbips
    xninj_box = r8dflt_nbips
    xboxhw = r8dflt_nbips
    yboxhw = r8dflt_nbips
    xlbox1 = r8dflt_nbips
    xlbox2 = r8dflt_nbips
    Rtube = r8dflt_nbips
    Ytube = r8dflt_nbips
    XZETAtube = r8dflt_nbips
    PHItube = r8dflt_nbips
    THETAtube = r8dflt_nbips
    XL1tube = r8dflt_nbips
    XL2tube = r8dflt_nbips
    RHOtube = r8dflt_nbips
    edbfac = r8dflt_nbips
 
    nonlin = idflt_nbips
    nseed = idflt_nbips
    nlbout = idflt_nbips
    lunnbx = idflt_nbips
    lunres = idflt_nbips
    mrstrt = idflt_nbips
    nclass = idflt_nbips
    only_io = idflt_nbips
    ref_namelist = idflt_nbips
    quasi_check = idflt_nbips
    nltest_output = idflt_nbips
    BLK_MPI_BCAST_INT = idflt_nbips
    BLK_MPI_BCAST_R8 = idflt_nbips
    nzones = idflt_nbips
    nzone_fb = idflt_nbips
    nlsym2b = idflt_nbips
    nth0 = idflt_nbips
    xplasma_in_memory = idflt_nbips
    nznbma = idflt_nbips
    nznbme = idflt_nbips
    nznbma0 = idflt_nbips
    nznbme0 = idflt_nbips
    ngyro = idflt_nbips
    nlusf3 = idflt_nbips
    nlusfa = idflt_nbips
    nlusft = idflt_nbips
    nlusfp = idflt_nbips
    nlusfp14 = idflt_nbips
    nlfatom = idflt_nbips
    nlbfpp = idflt_nbips
    nptcls = idflt_nbips
    nptclf = idflt_nbips
    nptclh = idflt_nbips
    NFAST_MAP = idflt_nbips
    nptcl_max = idflt_nbips
    nltrk_dep0 = idflt_nbips
    NLDEP0_GATHER = idflt_nbips
    ndep0 = idflt_nbips
    NDEP0_MAX = idflt_nbips
    ndep_set_beam = idflt_nbips
    ndep_set_ien = idflt_nbips
    nbbcal = idflt_nbips
    ncx0 = idflt_nbips
    nper_cx = idflt_nbips
    LEV_NBIDEP = idflt_nbips
    LEV_TTNUK = idflt_nbips
    LEVMOD_HALO = idflt_nbips
    NSPLT = idflt_nbips
    NSPLT_GEO = idflt_nbips
    NSPLT_KIN = idflt_nbips
    NSPLT_GEN = idflt_nbips
    nmimp = idflt_nbips
    NTEST_MAX = idflt_nbips
    NGOOCON_VPVBIN = idflt_nbips
    NGOOCON_EBIN = idflt_nbips
    NGOOCON_RBIN = idflt_nbips
    nchdvp = idflt_nbips
    orbrzv_option = idflt_nbips
    orbrzv_rf_option = idflt_nbips
    nlmcrf_on = idflt_nbips
    orbrzv_nr = idflt_nbips
    orbrzv_nz = idflt_nbips
    nlminsv = idflt_nbips
    nlebei = idflt_nbips
    nsigexc = idflt_nbips
    nlbbcx = idflt_nbips
    nbbcx_avg = idflt_nbips
    nbbcx_bb = idflt_nbips
    nbfallgr = idflt_nbips
    nlbeamcx = idflt_nbips
    nlhvion = idflt_nbips
    nlbcde = idflt_nbips
    nlbcoh = idflt_nbips
    nlbcpa = idflt_nbips
    nlorbo = idflt_nbips
    nlbflr = idflt_nbips
    nlfbmflr = idflt_nbips
    nlbgflr = idflt_nbips
    sawflag = idflt_nbips
    nlsawb = idflt_nbips
    nlsawf = idflt_nbips
    nmix_kdsaw = idflt_nbips
    nkaep = idflt_nbips
    ngradd_opt = idflt_nbips
    nrip = idflt_nbips
    nerngfi = idflt_nbips
    nsdbgb = idflt_nbips
    nlfbon = idflt_nbips
    nfbon_vpvopt = idflt_nbips
    nfbon_species = idflt_nbips
    nbbox = idflt_nbips
    nbsbox = idflt_nbips
    nsample_box = idflt_nbips
    nbebox = idflt_nbips
    nxbox = idflt_nbips
    nybox = idflt_nbips
    nlbox = idflt_nbips
    ndepbox = idflt_nbips
    nbtube = idflt_nbips
    nbstube = idflt_nbips
    nbetube = idflt_nbips
    LMIDtube = idflt_nbips
    NSEGtube = idflt_nbips
    NDEPtube = idflt_nbips
    nlcprb = idflt_nbips
    nlpsirz = idflt_nbips
    nmcurb = idflt_nbips
    nlfdep = idflt_nbips


    call find_io_unit(ilun_tmp)
    open(UNIT=ilun_tmp,file=ifile,status='old',iostat=istat)
    if(istat.eq.0) then
       read(UNIT=ilun_tmp,NML=NBI_INIT,iostat=istat)
       close(UNIT=ilun_tmp)
    endif
    if(istat.ne.0) then
       write(ilun_dbg,*) "?nbi_ctrl_init_read: error reading: "//trim(ifile)
       ierr=1
       return
    endif

    call nubeam_set_mission(mission,ierr)
    call nubeam_set_workpath(workpath,ierr)
 
    blk%xbmbnd = xbmbnd
    blk%orbrzv_toric_Prftot0 = orbrzv_toric_Prftot0
    blk%orbrzv_toric_frnm = orbrzv_toric_frnm
    blk%xdatsf3 = xdatsf3
    blk%xdatsfa = xdatsfa
    blk%xdatsft = xdatsft
    blk%xdatsfp = xdatsfp
    blk%xdatsp3 = xdatsp3
    blk%plfhe3 = plfhe3
    blk%plfhe4 = plfhe4
    blk%plfst = plfst
    blk%plfsp = plfsp
    blk%plfsp14 = plfsp14
    blk%cxsplt = cxsplt
    blk%dxbsmoo = dxbsmoo
    blk%dxbsm_nc = dxbsm_nc
    blk%xpand_nptcl = xpand_nptcl
    blk%frac_depmax = frac_depmax
    blk%frac_dep_lim = frac_dep_lim
    blk%frac_depmin = frac_depmin
    blk%frac_orbrr = frac_orbrr
    blk%AIMP = AIMP
    blk%XZIMP = XZIMP
    blk%BI_MAX_PL = BI_MAX_PL
    blk%BI_MAX_PR = BI_MAX_PR
    blk%wghta = wghta
    blk%goocon = goocon
    blk%cxpcon = cxpcon
    blk%fppcon = fppcon
    blk%fdtnxy = fdtnxy
    blk%dtn = dtn
    blk%dt_acc = dt_acc
    blk%orbrzv_zzerr_con = orbrzv_zzerr_con
    blk%xdepmod = xdepmod
    blk%xcfanbi = xcfanbi
    blk%xdfanbi = xdfanbi
    blk%xefanbi = xefanbi
    blk%xcfafus = xcfafus
    blk%xdfafus = xdfafus
    blk%xefafus = xefafus
    blk%gflr_min = gflr_min
    blk%gflr_rl = gflr_rl
    blk%gflr_ll = gflr_ll
    blk%gflr_xv = gflr_xv
    blk%gflr_op = gflr_op
    blk%xswfrac_allfast = xswfrac_allfast
    blk%xswfrac_beam = xswfrac_beam
    blk%xswfrac_fusn = xswfrac_fusn
    blk%fporcelli = fporcelli
    blk%AEP_ASCALE = AEP_ASCALE
    blk%AEP_USCALE = AEP_USCALE
    blk%TAEP_FAC = TAEP_FAC
    blk%taurip = taurip
    blk%asrd = asrd
    blk%bsrd = bsrd
    blk%erngfi = erngfi
    blk%fbemin = fbemin
    blk%fbemax = fbemax
    blk%fvpvmn = fvpvmn
    blk%fvpvmx = fvpvmx
    blk%fbltim = fbltim
    blk%fshper = fshper
    blk%fshwid = fshwid
    blk%tfshon = tfshon
    blk%tfshof = tfshof
    blk%xfishmin = xfishmin
    blk%xfishmax = xfishmax
    blk%fbtrap_depth = fbtrap_depth
    blk%xninja_box = xninja_box
    blk%xninjs_box = xninjs_box
    blk%xninj_box = xninj_box
    blk%xboxhw = xboxhw
    blk%yboxhw = yboxhw
    blk%xlbox1 = xlbox1
    blk%xlbox2 = xlbox2
    blk%Rtube = Rtube
    blk%Ytube = Ytube
    blk%XZETAtube = XZETAtube
    blk%PHItube = PHItube
    blk%THETAtube = THETAtube
    blk%XL1tube = XL1tube
    blk%XL2tube = XL2tube
    blk%RHOtube = RHOtube
    blk%edbfac = edbfac
 
    blk%nonlin = nonlin
    blk%nseed = nseed
    blk%nlbout = nlbout
    blk%lunnbx = lunnbx
    blk%lunres = lunres
    blk%mrstrt = mrstrt
    blk%nclass = nclass
    blk%only_io = only_io
    blk%ref_namelist = ref_namelist
    blk%quasi_check = quasi_check
    blk%nltest_output = nltest_output
    blk%BLK_MPI_BCAST_INT = BLK_MPI_BCAST_INT
    blk%BLK_MPI_BCAST_R8 = BLK_MPI_BCAST_R8
    blk%nzones = nzones
    blk%nzone_fb = nzone_fb
    blk%nlsym2b = nlsym2b
    blk%nth0 = nth0
    blk%xplasma_in_memory = xplasma_in_memory
    blk%nznbma = nznbma
    blk%nznbme = nznbme
    blk%nznbma0 = nznbma0
    blk%nznbme0 = nznbme0
    blk%ngyro = ngyro
    blk%nlusf3 = nlusf3
    blk%nlusfa = nlusfa
    blk%nlusft = nlusft
    blk%nlusfp = nlusfp
    blk%nlusfp14 = nlusfp14
    blk%nlfatom = nlfatom
    blk%nlbfpp = nlbfpp
    blk%nptcls = nptcls
    blk%nptclf = nptclf
    blk%nptclh = nptclh
    blk%NFAST_MAP = NFAST_MAP
    blk%nptcl_max = nptcl_max
    blk%nltrk_dep0 = nltrk_dep0
    blk%NLDEP0_GATHER = NLDEP0_GATHER
    blk%ndep0 = ndep0
    blk%NDEP0_MAX = NDEP0_MAX
    blk%ndep_set_beam = ndep_set_beam
    blk%ndep_set_ien = ndep_set_ien
    blk%nbbcal = nbbcal
    blk%ncx0 = ncx0
    blk%nper_cx = nper_cx
    blk%LEV_NBIDEP = LEV_NBIDEP
    blk%LEV_TTNUK = LEV_TTNUK
    blk%LEVMOD_HALO = LEVMOD_HALO
    blk%NSPLT = NSPLT
    blk%NSPLT_GEO = NSPLT_GEO
    blk%NSPLT_KIN = NSPLT_KIN
    blk%NSPLT_GEN = NSPLT_GEN
    blk%nmimp = nmimp
    blk%NTEST_MAX = NTEST_MAX
    blk%NGOOCON_VPVBIN = NGOOCON_VPVBIN
    blk%NGOOCON_EBIN = NGOOCON_EBIN
    blk%NGOOCON_RBIN = NGOOCON_RBIN
    blk%nchdvp = nchdvp
    blk%orbrzv_option = orbrzv_option
    blk%orbrzv_rf_option = orbrzv_rf_option
    blk%nlmcrf_on = nlmcrf_on
    blk%orbrzv_nr = orbrzv_nr
    blk%orbrzv_nz = orbrzv_nz
    blk%nlminsv = nlminsv
    blk%nlebei = nlebei
    blk%nsigexc = nsigexc
    blk%nlbbcx = nlbbcx
    blk%nbbcx_avg = nbbcx_avg
    blk%nbbcx_bb = nbbcx_bb
    blk%nbfallgr = nbfallgr
    blk%nlbeamcx = nlbeamcx
    blk%nlhvion = nlhvion
    blk%nlbcde = nlbcde
    blk%nlbcoh = nlbcoh
    blk%nlbcpa = nlbcpa
    blk%nlorbo = nlorbo
    blk%nlbflr = nlbflr
    blk%nlfbmflr = nlfbmflr
    blk%nlbgflr = nlbgflr
    blk%sawflag = sawflag
    blk%nlsawb = nlsawb
    blk%nlsawf = nlsawf
    blk%nmix_kdsaw = nmix_kdsaw
    blk%nkaep = nkaep
    blk%ngradd_opt = ngradd_opt
    blk%nrip = nrip
    blk%nerngfi = nerngfi
    blk%nsdbgb = nsdbgb
    blk%nlfbon = nlfbon
    blk%nfbon_vpvopt = nfbon_vpvopt
    blk%nfbon_species = nfbon_species
    blk%nbbox = nbbox
    blk%nbsbox = nbsbox
    blk%nsample_box = nsample_box
    blk%nbebox = nbebox
    blk%nxbox = nxbox
    blk%nybox = nybox
    blk%nlbox = nlbox
    blk%ndepbox = ndepbox
    blk%nbtube = nbtube
    blk%nbstube = nbstube
    blk%nbetube = nbetube
    blk%LMIDtube = LMIDtube
    blk%NSEGtube = NSEGtube
    blk%NDEPtube = NDEPtube
    blk%nlcprb = nlcprb
    blk%nlpsirz = nlpsirz
    blk%nmcurb = nmcurb
    blk%nlfdep = nlfdep


  end subroutine nbi_ctrl_init_read

  subroutine nbi_ctrl_update_read(ifile,blk,ierr)
    character*(*) :: ifile            ! input file
    type (nbi_ctrl_update) :: blk  ! read from file
    integer, intent(out) :: ierr      ! status, 0=OK

    !---------------------------
    ! local:
    character*40 :: tmpfilnam
    integer :: ilun_dbg = 0
    integer :: ilun_tmp,ilnb,istat
    !---------------------------
    ! namelist:
 
    real(fp) :: orbrzv_toric_Prftot0(15)  ! (1:mibs)
    REAL(fp) :: orbrzv_toric_frnm
    REAL(fp) :: xdatsf3
    REAL(fp) :: xdatsfa
    REAL(fp) :: xdatsft
    REAL(fp) :: xdatsfp
    REAL(fp) :: xdatsp3
    REAL(fp) :: xpand_nptcl
    REAL(fp) :: frac_dep_lim
    REAL(fp) :: AIMP
    REAL(fp) :: XZIMP
    REAL(fp) :: BI_MAX_PL
    REAL(fp) :: BI_MAX_PR
    REAL(fp) :: wghta
    real(fp) :: AEP_ASCALE(10)  ! (1:mnkaep)
    REAL(fp) :: AEP_USCALE
    real(fp) :: TAEP_FAC(10)  ! (1:mnkaep)
    REAL(fp) :: taurip
    REAL(fp) :: edbfac
 
    INTEGER :: nlbout
    INTEGER :: nltest_output
    INTEGER :: xplasma_in_memory
    INTEGER :: ngyro
    INTEGER :: nlusf3
    INTEGER :: nlusfa
    INTEGER :: nlusft
    INTEGER :: nlusfp
    INTEGER :: nlusfp14
    INTEGER :: nptcls
    INTEGER :: nptclf
    INTEGER :: nptclh
    INTEGER :: NFAST_MAP
    INTEGER :: nltrk_dep0
    INTEGER :: NLDEP0_GATHER
    INTEGER :: ndep0
    INTEGER :: NDEP0_MAX
    INTEGER :: ndep_set_beam
    INTEGER :: ndep_set_ien
    INTEGER :: ncx0
    INTEGER :: nper_cx
    INTEGER :: LEV_NBIDEP
    INTEGER :: LEV_TTNUK
    INTEGER :: LEVMOD_HALO
    INTEGER :: NSPLT
    INTEGER :: NSPLT_GEO
    INTEGER :: NSPLT_KIN
    INTEGER :: NSPLT_GEN
    INTEGER :: nmimp
    INTEGER :: NTEST_MAX
    INTEGER :: orbrzv_rf_option
    INTEGER :: nlmcrf_on
    INTEGER :: nsigexc
    INTEGER :: nlbbcx
    INTEGER :: nbbcx_avg
    INTEGER :: nbbcx_bb
    INTEGER :: nlorbo
    INTEGER :: nlbgflr
    INTEGER :: sawflag
    INTEGER :: nlsawb
    INTEGER :: nlsawf
    INTEGER :: nkaep
    INTEGER :: nlfbon
 
    namelist/NBI_UPDATE/ &
        orbrzv_toric_Prftot0, orbrzv_toric_frnm, xdatsf3, xdatsfa,  &
        xdatsft, xdatsfp, xdatsp3, xpand_nptcl,  &
        frac_dep_lim, AIMP, XZIMP, BI_MAX_PL,  &
        BI_MAX_PR, wghta, AEP_ASCALE, AEP_USCALE,  &
        TAEP_FAC, taurip, edbfac, nlbout,  &
        nltest_output, xplasma_in_memory, ngyro, nlusf3,  &
        nlusfa, nlusft, nlusfp, nlusfp14,  &
        nptcls, nptclf, nptclh, NFAST_MAP,  &
        nltrk_dep0, NLDEP0_GATHER, ndep0, NDEP0_MAX,  &
        ndep_set_beam, ndep_set_ien, ncx0, nper_cx,  &
        LEV_NBIDEP, LEV_TTNUK, LEVMOD_HALO, NSPLT,  &
        NSPLT_GEO, NSPLT_KIN, NSPLT_GEN, nmimp,  &
        NTEST_MAX, orbrzv_rf_option, nlmcrf_on, nsigexc,  &
        nlbbcx, nbbcx_avg, nbbcx_bb, nlorbo,  &
        nlbgflr, sawflag, nlsawb, nlsawf,  &
        nkaep, nlfbon 


    !---------------------------
    ierr = 0

    !---------------------------
    ! set default values:

    orbrzv_toric_Prftot0 = r8dflt_nbips
    orbrzv_toric_frnm = r8dflt_nbips
    xdatsf3 = r8dflt_nbips
    xdatsfa = r8dflt_nbips
    xdatsft = r8dflt_nbips
    xdatsfp = r8dflt_nbips
    xdatsp3 = r8dflt_nbips
    xpand_nptcl = r8dflt_nbips
    frac_dep_lim = r8dflt_nbips
    AIMP = r8dflt_nbips
    XZIMP = r8dflt_nbips
    BI_MAX_PL = r8dflt_nbips
    BI_MAX_PR = r8dflt_nbips
    wghta = r8dflt_nbips
    AEP_ASCALE = r8dflt_nbips
    AEP_USCALE = r8dflt_nbips
    TAEP_FAC = r8dflt_nbips
    taurip = r8dflt_nbips
    edbfac = r8dflt_nbips
 
    nlbout = idflt_nbips
    nltest_output = idflt_nbips
    xplasma_in_memory = idflt_nbips
    ngyro = idflt_nbips
    nlusf3 = idflt_nbips
    nlusfa = idflt_nbips
    nlusft = idflt_nbips
    nlusfp = idflt_nbips
    nlusfp14 = idflt_nbips
    nptcls = idflt_nbips
    nptclf = idflt_nbips
    nptclh = idflt_nbips
    NFAST_MAP = idflt_nbips
    nltrk_dep0 = idflt_nbips
    NLDEP0_GATHER = idflt_nbips
    ndep0 = idflt_nbips
    NDEP0_MAX = idflt_nbips
    ndep_set_beam = idflt_nbips
    ndep_set_ien = idflt_nbips
    ncx0 = idflt_nbips
    nper_cx = idflt_nbips
    LEV_NBIDEP = idflt_nbips
    LEV_TTNUK = idflt_nbips
    LEVMOD_HALO = idflt_nbips
    NSPLT = idflt_nbips
    NSPLT_GEO = idflt_nbips
    NSPLT_KIN = idflt_nbips
    NSPLT_GEN = idflt_nbips
    nmimp = idflt_nbips
    NTEST_MAX = idflt_nbips
    orbrzv_rf_option = idflt_nbips
    nlmcrf_on = idflt_nbips
    nsigexc = idflt_nbips
    nlbbcx = idflt_nbips
    nbbcx_avg = idflt_nbips
    nbbcx_bb = idflt_nbips
    nlorbo = idflt_nbips
    nlbgflr = idflt_nbips
    sawflag = idflt_nbips
    nlsawb = idflt_nbips
    nlsawf = idflt_nbips
    nkaep = idflt_nbips
    nlfbon = idflt_nbips


    call find_io_unit(ilun_tmp)
    open(UNIT=ilun_tmp,file=ifile,status='old',iostat=istat)
    if(istat.eq.0) then
       read(UNIT=ilun_tmp,NML=NBI_UPDATE,iostat=istat)
       close(UNIT=ilun_tmp)
    endif
    if(istat.ne.0) then
       write(ilun_dbg,*) "?nbi_ctrl_update_read: error reading: "//trim(ifile)
       ierr=1
       return
    endif

    blk%orbrzv_toric_Prftot0 = orbrzv_toric_Prftot0
    blk%orbrzv_toric_frnm = orbrzv_toric_frnm
    blk%xdatsf3 = xdatsf3
    blk%xdatsfa = xdatsfa
    blk%xdatsft = xdatsft
    blk%xdatsfp = xdatsfp
    blk%xdatsp3 = xdatsp3
    blk%xpand_nptcl = xpand_nptcl
    blk%frac_dep_lim = frac_dep_lim
    blk%AIMP = AIMP
    blk%XZIMP = XZIMP
    blk%BI_MAX_PL = BI_MAX_PL
    blk%BI_MAX_PR = BI_MAX_PR
    blk%wghta = wghta
    blk%AEP_ASCALE = AEP_ASCALE
    blk%AEP_USCALE = AEP_USCALE
    blk%TAEP_FAC = TAEP_FAC
    blk%taurip = taurip
    blk%edbfac = edbfac
 
    blk%nlbout = nlbout
    blk%nltest_output = nltest_output
    blk%xplasma_in_memory = xplasma_in_memory
    blk%ngyro = ngyro
    blk%nlusf3 = nlusf3
    blk%nlusfa = nlusfa
    blk%nlusft = nlusft
    blk%nlusfp = nlusfp
    blk%nlusfp14 = nlusfp14
    blk%nptcls = nptcls
    blk%nptclf = nptclf
    blk%nptclh = nptclh
    blk%NFAST_MAP = NFAST_MAP
    blk%nltrk_dep0 = nltrk_dep0
    blk%NLDEP0_GATHER = NLDEP0_GATHER
    blk%ndep0 = ndep0
    blk%NDEP0_MAX = NDEP0_MAX
    blk%ndep_set_beam = ndep_set_beam
    blk%ndep_set_ien = ndep_set_ien
    blk%ncx0 = ncx0
    blk%nper_cx = nper_cx
    blk%LEV_NBIDEP = LEV_NBIDEP
    blk%LEV_TTNUK = LEV_TTNUK
    blk%LEVMOD_HALO = LEVMOD_HALO
    blk%NSPLT = NSPLT
    blk%NSPLT_GEO = NSPLT_GEO
    blk%NSPLT_KIN = NSPLT_KIN
    blk%NSPLT_GEN = NSPLT_GEN
    blk%nmimp = nmimp
    blk%NTEST_MAX = NTEST_MAX
    blk%orbrzv_rf_option = orbrzv_rf_option
    blk%nlmcrf_on = nlmcrf_on
    blk%nsigexc = nsigexc
    blk%nlbbcx = nlbbcx
    blk%nbbcx_avg = nbbcx_avg
    blk%nbbcx_bb = nbbcx_bb
    blk%nlorbo = nlorbo
    blk%nlbgflr = nlbgflr
    blk%sawflag = sawflag
    blk%nlsawb = nlsawb
    blk%nlsawf = nlsawf
    blk%nkaep = nkaep
    blk%nlfbon = nlfbon


  end subroutine nbi_ctrl_update_read

  subroutine nbi_ctrl_init_cdfwrite(ofile,blk,ierr)
    ! write nbi_ctrl_init block in ezcdf file

    character*(*) :: ofile            ! output file
    type (nbi_ctrl_init) :: blk    ! written to file
    integer, intent(out) :: ierr      ! status, 0=OK

    !---------------------------
    ! local:
    integer :: icdf
    !---------------------------

    call ezcdf_open(icdf,ofile,'w',ierr)
    if(ierr.ne.0) then
       call nbi_warning( &
            ' ?nbi_ctrl_init_cdfwrite: ezcdf open failure, file: '// &
            trim(ofile))
       return
    endif

 
    call cdf_define(icdf,'mission',saved_mission)
    call cdf_define(icdf,'workpath',saved_workpath)
 
    call cdf_define(icdf,'xbmbnd',blk%xbmbnd)
    call cdf_define(icdf,'orbrzv_toric_Prftot0',blk%orbrzv_toric_Prftot0)
    call cdf_define(icdf,'orbrzv_toric_frnm',blk%orbrzv_toric_frnm)
    call cdf_define(icdf,'xdatsf3',blk%xdatsf3)
    call cdf_define(icdf,'xdatsfa',blk%xdatsfa)
    call cdf_define(icdf,'xdatsft',blk%xdatsft)
    call cdf_define(icdf,'xdatsfp',blk%xdatsfp)
    call cdf_define(icdf,'xdatsp3',blk%xdatsp3)
    call cdf_define(icdf,'plfhe3',blk%plfhe3)
    call cdf_define(icdf,'plfhe4',blk%plfhe4)
    call cdf_define(icdf,'plfst',blk%plfst)
    call cdf_define(icdf,'plfsp',blk%plfsp)
    call cdf_define(icdf,'plfsp14',blk%plfsp14)
    call cdf_define(icdf,'cxsplt',blk%cxsplt)
    call cdf_define(icdf,'dxbsmoo',blk%dxbsmoo)
    call cdf_define(icdf,'dxbsm_nc',blk%dxbsm_nc)
    call cdf_define(icdf,'xpand_nptcl',blk%xpand_nptcl)
    call cdf_define(icdf,'frac_depmax',blk%frac_depmax)
    call cdf_define(icdf,'frac_dep_lim',blk%frac_dep_lim)
    call cdf_define(icdf,'frac_depmin',blk%frac_depmin)
    call cdf_define(icdf,'frac_orbrr',blk%frac_orbrr)
    call cdf_define(icdf,'AIMP',blk%AIMP)
    call cdf_define(icdf,'XZIMP',blk%XZIMP)
    call cdf_define(icdf,'BI_MAX_PL',blk%BI_MAX_PL)
    call cdf_define(icdf,'BI_MAX_PR',blk%BI_MAX_PR)
    call cdf_define(icdf,'wghta',blk%wghta)
    call cdf_define(icdf,'goocon',blk%goocon)
    call cdf_define(icdf,'cxpcon',blk%cxpcon)
    call cdf_define(icdf,'fppcon',blk%fppcon)
    call cdf_define(icdf,'fdtnxy',blk%fdtnxy)
    call cdf_define(icdf,'dtn',blk%dtn)
    call cdf_define(icdf,'dt_acc',blk%dt_acc)
    call cdf_define(icdf,'orbrzv_zzerr_con',blk%orbrzv_zzerr_con)
    call cdf_define(icdf,'xdepmod',blk%xdepmod)
    call cdf_define(icdf,'xcfanbi',blk%xcfanbi)
    call cdf_define(icdf,'xdfanbi',blk%xdfanbi)
    call cdf_define(icdf,'xefanbi',blk%xefanbi)
    call cdf_define(icdf,'xcfafus',blk%xcfafus)
    call cdf_define(icdf,'xdfafus',blk%xdfafus)
    call cdf_define(icdf,'xefafus',blk%xefafus)
    call cdf_define(icdf,'gflr_min',blk%gflr_min)
    call cdf_define(icdf,'gflr_rl',blk%gflr_rl)
    call cdf_define(icdf,'gflr_ll',blk%gflr_ll)
    call cdf_define(icdf,'gflr_xv',blk%gflr_xv)
    call cdf_define(icdf,'gflr_op',blk%gflr_op)
    call cdf_define(icdf,'xswfrac_allfast',blk%xswfrac_allfast)
    call cdf_define(icdf,'xswfrac_beam',blk%xswfrac_beam)
    call cdf_define(icdf,'xswfrac_fusn',blk%xswfrac_fusn)
    call cdf_define(icdf,'fporcelli',blk%fporcelli)
    call cdf_define(icdf,'AEP_ASCALE',blk%AEP_ASCALE)
    call cdf_define(icdf,'AEP_USCALE',blk%AEP_USCALE)
    call cdf_define(icdf,'TAEP_FAC',blk%TAEP_FAC)
    call cdf_define(icdf,'taurip',blk%taurip)
    call cdf_define(icdf,'asrd',blk%asrd)
    call cdf_define(icdf,'bsrd',blk%bsrd)
    call cdf_define(icdf,'erngfi',blk%erngfi)
    call cdf_define(icdf,'fbemin',blk%fbemin)
    call cdf_define(icdf,'fbemax',blk%fbemax)
    call cdf_define(icdf,'fvpvmn',blk%fvpvmn)
    call cdf_define(icdf,'fvpvmx',blk%fvpvmx)
    call cdf_define(icdf,'fbltim',blk%fbltim)
    call cdf_define(icdf,'fshper',blk%fshper)
    call cdf_define(icdf,'fshwid',blk%fshwid)
    call cdf_define(icdf,'tfshon',blk%tfshon)
    call cdf_define(icdf,'tfshof',blk%tfshof)
    call cdf_define(icdf,'xfishmin',blk%xfishmin)
    call cdf_define(icdf,'xfishmax',blk%xfishmax)
    call cdf_define(icdf,'fbtrap_depth',blk%fbtrap_depth)
    call cdf_define(icdf,'xninja_box',blk%xninja_box)
    call cdf_define(icdf,'xninjs_box',blk%xninjs_box)
    call cdf_define(icdf,'xninj_box',blk%xninj_box)
    call cdf_define(icdf,'xboxhw',blk%xboxhw)
    call cdf_define(icdf,'yboxhw',blk%yboxhw)
    call cdf_define(icdf,'xlbox1',blk%xlbox1)
    call cdf_define(icdf,'xlbox2',blk%xlbox2)
    call cdf_define(icdf,'Rtube',blk%Rtube)
    call cdf_define(icdf,'Ytube',blk%Ytube)
    call cdf_define(icdf,'XZETAtube',blk%XZETAtube)
    call cdf_define(icdf,'PHItube',blk%PHItube)
    call cdf_define(icdf,'THETAtube',blk%THETAtube)
    call cdf_define(icdf,'XL1tube',blk%XL1tube)
    call cdf_define(icdf,'XL2tube',blk%XL2tube)
    call cdf_define(icdf,'RHOtube',blk%RHOtube)
    call cdf_define(icdf,'edbfac',blk%edbfac)
 
    call cdf_define(icdf,'nonlin',blk%nonlin)
    call cdf_define(icdf,'nseed',blk%nseed)
    call cdf_define(icdf,'nlbout',blk%nlbout)
    call cdf_define(icdf,'lunnbx',blk%lunnbx)
    call cdf_define(icdf,'lunres',blk%lunres)
    call cdf_define(icdf,'mrstrt',blk%mrstrt)
    call cdf_define(icdf,'nclass',blk%nclass)
    call cdf_define(icdf,'only_io',blk%only_io)
    call cdf_define(icdf,'ref_namelist',blk%ref_namelist)
    call cdf_define(icdf,'quasi_check',blk%quasi_check)
    call cdf_define(icdf,'nltest_output',blk%nltest_output)
    call cdf_define(icdf,'BLK_MPI_BCAST_INT',blk%BLK_MPI_BCAST_INT)
    call cdf_define(icdf,'BLK_MPI_BCAST_R8',blk%BLK_MPI_BCAST_R8)
    call cdf_define(icdf,'nzones',blk%nzones)
    call cdf_define(icdf,'nzone_fb',blk%nzone_fb)
    call cdf_define(icdf,'nlsym2b',blk%nlsym2b)
    call cdf_define(icdf,'nth0',blk%nth0)
    call cdf_define(icdf,'xplasma_in_memory',blk%xplasma_in_memory)
    call cdf_define(icdf,'nznbma',blk%nznbma)
    call cdf_define(icdf,'nznbme',blk%nznbme)
    call cdf_define(icdf,'nznbma0',blk%nznbma0)
    call cdf_define(icdf,'nznbme0',blk%nznbme0)
    call cdf_define(icdf,'ngyro',blk%ngyro)
    call cdf_define(icdf,'nlusf3',blk%nlusf3)
    call cdf_define(icdf,'nlusfa',blk%nlusfa)
    call cdf_define(icdf,'nlusft',blk%nlusft)
    call cdf_define(icdf,'nlusfp',blk%nlusfp)
    call cdf_define(icdf,'nlusfp14',blk%nlusfp14)
    call cdf_define(icdf,'nlfatom',blk%nlfatom)
    call cdf_define(icdf,'nlbfpp',blk%nlbfpp)
    call cdf_define(icdf,'nptcls',blk%nptcls)
    call cdf_define(icdf,'nptclf',blk%nptclf)
    call cdf_define(icdf,'nptclh',blk%nptclh)
    call cdf_define(icdf,'NFAST_MAP',blk%NFAST_MAP)
    call cdf_define(icdf,'nptcl_max',blk%nptcl_max)
    call cdf_define(icdf,'nltrk_dep0',blk%nltrk_dep0)
    call cdf_define(icdf,'NLDEP0_GATHER',blk%NLDEP0_GATHER)
    call cdf_define(icdf,'ndep0',blk%ndep0)
    call cdf_define(icdf,'NDEP0_MAX',blk%NDEP0_MAX)
    call cdf_define(icdf,'ndep_set_beam',blk%ndep_set_beam)
    call cdf_define(icdf,'ndep_set_ien',blk%ndep_set_ien)
    call cdf_define(icdf,'nbbcal',blk%nbbcal)
    call cdf_define(icdf,'ncx0',blk%ncx0)
    call cdf_define(icdf,'nper_cx',blk%nper_cx)
    call cdf_define(icdf,'LEV_NBIDEP',blk%LEV_NBIDEP)
    call cdf_define(icdf,'LEV_TTNUK',blk%LEV_TTNUK)
    call cdf_define(icdf,'LEVMOD_HALO',blk%LEVMOD_HALO)
    call cdf_define(icdf,'NSPLT',blk%NSPLT)
    call cdf_define(icdf,'NSPLT_GEO',blk%NSPLT_GEO)
    call cdf_define(icdf,'NSPLT_KIN',blk%NSPLT_KIN)
    call cdf_define(icdf,'NSPLT_GEN',blk%NSPLT_GEN)
    call cdf_define(icdf,'nmimp',blk%nmimp)
    call cdf_define(icdf,'NTEST_MAX',blk%NTEST_MAX)
    call cdf_define(icdf,'NGOOCON_VPVBIN',blk%NGOOCON_VPVBIN)
    call cdf_define(icdf,'NGOOCON_EBIN',blk%NGOOCON_EBIN)
    call cdf_define(icdf,'NGOOCON_RBIN',blk%NGOOCON_RBIN)
    call cdf_define(icdf,'nchdvp',blk%nchdvp)
    call cdf_define(icdf,'orbrzv_option',blk%orbrzv_option)
    call cdf_define(icdf,'orbrzv_rf_option',blk%orbrzv_rf_option)
    call cdf_define(icdf,'nlmcrf_on',blk%nlmcrf_on)
    call cdf_define(icdf,'orbrzv_nr',blk%orbrzv_nr)
    call cdf_define(icdf,'orbrzv_nz',blk%orbrzv_nz)
    call cdf_define(icdf,'nlminsv',blk%nlminsv)
    call cdf_define(icdf,'nlebei',blk%nlebei)
    call cdf_define(icdf,'nsigexc',blk%nsigexc)
    call cdf_define(icdf,'nlbbcx',blk%nlbbcx)
    call cdf_define(icdf,'nbbcx_avg',blk%nbbcx_avg)
    call cdf_define(icdf,'nbbcx_bb',blk%nbbcx_bb)
    call cdf_define(icdf,'nbfallgr',blk%nbfallgr)
    call cdf_define(icdf,'nlbeamcx',blk%nlbeamcx)
    call cdf_define(icdf,'nlhvion',blk%nlhvion)
    call cdf_define(icdf,'nlbcde',blk%nlbcde)
    call cdf_define(icdf,'nlbcoh',blk%nlbcoh)
    call cdf_define(icdf,'nlbcpa',blk%nlbcpa)
    call cdf_define(icdf,'nlorbo',blk%nlorbo)
    call cdf_define(icdf,'nlbflr',blk%nlbflr)
    call cdf_define(icdf,'nlfbmflr',blk%nlfbmflr)
    call cdf_define(icdf,'nlbgflr',blk%nlbgflr)
    call cdf_define(icdf,'sawflag',blk%sawflag)
    call cdf_define(icdf,'nlsawb',blk%nlsawb)
    call cdf_define(icdf,'nlsawf',blk%nlsawf)
    call cdf_define(icdf,'nmix_kdsaw',blk%nmix_kdsaw)
    call cdf_define(icdf,'nkaep',blk%nkaep)
    call cdf_define(icdf,'ngradd_opt',blk%ngradd_opt)
    call cdf_define(icdf,'nrip',blk%nrip)
    call cdf_define(icdf,'nerngfi',blk%nerngfi)
    call cdf_define(icdf,'nsdbgb',blk%nsdbgb)
    call cdf_define(icdf,'nlfbon',blk%nlfbon)
    call cdf_define(icdf,'nfbon_vpvopt',blk%nfbon_vpvopt)
    call cdf_define(icdf,'nfbon_species',blk%nfbon_species)
    call cdf_define(icdf,'nbbox',blk%nbbox)
    call cdf_define(icdf,'nbsbox',blk%nbsbox)
    call cdf_define(icdf,'nsample_box',blk%nsample_box)
    call cdf_define(icdf,'nbebox',blk%nbebox)
    call cdf_define(icdf,'nxbox',blk%nxbox)
    call cdf_define(icdf,'nybox',blk%nybox)
    call cdf_define(icdf,'nlbox',blk%nlbox)
    call cdf_define(icdf,'ndepbox',blk%ndepbox)
    call cdf_define(icdf,'nbtube',blk%nbtube)
    call cdf_define(icdf,'nbstube',blk%nbstube)
    call cdf_define(icdf,'nbetube',blk%nbetube)
    call cdf_define(icdf,'LMIDtube',blk%LMIDtube)
    call cdf_define(icdf,'NSEGtube',blk%NSEGtube)
    call cdf_define(icdf,'NDEPtube',blk%NDEPtube)
    call cdf_define(icdf,'nlcprb',blk%nlcprb)
    call cdf_define(icdf,'nlpsirz',blk%nlpsirz)
    call cdf_define(icdf,'nmcurb',blk%nmcurb)
    call cdf_define(icdf,'nlfdep',blk%nlfdep)
 
    call cdf_write(icdf,'mission',saved_mission)
    call cdf_write(icdf,'workpath',saved_workpath)
 
    call cdf_write(icdf,'xbmbnd',blk%xbmbnd)
    call cdf_write(icdf,'orbrzv_toric_Prftot0',blk%orbrzv_toric_Prftot0)
    call cdf_write(icdf,'orbrzv_toric_frnm',blk%orbrzv_toric_frnm)
    call cdf_write(icdf,'xdatsf3',blk%xdatsf3)
    call cdf_write(icdf,'xdatsfa',blk%xdatsfa)
    call cdf_write(icdf,'xdatsft',blk%xdatsft)
    call cdf_write(icdf,'xdatsfp',blk%xdatsfp)
    call cdf_write(icdf,'xdatsp3',blk%xdatsp3)
    call cdf_write(icdf,'plfhe3',blk%plfhe3)
    call cdf_write(icdf,'plfhe4',blk%plfhe4)
    call cdf_write(icdf,'plfst',blk%plfst)
    call cdf_write(icdf,'plfsp',blk%plfsp)
    call cdf_write(icdf,'plfsp14',blk%plfsp14)
    call cdf_write(icdf,'cxsplt',blk%cxsplt)
    call cdf_write(icdf,'dxbsmoo',blk%dxbsmoo)
    call cdf_write(icdf,'dxbsm_nc',blk%dxbsm_nc)
    call cdf_write(icdf,'xpand_nptcl',blk%xpand_nptcl)
    call cdf_write(icdf,'frac_depmax',blk%frac_depmax)
    call cdf_write(icdf,'frac_dep_lim',blk%frac_dep_lim)
    call cdf_write(icdf,'frac_depmin',blk%frac_depmin)
    call cdf_write(icdf,'frac_orbrr',blk%frac_orbrr)
    call cdf_write(icdf,'AIMP',blk%AIMP)
    call cdf_write(icdf,'XZIMP',blk%XZIMP)
    call cdf_write(icdf,'BI_MAX_PL',blk%BI_MAX_PL)
    call cdf_write(icdf,'BI_MAX_PR',blk%BI_MAX_PR)
    call cdf_write(icdf,'wghta',blk%wghta)
    call cdf_write(icdf,'goocon',blk%goocon)
    call cdf_write(icdf,'cxpcon',blk%cxpcon)
    call cdf_write(icdf,'fppcon',blk%fppcon)
    call cdf_write(icdf,'fdtnxy',blk%fdtnxy)
    call cdf_write(icdf,'dtn',blk%dtn)
    call cdf_write(icdf,'dt_acc',blk%dt_acc)
    call cdf_write(icdf,'orbrzv_zzerr_con',blk%orbrzv_zzerr_con)
    call cdf_write(icdf,'xdepmod',blk%xdepmod)
    call cdf_write(icdf,'xcfanbi',blk%xcfanbi)
    call cdf_write(icdf,'xdfanbi',blk%xdfanbi)
    call cdf_write(icdf,'xefanbi',blk%xefanbi)
    call cdf_write(icdf,'xcfafus',blk%xcfafus)
    call cdf_write(icdf,'xdfafus',blk%xdfafus)
    call cdf_write(icdf,'xefafus',blk%xefafus)
    call cdf_write(icdf,'gflr_min',blk%gflr_min)
    call cdf_write(icdf,'gflr_rl',blk%gflr_rl)
    call cdf_write(icdf,'gflr_ll',blk%gflr_ll)
    call cdf_write(icdf,'gflr_xv',blk%gflr_xv)
    call cdf_write(icdf,'gflr_op',blk%gflr_op)
    call cdf_write(icdf,'xswfrac_allfast',blk%xswfrac_allfast)
    call cdf_write(icdf,'xswfrac_beam',blk%xswfrac_beam)
    call cdf_write(icdf,'xswfrac_fusn',blk%xswfrac_fusn)
    call cdf_write(icdf,'fporcelli',blk%fporcelli)
    call cdf_write(icdf,'AEP_ASCALE',blk%AEP_ASCALE)
    call cdf_write(icdf,'AEP_USCALE',blk%AEP_USCALE)
    call cdf_write(icdf,'TAEP_FAC',blk%TAEP_FAC)
    call cdf_write(icdf,'taurip',blk%taurip)
    call cdf_write(icdf,'asrd',blk%asrd)
    call cdf_write(icdf,'bsrd',blk%bsrd)
    call cdf_write(icdf,'erngfi',blk%erngfi)
    call cdf_write(icdf,'fbemin',blk%fbemin)
    call cdf_write(icdf,'fbemax',blk%fbemax)
    call cdf_write(icdf,'fvpvmn',blk%fvpvmn)
    call cdf_write(icdf,'fvpvmx',blk%fvpvmx)
    call cdf_write(icdf,'fbltim',blk%fbltim)
    call cdf_write(icdf,'fshper',blk%fshper)
    call cdf_write(icdf,'fshwid',blk%fshwid)
    call cdf_write(icdf,'tfshon',blk%tfshon)
    call cdf_write(icdf,'tfshof',blk%tfshof)
    call cdf_write(icdf,'xfishmin',blk%xfishmin)
    call cdf_write(icdf,'xfishmax',blk%xfishmax)
    call cdf_write(icdf,'fbtrap_depth',blk%fbtrap_depth)
    call cdf_write(icdf,'xninja_box',blk%xninja_box)
    call cdf_write(icdf,'xninjs_box',blk%xninjs_box)
    call cdf_write(icdf,'xninj_box',blk%xninj_box)
    call cdf_write(icdf,'xboxhw',blk%xboxhw)
    call cdf_write(icdf,'yboxhw',blk%yboxhw)
    call cdf_write(icdf,'xlbox1',blk%xlbox1)
    call cdf_write(icdf,'xlbox2',blk%xlbox2)
    call cdf_write(icdf,'Rtube',blk%Rtube)
    call cdf_write(icdf,'Ytube',blk%Ytube)
    call cdf_write(icdf,'XZETAtube',blk%XZETAtube)
    call cdf_write(icdf,'PHItube',blk%PHItube)
    call cdf_write(icdf,'THETAtube',blk%THETAtube)
    call cdf_write(icdf,'XL1tube',blk%XL1tube)
    call cdf_write(icdf,'XL2tube',blk%XL2tube)
    call cdf_write(icdf,'RHOtube',blk%RHOtube)
    call cdf_write(icdf,'edbfac',blk%edbfac)
 
    call cdf_write(icdf,'nonlin',blk%nonlin)
    call cdf_write(icdf,'nseed',blk%nseed)
    call cdf_write(icdf,'nlbout',blk%nlbout)
    call cdf_write(icdf,'lunnbx',blk%lunnbx)
    call cdf_write(icdf,'lunres',blk%lunres)
    call cdf_write(icdf,'mrstrt',blk%mrstrt)
    call cdf_write(icdf,'nclass',blk%nclass)
    call cdf_write(icdf,'only_io',blk%only_io)
    call cdf_write(icdf,'ref_namelist',blk%ref_namelist)
    call cdf_write(icdf,'quasi_check',blk%quasi_check)
    call cdf_write(icdf,'nltest_output',blk%nltest_output)
    call cdf_write(icdf,'BLK_MPI_BCAST_INT',blk%BLK_MPI_BCAST_INT)
    call cdf_write(icdf,'BLK_MPI_BCAST_R8',blk%BLK_MPI_BCAST_R8)
    call cdf_write(icdf,'nzones',blk%nzones)
    call cdf_write(icdf,'nzone_fb',blk%nzone_fb)
    call cdf_write(icdf,'nlsym2b',blk%nlsym2b)
    call cdf_write(icdf,'nth0',blk%nth0)
    call cdf_write(icdf,'xplasma_in_memory',blk%xplasma_in_memory)
    call cdf_write(icdf,'nznbma',blk%nznbma)
    call cdf_write(icdf,'nznbme',blk%nznbme)
    call cdf_write(icdf,'nznbma0',blk%nznbma0)
    call cdf_write(icdf,'nznbme0',blk%nznbme0)
    call cdf_write(icdf,'ngyro',blk%ngyro)
    call cdf_write(icdf,'nlusf3',blk%nlusf3)
    call cdf_write(icdf,'nlusfa',blk%nlusfa)
    call cdf_write(icdf,'nlusft',blk%nlusft)
    call cdf_write(icdf,'nlusfp',blk%nlusfp)
    call cdf_write(icdf,'nlusfp14',blk%nlusfp14)
    call cdf_write(icdf,'nlfatom',blk%nlfatom)
    call cdf_write(icdf,'nlbfpp',blk%nlbfpp)
    call cdf_write(icdf,'nptcls',blk%nptcls)
    call cdf_write(icdf,'nptclf',blk%nptclf)
    call cdf_write(icdf,'nptclh',blk%nptclh)
    call cdf_write(icdf,'NFAST_MAP',blk%NFAST_MAP)
    call cdf_write(icdf,'nptcl_max',blk%nptcl_max)
    call cdf_write(icdf,'nltrk_dep0',blk%nltrk_dep0)
    call cdf_write(icdf,'NLDEP0_GATHER',blk%NLDEP0_GATHER)
    call cdf_write(icdf,'ndep0',blk%ndep0)
    call cdf_write(icdf,'NDEP0_MAX',blk%NDEP0_MAX)
    call cdf_write(icdf,'ndep_set_beam',blk%ndep_set_beam)
    call cdf_write(icdf,'ndep_set_ien',blk%ndep_set_ien)
    call cdf_write(icdf,'nbbcal',blk%nbbcal)
    call cdf_write(icdf,'ncx0',blk%ncx0)
    call cdf_write(icdf,'nper_cx',blk%nper_cx)
    call cdf_write(icdf,'LEV_NBIDEP',blk%LEV_NBIDEP)
    call cdf_write(icdf,'LEV_TTNUK',blk%LEV_TTNUK)
    call cdf_write(icdf,'LEVMOD_HALO',blk%LEVMOD_HALO)
    call cdf_write(icdf,'NSPLT',blk%NSPLT)
    call cdf_write(icdf,'NSPLT_GEO',blk%NSPLT_GEO)
    call cdf_write(icdf,'NSPLT_KIN',blk%NSPLT_KIN)
    call cdf_write(icdf,'NSPLT_GEN',blk%NSPLT_GEN)
    call cdf_write(icdf,'nmimp',blk%nmimp)
    call cdf_write(icdf,'NTEST_MAX',blk%NTEST_MAX)
    call cdf_write(icdf,'NGOOCON_VPVBIN',blk%NGOOCON_VPVBIN)
    call cdf_write(icdf,'NGOOCON_EBIN',blk%NGOOCON_EBIN)
    call cdf_write(icdf,'NGOOCON_RBIN',blk%NGOOCON_RBIN)
    call cdf_write(icdf,'nchdvp',blk%nchdvp)
    call cdf_write(icdf,'orbrzv_option',blk%orbrzv_option)
    call cdf_write(icdf,'orbrzv_rf_option',blk%orbrzv_rf_option)
    call cdf_write(icdf,'nlmcrf_on',blk%nlmcrf_on)
    call cdf_write(icdf,'orbrzv_nr',blk%orbrzv_nr)
    call cdf_write(icdf,'orbrzv_nz',blk%orbrzv_nz)
    call cdf_write(icdf,'nlminsv',blk%nlminsv)
    call cdf_write(icdf,'nlebei',blk%nlebei)
    call cdf_write(icdf,'nsigexc',blk%nsigexc)
    call cdf_write(icdf,'nlbbcx',blk%nlbbcx)
    call cdf_write(icdf,'nbbcx_avg',blk%nbbcx_avg)
    call cdf_write(icdf,'nbbcx_bb',blk%nbbcx_bb)
    call cdf_write(icdf,'nbfallgr',blk%nbfallgr)
    call cdf_write(icdf,'nlbeamcx',blk%nlbeamcx)
    call cdf_write(icdf,'nlhvion',blk%nlhvion)
    call cdf_write(icdf,'nlbcde',blk%nlbcde)
    call cdf_write(icdf,'nlbcoh',blk%nlbcoh)
    call cdf_write(icdf,'nlbcpa',blk%nlbcpa)
    call cdf_write(icdf,'nlorbo',blk%nlorbo)
    call cdf_write(icdf,'nlbflr',blk%nlbflr)
    call cdf_write(icdf,'nlfbmflr',blk%nlfbmflr)
    call cdf_write(icdf,'nlbgflr',blk%nlbgflr)
    call cdf_write(icdf,'sawflag',blk%sawflag)
    call cdf_write(icdf,'nlsawb',blk%nlsawb)
    call cdf_write(icdf,'nlsawf',blk%nlsawf)
    call cdf_write(icdf,'nmix_kdsaw',blk%nmix_kdsaw)
    call cdf_write(icdf,'nkaep',blk%nkaep)
    call cdf_write(icdf,'ngradd_opt',blk%ngradd_opt)
    call cdf_write(icdf,'nrip',blk%nrip)
    call cdf_write(icdf,'nerngfi',blk%nerngfi)
    call cdf_write(icdf,'nsdbgb',blk%nsdbgb)
    call cdf_write(icdf,'nlfbon',blk%nlfbon)
    call cdf_write(icdf,'nfbon_vpvopt',blk%nfbon_vpvopt)
    call cdf_write(icdf,'nfbon_species',blk%nfbon_species)
    call cdf_write(icdf,'nbbox',blk%nbbox)
    call cdf_write(icdf,'nbsbox',blk%nbsbox)
    call cdf_write(icdf,'nsample_box',blk%nsample_box)
    call cdf_write(icdf,'nbebox',blk%nbebox)
    call cdf_write(icdf,'nxbox',blk%nxbox)
    call cdf_write(icdf,'nybox',blk%nybox)
    call cdf_write(icdf,'nlbox',blk%nlbox)
    call cdf_write(icdf,'ndepbox',blk%ndepbox)
    call cdf_write(icdf,'nbtube',blk%nbtube)
    call cdf_write(icdf,'nbstube',blk%nbstube)
    call cdf_write(icdf,'nbetube',blk%nbetube)
    call cdf_write(icdf,'LMIDtube',blk%LMIDtube)
    call cdf_write(icdf,'NSEGtube',blk%NSEGtube)
    call cdf_write(icdf,'NDEPtube',blk%NDEPtube)
    call cdf_write(icdf,'nlcprb',blk%nlcprb)
    call cdf_write(icdf,'nlpsirz',blk%nlpsirz)
    call cdf_write(icdf,'nmcurb',blk%nmcurb)
    call cdf_write(icdf,'nlfdep',blk%nlfdep)


    call ezcdf_close(icdf)

  end subroutine nbi_ctrl_init_cdfwrite

  subroutine nbi_ctrl_update_cdfwrite(ofile,blk,ierr)
    ! write nbi_ctrl_update block in ezcdf file

    character*(*) :: ofile            ! output file
    type (nbi_ctrl_update) :: blk    ! written to file
    integer, intent(out) :: ierr      ! status, 0=OK

    !---------------------------
    ! local:
    integer :: icdf
    !---------------------------

    call ezcdf_open(icdf,ofile,'w',ierr)
    if(ierr.ne.0) then
       call nbi_warning( &
            ' ?nbi_ctrl_update_cdfwrite: ezcdf open failure, file: '// &
            trim(ofile))
       return
    endif

 
    call cdf_define(icdf,'orbrzv_toric_Prftot0',blk%orbrzv_toric_Prftot0)
    call cdf_define(icdf,'orbrzv_toric_frnm',blk%orbrzv_toric_frnm)
    call cdf_define(icdf,'xdatsf3',blk%xdatsf3)
    call cdf_define(icdf,'xdatsfa',blk%xdatsfa)
    call cdf_define(icdf,'xdatsft',blk%xdatsft)
    call cdf_define(icdf,'xdatsfp',blk%xdatsfp)
    call cdf_define(icdf,'xdatsp3',blk%xdatsp3)
    call cdf_define(icdf,'xpand_nptcl',blk%xpand_nptcl)
    call cdf_define(icdf,'frac_dep_lim',blk%frac_dep_lim)
    call cdf_define(icdf,'AIMP',blk%AIMP)
    call cdf_define(icdf,'XZIMP',blk%XZIMP)
    call cdf_define(icdf,'BI_MAX_PL',blk%BI_MAX_PL)
    call cdf_define(icdf,'BI_MAX_PR',blk%BI_MAX_PR)
    call cdf_define(icdf,'wghta',blk%wghta)
    call cdf_define(icdf,'AEP_ASCALE',blk%AEP_ASCALE)
    call cdf_define(icdf,'AEP_USCALE',blk%AEP_USCALE)
    call cdf_define(icdf,'TAEP_FAC',blk%TAEP_FAC)
    call cdf_define(icdf,'taurip',blk%taurip)
    call cdf_define(icdf,'edbfac',blk%edbfac)
 
    call cdf_define(icdf,'nlbout',blk%nlbout)
    call cdf_define(icdf,'nltest_output',blk%nltest_output)
    call cdf_define(icdf,'xplasma_in_memory',blk%xplasma_in_memory)
    call cdf_define(icdf,'ngyro',blk%ngyro)
    call cdf_define(icdf,'nlusf3',blk%nlusf3)
    call cdf_define(icdf,'nlusfa',blk%nlusfa)
    call cdf_define(icdf,'nlusft',blk%nlusft)
    call cdf_define(icdf,'nlusfp',blk%nlusfp)
    call cdf_define(icdf,'nlusfp14',blk%nlusfp14)
    call cdf_define(icdf,'nptcls',blk%nptcls)
    call cdf_define(icdf,'nptclf',blk%nptclf)
    call cdf_define(icdf,'nptclh',blk%nptclh)
    call cdf_define(icdf,'NFAST_MAP',blk%NFAST_MAP)
    call cdf_define(icdf,'nltrk_dep0',blk%nltrk_dep0)
    call cdf_define(icdf,'NLDEP0_GATHER',blk%NLDEP0_GATHER)
    call cdf_define(icdf,'ndep0',blk%ndep0)
    call cdf_define(icdf,'NDEP0_MAX',blk%NDEP0_MAX)
    call cdf_define(icdf,'ndep_set_beam',blk%ndep_set_beam)
    call cdf_define(icdf,'ndep_set_ien',blk%ndep_set_ien)
    call cdf_define(icdf,'ncx0',blk%ncx0)
    call cdf_define(icdf,'nper_cx',blk%nper_cx)
    call cdf_define(icdf,'LEV_NBIDEP',blk%LEV_NBIDEP)
    call cdf_define(icdf,'LEV_TTNUK',blk%LEV_TTNUK)
    call cdf_define(icdf,'LEVMOD_HALO',blk%LEVMOD_HALO)
    call cdf_define(icdf,'NSPLT',blk%NSPLT)
    call cdf_define(icdf,'NSPLT_GEO',blk%NSPLT_GEO)
    call cdf_define(icdf,'NSPLT_KIN',blk%NSPLT_KIN)
    call cdf_define(icdf,'NSPLT_GEN',blk%NSPLT_GEN)
    call cdf_define(icdf,'nmimp',blk%nmimp)
    call cdf_define(icdf,'NTEST_MAX',blk%NTEST_MAX)
    call cdf_define(icdf,'orbrzv_rf_option',blk%orbrzv_rf_option)
    call cdf_define(icdf,'nlmcrf_on',blk%nlmcrf_on)
    call cdf_define(icdf,'nsigexc',blk%nsigexc)
    call cdf_define(icdf,'nlbbcx',blk%nlbbcx)
    call cdf_define(icdf,'nbbcx_avg',blk%nbbcx_avg)
    call cdf_define(icdf,'nbbcx_bb',blk%nbbcx_bb)
    call cdf_define(icdf,'nlorbo',blk%nlorbo)
    call cdf_define(icdf,'nlbgflr',blk%nlbgflr)
    call cdf_define(icdf,'sawflag',blk%sawflag)
    call cdf_define(icdf,'nlsawb',blk%nlsawb)
    call cdf_define(icdf,'nlsawf',blk%nlsawf)
    call cdf_define(icdf,'nkaep',blk%nkaep)
    call cdf_define(icdf,'nlfbon',blk%nlfbon)
 
    call cdf_write(icdf,'orbrzv_toric_Prftot0',blk%orbrzv_toric_Prftot0)
    call cdf_write(icdf,'orbrzv_toric_frnm',blk%orbrzv_toric_frnm)
    call cdf_write(icdf,'xdatsf3',blk%xdatsf3)
    call cdf_write(icdf,'xdatsfa',blk%xdatsfa)
    call cdf_write(icdf,'xdatsft',blk%xdatsft)
    call cdf_write(icdf,'xdatsfp',blk%xdatsfp)
    call cdf_write(icdf,'xdatsp3',blk%xdatsp3)
    call cdf_write(icdf,'xpand_nptcl',blk%xpand_nptcl)
    call cdf_write(icdf,'frac_dep_lim',blk%frac_dep_lim)
    call cdf_write(icdf,'AIMP',blk%AIMP)
    call cdf_write(icdf,'XZIMP',blk%XZIMP)
    call cdf_write(icdf,'BI_MAX_PL',blk%BI_MAX_PL)
    call cdf_write(icdf,'BI_MAX_PR',blk%BI_MAX_PR)
    call cdf_write(icdf,'wghta',blk%wghta)
    call cdf_write(icdf,'AEP_ASCALE',blk%AEP_ASCALE)
    call cdf_write(icdf,'AEP_USCALE',blk%AEP_USCALE)
    call cdf_write(icdf,'TAEP_FAC',blk%TAEP_FAC)
    call cdf_write(icdf,'taurip',blk%taurip)
    call cdf_write(icdf,'edbfac',blk%edbfac)
 
    call cdf_write(icdf,'nlbout',blk%nlbout)
    call cdf_write(icdf,'nltest_output',blk%nltest_output)
    call cdf_write(icdf,'xplasma_in_memory',blk%xplasma_in_memory)
    call cdf_write(icdf,'ngyro',blk%ngyro)
    call cdf_write(icdf,'nlusf3',blk%nlusf3)
    call cdf_write(icdf,'nlusfa',blk%nlusfa)
    call cdf_write(icdf,'nlusft',blk%nlusft)
    call cdf_write(icdf,'nlusfp',blk%nlusfp)
    call cdf_write(icdf,'nlusfp14',blk%nlusfp14)
    call cdf_write(icdf,'nptcls',blk%nptcls)
    call cdf_write(icdf,'nptclf',blk%nptclf)
    call cdf_write(icdf,'nptclh',blk%nptclh)
    call cdf_write(icdf,'NFAST_MAP',blk%NFAST_MAP)
    call cdf_write(icdf,'nltrk_dep0',blk%nltrk_dep0)
    call cdf_write(icdf,'NLDEP0_GATHER',blk%NLDEP0_GATHER)
    call cdf_write(icdf,'ndep0',blk%ndep0)
    call cdf_write(icdf,'NDEP0_MAX',blk%NDEP0_MAX)
    call cdf_write(icdf,'ndep_set_beam',blk%ndep_set_beam)
    call cdf_write(icdf,'ndep_set_ien',blk%ndep_set_ien)
    call cdf_write(icdf,'ncx0',blk%ncx0)
    call cdf_write(icdf,'nper_cx',blk%nper_cx)
    call cdf_write(icdf,'LEV_NBIDEP',blk%LEV_NBIDEP)
    call cdf_write(icdf,'LEV_TTNUK',blk%LEV_TTNUK)
    call cdf_write(icdf,'LEVMOD_HALO',blk%LEVMOD_HALO)
    call cdf_write(icdf,'NSPLT',blk%NSPLT)
    call cdf_write(icdf,'NSPLT_GEO',blk%NSPLT_GEO)
    call cdf_write(icdf,'NSPLT_KIN',blk%NSPLT_KIN)
    call cdf_write(icdf,'NSPLT_GEN',blk%NSPLT_GEN)
    call cdf_write(icdf,'nmimp',blk%nmimp)
    call cdf_write(icdf,'NTEST_MAX',blk%NTEST_MAX)
    call cdf_write(icdf,'orbrzv_rf_option',blk%orbrzv_rf_option)
    call cdf_write(icdf,'nlmcrf_on',blk%nlmcrf_on)
    call cdf_write(icdf,'nsigexc',blk%nsigexc)
    call cdf_write(icdf,'nlbbcx',blk%nlbbcx)
    call cdf_write(icdf,'nbbcx_avg',blk%nbbcx_avg)
    call cdf_write(icdf,'nbbcx_bb',blk%nbbcx_bb)
    call cdf_write(icdf,'nlorbo',blk%nlorbo)
    call cdf_write(icdf,'nlbgflr',blk%nlbgflr)
    call cdf_write(icdf,'sawflag',blk%sawflag)
    call cdf_write(icdf,'nlsawb',blk%nlsawb)
    call cdf_write(icdf,'nlsawf',blk%nlsawf)
    call cdf_write(icdf,'nkaep',blk%nkaep)
    call cdf_write(icdf,'nlfbon',blk%nlfbon)


    call ezcdf_close(icdf)

  end subroutine nbi_ctrl_update_cdfwrite

  subroutine nbi_ctrl_init_write(ofile,blk,ierr)

    ! write NON-DEFAULT values in the nbi_ctrl_init block
    ! to a commented namelist file

    character*(*) :: ofile            ! output file
    type (nbi_ctrl_init) :: blk    ! written to file
    integer, intent(out) :: ierr      ! status, 0=OK

    !---------------------------
    ! local:
    integer :: ilun_dbg = 0
    integer :: ilun_tmp,istat,idiff
    !---------------------------
    ! local copies of elements for default values:
 
    CHARACTER*120 :: mission
    CHARACTER*120 :: workpath
 
    REAL(fp) :: xbmbnd
    real(fp) :: orbrzv_toric_Prftot0(15)  ! (1:mibs)
    REAL(fp) :: orbrzv_toric_frnm
    REAL(fp) :: xdatsf3
    REAL(fp) :: xdatsfa
    REAL(fp) :: xdatsft
    REAL(fp) :: xdatsfp
    REAL(fp) :: xdatsp3
    REAL(fp) :: plfhe3
    REAL(fp) :: plfhe4
    REAL(fp) :: plfst
    REAL(fp) :: plfsp
    REAL(fp) :: plfsp14
    REAL(fp) :: cxsplt
    REAL(fp) :: dxbsmoo
    REAL(fp) :: dxbsm_nc
    REAL(fp) :: xpand_nptcl
    REAL(fp) :: frac_depmax
    REAL(fp) :: frac_dep_lim
    REAL(fp) :: frac_depmin
    REAL(fp) :: frac_orbrr
    REAL(fp) :: AIMP
    REAL(fp) :: XZIMP
    REAL(fp) :: BI_MAX_PL
    REAL(fp) :: BI_MAX_PR
    REAL(fp) :: wghta
    REAL(fp) :: goocon
    REAL(fp) :: cxpcon
    REAL(fp) :: fppcon
    REAL(fp) :: fdtnxy
    REAL(fp) :: dtn
    REAL(fp) :: dt_acc
    REAL(fp) :: orbrzv_zzerr_con
    REAL(fp) :: xdepmod
    REAL(fp) :: xcfanbi
    REAL(fp) :: xdfanbi
    REAL(fp) :: xefanbi
    REAL(fp) :: xcfafus
    REAL(fp) :: xdfafus
    REAL(fp) :: xefafus
    REAL(fp) :: gflr_min
    REAL(fp) :: gflr_rl
    REAL(fp) :: gflr_ll
    real(fp) :: gflr_xv(2)  ! (1:2)
    real(fp) :: gflr_op(15)  ! (1:15)
    REAL(fp) :: xswfrac_allfast
    REAL(fp) :: xswfrac_beam
    REAL(fp) :: xswfrac_fusn
    REAL(fp) :: fporcelli
    real(fp) :: AEP_ASCALE(10)  ! (1:mnkaep)
    REAL(fp) :: AEP_USCALE
    real(fp) :: TAEP_FAC(10)  ! (1:mnkaep)
    REAL(fp) :: taurip
    REAL(fp) :: asrd
    REAL(fp) :: bsrd
    real(fp) :: erngfi(10,15)  ! (1:nerngfi,1:mibs)
    REAL(fp) :: fbemin
    REAL(fp) :: fbemax
    REAL(fp) :: fvpvmn
    REAL(fp) :: fvpvmx
    REAL(fp) :: fbltim
    REAL(fp) :: fshper
    REAL(fp) :: fshwid
    REAL(fp) :: tfshon
    REAL(fp) :: tfshof
    REAL(fp) :: xfishmin
    REAL(fp) :: xfishmax
    REAL(fp) :: fbtrap_depth
    real(fp) :: xninja_box(100)  ! (1:nbbox)
    real(fp) :: xninjs_box(100)  ! (1:nbbox)
    REAL(fp) :: xninj_box
    REAL(fp) :: xboxhw
    REAL(fp) :: yboxhw
    REAL(fp) :: xlbox1
    REAL(fp) :: xlbox2
    real(fp) :: Rtube(200)  ! (1:nbtube)
    real(fp) :: Ytube(200)  ! (1:nbtube)
    real(fp) :: XZETAtube(200)  ! (1:nbtube)
    real(fp) :: PHItube(200)  ! (1:nbtube)
    real(fp) :: THETAtube(200)  ! (1:nbtube)
    real(fp) :: XL1tube(200)  ! (1:nbtube)
    real(fp) :: XL2tube(200)  ! (1:nbtube)
    REAL(fp) :: RHOtube
    REAL(fp) :: edbfac
 
    INTEGER :: nonlin
    INTEGER :: nseed
    INTEGER :: nlbout
    INTEGER :: lunnbx
    INTEGER :: lunres
    INTEGER :: mrstrt
    INTEGER :: nclass
    INTEGER :: only_io
    INTEGER :: ref_namelist
    INTEGER :: quasi_check
    INTEGER :: nltest_output
    INTEGER :: BLK_MPI_BCAST_INT
    INTEGER :: BLK_MPI_BCAST_R8
    INTEGER :: nzones
    INTEGER :: nzone_fb
    INTEGER :: nlsym2b
    INTEGER :: nth0
    INTEGER :: xplasma_in_memory
    INTEGER :: nznbma
    INTEGER :: nznbme
    INTEGER :: nznbma0
    INTEGER :: nznbme0
    INTEGER :: ngyro
    INTEGER :: nlusf3
    INTEGER :: nlusfa
    INTEGER :: nlusft
    INTEGER :: nlusfp
    INTEGER :: nlusfp14
    INTEGER :: nlfatom
    INTEGER :: nlbfpp
    INTEGER :: nptcls
    INTEGER :: nptclf
    INTEGER :: nptclh
    INTEGER :: NFAST_MAP
    INTEGER :: nptcl_max
    INTEGER :: nltrk_dep0
    INTEGER :: NLDEP0_GATHER
    INTEGER :: ndep0
    INTEGER :: NDEP0_MAX
    INTEGER :: ndep_set_beam
    INTEGER :: ndep_set_ien
    INTEGER :: nbbcal
    INTEGER :: ncx0
    INTEGER :: nper_cx
    INTEGER :: LEV_NBIDEP
    INTEGER :: LEV_TTNUK
    INTEGER :: LEVMOD_HALO
    INTEGER :: NSPLT
    INTEGER :: NSPLT_GEO
    INTEGER :: NSPLT_KIN
    INTEGER :: NSPLT_GEN
    INTEGER :: nmimp
    INTEGER :: NTEST_MAX
    INTEGER :: NGOOCON_VPVBIN
    INTEGER :: NGOOCON_EBIN
    INTEGER :: NGOOCON_RBIN
    INTEGER :: nchdvp
    INTEGER :: orbrzv_option
    INTEGER :: orbrzv_rf_option
    INTEGER :: nlmcrf_on
    INTEGER :: orbrzv_nr
    INTEGER :: orbrzv_nz
    INTEGER :: nlminsv
    INTEGER :: nlebei
    INTEGER :: nsigexc
    INTEGER :: nlbbcx
    INTEGER :: nbbcx_avg
    INTEGER :: nbbcx_bb
    INTEGER :: nbfallgr
    INTEGER :: nlbeamcx
    INTEGER :: nlhvion
    INTEGER :: nlbcde
    INTEGER :: nlbcoh
    INTEGER :: nlbcpa
    INTEGER :: nlorbo
    INTEGER :: nlbflr
    INTEGER :: nlfbmflr
    INTEGER :: nlbgflr
    INTEGER :: sawflag
    INTEGER :: nlsawb
    INTEGER :: nlsawf
    INTEGER :: nmix_kdsaw
    INTEGER :: nkaep
    INTEGER :: ngradd_opt
    INTEGER :: nrip
    INTEGER :: nerngfi
    INTEGER :: nsdbgb
    INTEGER :: nlfbon
    INTEGER :: nfbon_vpvopt
    INTEGER :: nfbon_species
    INTEGER :: nbbox
    integer :: nbsbox(100)  ! (1:nbbox)
    integer :: nsample_box(100)  ! (1:nbbox)
    integer :: nbebox(100)  ! (1:nbbox)
    INTEGER :: nxbox
    INTEGER :: nybox
    INTEGER :: nlbox
    INTEGER :: ndepbox
    INTEGER :: nbtube
    integer :: nbstube(200)  ! (1:nbtube)
    integer :: nbetube(200)  ! (1:nbtube)
    integer :: LMIDtube(200)  ! (1:nbtube)
    integer :: NSEGtube(200)  ! (1:nbtube)
    INTEGER :: NDEPtube
    INTEGER :: nlcprb
    INTEGER :: nlpsirz
    INTEGER :: nmcurb
    INTEGER :: nlfdep

    !---------------------------
    ierr = 0

    call find_io_unit(ilun_tmp)
    open(unit=ilun_tmp,file=trim(ofile),status='unknown')

    write(ilun_tmp,*) ' !! nbi_ctrl_init_write -- this file contains'
    write(ilun_tmp,*) ' !!   non-default values of initialization inputs'
    write(ilun_tmp,*) ' !!   to NUBEAM, in namelist format.'
    write(ilun_tmp,*) ' !!      LOGICAL variables mapped to INTEGER: 1=TRUE; 0=FALSE'
    write(ilun_tmp,*) ' '
    write(ilun_tmp,*) '&NBI_INIT'
    write(ilun_tmp,*) ' '

    ! set default values...

    mission =  "standard"
    workpath =  " "
 
    xbmbnd = 1.3
    orbrzv_toric_Prftot0 = 0.0_fp
    orbrzv_toric_frnm = 1.0_fp
    xdatsf3 = 0.0_fp
    xdatsfa = 0.0_fp
    xdatsft = 0.0_fp
    xdatsfp = 0.0_fp
    xdatsp3 = 0.0_fp
    plfhe3 = 100.0_fp
    plfhe4 = 10000.0_fp
    plfst = 100.0_fp
    plfsp = 100.0_fp
    plfsp14 = 100.0_fp
    cxsplt = 2.0_fp
    dxbsmoo = 0.05_fp
    dxbsm_nc = 0.0_fp
    xpand_nptcl = 3.0_fp
    frac_depmax = 0.25_fp
    frac_dep_lim = 1.5_fp
    frac_depmin = 0.01_fp
    frac_orbrr = 0.10_fp
    AIMP =    16._fp
    XZIMP =    8._fp
    BI_MAX_PL =  1._fp
    BI_MAX_PR =  1._fp
    wghta = 1.0_fp
    goocon = 10.0_fp
    cxpcon = 20.0_fp
    fppcon = 8.0_fp
    fdtnxy = 0.5_fp
    dtn = 0.0_fp
    dt_acc = 1.0e-4
    orbrzv_zzerr_con = 1.0e-4_fp
    xdepmod = 1.0_fp
    xcfanbi = 1.0_fp
    xdfanbi = 1.0_fp
    xefanbi = 1.0_fp
    xcfafus = 1.0_fp
    xdfafus = 1.0_fp
    xefafus = 1.0_fp
    gflr_min = 0.0_fp
    gflr_rl = 1.0e+6_fp
    gflr_ll = 30.0_fp
    gflr_xv =  (/1.5_fp, 0.4_fp/)
    gflr_op =  (/0._fp,1.e-4_fp,1.e-5_fp,0._fp,3._fp,10._fp,1._fp,0.2_fp,0.05_fp,1._fp,2._fp,0._fp,0._fp,0._fp,0._fp/)
    xswfrac_allfast = 1.0_fp
    xswfrac_beam = 1.0_fp
    xswfrac_fusn = 1.0_fp
    fporcelli = 1.0_fp
    AEP_ASCALE =  0.0_fp
    AEP_USCALE =  1.0_fp
    TAEP_FAC =  0.0_fp
    taurip = 0.0_fp
    asrd = 0.0_fp
    bsrd = 0.0_fp
    erngfi = 0.0_fp
    fbemin = 0.0_fp
    fbemax = 0.0_fp
    fvpvmn =  0.0_fp
    fvpvmx =  0.0_fp
    fbltim =  1.0e-7_fp
    fshper =  5.0e-3_fp
    fshwid =  1.0e-3_fp
    tfshon =  -1000.0_fp
    tfshof =  +1.0e+34_fp
    xfishmin =  0.0_fp
    xfishmax =  1.0_fp
    fbtrap_depth = 0.0_fp
    xninja_box = 0
    xninjs_box = 0
    xninj_box = 0
    xboxhw = 5.0_fp
    yboxhw = 5.0_fp
    xlbox1 = 100.0_fp
    xlbox2 = 500.0_fp
    Rtube = 0.
    Ytube = 0.
    XZETAtube = 0.
    PHItube = 0.
    THETAtube = 0.
    XL1tube = 0.
    XL2tube = 0.
    RHOtube =  1._fp
    edbfac = 10.0_fp
 
    nonlin = 6
    nseed = 1
    nlbout = inbi_logical(.TRUE.)
    lunnbx = 0
    lunres = 0
    mrstrt = 1
    nclass = 0
    only_io = inbi_logical(.FALSE.)
    ref_namelist = inbi_logical(.FALSE.)
    quasi_check = inbi_logical(.TRUE.)
    nltest_output = inbi_logical(.TRUE.)
    BLK_MPI_BCAST_INT =  2000000
    BLK_MPI_BCAST_R8 =   30000000
    nzones = 0
    nzone_fb = 0
    nlsym2b = inbi_logical( .FALSE.)
    nth0 =  2
    xplasma_in_memory = 0
    nznbma = 50
    nznbme = 100
    nznbma0 = 10
    nznbme0 = 50
    ngyro = 4
    nlusf3 = inbi_logical(.FALSE.)
    nlusfa = inbi_logical(.FALSE.)
    nlusft = inbi_logical(.FALSE.)
    nlusfp = inbi_logical(.FALSE.)
    nlusfp14 = inbi_logical(.FALSE.)
    nlfatom = inbi_logical(.TRUE.)
    nlbfpp = inbi_logical(.FALSE.)
    nptcls = 1000
    nptclf = 1000
    nptclh = 500
    NFAST_MAP = 1
    nptcl_max = 0
    nltrk_dep0 = inbi_logical(.FALSE.)
    NLDEP0_GATHER = inbi_logical( .FALSE.)
    ndep0 = 500
    NDEP0_MAX =    100000
    ndep_set_beam = 0
    ndep_set_ien = 0
    nbbcal = 1000000
    ncx0 = 100
    nper_cx = 4
    LEV_NBIDEP =  1
    LEV_TTNUK =  0
    LEVMOD_HALO =  0
    NSPLT =  100
    NSPLT_GEO =  1
    NSPLT_KIN =  1
    NSPLT_GEN =  1
    nmimp =  1
    NTEST_MAX =  0
    NGOOCON_VPVBIN =  0
    NGOOCON_EBIN =  1
    NGOOCON_RBIN =  1
    nchdvp = 3
    orbrzv_option = 0
    orbrzv_rf_option = 0
    nlmcrf_on = inbi_logical(.false.)
    orbrzv_nr = 160
    orbrzv_nz = 160
    nlminsv = inbi_logical(.TRUE.)
    nlebei = inbi_logical(.TRUE.)
    nsigexc = 0
    nlbbcx = inbi_logical(.TRUE.)
    nbbcx_avg = 0
    nbbcx_bb = 0
    nbfallgr = 0
    nlbeamcx = inbi_logical(.TRUE.)
    nlhvion = inbi_logical(.TRUE.)
    nlbcde = inbi_logical(.true.)
    nlbcoh = inbi_logical(.true.)
    nlbcpa = inbi_logical(.true.)
    nlorbo = inbi_logical(.false.)
    nlbflr = inbi_logical(.TRUE.)
    nlfbmflr = inbi_logical(.TRUE.)
    nlbgflr = inbi_logical(.FALSE.)
    sawflag = inbi_logical(.FALSE.)
    nlsawb = inbi_logical(.TRUE.)
    nlsawf = inbi_logical(.TRUE.)
    nmix_kdsaw = 1
    nkaep =  0
    ngradd_opt =  0
    nrip = 0
    nerngfi = 1
    nsdbgb = 1
    nlfbon = inbi_logical(.FALSE.)
    nfbon_vpvopt = 0
    nfbon_species = 3
    nbbox = 0
    nbsbox = 0
    nsample_box = 0
    nbebox = 1
    nxbox = 1
    nybox = 1
    nlbox = 100
    ndepbox = 500
    nbtube = 0
    nbstube = 0
    nbetube = 1
    LMIDtube = 0
    NSEGtube = 100
    NDEPtube = 500
    nlcprb = inbi_logical(.TRUE.)
    nlpsirz = inbi_logical(.FALSE.)
    nmcurb = 1
    nlfdep = inbi_logical(.FALSE.)
 
    call dwrite(ilun_tmp,'mission', "standard",saved_mission,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! NUBEAM "mission" in presence of Plasma State data'
       write(ilun_tmp,*) '     !'// &
            '!   (each supported mission requires special code; at present'
       write(ilun_tmp,*) '     !'// &
            '!   only "standard" is supported).'
       write(ilun_tmp,*) '     !'// &
            '! "standard" means:  assume responsibility for deposition'
       write(ilun_tmp,*) '     !'// &
            '!   and slowing down of neutral beam and fusion product'
       write(ilun_tmp,*) '     !'// &
            '!   fast ion species found in the Plasma State'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'workpath', " ",saved_workpath,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! work directory path -- for debug statefile...'
       write(ilun_tmp,*) '     !'// &
            '! blank means current working directory'
       write(ilun_tmp,*) ' '
    endif
 
    call dwrite(ilun_tmp,'xbmbnd',xbmbnd,blk%xbmbnd,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! boundary of xi-space'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'orbrzv_toric_Prftot0',orbrzv_toric_Prftot0,blk%orbrzv_toric_Prftot0,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! total power calculated by toric in the previous step'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'orbrzv_toric_frnm',orbrzv_toric_frnm,blk%orbrzv_toric_frnm,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! toric RF-field strength renormalizer'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xdatsf3',xdatsf3,blk%xdatsf3,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! source magnitude (n/sec) used to renormalize He3 fusion'
       write(ilun_tmp,*) '     !'// &
            '! product source profile from previous timestep'
       write(ilun_tmp,*) '     !'// &
            '! only applies if nlusf3.and.nlfhe3 both .TRUE.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xdatsfa',xdatsfa,blk%xdatsfa,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! source magnitude (n/sec) used to renormalize alpha fusion'
       write(ilun_tmp,*) '     !'// &
            '! product source profile from previous timestep'
       write(ilun_tmp,*) '     !'// &
            '! only applies if nlusfa.and.nlfhe4 both .TRUE.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xdatsft',xdatsft,blk%xdatsft,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! source magnitude (n/sec) used to renormalize triton fusion'
       write(ilun_tmp,*) '     !'// &
            '! product source profile from previous timestep'
       write(ilun_tmp,*) '     !'// &
            '! only applies if nlusft.and.nlfst both .TRUE.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xdatsfp',xdatsfp,blk%xdatsfp,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! source magnitude (n/sec) used to renormalize proton fusion'
       write(ilun_tmp,*) '     !'// &
            '! product source profile from previous timestep'
       write(ilun_tmp,*) '     !'// &
            '! only applies if nlusfp.and.nlfsp both .TRUE.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xdatsp3',xdatsp3,blk%xdatsp3,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! source magnitude (n/sec) used to renormalize proton fusion'
       write(ilun_tmp,*) '     !'// &
            '! product source profile from previous timestep'
       write(ilun_tmp,*) '     !'// &
            '! only applies if nlusfp.and.nlfsp both .TRUE.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'plfhe3',plfhe3,blk%plfhe3,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! (watts) threshhold power above which full Monte Carlo'
       write(ilun_tmp,*) '     !'// &
            '! statistics (NPTCLF particles) are assigned to model the'
       write(ilun_tmp,*) '     !'// &
            '! slowing down of the He3 fusion product ion population'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'plfhe4',plfhe4,blk%plfhe4,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! (watts) threshhold power above which full Monte Carlo'
       write(ilun_tmp,*) '     !'// &
            '! statistics (NPTCLF particles) are assigned to model the'
       write(ilun_tmp,*) '     !'// &
            '! slowing down of the He4 (alpha) fusion product ion population'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'plfst',plfst,blk%plfst,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! (watts) threshhold power above which full Monte Carlo'
       write(ilun_tmp,*) '     !'// &
            '! statistics (NPTCLF particles) are assigned to model the'
       write(ilun_tmp,*) '     !'// &
            '! slowing down of the triton fusion product ion population'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'plfsp',plfsp,blk%plfsp,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! (watts) threshhold power above which full Monte Carlo'
       write(ilun_tmp,*) '     !'// &
            '! statistics (NPTCLF particles) are assigned to model the'
       write(ilun_tmp,*) '     !'// &
            '! slowing down of the proton fusion product ion population'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'plfsp14',plfsp14,blk%plfsp14,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! (watts) threshhold power above which full Monte Carlo'
       write(ilun_tmp,*) '     !'// &
            '! statistics (NPTCLF particles) are assigned to model the'
       write(ilun_tmp,*) '     !'// &
            '! slowing down of the proton fusion product ion population'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'cxsplt',cxsplt,blk%cxsplt,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! charge exchange model: splitting factor, tracks per event.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'dxbsmoo',dxbsmoo,blk%dxbsmoo,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! smoothing parameter: half-width of triangular hat'
       write(ilun_tmp,*) '     !'// &
            '! function convolution smoothing, in x=sqrt(phi/philim)'
       write(ilun_tmp,*) '     !'// &
            '! applied to all output profiles'
       write(ilun_tmp,*) '     !'// &
            '! NOTE: if dxbsmoo.gt.0 a spline method will be applied'
       write(ilun_tmp,*) '     !'// &
            '!   to integrated profiles; if dxbsmoo.le.0 a piecewise'
       write(ilun_tmp,*) '     !'// &
            '!   linear method will be applied.  Thus:'
       write(ilun_tmp,*) '     !'// &
            '!      dxbsmoo .le. 0 ==> PBI is a step function'
       write(ilun_tmp,*) '     !'// &
            '!      dxbsmoo .gt. 0 ==> PBI is piecewise quadratic, C1.'
       write(ilun_tmp,*) '     !'// &
            '!   which follows because the integrated profile is'
       write(ilun_tmp,*) '     !'// &
            '!   differenced to get the local value.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'dxbsm_nc',dxbsm_nc,blk%dxbsm_nc,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! (Not currently active -- left in namelist for backwards'
       write(ilun_tmp,*) '     !'// &
            '! compatibility and possible later use).'
       write(ilun_tmp,*) '     !'// &
            '! original intent:'
       write(ilun_tmp,*) '     !'// &
            '! dxbsm_nc smoothing parameter (like dxbsmoo) for nclass'
       write(ilun_tmp,*) '     !'// &
            '! profile outputs only.  Actual smoothing for these'
       write(ilun_tmp,*) '     !'// &
            '! profiles will be based on max(abs(dxbsmoo),abs(dxbsm_nc));'
       write(ilun_tmp,*) '     !'// &
            '! spline or linear treatment still controlled by sign of'
       write(ilun_tmp,*) '     !'// &
            '! dxbsmoo; effect is optional extra smoothing for nclass'
       write(ilun_tmp,*) '     !'// &
            '! outputs. NO more active MG dec.09,2008'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xpand_nptcl',xpand_nptcl,blk%xpand_nptcl,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! If xpand_nptcl.gt.0.5 it will be a limit'
       write(ilun_tmp,*) '     !'// &
            '! of the total growth of the particle list'
       write(ilun_tmp,*) '     !'// &
            '! after this limit (> (1.0+xpand_nptcl)*NPTCLS )'
       write(ilun_tmp,*) '     !'// &
            '! revert to Russian Roulette.'
       write(ilun_tmp,*) '     !'// &
            '! For xpand_nptcl.le.0.5          it will be a fraction by which'
       write(ilun_tmp,*) '     !'// &
            '! nptcl{*} can expand during'
       write(ilun_tmp,*) '     !'// &
            '! deposition (see below) to avoid Russian Roulette (min val 0.05_fp)'
       write(ilun_tmp,*) '     !'// &
            '! (max val 0.5_fp).'
       write(ilun_tmp,*) '     !'// &
            '! (max(nptcls,nptclf,nptclh,nptcl_max)+1000)*1.1_fp'
       write(ilun_tmp,*) '     !'// &
            '! *(1+xpand_nptcl+frac_orbrr)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'frac_depmax',frac_depmax,blk%frac_depmax,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! max fraction of nptcl{*} # of MC ptcls that can be'
       write(ilun_tmp,*) '     !'// &
            '! deposited in a single step (min accepted val 0.1_fp)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'frac_dep_lim',frac_dep_lim,blk%frac_dep_lim,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! nptcl{*} # of MC ptcls that can be'
       write(ilun_tmp,*) '     !'// &
            '! deposited in a single step'
       write(ilun_tmp,*) '     !'// &
            '! is max(1,nptcls*min(1,delt/<tau_sl>*frac_dep_lim))'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'frac_depmin',frac_depmin,blk%frac_depmin,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! min fraction of nptcl{*}, min # of MC ptcls to'
       write(ilun_tmp,*) '     !'// &
            '! deposit in a single step (btw 0.0_fp and 0.1_fp)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'frac_orbrr',frac_orbrr,blk%frac_orbrr,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! fraction of population needing to be "underweight"'
       write(ilun_tmp,*) '     !'// &
            '! before application of Russian Roulette in orball routine'
       write(ilun_tmp,*) '     !'// &
            '! accepted values betwe 0.05_fp and 0.5_fp.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'AIMP',AIMP,blk%AIMP,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! ATOMIC WEIGHT OF IMPURITY, for tokamakium model only'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'XZIMP',XZIMP,blk%XZIMP,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! IMPURITY ATOMIC NUMBER, for tokamakium model only'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'BI_MAX_PL',BI_MAX_PL,blk%BI_MAX_PL,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! factor for an E_PLL component in bi-Maxwellian FI distribusion'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'BI_MAX_PR',BI_MAX_PR,blk%BI_MAX_PR,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! factor for an E_Perp component in bi-Maxwellian FI distribusion'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'wghta',wghta,blk%wghta,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! weight profile adjustment.  Larger values of WGHTA result'
       write(ilun_tmp,*) '     !'// &
            '! in more particles with less weight per particle in the core'
       write(ilun_tmp,*) '     !'// &
            '! and fewer particles with more weight per particle at the'
       write(ilun_tmp,*) '     !'// &
            '! edge.  (dimensionless).'
       write(ilun_tmp,*) '     !'// &
            '!   wghta.ge.1.0 enforced; wghta.le.nzones enforced.'
       write(ilun_tmp,*) '     !'// &
            '!!! Larger WGHTA -> better statistics at core, worse at edge'
       write(ilun_tmp,*) '     !'// &
            '!!! Smaller WGHTA -> better statistics at edge, worse at core'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'goocon',goocon,blk%goocon,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! "goose" control -- strong affect on runtime!'
       write(ilun_tmp,*) '     !'// &
            '! #bounces computed per min(tau(slow),tau(P.A.scatter),tau(cx)).'
       write(ilun_tmp,*) '     !'// &
            '! magnitude of constraint is quadratic for diffusive processes.'
       write(ilun_tmp,*) '     !'// &
            '! see also NGOOCON controls; if any of the NGOOCON controls have'
       write(ilun_tmp,*) '     !'// &
            '! greater than their default values, an upper limit of 1.0e-4 is'
       write(ilun_tmp,*) '     !'// &
            '! imposed on DT_ACC; otherwise upper limit of 1.0e-3 is allowed.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'cxpcon',cxpcon,blk%cxpcon,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! adjustment on frequency of calls to charge exchange'
       write(ilun_tmp,*) '     !'// &
            '! operator during slowing down.  Higher number --> more calls'
       write(ilun_tmp,*) '     !'// &
            '! default value generally OK.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'fppcon',fppcon,blk%fppcon,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! adjustment on frequency of calls to collision operator'
       write(ilun_tmp,*) '     !'// &
            '! per orbit half-bounce during slowing down.  Higher number'
       write(ilun_tmp,*) '     !'// &
            '! --> more calls; default value generally OK.'
       write(ilun_tmp,*) '     !'// &
            '! if nclass is set, the number is increased to at least'
       write(ilun_tmp,*) '     !'// &
            '! 20.0_fp'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'fdtnxy',fdtnxy,blk%fdtnxy,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! reduction factor for orbit timesteps near the axis'
       write(ilun_tmp,*) '     !'// &
            '! where a pseudo x-y space instead of a polar space is used'
       write(ilun_tmp,*) '     !'// &
            '! to avoid effects of the axial singularity.  Default OK.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'dtn',dtn,blk%dtn,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! ***orbit timestep adjustment parameter*** if left to zero'
       write(ilun_tmp,*) '     !'// &
            '! an automatic timestep will be selected and adjusted based'
       write(ilun_tmp,*) '     !'// &
            '! on orbit integration error analysis.  If set, constraints'
       write(ilun_tmp,*) '     !'// &
            '! will be applied, and the time step will still be adjusted'
       write(ilun_tmp,*) '     !'// &
            '! based on the error analysis.  General advice: leave at'
       write(ilun_tmp,*) '     !'// &
            '! zero.  (dtn, seconds).'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'dt_acc',dt_acc,blk%dt_acc,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! orbit accuracy criterion -- orbit timesteps adjusted so'
       write(ilun_tmp,*) '     !'// &
            '! that error delta(E)/Einj (and a similar measure for momentum)'
       write(ilun_tmp,*) '     !'// &
            '! is less than dt_acc per half bounce; another criterion'
       write(ilun_tmp,*) '     !'// &
            '! is that there be at least 10 steps per half bounce so'
       write(ilun_tmp,*) '     !'// &
            '! that goosing & timestep adjustment algorithms have'
       write(ilun_tmp,*) '     !'// &
            '! enough data to work.'
       write(ilun_tmp,*) '     !'// &
            '!    dt_acc<=0.0_fp => no adjustment (old constant dtn used).'
       write(ilun_tmp,*) '     !'// &
            '! if dt_acc > 0, 1.0e-6 <= dt_acc <= 1.0e-3 is enforced.'
       write(ilun_tmp,*) '     !'// &
            '! an upper limit of 1.0e-3 is enforced unless GOOCON controls'
       write(ilun_tmp,*) '     !'// &
            '! are increased from their default values in which case the'
       write(ilun_tmp,*) '     !'// &
            '! upper limit is 1.0e-4'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'orbrzv_zzerr_con',orbrzv_zzerr_con,blk%orbrzv_zzerr_con,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! relative error control criterion'
       write(ilun_tmp,*) '     !'// &
            '! 1.0E-4 does very expeditious job'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xdepmod',xdepmod,blk%xdepmod,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! anomalous opacity adjustment'
       write(ilun_tmp,*) '     !'// &
            '! this applies a multiplicative factor to all stopping'
       write(ilun_tmp,*) '     !'// &
            '! cross sections.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xcfanbi',xcfanbi,blk%xcfanbi,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! anomalous multiplier: electron drag and energy'
       write(ilun_tmp,*) '     !'// &
            '! diffusion on beam ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xdfanbi',xdfanbi,blk%xdfanbi,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! anomalous multiplier: ion drag and energy'
       write(ilun_tmp,*) '     !'// &
            '! diffusion on beam ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xefanbi',xefanbi,blk%xefanbi,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! anomalous multiplier: pitch angle scattering on'
       write(ilun_tmp,*) '     !'// &
            '! beam ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xcfafus',xcfafus,blk%xcfafus,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! anomalous multiplier: electron drag and energy'
       write(ilun_tmp,*) '     !'// &
            '! diffusion on fusion product ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xdfafus',xdfafus,blk%xdfafus,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! anomalous multiplier: ion drag and energy'
       write(ilun_tmp,*) '     !'// &
            '! diffusion on fusion product ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xefafus',xefafus,blk%xefafus,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! anomalous multiplier: pitch angle scattering on'
       write(ilun_tmp,*) '     !'// &
            '! fusion product ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'gflr_min',gflr_min,blk%gflr_min,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! for nlbgflr=.TRUE., if the guiding-center estimate'
       write(ilun_tmp,*) '     !'// &
            '! of larmor radius, in cm, is less than this, revert'
       write(ilun_tmp,*) '     !'// &
            '! to the standard guiding-center based treatment (to'
       write(ilun_tmp,*) '     !'// &
            '! save computational effort).'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'gflr_rl',gflr_rl,blk%gflr_rl,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! nlbgflr=.TRUE. expert control (do not touch)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'gflr_ll',gflr_ll,blk%gflr_ll,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! nlbgflr=.TRUE. expert control (do not touch)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'gflr_xv',gflr_xv,blk%gflr_xv,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! nlbgflr=.TRUE. expert controls (do not touch)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'gflr_op',gflr_op,blk%gflr_op,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! nlbgflr=.TRUE. expert controls (do not touch)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xswfrac_allfast',xswfrac_allfast,blk%xswfrac_allfast,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! fraction multiplier -- fast ions participating'
       write(ilun_tmp,*) '     !'// &
            '! in sawtooth mixing'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xswfrac_beam',xswfrac_beam,blk%xswfrac_beam,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! fraction multiplier -- beam ions participating'
       write(ilun_tmp,*) '     !'// &
            '! in sawtooth mixing: net xswfrac_allfast*xswfrac_beam'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xswfrac_fusn',xswfrac_fusn,blk%xswfrac_fusn,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! fraction multiplier -- fusn prod ions participating'
       write(ilun_tmp,*) '     !'// &
            '! in sawtooth mixing: net xswfrac_allfast*xswfrac_fusn'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'fporcelli',fporcelli,blk%fporcelli,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! (if nmix_kdsaw = 3 or 4) interior fractional'
       write(ilun_tmp,*) '     !'// &
            '! width of q=1 island towards the magnetic axis;'
       write(ilun_tmp,*) '     !'// &
            '! fporcelli=1 means island reaches all the way to'
       write(ilun_tmp,*) '     !'// &
            '! axis => Kadomtsev-like mixing.'
       write(ilun_tmp,*) '     !'// &
            '!   0.0_fp <= fporcelli <= 1.0'
       write(ilun_tmp,*) '     !'// &
            '! smaller Porcelli fractions => smaller mixing'
       write(ilun_tmp,*) '     !'// &
            '! region, but the region from the axis to the q=1'
       write(ilun_tmp,*) '     !'// &
            '! surface is always mixed.'
       write(ilun_tmp,*) '     !'// &
            '! -------> see KDSAW NTCC MODULE documentation'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'AEP_ASCALE',AEP_ASCALE,blk%AEP_ASCALE,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! scaling factor for mode amplitudes in  MHD transport operator (Mario Podesta)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'AEP_USCALE',AEP_USCALE,blk%AEP_USCALE,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! scaling factor for electrical potential for MHD transport operator (Mario Podesta)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'TAEP_FAC',TAEP_FAC,blk%TAEP_FAC,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! scaling factor to redefine time step for MHD transport operator (Mario Podesta)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'taurip',taurip,blk%taurip,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! must be set if nrip=1: lint* readMyid0,oss time (sec) for trapped ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'asrd',asrd,blk%asrd,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! nrip=2 adjustment factor for fusion product ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'bsrd',bsrd,blk%bsrd,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! nrip=2 adjustment factor for beam ions.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'erngfi',erngfi,blk%erngfi,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! eV'
       write(ilun_tmp,*) '     !'// &
            '! coarse energy bins for trapping fraction output profiles'
       write(ilun_tmp,*) '     !'// &
            '! if nerngfi=1, there is one bin: [erngfi(1),infinity]'
       write(ilun_tmp,*) '     !'// &
            '! if nergnfi>1, the bins are: [erngfi(1),erngfi(2)]'
       write(ilun_tmp,*) '     !'// &
            '!                             [erngfi(2),erngfi(3)]'
       write(ilun_tmp,*) '     !'// &
            '!                               ...'
       write(ilun_tmp,*) '     !'// &
            '!                             [erngfi(nerngfi),infinity]'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'fbemin',fbemin,blk%fbemin,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! minimum energy (eV) of affected ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'fbemax',fbemax,blk%fbemax,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! maximum energy (eV) of affected ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'fvpvmn',fvpvmn,blk%fvpvmn,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! minimum midplane |vpll/v| of affected ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'fvpvmx',fvpvmx,blk%fvpvmx,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! maximum midplane |vpll/v| of affected ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'fbltim',fbltim,blk%fbltim,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! average loss time (seconds) of affected ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'fshper',fshper,blk%fshper,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! fishbone period (cycle time, seconds)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'fshwid',fshwid,blk%fshwid,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! fishbone duration (seconds)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'tfshon',tfshon,blk%tfshon,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! fishbone onset time (seconds) default always on'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'tfshof',tfshof,blk%tfshof,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! fishbone stop time (seconds) default always on'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xfishmin',xfishmin,blk%xfishmin,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! min (x) where fishbones have an effect'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xfishmax',xfishmax,blk%xfishmax,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! max (x) where fishbones have an effect'
       write(ilun_tmp,*) '     !'// &
            '! (x) the usual sqrt(tor.flux) normalized coordinate; average position'
       write(ilun_tmp,*) '     !'// &
            '! data from last half orbit is used.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'fbtrap_depth',fbtrap_depth,blk%fbtrap_depth,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! trapping depth threshold (nfbon_vpvopt = 1 only)'
       write(ilun_tmp,*) '     !'// &
            '! an orbit is considered trapped if either:'
       write(ilun_tmp,*) '     !'// &
            '!    (a) max(|B|) over orbit half-bounce > max(|B1|,|B2|)'
       write(ilun_tmp,*) '     !'// &
            '!        where B1 and B2 are taken at the midplane crossings (all'
       write(ilun_tmp,*) '     !'// &
            '!        |B|s are guiding center values), or,'
       write(ilun_tmp,*) '     !'// &
            '!    (b) the orbit never reaches the midplane after a multiple of'
       write(ilun_tmp,*) '     !'// &
            '!        tau_bounce has been exceeded (i.e. ST equilibria off-midplane'
       write(ilun_tmp,*) '     !'// &
            '!        trapped orbit class).'
       write(ilun_tmp,*) '     !'// &
            '! IF an orbit is deemed trapped, then, its trapping depth is defined as'
       write(ilun_tmp,*) '     !'// &
            '!        d = (Bmax(psi_avg)-max(|B|)_orb)/(Bmax(psi_avg)-Bmin(psi_avg))'
       write(ilun_tmp,*) '     !'// &
            '! and it is considered eligible for fishbones if d >= fbtrap_depth.'
       write(ilun_tmp,*) '     !'// &
            '! Here, psi_avg is the "average" flux surface seen by the orbit, defined'
       write(ilun_tmp,*) '     !'// &
            '! as 1/2(psi_min+psi_max), where the min and max values are taken over'
       write(ilun_tmp,*) '     !'// &
            '! the last half bounce;'
       write(ilun_tmp,*) '     !'// &
            '!'
       write(ilun_tmp,*) '     !'// &
            '! note that 0 < d < 1 is expected, and is enforced with min & max'
       write(ilun_tmp,*) '     !'// &
            '! functions, as the limits could be exceeded slightly due to the'
       write(ilun_tmp,*) '     !'// &
            '! psi_avg calculation procedure.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xninja_box',xninja_box,blk%xninja_box,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! NO. OF PTCLS COMING IN Ith BOX  N/SEC'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xninjs_box',xninjs_box,blk%xninjs_box,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! n/sec NO. of beam ptcls.injected, by species for BOXes subset only'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xninj_box',xninj_box,blk%xninj_box,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! NO. OF PTCLS INJECTED N/SEC for BOXes subset only'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xboxhw',xboxhw,blk%xboxhw,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! (cm) half-width of entire Cartesian box in x direction'
       write(ilun_tmp,*) '     !'// &
            '! individual bins" x width = 2*xboxhw/nxbox'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'yboxhw',yboxhw,blk%yboxhw,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! (cm) half-height of entire Cartesian box in y direction'
       write(ilun_tmp,*) '     !'// &
            '! individual bins" y width = 2*yboxhw/nybox'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xlbox1',xlbox1,blk%xlbox1,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! (cm) distance from beam source at which box starts'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xlbox2',xlbox2,blk%xlbox2,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! (cm) distance from beam source at which box ends'
       write(ilun_tmp,*) '     !'// &
            '! individual bins" l length = (xlbox2-xlbox1)/nlbox'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'Rtube',Rtube,blk%Rtube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! radial coordinate of start of tube (cm)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'Ytube',Ytube,blk%Ytube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! vertical (z-axis) coordinate of start of tube (cm)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'XZETAtube',XZETAtube,blk%XZETAtube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! angular coordinate of start of tube (degrees)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'PHItube',PHItube,blk%PHItube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! tube"s sightline azimuthal angle from start of tube measured from'
       write(ilun_tmp,*) '     !'// &
            '! the centerline with the positive direction in direction of the'
       write(ilun_tmp,*) '     !'// &
            '! plasma current (degrees)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'THETAtube',THETAtube,blk%THETAtube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! tube"s sightline elevation angle from start of tube measured from'
       write(ilun_tmp,*) '     !'// &
            '! the midplane (degrees)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'XL1tube',XL1tube,blk%XL1tube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! tube data will be collected between XL1tube and XL2tube along sightline.'
       write(ilun_tmp,*) '     !'// &
            '! this is the distance at which data will begin to be collected (cm)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'XL2tube',XL2tube,blk%XL2tube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! tube data will be collected between XL1tube and XL2tube along sightline.'
       write(ilun_tmp,*) '     !'// &
            '! this is the distance at which data will stop being collected (cm)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'RHOtube',RHOtube,blk%RHOtube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! radius of the tubes (cm) -- note that the number of trajectories used to'
       write(ilun_tmp,*) '     !'// &
            '! compute the neutral particle density (tuben0) is inversely proportional'
       write(ilun_tmp,*) '     !'// &
            '! to the tube radius.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'edbfac',edbfac,blk%edbfac,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! debug tripwire -- if an ion reaches edbfac x its initial'
       write(ilun_tmp,*) '     !'// &
            '! deposition energy, kill the run. (dimensionless).'
       write(ilun_tmp,*) ' '
    endif
 
    call dwrite(ilun_tmp,'nonlin',nonlin,blk%nonlin,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! unit number for messages.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nseed',nseed,blk%nseed,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! random number seed; default not recommended!'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlbout',nlbout,blk%nlbout,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) to turn on "lost particle" output data accumulation'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'lunnbx',lunnbx,blk%lunnbx,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! unit number for "lost particles" ascii output file'
       write(ilun_tmp,*) '     !'// &
            '! (will be used if ref_namelist=T is set, see below).'
       write(ilun_tmp,*) '     !'// &
            '! if lunnbx<=0 or lunnbx>99, an available I/O unit is'
       write(ilun_tmp,*) '     !'// &
            '! selected automatically.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'lunres',lunres,blk%lunres,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! auxilliary unit number for ascii file i/o -- supports'
       write(ilun_tmp,*) '     !'// &
            '! debug restarts and restarts of "lunnbx" file.'
       write(ilun_tmp,*) '     !'// &
            '! **** this LUN will be used **** even if .NOT.NLBOUT.'
       write(ilun_tmp,*) '     !'// &
            '! if lunres<=0 or lunres>99, an available I/O unit is'
       write(ilun_tmp,*) '     !'// &
            '! selected automatically.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'mrstrt',mrstrt,blk%mrstrt,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! set to zero if there are *no* check-point / restart files'
       write(ilun_tmp,*) '     !'// &
            '! (a property of the driver code).'
       write(ilun_tmp,*) '     !'// &
            '! this changes the way the NB.OUT file is handled, and may'
       write(ilun_tmp,*) '     !'// &
            '! have other effects in the future.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nclass',nclass,blk%nclass,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! set =1 to activate beam-by-beam full:half:third'
       write(ilun_tmp,*) '     !'// &
            '! energy species resolved profiles of n, Pbe, Pbi,'
       write(ilun_tmp,*) '     !'// &
            '! <v.B>, <(vcrit**3/v**3)v.B> (lab frame v.B) for NCLASS'
       write(ilun_tmp,*) '     !'// &
            '! fast ion interface -- see nclass output block.'
       write(ilun_tmp,*) '     !'// &
            '! *** warning *** dmc Dec 2003 -- definition and'
       write(ilun_tmp,*) '     !'// &
            '! construction of nclass=1 outputs is not yet complete ***'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'only_io',only_io,blk%only_io,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! set .TRUE. (1) when calling NUBEAM from a serial driver that'
       write(ilun_tmp,*) '     !'// &
            '! will use a separate MPI-parallel process to compute the'
       write(ilun_tmp,*) '     !'// &
            '! fast ion evolution.  In this mode, only the input and'
       write(ilun_tmp,*) '     !'// &
            '! output structures are allocated-- NOT the large internal'
       write(ilun_tmp,*) '     !'// &
            '! computational structures.  The following routines are'
       write(ilun_tmp,*) '     !'// &
            '! NOT available in this mode:  NBI_INTERP_PROFILES,'
       write(ilun_tmp,*) '     !'// &
            '! NBSTART, DEPALL, ORBALL, FI_FINISH; instead the timestep'
       write(ilun_tmp,*) '     !'// &
            '! advance sequence is:'
       write(ilun_tmp,*) '     !'// &
            '!    CALL NUBEAM_WRITE_INPUT(...)'
       write(ilun_tmp,*) '     !'// &
            '!     (invoke local procedure for calling parallel NUBEAM'
       write(ilun_tmp,*) '     !'// &
            '!     as separate process)'
       write(ilun_tmp,*) '     !'// &
            '!    CALL NUBEAM_READ_OUTPUT(...)'
       write(ilun_tmp,*) '     !'// &
            '! these calls are more fully explained elsewhere.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'ref_namelist',ref_namelist,blk%ref_namelist,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! NOTE: this is a legacy feature -- mothballed!'
       write(ilun_tmp,*) '     !'// &
            '! set .TRUE. (1) to write a reference namelist'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'quasi_check',quasi_check,blk%quasi_check,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. to check quasineutrality of density input profiles'
       write(ilun_tmp,*) '     !'// &
            '! (can set .FALSE. to disable test, but this can be dangerous!)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nltest_output',nltest_output,blk%nltest_output,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! Set .FALSE. (0) to suppress NBSTART output of interim state'
       write(ilun_tmp,*) '     !'// &
            '! data for nubeam_test time step debug rerun capability'
       write(ilun_tmp,*) '     !'// &
            '! (suppress e.g. to optimize NUBEAM mpi scaling).Default - .TRUE.'
       write(ilun_tmp,*) '     !'// &
            '! debug_mpi= nltest_output for'
       write(ilun_tmp,*) '     !'// &
            '! MPI job debugging'
       write(ilun_tmp,*) '     !'// &
            '! debug_mpi   =.FALSE. for serial mode'
       write(ilun_tmp,*) '     !'// &
            '! see below'
       write(ilun_tmp,*) '     !'// &
            '! in TRANSP  analog of this switch is nlnbi_test_out'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'BLK_MPI_BCAST_INT',BLK_MPI_BCAST_INT,blk%BLK_MPI_BCAST_INT,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! default value for size of a block of integer variables to be used'
       write(ilun_tmp,*) '     !'// &
            '! for MPI communications (bytes)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'BLK_MPI_BCAST_R8',BLK_MPI_BCAST_R8,blk%BLK_MPI_BCAST_R8,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! default value for size of a block of real variables to be used'
       write(ilun_tmp,*) '     !'// &
            '! for MPI communications (bytes)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nzones',nzones,blk%nzones,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of radial zones (equally spaced in sqrt(tor.flux))'
       write(ilun_tmp,*) '     !'// &
            '! this determines the grid for calculation of 1d outputs'
       write(ilun_tmp,*) '     !'// &
            '! such as Pbe and Pbi; must be set to a number .le. mj-3'
       write(ilun_tmp,*) '     !'// &
            '! where "mj" is a user supplied array dimension.'
       write(ilun_tmp,*) '     !'// &
            '! NOTE: not included in Plasma_State_Input, because this'
       write(ilun_tmp,*) '     !'// &
            '! value is used to **set** the plasma state rho_nbi grid.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nzone_fb',nzone_fb,blk%nzone_fb,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of radial zones in 4d fast ion distributions'
       write(ilun_tmp,*) '     !'// &
            '! (r,theta,E,vpll/v).  nzones should be an integer multiple'
       write(ilun_tmp,*) '     !'// &
            '! of nzone_fb; if not, nzone_fb is increased until this'
       write(ilun_tmp,*) '     !'// &
            '! condition is satisfied, or if this is not possible,'
       write(ilun_tmp,*) '     !'// &
            '! nzone_fb=nzones is imposed.  Note: if id_mcgrid is set,'
       write(ilun_tmp,*) '     !'// &
            '! nzone_fb is reset to match the 2d GRID in xplasma.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlsym2b',nlsym2b,blk%nlsym2b,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) to treat fast ion dist. fcn. as up-down symmetric.'
       write(ilun_tmp,*) '     !'// &
            '! Note: if id_mcgrid is set, this is overridden by the 2d'
       write(ilun_tmp,*) '     !'// &
            '! "irregular" grid in xplasma.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nth0',nth0,blk%nth0,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of poloidal zones subtending [0:pi] in the first'
       write(ilun_tmp,*) '     !'// &
            '! radial zone row adjacent to the magnetic axis.'
       write(ilun_tmp,*) '     !'// &
            '! Note: if id_mcgrid is set, this is overridden by the 2d'
       write(ilun_tmp,*) '     !'// &
            '! "irregular" grid in xplasma.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'xplasma_in_memory',xplasma_in_memory,blk%xplasma_in_memory,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! if >0, assume "old_xplasma" shared xplasma'
       write(ilun_tmp,*) '     !'// &
            '! object has been prepositioned by caller to receive NUBEAM'
       write(ilun_tmp,*) '     !'// &
            '! output; if =0, just use "old_xplasma" object; if <0, save'
       write(ilun_tmp,*) '     !'// &
            '! old xplasma object for later restoration.'
       write(ilun_tmp,*) '     !'// &
            '!   ** most users should never touch this **'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nznbma',nznbma,blk%nznbma,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of pitch angle zones for distribution functions'
       write(ilun_tmp,*) '     !'// &
            '! this is the same for all NUBEAM fast ion species'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nznbme',nznbme,blk%nznbme,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of energy zones for beam ion distribution functions'
       write(ilun_tmp,*) '     !'// &
            '! note for fusion product species the number of energy zones'
       write(ilun_tmp,*) '     !'// &
            '! is always equal to the maximum, mimxbe, regardless of the'
       write(ilun_tmp,*) '     !'// &
            '! nznbme specification for beam species.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nznbma0',nznbma0,blk%nznbma0,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of pitch angle zones for neutrals'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nznbme0',nznbme0,blk%nznbme0,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of energy zones for neutrals'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'ngyro',ngyro,blk%ngyro,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! NUMBER OF poloidal AIMING ANGLE ZONES, BEAM F-DIST. CALC.'
       write(ilun_tmp,*) '     !'// &
            '! atan2(V_Z,V_R)-atan2(B_Z,B_R)'
       write(ilun_tmp,*) '     !'// &
            '! must be > 0'
       write(ilun_tmp,*) '     !'// &
            '! for ngyro=4'
       write(ilun_tmp,*) '     !'// &
            '! zone 1 -- [-pi/4,pi/4]         always means perpendicular to the flux surface and outward'
       write(ilun_tmp,*) '     !'// &
            '! zone 2 -- [pi/4,3/4pi]  is tangential to the flux surface with an inward FLR displacement to GC'
       write(ilun_tmp,*) '     !'// &
            '! zone 4 -- [-pi/4,-3/4pi] is tangential to the flux surface with an outward FLR displacement to GC'
       write(ilun_tmp,*) '     !'// &
            '! zone 3 -- [3pi/4,-3/4pi] is perpendicular and inward'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlusf3',nlusf3,blk%nlusf3,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) to set He3 fusion product source magnitude from'
       write(ilun_tmp,*) '     !'// &
            '! data instead of from past timestep fusion rates'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlusfa',nlusfa,blk%nlusfa,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) to set alpha fusion product source magnitude from'
       write(ilun_tmp,*) '     !'// &
            '! data instead of from past timestep fusion rates'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlusft',nlusft,blk%nlusft,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) to set triton fusion product source magnitude from'
       write(ilun_tmp,*) '     !'// &
            '! data instead of from past timestep fusion rates'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlusfp',nlusfp,blk%nlusfp,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) to set proton fusion product source magnitude from'
       write(ilun_tmp,*) '     !'// &
            '! data instead of from past timestep fusion rates'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlusfp14',nlusfp14,blk%nlusfp14,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) to set proton fusion product source magnitude from'
       write(ilun_tmp,*) '     !'// &
            '! data instead of from past timestep fusion rates'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlfatom',nlfatom,blk%nlfatom,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .FALSE. (0) to suppress atomic physics calculations on'
       write(ilun_tmp,*) '     !'// &
            '! slowing down fusion product ions (recommendation:'
       write(ilun_tmp,*) '     !'// &
            '! leave this switch alone).'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlbfpp',nlbfpp,blk%nlbfpp,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! set .TRUE. (1) if beam slowing down is to be modeled using'
       write(ilun_tmp,*) '     !'// &
            '! a fokker planck code, in which case the Monte Carlo code'
       write(ilun_tmp,*) '     !'// &
            '! will be used to calculate deposition only'
       write(ilun_tmp,*) '     !'// &
            '! NOTE: this feature NOT implemented DMC May 2007'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nptcls',nptcls,blk%nptcls,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of Monte Carlo particles per (normal) beam ion specie'
       write(ilun_tmp,*) '     !'// &
            '! affects statistics of slowing down calculation'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nptclf',nptclf,blk%nptclf,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of Monte Carlo particles per fusion product specie'
       write(ilun_tmp,*) '     !'// &
            '! affects statistics of slowing down calculation'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nptclh',nptclh,blk%nptclh,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of Monte Carlo particles per heavy beam ion specie'
       write(ilun_tmp,*) '     !'// &
            '! affects statistics of slowing down / stripping calculation'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NFAST_MAP',NFAST_MAP,blk%NFAST_MAP,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! in NUBEAM only: NFAST_MAP=1 - use bilinear maps to evaluate'
       write(ilun_tmp,*) '     !'// &
            '! R,Z coordinates and 2x2 Jacobian,'
       write(ilun_tmp,*) '     !'// &
            '! if NFAST=0 - use xplasma spline maps with tol=1.e-6 (~1.5 slower)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nptcl_max',nptcl_max,blk%nptcl_max,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! max number of Monte Carlo particles of any type that will'
       write(ilun_tmp,*) '     !'// &
            '! ever be requested; max(nptclh,nptclf,nptcls,nptcl_max) is'
       write(ilun_tmp,*) '     !'// &
            '! used.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nltrk_dep0',nltrk_dep0,blk%nltrk_dep0,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) to save details of ndep0 beam neutral tracks'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NLDEP0_GATHER',NLDEP0_GATHER,blk%NLDEP0_GATHER,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! IF .TRUE. fast ion birth source data (in NUBEAM_COMP_EXEC)'
       write(ilun_tmp,*) '     !'// &
            '! will be collected for all time steps or only for the last one'
       write(ilun_tmp,*) '     !'// &
            '! if NLDEP0_GATHER = .false.'
       write(ilun_tmp,*) '     !'// &
            '! The output files <RINID>_nubeam_birth.cdf<nn> will be kept in'
       write(ilun_tmp,*) '     !'// &
            '! main directory from where program was started (in NUBEAM_COMP_EXEC).'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'ndep0',ndep0,blk%ndep0,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! minimum number of deposition tracks per beam ion specie--'
       write(ilun_tmp,*) '     !'// &
            '! also, the number of tracks gathered, if deposition'
       write(ilun_tmp,*) '     !'// &
            '! distribution data is saved for output, i.e. if'
       write(ilun_tmp,*) '     !'// &
            '! nltrk_dep0 is .TRUE.'
       write(ilun_tmp,*) '     !'// &
            '! Regardless of nltrk_dep0, This control'
       write(ilun_tmp,*) '     !'// &
            '! affects statistics of deposition calculations (e.g.'
       write(ilun_tmp,*) '     !'// &
            '! cold electron source, CX neutral sources, etc.).'
       write(ilun_tmp,*) '     !'// &
            '! In an MPI run this value may be adjusted to be an exact'
       write(ilun_tmp,*) '     !'// &
            '! multiple of the number of processors used.'
       write(ilun_tmp,*) '     !'// &
            '! When deposition track data is gathered, there is an'
       write(ilun_tmp,*) '     !'// &
            '! option to restrict the gathering to a single specific'
       write(ilun_tmp,*) '     !'// &
            '! beam injector, or even a specific energy fraction for'
       write(ilun_tmp,*) '     !'// &
            '! single injector.  (See ndep_set_beam and ndep_set_ien'
       write(ilun_tmp,*) '     !'// &
            '! controls).  But, if this is done, the beam current'
       write(ilun_tmp,*) '     !'// &
            '! fraction (call it f) of the selected beam injector can'
       write(ilun_tmp,*) '     !'// &
            '! come into plan in determining the track data gathering.'
       write(ilun_tmp,*) '     !'// &
            '! If f < 10%, the tracks gathered will be'
       write(ilun_tmp,*) '     !'// &
            '!    ndep0_use = min(ndep0,f*ndep0_max)'
       write(ilun_tmp,*) '     !'// &
            '! if f < 1%, the restriction to the selected beam subset'
       write(ilun_tmp,*) '     !'// &
            '! is aborted, and data, ndep0 tracks, are gathered on'
       write(ilun_tmp,*) '     !'// &
            '! all beam lines.  f is output as frac_cur_dep0.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NDEP0_MAX',NDEP0_MAX,blk%NDEP0_MAX,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! approximate max # of tracks to compute, when'
       write(ilun_tmp,*) '     !'// &
            '! track data is gathered for only a single beam or'
       write(ilun_tmp,*) '     !'// &
            '! single beam energy fraction-- used if the selected'
       write(ilun_tmp,*) '     !'// &
            '! beam current fraction f is < 10%.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'ndep_set_beam',ndep_set_beam,blk%ndep_set_beam,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! beam source # to which deposition data will be gathered'
       write(ilun_tmp,*) '     !'// &
            '! (must be in range 0 to mib).  Default is 0:'
       write(ilun_tmp,*) '     !'// &
            '! 0 - means gather data for all available beame source'
       write(ilun_tmp,*) '     !'// &
            '! if ndep_set_beam set 0, then ndep_set_ien must be set 0.'
       write(ilun_tmp,*) '     !'// &
            '! If beam source# was incorrectly set, then a warning is'
       write(ilun_tmp,*) '     !'// &
            '! printed, and ndep_set_beam=0 is enforced.'
       write(ilun_tmp,*) '     !'// &
            '! If there is no particle current for requested beam or'
       write(ilun_tmp,*) '     !'// &
            '! this current is less than 1% of total ptcl. current then'
       write(ilun_tmp,*) '     !'// &
            '! collect track data for all beam sources and energy fractions'
       write(ilun_tmp,*) '     !'// &
            '! (i.e. revert to ndep_set_beam=0 mode).'
       write(ilun_tmp,*) '     !'// &
            '! If particle current for requested beam is less than 10%'
       write(ilun_tmp,*) '     !'// &
            '! of total ptcl. current - the number of tracks collected'
       write(ilun_tmp,*) '     !'// &
            '! is adjusted to min(NDEP0,f*NDEP0_MAX), where f is the beam'
       write(ilun_tmp,*) '     !'// &
            '! current fraction for the selected beam, relative to the'
       write(ilun_tmp,*) '     !'// &
            '! total summed over all beams.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'ndep_set_ien',ndep_set_ien,blk%ndep_set_ien,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! energy fractions to be used when gathering deposition data'
       write(ilun_tmp,*) '     !'// &
            '! for a given beam source (ndep_set_beam .gt. 0).'
       write(ilun_tmp,*) '     !'// &
            '! 1 - full energy fraction'
       write(ilun_tmp,*) '     !'// &
            '! 2 - half energy fraction'
       write(ilun_tmp,*) '     !'// &
            '! 3 - one third energy'
       write(ilun_tmp,*) '     !'// &
            '! 0 - for all energy fraction'
       write(ilun_tmp,*) '     !'// &
            '! if energy fraction# was incorrect set data is collected'
       write(ilun_tmp,*) '     !'// &
            '! for all energy fractions.'
       write(ilun_tmp,*) '     !'// &
            '! For beams without energy fractions (i.e. Helium neutral'
       write(ilun_tmp,*) '     !'// &
            '! beams or high energy negative ion based H/D/T beams),'
       write(ilun_tmp,*) '     !'// &
            '! this control is ignored.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nbbcal',nbbcal,blk%nbbcal,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of interactions for [fast ion] - [fast ion]'
       write(ilun_tmp,*) '     !'// &
            '! fusion rate Monte Carlo integrals-- affects statistics'
       write(ilun_tmp,*) '     !'// &
            '! of this component of fusion product and neutron source'
       write(ilun_tmp,*) '     !'// &
            '! calculation'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'ncx0',ncx0,blk%ncx0,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! the total number of CX tracks, independent control of CX statistics'
       write(ilun_tmp,*) '     !'// &
            '! if ncx0 = 0, then it will be no fbm_cx gathered and CX statistics will not be'
       write(ilun_tmp,*) '     !'// &
            '! improved'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nper_cx',nper_cx,blk%nper_cx,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! the  number of gyro phase angles between [0,2*pi] will be used for CX statistics improvement'
       write(ilun_tmp,*) '     !'// &
            '! if ncx0 > 0 , nper_cx=max(1,nper_cx)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'LEV_NBIDEP',LEV_NBIDEP,blk%LEV_NBIDEP,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! beam "deposision" model level:'
       write(ilun_tmp,*) '     !'// &
            '! 1 -- legacy of PREACT atomic physics'
       write(ilun_tmp,*) '     !'// &
            '! 2 -- ADAS atomic physics for H and He beams.'
       write(ilun_tmp,*) '     !'// &
            '!      He beam -- only ground state model available'
       write(ilun_tmp,*) '     !'// &
            '! 3 -- PREACT ground state atomic physics'
       write(ilun_tmp,*) '     !'// &
            '!      ADAS interaction with impurities'
       write(ilun_tmp,*) '     !'// &
            '! 4 -- ADAS ground state atomic physics'
       write(ilun_tmp,*) '     !'// &
            '!      PREACT interaction with impurities'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'LEV_TTNUK',LEV_TTNUK,blk%LEV_TTNUK,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! nuclear data for T(T,2n)4He  fusion reaction'
       write(ilun_tmp,*) '     !'// &
            '! 0 -- legacy of PREACT nuclear physics for T(T,2n)4He'
       write(ilun_tmp,*) '     !'// &
            '! 1 -- DPC fit (David.P.Coster fit added on 07/2018)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'LEVMOD_HALO',LEVMOD_HALO,blk%LEVMOD_HALO,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! 0 -- driver program calls FRANTIC (default)'
       write(ilun_tmp,*) '     !'// &
            '! 1 -- FRANTIC will be called in NUBEAM'
       write(ilun_tmp,*) '     !'// &
            '! 2 -- 3d halo model will be called in NUBEAM'
       write(ilun_tmp,*) '     !'// &
            '!      for 3d halo model NSPLT_GEO and NSPLT_KIN have to be set'
       write(ilun_tmp,*) '     !'// &
            '!      or default values will be used'
       write(ilun_tmp,*) '     !'// &
            '!      as 3d halo model is a "beam in box" then'
       write(ilun_tmp,*) '     !'// &
            '!      NBSBOX, NBEBOX, NXBOX, NYBOX, NLBOX, NDEPBOX etc. have to be set'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NSPLT',NSPLT,blk%NSPLT,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! activated in case LEVMOD_HALO=2'
       write(ilun_tmp,*) '     !'// &
            '! max allowed spliting along each neutral track (3D halo model),'
       write(ilun_tmp,*) '     !'// &
            '! NSPLT must be >= NSPLT_GEO* NSPLT_KIN'
       write(ilun_tmp,*) '     !'// &
            '! if NSPLT < 0 then it  maybe use to gather 3d density 0 generation of neutrals, only'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NSPLT_GEO',NSPLT_GEO,blk%NSPLT_GEO,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! activated in case LEVMOD_HALO=2, and NSPLT > 0'
       write(ilun_tmp,*) '     !'// &
            '! max allowed spliting along each neutral track (3D halo model),'
       write(ilun_tmp,*) '     !'// &
            '! max allowed geometrical spliting along each neutral track (3D halo model)'
       write(ilun_tmp,*) '     !'// &
            '!'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NSPLT_KIN',NSPLT_KIN,blk%NSPLT_KIN,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! activated in case LEVMOD_HALO=2, and NSPLT > 0'
       write(ilun_tmp,*) '     !'// &
            '! max allowed spliting along each neutral track (3D halo model),'
       write(ilun_tmp,*) '     !'// &
            '! max allowed kinetic spliting at each CX launch point; # of velocities'
       write(ilun_tmp,*) '     !'// &
            '! that are sampled from eligible ion populations (3D halo model)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NSPLT_GEN',NSPLT_GEN,blk%NSPLT_GEN,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! # of halo generation to be splitted at the'
       write(ilun_tmp,*) '     !'// &
            '! rate NSPLT_KIN * NSPLT_GEO, and for higher generation will be no additional'
       write(ilun_tmp,*) '     !'// &
            '! splitting made.  If NSPLT_GEN = 0, all halo generation will be splitted at the'
       write(ilun_tmp,*) '     !'// &
            '! rate NSPLT_KIN * NSPLT_GEO. Activated in case LEVMOD_HALO=2, and NSPLT > 0'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nmimp',nmimp,blk%nmimp,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! 1 -> using multiple impurities'
       write(ilun_tmp,*) '     !'// &
            '! 0 -> using single tokamakium impurity'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NTEST_MAX',NTEST_MAX,blk%NTEST_MAX,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! set 1 to simulate bi-Maxwellian FI distribusion;'
       write(ilun_tmp,*) '     !'// &
            '!  -1 to skip re-normalization of electric field from TORIC'
       write(ilun_tmp,*) '     !'// &
            '! in RF kick off operator in NUBEAM for bi-Maxwellian FI distribusion;'
       write(ilun_tmp,*) '     !'// &
            '! -10 to skip re-normalization of electric field from TORIC'
       write(ilun_tmp,*) '     !'// &
            '! in RF kick off operator in NUBEAM for realistic FI distribusion'
       write(ilun_tmp,*) '     !'// &
            '! this switch may be used for standalone nubeam_comp_exec program'
       write(ilun_tmp,*) '     !'// &
            '! and can be modified through nubeam_comp_exec namelist'
       write(ilun_tmp,*) '     !'// &
            '! 10 same as default but extra output files will be saved to be used in TORIC:'
       write(ilun_tmp,*) '     !'// &
            '! some variables on sqrt(norm poloidal flux)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NGOOCON_VPVBIN',NGOOCON_VPVBIN,blk%NGOOCON_VPVBIN,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! min #bounces per FBM pitch zone width P.A. scatter'
       write(ilun_tmp,*) '     !'// &
            '! setting NGOOCON_VPVBIN constrains tau(bounce)/tau(P.A.scatter) by'
       write(ilun_tmp,*) '     !'// &
            '! a factor of 4/(NGOOCON_VPVBIN**2*NZNBMA**2)'
       write(ilun_tmp,*) '     !'// &
            '! a limit that scales as 1/(nznbma**2) on goosing is imposed if .gt.0'
       write(ilun_tmp,*) '     !'// &
            '! Caution: setting this may slow the code considerably.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NGOOCON_EBIN',NGOOCON_EBIN,blk%NGOOCON_EBIN,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! min #bounces per FBM energy zone width slowing down'
       write(ilun_tmp,*) '     !'// &
            '! additional linear constraint on tau(bounce)/tau(slowing-down)'
       write(ilun_tmp,*) '     !'// &
            '! a limit that scales as 1/nznbme on goosing is imposed'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NGOOCON_RBIN',NGOOCON_RBIN,blk%NGOOCON_RBIN,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! min #bounces per zone width radial transport'
       write(ilun_tmp,*) '     !'// &
            '! linear constraint on tau(bounce)/tau(radial-flow)'
       write(ilun_tmp,*) '     !'// &
            '! quadratic constraint on tau(bounce)/tau(radial-diffusion)'
       write(ilun_tmp,*) '     !'// &
            '! limits that scale as 1/(nzone_fb) and 1/(nzone_fb**2) are imposed.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nchdvp',nchdvp,blk%nchdvp,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! orbit integrator options:'
       write(ilun_tmp,*) '     !'// &
            '!  =1: enforce exact conservation of angular momentum'
       write(ilun_tmp,*) '     !'// &
            '!  =2: enforce exact conservation of energy'
       write(ilun_tmp,*) '     !'// &
            '!  =3: (recommended) obtain good but not exact conservation'
       write(ilun_tmp,*) '     !'// &
            '!      of both quantities.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'orbrzv_option',orbrzv_option,blk%orbrzv_option,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! option to switch on & off ORBRZ(RZ-integrator) and ORBG'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'orbrzv_rf_option',orbrzv_rf_option,blk%orbrzv_rf_option,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! option to switch on & off Monte-Carlo RF calculation'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlmcrf_on',nlmcrf_on,blk%nlmcrf_on,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'orbrzv_nr',orbrzv_nr,blk%orbrzv_nr,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'orbrzv_nz',orbrzv_nz,blk%orbrzv_nz,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! grid resolution for RZ-interpolation in ORBRZ'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlminsv',nlminsv,blk%nlminsv,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) to loop over all impurities when calculating'
       write(ilun_tmp,*) '     !'// &
            '! impurity stopping cross section; .FALSE. to use an'
       write(ilun_tmp,*) '     !'// &
            '! "average Z" single impurity simplifying approximation.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlebei',nlebei,blk%nlebei,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) to include beam energy in approximate'
       write(ilun_tmp,*) '     !'// &
            '! expression for <sigma*v> for electron impact ionization'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nsigexc',nsigexc,blk%nsigexc,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! NSIGEXC=0 means ground state model.'
       write(ilun_tmp,*) '     !'// &
            '! NSIGEXC=1 means "appropriate applicable" excited states model:'
       write(ilun_tmp,*) '     !'// &
            '! ADAS for LEV_NBIDEP=2 and'
       write(ilun_tmp,*) '     !'// &
            '! Janev-Boley-Post for other values of LEV_NBIDEP=1'
       write(ilun_tmp,*) '     !'// &
            '! NSIGEXC=2 means Janev-Boley-Post applied to any model including ADAS.'
       write(ilun_tmp,*) '     !'// &
            '! Janev/Boley/Post article, Nucl. Fusion Vol.29, No. 12 (1989)'
       write(ilun_tmp,*) '     !'// &
            '! NSIGEXC=3 means ADAS excited states model (adas310 library):'
       write(ilun_tmp,*) '     !'// &
            '! ADAS for LEV_NBIDEP=2 and'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlbbcx',nlbbcx,blk%nlbbcx,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .FALSE. (0) to disable beam-beam charge-exchange and'
       write(ilun_tmp,*) '     !'// &
            '! beam-beam impact-ionization contribution to deposition.'
       write(ilun_tmp,*) '     !'// &
            '! This concerns beam neutrals being stopped by collisions'
       write(ilun_tmp,*) '     !'// &
            '! with slowing down beam ions.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nbbcx_avg',nbbcx_avg,blk%nbbcx_avg,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! use non average over the flux surface relative energy (ebb_rel_avg)'
       write(ilun_tmp,*) '     !'// &
            '! and target temperature (rms_ebb_rel) for beam-beam reaction to calculate'
       write(ilun_tmp,*) '     !'// &
            '! enhancement factor'
       write(ilun_tmp,*) '     !'// &
            '! =1 then use average over the flux surface relative energy (ebb_rel_avgx)'
       write(ilun_tmp,*) '     !'// &
            '! and target temperature (rms_ebb_relx)'
       write(ilun_tmp,*) '     !'// &
            '! this switch is activated if nlbbcx = .TRUE.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nbbcx_bb',nbbcx_bb,blk%nbbcx_bb,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! integer switch. If nbbcx_bb=0 then contribution from beam-beam reactions to the enhancement'
       write(ilun_tmp,*) '     !'// &
            '! factor will be included in case if NLBBCX=.TRUE.'
       write(ilun_tmp,*) '     !'// &
            '! If nbbcx_bb.ne.0  then contribution from beam-beam reactions to the enhancement'
       write(ilun_tmp,*) '     !'// &
            '! factor will be NOT included even if NLBBCX=.TRUE.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nbfallgr',nbfallgr,blk%nbfallgr,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! if set 1, then program will be stopped, if enhancement'
       write(ilun_tmp,*) '     !'// &
            '! factor computed from ADAS data set became less than 1.0.'
       write(ilun_tmp,*) '     !'// &
            '! This setting will be used for a debugging purposes.'
       write(ilun_tmp,*) '     !'// &
            '! If set 0,then enhancement factor computed from ADAS data set'
       write(ilun_tmp,*) '     !'// &
            '! will be forced to be 1.0 if it became less than 1.0. It means that'
       write(ilun_tmp,*) '     !'// &
            '! ground state data will be used for this region.'
       write(ilun_tmp,*) '     !'// &
            '! This switch will be used if nlbbcx=.true.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlbeamcx',nlbeamcx,blk%nlbeamcx,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! set .FALSE. (0) to disable beam charge exchange losses'
       write(ilun_tmp,*) '     !'// &
            '! (if .FALSE. beamcx subroutine is never called).  For'
       write(ilun_tmp,*) '     !'// &
            '! technical reasons nlbeamcx=.FALSE. implies nlbbcx=.FALSE.'
       write(ilun_tmp,*) '     !'// &
            '! but cx stopping of neutral beams on thermal species still'
       write(ilun_tmp,*) '     !'// &
            '! occurs, and a beam halo neutral source is still computed.'
       write(ilun_tmp,*) '     !'// &
            '! Caution: nb*sigma*v ionization rate profile for background'
       write(ilun_tmp,*) '     !'// &
            '! neutrals will be ZERO if nlbeamcx=.FALSE. -- this is for'
       write(ilun_tmp,*) '     !'// &
            '! debugging only!'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlhvion',nlhvion,blk%nlhvion,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! this switch only used for heavy neutral beam deposition'
       write(ilun_tmp,*) '     !'// &
            '! (i.e. experiments using neutral beams to inject Argon or'
       write(ilun_tmp,*) '     !'// &
            '! Neon-- very rare).  If set .TRUE. (1), ion collisions are used'
       write(ilun_tmp,*) '     !'// &
            '! in calculating the stripping of the deposited heavy ion; if'
       write(ilun_tmp,*) '     !'// &
            '! .FALSE. (0) only electron collisions are used.  For ordinary'
       write(ilun_tmp,*) '     !'// &
            '! (Hydrogen or Helium) beams this switch has no effect.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlbcde',nlbcde,blk%nlbcde,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .FALSE. (0) to turn off energy diffusion'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlbcoh',nlbcoh,blk%nlbcoh,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .FALSE. (0) to suppress acceleration of beam ions by'
       write(ilun_tmp,*) '     !'// &
            '! toroidal electric field'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlbcpa',nlbcpa,blk%nlbcpa,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .FALSE. (0) to turn off pitch angle scattering'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlorbo',nlorbo,blk%nlorbo,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) for "orbit only" mode: suppress all collisions'
       write(ilun_tmp,*) '     !'// &
            '! used only in specialized debugging situations.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlbflr',nlbflr,blk%nlbflr,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) for standard FLR treatment, based on a mod(B)'
       write(ilun_tmp,*) '     !'// &
            '! at guiding center and notion of a FLR circle about'
       write(ilun_tmp,*) '     !'// &
            '! the guiding center, upon which a random gyro-phase'
       write(ilun_tmp,*) '     !'// &
            '! angle is taken to define the current particle position.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlfbmflr',nlfbmflr,blk%nlfbmflr,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) to use the FLR localized fast ion'
       write(ilun_tmp,*) '     !'// &
            '! distribution for beam-beam neutron calculations.'
       write(ilun_tmp,*) '     !'// &
            '! Note that recent code changes (by Marina Gorelenkova)'
       write(ilun_tmp,*) '     !'// &
            '! ensure that both an FLR and a GC localized fast ion'
       write(ilun_tmp,*) '     !'// &
            '! distribution are computed and output on every run.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlbgflr',nlbgflr,blk%nlbgflr,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. (1) for enhanced FLR treatment'
       write(ilun_tmp,*) '     !'// &
            '! this deforms the FLR trajectory by looking at the'
       write(ilun_tmp,*) '     !'// &
            '! variation of mod(B) in the guiding center vicinity,'
       write(ilun_tmp,*) '     !'// &
            '! and is appropriate when the larmor radius is >~ L(B)'
       write(ilun_tmp,*) '     !'// &
            '! e.g. in low field systems such as STs.  **but** this'
       write(ilun_tmp,*) '     !'// &
            '! option is extremely expensive computationally!'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'sawflag',sawflag,blk%sawflag,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! set .TRUE. (1) if sawtooth occurred since prior step'
       write(ilun_tmp,*) '     !'// &
            '! (this means: apply previously calculated sawtooth'
       write(ilun_tmp,*) '     !'// &
            '! mixed new start coordinates when restarting partially'
       write(ilun_tmp,*) '     !'// &
            '! slowed down orbiting ions).'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlsawb',nlsawb,blk%nlsawb,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! if .TRUE. (1) calculate sawtooth mixing of beam ions.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlsawf',nlsawf,blk%nlsawf,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! if .TRUE. (1) calculate sawtooth mixing of fusion products.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nmix_kdsaw',nmix_kdsaw,blk%nmix_kdsaw,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! KDSAW (Kadomtsev/Porcelli) mixing model option'
       write(ilun_tmp,*) '     !'// &
            '! =1: standard Kadomtsev'
       write(ilun_tmp,*) '     !'// &
            '! =2: Kadomtsev, ions fully mixed => flat density'
       write(ilun_tmp,*) '     !'// &
            '! =3: Porcelli, two mixing regions (island annulus +'
       write(ilun_tmp,*) '     !'// &
            '!     interior axial region separately mixed).'
       write(ilun_tmp,*) '     !'// &
            '! =4: Porcelli, ions fully mixed => flat density;'
       write(ilun_tmp,*) '     !'// &
            '! -------> see KDSAW NTCC MODULE documentation'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nkaep',nkaep,blk%nkaep,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of terms (NK) in expansion of MHD transport operator (Mario Podesta)'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'ngradd_opt',ngradd_opt,blk%ngradd_opt,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! grad(Db) pinch cancellation option'
       write(ilun_tmp,*) '     !'// &
            '! 0, default, means automatic: active for 1d diffusivity'
       write(ilun_tmp,*) '     !'// &
            '! profile inputs, inactive for 2d inputs.  Set =1 to force'
       write(ilun_tmp,*) '     !'// &
            '! grad(Db) correction; set to -1 to force its absence.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nrip',nrip,blk%nrip,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! =0: no ripple loss;  =1: simple "loss time" model'
       write(ilun_tmp,*) '     !'// &
            '! =2: adjustable model based on Goldston-White-Boozer and'
       write(ilun_tmp,*) '     !'// &
            '!     ripple field strength. CAUTION-- calibration against'
       write(ilun_tmp,*) '     !'// &
            '!     a more exact model is needed.  PPPL expert help should'
       write(ilun_tmp,*) '     !'// &
            '!     be sought before using this model'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nerngfi',nerngfi,blk%nerngfi,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of energy bins for trapping fraction output profiles'
       write(ilun_tmp,*) '     !'// &
            '! generally only coarse binning is useful, e.g. nerngfi=5.'
       write(ilun_tmp,*) '     !'// &
            '! the maximum allowed value is the (user settable) array'
       write(ilun_tmp,*) '     !'// &
            '! dimension "miefi".'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nsdbgb',nsdbgb,blk%nsdbgb,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! index to fast ion specie selected for debug output'
       write(ilun_tmp,*) '     !'// &
            '! (artifact-- not currently used).'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlfbon',nlfbon,blk%nlfbon,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! .TRUE. to turn on fishbone loss model'
       write(ilun_tmp,*) '     !'// &
            '! "fishbones" are an MHD phenomenon though to be capable'
       write(ilun_tmp,*) '     !'// &
            '! of ejecting fast ions.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nfbon_vpvopt',nfbon_vpvopt,blk%nfbon_vpvopt,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! =0: control vpll/v of affected ions with {fvpvmn,fvpvmx}'
       write(ilun_tmp,*) '     !'// &
            '! =1: only operate on trapped ions with a trapping depth'
       write(ilun_tmp,*) '     !'// &
            '!     exceeding the threshold fbtrap_depth (see descr.).'
       write(ilun_tmp,*) '     !'// &
            '!     Note (fbemin,fbemax) constraints still apply.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nfbon_species',nfbon_species,blk%nfbon_species,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! species control'
       write(ilun_tmp,*) '     !'// &
            '!   =1 beam ions only'
       write(ilun_tmp,*) '     !'// &
            '!   =2 fusion product ions only'
       write(ilun_tmp,*) '     !'// &
            '!   =3 (default) all fast ions'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nbbox',nbbox,blk%nbbox,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of "beam-in-box" neutral density profile'
       write(ilun_tmp,*) '     !'// &
            '! calculations'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nbsbox',nbsbox,blk%nbsbox,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! beam source numbers for neutral density calculation.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nsample_box',nsample_box,blk%nsample_box,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! # of lanched MC particles in each box'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nbebox',nbebox,blk%nbebox,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! beam energy fractions, before which to compute a 3d'
       write(ilun_tmp,*) '     !'// &
            '! neutral density profile on a Cartesian grid alligned'
       write(ilun_tmp,*) '     !'// &
            '! on the beam centerline.'
       write(ilun_tmp,*) '     !'// &
            '!   nbsbox(i)=j  nbebox(i)=k'
       write(ilun_tmp,*) '     !'// &
            '! means the i"th neutral density calculation is done in'
       write(ilun_tmp,*) '     !'// &
            '! front of beam source j, using energy fraction k (for H'
       write(ilun_tmp,*) '     !'// &
            '! isotope beams k=1 means full energy, k=2 means half-'
       write(ilun_tmp,*) '     !'// &
            '! energy, k=3 means 1/3 energy).'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nxbox',nxbox,blk%nxbox,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of bins in horizontal direction (x) in front of beam'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nybox',nybox,blk%nybox,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of bins in vertical direction (y) in front of beam'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlbox',nlbox,blk%nlbox,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of bins in the direction (l) of beam injection'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'ndepbox',ndepbox,blk%ndepbox,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! Monte Carlo statistics control:  minimum number of neutral'
       write(ilun_tmp,*) '     !'// &
            '! tracks to use per calculation.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nbtube',nbtube,blk%nbtube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of beam-in-tube neutral density calculations'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nbstube',nbstube,blk%nbstube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! tube beam source numbers'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nbetube',nbetube,blk%nbetube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! beam energy fractions to be used when calculating neutral'
       write(ilun_tmp,*) '     !'// &
            '! density'
       write(ilun_tmp,*) '     !'// &
            '!   nbstube(i)=j  nbetube(i)=k'
       write(ilun_tmp,*) '     !'// &
            '! means the i"th neutral density calculation is done in'
       write(ilun_tmp,*) '     !'// &
            '! front of beam source j, using energy fraction k (for H'
       write(ilun_tmp,*) '     !'// &
            '! isotope beams k=1 means full energy, k=2 means half-'
       write(ilun_tmp,*) '     !'// &
            '! energy, k=3 means 1/3 energy).'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'LMIDtube',LMIDtube,blk%LMIDtube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! 0->tube data will be collected between XL1tube<s<XL2tube'
       write(ilun_tmp,*) '     !'// &
            '! 1->tube data will be collected from XL1tube to XL2tube relative'
       write(ilun_tmp,*) '     !'// &
            '!    to the point on the tube which is closest to the beam"s sightline'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NSEGtube',NSEGtube,blk%NSEGtube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! number of data segments between XL1tube and XL2tube in which to compute'
       write(ilun_tmp,*) '     !'// &
            '! the neutral density data.  This number must be less then mituben.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'NDEPtube',NDEPtube,blk%NDEPtube,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! estimated number of beam intersections per tube assuming no beam loss and a tube'
       write(ilun_tmp,*) '     !'// &
            '! perfectly centered within the beam aperture.  The number of trajectories which'
       write(ilun_tmp,*) '     !'// &
            '! will be attempted will be  NDEPtube*sqrt(area_aperture)/2*RHOtube'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlcprb',nlcprb,blk%nlcprb,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! set .FALSE. (0) to suppress compression operator and other'
       write(ilun_tmp,*) '     !'// &
            '! effects of time varying MHD equilibrium on beam ions'
       write(ilun_tmp,*) '     !'// &
            '! (recommendation: leave default, .TRUE., alone).'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlpsirz',nlpsirz,blk%nlpsirz,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! set .TRUE. (1) if an accurate Psi(R,Z) (free boundary) MHD'
       write(ilun_tmp,*) '     !'// &
            '! equilibrium, extending beyond the plasma boundary, is'
       write(ilun_tmp,*) '     !'// &
            '! available.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nmcurb',nmcurb,blk%nmcurb,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! set to 0 to suppress fast ion driven current.'
       write(ilun_tmp,*) '     !'// &
            '! nmcurb=1 gives an aspect-ratio approximate neoclassical'
       write(ilun_tmp,*) '     !'// &
            '! shielding calculation for the current; nmcurb=2 gives'
       write(ilun_tmp,*) '     !'// &
            '! the simplified shielding correction (1-1/Zeff).'
       write(ilun_tmp,*) '     !'// &
            '! nmcurb=3  gives neoclassical shielding calculation for the current'
       write(ilun_tmp,*) '     !'// &
            '! by Lin-Liu formula Phys.Plasmas 4(11),p.4179, 1997. Other'
       write(ilun_tmp,*) '     !'// &
            '! non-zero values of nmcurb give the same result as nmcurb=1'
       write(ilun_tmp,*) '     !'// &
            '! (for now), but, a fully generalized geometry neoclassical'
       write(ilun_tmp,*) '     !'// &
            '! shielding calculation will be implemented at some point'
       write(ilun_tmp,*) '     !'// &
            '! in the future, and nmcurb=3 may be used to select this,'
       write(ilun_tmp,*) '     !'// &
            '! when it exists.'
       write(ilun_tmp,*) '     !'// &
            '! NMCURB  = 4, shielding calculation by Honda M., Kikuchi M., Azumi M.'
       write(ilun_tmp,*) '     !'// &
            '! "Collisionality dependence on shielding factor of beam driven current"'
       write(ilun_tmp,*) '     !'// &
            '! Nuclear Fusion, submitted by October 2011. Martix inversion method are used'
       write(ilun_tmp,*) '     !'// &
            '! to include collisionality dependence on a shielding factor at arbitrary aspect ratio.'
       write(ilun_tmp,*) ' '
    endif
    call dwrite(ilun_tmp,'nlfdep',nlfdep,blk%nlfdep,idiff)
    if(idiff.gt.0) then
       write(ilun_tmp,*) '     !'// &
            '! set .TRUE. (1) to compute midplane index oriented deposition'
       write(ilun_tmp,*) '     !'// &
            '! distribution function in the lab frame using the "fbm"'
       write(ilun_tmp,*) '     !'// &
            '! grids for pitch and energy, with particles mapped to'
       write(ilun_tmp,*) '     !'// &
            '! their outer midplane crossing (a few particles which'
       write(ilun_tmp,*) '     !'// &
            '! never cross the midplane at R > Raxis will be missed!).'
       write(ilun_tmp,*) '     !'// &
            '! setting this switch causes a specialized, rarely used'
       write(ilun_tmp,*) '     !'// &
            '! output data structure to be created'
       write(ilun_tmp,*) '     !'// &
            '! nlfdep ***DISABLED*** DMC May 2 2007'
       write(ilun_tmp,*) '     !'// &
            '! deposition data is now output on original chordal tracks'
       write(ilun_tmp,*) ' '
    endif

    write(ilun_tmp,*) '/'
    close(unit=ilun_tmp)

  end subroutine nbi_ctrl_init_write

  subroutine nbi_ctrl_update_write(ofile,blk,ierr)

    ! write NON-DEFAULT values in the nbi_ctrl_update block
    ! to a commented namelist file

    character*(*) :: ofile            ! output file
    type (nbi_ctrl_update) :: blk  ! written to file
    integer, intent(out) :: ierr      ! status, 0=OK

    !---------------------------
    ! local:
    integer :: ilun_dbg = 0
    integer :: ilun_tmp,istat,idiff
    !---------------------------
    ! local copies of elements for default values:
 
    real(fp) :: orbrzv_toric_Prftot0(15)  ! (1:mibs)
    REAL(fp) :: orbrzv_toric_frnm
    REAL(fp) :: xdatsf3
    REAL(fp) :: xdatsfa
    REAL(fp) :: xdatsft
    REAL(fp) :: xdatsfp
    REAL(fp) :: xdatsp3
    REAL(fp) :: xpand_nptcl
    REAL(fp) :: frac_dep_lim
    REAL(fp) :: AIMP
    REAL(fp) :: XZIMP
    REAL(fp) :: BI_MAX_PL
    REAL(fp) :: BI_MAX_PR
    REAL(fp) :: wghta
    real(fp) :: AEP_ASCALE(10)  ! (1:mnkaep)
    REAL(fp) :: AEP_USCALE
    real(fp) :: TAEP_FAC(10)  ! (1:mnkaep)
    REAL(fp) :: taurip
    REAL(fp) :: edbfac
 
    INTEGER :: nlbout
    INTEGER :: nltest_output
    INTEGER :: xplasma_in_memory
    INTEGER :: ngyro
    INTEGER :: nlusf3
    INTEGER :: nlusfa
    INTEGER :: nlusft
    INTEGER :: nlusfp
    INTEGER :: nlusfp14
    INTEGER :: nptcls
    INTEGER :: nptclf
    INTEGER :: nptclh
    INTEGER :: NFAST_MAP
    INTEGER :: nltrk_dep0
    INTEGER :: NLDEP0_GATHER
    INTEGER :: ndep0
    INTEGER :: NDEP0_MAX
    INTEGER :: ndep_set_beam
    INTEGER :: ndep_set_ien
    INTEGER :: ncx0
    INTEGER :: nper_cx
    INTEGER :: LEV_NBIDEP
    INTEGER :: LEV_TTNUK
    INTEGER :: LEVMOD_HALO
    INTEGER :: NSPLT
    INTEGER :: NSPLT_GEO
    INTEGER :: NSPLT_KIN
    INTEGER :: NSPLT_GEN
    INTEGER :: nmimp
    INTEGER :: NTEST_MAX
    INTEGER :: orbrzv_rf_option
    INTEGER :: nlmcrf_on
    INTEGER :: nsigexc
    INTEGER :: nlbbcx
    INTEGER :: nbbcx_avg
    INTEGER :: nbbcx_bb
    INTEGER :: nlorbo
    INTEGER :: nlbgflr
    INTEGER :: sawflag
    INTEGER :: nlsawb
    INTEGER :: nlsawf
    INTEGER :: nkaep
    INTEGER :: nlfbon

    !---------------------------
    ierr = 0

    call find_io_unit(ilun_tmp)
    open(unit=ilun_tmp,file=trim(ofile),status='unknown')

    write(ilun_tmp,*) ' !! nbi_ctrl_update_write -- this file contains'
    write(ilun_tmp,*) ' !!   non-default values of time step update inputs'
    write(ilun_tmp,*) ' !!   to NUBEAM, in namelist format.'
    write(ilun_tmp,*) ' !!      LOGICAL variables mapped to INTEGER: 1=TRUE; 0=FALSE'
    write(ilun_tmp,*) ' '
    write(ilun_tmp,*) '&NBI_UPDATE'
    write(ilun_tmp,*) ' '

    ! set default values...

    orbrzv_toric_Prftot0 = 0.0_fp
    orbrzv_toric_frnm = 1.0_fp
    xdatsf3 = 0.0_fp
    xdatsfa = 0.0_fp
    xdatsft = 0.0_fp
    xdatsfp = 0.0_fp
    xdatsp3 = 0.0_fp
    xpand_nptcl = 3.0_fp
    frac_dep_lim = 1.5_fp
    AIMP =    16._fp
    XZIMP =    8._fp
    BI_MAX_PL =  1._fp
    BI_MAX_PR =  1._fp
    wghta = 1.0_fp
    AEP_ASCALE =  0.0_fp
    AEP_USCALE =  1.0_fp
    TAEP_FAC =  0.0_fp
    taurip = 0.0_fp
    edbfac = 10.0_fp
 
    nlbout = inbi_logical(.TRUE.)
    nltest_output = inbi_logical(.TRUE.)
    xplasma_in_memory = 0
    ngyro = 4
    nlusf3 = inbi_logical(.FALSE.)
    nlusfa = inbi_logical(.FALSE.)
    nlusft = inbi_logical(.FALSE.)
    nlusfp = inbi_logical(.FALSE.)
    nlusfp14 = inbi_logical(.FALSE.)
    nptcls = 1000
    nptclf = 1000
    nptclh = 500
    NFAST_MAP = 1
    nltrk_dep0 = inbi_logical(.FALSE.)
    NLDEP0_GATHER = inbi_logical( .FALSE.)
    ndep0 = 500
    NDEP0_MAX =    100000
    ndep_set_beam = 0
    ndep_set_ien = 0
    ncx0 = 100
    nper_cx = 4
    LEV_NBIDEP =  1
    LEV_TTNUK =  0
    LEVMOD_HALO =  0
    NSPLT =  100
    NSPLT_GEO =  1
    NSPLT_KIN =  1
    NSPLT_GEN =  1
    nmimp =  1
    NTEST_MAX =  0
    orbrzv_rf_option = 0
    nlmcrf_on = inbi_logical(.false.)
    nsigexc = 0
    nlbbcx = inbi_logical(.TRUE.)
    nbbcx_avg = 0
    nbbcx_bb = 0
    nlorbo = inbi_logical(.false.)
    nlbgflr = inbi_logical(.FALSE.)
    sawflag = inbi_logical(.FALSE.)
    nlsawb = inbi_logical(.TRUE.)
    nlsawf = inbi_logical(.TRUE.)
    nkaep =  0
    nlfbon = inbi_logical(.FALSE.)
 
    call dwrite(ilun_tmp,'orbrzv_toric_Prftot0',orbrzv_toric_Prftot0,blk%orbrzv_toric_Prftot0,idiff)
    call dwrite(ilun_tmp,'orbrzv_toric_frnm',orbrzv_toric_frnm,blk%orbrzv_toric_frnm,idiff)
    call dwrite(ilun_tmp,'xdatsf3',xdatsf3,blk%xdatsf3,idiff)
    call dwrite(ilun_tmp,'xdatsfa',xdatsfa,blk%xdatsfa,idiff)
    call dwrite(ilun_tmp,'xdatsft',xdatsft,blk%xdatsft,idiff)
    call dwrite(ilun_tmp,'xdatsfp',xdatsfp,blk%xdatsfp,idiff)
    call dwrite(ilun_tmp,'xdatsp3',xdatsp3,blk%xdatsp3,idiff)
    call dwrite(ilun_tmp,'xpand_nptcl',xpand_nptcl,blk%xpand_nptcl,idiff)
    call dwrite(ilun_tmp,'frac_dep_lim',frac_dep_lim,blk%frac_dep_lim,idiff)
    call dwrite(ilun_tmp,'AIMP',AIMP,blk%AIMP,idiff)
    call dwrite(ilun_tmp,'XZIMP',XZIMP,blk%XZIMP,idiff)
    call dwrite(ilun_tmp,'BI_MAX_PL',BI_MAX_PL,blk%BI_MAX_PL,idiff)
    call dwrite(ilun_tmp,'BI_MAX_PR',BI_MAX_PR,blk%BI_MAX_PR,idiff)
    call dwrite(ilun_tmp,'wghta',wghta,blk%wghta,idiff)
    call dwrite(ilun_tmp,'AEP_ASCALE',AEP_ASCALE,blk%AEP_ASCALE,idiff)
    call dwrite(ilun_tmp,'AEP_USCALE',AEP_USCALE,blk%AEP_USCALE,idiff)
    call dwrite(ilun_tmp,'TAEP_FAC',TAEP_FAC,blk%TAEP_FAC,idiff)
    call dwrite(ilun_tmp,'taurip',taurip,blk%taurip,idiff)
    call dwrite(ilun_tmp,'edbfac',edbfac,blk%edbfac,idiff)
 
    call dwrite(ilun_tmp,'nlbout',nlbout,blk%nlbout,idiff)
    call dwrite(ilun_tmp,'nltest_output',nltest_output,blk%nltest_output,idiff)
    call dwrite(ilun_tmp,'xplasma_in_memory',xplasma_in_memory,blk%xplasma_in_memory,idiff)
    call dwrite(ilun_tmp,'ngyro',ngyro,blk%ngyro,idiff)
    call dwrite(ilun_tmp,'nlusf3',nlusf3,blk%nlusf3,idiff)
    call dwrite(ilun_tmp,'nlusfa',nlusfa,blk%nlusfa,idiff)
    call dwrite(ilun_tmp,'nlusft',nlusft,blk%nlusft,idiff)
    call dwrite(ilun_tmp,'nlusfp',nlusfp,blk%nlusfp,idiff)
    call dwrite(ilun_tmp,'nlusfp14',nlusfp14,blk%nlusfp14,idiff)
    call dwrite(ilun_tmp,'nptcls',nptcls,blk%nptcls,idiff)
    call dwrite(ilun_tmp,'nptclf',nptclf,blk%nptclf,idiff)
    call dwrite(ilun_tmp,'nptclh',nptclh,blk%nptclh,idiff)
    call dwrite(ilun_tmp,'NFAST_MAP',NFAST_MAP,blk%NFAST_MAP,idiff)
    call dwrite(ilun_tmp,'nltrk_dep0',nltrk_dep0,blk%nltrk_dep0,idiff)
    call dwrite(ilun_tmp,'NLDEP0_GATHER',NLDEP0_GATHER,blk%NLDEP0_GATHER,idiff)
    call dwrite(ilun_tmp,'ndep0',ndep0,blk%ndep0,idiff)
    call dwrite(ilun_tmp,'NDEP0_MAX',NDEP0_MAX,blk%NDEP0_MAX,idiff)
    call dwrite(ilun_tmp,'ndep_set_beam',ndep_set_beam,blk%ndep_set_beam,idiff)
    call dwrite(ilun_tmp,'ndep_set_ien',ndep_set_ien,blk%ndep_set_ien,idiff)
    call dwrite(ilun_tmp,'ncx0',ncx0,blk%ncx0,idiff)
    call dwrite(ilun_tmp,'nper_cx',nper_cx,blk%nper_cx,idiff)
    call dwrite(ilun_tmp,'LEV_NBIDEP',LEV_NBIDEP,blk%LEV_NBIDEP,idiff)
    call dwrite(ilun_tmp,'LEV_TTNUK',LEV_TTNUK,blk%LEV_TTNUK,idiff)
    call dwrite(ilun_tmp,'LEVMOD_HALO',LEVMOD_HALO,blk%LEVMOD_HALO,idiff)
    call dwrite(ilun_tmp,'NSPLT',NSPLT,blk%NSPLT,idiff)
    call dwrite(ilun_tmp,'NSPLT_GEO',NSPLT_GEO,blk%NSPLT_GEO,idiff)
    call dwrite(ilun_tmp,'NSPLT_KIN',NSPLT_KIN,blk%NSPLT_KIN,idiff)
    call dwrite(ilun_tmp,'NSPLT_GEN',NSPLT_GEN,blk%NSPLT_GEN,idiff)
    call dwrite(ilun_tmp,'nmimp',nmimp,blk%nmimp,idiff)
    call dwrite(ilun_tmp,'NTEST_MAX',NTEST_MAX,blk%NTEST_MAX,idiff)
    call dwrite(ilun_tmp,'orbrzv_rf_option',orbrzv_rf_option,blk%orbrzv_rf_option,idiff)
    call dwrite(ilun_tmp,'nlmcrf_on',nlmcrf_on,blk%nlmcrf_on,idiff)
    call dwrite(ilun_tmp,'nsigexc',nsigexc,blk%nsigexc,idiff)
    call dwrite(ilun_tmp,'nlbbcx',nlbbcx,blk%nlbbcx,idiff)
    call dwrite(ilun_tmp,'nbbcx_avg',nbbcx_avg,blk%nbbcx_avg,idiff)
    call dwrite(ilun_tmp,'nbbcx_bb',nbbcx_bb,blk%nbbcx_bb,idiff)
    call dwrite(ilun_tmp,'nlorbo',nlorbo,blk%nlorbo,idiff)
    call dwrite(ilun_tmp,'nlbgflr',nlbgflr,blk%nlbgflr,idiff)
    call dwrite(ilun_tmp,'sawflag',sawflag,blk%sawflag,idiff)
    call dwrite(ilun_tmp,'nlsawb',nlsawb,blk%nlsawb,idiff)
    call dwrite(ilun_tmp,'nlsawf',nlsawf,blk%nlsawf,idiff)
    call dwrite(ilun_tmp,'nkaep',nkaep,blk%nkaep,idiff)
    call dwrite(ilun_tmp,'nlfbon',nlfbon,blk%nlfbon,idiff)

    write(ilun_tmp,*) '/'
    close(unit=ilun_tmp)

  end subroutine nbi_ctrl_update_write

 
  subroutine nbi_ctrl_init_apply(blk1,blk2,ierr)
    type (nbi_ctrl_init) :: blk1
    type (nbi_ps_init) :: blk2
    integer, intent(out) :: ierr
 
    ierr = 0
 
    call nbi_init_dims(zdims)
 
    call nbi_init_adif(zadif)
    call nbi_init_atomic(zatomic)
    call nbi_init_beams(zbeams)
    call nbi_init_box(zbox)
    call nbi_init_collid(zcollid)
    call nbi_init_fishbone(zfishbone)
    call nbi_init_flr(zflr)
    call nbi_init_fpp(zfpp)
    call nbi_init_fusion(zfusion)
    call nbi_init_grid(zgrid)
    call nbi_init_impurity(zimpurity)
    call nbi_init_minority(zminority)
    call nbi_init_misc(zmisc)
    call nbi_init_num(znum)
    call nbi_init_outcon(zoutcon)
    call nbi_init_powers(zpowers)
    call nbi_init_profiles(zprofiles)
    call nbi_init_ripple(zripple)
    call nbi_init_saw(zsaw)
    call nbi_init_sys(zsys)
    call nbi_init_times(ztimes)
    call nbi_init_tube(ztube)
 
    call trans(blk1%nonlin,zsys%nonlin)
    call trans(blk1%nseed,zsys%nseed)
    call trans(blk1%nlbout,zsys%nlbout)
    call trans(blk1%lunnbx,zsys%lunnbx)
    call trans(blk1%lunres,zsys%lunres)
    call trans(blk1%mrstrt,zsys%mrstrt)
    call trans(blk1%nclass,zsys%nclass)
    call trans(blk1%only_io,zsys%only_io)
    call trans(blk1%ref_namelist,zsys%ref_namelist)
    call trans(blk1%quasi_check,zsys%quasi_check)
    call trans(blk1%nltest_output,zsys%nltest_output)
    call trans(blk1%BLK_MPI_BCAST_INT,zsys%BLK_MPI_BCAST_INT)
    call trans(blk1%BLK_MPI_BCAST_R8,zsys%BLK_MPI_BCAST_R8)
 
    call trans(blk1%nzones,zgrid%nzones)
    call trans(blk1%nzone_fb,zgrid%nzone_fb)
    call trans(blk1%nlsym2b,zgrid%nlsym2b)
    call trans(blk1%nth0,zgrid%nth0)
    call trans(blk1%xplasma_in_memory,zgrid%xplasma_in_memory)
    call trans(blk1%nznbma,zgrid%nznbma)
    call trans(blk1%nznbme,zgrid%nznbme)
    call trans(blk1%nznbma0,zgrid%nznbma0)
    call trans(blk1%nznbme0,zgrid%nznbme0)
    call trans(blk1%ngyro,zgrid%ngyro)
 
    call trans(blk1%nlusf3,zfusion%nlusf3)
    call trans(blk1%nlusfa,zfusion%nlusfa)
    call trans(blk1%nlusft,zfusion%nlusft)
    call trans(blk1%nlusfp,zfusion%nlusfp)
    call trans(blk1%nlusfp14,zfusion%nlusfp14)
    call trans(blk1%nlfatom,zfusion%nlfatom)
 
    call trans(blk1%nlbfpp,zfpp%nlbfpp)
 
    call trans(blk1%nptcls,znum%nptcls)
    call trans(blk1%nptclf,znum%nptclf)
    call trans(blk1%nptclh,znum%nptclh)
    call trans(blk1%NFAST_MAP,znum%NFAST_MAP)
    call trans(blk1%nptcl_max,znum%nptcl_max)
    call trans(blk1%nltrk_dep0,znum%nltrk_dep0)
    call trans(blk1%NLDEP0_GATHER,znum%NLDEP0_GATHER)
    call trans(blk1%ndep0,znum%ndep0)
    call trans(blk1%NDEP0_MAX,znum%NDEP0_MAX)
    call trans(blk1%ndep_set_beam,znum%ndep_set_beam)
    call trans(blk1%ndep_set_ien,znum%ndep_set_ien)
    call trans(blk1%nbbcal,znum%nbbcal)
    call trans(blk1%ncx0,znum%ncx0)
    call trans(blk1%nper_cx,znum%nper_cx)
    call trans(blk1%LEV_NBIDEP,znum%LEV_NBIDEP)
    call trans(blk1%LEV_TTNUK,znum%LEV_TTNUK)
    call trans(blk1%LEVMOD_HALO,znum%LEVMOD_HALO)
    call trans(blk1%NSPLT,znum%NSPLT)
    call trans(blk1%NSPLT_GEO,znum%NSPLT_GEO)
    call trans(blk1%NSPLT_KIN,znum%NSPLT_KIN)
    call trans(blk1%NSPLT_GEN,znum%NSPLT_GEN)
    call trans(blk1%nmimp,znum%nmimp)
    call trans(blk1%NTEST_MAX,znum%NTEST_MAX)
    call trans(blk1%NGOOCON_VPVBIN,znum%NGOOCON_VPVBIN)
    call trans(blk1%NGOOCON_EBIN,znum%NGOOCON_EBIN)
    call trans(blk1%NGOOCON_RBIN,znum%NGOOCON_RBIN)
    call trans(blk1%nchdvp,znum%nchdvp)
    call trans(blk1%orbrzv_option,znum%orbrzv_option)
    call trans(blk1%orbrzv_rf_option,znum%orbrzv_rf_option)
    call trans(blk1%nlmcrf_on,znum%nlmcrf_on)
    call trans(blk1%orbrzv_nr,znum%orbrzv_nr)
    call trans(blk1%orbrzv_nz,znum%orbrzv_nz)
 
    call trans(blk1%nlminsv,zatomic%nlminsv)
    call trans(blk1%nlebei,zatomic%nlebei)
    call trans(blk1%nsigexc,zatomic%nsigexc)
    call trans(blk1%nlbbcx,zatomic%nlbbcx)
    call trans(blk1%nbbcx_avg,zatomic%nbbcx_avg)
    call trans(blk1%nbbcx_bb,zatomic%nbbcx_bb)
    call trans(blk1%nbfallgr,zatomic%nbfallgr)
    call trans(blk1%nlbeamcx,zatomic%nlbeamcx)
    call trans(blk1%nlhvion,zatomic%nlhvion)
 
    call trans(blk1%nlbcde,zcollid%nlbcde)
    call trans(blk1%nlbcoh,zcollid%nlbcoh)
    call trans(blk1%nlbcpa,zcollid%nlbcpa)
    call trans(blk1%nlorbo,zcollid%nlorbo)
 
    call trans(blk1%nlbflr,zflr%nlbflr)
    call trans(blk1%nlfbmflr,zflr%nlfbmflr)
    call trans(blk1%nlbgflr,zflr%nlbgflr)
 
    call trans(blk1%sawflag,zsaw%sawflag)
    call trans(blk1%nlsawb,zsaw%nlsawb)
    call trans(blk1%nlsawf,zsaw%nlsawf)
    call trans(blk1%nmix_kdsaw,zsaw%nmix_kdsaw)
 
    call trans(blk1%nkaep,zadif%nkaep)
    call trans(blk1%ngradd_opt,zadif%ngradd_opt)
 
    call trans(blk1%nrip,zripple%nrip)
 
    call trans(blk1%nerngfi,zoutcon%nerngfi)
    call trans(blk1%nsdbgb,zoutcon%nsdbgb)
 
    call trans(blk1%nlfbon,zfishbone%nlfbon)
    call trans(blk1%nfbon_vpvopt,zfishbone%nfbon_vpvopt)
    call trans(blk1%nfbon_species,zfishbone%nfbon_species)
 
    call trans(blk1%nbbox,zbox%nbbox)
    call trans(blk1%nbsbox,zbox%nbsbox)
    call trans(blk1%nsample_box,zbox%nsample_box)
    call trans(blk1%nbebox,zbox%nbebox)
    call trans(blk1%nxbox,zbox%nxbox)
    call trans(blk1%nybox,zbox%nybox)
    call trans(blk1%nlbox,zbox%nlbox)
    call trans(blk1%ndepbox,zbox%ndepbox)
 
    call trans(blk1%nbtube,ztube%nbtube)
    call trans(blk1%nbstube,ztube%nbstube)
    call trans(blk1%nbetube,ztube%nbetube)
    call trans(blk1%LMIDtube,ztube%LMIDtube)
    call trans(blk1%NSEGtube,ztube%NSEGtube)
    call trans(blk1%NDEPtube,ztube%NDEPtube)
 
    call trans(blk1%nlcprb,zmisc%nlcprb)
    call trans(blk1%nlpsirz,zmisc%nlpsirz)
    call trans(blk1%nmcurb,zmisc%nmcurb)
    call trans(blk1%nlfdep,zmisc%nlfdep)
 
    call trans(blk1%xbmbnd,zgrid%xbmbnd)
 
    call trans(blk1%orbrzv_toric_Prftot0,zpowers%orbrzv_toric_Prftot0)
    call trans(blk1%orbrzv_toric_frnm,zpowers%orbrzv_toric_frnm)
 
    call trans(blk1%xdatsf3,zfusion%xdatsf3)
    call trans(blk1%xdatsfa,zfusion%xdatsfa)
    call trans(blk1%xdatsft,zfusion%xdatsft)
    call trans(blk1%xdatsfp,zfusion%xdatsfp)
    call trans(blk1%xdatsp3,zfusion%xdatsp3)
    call trans(blk1%plfhe3,zfusion%plfhe3)
    call trans(blk1%plfhe4,zfusion%plfhe4)
    call trans(blk1%plfst,zfusion%plfst)
    call trans(blk1%plfsp,zfusion%plfsp)
    call trans(blk1%plfsp14,zfusion%plfsp14)
 
    call trans(blk1%cxsplt,znum%cxsplt)
    call trans(blk1%dxbsmoo,znum%dxbsmoo)
    call trans(blk1%dxbsm_nc,znum%dxbsm_nc)
    call trans(blk1%xpand_nptcl,znum%xpand_nptcl)
    call trans(blk1%frac_depmax,znum%frac_depmax)
    call trans(blk1%frac_dep_lim,znum%frac_dep_lim)
    call trans(blk1%frac_depmin,znum%frac_depmin)
    call trans(blk1%frac_orbrr,znum%frac_orbrr)
    call trans(blk1%AIMP,znum%AIMP)
    call trans(blk1%XZIMP,znum%XZIMP)
    call trans(blk1%BI_MAX_PL,znum%BI_MAX_PL)
    call trans(blk1%BI_MAX_PR,znum%BI_MAX_PR)
    call trans(blk1%wghta,znum%wghta)
    call trans(blk1%goocon,znum%goocon)
    call trans(blk1%cxpcon,znum%cxpcon)
    call trans(blk1%fppcon,znum%fppcon)
    call trans(blk1%fdtnxy,znum%fdtnxy)
    call trans(blk1%dtn,znum%dtn)
    call trans(blk1%dt_acc,znum%dt_acc)
    call trans(blk1%orbrzv_zzerr_con,znum%orbrzv_zzerr_con)
 
    call trans(blk1%xdepmod,zatomic%xdepmod)
 
    call trans(blk1%xcfanbi,zcollid%xcfanbi)
    call trans(blk1%xdfanbi,zcollid%xdfanbi)
    call trans(blk1%xefanbi,zcollid%xefanbi)
    call trans(blk1%xcfafus,zcollid%xcfafus)
    call trans(blk1%xdfafus,zcollid%xdfafus)
    call trans(blk1%xefafus,zcollid%xefafus)
 
    call trans(blk1%gflr_min,zflr%gflr_min)
    call trans(blk1%gflr_rl,zflr%gflr_rl)
    call trans(blk1%gflr_ll,zflr%gflr_ll)
    call trans(blk1%gflr_xv,zflr%gflr_xv)
    call trans(blk1%gflr_op,zflr%gflr_op)
 
    call trans(blk1%xswfrac_allfast,zsaw%xswfrac_allfast)
    call trans(blk1%xswfrac_beam,zsaw%xswfrac_beam)
    call trans(blk1%xswfrac_fusn,zsaw%xswfrac_fusn)
    call trans(blk1%fporcelli,zsaw%fporcelli)
 
    call trans(blk1%AEP_ASCALE,zadif%AEP_ASCALE)
    call trans(blk1%AEP_USCALE,zadif%AEP_USCALE)
    call trans(blk1%TAEP_FAC,zadif%TAEP_FAC)
 
    call trans(blk1%taurip,zripple%taurip)
    call trans(blk1%asrd,zripple%asrd)
    call trans(blk1%bsrd,zripple%bsrd)
 
    call trans(blk1%erngfi,zoutcon%erngfi)
 
    call trans(blk1%fbemin,zfishbone%fbemin)
    call trans(blk1%fbemax,zfishbone%fbemax)
    call trans(blk1%fvpvmn,zfishbone%fvpvmn)
    call trans(blk1%fvpvmx,zfishbone%fvpvmx)
    call trans(blk1%fbltim,zfishbone%fbltim)
    call trans(blk1%fshper,zfishbone%fshper)
    call trans(blk1%fshwid,zfishbone%fshwid)
    call trans(blk1%tfshon,zfishbone%tfshon)
    call trans(blk1%tfshof,zfishbone%tfshof)
    call trans(blk1%xfishmin,zfishbone%xfishmin)
    call trans(blk1%xfishmax,zfishbone%xfishmax)
    call trans(blk1%fbtrap_depth,zfishbone%fbtrap_depth)
 
    call trans(blk1%xninja_box,zbox%xninja_box)
    call trans(blk1%xninjs_box,zbox%xninjs_box)
    call trans(blk1%xninj_box,zbox%xninj_box)
    call trans(blk1%xboxhw,zbox%xboxhw)
    call trans(blk1%yboxhw,zbox%yboxhw)
    call trans(blk1%xlbox1,zbox%xlbox1)
    call trans(blk1%xlbox2,zbox%xlbox2)
 
    call trans(blk1%Rtube,ztube%Rtube)
    call trans(blk1%Ytube,ztube%Ytube)
    call trans(blk1%XZETAtube,ztube%XZETAtube)
    call trans(blk1%PHItube,ztube%PHItube)
    call trans(blk1%THETAtube,ztube%THETAtube)
    call trans(blk1%XL1tube,ztube%XL1tube)
    call trans(blk1%XL2tube,ztube%XL2tube)
    call trans(blk1%RHOtube,ztube%RHOtube)
 
    call trans(blk1%edbfac,zmisc%edbfac)
 
    call trans(blk2%runid,zsys%runid)
 
    call trans(blk2%tbm1,ztimes%tbm1)
    call trans(blk2%tbm2,ztimes%tbm2)
 
    call trans(blk2%id_mcgrid,zgrid%id_mcgrid)
    call trans(blk2%nthets,zgrid%nthets)
    call trans(blk2%ebdmax,zgrid%ebdmax)
 
    call trans(blk2%ngmax,zbeams%ngmax)
    call trans(blk2%backz,zbeams%backz)
    call trans(blk2%aplasm,zbeams%aplasm)
    call trans(blk2%nbeam,zbeams%nbeam)
    call trans(blk2%xzbeama,zbeams%xzbeama)
    call trans(blk2%abeama,zbeams%abeama)
    call trans(blk2%nlco,zbeams%nlco)
    call trans(blk2%ntrace,zbeams%ntrace)
    call trans(blk2%rtcena,zbeams%rtcena)
    call trans(blk2%xlbtna,zbeams%xlbtna)
    call trans(blk2%xybsca,zbeams%xybsca)
    call trans(blk2%xbzeta,zbeams%xbzeta)
    call trans(blk2%nbshapa,zbeams%nbshapa)
    call trans(blk2%bmwidra,zbeams%bmwidra)
    call trans(blk2%bmwidza,zbeams%bmwidza)
    call trans(blk2%divra,zbeams%divra)
    call trans(blk2%divza,zbeams%divza)
    call trans(blk2%foclra,zbeams%foclra)
    call trans(blk2%foclza,zbeams%foclza)
    call trans(blk2%nbapsha,zbeams%nbapsha)
    call trans(blk2%rapedga,zbeams%rapedga)
    call trans(blk2%xzpedga,zbeams%xzpedga)
    call trans(blk2%xlbapa,zbeams%xlbapa)
    call trans(blk2%xybapa,zbeams%xybapa)
    call trans(blk2%xrapoffa,zbeams%xrapoffa)
    call trans(blk2%xzapoffa,zbeams%xzapoffa)
    call trans(blk2%nbapsh2,zbeams%nbapsh2)
    call trans(blk2%rapedg2,zbeams%rapedg2)
    call trans(blk2%xzpedg2,zbeams%xzpedg2)
    call trans(blk2%xlbapa2,zbeams%xlbapa2)
    call trans(blk2%xrapoff2,zbeams%xrapoff2)
    call trans(blk2%xzapoff2,zbeams%xzapoff2)
 
    call trans(blk2%nrhix,zimpurity%nrhix)
    call trans(blk2%xzimpx,zimpurity%xzimpx)
    call trans(blk2%aimpx,zimpurity%aimpx)
 
    call trans(blk2%nmini,zminority%nmini)
    call trans(blk2%xzmini,zminority%xzmini)
    call trans(blk2%amini,zminority%amini)
    call trans(blk2%nthrf,zminority%nthrf)
 
    call trans(blk2%einjmin,zpowers%einjmin)
    call trans(blk2%einja,zpowers%einja)
    call trans(blk2%pinja,zpowers%pinja)
    call trans(blk2%ffulla,zpowers%ffulla)
    call trans(blk2%fhalfa,zpowers%fhalfa)
 
    call trans(blk2%nlfhe3,zfusion%nlfhe3)
    call trans(blk2%nlfhe4,zfusion%nlfhe4)
    call trans(blk2%nlfst,zfusion%nlfst)
    call trans(blk2%nlfsp,zfusion%nlfsp)
    call trans(blk2%nlfsp14,zfusion%nlfsp14)
 
    call trans(blk2%nsmax,zatomic%nsmax)
    call trans(blk2%khalo,zatomic%khalo)
    call trans(blk2%dn0out,zatomic%dn0out)
    call trans(blk2%nmsigx,zatomic%nmsigx)
 
    call trans(blk2%nmdifb,zadif%nmdifb)
    call trans(blk2%nkdifb,zadif%nkdifb)
    call trans(blk2%ndifbe,zadif%ndifbe)
    call trans(blk2%fdifbe,zadif%fdifbe)
    call trans(blk2%edifbe,zadif%edifbe)
    call trans(blk2%x_qlim0,zadif%x_qlim0)
    call trans(blk2%ndifbe_2d,zadif%ndifbe_2d)
    call trans(blk2%dif2d_conv,zadif%dif2d_conv)
    call trans(blk2%dif2d_dpa,zadif%dif2d_dpa)
 
    call trans(blk2%id_ripple,zripple%id_ripple)
    call trans(blk2%id_ripple2,zripple%id_ripple2)
    call trans(blk2%ncoils,zripple%ncoils)
    call trans(blk2%ncoils_2,zripple%ncoils_2)
 
    call trans(blk2%plhgt,zmisc%plhgt)
 
    call trans(blk2%id_te,zprofiles%id_te)
    call trans(blk2%id_ti,zprofiles%id_ti)
    call trans(blk2%id_tifusn,zprofiles%id_tifusn)
    call trans(blk2%id_ne,zprofiles%id_ne)
    call trans(blk2%ng,zprofiles%ng)
    call trans(blk2%id_nh,zprofiles%id_nh)
    call trans(blk2%id_nimp,zprofiles%id_nimp)
    call trans(blk2%id_nrf,zprofiles%id_nrf)
    call trans(blk2%id_eperpmin,zprofiles%id_eperpmin)
    call trans(blk2%id_epllmin,zprofiles%id_epllmin)
    call trans(blk2%id_nfast,zprofiles%id_nfast)
    call trans(blk2%id_eperpfast,zprofiles%id_eperpfast)
    call trans(blk2%id_epllfast,zprofiles%id_epllfast)
    call trans(blk2%id_aimp,zprofiles%id_aimp)
    call trans(blk2%id_zimp,zprofiles%id_zimp)
    call trans(blk2%id_omega,zprofiles%id_omega)
    call trans(blk2%id_epot,zprofiles%id_epot)
    call trans(blk2%id_vpoh,zprofiles%id_vpoh)
    call trans(blk2%id_curt,zprofiles%id_curt)
    call trans(blk2%id_difb,zprofiles%id_difb)
    call trans(blk2%id_velb,zprofiles%id_velb)
    call trans(blk2%id_difb_bi,zprofiles%id_difb_bi)
    call trans(blk2%id_velb_bi,zprofiles%id_velb_bi)
    call trans(blk2%id_difb_fi,zprofiles%id_difb_fi)
    call trans(blk2%id_velb_fi,zprofiles%id_velb_fi)
    call trans(blk2%id_fd0,zprofiles%id_fd0)
    call trans(blk2%id_fdb,zprofiles%id_fdb)
    call trans(blk2%id_fdp,zprofiles%id_fdp)
    call trans(blk2%id_fdq,zprofiles%id_fdq)
    call trans(blk2%id_fdr,zprofiles%id_fdr)
    call trans(blk2%id_fds,zprofiles%id_fds)
    call trans(blk2%id_n0,zprofiles%id_n0)
    call trans(blk2%id_t0,zprofiles%id_t0)
    call trans(blk2%id_omeg0,zprofiles%id_omeg0)
 
    zsys%mission = saved_mission
    zsys%workpath = saved_workpath
 
    call nbi_init_dims(zdims)
 
    zdims%med2d = zadif%ndifbe_2d
    zdims%mgyro = zgrid%ngyro
    zdims%mib = zbeams%nbeam
    zdims%miboxm = zbox%nbbox
    zdims%mibs = inumfast(2)
    zdims%miefi = zoutcon%nerngfi
    zdims%mig = zbeams%ngmax
    zdims%mimpt = zimpurity%nrhix
    zdims%mimxba = zgrid%nznbma
    zdims%mimxba0 = zgrid%nznbma0
    zdims%mimxbe = zgrid%nznbme
    zdims%mimxbe0 = zgrid%nznbme0
    zdims%mis = zatomic%nsmax
    zdims%mitubem = ztube%nbtube
    zdims%mituben = maxval(ztube%nsegtube)
    zdims%mj = zgrid%nzones+ 3
    zdims%mmini = zminority%nmini
    zdims%mnkaep = zadif%nkaep
    zdims%mtheta = zgrid%nthets
 
    call nbi_set_dims(zdims,ierr)
    if(ierr.ne.0) return
 
    call nbi_alloc_init
 
    call nbi_set_adif(zadif,ierr)
    if(ierr.ne.0) return
    call nbi_set_atomic(zatomic,ierr)
    if(ierr.ne.0) return
    call nbi_add_beams(zbeams,ierr)
    if(ierr.ne.0) return
    call nbi_set_box(zbox,ierr)
    if(ierr.ne.0) return
    call nbi_set_collid(zcollid,ierr)
    if(ierr.ne.0) return
    call nbi_set_fishbone(zfishbone,ierr)
    if(ierr.ne.0) return
    call nbi_set_flr(zflr,ierr)
    if(ierr.ne.0) return
    call nbi_set_fpp(zfpp,ierr)
    if(ierr.ne.0) return
    call nbi_set_fusion(zfusion,ierr)
    if(ierr.ne.0) return
    call nbi_set_grid(zgrid,ierr)
    if(ierr.ne.0) return
    call nbi_add_impurity(zimpurity,ierr)
    if(ierr.ne.0) return
    call nbi_add_minority(zminority,ierr)
    if(ierr.ne.0) return
    call nbi_set_misc(zmisc,ierr)
    if(ierr.ne.0) return
    call nbi_set_num(znum,ierr)
    if(ierr.ne.0) return
    call nbi_set_outcon(zoutcon,ierr)
    if(ierr.ne.0) return
    call nbi_set_powers(zpowers,ierr)
    if(ierr.ne.0) return
    call nbi_set_profiles(zprofiles,ierr)
    if(ierr.ne.0) return
    call nbi_set_ripple(zripple,ierr)
    if(ierr.ne.0) return
    call nbi_set_saw(zsaw,ierr)
    if(ierr.ne.0) return
    call nbi_set_sys(zsys,ierr)
    if(ierr.ne.0) return
    call nbi_set_times(ztimes,ierr)
    if(ierr.ne.0) return
    call nbi_set_tube(ztube,ierr)
    if(ierr.ne.0) return
 
    init_called = .TRUE.
 
  end subroutine nbi_ctrl_init_apply
 
  subroutine nbi_ctrl_update_apply(blk1,blk2,ierr)
    type (nbi_ctrl_update) :: blk1
    type (nbi_ps_update) :: blk2
    integer, intent(out) :: ierr
 
    ierr = 0
 
    call nbi_get_adif(zadif)
    call nbi_get_atomic(zatomic)
    call nbi_get_collid(zcollid)
    call nbi_get_fishbone(zfishbone)
    call nbi_get_flr(zflr)
    call nbi_get_fusion(zfusion)
    call nbi_get_grid(zgrid)
    call nbi_get_minority(zminority)
    call nbi_get_misc(zmisc)
    call nbi_get_num(znum)
    call nbi_get_powers(zpowers)
    call nbi_get_profiles(zprofiles)
    call nbi_get_ripple(zripple)
    call nbi_get_saw(zsaw)
    call nbi_get_sys(zsys)
    call nbi_get_times(ztimes)
 
    call trans(blk1%nlbout,zsys%nlbout)
    call trans(blk1%nltest_output,zsys%nltest_output)
 
    call trans(blk1%xplasma_in_memory,zgrid%xplasma_in_memory)
    call trans(blk1%ngyro,zgrid%ngyro)
 
    call trans(blk1%nlusf3,zfusion%nlusf3)
    call trans(blk1%nlusfa,zfusion%nlusfa)
    call trans(blk1%nlusft,zfusion%nlusft)
    call trans(blk1%nlusfp,zfusion%nlusfp)
    call trans(blk1%nlusfp14,zfusion%nlusfp14)
 
    call trans(blk1%nptcls,znum%nptcls)
    call trans(blk1%nptclf,znum%nptclf)
    call trans(blk1%nptclh,znum%nptclh)
    call trans(blk1%NFAST_MAP,znum%NFAST_MAP)
    call trans(blk1%nltrk_dep0,znum%nltrk_dep0)
    call trans(blk1%NLDEP0_GATHER,znum%NLDEP0_GATHER)
    call trans(blk1%ndep0,znum%ndep0)
    call trans(blk1%NDEP0_MAX,znum%NDEP0_MAX)
    call trans(blk1%ndep_set_beam,znum%ndep_set_beam)
    call trans(blk1%ndep_set_ien,znum%ndep_set_ien)
    call trans(blk1%ncx0,znum%ncx0)
    call trans(blk1%nper_cx,znum%nper_cx)
    call trans(blk1%LEV_NBIDEP,znum%LEV_NBIDEP)
    call trans(blk1%LEV_TTNUK,znum%LEV_TTNUK)
    call trans(blk1%LEVMOD_HALO,znum%LEVMOD_HALO)
    call trans(blk1%NSPLT,znum%NSPLT)
    call trans(blk1%NSPLT_GEO,znum%NSPLT_GEO)
    call trans(blk1%NSPLT_KIN,znum%NSPLT_KIN)
    call trans(blk1%NSPLT_GEN,znum%NSPLT_GEN)
    call trans(blk1%nmimp,znum%nmimp)
    call trans(blk1%NTEST_MAX,znum%NTEST_MAX)
    call trans(blk1%orbrzv_rf_option,znum%orbrzv_rf_option)
    call trans(blk1%nlmcrf_on,znum%nlmcrf_on)
 
    call trans(blk1%nsigexc,zatomic%nsigexc)
    call trans(blk1%nlbbcx,zatomic%nlbbcx)
    call trans(blk1%nbbcx_avg,zatomic%nbbcx_avg)
    call trans(blk1%nbbcx_bb,zatomic%nbbcx_bb)
 
    call trans(blk1%nlorbo,zcollid%nlorbo)
 
    call trans(blk1%nlbgflr,zflr%nlbgflr)
 
    call trans(blk1%sawflag,zsaw%sawflag)
    call trans(blk1%nlsawb,zsaw%nlsawb)
    call trans(blk1%nlsawf,zsaw%nlsawf)
 
    call trans(blk1%nkaep,zadif%nkaep)
 
    call trans(blk1%nlfbon,zfishbone%nlfbon)
 
    call trans(blk1%orbrzv_toric_Prftot0,zpowers%orbrzv_toric_Prftot0)
    call trans(blk1%orbrzv_toric_frnm,zpowers%orbrzv_toric_frnm)
 
    call trans(blk1%xdatsf3,zfusion%xdatsf3)
    call trans(blk1%xdatsfa,zfusion%xdatsfa)
    call trans(blk1%xdatsft,zfusion%xdatsft)
    call trans(blk1%xdatsfp,zfusion%xdatsfp)
    call trans(blk1%xdatsp3,zfusion%xdatsp3)
 
    call trans(blk1%xpand_nptcl,znum%xpand_nptcl)
    call trans(blk1%frac_dep_lim,znum%frac_dep_lim)
    call trans(blk1%AIMP,znum%AIMP)
    call trans(blk1%XZIMP,znum%XZIMP)
    call trans(blk1%BI_MAX_PL,znum%BI_MAX_PL)
    call trans(blk1%BI_MAX_PR,znum%BI_MAX_PR)
    call trans(blk1%wghta,znum%wghta)
 
    call trans(blk1%AEP_ASCALE,zadif%AEP_ASCALE)
    call trans(blk1%AEP_USCALE,zadif%AEP_USCALE)
    call trans(blk1%TAEP_FAC,zadif%TAEP_FAC)
 
    call trans(blk1%taurip,zripple%taurip)
 
    call trans(blk1%edbfac,zmisc%edbfac)
 
    call trans(blk2%tbm1,ztimes%tbm1)
    call trans(blk2%tbm2,ztimes%tbm2)
 
    call trans(blk2%id_mcgrid,zgrid%id_mcgrid)
 
    call trans(blk2%nthrf,zminority%nthrf)
 
    call trans(blk2%einja,zpowers%einja)
    call trans(blk2%pinja,zpowers%pinja)
    call trans(blk2%ffulla,zpowers%ffulla)
    call trans(blk2%fhalfa,zpowers%fhalfa)
 
    call trans(blk2%dn0out,zatomic%dn0out)
 
    call trans(blk2%nmdifb,zadif%nmdifb)
    call trans(blk2%fdifbe,zadif%fdifbe)
    call trans(blk2%x_qlim0,zadif%x_qlim0)
 
    call trans(blk2%id_ripple,zripple%id_ripple)
    call trans(blk2%id_ripple2,zripple%id_ripple2)
 
    call trans(blk2%plhgt,zmisc%plhgt)
 
    call trans(blk2%id_te,zprofiles%id_te)
    call trans(blk2%id_ti,zprofiles%id_ti)
    call trans(blk2%id_tifusn,zprofiles%id_tifusn)
    call trans(blk2%id_ne,zprofiles%id_ne)
    call trans(blk2%ng,zprofiles%ng)
    call trans(blk2%id_nh,zprofiles%id_nh)
    call trans(blk2%id_nimp,zprofiles%id_nimp)
    call trans(blk2%id_nrf,zprofiles%id_nrf)
    call trans(blk2%id_eperpmin,zprofiles%id_eperpmin)
    call trans(blk2%id_epllmin,zprofiles%id_epllmin)
    call trans(blk2%id_nfast,zprofiles%id_nfast)
    call trans(blk2%id_eperpfast,zprofiles%id_eperpfast)
    call trans(blk2%id_epllfast,zprofiles%id_epllfast)
    call trans(blk2%id_aimp,zprofiles%id_aimp)
    call trans(blk2%id_zimp,zprofiles%id_zimp)
    call trans(blk2%id_omega,zprofiles%id_omega)
    call trans(blk2%id_epot,zprofiles%id_epot)
    call trans(blk2%id_vpoh,zprofiles%id_vpoh)
    call trans(blk2%id_curt,zprofiles%id_curt)
    call trans(blk2%id_difb,zprofiles%id_difb)
    call trans(blk2%id_velb,zprofiles%id_velb)
    call trans(blk2%id_difb_bi,zprofiles%id_difb_bi)
    call trans(blk2%id_velb_bi,zprofiles%id_velb_bi)
    call trans(blk2%id_difb_fi,zprofiles%id_difb_fi)
    call trans(blk2%id_velb_fi,zprofiles%id_velb_fi)
    call trans(blk2%id_fd0,zprofiles%id_fd0)
    call trans(blk2%id_fdb,zprofiles%id_fdb)
    call trans(blk2%id_fdp,zprofiles%id_fdp)
    call trans(blk2%id_fdq,zprofiles%id_fdq)
    call trans(blk2%id_fdr,zprofiles%id_fdr)
    call trans(blk2%id_fds,zprofiles%id_fds)
    call trans(blk2%id_n0,zprofiles%id_n0)
    call trans(blk2%id_t0,zprofiles%id_t0)
    call trans(blk2%id_omeg0,zprofiles%id_omeg0)
 
    call nbi_update_adif(zadif)
    call nbi_update_atomic(zatomic)
    call nbi_update_collid(zcollid)
    call nbi_update_fishbone(zfishbone)
    call nbi_update_flr(zflr)
    call nbi_update_fusion(zfusion)
    call nbi_update_grid(zgrid)
    call nbi_update_minority(zminority)
    call nbi_update_misc(zmisc)
    call nbi_update_num(znum)
    call nbi_update_powers(zpowers)
    call nbi_update_profiles(zprofiles)
    call nbi_update_ripple(zripple)
    call nbi_update_saw(zsaw)
    call nbi_update_sys(zsys)
    call nbi_update_times(ztimes)
 
  end subroutine nbi_ctrl_update_apply
 
  ! PUBLIC routines to set CHARACTER control variables
 
  subroutine nubeam_set_mission(cstr,ierr)
    character*(*) :: cstr
    integer :: ierr
 
    if(init_called) then
      ierr = 1
      return
    else
      ierr = 0
    endif
 
    saved_mission = cstr
 
  end subroutine nubeam_set_mission
 
  subroutine nubeam_set_workpath(cstr,ierr)
    character*(*) :: cstr
    integer :: ierr
 
    if(init_called) then
      ierr = 1
      return
    else
      ierr = 0
    endif
 
    saved_workpath = cstr
 
  end subroutine nubeam_set_workpath

  ! PUBLIC routines (written verbatim from nubeam/nbigen.py)

  subroutine nubeam_ctrl_init(ss,nbi_ctrl,ierr)

    ! initialize NUBEAM controls from plasma state & from nbi_ctrl_init data
    ! MPI runs: only myidd.eq.0 thread needs to call this.

    type (plasma_state) :: ss
    type (nbi_ctrl_init) :: nbi_ctrl
    integer, intent(out) :: ierr

    ! local:

    type (nbi_ps_init) :: psinit

    integer :: myidd
    real(fp) :: zcptim_loc1,zcptim_loc2
    integer :: ilun_msg = 6
    !-------------------------------------

    call nbi_get_myid(myidd)

    if(myidd.eq.0) then

       call cpu_time(zcptim_loc1)

       ! transfer state data (call hand maintained routine)
       call nubeam_ctrl_psinit(ss,psinit,ierr)
       if(ierr.ne.0) return

       ! initialize nubeam internals (nbi_com_mod) -- allocate arrays in nubeam
       call nbi_ctrl_init_apply(nbi_ctrl, psinit, ierr)
       if(ierr.ne.0) return

       call nbi_datchk(ierr)
       if(ierr.ne.0) return

       call cpu_time(zcptim_loc2)
       write(ilun_msg,'(1x,a,1x,1pe11.4)') &
            "cpu time (sec) in nubeam_ctrl_init: ", zcptim_loc2-zcptim_loc1
    endif

  end subroutine nubeam_ctrl_init

  subroutine nubeam_ctrl_step(ss_in,ss_out,nbi_ctrl,ierr)

    ! update NUBEAM controls from plasma state & from nbi_ctrl_init data
    ! and execute a NUBEAM step

    type (plasma_state) :: ss_in,ss_out  ! input, output states

    type (nbi_ctrl_update) :: nbi_ctrl
    integer, intent(out) :: ierr
    ! local:

    type (nbi_ps_update) :: psupdate

    real(fp) :: zcptime,ztim1,ztim2
    integer :: myidd
    integer :: imem

    real(fp) :: zcptim_loc
    !-------------------------------------

    call cpu_time(zcptim_loc)

    call nbi_get_myid(myidd)

    if (myidd.eq.0) then
       call cpu_time(ztim1)

       if(nbi_ctrl%xplasma_in_memory.ne.idflt_nbips) then
          imem = nbi_ctrl%xplasma_in_memory
       else
          call nbi_get_grid(zgrid)
          imem = zgrid%xplasma_in_memory
       endif

       ! transfer state data (call hand maintained routine)
       call nubeam_ctrl_psupdate(imem, ss_in,psupdate, ierr)
       if(ierr.ne.0) return

       call nbi_ctrl_update_apply(nbi_ctrl, psupdate, ierr)
       if(ierr.ne.0) return

       call cpu_time(ztim2)
       zcptime = ztim2 - ztim1
    endif

    call nubeam_step(ss_out,zcptime,ierr)

    if (myidd.eq.0) then
       if(imem.lt.0) then
          call nubeam_ctrl_psupdate_cleanup(ierr)
       endif
    endif

    call nubeam_mpi_timer(zcptim_loc)

  end subroutine nubeam_ctrl_step

  subroutine nubeam_ctrl_step_child(ierr)

    ! execute a NUBEAM step -- MPI child process

    ! (myidd.gt.0) CPU threads can call this instead of nubeam_ctrl_step
    ! for a simplified argument list

    integer, intent(out) :: ierr
    !---------------------------------
    integer :: myidd

    real(fp) :: zcptim_loc
    !-------------------------------------

    call cpu_time(zcptim_loc)

    call nbi_get_myid(myidd)

    if (myidd.eq.0) then
       ierr=1
       call errmsg_exit(' ?? nubeam_ctrl_step_child: myidd=0 call to nubeam_ctrl_step_child')
       return
    endif
    call nubeam_step_child(ierr)

    call nubeam_mpi_timer(zcptim_loc)

  end subroutine nubeam_ctrl_step_child

  subroutine nubeam_ctrl_nostep(ss_in,ierr)

    ! use a plasma_state object to complete a NUBEAM internal state,
    ! without performing a full NUBEAM time step.
    ! MPI runs: only myidd.eq.0 thread needs to call this.

    type (plasma_state) :: ss_in  ! input plasma state
    integer, intent(out) :: ierr

    ! local:

    type (nbi_ctrl_update) :: nbi_ctrl

    type (nbi_ps_update) :: psupdate

    real(fp) :: zcptime,ztim1,ztim2

    integer :: myidd
    real(fp) :: zcptim_loc
    !-------------------------------------

    call nbi_get_myid(myidd)

    if (myidd.eq.0) then
       call cpu_time(ztim1)

       ! transfer state data (call hand maintained routine)
       call nubeam_ctrl_psupdate(-1, ss_in, psupdate, ierr)
       if(ierr.ne.0) return

       call nbi_ctrl_update_dflt(nbi_ctrl)

       call nbi_ctrl_update_apply(nbi_ctrl, psupdate, ierr)
       if(ierr.ne.0) return

       call nbi_interp_profiles(ierr)
       if(ierr.ne.0) return

       call nbstart_init(ierr)
       if(ierr.ne.0) return

       ! because nubeam_ctrl_psupdate(-1, ...) was called, need this also:
       call nubeam_ctrl_psupdate_cleanup(ierr)
       if(ierr.ne.0) return

       call cpu_time(ztim2)
       zcptime = ztim2 - ztim1
    endif

  end subroutine nubeam_ctrl_nostep

  ! PRIVATE routine to count number of fast species

  integer function inumfast(imin)

    integer, intent(in) :: imin   ! minimum result allowed

    !----------------------
    integer :: isum
    integer :: insb,ii,jj,imatch,iai,izi,ia(200),iz(200)
    !----------------------

    isum = zminority%nmini

    if(zfusion%nlfhe3) isum = isum + 1
    if(zfusion%nlfhe4) isum = isum + 1
    if(zfusion%nlfst) isum = isum + 1
    if(zfusion%nlfsp) isum = isum + 1
    if(zfusion%nlfsp14) isum = isum + 1

    insb = 0
    do ii=1,zbeams%nbeam
       izi = zbeams%xzbeama(ii) + 0.1_fp
       if(izi.eq.0) exit

       iai = zbeams%abeama(ii) + 0.1_fp
       if(iai.eq.0) exit

       imatch = 0
       do jj=1,insb
          if((izi.eq.iz(jj)).and.(iai.eq.ia(jj))) then
             imatch=jj
             exit
          endif
       enddo

       if(imatch.eq.0) then
          insb = insb + 1
          iz(insb) = izi
          ia(insb) = iai
       endif
    enddo

    isum = isum + insb

    isum = max(isum,imin)
    inumfast = isum

  end function inumfast

  ! PRIVATE data transfer routines

  subroutine trans_I0(iin,iout)
    integer :: iin,iout

    if(iin.ne.idflt_nbips) iout=iin

  end subroutine trans_I0

  subroutine trans_I1(iin,iout)
    integer, dimension(:) :: iin,iout

    integer :: ii

    do ii=1,size(iin)
       if(iin(ii).ne.idflt_nbips) iout(ii)=iin(ii)
    enddo

  end subroutine trans_I1

  subroutine trans_I2(iin,iout)
    integer, dimension(:,:) :: iin,iout

    integer :: ii,jj

    do jj=1,size(iin,2)
       do ii=1,size(iin,1)
         if(iin(ii,jj).ne.idflt_nbips) iout(ii,jj)=iin(ii,jj)
       enddo
    enddo

  end subroutine trans_I2

  subroutine trans_L0(iin,iout)
    integer :: iin
    logical :: iout

    if(iin.ne.idflt_nbips) iout= (iin.NE.0)

  end subroutine trans_L0

  subroutine trans_L1(iin,iout)
    integer, dimension(:) :: iin
    logical, dimension(:) :: iout

    integer :: ii

    do ii=1,size(iin)
       if(iin(ii).ne.idflt_nbips) iout(ii)= (iin(ii).NE.0)
    enddo

  end subroutine trans_L1

  subroutine trans_D0(zin,zout)
    real(fp) :: zin,zout

    if(zin.ne.r8dflt_nbips) zout=zin

  end subroutine trans_D0

  subroutine trans_D1(zin,zout)
    real(fp), dimension(:) :: zin,zout

    integer :: ii

    do ii=1,size(zin)
       if(zin(ii).ne.r8dflt_nbips) zout(ii)=zin(ii)
    enddo

  end subroutine trans_D1

  subroutine trans_D2(zin,zout)
    real(fp), dimension(:,:) :: zin,zout

    integer :: ii,jj

    do jj=1,size(zin,2)
       do ii=1,size(zin,1)
          if(zin(ii,jj).ne.r8dflt_nbips) zout(ii,jj)=zin(ii,jj)
       enddo
    enddo

  end subroutine trans_D2

  subroutine trans_C0(cin,cout)
    character*(*) :: cin,cout

    cout=cin

  end subroutine trans_C0

  ! PRIVATE data file write routines -- only right non-default values

  subroutine dwrite_I0(ilun,name,idflt,ihave,idiff)
    integer :: ilun         ! unit number
    character*(*) :: name   ! name of variable
    integer :: idflt        ! default value
    integer :: ihave        ! actual value

    integer, intent(out) :: idiff   ! =0: nothing written; >0: writes were done

    character*12 :: zival
    !----------------

    idiff = 0
    if((ihave.ne.idflt_nbips).and.(ihave.ne.idflt)) then
       idiff = 1
       call itrans(ihave,zival)
       write(ilun,*) ' '//trim(name)//' = '//trim(zival)
    endif

  end subroutine dwrite_I0

  subroutine dwrite_I1(ilun,name,idflt,ihave,idiff)
    integer :: ilun         ! unit number
    character*(*) :: name   ! name of variable
    integer, dimension(:) :: idflt        ! default value
    integer, dimension(:) :: ihave        ! actual value

    integer, intent(out) :: idiff   ! =0: nothing written; >0: writes were done

    integer :: ii
    character*12 :: zival,zindx
    !----------------

    idiff = 0

    do ii=1,size(ihave)
       if((ihave(ii).ne.idflt_nbips).and.(ihave(ii).ne.idflt(ii))) then
          idiff=idiff + 1
          call itrans(ii,zindx)
          call itrans(ihave(ii),zival)
          write(ilun,*) ' '//trim(name)//'('//trim(zindx)//') = '//trim(zival)
       endif
    enddo

  end subroutine dwrite_I1

  subroutine dwrite_I2(ilun,name,idflt,ihave,idiff)
    integer :: ilun         ! unit number
    character*(*) :: name   ! name of variable
    integer, dimension(:,:) :: idflt        ! default value
    integer, dimension(:,:) :: ihave        ! actual value

    integer, intent(out) :: idiff   ! =0: nothing written; >0: writes were done

    integer :: ii,jj
    character*12 :: zival,zindx,zjndx
    !----------------

    idiff = 0

    do jj=1,size(ihave,2)
       do ii=1,size(ihave,1)
          if((ihave(ii,jj).ne.idflt_nbips).and.(ihave(ii,jj).ne.idflt(ii,jj))) then
             idiff=idiff + 1
             call itrans(ii,zindx)
             call itrans(jj,zjndx)
             call itrans(ihave(ii,jj),zival)
             write(ilun,*) ' '//trim(name)//'('//trim(zindx)//','// &
                  trim(zjndx)//') = '//trim(zival)
          endif
       enddo
    enddo

  end subroutine dwrite_I2

  subroutine dwrite_D0(ilun,name,idflt,ihave,idiff)
    integer :: ilun         ! unit number
    character*(*) :: name   ! name of variable
    real(fp) :: idflt         ! default value
    real(fp) :: ihave         ! actual value

    integer, intent(out) :: idiff   ! =0: nothing written; >0: writes were done

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

    idiff = 0

    if((ihave.ne.r8dflt_nbips).and.(ihave.ne.idflt)) then
       if(abs(ihave-idflt).gt.1.0e-12_fp*max(abs(ihave),abs(idflt))) then
          idiff=idiff + 1
          write(ilun,1001) ' '//trim(name)//' = ',ihave
       endif
    endif
1001 format(1x,a,1pe22.15)

  end subroutine dwrite_D0

  subroutine dwrite_D1(ilun,name,idflt,ihave,idiff)
    integer :: ilun         ! unit number
    character*(*) :: name   ! name of variable
    real(fp), dimension(:) :: idflt        ! default value
    real(fp), dimension(:) :: ihave        ! actual value

    integer, intent(out) :: idiff   ! =0: nothing written; >0: writes were done

    integer :: ii
    character*12 :: zindx
    !----------------

    idiff = 0

    do ii=1,size(ihave)
       if((ihave(ii).ne.r8dflt_nbips).and.(ihave(ii).ne.idflt(ii))) then
          if(abs(ihave(ii)-idflt(ii)).gt.1.0e-12_fp*max(abs(ihave(ii)),abs(idflt(ii)))) then
             idiff=idiff + 1
             call itrans(ii,zindx)
             write(ilun,1001) ' '//trim(name)//'('//trim(zindx)//') = ',ihave(ii)
          endif
       endif
    enddo
1001 format(1x,a,1pe22.15)

  end subroutine dwrite_D1

  subroutine dwrite_D2(ilun,name,idflt,ihave,idiff)
    integer :: ilun         ! unit number
    character*(*) :: name   ! name of variable
    real(fp), dimension(:,:) :: idflt        ! default value
    real(fp), dimension(:,:) :: ihave        ! actual value

    integer, intent(out) :: idiff   ! =0: nothing written; >0: writes were done

    integer :: ii,jj
    character*12 :: zindx,zjndx
    !----------------

    idiff = 0

    do jj=1,size(ihave,2)
       do ii=1,size(ihave,1)
          if((ihave(ii,jj).ne.r8dflt_nbips).and.(ihave(ii,jj).ne.idflt(ii,jj))) then
             if(abs(ihave(ii,jj)-idflt(ii,jj)).gt.1.0e-12_fp*max(abs(ihave(ii,jj)),abs(idflt(ii,jj)))) then
                idiff=idiff + 1
                call itrans(ii,zindx)
                call itrans(jj,zjndx)
                write(ilun,1001) ' '//trim(name)//'('//trim(zindx)//','// &
                     trim(zjndx)//') = ',ihave(ii,jj)
             endif
          endif
       enddo
    enddo
1001 format(1x,a,1pe22.15)

  end subroutine dwrite_D2

  subroutine dwrite_C0(ilun,name,idflt,ihave,idiff)
    integer :: ilun         ! unit number
    character*(*) :: name   ! name of variable

    character*(*) :: idflt        ! default value
    character*(*) :: ihave        ! actual value

    integer, intent(out) :: idiff   ! =0: nothing written; >0: writes were done

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

    idiff = 0

    if(idflt.ne.ihave) then
       idiff = 1
       write(ilun,*) ' '//trim(name)//' = "'//trim(ihave)//'"'
    endif

  end subroutine dwrite_C0

  subroutine itrans(ival,jchv)
    ! write integer & left justify in output argument

    integer :: ival
    character*12 jchv

    character*12 ichv
    integer :: ic,id

    ichv = ' '
    jchv = ' '
    write(ichv,'(I12)') ival

    id=0
    do ic=1,12
       if(ichv(ic:ic).ne.' ') then
          id=id+1
          jchv(id:id)=ichv(ic:ic)
       endif
    enddo

  end subroutine itrans

  ! PUBLIC:

  integer function inbi_logical(ilogical)

    logical, intent(in) :: ilogical

    if(ilogical) then
       inbi_logical = 1
    else
       inbi_logical = 0
    endif

  end function inbi_logical

end module nbi_pstypes
