conference_footprint

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

co2.py (2571B)


      1 #!/usr/bin/env python3
      2 
      3 # From a list of trip legs (mode, distance_in_km), compute the total CO2
      4 # footprint and statistics
      5 
      6 import sys
      7 from collections import defaultdict
      8 
      9 LONG_THRESH = 2000
     10 
     11 def co2(distance, mode):
     12     if mode == "train":
     13         g_km_person = 37
     14     if mode == "bus/coach":
     15         g_km_person = 28
     16     if mode == "plane":
     17         if distance <= 1000:
     18             g_km_person = 258
     19         elif 1000< distance <= 3500:
     20             g_km_person = 187
     21         elif 3500< distance:
     22             g_km_person = 152
     23     return distance * g_km_person
     24 
     25 co2_by_mode = defaultdict(lambda: 0)
     26 co2_total = 0
     27 co2_total_long_plane = 0
     28 num_total_long_plane = 0
     29 num_total_long_nonplane = 0
     30 num_total = 0
     31 num_short_plane = 0
     32 num_short = 0
     33 dist_plane = 0
     34 km_by_mode = defaultdict(lambda: 0)
     35 num_by_mode = defaultdict(lambda: 0)
     36 
     37 with open("footprints.txt", 'w') as fp:
     38     for l in sys.stdin.readlines():
     39         f = l.strip().split(",")
     40         mode = f[0]
     41         dist = float(f[1])
     42         co2v = co2(dist, mode)
     43         co2_by_mode[mode] += co2v
     44         print(co2v, file=fp)
     45         co2_total += co2v
     46         num_total += 1
     47         num_by_mode[mode] += 1
     48         km_by_mode[mode] += dist
     49         if mode == "plane":
     50             dist_plane += dist
     51         if dist >= LONG_THRESH:
     52             if mode == "plane":
     53                 co2_total_long_plane += co2v
     54                 num_total_long_plane += 1
     55             else:
     56                 num_total_long_nonplane += 1
     57         if dist < LONG_THRESH:
     58             num_short += 1
     59             if mode == "plane":
     60                 num_short_plane += 1
     61 
     62 assert (num_short + num_total_long_plane + num_total_long_nonplane  == num_total)
     63 
     64 print("total CO2e emissions (tons): %f" % (co2_total/1000000))
     65 for m in co2_by_mode.keys():
     66     print("for mode %s: CO2e emissions (tons): %f" % (m, co2_by_mode[m]/1000000))
     67 
     68 print ("for distances <%d km, plane is used for %d/%d trips" %
     69        (LONG_THRESH, num_short_plane, num_short))
     70 print ("for distances >=%d km, plane is used for %d/%d trips" %
     71        (LONG_THRESH, num_total_long_plane, num_total_long_plane+num_total_long_nonplane))
     72 
     73 print( "flights of over %d km account for %f CO2e emissions (tons) i.e. %f percent of total for %d/%d total legs"
     74         % (LONG_THRESH, co2_total_long_plane, 100*co2_total_long_plane/co2_total,
     75            num_total_long_plane, num_total))
     76 print("distance by plane: %d" % dist_plane)
     77 
     78 for k in num_by_mode.keys():
     79     print("num by mode %s: %d" % (k, num_by_mode[k]))
     80 for k in km_by_mode.keys():
     81     print("dist by mode %s: %d" % (k, km_by_mode[k]))