saburq commited on
Commit
93f7649
·
1 Parent(s): 6085d0a
Files changed (1) hide show
  1. app.py +67 -7
app.py CHANGED
@@ -23,6 +23,43 @@ LEVELS = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 50]
23
  SOIL_LEVELS = [1, 2]
24
  DEFAULT_DATE = OpendataClient().latest()
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def get_open_data(param, levelist=[]):
27
  fields = {}
28
  # Get the data for the current date and the previous date
@@ -74,36 +111,59 @@ def run_forecast(date, lead_time, device):
74
  results.append(state)
75
  return results[-1]
76
 
77
- def plot_forecast(state):
78
  latitudes, longitudes = state["latitudes"], state["longitudes"]
79
- values = state["fields"]["100u"]
80
  fig, ax = plt.subplots(figsize=(11, 6), subplot_kw={"projection": ccrs.PlateCarree()})
81
  ax.coastlines()
82
  ax.add_feature(cfeature.BORDERS, linestyle=":")
83
  triangulation = tri.Triangulation(longitudes, latitudes)
84
  contour = ax.tricontourf(triangulation, values, levels=20, transform=ccrs.PlateCarree(), cmap="RdBu")
85
- plt.title(f"100m winds at {state['date']}")
86
  plt.colorbar(contour)
87
  return fig
88
 
89
- def gradio_interface(date_str, lead_time, device):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  try:
91
  date = datetime.datetime.strptime(date_str, "%Y-%m-%d")
92
  except ValueError:
93
  raise gr.Error("Please enter a valid date in YYYY-MM-DD format")
94
  state = run_forecast(date, lead_time, device)
95
- return plot_forecast(state)
96
 
97
  demo = gr.Interface(
98
  fn=gradio_interface,
99
  inputs=[
100
  gr.Textbox(value=DEFAULT_DATE.strftime("%Y-%m-%d"), label="Forecast Date (YYYY-MM-DD)"),
101
  gr.Slider(minimum=6, maximum=48, step=6, value=12, label="Lead Time (Hours)"),
102
- gr.Radio(choices=["cuda", "cpu"], value="cuda", label="Compute Device")
 
 
 
 
 
 
103
  ],
104
  outputs=gr.Plot(),
105
  title="AIFS Weather Forecast",
106
- description="Run ECMWF AIFS forecasts based on selected parameters."
107
  )
108
 
109
  demo.launch()
 
23
  SOIL_LEVELS = [1, 2]
24
  DEFAULT_DATE = OpendataClient().latest()
25
 
26
+ # First define the variable descriptions
27
+ VARIABLE_DESCRIPTIONS = {
28
+ # Surface variables (10m)
29
+ "10u": "10m U Wind Component",
30
+ "10v": "10m V Wind Component",
31
+ "2d": "2m Dewpoint Temperature",
32
+ "2t": "2m Temperature",
33
+ "msl": "Mean Sea Level Pressure",
34
+ "skt": "Skin Temperature",
35
+ "sp": "Surface Pressure",
36
+ "tcw": "Total Column Water",
37
+ "lsm": "Land-Sea Mask",
38
+ "z": "Surface Geopotential",
39
+ "slor": "Slope of Sub-gridscale Orography",
40
+ "sdor": "Standard Deviation of Orography",
41
+
42
+ # Soil variables
43
+ "stl1": "Soil Temperature Level 1",
44
+ "stl2": "Soil Temperature Level 2",
45
+ "swvl1": "Soil Water Volume Level 1",
46
+ "swvl2": "Soil Water Volume Level 2",
47
+ }
48
+
49
+ # Add pressure level variable descriptions dynamically
50
+ for var in ["t", "u", "v", "w", "q", "z"]:
51
+ var_name = {
52
+ "t": "Temperature",
53
+ "u": "U Wind Component",
54
+ "v": "V Wind Component",
55
+ "w": "Vertical Velocity",
56
+ "q": "Specific Humidity",
57
+ "z": "Geopotential"
58
+ }[var]
59
+
60
+ for level in LEVELS:
61
+ VARIABLE_DESCRIPTIONS[f"{var}_{level}"] = f"{var_name} at {level}hPa"
62
+
63
  def get_open_data(param, levelist=[]):
64
  fields = {}
65
  # Get the data for the current date and the previous date
 
111
  results.append(state)
112
  return results[-1]
113
 
114
+ def plot_forecast(state, selected_variable):
115
  latitudes, longitudes = state["latitudes"], state["longitudes"]
116
+ values = state["fields"][selected_variable]
117
  fig, ax = plt.subplots(figsize=(11, 6), subplot_kw={"projection": ccrs.PlateCarree()})
118
  ax.coastlines()
119
  ax.add_feature(cfeature.BORDERS, linestyle=":")
120
  triangulation = tri.Triangulation(longitudes, latitudes)
121
  contour = ax.tricontourf(triangulation, values, levels=20, transform=ccrs.PlateCarree(), cmap="RdBu")
122
+ plt.title(f"{selected_variable} at {state['date']}")
123
  plt.colorbar(contour)
124
  return fig
125
 
126
+ # Then create the available variables list
127
+ AVAILABLE_VARIABLES = (
128
+ # Surface variables
129
+ ["10u", "10v", "2d", "2t", "msl", "skt", "sp", "tcw", "lsm", "z", "slor", "sdor"] +
130
+ # Soil variables
131
+ ["stl1", "stl2", "swvl1", "swvl2"] +
132
+ # Pressure level variables (adding level suffix)
133
+ [f"{var}_{level}" for var in ["t", "u", "v", "w", "q", "z"]
134
+ for level in LEVELS]
135
+ )
136
+
137
+ # Finally create the dropdown choices
138
+ DROPDOWN_CHOICES = [
139
+ (f"{VARIABLE_DESCRIPTIONS[var_id]} ({var_id})", var_id)
140
+ for var_id in sorted(AVAILABLE_VARIABLES)
141
+ ]
142
+
143
+ def gradio_interface(date_str, lead_time, device, selected_variable):
144
  try:
145
  date = datetime.datetime.strptime(date_str, "%Y-%m-%d")
146
  except ValueError:
147
  raise gr.Error("Please enter a valid date in YYYY-MM-DD format")
148
  state = run_forecast(date, lead_time, device)
149
+ return plot_forecast(state, selected_variable)
150
 
151
  demo = gr.Interface(
152
  fn=gradio_interface,
153
  inputs=[
154
  gr.Textbox(value=DEFAULT_DATE.strftime("%Y-%m-%d"), label="Forecast Date (YYYY-MM-DD)"),
155
  gr.Slider(minimum=6, maximum=48, step=6, value=12, label="Lead Time (Hours)"),
156
+ gr.Radio(choices=["cuda", "cpu"], value="cuda", label="Compute Device"),
157
+ gr.Dropdown(
158
+ choices=DROPDOWN_CHOICES,
159
+ value="t_850", # This should be the variable ID
160
+ label="Select Variable to Plot",
161
+ info="Choose a meteorological variable to visualize"
162
+ )
163
  ],
164
  outputs=gr.Plot(),
165
  title="AIFS Weather Forecast",
166
+ description="Interactive visualization of ECMWF AIFS weather forecasts. Select a date, forecast lead time, and meteorological variable to plot."
167
  )
168
 
169
  demo.launch()