       MODULE MODULE_EBI_SOLVER
!MONARCH DRIVER EBI SOLVER CB05+NO2E CMAQ
!
      USE MODULE_DM_PARALLEL
      USE MODULE_BSC_CHEM_DATA

      IMPLICIT NONE
!
!-----------------------------------------------------------------------
!
      INTEGER :: MYPE
!
!-----------------------------------------------------------------------
!
      CONTAINS
CModels-3/CMAQ code adapted for MONARCH

C***********************************************************************
C   Portions of Models-3/CMAQ software were developed or based on      *
C   information from various groups: Federal Government employees,     *
C   contractors working on a United States Government contract, and    *
C   non-Federal sources (including research institutions).  These      *
C   research institutions have given the Government permission to      *
C   use, prepare derivative works, and distribute copies of their      *
C   work in Models-3/CMAQ to the public and to permit others to do     *
C   so.  EPA therefore grants similar permissions for use of the       *
C   Models-3/CMAQ software, but users are requested to provide copies  *
C   of derivative works to the Government without restrictions as to   *
C   use by others.  Users are responsible for acquiring their own      *
C   copies of commercial software associated with Models-3/CMAQ and    *
C   for complying with vendor requirements.  Software copyrights by    *
C   the MCNC Environmental Modeling Center are used with their         *
C   permissions subject to the above restrictions.                     *
C***********************************************************************

C RCS file, release, date & time of last delta, author, state, [and locker]
C $Header: /project/work/rep/CCTM/src/chem/ebi_cb05cl/hrdriver.F,v 1.2 2008/09/10 17:54:34 sjr Exp $ 

C what(1) key, module and SID; SCCS file; date and time of last delta:
C %W% %P% %G% %U%

c      SUBROUTINE CHEM( CGRID, JDATE, JTIME, TSTEP )
!MONARCH
      SUBROUTINE RUN_EBI(NTIMESTEP,DT,NPHS,TEMPERATURE                  
     &          ,START_HOUR                                              
     &          ,CHEM,NUM_GAS_TOTAL                                      
     &          ,P8W,RHO_PHY,WATER,NUM_WATER,P_QV                        
     &          ,JPNL                                                    
     &          ,IDS,IDE,JDS,JDE,LM                                      
     &          ,IMS,IME,JMS,JME,KMS,KME                                 
     &          ,ITS,ITE,JTS,JTE)
!MONARCH
C**********************************************************************
C
C  FUNCTION: Driver subroutine for Euler Backward Iterative solver
C
C  PRECONDITIONS: For the CB05CL family of mechanisms
C
C  KEY SUBROUTINES/FUNCTIONS CALLED:  HRINIT, PHOT, HRCALCKS, HRSOLVER
C
C  REVISION HISTORY: Created by EBI solver program, Nov. 6, 2006
C                       Based on the algorithm in "Test of Two Numerical
C                       Schemes for Use in Atmospheric Transport-Chemistry
C                       Models", O. Hertel, R. Berkowicz, J. Christensen,
C                       and O. Hov, Atm Env., Vol. 27A, No. 16, 1993.
C                       Original MEBI code developed by Ho-Chun Huang,
C                       SUNY, Albany -- "On the performance of numerical
C                       solvers for a chemistry submodel in three-dimensional
C                       air quality models 1. Box model simulations",
C                       H. Huang and J.S. Chang, JGR, Vol 106, No. D17, 2001.
C                       This version replaces Huang and Chang use of numerical
C                       solutions with analytical solutions derived in
C                       Hertel et al.
C
C**********************************************************************

c      USE HGRD_DEFN             ! horizontal domain specifications
c      USE VGRD_DEFN             ! vertical layer specifications
      USE HRDATA
      USE CONST
!	USE GC_SPC_mod
!      USE MODULE_CHEM_DATA
	USE RXCM

      IMPLICIT NONE

C..Includes:
c      INCLUDE SUBST_IOPARMS   ! Io/api parameters
c      INCLUDE SUBST_IOFDESC   ! Io/api file descriptions
c      INCLUDE SUBST_IODECL    ! Io/api declarations
c      INCLUDE SUBST_FILES_ID  ! CMAQ files
c      INCLUDE "CONST.EXT" !SUBST_CONST     ! CMAQ constants
c      INCLUDE "GC_SPC.EXT" !SUBST_GC_SPC    ! Gas chem species names and MWs
c      INCLUDE "RXCM.EXT" !SUBST_RXCMMN    ! Mechanism reaction common block
c      INCLUDE SUBST_GC_EMIS   ! Gas chem emissions name and mapping tables

