Blame view

flaskr/laws/travel_emission_linear_fit.py 4.9 KB
2d6b5394   Antoine Goutenoir   Add an example of...
1
2
3
import numpy as np
from geopy.distance import great_circle

18ba3d7a   Antoine Goutenoir   Boost the models'...
4
from flaskr.laws import BaseEmissionModel
8c2482e3   Antoine Goutenoir   Grunt! Grunt! GRUNT.
5
6
7
8


class EmissionModel(BaseEmissionModel):
    # @abc
2d6b5394   Antoine Goutenoir   Add an example of...
9
10
    def compute_travel_footprint(
            self,
1bbf5d52   Antoine Goutenoir   Implement the sca...
11
12
13
14
            origin_latitude,        # degrees
            origin_longitude,       # degrees
            destination_latitude,   # degrees
            destination_longitude,  # degrees
18ba3d7a   Antoine Goutenoir   Boost the models'...
15
            extra_config=None,
1bbf5d52   Antoine Goutenoir   Implement the sca...
16
17
    ):
        footprint = 0.0
5634c975   Antoine Goutenoir   Expose travel dis...
18
        distance = 0.0
2d6b5394   Antoine Goutenoir   Add an example of...
19
20

        #############################################
4276f1aa   Antoine Goutenoir   Support train emi...
21
        # TODO (?): find closest airport(s) and pick one
2d6b5394   Antoine Goutenoir   Add an example of...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        # We're going to need caching here as well.
        from collections import namedtuple
        origin_airport = namedtuple('Position', [
            'latitude',
            'longitude',
            'address',  # perhaps
        ])
        origin_airport.latitude = origin_latitude
        origin_airport.longitude = origin_longitude
        destination_airport = namedtuple('Position', [
            'latitude',
            'longitude',
            'address',  # perhaps
        ])
        destination_airport.latitude = destination_latitude
        destination_airport.longitude = destination_longitude
        #############################################
        #############################################

18ba3d7a   Antoine Goutenoir   Boost the models'...
41
        # Let's start by computing the distance between the locations
5634c975   Antoine Goutenoir   Expose travel dis...
42
        great_circle_distance = self.get_distance_between(
1bbf5d52   Antoine Goutenoir   Implement the sca...
43
44
45
46
            origin_latitude=origin_airport.latitude,
            origin_longitude=origin_airport.longitude,
            destination_latitude=destination_airport.latitude,
            destination_longitude=destination_airport.longitude,
2d6b5394   Antoine Goutenoir   Add an example of...
47
        )
b935618e   Antoine Goutenoir   Count the number ...
48
49
50
51
52
53
54
55
        distance += great_circle_distance

        use_train = False
        use_plane = False
        if distance < extra_config['use_train_below_distance']:
            use_train = True
        else:
            use_plane = True
18ba3d7a   Antoine Goutenoir   Boost the models'...
56
57

        # I.a Train travel footprint
4276f1aa   Antoine Goutenoir   Support train emi...
58
59
60
61
        if use_train:
            footprint += self.compute_train_footprint(
                distance=great_circle_distance
            )
18ba3d7a   Antoine Goutenoir   Boost the models'...
62
        # I.b Airplane travel footprint
4276f1aa   Antoine Goutenoir   Support train emi...
63
64
65
66
        elif use_plane:
            footprint += self.compute_airplane_footprint(
                distance=great_circle_distance
            )
2d6b5394   Antoine Goutenoir   Add an example of...
67

5634c975   Antoine Goutenoir   Expose travel dis...
68
        # II.a Double it up since it's a round-trip
1bbf5d52   Antoine Goutenoir   Implement the sca...
69
        footprint *= 2.0
5634c975   Antoine Goutenoir   Expose travel dis...
70
        distance *= 2.0
1bbf5d52   Antoine Goutenoir   Implement the sca...
71

5634c975   Antoine Goutenoir   Expose travel dis...
72
        return {
b935618e   Antoine Goutenoir   Count the number ...
73
            'distance_km': distance,
5634c975   Antoine Goutenoir   Expose travel dis...
74
            'co2eq_kg': footprint,
4276f1aa   Antoine Goutenoir   Support train emi...
75
76
            'train_trips': 1 if use_train else 0,  # amount of round trips
            'plane_trips': 1 if use_plane else 0,  # amount of round trips
5634c975   Antoine Goutenoir   Expose travel dis...
77
        }
2d6b5394   Antoine Goutenoir   Add an example of...
78

4276f1aa   Antoine Goutenoir   Support train emi...
79
    def compute_train_footprint(self, distance):
