      SUBROUTINE MUON_COSMIC_FLUX_SURFACE
     $     (EMUON,cz,WEIGHT,flux)
*
*     Calculate cosmic ray flux at the surface.
*
*
*     Contact:  Alfred Tang
*
*     Parameters:
*     ==========
*
*           emuon,theta,phi:           <out>
*                      muon energy (GeV) and direction
*           weight:                    <out>
*                      probability of event.
*
*     Specification:-
*     =============
*     This code uses modified Gaisser's muon flux at the surface to find energy
*     and angular distribution.
 
      IMPLICIT NONE
      LOGICAL FIRST
      integer model
      DOUBLE PRECISION EMUON,WEIGHT,costh
      DOUBLE PRECISION DIDE, EMU,flux,cz,lmin,lmax
      DOUBLE PRECISION I_TOT, DE, DCOSTH,iranlux
      DOUBLE PRECISION LEMU, JACOBIAN, COSMIC_FLUX
      real*4 re
      REAL RANUM
      DATA FIRST /.TRUE./
      SAVE FIRST
      DOUBLE PRECISION ZERO
      PARAMETER (ZERO = 0.D0)
      EXTERNAL RANUM
      common/le/lmin,lmax
      DOUBLE PRECISION PI
      PARAMETER (PI = 3.1415926535897932384626433)

      DOUBLE PRECISION LEMAX, LEMIN
      PARAMETER (LEMIN  = -0.97469413)         ! log10 of 0.106 GeV
      PARAMETER (LEMAX  = 6.00000)             ! log10 of 1000 TeV
 
      real xs_cosmic_total

C     First, integrate muon flux to find normalization.
 
      I_TOT    = ZERO
      IF (FIRST) THEN
         FIRST = .FALSE.
         DE = (LEMAX-LEMIN)/1000.
         DCOSTH = 1./1000.
         DO LEMU = LEMIN, LEMAX, DE
            EMU = 10.**LEMU
            JACOBIAN = LOG(10.)*EMU
            DO COSTH = 0., 1., DCOSTH
               DIDE = COSMIC_FLUX(EMU,COSTH)
               I_TOT = I_TOT + JACOBIAN * DIDE * DE * DCOSTH
            ENDDO
         ENDDO
         XS_COSMIC_TOTAL = I_TOT*2.*pi
         WRITE(6,*) 'COSMIC FLUX = ',XS_COSMIC_TOTAL,'/cm-sq/s'
      ENDIF
 
C     For each event, throw the direction, energy, and associated weight
      lmin=lemin
      lmax=lemax

      call ranlux(re,1)
      lemu=lemin+re*(lemax-lemin)
      EMUON = 10.**(LEMU)
      JACOBIAN = LOG(10.)*EMUON

      costh=cz
      flux=COSMIC_FLUX(EMUON,COSTH)
      WEIGHT=(lemax-lemin)*JACOBIAN*flux
      flux=flux*emuon
 
      RETURN
      END
 
      DOUBLE PRECISION FUNCTION COSMIC_FLUX(EMU,COSTH)
 
      IMPLICIT NONE
      REAL CS_A, CS_GAMMA, CS_RC
      integer model

      DOUBLE PRECISION CTMIN, CTMAX,de,emu0,emu1,em,low
      PARAMETER (CTMIN = 0.0)
      PARAMETER (CTMAX = 1.0)
      parameter (low = 1.)
 
      DOUBLE PRECISION EMU,COSTH,costa,PAR(2)
      DATA PAR /115., 850./

      costa=costh
      if(costa.ne.1.) then
      costa=dsqrt((costa**2+1.052122e-2-.068287*costa**.958633+
     $     .0407253*costa**.817285)/.9829595)
      endif
      if(costa.ge.1.) costa=1.

      cs_a=1.
      cs_gamma=2.7
      cs_rc=0.
      model=0
      emu0=emu
      emu1=emu

      if(emu0.le.low/costa) then
         emu0=(3.*emu0+7.*low/costa)/10.
         emu1=emu0
      endif

      if(emu0.le.100./costa) then
         cs_rc=1.e-4
         de=2.06e-3*(950./costa-90.)
         emu0=emu1+de
         cs_a=1.1*(9.*dsqrt(costh+.001)/103.)**(4.5/costa/emu0)
      endif
  
C     This function returns DI/Dcos*DE for muons at the Earth's surface.
 
      COSMIC_FLUX = 0.
      IF ((COSTH.LE.CTMIN).OR.(COSTH.GT.CTMAX)) RETURN
 
      IF (MODEL.EQ.0) THEN

         COSMIC_FLUX = CS_A * 0.14 * 
     $        (EMU1)**(-CS_GAMMA) *
     $        (1./(1.+1.1*EMU0*COSTA/PAR(1)) +
     $        0.054/(1.+1.1*EMU0*COSTA/PAR(2)) + CS_RC)
         
      ELSEIF (MODEL.EQ.1) THEN

         COSMIC_FLUX = CS_A * 372. / (EMU*COSTH + 80.) *
     $        (5.*EMU + 10./COSTH)**(-CS_GAMMA) *
     $        ((EMU + 15.)/(EMU + 15./COSTH))

      ENDIF

      RETURN
      END
