subroutine trxpl_eqmoms(ieq)
 
  implicit NONE
 
  integer,intent(in) :: ieq   ! equilibrium moments select flag:
  !
  !  ieq=1 -- boundary only
  !  ieq=2 -- profiles only
  !  ieq=3 -- both
  !
  !  boundary -- write Fourier moments of boundary
  !    in asymmetric form, but for updown symmetry the R sin moments
  !    and Z cos moments are strictly zero EXCEPT that Z0 is incremented
  !    by the "plasma height" TRANSP variable (ZPLAS).
  !
  !  interior -- write moments profiles in normalized form
  !       Rcm_norm(x) = Rcm(x)/x**m  e.g. for m'th R cos moments profiles.
  !  (this improves the numerical properties of interpolations on these
  !  profiles; and, xplasma deals intelligently with the axial singularity).
  !
  !  use xplasma routines to provide the moments...
  !---------------------------------------------------
 
  include 'trxpl.blk'
 
  !---------------------------------------------------
  integer :: id_rho=0,inrho,idum,inmom,ierr
  logical :: do_eq_bdy,do_eq_prof,isym
  real*8, dimension(:), allocatable :: zrho,zbuf
  real*8 :: plhgt
  character*10 zname,zuns
  !---------------------------------------------------
 
  call eq_ganum('__RHO',id_rho)
  call eq_ngrid(id_rho,inrho)
 
  allocate(zrho(inrho),zbuf(inrho)); zbuf=0
 
  call eq_grid(id_rho,zrho,inrho,idum,ierr)
  if(ierr.ne.0) call errmsg_exit(' ?trxpl: internal error 2.')
 
  call rp_eq_symflag(idum)
  isym = .not.(idum.eq.1)    ! isym=.TRUE. => updown symmetry
  if(isym) then
     call trx_scal('ZPLAS',zname,zuns,plhgt,ierr)
     if(ierr.ne.0) plhgt=0
  else
     plhgt=0
  endif
 
  do_eq_bdy  = (ieq.eq.1).or.(ieq.eq.3)
  do_eq_prof = (ieq.eq.2).or.(ieq.eq.3)
 
  call xmoments_kmom_get(inmom)
 
  if(do_eq_bdy) call getm(inrho,inrho)
  if(do_eq_prof) call getm(1,inrho)
 
  deallocate(zrho,zbuf)
 
  contains
    subroutine getm(i1,i2)
 
      integer :: i1,i2   ! range of pts this call
 
      integer im,inum,inorm
      real*8, dimension(:,:), allocatable :: rc,rs,zc,zs
 
      inum=i2-i1+1
      if(inum.eq.1) then
         inorm=0
      else
         inorm=1
      endif
 
      allocate(rc(0:inmom,i1:i2),rs(0:inmom,i1:i2), &
           zc(0:inmom,i1:i2),zs(0:inmom,i1:i2))
 
      ! 0'th moments
      call eqmom_Rcos(zrho(i1:i2),inum,inorm,0,rc(0,i1:i2),idum)
      if(isym) then
         zc(0,i1:i2)=plhgt
      else
         call eqmom_Zcos(zrho(i1:i2),inum,inorm,0,zc(0,i1:i2),idum)
      endif
 
      ! higher moments
      do im=1,inmom
         call eqmom_Rcos(zrho(i1:i2),inum,inorm,im,rc(im,i1:i2),idum)
         call eqmom_Zsin(zrho(i1:i2),inum,inorm,im,zs(im,i1:i2),idum)
         if(isym) then
            rs(im,i1:i2)=0
            zc(im,i1:i2)=0
         else
            call eqmom_Rsin(zrho(i1:i2),inum,inorm,im,rs(im,i1:i2),idum)
            call eqmom_Zcos(zrho(i1:i2),inum,inorm,im,zc(im,i1:i2),idum)
         endif
      enddo
 
      if(inum.eq.1) then
         call addsc('_R0_B',rc(0,i1))
         call addsc('_Z0_B',zc(0,i1))
         do im=1,inmom
            zname=' '
            if(im.lt.10) write(zname,"('_R',i1,'C_B')") im
            if(im.ge.10) write(zname,"('_R',i2,'C_B')") im
            call addsc(zname,rc(im,i1))
            if(im.lt.10) write(zname,"('_R',i1,'S_B')") im
            if(im.ge.10) write(zname,"('_R',i2,'S_B')") im
            call addsc(zname,rs(im,i1))
            if(im.lt.10) write(zname,"('_Z',i1,'C_B')") im
            if(im.ge.10) write(zname,"('_Z',i2,'C_B')") im
            call addsc(zname,zc(im,i1))
            if(im.lt.10) write(zname,"('_Z',i1,'S_B')") im
            if(im.ge.10) write(zname,"('_Z',i2,'S_B')") im
            call addsc(zname,zs(im,i1))
         enddo
      else
         call addpr('_R0',rc(0,i1:i2))
         call addpr('_Z0',zc(0,i1:i2))
         do im=1,inmom
            zname=' '
            if(im.lt.10) write(zname,"('_R',i1,'C_X')") im
            if(im.ge.10) write(zname,"('_R',i2,'C_X')") im
            call addpr(zname,rc(im,i1:i2))
            if(im.lt.10) write(zname,"('_R',i1,'S_X')") im
            if(im.ge.10) write(zname,"('_R',i2,'S_X')") im
            call addpr(zname,rs(im,i1:i2))
            if(im.lt.10) write(zname,"('_Z',i1,'C_X')") im
            if(im.ge.10) write(zname,"('_Z',i2,'C_X')") im
            call addpr(zname,zc(im,i1:i2))
            if(im.lt.10) write(zname,"('_Z',i1,'S_X')") im
            if(im.ge.10) write(zname,"('_Z',i2,'S_X')") im
            call addpr(zname,zs(im,i1:i2))
         enddo
      endif
 
      deallocate(rc,rs,zc,zs)
    end subroutine getm
 
    subroutine addsc(znamsc,zval)
      character*(*) znamsc   ! scalar name
      real*8 zval            ! value
 
      integer idup,i
 
      idup=0
      do i=1,numsc
         if(znamsc.eq.sclabs(1,i)) then
            idup=i
            exit
         endif
      enddo
 
      if(idup.eq.0) then
         numsc=numsc+1
         sclabs(1,numsc)=znamsc
         sclabs(2,numsc)=' '
         sclabs(3,numsc)='m'
         scvals(numsc)=zval
      endif
 
    end subroutine addsc
 
    subroutine addpr(znampr,zvals)
      character*(*) znampr   ! profile name
      real*8 zvals(*)        ! values
 
      integer idup,i
      real*8 zdum
 
      idup=0
      do i=1,numpro
         if(znampr.eq.prolabs(1,i)) then
            idup=i
            exit
         endif
      enddo
 
      if(idup.eq.0) then
         numpro=numpro+1
         prolabs(1,numpro)=znampr
         prolabs(2,numpro)=' '
         prolabs(3,numpro)='m'
         id_xaxis(numpro)=id_rho
         call eqm_rhofun(2,id_rho,znampr,zvals,0,zdum,0,zdum, &
              id_pros(numpro),ierr)
         if(ierr.ne.0) call errmsg_exit(' ?trxpl: internal error no. 3')
      endif
    end subroutine addpr
 
end subroutine trxpl_eqmoms
