File size: 11,676 Bytes
979b9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2752b4d
5e8a806
 
 
 
 
 
 
979b9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2752b4d
 
 
 
 
979b9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92a9734
2752b4d
 
 
 
979b9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
013be0c
979b9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322


# -*- coding: utf-8 -*-
"""
Created on Tue Dec  6 09:56:29 2022

@author: mritchey
"""
#streamlit run "C:\Users\mritchey\.spyder-py3\Python Scripts\streamlit projects\mrms\mrms_hail2 buffer.py"

import plotly.express as px
import os
from PIL import Image
from joblib import Parallel, delayed
import pandas as pd
import streamlit as st
from geopy.extra.rate_limiter import RateLimiter
from geopy.geocoders import Nominatim
import folium
from streamlit_folium import st_folium
import math
import geopandas as gpd
from skimage.io import imread
from streamlit_plotly_events import plotly_events
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import rasterio
import rioxarray
import numpy as np
import io
# from urllib import request
# import certifi
# import ssl
# context = ssl.create_default_context(cafile=certifi.where())
# https_handler = request.HTTPSHandler(context=context)
# opener = request.build_opener(https_handler)
# request.install_opener(opener)

@st.cache(allow_output_mutation=True)
def geocode(address, buffer_size):
    try:
        address2 = address.replace(' ', '+').replace(',', '%2C')
        df = pd.read_json(
            f'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={address2}&benchmark=2020&format=json')
        results = df.iloc[:1, 0][0][0]['coordinates']
        lat, lon = results['y'], results['x']
    except:
        geolocator = Nominatim(user_agent="GTA Lookup")
        geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
        location = geolocator.geocode(address)
        lat, lon = location.latitude, location.longitude

    df = pd.DataFrame({'Lat': [lat], 'Lon': [lon]})
    gdf = gpd.GeoDataFrame(
        df, geometry=gpd.points_from_xy(df.Lon, df.Lat, crs=4326))
    gdf['buffer'] = gdf['geometry'].to_crs(
        3857).buffer(buffer_size/2*2580).to_crs(4326)
    return gdf


@st.cache(allow_output_mutation=True)
def get_pngs(date):
    year, month, day = date[:4], date[4:6], date[6:]
    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'
    # data = imread(url,verify=False)[:, :, :3]
    response = requests.get(url,verify=False)
    image_data = io.BytesIO(response.content)
    data = imread(image_data)[:, :, :3]
    
    data2 = data.reshape(630*920, 3)
    data2_df = pd.DataFrame(data2, columns=['R', 'G', 'B'])
    data2_df2 = pd.merge(data2_df, lut[['R', 'G', 'B', 'Hail Scale', 'Hail Scale In']], on=['R', 'G', 'B'],
                         how='left')[['Hail Scale', 'Hail Scale In']]
    data2_df2['Date'] = date
    return data2_df2.reset_index()


@st.cache(allow_output_mutation=True)
def get_pngs_parallel(dates):
    results1 = Parallel(n_jobs=32, prefer="threads")(
        delayed(get_pngs)(i) for i in dates)
    return results1


@st.cache(allow_output_mutation=True)
def png_data(date):
    year, month, day = date[:4], date[4:6], date[6:]
    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'
    response = requests.get(url,verify=False)
    image_data = io.BytesIO(response.content)
    data = imread(image_data)

    # data = imread(url,verify=False)
    return data


@st.cache(allow_output_mutation=True)
def map_folium(data, gdf):
    m = folium.Map(location=[lat, lon],  zoom_start=zoom, height=300)
    folium.Marker(
        location=[lat, lon],
        popup=address).add_to(m)

    folium.GeoJson(gdf['buffer']).add_to(m)
    folium.raster_layers.ImageOverlay(
        data, opacity=0.8, bounds=bounds).add_to(m)
    return m


def to_radians(degrees):
  return degrees * math.pi / 180


