@sh.add_function(dsp, outputs=['starter_powers'])
def calculate_starter_powers(
start_demand_function, times, on_engine, delta_time_engine_starter,
engine_speeds_out):
"""
Calculates starter power [kW].
:param start_demand_function:
Energy required to start the engine function.
:type start_demand_function: function
:param times:
Time vector.
:type times: numpy.array
:param on_engine:
If the engine is on [-].
:type on_engine: numpy.array
:param delta_time_engine_starter:
Time elapsed to turn on the engine with electric starter [s].
:type delta_time_engine_starter: float
:param engine_speeds_out:
Engine speed [RPM].
:type engine_speeds_out: numpy.array
:return:
Starter power [kW].
:rtype: numpy.array
"""
i = np.where(np.bitwise_xor(on_engine[:-1], on_engine[1:]))[0]
start = on_engine[i + 1]
j = np.searchsorted(times, times[i] + delta_time_engine_starter + dfl.EPS)
e = start_demand_function(engine_speeds_out[i + start.astype(int)])
e /= (times.take(j, mode='clip') - times[i])
e[~start] *= -1
p = np.zeros_like(times, float)
for i, j, e in zip(i, j, e):
p[i:j] += e
return p