*=*=*=*= stats.html =*=*=*=*
subroutine stats

subroutine stats


      subroutine stats(ngrid, nlayer, co2ice, emis, tsurf
     &                ,plev, play, t, u, v, q2)
c
#include "dimensions.h"
#include "dimphys.h"
#include "statto.h"
#include "comcstfi.h"
c
c  Output statistics at regular intervals to a direct access file.
c  Each variable is stored as a sum followed by a sum of squares.
c
c     co2ice  2d        co2 ice cover
c     emis    2d        surface emissivity
c     tsurf   2d        surface temperature
c     ps      2d        surface pressure
c     t       3d        temperature
c     u       3d        zonal wind
c     v       3d        meridional wind
c     rho     3d        density
c     q2      3d        b/layer eddy kinetic energy
c
c  If more variables are added amend values in include file statto.h
c
c  INPUT:
      integer       ngrid
      integer       nlayer
      real*4        co2ice(ngrid)
      real*4        emis(ngrid)
      real*4        tsurf(ngrid)
      real*4        plev(ngrid,nlayer+1)
      real*4        play(ngrid,nlayer)
      real*4        t(ngrid,nlayer)
      real*4        u(ngrid,nlayer)
      real*4        v(ngrid,nlayer)
      real*4        q2(ngrid,nlayer)
c
c  LOCAL:
      real*4        rho(ngridmx,nlayermx)
      integer       irec
      integer       count
      data          count /0/
      save          count
      integer       ctime
      data          ctime /1/
      save          ctime
C
C     PRINT*,'Start of : stats'
C
      if ((istats.gt.0).and.(mod(count,istats).eq.0)) then
        irec = nltime*(ctime-1) + 1
c
c  Calculate density
        do l = 1, nlayer
          do i = 1, ngrid
            rho(i,l) = play(i,l)/(r*t(i,l))
           enddo
        enddo
c
c  Store the fields
        call wstats(irec, co2ice,    ngrid, 1)
        call wstats(irec, emis,      ngrid, 1)
        call wstats(irec, tsurf,     ngrid, 1)
        call wstats(irec, plev(1,1), ngrid, 1)
        call wstats(irec, t,         ngrid, nlayer)
        call wstats(irec, u,         ngrid, nlayer)
        call wstats(irec, v,         ngrid, nlayer)
        call wstats(irec, rho,       ngrid, nlayer)
        call wstats(irec, q2,        ngrid, nlayer)
c
c  Update the record of stores in the header file
        nstore(ctime) = nstore(ctime) + 1
        backspace(ushead)
        write(ushead) nstore
        ctime = ctime + 1
        if (ctime.gt.istime) ctime = 1
      endif
c
      count = count + 1
C
C     PRINT*,'End   of : stats'
C
      RETURN
      END