      subroutine rfimap(ivec,ztol,R,Z,x,th,ierr)
c
c  inverse map calculation -- for fppmod
c  based on xplasma; R,Z in cm
c  **internal use only**
c
      IMPLICIT NONE
      integer ivec                      ! vector size
      REAL*8 ztol                         ! rel. tolerance of inv. map
      REAL*8 R(ivec),Z(ivec)              ! R,Z input
c
      REAL*8 x(ivec),th(ivec)             ! xi,theta (mag. coords) output
c
      integer ierr                      ! completion code, 0 = OK
c
c---------------------------------
c
      REAL*8 phi(ivec)
      integer iregion(ivec)
c
      integer i
c
      real*8, parameter :: c2pi = 6.2831853071795862D+00
      real*8, parameter :: cpi = 3.1415926535897931D+00
c
c---------------------------------
c
      phi=0.0d0
      call eq_inv(ivec,0.01d0*R,0.01d0*Z,phi,ztol,
     >   x,th,iregion,ierr)
c
      do i=1,ivec
         do while ( th(i) < -cpi )
            th(i) = th(i) + c2pi
         enddo
         do while ( th(i) > cpi )
            th(i) = th(i) - c2pi
         enddo
      enddo
c
      return
      end
