nakas commited on
Commit
fad1ae6
·
verified ·
1 Parent(s): d47ce5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -20
app.py CHANGED
@@ -10,7 +10,7 @@ import sys
10
  import matplotlib.pyplot as plt
11
  from matplotlib.gridspec import GridSpec
12
  import matplotlib.dates as mdates
13
- from windrose import WindRose, WindroseAxes
14
  from datetime import datetime
15
 
16
  # Install Playwright browsers on startup
@@ -130,20 +130,22 @@ def parse_weather_data(data):
130
 
131
  return df
132
 
133
- def create_wind_rose(df, ax):
134
  """Create a wind rose plot"""
 
 
 
 
135
  ws = df['wind_speed'].dropna().values
136
  wd = df['wind_dir_deg'].dropna().values
137
 
138
- # Define the wind speed bins
139
- bins = np.array([0, 5, 10, 15, 20, 25, 30, 35, 40])
140
-
141
- # Create the wind rose
142
- ax.bar(wd, ws, nsector=36, normed=True, opening=0.8,
143
- bins=bins, colors=plt.cm.viridis(np.linspace(0, 1, len(bins)-1)))
144
-
145
- # Set legend
146
- ax.set_legend(title='Wind Speed (mph)', bbox_to_anchor=(1.2, 0.5))
147
 
148
  def create_daily_wind_roses(df):
149
  """Create wind roses for each day"""
@@ -154,16 +156,11 @@ def create_daily_wind_roses(df):
154
  if len(group) > 0: # Only create rose if we have data
155
  fig = plt.figure(figsize=(8, 8))
156
  ax = WindroseAxes.from_ax(fig=fig)
157
- ws = group['wind_speed'].dropna().values
158
- wd = group['wind_dir_deg'].dropna().values
159
- if len(ws) > 0 and len(wd) > 0:
160
- ax.bar(wd, ws, nsector=36, normed=True, opening=0.8,
161
- bins=np.array([0, 5, 10, 15, 20, 25, 30, 35, 40]),
162
- colors=plt.cm.viridis(np.linspace(0, 1, 8)))
163
- ax.set_legend(title='Wind Speed (mph)', bbox_to_anchor=(1.2, 0.5))
164
  ax.set_title(f'Wind Rose - {date.strftime("%Y-%m-%d")}')
165
  plt.tight_layout()
166
  roses.append(fig)
 
167
 
168
  return roses
169
 
@@ -231,14 +228,16 @@ def create_plots(df):
231
  ax4.grid(True, axis='y')
232
 
233
  # Add overall wind rose
234
- ax5 = fig.add_subplot(gs[4, 0], projection='windrose')
 
235
  create_wind_rose(df, ax5)
236
  ax5.set_title('Overall Wind Rose')
237
 
238
  # Add latest day's wind rose
239
  latest_date = df['date'].iloc[0]
240
  latest_data = df[df['date'] == latest_date]
241
- ax6 = fig.add_subplot(gs[4, 1], projection='windrose')
 
242
  create_wind_rose(latest_data, ax6)
243
  ax6.set_title(f'Latest Day Wind Rose\n{latest_date}')
244
 
@@ -313,6 +312,7 @@ with gr.Blocks(title="Weather Station Data Analyzer") as demo:
313
  minimum=1,
314
  maximum=1440
315
  )
 
316
  analyze_btn = gr.Button("Fetch and Analyze Weather Data")
317
 
318
  with gr.Row():
 
10
  import matplotlib.pyplot as plt
11
  from matplotlib.gridspec import GridSpec
12
  import matplotlib.dates as mdates
13
+ from windrose import WindroseAxes
14
  from datetime import datetime
15
 
16
  # Install Playwright browsers on startup
 
130
 
131
  return df
132
 
133
+ def create_wind_rose(df, ax=None):
134
  """Create a wind rose plot"""
135
+ if ax is None:
136
+ fig = plt.figure(figsize=(8, 8))
137
+ ax = WindroseAxes.from_ax(fig=fig)
138
+
139
  ws = df['wind_speed'].dropna().values
140
  wd = df['wind_dir_deg'].dropna().values
141
 
142
+ if len(ws) > 0 and len(wd) > 0:
143
+ # Create the wind rose
144
+ ax.bar(wd, ws, nsector=36, normed=True, opening=0.8,
145
+ bins=np.array([0, 5, 10, 15, 20, 25, 30, 35, 40]),
146
+ colors=plt.cm.viridis(np.linspace(0, 1, 8)))
147
+ ax.set_legend(title='Wind Speed (mph)', bbox_to_anchor=(1.2, 0.5))
148
+ return ax
 
 
149
 
150
  def create_daily_wind_roses(df):
151
  """Create wind roses for each day"""
 
156
  if len(group) > 0: # Only create rose if we have data
157
  fig = plt.figure(figsize=(8, 8))
158
  ax = WindroseAxes.from_ax(fig=fig)
159
+ if create_wind_rose(group, ax):
 
 
 
 
 
 
160
  ax.set_title(f'Wind Rose - {date.strftime("%Y-%m-%d")}')
161
  plt.tight_layout()
162
  roses.append(fig)
163
+ plt.close(fig)
164
 
165
  return roses
166
 
 
228
  ax4.grid(True, axis='y')
229
 
230
  # Add overall wind rose
231
+ fig_wind = plt.figure(figsize=(8, 8))
232
+ ax5 = WindroseAxes.from_ax(fig=fig_wind)
233
  create_wind_rose(df, ax5)
234
  ax5.set_title('Overall Wind Rose')
235
 
236
  # Add latest day's wind rose
237
  latest_date = df['date'].iloc[0]
238
  latest_data = df[df['date'] == latest_date]
239
+ fig_latest = plt.figure(figsize=(8, 8))
240
+ ax6 = WindroseAxes.from_ax(fig=fig_latest)
241
  create_wind_rose(latest_data, ax6)
242
  ax6.set_title(f'Latest Day Wind Rose\n{latest_date}')
243
 
 
312
  minimum=1,
313
  maximum=1440
314
  )
315
+
316
  analyze_btn = gr.Button("Fetch and Analyze Weather Data")
317
 
318
  with gr.Row():