conference_footprint

compute the CO2 footprint of an academic conference
git clone https://a3nm.net/git/conference_footprint/
Log | Files | Refs

co2.py (2686B)


      1 #!/usr/bin/env python3
      2 
      3 # output: two fields, distance, inferred mode, CO2e emission in kg
      4 # also output a map (map.geojson)
      5 
      6 import json
      7 
      8 import sys
      9 from collections import defaultdict
     10 
     11 places = defaultdict(lambda: (0, 0, None))
     12 
     13 n_trips = 0
     14 total_dist = 0
     15 total_co2 = 0
     16 trips_by_type = defaultdict(lambda : 0)
     17 dist_by_type = defaultdict(lambda : 0)
     18 co2_by_type = defaultdict(lambda : 0)
     19 
     20 
     21 for l in sys.stdin.readlines():
     22     f = l.strip().split(',')
     23     mode = f[0]
     24     lat = f[1]
     25     lon = f[2]
     26 
     27     distance = float(f[3])
     28 
     29     if mode.strip() not in ['plane', 'train', 'bus/coach', 'car']:
     30         if distance > 400000:
     31             mode = "plane"
     32             trips_by_type['plane_assumed'] += 1
     33         else:
     34             mode = "train"
     35             trips_by_type['train_assumed'] += 1
     36     else:
     37         trips_by_type[mode] += 1
     38 
     39     k = (lat,lon)
     40     plane = mode == "plane"
     41     places[k] = (places[k][0] + (1 if plane else 0), places[k][1] + 1)
     42 
     43     dist_by_type[mode] += distance
     44     n_trips += 1
     45     total_dist += distance
     46     g_km_person = None
     47     if mode == "train":
     48         g_km_person = 37
     49     if mode == "bus/coach":
     50         g_km_person = 28
     51     if mode == "car":
     52         g_km_person = 192
     53     if mode == "plane":
     54         if distance <= 1000000:
     55             g_km_person = 258
     56         elif 1000000 < distance <= 3500000:
     57             g_km_person = 187
     58         elif 3500000 < distance:
     59             g_km_person = 152
     60     co2 = (g_km_person * (distance / 1000.))/1000.
     61     co2_by_type[mode] += co2
     62     total_co2 += co2
     63     print (','.join([str(distance), mode, str(co2)]))
     64 
     65 
     66 ## OUTPUT GEOJSON
     67 
     68 features = []
     69 for k in places.keys():
     70     red = int(255.*places[k][0]/places[k][1])
     71     green = 0
     72     blue = int(255.*(places[k][1]-places[k][0])/places[k][1])
     73     color = '#%02X%02X%02X' % (red, green, blue)
     74     feature = {
     75       "type": "Feature",
     76       "properties": {
     77           #"name":places[k][2],
     78           "_umap_options": {"color": color}
     79       },
     80       "geometry": {
     81         "type": "Point",
     82         "coordinates": [
     83             k[1], k[0]
     84         ]
     85       }
     86     }
     87     features.append(feature)
     88 
     89 output = {
     90   "type": "FeatureCollection",
     91   "features": features
     92 }
     93 
     94 with open("map.geojson", 'w') as f:
     95     print (json.dumps(output), file=f)
     96 
     97 print("%d total trips" % n_trips, file=sys.stderr)
     98 print("%d total distance" % total_dist, file=sys.stderr)
     99 print("%f total CO2" % total_co2, file=sys.stderr)
    100 for k in trips_by_type:
    101     print("%d trips by %s" % (trips_by_type[k], k), file=sys.stderr)
    102 for k in dist_by_type:
    103     print("%d distance by %s" % (dist_by_type[k], k), file=sys.stderr)
    104 for k in co2_by_type:
    105     print("%f kgCO2e by %s" % (co2_by_type[k], k), file=sys.stderr)
    106