subroutine nbi_save_id0(idname,id)
  !  a single id

  character*(*), intent(in) :: idname
  integer, intent(in) :: id

  integer ida(1,1,1)

  ida = id
  call nbi_save_id(idname,ida,1,1,1)
end subroutine nbi_save_id0

subroutine nbi_save_id1(idname,id,idim1)
  !  a vector of ids

  character*(*), intent(in) :: idname
  integer, intent(in) :: idim1
  integer, intent(in) :: id(idim1)

  integer ida(idim1,1,1)

  ida(1:idim1,1,1)=id(1:idim1)

  call nbi_save_id(idname,ida,idim1,1,1)
end subroutine nbi_save_id1

subroutine nbi_save_id2(idname,id,idim1,idim2)
  !  a vector of ids

  character*(*), intent(in) :: idname
  integer, intent(in) :: idim1,idim2
  integer, intent(in) :: id(idim1,idim2)

  integer ida(idim1,idim2,1)

  ida(1:idim1,1:idim2,1)=id(1:idim1,1:idim2)

  call nbi_save_id(idname,ida,idim1,idim2,1)
end subroutine nbi_save_id2

subroutine nbi_save_id(idname,id,idim1,idim2,idim3)

  !  save xplasma ids -- translated to name -- in ascii file
  !  3d array of ids

  use nbi_com
  use nbi_idhdr

  implicit none

  character*(*), intent(in) :: idname   ! id name string
  integer, intent(in) :: idim1,idim2,idim3   ! dimensioning information
  integer, intent(in) :: id(idim1,idim2,idim3)  ! the id values

  !--------------------------------------
  integer i,j,k
  !--------------------------------------

  hdr1 = 'name: '//idname
  call nbi_id_sendstr

  hdr1 = ' '
  write(hdr1,'(3(i5,1x))') idim1,idim2,idim3
  call nbi_id_sendstr

  do k=1,idim3
     do j=1,idim2
        do i=1,idim1
           call eq_get_fname(id(i,j,k),hdr1)
           if(hdr1.eq.' ') hdr1 = '(none)'
           call nbi_id_sendstr
        enddo
     enddo
  enddo

  contains
    subroutine nbi_id_sendstr

      nbi_curhdr = nbi_curhdr + 1
      if(nbi_curhdr.gt.nbi_numhdr) call hdr_expand
      nbi_xnames(nbi_curhdr) = hdr1

    end subroutine nbi_id_sendstr

end subroutine nbi_save_id


subroutine nbi_restore_id0(idname,id)
  !  a single id

  character*(*), intent(in) :: idname
  integer, intent(out) :: id

  integer ida(1,1,1)

  call nbi_restore_id(idname,ida,1,1,1)
  id = ida(1,1,1)

end subroutine nbi_restore_id0

subroutine nbi_restore_id1(idname,id,idim1)
  !  a vector of ids

  character*(*), intent(in) :: idname
  integer, intent(in) :: idim1
  integer, intent(out) :: id(idim1)

  integer ida(idim1,1,1)

  call nbi_restore_id(idname,ida,idim1,1,1)
  id(1:idim1)=ida(1:idim1,1,1)

end subroutine nbi_restore_id1

subroutine nbi_restore_id2(idname,id,idim1,idim2)
  !  a vector of ids

  character*(*), intent(in) :: idname
  integer, intent(in) :: idim1,idim2
  integer, intent(out) :: id(idim1,idim2)

  integer ida(idim1,idim2,1)

  call nbi_restore_id(idname,ida,idim1,idim2,1)
  id(1:idim1,1:idim2)=ida(1:idim1,1:idim2,1)

end subroutine nbi_restore_id2

subroutine nbi_restore_id(idname,id,idim1,idim2,idim3)

  !  restore xplasma ids from ascii file -- translate to integer
  !  3d array of ids

  use nbi_com
  use nbi_idhdr

  implicit none

  character*(*), intent(in) :: idname   ! id name string
  integer, intent(in) :: idim1,idim2,idim3   ! dimensioning information
  integer, intent(out) :: id(idim1,idim2,idim3)  ! the id values

  !--------------------------------------
  integer i,j,k
  character*30 hsave
  !--------------------------------------

  htest = 'name: '//idname
  hsave = htest

  nbi_curhdr = nbi_curhdr + 1
  hdr1 = nbi_xnames(nbi_curhdr)
  
  if(hdr1.ne.htest) then
     write(nonlin,*) ' ?nbi_restore_id:  expected hdr = "',htest,'";'
     write(nonlin,*) '                   found instead: "',hdr1,'".'
     write(nonlin,*) ' ?state file from incompatible version of code.'
  endif

  htest = ' '
  write(htest,'(3(i5,1x))') idim1,idim2,idim3

  nbi_curhdr = nbi_curhdr + 1
  hdr1 = nbi_xnames(nbi_curhdr)
  
  if(hdr1.ne.htest) then
     write(nonlin,*) ' while examining ids for: ',hsave
     write(nonlin,*) ' ?nbi_restore_id:  expected dims= "',htest,'";'
     write(nonlin,*) '                   found instead: "',hdr1,'".'
     write(nonlin,*) ' ?state file from incompatible version of code.'
  endif

  do k=1,idim3
     do j=1,idim2
        do i=1,idim1

           nbi_curhdr = nbi_curhdr + 1
           hdr1 = nbi_xnames(nbi_curhdr)

           if(hdr1.eq.'(none)') then
              id(i,j,k)=0
           else
              call eq_gfnum(hdr1,id(i,j,k))
              if(id(i,j,k).eq.0) then
                 write(nonlin,*) ' %warning: nbi_restore_id:'
                 write(nonlin,*) '  xplasma hdr1 not found: ',hdr1
              endif
           endif

        enddo
     enddo
  enddo

end subroutine nbi_restore_id
