**==xsnteea.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
c
c       Modified to interface with xsnteaa.cxx C++ wrapper 
c          and bug fixes to GREENCE and GRNCEPR routines, which needed
c          to save xnor variable.
c          Jan 2009
c
      SUBROUTINE XSNTEEASB(Ear,Ne,Param,Ifl,Photar, xtot, sptot, spref)

      INTEGER Ne , Ifl
      REAL Param(15) , Ear(0:Ne) , Photar(Ne)
      REAL xtot(0:Ne), sptot(0:Ne), spref(0:Ne)

c
c     driver for the nonthermal/thermal/reflection program
c            see MULMOD for parameter descriptions
c
c     number of model parameters: 15
c     parameters are given below in the order of decreasing importance:
c     1: nonthermal electron compactness
c     2: blackbody compactness
c     3: scaling factor for reflection (1 for isotropic source above disk)
c     4: blackbody temperature in eV
c     5: the maximum Lorentz factor
c     6: thermal compactness (0 for pure nonthermal plasma)
c     7: Thomson optical depth of ionization electrons (e.g., 0)
c     8: electron injection index (0 for monoenergetic injection)
c     9: minimum Lorentz factor of the power law injection (not used for mono)
c    10: minimum Lorentz factor for nonthermal reprocessing (>1; <= par. 9)
c    11: radius in cm (for Coulomb/bremsstrahlung only)
c    12: pair escape rate (0-1)
c    13: cosine of inclination angle
c    14: iron abundance relative to the solar iron abundance
c    15: redshift
c
c     algorithm:
c           a(x)=(non-thermal/thermal pair spectrum)+reflection
c

      INTEGER n , np , jnth, ierr
      REAL pa0(15)
      REAL xn , normfac , SPP
      REAL xnth(900) , spnth(900) , sphth(900)
 
      LOGICAL firstcall , fl0
      CHARACTER contxt*127,fgsolr*4,fgsolr0*4
      EXTERNAL fgsolr

      SAVE firstcall , fl0 , pa0 , xnth , jnth , spnth , sphth , normfac
      SAVE fgsolr0

      DATA firstcall/.TRUE./
      DATA pa0/15*9999./
      DATA fgsolr0/'    '/

      ierr = 0
 
      IF ( firstcall ) THEN
         CALL xwrite('WARNING: This is an experimental model',5)
         CALL xwrite('   Consult Andrzej Zdziarski for advice',5)
         firstcall = .FALSE.
      ENDIF


c     xtot is the energy array (units m_e c^2)
c     spnth is the nonthermal spectrum alone (E F_E)
c     spref is the reflected spectrum array (E F_E)
c     sptot is the total spectrum array (E F_E), = spref if no reflection
c     all dimensions = jmax
 
 
c  calculate internal source spectrum if necessary
      np = 15
      DO 100 n = 1 , np
         IF ( Param(n).NE.pa0(n) ) fl0 = .TRUE.
 100  CONTINUE
      IF ( fl0 .or. (fgsolr().ne.fgsolr0) ) THEN
         CALL NONTH(Param,xnth,jnth,spnth,sphth)
         xn = (1+Param(15))/511
         normfac = 1/(SPP(1/xn,xnth,jnth,spnth)
     &             +SPP(1/xn,xnth,jnth,sphth))
         DO 150 n = 1 , np
            pa0(n) = Param(n)
 150     CONTINUE
         fl0 = .FALSE.
      ENDIF
 
c
      DO 200 n = 0 , Ne
         xtot(n) = (1+Param(15))*Ear(n)/511
 200  CONTINUE
 
c  calculate reflected and total components
      CALL GRNTEEPR(xnth,jnth,spnth,sphth,xtot,Ne+1,
     &              sptot,spref,Param(3)
     &              ,Param(13),ALOG10(Param(14)))
 
c  convert to photons and integrate over the bin size,
c  sptot is normalized at 1keV at the Earth frame
      DO 300 n = 0 , Ne
         sptot(n) = normfac*sptot(n)/Ear(n)**2
 300  CONTINUE
      DO 400 n = 1 , Ne
         Photar(n) = (sptot(n)+sptot(n-1))
     &                    *(Ear(n)-Ear(n-1))/2
 400  CONTINUE

 999  CONTINUE
      IF ( ierr .NE. 0 ) THEN
         CALL xwrite(contxt, 10)
      ENDIF
 
      RETURN
      END
**==nonth.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
c ------------------------------------------------------------------- c
 
      SUBROUTINE NONTH(Param,X,Jmax,Spnth,Sphth)
c  version: June 93
c  revised for reflection 1995
c
c
C  NONTHERMAL MODEL OF A COMPACT SOURCE
C  INITIAL POWER LAW ELECTRON INJECTION BETWEEN GMIN AND GMAX
C  COOLING ON MAGNETIC FIELD AND ON SYNCHROTRON PHOTONS
c  PAIR PRODUCTION  -> A STEADY STATE ELECTRON DISTRIBUTION
c
c Units: photon density  1/(sigma_T*R)
c        injection rate  c/(sigma_T*R^2)
c        gamma dot       c/R
c        radiated spectrum  dl/dln x
c
      IMPLICIT NONE
      REAL besc , scale , ctaug , xplasm , delta0 , delta , xmin , b0 , 
     &     syncon , xmax , z1 , z2 , z3 , z4 , z5 , z6 , planck , 
     &     emagn , aa , cn , cndis , brcth , tautom , xl , sum , ga , 
     &     clpair , coulht , brcnth , gth , annlum , annht , thetaan , 
     &     dlt , xanlo , xanup , deltal , xnr , xr , taukn , arg , flz , 
     &     xs , xlth , taup , pterm
c     real functions
      REAL COMPTT , ANNSP
      REAL xm , ab0
      REAL Param(15) , g(900) , del(900) , qint0(900) , qint(900) , 
     &     taug(900) , dphth(900) , dwork(900) , qintp(900) , eint(900)
     &     , tauc(900) , gcoul(900) , brnth(900) , gbrem(900) , 
     &     dsyn(900) , brth(900) , X(900) , DPH(900) , DPHesc(900) , 
     &     DPHdot(900) , TAUabs(900) , REL(900) , BET(900) , C1(900) , 
     &     C2(900) , Spnth(900) , Sphth(900) , spref(900) , sptot(900)
      INTEGER jmin2(21) , jmax2(21)
      INTEGER ith , ith2 , n34 , i , j , jsyn , jmc22 , ju , j34 , 
     &        imaxth , iminth , ijmax , k1 , k2 , k3 , k , n , jlold , 
     &        jl , il , iu , kl , ku , janlo , janup , jnr , jrel , kk
      REAL*8 q1 , q2 , q3 , q4 , q5 , q6 , qa , qb , qc , w , w1
c  physical constants
      REAL A0 , SIGmat , VLIght , BCRit , XLAmbd , XME , PI , XRS
c  general parameters:
      REAL GMIn , GMAx , EXP0 , BFIeld , TEMpbb , OMEga , RADius , 
     &     XLEl , XLBb , GAMma0 , GT , theta
      INTEGER IMIns , IMAx , Jmax , JMC2 , ITEr
c
      COMMON /PHYSNT/ A0 , SIGmat , VLIght , BCRit , XLAmbd , XME , PI , 
     &                XRS
      COMMON /PARNT / GMIn , GMAx , EXP0 , BFIeld , TEMpbb , OMEga , 
     &                RADius , XLEl , XLBb , GAMma0 , GT
      COMMON /INTNT / IMIns , IMAx , JMC2 , ITEr
      COMMON /NTEE_THERM/ DPH , DPHesc , DPHdot , TAUabs , REL , BET , 
     &                    C1 , C2
c
C  INPUT:
c   the minimum and maximum Lorentz factor for the electron injection
c   gmin used only for power law injection
      GMIn = Param(9)
      GMAx = Param(5)
C  EXP0 - INDEX OF POWER LAW INJECTION DISTRIBUTION
C   EXP0=0 - DELTA-FUNCTION ELECTRON INJECTION
      EXP0 = Param(8)
C  xlel - DIMENSIONLESS LUMINOSITY IN INJECTED ELECTRON SPECTRUM
      XLEl = Param(1)
c  xlbb - dimensionless blackbody luminosity
      XLBb = Param(2)
c     the thermal compactness:
c     For pure nonthermal plasmas, xlth=0
      xlth = Param(6)
C  TEMPBB - TEMPERATURE OF EXTERNAL PHOTONS IN MC**2
      TEMpbb = Param(4)/5.11E5
c    taup is the optical depth of ionization electrons
      taup = Param(7)
c  besc - the beta escape parameters of pairs, defined as in Zdziarski (1985)
      besc = Param(12)
C  RADIUS - RADIUS OF THE SOURCE in cm
      RADius = Param(11)
C  GAMMA0 - MINIMUM LORENTZ FACTOR (E.G., =1.1) used for nonthermal
c  reprocessing; gmin.geq.gamma0; gamma0>1+3*Theta
      GAMma0 = Param(10)
c     the scaling factor for the reflected spectrum;
c     1 corresponds to seeing equal
c     contributions from the reflected and direct spectra
      scale = Param(3)
c  inclination angle
      xm = Param(13)
c  iron abundance
      ab0 = ALOG10(Param(14))
c
C  ITER - NUMBER OF ITERATIONS
c  ith  - number of iterations with 'thermal'
c  ith2 - number of calls to 'thermal' within one iteration
      ITEr = 16
      ith = 11
      ith2 = 4
C  BFIELD - MAGNETIC FIELD STRENGTH
C     IF(BFIELD.LE.0.) BFIELD IS IN EQUIPARTITION WITH the TOTAL PHOTON ENERGY
      BFIeld = 0
C  GT - VALUE OF THE LORENTZ FACTOR CORRESPONDING TO THE SELF-ABSORPTION
C    FREQUENCY  -- if bfield=0, no self-absorption
      GT = 50
 
c  clear arrays (important for repeated calls)
 
      DO 100 j = 1 , 900
         dphth(j) = 0
         dsyn(j) = 0
         taug(j) = 0
         tauc(j) = 0
         g(j) = 0
         del(j) = 0
         qint0(j) = 0
         qint(j) = 0
         dwork(j) = 0
         qintp(j) = 0
         eint(j) = 0
         gcoul(j) = 0
         brnth(j) = 0
         gbrem(j) = 0
         brth(j) = 0
         X(j) = 0
         DPH(j) = 0
         DPHesc(j) = 0
         DPHdot(j) = 0
         TAUabs(j) = 0
         REL(j) = 0
         BET(j) = 0
         C1(j) = 0
         C2(j) = 0
 100  CONTINUE
 
C
C  PHYSICAL CONSTANTS:
C
      BCRit = 4.414E13
      VLIght = 3E10
      SIGmat = 6.65E-25
      XLAmbd = 2.426E-10
      XME = 9.1095E-28
      A0 = 5.29E-9
      PI = 3.14159
c     dimensionless volume
      XRS = 4*PI/3
c     pair optical depth constant (Svensson 1987)
      ctaug = 0.2
c     dimensionless plasma frequency for unit Thomson depth,
c     should be multiplied by sqrt(tautom):
      xplasm = SQRT(3*A0/2/RADius)
c     n34 - number of bins between 3/4 and 1
      n34 = 4
c
c
c
C  ARRAY FOR LORENTZ FACTORS:
C
      delta0 = LOG10(4./3)/n34
      IMAx = LOG10(GMAx/GAMma0)/delta0 + 1
c     delta .ge. delta0
c     delta is the 10-log interval between the energy points
      delta = LOG10(GMAx/GAMma0)/(IMAx-1)
c  deltal - the e-log interval between the energy points
      deltal = delta*LOG(10.)
      DO 200 i = 1 , IMAx
         g(i) = 10.**((i-1)*delta)*GAMma0
 200  CONTINUE
      IF ( ABS(EXP0).GT.1.E-15 ) IMAx = IMAx - 1
C
C SYNCHROTRON -----
C
C       DIMENSIONLESS CYCLOTRON FREQUENCY:
      b0 = BFIeld/BCRit
c       constant for synchrotron radiation (depending on radius)
      syncon = RADius/A0/8
 
      IF ( ABS(BFIeld).LT.1.E-15 ) THEN
         GT = GAMma0
         IMIns = 0
c       the minimum blackbody energy:
         xmin = 8.E-2*TEMpbb
      ELSE
C       THE EQUIPARTITION VALUE OF BFIELD - with synchrotron photons
c       only - assuming Lsyn=Lcom=1/2 Ltotal
c       the mean photon time inside the source =R/c (no 3/4, taut<1)
         IF ( BFIeld.LT.0. ) BFIeld = VLIght*SQRT(3*XLEl*XME/SIGmat/
     &                                RADius)
C       del(imins) corresponds to the synchrotron turnover frequency
         IMIns = LOG10(GT/GAMma0)/delta + 1
         xmin = 1.3334*b0*g(IMIns+1)**2
      ENDIF
 
c       maximum synchrotron energy (reduced since 'synchr' interpolates
c       forward):
      jsyn = 2*(IMAx-IMIns) - 1
C
C -----------------
 
C
C  JMAX - # OF PHOTON ENERGIES
C  X(JMAX) < GMAX-1
C
      Jmax = LOG10((g(IMAx)-1)/xmin)/delta + 1
C  X(JMC2) = ME C**2
      JMC2 = LOG10(1/xmin)/delta + 1
C
C  X - ARRAY FOR PHOTON ENERGIES
C
      DO 300 j = 1 , Jmax + 1
         X(j) = 10.**((j-JMC2)*delta)
 300  CONTINUE
      xmin = X(1)
      xmax = X(Jmax)
c     used in calls to comptt
      jmc22 = LOG10(0.5/xmin)/delta + 2
c     minimum upper limit in the tauc integral
      ju = LOG10(0.75/g(IMAx)/xmin)/delta + 1
c     x(j34)=3/4/gamma0
      j34 = JMC2 + LOG10(0.75/GAMma0)/delta
c
c compute c1(x), c2(x), and rel(x) arrays
c c1(x) and c2(x) are the relativistic corrections to K-equation
c rel(x) is Klein-Nishina cross section divided by thomson crossection
      DO 400 j = 1 , Jmax
         w = X(j)
c  c1, c2 is the Cooper's coefficient calculated at w and w1, resp.
         C1(j) = w**4/(1+4.6*w+1.1*w*w)
c   the commented lines are for 0-Theta Fokker-Planck eq. (Spring 1987)
c   use the file coeff.f
c d1(x)=c1(x), d2 is derivative of d1, used to determine theta
c       d1(j)=coeff1(w)
c   coeff3 is derivative of x**4*coeff1
c       if (w.gt.5.e-3) then
c         d2(j)=coeff3(w)/w**4-d1(j)*4/w
c       else
c         d2(j)=-161./5*w+2304./7*w*w
c       end if
         w1 = SQRT(X(j)*X(j+1))
         C2(j) = w1**4/(1+4.6*w1+1.1*w1*w1)
c       w1 is x(j+1/2) (x(i) defined up to jmax+1)
c       c1(j)=coeff1(w1)
c       c2(j)=coeff2(w1)
         IF ( w.LE.0.05 ) THEN
c         use asymptotic limit for rel(x) for x less than 0.05
            REL(j) = 1. - 2.*w + 26.*w*w/5.
         ELSE
            z1 = (1.+w)/w**3
            z2 = 1. + 2.*w
            z3 = LOG(z2)
            z4 = 2.*w*(1.+w)/z2
            z5 = z3/2./w
            z6 = (1.+3.*w)/z2/z2
            REL(j) = 0.75*(z1*(z4-z3)+z5-z6)
         ENDIF
 400  CONTINUE
 
c  the thermal emision spectrum 'dphth'
      IF ( ABS(XLBb).GT.1.E-15 ) THEN
c       blackbody dilution factor
         OMEga = 45*XLAmbd**3/32/PI/PI/SIGmat/RADius/(PI*TEMpbb)**4*XLBb
         imaxth = LOG10(50*TEMpbb/xmin)/delta
         iminth = MAX(LOG10(8.E-2*TEMpbb/xmin)/delta+1,1.)
         planck = 15/(PI*TEMpbb)**4/XRS*XLBb
         DO 450 j = iminth , imaxth
            dphth(j) = planck*X(j)**2/(EXP(X(j)/TEMpbb)-1)
 450     CONTINUE
      ENDIF
C
C  2*G(IJMAX) < X(JMAX) -
C   THE CONDITION FOR THE MAXIMUM ENERGY OF THE PRODUCED PAIRS
C
      ijmax = LOG10(xmax/2/GAMma0)/delta + 1
C
C  PHOTON ENERGY RANGES .4-2 AND 2-10 keV
C  (TO COMPUTE LUMINOSITIES & SPECTRAL INDICES IN THAT REGIONS)
C
      k1 = LOG10(7.83E-4/xmin)/delta + 2
      k2 = LOG10(3.91E-3/xmin)/delta + 1
      k3 = LOG10(1.96E-2/xmin)/delta + 1
C
C  X(JMIN2(K)) - MINIMUM PHOTON ENERGY IN THE K-TH SCATTERING
C  X(JMAX2(K)) - MAXIMUM PHOTON ENERGY IN THE K-TH SCATTERING
C
C  X(JMAX2(1)) < 1.333*GMAX**2*X0
C
      jmin2(1) = 1
c  the maximum energy in the 0-order photons:
c     I assume that the non-zero synchrotron spectrum always extends to a
c     larger energy then the thermal spectrum
      IF ( ABS(BFIeld).GT.1.E-15 ) THEN
         jmax2(1) = imaxth
      ELSE
         jmax2(1) = IMAx
      ENDIF
      DO 500 k = 2 , ITEr + 1
         jmin2(k) = MIN(Jmax+1,INT(LOG10(4./3*GAMma0**2*X(jmin2(k-1))/
     &              xmin)/delta+2))
         jmax2(k) = MIN(Jmax,INT(LOG10(4./3*g(IMAx)**2*X(jmax2(k-1))/
     &              xmin)/delta+1))
 500  CONTINUE
C
c     set up the initial (nonzero) theta and tautom
      theta = 2.E-2
      tautom = MAX(1.E-4,taup)
c
c  determine the initial electron distribution constants:
c     constants:
      CALL ZEROCN(emagn,aa,cn,cndis)
C
c     the electron injection and initial electron distribution:
      CALL ELDIS(g,aa,cn,cndis,qint0,del)
c     thermal bremsstrahlung emission
      CALL THBREM(X,brth,brcth,theta,tautom,taup,deltal,Jmax)
c     the synchrotron component
      IF ( ABS(BFIeld).GT.1.E-15 )
     &     CALL SYNCHR(dsyn,del,g,X,jsyn,b0,syncon,deltal)
 
c     the initial photon spectrum:
      DO 600 j = 1 , Jmax
         DPH(j) = dphth(j) + dsyn(j)
 600  CONTINUE
c
C*****  iteration loop
c      ipl is the index used to call 'table', to print out results
c       ipl=1
      DO 1900 n = 1 , ITEr
C
C       PAIR INJECTION DISTRIBUTION:
         DO 650 j = JMC2 , Jmax
            dwork(j) = DPH(j)*taug(j)*X(j)*deltal
 650     CONTINUE
c       integral over dwork; see Lighman and Zdziarski (1987)
c       ijmax assumed ge. 2
c       qintp is equal 2 times the integral, so no /2
         qintp(ijmax) = dwork(Jmax)
         qintp(ijmax-1) = dwork(Jmax) + dwork(Jmax-1)
         jlold = Jmax - 1
