conference_footprint

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

co2.py (2631B)


      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']:
     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 == "plane":
     52         if distance <= 1000000:
     53             g_km_person = 258
     54         elif 1000000 < distance <= 3500000:
     55             g_km_person = 187
     56         elif 3500000 < distance:
     57             g_km_person = 152
     58     co2 = (g_km_person * (distance / 1000.))/1000.
     59     co2_by_type[mode] += co2
     60     total_co2 += co2
     61     print (','.join([str(distance), mode, str(co2)]))
     62 
     63 
     64 ## OUTPUT GEOJSON
     65 
     66 features = []
     67 for k in places.keys():
     68     red = int(255.*places[k][0]/places[k][1])
     69     green = 0
     70     blue = int(255.*(places[k][1]-places[k][0])/places[k][1])
     71     color = '#%02X%02X%02X' % (red, green, blue)
     72     feature = {
     73       "type": "Feature",
     74       "properties": {
     75           #"name":places[k][2],
     76           "_umap_options": {"color": color}
     77       },
     78       "geometry": {
     79         "type": "Point",
     80         "coordinates": [
     81             k[1], k[0]
     82         ]
     83       }
     84     }
     85     features.append(feature)
     86 
     87 output = {
     88   "type": "FeatureCollection",
     89   "features": features
     90 }
     91 
     92 with open("map.geojson", 'w') as f:
     93     print (json.dumps(output), file=f)
     94 
     95 print("%d total trips" % n_trips, file=sys.stderr)
     96 print("%d total distance" % total_dist, file=sys.stderr)
     97 print("%f total CO2" % total_co2, file=sys.stderr)
     98 for k in trips_by_type:
     99     print("%d trips by %s" % (trips_by_type[k], k), file=sys.stderr)
    100 for k in dist_by_type:
    101     print("%d distance by %s" % (dist_by_type[k], k), file=sys.stderr)
    102 for k in co2_by_type:
    103     print("%f kgCO2e by %s" % (co2_by_type[k], k), file=sys.stderr)
    104