!
!  these are a set of hand maintained routines to define/write/read the
!  portion of the nbi_state file that is not automatically generated
!

subroutine nbi_state_userdef(icdf)
  use nbi_random
  use rngdef
  use ezcdf
  use nbi_idhdr

  implicit none

  integer icdf  ! netcdf file id

  integer idm(3)

  idm=0
  call cdfdefvar(icdf, 'nbi_srng_ptr',   idm, 'int')
  call cdfdefvar(icdf, 'nbi_sgauss_ptr', idm, 'int')
  call cdfdefvar(icdf, 'nbi_state_indx', idm, 'int')
 
  idm = (/rng_k, 0, 0/)
  call cdfdefvar(icdf, 'nbi_state_arr', idm, 'R8')
 
  idm = (/rng_batch_size, 0, 0/)
  call cdfdefvar(icdf, 'nbi_srng', idm, 'R8')
  call cdfdefvar(icdf, 'nbi_sgauss', idm, 'R8')

  idm=0
  call cdfdefvar(icdf, 'nbi_numhdr', idm, 'int')

  if(nbi_numhdr.gt.0) then
     idm = (/ len(hdr1), nbi_numhdr, 0 /)
     call cdfdefvar(icdf, 'nbi_xnames', idm, 'char')
  endif

end subroutine nbi_state_userdef

subroutine nbi_state_userout(icdf)
  use nbi_random
  use rngdef
  use ezcdf
  use nbi_idhdr

  implicit none

  integer icdf  ! netcdf file id

  call cdfputvar(icdf, 'nbi_srng_ptr', nbi_srng_ptr)
  call cdfputvar(icdf, 'nbi_sgauss_ptr', nbi_sgauss_ptr)
  call cdfputvar(icdf, 'nbi_state_indx', rng_curstate%index)
  call cdfputvar(icdf, 'nbi_state_arr', rng_curstate%array(0:rng_k-1))
  call cdfputvar(icdf, 'nbi_srng', nbi_srng)
  call cdfputvar(icdf, 'nbi_sgauss', nbi_sgauss)

  call cdfputvar(icdf, 'nbi_numhdr', nbi_numhdr)
  if(nbi_numhdr.gt.0) then
     call cdfputvar(icdf, 'nbi_xnames', nbi_xnames)
  endif

end subroutine nbi_state_userout

subroutine nbi_state_useread(icdf)
  use nbi_random
  use rngdef
  use ezcdf
  use nbi_idhdr
  implicit none

  integer icdf  ! netcdf file id

  real, dimension(:), allocatable :: r4buf

  integer idm(3)
  character*10 zvartype

  call cdfinqvar(icdf, 'nbi_srng_ptr', idm, zvartype)
  call cdfgetvar(icdf, 'nbi_srng_ptr', nbi_srng_ptr)

  call cdfinqvar(icdf, 'nbi_sgauss_ptr', idm, zvartype)
  call cdfgetvar(icdf, 'nbi_sgauss_ptr', nbi_sgauss_ptr)

  call cdfinqvar(icdf, 'nbi_state_indx', idm, zvartype)
  call cdfgetvar(icdf, 'nbi_state_indx', rng_curstate%index)

  call cdfinqvar(icdf, 'nbi_state_arr', idm, zvartype)
  call cdfgetvar(icdf, 'nbi_state_arr', rng_curstate%array(0:rng_k-1))

  call cdfinqvar(icdf, 'nbi_srng', idm, zvartype)
  if(zvartype.eq.'R4') then
     allocate(r4buf(idm(1)))
     call cdfgetvar(icdf, 'nbi_srng', r4buf)
     nbi_srng(1:idm(1))=r4buf
     deallocate(r4buf)
  else
     call cdfgetvar(icdf, 'nbi_srng', nbi_srng)
  endif

  call cdfinqvar(icdf, 'nbi_sgauss', idm, zvartype)
  if(zvartype.eq.'R4') then
     allocate(r4buf(idm(1)))
     call cdfgetvar(icdf, 'nbi_sgauss', r4buf)
     nbi_sgauss(1:idm(1))=r4buf
     deallocate(r4buf)
  else
     call cdfgetvar(icdf, 'nbi_sgauss', nbi_sgauss)
  endif
 
  call cdfinqvar(icdf, 'nbi_numhdr', idm, zvartype)
  call cdfgetvar(icdf, 'nbi_numhdr', nbi_numhdr)

  if(nbi_numhdr.gt.0) then
     if(allocated(nbi_xnames)) deallocate(nbi_xnames)
     allocate(nbi_xnames(nbi_numhdr))

     call cdfinqvar(icdf, 'nbi_xnames', idm, zvartype)
     call cdfgetvar(icdf, 'nbi_xnames', nbi_xnames)
  endif

end subroutine nbi_state_useread
