-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaqi_api.py
52 lines (41 loc) · 1.47 KB
/
aqi_api.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
import json
import http.client
API_KEY = "cc157781029ab786dff9cc778ce027d86e25f9eea770e41c95534bd8361999db"
def fetch_aqi(city):
conn = http.client.HTTPSConnection("api.ambeedata.com")
headers = {
'x-api-key': API_KEY,
'Content-type': "application/json"
}
conn.request("GET", f"/latest/by-city?city={city}", headers=headers)
res = conn.getresponse()
return json.loads(res.read().decode("utf-8"))
def estimate_solar_generation(aqi_data):
ideal_solar_irradiance = 1000
reduction_factor = {
"CO": 0.1,
"NO2": 0.2,
"OZONE": 0.15,
"PM10": 0.25,
"PM25": 0.3,
"SO2": 0.1
}
pollutants = {
"CO": aqi_data.get("CO", 0),
"NO2": aqi_data.get("NO2", 0),
"OZONE": aqi_data.get("OZONE", 0),
"PM10": aqi_data.get("PM10", 0),
"PM25": aqi_data.get("PM25", 0),
"SO2": aqi_data.get("SO2", 0)
}
total_concentration = sum(pollutants.values())
contributions = {
pollutant: (level / total_concentration) * 100 if total_concentration > 0 else 0
for pollutant, level in pollutants.items()
}
reduction_percentage = sum(
pollutants[p] * reduction_factor[p] for p in pollutants if pollutants[p] > 0
)
reduction_percentage = min(reduction_percentage, 100)
estimated_generation = ideal_solar_irradiance * (1 - reduction_percentage / 100)
return round(estimated_generation, 2), contributions