c#ifdef emis_chem
c      INCLUDE SUBST_EMPR_CH   ! Emissions processing in chem
c#else
c      INCLUDE SUBST_EMPR_VD   ! Emissions processing in vdif
c#endif

c      INCLUDE SUBST_PACTL_ID  ! Process analysis control parameters

!MONARCH
!-----------------------------------------------------------------------
!
      INTEGER,INTENT(IN) :: IDS,IDE,JDS,JDE,LM                          
     &                      ,IMS,IME,JMS,JME,KMS,KME                     
     &                      ,ITS,ITE,JTS,JTE                             
     &                      ,NPHS,NTIMESTEP,START_HOUR                   
     &                      ,NUM_WATER,P_QV
!
      INTEGER,DIMENSION(ITS:ITE,JTS:JTE),INTENT(IN) :: JPNL
!
      REAL,INTENT(IN) :: DT

      REAL,DIMENSION(ITS:ITE,JTS:JTE,1:LM,1:NUM_WATER),INTENT(IN) :: WATER

      INTEGER,INTENT(IN) :: NUM_GAS_TOTAL

      REAL,DIMENSION(ITS:ITE,JTS:JTE,1:LM,NUM_GAS_TOTAL),INTENT(INOUT) ::  CHEM

      REAL,DIMENSION(IMS:IME,JMS:JME,1:LM),INTENT(IN) :: TEMPERATURE

      REAL,DIMENSION(ITS:ITE,1:LM+1,JTS:JTE),INTENT(IN) :: P8W

      REAL,DIMENSION(ITS:ITE,1:LM,JTS:JTE),INTENT(IN) :: RHO_PHY
!
!-----------------------------------------------------------------------
!***
!***  LOCAL VARIABLES
!***
!-----------------------------------------------------------------------
!MONARCH
C..Arguments:
!MONARCH
      REAL,DIMENSION(ITS:ITE,JTS:JTE
     &    ,1:LM,1:63) :: CGRID    !!!MONARCH N_GC_SPCD=73 !
      INTEGER JDATE           ! Current date (YYYYDDD)
      INTEGER JTIME           ! Current time (HHMMSS)
      INTEGER TSTEP( 2 )      ! Time step vector (HHMMSS)

C..Parameters:
      REAL, PARAMETER :: PA2ATM = 1.0 / STDATMPA    ! Pascal to atm conv fac

C..Saved Local Variables:

      INTEGER, SAVE :: ISTFL            ! Unit no. of iteration stat output file
      LOGICAL, SAVE :: LFIRST = .TRUE.  ! Flag for first call to this subroutine

      REAL, SAVE :: MAOMV               ! Mol Wt of air over Mol Wt of water

C..Scratch Local Variables:
      CHARACTER( 132 ) :: MSG           ! Message text
      CHARACTER(  16 ) :: VNAME         ! Name of I/O API data variable

      INTEGER C, E, L, R, S   ! Loop indices

      INTEGER AVGEBI          ! Average no. of EBI iterations
      INTEGER DELT_SEC        ! EBI max time step in seconds
      INTEGER ESP             ! Loop index for emissions species
      INTEGER ITMSTEP         ! Chemistry integration interval (sec)
      INTEGER LEV             ! Layer index
      INTEGER MIDDATE         ! Date at time step midpoint
      INTEGER MIDTIME         ! Time at time step midpoint
      INTEGER MNEBI           ! Min no. of EBI iterations
      INTEGER MXEBI           ! Max no. of EBI iterations
      INTEGER NDARK           ! Number of layer 1 cells in darkness
      INTEGER NPH             ! Index for number of phot. rxns in PHOT
      INTEGER SPC             ! Species loop index
      INTEGER STATUS          ! Status code
      INTEGER VAR             ! Variable number on I/O API file

      LOGICAL LSUNLIGHT       ! Flag for sunlight

      REAL ATMPRES            ! Cell pressure
      REAL CHEMSTEP           ! Chemistry integration interval (min)
      REAL H2O                ! Cell H2O mixing ratio (ppmV)
      REAL SUMEBI             ! Sum of EBI iterations
      REAL TEMP               ! Cell Temperature
