#include "fpreproc/f77_dcomplx.h"
!==============================================================================
!
SUBROUTINE INTEG_Z(A, MAXA, B, MAXB, KER, NREACT)
  use iso_c_binding, only: fp => c_double
  use physconst_mod, only : pi,two
!
!   THIS INTEGRATES SIGVBB_Z(SIG,A,B,THETA) W.R.T. THETA
!
!============
! idecl:  explicitize implicit INTEGER declarations:
  IMPLICIT NONE
  INTEGER maxb,maxa,i,j,k
!============
! idecl:  explicitize implicit REAL declarations:
  real(fp) delth,deltab
!============
  INTEGER,PARAMETER :: MAXTH = 101	!ODD FOR SIMPSON'S RULE
!
  real(fp) A(MAXA), B(MAXB)
  real(fp) KER(MAXA, MAXB)
!
  real(fp) SIG(MAXTH)
  real(fp) COS_THETA(MAXTH)
  real(fp) E(MAXTH),T1(MAXTH),T2(MAXTH),ZEXP(MAXTH)
!
  INTEGER NREACT	!WHICH REACTION TO COMPUTE (PASS TO SIGVBB_Z)
!
!==============================================================================
!
  DELTH = PI/AREAL(MAXTH-1)
!
!   THE INTEGRAL IS SYMMETRIC, SO 2*INTEG(0 TO PI) IS OK
!
  DO I = 1, MAXTH
     COS_THETA(I) = COS(AREAL(I-1)*DELTH)	!Only compute these once
  enddo

!
  DO I = 1, MAXA	!LOOP OVER A(I)
!
!   NOW CREATE THE B VALUES FROM 0 TO A(I) EQUALLY SPACED
!
     DELTAB = A(I)/AREAL(MAXB-1)
!
!   BEWARE E(I) = 0.0 IN SIGVBB_Z! ... I THINK I FIXED THAT...B**2
!
     DO J = 1, MAXB
        B(J) = AREAL(J-1)*DELTAB
        B(J) = min( B(J), A(I) )
     enddo
!
!   NOW GET THE INTEGRAND VALUES FOR EACH A(I) AND B(J)
!
     DO J = 1, MAXB
        CALL SIGVBB_Z(SIG, A(I), B(J), COS_THETA, E, T1, T2, ZEXP,&
             MAXTH, NREACT)
!
!   NOW DO A SIMPSON'S RULE INTEGRATION
!
        KER(I,J) = SIG(1) + SIG(MAXTH)
!
        DO K = 2, MAXTH-1, 2
           KER(I,J) = KER(I,J) + 4.0_fp*SIG(K)
        enddo
!
        DO  K = 3, MAXTH-2, 2
           KER(I,J) = KER(I,J) + two*SIG(K)
        enddo
!
        KER(I,J) = DELTH*KER(I,J)/3.0_fp
!
!   NOW MULTIPLY BY 2.0 TO RE!OVER THE FULL INTEGRAL
!
        KER(I,J) = two*KER(I,J)
!
!   NOW DIVIDE BY 2*PI TO RECOVER THE GYROPHASE AVERAGE
!
        KER(I,J) = KER(I,J)/(two*PI)
!
     enddo
  enddo
!
  RETURN
END SUBROUTINE INTEG_Z
!
!------------------------------------------------------------------------------
! 30Jun2000 fgtok -s r8_precision.sub "r8con.csh conversion"
! 30Jun2000 fgtok