def lat_lon_to_bounds(lat, lng, zoom, width, height):
    earth_cir_m = 40075016.686
    degreesPerMeter = 360 / earth_cir_m
    m_pixel_ew = earth_cir_m / math.pow(2, zoom + 8)
    m_pixel_ns = earth_cir_m / \
        math.pow(2, zoom + 8) * math.cos(to_radians(lat))

    shift_m_ew = width/2 * m_pixel_ew
    shift_m_ns = height/2 * m_pixel_ns

    shift_deg_ew = shift_m_ew * degreesPerMeter
    shift_deg_ns = shift_m_ns * degreesPerMeter

    return [[lat-shift_deg_ns, lng-shift_deg_ew], [lat+shift_deg_ns, lng+shift_deg_ew]]


def image_to_geotiff(bounds, input_file_path, output_file_path='template.tiff'):
    south, west, north, east = tuple(
        [item for sublist in bounds for item in sublist])
    dataset = rasterio.open(input_file_path, 'r')
    bands = [1, 2, 3]
    data = dataset.read(bands)
    transform = rasterio.transform.from_bounds(west, south, east, north,
                                               height=data.shape[1],
                                               width=data.shape[2])
    crs = {'init': 'epsg:4326'}

    with rasterio.open(output_file_path, 'w', driver='GTiff',
                       height=data.shape[1],
                       width=data.shape[2],
                       count=3, dtype=data.dtype, nodata=0,
                       transform=transform, crs=crs,
                       compress='lzw') as dst:
        dst.write(data, indexes=bands)


def get_mask(bounds, buffer_size):
    year, month, day = date[:4], date[4:6], date[6:]
    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'
    img_data = requests.get(url, verify=False).content
    input_file_path = f'image_name_{date}_{var}.png'
    output_file_path = 'template.tiff'
    with open(input_file_path, 'wb') as handler:
        handler.write(img_data)

    image_to_geotiff(bounds, input_file_path, output_file_path)
    rds = rioxarray.open_rasterio(output_file_path)
    # rds.plot.imshow()

    rds = rds.assign_coords(distance=(haversine(rds.x, rds.y, lon, lat)))
    mask = rds['distance'].values <= buffer_size
    mask = np.transpose(np.stack([mask, mask, mask]), (1, 2, 0))
    return mask


def haversine(lon1, lat1, lon2, lat2):
    # convert decimal degrees to radians
    lon1 = np.deg2rad(lon1)
    lon2 = np.deg2rad(lon2)
    lat1 = np.deg2rad(lat1)
    lat2 = np.deg2rad(lat2)

    # haversine formula
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = np.sin(dlat/2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2)**2
    c = 2 * np.arcsin(np.sqrt(a))
    r = 6371
    return c * r


#Set Columns
st.set_page_config(layout="wide")
col1, col2, col3 = st.columns((3))
col1, col2, col3 = st.columns((3, 3, 1))

#Input Data
zoom = 10
_ = st.sidebar.text_input(
    "Claim Number", "836-xxxxxxx")
address = st.sidebar.text_input(
    "Address", "1000 Main St, Cincinnati, OH 45202")

date = st.sidebar.date_input("Date",  pd.Timestamp(
    2022, 7, 6), key='date').strftime('%Y%m%d')
d = pd.Timestamp(date)
days_within = st.sidebar.selectbox('Within Days:', (5, 30, 60, 90, 180))
var = 'Hail'
var_input = 'hails&product=MESHMAX1440M'
mask_select = st.sidebar.radio('Only Show Buffer Data:', ("No", "Yes"))
buffer_size = st.sidebar.radio('Buffer Size (miles):', (5, 10, 15))

year, month, day = date[:4], date[4:6], date[6:]
hour = 23
minute = 30

prod_root = var_input[var_input.find('=')+1:]

#Geocode
gdf = geocode(address, buffer_size)
lat, lon = tuple(gdf[['Lat', 'Lon']].values[0])

#Get Value
url = 'https://mrms.nssl.noaa.gov/qvs/product_viewer/local/get_multi_domain_rect_binary_value.php?mode=run&cpp_exec_dir=/home/metop/web/specific/opv/&web_resources_dir=/var/www/html/qvs/product_viewer/resources/'\
    + f'&prod_root={prod_root}&lon={lon}&lat={lat}&year={year}&month={month}&day={day}&hour={hour}&minute={minute}'

