mattritchey commited on
Commit
e896716
·
verified ·
1 Parent(s): 99b4a85

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +68 -67
main.py CHANGED
@@ -41,14 +41,14 @@ def get_hail_data(address, start_date, end_date, radius_miles, get_max):
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)
@@ -60,64 +60,64 @@ def get_hail_data(address, start_date, end_date, radius_miles, get_max):
60
  row, col = rasterio.transform.rowcol(transform, lon, lat)
61
 
62
 
63
- files = [
64
- 'Data/2023_hail.h5',
65
- 'Data/2022_hail.h5',
66
- 'Data/2021_hail.h5',
67
- 'Data/2020_hail.h5'
68
- ]
69
-
70
- files_choosen = [i for i in files if any(i for j in years if str(j) in i)]
71
-
72
- # Query and Collect H5 Data
73
- all_data = []
74
- all_dates = []
75
- for file in files_choosen:
76
- with h5py.File(file, 'r') as f:
77
- # Get Dates from H5
78
- dates = f['dates'][:]
79
- date_idx = np.where((dates >= int(start_date))
80
- & (dates <= int(end_date)))[0]
81
-
82
- # Select Data by Date and Radius
83
- dates = dates[date_idx]
84
- data = f['hail'][date_idx, row-radius_miles:row +
85
- radius_miles+1, col-radius_miles:col+radius_miles+1]
86
-
87
- all_data.append(data)
88
- all_dates.append(dates)
89
-
90
- data_all = np.vstack(all_data)
91
- dates_all = np.concatenate(all_dates)
92
-
93
- # Convert to Inches
94
- data_mat = np.where(data_all < 0, 0, data_all)*0.0393701
95
-
96
- # Get Radius of Data
97
- disk_mask = np.where(disk(radius_miles) == 1, True, False)
98
- data_mat = np.where(disk_mask, data_mat, -1).round(3)
99
-
100
- # Process to DataFrame
101
- # Find Max of Data
102
- if get_max == True:
103
- data_max = np.max(data_mat, axis=(1, 2))
104
- df_data = pd.DataFrame({'Date': dates_all,
105
- 'Hail_max': data_max})
106
- # Get all Data
107
- else:
108
- data_all = list(data_mat)
109
- df_data = pd.DataFrame({'Date': dates_all,
110
- 'Hail_all': data_all})
111
-
112
- df_data['Date'] = pd.to_datetime(df_data['Date'], format='%Y%m%d')
113
- df_data = df_data.set_index('Date')
114
-
115
- df_data = df_data.reindex(date_range_days, fill_value=0).reset_index().rename(
116
- columns={'index': 'Date'})
117
- df_data['Date'] = df_data['Date'].dt.strftime('%Y-%m-%d')
118
-
119
- return df_data
120
-
121
 
122
  @app.get('/Hail_Docker_Data')
123
  async def predict(address: str, start_date: str, end_date: str, radius_miles: int, get_max: bool):
@@ -128,5 +128,6 @@ async def predict(address: str, start_date: str, end_date: str, radius_miles: in
128
  except:
129
  results = pd.DataFrame({'Date': ['error'], 'Hail_max': ['error']})
130
 
131
- return results.to_json()
 
132
 
 
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)
 
60
  row, col = rasterio.transform.rowcol(transform, lon, lat)
61
 
62
 
63
+ # files = [
64
+ # 'Data/2023_hail.h5',
65
+ # 'Data/2022_hail.h5',
66
+ # 'Data/2021_hail.h5',
67
+ # 'Data/2020_hail.h5'
68
+ # ]
69
+
70
+ # files_choosen = [i for i in files if any(i for j in years if str(j) in i)]
71
+
72
+ # # Query and Collect H5 Data
73
+ # all_data = []
74
+ # all_dates = []
75
+ # for file in files_choosen:
76
+ # with h5py.File(file, 'r') as f:
77
+ # # Get Dates from H5
78
+ # dates = f['dates'][:]
79
+ # date_idx = np.where((dates >= int(start_date))
80
+ # & (dates <= int(end_date)))[0]
81
+
82
+ # # Select Data by Date and Radius
83
+ # dates = dates[date_idx]
84
+ # data = f['hail'][date_idx, row-radius_miles:row +
85
+ # radius_miles+1, col-radius_miles:col+radius_miles+1]
86
+
87
+ # all_data.append(data)
88
+ # all_dates.append(dates)
89
+
90
+ # data_all = np.vstack(all_data)
91
+ # dates_all = np.concatenate(all_dates)
92
+
93
+ # # Convert to Inches
94
+ # data_mat = np.where(data_all < 0, 0, data_all)*0.0393701
95
+
96
+ # # Get Radius of Data
97
+ # disk_mask = np.where(disk(radius_miles) == 1, True, False)
98
+ # data_mat = np.where(disk_mask, data_mat, -1).round(3)
99
+
100
+ # # Process to DataFrame
101
+ # # Find Max of Data
102
+ # if get_max == True:
103
+ # data_max = np.max(data_mat, axis=(1, 2))
104
+ # df_data = pd.DataFrame({'Date': dates_all,
105
+ # 'Hail_max': data_max})
106
+ # # Get all Data
107
+ # else:
108
+ # data_all = list(data_mat)
109
+ # df_data = pd.DataFrame({'Date': dates_all,
110
+ # 'Hail_all': data_all})
111
+
112
+ # df_data['Date'] = pd.to_datetime(df_data['Date'], format='%Y%m%d')
113
+ # df_data = df_data.set_index('Date')
114
+
115
+ # df_data = df_data.reindex(date_range_days, fill_value=0).reset_index().rename(
116
+ # columns={'index': 'Date'})
117
+ # df_data['Date'] = df_data['Date'].dt.strftime('%Y-%m-%d')
118
+
119
+ # return df_data
120
+ return lat, lon,row, col
121
 
122
  @app.get('/Hail_Docker_Data')
123
  async def predict(address: str, start_date: str, end_date: str, radius_miles: int, get_max: bool):
 
128
  except:
129
  results = pd.DataFrame({'Date': ['error'], 'Hail_max': ['error']})
130
 
131
+ # return results.to_json()
132
+ return results
133