       SUBROUTINE  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, DSTCEMLS,
     B                          NBP, LITRFALL, HUMIFTRS,
     C                          ICC,      ILG,      IL1,      IL2)    
C     -----------------------------------------------------------------      
C
C               CANADIAN TERRESTRIAL ECOSYSTEM MODEL (CTEM) V1.0
C                          CARBON BALANCE SUBROUTINE
C
C     SK: MAY  2018 - REVISE TOLERANCES FOR 32-BIT VERSION.
C     27  MAY 2003  - THIS SUBROUTINE CHECKS IF THE VARIOUS C FLUXES
C     V. ARORA        BETWEEN THE DIFFERENT POOLS BALANCE PROPERLY TO
C                     MAKE SURE THAT CONSERVATION OF MASS IS ACHIEVED 
C                     WITH IN A SPECIFIED TOLERANCE.
C     INPUTS 
C
C                 UNLESS MENTIONED ALL POOLS ARE IN Kg C/M2
C
C                 POOLS (AFTER BEING UPDATED)
C
C     STEMMASS  - STEM MASS FOR EACH OF THE 9 CTEM PFTs
C     ROOTMASS  - ROOT MASS FOR EACH OF THE 9 CTEM PFTs
C     GLEAFMAS  - GREEN LEAF MASS FOR EACH OF THE 9 CTEM PFTs
C     BLEAFMAS  - BROWN LEAF MASS FOR EACH OF THE 9 CTEM PFTs
C     LITRMASS  - LITTER MASS OVER THE 9 PFTs AND THE BARE FRACTION
C                 OF THE GRID CELL
C     SOILCMAS  - SOIL CARBON MASS OVER THE 9 PFTs AND THE BARE FRACTION
C                 OF THE GRID CELL
C
C                 GRID AVERAGED POOLS
C     VGBIOMAS  - VEGETATION BIOMASS
C     GAVGLTMS  - LITTER MASS
C     GAVGSCMS  - SOIL CARBON MASS
C
C                 POOLS (BEFORE BEING UPDATED)

C                 VARIABLE EXPLANATION SAME AS ABOVE.
C     PGLFMASS  - PREVIOUS GREEN LEAF MASS
C     PBLFMASS  - PREVIOUS BROWN LEAF MASS
C     PSTEMASS  - PREVIOUS STEM MASS
C     PROTMASS  - PREVIOUS ROOT MASS
C     PLITMASS  - PREVIOUS LITTER MASS
C     PSOCMASS  - PREVIOUS SOIL C MASS
C       
C                 GRID AVERAGE POOLS
C     PVGBIOMS  - PREVIOUS VEGETATION BIOMASS
C     PGAVLTMS  - PREVIOUS LITTER MASS
C     PGAVSCMS  - PREVIOUS SOIL C MASS
C                 
C                 UNLESS MENTIONED ALL FLUXES ARE IN UNITS OF 
C                 u-MOL CO2/M2.SEC
C
C                 FLUXES FOR EACH PFT
C
C     NTCHLVEG  - NET CHANGE IN LEAF BIOMASS
C     NTCHSVEG  - NET CHANGE IN STEM BIOMASS
C     NTCHRVEG  - NET CHANGE IN ROOT BIOMASS
C                 THE NET CHANGE IS THE DIFFERENCE BETWEEN ALLOCATION
C                 AND AUTOTROPHIC RESPIRATORY FLUXES 
C
C     TLTRLEAF  - TOTAL LEAF LITTER FALLING RATE
C     TLTRSTEM  - TOTAL STEM LITTER FALLING RATE
C     TLTRROOT  - TOTAL ROOT LITTER FALLING RATE
C 
C                 CARBON EMISSION LOSSES MAINLY DUE TO FIRE 
C     GLCAEMLS  - GREEN LEAF CARBON EMISSION LOSSES 
C     BLCAEMLS  - BROWN LEAF CARBON EMISSION LOSSES 
C     STCAEMLS  - STEM CARBON EMISSION LOSSES 
C     RTCAEMLS  - ROOT CARBON EMISSION LOSSES 
C     LTRCEMLS  - LITTER CARBON EMISSION LOSSES
C
C     LTRESVEG  - LITTER RESPIRATION FOR EACH PFT + BARE FRACTION 
C     SCRESVEG  - SOIL C RESPIRATION FOR EACH PFT + BARE FRACTION
C     HUMTRSVG  - HUMIFICATION FOR EACH PFT + BARE FRACTION
C
C                 GRID AVERAGED FLUXES
C
C     NPP       - NET PRIMARY PRODUCTIVITY 
C     AUTORES   - AUTOTROPHIC RESPIRATION
C     HETRORES  - HETEROTROPHIC RESPIRATION
C     GPP       - GROSS PRIMARY PRODUCTIVITY
C     NEP       - NET PRIMARY PRODUCTIVITY     
C     LITRES    - LITTER RESPIRATION
C     SOCRES    - SOIL CARBON RESPIRATION
C     DSTCEMLS  - CARBON EMISSION LOSSES DUE TO DISTURBANCE, MAINLY FIRE
C     GALTCELS  - CARBON EMISSION LOSSES FROM LITTER
C     NBP       - NET BIOME PRODUCTIVITY
C     LITRFALL  - COMBINED (LEAVES, STEM, AND ROOT) TOTAL LITTER FALL RATE   
C     HUMIFTRS  - HUMIFICATION
C
C                 OTHER VARIABLES
C 
C     DELTAT    - CTEM's TIME STEP
C     ICC       - NO. OF CTEM PLANT FUNCTION TYPES, CURRENTLY 8
C     ILG       - NO. OF GRID CELLS IN LATITUDE CIRCLE
C     IL1,IL2   - IL1=1, IL2=ILG
C
      IMPLICIT NONE
