      subroutine dgns2ll(npes)

      implicit none
      include 'param_o.h'
      integer,intent(in)::npes
      real, dimension(0:im+1,0:jm+1,nm,lm)::tavg,uavg,vavg,u2avg,v2avg,  &
          t2avg, uvavg,thavg,ull,vll,ull2,tavgzn_c,tvar
      real,dimension(igm,jgm,lm)::tavgp,uavgp,vavgp,u2avgp,v2avgp,  &
          t2avgp, thavgp,ustrp
      real,dimension(igm,jgm)::ustr
      real,dimension(0:ilm+1,0:jlm+1,lm)::dum1,dum2,dum3,dum4,dum5,dum6  &
                      ,dum7,dum11
      real,dimension(jgm,lm)::tavg_zn,thavg_zn,tvar_zn,uavg_zn
      integer::i,mype,n,mype0,ix,jy,list,imaxv,imaxh,imin,jmaxv,jmaxh  &
                   ,jmin,i,j,l
      character (len=03):: c_mype
      character (len=80):: c_filename1

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

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

!-----------------------------------------------------------------------   
          write(c_mype,'(i3.3)') mype
          c_filename1='../../../RUN/data/data_out/dgns.'//c_mype
          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) dum1,dum11,dum2,dum3,dum4,dum5,dum6,dum7
         close(list)
	 tavg(imin:imaxH, jmin:jmaxH,n,:) = dum1(1:ilm,1:jlm,:)
	 thavg(imin:imaxH, jmin:jmaxH,n,:) = dum11(1:ilm,1:jlm,:)
         uavg(imin:imaxH, jmin:jmaxH,n,:) = dum2(1:ilm,1:jlm,:)
         vavg(imin:imaxH, jmin:jmaxH,n,:) = dum3(1:ilm,1:jlm,:)
         t2avg(imin:imaxH, jmin:jmaxH,n,:) = dum4(1:ilm,1:jlm,:)
         u2avg(imin:imaxV, jmin:jmaxV,n,:) = dum5(1:ilm-1,1:jlm-1,:)
         v2avg(imin:imaxV, jmin:jmaxV,n,:) = dum6(1:ilm-1,1:jlm-1,:)
         uvavg(imin:imaxV, jmin:jmaxV,n,:) = dum7(1:ilm-1,1:jlm-1,:)

!-----------------------------------------------------------------------   
        end do

        call octa2llH(tavg, tavgp, lm) 
        call octa2llH(thavg, thavgp, lm) 

	tavg_zn=0.
	tvar_zn=0.
	thavg_zn=0.

        do l=1,lm
	do j=1,jgm
	do i=1,igm
	  tavg_zn(j,l)=tavg_zn(j,l)+tavgp(i,j,l)
	  thavg_zn(j,l)=thavg_zn(j,l)+thavgp(i,j,l)
        enddo
        enddo
        enddo

	tavg_zn=tavg_zn/igm
	thavg_zn=thavg_zn/igm
	call znll2c(tavg_zn,tavgzn_c)
	tvar=t2avg-2.*tavg*tavgzn_c+tavgzn_c**2

	do l=1,lm
	do n=1,nm
	do i=0,im+1
	do j=0,jm+1
!	  print *,i,j,n,l,t2avg(i,j,n,l),tavg(i,j,n,l),tavgzn_c(i,j,n,l)
!     &            ,tvar(i,j,n,l)
        enddo
        enddo
        enddo
        enddo


        call llwinds(uavg, vavg, ull, vll,lm)
        call bocovs(ull,lm)
        call bocovs(vll,lm)
        call avrv(ull, uavg,lm)
        call avrv(vll, vavg,lm)

        uavg = uavg * .25
        uavg(1 ,1 ,:,:)=uavg(1 ,1 ,:,:)*4./3.
        uavg(im,1 ,:,:)=uavg(im,1 ,:,:)*4./3.
        uavg(1 ,jm,:,:)=uavg(1 ,jm,:,:)*4./3.
        uavg(im,jm,:,:)=uavg(im,jm,:,:)*4./3.

        vavg = vavg * .25
        vavg(1 ,1 ,:,:)=vavg(1 ,1 ,:,:)*4./3.
        vavg(im,1 ,:,:)=vavg(im,1 ,:,:)*4./3.
        vavg(1 ,jm,:,:)=vavg(1 ,jm,:,:)*4./3.
        vavg(im,jm,:,:)=vavg(im,jm,:,:)*4./3.
         
        call cube2llH(uavg, uavgp, lm) 
        call cube2llH(vavg, vavgp, lm) 

        uavg_zn=0.

	do l=1,lm
	do j=1,jgm
	do i=1,igm
	  uavg_zn(j,l)=uavg_zn(j,l)+uavgp(i,j,l)
        enddo
        enddo
        enddo
	uavg_zn=uavg_zn/igm

	call llwinds2(u2avg,v2avg,uvavg,ull2,lm)
	call bocovs(ull2,lm)
	call avrv(ull2,u2avg,lm)

        u2avg = u2avg * .25
        u2avg(1 ,1 ,:,:)=u2avg(1 ,1 ,:,:)*4./3.
        u2avg(im,1 ,:,:)=u2avg(im,1 ,:,:)*4./3.
        u2avg(1 ,jm,:,:)=u2avg(1 ,jm,:,:)*4./3.
        u2avg(im,jm,:,:)=u2avg(im,jm,:,:)*4./3.

	call cube2llH(u2avg,u2avgp,lm)
        ustrp=u2avgp-uavgp**2

	do i=1,igm
	do j=1,jgm
	  ustr(i,j)=0.
	  do l=1,lm
	   ustr(i,j)=ustr(i,j)+ustrp(i,j,l)
          enddo
	  ustr(i,j)=ustr(i,j)/lm
        enddo
        enddo
 
	open(10,file='dgns/dgns.dat',form='unformatted')
	write(10)tavg_zn
	write(10)thavg_zn
	write(10)uavg_zn
	write(10)tvar_zn
	write(10)ustr
	close(10)

      end subroutine dgns2ll
