Spaces:
Sleeping
Sleeping
mattritchey
commited on
Update main.py
Browse files
main.py
CHANGED
@@ -55,72 +55,72 @@ def get_data(address, start_date, end_date, radius_miles, get_max):
|
|
55 |
|
56 |
# Convert Lat Lon to row & col on Array
|
57 |
try:
|
58 |
-
transform = pickle.load(open('
|
59 |
row, col = rasterio.transform.rowcol(transform['affine'], lon, lat)
|
60 |
except:
|
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 |
-
return lat, lon, transform, row, col
|
124 |
|
125 |
@app.get('/APCP_Docker_Data')
|
126 |
async def predict(address: str, start_date: str, end_date: str, radius_miles: int, get_max: bool):
|
@@ -131,6 +131,6 @@ async def predict(address: str, start_date: str, end_date: str, radius_miles: in
|
|
131 |
except:
|
132 |
results = pd.DataFrame({'Date': ['error'], 'APCP_max': ['error']})
|
133 |
|
134 |
-
|
135 |
-
return results
|
136 |
|
|
|
55 |
|
56 |
# Convert Lat Lon to row & col on Array
|
57 |
try:
|
58 |
+
transform = pickle.load(open('Data/hrrr_crs.pkl', 'rb'))
|
59 |
row, col = rasterio.transform.rowcol(transform['affine'], lon, lat)
|
60 |
except:
|
61 |
+
row=col=1000
|
62 |
|
63 |
|
64 |
+
files = [
|
65 |
+
# 'Data/APCP_2024_hrrr_v2.h5',
|
66 |
+
'Data/APCP_2020_hrrr_v3.h5',
|
67 |
+
'Data/APCP_2021_hrrr_3.h5',
|
68 |
+
'Data/APCP_2022_hrrr_v2.h5',
|
69 |
+
# 'Data/APCP_2023_hrrr_v2c.h5'
|
70 |
+
]
|
71 |
+
|
72 |
+
files_choosen = [i for i in files if any(i for j in years if str(j) in i)]
|
73 |
+
|
74 |
+
|
75 |
+
# Query and Collect H5 Data
|
76 |
+
all_data = []
|
77 |
+
all_dates = []
|
78 |
+
for file in files_choosen:
|
79 |
+
with h5py.File(file, 'r') as f:
|
80 |
+
# Get Dates from H5
|
81 |
+
dates = f['date_time_hr'][:]
|
82 |
+
date_idx = np.where((dates >= int(start_date))
|
83 |
+
& (dates <= int(end_date)))[0]
|
84 |
+
|
85 |
+
# Select Data by Date and Radius
|
86 |
+
dates = dates[date_idx]
|
87 |
+
data = f['APCP'][date_idx, row-radius_miles:row +
|
88 |
+
radius_miles+1, col-radius_miles:col+radius_miles+1]
|
89 |
+
|
90 |
+
all_data.append(data)
|
91 |
+
all_dates.append(dates)
|
92 |
+
|
93 |
+
data_all = np.vstack(all_data)
|
94 |
+
dates_all = np.concatenate(all_dates)
|
95 |
+
|
96 |
+
# Convert to Inches
|
97 |
+
data_mat = np.where(data_all < 0, 0, data_all)*0.0393701
|
98 |
+
|
99 |
+
# Get Radius of Data
|
100 |
+
disk_mask = np.where(disk(radius_miles) == 1, True, False)
|
101 |
+
data_mat = np.where(disk_mask, data_mat, -1).round(3)
|
102 |
+
|
103 |
+
# Process to DataFrame
|
104 |
+
# Find Max of Data
|
105 |
+
if get_max == True:
|
106 |
+
data_max = np.max(data_mat, axis=(1, 2))
|
107 |
+
df_data = pd.DataFrame({'Date': dates_all,
|
108 |
+
'APCP_max': data_max})
|
109 |
+
# Get all Data
|
110 |
+
else:
|
111 |
+
data_all = list(data_mat)
|
112 |
+
df_data = pd.DataFrame({'Date': dates_all,
|
113 |
+
'APCP_all': data_all})
|
114 |
+
|
115 |
+
df_data['Date'] = pd.to_datetime(df_data['Date'], format='%Y%m%d%H')
|
116 |
+
df_data = df_data.set_index('Date')
|
117 |
+
|
118 |
+
df_data = df_data.reindex(date_range_days, fill_value=0).reset_index().rename(
|
119 |
+
columns={'index': 'Date'})
|
120 |
+
df_data['Date'] = df_data['Date'].dt.strftime('%Y-%m-%d:%H')
|
121 |
+
|
122 |
+
return df_data
|
123 |
+
# return lat, lon, transform, row, col
|
124 |
|
125 |
@app.get('/APCP_Docker_Data')
|
126 |
async def predict(address: str, start_date: str, end_date: str, radius_miles: int, get_max: bool):
|
|
|
131 |
except:
|
132 |
results = pd.DataFrame({'Date': ['error'], 'APCP_max': ['error']})
|
133 |
|
134 |
+
return results.to_json()
|
135 |
+
# return results
|
136 |
|