response = requests.get(url, verify=False).json()
qvs_values = pd.DataFrame(response, index=[0])[
    ['qvs_value', 'qvs_units']].values[0]
qvs_value = qvs_values[0]
qvs_unit = qvs_values[1]

#Get PNG Focus
data = png_data(date)

#Legend
legend = Image.open('hail scale3b.png')

#Get PNG Max
start_date, end_date = d - \
    pd.Timedelta(days=days_within), d+pd.Timedelta(days=days_within)
dates = pd.date_range(start_date,
                      end_date).strftime('%Y%m%d')
lut = pd.read_csv('hail scale.csv')
bounds = lat_lon_to_bounds(lat, lon, zoom, 920, 630)


results1 = get_pngs_parallel(dates)
# results1 = Parallel(n_jobs=32, prefer="threads")(delayed(get_pngs)(i) for i in dates)
results = pd.concat(results1)
max_data = results.groupby('index')[['Hail Scale']].max()

max_data2 = pd.merge(max_data,
                     lut[['R', 'G', 'B', 'Hail Scale']],
                     on=['Hail Scale'],
                     how='left')[['R', 'G', 'B']]

data_max = max_data2.values.reshape(630, 920, 3)

#Masked Data
if mask_select == "Yes":
    mask = get_mask(bounds, buffer_size)
    mask1 = mask[:, :, 0].reshape(630*920)
    results = pd.concat([i[mask1] for i in results1])
    data_max = data_max*mask
else:
    pass


#Bar
bar = results.query("`Hail Scale`>4").groupby(
    ['Date', 'Hail Scale In'])['index'].count().reset_index()
bar['Date'] = pd.to_datetime(bar['Date'])

bar = bar.reset_index()
bar.columns = ['level_0', 'Date', 'Hail Scale In', 'count']
bar['Hail Scale In'] = bar['Hail Scale In'].astype(str)
bar = bar.sort_values('Hail Scale In', ascending=True)

color_discrete_map = lut[['Hail Scale In', 'c_code']].sort_values(
    'Hail Scale In', ascending=True).astype(str)
color_discrete_map = color_discrete_map.set_index(
    'Hail Scale In').to_dict()['c_code']

fig = px.bar(bar, x="Date", y="count", color="Hail Scale In",
             barmode='stack',
             color_discrete_map=color_discrete_map)

#Submit Url to New Tab
url = f'https://mrms.nssl.noaa.gov/qvs/product_viewer/index.php?web_exec_mode=run&menu=menu_config.txt&year={year}&month={month}&day={day}&hour=23&minute=30&time_mode=static&zoom=9&clon={lon}&clat={lat}&base=0&overlays=1&mping_mode=0&product_type={var_input}&qpe_pal_option=0&opacity=.75&looping_active=off&num_frames=6&frame_step=200&seconds_step=600'

#Map Focus
m = map_folium(data, gdf)
#Map Max
m_max = map_folium(data_max, gdf)

with st.container():
    col1, col2, col3 = st.columns((1, 2, 2))
    with col1:
        link = f'[Go To MRMS Site]({url})'
        st.markdown(link, unsafe_allow_html=True)
        st.image(legend)
    with col2:
        st.header(f'{var} on {pd.Timestamp(date).strftime("%D")}')
        st_folium(m, height=300)
    with col3:
        st.header(
            f'Max from {start_date.strftime("%D")} to {end_date.strftime("%D")}')
        st_folium(m_max, height=300)

try:
    selected_points = plotly_events(fig, click_event=True, hover_event=False)
    date2 = pd.Timestamp(selected_points[0]['x']).strftime('%Y%m%d')
    data2 = png_data(date2)
    m3 = map_folium(data2, gdf)
    st.header(f'{var} on {pd.Timestamp(date2).strftime("%D")}')
    st_folium(m3, height=300)
except:
    pass


st.markdown(""" <style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style> """, unsafe_allow_html=True)