Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,117 +1,103 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
import uvicorn
|
| 3 |
|
| 4 |
-
from geopy.extra.rate_limiter import RateLimiter
|
| 5 |
-
from geopy.geocoders import Nominatim
|
| 6 |
import pandas as pd
|
| 7 |
import numpy as np
|
| 8 |
import pickle
|
| 9 |
import rasterio
|
| 10 |
import h5py
|
| 11 |
-
from skimage.morphology import disk
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
|
| 16 |
-
#Endpoints
|
| 17 |
#Root endpoints
|
| 18 |
@app.get("/")
|
| 19 |
def root():
|
| 20 |
return {"API": "Hail Docker Data"}
|
| 21 |
|
| 22 |
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
-
try:
|
| 26 |
-
address2 = address.replace(' ', '+').replace(',', '%2C')
|
| 27 |
-
df = pd.read_json(
|
| 28 |
-
f'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={address2}&benchmark=2020&format=json')
|
| 29 |
-
results = df.iloc[:1, 0][0][0]['coordinates']
|
| 30 |
-
lat, lon = results['y'], results['x']
|
| 31 |
-
except:
|
| 32 |
-
geolocator = Nominatim(user_agent='GTA Lookup')
|
| 33 |
-
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=2)
|
| 34 |
-
location = geolocator.geocode(address)
|
| 35 |
-
lat, lon = location.latitude, location.longitude
|
| 36 |
-
|
| 37 |
-
return lat, lon
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def get_hail_data(address, start_date,end_date,radius_miles):
|
| 41 |
|
| 42 |
start_date = pd.Timestamp(str(start_date)).strftime('%Y%m%d')
|
| 43 |
end_date = pd.Timestamp(str(end_date)).strftime('%Y%m%d')
|
| 44 |
date_years = pd.date_range(start=start_date, end=end_date, freq='M')
|
|
|
|
| 45 |
years = list(set([d.year for d in date_years]))
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# Geocode Address
|
| 52 |
-
lat, lon= geocode_address(address)
|
| 53 |
-
|
| 54 |
# Convert Lat Lon to row & col on Array
|
| 55 |
-
transform = pickle.load(open('
|
| 56 |
row, col = rasterio.transform.rowcol(transform, lon, lat)
|
| 57 |
-
|
| 58 |
files = [
|
| 59 |
-
'
|
| 60 |
-
'
|
| 61 |
-
'
|
| 62 |
-
'
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
files_choosen=[i for i in files if any(i for j in years if str(j) in i)]
|
| 66 |
-
|
| 67 |
-
# Query and Collect H5 Data
|
| 68 |
-
all_data=[]
|
| 69 |
-
all_dates=[]
|
| 70 |
for file in files_choosen:
|
| 71 |
with h5py.File(file, 'r') as f:
|
| 72 |
# Get Dates from H5
|
| 73 |
dates = f['dates'][:]
|
| 74 |
-
date_idx=np.where((dates>=int(start_date))
|
| 75 |
-
|
|
|
|
| 76 |
# Select Data by Date and Radius
|
| 77 |
-
dates=dates[date_idx]
|
| 78 |
data = f['hail'][date_idx, row-radius_miles:row +
|
| 79 |
-
|
| 80 |
-
|
| 81 |
all_data.append(data)
|
| 82 |
all_dates.append(dates)
|
| 83 |
-
|
| 84 |
-
data_all=np.vstack(all_data)
|
| 85 |
-
dates_all=np.concatenate(all_dates)
|
| 86 |
-
|
| 87 |
# Convert to Inches
|
| 88 |
-
data_mat = np.where(data_all < 0, 0, data_all)*0.0393701
|
| 89 |
-
|
| 90 |
# Get Radius of Data
|
| 91 |
-
disk_mask = np.where(disk(radius_miles)==1,True, False)
|
| 92 |
-
data_mat = np.where(disk_mask, data_mat, -1)
|
| 93 |
-
|
| 94 |
-
# Find Max of Data
|
| 95 |
-
data_max = np.max(data_mat, axis=(1, 2)).round(3)
|
| 96 |
-
|
| 97 |
# Process to DataFrame
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
return df_data
|
| 107 |
|
| 108 |
-
|
| 109 |
@app.get('/Hail_Docker_Data')
|
| 110 |
-
async def predict(
|
| 111 |
-
|
| 112 |
try:
|
| 113 |
-
results = get_hail_data(
|
|
|
|
| 114 |
except:
|
| 115 |
-
results=pd.DataFrame({'Date':['error'],'
|
| 116 |
-
|
| 117 |
return results.to_json()
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
import uvicorn
|
| 3 |
|
|
|
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
import numpy as np
|
| 6 |
import pickle
|
| 7 |
import rasterio
|
| 8 |
import h5py
|
| 9 |
+
from skimage.morphology import disk
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
|
| 14 |
+
#Endpoints
|
| 15 |
#Root endpoints
|
| 16 |
@app.get("/")
|
| 17 |
def root():
|
| 18 |
return {"API": "Hail Docker Data"}
|
| 19 |
|
| 20 |
|
| 21 |
+
def get_hail_data(lat, lon, start_date, end_date, radius_miles, get_max):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
start_date = pd.Timestamp(str(start_date)).strftime('%Y%m%d')
|
| 24 |
end_date = pd.Timestamp(str(end_date)).strftime('%Y%m%d')
|
| 25 |
date_years = pd.date_range(start=start_date, end=end_date, freq='M')
|
| 26 |
+
date_range_days = pd.date_range(start_date, end_date)
|
| 27 |
years = list(set([d.year for d in date_years]))
|
| 28 |
+
|
| 29 |
+
if len(years) == 0:
|
| 30 |
+
years = [pd.Timestamp(start_date).year]
|
| 31 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Convert Lat Lon to row & col on Array
|
| 33 |
+
transform = pickle.load(open('transform_mrms.pkl', 'rb'))
|
| 34 |
row, col = rasterio.transform.rowcol(transform, lon, lat)
|
| 35 |
+
|
| 36 |
files = [
|
| 37 |
+
'C:/Users/mritchey/Documents/Desktop 2/Inforce with Geocodes/Hail/Hail Mesh/raw data 1440/2023_hail.h5',
|
| 38 |
+
'C:/Users/mritchey/Documents/Desktop 2/Inforce with Geocodes/Hail/Hail Mesh/raw data 1440/2022_hail.h5',
|
| 39 |
+
'C:/Users/mritchey/Documents/Desktop 2/Inforce with Geocodes/Hail/Hail Mesh/raw data 1440/2021_hail.h5',
|
| 40 |
+
'C:/Users/mritchey/Documents/Desktop 2/Inforce with Geocodes/Hail/Hail Mesh/raw data 1440/2020_hail.h5'
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
files_choosen = [i for i in files if any(i for j in years if str(j) in i)]
|
| 44 |
+
|
| 45 |
+
# Query and Collect H5 Data
|
| 46 |
+
all_data = []
|
| 47 |
+
all_dates = []
|
| 48 |
for file in files_choosen:
|
| 49 |
with h5py.File(file, 'r') as f:
|
| 50 |
# Get Dates from H5
|
| 51 |
dates = f['dates'][:]
|
| 52 |
+
date_idx = np.where((dates >= int(start_date))
|
| 53 |
+
& (dates <= int(end_date)))[0]
|
| 54 |
+
|
| 55 |
# Select Data by Date and Radius
|
| 56 |
+
dates = dates[date_idx]
|
| 57 |
data = f['hail'][date_idx, row-radius_miles:row +
|
| 58 |
+
radius_miles+1, col-radius_miles:col+radius_miles+1]
|
| 59 |
+
|
| 60 |
all_data.append(data)
|
| 61 |
all_dates.append(dates)
|
| 62 |
+
|
| 63 |
+
data_all = np.vstack(all_data)
|
| 64 |
+
dates_all = np.concatenate(all_dates)
|
| 65 |
+
|
| 66 |
# Convert to Inches
|
| 67 |
+
data_mat = np.where(data_all < 0, 0, data_all)*0.0393701
|
| 68 |
+
|
| 69 |
# Get Radius of Data
|
| 70 |
+
disk_mask = np.where(disk(radius_miles) == 1, True, False)
|
| 71 |
+
data_mat = np.where(disk_mask, data_mat, -1).round(3)
|
| 72 |
+
|
|
|
|
|
|
|
|
|
|
| 73 |
# Process to DataFrame
|
| 74 |
+
# Find Max of Data
|
| 75 |
+
if get_max == True:
|
| 76 |
+
data_max = np.max(data_mat, axis=(1, 2))
|
| 77 |
+
df_data = pd.DataFrame({'Date': dates_all,
|
| 78 |
+
'Hail_max': data_max})
|
| 79 |
+
else:
|
| 80 |
+
data_all = list(data_mat)
|
| 81 |
+
df_data = pd.DataFrame({'Date': dates_all,
|
| 82 |
+
'Hail_all': data_all})
|
| 83 |
+
|
| 84 |
+
df_data['Date'] = pd.to_datetime(df_data['Date'], format='%Y%m%d')
|
| 85 |
+
df_data = df_data.set_index('Date')
|
| 86 |
+
|
| 87 |
+
df_data = df_data.reindex(date_range_days, fill_value=0).reset_index().rename(
|
| 88 |
+
columns={'index': 'Date'})
|
| 89 |
+
df_data['Date'] = df_data['Date'].dt.strftime('%Y-%m-%d')
|
| 90 |
+
|
| 91 |
return df_data
|
| 92 |
|
| 93 |
+
|
| 94 |
@app.get('/Hail_Docker_Data')
|
| 95 |
+
async def predict(lat: float, lon: float, start_date: int, end_date: int, radius_miles: int, get_max: bool):
|
| 96 |
+
|
| 97 |
try:
|
| 98 |
+
results = get_hail_data(lat, lon, start_date,
|
| 99 |
+
end_date, radius_miles, get_max)
|
| 100 |
except:
|
| 101 |
+
results = pd.DataFrame({'Date': ['error'], 'Hail_max': ['error']})
|
| 102 |
+
|
| 103 |
return results.to_json()
|