Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -37,91 +37,88 @@ def geocode_address(address):
|
|
| 37 |
|
| 38 |
def get_hail_data(address, start_date, end_date, radius_miles, get_max):
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
# Geocode Address
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
except:
|
| 57 |
-
lat, lon=None
|
| 58 |
# Convert Lat Lon to row & col on Array
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
row=col=None
|
| 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 |
@app.get('/Hail_Docker_Data')
|
| 127 |
async def predict(address: str, start_date: str, end_date: str, radius_miles: int, get_max: bool):
|
|
@@ -132,6 +129,6 @@ async def predict(address: str, start_date: str, end_date: str, radius_miles: in
|
|
| 132 |
except:
|
| 133 |
results = pd.DataFrame({'Date': ['error'], 'Hail_max': ['error']})
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
|
|
|
|
| 37 |
|
| 38 |
def get_hail_data(address, start_date, end_date, radius_miles, get_max):
|
| 39 |
|
| 40 |
+
resolution=1 # mrms 1 and hrrr is 3
|
| 41 |
+
radius = int(np.ceil(radius_miles*1.6/resolution))
|
| 42 |
|
| 43 |
|
| 44 |
+
start_date = pd.Timestamp(str(start_date)).strftime('%Y%m%d')
|
| 45 |
+
end_date = pd.Timestamp(str(end_date)).strftime('%Y%m%d')
|
| 46 |
+
date_years = pd.date_range(start=start_date, end=end_date, freq='M')
|
| 47 |
+
date_range_days = pd.date_range(start_date, end_date)
|
| 48 |
+
years = list(set([d.year for d in date_years]))
|
| 49 |
+
|
| 50 |
+
if len(years) == 0:
|
| 51 |
+
years = [pd.Timestamp(start_date).year]
|
| 52 |
|
| 53 |
# Geocode Address
|
| 54 |
+
lat, lon= geocode_address(address)
|
| 55 |
+
|
|
|
|
|
|
|
| 56 |
# Convert Lat Lon to row & col on Array
|
| 57 |
+
|
| 58 |
+
transform = pickle.load(open('Data/transform_mrms.pkl', 'rb'))
|
| 59 |
+
|
| 60 |
+
row, col = rasterio.transform.rowcol(transform, lon, lat)
|
| 61 |
+
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
+
files = [
|
| 65 |
+
'Data/2023_hail.h5',
|
| 66 |
+
'Data/2022_hail.h5',
|
| 67 |
+
'Data/2021_hail.h5',
|
| 68 |
+
'Data/2020_hail.h5'
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
files_choosen = [i for i in files if any(i for j in years if str(j) in i)]
|
| 72 |
+
|
| 73 |
+
# Query and Collect H5 Data
|
| 74 |
+
all_data = []
|
| 75 |
+
all_dates = []
|
| 76 |
+
for file in files_choosen:
|
| 77 |
+
with h5py.File(file, 'r') as f:
|
| 78 |
+
# Get Dates from H5
|
| 79 |
+
dates = f['dates'][:]
|
| 80 |
+
date_idx = np.where((dates >= int(start_date))
|
| 81 |
+
& (dates <= int(end_date)))[0]
|
| 82 |
+
|
| 83 |
+
# Select Data by Date and Radius
|
| 84 |
+
dates = dates[date_idx]
|
| 85 |
+
data = f['hail'][date_idx, row-radius_miles:row +
|
| 86 |
+
radius_miles+1, col-radius_miles:col+radius_miles+1]
|
| 87 |
+
|
| 88 |
+
all_data.append(data)
|
| 89 |
+
all_dates.append(dates)
|
| 90 |
+
|
| 91 |
+
data_all = np.vstack(all_data)
|
| 92 |
+
dates_all = np.concatenate(all_dates)
|
| 93 |
+
|
| 94 |
+
# Convert to Inches
|
| 95 |
+
data_mat = np.where(data_all < 0, 0, data_all)*0.0393701
|
| 96 |
+
|
| 97 |
+
# Get Radius of Data
|
| 98 |
+
disk_mask = np.where(disk(radius_miles) == 1, True, False)
|
| 99 |
+
data_mat = np.where(disk_mask, data_mat, -1).round(3)
|
| 100 |
+
|
| 101 |
+
# Process to DataFrame
|
| 102 |
+
# Find Max of Data
|
| 103 |
+
if get_max == True:
|
| 104 |
+
data_max = np.max(data_mat, axis=(1, 2))
|
| 105 |
+
df_data = pd.DataFrame({'Date': dates_all,
|
| 106 |
+
'Hail_max': data_max})
|
| 107 |
+
# Get all Data
|
| 108 |
+
else:
|
| 109 |
+
data_all = list(data_mat)
|
| 110 |
+
df_data = pd.DataFrame({'Date': dates_all,
|
| 111 |
+
'Hail_all': data_all})
|
| 112 |
+
|
| 113 |
+
df_data['Date'] = pd.to_datetime(df_data['Date'], format='%Y%m%d')
|
| 114 |
+
df_data = df_data.set_index('Date')
|
| 115 |
+
|
| 116 |
+
df_data = df_data.reindex(date_range_days, fill_value=0).reset_index().rename(
|
| 117 |
+
columns={'index': 'Date'})
|
| 118 |
+
df_data['Date'] = df_data['Date'].dt.strftime('%Y-%m-%d')
|
| 119 |
+
|
| 120 |
+
return df_data
|
| 121 |
+
|
| 122 |
|
| 123 |
@app.get('/Hail_Docker_Data')
|
| 124 |
async def predict(address: str, start_date: str, end_date: str, radius_miles: int, get_max: bool):
|
|
|
|
| 129 |
except:
|
| 130 |
results = pd.DataFrame({'Date': ['error'], 'Hail_max': ['error']})
|
| 131 |
|
| 132 |
+
return results.to_json()
|
| 133 |
+
|
| 134 |
|