      SUBROUTINE     CTEM( FCANCMX,    FSNOW,     SAND,      CLAY,  
     2                          IC,      ILG,      IL1,       IL2,
     3                          IG,      ICC,     IDAY,      RADJ, 
     4                       TCANO,    TCANS,    TBARC,    TBARCS,    
     5                       TBARG,   TBARGS,       TA,     DELZW,
     6                     ANCSVEG,  ANCGVEG, RMLCSVEG,  RMLCGVEG,    
     7                       ZBOTW,   THLIQC,   THLIQG,    DELTAT,
     8                        VMOD,  LIGHTNG,  
     9                        TBAR,    L2MAX,
     A                    NOL2PFTS, PFCANCMX, NFCANCMX,  LNDUSEON,
     B                      THICEC, SOILDPTH, SPINFAST,   TODFRAC,
     C                     WETFRAC,WETFRAC_S,   POPDIN,    DOFIRE,
     D                     ISAND,       AREA,
C
C    -------------- INPUTS USED BY CTEM ARE ABOVE THIS LINE ---------
C
     C                    STEMMASS, ROOTMASS, LITRMASS,  GLEAFMAS,
     D                    BLEAFMAS, SOILCMAS,    AILCG,      AILC,
     E                       ZOLNC, RMATCTEM,    RMATC,     AILCB,
     F                    FLHRLOSS,  PANDAYS, LFSTATUS,  GRWTHEFF,
     G                    LYSTMMAS, LYROTMAS, TYMAXLAI,  VGBIOMAS,
     H                    GAVGLTMS, GAVGSCMS, STMHRLOS,      SLAI, 
     I                     BMASVEG, CMASVEGC, COLDDAYS,  ROTHRLOS,
     J                      FCANMX,   ALVISC,   ALNIRC,   GAVGLAI,
     K                    GAVGLFMS, GAVGRTMS, GAVGSTMS,
C
C    -------------- INPUTS UPDATED BY CTEM ARE ABOVE THIS LINE ------
C
     L                        NPP,       NEP, HETRORES,   AUTORES,
     M                   SOILRESP,        RM,       RG,       NBP,
     N                     LITRES,    SOCRES,      GPP, DSTCEMLS1,
     O                   LITRFALL,  HUMIFTRS,  VEGHGHT,  ROOTDPTH,
     P                        RML,       RMS,      RMR,  TLTRLEAF,
     Q                   TLTRSTEM,  TLTRROOT, LEAFLITR,  ROOTTEMP,
     R                   BURNFRAC, PROBFIRE,  LUCEMCOM,  LUCLTRIN, 
     S                   LUCSOCIN, DSTCEMLS3, GAVGNPLF,  GAVGNPST,
     T                   GAVGNPRT,   CH4WET1,  CH4WET2,   WETFDYN,
     U                    CH4DYN1,   CH4DYN2,  PAICGAT,  SLAICGAT)
C
C    ---------------- OUTPUTS ARE LISTED ABOVE THIS LINE ------------
C
C
C             CANADIAN TERRESTRIAL ECOSYSTEM MODEL (CTEM) - V1.1 incomplete
C
C
C     15  JUL 2016  - REMOVED UNUSED "CURLATNO".
C     12  MAR 2015  - REMOVE ALL CODE RELATED TO CALCULATION OF GRCLAREA
C     M. LAZARE       AND PASS IN "AREA" ARRAY WHICH HAS ALREADY BEEN
C                     CALCULATED IN THE AGCM PHYSICS WRAPPER.
C                   - "STDALN" OPTION REMOVED.
C     31  OCT 2015  - INPUT {UWIND,VWIND} REPLACED BY VMOD IN CONJUNCTION
C     M. LAZARE       WITH OTHER CHANGES TO USE PROPER WIND MAGNITUDE
C                     AND NOT STRESSES. SEE ALSO ROUTINE "DISTURB".
C     19  SEP. 2001 - THIS IS THE MAIN TERRESTRIAL CARBON MODEL SUBROUTINE
C     V. ARORA        
C                     ALL PRIMARY CTEM SUBROUTINES ARE CALLED FROM HERE,
C                     EXCEPT PHTSYN WHICH IS CALLED FROM TSOLVC
C    
C    -----------------------------------------------------------------
C
C     INPUTS
C
C     FCANCMX  - MAX. FRACTIONAL COVERAGE OF CTEM's 9 PFTs, BUT THIS CAN BE
C                MODIFIED BY LAND-USE CHANGE, AND COMPETITION BETWEEN PFTs
C     FSNOW    - FRACTION OF SNOW SIMULATED BY CLASS
C     SAND     - PERCENTAGE SAND
C     CLAY     - PERCENTAGE CLAY
C     AREA     - FRACTION AREA OF LAND (FOR ROUTINE DISTURB, IE FOR FIRE)
C     ICC      - NO OF PFTs FOR USE BY CTEM, CURRENTLY 9
C     IC       - NO OF PFTs FOR USE BY CLASS, CURRENTLY 4
C     IG       - NO. OF SOIL LAYERS, 3
C     ILG      - NO. OF GRID CELLS IN LATITUDE CIRCLE
C     IL1,IL2  - IL1=1, IL2=ILG
C     IDAY     - DAY OF YEAR
C     RADJ     - LATITUDE IN RADIANS
C     TCANO    - CANOPY TEMPERATURE FOR CANOPY OVER GROUND SUBAREA, K
C     TCANS    - CANOPY TEMPERATURE FOR CANOPY OVER SNOW SUBAREA
C     TBARC    - SOIL TEMPERATURE FOR CANOPY OVER GROUND SUBAREA, K
C     TBARCS   - SOIL TEMPERATURE FOR CANOPY OVER SNOW SUBAREA
C     TBARG    - SOIL TEMPERATURE FOR GROUND SUBAREA
C     TBARGS   - SOIL TEMPERATURE FOR SNOW OVER GROUND SUBAREA
C     TA       - AIR TEMPERATURE, K
C     ANCSVEG  - NET PHOTOSYNTHETIC RATE FOR CTEMs 9 PFTs FOR
C                CANOPY OVER SNOW SUBAREA
C     ANCGVEG  - NET PHOTOSYNTHETIC RATE FOR CTEMs 9 PFTs FOR
C                CANOPY OVER GROUND SUBAREA
C     RMLCSVEG - LEAF RESPIRATION RATE FOR CTEMs 9 PFTs FOR
C                CANOPY OVER SNOW SUBAREA
C     RMLCGVEG - LEAF RESPIRATION RATE FOR CTEMs 9 PFTs FOR
C                CANOPY OVER GROUND SUBAREA
C     DELZW    - THICKNESSES OF THE 3 SOIL LAYERS
C     ZBOTW    - BOTTOM OF SOIL LAYERS
C     THLIQC   - LIQUID MOIS. CONTENT OF 3 SOIL LAYERS, FOR CANOPY
C                OVER SNOW AND CANOPY OVER GROUND SUBAREAS
C     THLIQG   - LIQUID MOIS. CONTENT OF 3 SOIL LAYERS, FOR GROUND
C                AND SNOW OVER GROUND SUBAREAS
C     DELTAT   - CTEM TIME STEP IN DAYS
C     VMOD     - WIND SPEED, M/S
C     LIGHTNG  - TOTAL LIGHTNING FREQUENCY, FLASHES/KM2.YEAR
C     PRBFRHUC - PROBABILITY OF FIRE DUE TO HUMAN CAUSES
C     EXTNPROB - FIRE EXTINGUSINGING PROBABILITY
C     TBAR     - SOIL TEMPERATURE, K
C     L2MAX    - MAX. NUMBER OF LEVEL 2 CTEM PFTs
C     NOL2PFTS - NUMBER OF LEVEL 2 CTEM PFTs
C     PFCANCMX - PREVIOUS YEAR's FRACTIONAL COVERAGES OF PFTs
C     NFCANCMX - NEXT YEAR's FRACTIONAL COVERAGES OF PFTs
C     LNDUSEON - INTEGER SWITCH TO RUN THE LAND USE CHANGE SUBROUTINE
C                OR NOT. =1 MEANS YES.
C     THICEC   - FROZEN MOIS. CONTENT OF 3 SOIL LAYERS, FOR CANOPY
C                OVER SNOW AND CANOPY OVER GROUND SUBAREAS
C     SOILDPTH - SOIL DEPTH (M)
C     SPINFAST - SPINUP FACTOR FOR SOIL CARBON WHOSE DEFAULT VALUE IS
C                1. AS THIS FACTOR INCREASES THE SOIL C POOL WILL COME
C                INTO EQUILIBRIUM FASTER. REASONABLE VALUE FOR SPINFAST
C                IS BETWEEN 5 AND 10. WHEN SPINFAST.NE.1 THEN THE 
C                BALCAR SUBROUTINE IS NOT RUN.
C     TODFRAC  - MAX. FRACTIONAL COVERAGE OF CTEM's 9 PFTs BY THE END
C                OF THE DAY, FOR USE BY LAND USE SUBROUTINE
C     WETFRAC  - SPECIFIED CONSTANT WETLAND FRACTION FOR NOW READ
C                FROM A FILE
C   WETFRAC_S  - SLOPE BASED WETLAND FRACTION FOR ESTIMATING DYNAMIC
C                WETLAND FRACTION
C     POPDIN   - POPULATION DENSITY (PEOPLE / KM^2)
C
C     UPDATES
C
C     STEMMASS - STEM MASS FOR EACH OF THE 9 CTEM PFTs, Kg C/M2
C     ROOTMASS - ROOT MASS FOR EACH OF THE 9 CTEM PFTs, Kg C/M2
C     GLEAFMAS - GREEN LEAF MASS FOR EACH OF THE 9 CTEM PFTs, Kg C/M2
C     BLEAFMAS - BROWN LEAF MASS FOR EACH OF THE 9 CTEM PFTs, Kg C/M2
C     LITRMASS - LITTER MASS FOR EACH OF THE 9 CTEM PFTs + BARE, Kg C/M2
C     SOILCMAS - SOIL CARBON MASS FOR EACH OF THE 9 CTEM PFTs 
C                 + BARE, Kg C/M2
C     AILCG    - GREEN LAI FOR CTEM's 9 PFTs
C     AILC     - LUMPED LAI FOR CLASS' 4 PFTs
C     ZOLNC    - LUMPED LOG OF ROUGHNESS LENGTH FOR CLASS' 4 PFTs
C     RMATCTEM - FRACTION OF ROOTS FOR EACH OF CTEM's 9 PFTs IN EACH
C                SOIL LAYER
C     RMATC    - FRACTION OF ROOTS FOR EACH OF CLASS' 4 PFTs IN EACH
C                SOIL LAYER
C     AILCB    - BROWN LAI FOR CTEM's 9 PFTs. FOR NOW WE ASSUME ONLY
C                GRASSES CAN HAVE BROWN LAI
C     FLHRLOSS - FALL OR HARVEST LOSS FOR DECIDUOUS TREES AND CROPS,
C                RESPECTIVELY, Kg C/M2
C     PANDAYS  - DAYS WITH POSITIVE NET PHOTOSYNTHESIS (An) FOR USE IN
C                THE PHENOLOGY SUBROUTINE
C     LFSTATUS - LEAF PHENOLOGY STATUS
C     GRWTHEFF - GROWTH EFFICIENCY. CHANGE IN BIOMASS PER YEAR PER
C                UNIT MAX. LAI (Kg C/M2)/(M2/M2), FOR USE IN MORTALITY
C                SUBROUTINE
C     LYSTMMAS - STEM MASS AT THE END OF LAST YEAR
C     LYROTMAS - ROOT MASS AT THE END OF LAST YEAR
C     TYMAXLAI - THIS YEAR's MAXIMUM LAI
C     VGBIOMAS - GRID AVERAGED VEGETATION BIOMASS, Kg C/M2
C     GAVGLTMS - GRID AVERAGED LITTER MASS, Kg C/M2
C     GAVGSCMS - GRID AVERAGED SOIL C MASS, Kg C/M2
C     STMHRLOS - STEM HARVEST LOSS FOR CROPS, Kg C/M2
C     SLAI     - STORAGE/IMAGINARY LAI FOR PHENOLOGY PURPOSES
C     BMASVEG  - TOTAL (GLEAF + STEM + ROOT) BIOMASS FOR EACH CTEM PFT, Kg C/M2
C     CMASVEGC - TOTAL CANOPY MASS FOR EACH OF THE 4 CLASS PFTs. RECALL THAT 
C                CLASS REQUIRES CANOPY MASS AS AN INPUT, AND THIS IS NOW 
C                PROVIDED BY CTEM. KG/M2.
C     COLDDAYS - COLD DAYS COUNTER FOR TRACKING DAYS BELOW A CERTAIN
C                TEMPERATURE THRESHOLD FOR NDL DCD AND CROP PFTs.
C     ROTHRLOS - ROOT DEATH AS CROPS ARE HARVESTED, Kg C/M2
C     FCANMX   - FRACTIONAL COVERAGE OF CLASS' 4 PFTs
C     ALVISC   - VISIBLE ALBEDO FOR CLASS' 4 PFTs
C     ALNIRC   - NEAR IR ALBEDO FOR CLASS' 4 PFTs
C     GAVGLAI  - GRID AVERAGED GREEN LEAF AREA INDEX
C     GAVGLFMS - GRID AVERAGED GREEN LEAF MASS, Kg C/M2
C     GAVGSTMS - GRID AVERAGED STEM MASS, Kg C/M2
C     GAVGRTMS - GRID AVERAGED ROOT MASS, Kg C/M2
C
C
C     OUTPUTS
C                GRID-AVERAGED FLUXES IN u-MOL CO2/M2.SEC
C
C     NPP      - NET PRIMARY PRODUCTIVITY
C     NEP      - NET ECOSYSTEM PRODUCTIVITY
C     HETRORES - HETEROTROPHIC RESPIRATION
C     AUTORES  - AUTOTROPHIC RESPIRATION
C     SOILRESP - SOIL RESPIRATION. THIS INCLUDES ROOT RESPIRATION
C                AND RESPIRATION FROM LITTER AND SOIL CARBON POOLS.
C                NOTE THAT SOILRESP IS DIFFERENT FROM SOCRES, WHICH IS
C                RESPIRATION FROM THE SOIL C POOL.
C     RM       - MAINTENANCE RESPIRATION
C     RG       - GROWTH RESPIRATION
C     NBP      - NET BIOME PRODUCTIVITY
C     GPP      - GROSS PRIMARY PRODUCTIVITY
C     LITRES   - LITTER RESPIRATION
C     SOCRES   - SOIL CARBON RESPIRATION
C     DSTCEMLS1- CARBON EMISSION LOSSES DUE TO DISTURBANCE (FIRE AT PRESENT)
C                FROM VEGETATION  
C     LITRFALL - TOTAL LITTER FALL (FROM LEAVES, STEM, AND ROOT) DUE
C                TO ALL CAUSES (MORTALITY, TURNOVER, AND DISTURBANCE)
C     HUMIFTRS - TRANSFER OF HUMIDIFIED LITTER FROM LITTER TO SOIL C
C                POOL
C     LUCEMCOM - LAND USE CHANGE (LUC) RELATED COMBUSTION EMISSION LOSSES,
C                u-MOL CO2/M2.SEC 
C     LUCLTRIN - LUC RELATED INPUTS TO LITTER POOL, u-MOL CO2/M2.SEC
C     LUCSOCIN - LUC RELATED INPUTS TO SOIL C POOL, u-MOL CO2/M2.SEC
C     DSTCEMLS3- CARBON EMISSION LOSSES DUE TO DISTURBANCE (FIRE AT PRESENT)
C                FROM LITTER POOL
C     GAVGNPLF - GRID AVERAGED NPP OF LEAVES, u-MOL CO2/M2.SEC
C     GAVGNPST - GRID AVERAGED NPP OF STEM, u-MOL CO2/M2.SEC
C     GAVGNPRT - GRID AVERAGED NPP OF ROOTS, u-MOL CO2/M2.SEC
C     CH4WET1  - WETLAND CH4 EMISSIONS u-MOL CH4-C/M2.SEC (HETRORES)
C     CH4WET2  - WETLAND CH4 EMISSIONS u-MOL CH4-C/M2.SEC (NPP)
C     WETFDYN  - DYNAMIC GRIDCELL WETLAND FRACTION DETERMINED USING
C                SLOPE AND SOIL MOISTURE 
C     CH4DYN1  - WETLAND CH4 EMISSIONS u-MOL CH4-C/M2.SEC (HETRORES & WETFDYN)
C     CH4DYN2  - WETLAND CH4 EMISSIONS u-MOL CH4-C/M2.SEC (NPP & WETFDYN)