!MONARCH
      REAL PRES(ITS:ITE,JTS:JTE,1:LM)        ! Cell pressure (Pa)
      REAL QV(ITS:ITE,JTS:JTE,1:LM)        ! Cell water vapor (Kg/Kg air)
      REAL TA(ITS:ITE,JTS:JTE,1:LM)        ! Cell temperature (K)
      REAL RJIN( NPHOTAB )                       ! J-values for a cell
      REAL RJ(ITS:ITE,JTS:JTE,1:LM, NPHOTAB )    ! J-values for each cell

      INTEGER KFLIP
!MONARCH
      INTEGER     GXOFF, GYOFF            ! global origin offset from file
C for INTERPX
      INTEGER, SAVE :: STRTCOLMC3, ENDCOLMC3, STRTROWMC3, ENDROWMC3


C**********************************************************************
!      CGRID = 1e-30
!      CGRID(:,:,:,1:NUM_GAS_TOTAL)=CHEM(:,:,:,1:NUM_GAS_TOTAL)
c	CGRID(1,1,1,1)=0.134 !* 24624156175467.0
c	CGRID(1,1,1,2)=1E-30 * 1.e6 !* 24624156175467.0
c	CGRID(1,1,1,22)=250. !* 24624156175467.0
c      CGRID = .1 
!	CGRID(1,1,1,1) = 0.2e-4 !*CFACTOR
!	CGRID(1,1,1,2) = 0.2e-4 !*CFACTOR
!	CGRID(1,1,1,4) = 0.35e-1 !*CFACTOR
!	CGRID(1,1,1,10) = 0.5e-4 !*CFACTOR
!	CGRID(1,1,1,13) = 0.1e-2 !*CFACTOR
!	CGRID(1,1,1,19) = (0.3e-4 + 2*0.1e-6) !*CFACTOR
!	CGRID(1,1,1,18) = 0.25e-3 !*CFACTOR
!	CGRID(1,1,1,29) = 0.2e-4 !*CFACTOR
!	CGRID(1,1,1,22) = 0.8e-1 !*CFACTOR
!	CGRID(1,1,1,35) = 0.2e-6 !*CFACTOR
!	CGRID(1,1,1,38) = 0.1e-5 !*CFACTOR
!	CGRID(1,1,1,45) = 0.2e-6 !*CFACTOR
!	CGRID(1,1,1,11) = 0.1e-8 !*CFACTOR
!	CGRID(1,1,1,12) = 0.2e-4 !*CFACTOR
!	CGRID(1,1,1,44) = 0.25e-6 !*CFACTOR
!	CGRID(1,1,1,39) = 0.1e-8 !*CFACTOR
!	CGRID(1,1,1,47) = 0.2e-5 !*CFACTOR
!	CGRID(1,1,1,21) = (0.4*0.+2.9*0.4e-4+4.8*0.4e-4+7.9*0.2e-4
!     &	+0.8*0.2e-6+0.8*0.1e-6+3.9*0.3e-4) !*CFACTOR


      ITMSTEP = NPHS * DT


ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c  On first call, call routines to set-up for EBI solver
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      IF( LFIRST ) THEN

c         LOGDEV = INIT3( )
          LOGDEV = 6

c         IF( MECHNAME .NE. 'CB05CL_AE4_AQ' ) THEN
c             MSG = 'This version of the EBI solver can only be used with'
c     &            // ' the CB05CL_AE4_AQ chemical mechanism'
c             CALL M3EXIT( PNAME, 0, 0, MSG, XSTAT1 )
c         ENDIF

c         IF( INDEX( MECHNAME, 'AE' ) .NE. 0 ) THEN
c           L_AE_VRSN = .TRUE.
c         ELSE
           L_AE_VRSN = .FALSE.
c         ENDIF

c         IF( INDEX( MECHNAME, 'AQ' ) .NE. 0 ) THEN
c           L_AQ_VRSN = .TRUE.
c         ELSE
           L_AQ_VRSN = .FALSE.
c         ENDIF

c         IF( LIRR ) THEN
c            MSG = 'IRR Analysis not allowed with EBI solver'
c            CALL M3EXIT( PNAME, JDATE, JTIME, MSG, XSTAT1 )
c         ENDIF

         CALL HRINIT