c       trapezoidal integration; jlold is previous lower limit;
c       loop from high to low values of g(i)
         DO 700 i = ijmax - 2 , 1 , -1
c         the lower limit
            xl = g(i) + SQRT(g(i)*g(i)-1)
            jl = LOG10(xl/xmin)/delta + 2
            sum = qintp(i+1)
            DO 660 j = jl , jlold - 1
               sum = sum + dwork(j) + dwork(j+1)
 660        CONTINUE
            qintp(i) = sum
            jlold = jl
 700     CONTINUE
c       the sum of electron and pair integrals
         DO 750 i = 1 , IMAx
            qint(i) = qintp(i) + qint0(i)
 750     CONTINUE
C
C       EQUILIBRIUM THOMSON THICKNESS AND PAIR LUMINOSITY:
c   relativistic correction
         ga = 1/(1+2*theta**2/LOG(1.12*theta+1.3))
c        Thomson depth: acceleration has zero net effect; only secondary
c        pairs contribute; ionization electrons with the optical depth taup
c        included
         pterm = qintp(1)
c         assume tautom larger than zero, to calculate 'bet'
         IF ( pterm.NE.0 ) tautom = (0.375*ga*taup**2+2*(besc*taup+pterm
     &                              ))
     &                              /(besc+SQRT(besc**2+(0.375*ga*taup)
     &                              **2+0.75*ga*(besc*taup+pterm)))
c       pair luminosity (in the pair rest mass)
         clpair = pterm*XRS
c
c
c       the Coulomb and bremsstrahlung cooling rates
         DO 800 i = 1 , IMAx
            gcoul(i) = 0.75*tautom*LOG(g(i)/xplasm**2/tautom)
            gbrem(i) = MAX(0.,3.485E-3*tautom*(LOG(2*(g(i)-0.999))-1/3.)
     &                 )
 800     CONTINUE
C
C       COMPTON COOLING INTEGRAL
         DO 850 j = 1 , JMC2
            dwork(j) = DPH(j)*X(j)**2*deltal
 850     CONTINUE
c       calculate first integral from xmin to 3/4/g(imax)
         sum = (dwork(1)+dwork(ju))/2
         DO 900 j = 2 , ju - 1
            sum = sum + dwork(j)
 900     CONTINUE
         eint(IMAx) = sum
c       add imax-1 further terms to calculate eint(i)
         DO 950 i = IMAx - 1 , 1 , -1
            eint(i) = eint(i+1) + (dwork(ju+IMAx-i-1)+dwork(ju+IMAx-i))
     &                /2
 950     CONTINUE
C       CALCULATE THE ELECTRON DISTRIBUTION
         DO 1000 i = 1 , IMIns
            del(i) = qint(i)
     &               /(eint(i)*(1.333*g(i)**2-1)+gcoul(i)+gbrem(i))
 1000    CONTINUE
         DO 1050 i = IMIns + 1 , IMAx
            del(i) = qint(i)/(eint(i)*(1.333*g(i)**2-1)+emagn*1.333*g(i)
     &               **2+gcoul(i)+gbrem(i))
 1050    CONTINUE
c
c       calculate the Coulomb heating rate
c       (the integral over gcoul(i)*del(i) )
         sum = (gcoul(1)*del(1)*g(1)+gcoul(IMAx)*del(IMAx)*g(IMAx))/2
         DO 1100 i = 2 , IMAx - 1
            sum = sum + gcoul(i)*del(i)*g(i)
 1100    CONTINUE
         coulht = sum*deltal
C
C       THE NEW LORENTZ FACTOR CORRESPONDING TO THE TURNOVER FREQUENCY
c        if(imins.ge.2) then
c          pindex=-(log(del(imins))-log(del(imins-1)))/deltal
c         THE NEW LORENTZ FACTOR CORRESPONDING TO THE TURNOVER FREQUENCY
c          gtnew=gt0(del(imins)*g(imins)**pindex,pindex,radius,b0)
c        end if
c
c       Optical depth to Compton scatterings with relativistic electrons --
c       integral over the electron distribution
         DO 1150 i = 1 , IMAx
            dwork(i) = del(i)*g(i)*deltal
 1150    CONTINUE
         tauc(j34) = dwork(1)/2
         tauc(j34-1) = (dwork(1)+dwork(2))/2
c       continuous continuation up to 1:
         DO 1200 j = j34 + 1 , JMC2 - 1
            tauc(j) = tauc(j34)*LOG(X(j))/LOG(X(j34))
 1200    CONTINUE
c       trapezoidal integral
         DO 1250 j = j34 - 2 , j34 - IMAx + 1 , -1
            tauc(j) = tauc(j+1) + (dwork(j34-j)+dwork(j34-j+1))/2
 1250    CONTINUE
c       constant tauc:
         DO 1300 j = 1 , j34 - IMAx
            tauc(j) = tauc(j34-IMAx+1)
 1300    CONTINUE
c       synchrotron spectrum:
         IF ( ABS(BFIeld).GT.1.E-15 )
     &        CALL SYNCHR(dsyn,del,g,X,jsyn,b0,syncon,deltal)
c       nonthermal and thermal bremsstrahlung spectra
         CALL NTHBREM(brnth,brcnth,g,del,X,deltal,IMAx,Jmax,tauc(1),
     &                tautom)
         CALL THBREM(X,brth,brcth,theta,tautom,taup,deltal,Jmax)
c       annihilation heating and cooling
c***    IMPORTANT!
c**     for external pair injection, replace pterm by qint(1)
         CALL ANNIH(theta,tautom,taup,pterm,gth,annlum,annht)
c
c       compute Compton photon production rate
         DO 1350 j = jmin2(2) , Jmax
c         for each x, we will integrate over gamma
c         limits of integration:
            il = MAX(1,INT(LOG10(X(j)/GAMma0)/delta+1))
            iu = MIN(IMAx,INT(LOG10(0.75*X(j)/xmin/GAMma0**2)/2/delta+1)
     &           )
c         the boundary elements of the integral (kl.ge.ku)
            kl = LOG10(0.75*X(j)/g(il)**2/xmin)/delta + 1
            ku = LOG10(0.75*X(j)/g(iu)**2/xmin)/delta + 1
            sum = (DPH(kl)*del(il)/g(il)+DPH(ku)*del(iu)/g(iu))/2
            DO 1320 i = il + 1 , iu - 1
               k = LOG10(0.75*X(j)/g(i)**2/xmin)/delta + 1
               sum = sum + DPH(k)*del(i)/g(i)
 1320       CONTINUE
c         add contribution to dphdot from blackbody
c         synchrotron, and bremsstrahlung photons
            DPHdot(j) = 0.75*sum*deltal + dphth(j) + dsyn(j) + brnth(j)
     &                  + brth(j)
 1350    CONTINUE
c       define dphdot for lowest x's
         DO 1400 j = 1 , jmin2(2) - 1
            DPHdot(j) = dphth(j) + dsyn(j) + brnth(j) + brth(j)
 1400    CONTINUE
c
c       for n=1, tabulate the photon production rate w/o any other effects
c        if (n.eq.1) call table(dphdot,x,jmax,del,g,ipl,xrs)
c
c    Include in dphdot those annihilation photons
c    that get either scattered or absorbed
c    The annihilation spectrum is based on temperature from the previous
c    iteration
 
c  Correct dphdot for the case when theta is so low that the line width
c  becomes less than the bin width
         IF ( SQRT(PI*theta).LT.deltal ) THEN
            thetaan = deltal**2/PI
         ELSE
            thetaan = theta
         ENDIF
 
c     Maximum and minimum energies of annihilation photons
c     (see Svensson 1983; exp(-10) maximum decrease assumed)
         dlt = SQRT((10*thetaan)**2+40*thetaan)
         xanlo = 1 + 5*thetaan - dlt/2
         xanup = 1 + 5*thetaan + dlt/2
         janlo = LOG(xanlo/xmin)/deltal + 2
         janup = LOG(xanup/xmin)/deltal + 1
         DO 1450 j = janlo , janup
            DPHdot(j) = DPHdot(j) + ANNSP(X(j),thetaan,tautom,taup)
     &                  *(TAUabs(j)+tautom*REL(j))
     &                  /(1+tautom*REL(j)+TAUabs(j))
 1450    CONTINUE
c
c compute beta array, the probalility of escape per Thomson time
c bet evaluated for spherical geometry and nearly uniform sources.
c Between x=0.1 and 1.0, a function flz modifies beta to allow
c the increasingly large energy change per scattering to gradually
c eliminate spatial diffusion
         jnr = JMC2 + LOG10(0.1)/delta
         jrel = JMC2 + LOG10(1.)/delta
         xnr = X(jnr)
         xr = X(jrel)
         DO 1500 j = 1 , jnr - 1
            taukn = tautom*REL(j)
            BET(j) = 1./tautom/(1.+taukn/3.)
 1500    CONTINUE
         DO 1550 j = jnr , jrel
            taukn = tautom*REL(j)
            arg = (X(j)-xnr)/(xr-xnr)
            flz = 1. - arg
            BET(j) = 1./tautom/(1.+taukn/3.*flz)
 1550    CONTINUE
         DO 1600 j = jrel + 1 , Jmax
            BET(j) = 1./tautom
 1600    CONTINUE
c
c skip over solution of dph and taug if thermal already called
         IF ( tautom.LT.0.3 .OR. n.LE.ITEr-ith+1 ) THEN
c
c       compute dph and taug self-consistently
c
            DO 1620 j = 2*JMC2 - Jmax , Jmax
               q1 = DPHdot(j)
               q2 = BET(j)*tautom + tauc(j)
               q3 = ctaug/X(j)
               k = 2*JMC2 - j
               q4 = DPHdot(k)
               q5 = BET(k)*tautom + tauc(k)
               q6 = ctaug/X(k)
               qa = q6*q2
               qb = q2*q5 + q3*q4 - q1*q6
               qc = -q1*q5
c       see Numerical Recipes, quadratic equations
               IF ( qb.GE.0. ) THEN
                  DPH(j) = -2*qc/(qb+SQRT(qb*qb-4*qa*qc))
               ELSE
                  DPH(j) = (-qb+SQRT(qb*qb-4*qa*qc))/2/qa
               ENDIF
 1620       CONTINUE
            DO 1640 j = 2*JMC2 - Jmax , Jmax
               k = 2*JMC2 - j
               taug(j) = ctaug*DPH(k)/X(j)
 1640       CONTINUE
c       define tauabs(x)
            DO 1660 j = 1 , Jmax
               TAUabs(j) = taug(j) + tauc(j)
 1660       CONTINUE
c       define dph(x) in the region not covered above
            DO 1680 j = 1 , 2*JMC2 - Jmax - 1
               DPH(j) = DPHdot(j)/(BET(j)*tautom+TAUabs(j))
 1680       CONTINUE
 
c  compute theta (x(jmc22)=0.5)
c  XLTH is the direct thermal heating in addition to the radiative and
c  Coulomb ones. It appears in the command below and in the call to THERML
c  a few lines below.
            theta = COMPTT(DPH,jmc22,Jmax,X,deltal,REL,
     &              (xlth/XRS+coulht+annht-brcth)/tautom,theta)
c compute escaping photon density
            DO 1700 j = 1 , Jmax
               DPHesc(j) = DPH(j)*BET(j)*tautom
 1700       CONTINUE
         ENDIF
 
         IF ( n.GT.ITEr-ith .AND. tautom.GE.0.3 ) THEN
            DO 1720 kk = 1 , ith2
               CALL THERML(tautom,theta,deltal,XRS,xlth/XRS+coulht+
     &                     annht-brcth,X,Jmax)
c     tauabs for small indices not affected
               DO 1710 j = 2*JMC2 - Jmax , Jmax
                  k = 2*JMC2 - j
                  taug(j) = ctaug*DPH(k)/X(j)
                  TAUabs(j) = taug(j) + tauc(j)
 1710          CONTINUE
 1720       CONTINUE
         ENDIF
 
c compute xstar
         DO 1750 j = 1 , Jmax
            IF ( taug(j).GT.1. ) GOTO 1800
 1750    CONTINUE
 1800    xs = 0.5*(X(j)+X(j-1))
 
c  add freely escaping annihilation photons
         DO 1850 j = janlo , janup
            DPHesc(j) = DPHesc(j) + ANNSP(X(j),theta,tautom,taup)
     &                  /(1+tautom*REL(j)+TAUabs(j))
 1850    CONTINUE
c
c       store the distributions
c        if(n.eq.iter.or.n.eq.iter-ith) then
c          ipl=ipl+1
c          call table(dphesc,x,jmax,del,g,ipl,xrs)
c        end if
c
c     end of the main loop
c
 1900 CONTINUE
c
c     the nonthermal component in E F_E
      DO 2000 j = 1 , Jmax
         Spnth(j) = DPHesc(j)*X(j)**2*XRS
 2000 CONTINUE
 
c     compute spectral parameters of the final spectrum
c     (this can be omitted in xspec)
c      call specpar(x,spnth,jmax,deltal,k1,k2,k3)
c
c     compute the reflected component if present
      IF ( scale.GT.0 ) THEN
c       spref is the reflected component alone
c       sptot is the total spectrum
c       sphth is the black body component
         CALL GREENTEE(X,xmin,Jmax,deltal,Spnth,spref,sptot,scale,dphth,
     &                 Sphth,XRS,XLBb,xm,ab0)
         CALL SPECPAR(X,sptot,Jmax,deltal,k1,k2,k3)
      ELSE
         DO 2050 j = 1 , Jmax
            spref(j) = 0
            Sphth(j) = 0
            sptot(j) = Spnth(j)
 2050    CONTINUE
      ENDIF
 
      RETURN
c
C****  WRITE MODEL PARAMETERS:
C
c      write(6,41)GMIN,GMAX,EXP0,RADIUS,GAMMA0,XLEL,XLBB,
c     >  imins,IMAX,JMAX,IJMAX,jmc2,ju,j34,K1,K2,K3,n34,ith,ith2,
c     >  CN,TEMPBB,OMEGA
99001 FORMAT (//,11X,'PARAMETERS:',/,' GMIN = ',1PE9.2,' GMAX = ',E9.2,
     &        ' EXP0 = ',0PF7.3,/,' RADIUS [CM] = ',1PE9.2,' GAMMA0 = ',
     &        E9.2,' XLEL = ',E9.2,/,' XLBB=',E9.2,' imins=',i3,
     &        ' IMAX= ',I3,' JMAX= ',I3,/,' IJMAX= ',I3,' jmc2=',i3,
     &        ' ju=',i3,' j34=',i3,' K1= ',I3,' K2= ',I3,' K3= ',I3,/,
     &        ' n34=',i3,' ith=',i3,' ith2=',i3,' CN = ',E9.2,/,
     &        ' TEMPBB = ',E9.2,' OMEGA = ',E9.2)
c
c       write some output
c        write(6,60) n
99002 FORMAT (/,1x,'iteration # ',i3)
c       write the results for electrons:
C
c        write(6,42)tautom,clpair
99003 FORMAT (/,' tau_T=',1pe9.2,' L(pair rest mass)=',e9.2)
c       write the bremsstrahlung and Coulomb luminosities
c        write(6,66) brcth*xrs,brcnth*xrs,coulht*xrs
99004 FORMAT (1x,'thbr lum.=',1pe10.3,' nthbr lum.=',e10.3,
     &        ' Coul. lum.=',e10.3)
c        write(6,67) annlum*xrs,annht*xrs,gth
99005 FORMAT (1x,'ann. lum.=',1pe9.2,' ann. heat.=',e9.2,' gth=',e9.2)
 
 
c       print the results for photons
c        write(6,777) xs, tauc(1), theta
99006 FORMAT (' E(tau_gg=1) = ',1pe9.2,' tau_nth = ',e9.2,' theta = ',
     &        e9.2)
      END
**==specpar.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
c ------------------------------------------------------------------ c
 
      SUBROUTINE SPECPAR(X,Sptot,Jmax,Deltal,K1,K2,K3)
c     spectral parameters: L, alpha
      IMPLICIT NONE
      INTEGER Jmax , K1 , K2 , K3
      REAL X(Jmax) , Sptot(Jmax) , Deltal , cltot , cl1 , cl2 , alpha1 , 
     &     alpha2
c     real functions
      REAL CLUM , ALPHA
c     calculate the luminosities
      cltot = CLUM(Sptot,1,Jmax,Deltal)
      cl1 = CLUM(Sptot,K1,K2,Deltal)
      cl2 = CLUM(Sptot,K2+1,K3,Deltal)
c     calculate the energy spectral indices
      alpha1 = 1 - ALPHA(X,Sptot,K1,K2)
      alpha2 = 1 - ALPHA(X,Sptot,K2+1,K3)
      RETURN
c      write(6,46)cltot,cl1,cl2,alpha1,alpha2
99001 FORMAT (/,' Total lum.=',1pe9.2,' L(.4-2)=',e9.2,' L(2-10)=',e9.2,
     &        /,' alpha(.4-2)=',e9.2,' alpha(2-10) = ',e9.2,/)
      END
**==therml.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
      SUBROUTINE THERML(Tautom,Theta,Deltal,Xrs,Extht,X,Jmax)
c this program computes the effects of Comptonization by
c nonrelativistic thermal electrons in a sphere of radius R,
c including absorption, escape, and
c relativistic corrections up to photon energies of 1 MeV.
c the dimensionless photon energy is x=hv/(m*c*c)
c
c the input parameters and functions are:
c dphdot(x), the photon production rate (photons/volume/time/energy)
c tautom, the Thomson scattering depth
c tauabs(x), the absorption depth
c theta, the initial value of temperature in units of m*c*c
c c1(x), c2(x), and bet(x), the coefficients in the K-equation and the
c   probability of photon escape per Thomson time, respectively,
c   including Klein-Nishina corrections
c rel(x) is the ratio of k-n crossection to thomson crossection
c the output parameters and functions are:
c dph(x), the interior photon density, in units of R/c
c dphesc(x), the escaping photon density, in units of R/c
c theta, the equilibrium electron temperature
c exheat is the rate of nonradiative (e.g., Coulomb) heating.
c If exheat=0, the equilibrium temperature is the Compton temperature.
 
      IMPLICIT NONE
      REAL aa , BET , C1 , C2 , c20 , CLUM , COMPTT , Deltal , DPH , 
     &     DPHdot , DPHesc , dy , ein , eout , eta , Extht , REL , t1 , 
     &     t2 , t3
      REAL tau , TAUabs , Tautom , Theta , w , w1 , w2 , x12 , x23 , 
     &     x32 , Xrs , ynew , yp
      INTEGER IMAx , IMIns , ITEr , j , jj , jj1 , Jmax , JMC2 , jx1 , 
     &        jx2
      COMMON /NTEE_THERM/ DPH(900) , DPHesc(900) , DPHdot(900) , 
     &                    TAUabs(900) , REL(900) , BET(900) , C1(900) , 
     &                    C2(900)
      COMMON /INTNT / IMIns , IMAx , JMC2 , ITEr
      REAL a(900) , b(900) , c(900) , f1(900) , X(900)
      REAL d(900) , alp(900) , g(900) , gam(900) , u(900)
c u(x) is the dimensionless photon occupation number
c jmax is the maximum electron photon energy
c jmc2 is defined by x(jmc2)=1.0
 
      c20 = Tautom/Deltal
      x12 = 1.2
c x12 is the value of x separating region 1 from
c region 2. For larger x, photons are simply absorbed in
c relativistic scatterings and the K-equation is not used
      jx1 = JMC2 + LOG(x12)/Deltal + 1
