import math


def min_runtime_method(th_power_curve, timestep, min_runtime):
    """
    Calculate nominal thermal power output for necessary, minimum runtime
    (in hours!!!)

    Parameters
    ----------
    th_power_curve : np.array
        Thermal power values per timestep
    timestep : int
        Timestep in seconds
    min_runtime : float
        Minimum runtime thermal device should have per year (in hours!).

    Returns
    -------
    nom_power : float
        Nominal thermal power
    """

    assert timestep >= 0
    assert min_runtime > 0, 'Min. runtime should be larger than zero.'
    assert min_runtime <= 8760 + 24,  ('Runtime size exceeds ' +
                                       'maximum hours per year.')

    #  Convert min_runtime from hours to seconds
    min_runtime_s = min_runtime * 3600

    #  Calculate index
    index = math.ceil(min_runtime_s/timestep)

    #  Sort power curve descending
    th_power_curve.sort()
    load_dur_curve = th_power_curve[::-1]

    nom_power = load_dur_curve[index]

    assert nom_power > 0, ('Nominal power cannot be zero. It seems like' +
                           ' the load duration curve does not allow ' +
                           'the desired, mimimum runtime. Consider '+
                           'reducing the minimum runtime!')

    return nom_power