C                OTHER QUANTITIES
C
C     VEGHGHT  - VEGETATION HEIGHT (METERS)
C     ROOTDPTH - 99% SOIL ROOTING DEPTH (METERS)
C                BOTH VEGHGHT & ROOTDPTH CAN BE USED AS DIAGNOSTICS TO SEE
C                HOW VEGETATION GROWS ABOVE AND BELOW GROUND, RESPECTIVELY
C     RML      - LEAF MAINTENANCE RESPIRATION (u-MOL CO2/M2.SEC)
C     RMS      - STEM MAINTENANCE RESPIRATION (u-MOL CO2/M2.SEC)
C     RMR      - ROOT MAINTENANCE RESPIRATION (u-MOL CO2/M2.SEC)
C     TLTRLEAF - TOTAL LEAF LITTER FALL RATE (u-MOL CO2/M2.SEC)
C     TLTRSTEM - TOTAL STEM LITTER FALL RATE (u-MOL CO2/M2.SEC)
C     TLTRROOT - TOTAL ROOT LITTER FALL RATE (u-MOL CO2/M2.SEC)
C     LEAFLITR - LEAF LITTER FALL RATE (u-MOL CO2/M2.SEC). THIS LEAF LITTER
C                DOES NOT INCLUDE LITTER GENERATED DUE TO MORTALITY/FIRE
C     ROOTTEMP - ROOT TEMPERATURE, K
C     AFRLEAF  - ALLOCATION FRACTION FOR LEAVES
C     AFRSTEM  - ALLOCATION FRACTION FOR STEM
C     AFRROOT  - ALLOCATION FRACTION FOR ROOT
C     WTSTATUS - SOIL WATER STATUS USED FOR CALCULATING ALLOCATION FRACTIONS
C     LTSTATUS - LIGHT STATUS USED FOR CALCULATING ALLOCATION FRACTIONS
C     BURNAREA - AREA BURNED DUE TO FIRE FOR EVERY GRID CELL, KM2
C     PROBFIRE - PROBABILITY OF FIRE FOR EVERY GRID CELL
C
      IMPLICIT NONE
C     
      INTEGER     KK

      PARAMETER(KK=12)  ! PRODUCT OF CLASS PFTs AND L2MAX (4 x 3 = 12)
C
      LOGICAL   DOFIRE, DO_MORTALITY

      INTEGER      IC,      ICC,      ILG,      IL1,       IL2,      IG, 
     1           IDAY,        I,        J,        K,
     2         ICHECK,   ICOUNT,        N,        M, SORT(ICC),   L2MAX,
     3   NOL2PFTS(IC),       K1,       K2, LNDUSEON,  SPINFAST
C
      INTEGER       PANDAYS(ILG,ICC), COLDDAYS(ILG,2),
     1             LFSTATUS(ILG,ICC), ISAND(ILG,IG)                 
C
      REAL FSNOW(ILG),  SAND(ILG,IG),  CLAY(ILG,IG),     THLIQC(ILG,IG),
     1     TCANO(ILG),    TCANS(ILG), TBARC(ILG,IG),   RMATC(ILG,IC,IG),
     2  ZBOTW(ILG,IG),      RML(ILG),      GPP(ILG),   FCANCMX(ILG,ICC),
     3 TBARCS(ILG,IG), TBARG(ILG,IG),TBARGS(ILG,IG),     THLIQG(ILG,IG),
     4      RADJ(ILG),       TA(ILG),        DELTAT,      DELZW(ILG,IG),
     5   TBAR(ILG,IG),THICEC(ILG,IG), SOILDPTH(ILG),   TODFRAC(ILG,ICC),
     6              YESFRAC(ILG,ICC)
C
      REAL  STEMMASS(ILG,ICC),   ROOTMASS(ILG,ICC), LITRMASS(ILG,ICC+1),
     1      GLEAFMAS(ILG,ICC),   BLEAFMAS(ILG,ICC), SOILCMAS(ILG,ICC+1),
     2       ANCSVEG(ILG,ICC),    ANCGVEG(ILG,ICC),   RMLCSVEG(ILG,ICC),
     3      RMLCGVEG(ILG,ICC),      AILCG(ILG,ICC),        AILC(ILG,IC),
     4   RMATCTEM(ILG,ICC,IG),       ZOLNC(ILG,IC),      AILCB(ILG,ICC),
     5          VGBIOMAS(ILG),       GAVGLTMS(ILG),       GAVGSCMS(ILG),
     6          SLAI(ILG,ICC),    BMASVEG(ILG,ICC),    CMASVEGC(ILG,IC),
     7       VEGHGHT(ILG,ICC),   ROOTDPTH(ILG,ICC),   GPPCSVEG(ILG,ICC),
     8      GPPCGVEG(ILG,ICC),   PFCANCMX(ILG,ICC),      FCANMX(ILG,IC),
     9      NFCANCMX(ILG,ICC),      ALVISC(ILG,IC),      ALNIRC(ILG,IC),
     A           GAVGLAI(ILG),       GAVGLFMS(ILG),       GAVGSTMS(ILG),
     B          GAVGRTMS(ILG)
C
      REAL   NPP(ILG),      NEP(ILG),  HETRORES(ILG),      AUTORES(ILG),
     1  SOILRESP(ILG),       RM(ILG),        RG(ILG),          NBP(ILG),
     2 DSTCEMLS1(ILG), LITRFALL(ILG),  HUMIFTRS(ILG),     GALTCELS(ILG),
     3 DSTCEMLS2(ILG), LUCEMCOM(ILG),  LUCLTRIN(ILG),     LUCSOCIN(ILG),
     4 DSTCEMLS3(ILG)
C
      REAL    FC(ILG),       FG(ILG),       FCS(ILG),         FGS(ILG),
     1  FCANS(ILG,IC),  FCAN(ILG,IC),           ZERO,         RMS(ILG),
     2       RMR(ILG),  GRESCOEF(KK),    LITRES(ILG),      SOCRES(ILG),
     3   HUMICFAC(KK),                                          KN(KK),
     4           TERM,       ETA(KK),      KAPPA(KK),     LFESPANY(KK),
     5       FRACBOFG,   SPECSLA(KK)
C
      REAL PGLFMASS(ILG,ICC),   PBLFMASS(ILG,ICC),   PSTEMASS(ILG,ICC),
     1     PROTMASS(ILG,ICC), PLITMASS(ILG,ICC+1), PSOCMASS(ILG,ICC+1),
     2         PVGBIOMS(ILG),       PGAVLTMS(ILG),       PGAVSCMS(ILG)
C
      REAL   FCANCS(ILG,ICC),      FCANC(ILG,ICC),   RMSCGVEG(ILG,ICC),
     1     RMSCSVEG(ILG,ICC),   RMRCGVEG(ILG,ICC),   RMRCSVEG(ILG,ICC),
     2       RMSVEG(ILG,ICC),     RMRVEG(ILG,ICC),      ANVEG(ILG,ICC),
     3       RMLVEG(ILG,ICC),     GPPVEG(ILG,ICC),     NPPVEG(ILG,ICC),
     4        RGVEG(ILG,ICC),      RMVEG(ILG,ICC),     NEPVEG(ILG,ICC),
     5     RTTEMPCS(ILG,ICC),   RTTEMPCG(ILG,ICC),     NBPVEG(ILG,ICC),
     6     PHEANVEG(ILG,ICC),   PANCSVEG(ILG,ICC),   PANCGVEG(ILG,ICC)
