def mask_extreme(data, tech, var='r', how='max',
                 length=24, locations=None, padding=None):
    """
    Mask everywhere except the ``length`` where ``var`` for the technology
    ``tech`` across the given list of ``locations`` is either minmal
    or maximal.

    Parameters
    ----------
    data : Calliope model data
    tech : str
        Technology whose `var` to find extreme for.
    var : str, default 'r'
    how : str, default 'max'
        'max' or 'min'
    length : int, default 24
        Timesteps to mask.
    locations : list, default None
        List of locations to use, if None, uses all available locations.
    padding : int, default None
        Pad beginning and end of the unmasked area by the number of
        timesteps given.

    """
    df = data[var][tech]
    if locations:
        df = df.loc[:, locations]
    df = df.sum(axis=1)
    totals = []
    for i in range(0, len(df), length):
        totals.append(df[i:(i + 1) * length].sum(axis=0))
    totals = pd.Series(totals)
    if how == 'max':
        total_i = totals.argmax()
    elif how == 'min':
        total_i = totals.argmin()
    mask = pd.Series(1, index=list(range(len(df))))
    ifrom = total_i * length
    ito = (total_i + 1) * length - 1
    if padding:
        ifrom = ifrom - padding
        ito = ito + padding
        # Ensure ifrom and ito remain within bounds
        if ifrom < 0:
            ifrom = 0
        if ito > len(df):
            ito = len(df)
    mask.loc[ifrom:ito] = 0
    mask.name = 'mask'
    return mask
dataset/ETHPy150Open calliope-project/calliope/calliope/time_masks.py/mask_extreme/original