def mask_extreme_week(data, tech, var='r', how='sum', what='min'):
    """
    Mask everywhere except the week containing the day where ``var`` of
    ``tech`` is minimal (what='min') or maximal (what='max') across the
    sum (how='sum') of or the mode (most) of locations (how='mode').

    Parameters
    ----------
    data : Calliope model data
    tech : str
        Technology whose `var` to find extreme week for.
    var : str, default 'r'
    how : str, default 'sum'
        'sum' or 'mode'
    what : str, default 'min'
        'min' or 'max'

    """
    df = data[var][tech]
    # Get length of a day in timesteps
    day_len = int(24 / data.time_res_data)
    # Get day-wise sums
    dff_index = list(range(0, len(df), day_len))
    dff = df.rolling(window=day_len).sum().reindex(dff_index)
    # If what is 'min', this will get the 'idxmin' attribute (a method),
    # similar for 'max', else most likely raise an error!
    idx_extr = lambda x: getattr(x, 'idx{}'.format(what))
    if how == 'mode':
        # Find day where var/tech is min/max across the most locations
        selected = idx_extr(dff)().mode()[0]
    elif how == 'sum':
        # Find day where var/tech is min/max across all locations
        selected = idx_extr(dff.sum(axis=1))()
    d = data._dt.at[selected]
    # Determine the range for the calendar week
    # (7 days) to keep at full resolution
    week_start = selected - day_len * d.dayofweek
    week_end = selected + day_len * (7 - d.dayofweek)
    # Mask where everything is 1 (reduce resolution) by default
    mask = pd.Series(1, index=list(range(len(df))))
    # For the desired week, change the mask to native resolution (0)
    mask.loc[week_start:week_end - 1] = 0
    mask.name = 'mask'
    return mask
dataset/ETHPy150Open calliope-project/calliope/calliope/time_masks.py/mask_extreme_week/original