C
      REAL LTRSVGCS(ILG,ICC),   LTRSVGCG(ILG,ICC),   SCRSVGCS(ILG,ICC),
     1     SCRSVGCG(ILG,ICC), LTRESVEG(ILG,ICC+1), SCRESVEG(ILG,ICC+1),
     2          LTRSBRG(ILG),        SCRSBRG(ILG),       LTRSBRGS(ILG),
     3         SCRSBRGS(ILG), HETRSVEG(ILG,ICC+1), HUMTRSVG(ILG,ICC+1),
     4   SOILRSVG(ILG,ICC+1)             
C
      REAL LTRESTEP(ILG,ICC+1),SCRESTEP(ILG,ICC+1), HUTRSTEP(ILG,ICC+1) 
C
      REAL ROOTTEMP(ILG,ICC),     TBARCCS(ILG,IG),   LEAFLITR(ILG,ICC),
     1       FIELDSM(ILG,IG),   FLHRLOSS(ILG,ICC),      WILTSM(ILG,IG)
C
      REAL ROOTLITR(ILG,ICC),   STEMLITR(ILG,ICC),   STMHRLOS(ILG,ICC),
     1     ROTHRLOS(ILG,ICC)
C
      REAL  AFRLEAF(ILG,ICC),    AFRSTEM(ILG,ICC),    AFRROOT(ILG,ICC),
     1     WTSTATUS(ILG,ICC),   LTSTATUS(ILG,ICC)
C
      REAL NPPVGSTP(ILG,ICC),   RMLVGSTP(ILG,ICC),   RMSVGSTP(ILG,ICC),
     1     RMRVGSTP(ILG,ICC),   GPPVGSTP(ILG,ICC),   NTCHLVEG(ILG,ICC),
     2     NTCHSVEG(ILG,ICC),   NTCHRVEG(ILG,ICC)
C
      REAL  NPPLEAF(ILG,ICC),    NPPSTEM(ILG,ICC),    NPPROOT(ILG,ICC)
      REAL     GAVGNPLF(ILG),       GAVGNPST(ILG),       GAVGNPRT(ILG)
C
      REAL GRWTHEFF(ILG,ICC),   LYSTMMAS(ILG,ICC),   LYROTMAS(ILG,ICC), 
     1     TYMAXLAI(ILG,ICC),   STEMLTRM(ILG,ICC),   ROOTLTRM(ILG,ICC), 
     2     GLEALTRM(ILG,ICC),   GEREMORT(ILG,ICC),   INTRMORT(ILG,ICC)
C
      REAL      CURRLAT(ILG),           AREA(ILG),   GRCLAREA(ILG)
C
      REAL         VMOD(ILG),        LIGHTNG(ILG),
     1         PRBFRHUC(ILG),       EXTNPROB(ILG),   PFTAREAB(ILG,ICC),
     2     STEMLTDT(ILG,ICC),   ROOTLTDT(ILG,ICC),   GLFLTRDT(ILG,ICC),
     3     BLFLTRDT(ILG,ICC),   GLCAEMLS(ILG,ICC),   BLCAEMLS(ILG,ICC),
     4     RTCAEMLS(ILG,ICC),   STCAEMLS(ILG,ICC),   LTRCEMLS(ILG,ICC),
     5     PFTAREAA(ILG,ICC),       BURNFRAC(ILG),   DSCEMLV1(ILG,ICC),
     6     DSCEMLV2(ILG,ICC),       PROBFIRE(ILG),    BURNVEG(ILG,ICC)
C
      REAL     EMIT_CO2(ILG),        EMIT_CO(ILG),       EMIT_CH4(ILG),
     1        EMIT_NMHC(ILG),        EMIT_H2(ILG),       EMIT_NOX(ILG),
     2         EMIT_N2O(ILG),      EMIT_PM25(ILG),       EMIT_TPM(ILG),
     3          EMIT_TC(ILG),        EMIT_OC(ILG),        EMIT_BC(ILG)
C
      REAL TLTRLEAF(ILG,ICC),   TLTRSTEM(ILG,ICC),   TLTRROOT(ILG,ICC)
C
      REAL  PAICGAT(ILG,IC),  SLAICGAT(ILG,IC), POPDIN(ILG)  
C
      REAL      WETFRAC(ILG),        CH4WET1(ILG),        CH4WET2(ILG)
      REAL    WETFRAC_S(ILG),        WETFDYN(ILG) 
      REAL      CH4DYN1(ILG),        CH4DYN2(ILG)
C
      COMMON /CTEM1/ ETA, KAPPA, KN
      COMMON /CTEM2/ LFESPANY, FRACBOFG, SPECSLA
C     ---------------------------------------------------------------
C                     CONSTANTS AND PARAMETERS 
C
C     NOTE THE STRUCTURE OF VECTORS WHICH CLEARLY SHOWS THE CLASS
C     PFTs (ALONG ROWS) AND CTEM SUB-PFTs (ALONG COLUMNS)
C
C     NEEDLE LEAF |  EVG       DCD       ---
C     BROAD LEAF  |  EVG   DCD-CLD   DCD-DRY
C     CROPS       |   C3        C4       ---
C     GRASSES     |   C3        C4       ---
C
C     GROWTH RESPIRATION COEFFICIENT
      DATA GRESCOEF/0.15, 0.15, 0.00,
     &              0.15, 0.15, 0.15,
     &              0.15, 0.15, 0.00,
     &              0.15, 0.15, 0.00/
C
C     HUMIFICATION FACTOR - USED FOR TRANSFERRING CARBON FROM LITTER INTO 
C     SOIL C POOL
C     DATA HUMICFAC/0.42, 0.42, 0.00,
C    &              0.53, 0.48, 0.48,
C    &              0.10, 0.10, 0.00,
C    &              0.42, 0.42, 0.00/
C
C     VALUES FOR CMIP6 CANESM5-RC1. INCREASE HUMICFAC BY 0.04 TO INCREASE SOIL
C     CARBON POOL A LITTLE BIT
      DATA HUMICFAC/0.46, 0.46, 0.00,
     &              0.57, 0.52, 0.48,
C    &              0.42, 0.42, 0.00, THESE WERE THE ORIGINAL VALUES FOR CROPS
     &              0.10, 0.10, 0.00,
     &              0.45, 0.45, 0.00/



C     CANOPY LIGHT/NITROGEN EXTINCTION COEFFICIENT 
      DATA   KN/0.50, 0.50, 0.00,
     &          0.50, 0.50, 0.50,
     &          0.40, 0.48, 0.00,
     &          0.46, 0.44, 0.00/
C
C     ETA AND KAPPA, PARAMETERS FOR ESTIMATING MIN. STEM+ROOT BIOMASS
C     REQUIRED TO SUPPORT GREEN LEAF BIOMASS. KAPPA IS 1.6 FOR TREES
C     AND CROPS, AND 1.2 FOR GRASSES.
      DATA ETA/10.0, 30.8, 0.00,
     &         31.0, 50.0, 30.0,
     &          7.0,  7.0, 0.00,
     &          3.0,  3.0, 0.00/

      DATA KAPPA/1.6, 1.6, 0.0,
     &           1.6, 1.6, 1.6,
     &           1.6, 1.6, 0.0,
     &           1.2, 1.2, 0.0/
C
C     LEAF LIFE SPAN (IN YEARS) FOR CTEM's 9 PFTs
      DATA  LFESPANY/5.0, 1.00, 0.00,
     &              1.75, 1.00, 1.00,
     &              1.75, 1.75, 0.00,
     &              1.00, 1.00, 0.00/
C
C     CTEM CAN USE USER-SPECIFIED SPECIFIC LEAF AREAS (SLA) IF THE
C     FOLLOWING SPECIFIED VALUES ARE GREATER THAN ZERO
C
      DATA SPECSLA/11.0, 0.0, 0.0,
     &              0.0, 0.0, 0.0,
     &              0.0, 0.0, 0.0,
     &              0.0, 0.0, 0.0/
C
C     FRACBOFG, PARAMETER USED TO ESTIMATE LAI OF BROWN LEAVES. WE
C     ASSUME THAT SLA OF BROWN LEAVES IS THIS FRACTION OF SLA OF
C     GREEN LEAVES
      DATA FRACBOFG/0.55/
C
      REAL, PARAMETER :: PI=3.1415926535898
C
C     RADIUS OF EARTH, KM
      REAL, PARAMETER :: EARTHRAD=6371.22
C
C     ZERO
      DATA ZERO/1E-20/
C
C     -----------------------------------------------------------------
C
      IF(ICC.NE.9)                            CALL XIT('CTEM',-1)
      IF(IC.NE.4)                             CALL XIT('CTEM',-2)
      IF(DELTAT.NE.1.0)                       CALL XIT('CTEM',-3)
      IF(L2MAX.NE.3)                          CALL XIT('CTEM',-4)
C
C     FIND AREA OF THE GCM GRID CELLS. THIS IS NEEDED FOR LAND USE CHANGE
C     AND DISTURBANCE SUBROUTINES
C     NOTE THE CONVERSION TO KM^2 FROM M^2. THE FORMER IS USED FOR CTEM.
C
      DO 50 I = IL1, IL2
        CURRLAT(I)=RADJ(I)*180.0/PI                             
        GRCLAREA(I) = AREA(I)*1.E-6
50    CONTINUE
C
C     -----------------------------------------------------------------
C
C     IF LANDUSE IS ON, THEN IMPLELEMENT LUC, CHANGE FRACTIONAL COVERAGES, 
C     MOVE BIOMASSES AROUND, AND ESTIMATE LUC RELATED COMBUSTION EMISSION 
C     LOSSES.
C
      IF(LNDUSEON.EQ.1)THEN
         YESFRAC(:,:)=FCANCMX(:,:)
         CALL       LUC(     ICC,      ILG,      IL1,      IL2,
     1                        IC, NOL2PFTS,    L2MAX,  
     2                  GRCLAREA, PFCANCMX, NFCANCMX,     IDAY,
     3                   TODFRAC,  YESFRAC,   .FALSE.,
     4                  GLEAFMAS, BLEAFMAS, STEMMASS, ROOTMASS,
     5                  LITRMASS, SOILCMAS, VGBIOMAS, GAVGLTMS,
     6                  GAVGSCMS,  FCANCMX,   FCANMX,
     7                  LUCEMCOM, LUCLTRIN, LUCSOCIN)
      ENDIF
C
C     ---------------------------------------------------------------
C
C     FIND PFT AREAS BEFORE (THESE ARE REQUIRED FOR DISTURB SUBROUTINE)
C
      DO 82 J = 1, ICC
        DO  83 I = IL1, IL2
          PFTAREAB(I,J)=GRCLAREA(I)*FCANCMX(I,J)   ! AREA IN KM^2
83      CONTINUE
82    CONTINUE
C
C     GENERATE THE SORT INDEX FOR CORRESPONDENCE BETWEEN 9 PFTs AND THE
C     12 VALUES IN THE PARAMETER VECTORS
C
      ICOUNT=0
      DO 95 J = 1, IC
        DO 96 M = 1, NOL2PFTS(J)
          N = (J-1)*L2MAX + M
          ICOUNT = ICOUNT + 1
          SORT(ICOUNT)=N
96      CONTINUE
95    CONTINUE

C     INITIALIZE REQUIRED ARRAYS TO ZERO
C
      DO 100 I = IL1, IL2
        RMS(I) = 0.0         !GRID AVE. STEM MAINTENANCE RESPIRATION
        RMR(I) = 0.0         !GRID AVE. ROOT MAINTENANCE RESPIRATION
        RML(I) = 0.0         !GRID AVE. LEAF MAINTENANCE RESPIRATION
        RM(I) = 0.0          !GRID AVE. TOTAL MAINTENANCE RESPIRATION
        RG(I) = 0.0          !GRID AVE. GROWTH RESPIRATION
        NPP(I) = 0.0         !GRID AVE. NET PRIMARY PRODUCTIVITY
        GPP(I) = 0.0         !GRID AVE. GROSS PRIMARY PRODUCTIVITY
        NEP(I)=0.0           !GRID AVE. NET ECOSYSTEM PRODUCTIVITY
        NBP(I)=0.0           !GRID AVE. NET BIOME PRODUCTIVITY
