code: def convective_adjustment_direct(p, T, c, lapserate=6.5):
      
      # largely follows notation and algorithm in Akmaev (1991) MWR
      alpha = const.Rd / const.g * lapserate / 1.E3 # same dimensions as lapserate
      L = p.size
      ###  now handles variable lapse rate
      pextended = np.insert(p,0,const.ps)  # prepend const.ps = 1000 hPa as ref pressure to compute potential temperature
      Pi = np.cumprod((p / pextended[:-1])**alpha)  # Akmaev's equation 14 recurrence formula
      beta = 1./Pi
      theta = T * beta
      q = Pi * c
      n_k = np.zeros(L, dtype=np.int8)
      theta_k = np.zeros_like(p)
      s_k = np.zeros_like(p)
      t_k = np.zeros_like(p)
      thetaadj = Akmaev_adjustment_multidim(theta, q, beta, n_k,
                                             theta_k, s_k, t_k)
      T = thetaadj * Pi
      return T
docs: Convective Adjustment to a specified lapse rate.
  
      Input argument lapserate gives the lapse rate expressed in degrees K per km
      (positive means temperature increasing downward).
  
      Default lapse rate is 6.5 K / km.
  
      Returns the adjusted Column temperature.
      inputs:
      p is pressure in hPa
      T is temperature in K
      c is heat capacity in in J / m**2 / K
  
      Implements the conservative adjustment algorithm from Akmaev (1991) MWR