def slice_by_lev_and_time(
        ds,
        varname,
        itime,
        ilev,
        flip
):
    """
    Slice a DataArray by desired time and level.

    Args:
        ds: xarray Dataset
            Dataset containing GEOS-Chem data.
        varname: str
            Variable name for data variable to be sliced
        itime: int
            Index of time by which to slice
        ilev: int
            Index of level by which to slice
        flip: bool
            Whether to flip ilev to be indexed from ground or top of atmosphere

    Returns:
        ds[varname]: xarray DataArray
            DataArray of data variable sliced according to ilev and itime
    """
    # used in compare_single_level and compare_zonal_mean to get dataset slices
    vdims = ds[varname].dims
    if "time" in vdims and "lev" in vdims:
        if flip:
            maxlev_i = len(ds['lev'])-1
            return ds[varname].isel(time=itime, lev=maxlev_i - ilev)
        return ds[varname].isel(time=itime, lev=ilev)
    if ("time" not in vdims or itime == -1) and "lev" in vdims:
        if flip:
            maxlev_i = len(ds['lev'])-1
            return ds[varname].isel(lev=maxlev_i - ilev)
        return ds[varname].isel(lev=ilev)
    if "time" in vdims and "lev" not in vdims and itime != -1:
        return ds[varname].isel(time=itime)
    return ds[varname]