C
        LITRES(I)=0.0        !GRID AVE. LITTER RESPIRATION
        SOCRES(I)=0.0        !GRID AVE. SOIL CARBON RESPIRATION
C
        HETRORES(I)=0.0      !GRID AVE. HETEROTROPHIC RESPIRATION
        AUTORES(I)=0.0       !GRID AVE. AUTOTROPHIC RESPIRATION
        SOILRESP(I)=0.0      !GRID AVE. SOIL RESPIRATION
        HUMIFTRS(I)=0.0      !GRID AVE. HUMIFICATION RATE
        DSTCEMLS1(I)=0.0     !GRID AVE. CARBON EMISSION LOSSES DUE TO DISTURBANCE, VEGETATION
        DSTCEMLS2(I)=0.0     !GRID AVE. CARBON EMISSION LOSSES DUE TO DISTURBANCE, TOTAL
        DSTCEMLS3(I)=0.0     !GRID AVE. CARBON EMISSION LOSSES DUE TO DISTURBANCE, LITTER
        GALTCELS(I)=0.0      !GRID AVE. LITTER FIRE EMISSION LOSSES (REDUNDANT, SAME AS DSTCEMLS3)
C
        FC(I)=0.0            !FRACTION OF CANOPY OVER GROUND SUBAREA 
        FCS(I)=0.0           !FRACTION OF CANOPY OVER SNOW SUBAREA
        FG(I)=0.0            !FRACTION OF BARE GROUND SUBAREA 
        FGS(I)=0.0           !FRACTION OF SNOW OVER GROUND SUBAREA
C
        TBARCCS(I,1)=0.0     !AVG. SOIL TEMPERATURE OVER CANOPY OVER SNOW
        TBARCCS(I,2)=0.0     !AND CANOPY OVER GROUND SUBAREAS.
        TBARCCS(I,3)=0.0     
C
C                              OVER BARE FRACTION OF THE GRID CELL
        SCRESTEP(I,ICC+1)=0.0  !SOIL C RESPIRATION IN Kg C/M2 OVER THE TIME STEP
        LTRESTEP(I,ICC+1)=0.0  !LITTER C RESPIRATION IN Kg C/M2 OVER THE TIME STEP
        SOILRSVG(I,ICC+1)=0.0  !SOIL RESPIRATION OVER THE BARE FRACTION
        HUMTRSVG(I,ICC+1)=0.0  !HUMIFIED RATE THE BARE FRACTION
C
        LTRESVEG(I,ICC+1)=0.0  !LITTER RESPIRATION RATE OVER BARE FRACTION
        SCRESVEG(I,ICC+1)=0.0  !SOIL C RESPIRATION RATE OVER BARE FRACTION
        HETRSVEG(I,ICC+1)=0.0  !HETEROTROPHIC RESP. RATE OVER BARE FRACTION
100   CONTINUE 
C
      DO 110 J = I,ICC
        DO 120 I = IL1, IL2
          FCANC(I,J) =0.0
          FCANCS(I,J)=0.0
C
          RMSVEG(I,J)=0.0    !STEM MAINTENANCE RESP. RATE FOR EACH PFT
          RMRVEG(I,J)=0.0    !ROOT MAINTENANCE RESP. RATE FOR EACH PFT
          RMLVEG(I,J)=0.0    !LEAF MAINTENANCE RESP. RATE FOR EACH PFT
           RMVEG(I,J)=0.0    !TOTAL MAINTENANCE RESP. RATE FOR EACH PFT
           RGVEG(I,J)=0.0    !GROWTH RESP. RATE FOR EACH PFT
           ANVEG(I,J)=0.0    !NET PHOTOSYNTHESIS RATE FOR EACH PFT
          PHEANVEG(I,J)=0.0  !NET PHOTOSYNTHESIS RATE, FOR PHENOLOGY PURPOSES
          PANCSVEG(I,J)=0.0  !NET PHOTOSYNTHESIS RATE, CANOPY OVER SNOW SUBAREA, FOR PHENOLOGY PURPOSES
          PANCGVEG(I,J)=0.0  !NET PHOTOSYNTHESIS RATE, CANOPY OVER GROUND SUBAREA, FOR PHENOLOGY PURPOSES
C
          GPPVEG(I,J)=0.0    !GROSS PRIMARY PRODUCTITY FOR EACH PFT
          NPPVEG(I,J)=0.0    !NET PRIMARY PRODUCTITY FOR EACH PFT
          NBPVEG(I,J)=0.0    !NET BIOME PRODUCTITY FOR EACH PFT
          NEPVEG(I,J)=0.0    !NET ECOSYSTEM PRODUCTITY FOR EACH PFT
C
          LTRESVEG(I,J)=0.0  !LITTER RESPIRATION RATE FOR EACH PFT
          SCRESVEG(I,J)=0.0  !SOIL C RESPIRATION RATE FOR EACH PFT
          HETRSVEG(I,J)=0.0  !HETEROTROPHIC RESP. RATE FOR EACH PFT
          SOILRSVG(I,J)=0.0  !SOIL RESPIRATION RATE FOR EACH PFT
          HUMTRSVG(I,J)=0.0  !HUMIFICATION RATE FOR EACH PFT
          SCRESTEP(I,J)=0.0  !SOIL C RESPIRATION IN Kg C/M2 OVER THE TIME STEP
          LTRESTEP(I,J)=0.0  !LITTER C RESPIRATION IN Kg C/M2 OVER THE TIME STEP
          HUTRSTEP(I,J)=0.0  !HUMIFICATION RATE IN Kg C/M2 OVER THE TIME STEP
C
          ROOTTEMP(I,J)=0.0  !ROOT TEMPERATURE
          NPPVGSTP(I,J)=0.0  !NPP (Kg C/M2) SEQUESTERED OVER THE MODEL TIME STEP
          GPPVGSTP(I,J)=0.0  !GPP (Kg C/M2) SEQUESTERED OVER THE MODEL TIME STEP
          RMLVGSTP(I,J)=0.0  !LEAF MAINTENANCE RESP. (Kg C/M2) RESPIRED OVER THE MODEL TIME STEP
          RMSVGSTP(I,J)=0.0  !STEM MAINTENANCE RESP. (Kg C/M2) RESPIRED OVER THE MODEL TIME STEP
          RMRVGSTP(I,J)=0.0  !ROOT MAINTENANCE RESP. (Kg C/M2) RESPIRED OVER THE MODEL TIME STEP
C 
          NTCHLVEG(I,J)=0.0  !NET CHANGE IN GLEAF BIOMASS AFTER AUTO. RESP. & ALLOCATION
          NTCHSVEG(I,J)=0.0  !NET CHANGE IN STEM BIOMASS AFTER AUTO. RESP. & ALLOCATION
          NTCHRVEG(I,J)=0.0  !NET CHANGE IN ROOT BIOMASS AFTER AUTO. RESP. & ALLOCATION
C
          DSCEMLV1(I,J)=0.0  !TOTAL CARBON EMISSION LOSSES (Kg C/M2), MAINLY DUE TO FIRE
          DSCEMLV2(I,J)=0.0  !TOTAL CARBON EMISSION LOSSES (Kg C/M2), MAINLY DUE TO FIRE
C
          TLTRLEAF(I,J)=0.0  !TOTAL LEAF LITTER
          TLTRSTEM(I,J)=0.0  !TOTAL STEM LITTER
          TLTRROOT(I,J)=0.0  !TOTAL ROOT LITTER
C
          NPPLEAF(I,J)= 0.0  ! NPP OF LEAVES
          NPPSTEM(I,J)= 0.0  ! NPP OF STEM
          NPPROOT(I,J)= 0.0  ! NPP OF ROOTS
120     CONTINUE
110   CONTINUE
C
C     STORE GREEN AND BROWN LEAF, STEM, AND ROOT BIOMASS, AND LITTER AND 
C     SOIL C POOL MASS IN ARRAYS. KNOWING INITIAL SIZES OF ALL POOLS AND
C     FINAL SIZES AT THE END OF THIS SUBROUTINE, WE CHECK FOR CONSERVATION
C     OF MASS.
C
      DO 130 J = 1, ICC
        DO 140 I = IL1, IL2
          PGLFMASS(I,J)=GLEAFMAS(I,J)    !GREEN LEAF MASS FROM LAST TIME STEP
          PBLFMASS(I,J)=BLEAFMAS(I,J)    !BROWN LEAF MASS FROM LAST TIME STEP
          PSTEMASS(I,J)=STEMMASS(I,J)    !STEM MASS FROM LAST TIME STEP
          PROTMASS(I,J)=ROOTMASS(I,J)    !ROOT MASS FROM LAST TIME STEP
          PLITMASS(I,J)=LITRMASS(I,J)    !LITTER MASS FROM LAST TIME STEP
          PSOCMASS(I,J)=SOILCMAS(I,J)    !SOIL C MASS FROM LAST TIME STEP
140     CONTINUE
130   CONTINUE
C
      DO 145 I = IL1, IL2
        PVGBIOMS(I)=VGBIOMAS(I)          !VEGETATION BIOMASS FROM LAST TIME STEP
        VGBIOMAS(I)= 0.0
        PGAVLTMS(I)=GAVGLTMS(I)          !LITTER MASS FROM LAST TIME STEP
        GAVGLTMS(I)=0.0
        PGAVSCMS(I)=GAVGSCMS(I)          !SOIL C MASS FROM LAST TIME STEP
        GAVGSCMS(I)=0.0
        LITRFALL(I)=0.0                  !COMBINED TOTAL LITTER FALL RATE
        GAVGLAI (I)=0.0                  !GRID AVERAGED GREEN LAI
        GAVGLFMS(I)=0.0                  !GRID AVERAGED GREEN LEAF MASS
        GAVGSTMS(I)=0.0                  !GRID AVERAGED STEM MASS
        GAVGRTMS(I)=0.0                  !GRID AVERAGED ROOT MASS
C
        PLITMASS(I,ICC+1)=LITRMASS(I,ICC+1)  !LITTER MASS OVER BARE FRACTION
        PSOCMASS(I,ICC+1)=SOILCMAS(I,ICC+1)  !SOIL C MASS OVER BARE FRACTION
C
        GAVGNPLF(I) = 0.0                !GRID AVERAGED NPP OF LEAVES
        GAVGNPST(I) = 0.0                !GRID AVERAGED NPP OF STEM 
        GAVGNPRT(I) = 0.0                !GRID AVERAGED NPP OF ROOTS 
145   CONTINUE
C
C     INITIALIZATION ENDS
C
C     FIND FC AND FCS BASED ON FCANCMX
C
      DO 150 J = 1, ICC
        DO 160 I = IL1, IL2
          FCANCS(I,J) = FCANCMX(I,J)*FSNOW(I)
          FCANC(I,J)  = FCANCMX(I,J)*(1.-FSNOW(I))
          FCS(I) = FCS(I) + FCANCS(I,J)
          FC(I)  = FC(I)  + FCANC(I,J)
160     CONTINUE 
150   CONTINUE 
C
      DO 170 I = IL1, IL2
        FGS(I)=(1.0-FCS(I)-FC(I))*FSNOW(I) 
        FG(I)=(1.0-FCS(I)-FC(I))*(1.0-FSNOW(I)) 
