travel_emission_linear_fit.py
4.37 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import numpy as np
from geopy.distance import great_circle
class BaseEmissionModel():
def __init__(self, config): # Constructor
self.name = config.name
self.slug = config.slug
self.color = config.color
self.config = config.config
def __repr__(self): # Cast to String
return "Emission model\n" + \
"==============\n" + \
"%s (%s)" % (self.name, self.slug) + \
repr(self.config)
# def compute_travel_footprint()
class EmissionModel(BaseEmissionModel):
# @abc
def compute_travel_footprint(
self,
origin_latitude, # degrees
origin_longitude, # degrees
destination_latitude, # degrees
destination_longitude, # degrees
prefer_train_under_distance=0, # meters
):
footprint = 0.0
#############################################
# TODO: find closest airport(s) and pick one
# 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
#############################################
#############################################
# I.a Train travel footprint
# ... TODO
# I.b Airplane travel footprint
footprint += self.compute_airplane_footprint(
origin_latitude=origin_airport.latitude,
origin_longitude=origin_airport.longitude,
destination_latitude=destination_airport.latitude,
destination_longitude=destination_airport.longitude,
)
# II.a Double the footprint if it's a round-trip
footprint *= 2.0
return footprint
def compute_airplane_footprint(
self,
origin_latitude,
origin_longitude,
destination_latitude,
destination_longitude
):
config = self.config.plane_emission_linear_fit
great_circle_distance = self.get_distance_between(
origin_latitude, origin_longitude,
destination_latitude, destination_longitude
)
distance = great_circle_distance
distance = config.connecting_flights_scale * distance
footprint = self.compute_airplane_distance_footprint(distance, config)
return footprint
def compute_airplane_distance_footprint(self, distance, config=None):
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)
footprint = self.adjust_footprint_for_rfi(footprint, config)
return footprint
def adjust_footprint_for_rfi(self, footprint, config):
# Todo: grab data from config merged with form input?
return config.rfi * footprint
def apply_scaling_law(self, distance, config):
footprint = distance
for interval in config.intervals:
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
return footprint
def get_distance_between(
self,
origin_latitude,
origin_longitude,
destination_latitude,
destination_longitude
):
"""
:param origin_latitude:
:param origin_longitude:
:param destination_latitude:
:param destination_longitude:
:return: Distance in meters between the two locations,
along Earth's great circles.
"""
gcd = great_circle(
(np.float(origin_latitude), np.float(origin_longitude)),
(np.float(destination_latitude), np.float(destination_longitude))
).m
return gcd