subroutine gfbm_set_rzmoms(ilsym,iasize,iobj)

  ! from an XPLASMA object fetch MHD equilibrium fourier moments
  ! xitr(...) should be set and rmom0, rmom, ymom, rmc, and ymc should be
  ! allocated: i.e. prior call to gfbm_set_trgrid in get_fbm.f90 is required!

  use iso_c_binding, only: fp => c_double
  use get_fbm_module
  use xplasma_definitions
  implicit NONE

  logical :: ilsym   ! .TRUE. for up-down symmetric MHD equilibrium
  !  (generally in get_fbm this will mean an up-down symmetric f.i.
  !  distribution function dataset as well).

  integer :: iasize  ! size of integer array that transfers into an xplasma ptr
  integer :: iobj(iasize)  ! xplasma ptr in integer array form

  !-------------------------------
  type (xplasma), pointer :: s

  type :: interp_object

     type (xplasma), pointer :: s_xpobj => NULL()

  end type interp_object

  type (interp_object) :: xobj

  integer :: inorm = 0
  integer :: ierr

  integer :: im
  !-------------------------------
  ! some rudimentary checks...

  ierr = 0
  if(.not.allocated(xitr)) then
     write(0,*) ' ?gfbm_set_rzmoms: xitr not allocated, call gfbm_set_trgrid first!'
     ierr = ierr + 1
  endif

  if(.not.allocated(rmom)) then
     write(0,*) ' ?gfbm_set_rzmoms: rmom not allocated, call gfbm_set_trgrid first!'
     ierr = ierr + 1
  endif

  if(ierr.gt.0) call bad_exit

  !-------------------------------
  !  then: acquire the xplasma pointer from the passed integer data

  xobj = transfer(iobj,xobj)
  s => xobj%s_xpobj

  !-------------------------------
  !  get the moments; convert to cm

  if(ilsym) then

     call xpmom_get1(s,xitr(lcentr:lcentr+nzones,1),inorm,0,ierr, &
          rcmom=rmom0(lcentr:lcentr+nzones,1))
     if(ierr.eq.0) then
        call xpmom_get1(s,xitr(lcentr:lcentr+nzones-1,2),inorm,0,ierr, &
             rcmom=rmom0(lcentr:lcentr+nzones-1,2))
     endif

     if(ierr.ne.0) then
        call xplasma_error(s,ierr,0)
        call errmsg_exit(' ?gfbm_set_rzmoms: RMOM0 fetch error.')
     else
        rmom0 = rmom0*100.0_fp  ! m -> cm
     endif

     do im=1,nmom

        call xpmom_get1(s,xitr(lcentr:lcentr+nzones,1),inorm,im,ierr, &
             rcmom=rmom(im,lcentr:lcentr+nzones,1), &
             zsmom=ymom(im,lcentr:lcentr+nzones,1))
        if(ierr.eq.0) then
           call xpmom_get1(s,xitr(lcentr:lcentr+nzones-1,2),inorm,im,ierr, &
                rcmom=rmom(im,lcentr:lcentr+nzones-1,2), &
                zsmom=ymom(im,lcentr:lcentr+nzones-1,2))
        endif

        if(ierr.ne.0) exit
     enddo

     if(ierr.ne.0) then
        call xplasma_error(s,ierr,0)
        call errmsg_exit(' ?gfbm_set_rzmoms: RMOM/YMOM fetch error.')
     else
        rmom = rmom*100.0_fp  ! m -> cm
        ymom = ymom*100.0_fp  ! m -> cm
     endif

  else

     do im=0,nmom

        call xpmom_get1(s,xitr(lcentr:lcentr+nzones,1),inorm,im,ierr, &
             rcmom=rmc(lcentr:lcentr+nzones,im,1), &
             rsmom=rmc(lcentr:lcentr+nzones,im,2), &
             zcmom=ymc(lcentr:lcentr+nzones,im,1), &
             zsmom=ymc(lcentr:lcentr+nzones,im,2))

        if(ierr.ne.0) exit

     enddo

     if(ierr.ne.0) then
        call xplasma_error(s,ierr,0)
        write(0,*) ' RMC/YMC fetch failed on moment #',im
        call errmsg_exit(' ?gfbm_set_rzmoms: RMC/YMC fetch error.')
     else
        rmc = rmc*100.0_fp  ! m -> cm
        ymc = ymc*100.0_fp  ! m -> cm
     endif

  endif

end subroutine gfbm_set_rzmoms
