Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ import sunpy.net.attrs as a
|
|
11 |
from sunpy.net import Fido
|
12 |
from astropy.wcs import WCS
|
13 |
import astropy.units as u
|
14 |
-
from reproject import reproject_interp
|
15 |
import os
|
16 |
import warnings
|
17 |
import logging
|
@@ -21,7 +21,9 @@ import sunpy.visualization.colormaps as sunpy_cm
|
|
21 |
|
22 |
# --- Use the official Surya modules ---
|
23 |
from surya.models.helio_spectformer import HelioSpectFormer
|
24 |
-
from surya.utils.data import build_scalers
|
|
|
|
|
25 |
|
26 |
# --- Configuration ---
|
27 |
warnings.filterwarnings("ignore", category=UserWarning, module='sunpy')
|
@@ -106,20 +108,18 @@ def fetch_and_process_sdo_data(target_dt, progress):
|
|
106 |
for i, (channel, (physobs, sample)) in enumerate(SDO_CHANNELS_MAP.items()):
|
107 |
progress(downloads_done / total_downloads, desc=f"Downloading {channel} for {t.strftime('%H:%M')}...")
|
108 |
|
109 |
-
instrument = a.Instrument.hmi if "hmi" in channel else a.Instrument.aia
|
110 |
if channel in ["hmi_by", "hmi_bz"]:
|
111 |
if data_maps[t].get("hmi_bx"): data_maps[t][channel] = data_maps[t]["hmi_bx"]
|
112 |
continue
|
113 |
|
114 |
time_attr = a.Time(t - datetime.timedelta(minutes=10), t + datetime.timedelta(minutes=10))
|
115 |
-
|
116 |
-
# AIA and HMI queries are slightly different
|
117 |
if "aia" in channel:
|
118 |
-
|
119 |
else:
|
120 |
-
|
121 |
|
122 |
-
query = Fido.search(*
|
123 |
|
124 |
if not query: raise ValueError(f"No data found for {channel} at {t}")
|
125 |
files = Fido.fetch(query[0, 0], path="./data/sdo_cache")
|
@@ -183,11 +183,10 @@ def generate_visualization(last_input_map, prediction_tensor, target_map, channe
|
|
183 |
|
184 |
c_idx = SDO_CHANNELS.index(channel_name)
|
185 |
|
186 |
-
|
187 |
-
means, stds, epsilons, sl_scale_factors = APP_CACHE["scalers"][SDO_CHANNELS[0]].get_params()
|
188 |
pred_slice = inverse_transform_single_channel(
|
189 |
prediction_tensor[0, c_idx].numpy(),
|
190 |
-
mean=means
|
191 |
)
|
192 |
|
193 |
vmax = np.quantile(np.nan_to_num(target_map[channel_name].data), 0.995)
|
@@ -237,7 +236,6 @@ def update_visualization_controller(last_input_map, prediction_tensor, target_ma
|
|
237 |
return None, None, None
|
238 |
return generate_visualization(last_input_map, prediction_tensor, target_map, channel_name)
|
239 |
|
240 |
-
|
241 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
242 |
state_last_input = gr.State()
|
243 |
state_prediction = gr.State()
|
@@ -249,7 +247,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
249 |
# ☀️ Surya: Live Forecast Demo ☀️
|
250 |
### Generate a real forecast for any recent date using NASA's Heliophysics Model.
|
251 |
**Instructions:**
|
252 |
-
1. Pick a date and time (at least
|
253 |
2. Click 'Generate Forecast'. **This will be slow (5-15 minutes) as it downloads live data.**
|
254 |
3. Once complete, select different channels to explore the multi-spectrum forecast.
|
255 |
</div>
|
|
|
11 |
from sunpy.net import Fido
|
12 |
from astropy.wcs import WCS
|
13 |
import astropy.units as u
|
14 |
+
from reproject import reproject_interp
|
15 |
import os
|
16 |
import warnings
|
17 |
import logging
|
|
|
21 |
|
22 |
# --- Use the official Surya modules ---
|
23 |
from surya.models.helio_spectformer import HelioSpectFormer
|
24 |
+
from surya.utils.data import build_scalers
|
25 |
+
# *** FIX: Corrected import location for the inverse transform function ***
|
26 |
+
from surya.datasets.helio import inverse_transform_single_channel
|
27 |
|
28 |
# --- Configuration ---
|
29 |
warnings.filterwarnings("ignore", category=UserWarning, module='sunpy')
|
|
|
108 |
for i, (channel, (physobs, sample)) in enumerate(SDO_CHANNELS_MAP.items()):
|
109 |
progress(downloads_done / total_downloads, desc=f"Downloading {channel} for {t.strftime('%H:%M')}...")
|
110 |
|
|
|
111 |
if channel in ["hmi_by", "hmi_bz"]:
|
112 |
if data_maps[t].get("hmi_bx"): data_maps[t][channel] = data_maps[t]["hmi_bx"]
|
113 |
continue
|
114 |
|
115 |
time_attr = a.Time(t - datetime.timedelta(minutes=10), t + datetime.timedelta(minutes=10))
|
116 |
+
search_query_list = [time_attr, physobs, sample]
|
|
|
117 |
if "aia" in channel:
|
118 |
+
search_query_list.append(a.Instrument.aia)
|
119 |
else:
|
120 |
+
search_query_list.append(a.Instrument.hmi)
|
121 |
|
122 |
+
query = Fido.search(*search_query_list)
|
123 |
|
124 |
if not query: raise ValueError(f"No data found for {channel} at {t}")
|
125 |
files = Fido.fetch(query[0, 0], path="./data/sdo_cache")
|
|
|
183 |
|
184 |
c_idx = SDO_CHANNELS.index(channel_name)
|
185 |
|
186 |
+
means, stds, epsilons, sl_scale_factors = APP_CACHE["scalers"][SDO_CHANNELS[c_idx]].get_params()
|
|
|
187 |
pred_slice = inverse_transform_single_channel(
|
188 |
prediction_tensor[0, c_idx].numpy(),
|
189 |
+
mean=means, std=stds, epsilon=epsilons, sl_scale_factor=sl_scale_factors
|
190 |
)
|
191 |
|
192 |
vmax = np.quantile(np.nan_to_num(target_map[channel_name].data), 0.995)
|
|
|
236 |
return None, None, None
|
237 |
return generate_visualization(last_input_map, prediction_tensor, target_map, channel_name)
|
238 |
|
|
|
239 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
240 |
state_last_input = gr.State()
|
241 |
state_prediction = gr.State()
|
|
|
247 |
# ☀️ Surya: Live Forecast Demo ☀️
|
248 |
### Generate a real forecast for any recent date using NASA's Heliophysics Model.
|
249 |
**Instructions:**
|
250 |
+
1. Pick a date and time (at least 3 hours in the past).
|
251 |
2. Click 'Generate Forecast'. **This will be slow (5-15 minutes) as it downloads live data.**
|
252 |
3. Once complete, select different channels to explore the multi-spectrum forecast.
|
253 |
</div>
|