170   CONTINUE
C
C     ------------------------------------------------------------------
C
C     AUTOTROPHIC RESPIRATION PART STARTS
C
C     LEAF RESPIRATION IS CALCULATED IN PHTSYN SUBROUTINE, WHILE STEM
C     AND ROOT MAINTENANCE RESPIRATION ARE CALCULATED HERE.
C
C     WE TREAT CANOPY OVER GROUND AND CANOPY OVER SNOW SUBAREAS
C     SEPARATELY BECAUSE STEM TEMPERATURE (FOR WHICH WE USE CANOPY
C     TEMPERATURE AS A SURROGATE) CAN BE DIFFERENT FOR THESE TWO
C     SUBAREAS.
C
C     FIND MAINTENANCE RESPIRATION FOR CANOPY OVER SNOW SUB-AREA
C     in uMOL CO2/M2/SEC
C
      CALL   MAINRES (FCANCS,      FCS,     STEMMASS,   ROOTMASS,        
     1                   ICC,       IG,          ILG,        IL1,
     2                   IL2,       TA,       TBARCS,   RMATCTEM,
     3                  SORT, NOL2PFTS,           IC,      ISAND,
     4              RMSCSVEG, RMRCSVEG,     RTTEMPCS)
C
C     FIND MAINTENANCE RESPIRATION FOR CANOPY OVER GROUND SUB-AREA
C
      CALL   MAINRES ( FCANC,       FC,     STEMMASS,   ROOTMASS,        
     1                   ICC,       IG,          ILG,        IL1,
     2                   IL2,       TA,        TBARC,   RMATCTEM,
     3                  SORT, NOL2PFTS,           IC,      ISAND,
     4              RMSCGVEG, RMRCGVEG,     RTTEMPCG)
C
C
C     IF AILCG/GLEAFMAS IS ZERO, I.E. REAL LEAVES ARE NOT ON, THEN
C     MAKE MAINTENANCE RESPIRATION AND GPP FROM STORAGE/IMAGINARY LAI 
C     EQUAL TO ZERO SO THAT WE DON'T USE THESE NUMBERS IN CARBON BUDGET.
C
      DO 180 J = 1, ICC
        DO 190 I = IL1, IL2
          GPPCSVEG(I,J)=ANCSVEG(I,J)+RMLCSVEG(I,J)
          GPPCGVEG(I,J)=ANCGVEG(I,J)+RMLCGVEG(I,J)
C
          IF (LFSTATUS(I,J).EQ.4) THEN
            RMLCGVEG(I,J)=0.0
            RMLCSVEG(I,J)=0.0
            PANCSVEG(I,J)=ANCSVEG(I,J)   ! TO BE USED FOR PHENOLOGY
            PANCGVEG(I,J)=ANCGVEG(I,J)   ! PURPOSES
            ANCSVEG(I,J)=0.0
            ANCGVEG(I,J)=0.0
          ELSE
            PANCSVEG(I,J)=ANCSVEG(I,J)   ! TO BE USED FOR PHENOLOGY
            PANCGVEG(I,J)=ANCGVEG(I,J)   ! PURPOSES
            IF(SLAI(I,J).GT.AILCG(I,J))THEN
             TERM=((1.0/KN(SORT(J)))*(1.0-EXP(-KN(SORT(J))*AILCG(I,J))) 
     &          /(1.0/KN(SORT(J)))*(1.0-EXP(-KN(SORT(J))* SLAI(I,J))))
             RMLCGVEG(I,J)=RMLCGVEG(I,J)*TERM
             RMLCSVEG(I,J)=RMLCSVEG(I,J)*TERM
            ENDIF
          ENDIF
190     CONTINUE
180   CONTINUE
C
C     FIND VEGETATION AVERAGED LEAF, STEM, AND ROOT RESPIRATION, AND
C     GPP USING VALUES FROM CANOPY OVER GROUND AND CANOPY OVER SNOW
C     SUBAREAS
C
      DO 270 J = 1, ICC
        DO 280 I = IL1, IL2
          IF( (FCANC(I,J)+FCANCS(I,J)).GT.ZERO) THEN
            RMSVEG(I,J)= (FCANC(I,J)*RMSCGVEG(I,J) + 
     &        FCANCS(I,J)*RMSCSVEG(I,J)) / ( FCANC(I,J) + FCANCS(I,J))     
            RMRVEG(I,J)= (FCANC(I,J)*RMRCGVEG(I,J) + 
     &        FCANCS(I,J)*RMRCSVEG(I,J)) / ( FCANC(I,J) + FCANCS(I,J))
            RMLVEG(I,J)= (FCANC(I,J)*RMLCGVEG(I,J) + 
     &        FCANCS(I,J)*RMLCSVEG(I,J)) / ( FCANC(I,J) + FCANCS(I,J))
            ANVEG(I,J)= (FCANC(I,J)*ANCGVEG(I,J) + 
     &        FCANCS(I,J)*ANCSVEG(I,J)) / ( FCANC(I,J) + FCANCS(I,J))
            GPPVEG(I,J)= (FCANC(I,J)*GPPCGVEG(I,J) + 
     &        FCANCS(I,J)*GPPCSVEG(I,J)) / ( FCANC(I,J) + FCANCS(I,J))
            PHEANVEG(I,J)= (FCANC(I,J)*PANCGVEG(I,J) + 
     &        FCANCS(I,J)*PANCSVEG(I,J)) / ( FCANC(I,J) + FCANCS(I,J))
          ELSE
            RMSVEG(I,J)= 0.0
            RMRVEG(I,J)= 0.0
            RMLVEG(I,J)= 0.0
            ANVEG(I,J)= 0.0
            GPPVEG(I,J)= 0.0
            PHEANVEG(I,J)= 0.0
          ENDIF
C         
          IF(LFSTATUS(I,J).EQ.4)THEN
            GPPVEG(I,J) = ANVEG(I,J) + RMLVEG(I,J)
          ENDIF
C
          RMVEG(I,J)  = RMLVEG(I,J) + RMRVEG(I,J) + RMSVEG(I,J)
          NPPVEG(I,J) = GPPVEG(I,J) - RMVEG(I,J)
280     CONTINUE 
270   CONTINUE 
C
C     NOW THAT WE KNOW MAINTENANCE RESPIRATION FROM LEAF, STEM, AND ROOT,
C     AND GPP, WE CAN FIND GROWTH RESPIRATION FOR EACH VEGETATION 
C
      DO 300 J = 1, ICC
        DO 310 I = IL1, IL2
          IF( NPPVEG(I,J).GT.ZERO ) THEN
            RGVEG(I,J)=GRESCOEF(SORT(J))*NPPVEG(I,J)
          ELSE
            RGVEG(I,J)=0.0
          ENDIF
          NPPVEG(I,J) = NPPVEG(I,J) - RGVEG(I,J)
310     CONTINUE
300   CONTINUE
C
C     CALCULATE GRID-AVERAGED RATES OF RM, RG, NPP, AND GPP
C
      DO 320 J = 1,ICC
        DO 330 I = IL1, IL2
          RML(I)=RML(I)+FCANCMX(I,J)*RMLVEG(I,J)
          RMS(I)=RMS(I)+FCANCMX(I,J)*RMSVEG(I,J)
          RMR(I)=RMR(I)+FCANCMX(I,J)*RMRVEG(I,J)
          RM(I) =RM(I)+FCANCMX(I,J)*RMVEG(I,J)
          RG(I) =RG(I)+FCANCMX(I,J)*RGVEG(I,J)
          NPP(I)=NPP(I)+FCANCMX(I,J)*NPPVEG(I,J)
          GPP(I)=GPP(I)+FCANCMX(I,J)*GPPVEG(I,J)
          AUTORES(I)=RG(I)+RM(I)
330     CONTINUE
320   CONTINUE
C
C     AUTOTROPHIC RESPIRATION PART ENDS
C
C     ------------------------------------------------------------------
C
C     HETEROTROPHIC RESPIRATION PART STARTS
C
C     FIND HETEROTROPHIC RESPIRATION RATES (uMOL CO2/M2/SEC) FOR CANOPY
C     OVER SNOW SUBAREA
C
       CALL    HETRESV ( FCANCS,      FCS, LITRMASS, SOILCMAS,
     1                      ICC,       IG,      ILG,      IL1,
     2                      IL2,   TBARCS,   THLIQC,     SAND,
     3                     CLAY, RTTEMPCS,    ZBOTW,     SORT,
     4                     ISAND,
     5                 LTRSVGCS, SCRSVGCS) 
C
C     FIND HETEROTROPHIC RESPIRATION RATES FOR CANOPY OVER GROUND 
C     SUBAREA
C
       CALL    HETRESV (  FCANC,       FC, LITRMASS, SOILCMAS,
     1                      ICC,       IG,      ILG,      IL1,
     2                      IL2,    TBARC,   THLIQC,     SAND,
     3                     CLAY, RTTEMPCG,    ZBOTW,     SORT,
     4                     ISAND,
     5                 LTRSVGCG, SCRSVGCG) 

C
C     FIND HETEROTROPHIC RESPIRATION RATES FROM BARE GROUND SUBAREA
C
       CALL  HETRESG  (LITRMASS, SOILCMAS,      ICC,       IG,      
     1                      ILG,      IL1,      IL2,    TBARG,   
     2                   THLIQG,     SAND,      CLAY,   ZBOTW,   
     3                       FG,        0,
     4                     ISAND,
     5                   LTRSBRG,  SCRSBRG)
C
C     FIND HETEROTROPHIC RESPIRATION RATES FROM SNOW OVER GROUND 
C     SUBAREA
C
       CALL  HETRESG  (LITRMASS, SOILCMAS,      ICC,       IG,      
     1                      ILG,      IL1,      IL2,   TBARGS,   
     2                   THLIQG,     SAND,      CLAY,   ZBOTW,   
     3                      FGS,        1,
     4                     ISAND,
     5                   LTRSBRGS, SCRSBRGS)
C
C
C     FIND VEGETATION AVERAGED LITTER AND SOIL C RESPIRATION RATES
C     USING VALUES FROM CANOPY OVER GROUND AND CANOPY OVER SNOW SUBAREAS
C
      DO 340 J = 1, ICC
        DO 350 I = IL1, IL2
          IF( (FCANC(I,J)+FCANCS(I,J)).GT.ZERO) THEN
            LTRESVEG(I,J)= (FCANC(I,J)*LTRSVGCG(I,J) + 
     &        FCANCS(I,J)*LTRSVGCS(I,J)) / ( FCANC(I,J) + FCANCS(I,J))     
            SCRESVEG(I,J)= (FCANC(I,J)*SCRSVGCG(I,J) + 
     &        FCANCS(I,J)*SCRSVGCS(I,J)) / ( FCANC(I,J) + FCANCS(I,J))
            HETRSVEG(I,J) =  LTRESVEG(I,J) + SCRESVEG(I,J)
          ELSE
            LTRESVEG(I,J)= 0.0
            SCRESVEG(I,J)= 0.0
            HETRSVEG(I,J)= 0.0
          ENDIF
          NEPVEG(I,J)=NPPVEG(I,J)-HETRSVEG(I,J)
350     CONTINUE 
340   CONTINUE 
C
C     FIND LITTER AND SOIL C RESPIRATION RATES AVERAGED OVER THE BARE 
C     FRACTION OF THE GRID CELL USING VALUES FROM GROUND AND SNOW OVER
C     GROUND SUB-AREAS.
C
      DO 355 I = IL1, IL2
        IF( (FG(I)+FGS(I)).GT.ZERO) THEN
          LTRESVEG(I,ICC+1)= (FG(I)*LTRSBRG(I) + 
     &      FGS(I)*LTRSBRGS(I)) / ( FG(I) + FGS(I) )     
          SCRESVEG(I,ICC+1)= (FG(I)*SCRSBRG(I) + 
     &      FGS(I)*SCRSBRGS(I)) / ( FG(I) + FGS(I) )     
          HETRSVEG(I,ICC+1) =  LTRESVEG(I,ICC+1) + SCRESVEG(I,ICC+1)
        ELSE
          LTRESVEG(I,ICC+1)= 0.0
          SCRESVEG(I,ICC+1)= 0.0
          HETRSVEG(I,ICC+1)= 0.0
        ENDIF
355   CONTINUE
C
C     FIND GRID AVERAGED LITTER AND SOIL C RESPIRATION RATES
C
      DO 360 J = 1,ICC
        DO 370 I = IL1, IL2
          LITRES(I)=LITRES(I)+FCANCMX(I,J)*LTRESVEG(I,J)
          SOCRES(I)=SOCRES(I)+FCANCMX(I,J)*SCRESVEG(I,J)