c x23 separates regions where the first and the second order equations are
c used; it is not used when a 0-theta dispersion term is added to temperature
      x23 = MIN(1.2,0.2+80*Theta)
      jx2 = JMC2 + LOG(x23)/Deltal + 1
c compute input energy, 'ein'
      DO 100 j = 1 , Jmax
         f1(j) = (DPHdot(j)-DPH(j)*TAUabs(j))*X(j)**2
 100  CONTINUE
      ein = CLUM(f1,1,Jmax,Deltal*Xrs)
 
c determine u in region 2, j.ge.jx1
      DO 200 j = jx1 , Jmax
         tau = TAUabs(j) + Tautom*REL(j)
         u(j) = DPHdot(j)/X(j)/X(j)/(tau+BET(j)*Tautom)
 200  CONTINUE
 
c  the first-order equation:
c  the boundary value is at the point next to x(jx1)
c  set jx2=jx1 when removing this region
      DO 300 j = 1 , jx1 - jx2
         jj = jx1 - j
         jj1 = jj + 1
         w = X(jj1)
         eta = -w*DPHdot(jj1)
     &         /Tautom + w**3*(TAUabs(jj1)+BET(jj1)*Tautom)*u(jj1)
     &         /Tautom
         dy = -eta*Deltal
         yp = u(jj1)*C1(jj1) + dy
         ynew = C1(jj1)*u(jj1)
     &          + 0.5*(dy-Deltal*(-X(jj)*DPHdot(jj)/Tautom+X(jj)
     &          **3*(TAUabs(jj)+BET(jj)*Tautom)*yp/C1(jj)/Tautom))
         u(jj) = ynew/C1(jj)
 300  CONTINUE
c determine u in region 1, 1.ge.j.lt.jx2
c define coefficients going into equation
c a(j)*u(j+1)+b(j)*u(j)+c(j)*u(j-1)=d(j)
      DO 400 j = 2 , jx2 - 1
         w1 = SQRT(X(j)*X(j+1))
         w2 = SQRT(X(j-1)*X(j))
c  w1 is x(j+1/2)
c  w2 is x(j-1/2)
         a(j) = -c20*C2(j)*(Theta/Deltal/w1+0.5)
         t1 = -c20*C2(j)*(0.5-Theta/Deltal/w1)
         t2 = c20*C2(j-1)*(Theta/Deltal/w2+0.5)
         t3 = X(j)**3*(TAUabs(j)+Tautom*BET(j))
         b(j) = t1 + t2 + t3
         c(j) = c20*C2(j-1)*(0.5-Theta/Deltal/w2)
         d(j) = X(j)*DPHdot(j)
c  the 0-theta F-P eq.
c       a(j)=-c20*w1**4*((c2(j)+theta)/deltal/w1+0.5*c1(j))
c       t1=-c20*w1**4*(0.5*c1(j)-(c2(j)+theta)/deltal/w1)
c       t2=c20*w2**4*((c2(j-1)+theta)/deltal/w2+0.5*c1(j-1))
c       c(j)=c20*w2**4*(0.5*c1(j-1)-(c2(j-1)+theta)/deltal/w2)
 400  CONTINUE
 
c define constants going into boundary terms
c  u(1)=aa*u(2) (zero flux at lowest energy)
c  u(jx2) given from region 2 above
      x32 = SQRT(X(1)*X(2))
      aa = (Theta/Deltal/x32+0.5)/(Theta/Deltal/x32-0.5)
 
c invert tridiagonal matrix
      alp(2) = b(2) + c(2)*aa
      gam(2) = a(2)/alp(2)
      DO 500 j = 3 , jx2 - 1
         alp(j) = b(j) - c(j)*gam(j-1)
         gam(j) = a(j)/alp(j)
 500  CONTINUE
      g(2) = d(2)/alp(2)
      DO 600 j = 3 , jx2 - 2
         g(j) = (d(j)-c(j)*g(j-1))/alp(j)
 600  CONTINUE
      g(jx2-1) = (d(jx2-1)-a(jx2-1)*u(jx2)-c(jx2-1)*g(jx2-2))/alp(jx2-1)
      u(jx2-1) = g(jx2-1)
      DO 700 j = 3 , jx2 - 1
         jj = jx2 + 1 - j
         u(jj) = g(jj) - gam(jj)*u(jj+1)
 700  CONTINUE
      u(1) = aa*u(2)
 
c compute new value of dph(x) and new value of dphesc(x)
      DO 800 j = 1 , Jmax
         DPH(j) = X(j)*X(j)*u(j)
         DPHesc(j) = DPH(j)*BET(j)*Tautom
         f1(j) = DPHesc(j)*X(j)**2
 800  CONTINUE
c  compute output energy
      eout = CLUM(f1,1,Jmax,Deltal*Xrs)
c  the final output energy is larger by the energy of freely escaping
c  annihilation photons
 
c determine revised temperature based on new value of u
      Theta = COMPTT(DPH,jx1,Jmax,X,Deltal,REL,Extht/Tautom,Theta)
      RETURN
c      write(6,29) theta, ein, eout
99001 FORMAT (10x,'Theta= ',1pe9.2,5x,'P_in=',e9.2,5x,'P_out=',e9.2)
      END
**==comptt.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
      FUNCTION COMPTT(Dph,Jx1,Jmax,X,Deltal,Rel,Z8,Theta)
c     calculates the Compton temperature based on the internal photon
c     density dph
      IMPLICIT NONE
      REAL COMPTT , Deltal , sum , Theta , w , z , z7 , Z8 , zjx1
      INTEGER j , Jmax , Jx1
      REAL X(Jmax) , Dph(Jmax) , Rel(Jmax) , f(900) , f1(900) , f2(900)
      REAL*8 z1 , z2 , z3 , z4 , z5 , z6
c     for the 0-theta F-P eq.
c     common /fp/ d1(900),d2(900)
      DO 100 j = 1 , Jx1
         w = X(j)
c     for the 0-theta F-P eq.
c       f(j)=dph(j)*w**2*d1(j)
c       f1(j)=dph(j)*w**3*d2(j)
         z = 1/(1+4.6*w+1.1*w*w)
         f(j) = Dph(j)*w**2*z
         f1(j) = -Dph(j)*w**3*z*z*(4.6+2.2*w)
 100  CONTINUE
      zjx1 = 1/(1+4.6*X(Jx1)+1.1*X(Jx1)*X(Jx1))
      DO 200 j = Jx1 , Jmax
         w = X(j)
         f2(j) = Dph(j)*w**2*Rel(j)
 200  CONTINUE
      sum = X(1)*f(1) + X(Jx1)*f(Jx1)
      DO 300 j = 2 , Jx1 - 1
         sum = sum + 2.*X(j)*f(j)
 300  CONTINUE
      z1 = sum*Deltal/2.
      sum = f(1) + f(Jx1)
      DO 400 j = 2 , Jx1 - 1
         sum = sum + 2.*f(j)
 400  CONTINUE
      z2 = sum*4.*Deltal/2.
      sum = f1(1) + f1(Jx1)
      DO 500 j = 2 , Jx1 - 1
         sum = sum + 2.*f1(j)
 500  CONTINUE
      z3 = sum*Deltal/2.
      sum = f2(Jx1) + f2(Jmax)
      DO 600 j = Jx1 + 1 , Jmax - 1
         sum = sum + 2.*f2(j)
 600  CONTINUE
      z4 = sum*Deltal/2.
      z5 = zjx1*X(Jx1)**2*Dph(Jx1)
      z6 = z5*X(Jx1)
c  the derivative term (neglected in LZ87)
      z7 = (Dph(Jx1)/X(Jx1)**2-Dph(Jx1-1)/X(Jx1-1)**2)/(X(Jx1)-X(Jx1-1))
     &     *X(Jx1)**5*zjx1
c  comptt is the Compton temperature
      COMPTT = (z1-z6+z4+Z8)/(z2+z3-z5+z7)
c  the average of the previous and the new temperature is used to avoid
c   oscillations
      COMPTT = (COMPTT+Theta)/2
c  theta>0.15 causes instabilities; the method is not applicable then
c        write(6,*) 'Warning: comptt=',comptt
      IF ( COMPTT.GT.0.15 .OR. COMPTT.LT.1.E-10 ) COMPTT = 0.15
      RETURN
      END
**==annsp.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
      FUNCTION ANNSP(X,Temp,Tautom,Taup)
c     This program calculates the annihilation spectrum from a thermal
c     pure pair plasma of the total Thomson depth of tautom, which includes
c     the ionization electrons of the depth taup. (tautom=taup for pure
c     pair plasma.)
c     Using the formulae by Svensson 1983.
c     x and temp are in units of m c**2. The result gives photon number
c     spectrum in units of sigma(Thomson) c m c**2/ m c**2
      IMPLICIT NONE
      REAL a1 , a2 , ANNSP , cc , pi , Taup , Tautom , Temp , X , y , y4
      pi = 3.14159
      a1 = (X/Temp)**2/2/(1+2.0049/Temp+1.4774/Temp/Temp+pi/(2*Temp)**3)
      y = X*Temp
      IF ( y.LE.4. ) THEN
         a2 = pi/2*SQRT(pi/y)*EXP((2-X-1/X)/Temp)
         IF ( y.LE.0.25 ) THEN
            y4 = 4*y
            cc = 1 + 0.5288*y4 - 0.4483*y4**2 + 0.1643*y4**3
         ELSEIF ( y.LE.1. ) THEN
            cc = 1.125 + 0.6600*y - 0.7972*y**2 + 0.3060*y**3
         ELSE
            cc = 0.9049 + 1.1613/y - 1.2487/y**2 + 0.4770/y**3
         ENDIF
      ELSE
         a2 = pi/y*EXP((2-X)/Temp)*(LOG(4*0.56146*y)-1)
         y4 = 4/y
         cc = 1 + 0.5289*y4 - 0.8254*y4**2 + 0.9811*y4**3 - 0.3895*y4**4
      ENDIF
c  3/32/pi follow from assuming tautom=R sigma_T (n+ + n-)
      ANNSP = a1*a2*cc/Temp*3/32./pi*(Tautom**2-Taup**2)
      RETURN
      END
**==zerocn.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
c      FUNCTION GT0(CONST,P,RADIUS,B0)
c      implicit real (a-h,o-z)
c      implicit integer (i-n)
C  physical constants
c      COMMON /PHYSNT/ A0,SIGMAT,VLIGHT,bcrit,xlambd,xme,pi,xrs
C
C  THE NEW TURNOVER FREQUENCY
C
c      XTURN=(CONST*3.**((3+P)/2)/16.*137.*B0**(1+P/2))
c     >**(1/(2+P/2))
C  AND THE CORRESPONDING LORENTZ FACTOR
c      GT0=sqrt(3*XTURN/4/B0)
c      RETURN
c      END
 
      SUBROUTINE ZEROCN(Emagn,Aa,Cn,Cndis)
c    computes the initial parameters of the electron distribution
c  emagn -  the magnetic energy density
c  aa - the proportionality constant in the initial electron injection
c  cn - proportionality constant in the initial electron distribution
c      above gt
c  cndis - the same below gt
      IMPLICIT NONE
      REAL A0 , Aa , BCRit , BFIeld , Cn , Cndis , CONSTK , edens , 
     &     Emagn , EXP0 , fi , GAMma0 , GMAx , GMIn , GT , OMEga , PI , 
     &     RADius , SIGmat , TEMpbb
      REAL VLIght , XLAmbd , XLBb , XLEl , XME , XRS
C  physical constants
      COMMON /PHYSNT/ A0 , SIGmat , VLIght , BCRit , XLAmbd , XME , PI , 
     &                XRS
c  general parameters:
      COMMON /PARNT / GMIn , GMAx , EXP0 , BFIeld , TEMpbb , OMEga , 
     &                RADius , XLEl , XLBb , GAMma0 , GT
C
C  AA - ELECTRON INJECTION COEFFICIENT:
C    Q(GAMMA)=AA*GAMMA**(-EXP0)   CM**(-3)/SEC
c     take gamma0 as the last argument for electron injection, 0 for pair
      Aa = XLEl/XRS/CONSTK(EXP0,GMIn,GMAx,GAMma0)
      Emagn = BFIeld**2/8/PI/XME/VLIght**2*SIGmat*RADius
C     EDENS - DIMENSIONLESS ENERGY DENSITY OF MAGNETIC FIELD AND SOFT PHOTONS
c     for models with bremsstrahlung only, the initial energy density is
c     increased by alpha(fine)
      edens = Emagn + XLBb/XRS + 1/137.
C
      IF ( ABS(BFIeld).GT.1.E-15 ) THEN
C       FI - COEFFICIENT IN THE BINOMIAL SOLUTION FOR
c       INITIAL ELECTRON DISTRIBUTION (Zdziarski 1985, preprint)
         fi = 3*Emagn/edens**2
c       cn,cndis - proportionality constants in the electron distributions
         Cn = (SQRT(1+fi)-1)*edens/Emagn/2/CONSTK(EXP0,GMIn,GMAx,GT)/Aa
         Cndis = 1/edens
      ELSE
         Cn = 1/edens
      ENDIF
      RETURN
      END
**==eldis.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
      SUBROUTINE ELDIS(G,Aa,Cn,Cndis,Qint0,Del)
      IMPLICIT NONE
      REAL Aa , BFIeld , Cn , Cndis , EXP0 , GAMma0 , GMAx , GMIn , GT , 
     &     OMEga , RADius , TEMpbb , XLBb , XLEl
      INTEGER i , IMAx , IMIns , ITEr , JMC2
      REAL G(IMAx) , Qint0(IMAx) , Del(IMAx)
      COMMON /PARNT / GMIn , GMAx , EXP0 , BFIeld , TEMpbb , OMEga , 
     &                RADius , XLEl , XLBb , GAMma0 , GT
      COMMON /INTNT / IMIns , IMAx , JMC2 , ITEr
c  computes an INTEGRAL OVER THE the electron injection rate qint0
C  and the initial electron distribution del0, del1
C
      DO 100 i = 1 , IMAx
         IF ( EXP0.NE.0 ) THEN
            Qint0(i) = Aa*(MAX(G(i),GMIn)**(-EXP0+1)-GMAx**(-EXP0+1))
     &                 /(EXP0-1)
         ELSE
            Qint0(i) = Aa
         ENDIF
C       NORMALIZATION FOR COOLING:
         IF ( i.LE.IMIns ) THEN
            Del(i) = Qint0(i)*Cndis/(1.333*G(i)**2-1)
         ELSE
            Del(i) = Qint0(i)*Cn/(1.333*G(i)**2-1)
         ENDIF
 100  CONTINUE
      RETURN
      END
**==synchr.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
      SUBROUTINE SYNCHR(Dsyn,Del,G,X,Jsyn,B0,Syncon,Deltal)
      IMPLICIT NONE
      REAL B0 , BFIeld , Deltal , el , EXP0 , gamma , GAMma0 , GMAx , 
     &     GMIn , GT , OMEga , RADius , Syncon , TEMpbb , XLBb , XLEl
      INTEGER i , j , Jsyn
      REAL Dsyn(900) , Del(900) , G(900) , X(Jsyn)
c  general parameters:
      COMMON /PARNT / GMIn , GMAx , EXP0 , BFIeld , TEMpbb , OMEga , 
     &                RADius , XLEl , XLBb , GAMma0 , GT
C  SYNCHROTRON EMISSION:
      DO 100 j = 1 , Jsyn
         gamma = SQRT(0.75*X(j)/B0)
         i = LOG(gamma/GAMma0)/Deltal + 1
c       the interpolated value for the electron distribution
         el = EXP(LOG(Del(i))+LOG(Del(i+1)/Del(i))
     &        /Deltal*LOG(gamma/G(i)))
         Dsyn(j) = Syncon*el/gamma
 100  CONTINUE
      RETURN
      END
**==clum.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
      FUNCTION CLUM(Dph,J1,J2,Const)
      IMPLICIT NONE
      REAL Dph(900) , Const , sum , CLUM
      INTEGER j , J1 , J2
c     calculates a trapezoidal integral to obtain luminosity
c     dph is E F_E
      sum = (Dph(J1)+Dph(J2))/2
      DO 100 j = J1 + 1 , J2 - 1
         sum = sum + Dph(j)
 100  CONTINUE
      CLUM = sum*Const
      RETURN
      END
**==constk.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
      FUNCTION CONSTK(Exp0,Gmin,Gmax,Gt)
      IMPLICIT NONE
      REAL CONSTK , Exp0 , glower , Gmax , Gmin , Gt
C  SUBROUTINE COMPUTES AN INTEGRAL OVER THE INITIAL ELECTRON
C  INJECTION DISTRIBUTION
C  EXP0=0 - DELTA-FUNCTION ELECTRON INJECTION
C  OTHERWISE EXP0>1
C  EXP0=2 - INTEGRAL OF 1/X
C
      IF ( Gmax.LE.Gt ) PAUSE 'gt > gmax'
      IF ( Exp0.EQ.0 ) THEN
         CONSTK = Gmax - Gt
      ELSE
         glower = MAX(Gt,Gmin)
         IF ( Exp0.EQ.2 ) THEN
            CONSTK = LOG(Gmax/glower) - Gt*(1/glower-1/Gmax)
         ELSE
            CONSTK = (Gmax**(-Exp0+2)-glower**(-Exp0+2))/(-Exp0+2)
     &               - Gt*(Gmax**(-Exp0+1)-glower**(-Exp0+1))/(-Exp0+1)
         ENDIF
      ENDIF
      RETURN
      END
**==table.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
      SUBROUTINE TABLE(Dph,X,Jmax,Del,G,N,Phcon)
c     tabulates the results
      IMPLICIT NONE
      INTEGER i , IMAx , IMIns , ITEr , j , Jmax , JMC2 , N
      REAL Phcon
      REAL Dph(Jmax) , Del(IMAx) , X(Jmax) , G(IMAx)
      REAL ELDst(3,900) , PHDst(3,900)
      COMMON /INTNT / IMIns , IMAx , JMC2 , ITEr
      COMMON /NTHPLT/ ELDst , PHDst
      DO 100 j = 1 , Jmax
         PHDst(N,j) = Dph(j)*X(j)**2*Phcon
 100  CONTINUE
      DO 200 i = 1 , IMAx
         ELDst(N,i) = Del(i)*G(i)**2
 200  CONTINUE
      RETURN
      END
**==thbrem.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
      SUBROUTINE THBREM(X,Brth,Brcth,Theta,Tautom,Taup,Deltal,Jmax)
c     bremsstrahlung from thermal electrons
c     tautom is the total Thomson depth, taup is the ionization depth
      IMPLICIT NONE
      REAL besexp , BESSK , Brcth , Brth , Deltal , dp , Taup , Tautom , 
     &     THBR , Theta , X
      INTEGER j , Jmax , jmaxbr
      DIMENSION X(Jmax) , Brth(Jmax)
c     dp is the ratio of positrons to electrons+positrons, n_+/(n_+ + n_-)
      dp = 0.5 - Taup/2/Tautom
c     the maximum x for which the thermal bremsstrahlung is calculated
      jmaxbr = LOG(50*Theta/X(1))/Deltal + 1
c     besexp is K_2(1/theta)*exp(1/theta)
      IF ( Theta.GT.0.99 ) besexp = BESSK(2,1/Theta)*EXP(1/Theta)
      DO 100 j = 1 , jmaxbr
         Brth(j) = THBR(X(j),Theta,dp,Tautom,besexp)
 100  CONTINUE
      DO 200 j = jmaxbr + 1 , Jmax
         Brth(j) = 0
 200  CONTINUE
