!------------------------------------------------------------------------------
!
SUBROUTINE GAUS32_Z(A,B,FUN,TEST2,EPS,MAX,J,IPUNIT)
!	5/10/76
!	THIS SUBROUTINE WAS WRITTEN BY HARRY H TOWNER OF THE
!	PRINCETON PLASMA PHYSICS LAB.  THIS SUBROUTINE
!	WILL INTEGRATE THE FUNCTION FUN(X) BETWEEN THE LIMITS A AND B BY
!	USING 32-POINT GAUSS-LEGENDRE QUADRATURE ITERATIVELY.
!	A - THE LOWER LIMIT OF INTEGRATION
!	B - THE UPPER LIMIT OF INTEGRATION
!	FUN - THE FUNCTION TO BE INTEGRATED.  MUST BE DECLARED EXTERNAL
!	      IN THE CALLING PROGRAM.
!	TEST2 - THE VALUE OF THE INTEGRAL AFTER THE JTH ITERATION
!	EPS - THE ACCURACY TO WHICH YOU WANT THE VALUE.
!	   = ABS((NEW-OLD)/NEW).
!	MAX - MAXIMUM NUMBER OF ITERATIONS TO BE TAKEN.
!	J - TELLS THE NUMBER OF ITERATIONS TAKEN.
!	IPUNIT - THE LOGICAL UNIT FOR PRINTING.
!
!============
! idecl:  explicitize implicit INTEGER declarations:
  use iso_c_binding, only: fp => c_double
  IMPLICIT NONE
  INTEGER j,ipunit,k,i,max
!============
! idecl:  explicitize implicit REAL declarations:
  real(fp) b,test2,eps,a,e00,xlow,test1,sum,xhigh,c1,c0,z,fun,re
!============
  external FUN
!
  real(fp), DIMENSION(16) :: X,W
!	ARRAY X CONTAINS THE ROOTS OF THE LEGENDRE PLOYNOMIAL.
!	ARRAY W CONTAINS THE WEIGHT FACTORS.
  DATA X/.04830766568773832_fp,.1444719615827965_fp,&
       .2392873622521371_fp,.3318686022821276_fp,.4213512761306353_fp,&
       .5068999089322294_fp,.5877157572407623_fp,.6630442669302152_fp,&
       .7321821187402897_fp,.7944837959679424_fp,.8493676137325610_fp,&
       .8963211557660521_fp,.9349060759377397_fp,.9647622555875064_fp,&
       .9856115115452683_fp,.9972638618494816_fp/
  DATA W/.09654008851472780_fp,.09563872007927486_fp,&
       .09384439908080457_fp,.09117387869576388_fp,.08765209300440381_fp,&
       .08331192422694676_fp,.07819389578707031_fp,.07234579410884851_fp,&
       .06582222277636185_fp,.05868409347853555_fp,.05099805926237618_fp,&
       .04283589802222668_fp,.03427386291302143_fp,.02539206530926206_fp,&
       .01627439473090567_fp,.007018610009470097_fp/
!
!  DMC Jan 2009: limit the warnings...
  integer, save :: iwarn = 0
!
  integer, parameter :: iwarn_max = 10
!
!	J COUNTS THE NUMBER OF ITERATIONS.
  J=1
  TEST2=0._fp
200 XLOW=A
  TEST1=TEST2
  SUM=0._fp
  DO K=1,J
     XHIGH=XLOW+(B-A)/J
     C1=(XHIGH-XLOW)*.5_fp
     C0=C1+XLOW
     DO I=1,16
        Z=C1*X(I)
        SUM=SUM+C1*W(I)*(FUN(Z+C0)+FUN(-Z+C0))
     enddo
     XLOW=XHIGH
  enddo
  TEST2=SUM
  IF(TEST2.EQ.0._fp) RETURN
  RE=ABS((TEST2-TEST1)/TEST2)
  IF(RE.LE.EPS) return
  J=J*2
  IF(J.LE.MAX) GO TO 200
  J=J/2
!
  if((ipunit.gt.0).and.((iwarn.lt.iwarn_max).or.(RE.gt.0.1_fp))) then
     WRITE(IPUNIT,10) J,RE
10   FORMAT(1X,'AFTER',I4,' ITERATIONS INTEGRAL HAS NOT CONVERGED ',&
          'RE=',1PE13.5,' GAUS32_Z')
     iwarn = iwarn + 1
     if(iwarn.eq.iwarn_max) then
        write(ipunit,*) &
             ' [gaus32_z: suppressed further warnings w/ RE < 0.1]'
        write(ipunit,*) ' [gaus32_z: as can generally be ignored.]'
     endif
  end if
!
  RETURN
END SUBROUTINE GAUS32_Z
!
!------------------------------------------------------------------------------
! 30Jun2000 fgtok -s r8_precision.sub "r8con.csh conversion"
