Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,134 +1,132 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""
|
3 |
-
Created on Tue Apr 8 14:53:45 2025
|
4 |
-
|
5 |
-
@author: mritchey
|
6 |
-
"""
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
import
|
11 |
-
import
|
12 |
-
import
|
13 |
-
import
|
14 |
-
import
|
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 |
-
data = pd.
|
43 |
-
data
|
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 |
-
lat, lon
|
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 |
-
cols =
|
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 |
-
footer {visibility: hidden;}
|
134 |
-
</style> """, unsafe_allow_html=True)
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""
|
3 |
+
Created on Tue Apr 8 14:53:45 2025
|
4 |
+
|
5 |
+
@author: mritchey
|
6 |
+
"""
|
7 |
+
|
8 |
+
# streamlit run "C:\Users\mritchey\.spyder-py3\Python Scripts\streamlit projects\weather api\app.py"
|
9 |
+
import glob
|
10 |
+
import os
|
11 |
+
import numpy as np
|
12 |
+
import pandas as pd
|
13 |
+
import plotly.express as px
|
14 |
+
import streamlit as st
|
15 |
+
from geopy.extra.rate_limiter import RateLimiter
|
16 |
+
from geopy.geocoders import Nominatim
|
17 |
+
|
18 |
+
|
19 |
+
def geocode(address):
|
20 |
+
try:
|
21 |
+
address2 = address.replace(' ', '+').replace(',', '%2C')
|
22 |
+
df = pd.read_json(
|
23 |
+
f'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={address2}&benchmark=2020&format=json')
|
24 |
+
results = df.iloc[:1, 0][0][0]['coordinates']
|
25 |
+
lat, lon = results['y'], results['x']
|
26 |
+
except:
|
27 |
+
geolocator = Nominatim(user_agent="GTA Lookup")
|
28 |
+
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
|
29 |
+
location = geolocator.geocode(address)
|
30 |
+
lat, lon = location.latitude, location.longitude
|
31 |
+
return lat, lon
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
@st.cache_data
|
36 |
+
def get_weather_data(lat, lon, start_date, end_date):
|
37 |
+
|
38 |
+
url = f'https://archive-api.open-meteo.com/v1/archive?latitude={lat}&longitude={lon}&start_date={start_date}&end_date={end_date}&hourly=temperature_2m,precipitation,windspeed_10m,windgusts_10m&models=best_match&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch'
|
39 |
+
df = pd.read_json(url).reset_index()
|
40 |
+
data = pd.DataFrame({c['index']: c['hourly'] for r, c in df.iterrows()})
|
41 |
+
data['time'] = pd.to_datetime(data['time'])
|
42 |
+
data['date'] = pd.to_datetime(data['time'].dt.date)
|
43 |
+
data = data.query("temperature_2m==temperature_2m")
|
44 |
+
|
45 |
+
data_agg = data.groupby(['date']).agg({'temperature_2m': ['min', 'mean', 'max'],
|
46 |
+
'precipitation': ['sum'],
|
47 |
+
'windspeed_10m': ['min', 'mean', 'max'],
|
48 |
+
'windgusts_10m': ['min', 'mean', 'max']
|
49 |
+
})
|
50 |
+
data_agg.columns = data_agg.columns.to_series().str.join('_')
|
51 |
+
data_agg = data_agg.query("temperature_2m_min==temperature_2m_min")
|
52 |
+
return data.drop(columns=['date']), data_agg
|
53 |
+
|
54 |
+
|
55 |
+
@st.cache_data
|
56 |
+
def convert_df(df):
|
57 |
+
return df.to_csv(index=0).encode('utf-8')
|
58 |
+
|
59 |
+
|
60 |
+
st.set_page_config(layout="wide")
|
61 |
+
col1, col2 = st.columns((2))
|
62 |
+
|
63 |
+
|
64 |
+
address = st.sidebar.text_input(
|
65 |
+
"Address", "1000 Main St, Cincinnati, OH 45202")
|
66 |
+
start_date = st.sidebar.date_input("Start Date", pd.Timestamp(2022, 9, 28))
|
67 |
+
end_date = st.sidebar.date_input("End Date", pd.Timestamp(2022, 9, 30))
|
68 |
+
type_var = st.sidebar.selectbox(
|
69 |
+
'Type:', ('Gust', 'Wind', 'Temp', 'Precipitation'))
|
70 |
+
hourly_daily = st.sidebar.radio('Aggregate Data', ('Hourly', 'Daily'))
|
71 |
+
|
72 |
+
|
73 |
+
start_date = start_date.strftime("%Y-%m-%d")
|
74 |
+
end_date = end_date.strftime("%Y-%m-%d")
|
75 |
+
|
76 |
+
lat, lon = geocode(address)
|
77 |
+
|
78 |
+
df_all, df_all_agg = get_weather_data(lat, lon, start_date, end_date)
|
79 |
+
|
80 |
+
# Keys
|
81 |
+
var_key = {'Gust': 'i10fg', 'Wind': 'wind10',
|
82 |
+
'Temp': 't2m', 'Precipitation': 'tp'}
|
83 |
+
|
84 |
+
variable = var_key[type_var]
|
85 |
+
|
86 |
+
unit_key = {'Gust': 'MPH', 'Wind': 'MPH',
|
87 |
+
'Temp': 'F', 'Precipitation': 'In.'}
|
88 |
+
unit = unit_key[type_var]
|
89 |
+
|
90 |
+
cols_key = {'Gust': ['windgusts_10m'], 'Wind': ['windspeed_10m'], 'Temp': ['temperature_2m'],
|
91 |
+
'Precipitation': ['precipitation']}
|
92 |
+
|
93 |
+
cols_key_agg = {'Gust': ['windgusts_10m_min', 'windgusts_10m_mean',
|
94 |
+
'windgusts_10m_max'],
|
95 |
+
'Wind': ['windspeed_10m_min', 'windspeed_10m_mean',
|
96 |
+
'windspeed_10m_max'],
|
97 |
+
'Temp': ['temperature_2m_min', 'temperature_2m_mean', 'temperature_2m_max'],
|
98 |
+
'Precipitation': ['precipitation_sum']}
|
99 |
+
|
100 |
+
if hourly_daily == 'Hourly':
|
101 |
+
cols = cols_key[type_var]
|
102 |
+
else:
|
103 |
+
cols = cols_key_agg[type_var]
|
104 |
+
|
105 |
+
if hourly_daily == 'Hourly':
|
106 |
+
fig = px.line(df_all, x="time", y=cols[0])
|
107 |
+
df_downloald = df_all
|
108 |
+
else:
|
109 |
+
fig = px.line(df_all_agg.reset_index(), x="date", y=cols[0])
|
110 |
+
df_downloald = df_all_agg.reset_index()
|
111 |
+
|
112 |
+
with col1:
|
113 |
+
st.title('Weather Data')
|
114 |
+
st.plotly_chart(fig)
|
115 |
+
|
116 |
+
csv = convert_df(df_downloald)
|
117 |
+
|
118 |
+
st.download_button(
|
119 |
+
label="Download data as CSV",
|
120 |
+
data=csv,
|
121 |
+
file_name=f'{start_date}.csv',
|
122 |
+
mime='text/csv')
|
123 |
+
|
124 |
+
|
125 |
+
with col2:
|
126 |
+
st.title('')
|
127 |
+
|
128 |
+
|
129 |
+
st.markdown(""" <style>
|
130 |
+
#MainMenu {visibility: hidden;}
|
131 |
+
footer {visibility: hidden;}
|
132 |
+
</style> """, unsafe_allow_html=True)
|
|
|
|