370     CONTINUE
360   CONTINUE
C
      DO 380 I = IL1, IL2
        LITRES(I)=LITRES(I)+( (FG(I)+FGS(I))*LTRESVEG(I,ICC+1))
        SOCRES(I)=SOCRES(I)+( (FG(I)+FGS(I))*SCRESVEG(I,ICC+1))
        HETRORES(I)= LITRES(I)+SOCRES(I)
        NEP(I)=NPP(I)-HETRORES(I)
380   CONTINUE
C
C     ---------------------------------------------------------------
C
C     UPDATE THE LITTER AND SOIL C POOLS BASED ON LITTER AND SOIL C
C     RESPIRATION RATES FOUND ABOVE. ALSO TRANSFER HUMIDIFIED LITTER 
C     TO THE SOIL C POOL.
C
      DO 420 J = 1, (ICC+1)
        DO 430 I = IL1, IL2
C         CONVERT u MOL CO2/M2.SEC -> Kg C/M2 RESPIRED OVER THE MODEL
C         TIME STEP
          LTRESTEP(I,J)=LTRESVEG(I,J)*(1.0/963.62)*DELTAT
          SCRESTEP(I,J)=SCRESVEG(I,J)*(1.0/963.62)*DELTAT
C
C         UPDATE LITTER AND SOIL C POOLS
          IF(J.NE.ICC+1)THEN
           LITRMASS(I,J)=LITRMASS(I,J)-(LTRESTEP(I,J)*
     &                   (1.0+HUMICFAC(SORT(J))))
           HUTRSTEP(I,J)=(HUMICFAC(SORT(J))* LTRESTEP(I,J))
          ELSE
           LITRMASS(I,J)=LITRMASS(I,J)-(LTRESTEP(I,J)*(1.0+0.45))
           HUTRSTEP(I,J)=(0.45 * LTRESTEP(I,J))
          ENDIF
C
          HUMTRSVG(I,J)=HUTRSTEP(I,J)*(963.62/DELTAT) ! u-MOL CO2/M2.SEC
          SOILCMAS(I,J)=SOILCMAS(I,J) + 
     &          REAL(SPINFAST) * (HUTRSTEP(I,J) -  SCRESTEP(I,J)) 
C
          IF(LITRMASS(I,J).LT.ZERO) LITRMASS(I,J)=0.0
          IF(SOILCMAS(I,J).LT.ZERO) SOILCMAS(I,J)=0.0
430     CONTINUE
420   CONTINUE
C
C     ESTIMATE SOIL RESPIRATION. THIS IS SUM OF HETEROTROPHIC RESPIRATION
C     AND ROOT MAINTENANCE RESPIRATION.
C
      DO 440 J = 1, ICC
        DO 450 I = IL1, IL2
          SOILRSVG(I,J)=LTRESVEG(I,J)+SCRESVEG(I,J)+RMRVEG(I,J)
450     CONTINUE
440   CONTINUE
C
C     BUT OVER THE BARE FRACTION THERE IS NO LIVE ROOT.
C
      DO 460 I = IL1, IL2
        SOILRSVG(I,ICC+1)=LTRESVEG(I,ICC+1)+SCRESVEG(I,ICC+1)
460   CONTINUE
C
C     FIND GRID AVERAGED HUMIFICATION AND SOIL RESPIRATION RATES
C
      DO 470 J = 1,ICC
        DO 480 I = IL1, IL2
          SOILRESP(I)=SOILRESP(I)+FCANCMX(I,J)*SOILRSVG(I,J)
          HUMIFTRS(I)=HUMIFTRS(I)+FCANCMX(I,J)*HUMTRSVG(I,J)
480     CONTINUE
470   CONTINUE
C
      DO 490 I = IL1, IL2
        SOILRESP(I)=SOILRESP(I)+( (FG(I)+FGS(I))*SOILRSVG(I,ICC+1))
        HUMIFTRS(I)=HUMIFTRS(I)+( (FG(I)+FGS(I))*HUMTRSVG(I,ICC+1))
490   CONTINUE
C
C     HETEROTROPHIC RESPIRATION PART ENDS
C
C     ------------------------------------------------------------------
C
C     CH4 WETLAND EMISSIONS
C
      CALL wetland_methane (HETRORES, IL1, IL2, ILG, TA, WETFRAC,
     1                        IG, NPP, TBAR, THLIQG, CURRLAT,
     2                     SAND,  WETFRAC_S,
     3                  CH4WET1,    CH4WET2,    WETFDYN,
     4                  CH4DYN1,    CH4DYN2)

C
C     ------------------------------------------------------------------
C
C     ESTIMATE ALLOCATION FRACTIONS FOR LEAF, STEM, AND ROOT COMPONENTS.
C
           CALL ALLCAR (LFSTATUS,   THLIQC,    AILCG,
     1                         ICC,       IG,      ILG,       IL1,
     2                         IL2,     SAND,     CLAY,  RMATCTEM,
     3                    GLEAFMAS, STEMMASS, ROOTMASS,      SORT,
     4                       L2MAX, NOL2PFTS,       IC,   FCANCMX,
     5                     AFRLEAF,  AFRSTEM,  AFRROOT,    WILTSM,
     6                     FIELDSM, WTSTATUS, LTSTATUS)
C
C    ------------------------------------------------------------------
C
C     MAINTENANCE RESPIRATION ALSO REDUCES LEAF, STEM, AND ROOT BIOMASS.
C     WHEN NPP FOR A GIVEN PFT IS POSITIVE THEN THIS IS TAKEN CARE BY
C     ALLOCATING +VE NPP AMONGST THE LEAVES, STEM, AND ROOT COMPONENT.
C     WHEN NPP FOR A GIVEN PFT IS NEGATIVE THEN MAINTENANCE RESPIRATION
C     LOSS IS EXPLICITLY DEDUCTED FROM EACH COMPONENT.
C
      DO 600 J = 1, ICC
        DO 610 I = IL1, IL2
C
C         CONVERT NPP AND MAINTENANCE RESPIRATION FROM DIFFERENT COMPONENTS
C         FROM UNITS OF u MOL CO2/M2.SEC -> Kg C/M2 SEQUESTERED OR RESPIRED
C         OVER THE MODEL TIME STEP          
          GPPVGSTP(I,J)=GPPVEG(I,J)*(1.0/963.62)*DELTAT
          NPPVGSTP(I,J)=NPPVEG(I,J)*(1.0/963.62)*DELTAT
          RMLVGSTP(I,J)=RMLVEG(I,J)*(1.0/963.62)*DELTAT
          RMSVGSTP(I,J)=RMSVEG(I,J)*(1.0/963.62)*DELTAT
          RMRVGSTP(I,J)=RMRVEG(I,J)*(1.0/963.62)*DELTAT
C
          IF(LFSTATUS(I,J).NE.4)THEN
            IF(NPPVGSTP(I,J).GT.0.0) THEN
              NTCHLVEG(I,J)=AFRLEAF(I,J)*NPPVGSTP(I,J)
              NTCHSVEG(I,J)=AFRSTEM(I,J)*NPPVGSTP(I,J)
              NTCHRVEG(I,J)=AFRROOT(I,J)*NPPVGSTP(I,J)
            ELSE
              NTCHLVEG(I,J)=-RMLVGSTP(I,J)+AFRLEAF(I,J)*GPPVGSTP(I,J)
              NTCHSVEG(I,J)=-RMSVGSTP(I,J)+AFRSTEM(I,J)*GPPVGSTP(I,J)
              NTCHRVEG(I,J)=-RMRVGSTP(I,J)+AFRROOT(I,J)*GPPVGSTP(I,J)
            ENDIF
          ELSE  ! I.E. IF LFSTATUS.EQ.4
C           AND SINCE WE DO NOT HAVE ANY REAL LEAVES ON THEN WE DO NOT TAKE
C           INTO ACCOUNT CO2 UPTAKE BY IMAGINARY LEAVES IN CARBON BUDGET.
C           RMLVGSTP(I,J) SHOULD BE ZERO BECAUSE WE SET MAINTENANCE
C           RESPIRATION FROM STORAGE/IMAGINARY LEAVES EQUAL TO ZERO. 
C           IN LOOP 180 
C
            NTCHLVEG(I,J)=-RMLVGSTP(I,J) 
            NTCHSVEG(I,J)=-RMSVGSTP(I,J)
            NTCHRVEG(I,J)=-RMRVGSTP(I,J)
C
C           SINCE NO REAL LEAVES ARE ON, MAKE ALLOCATION FRACTIONS EQUAL TO
C           ZERO.
C
            AFRLEAF(I,J)=0.0
            AFRSTEM(I,J)=0.0
            AFRROOT(I,J)=0.0
          ENDIF
C
          GLEAFMAS(I,J)=GLEAFMAS(I,J)+NTCHLVEG(I,J)
          STEMMASS(I,J)=STEMMASS(I,J)+NTCHSVEG(I,J)
          ROOTMASS(I,J)=ROOTMASS(I,J)+NTCHRVEG(I,J)
C
C
          IF(GLEAFMAS(I,J).LT.0.0)THEN
            WRITE(6,1900)'GLEAFMAS LT ZERO AT I=',I,' FOR PFT=',J,''   
            WRITE(6,1901)'GLEAFMAS = ',GLEAFMAS(I,J)
            WRITE(6,1901)'NTCHLVEG = ',NTCHLVEG(I,J)
            WRITE(6,1902)'LFSTATUS = ',LFSTATUS(I,J)
            WRITE(6,1901)'AILCG    = ',AILCG(I,J)
            WRITE(6,1901)'SLAI     = ',SLAI(I,J)
1900        FORMAT(A23,I4,A10,I2,A1)
1902        FORMAT(A11,I4)
            CALL XIT ('CTEM',-6)
          ENDIF
C
          IF(STEMMASS(I,J).LT.0.0)THEN
            WRITE(6,1900)'STEMMASS LT ZERO AT I=(',I,') FOR PFT=',J,')'   
            WRITE(6,1901)'STEMMASS = ',STEMMASS(I,J)
            WRITE(6,1901)'NTCHSVEG = ',NTCHSVEG(I,J)
            WRITE(6,1902)'LFSTATUS = ',LFSTATUS(I,J)
            WRITE(6,1901)'RMSVGSTP = ',RMSVGSTP(I,J)
            WRITE(6,1901)'AFRSTEM  = ',AFRSTEM(I,J)
            WRITE(6,1901)'GPPVGSTP = ',GPPVGSTP(I,J)
            WRITE(6,1901)'RMSCSVEG = ',RMSCSVEG(I,J)
            WRITE(6,1901)'RMSCGVEG = ',RMSCGVEG(I,J)
1901        FORMAT(A11,F12.8)
            CALL XIT ('CTEM',-7)
          ENDIF
C
          IF(ROOTMASS(I,J).LT.0.0)THEN
            WRITE(6,1900)'ROOTMASS LT ZERO AT I=(',I,') FOR PFT=',J,')'   
            WRITE(6,1901)'ROOTMASS = ',ROOTMASS(I,J)
            CALL XIT ('CTEM',-8)
          ENDIF
C
C         CONVERT NET CHANGE IN LEAF, STEM, AND ROOT BIOMASS INTO 
C         u-MOL CO2/M2.SEC FOR USE IN BALCAR SUBROUTINE
C          
          NTCHLVEG(I,J)=NTCHLVEG(I,J)*(963.62/DELTAT)         
          NTCHSVEG(I,J)=NTCHSVEG(I,J)*(963.62/DELTAT)         
          NTCHRVEG(I,J)=NTCHRVEG(I,J)*(963.62/DELTAT)         
C
          NPPLEAF(I,J)=NTCHLVEG(I,J)
          NPPROOT(I,J)=NTCHSVEG(I,J)
          NPPSTEM(I,J)=NTCHRVEG(I,J)