c         ITMSTEP = 60. !180.+45. !600. !TIME2SEC( TSTEP( 2 ) )
         CHEMSTEP = FLOAT( ITMSTEP ) / 60.0
!         WRITE( LOGDEV, 92000 ) CHEMSTEP, DELTAT

!         WRITE( LOGDEV, 92020 )
!         DO SPC = 1, 72 !N_GC_SPC
!            WRITE( LOGDEV, 92040 ) GC_SPC( SPC ), RTOL( SPC )
!         ENDDO

         MAOMV =  MWAIR / MWWAT

c..If emissions processing requested stop
c         IF( EMISCH ) THEN

c            MSG = 'ERROR: EBI solver not configured to '//
c     &            'process emissions in chemistry'
c            CALL M3EXIT( PNAME, JDATE, JTIME, MSG, XSTAT1 )

c         ENDIF   ! End if doing emissions


c#ifdef hrstats
c         ISTFL = JUNIT()
c         OPEN( UNIT=ISTFL, FILE='iterstat.dat' )
c         WRITE( ISTFL, 94020 )
c#endif

c         CALL SUBHFILE ( MET_CRO_3D, GXOFF, GYOFF,
c     &                   STRTCOLMC3, ENDCOLMC3, STRTROWMC3, ENDROWMC3 )

         LFIRST = .FALSE.

      ENDIF      ! First time

