      SUBROUTINE CELLFLUX(CYSTINIT,CYSTBED,GRATE,
     &  CELLFLX,CCELLFLX,GRMADD,PGERM,II)
      INCLUDE 'comdeck'
      INCLUDE 'dino.inc'

C This subroutine calculates the amount of germinating cells based on spatial
C cyst bed data and on equations relating cyst germination rates to light,
C temperature, and time of year.  Variables are as follows:
C
C Variables in dino.inc
C BIOMOD: an array of biological model settings (set in biodat.f)
C DASWR: day averaged short-wave irradiance (set in biodat.f) 
C
C Passed variables:
C CYSTINIT: the initial # of cysts per m2 in top dgerm cm
C CYSTBED: the present # of cysts per m2 in top dgerm cm
C          
C GRATE: The germination rate at each grid point (%/day) 
C CELLFLX: The flux of new cells at each grid point 
C           (cells m-2 (time step)-1)
C CCELLFLX: The cumulative flux of cells at each grid point (cells m-2)
C GRMADD: The number of new cells added via germination at each
C         grid point (# cells)
C PGERM: The percentage of the initial cysts that have germinated since
C        the beginning of the simulation (%)
C II: index included to allow for multiple tracers
C
C Local variables:
C GERML: The germ. rate (%/day) under light conditions 
C GERMD: The germ. rate (%/day) under dark conditions
C GERM: The germ. rate (cysts/time step) at the light level of interest
C EFLUX: The diffuse downwelling irradiance (watts/m2) within the
C         sediment.
C ATTENK: diffuse light attenuation (m-1) from BIOMOD (+)
C ATTENS: diffuse light attenuation in sediment (mm-1) (+)
C EFLUXL: Irradiance threshold for germination at light rates (watts/m2)
C EFLUXD: Irradiance threshold for germination at dark rates (watts/m2)
C DGERM: The germination depth (cm)
C
C Hydrodynamic model variables:
C DT: the total water column depth (m,+)
C DTI: time step length
C ART: area of grid cell (m2)
C T: temperature
C KBM1: index of deepest sigma layer
C IM: x-dimemsion of the model grid
C JM: y-dimension of the model grid
     
C Local Variables 
      REAL GERML(IM,JM), GERMD(IM,JM), GERM(IM,JM)
      REAL EFLUX(IM,JM,10)

      REAL ATTENK, ATTENS, EFLUXL, EFLUXD, DGERM
      

C passed variables
      INTEGER II
      REAL CYSTINIT(IM,JM),CYSTBED(IM,JM)
      REAL GRATE(IM,JM),CELLFLX(IM,JM),CCELLFLX(IM,JM)
      REAL GRMADD(IM,JM),PGERM(IM,JM)
     
C zero the local arrays 
      DO 28 I = 1,IM
	DO 29 J = 1,JM
          GERM(I,J) = 0.0
          GERMD(I,J) = 0.0
          GERML(I,J) = 0.0
          GRMADD(I,J) = 0.0
          DO 27 K = 1,10
            EFLUX(IM,JM,K) = 0.0
27        CONTINUE 
29	CONTINUE
28    CONTINUE

C setting light attenuation constant, the values
C that differentiate the "light", "dark", and "transitional"
C conditions for germination, and the germination depth based
C on the input from the array BIOMOD, which was input in biodat
     
      ATTENK = BIOMOD(II,7) 		!m-1
      ATTENS = BIOMOD(II,8)*10.0	!convert from mm-1 to cm-1
      EFLUXL = BIOMOD(II,9)		!watts/m2 
      EFLUXD = BIOMOD(II,10)		!watts/m2 
      DGERM = BIOMOD(II,2)		!cm

C begin loop through all grid points to calculate
C the germination rate and fluxes

      DO 30 I = 2,IMM1
	DO 31 J = 2,JMM1

C calculate light/dark germination rate

          GERML(I,J) = 1.50 + (8.72 - 1.50) * 0.5 *
     &                (tanh(0.790 * T(I,J,KBM1) - 6.27) + 1)
          GERMD(I,J) = 1.04 + (4.26 - 1.04) * 0.5 * 
     &                (tanh(0.394 * T(I,J,KBM1) - 3.33) + 1) 

C*************************************************************************
C calculate the irradiance and germination rates within the sediment.  DGERM 
C is divided into 10 equally spaced vertical segments and the germination
C rate is calculated within each segment. The final flux of cells
C per unit area of sediment is obtained by integrating the flux per segment.
C Mixing is assumed to keep the cyst concentration relatively uniform 
C between 0 and dgerm cm. 
C*************************************************************************

            DO 32 K = 1,10
              EFLUX(I,J,K)=(DASWR*EXP(-ATTENK*DT(I,J)))
     &                  *EXP(-ATTENS*(DGERM/10.0*K - DGERM/20.0))

C checking to see which light regime applies: light, dark, or transitional

              IF (EFLUX(I,J,K).GT.EFLUXL) THEN
	        GERM(I,J) = GERM(I,J)+GERML(I,J)*0.1
	      ELSE IF (EFLUX(I,J,K).LT.EFLUXD) THEN
	        GERM(I,J) = GERM(I,J)+GERMD(I,J)*0.1
              ELSE
	        GERM(I,J)=GERM(I,J)+((GERML(I,J)-GERMD(I,J))*
     &          ((EFLUX(I,J,K)-EFLUXD)/(EFLUXL-EFLUXD)) 
     &          + GERMD(I,J)) * 0.1 
	      END IF
 32         CONTINUE

C ** The Endogenous Clock: scale the germination rate by the factor 
C ** calculated in endoclock: ENDOSCALE

	    GERM(I,J) = GERM(I,J)*ENDOSCALE

C convert % cysts/day into decimal fraction of cysts per time step, save
C % cysts/day data as GRATE for writing to CDF file

	    GRATE(I,J) = GERM(I,J) 
	    GERM(I,J) = (GERM(I,J)/100.0)*DTI/86400.0

C calculate the flux of cells away from the bottom. It is referenced
C to the initial number of cysts to be consistent with laboratory 
C experiments.

	    CELLFLX(I,J) = CYSTINIT(I,J)*GERM(I,J)

C calculate the number of cysts remaining after that flux
          
            IF ((CYSTBED(I,J) - CELLFLX(I,J)).GT.0.0) THEN
	      CYSTBED(I,J) = CYSTBED(I,J) - CELLFLX(I,J)
	    ELSE 
	      CELLFLX(I,J) = CYSTBED(I,J)
	      CYSTBED(I,J) = 0 
	    END IF

            CCELLFLX(I,J) = CCELLFLX(I,J) + CELLFLX(I,J)
            GRMADD(I,J) = CELLFLX(I,J)*ART(I,J)

C calculate the percent germination for display/analysis reasons

            IF (CYSTINIT(I,J).NE.0.0) THEN
              PGERM(I,J) = 100.0-((CYSTBED(I,J)/CYSTINIT(I,J))*100.0)
            ELSE
	      PGERM(I,J) = 100.0
	    END IF

  31    CONTINUE
  30    CONTINUE

      RETURN
      END