C
C         TO AVOID OVER/UNDERFLOW PROBLEMS SET GLEAFMAS, STEMMASS, AND
C         ROOTMASS TO ZERO IF THEY GET TOO SMALL
C
          IF(BLEAFMAS(I,J).LT.ZERO) BLEAFMAS(I,J)=0.0
          IF(GLEAFMAS(I,J).LT.ZERO) GLEAFMAS(I,J)=0.0
          IF(STEMMASS(I,J).LT.ZERO) STEMMASS(I,J)=0.0
          IF(ROOTMASS(I,J).LT.ZERO) ROOTMASS(I,J)=0.0
C
610     CONTINUE
600   CONTINUE
C    ------------------------------------------------------------------
C
C     PHENOLOGY PART STARTS
C
C     THE PHENOLOGY SUBROUTINE DETERMINES LEAF STATUS FOR EACH PFT AND 
C     CALCULATES LEAF LITTER. THE PHENOLOGY SUBROUTINE USES SOIL 
C     TEMPERATURE (TBAR) AND ROOT TEMPERATURE. HOWEVER, SINCE CTEM
C     DOESN'T MAKE THE DISTINCTION BETWEEN CANOPY OVER GROUND, AND
C     CANOPY OVER SNOW SUB-AREAS FOR PHENOLOGY PURPOSES (FOR  EXAMPLE,
C     LEAF ONSET IS NOT ASSUMED TO OCCUR AT DIFFERENT TIMES OVER THESE
C     SUB-AREAS) WE USE AVERAGE SOIL AND ROOT TEMPERATURE IN THE PHENOLOGY
C     SUBROUTINE.
C
C     CALCULATE AVERAGE SOIL TEMPERATURE AND ROOT TEMPERATURE USING
C     VALUES FOR CANOPY OVER GROUND AND CANOPY OVER SNOW SUB-AREAS, FOR
C     EACH VEGETATION TYPE.
C
      DO 650 J = 1, ICC
        DO 660 I = IL1, IL2
          IF( (FCANC(I,J)+FCANCS(I,J)).GT.ZERO) THEN
            ROOTTEMP(I,J)= (FCANC(I,J)*RTTEMPCG(I,J) + 
     &        FCANCS(I,J)*RTTEMPCS(I,J)) / ( FCANC(I,J) + FCANCS(I,J))     
          ELSE
            ROOTTEMP(I,J)= RTTEMPCG(I,J)
          ENDIF
660     CONTINUE
650   CONTINUE
C
      DO 680 J = 1, IG
        DO 690 I = IL1, IL2
          IF( (FC(I)+FCS(I)).GT.ZERO) THEN
            TBARCCS(I,J)= (FC(I)*TBARC(I,J) + 
     &        FCS(I)*TBARCS(I,J)) / ( FC(I) + FCS(I))     
          ELSE
            TBARCCS(I,J)= TBAR(I,J)
          ENDIF
690     CONTINUE
680   CONTINUE
C
C    -------------------------------------------------------------------
C
C     CALL THE PHENOLOGY SUBROUTINE, WHICH DETERMINES THE LEAF GROWTH
C     STATUS, CALCULATES LEAF LITTER, AND CONVERTS GREEN GRASS INTO
C     BROWN.
C
            CALL PHENOLGY(GLEAFMAS, BLEAFMAS,      ICC,       IG,
     1                         ILG,      IL1,      IL2,  TBARCCS,
     2                      THLIQC,   WILTSM,  FIELDSM,       TA,
     3                    PHEANVEG,     IDAY,     RADJ, ROOTTEMP,
     4                    RMATCTEM, STEMMASS, ROOTMASS,     SORT,
     5                       L2MAX, NOL2PFTS,       IC,  FCANCMX,
     6                    FLHRLOSS, LEAFLITR, LFSTATUS,  PANDAYS,
     7                    COLDDAYS)
C
C    -------------------------------------------------------------------
C
C     WHILE LEAF LITTER IS CALCULATED IN THE PHENOLOGY SUBROUTINE, STEM
C     AND ROOT TURNOVER IS CALCULATED IN THE TURNOVER SUBROUTINE.
C
            CALL TURNOVER (STEMMASS, ROOTMASS,  LFSTATUS,    AILCG,
     1                          ICC,      ILG,       IL1,      IL2,
     2                         SORT, NOL2PFTS,        IC,  FCANCMX,
     3                     STMHRLOS, ROTHRLOS,
     4                     STEMLITR, ROOTLITR)
C
C    -------------------------------------------------------------------
C
C     UPDATE GREEN LEAF BIOMASS FOR TREES AND CROPS AND BROWN LEAF BIOMASS 
C     FOR GRASSES
C
      K1=0
      DO 700 J = 1, IC 
       IF(J.EQ.1) THEN
         K1 = K1 + 1
       ELSE
         K1 = K1 + NOL2PFTS(J-1)
       ENDIF
       K2 = K1 + NOL2PFTS(J) - 1
       DO 705 M = K1, K2
        DO 710 I = IL1, IL2

          IF(J.LE.3)THEN    ! TREES AND CROPS
            GLEAFMAS(I,M)=GLEAFMAS(I,M)-LEAFLITR(I,M)
            IF( GLEAFMAS(I,M).LT.0.0) THEN
              LEAFLITR(I,M)=LEAFLITR(I,M)+GLEAFMAS(I,M)
              GLEAFMAS(I,M)=0.0
            ENDIF
          ELSE              ! GRASSES
            BLEAFMAS(I,M)=BLEAFMAS(I,M)-LEAFLITR(I,M)
            IF( BLEAFMAS(I,M).LT.0.0) THEN
              LEAFLITR(I,M)=LEAFLITR(I,M)+BLEAFMAS(I,M)
              BLEAFMAS(I,M)=0.0
            ENDIF
          ENDIF

710     CONTINUE
705    CONTINUE
700   CONTINUE
C
C     UPDATE STEM AND ROOT BIOMASS FOR LITTER DEDUCTIONS
C
      DO 780 J = 1, ICC
        DO 790 I = IL1, IL2
          STEMMASS(I,J)=STEMMASS(I,J)-STEMLITR(I,J)
          IF( STEMMASS(I,J).LT.0.0) THEN
            STEMLITR(I,J)=STEMLITR(I,J)+STEMMASS(I,J)
            STEMMASS(I,J)=0.0
          ENDIF
C
          ROOTMASS(I,J)=ROOTMASS(I,J)-ROOTLITR(I,J)
          IF( ROOTMASS(I,J).LT.0.0) THEN
            ROOTLITR(I,J)=ROOTLITR(I,J)+ROOTMASS(I,J)
            ROOTMASS(I,J)=0.0
          ENDIF
790     CONTINUE
780   CONTINUE
C
C     UPDATE LITTER POOL WITH LEAF LITTER CALCULATED IN THE PHENOLOGY 
C     SUBROUTINE AND STEM AND ROOT LITTER CALCULATED IN THE TURNOVER
C     SUBROUTINE.
C
      DO 800 J = 1, ICC
        DO 810 I = IL1, IL2
          LITRMASS(I,J)=LITRMASS(I,J)+LEAFLITR(I,J)+STEMLITR(I,J)+
     &                  ROOTLITR(I,J)
810     CONTINUE
800   CONTINUE
C
C
C    ------------------------------------------------------------------
C
C     CALL THE MORTALIY SUBROUTINE WHICH CALCULATES MORTALITY DUE TO 
C     REDUCED GROWTH AND AGING. EXOGENOUS MORTALITY DUE TO FIRE AND OTHER 
C     DISTURBANCES AND THE SUBSEQUENT LITTER THAT IS GENERATED IS 
C     CALCULATED IN THE DISTURB SUBROUTINE.
C    
C     SET DO_MORTALITY=.FALSE. TO SWITCH OFF MORTALITY DUE TO AGE AND 
C     REDUCED GROWTH. MORTALITY IS LINKED TO THE COMPETITION PARAMETERIZATION 
C     AND GENERATES BARE FRACTION.


      DO_MORTALITY=.FALSE.
C
      CALL       MORTALTY (STEMMASS, ROOTMASS,        AILCG, GLEAFMAS,
     1                     BLEAFMAS,      ICC,          ILG,      IL1, 
     2                          IL2,     IDAY, DO_MORTALITY,     SORT,
     3                      FCANCMX, LYSTMMAS,     LYROTMAS, TYMAXLAI,
     4                     GRWTHEFF, STEMLTRM,     ROOTLTRM, GLEALTRM,
     5                     GEREMORT, INTRMORT)
C
C    ------------------------------------------------------------------
C
C     UPDATE LEAF, STEM, AND ROOT BIOMASS POOLS TO TAKE INTO LOSS
C     DUE TO MORTALITY, AND PUT THE LITTER INTO THE LITTER POOL. THE 
C     MORTALITY FOR GREEN GRASSES DOESN'T GENERATE LITTER, INSTEAD
C     THEY TURN BROWN.
C
      K1=0
      DO 830 J = 1, IC 
       IF(J.EQ.1) THEN
         K1 = K1 + 1
       ELSE
         K1 = K1 + NOL2PFTS(J-1)
       ENDIF
       K2 = K1 + NOL2PFTS(J) - 1
       DO 835 M = K1, K2
        DO 840 I = IL1, IL2
C
          STEMMASS(I,M)=STEMMASS(I,M)-STEMLTRM(I,M)
          ROOTMASS(I,M)=ROOTMASS(I,M)-ROOTLTRM(I,M)
          LITRMASS(I,M)=LITRMASS(I,M)+STEMLTRM(I,M)+ROOTLTRM(I,M)  
C
          IF(J.EQ.4)THEN    ! GRASSES
            GLEAFMAS(I,M)=GLEAFMAS(I,M)-GLEALTRM(I,M)
            BLEAFMAS(I,M)=BLEAFMAS(I,M)+GLEALTRM(I,M)
            GLEALTRM(I,M)=0.0
          ELSE              ! TREES AND CROPS
            GLEAFMAS(I,M)=GLEAFMAS(I,M)-GLEALTRM(I,M)
          ENDIF
          LITRMASS(I,M)=LITRMASS(I,M)+GLEALTRM(I,M)
C
840     CONTINUE
835    CONTINUE 
830   CONTINUE 
C
C    ------------------------------------------------------------------
C
C     CALL THE DISTURBANCE SUBROUTINE WHICH CALCULATES MORTALITY DUE TO 
C     FIRE AND OTHER DISTURBANCES. THE PRIMARY OUTPUT FROM FROM 
C     DISTURBANCE SUBROUTINE IS LITTER GENERATED, C EMISSIONS DUE TO 
C     FIRE AND AREA BURNED, WHICH MAY BE USED TO ESTIMATE CHANGE IN 
C     FRACTIONAL COVERAGES.
C
C     DISTURBANCE IS SPATIAL AND REQUIRES AREA OF GCM GRID CELL AND AREAS
C     OF DIFFERENT PFTs PRESENT IN A GIVEN GRID CELL. HOWEVER, WHEN CTEM IS
C     OPERATED AT A POINT SCALE THEN IT IS ASSUMED THAT THE SPATIAL SCALE 
C     IS 1 HECTARE = 10,000 M2. THE DISTURBANCE SUBROUTINE MAY BE STOPPED 
C     FROM SIMULATING ANY FIRE BY SPECIFYING FIRE EXTINGUSHING PROBABILITY
C     EQUAL TO 1.
C
C
            CALL DISTURB (STEMMASS, ROOTMASS, GLEAFMAS, BLEAFMAS,
     1                      THLIQC,   WILTSM,  FIELDSM,     VMOD,
     2                     LIGHTNG,  FCANCMX, LITRMASS,
     3                     RMATCTEM, PFTAREAB,
     4                         IL1,      IL2,       IG,      ICC,
     5                         ILG,     SORT, NOL2PFTS,       IC,
     6                    GRCLAREA,   THICEC,   POPDIN, LUCEMCOM,
     7                      DOFIRE,
C    IN ABOVE, OUT BELOW 
     8                    STEMLTDT, ROOTLTDT, GLFLTRDT, BLFLTRDT,
     9                    PFTAREAA, GLCAEMLS, RTCAEMLS, STCAEMLS,
     A                    BLCAEMLS, LTRCEMLS, BURNFRAC, PROBFIRE,
     B                    EMIT_CO2, EMIT_CO,  EMIT_CH4, EMIT_NMHC,
     C                    EMIT_H2,  EMIT_NOX, EMIT_N2O, EMIT_PM25,
     D                    EMIT_TPM, EMIT_TC,  EMIT_OC,  EMIT_BC)
