      subroutine grid_prop

      implicit none
      include 'param_o.h'

      real,parameter::a=6371
      real,dimension(0:im+1,0:jm+1,nm)::sqv,sqh,q11,q12,q22
      real,dimension(0:im+1,0:jm+1,nm)::ds,csthta,csthtat
      real,dimension(igm,jgm)::dsll,csthtall
      integer::npes,mype,n,mype0,jy,ix,lsd,k,i,j
!-----------------------------------------------------------------------
!***********
      parameter(npes=nsub*nsub*nm,lsd=1) 

      open (unit = 11, file = '../data_in/grid/gridinit.dat',  &
          form = "unformatted")  
      read (11) SQV, SQH, Q11, Q12, Q22
      Close (11)  

      ds=sqrt(sqh*2)*a
      csthta=abs(q12/(sqrt(q11*q22)))

      do n=1,nm
      do j=1,jm
      do i=1,im
!        print *,i,j,n,ds(i,j,n),csthta(i,j,n)
      enddo
      enddo
      enddo

!-----------------------------------------------------------------------   

! *** Define values on a spherical grid
        
       if(nm.eq.14)then
        call octa2llH(ds, dsll, 1) 
        call avrv_oc(csthta, csthtat,lsd)
        csthtat = csthtat * .25

        do k=1,nm
	  if(k.eq.1.or.k.eq.2.or.k.eq.7.or.k.eq.8.or.k.eq.9  &
               .or.k.eq.12)then
              csthtat(1 ,1 ,k)=csthtat(1 ,1 ,k)*4./3.
          endif

	  if(k.eq.1.or.k.eq.4.or.k.eq.6.or.k.eq.7.or.k.eq.11  &
                .or.k.eq.14)then
             csthtat(im,1 ,k)=csthtat(im,1 ,k)*4./3.
          endif

	  if(k.eq.2.or.k.eq.5.or.k.eq.6.or.k.eq.9.or.k.eq.13  &
              .or.k.eq.14)then
             csthtat(1 ,jm,k)=csthtat(1 ,jm,k)*4./3.
          endif

	  if(k.eq.4.or.k.eq.5.or.k.eq.8.or.k.eq.11.or.k.eq.12  &
              .or.k.eq.13)then
             csthtat(im,jm,k)=csthtat(im,jm,k)*4./3.
           endif
        enddo
         
        call octa2llH(csthtat, csthtall, lsd) 

       else if(nm.eq.6)then
        call cube2llH(ds, dsll, 1)  
      
        call avrv(csthta,csthtat,lsd)
        csthtat = csthtat * .25
        csthtat(1 ,1 ,:)=csthtat(1 ,1 ,:)*4./3.
        csthtat(im,1 ,:)=csthtat(im,1 ,:)*4./3.
        csthtat(1 ,jm,:)=csthtat(1 ,jm,:)*4./3.
        csthtat(im,jm,:)=csthtat(im,jm,:)*4./3.
         
        call cube2llH(csthtat, csthtall, lsd) 
       endif

       open(11,file='../data_in/grid/gridp.dat',form='unformatted')
       write(11)dsll
       write(11)csthtall
       close(11)

       end subroutine grid_prop