C
      INTEGER ILG, ICC, IL1, IL2, I, J, K
C
      REAL STEMMASS(ILG,ICC),   ROOTMASS(ILG,ICC),   GLEAFMAS(ILG,ICC),    
     1     BLEAFMAS(ILG,ICC), LITRMASS(ILG,ICC+1), SOILCMAS(ILG,ICC+1), 
     2     NTCHLVEG(ILG,ICC),   NTCHSVEG(ILG,ICC),   NTCHRVEG(ILG,ICC),
     3     TLTRLEAF(ILG,ICC),   TLTRSTEM(ILG,ICC),   TLTRROOT(ILG,ICC),
     4     GLCAEMLS(ILG,ICC),   BLCAEMLS(ILG,ICC),   STCAEMLS(ILG,ICC),
     5     RTCAEMLS(ILG,ICC),   LTRCEMLS(ILG,ICC), LTRESVEG(ILG,ICC+1),
     6   SCRESVEG(ILG,ICC+1), HUMTRSVG(ILG,ICC+1),   PGLFMASS(ILG,ICC),
     7     PBLFMASS(ILG,ICC),   PSTEMASS(ILG,ICC),   PROTMASS(ILG,ICC),
     8   PLITMASS(ILG,ICC+1), PSOCMASS(ILG,ICC+1),            NPP(ILG),
     9         VGBIOMAS(ILG),       PVGBIOMS(ILG),       GAVGLTMS(ILG),
     A         PGAVLTMS(ILG),       GAVGSCMS(ILG),       PGAVSCMS(ILG),
     9          AUTORES(ILG),       HETRORES(ILG),            GPP(ILG),
     A              NEP(ILG),         LITRES(ILG),         SOCRES(ILG),
     B         DSTCEMLS(ILG),            NBP(ILG),       LITRFALL(ILG),
     C         HUMIFTRS(ILG),                ZERO,            TOLRANCE,
     D                DELTAT,       GALTCELS(ILG)
C
      REAL             DIFF1,               DIFF2
C
C
C     -----------------------------------------------------------------  
C     CONSTANTS
C
C     OUR TOLERANCE FOR BALANCING C BUDGET IN Kg C/M2 IN ONE DAY
C     DATA TOLRANCE/0.00001/    ! 64-BIT THIS IS 1/100th OF A GRAM OF C
      DATA TOLRANCE/0.0001/     ! 32-BIT THIS IS 1/10th OF A GRAM OF C
C     -----------------------------------------------------------------
C
      IF(ICC.NE.9)                            CALL XIT('BALCAR',-1)
C
C     TO CHECK C BUDGET WE GO THROUGH EACH POOL FOR EACH VEGETATION
C     TYPE.
C
C     GREEN AND BROWN LEAVES
C
      DO 100 J = 1, ICC
        DO 110 I = IL1, IL2
          DIFF1=(GLEAFMAS(I,J)+BLEAFMAS(I,J)- PGLFMASS(I,J)-
     &     PBLFMASS(I,J))
          DIFF2=(NTCHLVEG(I,J)- TLTRLEAF(I,J)- GLCAEMLS(I,J)- 
     &     BLCAEMLS(I,J))*(DELTAT/963.62)
          IF((ABS(DIFF1-DIFF2)).GT.TOLRANCE)THEN
            WRITE(6,2000)I,J,ABS(DIFF1-DIFF2),TOLRANCE
