@sh.add_function(dsp, inputs_kwargs=True, outputs=['phases_co2_emissions'])
def calculate_phases_co2_emissions(
times, phases_indices, co2_emissions, phases_distances=1.0):
"""
Calculates CO2 emission or cumulative CO2 of cycle phases [CO2g/km or CO2g].
If phases_distances is not given the result is the cumulative CO2 of cycle
phases [CO2g] otherwise it is CO2 emission of cycle phases [CO2g/km].
:param times:
Time vector [s].
:type times: numpy.array
:param phases_indices:
Indices of the cycle phases [-].
:type phases_indices: numpy.array
:param co2_emissions:
CO2 instantaneous emissions vector [CO2g/s].
:type co2_emissions: numpy.array
:param phases_distances:
Cycle phases distances [km].
:type phases_distances: numpy.array | float, optional
:return:
CO2 emission or cumulative CO2 of cycle phases [CO2g/km or CO2g].
:rtype: numpy.array
"""
from scipy.integrate import trapz
co2 = []
for i, j in phases_indices:
co2.append(trapz(co2_emissions[i:j], times[i:j]))
with np.errstate(divide='ignore', invalid='ignore'):
return np.nan_to_num(np.array(co2) / phases_distances)