mattritchey commited on
Commit
b0d4da1
·
verified ·
1 Parent(s): 1a57363

Update main.py

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