Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import tempfile
|
| 3 |
from datetime import datetime, timedelta, timezone
|
| 4 |
|
|
@@ -55,15 +56,24 @@ def fetch_reports(time_from, time_to):
|
|
| 55 |
return r.json()
|
| 56 |
|
| 57 |
# -----------------------------
|
| 58 |
-
# JSON 解析(讀 EarthquakeInfo
|
| 59 |
# -----------------------------
|
| 60 |
def _to_float(x):
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
return None
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def parse_ea0015(obj):
|
| 69 |
"""
|
|
@@ -78,30 +88,41 @@ def parse_ea0015(obj):
|
|
| 78 |
|
| 79 |
rows = []
|
| 80 |
for q in quakes:
|
| 81 |
-
ei
|
| 82 |
-
epic = ei.get("Epicenter")
|
| 83 |
-
|
| 84 |
|
| 85 |
origin = (
|
| 86 |
ei.get("OriginTime") or ei.get("originTime")
|
| 87 |
or q.get("OriginTime") or q.get("originTime")
|
| 88 |
)
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
depth = ei.get("Depth") or ei.get("depth")
|
| 92 |
mag = (
|
| 93 |
-
|
| 94 |
-
or
|
| 95 |
)
|
| 96 |
loc = epic.get("Location") or epic.get("location")
|
| 97 |
url = q.get("Web") or q.get("ReportURL") or q.get("reportURL")
|
| 98 |
|
| 99 |
rows.append({
|
| 100 |
"OriginTime": origin,
|
| 101 |
-
"Lat": _to_float(
|
| 102 |
-
"Lon": _to_float(
|
| 103 |
"Depth_km": _to_float(depth),
|
| 104 |
-
"Magnitude": _to_float(
|
| 105 |
"Location": loc,
|
| 106 |
"ReportURL": url,
|
| 107 |
})
|
|
@@ -124,7 +145,6 @@ def _save_fig_to_tmp(fig, suffix=".png", dpi=180):
|
|
| 124 |
def plot_trend_path(df):
|
| 125 |
if df.empty:
|
| 126 |
return None
|
| 127 |
-
# 顯式轉數值更穩
|
| 128 |
m = pd.to_numeric(df["Magnitude"], errors="coerce")
|
| 129 |
fig, ax = plt.subplots(figsize=(6, 4))
|
| 130 |
ax.scatter(df["OriginTime"], m)
|
|
@@ -146,13 +166,9 @@ def plot_map_path(df):
|
|
| 146 |
d = df.dropna(subset=["Lon", "Lat"]).copy()
|
| 147 |
if not d.empty:
|
| 148 |
mag = pd.to_numeric(d["Magnitude"], errors="coerce").fillna(0).clip(lower=0)
|
| 149 |
-
size_cm = 0.06 * (mag + 1.5)
|
| 150 |
depth = pd.to_numeric(d["Depth_km"], errors="coerce").fillna(0)
|
| 151 |
|
| 152 |
-
vmin = float(max(0, float(depth.min()) if len(depth) else 0))
|
| 153 |
-
vmax = float(max(100, float(depth.max()) if len(depth) else 100))
|
| 154 |
-
cmap = "roma"
|
| 155 |
-
|
| 156 |
fig = pygmt.Figure()
|
| 157 |
region = [lon_min, lon_max, lat_min, lat_max]
|
| 158 |
fig.coast(
|
|
@@ -161,11 +177,11 @@ def plot_map_path(df):
|
|
| 161 |
shorelines="0.5p,black", borders="1/0.6p,black",
|
| 162 |
frame=["WSen", "xaf", "yaf"]
|
| 163 |
)
|
| 164 |
-
#
|
| 165 |
fig.plot(
|
| 166 |
x=d["Lon"].to_list(), y=d["Lat"].to_list(),
|
| 167 |
-
style=
|
| 168 |
-
color=depth.to_list(), cmap=
|
| 169 |
)
|
| 170 |
fig.colorbar(frame=["x+lDepth (km)"], cmap=True, position="JMR+w7c/0.4c+o0.6c/0c")
|
| 171 |
fig.basemap(map_scale="jBL+w50k+o0.6c/0.6c+f+lkm")
|
|
@@ -174,7 +190,7 @@ def plot_map_path(df):
|
|
| 174 |
fig.savefig(outpath, dpi=220)
|
| 175 |
return outpath
|
| 176 |
|
| 177 |
-
# --- Matplotlib
|
| 178 |
if df.empty:
|
| 179 |
return None
|
| 180 |
fig, ax = plt.subplots(figsize=(6, 6))
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import tempfile
|
| 4 |
from datetime import datetime, timedelta, timezone
|
| 5 |
|
|
|
|
| 56 |
return r.json()
|
| 57 |
|
| 58 |
# -----------------------------
|
| 59 |
+
# JSON 解析(讀 EarthquakeInfo)+ 強化數字解析
|
| 60 |
# -----------------------------
|
| 61 |
def _to_float(x):
|
| 62 |
+
"""
|
| 63 |
+
將各種數字表達轉成 float:
|
| 64 |
+
- 純數字:23.5
|
| 65 |
+
- 含單位/文字:'23.5°N'、'121.6 E'、'25.3 公里' -> 取第一個浮點數
|
| 66 |
+
- 其他不可解析 -> None
|
| 67 |
+
"""
|
| 68 |
+
if x is None:
|
| 69 |
+
return None
|
| 70 |
+
if isinstance(x, (int, float)):
|
| 71 |
+
return float(x)
|
| 72 |
+
s = str(x).strip()
|
| 73 |
+
if s == "":
|
| 74 |
return None
|
| 75 |
+
m = re.search(r"[-+]?\d+(?:\.\d+)?", s)
|
| 76 |
+
return float(m.group()) if m else None
|
| 77 |
|
| 78 |
def parse_ea0015(obj):
|
| 79 |
"""
|
|
|
|
| 88 |
|
| 89 |
rows = []
|
| 90 |
for q in quakes:
|
| 91 |
+
ei = q.get("EarthquakeInfo") or q.get("earthquakeInfo") or {}
|
| 92 |
+
epic = ei.get("Epicenter") or ei.get("epicenter") or {}
|
| 93 |
+
mago = ei.get("Magnitude") or ei.get("magnitude") or {}
|
| 94 |
|
| 95 |
origin = (
|
| 96 |
ei.get("OriginTime") or ei.get("originTime")
|
| 97 |
or q.get("OriginTime") or q.get("originTime")
|
| 98 |
)
|
| 99 |
+
|
| 100 |
+
# 經緯度:同時嘗試多種鍵名(不同版本可能不同)
|
| 101 |
+
lat_raw = (
|
| 102 |
+
epic.get("EpicenterLat") or epic.get("epicenterLat")
|
| 103 |
+
or epic.get("EpicenterLatitude") or epic.get("epicenterLatitude")
|
| 104 |
+
or epic.get("Lat") or epic.get("lat")
|
| 105 |
+
)
|
| 106 |
+
lon_raw = (
|
| 107 |
+
epic.get("EpicenterLon") or epic.get("epicenterLon")
|
| 108 |
+
or epic.get("EpicenterLongitude") or epic.get("epicenterLongitude")
|
| 109 |
+
or epic.get("Lon") or epic.get("lon")
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
depth = ei.get("Depth") or ei.get("depth")
|
| 113 |
mag = (
|
| 114 |
+
mago.get("MagnitudeValue") or mago.get("magnitudeValue")
|
| 115 |
+
or mago.get("Magnitude") or mago.get("magnitude")
|
| 116 |
)
|
| 117 |
loc = epic.get("Location") or epic.get("location")
|
| 118 |
url = q.get("Web") or q.get("ReportURL") or q.get("reportURL")
|
| 119 |
|
| 120 |
rows.append({
|
| 121 |
"OriginTime": origin,
|
| 122 |
+
"Lat": _to_float(lat_raw),
|
| 123 |
+
"Lon": _to_float(lon_raw),
|
| 124 |
"Depth_km": _to_float(depth),
|
| 125 |
+
"Magnitude": _to_float(m),
|
| 126 |
"Location": loc,
|
| 127 |
"ReportURL": url,
|
| 128 |
})
|
|
|
|
| 145 |
def plot_trend_path(df):
|
| 146 |
if df.empty:
|
| 147 |
return None
|
|
|
|
| 148 |
m = pd.to_numeric(df["Magnitude"], errors="coerce")
|
| 149 |
fig, ax = plt.subplots(figsize=(6, 4))
|
| 150 |
ax.scatter(df["OriginTime"], m)
|
|
|
|
| 166 |
d = df.dropna(subset=["Lon", "Lat"]).copy()
|
| 167 |
if not d.empty:
|
| 168 |
mag = pd.to_numeric(d["Magnitude"], errors="coerce").fillna(0).clip(lower=0)
|
| 169 |
+
size_cm = 0.06 * (mag + 1.5) # 每點大小(cm)
|
| 170 |
depth = pd.to_numeric(d["Depth_km"], errors="coerce").fillna(0)
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
fig = pygmt.Figure()
|
| 173 |
region = [lon_min, lon_max, lat_min, lat_max]
|
| 174 |
fig.coast(
|
|
|
|
| 177 |
shorelines="0.5p,black", borders="1/0.6p,black",
|
| 178 |
frame=["WSen", "xaf", "yaf"]
|
| 179 |
)
|
| 180 |
+
# 變動大小:使用 sizes 參數(單位 cm),色彩以 depth
|
| 181 |
fig.plot(
|
| 182 |
x=d["Lon"].to_list(), y=d["Lat"].to_list(),
|
| 183 |
+
style="cc", sizes=size_cm.to_list(),
|
| 184 |
+
color=depth.to_list(), cmap="roma", pen="0.25p,black"
|
| 185 |
)
|
| 186 |
fig.colorbar(frame=["x+lDepth (km)"], cmap=True, position="JMR+w7c/0.4c+o0.6c/0c")
|
| 187 |
fig.basemap(map_scale="jBL+w50k+o0.6c/0.6c+f+lkm")
|
|
|
|
| 190 |
fig.savefig(outpath, dpi=220)
|
| 191 |
return outpath
|
| 192 |
|
| 193 |
+
# --- Matplotlib 備援 ---
|
| 194 |
if df.empty:
|
| 195 |
return None
|
| 196 |
fig, ax = plt.subplots(figsize=(6, 6))
|