! $Id: aver.f,v 1.6 2000/06/06 13:36:11 aake Exp $
************************************************************************
      subroutine aver (file, nio)
*
*  Write horizontal averages out to file.aver
*
      include 'cparam.inc'
      parameter (mav=7)
      include 'cdata.inc'
      include 'cwork.inc'
      character *(mfile) file, filea
      integer dot
      real, dimension(lb:ub):: rhom, pxm, pptm, ppgm, fcm, ekm, fkm
      real, dimension(my,mz):: px1, py1, pz1, ekin, ux1
*-----------------------------------------------------------------------
*
*  File for average data
*
      dot = index(file,'.')
      filea = file(1:dot)//'aver'
      open (13, file=filea,
     &         status='unknown', form='unformatted',
     &         access='direct', recl=lrec((ub-lb+1)*mav))
      write (*,*) filea(1:dot+4),' iscr,t= ',nio-1,t

      a=1./(my*mz)
      do i=lb,ub
        rhom(i)=sum(r(i,:,:))*a
        px1=0.5*(px(i,:,:)+px(i+1,:,:))
        pxm(i)=sum(px1)*a
        ux0=pxm(i)/rhom(i)
        px1=px1-ux0*r(i,:,:)
        ux1=px1/r(i,:,:)
        pptm(i)=sum(ux1*px1)*a
        ppgm(i)=sum(e(i,:,:)*(gamma-1.))*a
        fcm(i)=sum(ux1*e(i,:,:)*gamma)*a
        py1=py(i,:,:)
        pz1=pz(i,:,:)
        py1=0.5*(py1+cshift(py1,dim=1,shift=1))
        pz1=0.5*(pz1+cshift(pz1,dim=2,shift=1))
        ekin=(px1**2+py1**2+pz1**2)/r(i,:,:)
        ekm(i)=0.5*sum(ekin)*a
        fkm(i)=sum(ux1*ekin)*a
      enddo

      write (13,rec=nio) rhom, pxm, pptm, ppgm, fcm, ekm, fkm
      close (13)
*
      end
