      subroutine heat_budget

      USE mod_extra
       USE mod_vrbls
       USE mod_masks
       USE mod_pvrbls
      implicit none
      include 'param_o.h'
!!JNT      include 'extra.comm'
      include 'mapot.comm'
      include 'const.h'
      include 'dynam.comm'
 
      integer,parameter::npes=nsub*nsub*nm
      integer :: mype
      integer :: n
      integer :: mype0
      integer :: ix
      integer :: jy
      integer :: imaxV
      integer :: imaxH
      integer :: imin
      integer :: jmaxV
      integer :: jmaxH
      integer :: jmin
      integer :: i
      integer :: j
      integer :: l
!!      real, dimension(0:ilm+1,0:jlm+1,lm) :: dum2
!!      real, dimension(0:im+1,0:jm+1,nm,lm) :: rddtdt
!!      real, dimension(0:im+1,0:jm+1,nm,lm) :: cudtdt
!!      real, dimension(0:im+1,0:jm+1,nm,lm) :: acdtdt
!!      real, dimension(igm,jgm,lm) :: rddtdt_ll
!!      real, dimension(igm,jgm,lm) :: cudtdt_ll
!!      real, dimension(igm,jgm,lm) :: acdtdt_ll
!!      real, dimension(jgm,lm) :: rddtdt_avg
!!      real, dimension(jgm,lm) :: cudtdt_avg
!!      real, dimension(jgm,lm) :: acdtdt_avg
!!================================================
      real, allocatable, dimension(:,:,:) :: dum2
      real, allocatable, dimension(:,:,:,:) :: rddtdt
      real, allocatable, dimension(:,:,:,:) :: cudtdt
      real, allocatable, dimension(:,:,:,:) :: acdtdt
      real, allocatable, dimension(:,:,:) :: rddtdt_ll
      real, allocatable, dimension(:,:,:) :: cudtdt_ll
      real, allocatable, dimension(:,:,:) :: acdtdt_ll
      real, allocatable, dimension(:,:) :: rddtdt_avg
      real, allocatable, dimension(:,:) :: cudtdt_avg
      real, allocatable, dimension(:,:) :: acdtdt_avg
!!
    !  character (len=03) :: c_mype!!!!!!!!!!!!!!!!!!!!!!!!DRAGAN
    !  character (len=03) :: c_nh  !!!!!!!!!!!!!!!!!!!!!!!!
    
    character (len=04) :: c_mype, c_nh
    
      character (len=80) :: c_filename1
      character (len=80) :: c_filename2
 

!-----------------------------------------------------------------------   
        do mype = 0, npes-1
!-----------------------------------------------------------------------   

          n = mype/nxy + 1
          mype0 = mype - (n-1)*nxy
          jy = (mype0/ixm) + 1
          ix = mod(mype0, ixm)+1
          list = 100 + mype + 1

!-----------------------------------------------------------------------   
          write(c_mype,1000) mype
          c_filename1='heat_budget_'//   &
                  c_mype//".dat"
         open(unit=list, file=c_filename1, form='unformatted')
!-----------------------------------------------------------------------   
         imaxV = ix * ildom
         imaxH = imaxV + 1
         imin  = imaxH - ildom
         jmaxV = jy * jldom
         jmaxH = jmaxV + 1
         jmin  = jmaxH - jldom

	 read(list)dum2
         rddtdt(imin:imaxH, jmin:jmaxH,n,:) = dum2(1:ilm,1:jlm,:)
	 read(list)dum2
         cudtdt(imin:imaxH, jmin:jmaxH,n,:) = dum2(1:ilm,1:jlm,:)
 	 read(list)dum2
         acdtdt(imin:imaxH, jmin:jmaxH,n,:) = dum2(1:ilm,1:jlm,:)
       enddo

       if(nm.eq.14)then
!        print *,pslp(34,57,3),pslp(34,58,3),pslp(35,57,3),pslp(35,58,3)
        call octa2llH(rddtdt, rddtdt_ll, lm) 
        call octa2llH(cudtdt, cudtdt_ll, lm) 
        call octa2llH(acdtdt, acdtdt_ll, lm) 

       else if(nm.eq.6)then
        call cube2llH(rddtdt, rddtdt_ll, lm) 
        call cube2llH(cudtdt, cudtdt_ll, lm) 
        call cube2llH(acdtdt, acdtdt_ll, lm) 
       endif

       do l=1,lm
       do j=1,jgm
         rddtdt_avg(j,l)=0.
	 cudtdt_avg(j,l)=0.
	 acdtdt_avg(j,l)=0.
       do i=1,igm
         rddtdt_avg(j,l)=rddtdt_avg(j,l)+rddtdt_ll(i,j,l)
         cudtdt_avg(j,l)=cudtdt_avg(j,l)+cudtdt_ll(i,j,l)
         acdtdt_avg(j,l)=acdtdt_avg(j,l)+acdtdt_ll(i,j,l)
       enddo
         rddtdt_avg(j,l)=rddtdt_avg(j,l)/igm
         cudtdt_avg(j,l)=cudtdt_avg(j,l)/igm
         acdtdt_avg(j,l)=acdtdt_avg(j,l)/igm
       enddo
       enddo

       open (14,file='heat_budget.dat',form='unformatted')
       write(14) aeta
       write(14)rddtdt_avg
       write(14)cudtdt_avg
       write(14)acdtdt_avg
       close(14)


! 1000 format(i3.3)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  1000 format(i4.4)


      end subroutine heat_budget