a03065f3   Antoine Goutenoir   Squeeze in anothe...
80
81
        gcd_to_road_correction = self.shared_config.gcd_to_road_scale
        train_emission = self.shared_config.train_emission  # kg/km
0c5ac2ae   Antoine Goutenoir   Actually use the ...
82
        return gcd_to_road_correction * distance * train_emission
4276f1aa   Antoine Goutenoir   Support train emi...
83

2d6b5394   Antoine Goutenoir   Add an example of...
84
85
    def compute_airplane_footprint(
            self,
5634c975   Antoine Goutenoir   Expose travel dis...
86
            distance
1bbf5d52   Antoine Goutenoir   Implement the sca...
87
88
    ):
        config = self.config.plane_emission_linear_fit
2d6b5394   Antoine Goutenoir   Add an example of...
89

8c2482e3   Antoine Goutenoir   Grunt! Grunt! GRUNT.
90
91
        distance = config.connecting_flights_scale * distance

4c745e05   Antoine Goutenoir   Rewire the model ...
92
        footprint = self.compute_airplane_distance_footprint(distance, config)
1bbf5d52   Antoine Goutenoir   Implement the sca...
93

4c745e05   Antoine Goutenoir   Rewire the model ...
94
        return footprint
1bbf5d52   Antoine Goutenoir   Implement the sca...
95

4c745e05   Antoine Goutenoir   Rewire the model ...
96
    def compute_airplane_distance_footprint(self, distance, config=None):
f2301557   Antoine Goutenoir   Use kilometers.
97
98
99
100
101
        """
        :param distance: in km
        :param config:
        :return:
        """
4c745e05   Antoine Goutenoir   Rewire the model ...
102
103
104
105
        if config is None:
            config = self.config.plane_emission_linear_fit
        distance = distance * config.scale_before + config.offset_before
        footprint = self.apply_scaling_law(distance, config)
18ba3d7a   Antoine Goutenoir   Boost the models'...
106
        # We can totally ignore RFI in config by commenting the line below
8c2482e3   Antoine Goutenoir   Grunt! Grunt! GRUNT.
107
108
        footprint = self.adjust_footprint_for_rfi(footprint, config)

1bbf5d52   Antoine Goutenoir   Implement the sca...
109
110
        return footprint

8c2482e3   Antoine Goutenoir   Grunt! Grunt! GRUNT.
111
    def adjust_footprint_for_rfi(self, footprint, config):
8c2482e3   Antoine Goutenoir   Grunt! Grunt! GRUNT.
112
113
        return config.rfi * footprint

c20659f0   Antoine Goutenoir   Freshen up the AP...
114
    def apply_scaling_law(self, distance, config):
f2301557   Antoine Goutenoir   Use kilometers.
115
116
117
        """
        :param distance: in km
        :param config:
5634c975   Antoine Goutenoir   Expose travel dis...
118
        :return: float
f2301557   Antoine Goutenoir   Use kilometers.
119
        """
1bbf5d52   Antoine Goutenoir   Implement the sca...
120
        footprint = distance
c20659f0   Antoine Goutenoir   Freshen up the AP...
121
        for interval in config.intervals:
1bbf5d52   Antoine Goutenoir   Implement the sca...
122
123
124
125
126
            if interval.dmin <= distance < interval.dmax:
                offset = interval.offset if interval.offset else 0
                scale = interval.scale if interval.scale else 1
                footprint = footprint * scale + offset
                break
2d6b5394   Antoine Goutenoir   Add an example of...
127
128
129
130
131

        return footprint

    def get_distance_between(
            self,
1bbf5d52   Antoine Goutenoir   Implement the sca...
132
133
134
135
136
            origin_latitude,
            origin_longitude,
            destination_latitude,
            destination_longitude
    ):
2d6b5394   Antoine Goutenoir   Add an example of...
137
138
139
140
141
        """
        :param origin_latitude:
        :param origin_longitude:
        :param destination_latitude:
        :param destination_longitude:
f2301557   Antoine Goutenoir   Use kilometers.
142
        :return: Distance in kilometers between the two locations,
2d6b5394   Antoine Goutenoir   Add an example of...
143
144
                 along Earth's great circles.
        """
f2301557   Antoine Goutenoir   Use kilometers.
145
        return great_circle(
2d6b5394   Antoine Goutenoir   Add an example of...
146
147
            (np.float(origin_latitude), np.float(origin_longitude)),
            (np.float(destination_latitude), np.float(destination_longitude))
f2301557   Antoine Goutenoir   Use kilometers.
148
        ).kilometers