-
Notifications
You must be signed in to change notification settings - Fork 0
/
fingrid_api_experiment.py
136 lines (106 loc) · 4.36 KB
/
fingrid_api_experiment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
This program calculates the CO2 emission based on the data provided by
Fingrid and Energy Star.
"""
import requests
import json
import datetime
"""
Get the data from the API Link
:param link: str,
The link of the API
:param header: dict,
The header of the API link
:return json:
The JSON of the data
"""
def get_data(link, header):
response = requests.get(link, headers=header)
json = response.json()
return json
"""
Find the appropriate data from Energy Star according to the model number
:param model_number: str,
The model number of the computer
:return json:
The JSON of the data
"""
def find_model_number(model_number):
# Create the API link
link = "https://data.energystar.gov/resource/j7nq-iepp.json"
filtered_model_link = f"{link}?model_number={model_number}"
# Get the data
header = {'x-api-key': '50ggs7j2zhy8zcsf0nzghec44'}
json = get_data(filtered_model_link, header)
return json
"""
Decide the category of the computer according to Energy Star and return the
corresponding typical energy consumption (TEC)
:param energy_star_data_json:
The JSON of the data of the product that has been found with the model number
:return tec: float,
The typical energy consumption of the product
"""
def tec_of_product(energy_star_data_json):
# Decide the category of the computer according to Energy Star
category = energy_star_data_json[0]['comp_cat_for_tec_typical_energy_consumption_criteria']
field_list = [f"comp_cat_i{category}_tec_of_model_kwh", f"category_{category}_tec_of_model_kwh",
f"comp_cat_d{category}_tec_of_model_kwh"]
# Get the TEC (typical energy consumption)
for field in field_list:
try:
tec = float(energy_star_data_json[0][field])
break
except KeyError:
pass
return tec
"""
Calculate the average CO2 emission of the product and print the
result message
:param avg_emi_con: float,
The average emission of electricity consumption
:param tec: float,
The typical energy consumption of the product
:param time:
The time of update of the avaerage emission of electricity consumption
"""
def calculate_emission(avg_emi_con, tec, time):
# The CO2 emission will be calculated using the equation:
# CO2 emission = TEC(kWh) * average emission of energy consumption(gCO2/kWh)
result = tec * avg_emi_con
# Print the message
print()
print("The CO2 emission is calculated using the equation:")
print("CO2 emission = typical energy consumption (kWh) * average emission of energy consumption (gCO2/kWh)")
print()
print(f"The average emission of electricity consumption in Finland, according to Fingrid, updated at {time} is {avg_emi_con} gCO2/kWh.")
print(f"The typical energy consumption of your computer, according to Energy Star, is {tec} kWh.")
print()
print(f"The CO2 emission of your computer is {result:.2f} grams of CO2.")
print()
# Process the data for average emisson of electricity consumption in Finland from Fingrid
# Get the data
header_fingrid = {'x-api-key': '0F7Lvj4uQT9qjVQzs23SH57x0ynbM8AB8hrHpaWe'}
fingrid_data_json = get_data("https://api.fingrid.fi/v1/variable/265/event/json", header_fingrid)
# Get the time stamp of the data
time_string = fingrid_data_json['start_time']
time = datetime.datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%S%z")
# Get the value of the data
avg_emi_con = float(fingrid_data_json['value'])
# Process the data for total energy consumption of computers from Energy Star
while True:
# Get the model number from the user
print("The model numbers can be found at https://www.energystar.gov/productfinder/product/certified-computers")
model_number = input("Enter model number of your computer, or enter QUIT to quit: ")
if (model_number.upper() == "QUIT"):
break
# Get the data
energy_star_data_json = find_model_number(model_number)
# Calculate the result
if (energy_star_data_json == []):
print("Model number invalid")
else:
# Decide the category of the computer according to Energy Star
tec = tec_of_product(energy_star_data_json)
# Calculate the CO2 emission of the product according to the 2 database, print the result message
calculate_emission(avg_emi_con, tec, time)