CS 100

Logo

Scientific Computing

Activity 23 - Carbon 👣

  1. Download and save the file co2-by-nation.csv to your cs100/ch5 folder. Be sure you use the same name for the file.

  2. Create a new file called carbon-emissions.py and save it to your cs100/ch5 folder. It is important that this file is in the same folder as the co2-by-nation.csv file you downloaded.

  3. Copy and paste the following code to your python file.
    # data from https://datahub.io/core/co2-fossil-by-nation#data
    file = open("co2-by-nation.csv", "r")
       
    # column number corresponding to different data fields
    IDX_YEAR = 0          # Year
    IDX_COUNTRY = 1       # string - Nation
    IDX_TOTAL_FUELS = 2   # Total carbon emissions from fossil fuel consumption and cement production (million metric tons of C)
    IDX_SOLID_FUEL = 3    # Carbon emissions from solid fuel consumption
    IDX_LIQUID_FUEL = 4   # Carbon emissions from liquid fuel consumption
    IDX_GAS_FUEL = 5      # Carbon emissions from gas fuel consumption
    IDX_CEMENT = 6        # Carbon emissions from cement production
    IDX_GAS_FLARING = 7   # Carbon emissions from gas flaring
    IDX_PER_CAPITY = 8    # Per capita carbon emissions (metric tons of carbon; after 1949 only)
    IDX_BUNKER_FUELS = 9  # Carbon emissions from bunker fuels (not included in total)
       
    perYear = {}
       
    file.readline() # first line is header columns
    for line in file:
        lst = line.split(",")
           
        year = lst[IDX_YEAR]
        total = int(lst[IDX_TOTAL_FUELS])
           
        if year in perYear:
            perYear[year] += total
        else:
            perYear[year] = total
       
    for yr in range(1950, 2015):
        print("%u: %s million metric tons of C" % (yr, perYear[str(yr)]))
       
    file.close()
    
  4. Read through each line to understand what the program is doing and how it works.

  5. Add python code to determine the top 5 countries with the most carbon emissions. What are they? How many million metric tons of C are emitted? Print the results so that it is the last thing which appears in the shell/terminal when your program is run.

  6. Which country has the most carbon emissions from cement production?

If you finish early

  1. What other information can you determine from this data? Explore a bit! For example, which year had the most emissions for liquid fuel consumption? What other questions can you answer? Print your results.

  2. Find another data set related to an area of interested! Here are a bunch - be sure to look for csv files (Comma Separated Values) or tsv files (Tab Separated Values).

How to submit

Submit your working python file carbon-emissions.py to Moodle.