-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunblock_monolithic.py
80 lines (66 loc) · 2.14 KB
/
sunblock_monolithic.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
"""
Find the next block of sun shining for N hours at your location
"""
import sys
import pandas as pd
import datetime
import requests
CLEARISH_SKY_WMO_CODES = [0, 1]
# example locations
OXFORD = (51.75, -1.25)
NEWCASTLE = (54.98, -1.61)
BRIGHTON = (50.75, 0)
loc = NEWCASTLE
sunblock_hours = 2 # try to get a 2 hour sun block
def fahrenheit_to_celsius(temp):
return 5 * (temp - 32) / 9
# Fetch data from OpenMeteo
res = requests.get(
f"https://api.open-meteo.com/v1/forecast?latitude={loc[0]}&longitude={loc[1]}"
"&hourly=temperature_2m,precipitation,weather_code"
"&daily=sunrise,sunset"
"&temperature_unit=fahrenheit"
)
if res.status_code != 200:
print("Error fetching from OpenMeteo")
sys.exit(1)
# Process data, creating a dataframe that can be filtered
data = res.json()
sunrise_times = data["daily"]["sunrise"]
sunset_times = data["daily"]["sunset"]
hourly = data["hourly"]
sunrise = sum([[x] * 24 for x in sunrise_times], [])
sunset = sum([[x] * 24 for x in sunset_times], [])
df = pd.DataFrame(
{
"time": hourly["time"],
"sunrise": sunrise,
"sunset": sunset,
"temperature_celsius": fahrenheit_to_celsius(
pd.Series(hourly["temperature_2m"])
),
"precipitation_mm": hourly["precipitation"],
"weather_code": hourly["weather_code"],
}
)
NOW = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M")
assert (
isinstance(sunblock_hours, int) and 0 < sunblock_hours < 23
), "Number of hours must be between 0 and 23"
# Sunny when it is day time and the sky is clear or partly cloudy
sunny = (
(df.sunrise <= df.time)
& (df.time > NOW)
& (df.time <= df.sunset)
& (df.weather_code.isin(CLEARISH_SKY_WMO_CODES))
)
sunny_coded = sunny.map(lambda x: "S" if x else " ").sum()
# Use substring search to find first instance of sun block
if (idx := sunny_coded.find("S" * sunblock_hours)) == -1:
print("No sunny interval found")
else:
mean_temp = df.loc[idx : idx + sunblock_hours].temperature_celsius.mean()
print(
f"""Found sun block of {sunblock_hours} hours, starting at:
{df.loc[idx].time}, mean temperature is {mean_temp:.1f}°C"""
)