!MONARCH
      RKI(:)=0.
      RXRAT(:)=0.
      YC(:)=0.
      YC0(:)=0.
      YCP(:)=0.
      PROD(:)=0.
      LOSS(:)=0.
      PNEG(:)=0.

      CGRID = 1e-30
      CGRID(:,:,:,NO2) = MAX(CHEM(:,:,:,P_NO2),1.0E-30)
      CGRID(:,:,:,NO) = MAX(CHEM(:,:,:,P_NO),1.0E-30)
      CGRID(:,:,:,O3) = MAX(CHEM(:,:,:,P_O3),1.0E-30)
      CGRID(:,:,:,NO3) = MAX(CHEM(:,:,:,P_NO3),1.0E-30)
      CGRID(:,:,:,N2O5) = MAX(CHEM(:,:,:,P_N2O5),1.0E-30)
      CGRID(:,:,:,HNO3) = MAX(CHEM(:,:,:,P_HNO3),1.0E-30)
      CGRID(:,:,:,HONO) = MAX(CHEM(:,:,:,P_HONO),1.0E-30)
      CGRID(:,:,:,PNA) = MAX(CHEM(:,:,:,P_PNA),1.0E-30)
      CGRID(:,:,:,H2O2) = MAX(CHEM(:,:,:,P_H2O2),1.0E-30)
      CGRID(:,:,:,NTR) = MAX(CHEM(:,:,:,P_NTR),1.0E-30)
      CGRID(:,:,:,ROOH) = MAX(CHEM(:,:,:,P_ROOH),1.0E-30)
      CGRID(:,:,:,FORM) = MAX(CHEM(:,:,:,P_FORM),1.0E-30)
      CGRID(:,:,:,ALD2) = MAX(CHEM(:,:,:,P_ALD2),1.0E-30)
      CGRID(:,:,:,ALDX) = MAX(CHEM(:,:,:,P_ALDX),1.0E-30)
      CGRID(:,:,:,PAR) = MAX(CHEM(:,:,:,P_PAR),1.0E-30)
      CGRID(:,:,:,CO) = MAX(CHEM(:,:,:,P_CO),1.0E-30)
      CGRID(:,:,:,MEPX) = MAX(CHEM(:,:,:,P_MEPX),1.0E-30)
      CGRID(:,:,:,MEOH) = MAX(CHEM(:,:,:,P_MEOH),1.0E-30)
      CGRID(:,:,:,FACD) = MAX(CHEM(:,:,:,P_FACD),1.0E-30)
      CGRID(:,:,:,PAN) = MAX(CHEM(:,:,:,P_PAN),1.0E-30)
      CGRID(:,:,:,PACD) = MAX(CHEM(:,:,:,P_PACD),1.0E-30)
      CGRID(:,:,:,AACD) = MAX(CHEM(:,:,:,P_AACD),1.0E-30)
      CGRID(:,:,:,PANX) = MAX(CHEM(:,:,:,P_PANX),1.0E-30)
      CGRID(:,:,:,OLE) = MAX(CHEM(:,:,:,P_OLE),1.0E-30)
      CGRID(:,:,:,ETH) = MAX(CHEM(:,:,:,P_ETH),1.0E-30)
      CGRID(:,:,:,IOLE) = MAX(CHEM(:,:,:,P_IOLE),1.0E-30)
      CGRID(:,:,:,TOL) = MAX(CHEM(:,:,:,P_TOL),1.0E-30)
      CGRID(:,:,:,CRES) = MAX(CHEM(:,:,:,P_CRES),1.0E-30)
      CGRID(:,:,:,OPEN) = MAX(CHEM(:,:,:,P_OPEN),1.0E-30)
      CGRID(:,:,:,MGLY) = MAX(CHEM(:,:,:,P_MGLY),1.0E-30)
      CGRID(:,:,:,XYL) = MAX(CHEM(:,:,:,P_XYL),1.0E-30)
      CGRID(:,:,:,ISOP) = MAX(CHEM(:,:,:,P_ISOP),1.0E-30)
      CGRID(:,:,:,ISPD) = MAX(CHEM(:,:,:,P_ISPD),1.0E-30)
      CGRID(:,:,:,TERP) = MAX(CHEM(:,:,:,P_TERP),1.0E-30)
      CGRID(:,:,:,SO2) = MAX(CHEM(:,:,:,P_SO2),1.0E-30)
      CGRID(:,:,:,SULF) = MAX(CHEM(:,:,:,P_SULF),1.0E-30)
      CGRID(:,:,:,ETOH) = MAX(CHEM(:,:,:,P_ETOH),1.0E-30)
      CGRID(:,:,:,ETHA) = MAX(CHEM(:,:,:,P_ETHA),1.0E-30)
      CGRID(:,:,:,O) = MAX(CHEM(:,:,:,P_O),1.0E-30)
      CGRID(:,:,:,O1D) = MAX(CHEM(:,:,:,P_O1D),1.0E-30)
      CGRID(:,:,:,OH) = MAX(CHEM(:,:,:,P_OH),1.0E-30)
      CGRID(:,:,:,HO2) = MAX(CHEM(:,:,:,P_HO2),1.0E-30)
      CGRID(:,:,:,XO2) = MAX(CHEM(:,:,:,P_XO2),1.0E-30)
      CGRID(:,:,:,XO2N) = MAX(CHEM(:,:,:,P_XO2N),1.0E-30)
      CGRID(:,:,:,MEO2) = MAX(CHEM(:,:,:,P_MEO2),1.0E-30)
      CGRID(:,:,:,HCO3) = MAX(CHEM(:,:,:,P_HCO3),1.0E-30)
      CGRID(:,:,:,C2O3) = MAX(CHEM(:,:,:,P_C2O3),1.0E-30)
      CGRID(:,:,:,CXO3) = MAX(CHEM(:,:,:,P_CXO3),1.0E-30)
      CGRID(:,:,:,ROR) = MAX(CHEM(:,:,:,P_ROR),1.0E-30)
      CGRID(:,:,:,TO2) = MAX(CHEM(:,:,:,P_TO2),1.0E-30)
      CGRID(:,:,:,CRO) = MAX(CHEM(:,:,:,P_CRO),1.0E-30)
      CGRID(:,:,:,NO2E) = MAX(CHEM(:,:,:,P_NO2E),1.0E-30)
!MONARCH


ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
C  Set date and time to center of time step, get necessary physical
C  data, and get photolysis rates
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

c      MIDDATE = 2009168 !JDATE
c      MIDTIME = 120000   !JTIME
c      ITMSTEP = 60. !180.+45. !600. !TIME2SEC( TSTEP( 2 ) )
      CHEMSTEP = FLOAT( ITMSTEP ) / 60.0D+00
c      CALL NEXTIME( MIDDATE, MIDTIME, SEC2TIME( ITMSTEP / 2 ) )

c      MIDTIME  = 120345

