-
Notifications
You must be signed in to change notification settings - Fork 16
/
carbon_flight.py
170 lines (105 loc) · 5.5 KB
/
carbon_flight.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"carbon-flight.py calculates the emissions of passengers taking a flight"
from math import radians, cos, sin, asin, sqrt
try:
from carbon import Carbon
except ImportError:
from results.carbon_calculator_git.carbon import Carbon #complete path inside Django project needed to make it work in Django Framework
class CarbonFlight(Carbon):
"""Class to calculate CO2 emmissions in flights."""
def __init__(self):
self.domestic_flight_max_km = 400
self.short_haul_max_km = 3700
self.detour_constant = 95 # km
def calculate_co2(self, dist_km, pax_class, trip_type):
"""Calculate the CO2 eq emission of a flight."""
flight_co2_dict = self.flight_co2_dict_from_json()
if dist_km < self.domestic_flight_max_km:
gr_co2 = flight_co2_dict['domestic']['avg']
elif dist_km < self.short_haul_max_km:
if pax_class == 'business-class':
gr_co2 = flight_co2_dict['shortHaul']['businessClass']
else:
gr_co2 = flight_co2_dict['shortHaul']['economyClass']
else:
if pax_class == 'business-class':
gr_co2 = flight_co2_dict['longHaul']['businessClass']
else:
gr_co2 = flight_co2_dict['longHaul']['economyClass']
gr_co2_person = gr_co2 * dist_km
if trip_type == "round-trip":
gr_co2_person = gr_co2_person*2
return int(gr_co2_person)
def calculate_co2_from_airports(self, iata_orig, iata_dest, pax_class, trip_type):
"""Calculate the CO2 eq emission of a ride by plane, given the airport iata codes as input."""
dist_km = self.real_distance(iata_orig, iata_dest)
gr_co2_person = self.calculate_co2(dist_km, pax_class, trip_type)
return int(gr_co2_person)
def calculate_co2_from_duration(self, duration_in_minutes, pax_class, trip_type):
"""Calculate the CO2 eq emission of a ride by plane, given a duration as input."""
dist_km = self.estimate_flight_distance_from_duration(duration_in_minutes)
gr_co2_person = self.calculate_co2(dist_km, pax_class, trip_type)
return int(gr_co2_person)
def flight_co2_dict_from_json(self):
"""Return a dictionary with the data for flights from a given source."""
relative_path = '/sources/gov_uk.json'
gov_uk_dict = self.dict_from_json(relative_path)
co2_dict = gov_uk_dict["gCO2"]["flight"]
return co2_dict
def estimate_flight_distance_from_duration(self, duration_in_minutes):
"""Given duration as input, estimate distance covered."""
dist_km = self.average_speed_from_duration(duration_in_minutes) * duration_in_minutes/60
return int(dist_km)
def average_speed_from_duration(self, duration_in_minutes):
"""
Taken from NorthApp https://github.com/tmrowco/northapp-contrib/blob/807646968a2878f30820197b2f3c73a7dfc59db2/co2eq/flights/index.js#L137
which adapted it from https://airasia.listedcompany.com/images/ir-speed-length_7.gif
"""
hour = duration_in_minutes/60
if hour < 3.3:
avg_speed = 14.1 + 495 * hour - 110 * hour * hour + 9.85 * hour * hour * hour - 0.309 * hour * hour * hour * hour
else:
avg_speed = 770
return int(avg_speed)
def real_distance(self, iata_orig, iata_dest):
"""Calculate real distance between airports in km, given two airport as iata codes (3-digit code)."""
relative_path = '/sources/airports.json'
airports_json = self.dict_from_json(relative_path)
orig_airport_dict = airports_json[iata_orig]
dest_airport_dict = airports_json[iata_dest]
dist_m = self.haversine(lat1=self.parse_airport_lat(orig_airport_dict), lon1=self.parse_airport_lon(orig_airport_dict), lat2=self.parse_airport_lat(dest_airport_dict), lon2=self.parse_airport_lon(dest_airport_dict))
dist_km = dist_m//1000 + self.detour_constant
print('Real distance:', dist_km, 'km')
return dist_km
def haversine(self, lat1, lon1, lat2, lon2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
Source: http://stackoverflow.com/a/15737218/5802289
"""
#convert input to floats
lat1 = float(lat1)
lon1 = float(lon1)
lat2 = float(lat2)
lon2 = float(lon2)
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
meters = int(km*1000)
return meters
def parse_airport_lat(self, airport_dict):
"""Return latitude of airport, given as argument an airport dict from the json file."""
airport_coordinates = self.parse_airport_coordinates(airport_dict)
return airport_coordinates[1]
def parse_airport_lon(self, airport_dict):
"""Return latitude of airport, given as argument an airport dict from the json file."""
airport_coordinates = self.parse_airport_coordinates(airport_dict)
return airport_coordinates[0]
def parse_airport_coordinates(self, airport_dict):
"""Return latitude of airport, given as argument an airport dict from the json file."""
airport_coordinates = airport_dict['lonlat']
return airport_coordinates