c
c %W%
c
      subroutine ibudget

      include'mladv.inc'

      common/parm/kz,m,mm1,mm2,mp1,msq,tim,tmax,tstart,hy,rea,dt,
     +   xbasin,yy(mx0),icalc,kzp1,kzm1,iftop,ifbot,iftvv,ifrel,theta,
     +   ifsbl,ifeva

      common /extpar/ rlat0,rlng0,t0,v0,dhor,ht,time0,r0,f0star

      common/cnn/n,nm1,nm2,np1

      common/inform/ifdiff,ifpert,titl(20,2),runid(6),restid(9),
     +              idstring,ident

      character*70 idstring
      character*80 ident,fname
      character*4 restid,runid

      character*1 trno

      dimension tbud(lmax+1,maxtrace)

      data icall/0/

      if (icall.eq.0) then
         iflen=lnblk(ident,80)
         do 5 i=1,ntrace
            write(trno,6)i
  6         format(i1)
            fname=ident(:iflen)//'.bi'//trno
            open(unit=iibunit+i,file=fname,status='new',
     .                                     form='formatted')
  5       continue
         ml=mm1/istrip + 1
         mlp1=ml+1
         mu=m-ml + 1
         mln=nm1/istrip + 1
         mlp1n=mln+1
         mun=n-mln+1
         nmul=mu-ml+1
         nmuln=mun-mln+1
         xnm=nmuln*nmul
         icall=1
        endif

      tstd = tstart * t0 / 86400.
      if (amod(mltime-tstd+qgts*.5/86400.,dtbudi)
     .                           .ge.qgts/86400.) return

      do 1 i=1,ntrace

         do 2 j=1,l+1
            tbud(j,i)=0.
  2       continue

         icurtrace = i

         do 3 k=1,l
            do 4 j=mln,mun
               ip = (j - 1) * m + ml - 1
               do 8 j2=ml,mu
                  ip = ip + 1
                  tbud(k,i) = tbud(k,i) + trace(ip,k,mlcur,i) + 
     .                                    trcqgofk(ip,k)
  8             continue
  4          continue
            tbud(k,i) = tbud(k,i) / xnm
  3       continue

         do 9 j=mln,mun
            ip = (j - 1) * m + ml - 1
            do 10 j2=ml,mu 
               ip = ip + 1
               tbud(l+1,i) = tbud(l+1,i) + trace(ip,l+1,mlcur,i)
 10          continue
  9       continue
         tbud(l+1,i) = tbud(l+1,i) / xnm

         write(iibunit+i,7)mltime,(tbud(j,i),j=1,l+1)

  7      format(99e13.5)

  1    continue

      return
      end