C.. Compute number of time step loops and step size for EBI solver
      DELT_SEC = DELTAT * 60.0 + 0.1
      IF( DELT_SEC .GE. ITMSTEP ) THEN
         N_EBI_STEPS = 1
         EBI_TMSTEP = FLOAT( ITMSTEP ) / 60.0
      ELSE
         IF( MOD( ITMSTEP, DELT_SEC ) .EQ. 0 ) THEN
            N_EBI_STEPS = ITMSTEP / DELT_SEC
         ELSE
            N_EBI_STEPS = ITMSTEP / DELT_SEC + 1
         ENDIF
         EBI_TMSTEP =  FLOAT( ITMSTEP ) / FLOAT( N_EBI_STEPS ) / 60.0
      ENDIF

      N_INR_STEPS = 1


C.. Get ambient temperature in K

c      VNAME = 'TA'
c      IF ( .NOT. INTERPX( MET_CRO_3D, VNAME, PNAME,
c     &                    STRTCOLMC3,ENDCOLMC3, STRTROWMC3,ENDROWMC3, 1,NLAYS,
c     &                    MIDDATE, MIDTIME, TA ) ) THEN
c         MSG = 'Could not read TA from MET_CRO_3D'
c         CALL M3EXIT( PNAME, JDATE, JTIME, MSG, XSTAT1 )
c      ENDIF

!       TA = 298.

C.. Get specific humidity in Kg H2O / Kg air
c      VNAME = 'QV'
c      IF ( .NOT. INTERPX( MET_CRO_3D, VNAME, PNAME,
c     &                    STRTCOLMC3,ENDCOLMC3, STRTROWMC3,ENDROWMC3, 1,NLAYS,
c     &                    MIDDATE, MIDTIME, QV ) ) THEN
c         MSG = 'Could not read QV from MET_CRO_3D'
c         CALL M3EXIT( PNAME, JDATE, JTIME, MSG, XSTAT1 )
c      ENDIF

!       QV = 0. !0064

C.. Get pressure in Pascals
c      VNAME = 'PRES'
c      IF ( .NOT. INTERPX( MET_CRO_3D, VNAME, PNAME,
c     &                    STRTCOLMC3,ENDCOLMC3, STRTROWMC3,ENDROWMC3, 1,NLAYS,
c     &                    MIDDATE, MIDTIME, PRES ) ) THEN
c         MSG = 'Could not read PRES from MET_CRO_3D'
c         CALL M3EXIT ( PNAME, JDATE, JTIME, MSG, XSTAT1 )
c      ENDIF

!        PRES = 101300.

C.. Get photolysis rates in /min
c      CALL PHOT ( MIDDATE, MIDTIME, JDATE, JTIME, NDARK, RJ )

!       RJ = 0.

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c  Top of loop over cells
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

c#ifdef hrstats
c      MNEBI = 1000
c      MXEBI = 0
c      SUMEBI = 0.0
c#endif
      DO L = 1,JPNL(ITS,JTS) !MONARCH 1, NLAYS
      DO R = JTS,JTE !MONARCH 1, MY_NROWS
         DO C = ITS,ITE !MONARCH 1, MY_NCOLS
            KFLIP=LM+1-L

c..Load ICs
!MONARCH               DO S = 1, 62 !72 !N_GC_SPC
               DO S = 1, 63 !72 !N_GC_SPC
!MONARCH
                  YC( S ) = MAX( CGRID( C, R, KFLIP, S ), 1.0E-30 ) !Modified 20100125 MAX( CGRID( C, R, L, S ), 1.0E-30 )
               ENDDO

c..Set physical quantities
               TEMP = TEMPERATURE(C,R,KFLIP) !TA( C, R, L )
               ATMPRES = p8w(C,L,R) * PA2ATM !* PRES( C, R, L ) !P8W should be in Pa
               H2O  = MAX(WATER(C,R,kflip,P_QV) * MAOMV * 1.0e+06,0.0) !MAX ( QV( C, R, L ) * MAOMV *  1.0E+06, 0.0 )

c..Get rate constants
               LSUNLIGHT = .FALSE.
!MONARCH
        RJIN(:) = 0. !First of all we initialize to zero all photolysis rates  

