Blame view

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


5634c975   Antoine Goutenoir   Expose travel dis...
5
# @abc
8c2482e3   Antoine Goutenoir   Grunt! Grunt! GRUNT.
6
class BaseEmissionModel():
1bbf5d52   Antoine Goutenoir   Implement the sca...
7
8
9
    def __init__(self, config):  # Constructor
        self.name = config.name
        self.slug = config.slug
4c862b54   Antoine Goutenoir   Add a grouped bar...
10
        self.color = config.color
1bbf5d52   Antoine Goutenoir   Implement the sca...
11
        self.config = config.config
2d6b5394   Antoine Goutenoir   Add an example of...
12

1bbf5d52   Antoine Goutenoir   Implement the sca...
13
    def __repr__(self):  # Cast to String
2d6b5394   Antoine Goutenoir   Add an example of...
14
15
        return "Emission model\n" + \
               "==============\n" + \
1bbf5d52   Antoine Goutenoir   Implement the sca...
16
               "%s (%s)" % (self.name, self.slug) + \
2d6b5394   Antoine Goutenoir   Add an example of...
17
18
               repr(self.config)

8c2482e3   Antoine Goutenoir   Grunt! Grunt! GRUNT.
19
20
21
22
23
    # def compute_travel_footprint()


class EmissionModel(BaseEmissionModel):
    # @abc
2d6b5394   Antoine Goutenoir   Add an example of...
24
25
    def compute_travel_footprint(
            self,
1bbf5d52   Antoine Goutenoir   Implement the sca...
26
27
28
29
30
31
32
            origin_latitude,        # degrees
            origin_longitude,       # degrees
            destination_latitude,   # degrees
            destination_longitude,  # degrees
            prefer_train_under_distance=0,  # meters
    ):
        footprint = 0.0
5634c975   Antoine Goutenoir   Expose travel dis...
33
        distance = 0.0
2d6b5394   Antoine Goutenoir   Add an example of...
34
35

        #############################################
5634c975   Antoine Goutenoir   Expose travel dis...
36
        # TODO: find closest airport(s) and pick one?
2d6b5394   Antoine Goutenoir   Add an example of...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        # 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
        #############################################
        #############################################

1bbf5d52   Antoine Goutenoir   Implement the sca...
56
57
58
59
        # I.a Train travel footprint
        # ... TODO

        # I.b Airplane travel footprint
5634c975   Antoine Goutenoir   Expose travel dis...
60
        great_circle_distance = self.get_distance_between(
1bbf5d52   Antoine Goutenoir   Implement the sca...
61
62
63
64
            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...
65
        )
5634c975   Antoine Goutenoir   Expose travel dis...
66
67
68
69
        footprint += self.compute_airplane_footprint(
            distance=great_circle_distance
        )
        distance += great_circle_distance
2d6b5394   Antoine Goutenoir   Add an example of...
70

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

5634c975   Antoine Goutenoir   Expose travel dis...
75
76
77
78
79
        return {
            'distance': distance,
            'co2eq_kg': footprint,
        }
        # return footprint
2d6b5394   Antoine Goutenoir   Add an example of...
80
81
82

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

8c2482e3   Antoine Goutenoir   Grunt! Grunt! GRUNT.
87
88
        distance = config.connecting_flights_scale * distance

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

4c745e05   Antoine Goutenoir   Rewire the model ...
91
        return footprint
1bbf5d52   Antoine Goutenoir   Implement the sca...
92

4c745e05   Antoine Goutenoir   Rewire the model ...
93
    def compute_airplane_distance_footprint(self, distance, config=None):
f2301557   Antoine Goutenoir   Use kilometers.
94
95
96
97
98
        """
        :param distance: in km
        :param config:
        :return:
        """
4c745e05   Antoine Goutenoir   Rewire the model ...
99
100
101
102
        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)
8c2482e3   Antoine Goutenoir   Grunt! Grunt! GRUNT.
103
104
        footprint = self.adjust_footprint_for_rfi(footprint, config)

1bbf5d52   Antoine Goutenoir   Implement the sca...
105
106
        return footprint

8c2482e3   Antoine Goutenoir   Grunt! Grunt! GRUNT.
107
108
109
110
    def adjust_footprint_for_rfi(self, footprint, config):
        # Todo: grab data from config merged with form input?
        return config.rfi * footprint

c20659f0   Antoine Goutenoir   Freshen up the AP...
111
    def apply_scaling_law(self, distance, config):
f2301557   Antoine Goutenoir   Use kilometers.
112
113
114
        """
        :param distance: in km
        :param config:
5634c975   Antoine Goutenoir   Expose travel dis...
115
        :return: float
f2301557   Antoine Goutenoir   Use kilometers.
116
        """
1bbf5d52   Antoine Goutenoir   Implement the sca...
117
        footprint = distance
c20659f0   Antoine Goutenoir   Freshen up the AP...
118
        for interval in config.intervals:
1bbf5d52   Antoine Goutenoir   Implement the sca...
119
120
121
122
123
            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...
124
125
126
127
128

        return footprint

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