SUBROUTINE TSTAV90
!use consts
use global   
      implicit none  
 
      real(DP) :: TGMLAY(NSTLAY),a1
      integer :: iloc,iz,i,j,izloc
      SAVE

      DO 10 ILOC=1,NLOC
        TGM(ILOC)=0_DP
   10 CONTINUE
      DO 20 IZ=1,NLAYST
        TGMLAY(IZ)=0_DP
   20 CONTINUE
      DO 101 I=IVOHSN,IVOHSX
        DO 100 J=JVOHSN,JVOHSX
          ILOC=INVOL(I,J)
          IF(ILOC.GT.0) THEN
            IZLOC=MOD(ILOC-1,NZLOC)+1
            IZ=ILAYST(IZLOC)
            A1=VOL(I,J)*T(I,J)
            TGMLAY(IZ)=TGMLAY(IZ)+A1
            TGM(ILOC)=TGM(ILOC)+A1
          END IF
  100   CONTINUE
  101 CONTINUE
      TSTAVE=0_DP
      
      DO 110 IZ=1,NLAYST
        TSTAVE=TSTAVE+CR(IZ)*TGMLAY(IZ)
        TGMLAY(IZ)=TGMLAY(IZ)/(VOLST*THLST(IZ)/HEIGHT)
  110 CONTINUE
  
      TSTAVE=TSTAVE/(CAPSTO*VOLST)

      DO 200 ILOC=1,NLOC
        TGM(ILOC)=TGM(ILOC)/(VOLST*VOLOC(ILOC))
  200 CONTINUE

!write(520,*) tstave,a1

RETURN
END subroutine tstav90