C
C    ------------------------------------------------------------------
C
C     UPDATE LITTER POOL AND REDUCE LEAF, STEM, AND ROOT BIOMASS TO
C     TAKE INTO ACCOUNT LOSSES FROM DISTURBANCE. CALCULATE NBP (NET BIOME
C     PRODUCTION) FOR EACH PFT BY TAKING INTO ACCOUNT C EMISSION LOSSES.
C
      DO 1000 J = 1, ICC
        DO 1010 I = IL1, IL2
          GLEAFMAS(I,J)=GLEAFMAS(I,J) - GLFLTRDT(I,J) - GLCAEMLS(I,J)
          BLEAFMAS(I,J)=BLEAFMAS(I,J) - BLFLTRDT(I,J) - BLCAEMLS(I,J)
          STEMMASS(I,J)=STEMMASS(I,J) - STEMLTDT(I,J) - STCAEMLS(I,J)
          ROOTMASS(I,J)=ROOTMASS(I,J) - ROOTLTDT(I,J) - RTCAEMLS(I,J)
          LITRMASS(I,J)=LITRMASS(I,J) + GLFLTRDT(I,J) + BLFLTRDT(I,J) +  
     &                  STEMLTDT(I,J) + ROOTLTDT(I,J) - LTRCEMLS(I,J)
          DSCEMLV1(I,J)=GLCAEMLS(I,J) + BLCAEMLS(I,J) + STCAEMLS(I,J) +
     &                  RTCAEMLS(I,J) 
          DSCEMLV2(I,J)=GLCAEMLS(I,J) + BLCAEMLS(I,J) + STCAEMLS(I,J) +
     &                  RTCAEMLS(I,J) + LTRCEMLS(I,J)
C         CONVERT Kg C/M2 EMITTED IN ONE DAY INTO u MOL CO2/M2.SEC BEFORE
C         SUBTRACTING EMISSION LOSSES FROM NEP
          NBPVEG(I,J)  =NEPVEG(I,J)   - DSCEMLV2(I,J)*(963.62/DELTAT)
1010    CONTINUE
1000  CONTINUE
C
C     CALCULATE GRID. AVERAGED RATE OF CARBON EMISSIONS DUE TO FIRE IN
C     u-MOL CO2/M2.SEC. CONVERT ALL EMISSION LOSSES FROM Kg C/M2
C     EMITTED IN 1 DAY TO u-MOL CO2/M2.SEC. CALCULATE GRID AVERAGED
C     CARBON EMISSION LOSSES FROM LITTER.
C
      DO 1030 J = 1,ICC
        DO 1040 I = IL1, IL2
          DSTCEMLS1(I)=DSTCEMLS1(I) +
     &     FCANCMX(I,J)*DSCEMLV1(I,J)*(963.62/DELTAT)
          DSTCEMLS2(I)=DSTCEMLS2(I) +
     &     FCANCMX(I,J)*DSCEMLV2(I,J)*(963.62/DELTAT)
          GALTCELS(I)=GALTCELS(I) +
     &     FCANCMX(I,J)*LTRCEMLS(I,J)*(963.62/DELTAT)
          GLCAEMLS(I,J)=GLCAEMLS(I,J)*(963.62/DELTAT)
          BLCAEMLS(I,J)=BLCAEMLS(I,J)*(963.62/DELTAT)
          STCAEMLS(I,J)=STCAEMLS(I,J)*(963.62/DELTAT)
          RTCAEMLS(I,J)=RTCAEMLS(I,J)*(963.62/DELTAT)
          LTRCEMLS(I,J)=LTRCEMLS(I,J)*(963.62/DELTAT)
1040    CONTINUE
1030  CONTINUE
C

      DO 1041 I = IL1, IL2
        NBP(I)=NEP(I)-DSTCEMLS2(I)
        DSTCEMLS3(I)=DSTCEMLS2(I)-DSTCEMLS1(I)
1041  CONTINUE
C
C     CALCULATE TOTAL LITTER FALL FROM EACH COMPONENT (LEAVES, STEM, AND
C     ROOT) FROM ALL CAUSES (NORMAL TURNOVER, DROUGHT AND COLD STRESS FOR
C     LEAVES, MORTALITY, AND DISTURBANCE) FOR USE IN BALCAR SUBROUTINE
C
      DO 1050 J = 1,ICC
        DO 1060 I = IL1, IL2
C     
C        UNITS HERE ARE Kg C/M2.DAY
         TLTRLEAF(I,J)=LEAFLITR(I,J)+GLEALTRM(I,J)+GLFLTRDT(I,J)+
     &                 BLFLTRDT(I,J)
         TLTRSTEM(I,J)=STEMLITR(I,J)+STEMLTRM(I,J)+STEMLTDT(I,J)
         TLTRROOT(I,J)=ROOTLITR(I,J)+ROOTLTRM(I,J)+ROOTLTDT(I,J)
C          
C        CONVERT UNITS TO u-MOL CO2/M2.SEC
         LEAFLITR(I,J)=LEAFLITR(I,J)*(963.62/DELTAT)
         TLTRLEAF(I,J)=TLTRLEAF(I,J)*(963.62/DELTAT)
         TLTRSTEM(I,J)=TLTRSTEM(I,J)*(963.62/DELTAT)
         TLTRROOT(I,J)=TLTRROOT(I,J)*(963.62/DELTAT)
1060    CONTINUE
1050  CONTINUE
C
C     CALCULATE GRID-AVERAGE VEGETATION BIOMASS, LITTER MASS, AND SOIL
C     CARBON MASS, AND LITTER FALL RATE
C
      DO 1100 J = 1, ICC
        DO 1110 I = IL1, IL2
          VGBIOMAS(I)=VGBIOMAS(I)+FCANCMX(I,J)*(GLEAFMAS(I,J)+
     &     BLEAFMAS(I,J)+STEMMASS(I,J)+ROOTMASS(I,J))
          LITRFALL(I)=LITRFALL(I)+FCANCMX(I,J)*(TLTRLEAF(I,J)+
     &     TLTRSTEM(I,J)+TLTRROOT(I,J))
          GAVGLTMS(I)=GAVGLTMS(I)+FCANCMX(I,J)*LITRMASS(I,J)
          GAVGSCMS(I)=GAVGSCMS(I)+FCANCMX(I,J)*SOILCMAS(I,J)
C         GAVGLAI (I)=GAVGLAI (I)+FCANCMX(I,J)*AILCG(I,J)
C
          GAVGLFMS(I)=GAVGLFMS(I)+FCANCMX(I,J)*GLEAFMAS(I,J)
          GAVGSTMS(I)=GAVGSTMS(I)+FCANCMX(I,J)*STEMMASS(I,J)
          GAVGRTMS(I)=GAVGRTMS(I)+FCANCMX(I,J)*ROOTMASS(I,J)
C
          GAVGNPLF(I) = GAVGNPLF(I) + FCANCMX(I,J)*NPPLEAF(I,J)
          GAVGNPST(I) = GAVGNPST(I) + FCANCMX(I,J)*NPPSTEM(I,J)
          GAVGNPRT(I) = GAVGNPRT(I) + FCANCMX(I,J)*NPPROOT(I,J)
1110    CONTINUE
1100  CONTINUE
C
      DO 1020 I = IL1, IL2
        GAVGLTMS(I)=GAVGLTMS(I)+( (FG(I)+FGS(I))*LITRMASS(I,ICC+1))
        GAVGSCMS(I)=GAVGSCMS(I)+( (FG(I)+FGS(I))*SOILCMAS(I,ICC+1))
1020  CONTINUE
C
C     -----------------------------------------------------------------
C
C     AT THIS STAGE WE HAVE ALL REQUIRED FLUXES IN u-MOL CO2/M2.SEC AND
C     INITIAL (LOOP 140 AND 145) AND UPDATED SIZES OF ALL POOLS 
C     (IN Kg C/M2). NOW WE CALL THE BALCAR SUBROUTINE AND MAKE SURE THAT
C     C IN LEAVES, STEM, ROOT, LITTER AND SOIL C POOL BALANCES WITHIN A
C     CERTAIN TOLERANCE.
C
      IF(SPINFAST.EQ.1)THEN
             CALL  BALCAR(GLEAFMAS, STEMMASS, ROOTMASS,  BLEAFMAS,
     1                    LITRMASS, SOILCMAS, NTCHLVEG,  NTCHSVEG,
     2                    NTCHRVEG, TLTRLEAF, TLTRSTEM,  TLTRROOT,
     3                    GLCAEMLS, BLCAEMLS, STCAEMLS,  RTCAEMLS,
     4                    LTRCEMLS, LTRESVEG, SCRESVEG,  HUMTRSVG,
     5                    PGLFMASS, PBLFMASS, PSTEMASS,  PROTMASS,
     6                    PLITMASS, PSOCMASS,   DELTAT,  VGBIOMAS,
     7                    PVGBIOMS, GAVGLTMS, PGAVLTMS,  GAVGSCMS,
     8                    PGAVSCMS, GALTCELS,
     9                         NPP,  AUTORES, HETRORES,       GPP,
     A                         NEP,   LITRES,   SOCRES, DSTCEMLS1,
     B                         NBP, LITRFALL, HUMIFTRS,
     C                         ICC,      ILG,      IL1,       IL2)
      ENDIF
C
C     -----------------------------------------------------------------
C
C     THE LUC SUBROUTINE ALSO ESTIMATES CARBON EMISSIONS DUE TO
C     COMBUSTION ASSOCITED WITH LUC, AND THIS FLUX IS SPREAD OUT OVER 
C     THE WHOLE YEAR AND IS THEREFORE SUBTRACTED TO GET NBP OF EACH PFT
C     AS WELL AS THE GRID AVERAGED VALUE OF NBP.
C
      IF(LNDUSEON.EQ.1)THEN
          DO 1150 J = 1, ICC
            DO 1151 I = IL1, IL2
C             LUC RELATED COMBUSTION FLUX IS ASSUMED TO BE SPREAD
C             UNIFORMLY OVER THE GRID CELL AND THUS REDUCES NBP OF EACH
C             PFT
              NBPVEG(I,J)=NBPVEG(I,J)-LUCEMCOM(I) 
1151        CONTINUE
1150      CONTINUE
C
          DO 1160 I = IL1, IL2
            NBP(I)=NBP(I)-LUCEMCOM(I)
1160      CONTINUE
      ENDIF        
C
C     -----------------------------------------------------------------
C
C     FINALLY FIND VEGETATION STRUCTURAL ATTRIBUTES WHICH CAN BE PASSED
C     TO THE LAND SURFACE SCHEME USING LEAF, STEM, AND ROOT BIOMASS. 
C
              CALL  BIO2STR( GLEAFMAS, BLEAFMAS, STEMMASS, ROOTMASS,
     1                            ICC,      ILG,      IL1,      IL2,
     2                             IG,       IC,  FCANCMX,    ZBOTW,
     3                          DELZW, NOL2PFTS,    L2MAX, SOILDPTH,
     4                          AILCG,    AILCB,     AILC,    ZOLNC,
     5                          RMATC, RMATCTEM,     SLAI,  BMASVEG,
     6                       CMASVEGC,  VEGHGHT, ROOTDPTH,   ALVISC,
     8                         ALNIRC,
     9                         PAICGAT,SLAICGAT  )
C
C    CALCULATION OF GAVGLAI IS MOVED FROM LOOP 1100 TO HERE 
C    SINCE AILCG IS UPDATED BY BIO2STR
C
      DO J = 1, ICC
        DO I = IL1, IL2
          GAVGLAI (I)=GAVGLAI (I)+FCANCMX(I,J)*AILCG(I,J)
        ENDDO
      ENDDO
C
C
C     -----------------------------------------------------------------
C
      RETURN
      END