c     integral thermal cooling rate
      Brcth = (Brth(1)*X(1)**2+Brth(jmaxbr)*X(jmaxbr)**2)/2
      DO 300 j = 2 , jmaxbr - 1
         Brcth = Brcth + Brth(j)*X(j)**2
 300  CONTINUE
      Brcth = Brcth*Deltal
      RETURN
      END
**==thbr.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
      FUNCTION THBR(E,Theta,Dp,Tautom,Besexp)
c     bremsstrahlung photon number spectrum from thermal electrons
c     e is the photon energy and theta the temperature, in units of m_e c^2
c     dp=n+/(n+ + n-)
      IMPLICIT NONE
      REAL bes0 , bes1 , Besexp , BESSK0 , BESSK1 , Dp , E , e1x , ee0 , 
     &     ee1 , ee9 , ep0 , ep1 , ep2 , ep3 , ep4 , ep5 , ep9 , epm , 
     &     EXP1
      REAL f , pol1 , pol2 , Tautom , THBR , Theta , x , x2 , y
      x = E/Theta
C***  CHOICE OF THE APPRIOPRIATE FORMULA
      IF ( Theta.GE.1. ) THEN
C    THE ULTRARELATIVISTIC FORMULAS (THETA.GE.1.)
         y = EXP(-x)
         f = 1/Theta
c      EF=EXP(F)
         e1x = EXP1(x)
         pol1 = 6*x*x + 8*x + 16
         pol2 = pol1 - 16*x
C    ELECTRON-PROTON BREMSSTRAHLUNG
         ep0 = y*2./3./Besexp
         ep1 = LOG(2.)*(pol1*Theta*Theta+(8*x+16)*Theta+8)
         ep2 = e1x*(pol2*Theta*Theta+(-8*x+16)*Theta+8)
         ep3 = EXP1(f)*pol1*Theta*Theta
         ep4 = (-3*x*x+4*x+40)*Theta*Theta + (-4*x+16)*Theta
         ep5 = 3*x*x/(f+x) + EXP1(f+x)*(-3*x*x-4*x)
         ep9 = ep0*(ep1+ep2+ep3+ep4+ep5)
C    ELECTRON-ELECTRON BREMSSTRAHLUNG
         ee9 = y*2./3.*(pol2*e1x+2*(LOG(2*Theta)-.5772)
     &         *pol1+(3*x*x+12*x+56))
C    ELECTRON-POSITRON BREMSSTRAHLUNG
         epm = ee9
      ELSE
C     THE NONRELATIVISTIC FORMULAS (THETA.LE.1.)
         x2 = x/2
C     BESSKi CALCULATES VALUES OF THE K FUNCTION
         bes0 = BESSK0(x2)
         bes1 = BESSK1(x2)
C    ELECTRON-PROTON BREMSSTRAHLUNG
         ep0 = 4.2554/SQRT(Theta)*EXP(-x2)*bes0
         ep1 = ep0*(1+2*Theta+2*Theta**2+E*E*(.51875+.44018*Theta)
     &         +E*bes1/bes0*(.25+1.975*Theta+.073884*E*E))
     &         /(1+1.875*Theta+.82031*Theta*Theta-.30762*Theta**3)
         ep9 = ep1/(1+4.56*E/(168+E))
C    ELECTRON-ELECTRON BREMSSTRAHLUNG
         IF ( E.GE.1. ) THEN
            ee0 = ep9
            ee9 = ee0*(1+3*Theta)
         ELSE
            ee0 = ep0*Theta
            ee1 = ee0*2.2627*(.75+x2*bes1/bes0+.91438/bes0/EXP(x2))
            ee9 = ee1*(1+Theta)
         ENDIF
C    ELECTRON-POSITRON BREMSSTRAHLUNG
         epm = ep9*(1+3*Theta+(2.8284*(1-Theta)-1)/(1+E))
      ENDIF
C    THE FULL FORMULA; THE CONSTANT IS 3*ALPHA/8/PI
      THBR = 8.712E-4*(ep9*(1-2*Dp)+ee9*(Dp*Dp+(1-Dp)*(1-Dp))
     &       /2+epm*Dp*(1-Dp))*Tautom**2/E
      RETURN
      END
**==exp1.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
      FUNCTION EXP1(X)
C*** THE EXPONENTIAL INTEGRAL APPROXIMATION
c    TIMES EXP(X) TO AVOID UNDERFLOWS
      IMPLICIT NONE
      REAL EXP1 , f , s , X , xn , z
      INTEGER n
      IF ( X.LT.1. ) THEN
         f = 1
         s = 0.
         xn = 1
         DO 50 n = 1 , 5
            f = f*n
            xn = -xn*X
            s = s + xn/n/f
 50      CONTINUE
         EXP1 = -.5772 - LOG(X) - s
         EXP1 = EXP1*EXP(X)
      ELSE
         z = (X*X+2.334733*X+.250621)/(X*X+3.330657*X+1.681534)
         EXP1 = z/X
      ENDIF
      RETURN
      END
**==nthbrem.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
      SUBROUTINE NTHBREM(Brnth,Brcnth,G,Del,X,Deltal,Imax,Jmax,Taunth,
     &                   Tautom)
c     Gives bremsstrahlung emissivity from nonthermal electrons
c     interacting with cold plasma.
      IMPLICIT NONE
      REAL Brcnth , BRSIG , Deltal , Taunth , Tautom
      INTEGER i , Imax , iminbr , j , Jmax
      REAL Brnth(Jmax) , G(Imax) , X(Jmax) , Del(Imax)
      DO 100 j = 1 , Jmax
c     it used to be min, which caused problems ???
c     obviously it should be max
         iminbr = MAX(1,INT(LOG((1+X(j))/G(1))/Deltal+1))
c        iold=min(1,int(log((1+x(j))/g(1))/deltal+2))
c        write(6,*) j,jmax,iold,iminbr,imax
         Brnth(j) = (BRSIG(G(iminbr),X(j))*G(iminbr)*Del(iminbr)
     &              +BRSIG(G(Imax),X(j))*G(Imax)*Del(Imax))/2
         DO 50 i = iminbr + 1 , Imax - 1
            Brnth(j) = Brnth(j) + BRSIG(G(i),X(j))*Del(i)*G(i)
 50      CONTINUE
         Brnth(j) = Brnth(j)*Deltal*(Tautom+2*Taunth)
 100  CONTINUE
c     the nonthermal bremsstrahlung integral emissivity
      Brcnth = (Brnth(1)*X(1)**2+Brnth(Jmax)*X(Jmax)**2)/2
      DO 200 j = 2 , Jmax - 1
         Brcnth = Brcnth + Brnth(j)*X(j)**2
 200  CONTINUE
      Brcnth = Brcnth*Deltal
      RETURN
      END
**==brsig.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
      FUNCTION BRSIG(G,X)
c     cross section for relativistic bremsstrahlung
c     in units of sigma_T, 3.485e-3 is 3/2/pi*alpha
c     the electron energy is taken to be the kinetic one only
      IMPLICIT NONE
      REAL BRSIG , e , e2 , G , X
      e = G - 1
      e2 = G - 1 - X
      IF ( e2.GT.0. ) THEN
         BRSIG = 3.485E-3*e2/e*(e2/e+e/e2-.6667)*(LOG(2*e*e2/X)-0.5)/X
      ELSE
         BRSIG = 0
      ENDIF
      IF ( BRSIG.LT.0. ) BRSIG = 0
      RETURN
      END
**==annih.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
      SUBROUTINE ANNIH(Theta,Tautom,Taup,Pprate,Gth,Annlum,Annht)
c     annihilation luminosity, and
c     residual annihilation heating
c     Calculate first the average thermal Lorentz factor
      IMPLICIT NONE
      REAL Annht , Annlum , BESSK , BESSK1 , Gth , Pprate , Taup , 
     &     Tautom , Theta
      IF ( Theta.GT.0.014 ) THEN
         Gth = 3*Theta + BESSK1(1/Theta)/BESSK(2,1/Theta)
      ELSE
         Gth = 1 + 1.5*Theta
      ENDIF
c     annihilation luminosity - a fit by Svensson (1982a)
      Annlum = 3/16./(1/(1+6*Theta)+Theta/(LOG(1.12*Theta+1)+0.25))
     &         *(Tautom**2-Taup**2)
c     residual heating
c  ********
c     replace pprate by the actual annihilation rate, which is important
c     for nonzero escape rate (e.g., use a fit by Svensson)
c     THIS IS NOT DONE YET
      Annht = Gth*Pprate - Annlum
      RETURN
      END
**==alpha.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
      FUNCTION ALPHA(X,Dph,J1,J2)
c     fits a power law between x(j1) and x(j2) to dph(j)
      IMPLICIT NONE
      REAL a , ALPHA , b , chi2 , siga , sigb
      INTEGER j , J1 , J2
      REAL X(J2) , Dph(J2)
      REAL x4(900) , y(900)
      DO 100 j = 1 , J2 - J1 + 1
         x4(j) = LOG(X(J1+j-1))
         y(j) = LOG(Dph(J1+j-1))
 100  CONTINUE
      CALL MYFIT(x4,y,J2-J1+1,a,b,siga,sigb,chi2)
      ALPHA = b
      RETURN
      END
**==myfit.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
      SUBROUTINE MYFIT(X,Y,Ndata,A,B,Siga,Sigb,Chi2)
c     based on Numerical Recipes FIT
      IMPLICIT NONE
      REAL A , B , Chi2 , Siga , Sigb , sigdat , ss , st2 , sx , sxoss , 
     &     sy , t
      INTEGER i , Ndata
      REAL X(Ndata) , Y(Ndata)
c  only phe part for MWT=0 used (see NR)
      sx = 0.
      sy = 0.
      st2 = 0.
      B = 0.
      DO 100 i = 1 , Ndata
         sx = sx + X(i)
         sy = sy + Y(i)
 100  CONTINUE
c  FLOAT changed to REAL (AZ)
      ss = REAL(Ndata)
      sxoss = sx/ss
      DO 200 i = 1 , Ndata
         t = X(i) - sxoss
         st2 = st2 + t*t
         B = B + t*Y(i)
 200  CONTINUE
      B = B/st2
      A = (sy-sx*B)/ss
      Siga = SQRT((1.+sx*sx/(ss*st2))/ss)
      Sigb = SQRT(1./st2)
      Chi2 = 0.
      DO 300 i = 1 , Ndata
         Chi2 = Chi2 + (Y(i)-A-B*X(i))**2
 300  CONTINUE
      sigdat = SQRT(Chi2/(Ndata-2))
      Siga = Siga*sigdat
      Sigb = Sigb*sigdat
      RETURN
      END
**==bessi0.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
      FUNCTION BESSI0(X)
      IMPLICIT NONE
      REAL ax , BESSI0 , X
      REAL*8 y , p1 , p2 , p3 , p4 , p5 , p6 , p7 , q1 , q2 , q3 , q4 , 
     &       q5 , q6 , q7 , q8 , q9
      DATA p1 , p2 , p3 , p4 , p5 , p6 , p7/1.0D0 , 3.5156229D0 , 
     &     3.0899424D0 , 1.2067492D0 , 0.2659732D0 , 0.360768D-1 , 
     &     0.45813D-2/
      DATA q1 , q2 , q3 , q4 , q5 , q6 , q7 , q8 , q9/0.39894228D0 , 
     &     0.1328592D-1 , 0.225319D-2 , -0.157565D-2 , 0.916281D-2 , 
     &     -0.2057706D-1 , 0.2635537D-1 , -0.1647633D-1 , 0.392377D-2/
      IF ( ABS(X).LT.3.75 ) THEN
         y = (X/3.75)**2
         BESSI0 = p1 + y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7)))))
      ELSE
         ax = ABS(X)
         y = 3.75/ax
         BESSI0 = (EXP(ax)/SQRT(ax))
     &            *(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*(q7+y*(q8+y*q9)))
     &            )))))
      ENDIF
      RETURN
      END
**==bessi1.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
      FUNCTION BESSI1(X)
      IMPLICIT NONE
      REAL ax , BESSI1 , X
      REAL*8 y , p1 , p2 , p3 , p4 , p5 , p6 , p7 , q1 , q2 , q3 , q4 , 
     &       q5 , q6 , q7 , q8 , q9
      DATA p1 , p2 , p3 , p4 , p5 , p6 , p7/0.5D0 , 0.87890594D0 , 
     &     0.51498869D0 , 0.15084934D0 , 0.2658733D-1 , 0.301532D-2 , 
     &     0.32411D-3/
      DATA q1 , q2 , q3 , q4 , q5 , q6 , q7 , q8 , q9/0.39894228D0 , 
     &     -0.3988024D-1 , -0.362018D-2 , 0.163801D-2 , -0.1031555D-1 , 
     &     0.2282967D-1 , -0.2895312D-1 , 0.1787654D-1 , -0.420059D-2/
      IF ( ABS(X).LT.3.75 ) THEN
         y = (X/3.75)**2
         BESSI1 = X*(p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7))))))
      ELSE
         ax = ABS(X)
         y = 3.75/ax
         BESSI1 = (EXP(ax)/SQRT(ax))
     &            *(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*(q7+y*(q8+y*q9)))
     &            )))))
      ENDIF
      RETURN
      END
**==bessk0.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
      FUNCTION BESSK0(X)
      IMPLICIT NONE
      REAL BESSI0 , BESSK0 , X
      REAL*8 y , p1 , p2 , p3 , p4 , p5 , p6 , p7 , q1 , q2 , q3 , q4 , 
     &       q5 , q6 , q7
      DATA p1 , p2 , p3 , p4 , p5 , p6 , p7/ - 0.57721566D0 , 
     &     0.42278420D0 , 0.23069756D0 , 0.3488590D-1 , 0.262698D-2 , 
     &     0.10750D-3 , 0.74D-5/
      DATA q1 , q2 , q3 , q4 , q5 , q6 , q7/1.25331414D0 , 
     &     -0.7832358D-1 , 0.2189568D-1 , -0.1062446D-1 , 0.587872D-2 , 
     &     -0.251540D-2 , 0.53208D-3/
      IF ( X.LE.2.0 ) THEN
         y = X*X/4.0
         BESSK0 = (-LOG(X/2.0)*BESSI0(X))
     &            + (p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7))))))
      ELSE
         y = (2.0/X)
         BESSK0 = (EXP(-X)/SQRT(X))
     &            *(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*q7))))))
      ENDIF
      RETURN
      END
**==bessk1.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
      FUNCTION BESSK1(X)
      IMPLICIT NONE
      REAL BESSI1 , BESSK1 , X
      REAL*8 y , p1 , p2 , p3 , p4 , p5 , p6 , p7 , q1 , q2 , q3 , q4 , 
     &       q5 , q6 , q7
      DATA p1 , p2 , p3 , p4 , p5 , p6 , p7/1.0D0 , 0.15443144D0 , 
     &     -0.67278579D0 , -0.18156897D0 , -0.1919402D-1 , 
     &     -0.110404D-2 , -0.4686D-4/
      DATA q1 , q2 , q3 , q4 , q5 , q6 , q7/1.25331414D0 , 
     &     0.23498619D0 , -0.3655620D-1 , 0.1504268D-1 , -0.780353D-2 , 
     &     0.325614D-2 , -0.68245D-3/
      IF ( X.LE.2.0 ) THEN
         y = X*X/4.0
         BESSK1 = (LOG(X/2.0)*BESSI1(X)) + (1.0/X)
     &            *(p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7))))))
      ELSE
         y = 2.0/X
         BESSK1 = (EXP(-X)/SQRT(X))
     &            *(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*q7))))))
      ENDIF
      RETURN
      END
**==bessk.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
      FUNCTION BESSK(N,X)
      IMPLICIT NONE
      REAL BESSK , BESSK0 , BESSK1 , bk , bkm , bkp , tox , X
      INTEGER j , N
      IF ( N.LT.2 ) PAUSE 'bad argument N in BESSK'
      tox = 2.0/X
      bkm = BESSK0(X)
      bk = BESSK1(X)
      DO 100 j = 1 , N - 1
         bkp = bkm + j*tox*bk
         bkm = bk
         bk = bkp
 100  CONTINUE
      BESSK = bk
      RETURN
      END
**==greentee.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
c ------------------------------------------------------------------ c
c ------------------------------------------------------------------ c
 
      SUBROUTINE GREENTEE(X,Xmin,Jmax,Deltal,Spnth,Spref,Sptot,Scale,
     &                    Dphth,Sphth,Xrs,Xlbb,Xm,Ab0)
c     calculates the reflected spectrum from pair cascades using Green's
c     functions of White et al 88, Lightman and White 88
      IMPLICIT NONE
      REAL Ab0 , albedo , cadd , clnth , clref , CLUM , Deltal , Scale , 
     &     scdir , scref , Xm , Xmin
      INTEGER j
      INTEGER Jmax
      REAL Sptot(900) , X(900) , Spref(900) , Spnth(900) , Dphth(900)
      REAL Sphth(900) , Xlbb , Xrs
c
c     scale=1 for 2pi solid angle
c     the scaling factor; 1 corresponds to seeing equal
c     contributions from the reflected and direct spectra
c     the scaling for the direct component is 1/(1+scale)
c     the scaling for the reflected component is scale/(1+scale)
c     the sum is unity: conservation of energy
c      scdir=1/(1+scale)
c      scref=scale/(1+scale)
c
c     The above assumptions cause problems in xspec. The normalization cannot
c     change from one iteration to another. Therefore we keep constant the
c     normalization of the direct component as below:    (29.10.1994)
      scdir = 1
      scref = Scale
c
      CALL GREENCE(X,Jmax,Spnth,Spref,Xm,Ab0)
c
c     compute the scaled reflected power
      clref = CLUM(Spref,1,Jmax,Deltal)*scref
c     the power in the SCALED incident spectrum
      clnth = CLUM(Spnth,1,Jmax,Deltal)*scdir
c     integrated albedo (1. not scaled; 2. including the incident soft photons)
      albedo = clref/clnth/Scale
c      write(6,99) clref,clnth,albedo
c 99   format(/,' REFLECTION:',/,
c     >  ' The scaled reflected luminosity=',1pe9.2,/,
c     >  ' the scaled incident luminosity=',e9.2,/,
c     >  ' the unscaled albedo (including l_s)=',e9.2)
c
c   The total spectrum, E F_E, taking into account rescaling of the direct
c   component:
 
c   add the contribution from blackbody photons from absorption of
c   the incident radiation - reradiated by the disk
c   it is assumed that this radiation can escape without interaction
c   with the nonthermal source. This is consistent with the assumption that
c   the reflected component is simply added to the direct component.
c   Thus, the total luminosity should be (l_th+l_nth+l_s)
c   For that, one needs to multiply the soft photon spectrum by cltot/xlbb
c   Furthermore, one needs to convert from photons to E F_E and normalize
C   Note that ZC91 used different assumptions.
C
c     here is the scaled refl. component + the scaled direct
c     component + the reprocessed component reemerging as blackbody
      cadd = (1-albedo)*clnth/Xlbb*Scale*Xrs
      DO 100 j = 1 , Jmax
         Spref(j) = Spref(j)*scref
         Spnth(j) = Spnth(j)*scdir
         Sphth(j) = Dphth(j)*X(j)**2*cadd
         Sptot(j) = Spref(j) + Spnth(j) + Sphth(j)
 100  CONTINUE
C
      RETURN
      END
