Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,10 +4,12 @@ Created on Fri Oct 14 10:35:25 2022
|
|
4 |
|
5 |
@author: mritchey
|
6 |
"""
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
9 |
import rasterio
|
10 |
-
import glob
|
11 |
from PIL import Image
|
12 |
import streamlit as st
|
13 |
import os
|
@@ -24,10 +26,12 @@ import xarray as xr
|
|
24 |
import warnings
|
25 |
warnings.filterwarnings("ignore")
|
26 |
|
|
|
27 |
@st.cache_data
|
28 |
def convert_df(df):
|
29 |
return df.to_csv(index=0).encode('utf-8')
|
30 |
|
|
|
31 |
def geocode(address):
|
32 |
try:
|
33 |
address2 = address.replace(' ', '+').replace(',', '%2C')
|
@@ -51,10 +55,14 @@ def map_folium(data, zoom=12):
|
|
51 |
|
52 |
# folium.GeoJson(gdf['buffer']).add_to(m)
|
53 |
folium.raster_layers.ImageOverlay(
|
54 |
-
data, opacity=0.8, bounds=[[bottom, left], [top, right]]
|
|
|
|
|
55 |
return m
|
56 |
|
57 |
-
@st.cache_data
|
|
|
|
|
58 |
def crop_hail_jpg_filter(f, crop_coords, scaling_factor=255):
|
59 |
date = f[-19:-11]
|
60 |
image = Image.open(f)
|
@@ -62,68 +70,80 @@ def crop_hail_jpg_filter(f, crop_coords, scaling_factor=255):
|
|
62 |
image = (np.array(cropped_image)/scaling_factor)
|
63 |
if image.sum() > 0:
|
64 |
return date, image
|
65 |
-
|
66 |
-
@st.cache_data
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
#Set up 2 Columns
|
82 |
st.set_page_config(layout="wide")
|
83 |
col1, col2 = st.columns((2))
|
84 |
|
|
|
85 |
|
86 |
|
87 |
#Input Values
|
88 |
address = st.sidebar.text_input("Address", "123 Main Street, Dallas, TX 75126")
|
89 |
-
|
90 |
-
|
|
|
|
|
91 |
circle_radius = st.sidebar.selectbox('Box Radius (Miles)', (5, 10, 25))
|
92 |
|
93 |
zoom_dic = {5: 12, 10: 11, 25: 10}
|
94 |
zoom = zoom_dic[circle_radius]
|
95 |
|
96 |
-
try:
|
97 |
-
days_within = int(days_within)
|
98 |
-
start_date, end_date = d - \
|
99 |
-
pd.Timedelta(days=days_within), d+pd.Timedelta(days=days_within+1)
|
100 |
-
start_date, end_date = start_date.strftime(
|
101 |
-
'%Y%m%d'), end_date.strftime('%Y%m%d')
|
102 |
-
except:
|
103 |
-
days_within = 0
|
104 |
-
start_date = end_date=d -pd.Timedelta(days=days_within)
|
105 |
-
start_date = end_date=start_date.strftime('%Y%m%d')
|
106 |
#Geocode and get Data
|
107 |
result = geocode(address)
|
108 |
lat, lon = result.values[0]
|
109 |
|
110 |
-
ds_stage = xr.open_rasterio('hail_stage.grib2')
|
111 |
-
transform = ds_stage.rio.transform()
|
112 |
|
|
|
|
|
|
|
|
|
113 |
row, col = rasterio.transform.rowcol(transform, lon, lat)
|
114 |
|
115 |
-
|
116 |
-
radius = int(np.ceil(circle_radius*1.6))
|
117 |
crop_coords = col-radius, row-radius, col+radius+1, row+radius+1
|
118 |
|
119 |
# Get Data
|
120 |
-
|
121 |
-
|
|
|
122 |
|
123 |
-
try:
|
124 |
-
max_values = np.max(np.array([i[1] for i in results]), axis=0)*0.0393701
|
125 |
-
except:
|
126 |
-
max_values=np.zeros(shape=(2*radius+1,2*radius+1))
|
127 |
|
128 |
# Bin Data
|
129 |
bin_edges = [0, 0.1, 0.2, 0.4, 0.8, 1.2, 1.6, 2, 3, 4, np.inf]
|
@@ -133,38 +153,26 @@ colors_values = ['#ffffff', '#ffff00', '#d1ab00', '#ff9b00', '#fe0000', '#cd0000
|
|
133 |
'#ff30cd', '#9a009b', '#4a4d4c']
|
134 |
color_discrete_map = dict(zip(bin_names, colors_values))
|
135 |
|
136 |
-
all_data = []
|
137 |
-
for date, mat in results:
|
138 |
-
mat = mat*0.0393701
|
139 |
-
hist_values, _ = np.histogram(mat, bins=bin_edges)
|
140 |
-
df = pd.DataFrame({'bin_edges': bin_edges[:-1],
|
141 |
-
'Bin': bin_names,
|
142 |
-
'Values': hist_values},
|
143 |
-
)
|
144 |
-
df['Date'] = pd.to_datetime(date)
|
145 |
-
all_data.append(df)
|
146 |
-
|
147 |
-
try:
|
148 |
-
final = pd.concat(all_data)
|
149 |
-
final = final.query('bin_edges!=0')
|
150 |
-
fig = px.bar(final, x="Date", y="Values", color="Bin",
|
151 |
-
barmode="stack",
|
152 |
-
color_discrete_map=color_discrete_map)
|
153 |
-
except:
|
154 |
-
pass
|
155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
# Crop the raster using the bounds
|
158 |
cropped_data = ds_stage[0][row-radius:row+radius+1, col-radius:col+radius+1]
|
159 |
cropped_data.values = max_values
|
160 |
|
161 |
-
# Max Values Bin for RGB
|
162 |
|
|
|
163 |
def hex_to_rgb(hex_code):
|
164 |
hex_code = hex_code.lstrip('#') # Remove the '#' character if present
|
165 |
rgb = tuple(int(hex_code[i:i+2], 16) for i in (0, 2, 4))
|
166 |
return rgb
|
167 |
|
|
|
168 |
def hex_to_rgba(hex_code, alpha=.8):
|
169 |
if hex_code == '#ffffff':
|
170 |
alpha = 0.0
|
@@ -182,10 +190,10 @@ max_values_rgb = np.array([hex_to_rgba(i) for i in bin_colors.flatten()]).reshap
|
|
182 |
max_values.shape[0], max_values.shape[0], 4)
|
183 |
|
184 |
|
185 |
-
|
186 |
#Mapping
|
187 |
img = max_values_rgb.astype('uint8')
|
188 |
|
|
|
189 |
boundary = cropped_data.rio.bounds()
|
190 |
left, bottom, right, top = boundary
|
191 |
|
@@ -197,10 +205,10 @@ clon = (left + right)/2
|
|
197 |
vmin = np.floor(np.nanmin(img))
|
198 |
vmax = np.ceil(np.nanmax(img))
|
199 |
|
200 |
-
|
201 |
-
|
202 |
-
#
|
203 |
-
|
204 |
|
205 |
|
206 |
m = map_folium(img, zoom)
|
@@ -215,7 +223,7 @@ with col2:
|
|
215 |
st.title(f'Hail')
|
216 |
try:
|
217 |
st.plotly_chart(fig)
|
218 |
-
csv = convert_df(
|
219 |
st.download_button(
|
220 |
label="Download data as CSV",
|
221 |
data=csv,
|
|
|
4 |
|
5 |
@author: mritchey
|
6 |
"""
|
7 |
+
# streamlit run "C:\Users\mritchey\.spyder-py3\Python Scripts\streamlit projects\hail\hail h5.py"
|
8 |
+
|
9 |
+
import gzip
|
10 |
+
import pickle
|
11 |
+
import h5py
|
12 |
import rasterio
|
|
|
13 |
from PIL import Image
|
14 |
import streamlit as st
|
15 |
import os
|
|
|
26 |
import warnings
|
27 |
warnings.filterwarnings("ignore")
|
28 |
|
29 |
+
|
30 |
@st.cache_data
|
31 |
def convert_df(df):
|
32 |
return df.to_csv(index=0).encode('utf-8')
|
33 |
|
34 |
+
|
35 |
def geocode(address):
|
36 |
try:
|
37 |
address2 = address.replace(' ', '+').replace(',', '%2C')
|
|
|
55 |
|
56 |
# folium.GeoJson(gdf['buffer']).add_to(m)
|
57 |
folium.raster_layers.ImageOverlay(
|
58 |
+
data, opacity=0.8, bounds=[[bottom, left], [top, right]],
|
59 |
+
interactive=True
|
60 |
+
).add_to(m)
|
61 |
return m
|
62 |
|
63 |
+
# @st.cache_data
|
64 |
+
|
65 |
+
|
66 |
def crop_hail_jpg_filter(f, crop_coords, scaling_factor=255):
|
67 |
date = f[-19:-11]
|
68 |
image = Image.open(f)
|
|
|
70 |
image = (np.array(cropped_image)/scaling_factor)
|
71 |
if image.sum() > 0:
|
72 |
return date, image
|
73 |
+
|
74 |
+
# @st.cache_data
|
75 |
+
|
76 |
+
|
77 |
+
def get_data(row, col, radius):
|
78 |
+
files = [
|
79 |
+
"2023_hail.h5",
|
80 |
+
"2022_hail.h5"]
|
81 |
+
|
82 |
+
all_data = []
|
83 |
+
all_dates = []
|
84 |
+
for f in files:
|
85 |
+
with h5py.File(f, 'r') as f:
|
86 |
+
data = f['hail'][:, row-radius:row +
|
87 |
+
radius+1, col-radius:col+radius+1]
|
88 |
+
dates = f['dates'][:]
|
89 |
+
all_data.append(data)
|
90 |
+
all_dates.append(dates)
|
91 |
+
|
92 |
+
data_mat = np.concatenate(all_data)
|
93 |
+
data_mat = np.where(data_mat < 0, 0, data_mat)*0.0393701
|
94 |
+
dates_mat = np.concatenate(all_dates)
|
95 |
+
|
96 |
+
data_actual = [i[radius, radius] for i in data_mat]
|
97 |
+
data_max = np.max(data_mat, axis=(1, 2))
|
98 |
+
data_max_2 = np.max(data_mat, axis=0)
|
99 |
+
|
100 |
+
df = pd.DataFrame({'Date': dates_mat,
|
101 |
+
'Actual': data_actual,
|
102 |
+
'Max': data_max})
|
103 |
+
|
104 |
+
df['Date'] = pd.to_datetime(df['Date'], format='%Y%m%d')
|
105 |
+
|
106 |
+
return df, data_max_2
|
107 |
+
|
108 |
|
109 |
#Set up 2 Columns
|
110 |
st.set_page_config(layout="wide")
|
111 |
col1, col2 = st.columns((2))
|
112 |
|
113 |
+
os.chdir(r'C:\Users\mritchey')
|
114 |
|
115 |
|
116 |
#Input Values
|
117 |
address = st.sidebar.text_input("Address", "123 Main Street, Dallas, TX 75126")
|
118 |
+
start_date = st.sidebar.date_input("Start Date", pd.Timestamp(2022, 1, 1))
|
119 |
+
end_date = st.sidebar.date_input("End Date", pd.Timestamp(2023, 12, 31))
|
120 |
+
|
121 |
+
|
122 |
circle_radius = st.sidebar.selectbox('Box Radius (Miles)', (5, 10, 25))
|
123 |
|
124 |
zoom_dic = {5: 12, 10: 11, 25: 10}
|
125 |
zoom = zoom_dic[circle_radius]
|
126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
#Geocode and get Data
|
128 |
result = geocode(address)
|
129 |
lat, lon = result.values[0]
|
130 |
|
|
|
|
|
131 |
|
132 |
+
#Raster Data
|
133 |
+
extracted_file = 'hail_stage.grib2'
|
134 |
+
ds_stage = xr.open_dataarray(extracted_file,engine='rasterio')
|
135 |
+
transform = ds_stage.rio.transform()
|
136 |
row, col = rasterio.transform.rowcol(transform, lon, lat)
|
137 |
|
138 |
+
|
139 |
+
radius = int(np.ceil(circle_radius*1.6))
|
140 |
crop_coords = col-radius, row-radius, col+radius+1, row+radius+1
|
141 |
|
142 |
# Get Data
|
143 |
+
df_data, max_values = get_data(row, col, radius)
|
144 |
+
|
145 |
+
df_data=df_data.query(f"'{start_date}'<=Date<='{end_date}'")
|
146 |
|
|
|
|
|
|
|
|
|
147 |
|
148 |
# Bin Data
|
149 |
bin_edges = [0, 0.1, 0.2, 0.4, 0.8, 1.2, 1.6, 2, 3, 4, np.inf]
|
|
|
153 |
'#ff30cd', '#9a009b', '#4a4d4c']
|
154 |
color_discrete_map = dict(zip(bin_names, colors_values))
|
155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
+
fig = px.bar(df_data, x="Date", y="Actual", color="Actual",
|
158 |
+
# barmode="stack",
|
159 |
+
# color='red',
|
160 |
+
# color_discrete_map=color_discrete_map,
|
161 |
+
|
162 |
+
)
|
163 |
|
164 |
# Crop the raster using the bounds
|
165 |
cropped_data = ds_stage[0][row-radius:row+radius+1, col-radius:col+radius+1]
|
166 |
cropped_data.values = max_values
|
167 |
|
|
|
168 |
|
169 |
+
# Max Values Bin for RGB
|
170 |
def hex_to_rgb(hex_code):
|
171 |
hex_code = hex_code.lstrip('#') # Remove the '#' character if present
|
172 |
rgb = tuple(int(hex_code[i:i+2], 16) for i in (0, 2, 4))
|
173 |
return rgb
|
174 |
|
175 |
+
|
176 |
def hex_to_rgba(hex_code, alpha=.8):
|
177 |
if hex_code == '#ffffff':
|
178 |
alpha = 0.0
|
|
|
190 |
max_values.shape[0], max_values.shape[0], 4)
|
191 |
|
192 |
|
|
|
193 |
#Mapping
|
194 |
img = max_values_rgb.astype('uint8')
|
195 |
|
196 |
+
|
197 |
boundary = cropped_data.rio.bounds()
|
198 |
left, bottom, right, top = boundary
|
199 |
|
|
|
205 |
vmin = np.floor(np.nanmin(img))
|
206 |
vmax = np.ceil(np.nanmax(img))
|
207 |
|
208 |
+
colormap = cm.StepColormap(colors=list(color_discrete_map.values()),
|
209 |
+
index=bin_edges,
|
210 |
+
# vmin=vmin, vmax=vmax
|
211 |
+
)
|
212 |
|
213 |
|
214 |
m = map_folium(img, zoom)
|
|
|
223 |
st.title(f'Hail')
|
224 |
try:
|
225 |
st.plotly_chart(fig)
|
226 |
+
csv = convert_df(df_data)
|
227 |
st.download_button(
|
228 |
label="Download data as CSV",
|
229 |
data=csv,
|