!1    1,
!2    8,
!3    9,
!4   14,
!5   15,
!6   25,
!7   36,
!8   51,
!9   52,
!10   53,
!11   62,
!12   64,
!13   71,
!14   74,
!15   75,
!16   86,
!17   90,
!18   96,
!19  101,
!20  105,
!21  135,
!22  140,
!23  148,

        RJIN(1)    = ph_no2(C,L,R)
        RJIN(2)    = ph_o33p(C,L,R)
        RJIN(3)    = ph_o31d(C,L,R)
        RJIN(4)    = ph_no3o(C,L,R) !corrected 20100125 ph_no3o2(C,L,R)
        RJIN(5)    = ph_no3o2(C,L,R)!corrected 20100125 ph_no3o(C,L,R)
        RJIN(6)    = ph_hno2(C,L,R)
        RJIN(7)    = ph_h2o2(C,L,R)
        RJIN(8)    = ph_hno4(C,L,R) !HNO4=HO2NO2
        RJIN(9)    = ph_hno3(C,L,R)
        RJIN(10)   = ph_n2o5(C,L,R)
        RJIN(11)   = ph_ntr(C,L,R) !!!!!
        RJIN(12)   = ph_mepx(C,L,R) !!!!!
        RJIN(13)   = ph_ch2or(C,L,R)
        RJIN(14)   = ph_ch2om(C,L,R)
        RJIN(15)   = ph_ald2(C,L,R) !!!CCHO_R_SAPRC99  ????
        RJIN(16)   = ph_pan(C,L,R) !!!!!
        RJIN(17)   = ph_pacd(C,L,R) !!!!!
        RJIN(18)   = ph_aldx(C,L,R) !!!!!
        RJIN(19)   = ph_mgly(C,L,R) !!!!!
        RJIN(20)   = ph_ispd(C,L,R) !!!!!
        RJIN(24)   = ph_no2e(C,L,R)
!MONARCH



!MONARCH       DO NPH=1,19
       DO NPH=1,24
        IF (RJIN(NPH) .GT. 0.0) LSUNLIGHT = .TRUE.
       ENDDO
!MONARCH

               CALL HRCALCKS( NPHOTAB, LSUNLIGHT, RJIN, TEMP,
     &                        ATMPRES, H2O, RKI )

c..Call EBI solver
               N_EBI_IT = 0

               CALL HRSOLVER( JDATE, JTIME, C, R, L )

c#ifdef hrstats
c               MXEBI  = MAX( MXEBI, N_EBI_IT )
c               MNEBI  = MIN( MNEBI, N_EBI_IT )
c               SUMEBI = SUMEBI + FLOAT( N_EBI_IT )
c#endif


c..Update concentration array
!MONARCH               DO S = 1, 62 !72 !N_GC_SPC
               DO S = 1, 63
                  CGRID( C, R, KFLIP, S ) = YC( S ) !Modified 20100125 CGRID( C, R, L, S ) = YC( S )
               ENDDO

            ENDDO
         ENDDO
      ENDDO

