-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
156 lines (130 loc) · 5.06 KB
/
app.py
File metadata and controls
156 lines (130 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
from pathlib import Path
import streamlit as st
# ---------------- Configuration ----------------
BASE_PNG = Path("output_data/forecast_files/forecast_pngs")
BASE_TXT = Path("output_data/forecast_files/forecast_text_files")
PNG_TYPES = {
"LOS": "los_forecast_pngs",
"MHD": "mhd_forecast_pngs",
"VEC": "vec_forecast_pngs",
}
TXT_TYPES = {
"LOS": "los_full_disk_forecasts",
"MHD": "mhd_fl_forecasts",
"VEC": "vec_full_disk_forecasts",
}
# Try these text layouts in order:
# 1) .../<run_subdir>/old_forecasts/YYYY/MM/DD
# 2) .../<run_subdir>/YYYY/MM/DD
TXT_VARIANTS = [
("old_forecasts",),
tuple(),
]
# ---------------- Helpers ----------------
def list_ymd_dates(root: Path) -> list[str]:
"""Return dates like 'YYYY/MM/DD' that exist under root (root/YYYY/MM/DD)."""
dates: list[str] = []
if not root.exists():
return dates
for y in sorted([p for p in root.iterdir() if p.is_dir() and p.name.isdigit() and len(p.name) == 4]):
for m in sorted([p for p in y.iterdir() if p.is_dir() and p.name.isdigit()]):
for d in sorted([p for p in m.iterdir() if p.is_dir() and p.name.isdigit()]):
dates.append(f"{y.name}/{m.name}/{d.name}")
return dates
def sort_dates_ymd(dates: list[str]) -> list[str]:
def key(s: str):
y, m, d = s.split("/")
return (int(y), int(m), int(d))
return sorted(set(dates), key=key)
def list_pngs(path: Path) -> list[Path]:
if not path.exists():
return []
return sorted([p for p in path.glob("*.png") if p.is_file()])
def list_txts(path: Path) -> list[Path]:
if not path.exists():
return []
return sorted([p for p in path.glob("*.txt") if p.is_file()])
def resolve_text_day_dir(run_type: str, selected_date: str) -> Path | None:
"""Return the first existing text directory for the given run type and date."""
run_subdir = TXT_TYPES[run_type]
for variant in TXT_VARIANTS:
candidate = BASE_TXT / run_subdir
for v in variant:
candidate = candidate / v
candidate = candidate / selected_date
if candidate.exists():
return candidate
return None
# ---------------- UI ----------------
st.title("MagPy Forecast Results Viewer")
# 1) Date first: build date list from the UNION of all PNG trees
all_dates: list[str] = []
for run_type, subdir in PNG_TYPES.items():
all_dates.extend(list_ymd_dates(BASE_PNG / subdir))
date_options = sort_dates_ymd(all_dates)
if not date_options:
st.error("No date folders found under any PNG directories.")
st.stop()
selected_date = st.selectbox("Select date (YYYY/MM/DD)", date_options)
# 2) After date selection, offer ONLY run types that exist for that date
available_types: list[str] = []
for run_type, subdir in PNG_TYPES.items():
day_dir = BASE_PNG / subdir / selected_date
if day_dir.exists() and list_pngs(day_dir):
available_types.append(run_type)
if not available_types:
st.warning("No run types (LOS/VEC/MHD) have PNGs for this date.")
st.stop()
run_type = st.selectbox("Select run type", available_types)
png_day_dir = BASE_PNG / PNG_TYPES[run_type] / selected_date
txt_day_dir = resolve_text_day_dir(run_type, selected_date)
tabs = st.tabs(["PNG", "Text"])
with tabs[0]:
st.subheader(f"{run_type} PNGs")
st.caption(f"Folder: {png_day_dir}")
pngs = list_pngs(png_day_dir)
if not pngs:
st.warning("No PNG files found for this date/run type.")
else:
png_choice = st.selectbox(
"Choose PNG",
options=pngs,
format_func=lambda p: p.name,
key="png_select",
)
st.image(str(png_choice), caption=png_choice.name, width='content')
with st.expander("Show all PNGs in this folder"):
cols = st.columns(2)
for i, p in enumerate(pngs):
with cols[i % 2]:
st.image(str(p), caption=p.name, width='content')
with tabs[1]:
st.subheader(f"{run_type} Text")
if txt_day_dir is None:
st.warning("Text directory not found for this run type/date.")
st.caption("Tried:")
for variant in TXT_VARIANTS:
candidate = BASE_TXT / TXT_TYPES[run_type]
for v in variant:
candidate = candidate / v
candidate = candidate / selected_date
st.code(str(candidate), language="text")
else:
st.caption(f"Folder: {txt_day_dir}")
txts = list_txts(txt_day_dir)
if not txts:
st.info("No .txt files found for this date/run type.")
other_files = sorted([p for p in txt_day_dir.iterdir() if p.is_file()])
if other_files:
with st.expander("Other files found (not .txt)"):
for p in other_files:
st.write(p.name)
else:
txt_choice = st.selectbox(
"Choose text file",
options=txts,
format_func=lambda p: p.name,
key="txt_select",
)
st.code(txt_choice.read_text(errors="replace"), language="text")