Spaces:
Runtime error
Runtime error
File size: 8,719 Bytes
1decd71 |
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
import openmeteo_requests
import requests_cache
import polars as pl
from retry_requests import retry
import streamlit as st
from datetime import datetime, timedelta
import pytz
from lets_plot import *
from streamlit_letsplot import st_letsplot
import numpy as np
#API Fetch
openmeteo = openmeteo_requests.Client()
#Empty Frames
locations = [
{"name": "Rexburg, Idaho", "latitude": 43.8251, "longitude": -111.7924},
{"name": "Provo, Utah", "latitude": 40.2338, "longitude": -111.6585},
{"name": "Laie, Hawaii", "latitude": 21.6210, "longitude": -157.9753}
]
location_names = [location["name"] for location in locations]
filtered_forecasts = {}
filtered_histories = {}
historical_data = []
forecast_data = []
timezones = pytz.all_timezones
def find_max(df):
daily_highs = df.group_by(by = 'Date').agg(pl.max("Temperature").alias('max'))
return daily_highs, df
# Date Variables
today = datetime.today()
default = today - timedelta(days=14)
# Streamlit Variables
start_date = st.sidebar.date_input(
'Select start date:',
value = default,
max_value= default
)
selected_timezone = st.sidebar.selectbox(
'Choose a timezone:',
timezones
)
end_date = start_date + timedelta(days=14)
temperature_option = st.sidebar.selectbox(
'Select Temperature Type',
('Highest', 'Lowest')
)
city_option = st.sidebar.selectbox(
'Select City',
('Rexburg, Idaho', 'Provo, Utah', 'Laie, Hawaii')
)
# Display the selected date range
st.sidebar.write(f"Date Range: {start_date} to {end_date}")
# Get Forecast Data
for location in locations:
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": location["latitude"],
"longitude": location["longitude"],
"start_date": start_date.strftime('%Y-%m-%d'),
"end_date": end_date.strftime('%Y-%m-%d'),
"hourly": "temperature_2m"
}
# Fetch weather data
responses = openmeteo.weather_api(url, params=params)
response = responses[0]
hourly = response.Hourly()
hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
# Convert timestamp to datetime
start = datetime.fromtimestamp(hourly.Time())
end = datetime.fromtimestamp(hourly.TimeEnd())
freq = timedelta(seconds=hourly.Interval())
# Create Polars DataFrame
hourly_data = pl.select(
date = pl.datetime_range(start, end, freq, closed = "left"),
temperature_2m = hourly_temperature_2m,
location = [location["name"]])
hourly_dataframe = pl.DataFrame(data = hourly_data)
historical_data.append(hourly_dataframe)
# Concatenate all DataFrames
combined_historical = pl.DataFrame(pl.concat(historical_data)).explode('location')
# Get True Historical Data
for location in locations:
url = "https://archive-api.open-meteo.com/v1/archive"
params = {
"latitude": location["latitude"],
"longitude": location["longitude"],
"start_date": start_date.strftime('%Y-%m-%d'),
"end_date": end_date.strftime('%Y-%m-%d'),
"hourly": "temperature_2m"
}
# Fetch weather data
responses = openmeteo.weather_api(url, params=params)
response = responses[0]
hourly = response.Hourly()
hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
# Convert timestamp to datetime
start = datetime.fromtimestamp(hourly.Time())
end = datetime.fromtimestamp(hourly.TimeEnd())
freq = timedelta(seconds=hourly.Interval())
# Create Polars DataFrame
hourly_data = pl.select(
date = pl.datetime_range(start, end, freq, closed = "left"),
temperature_2m = hourly_temperature_2m,
location = [location["name"]])
hourly_dataframe = pl.DataFrame(data = hourly_data)
forecast_data.append(hourly_dataframe)
combined_forecast = pl.DataFrame(pl.concat(forecast_data)).explode('location')
for name in location_names:
filtered_forecasts[name] = (combined_forecast.filter(pl.col("location") == name).drop('location').rename({'date': 'Date'}).rename({'temperature_2m': 'Temperature'}).with_columns(pl.col('Temperature') * 9/5 + 32))
filtered_histories[name] = (combined_historical.filter(pl.col("location") == name).drop('location').rename({'date': 'Date'}).rename({'temperature_2m': 'Temperature'}).with_columns(pl.col('Temperature') * 9/5 + 32))
tab1, tab2, tab3 = st.tabs(["Data", "Visualisations", "KPIs"])
with tab1:
st.title("Forecasted Weather vs Actual Weather by City")
st.header('Forecasts')
st.markdown("<h2 style='text-align: center; color: white;'>Rexburg</h2>", unsafe_allow_html=True)
# Create two columns for Rexburg content
rexburg_col1, rexburg_col2 = st.columns(2)
# Rexburg content
with rexburg_col1:
st.write("Forecasts")
st.dataframe(filtered_forecasts["Rexburg, Idaho"], use_container_width=True, hide_index=True)
with rexburg_col2:
st.write("Historical Data")
st.dataframe(filtered_histories["Rexburg, Idaho"], use_container_width=True, hide_index=True)
# Provo Header
st.markdown("<h2 style='text-align: center; color: white;'>Provo</h2>", unsafe_allow_html=True)
# Create two columns for Provo content
provo_col1, provo_col2 = st.columns(2)
# Provo content
with provo_col1:
st.write("Forecasts")
st.dataframe(filtered_forecasts["Provo, Utah"], use_container_width=True, hide_index=True)
with provo_col2:
st.write("Historical Data")
st.dataframe(filtered_histories["Provo, Utah"], use_container_width=True, hide_index=True)
# Laie Header
st.markdown("<h2 style='text-align: center; color: white;'>Laie</h2>", unsafe_allow_html=True)
# Create two columns for Laie content
laie_col1, laie_col2 = st.columns(2)
# Laie content
with laie_col1:
st.write("Forecasts")
st.dataframe(filtered_forecasts["Laie, Hawaii"], use_container_width=True, hide_index=True)
with laie_col2:
st.write("Historical Data")
st.dataframe(filtered_histories["Laie, Hawaii"], use_container_width=True, hide_index=True)
with tab2:
st.header('Visualisations by City')
st.subheader("Forecasted Data vs. Historical Data")
combined_forecast = combined_forecast.rename({'date': 'Date'}).rename({'temperature_2m': 'Temperature'}).with_columns(pl.col('Temperature') * 9/5 + 32).with_columns(pl.col('Date').cast(datetime))
combined_historical = combined_historical.rename({'date': 'Date'}).rename({'temperature_2m': 'Temperature'}).with_columns(pl.col('Temperature') * 9/5 + 32).with_columns(pl.col('Date').cast(datetime))
reg_forecasted = ggplot(combined_forecast, aes(x='Date', y='Temperature', color = 'location')) \
+ geom_line() \
+ facet_wrap('location', ncol = 1) \
+ labs(title = f'Hourly Temperatures',
x = 'Date', y = 'Temperature (°F)', color = 'City Name') + \
guides(color="none") + \
scale_x_datetime(format = '%m/%d')
reg_historical = ggplot(combined_historical, aes(x='Date', y='Temperature', color = 'location')) \
+ geom_line() \
+ facet_wrap('location', ncol = 1) \
+ labs(title = f'Hourly Temperatures',
x = 'Date', y = 'Temperature (°F)', color = 'City Name') + \
guides(color="none") + \
scale_x_datetime(format = '%m/%d')
st.subheader('Forecasted')
st_letsplot(reg_forecasted)
st.subheader('Historical')
st_letsplot(reg_historical)
st.subheader('Boxplot of Average Hourly Temperature')
box_plot = ggplot(combined_forecast, aes(x='location', y='Temperature', color = 'location')) + \
geom_jitter(alpha = .65) + \
geom_boxplot(alpha = .9) + \
labs(title=f'Hourly Temperature Readings',
x='City', y='Temperature (°F)') + \
guides(color = "none")
st_letsplot(box_plot)
maxes = combined_historical.group_by('location').agg((pl.col('Temperature').max()))
max_plot = ggplot(maxes, aes(x = 'location', y = 'Temperature', color = 'location')) + \
geom_bar(stat='identity') + \
labs(x = 'City Name', y = 'Max Temperature in Month (°F)')
st.subheader('Maximum Temperature for Period')
st_letsplot(max_plot)
with tab3:
if temperature_option == 'Highest':
highest_temp = combined_historical.filter(pl.col('location') == city_option).select(pl.max('Temperature')).item()
st.metric(label=f"Highest Temperature in {city_option}", value=f"{round(highest_temp,2)} °F")
else:
# Get the lowest temperature and its corresponding date
lowest_temp = combined_historical.filter(pl.col('location') == city_option).select(pl.min('Temperature')).item()
st.metric(label=f"Highest Temperature in {city_option}", value=f"{round(lowest_temp,2)} °F")
|