!
! From proclat.F:
!-------------------------------------------------------------------
      subroutine calchts
!
! Calculate heights from mean mass, with gravity varying w/ height:
! 5/25/05 btf: new sub calchts from liying (lqian), to include
!              latitudinally varying gravity (sub glatf). This
!              is included in tgcmproc1.7.
!
      real :: g0,r0
      real :: g(kmx),xmas(kmx),dz
      real :: ftn(kmx),fo2(kmx),fo1(kmx),fn2(kmx)
      integer :: i
      dz=(h%zpt-h%zpb)/float(kmx-1)
! from cm to km
!
      do i=1,imx
!
! Process t,o2,o1,n2 and store in locals ftn,fo2,fo1,fn2:
! ftn,fo1,fo2,fn2 are values at model midpoint
	ftn(:)=raw_tn(i,:)
	fo1(:)=raw_o1(i,:)
	fo2(:)=raw_o2(i,:)
        fn2(:) = max(.00001,(1.-fo2(:)-fo1(:)))
!
! Get mean mass and calculate new heights:
! Note bottom boundary has already been read from the history. 
! xmas and g are values at model midpoint as well

        xmas(:) = 1./(fo1(:)/16.+fo2(:)/32.+fn2(:)/28.)*1.66e-24
C calculate latitude dependent g0 (at Earth surface) and Earth radius r0
	call glatf(gcmlat(lat),g0,r0)
        g(1)=g0*(r0/(r0+0.5*(flat(i,1,ix)+flat(i,2,ix))))**2
        do k=2,kmx
          flat(i,k,ix) = flat(i,k-1,ix) + boltz*dz*
     +      ftn(k-1) / (xmas(k-1)*g(k-1))
!
! 3/22/06 btf: add conditional on k to avoid flat overflow at k+1
          if (k < kmx)
     |      g(k)=g0*(r0/(r0+0.5*(flat(i,k,ix)+flat(i,k+1,ix))))**2
        enddo
        flat(i,:,ix) = flat(i,:,ix) * 1.e-5 ! cm to km
      enddo 
      return
      end subroutine calchts
!-------------------------------------------------------------------
      subroutine glatf(lat,gv,reff)
!
! CALCULATE LATITUDE VARIABLE GRAVITY (GV) AND EFFECTIVE
! RADIUS (REFF)
! Earth radius in cm
!
      real,intent(in) :: lat  
      real,intent(out) :: gv,reff  
      real :: dgtr=1.74533E-2
      real :: c2

      C2 = COS(2.*DGTR*LAT)
      GV = 980.616*(1.-.0026373*C2)
      REFF = 2.*GV/(3.085462E-6 + 2.27E-9*C2)
      RETURN
      end subroutine glatf
!-------------------------------------------------------------------

