!-----------------------------------------------------------------------
!  PSGCHEV -- EVALUATE A CHEBYCHEV FIT TO A CROSS-SECTION
!
real(fp) FUNCTION psgchev8(ZE,EMIN,EMAX,CCHEV)
!
!
!  INPUT
!
!============
! idecl:  explicitize implicit INTEGER declarations:
  use iso_c_binding, only: fp => c_double
  IMPLICIT NONE
  INTEGER nterms,k
!============
! idecl:  explicitize implicit DOUBLE PRECISION declarations:
  real(fp) eminl,emaxl,prev2,cheb,el,xnorm,twox,prev
!============
  real(fp) ZE		! INPUT ENERGY, BTW. EMIN AND EMAX INCL.
  real(fp) EMIN,EMAX	! ENERGY RANGE OF FIT -- ERROR IF OUTSIDE
!
  real(fp) CCHEV(9)	! CHEBYCHEV POLYNOMIAL FIT COEFFICIENTS
!
!  OUTPUT PSGCHEV = FIT VALUE RETURNED
!
!  ERROR MESSAGE AND ABORT ROUTINE CALLED IF ZE IS OUT OF RANGE
!
!  REF:  ALGORITHM COPIED FROM FORTRAN SOURCE EXAMPLE, APPENDIX A OF
!    ORNL-6090/V5, ATOMIC DATA FOR FUSION, VOLUME V,
!    "COLLISIONS OF CARBON AND OXYGEN IONS WITH ELECTRONS, H, H2, & He"
!    R.A. PHANEUF, R.K. JANEV, M.S. PINDZOLA
!
!-------------------------------------------------------------------
!
!  ERROR CHECK
!
  IF((ZE.LT.EMIN).OR.(ZE.GT.EMAX)) THEN
     WRITE(6,9001) ZE,EMIN,EMAX
     CALL bad_exit
9001 FORMAT(&
          ' ?PSGCHEV -- PASSED ENERGY ARGUMENT ZE=',1PE11.4/&
          '  NOT IN RANGE BETWEEN EMIN=',1PE11.4,' AND EMAX=',1PE11.4)
  ENDIF
!
!------------------------
!
  NTERMS=9
!
  EMINL=log(EMIN)
  EMAXL=log(EMAX)
!
  K=NTERMS
!
  PREV2=0.0_fp
  CHEB=CCHEV(K)
  K=K-1
!
  EL=log(ZE)
!
!  THIS NUMBER MAPS LOG(ZE) TO RANGE -1 TO 1 AS ZE RANGES FROM
!  EMIN TO EMAX
!
  XNORM=(2.0_fp*EL-EMINL-EMAXL)/(EMAXL-EMINL)
!
  TWOX=2.0_fp*XNORM
!
!  LOOP
!
10 CONTINUE
!
  PREV=CHEB
  IF(K.EQ.1) GO TO 20
!
  CHEB=CCHEV(K)+TWOX*PREV-PREV2
  PREV2=PREV
  K=K-1
!
  GO TO 10
!
20 CONTINUE
!
  CHEB=0.5_fp*CCHEV(1)+XNORM*PREV-PREV2
!
  CHEB=EXP(CHEB)
  psgchev8=CHEB
!
  RETURN
END FUNCTION psgchev8
! 21Jun2001 fgtok -s r8_precision.sub foo.tab "r8con.csh conversion"
! 21Jun2001 fgtok
