c=========================================================================


      subroutine zgrid(nz,z,p,t,wh,wo,zgrid1,zgrid2,ngrid) 1

c     module:    zgrid
c
c     purpose:   change vertical resolution of model atmosphere
c
c     input:
c       zgrid1   variable resolution parameter, a floating point number
c                that specifies the grid resolution of the lower part
c                of the grid in kilometers. 
c
c       zgrid2   variable resolution parameter, a floating point number
c                that specifies the maximum allowable grid spacing.
c                For example ZGRID2=40 indicates that the top layer
c                should be 40 km thick and that all other layers should
c                be smaller. 
c
c                A smooth function is used to set the vertical
c                distribution of grid points.  Since this distribution
c                varies smoothly with height, problems associated sharp
c                transitions in step size are avoided (such as
c                spuriously large flux divergence at the jump point).
c
c                if either ZGRID1 or ZGRID2 are specified as a negative
c                numbers REGRID will use the absolute value of the
c                parameters to compute a vertical scale, print the
c                regrided model atmosphere and stop execution.  This
c                option can be used to preview the effect of a given
c                values of ZGRID1 and ZGRID2. 
c
c       ngrid    desired number of vertical grid points (must be less
c                then mxly).  
c
c
c     input/output:
c
c       nz       input as number of vertical grid points in orginal
c                atmosphere, set to NGRID on output 
c
c       z        input:  original vertical position of grid points
c                output: regrided vertical positions
c
c       p        pressure at height z
c       t        temperature at heigth z
c       wh       water vapor density at height z
c       wo       ozone density at height z
c
c       
c
      dimension z(*),p(*),t(*),wh(*),wo(*)             
      parameter (mxly=50)
      dimension zz(mxly),pp(mxly),tt(mxly),whh(mxly),woo(mxly)


      if(ngrid.gt.mxly) then 
        write(*,*) 'Fatal Error in ZGRID: NGRID GT MXLY'
        stop 'ZGRID'
      endif

      a=abs(zgrid1)*float(ngrid-1)
      ztop=z(nz)
      if( a .ge. ztop) then 
        a=ztop
        beta=0.
      else
        a=min(a,.99*(ztop-abs(zgrid2)))
        toprat=float(ngrid-2)/(ngrid-1)
        beta=log(((ztop-abs(zgrid2))/(toprat*a)-1.)/
     &       (ztop/a-1.))/log(toprat)
      endif

      j=2

      do 30 i=1,ngrid
        x=float(i-1)/float(ngrid-1)
        zz(i)=a*x*(1.+(ztop/a-1.)*x**beta)
        do 10 jj=j,nz
          if(zz(i).le.z(jj)) goto 20
 10     continue
 20     continue
        j=min(jj,nz)
        fz=(zz(i)-z(j-1))/(        fz=min(max(fz,0.),1.)
        
c         write(*,'(2i10,3f12.4)') i,j,zz(i),z(j),fz
        
        pp(i)  =  p(j-1)  * (        tt(i)  =  t(j-1)  * (1.-fz) + t(j)  * fz
        whh(i) =  wh(j-1) * (1.-fz) + wh(j) * fz
        woo(i) =  wo(j-1) * (1.-fz) + wo(j) * fz
 30   continue
      
      do 40 i=1,ngrid
        z(i)=zz(i)
        p(i)=pp(i)
        t(i)=tt(i)
        wh(i)=whh(i)
        wo(i)=woo(i)
 40   continue
      
      nz=ngrid
      
      if(min(zgrid1,zgrid2).lt.0) then
        write(*,'(5x,5a11)') 'z','p','t','wh','wo'
        
        do 50 i=ngrid,1,-1
          write(*,'(i5,f11.3,1p4e11.3)') i,z(i),p(i),t(i),wh(i),wo(i)
 50     continue

        stop 'zgrid'
      endif
        
      return
      end