#include "fpreproc/f77_dcomplx.h"
      subroutine rfx_zoneres(ishe,zBrequ,zdyavg)
c
c  dmc 23 Jan 1997 -- common subsection of code extracted from
c   rfoper.for
c
c  compute the length of the vertical line (B=const=resonant) with
c  the current zone (ishe).
c
c   modified for updown asymmetry:  the zone intersections top and bottom
c  might not be the same.  the averaged value is returned.
c
c   *caution* I am not quite certain that this is the correct averaging
c  procedure for updown asymmetry that sr. rfoper needs -- further
c  investigation is needed.
c
c   *internal use only -- units are Gauss & cm I think*
c
      use rfxqlo_private
c
c-----------------------------------------
c
C============
C idecl:  explicitize implicit INTEGER declarations:
!
      IMPLICIT NONE
      REAL*8, PARAMETER :: ZERO = 0.0d0
      REAL*8, PARAMETER :: HALF = 0.5d0
      REAL*8, PARAMETER :: ONE = 1.0d0
!
      INTEGER isides,is,ir,imin,imax,ishe
C============
C idecl:  explicitize implicit REAL declarations:
      REAL*8 zdyavg,zbrequ,zy2,zarg,zthstar,rfinteq1d,zy1
C============
      if(rfx_grid%symflag) then
         isides=1
      else
         isides=2
      endif
c
      zdyavg=ZERO
      do is=1,isides
c
         ir=ishe*2

         imin=0
         imax=0
         if(zBrequ .le. Bminofr(ir)) then
            zY2=YatBmin(ir)
            imin=imin+1
         else if(zBrequ .ge. Bmaxofr(ir))then
            if(is.eq.1) then
               zy2=YatBmaxP(ir)
            else
               zy2=YatBmaxM(ir)
            endif
            imax=imax+1
         else
c test:
            zarg=(Bavgofr(ir)-zBrequ)/Bdelofr(ir)
            if(zarg .gt. 1.01d0.or. zarg .lt. -1.01d0)
     $         call errmsg_exit('zarg much too large')

            zthstar=acos(min(ONE,max(-ONE,zarg)))
            zY2=rfinteq1d(zthstar,
     $         YofBr(1,ir,is),ZERO,delthofB(ir),numthofB(ir))
         endif

         ir=ishe*2-2
         if(ir .ne. 0)then

            if(zBrequ .le. Bminofr(ir)) then
               zy1=yatBmin(ir)
               imin=imin+1
            else if(zBrequ .ge. Bmaxofr(ir)) then
               if(is.eq.1) then
                  zy1=YatBmaxP(ir)
               else
                  zy1=YatBmaxM(ir)
               endif
               imax=imax+1
            else
c test:
               zarg=(Bavgofr(ir)-zBrequ)/Bdelofr(ir)
               if(zarg .gt. 1.01d0.or. zarg .lt. -1.01d0)
     $            call errmsg_exit('zarg much too large')

               zthstar=acos(min(ONE,max(-ONE,zarg)))
               zY1=rfinteq1d(zthstar,
     $            YofBr(1,ir,is),ZERO,delthofB(ir),numthofB(ir))
            endif

         else
            imin=imin+1
            imax=imax+1
            zy1=HALF*(YatBmin(ir+1)+YatBmaxP(ir+1))
         endif

         if(imin.eq.2) go to 960        !resonance does not intersect
         if(imax.eq.2) go to 960        !resonance does not intersect

         zdyavg=zdyavg+abs(zy2-zy1)
      enddo

      zdyavg=zdyavg/AREAL(isides)
      go to 1000

 960  continue
      zdyavg=ZERO

 1000 continue
      return
      end