2000        FORMAT('AT (I)= (',I3,'), PFT=',I2,', ',F12.6,' IS GREATER    
     & THAN OUR TOLERANCE OF ',F12.6,' FOR LEAVES')  
            CALL XIT('BALCAR',-2)
          ENDIF
110     CONTINUE
100   CONTINUE     
C
C     STEM
C
      DO 150 J = 1, ICC
        DO 160 I = IL1, IL2
          DIFF1=STEMMASS(I,J) - PSTEMASS(I,J)
          DIFF2=(NTCHSVEG(I,J)- TLTRSTEM(I,J)- 
     &     STCAEMLS(I,J))*(DELTAT/963.62)
          IF((ABS(DIFF1-DIFF2)).GT.TOLRANCE)THEN
            WRITE(6,2001)I,J,ABS(DIFF1-DIFF2),TOLRANCE
2001        FORMAT('AT (I)= (',I3,'), PFT=',I2,', ',F12.6,' IS GREATER
     & THAN OUR TOLERANCE OF ',F12.6,' FOR STEM')
            CALL XIT('BALCAR',-3)
          ENDIF
160     CONTINUE
150   CONTINUE    
C
C     ROOT
C
      DO 200 J = 1, ICC
        DO 210 I = IL1, IL2
          DIFF1=ROOTMASS(I,J) - PROTMASS(I,J)
          DIFF2=(NTCHRVEG(I,J)- TLTRROOT(I,J)- 
     &     RTCAEMLS(I,J))*(DELTAT/963.62)
          IF((ABS(DIFF1-DIFF2)).GT.TOLRANCE)THEN
            WRITE(6,2002)I,J,ABS(DIFF1-DIFF2),TOLRANCE
2002        FORMAT('AT (I)= (',I3,'), PFT=',I2,', ',F12.6,' IS GREATER
     & THAN OUR TOLERANCE OF ',F12.6,' FOR ROOT')
            CALL XIT('BALCAR',-4)
          ENDIF
210     CONTINUE
200   CONTINUE    
C
C     LITTER OVER ALL PFTs
C
      DO 250 J = 1, ICC
        DO 260 I = IL1, IL2
          DIFF1=LITRMASS(I,J) - PLITMASS(I,J)
          DIFF2=( TLTRLEAF(I,J)+TLTRSTEM(I,J)+TLTRROOT(I,J)-
     &      LTRESVEG(I,J)-HUMTRSVG(I,J)-LTRCEMLS(I,J))*(DELTAT/963.62)  
          IF((ABS(DIFF1-DIFF2)).GT.TOLRANCE)THEN
            WRITE(6,2003)I,J,ABS(DIFF1-DIFF2),TOLRANCE
2003        FORMAT('AT (I)= (',I3,'), PFT=',I2,', ',F12.6,' IS GREATER
     & THAN OUR TOLERANCE OF ',F12.6,' FOR LITTER')
            CALL XIT('BALCAR',-5)
          ENDIF
260     CONTINUE
250   CONTINUE    
C
C     LITTER OVER THE BARE FRACTION
C
      DO 270 J = ICC+1, ICC+1
        DO 280 I = IL1, IL2
          DIFF1=LITRMASS(I,J) - PLITMASS(I,J)
          DIFF2=( -LTRESVEG(I,J)-HUMTRSVG(I,J))*
     &          ( DELTAT/963.62 )  
          IF((ABS(DIFF1-DIFF2)).GT.TOLRANCE)THEN
            WRITE(6,2003)I,J,ABS(DIFF1-DIFF2),TOLRANCE
            CALL XIT('BALCAR',-6)
          ENDIF
280     CONTINUE
270   CONTINUE    
C
C
C     SOIL CARBON
C
      DO 300 J = 1, ICC+1
        DO 310 I = IL1, IL2
          DIFF1=SOILCMAS(I,J) - PSOCMASS(I,J)
          DIFF2=( HUMTRSVG(I,J)-SCRESVEG(I,J) )*(DELTAT/963.62)  
          IF((ABS(DIFF1-DIFF2)).GT.TOLRANCE)THEN
            WRITE(6,2004)I,J,ABS(DIFF1-DIFF2),TOLRANCE
2004        FORMAT('AT (I)= (',I3,'), PFT=',I2,', ',F12.6,' IS GREATER
     & THAN OUR TOLERANCE OF ',F12.6,' FOR SOIL C')
            CALL XIT('BALCAR',-7)
          ENDIF
310     CONTINUE
300   CONTINUE   
C
C     -----------------------------------------------------------------
C
C     GRID AVERAGED FLUXES MUST ALSO BALANCE
C
C     VEGETATION BIOMASS
C
      DO 350 I = IL1, IL2
        DIFF1=VGBIOMAS(I)-PVGBIOMS(I)
        DIFF2=(GPP(I)-AUTORES(I)-LITRFALL(I)-
     &   DSTCEMLS(I) )*(DELTAT/963.62)
        IF((ABS(DIFF1-DIFF2)).GT.TOLRANCE)THEN
          WRITE(6,3001)'VGBIOMAS(',I,')=',VGBIOMAS(I)
          WRITE(6,3001)'PVGBIOMS(',I,')=',PVGBIOMS(I)
          WRITE(6,3001)'     GPP(',I,')=',GPP(I)
          WRITE(6,3001)' AUTORES(',I,')=',AUTORES(I)
          WRITE(6,3001)'LITRFALL(',I,')=',LITRFALL(I)
          WRITE(6,3001)'DSTCEMLS(',I,')=',DSTCEMLS(I)
3001      FORMAT(A9,I2,A2,F14.9) 
          WRITE(6,2005)I,ABS(DIFF1-DIFF2),TOLRANCE
2005      FORMAT('AT (I)= (',I3,'),',F12.6,' IS GREATER
     & THAN OUR TOLERANCE OF ',F12.6,' FOR VEGETATION BIOMASS')
          CALL XIT('BALCAR',-8)
        ENDIF
350   CONTINUE
C
C     LITTER
C
      DO 380 I = IL1, IL2
        DIFF1=GAVGLTMS(I)-PGAVLTMS(I)
        DIFF2=(LITRFALL(I)-LITRES(I)-HUMIFTRS(I)-GALTCELS(I))*
     &   (DELTAT/963.62)
        IF((ABS(DIFF1-DIFF2)).GT.TOLRANCE)THEN
          WRITE(6,3001)'PGAVLTMS(',I,')=',PGAVLTMS(I)
          WRITE(6,3001)'GAVGLTMS(',I,')=',GAVGLTMS(I)
          WRITE(6,3001)'LITRFALL(',I,')=',LITRFALL(I)
          WRITE(6,3001)'  LITRES(',I,')=',LITRES(I)
          WRITE(6,3001)'HUMIFTRS(',I,')=',HUMIFTRS(I)
          WRITE(6,3001)'GALTCELS(',I,')=',GALTCELS(I)
          WRITE(6,2006)I,ABS(DIFF1-DIFF2),TOLRANCE
2006      FORMAT('AT (I)= (',I3,'),',F12.6,' IS GREATER
     & THAN OUR TOLERANCE OF ',F12.6,' FOR LITTER MASS')
          CALL XIT('BALCAR',-9)
        ENDIF
380   CONTINUE
C
C     SOIL CARBON
C
      DO 390 I = IL1, IL2
        DIFF1=GAVGSCMS(I)-PGAVSCMS(I)
        DIFF2=(HUMIFTRS(I)-SOCRES(I))*(DELTAT/963.62)
        IF((ABS(DIFF1-DIFF2)).GT.TOLRANCE)THEN
          WRITE(6,3001)'PGAVSCMS(',I,')=',PGAVSCMS(I)
          WRITE(6,3001)'GAVGSCMS(',I,')=',GAVGSCMS(I)
          WRITE(6,3001)'HUMIFTRS(',I,')=',HUMIFTRS(I)
          WRITE(6,3001)'  SOCRES(',I,')=',SOCRES(I)
          WRITE(6,2007)I,ABS(DIFF1-DIFF2),TOLRANCE
2007      FORMAT('AT (I)= (',I3,'),',F12.6,' IS GREATER
     & THAN OUR TOLERANCE OF ',F12.6,' FOR SOIL C MASS')
          CALL XIT('BALCAR',-10)
        ENDIF
390   CONTINUE
C
      RETURN
      END