**==spp.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
c ------------------------------------------------------------------ c
 
      REAL FUNCTION SPP(Y,Xnth,Jnth,Spnth)
 
      IMPLICIT NONE
      INTEGER Jnth
      REAL Y , Xnth(Jnth) , Spnth(Jnth)
      INTEGER il , ih
      REAL xx
      SAVE ih
      DATA ih/2/
 
      xx = 1/Y
      IF ( xx.LT.Xnth(ih) ) ih = 2
 100  IF ( xx.GT.Xnth(ih) ) THEN
         ih = ih + 1
         IF ( ih.LT.Jnth ) GOTO 100
      ENDIF
      il = ih - 1
      SPP = Spnth(il) + (Spnth(ih)-Spnth(il))*(xx-Xnth(il))
     &      /(Xnth(ih)-Xnth(il))
 
      RETURN
      END
**==greence.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
c ------------------------------------------------------------------ c
 
      SUBROUTINE GREENCE(X,Jmax,Spnth,Spref,Xm,Ab0)
c     calculates the reflected spectrum
c     ab0 - iron log10 abundance relative to hydrogen
      IMPLICIT NONE
      REAL Ab0 , ddy , dy , dym , fjc , GNR , GRXYH , GRXYL , SIGMABFE , 
     &     SPP , sr , srp , xjd , xjl , xkabs , Xm , xmin , xmo , xnor , 
     &     xrefmax
      REAL xtransh , xtransl , y , y0 , ymin
      INTEGER i , j , Jmax , jmin , jrefmax , jtransh , jtransl , nc , 
     &        ncp
      REAL X(900) , Spnth(900) , Spref(900)
      REAL pm1(19) , pmx(26) , apf, ap(3)
      
      SAVE xnor
      DATA xnor/0.0/
 
c  precision factor for Green's function integration
      ncp = 100
c  ranges for different methods
      xmin = 2.E-4
      xtransl = 0.01957
      xtransh = 0.02348
      ymin = .03333
c  angle dependent parameters
      xmo = MAX(.05,MIN(.95,Xm))
      CALL PM1Y(xmo,pm1)
      CALL PMXY(xmo,pmx)
      ap(2) = apf(xmo)
      ap(3) = 0.381
      xrefmax = 1/(ymin+pmx(26))
 
      jmin = 0
      DO 100 j = 1 , Jmax
         IF ( X(j).GT.xmin ) GOTO 200
         jmin = j
 100  CONTINUE
 200  jtransl = 0
      DO 300 j = MAX(1,jmin) , Jmax
         IF ( X(j).GT.xtransl ) GOTO 400
         jtransl = j
 300  CONTINUE
 400  jtransh = 0
      DO 500 j = MAX(1,jtransl) , Jmax
         IF ( X(j).GT.xtransh ) GOTO 600
         jtransh = j
 500  CONTINUE
 600  jrefmax = 0
      DO 700 j = MAX(1,jtransh) , Jmax
         IF ( X(j).GT.xrefmax ) GOTO 800
         jrefmax = j
 700  CONTINUE
 
 800  DO 900 j = 1 , jmin
         Spref(j) = 0
 900  CONTINUE
c-----------------------
      DO 1000 j = jmin + 1 , jtransl
         xkabs = SIGMABFE(j,X,jtransh,Ab0,xnor)
         Spref(j) = SPP(1/X(j),X,Jmax,Spnth)*GNR(xkabs,xmo)
 1000 CONTINUE
c-----------------------
      IF ( jmin.GE.jtransl ) xkabs = SIGMABFE(1,X,MAX(1,jtransh),Ab0,
     &                               xnor)
      ap(1) = xnor/1.21
      xjl = ALOG(xtransl)
      xjd = ALOG(xtransh) - xjl
      DO 1100 j = jtransl + 1 , jtransh
         fjc = .5*SIN(3.14159*((ALOG(X(j))-xjl)/xjd-.5))
         xkabs = SIGMABFE(j,X,jtransh,Ab0,xnor)
         y = 1/X(j)
         Spref(j) = SPP(y,X,Jmax,Spnth)*GNR(xkabs,xmo)
         dym = y - ymin
         dy = MIN(2.,dym)
         ddy = (dy-pmx(26))/(ncp+1)
         y0 = y - dy
         sr = SPP(y0,X,Jmax,Spnth)*GRXYL(pm1,pmx,dy,y0,ap)
         dy = pmx(26)
         y0 = y - dy
         sr = .5*(sr+SPP(y0,X,Jmax,Spnth)*GRXYL(pm1,pmx,dy,y0,ap))
         DO 1050 i = 1 , ncp
            dy = dy + ddy
            y0 = y - dy
            sr = sr + SPP(y0,X,Jmax,Spnth)*GRXYL(pm1,pmx,dy,y0,ap)
 1050    CONTINUE
         sr = sr*ddy
         IF ( dym.GT.2. ) THEN
            ddy = dym - 2
            nc = INT(ncp*ddy/(dym-pmx(26)))
            ddy = ddy/(nc+1)
            dy = dym
            y0 = y - dy
            srp = SPP(y0,X,Jmax,Spnth)*GRXYH(pm1,pmx,dy,y0,ap)
            dy = 2
            y0 = y - dy
            srp = .5*(srp+SPP(y0,X,Jmax,Spnth)*GRXYH(pm1,pmx,dy,y0,ap))
            DO 1060 i = 1 , nc
               dy = dy + ddy
               y0 = y - dy
               srp = srp + SPP(y0,X,Jmax,Spnth)*GRXYH(pm1,pmx,dy,y0,ap)
 1060       CONTINUE
            sr = sr + srp*ddy
         ENDIF
         Spref(j) = (.5-fjc)*Spref(j) + (.5+fjc)*sr*X(j)
 1100 CONTINUE
c-----------------------
      DO 1200 j = jtransh + 1 , jrefmax
         y = 1/X(j)
         dym = y - ymin
         dy = MIN(2.,dym)
         ddy = (dy-pmx(26))/(ncp+1)
         y0 = y - dy
         sr = SPP(y0,X,Jmax,Spnth)*GRXYL(pm1,pmx,dy,y0,ap)
         dy = pmx(26)
         y0 = y - dy
         sr = .5*(sr+SPP(y0,X,Jmax,Spnth)*GRXYL(pm1,pmx,dy,y0,ap))
         DO 1150 i = 1 , ncp
            dy = dy + ddy
            y0 = y - dy
            sr = sr + SPP(y0,X,Jmax,Spnth)*GRXYL(pm1,pmx,dy,y0,ap)
 1150    CONTINUE
         sr = sr*ddy
         IF ( dym.GT.2. ) THEN
            ddy = dym - 2
            nc = INT(ncp*ddy/(dym-pmx(26)))
            ddy = ddy/(nc+1)
            dy = dym
            y0 = y - dy
            srp = SPP(y0,X,Jmax,Spnth)*GRXYH(pm1,pmx,dy,y0,ap)
            dy = 2
            y0 = y - dy
            srp = .5*(srp+SPP(y0,X,Jmax,Spnth)*GRXYH(pm1,pmx,dy,y0,ap))
            DO 1160 i = 1 , nc
               dy = dy + ddy
               y0 = y - dy
               srp = srp + SPP(y0,X,Jmax,Spnth)*GRXYH(pm1,pmx,dy,y0,ap)
 1160       CONTINUE
            sr = sr + srp*ddy
         ENDIF
         Spref(j) = sr*X(j)
 1200 CONTINUE
c----------------------
      DO 1300 j = jrefmax + 1 , Jmax
         Spref(j) = 0
 1300 CONTINUE
 
      RETURN
      END
**==totlxse.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
C----------------------------------------------------------------------
C
      FUNCTION TOTLXSE(Ab,In,X,N,Fl1,Fl2,Xnor, A, Toth)
c
c     - Added input args A and Toth after moving dynamic array allocation
c       to xsnteea.cxx Jan 2009 (CG)
c
c     *** this version with fully ionized H and He!  AAZ, 18.05.94
c     Optimized version: calculetes cross section for abundances
c     contained in the vector AB at energy in 511.keV units given as
c     an element numbered in of the vector x; n is the length of x.
c     Flag fl1=.true. must indicate changes of AB, and fl2=.true.
c     changes of the energy vector x. For energy >10.keV the law
c     xnor*e^-3 is used, xnor is an output parameter.
c     Note: result in sigma_Thomson units.
C
C     Reference:
C      Monika Balucinska-Church and Dan McCammon
C      "Photoelectric Absorption Cross Sections with Variable Abundances"
C      Ap.J. 400, 699 (1992)
C
C
C     Description:
C     Calculates the effective absorption cross section in units of
c     sigma_Thomson per hydrogen atom at energy E in eV for the
c     abundances of the elements specified in vector AB
C
C     Method:
C     Calls seventeen functions that calculate the mass absorption coeffs
C     in cm**2/g for the following elements: H, He, C, N, O, Ne, Na, Mg,
C     Al, Si, S, Cl, Ar, Ca, Cr, Fe, Ni.
C
C     Deficiencies:
C     Works only in the range of energy from 30 eV to 10,000 eV.
C
C     Bugs:
C     None known -- please report any problems to authors
C
C     Authors:
C     Dan McCammon               (47413::MCCAMMON)
C     Monika Balucinska-Church   (19775::MBC)
C
C     History:
C     8.8.91  - original
C     8.1.92  - modified to remove VAX fortran77 extensions
C     12.3.93 - function HELIUM has been replaced by HELIUM_V2 with
C               Fano line profile
C     21.9.93 - Names of functions simplified: TOTLXS_V2 to TOTLX2,
C               HELIUM_V2 to HEL2.
C     24.1.94 - Names of functions simplified: TOTLX2 to TOTLXS,
C               HEL2 to HELIUM.
C
C     Parameters:
C     E - energy in eV
C     AB - vector of length 17 of Log10 abundances relative to
C          hydrogen = AB(1).  N.B. The order of the elements in AB
C          is as follows: H,He,C,N,O,Ne,Na,Mg,Al,Si,S,Cl,Ar,Ca,Cr,Fe,Ni.
C     TOTLX2 - effective cross section in cm**2/H atom for assumed
C             abundances of cosmic elements.  Note that this will be per
C             hydrogen atom for the relative abundances given in AB.
C
C     Type Definitions:


      INTEGER i, ierr
      REAL Xnor , xnors
      CHARACTER contxt*80
C
C     Local variables:
      INTEGER k
C          ( index through elements)
      REAL X(*) , ap(17)
      REAL A(N*17), Toth(N)
      INTEGER In , N
c          x  - energy points
c          in - index for x
c          n  - number of energy points
C          ( mass absorption cross sections in cm**2/g
C           for the cosmic elements -- in the same order as for AB)
C     Import:
      REAL e
C          ( energy in eV)
      REAL Ab(17)
C          ( log10 abundances relative to hydrogen)
 
C     Export:
      REAL TOTLXSE
C          ( effective cross section in cm**2/H atom)
C     External functions: ( mass absorption coefficients for the elements)
      REAL CARBON0
      REAL NITRO0
      REAL OXYGEN0
      REAL NEON0
      REAL SODIUM0
      REAL MAGNES0
      REAL ALUM0
      REAL SILICN0
      REAL SULFUR0
      REAL CHLORN0
      REAL ARGON0
      REAL CALC0
      REAL CHROM0
      REAL IRON0
      REAL NICKEL0
 
 
C     Local constants:
      REAL aw(17)
C          ( atomic weights of the elements)
      REAL av
