travel_emission_lerp_fit.py
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy as np
from .travel_emission_linear_fit import EmissionModel as BaseEmissionModel
class EmissionModel(BaseEmissionModel):
def apply_scaling_law(self, distance, config):
"""
This interpolation algorithm will handle duplicates
as one would expect it to do.
For triplicates and more it will only use the outmost values
in the list and ignore the middle ones.
And you do not have to provide them in order,
as this list is also sorted, and thankfully :
> In Python, when you sort equal values,
> they will retain their original order in the output.
:param distance: float
:param config:
:return: float
"""
assert config.points
assert len(config.points) > 0
footprint = None
sample_points = sorted(config.points, key=lambda p: float(p[0]))
# Numpy!
previous_point = [0, 0]
for i, sample_point in enumerate(sample_points):
if distance <= sample_point[0]:
t = 0
if sample_point[0] != previous_point[0]:
t = (distance - previous_point[0]) * 1.0 \
/ \
(sample_point[0] - previous_point[0])
footprint = previous_point[1] + t * (sample_point[1] - previous_point[1])
previous_point = sample_point
break
previous_point = sample_point
if footprint is None:
if len(config.points) == 1:
last = sample_points[0]
penu = [0, 0]
else:
last = sample_points[-1]
penu = sample_points[-2]
t = 0
if last[0] != penu[0]:
t = (distance - last[0]) * 1.0 / (last[0] - penu[0])
footprint = last[1] + t * (last[1] - penu[1])
return footprint