Spaces:
Runtime error
Runtime error
Commit
·
ac5fe72
1
Parent(s):
de76b43
Delete ERA2.py
Browse files
ERA2.py
DELETED
@@ -1,237 +0,0 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""
|
3 |
-
Created on Fri Oct 14 10:35:25 2022
|
4 |
-
|
5 |
-
@author: mritchey
|
6 |
-
"""
|
7 |
-
# streamlit run "C:\Users\mritchey\.spyder-py3\Python Scripts\streamlit projects\ERA\ERA2.py"
|
8 |
-
import datetime
|
9 |
-
import glob
|
10 |
-
import os
|
11 |
-
import branca.colormap as cm
|
12 |
-
import folium
|
13 |
-
import numpy as np
|
14 |
-
import pandas as pd
|
15 |
-
import plotly.express as px
|
16 |
-
import streamlit as st
|
17 |
-
from geopy.extra.rate_limiter import RateLimiter
|
18 |
-
from geopy.geocoders import Nominatim
|
19 |
-
from matplotlib import colors as colors
|
20 |
-
from streamlit_folium import st_folium
|
21 |
-
import xarray as xr
|
22 |
-
import cdsapi
|
23 |
-
|
24 |
-
|
25 |
-
def mapvalue2color(value, cmap):
|
26 |
-
if np.isnan(value):
|
27 |
-
return (1, 0, 0, 0)
|
28 |
-
else:
|
29 |
-
return colors.to_rgba(cmap(value), 0.7)
|
30 |
-
|
31 |
-
|
32 |
-
def geocode(address):
|
33 |
-
try:
|
34 |
-
address2 = address.replace(' ', '+').replace(',', '%2C')
|
35 |
-
df = pd.read_json(
|
36 |
-
f'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={address2}&benchmark=2020&format=json')
|
37 |
-
results = df.iloc[:1, 0][0][0]['coordinates']
|
38 |
-
lat, lon = results['y'], results['x']
|
39 |
-
except:
|
40 |
-
geolocator = Nominatim(user_agent="GTA Lookup")
|
41 |
-
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
|
42 |
-
location = geolocator.geocode(address)
|
43 |
-
lat, lon = location.latitude, location.longitude
|
44 |
-
return lat, lon
|
45 |
-
|
46 |
-
|
47 |
-
def graph_within_date_range(d, number_days_range):
|
48 |
-
year, month, day = d[:4], d[4:6], d[6:8]
|
49 |
-
date = pd.Timestamp(d)
|
50 |
-
start_date, end_date = date - \
|
51 |
-
pd.Timedelta(days=number_days_range), date + \
|
52 |
-
pd.Timedelta(days=number_days_range+1)
|
53 |
-
start_date = start_date.strftime("%Y-%m-%d")
|
54 |
-
end_date = end_date.strftime("%Y-%m-%d")
|
55 |
-
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'
|
56 |
-
df = pd.read_json(url).reset_index()
|
57 |
-
data = pd.DataFrame({c['index']: c['hourly'] for r, c in df.iterrows()})
|
58 |
-
data['time'] = pd.to_datetime(data['time'])
|
59 |
-
data['date'] = pd.to_datetime(data['time'].dt.date)
|
60 |
-
data = data.query("temperature_2m==temperature_2m")
|
61 |
-
|
62 |
-
data_agg = data.groupby(['date']).agg({'temperature_2m': ['min', 'mean', 'max'],
|
63 |
-
'precipitation': ['sum'],
|
64 |
-
'windspeed_10m': ['min', 'mean', 'max'],
|
65 |
-
'windgusts_10m': ['min', 'mean', 'max']
|
66 |
-
})
|
67 |
-
data_agg.columns = data_agg.columns.to_series().str.join('_')
|
68 |
-
data_agg = data_agg.query("temperature_2m_min==temperature_2m_min")
|
69 |
-
return data.drop(columns=['date']), data_agg
|
70 |
-
|
71 |
-
|
72 |
-
@st.cache(allow_output_mutation=True)
|
73 |
-
def get_era5_data(year, month, day):
|
74 |
-
c = cdsapi.Client(key='82849:2dc2d4ad-9e03-4a42-8914-d7faf8c4a44d',
|
75 |
-
url="https://cds.climate.copernicus.eu/api/v2")
|
76 |
-
|
77 |
-
c.retrieve(
|
78 |
-
'reanalysis-era5-single-levels',
|
79 |
-
{
|
80 |
-
'product_type': 'reanalysis',
|
81 |
-
'variable': ['10m_u_component_of_wind', '10m_v_component_of_wind',
|
82 |
-
'instantaneous_10m_wind_gust',
|
83 |
-
'2m_temperature', 'total_precipitation'],
|
84 |
-
'year': year,
|
85 |
-
'month': [month],
|
86 |
-
'day': [day],
|
87 |
-
'time': ['00:00', '06:00', '12:00', '18:00'],
|
88 |
-
'area': [49.5, -125, 24.5, -66.5, ],
|
89 |
-
'format': 'netcdf',
|
90 |
-
},
|
91 |
-
'data.nc')
|
92 |
-
|
93 |
-
|
94 |
-
@st.cache
|
95 |
-
def convert_df(df):
|
96 |
-
return df.to_csv(index=0).encode('utf-8')
|
97 |
-
|
98 |
-
|
99 |
-
try:
|
100 |
-
for i in glob.glob('*.grib2'):
|
101 |
-
os.remove(i)
|
102 |
-
except:
|
103 |
-
pass
|
104 |
-
|
105 |
-
st.set_page_config(layout="wide")
|
106 |
-
col1, col2 = st.columns((2))
|
107 |
-
|
108 |
-
address = st.sidebar.text_input(
|
109 |
-
"Address", "123 Main Street, Columbus, OH 43215")
|
110 |
-
date = st.sidebar.date_input(
|
111 |
-
"Date", pd.Timestamp(2022, 9, 28))
|
112 |
-
d = date.strftime('%Y%m%d')
|
113 |
-
date = date.strftime('%Y-%m-%d')
|
114 |
-
time = st.sidebar.selectbox('Time (UTC):', ('12 AM', '6 AM', '12 PM', '6 PM',))
|
115 |
-
type_var = st.sidebar.selectbox(
|
116 |
-
'Type:', ('Gust', 'Wind', 'Temp', 'Precipitation'))
|
117 |
-
number_days_range = st.sidebar.selectbox(
|
118 |
-
'Within Day Range:', (5, 10, 30, 90, 180))
|
119 |
-
hourly_daily = st.sidebar.radio('Aggregate Data', ('Hourly', 'Daily'))
|
120 |
-
|
121 |
-
# Keys
|
122 |
-
var_key = {'Gust': 'i10fg', 'Wind': 'wind10',
|
123 |
-
'Temp': 't2m', 'Precipitation': 'tp'}
|
124 |
-
|
125 |
-
variable = var_key[type_var]
|
126 |
-
|
127 |
-
unit_key = {'Gust': 'MPH', 'Wind': 'MPH',
|
128 |
-
'Temp': 'F', 'Precipitation': 'In.'}
|
129 |
-
unit = unit_key[type_var]
|
130 |
-
|
131 |
-
cols_key = {'Gust': ['windgusts_10m'], 'Wind': ['windspeed_10m'], 'Temp': ['temperature_2m'],
|
132 |
-
'Precipitation': ['precipitation']}
|
133 |
-
|
134 |
-
cols_key_agg = {'Gust': ['windgusts_10m_min', 'windgusts_10m_mean',
|
135 |
-
'windgusts_10m_max'],
|
136 |
-
'Wind': ['windspeed_10m_min', 'windspeed_10m_mean',
|
137 |
-
'windspeed_10m_max'],
|
138 |
-
'Temp': ['temperature_2m_min', 'temperature_2m_mean', 'temperature_2m_max'],
|
139 |
-
'Precipitation': ['precipitation_sum']}
|
140 |
-
|
141 |
-
if hourly_daily == 'Hourly':
|
142 |
-
cols = cols_key[type_var]
|
143 |
-
else:
|
144 |
-
cols = cols_key_agg[type_var]
|
145 |
-
|
146 |
-
|
147 |
-
if time[-2:] == 'PM' and int(time[:2].strip()) < 12:
|
148 |
-
t = datetime.time(int(time[:2].strip())+12, 00).strftime('%H')+'00'
|
149 |
-
elif time[-2:] == 'AM' and int(time[:2].strip()) == 12:
|
150 |
-
t = '00:00'
|
151 |
-
else:
|
152 |
-
t = datetime.time(int(time[:2].strip()), 00).strftime('%H')+'00'
|
153 |
-
|
154 |
-
year, month, day = d[:4], d[4:6], d[6:8]
|
155 |
-
|
156 |
-
get_era5_data(year, month, day)
|
157 |
-
ds = xr.open_dataset('data.nc')
|
158 |
-
ds = ds.sel(time=f'{date}T{t}').drop('time')
|
159 |
-
|
160 |
-
#Convert Units
|
161 |
-
ds = ds.assign(t2m=(ds.t2m - 273.15) * 9/5 + 32)
|
162 |
-
ds = ds.assign(i10fg=(ds.i10fg*2.237))
|
163 |
-
ds = ds.assign(tp=(ds.tp/24.5))
|
164 |
-
ds = ds.assign(wind10=((ds.v10**2+ds.u10**2)**.5)*2.237)
|
165 |
-
|
166 |
-
lat, lon = geocode(address)
|
167 |
-
|
168 |
-
var_value = ds[variable].sel(
|
169 |
-
longitude=lon, latitude=lat, method="nearest").values.item()
|
170 |
-
var_value = round(var_value, 1)
|
171 |
-
|
172 |
-
img = ds[variable].values
|
173 |
-
boundary = ds.rio.bounds()
|
174 |
-
left, bottom, right, top = boundary
|
175 |
-
|
176 |
-
img[img < 0.0] = np.nan
|
177 |
-
|
178 |
-
clat = (bottom + top)/2
|
179 |
-
clon = (left + right)/2
|
180 |
-
|
181 |
-
vmin = np.floor(np.nanmin(img))
|
182 |
-
vmax = np.ceil(np.nanmax(img))
|
183 |
-
|
184 |
-
colormap = cm.LinearColormap(
|
185 |
-
colors=['blue', 'lightblue', 'red'], vmin=vmin, vmax=vmax)
|
186 |
-
|
187 |
-
m = folium.Map(location=[lat, lon], zoom_start=5, height=500)
|
188 |
-
|
189 |
-
folium.Marker(
|
190 |
-
location=[lat, lon],
|
191 |
-
popup=f"{var_value} {unit}"
|
192 |
-
).add_to(m)
|
193 |
-
|
194 |
-
folium.raster_layers.ImageOverlay(
|
195 |
-
image=img,
|
196 |
-
name='Wind Speed Map',
|
197 |
-
opacity=.8,
|
198 |
-
bounds=[[bottom, left], [top, right]],
|
199 |
-
colormap=lambda value: mapvalue2color(value, colormap)
|
200 |
-
).add_to(m)
|
201 |
-
|
202 |
-
|
203 |
-
folium.LayerControl().add_to(m)
|
204 |
-
colormap.caption = 'Wind Speed: MPH'
|
205 |
-
m.add_child(colormap)
|
206 |
-
|
207 |
-
with col1:
|
208 |
-
st.title('ERA5 Model')
|
209 |
-
# st.write(
|
210 |
-
# f"{type_wind.title()} Speed: {wind_mph[0].round(2)} MPH at {time} UTC")
|
211 |
-
st_folium(m, height=500)
|
212 |
-
df_all, df_all_agg = graph_within_date_range(d, number_days_range)
|
213 |
-
|
214 |
-
if hourly_daily == 'Hourly':
|
215 |
-
fig = px.line(df_all, x="time", y=cols)
|
216 |
-
df_downloald = df_all
|
217 |
-
else:
|
218 |
-
fig = px.line(df_all_agg.reset_index(), x="date", y=cols)
|
219 |
-
df_downloald = df_all_agg.reset_index()
|
220 |
-
|
221 |
-
with col2:
|
222 |
-
st.title('Analysis')
|
223 |
-
st.plotly_chart(fig)
|
224 |
-
|
225 |
-
csv = convert_df(df_downloald)
|
226 |
-
|
227 |
-
st.download_button(
|
228 |
-
label="Download data as CSV",
|
229 |
-
data=csv,
|
230 |
-
file_name=f'{d}.csv',
|
231 |
-
mime='text/csv')
|
232 |
-
|
233 |
-
|
234 |
-
st.markdown(""" <style>
|
235 |
-
#MainMenu {visibility: hidden;}
|
236 |
-
footer {visibility: hidden;}
|
237 |
-
</style> """, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|