C          ( Avogadro's number) <- multiplied by sigma_Thomson
      INTEGER numb
C          ( number of elements)
      INTEGER inh
c          ( limiting bin for asymptotic absorption )
 
      LOGICAL fl0 , Fl1 , Fl2

      SAVE aw , av , numb , ap , fl0 , xnors
 
      DATA aw/1.00797 , 4.0026 , 12.01115 , 14.0067 , 15.9994 , 20.183 , 
     &     22.9898 , 24.312 , 26.9815 , 28.086 , 32.064 , 35.453 , 
     &     39.94 , 40.08 , 51.996 , 55.847 , 58.71/
 
c      DATA AV /6.022045E23/
      DATA av/.400614/
 
      DATA numb/17/
 
      DATA fl0/.TRUE./

 
C     Start:

      ierr = 0
 
c     normalizing factor for the asymptotic relation
      IF ( Fl1 ) THEN
C        mass absorption coefficients for the elements at 10.keV
         IF ( fl0 ) THEN
            e = 10000.
c            ap( 1)=HYDRO0(E)
c            ap( 2)=HELIUM0(E)
            ap(1) = 0
            ap(2) = 0
            ap(3) = CARBON0(e)
            ap(4) = NITRO0(e)
            ap(5) = OXYGEN0(e)
            ap(6) = NEON0(e)
            ap(7) = SODIUM0(e)
            ap(8) = MAGNES0(e)
            ap(9) = ALUM0(e)
            ap(10) = SILICN0(e)
            ap(11) = SULFUR0(e)
            ap(12) = CHLORN0(e)
            ap(13) = ARGON0(e)
            ap(14) = CALC0(e)
            ap(15) = CHROM0(e)
            ap(16) = IRON0(e)
            ap(17) = NICKEL0(e)
            fl0 = .FALSE.
         ENDIF
         xnors = 0.
         DO 50 k = 1 , numb
            xnors = xnors + aw(k)*ap(k)*(10.**(Ab(k)-Ab(1)))
 50      CONTINUE
         xnors = xnors/av*(0.01947)**3
         Fl1 = .FALSE.
      ENDIF
      Xnor = xnors
 
      IF ( Fl2 ) THEN
         DO 100 i = 1 , N
c           above 10 keV, use e**-3 law
            IF ( X(i).LT.0.01947 ) THEN
               e = X(i)*511000
c               A( 1,i)=HYDRO0(E)
c               A( 2,i)=HELIUM0(E)
               A(1+17*(i-1)) = 0
               A(2+17*(i-1)) = 0
               A(3+17*(i-1)) = CARBON0(e)
               A(4+17*(i-1)) = NITRO0(e)
               A(5+17*(i-1)) = OXYGEN0(e)
               A(6+17*(i-1)) = NEON0(e)
               A(7+17*(i-1)) = SODIUM0(e)
               A(8+17*(i-1)) = MAGNES0(e)
               A(9+17*(i-1)) = ALUM0(e)
               A(10+17*(i-1)) = SILICN0(e)
               A(11+17*(i-1)) = SULFUR0(e)
               A(12+17*(i-1)) = CHLORN0(e)
               A(13+17*(i-1)) = ARGON0(e)
               A(14+17*(i-1)) = CALC0(e)
               A(15+17*(i-1)) = CHROM0(e)
               A(16+17*(i-1)) = IRON0(e)
               A(17+17*(i-1)) = NICKEL0(e)
               inh = i
            ELSE
               Toth(i) = X(i)**(-3)
            ENDIF
 100     CONTINUE
         inh = inh + 1
         Fl2 = .FALSE.
      ENDIF
 
      IF ( X(In).LT..01947 ) THEN
         TOTLXSE = 0.
C        loop through elements
         DO 150 k = 1 , numb
            TOTLXSE = TOTLXSE + aw(k)*A(k+17*(In-1))
     &                               *(10.**(Ab(k)-Ab(1)))
 150     CONTINUE
         TOTLXSE = TOTLXSE/av
      ELSE
         TOTLXSE = Xnor*Toth(In)
      ENDIF
 
 999  CONTINUE
      IF ( ierr .NE. 0 ) THEN
         CALL xwrite(contxt, 10)
         TOTLXSE = 0.
      ENDIF

      RETURN
      END
**==grnteepr.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
 
C-------------------------------------------------------------------------
c ------------------------------------------------------------------- c
 
      SUBROUTINE GRNTEEPR(Xnth,Jnth,Spnth,Sphth,Xtot,Jtot,Sptot,Spref,
     &                    Scale,Xm,Ab0)
c     calculates the reflected and total spectrum from pair cascades
c     using Green's functions of Magdziarz & Zdziarski 1995
      IMPLICIT NONE
      INTEGER Jnth , Jtot
      REAL Xnth(Jnth) , Spnth(Jnth) , Sphth(Jnth) , Xtot(Jtot) , 
     &     Sptot(Jtot) , Spref(Jtot) , Scale , Xm , Ab0
      INTEGER i , il , j
      REAL scdir , scref , sc , spo , grad , delta
 
      IF ( (Xtot(1).LT.Xnth(1)) .OR. (Xtot(Jtot).GT.Xnth(Jnth)) ) THEN
         CALL xwrite('stop: grid problem',5)
         STOP
      ENDIF
c  ** see comments in greentee **
c
c      scdir=1/(1+scale)
c      scref=scale/(1+scale)
c
      scdir = 1
      scref = Scale
c
      IF ( scref.GT.0. ) THEN
         CALL GRNCEPR(Xnth,Jnth,Spnth,Xtot,Jtot,Spref,Xm,Ab0)
      ELSE
         DO 50 j = 1 , Jtot
            Spref(j) = 0.
 50      CONTINUE
      ENDIF
c
      sc = scref/scdir
      DO 100 j = 1 , Jtot
         Spref(j) = Spref(j)*sc
         i = 1
         DO WHILE ( Xnth(i).LT.Xtot(j) )
            i = i + 1
         ENDDO
         il = i - 1
         spo = (Spnth(il)+Sphth(il))/Xnth(il)**2
         grad = ((Spnth(i)+Sphth(i))/Xnth(i)**2-spo)/(Xnth(i)-Xnth(il))
         delta = Xtot(j) - Xnth(il)
         Sptot(j) = Spref(j) + (spo+grad*delta)*Xtot(j)**2
 100  CONTINUE
 
      RETURN
      END
**==grncepr.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
c ------------------------------------------------------------------ c
 
      SUBROUTINE GRNCEPR(Xnth,Jnth,Spnth,Xtot,Jtot,Spref,Xm,Ab0)
c     calculates the reflected specutrum according to MZ95
c     ab0 - iron log10 abundance relative to hydrogen
      IMPLICIT NONE
      REAL GNR
      INTEGER Jnth , Jtot
      REAL Xnth(Jnth) , Spnth(Jnth) , Xtot(Jtot) , Spref(Jtot) , Xm , 
     &     Ab0
      INTEGER i , j
      REAL xmin , xtransl , xtransh , xrefmax , ymin
      REAL pm1(19) , pmx(26) , apf, ap(3) , xmo
      REAL xkabs , xnor , xjl , xjd , fjc
      REAL y , dym , dy , ddy , y0 , sr , srp
      INTEGER nc , ncp , jmin , jtransl , jtransh , jrefmax
      REAL SIGBFEPR , SPP , GRXYL , GRXYH
      
      SAVE xnor
      DATA xnor/0.0/
 
c  precision factor for Green's function integration
      ncp = 100
c  ranges for different methods
      xmin = 2.E-4
      xtransl = 0.01957
      xtransh = 0.02348
      ymin = .03333
c  angle dependent parameters
      xmo = MAX(.05,MIN(.95,Xm))
      CALL PM1Y(xmo,pm1)
      CALL PMXY(xmo,pmx)
      ap(2) = apf(xmo)
      ap(3) = 0.381
      xrefmax = 1/(ymin+pmx(26))
 
      jmin = 0
      DO 100 j = 1 , Jtot
         IF ( Xtot(j).GT.xmin ) GOTO 200
         jmin = j
 100  CONTINUE
 200  jtransl = 0
      DO 300 j = MAX(1,jmin) , Jtot
         IF ( Xtot(j).GT.xtransl ) GOTO 400
         jtransl = j
 300  CONTINUE
 400  jtransh = 0
      DO 500 j = MAX(1,jtransl) , Jtot
         IF ( Xtot(j).GT.xtransh ) GOTO 600
         jtransh = j
 500  CONTINUE
 600  jrefmax = 0
      DO 700 j = MAX(1,jtransh) , Jtot
         IF ( Xtot(j).GT.xrefmax ) GOTO 800
         jrefmax = j
 700  CONTINUE
 
 800  DO 900 j = 1 , jmin
         Spref(j) = 0
 900  CONTINUE
c-----------------------
      DO 1000 j = jmin + 1 , jtransl
         xkabs = SIGBFEPR(j,Xtot,jtransh,Ab0,xnor)
         Spref(j) = SPP(1/Xtot(j),Xnth,Jnth,Spnth)*GNR(xkabs,xmo)
 1000 CONTINUE
c-----------------------
      IF ( jmin.GE.jtransl ) xkabs = SIGBFEPR(1,Xtot,MAX(1,jtransh),Ab0,
     &                               xnor)
      ap(1) = xnor/1.21
      xjl = ALOG(xtransl)
      xjd = ALOG(xtransh) - xjl
      DO 1100 j = jtransl + 1 , jtransh
         fjc = .5*SIN(3.14159*((ALOG(Xtot(j))-xjl)/xjd-.5))
         xkabs = SIGBFEPR(j,Xtot,jtransh,Ab0,xnor)
         y = 1/Xtot(j)
         Spref(j) = SPP(y,Xnth,Jnth,Spnth)*GNR(xkabs,xmo)
         dym = y - ymin
         dy = MIN(2.,dym)
         ddy = (dy-pmx(26))/(ncp+1)
         y0 = y - dy
         sr = SPP(y0,Xnth,Jnth,Spnth)*GRXYL(pm1,pmx,dy,y0,ap)
         dy = pmx(26)
         y0 = y - dy
         sr = .5*(sr+SPP(y0,Xnth,Jnth,Spnth)*GRXYL(pm1,pmx,dy,y0,ap))
         DO 1050 i = 1 , ncp
            dy = dy + ddy
            y0 = y - dy
            sr = sr + SPP(y0,Xnth,Jnth,Spnth)*GRXYL(pm1,pmx,dy,y0,ap)
 1050    CONTINUE
         sr = sr*ddy
         IF ( dym.GT.2. ) THEN
            ddy = dym - 2
            nc = INT(ncp*ddy/(dym-pmx(26)))
            ddy = ddy/(nc+1)
            dy = dym
            y0 = y - dy
            srp = SPP(y0,Xnth,Jnth,Spnth)*GRXYH(pm1,pmx,dy,y0,ap)
            dy = 2
            y0 = y - dy
            srp = .5*(srp+SPP(y0,Xnth,Jnth,Spnth)
     &            *GRXYH(pm1,pmx,dy,y0,ap))
            DO 1060 i = 1 , nc
               dy = dy + ddy
               y0 = y - dy
               srp = srp + SPP(y0,Xnth,Jnth,Spnth)
     &               *GRXYH(pm1,pmx,dy,y0,ap)
 1060       CONTINUE
            sr = sr + srp*ddy
         ENDIF
         Spref(j) = (.5-fjc)*Spref(j) + (.5+fjc)*sr*Xtot(j)
 1100 CONTINUE
c-----------------------
      DO 1200 j = jtransh + 1 , jrefmax
         y = 1/Xtot(j)
         dym = y - ymin
         dy = MIN(2.,dym)
         ddy = (dy-pmx(26))/(ncp+1)
         y0 = y - dy
         sr = SPP(y0,Xnth,Jnth,Spnth)*GRXYL(pm1,pmx,dy,y0,ap)
         dy = pmx(26)
         y0 = y - dy
         sr = .5*(sr+SPP(y0,Xnth,Jnth,Spnth)*GRXYL(pm1,pmx,dy,y0,ap))
         DO 1150 i = 1 , ncp
            dy = dy + ddy
            y0 = y - dy
            sr = sr + SPP(y0,Xnth,Jnth,Spnth)*GRXYL(pm1,pmx,dy,y0,ap)
 1150    CONTINUE
         sr = sr*ddy
         IF ( dym.GT.2. ) THEN
            ddy = dym - 2
            nc = INT(ncp*ddy/(dym-pmx(26)))
            ddy = ddy/(nc+1)
            dy = dym
            y0 = y - dy
            srp = SPP(y0,Xnth,Jnth,Spnth)*GRXYH(pm1,pmx,dy,y0,ap)
            dy = 2
            y0 = y - dy
            srp = .5*(srp+SPP(y0,Xnth,Jnth,Spnth)
     &            *GRXYH(pm1,pmx,dy,y0,ap))
            DO 1160 i = 1 , nc
               dy = dy + ddy
               y0 = y - dy
               srp = srp + SPP(y0,Xnth,Jnth,Spnth)
     &               *GRXYH(pm1,pmx,dy,y0,ap)
 1160       CONTINUE
            sr = sr + srp*ddy
         ENDIF
         Spref(j) = sr*Xtot(j)
 1200 CONTINUE
c----------------------
      DO 1300 j = jrefmax + 1 , Jtot
         Spref(j) = 0
 1300 CONTINUE
 
      RETURN
      END
**==totlxsep.spg  processed by SPAG 4.50J  at 11:13 on 27 Feb 1996
 
C----------------------------------------------------------------------
C
      FUNCTION TOTLXSEP(Ab,In,X,N,Fl1,Fl2,Xnor, A, Toth)
c
c     - Added input args A and Toth after moving dynamic array allocation
c       to xsnteea.cxx Jan 2009 (CG)
c
c     *** this version with fully ionized H and He!  AAZ, 18.05.94
c     Optimized version: calculetes cross section for abundances
c     contained in the vector AB at energy in 511.keV units given as
c     an element numbered in of the vector x; n is the length of x.
c     Flag fl1=.true. must indicate changes of AB, and fl2=.true.
c     changes of the energy vector x. For energy >10.keV the law
c     xnor*e^-3 is used, xnor is an output parameter.
c     Note: result in sigma_Thomson units.
C
C     Reference:
C      Monika Balucinska-Church and Dan McCammon
C      "Photoelectric Absorption Cross Sections with Variable Abundances"
C      Ap.J. 400, 699 (1992)
C
C
C     Description:
C     Calculates the effective absorption cross section in units of
c     sigma_Thomson per hydrogen atom at energy E in eV for the
c     abundances of the elements specified in vector AB
C
C     Method:
C     Calls seventeen functions that calculate the mass absorption coeffs
C     in cm**2/g for the following elements: H, He, C, N, O, Ne, Na, Mg,
C     Al, Si, S, Cl, Ar, Ca, Cr, Fe, Ni.
C
C     Deficiencies:
C     Works only in the range of energy from 30 eV to 10,000 eV.
C
C     Bugs:
C     None known -- please report any problems to authors
C
C     Authors:
C     Dan McCammon               (47413::MCCAMMON)
C     Monika Balucinska-Church   (19775::MBC)
C
C     History:
C     8.8.91  - original
C     8.1.92  - modified to remove VAX fortran77 extensions
C     12.3.93 - function HELIUM has been replaced by HELIUM_V2 with
C               Fano line profile
C     21.9.93 - Names of functions simplified: TOTLXS_V2 to TOTLX2,
C               HELIUM_V2 to HEL2.
C     24.1.94 - Names of functions simplified: TOTLX2 to TOTLXS,
C               HEL2 to HELIUM.
C
C     Parameters:
C     E - energy in eV
C     AB - vector of length 17 of Log10 abundances relative to
C          hydrogen = AB(1).  N.B. The order of the elements in AB
C          is as follows: H,He,C,N,O,Ne,Na,Mg,Al,Si,S,Cl,Ar,Ca,Cr,Fe,Ni.
C     TOTLX2 - effective cross section in cm**2/H atom for assumed
C             abundances of cosmic elements.  Note that this will be per
C             hydrogen atom for the relative abundances given in AB.
C
C     Type Definitions:


      INTEGER i, ierr
      REAL Xnor , xnors
      CHARACTER contxt*80
C
C     Local variables:
      INTEGER k
C          ( index through elements)
      REAL X(*) , ap(17)
      REAL A(N*17), Toth(N)
      INTEGER In , N
c          x  - energy points
c          in - index for x
c          n  - number of energy points
C          ( mass absorption cross sections in cm**2/g
C           for the cosmic elements -- in the same order as for AB)
C     Import:
      REAL e
C          ( energy in eV)
      REAL Ab(17)
C          ( log10 abundances relative to hydrogen)
 
C     Export:
      REAL TOTLXSEP
C          ( effective cross section in cm**2/H atom)
C     External functions: ( mass absorption coefficients for the elements)
      REAL CARBON0
      REAL NITRO0
      REAL OXYGEN0
      REAL NEON0
      REAL SODIUM0
      REAL MAGNES0
      REAL ALUM0
      REAL SILICN0
      REAL SULFUR0
      REAL CHLORN0
      REAL ARGON0
      REAL CALC0
      REAL CHROM0
      REAL IRON0
      REAL NICKEL0
 
 
C     Local constants:
      REAL aw(17)
C          ( atomic weights of the elements)
      REAL av
C          ( Avogadro's number) <- multiplied by sigma_Thomson
      INTEGER numb
C          ( number of elements)
      INTEGER inh
c          ( limiting bin for asymptotic absorption )
 
      LOGICAL fl0 , Fl1 , Fl2

      SAVE aw , av , numb , ap , fl0 , xnors
 
      DATA aw/1.00797 , 4.0026 , 12.01115 , 14.0067 , 15.9994 , 20.183 , 
     &     22.9898 , 24.312 , 26.9815 , 28.086 , 32.064 , 35.453 , 
     &     39.94 , 40.08 , 51.996 , 55.847 , 58.71/
 
c      DATA AV /6.022045E23/
      DATA av/.400614/
 
      DATA numb/17/
 
      DATA fl0/.TRUE./

C     Start:

      ierr = 0
 
c     normalizing factor for the asymptotic relation
      IF ( Fl1 ) THEN
C        mass absorption coefficients for the elements at 10.keV
         IF ( fl0 ) THEN
            e = 10000.
c            ap( 1)=HYDRO0(E)
c            ap( 2)=HELIUM0(E)
            ap(1) = 0
            ap(2) = 0
            ap(3) = CARBON0(e)
            ap(4) = NITRO0(e)
            ap(5) = OXYGEN0(e)
            ap(6) = NEON0(e)
            ap(7) = SODIUM0(e)
            ap(8) = MAGNES0(e)
            ap(9) = ALUM0(e)
            ap(10) = SILICN0(e)
            ap(11) = SULFUR0(e)
            ap(12) = CHLORN0(e)
            ap(13) = ARGON0(e)
            ap(14) = CALC0(e)
            ap(15) = CHROM0(e)
            ap(16) = IRON0(e)
            ap(17) = NICKEL0(e)
            fl0 = .FALSE.
         ENDIF
         xnors = 0.
         DO 50 k = 1 , numb
            xnors = xnors + aw(k)*ap(k)*(10.**(Ab(k)-Ab(1)))
 50      CONTINUE
         xnors = xnors/av*(0.01947)**3
         Fl1 = .FALSE.
      ENDIF
      Xnor = xnors
 
      IF ( Fl2 ) THEN
         DO 100 i = 1 , N
c           above 10 keV, use e**-3 law
            IF ( X(i).LT.0.01947 ) THEN
               e = X(i)*511000
c               A( 1,i)=HYDRO0(E)
c               A( 2,i)=HELIUM0(E)
               A(1+17*(i-1)) = 0
               A(2+17*(i-1)) = 0
               A(3+17*(i-1)) = CARBON0(e)
               A(4+17*(i-1)) = NITRO0(e)
               A(5+17*(i-1)) = OXYGEN0(e)
               A(6+17*(i-1)) = NEON0(e)
               A(7+17*(i-1)) = SODIUM0(e)
               A(8+17*(i-1)) = MAGNES0(e)
               A(9+17*(i-1)) = ALUM0(e)
               A(10+17*(i-1)) = SILICN0(e)
               A(11+17*(i-1)) = SULFUR0(e)
               A(12+17*(i-1)) = CHLORN0(e)
               A(13+17*(i-1)) = ARGON0(e)
               A(14+17*(i-1)) = CALC0(e)
               A(15+17*(i-1)) = CHROM0(e)
               A(16+17*(i-1)) = IRON0(e)
               A(17+17*(i-1)) = NICKEL0(e)
               inh = i
            ELSE
               Toth(i) = X(i)**(-3)
            ENDIF
 100     CONTINUE
         inh = inh + 1
         Fl2 = .FALSE.
      ENDIF
 
      IF ( X(In).LT..01947 ) THEN
         TOTLXSEP = 0.
C        loop through elements
         DO 150 k = 1 , numb
            TOTLXSEP = TOTLXSEP + aw(k)*A(k+17*(In-1))
     &                                 *(10.**(Ab(k)-Ab(1)))
 150     CONTINUE
         TOTLXSEP = TOTLXSEP/av
      ELSE
         TOTLXSEP = Xnor*Toth(In)
      ENDIF

 999  CONTINUE
      IF ( ierr .NE. 0 ) THEN
         CALL xwrite(contxt, 10)
         TOTLXSEP = 0.
      ENDIF

      RETURN
      END
 
 
C-------------------------------------------------------------------------
C-------------------------------------------------------------------------
C
C  File XSCTNS.FOR
C
C        This set of subroutines calculates the photoelectric absorption
C   cross sections for the elements H, He, C, N, O, Ne, Na, Mg, Al, Si,
C   S, Cl, A, Ca, Cr, Fe, and Ni.  The result is in cm**2/g, given the
C   photon energy in eV.  These functions are valid only over the energy
C   range 30 - 10,000 eV, but do NOT check that the input energy is
C   within the valid range.  These functions are called by TOTLX2 to
C   calculate the total effective cross section, given a set of relative
C   abundances.  They can also be used by themselves.
C
C   history : 26 May, 1993 - original HELIUM function replaced by HEL2 ;
C              HELIUM not deleted from the program (19775::MBC)
C
C           : 21 Sep, 1993 - Function HELIUM now deleted, all remnants
C             of VAX Fortran 77 extensions removed, file renamed
C             XSCTN2 (19775::MBC)
C
C           : 22 Sep, 1993 - bug in FANO routine (called by HEL2) corrected
C             that resulted in incorrect line shape and energy (19775::MBC)
C
C           : 24 Jan, 1994 - name HEL2 changed to HELIUM
C   References:
C
C      Monika Balucinska-Church and Dan McCammon
C      "Photoelectric Absorption Cross Sections with Variable Abunances"
C      Ap.J. 400, 699 (1992)
C
C      All data are from:
C      B. L. Henke, P. Lee, T. J. Tanaka, R. L. Shimabukuro and B. K.
C      Fujikawa, 1982, Atomic Data and Nuclear Data Tables, vol 27, p 1.
C
C-----------------------------------------------------------------------
C-----------------------------------------------------------------------
C
C  Real Function: ALUM
C  Source: Atomic & Nuclear Data Tables, January 1982
C
C  Description:
C      Calculates mass absorption coefficient (mu/rho) for aluminum.
C  History:  updated below L-edge (72.78 eV) - Aug 30, 1991 (MBC)
C
C  Usage:  FUNCTION ALUM(E)
C      E = Energy in eV.
C      ALUM returns mu/rho in cm**2/gm.
C
C  COMMON Blocks:
C      none
C  IMPLICIT
C      none
C  Subroutines called by ALUM:
C      none
C
C--------------------------------------------------------------------------
      FUNCTION ALUM0(E)
      IMPLICIT NONE
 
      REAL E , elog , x , ALUM0
 
      elog = ALOG(E)
      IF ( E.LT.72.78 ) THEN
         x = 26.90487 + (3.-9.135221)*elog + 1.175546*elog*elog
 
      ELSEIF ( E.LT.1559.9 ) THEN
         x = -38.1232 + 29.5161*elog - 4.45416*elog*elog + 
     &       0.226204*elog*elog*elog
      ELSE
         x = 14.6897 + 4.22743*elog - 0.344185*elog*elog + 
     &       8.18542E-3*elog*elog*elog
      ENDIF
 
      ALUM0 = EXP(x)/(E*E*E)
 
      RETURN
      END
**==argon0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C------------------------------------------------------------------------
C------------------------------------------------------------------------
C------------------------------------------------------------------------
C
C  REAL FUNCTION: ARGON
C  Source: Atomic & Nuclear Data Tables, January 1982
C  Description: ARGON calculates the mass absorbtion coefficient of argon.
C
C ****          works well for energies above 40 eV !!!
C
C  History: updated below L-edge (245 eV) - Aug 30, 1991 (MBC)
C
C  Usage: FUNCTION ARGON(E)
C      E = Energy in eV
C      ARGON returns the mass absorption cross section in cm**2/g
C
C  COMMON BLOCKS:
C      NONE
C
C  IMPLICIT
C      NONE
C  SUBROUTINES CALLED BY ARGON:
C      NONE
C
C---------------------------------------------------------------------------
      FUNCTION ARGON0(E)
      IMPLICIT NONE
 
      REAL E , elog , x , ARGON0
 
      elog = ALOG(E)
      IF ( E.LT.245.0 ) THEN
         x = -330.3509 + (267.7433+3.)*elog - 78.90498*elog*elog + 
     &       10.35983*(elog**3) - 0.5140201*(elog**4)
 
      ELSEIF ( E.LT.3202.9 ) THEN
         x = -5.71870 + (8.85812*elog) + (-0.307357*elog*elog)
     &       + (0.00169351*(elog**3)) + (-0.0138134*(elog**4))
     &       + (0.00120451*(elog**5))
      ELSE
         x = 19.1905 + (2.74276*elog) + (-0.164603*elog*elog)
     &       + (0.00165895*elog*elog*elog)
      ENDIF
 
      ARGON0 = EXP(x)/(E*E*E)
 
      RETURN
      END
**==calc0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C--------------------------------------------------------------------------
C------------------------------------------------------------------------------
C------------------------------------------------------------------------------
C     Real Function: CALC
C     Source: Atomic & Nuclear Data Tables, January 1982
C
C     Description:
C     Calculates mass absorption coefficient (mu/rho) for calcium
C
C     History: original Aug 6, 1991 (MBC)
C
C     Usage:   FUNCTION CALC(E)
C              E = Energy in eV
C              CALC returns mu/rho in cm**2/g
C
C     COMMON Blocks:
C            none
C
C     IMPLICIT
C            none
C-------------------------------------------------------------------------
C
      FUNCTION CALC0(E)
      IMPLICIT NONE
C
C
      REAL E , elog , x , CALC0
C
C
      elog = ALOG(E)
 
 
      IF ( E.LT.349.31 ) THEN
         x = -873.972 + (865.5231+3.)*elog - 339.678*elog*elog + 
     &       66.83369*(elog**3) - 6.590398*(elog**4)
     &       + 0.2601044*(elog**5)
 
      ELSEIF ( E.LT.4038.1 ) THEN
         x = -3449.707 + (2433.409+3.)*elog - 682.0668*elog*elog + 
     &       95.3563*(elog**3) - 6.655018*(elog**4)
     &       + 0.1854492*(elog**5)
 
      ELSE
         x = 18.89376 + (3.-0.2903538)*elog - 0.1377201*elog*elog
 
      ENDIF
      CALC0 = EXP(x)/(E*E*E)
 
 
      RETURN
      END
**==carbon0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C
C--------------------------------------------------------------------------
C--------------------------------------------------------------------------
C
C  REAL FUNCTION: CARBON
C  Source: Atomic & Nuclear Data Tables, Jan. 1982
C
C  Description: CARBON calculates the mass absorbtion cross-section of carbon.
C
C  USAGE: FUNCTION CARBON(E)
C      E = Energy in EV
C      CARBON returns the mass absorption cross-section in cm**2/g
C
C  COMMON BLOCKS:
C      NONE
C
C  IMPLICIT
C      NONE
C
C  SUBROUTINES CALLED BY CARBON:
C      NONE
C
C-------------------------------------------------------------------------
      FUNCTION CARBON0(E)
      IMPLICIT NONE
C
C
      REAL E , elog , x , CARBON0
C
C
      elog = ALOG(E)
      IF ( E.LT.284.0 ) THEN
         x = 8.74161 + (7.13348*elog) + (-1.14604*elog*elog)
     &       + (0.0677044*elog*elog*elog)
      ELSE
         x = 3.81334 + (8.93626*elog) + (-1.06905*elog*elog)
     &       + (0.0422195*elog*elog*elog)
      ENDIF
 
      CARBON0 = EXP(x)/(E*E*E)
      RETURN
      END
**==chlorn0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C
C---------------------------------------------------------------------------
C---------------------------------------------------------------------------
C     Real Function: CHLORN
C     Source: Atomic & Nuclear Data Tables, January 1982
C
C     Description:
C     Calculates mass absorption coefficient (mu/rho) for chlorine
C
C     History: original Aug 6, 1991 (MBC)
C
C     Usage:   FUNCTION CHLORN(E)
C              E = Energy in eV
C              CHLORN returns mu/rho in cm**2/g
C
C     COMMON Blocks:
C            none
C
C     IMPLICIT
C            none
C-------------------------------------------------------------------------
C
      FUNCTION CHLORN0(E)
      IMPLICIT NONE
C
C
      REAL E , elog , x , CHLORN0
C
C
      elog = ALOG(E)
 
 
      IF ( E.LT.202.0 ) THEN
         x = 6253.247 + (3.-8225.248)*elog + 4491.675*elog*elog - 
     &       1302.145*(elog**3) + 211.4881*(elog**4)
     &       - 18.25547*(elog**5) + 0.6545154*(elog**6)
 
      ELSEIF ( E.LT.2819.6 ) THEN
         x = -233.0502 + (143.9776+3.)*elog - 31.12463*elog*elog + 
     &       2.938618*(elog**3) - 0.104096*(elog**4)
 
      ELSE
         x = -23.74675 + (14.50997+3.)*elog - 1.857953*elog*elog + 
     &       6.6208832E-2*(elog**3)
 
      ENDIF
      CHLORN0 = EXP(x)/(E*E*E)
 
 
      RETURN
      END
**==chrom0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C
C---------------------------------------------------------------------------
C---------------------------------------------------------------------------
C     Real Function: CHROM
C     Source: Atomic & Nuclear Data Tables, January 1982
C
C     Description:
C     Calculates mass absorption coefficient (mu/rho) for chromium
C
C     History: original Aug 5, 1991 (MBC)
C
C     Usage:   FUNCTION CHROM(E)
C              E = Energy in eV
C              CHROM returns mu/rho in cm**2/g
C
C     COMMON Blocks:
C            none
C
C     IMPLICIT
C            none
C-------------------------------------------------------------------------
C
      FUNCTION CHROM0(E)
      IMPLICIT NONE
C
C
      REAL E , elog , x , CHROM0
C
C
      elog = ALOG(E)
 
 
      IF ( E.LT.598.0 ) THEN
         x = -0.4919405 + (12.66939+3.)*elog - 5.199775*elog*elog + 
     &       1.086566*(elog**3) - 0.1196001*(elog**4)
     &       + 5.2152011E-3*(elog**5)
 
      ELSEIF ( E.LT.691.0 ) THEN
         x = 27.29282 + (3.-2.703336)*elog
 
      ELSEIF ( E.LT.5988.8 ) THEN
         x = -15.2525 + (13.23729+3.)*elog - 1.966778*elog*elog + 
     &       8.062207E-2*(elog**3)
 
      ELSE
         x = 8.307041 + (2.008987+3.)*elog - 0.2580816*elog*elog
 
      ENDIF
      CHROM0 = EXP(x)/(E*E*E)
 
 
      RETURN
      END
**==helium0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C------------------------------------------------------------------------------
C-----------------------------------------------------------------------------
C
C     Real Funcion : HELIUM
C     Source : Marr, G. V., and West, J. B., Atomic and Nuclear Data Tables,
C                (1976) 18, 497.
C             Oza, D. H., (1986), Phys. Rev. A, 33,  824.
C             Fernley, J. A., Taylor, K. T., and Seaton, M. J., (1987),
C                J. Phys. B., 20, 6457.
C
C     Description :
C     calculates mass absorption coefficient (mu/rho) in cm2/g for neutral
C     helium for the given energy in eV.
C     Cross sections come from experimental data compiled by Marr and
C     West (Atomic Data and Nuclear Data Tables (1976) 18, 497).
C     The four strongest autoionization resonances are taken into account;
C     numbers come from Oza (Phys Rev A (1986), 33, 824), and Fernley et al.
C     (J. Phys B (1987) 20, 6457).
C
C     Deficiencies :
C     works in the energy range from 30 eV to 10,000 eV
 
C     Bugs :
C     if any are found please report to the authors
C
C     History :
C     this subroutine replaces the previous version of HELIUM which
C     calculated mass absoprtion coefficients based on Henke's data
C     (Henke, B. L., et al., (1982), Atomic and Nuclear Data Tables, 27, 1).
C     This version of HELIUM returns mass  absorption coefficients which
C     are in better agreement with the best experiments as well as
C     theoretical models (see Chen, W. F., Cooper, G., and Brion, C. E.,
C     (1991), Phys. Rev. A, 44, 186).  This fortran-77 version of the
C     subroutine is based on Jelinsky's program written in C
C     (EUVE Archive)
C
C     History :
C     Jan 4, 1993 : original (19775::MBC)
C     Feb 23, 1993 : comments added and modified to remove VAX
C                    fortran 77 extensions
C
C     Sep 21, 1993 : further remnants of VAX fortran 77 extensions
C                    have been removed (19775::MBC)
C
C     Sep 22, 1993 : bug in the FANO routine has been removed (19775::MBC)
C
C     Usage : FUNCTION HELIUM(E)
C            E = Energy in eV
C
C     Common Blocks :
C           none
C
C     Implicit :
C           none
C
C     Functions called by HELIUM
C           FANO
C
C------------------------------------------------------------------------------
      FUNCTION HELIUM0(E)
      IMPLICIT NONE
 
C    Type definitions :
C     IMPLICIT NONE
C    Global variables :
C    Structure definitions :
C    Function declarations :
      REAL FANOO
C    Local constants :
      INTEGER IP
C         ( index through loop )
      PARAMETER (IP=8)
      INTEGER IF
C         ( index through loop )
      PARAMETER (IF=4)
      REAL AV
C         ( Avogadro's number )
      PARAMETER (AV=6.022045E23)
      REAL AW
C         ( atomic weight of HYDROgen )
      PARAMETER (AW=4.0026E0)
C    Local variables :
      REAL lambda
C          ( wavelength in Angstroms)
      REAL x
      REAL y
      REAL sigma
C          ( cross section in cm2/atom)
      INTEGER i
C          ( index trough loop)
C     Import :
      REAL E
C          ( energy in eV)
C     Export :
      REAL HELIUM0
C          ( cross section in cm**2/g)
C    Local data :
      REAL c1(IP)
      REAL c2(IP)
      REAL q(IF)
      REAL nu(IF)
      REAL gamma(IF)
 
C          ( polynomial coefficients for Marr and West data)
      DATA c1/ - 2.953607E1 , 7.083061E0 , 8.678646E-1 , -1.221932E0 , 
     &     4.052997E-2 , 1.317109E-1 , -3.265795E-2 , 2.500933E-3/
 
C          ( polynomial coefficients for Marr and West data )
      DATA c2/ - 2.465188E1 , 4.354679E0 , -3.553024E0 , 5.573040E0 , 
     &     -5.872938E0 , 3.720797E0 , -1.226919E0 , 1.576657E-1/
 
C          ( parameters Q for resonances (Fernley et al. 1987) )
      DATA q/2.81E0 , 2.51E0 , 2.45E0 , 2.44E0/
 
C          ( parameters NU for resonances (Oza 1986) )
      DATA nu/1.610E0 , 2.795E0 , 3.817E0 , 4.824E0/
 
C          ( parameters GAMMA for resonances (Oza 1986) )
      DATA gamma/2.64061E-3 , 6.20116E-4 , 2.56061E-4 , 1.320159E-4/
 
 
C     Start :
 
      lambda = 12398.54E0/E
      x = ALOG10(lambda)
 
      IF ( lambda.GT.503.97E0 ) THEN
         HELIUM0 = 0.E0
         RETURN
 
      ELSEIF ( lambda.LT.46.E0 ) THEN
         y = 0.E0
         DO 50 i = 1 , IP
            y = y + c2(i)*(x**(i-1))
 50      CONTINUE
 
      ELSE
         y = 0.E0
         DO 100 i = 1 , IP
            y = y + c1(i)*(x**(i-1))
 100     CONTINUE
 
         DO 150 i = 1 , IF
            y = y + ALOG10(FANOO(q(i),nu(i),gamma(i),lambda))
 150     CONTINUE
 
      ENDIF
 
      sigma = 10.E0**y
      HELIUM0 = sigma*AV/AW
 
      END
**==fanoo.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C
C----------------------------------------------------------------------------
C----------------------------------------------------------------------------
C    Real Function FANO
C
C    Source :
C             Fernley, J. A., Taylor, K. T., and Seaton, M. J., (1987),
C                J. Phys. B., 20, 6457.
C             Oza, D. H., (1986), Phys. Rev. A, 33,  824.
C
C    Description :
C     returns a Fano line profile for use by HELIUM. The form of
C     this line shape is taken from Fernley, Taylor and Seaton (above).
C
C    Deficiencies :
C     works in the energy range from 30 eV to 10,000 eV
C
C    Bugs :
C    if any are found please report to the authors
C
C    22 Sep, 1993 - bug fixed in calculations of EPSI - devided by 1.807317
C    not added (19775::MBC)
C
C    History :
C     Jan 4, 1993 : original, based on Jelinsky's program written in
C     C (EUVE Archive) - (19775::MBC)
C
C     Feb 23, 1993 : comments added and modified to remove VAX
C     fortran 77 extensions (19775::MC)
C
C     Sep 21, 1993 : further remnants of VAX fortran 77 extensions
C     removed (19775::MBC)
C
C
C    Common Blocks :
C           none
C    Implicit :
C           none
C
C    Functions called by FANO
C           none
C
C----------------------------------------------------------------------------
C
      FUNCTION FANOO(A,B,C,Lambda)
      IMPLICIT NONE
 
C    Type definitions :
C     IMPLICIT NONE
C    Global variables :
C    Structure definitions :
C    Function declarations :
C    Local constants :
C    Local variables :
      REAL eps
C          ( energy in Rydbergs )
      REAL epsi
      REAL x
C          ( log_10 of wavelength in Angstroms )
C     Import :
      REAL A
C          ( Q coefficient (Fernley et al. 1987) )
      REAL B
C          ( NU coefficient (Oza 1986) )
      REAL C
C          ( GAMMA coefficient (Oza 1986) )
      REAL Lambda
C          ( wavelength in Angstroms )
C     Export :
      REAL FANOO
C    Start :
 
      eps = 911.2671E0/Lambda
      epsi = 3.0E0 - 1.E0/(B*B) + 1.807317
      x = 2.0*(eps-epsi)/C
      FANOO = (x-A)*(x-A)/(1.0E0+x*x)
 
      END
**==hydro0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C-----------------------------------------------------------------------
C       REAL FUNCTION HYDRO(E)
C
C       DATE:3/6/84
C       AUTHOR: ANGIE BETKER
C       SOURCE: ATOMIC AND NUCLEAR DATA TABLES, JANUARY 1982
C
C        History: modified: 6/5/87 - J. Bloch - Created F77 Vax/VMS version.
C                 updated Aug 30, 1991 (MBC)
C
C
C       DESCRIPTION: HYDRO CALCULATES MU/RHO FOR HYDROGEN IN CM**2/GM
C
C       COMMON BLOCKS:
C               NONE
C
C       SUBROUTINES CALLED:
C               NONE
C
C       IMPLICIT
C               NONE
C
C---------------------------------------------------------------------------
C
      FUNCTION HYDRO0(E)
      IMPLICIT NONE
C
C
      REAL E , elog , x , HYDRO0
C
C
C        IF (E.LT.180) HYDRO0 = (10 ** 10.11) * (E ** -3.03)
C        IF ((E.GE.180).AND.(E.LT.750)) HYDRO0=(10**10.54)*(E**-3.23)
C        IF ((E.GE.750).AND.(E.LT.6000)) HYDRO0=(10**10.94)*(E**-3.37)
C        IF (E.GE.6000) HYDRO0 = (10 ** 10.42) * (E ** -3.24)
 
      elog = ALOG(E)
 
      x = 21.46941 + (3.-2.060152)*elog - 0.1492932*elog*elog + 
     &    5.4634294E-3*(elog**3)
 
      HYDRO0 = EXP(x)/(E*E*E)
 
      RETURN
      END
**==iron0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C
C----------------------------------------------------------------------
C------------------------------------------------------------------------------
C     Real Function: IRON
C     Source: Atomic & Nuclear Data Tables, January 1982
C
C     Description:
C     Calculates mass absorption coefficient (mu/rho) for iron
C     History: original Aug 6, 1991 (MBC)
C
C     Usage:   FUNCTION IRON(E)
C              E = Energy in eV
C              IRON returns mu/rho in cm**2/g
C
C     COMMON Blocks:
C            none
C
C     IMPLICIT:
C            none
C
C-------------------------------------------------------------------------
C
      FUNCTION IRON0(E)
      IMPLICIT NONE
C
C
      REAL E , elog , x , IRON0
C
C
      elog = ALOG(E)
 
      IF ( E.LT.707.4 ) THEN
         x = -15.07332 + (18.94335+3.)*elog - 4.862457*elog*elog + 
     &       0.5573765*(elog**3) - 3.0065542E-2*(elog**4)
     &       + 4.9834867E-4*(elog**5)
 
      ELSEIF ( E.LT.7111.2 ) THEN
         x = -253.0979 + (135.4238+3.)*elog - 25.47119*elog*elog + 
     &       2.08867*(elog**3) - 6.4264648E-2*(elog**4)
 
      ELSE
         x = -1.037655 + (4.022304+3.)*elog - 0.3638919*elog*elog
 
      ENDIF
      IRON0 = EXP(x)/(E*E*E)
 
 
      RETURN
      END
**==magnes0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C-------------------------------------------------------------------------
C-------------------------------------------------------------------------
C     Real Function: MAGNES
C     Source: Atomic & Nuclear Data Tables, January 1982
C
C     Description:
C     Calculates mass absorption coefficient (mu/rho) for magnesium
C     History: original Aug 6, 1991 (MBC)
C
C     Usage:   FUNCTION MAGNES(E)
C              E = Energy in eV
C              MAGNES returns mu/rho in cm**2/g
C
C     COMMON Blocks:
C            none
C
C     IMPLICIT:
C            none
C-------------------------------------------------------------------------
C
      FUNCTION MAGNES0(E)
      IMPLICIT NONE
C
C
      REAL E , elog , x , MAGNES0
C
C
      elog = ALOG(E)
 
 
      IF ( E.LT.49.45 ) THEN
         x = 7.107172 + (0.7359418+3.)*elog
 
      ELSEIF ( E.LT.1303.4 ) THEN
         x = -81.32915 + (62.2775+3.)*elog - 15.00826*elog*elog + 
     &       1.558686*(elog**3) - 6.1339621E-2*(elog**4)
 
      ELSE
         x = -9.161526 + (10.07448+3.)*elog - 1.435878*elog*elog + 
     &       5.2728362E-2*(elog**3)
 
      ENDIF
      MAGNES0 = EXP(x)/(E*E*E)
 
 
      RETURN
      END
**==neon0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C
C------------------------------------------------------------------------
C----------------------------------------------------------------------
C  REAL FUNCTION: NEON
C  Source: Atomic and Nuclear Data Tables, Jan. 1982
C
C  Description:  NEON calculates the mass absorption coefficient
C      for NEON0 gas.
C
C  Usage:  REAL FUNCTION NEON(E)
C      E = Energy in eV
C      NEON returns the mass absorption cross section in cm**2/g.
C
C  COMMON BLOCKS:
C      NONE
C
C  IMPLICIT
C      NONE
C
C  SUBROUTINES CALLED BY NEON:
C      NONE
C
C------------------------------------------------------------------------------
      FUNCTION NEON0(E)
      IMPLICIT NONE
C
C
      REAL E , elog , x , NEON0
C
C
      elog = ALOG(E)
      IF ( E.LT.867. ) THEN
         x = -3.04041 + (13.0071*elog) + (-1.93205*elog*elog)
     &       + (0.0977639*elog*elog*elog)
      ELSE
         x = 17.6007 + (3.29278*elog) + (-0.263065*elog*elog)
     &       + (5.68290E-3*elog*elog*elog)
      ENDIF
      NEON0 = EXP(x)/(E*E*E)
      RETURN
      END
**==nickel0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C
C---------------------------------------------------------------------------
C---------------------------------------------------------------------------
C
C  REAL FUNCTION: NICKEL
C  Source: Atomic & Nuclear Data Tables, January 1982
C
C  Description: NICKEL calculates the mass absorbtion coefficient for nickel.
C  History: updated below L-edge (853.6 eV) - Aug 30, 1991 (MBC)
C
C  Usage: REAL FUNCTION NICKEL(E)
C      E = Energy in eV
C      NICKEL returns the mass absorption cross section in cm**2/g.
C
C  COMMON BLOCKS:
C      NONE
C  IMPLICIT
C      NONE
C
C  SUBROUTINES CALLED BY NICKEL:
C      NONE
C
C
C---------------------------------------------------------------------------
      FUNCTION NICKEL0(E)
      IMPLICIT NONE
 
      REAL E , elog , x , NICKEL0
 
      elog = ALOG(E)
 
      IF ( E.LT.853.6 ) THEN
         x = -7.919931 + (11.06475+3.)*elog - 1.935318*elog*elog + 
     &       9.3929626E-2*(elog**3)
 
      ELSEIF ( E.LT.8331.6 ) THEN
         x = 3.71129 + (8.45098*elog) + (-0.896656*elog*elog)
     &       + (0.0324889*elog*elog*elog)
      ELSE
         x = 28.4989 + (0.485797*elog)
      ENDIF
 
      NICKEL0 = EXP(x)/(E*E*E)
 
      RETURN
      END
**==nitro0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C--------------------------------------------------------------------------
C--------------------------------------------------------------------------
C--------------------------------------------------------------------------
C
C  REAL FUNCTION: NITRO
C  Source: Atomic & Nuclear Data Tables, January 1982
C
C  Description:  NITRO calculates the mass absorption cross-section of
C        nitrogen as a function of energy.
C
C  Usage: REAL FUNCTION NITRO(E)
C      E = Energy in eV
C      NITRO returns the mass absorption cross-section in cm**2/g.
C
C  COMMON BLOCKS:
C      NONE
C
C  IMPLICIT
C      NONE
C
C  SUBROUTINES CALLED BY NITRO:
C      NONE
C
C--------------------------------------------------------------------------
      FUNCTION NITRO0(E)
      IMPLICIT NONE
C
C
      REAL E , elog , x , NITRO0
C
C
      elog = ALOG(E)
      IF ( E.LT.401. ) THEN
         x = 9.24058 + (7.02985*elog) + (-1.08849*elog*elog)
     &       + (0.0611007*elog*elog*elog)
      ELSE
         x = -13.0353 + (15.4851*elog) + (-1.89502*elog*elog)
     &       + (0.0769412*elog*elog*elog)
      ENDIF
      NITRO0 = EXP(x)/(E*E*E)
      RETURN
      END
**==oxygen0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C
C------------------------------------------------------------------------------
C-----------------------------------------------------------------------
C-----------------------------------------------------------------------
C
C  REAL FUNCTION: OXYGEN
C
C  Source:        Atomic & Nuclear Data Tables, January 1982
C
C  Description:  OXY Calculates the mass absorption cross-section of oxygen
C      as a function of energy.
C  History: updated above K-edge (531.7 eV) - Aug 30, 1991 (MBC)
C
C  Usage: REAL FUNCTION OXYGEN(E)
C      E = Energy in eV
C      OXYGEN returns the mass absorption cross-section in cm**2/g.
C
C  COMMON BLOCKS:
C      NONE
C
C  IMPLICIT
C      NONE
C  SUBROUTINES CALLED BY OXYGEN:
C      NONE
C
C------------------------------------------------------------------------
      FUNCTION OXYGEN0(E)
      IMPLICIT NONE
 
      REAL E , elog , x , OXYGEN0
 
      elog = ALOG(E)
      IF ( E.LT.531.7 ) THEN
         x = 2.57264 + (10.9321*elog) + (-1.79383*elog*elog)
     &       + (0.102619*elog*elog*elog)
      ELSE
         x = 16.53869 + (0.6428144+3.)*elog - 0.3177744*elog*elog + 
     &       7.9471897E-3*(elog**3)
 
      ENDIF
 
      OXYGEN0 = EXP(x)/(E*E*E)
 
      RETURN
      END
**==silicn0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C--------------------------------------------------------------------------
C--------------------------------------------------------------------------
C       FUNCTION SILICN
C
C       Source: Atomic and Nuclear Data Tables, January 1982
C
C       Description: SILICN calculates the mass absorption cross section
C                for silicon in cm**2/g.
C       History: updated Aug 30, 1991 (MBC)
C                updated March 4, 1992 (MBC)
C
C       COMMON BLOCKS:
C               NONE
C
C       IMPLICIT
C               none
C
C       SUBROUTINES CALLED:
C               NONE
C
C---------------------------------------------------------------------------
      FUNCTION SILICN0(E)
      IMPLICIT NONE
 
      REAL E , elog , x , SILICN0
C
      elog = ALOG(E)
 
      IF ( E.LT.100.6 ) THEN
         x = -3.066295 + (7.006248+3.)*elog - 0.9627411*elog*elog
      ELSEIF ( E.LT.1840.0 ) THEN
         x = -182.7217 + (125.061+3.)*elog - 29.47269*elog*elog + 
     &       3.03284*(elog**3) - 0.1173096*(elog**4)
      ELSE
         x = -33.39074 + (18.42992+3.)*elog - 2.385117*elog*elog + 
     &       8.887583E-2*(elog**3)
 
      ENDIF
 
      SILICN0 = EXP(x)/(E*E*E)
      RETURN
      END
**==sodium0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C-------------------------------------------------------------------------
C------------------------------------------------------------------------
C     Real Function: SODIUM
C     Source: Atomic & Nuclear Data Tables, January 1982
C
C     Description:
C     Calculates mass absorption coefficient (mu/rho) for sodium
C
C     History: original Aug 6, 1991 (MBC)
C
C     Usage:   FUNCTION SODIUM(E)
C              E = Energy in eV
C              SODIUM returns mu/rho in cm**2/g
C
C     COMMON Blocks:
C            none
C
C     IMPLICIT:
C            none
C-------------------------------------------------------------------------
C
      FUNCTION SODIUM0(E)
      IMPLICIT NONE
C
C     IMPLICIT NONE
C
      REAL E , elog , x , SODIUM0
C
C
      elog = ALOG(E)
 
      IF ( E.LT.1071.7 ) THEN
         x = -2737.598 + (2798.704+3.)*elog - 1009.892*elog*elog + 
     &       87.16455*(elog**3) + 43.20644*(elog**4)
     &       - 15.27259*(elog**5) + 2.180531*(elog**6)
     &       - 0.1526546*(elog**7) + 4.3137977E-3*(elog**8)
 
      ELSE
         x = 1.534019 + (6.261744+3.)*elog - 0.9914126*elog*elog + 
     &       3.5278253E-2*(elog**3)
 
 
      ENDIF
      SODIUM0 = EXP(x)/(E*E*E)
 
 
      RETURN
      END
**==sulfur0.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
C----------------------------------------------------------------------
C----------------------------------------------------------------------
C     Real Function: SULFUR
C     Source: Atomic & Nuclear Data Tables, January 1982
C
C     Description:
C     Calculates mass absorption coefficient (mu/rho) for sulfur
C
C     History: original Aug 6, 1991 (MBC)
C
C     Usage:   FUNCTION SULFUR(E)
C              E = Energy in eV
C              SULFUR returns mu/rho in cm**2/g
C
C     COMMON Blocks:
C            none
C
C     IMPLICIT
C            none
C-------------------------------------------------------------------------
C
      FUNCTION SULFUR0(E)
      IMPLICIT NONE
C
C
      REAL E , elog , x , SULFUR0
C
C
      elog = ALOG(E)
 
 
      IF ( E.LT.165.0 ) THEN
         x = 598.2911 + (3.-678.2265)*elog + 308.1133*elog*elog - 
     &       68.99324*(elog**3) + 7.62458*(elog**4)
     &       - 0.3335031*(elog**5)
 
      ELSEIF ( E.LT.2470.5 ) THEN
         x = 3994.831 + (3.-3693.886)*elog + 1417.287*elog*elog - 
     &       287.9909*(elog**3) + 32.70061*(elog**4)
     &       - 1.968987*(elog**5) + 4.9149349E-2*(elog**6)
 
      ELSE
         x = -22.49628 + (14.24599+3.)*elog - 1.848444*elog*elog + 
     &       6.6506132E-2*(elog**3)
 
      ENDIF
      SULFUR0 = EXP(x)/(E*E*E)
 
 
      RETURN
      END
C
C--------------------------------------------------------------------------
C--------------------------------------------------------------------------
c ------------------------------------------------------------------ c
c  Reflection factor for nonrelativistic Compton reflection,
c  reflection orders >1 approximated with isotropic phase function,
c  and approximation of H-function by Basko ApJ 223,268,(1978),
c  valid up to 14keV;
c  sig - sigma_bf per hydrogen atom in sigma_Thomson units
c  xm  - cosine of the angle between normal to the slab and the
c        observation direction
 
      REAL FUNCTION GNR(Sig,Xm)
      IMPLICIT NONE
      REAL Sig , Xm
      REAL al0 , al1 , al2 , xl , x2 , x4 , SQ3
      PARAMETER (SQ3=1.732051)
 
      al0 = 1/(1+Sig/1.21)
      al1 = SQRT(1-al0)
      al2 = SQ3*al1
      xl = LOG(1+1/Xm)
      x2 = Xm*Xm
      x4 = x2*x2
 
      GNR = .5*al0*Xm*(.375*((3-2*x2+3*x4)*xl+(3*x2-1)*(.5-Xm))
     &      +((1+(1/al1-1)*(1-LOG(1+al2)/al2))*(1+Xm*SQ3)/(1+Xm*al2)-1)
     &      *xl)
 
      RETURN
      END
**==grxyh.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
 
c ------------------------------------------------------------------ c
c  Green's functions for Compton reflection with asymtotic
c  absorption included; output is (dy+y0)*G(mu,dy,y0)*W(mu,dy,y0),
c  for .0316<y0<100.; in two parts according to dy=2.
c  grxyh - subroutine for dy>2
c  grxyl - subroutine for pmx(26)<dy<2
c  pm1   - vector of angle-dependent coefficients,
c          set-up by pm1y subroutine
c  pmx   - vector of angle-dependent coefficients,
c          set-up by pmxy subroutine
c  dy    - wavelength shift dy=1/x-1/x0
c  y0    - incident photon wavelength y0=1/x0
c  ap    - absorption parameters
 
      REAL FUNCTION GRXYH(Pm1,Pmx,Dy,Y0,Ap)
      IMPLICIT NONE
      REAL Pm1(19) , Pmx(26) , Dy , Y0 , Ap(3)
      REAL pyx(10) , p1 , p4 , gr , AIC
 
      p1 = LOG(Y0)
      p4 = 1/Y0
      pyx(5) = .230 + .033*p1 + .763*Y0**(-.507) + .007*Y0**(-1.553)
      pyx(6) = 75.180*p4
      pyx(7) = -.100 + .115*p1 + .100*Y0**1.623
      IF ( Y0.LT.1. ) THEN
         pyx(8) = Pmx(19) + Pmx(20)*p1
      ELSE
         pyx(8) = Pmx(21) + Pmx(22)*p1 + Pmx(23)*(Pmx(24)+p4)**Pmx(25)
      ENDIF
      p1 = 1/(Y0+Dy)
      gr = pyx(5)*(1+pyx(6)*p1)**(pyx(7)*(1+pyx(8)*p1))
      p1 = 1 + Dy
      gr = gr*Pm1(18)*Dy**(-1.5)*p1*(1+Pm1(14)/p1)
     &     **(Pm1(15)+Pm1(16)*p1**Pm1(17))
      gr = gr*EXP(AIC(Y0,(Dy-.15),Ap(1)))
 
      GRXYH = gr
 
      RETURN
      END
**==grxyl.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
 
      REAL FUNCTION GRXYL(Pm1,Pmx,Dy,Y0,Ap)
      IMPLICIT NONE
      REAL Pm1(19) , Pmx(26) , Dy , Y0 , Ap(3)
      REAL pyx(10) , p1 , p2 , p3 , p4 , gr , AIC , xla0 , xla
 
      p1 = LOG(MIN(Y0,10.))
      p2 = p1*p1
      p3 = p2*p1
      pyx(1) = p1*(Pmx(1)+Pmx(2)*p1+Pmx(3)*p2+Pmx(4)*p3)
      pyx(2) = p1*(Pmx(5)+Pmx(6)*p1+Pmx(7)*p2+Pmx(8)*p3)
      pyx(3) = p1*(Pmx(9)+Pmx(10)*p1+Pmx(11)*p2+Pmx(12)*p3)
      pyx(4) = p1*(Pmx(13)+Pmx(14)*p1+Pmx(15)*p2+Pmx(16)*p3+Pmx(17)
     &         *p3*p1)
      pyx(9) = 1 + Pmx(18)*(.326*((Y0*.1)**1.692-1)+p1-2.303)
      IF ( Y0.LT.10. ) THEN
         p1 = Y0 + 2
         p1 = LOG(p1/(Y0+Dy))/LOG(p1/Y0)
         p2 = p1*p1
         p3 = p2*p1
         gr = EXP(pyx(1)+pyx(2)*p1+pyx(3)*p2+pyx(4)*p3)
      ELSE
         p4 = 1/(10+Dy)
         p1 = LOG(12*p4)*5.4848
         p2 = p1*p1
         p3 = p2*p1
         p4 = pyx(9)*(Y0+Dy)*p4
         gr = p4*EXP(pyx(1)+pyx(2)*p1+pyx(3)*p2+pyx(4)*p3)
      ENDIF
      p1 = Pm1(1) + Pm1(2)*Dy
      p2 = (Pm1(3)+Pm1(4)*MAX(0.,(Pm1(5)-Dy))**Pm1(6)+Pm1(7)*(Pm1(8)-Dy)
     &     **Pm1(9))**Pm1(10)/(1+EXP(Pm1(11)*(Pm1(12)-Dy**Pm1(13))))
      gr = gr*MIN(p1,p2)
      xla0 = 1/(1+Ap(1)*Y0**3)
      xla = 1/(1+Ap(1)*(Y0+Dy)**3)
      gr = gr*xla0*(Ap(2)+(1-Ap(2))*(1+(EXP(Ap(3)*Dy)-2)*SQRT(1-xla))
     &     *EXP(AIC(Y0,Dy,Ap(1))))

      GRXYL = gr
 
      RETURN
      END
**==aic.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
 
      REAL FUNCTION AIC(Y0,Dy,Apc)
      IMPLICIT NONE
      REAL Y0 , Dy , Apc
      REAL yp , a0 , a1 , c0 , c1 , c2 , d0l , d0h , d1l , d1h
      REAL aih , ail
 
      yp = Y0 + Dy
      a0 = Apc
      a1 = Apc**.3333
      c0 = .5/a1
      c1 = -3.4641*c0
      c2 = 0.5774
      d0l = a0*Y0**3
      d0h = a0*yp**3
      d1l = a1*Y0
      d1h = a1*yp
      ail = Y0*(3-LOG(1+d0l)) + c0*LOG((1-d1l+d1l**2)/(1+d1l)**2)
     &      + c1*ATAN(c2*(2*d1l-1))
      aih = yp*(3-LOG(1+d0h)) + c0*LOG((1-d1h+d1h**2)/(1+d1h)**2)
     &      + c1*ATAN(c2*(2*d1h-1))
 
      AIC = aih - ail
 
      RETURN
      END
**==pmxy.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
 
c ------------------------------------------------------------------ c
c  Angle dependent coefficients for Green's function for Compton
c  reflection;
c  xm  - cosine of the angle between normal to the slab and the
c        observation direction
c  pmx - output coefficients vector
 
      SUBROUTINE PMXY(Xm,Pmx)
      IMPLICIT NONE
      REAL Xm , Pmx(26)
      REAL p2 , p3 , p4
 
      p2 = Xm*Xm
      p3 = p2*Xm
      p4 = p3*Xm
      Pmx(1) = .8827 - .5885*Xm + .7988*p2 - .4117*p3
      Pmx(2) = .0517 + .1076*Xm - .1691*p2 + .0678*p3
      Pmx(3) = .0014 + .0043*Xm
      Pmx(4) = -.0003 - .0027*Xm + .0021*p2
      Pmx(5) = -1.3259 + 1.6214*Xm - 3.7007*p2 + 2.1722*p3 + 
     &         EXP(20.3*(Xm-1.063))
      Pmx(6) = .0790 - .4029*Xm + .6619*p2 + .2210*p3 - 
     &         EXP(17.7*(Xm-1.041))
      Pmx(7) = .0751 - .1848*Xm + .4068*p2 - .4126*p3 + 
     &         EXP(9.8*(Xm-1.185))
      Pmx(8) = -.0020 - .0394*Xm + .1004*p2 - .0597*p3
      Pmx(9) = 3.2953 - 3.6996*Xm + 7.9837*p2 - 4.6855*p3
      Pmx(10) = -.5278 + .9857*Xm - 1.7454*p2 - .3464*p3 + 
     &          EXP(27.6*(Xm-0.959))
      Pmx(11) = -.1919 + .5798*Xm - 1.2879*p2 + 1.4885*p3 - 
     &          EXP(18.3*(Xm-0.986))
      Pmx(12) = .0200 + .0832*Xm - .0333*p2 - .2370*p3 + 
     &          EXP(16.6*(Xm-1.086))
      Pmx(13) = -2.2779 + 2.4031*Xm - 4.0733*p2 + 1.9499*p3 - 
     &          EXP(19.6*(Xm-0.968))
      Pmx(14) = .4790 - 1.0166*Xm + 3.1727*p2 - 4.0108*p3 + 3.0545*p4 - 
     &          EXP(30.4*(Xm-0.957))
      Pmx(15) = .1122 - .3580*Xm + .4985*p2 - .3750*p3 - .5349*p4 + 
     &          EXP(31.2*(Xm-0.972))
      Pmx(16) = -.0160 - .0471*Xm - .0536*p2 + .2006*p3 + .2929*p4 - 
     &          EXP(30.6*(Xm-1.009))
      Pmx(17) = .0005 + .0021*Xm - .0447*p2 + .1749*p3 - .2303*p4 + 
     &          EXP(16.9*(Xm-1.130))
      Pmx(18) = .006 + .089*Xm - .102*p2 + .056*p3
      Pmx(19) = .618 - .830*Xm
      Pmx(20) = .128 - .132*Xm
      Pmx(21) = .632 - .875*Xm
      Pmx(22) = -.672 - .286*Xm + .717*p2 - .481*p3
      Pmx(23) = .0126 - .0160*Xm + .0077*p2
      Pmx(24) = .0111 + .0030*Xm - .0014*p2
      Pmx(25) = -2.437 - .328*Xm - .260*p2 + .279*p3
      Pmx(26) = 1 - SQRT(1-(Xm-.05)**2)
 
      RETURN
      END
**==pm1y.spg  processed by SPAG 4.50J  at 11:22 on 27 Feb 1996
 
c ------------------------------------------------------------------ c
c  Angle dependent coefficients for Green's function for Compton
c  reflection of incident photon at energy x0=1;
c  xm  - cosine of the angle between normal to the slab and the
c        observation direction
c  pm1 - output coefficients vector
 
      SUBROUTINE PM1Y(Xm,Pm1)
      IMPLICIT NONE
      REAL Xm , Pm1(19)
      REAL p2 , p3 , p4
 
      p2 = .911 - .549*(1-Xm)**1.471
      p3 = .254 - .041*Xm**(-3.798)
      Pm1(1) = p2 - 2*p3
      Pm1(2) = p3
      Pm1(3) = .161 + .439*Xm**.189 - .791*Xm**7.789
      Pm1(4) = MAX(0.,(-.871+1.740*Xm**.176-1.088*Xm**.907))
      Pm1(5) = .934 + .054*Xm**(-.666)
      p4 = MIN(.524,Xm)
      Pm1(6) = 4.647 - 11.574*p4 + 11.046*p4*p4
      Pm1(7) = .012 + .199*Xm**.939 + .861*Xm**8.999
      Pm1(8) = 2.150
      p2 = Xm*Xm
      p3 = p2*Xm
      Pm1(9) = -1.281 + 2.374*Xm - 4.332*p2 + 2.630*p3
      Pm1(10) = 2.882 + .035*Xm**(-1.065)
      Pm1(11) = 30.28 + 69.29*Xm
      Pm1(12) = 1.037*(1-SQRT(1-.874*p2))**.123
      Pm1(13) = .123
      p4 = p3*Xm
      Pm1(14) = 56.50 - 27.54*Xm + 671.8*p2 - 1245.*p3 + 708.4*p4
      Pm1(15) = -.897 - .826*Xm + 2.273*p2 - .984*p3
      Pm1(16) = 1.240 - 1.297*Xm
      Pm1(17) = -.490 + 1.181*Xm - 1.038*p2
      Pm1(18) = 1.858*(Xm**2.033+0.745*Xm**1.065)
      Pm1(19) = 1 - SQRT(1-(Xm-.05)**2)
 
      RETURN
      END

      real function apf (x)
      implicit none
      real x, xinv, xinv2,xinv4
      real c0,c1,c2,c3,c4,c4inv
      data c0/0.802/c1/-1.019/c2/2.528/c3/-3.198/c4/1.457/c4inv/0.030/
* code outside will check for x == 0

      xinv = (c4inv/x)
      xinv2 = xinv*xinv
      xinv4 = xinv2*xinv2
           
      apf = c0 + x*(c1 + x*(c2 + x*(c3 + x*c4))) + xinv4
      return
      end

