Spaces:
Sleeping
Sleeping
File size: 3,138 Bytes
3671cba ee259c3 3671cba eac0454 1fb03fe c4f7382 eac0454 3671cba 697988f c4f7382 d687e0e 1fb03fe eac0454 c4f7382 eac0454 1fb03fe c4f7382 1fb03fe c4f7382 1fb03fe e477662 1fb03fe c4f7382 1fb03fe e477662 c4f7382 1fb03fe c4f7382 1fb03fe c4f7382 1fb03fe c4f7382 1fb03fe c4f7382 1fb03fe c4f7382 1fb03fe c4f7382 1fb03fe c4f7382 4ccbe5b c4f7382 1fb03fe c4f7382 1fb03fe 4ccbe5b c4f7382 1fb03fe c4f7382 1fb03fe c4f7382 1fb03fe |
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 |
from fastapi import FastAPI
import uvicorn
import pandas as pd
import numpy as np
import pickle
import rasterio
import h5py
from skimage.morphology import disk
app = FastAPI()
#Endpoints
#Root endpoints
@app.get("/")
def root():
return {"API": "Hail Docker Data"}
def get_hail_data(lat, lon, start_date, end_date, radius_miles, get_max):
start_date = pd.Timestamp(str(start_date)).strftime('%Y%m%d')
end_date = pd.Timestamp(str(end_date)).strftime('%Y%m%d')
date_years = pd.date_range(start=start_date, end=end_date, freq='M')
date_range_days = pd.date_range(start_date, end_date)
years = list(set([d.year for d in date_years]))
if len(years) == 0:
years = [pd.Timestamp(start_date).year]
# Convert Lat Lon to row & col on Array
transform = pickle.load(open('Data/transform_mrms.pkl', 'rb'))
row, col = rasterio.transform.rowcol(transform, lon, lat)
files = [
'Data/2023_hail.h5',
'Data/2022_hail.h5',
'Data/2021_hail.h5',
'Data/2020_hail.h5'
]
files_choosen = [i for i in files if any(i for j in years if str(j) in i)]
# Query and Collect H5 Data
all_data = []
all_dates = []
for file in files_choosen:
with h5py.File(file, 'r') as f:
# Get Dates from H5
dates = f['dates'][:]
date_idx = np.where((dates >= int(start_date))
& (dates <= int(end_date)))[0]
# Select Data by Date and Radius
dates = dates[date_idx]
data = f['hail'][date_idx, row-radius_miles:row +
radius_miles+1, col-radius_miles:col+radius_miles+1]
all_data.append(data)
all_dates.append(dates)
data_all = np.vstack(all_data)
dates_all = np.concatenate(all_dates)
# Convert to Inches
data_mat = np.where(data_all < 0, 0, data_all)*0.0393701
# Get Radius of Data
disk_mask = np.where(disk(radius_miles) == 1, True, False)
data_mat = np.where(disk_mask, data_mat, -1).round(3)
# Process to DataFrame
# Find Max of Data
if get_max == True:
data_max = np.max(data_mat, axis=(1, 2))
df_data = pd.DataFrame({'Date': dates_all,
'Hail_max': data_max})
else:
data_all = list(data_mat)
df_data = pd.DataFrame({'Date': dates_all,
'Hail_all': data_mat})
df_data['Date'] = pd.to_datetime(df_data['Date'], format='%Y%m%d')
df_data = df_data.set_index('Date')
df_data = df_data.reindex(date_range_days, fill_value=0).reset_index().rename(
columns={'index': 'Date'})
df_data['Date'] = df_data['Date'].dt.strftime('%Y-%m-%d')
return df_data
@app.get('/Hail_Docker_Data')
async def predict(lat: float, lon: float, start_date: str, end_date: str, radius_miles: int, get_max: bool):
try:
results = get_hail_data(lat, lon, start_date,
end_date, radius_miles, get_max)
except:
results = pd.DataFrame({'Date': ['error'], 'Hail_max': ['error']})
return results.to_json()
|