nakas commited on
Commit
c245f9d
·
verified ·
1 Parent(s): 23ad193

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -23
app.py CHANGED
@@ -3,8 +3,7 @@ Radar Data Downloader with Two-Step Workflow via Gradio Blocks
3
  ===============================================================
4
 
5
  Step 1: Click "Fetch Stations" to dynamically retrieve the full list of radar sites.
6
- Step 2: In the "Download Data" tab, select a radar type, choose a station (from the updated list),
7
- enter a date (YYYY-MM-DD), and click "Download Data" to fetch the latest radar file.
8
 
9
  Required packages:
10
  pip install gradio boto3 requests
@@ -23,8 +22,9 @@ import requests
23
  def get_full_station_list():
24
  """
25
  Fetches a full list of radar stations from a public API endpoint.
26
- For example, we use NOAA’s public radar station API endpoint as an example.
27
- (If the API fails, falls back to fetching the static JSON file from GitHub.)
 
28
  Returns a sorted list of station identifiers.
29
  """
30
  api_url = "https://api.weather.gov/radar/stations"
@@ -44,7 +44,7 @@ def get_full_station_list():
44
  return stations
45
  except Exception as e:
46
  print(f"Error fetching station list from API: {e}")
47
- # Fallback to static JSON from GitHub
48
  return get_station_list_from_github()
49
 
50
 
@@ -53,7 +53,7 @@ def get_station_list_from_github():
53
  Fallback: Downloads and parses the radar_sites.json file from the Supercell Wx GitHub repository.
54
  Returns a sorted list of station identifiers.
55
  """
56
- url = "https://raw.githubusercontent.com/dpaulat/supercell-wx/develop/data/radar_sites.json"
57
  try:
58
  response = requests.get(url, timeout=10)
59
  response.raise_for_status()
@@ -74,14 +74,14 @@ def get_station_list_from_github():
74
 
75
  def update_station_list():
76
  """
77
- Retrieves the full station list and returns an update for the dropdown component.
 
78
  """
79
  stations = get_full_station_list()
80
  if stations:
81
- # Update with the full list and set the default to the first station.
82
- return gr.Dropdown.update(choices=stations, value=stations[0])
83
  else:
84
- return gr.Dropdown.update(choices=[], value=None)
85
 
86
 
87
  def fetch_latest_radar_data(radar_type, station, date_str):
@@ -89,12 +89,12 @@ def fetch_latest_radar_data(radar_type, station, date_str):
89
  Downloads the latest radar data file from the appropriate AWS S3 bucket.
90
 
91
  Parameters:
92
- radar_type (str): Either "Level 2" or "Level 3".
93
- station (str): Radar station identifier (e.g. "KTLX"). Must be non-empty.
94
- date_str (str): Date string in "YYYY-MM-DD" format. If blank, uses current UTC date.
95
 
96
  Returns:
97
- str: A summary of the downloaded file (S3 key, size, last modified, and hex preview).
98
  """
99
  if not station:
100
  return "Error: No station selected. Please fetch stations first."
@@ -167,26 +167,19 @@ with gr.Blocks() as demo:
167
  with gr.Tab("Fetch Stations"):
168
  with gr.Row():
169
  fetch_btn = gr.Button("Fetch Stations")
170
- # This dropdown will show the fetched station list.
171
  station_dropdown_fetch = gr.Dropdown(choices=[], label="Available Radar Stations")
172
- # Clicking fetch_btn will update the dropdown.
173
  fetch_btn.click(fn=update_station_list, inputs=[], outputs=station_dropdown_fetch)
174
 
175
  with gr.Tab("Download Data"):
176
  with gr.Row():
177
  radar_type = gr.Radio(choices=["Level 2", "Level 3"], label="Radar Data Type")
178
- # The download dropdown; initially empty. It can be updated by a separate sync button.
179
  station_dropdown_download = gr.Dropdown(choices=[], label="Select Radar Station")
180
- date_input = gr.Textbox(value=datetime.datetime.utcnow().strftime("%Y-%m-%d"),
181
- label="Date (YYYY-MM-DD)")
182
  download_btn = gr.Button("Download Data")
183
  download_output = gr.Textbox(label="Download Output", interactive=False)
184
- # When download_btn is clicked, call fetch_latest_radar_data.
185
  download_btn.click(fn=fetch_latest_radar_data,
186
  inputs=[radar_type, station_dropdown_download, date_input],
187
  outputs=download_output)
