!-----------------------------------------------------------------------
!  PSGCHVX -- EVALUATE CHEBYCHEV FIT TO A CROSS-SECTION, W/EXTRAPOLATION
!
real(fp) FUNCTION psgchvx8(itype,ZE,EMIN,EMAX,CCHEV)
!
!  INPUT
!
!============
! idecl:  explicitize implicit REAL declarations:
  use iso_c_binding, only: fp => c_double
  IMPLICIT NONE
  real(fp) zmin,zmax,zans,psgchev8,eminl,emaxl,zainc,ze1,zel1,zel2,&
       ze2,zsig1,zsig2,zlsig1,zlsig2,zs,zel,zlsig0,zlsdif,za,zb
!============
  integer itype     ! 1 for CX, 2 for II
  real(fp) ZE		! INPUT ENERGY EV
  real(fp) EMIN,EMAX	! ENERGY RANGE OF FIT EV
!
  real(fp) CCHEV(9)	! CHEBYCHEV POLYNOMIAL FIT COEFFICIENTS
!
!  OUTPUT PSGCHEV = FIT VALUE RETURNED
!
!  IF ZE IS IN RANGE, EVALUATE FIT VIA SUBROUTINE PSGCHEV
!
!  IF ZE IS OUT OF RANGE, PERFORM AN EXTRAPOLATION
!
!  MOD DMC JULY 1989 -- CHEBYCHEV VALID FIT RANGE DETERMINED TO START
!  ONLY AT 100 EV.  LOCAL VARIABLES ZMIN,ZMAX GIVE LOCALLY ACCEPTED
!  FIT RANGE, BUT ORIGINAL FIT RANGE EMIN,EMAX MUST BE PROVIDED TO
!  THE CHEBYCHEV FIT ROUTINE.
!
!  IF OUT OF RANGE HIGH, A LINEAR EXTRAPOLATION IS PERFORMED;
!
!  IF OUT OF RANGE LOW, A CUBIC INTERPOLATION IS PERFORMED BTW
!  MAX(100 EV, EMIN) AND 1 EV, USING (for cx, itype=1, 2.5e-18 =
!  the approximate H,C+6 cx coeff. at 1 eV) (for ii, itype=2, a
!  very small number, 2.5e-25).
!
!  IN THIS MANNER I HOPE TO REMOVE THE WILD VARIATIONS VS. Z THAT
!  OCCUR AT LOW ENERGY IN THE PHANEUF DATA WHEN EXTRAPOLATED ...
!
!-----------------------------------------------------------------------
!
  ZMIN=max(100.0_fp,EMIN)
  ZMAX=EMAX
!
  IF((ZE.GE.ZMIN).AND.(ZE.LE.ZMAX)) THEN
     ZANS=psgchev8(ZE,EMIN,EMAX,CCHEV)
!
  ELSE
!
!  OUT OF RANGE
!
     EMINL=log(ZMIN)
     EMAXL=log(ZMAX)
!
     ZAINC=0.005_fp*(EMAXL-EMINL)
     IF(ZE.LT.ZMIN) THEN
        ZE1=ZMIN
        ZEL1=EMINL
        ZEL2=EMINL+ZAINC
        ZE2=EXP(ZEL2)
     ELSE IF(ZE.GT.EMAX) THEN
        ZEL1=EMAXL-ZAINC
        ZE1=EXP(ZEL1)
        ZE2=ZMAX
        ZEL2=EMAXL
     ENDIF
!
     ZSIG1=psgchev8(ZE1,EMIN,EMAX,CCHEV)
     ZSIG2=psgchev8(ZE2,EMIN,EMAX,CCHEV)
!
     ZLSIG1=log(ZSIG1)
     ZLSIG2=log(ZSIG2)
!
!  EXTRAPOLATION
!
     ZS=(ZLSIG2-ZLSIG1)/(ZEL2-ZEL1)
     ZEL=log(ZE)
!
     IF(ZE.LT.ZMIN) THEN
!  LOW END - CUBIC
        ZEL=max(0.0_fp,ZEL)
        if(itype.eq.1) then
           zlsig0=log(2.5e-18_fp)
        else
           zlsig0=log(2.5e-25_fp)
        endif
        ZLSDIF=(ZLSIG1-ZLSIG0)/ZEL1
        ZA=(ZS-2.0_fp*ZLSDIF)/ZEL1**2
        ZB=(3.0_fp*ZLSDIF-ZS)/ZEL1
!
        ZANS=ZEL**2*(ZA*ZEL+ZB)+ZLSIG0
!
     ELSE IF(ZE.GT.ZMAX) THEN
!  HIGH END - LINEAR
        ZANS=ZLSIG2+ZS*(ZEL-ZEL2)
!
     ENDIF
!
     ZANS=EXP(ZANS)
!
  ENDIF
!
  psgchvx8=ZANS
!
  RETURN
END FUNCTION psgchvx8
! 21Jun2001 fgtok -s r8_precision.sub foo.tab "r8con.csh conversion"
! 21Jun2001 fgtok
