def emissions_comparison(process_kwh, locations, year): # TODO: Disambiguation of states such as Georgia, US and Georgia intl_data = utils.get_data("data/json/energy-mix-intl_" + year + ".json") us_data = utils.get_data("data/json/us-emissions_" + year + ".json") emissions = [] # list of tuples w/ format (location, emission) for location in locations: if locate.in_US(location): emission = convert.lbs_to_kgs(us_data[location] * convert.to_MWh(process_kwh)) emissions.append((location, emission)) else: c = intl_data[location] total, breakdown = c['total'], [c['coal'], c['petroleum'], \ c['naturalGas'], c['lowCarbon']] if isinstance(total, float) and float(total) > 0: breakdown = list(map(lambda x: 100 * x / total, breakdown)) coal, petroleum, natural_gas, low_carbon = breakdown breakdown = [ convert.coal_to_carbon(process_kwh * coal / 100), convert.petroleum_to_carbon(process_kwh * petroleum / 100), convert.natural_gas_to_carbon(process_kwh * natural_gas / 100), 0 ] emission = sum(breakdown) emissions.append((location, emission)) if emissions != []: utils.log('Emissions Comparison', emissions) return emissions
def emissions(process_kwh, breakdown, location, year): """ Calculates the CO2 emitted by the program based on the location Parameters: process_kwh (int): kWhs used by the process breakdown (list): energy mix corresponding to user's location location (str): location of user Returns: emission (float): kilograms of CO2 emitted state_emission (float): lbs CO2 per MWh; 0 if international location """ if process_kwh < 0: raise OSError( "Process wattage lower than baseline wattage. Do not run other processes" " during the evaluation, or try evaluating a more resource-intensive process." ) utils.log("Energy Data", breakdown, location) state_emission = 0 # Case 1: Unknown location, default to US data # Case 2: United States location if locate.in_US(location): if location == "Unknown": location = "United States" # US Emissions data is in lbs/Mwh data = utils.get_data("data/json/us-emissions_" + year + ".json") state_emission = data[location] emission = convert.lbs_to_kgs(state_emission * convert.to_MWh(process_kwh)) # Case 3: International location else: # Breaking down energy mix coal, petroleum, natural_gas, low_carbon = breakdown breakdown = [ convert.coal_to_carbon(process_kwh * coal / 100), convert.petroleum_to_carbon(process_kwh * petroleum / 100), convert.natural_gas_to_carbon(process_kwh * natural_gas / 100), 0 ] emission = sum(breakdown) utils.log("Emissions", emission) return (emission, state_emission)
def energy_mix(location, location_of_default): """ Gets the energy mix information for a specific location Parameters: location (str): user's location location_of_default (str): Specifies which average to use if location cannot be determined Returns: breakdown (list): percentages of each energy type """ if location == "Unknown": if location_of_default == "USAverage": location = "United States" elif location_of_default == "EuropeAverage": location = "Europe" else: location = "World" if locate.in_US(location): # Default to U.S. average for unknown location data = utils.get_data("data/json/energy-mix-us.json") s = data[location]['mix'] # get state coal, oil, gas = s['coal'], s['oil'], s['gas'] nuclear, hydro, biomass, wind, solar, geo, = \ s['nuclear'], s['hydro'], s['biomass'], s['wind'], \ s['solar'], s['geothermal'] low_carbon = sum([nuclear, hydro, biomass, wind, solar, geo]) breakdown = [coal, oil, gas, low_carbon] return breakdown # list of % of each else: data = utils.get_data('data/json/energy-mix-intl.json') c = data[location] # get country total, breakdown = c['total'], [c['coal'], c['petroleum'], \ c['naturalGas'], c['lowCarbon']] # Get percentages breakdown = list(map(lambda x: 100 * x / total, breakdown)) return breakdown # list of % of each
def log(*args): if (re.search("Package|CPU.*|GPU|DRAM", args[0])): measurement = args[1] sys.stdout.write("\r{:<24} {:>49.2f} {:5<}".format( args[0] + ":", measurement, "watts")) if args[0] == "Baseline wattage": measurement = args[1] sys.stdout.write("\r{:<24} {:>49.2f} {:5<}".format( args[0] + ":", measurement, "watts")) elif args[0] == "Process wattage": measurement = args[1] sys.stdout.write("\r{:<17} {:>56.2f} {:5<}".format( args[0] + ":", measurement, "watts")) elif args[0] == "Final Readings": newline() baseline_average, process_average, difference_average, timedelta = args[ 1], args[2], args[3], args[4] delete_last_lines() log_header(args[0]) sys.stdout.write("{:<25} {:>48.2f} {:5<}\n".format( "Average baseline wattage:", baseline_average, "watts")) sys.stdout.write("{:<25} {:>48.2f} {:5<}\n".format( "Average total wattage:", process_average, "watts")) sys.stdout.write("{:<25} {:>48.2f} {:5<}\n".format( "Average process wattage:", difference_average, "watts")) sys.stdout.write("{:<17} {:>62}\n".format("Process duration:", timedelta)) elif args[0] == "Energy Data": location = args[2] log_header('Energy Data') if location == "Unknown" or locate.in_US(location): coal, oil, gas, low_carbon = args[1] if location == "Unknown": location = "United States" sys.stdout.write( "{:^80}\n{:<13}{:>66.2f}%\n{:<13}{:>66.2f}%\n{:<13}{:>66.2f}%\n" "{:<13}{:>66.2f}%\n".format( "Location unknown, default energy mix in " + location + ":", "Coal:", coal, "Oil:", oil, "Natural Gas:", gas, "Low Carbon:", low_carbon)) elif locate.in_US(location): sys.stdout.write( "{:^80}\n{:<13}{:>66.2f}%\n{:<13}{:>66.2f}%\n{:<13}{:>66.2f}%\n" "{:<13}{:>66.2f}%\n".format("Energy mix in " + location, "Coal:", coal, "Oil:", oil, "Natural Gas:", gas, "Low Carbon:", low_carbon)) else: coal, natural_gas, petroleum, low_carbon = args[1] sys.stdout.write( "{:^80}\n{:<13}{:>66.2f}%\n{:<13}{:>66.2f}%\n{:<13}{:>66.2f}%\n" "{:<13}{:>66.2f}%\n".format("Energy mix in " + location, "Coal:", coal, "Petroleum:", petroleum, "Natural Gas:", natural_gas, "Low Carbon:", low_carbon)) elif args[0] == "Emissions": emission = args[1] log_header('Emissions') sys.stdout.write("{:<19}{:>54.2e} kg CO2\n".format("Effective emission:", \ emission)) sys.stdout.write("{:<24}{:>50.2e} miles\n".format("Equivalent miles driven:", \ convert.carbon_to_miles(emission))) sys.stdout.write("{:<45}{:>27.2e} minutes\n".format("Equivalent minutes of 32-inch LCD TV watched:", \ convert.carbon_to_tv(emission))) sys.stdout.write("{:<45}{:>34.2e}%\n".format( "Percentage of CO2 used in a US" " household/day:", convert.carbon_to_home(emission))) elif args[0] == "Assumed Carbon Equivalencies": log_header('Assumed Carbon Equivalencies') sys.stdout.write("{:<14} {:>65}\n".format("Coal:", "995.725971 kg CO2/MWh")) sys.stdout.write("{:<14} {:>65}\n".format("Petroleum:", "816.6885263 kg CO2/MWh")) sys.stdout.write("{:<14} {:>65}\n".format("Natural gas:", "743.8415916 kg CO2/MWh")) sys.stdout.write("{:<14} {:>65}\n".format("Low carbon:", "0 kg CO2/MWh")) elif args[0] == "Emissions Comparison": log_header('Emissions Comparison') emissions = args[1] for location, emission in emissions: sys.stdout.write("{:<19}{:>54.2e} kg CO2\n".format( location + ":", emission)) #OLD VERSION: US, EU, Rest comparison elif args[0] == "Emissions Comparison default": log_header('Emissions Comparison') (max_global, median_global, min_global, max_europe, median_europe, min_europe, max_us, median_us, min_us) = args[1:] sys.stdout.write( "{:^80}\n".format("Quantities below expressed in kg CO2")) sys.stdout.write("{:8}{:<23} {:<23} {:<22}\n".format("", "US", "Europe", \ "Global minus US/Europe")) sys.stdout.write("{:<7} {:<13}{:>10.2e} {:<13}{:>10.2e} {:<14}{:>10.2e}\n".format("Max:", max_us[0], max_us[1], \ max_europe[0], max_europe[1], max_global[0], max_global[1])) sys.stdout.write("{:<7} {:<13}{:>10.2e} {:<13}{:>10.2e} {:<14}{:>10.2e}\n".format("Median:", median_us[0], median_us[1], \ median_europe[0], median_europe[1], median_global[0], median_global[1])) sys.stdout.write("{:<7} {:<13}{:>10.2e} {:<13}{:>10.2e} {:<14}{:>10.2e}\n".format("Min:", min_us[0], min_us[1], \ min_europe[0], min_europe[1], min_global[0], min_global[1])) elif args[0] == "Process Energy": energy = args[1] sys.stdout.write("-" * 80 + "\n" + "-" * 80 + "\n") sys.stdout.write("{:<13} {:51} {:>10.2e} {:>3}\n".format( "Process used:", "", energy, "kWh")) else: sys.stdout.write(args[0])