188
-
189
- # Optional: a button to sync the station list to the download dropdown.
190
  sync_btn = gr.Button("Sync Station List to Download Tab")
191
  sync_btn.click(fn=update_station_list, inputs=[], outputs=station_dropdown_download)
192
 
@@ -195,5 +188,4 @@ with gr.Blocks() as demo:
195
  )
196
 
197
  if __name__ == "__main__":
198
- # For Spaces you might want to use share=True
199
  demo.launch()
 
3
  ===============================================================
4
 
5
  Step 1: Click "Fetch Stations" to dynamically retrieve the full list of radar sites.
6
+ Step 2: In the "Download Data" tab, select a radar type, station, and date, then click "Download Data" to fetch the latest radar file.
 
7
 
8
  Required packages:
9
  pip install gradio boto3 requests
 
22
  def get_full_station_list():
23
  """
24
  Fetches a full list of radar stations from a public API endpoint.
25
+
26
+ Here we use NOAA’s public radar station endpoint.
27
+ If that fails, we fall back to a static JSON file from GitHub.
28
  Returns a sorted list of station identifiers.
29
  """
30
  api_url = "https://api.weather.gov/radar/stations"
 
44
  return stations
45
  except Exception as e:
46
  print(f"Error fetching station list from API: {e}")
47
+ # Fallback to static JSON from GitHub.
48
  return get_station_list_from_github()
49
 
50
 
 
53
  Fallback: Downloads and parses the radar_sites.json file from the Supercell Wx GitHub repository.
54
  Returns a sorted list of station identifiers.
55
  """
56
+ url = "https://raw.githubusercontent.com/dpaulat/supercell-wx/main/data/radar_sites.json"
57
  try:
58
  response = requests.get(url, timeout=10)
59
  response.raise_for_status()
 
74
 
75
  def update_station_list():
76
  """
77
+ Retrieves the full station list and returns a dictionary to update the dropdown component.
78
+ (Note: Some Gradio versions do not support .update(), so we return a plain dict.)
79
  """
80
  stations = get_full_station_list()
81
  if stations:
82
+ return {"choices": stations, "value": stations[0]}
 
83
  else:
84
+ return {"choices": [], "value": None}
85
 
86
 
87
  def fetch_latest_radar_data(radar_type, station, date_str):
 
89
  Downloads the latest radar data file from the appropriate AWS S3 bucket.
90
 
91
  Parameters:
92
+ radar_type (str): "Level 2" or "Level 3".
93
+ station (str): Radar station identifier (e.g. "KTLX").
94
+ date_str (str): Date in "YYYY-MM-DD" format. If blank, uses current UTC date.
95
 
96
  Returns:
97
+ str: A summary of the downloaded file (S3 key, file size, last modified, and hex preview).
98
  """
99
  if not station:
100
  return "Error: No station selected. Please fetch stations first."
 
167
  with gr.Tab("Fetch Stations"):
168
  with gr.Row():
169
  fetch_btn = gr.Button("Fetch Stations")
 
170
  station_dropdown_fetch = gr.Dropdown(choices=[], label="Available Radar Stations")
 
171
  fetch_btn.click(fn=update_station_list, inputs=[], outputs=station_dropdown_fetch)
172
 
173
  with gr.Tab("Download Data"):
174
  with gr.Row():
175
  radar_type = gr.Radio(choices=["Level 2", "Level 3"], label="Radar Data Type")
 
176
  station_dropdown_download = gr.Dropdown(choices=[], label="Select Radar Station")
177
+ date_input = gr.Textbox(value=datetime.datetime.utcnow().strftime("%Y-%m-%d"), label="Date (YYYY-MM-DD)")
 
178
  download_btn = gr.Button("Download Data")
179
  download_output = gr.Textbox(label="Download Output", interactive=False)
 
180
  download_btn.click(fn=fetch_latest_radar_data,
181
  inputs=[radar_type, station_dropdown_download, date_input],
182
  outputs=download_output)
 
 
183
  sync_btn = gr.Button("Sync Station List to Download Tab")
184
  sync_btn.click(fn=update_station_list, inputs=[], outputs=station_dropdown_download)
185
 
 
188
  )
189
 
190
  if __name__ == "__main__":
 
191
  demo.launch()