Spaces:
Runtime error
Runtime error
Update pages/Hail.py
Browse files- pages/Hail.py +107 -1
pages/Hail.py
CHANGED
@@ -1,4 +1,107 @@
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
folium.raster_layers.ImageOverlay(
|
3 |
data, opacity=0.8, bounds=bounds).add_to(m)
|
4 |
return m
|
@@ -212,4 +315,7 @@ except:
|
|
212 |
st.markdown(""" <style>
|
213 |
#MainMenu {visibility: hidden;}
|
214 |
footer {visibility: hidden;}
|
215 |
-
</style> """, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
1 |
|
2 |
+
|
3 |
+
# -*- coding: utf-8 -*-
|
4 |
+
"""
|
5 |
+
Created on Tue Dec 6 09:56:29 2022
|
6 |
+
|
7 |
+
@author: mritchey
|
8 |
+
"""
|
9 |
+
#streamlit run "C:\Users\mritchey\.spyder-py3\Python Scripts\streamlit projects\mrms\mrms_hail2 buffer.py"
|
10 |
+
|
11 |
+
import plotly.express as px
|
12 |
+
import os
|
13 |
+
from PIL import Image
|
14 |
+
from joblib import Parallel, delayed
|
15 |
+
import pandas as pd
|
16 |
+
import streamlit as st
|
17 |
+
from geopy.extra.rate_limiter import RateLimiter
|
18 |
+
from geopy.geocoders import Nominatim
|
19 |
+
import folium
|
20 |
+
from streamlit_folium import st_folium
|
21 |
+
import math
|
22 |
+
import geopandas as gpd
|
23 |
+
from skimage.io import imread
|
24 |
+
from streamlit_plotly_events import plotly_events
|
25 |
+
import requests
|
26 |
+
from requests.packages.urllib3.exceptions import InsecureRequestWarning
|
27 |
+
import rasterio
|
28 |
+
import rioxarray
|
29 |
+
import numpy as np
|
30 |
+
import io
|
31 |
+
# from urllib import request
|
32 |
+
# import certifi
|
33 |
+
# import ssl
|
34 |
+
# context = ssl.create_default_context(cafile=certifi.where())
|
35 |
+
# https_handler = request.HTTPSHandler(context=context)
|
36 |
+
# opener = request.build_opener(https_handler)
|
37 |
+
# request.install_opener(opener)
|
38 |
+
|
39 |
+
@st.cache(allow_output_mutation=True)
|
40 |
+
def geocode(address, buffer_size):
|
41 |
+
try:
|
42 |
+
address2 = address.replace(' ', '+').replace(',', '%2C')
|
43 |
+
df = pd.read_json(
|
44 |
+
f'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={address2}&benchmark=2020&format=json')
|
45 |
+
results = df.iloc[:1, 0][0][0]['coordinates']
|
46 |
+
lat, lon = results['y'], results['x']
|
47 |
+
except:
|
48 |
+
geolocator = Nominatim(user_agent="GTA Lookup")
|
49 |
+
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
|
50 |
+
location = geolocator.geocode(address)
|
51 |
+
lat, lon = location.latitude, location.longitude
|
52 |
+
|
53 |
+
df = pd.DataFrame({'Lat': [lat], 'Lon': [lon]})
|
54 |
+
gdf = gpd.GeoDataFrame(
|
55 |
+
df, geometry=gpd.points_from_xy(df.Lon, df.Lat, crs=4326))
|
56 |
+
gdf['buffer'] = gdf['geometry'].to_crs(
|
57 |
+
3857).buffer(buffer_size/2*2580).to_crs(4326)
|
58 |
+
return gdf
|
59 |
+
|
60 |
+
|
61 |
+
@st.cache(allow_output_mutation=True)
|
62 |
+
def get_pngs(date):
|
63 |
+
year, month, day = date[:4], date[4:6], date[6:]
|
64 |
+
url = f'https://mrms.nssl.noaa.gov/qvs/product_viewer/local/render_multi_domain_product_layer.php?mode=run&cpp_exec_dir=/home/metop/web/specific/opv/&web_resources_dir=/var/www/html/qvs/product_viewer/resources/&prod_root={prod_root}&qperate_pal_option=0&qpe_pal_option=0&year={year}&month={month}&day={day}&hour={hour}&minute={minute}&clon={lon}&clat={lat}&zoom={zoom}&width=920&height=630'
|
65 |
+
# data = imread(url,verify=False)[:, :, :3]
|
66 |
+
response = requests.get(url,verify=False)
|
67 |
+
image_data = io.BytesIO(response.content)
|
68 |
+
data = imread(image_data)[:, :, :3]
|
69 |
+
|
70 |
+
data2 = data.reshape(630*920, 3)
|
71 |
+
data2_df = pd.DataFrame(data2, columns=['R', 'G', 'B'])
|
72 |
+
data2_df2 = pd.merge(data2_df, lut[['R', 'G', 'B', 'Hail Scale', 'Hail Scale In']], on=['R', 'G', 'B'],
|
73 |
+
how='left')[['Hail Scale', 'Hail Scale In']]
|
74 |
+
data2_df2['Date'] = date
|
75 |
+
return data2_df2.reset_index()
|
76 |
+
|
77 |
+
|
78 |
+
@st.cache(allow_output_mutation=True)
|
79 |
+
def get_pngs_parallel(dates):
|
80 |
+
results1 = Parallel(n_jobs=32, prefer="threads")(
|
81 |
+
delayed(get_pngs)(i) for i in dates)
|
82 |
+
return results1
|
83 |
+
|
84 |
+
|
85 |
+
@st.cache(allow_output_mutation=True)
|
86 |
+
def png_data(date):
|
87 |
+
year, month, day = date[:4], date[4:6], date[6:]
|
88 |
+
url = f'https://mrms.nssl.noaa.gov/qvs/product_viewer/local/render_multi_domain_product_layer.php?mode=run&cpp_exec_dir=/home/metop/web/specific/opv/&web_resources_dir=/var/www/html/qvs/product_viewer/resources/&prod_root={prod_root}&qperate_pal_option=0&qpe_pal_option=0&year={year}&month={month}&day={day}&hour={hour}&minute={minute}&clon={lon}&clat={lat}&zoom={zoom}&width=920&height=630'
|
89 |
+
response = requests.get(url,verify=False)
|
90 |
+
image_data = io.BytesIO(response.content)
|
91 |
+
data = imread(image_data)
|
92 |
+
|
93 |
+
# data = imread(url,verify=False)
|
94 |
+
return data
|
95 |
+
|
96 |
+
|
97 |
+
@st.cache(allow_output_mutation=True)
|
98 |
+
def map_folium(data, gdf):
|
99 |
+
m = folium.Map(location=[lat, lon], zoom_start=zoom, height=300)
|
100 |
+
folium.Marker(
|
101 |
+
location=[lat, lon],
|
102 |
+
popup=address).add_to(m)
|
103 |
+
|
104 |
+
folium.GeoJson(gdf['buffer']).add_to(m)
|
105 |
folium.raster_layers.ImageOverlay(
|
106 |
data, opacity=0.8, bounds=bounds).add_to(m)
|
107 |
return m
|
|
|
315 |
st.markdown(""" <style>
|
316 |
#MainMenu {visibility: hidden;}
|
317 |
footer {visibility: hidden;}
|
318 |
+
</style> """, unsafe_allow_html=True)
|
319 |
+
|
320 |
+
|
321 |
+
|