code: def _bounds_from_array(arr, dim_name, bounds_name):
      
      # TODO: don't assume needed dimension is in axis=0
      # TODO: refactor to get rid of repetitive code
      spacing = arr.diff(dim_name).values
      lower = xr.DataArray(np.empty_like(arr), dims=arr.dims,
                           coords=arr.coords)
      lower.values[:-1] = arr.values[:-1] - 0.5*spacing
      lower.values[-1] = arr.values[-1] - 0.5*spacing[-1]
      upper = xr.DataArray(np.empty_like(arr), dims=arr.dims,
                           coords=arr.coords)
      upper.values[:-1] = arr.values[:-1] + 0.5*spacing
      upper.values[-1] = arr.values[-1] + 0.5*spacing[-1]
      bounds = xr.concat([lower, upper], dim='bounds')
      return bounds.T
docs: Get the bounds of an array given its center values.
  
      E.g. if lat-lon grid center lat/lon values are known, but not the
      bounds of each grid box.  The algorithm assumes that the bounds
      are simply halfway between each pair of center values.