
C**********************************************************************C
C                                                                      C
C     *** SUBROUTINE SUMMARY ***                                       C
C     CALCULATE AVERAGE WATER BALANCE COMPONENTS FROM IYSTART TO IYEND C
C     WRITE TO SUMMARRUNOFF.TXT                                        C
C                                                                      C
C                                                                      C
C**********************************************************************C
     
      SUBROUTINE SUMMARY(I)

      COMMON/BASIC/NGRID,NYEAR,NLC,BYEAR,IYSTART,IYEND,POP_FLAG,REGION,
     &CLIMATE_FLAG
      
      COMMON/CELLINFO/LADUSE(4000,10), HUCNO(4000), LATUDE(4000),
     &LONGI(4000),TAREA(4000)
      
      COMMON/SUMMARY/ANURAIN(200),ANURUN(200),ANUPET(200),ANUAET(200),
     &ANUSOIL(200),ANUTEMP(200)
     
      COMMON/SNOWPACK/SP(12), SNOWPACK, ANUMAXSWE(200)

      COMMON/SOIL/LZTWM(2200), LZFPM(2200), LZFSM(2200), LZSK(2200),
     >LZPK(2200), UZTWM(2200), UZFWM(2200), UZK(2200), ZPERC(2200),
     >REXP(2200), PFREE(2200), SMC(12)

      COMMON/MOMEAN/MOAVGRO(2200,12),MOAVGPET(2200,12),
     &MOAVGAET(2200,12),MOAVGPPT(2200,12),MOAVGSNW(2200,12),
     &MOAVGSOL(2200,12),MOAVGTEM(2200,12)

      COMMON/R/ RFACTOR(200)

C-----------------------------------------------------------------------      

      REAL RAINALL, AETALL, PETALL, RUNALL, RUNRATIO, ETRATIO, TRATIO
      
      REAL LZTWM(2200),LZFPM(2200),LZFSM(2200),UZTWM(2200),UZFWM(2200)
      
      REAL SOILALL, AETPETR, SWEALL,TEMPALL
          
      REAL ANURAIN(200), ANURUN(200), ANUPET(200), ANUAET(200), 
     &ANUSOIL(200), ANUMAXSWE(200),ANUTEMP(200)
            
      INTEGER HUCNO(4000), J, M, IYSTART, IYEND, ISTEP, BYEAR, R
      
      REAL RFACTOR (200), RALL

      CHARACTER*5 CLIMATE_FLAG
     
      
      CHARACTER*2 REGION

      REAL MOAVGRO(2200,12),MOAVGPET(2200,12),MOAVGAET(2200,12),
     &MOAVGPPT(2200,12),MOAVGSNW(2200,12),MOAVGSOL(2200,12),
     &MOAVGTEM(2200,12)
           
      RAINALL =0.
      AETALL =0.   
      PETALL=0.
      RUNALL=0.
      SOILALL=0.
      SWEALL=0.
      TEMPALL=0.
      RALL = 0.
           
      M=0
      
      ISTEP = IYEND - IYSTART + 1
          
      DO 101 R=1,12
      
      MOAVGRO(I,R)=MOAVGRO(I,R)/ISTEP
      MOAVGPET(I,R)=MOAVGPET(I,R)/ISTEP
      MOAVGAET(I,R)=MOAVGAET(I,R)/ISTEP
      MOAVGPPT(I,R)=MOAVGPPT(I,R)/ISTEP
      MOAVGTEM(I,R)=MOAVGTEM(I,R)/ISTEP
      MOAVGSNW(I,R)=MOAVGSNW(I,R)/ISTEP
      MOAVGSOL(I,R)=(MOAVGSOL(I,R)/ISTEP)/(UZTWM(I)+UZFWM(I)+
     &LZTWM(I)+LZFSM(I)+LZFPM(I))
      
101   CONTINUE

C----ONLY WRITE OUTPUT FOR YEARS AFTER THE MODEL WARMUP PERIOD

      DO 100 J = 1, ISTEP
       
            RAINALL = RAINALL + ANURAIN(J+IYSTART-BYEAR)    
            AETALL = AETALL + ANUAET(J+IYSTART-BYEAR)  
            PETALL = PETALL + ANUPET(J+IYSTART-BYEAR)
            RUNALL = RUNALL + ANURUN(J+IYSTART-BYEAR)
            SOILALL = SOILALL + ANUSOIL(J+IYSTART-BYEAR)
            SWEALL=SWEALL+ANUMAXSWE(J+IYSTART-BYEAR)
            TEMPALL=TEMPALL+ANUTEMP(J+IYSTART-BYEAR)
                 
            RALL = RALL + RFACTOR(J+IYSTART-BYEAR)

            M = M + 1

100   CONTINUE
     
      RAINALL = RAINALL /M  
      AETALL = AETALL / M
      PETALL = PETALL / M
      AETPETR = AETALL/PETALL
      RUNALL = RUNALL / M
      SOILALL = SOILALL / M
      SWEALL=SWEALL/M
      TEMPALL=TEMPALL/M
      RALL =RALL /M

      IF (RAINALL .NE. 0.) THEN  
      RUNRATIO = RUNALL/RAINALL
      ETRATIO = AETALL/RAINALL   
      ELSE
      
      RUNRATIO = 0.0
      ETRATIO = 0.0
      ENDIF  


      TRATIO = RUNRATIO + ETRATIO

   
C--WRITE TO FILE AVGANNRUNOFF.TXT

         WRITE (80,250) HUCNO(I), RAINALL, TEMPALL, SWEALL, SOILALL, 
     &PETALL, AETALL, AETPETR, RUNALL, RUNRATIO, ETRATIO, TRATIO
     
250      FORMAT (I10,',',F10.1,',',F10.1,',',F10.1,',',F10.2,',',F10.1,
     &',',F10.1,',',F10.2,',',F10.1,',',F10.2,',',F10.2,',',F10.2)    
     
         WRITE(82,2100) HUCNO(I), RAINALL, RALL
         
2100     FORMAT(I10,',',F10.1,',',F8.1)
    
C --- WRITE TO MOMEANRONOFF.TXT

      DO 252 R=1,12
      
      WRITE(803,253)HUCNO(I),R,MOAVGPPT(I,R),MOAVGTEM(I,R),
     &MOAVGSNW(I,R),
     &MOAVGSOL(I,R),MOAVGPET(I,R),MOAVGAET(I,R),MOAVGRO(I,R)

253   FORMAT(I10,',',I10,',',F10.1,',',F10.1,',',F10.1,',',F10.2,',',
     &F10.1,',',F10.1,',',F10.1)
     
252   CONTINUE
      
      RETURN
      END
