Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,7 @@ import matplotlib.pyplot as plt
|
|
13 |
import sunpy.visualization.colormaps as sunpy_cm
|
14 |
import traceback
|
15 |
from io import BytesIO
|
|
|
16 |
|
17 |
from surya.models.helio_spectformer import HelioSpectFormer
|
18 |
from surya.utils.data import build_scalers
|
@@ -74,22 +75,40 @@ def setup_and_load_model():
|
|
74 |
APP_CACHE["model"] = model
|
75 |
yield "✅ Model setup complete."
|
76 |
|
77 |
-
def
|
78 |
url_code = CHANNEL_TO_URL_CODE[channel]
|
79 |
base_url = "https://sdo.gsfc.nasa.gov/assets/img/browse"
|
80 |
|
81 |
-
for i in range(
|
82 |
-
dt_to_try = target_dt - datetime.timedelta(
|
83 |
-
|
84 |
-
img_str = dt_to_try.strftime(f"%Y%m%d_%H%M%S_4096_{url_code}.jpg")
|
85 |
-
url = f"{base_url}/{date_str}/{img_str}"
|
86 |
|
87 |
-
response = requests.get(
|
88 |
-
if response.status_code
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
raise FileNotFoundError(f"Could not find any
|
93 |
|
94 |
def fetch_and_process_sdo_data(target_dt, forecast_horizon_minutes):
|
95 |
config = APP_CACHE["config"]
|
@@ -109,10 +128,16 @@ def fetch_and_process_sdo_data(target_dt, forecast_horizon_minutes):
|
|
109 |
images[t] = {}
|
110 |
for channel in SDO_CHANNELS:
|
111 |
fetches_done += 1
|
112 |
-
yield f"
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
-
yield "✅ All images found. Starting preprocessing..."
|
116 |
scaler = APP_CACHE["scalers"]
|
117 |
processed_tensors = {}
|
118 |
for t, channel_images in images.items():
|
|
|
13 |
import sunpy.visualization.colormaps as sunpy_cm
|
14 |
import traceback
|
15 |
from io import BytesIO
|
16 |
+
import re
|
17 |
|
18 |
from surya.models.helio_spectformer import HelioSpectFormer
|
19 |
from surya.utils.data import build_scalers
|
|
|
75 |
APP_CACHE["model"] = model
|
76 |
yield "✅ Model setup complete."
|
77 |
|
78 |
+
def find_nearest_browse_image_url(channel, target_dt):
|
79 |
url_code = CHANNEL_TO_URL_CODE[channel]
|
80 |
base_url = "https://sdo.gsfc.nasa.gov/assets/img/browse"
|
81 |
|
82 |
+
for i in range(2): # Try today, then yesterday
|
83 |
+
dt_to_try = target_dt - datetime.timedelta(days=i)
|
84 |
+
dir_url = dt_to_try.strftime(f"{base_url}/%Y/%m/%d/")
|
|
|
|
|
85 |
|
86 |
+
response = requests.get(dir_url)
|
87 |
+
if response.status_code != 200:
|
88 |
+
continue
|
89 |
+
|
90 |
+
filenames = re.findall(r'href="(\d{8}_\d{6}_4096_' + url_code + r'\.jpg)"', response.text)
|
91 |
+
if not filenames:
|
92 |
+
continue
|
93 |
+
|
94 |
+
best_filename = ""
|
95 |
+
min_diff = float('inf')
|
96 |
+
|
97 |
+
for fname in filenames:
|
98 |
+
try:
|
99 |
+
timestamp_str = fname.split('_')[1]
|
100 |
+
img_dt = datetime.datetime.strptime(f"{dt_to_try.strftime('%Y%m%d')}{timestamp_str}", "%Y%m%d%H%M%S")
|
101 |
+
diff = abs((target_dt - img_dt).total_seconds())
|
102 |
+
if diff < min_diff:
|
103 |
+
min_diff = diff
|
104 |
+
best_filename = fname
|
105 |
+
except (ValueError, IndexError):
|
106 |
+
continue
|
107 |
+
|
108 |
+
if best_filename:
|
109 |
+
return dir_url + best_filename
|
110 |
|
111 |
+
raise FileNotFoundError(f"Could not find any browse images for {channel} in the last 48 hours.")
|
112 |
|
113 |
def fetch_and_process_sdo_data(target_dt, forecast_horizon_minutes):
|
114 |
config = APP_CACHE["config"]
|
|
|
128 |
images[t] = {}
|
129 |
for channel in SDO_CHANNELS:
|
130 |
fetches_done += 1
|
131 |
+
yield f"Finding [{fetches_done}/{total_fetches}]: Closest image for {channel} near {t.strftime('%Y-%m-%d %H:%M')}..."
|
132 |
+
|
133 |
+
image_url = find_nearest_browse_image_url(channel, t)
|
134 |
+
yield f"Downloading: {os.path.basename(image_url)}..."
|
135 |
+
|
136 |
+
response = requests.get(image_url)
|
137 |
+
response.raise_for_status()
|
138 |
+
images[t][channel] = Image.open(BytesIO(response.content))
|
139 |
|
140 |
+
yield "✅ All images found and downloaded. Starting preprocessing..."
|
141 |
scaler = APP_CACHE["scalers"]
|
142 |
processed_tensors = {}
|
143 |
for t, channel_images in images.items():
|