!MONARCH
                CHEM(:,:,:,P_NO2)     = CGRID(:,:,:,NO2)    
                CHEM(:,:,:,P_NO)      = CGRID(:,:,:,NO)     
                CHEM(:,:,:,P_O3)      = CGRID(:,:,:,O3)     
                CHEM(:,:,:,P_NO3)     = CGRID(:,:,:,NO3)    
                CHEM(:,:,:,P_N2O5)    = CGRID(:,:,:,N2O5)   
                CHEM(:,:,:,P_HNO3)    = CGRID(:,:,:,HNO3)   
                CHEM(:,:,:,P_HONO)    = CGRID(:,:,:,HONO)   
                CHEM(:,:,:,P_PNA)     = CGRID(:,:,:,PNA)    
                CHEM(:,:,:,P_H2O2)    = CGRID(:,:,:,H2O2)   
                CHEM(:,:,:,P_NTR)     = CGRID(:,:,:,NTR)    
                CHEM(:,:,:,P_ROOH)    = CGRID(:,:,:,ROOH)   
                CHEM(:,:,:,P_FORM)    = CGRID(:,:,:,FORM)   
                CHEM(:,:,:,P_ALD2)    = CGRID(:,:,:,ALD2)   
                CHEM(:,:,:,P_ALDX)    = CGRID(:,:,:,ALDX)   
                CHEM(:,:,:,P_PAR)     = CGRID(:,:,:,PAR)    
                CHEM(:,:,:,P_CO)      = CGRID(:,:,:,CO)     
                CHEM(:,:,:,P_MEPX)    = CGRID(:,:,:,MEPX)   
                CHEM(:,:,:,P_MEOH)    = CGRID(:,:,:,MEOH)   
                CHEM(:,:,:,P_FACD)    = CGRID(:,:,:,FACD)   
                CHEM(:,:,:,P_PAN)     = CGRID(:,:,:,PAN)    
                CHEM(:,:,:,P_PACD)    = CGRID(:,:,:,PACD)   
                CHEM(:,:,:,P_AACD)    = CGRID(:,:,:,AACD)   
                CHEM(:,:,:,P_PANX)    = CGRID(:,:,:,PANX)   
                CHEM(:,:,:,P_OLE)     = CGRID(:,:,:,OLE)    
                CHEM(:,:,:,P_ETH)     = CGRID(:,:,:,ETH)    
                CHEM(:,:,:,P_IOLE)    = CGRID(:,:,:,IOLE)   
                CHEM(:,:,:,P_TOL)     = CGRID(:,:,:,TOL)    
                CHEM(:,:,:,P_CRES)    = CGRID(:,:,:,CRES)   
                CHEM(:,:,:,P_OPEN)    = CGRID(:,:,:,OPEN)   
                CHEM(:,:,:,P_MGLY)    = CGRID(:,:,:,MGLY)   
                CHEM(:,:,:,P_XYL)     = CGRID(:,:,:,XYL)    
                CHEM(:,:,:,P_ISOP)    = CGRID(:,:,:,ISOP)   
                CHEM(:,:,:,P_ISPD)    = CGRID(:,:,:,ISPD)   
                CHEM(:,:,:,P_TERP)    = CGRID(:,:,:,TERP)   
                CHEM(:,:,:,P_SO2)     = CGRID(:,:,:,SO2)    
                CHEM(:,:,:,P_SULF)    = CGRID(:,:,:,SULF)   
                CHEM(:,:,:,P_ETOH)    = CGRID(:,:,:,ETOH)   
                CHEM(:,:,:,P_ETHA)    = CGRID(:,:,:,ETHA)   
                CHEM(:,:,:,P_O)       = CGRID(:,:,:,O)      
                CHEM(:,:,:,P_O1D)     = CGRID(:,:,:,O1D)    
                CHEM(:,:,:,P_OH)      = CGRID(:,:,:,OH)     
                CHEM(:,:,:,P_HO2)     = CGRID(:,:,:,HO2)    
                CHEM(:,:,:,P_XO2)     = CGRID(:,:,:,XO2)    
                CHEM(:,:,:,P_XO2N)    = CGRID(:,:,:,XO2N)   
                CHEM(:,:,:,P_MEO2)    = CGRID(:,:,:,MEO2)   
                CHEM(:,:,:,P_HCO3)    = CGRID(:,:,:,HCO3)   
                CHEM(:,:,:,P_C2O3)    = CGRID(:,:,:,C2O3)   
                CHEM(:,:,:,P_CXO3)    = CGRID(:,:,:,CXO3)   
                CHEM(:,:,:,P_ROR)     = CGRID(:,:,:,ROR)    
                CHEM(:,:,:,P_TO2)     = CGRID(:,:,:,TO2)    
                CHEM(:,:,:,P_CRO)     = CGRID(:,:,:,CRO)    
                CHEM(:,:,:,P_NO2E)    = CGRID(:,:,:,NO2E)
!MONARCH

c#ifdef hrstats
c      AVGEBI = SUMEBI / FLOAT( NCOLS * NROWS * NLAYS )
c      WRITE( ISTFL, 94040 ) JDATE, JTIME, MNEBI, AVGEBI, MXEBI
c#endif

c      RETURN

C*********************** FORMAT STATEMENTS ****************************

92000 FORMAT( / 10X, 'Euler Backward Iterative Parameters -'
     &       / 10X, 'Chemistry Integration Time Interval (min):', F12.4,
     &      / 10X, 'EBI maximum time step (min):              ', F12.4 )

92020 FORMAT( //10X, 'Species convergence tolerances:' )

92040 FORMAT(   10X, A16, 2X, 1PE12.2 )

92060 FORMAT( / 10X, 'Emissions Processing in Chemistry ...'
     &        / 10X, 'Number of Emissions Layers:         ', I3
     &        / 10X, 'out of total Number of Model Layers:', I3 )


94020 FORMAT( 'DATE      TIME ', 'MNEBI AVEBI MXEBI' )

94040 FORMAT( I7, 1X, I6, 1X, 3( I5, 1X ) )
      END SUBROUTINE RUN_EBI


       END MODULE MODULE_EBI_SOLVER
