Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
import io
|
| 3 |
import tempfile
|
| 4 |
from datetime import datetime, timedelta, timezone
|
| 5 |
|
|
@@ -56,17 +55,8 @@ def fetch_reports(time_from, time_to):
|
|
| 56 |
return r.json()
|
| 57 |
|
| 58 |
# -----------------------------
|
| 59 |
-
# JSON
|
| 60 |
# -----------------------------
|
| 61 |
-
def _safe_get(d, *keys, default=None):
|
| 62 |
-
cur = d
|
| 63 |
-
for k in keys:
|
| 64 |
-
if isinstance(cur, dict) and k in cur:
|
| 65 |
-
cur = cur[k]
|
| 66 |
-
else:
|
| 67 |
-
return default
|
| 68 |
-
return cur
|
| 69 |
-
|
| 70 |
def _to_float(x):
|
| 71 |
try:
|
| 72 |
if x is None or str(x).strip() == "":
|
|
@@ -134,8 +124,10 @@ def _save_fig_to_tmp(fig, suffix=".png", dpi=180):
|
|
| 134 |
def plot_trend_path(df):
|
| 135 |
if df.empty:
|
| 136 |
return None
|
|
|
|
|
|
|
| 137 |
fig, ax = plt.subplots(figsize=(6, 4))
|
| 138 |
-
ax.scatter(df["OriginTime"],
|
| 139 |
ax.set_xlabel("Origin Time (Taipei)")
|
| 140 |
ax.set_ylabel("Magnitude")
|
| 141 |
ax.grid(True, linestyle="--", alpha=0.4)
|
|
@@ -153,9 +145,9 @@ def plot_map_path(df):
|
|
| 153 |
if HAS_PYGMT and not df.empty:
|
| 154 |
d = df.dropna(subset=["Lon", "Lat"]).copy()
|
| 155 |
if not d.empty:
|
| 156 |
-
mag = d["Magnitude"].fillna(0).clip(lower=0)
|
| 157 |
size_cm = 0.06 * (mag + 1.5) # 每點大小(cm)
|
| 158 |
-
depth = d["Depth_km"].fillna(0)
|
| 159 |
|
| 160 |
vmin = float(max(0, float(depth.min()) if len(depth) else 0))
|
| 161 |
vmax = float(max(100, float(depth.max()) if len(depth) else 100))
|
|
@@ -182,17 +174,18 @@ def plot_map_path(df):
|
|
| 182 |
fig.savefig(outpath, dpi=220)
|
| 183 |
return outpath
|
| 184 |
|
| 185 |
-
# --- Matplotlib
|
| 186 |
if df.empty:
|
| 187 |
return None
|
| 188 |
fig, ax = plt.subplots(figsize=(6, 6))
|
| 189 |
ax.set_xlim(lon_min, lon_max)
|
| 190 |
ax.set_ylim(lat_min, lat_max)
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
| 196 |
cb = plt.colorbar(sc, ax=ax, fraction=0.046, pad=0.04)
|
| 197 |
cb.set_label("Depth (km)")
|
| 198 |
ax.set_xlabel("Longitude (°E)"); ax.set_ylabel("Latitude (°N)")
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import tempfile
|
| 3 |
from datetime import datetime, timedelta, timezone
|
| 4 |
|
|
|
|
| 55 |
return r.json()
|
| 56 |
|
| 57 |
# -----------------------------
|
| 58 |
+
# JSON 解析(讀 EarthquakeInfo)
|
| 59 |
# -----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def _to_float(x):
|
| 61 |
try:
|
| 62 |
if x is None or str(x).strip() == "":
|
|
|
|
| 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)
|
| 131 |
ax.set_xlabel("Origin Time (Taipei)")
|
| 132 |
ax.set_ylabel("Magnitude")
|
| 133 |
ax.grid(True, linestyle="--", alpha=0.4)
|
|
|
|
| 145 |
if HAS_PYGMT and not df.empty:
|
| 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) # 每點大小(cm)
|
| 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))
|
|
|
|
| 174 |
fig.savefig(outpath, dpi=220)
|
| 175 |
return outpath
|
| 176 |
|
| 177 |
+
# --- Matplotlib 備援(顯式轉數值,避免 FutureWarning) ---
|
| 178 |
if df.empty:
|
| 179 |
return None
|
| 180 |
fig, ax = plt.subplots(figsize=(6, 6))
|
| 181 |
ax.set_xlim(lon_min, lon_max)
|
| 182 |
ax.set_ylim(lat_min, lat_max)
|
| 183 |
+
|
| 184 |
+
m = pd.to_numeric(df["Magnitude"], errors="coerce").fillna(0).clip(lower=0)
|
| 185 |
+
s = (m + 2) ** 3
|
| 186 |
+
depth = pd.to_numeric(df["Depth_km"], errors="coerce")
|
| 187 |
+
|
| 188 |
+
sc = ax.scatter(df["Lon"], df["Lat"], s=s, c=depth, alpha=0.85, edgecolor="black")
|
| 189 |
cb = plt.colorbar(sc, ax=ax, fraction=0.046, pad=0.04)
|
| 190 |
cb.set_label("Depth (km)")
|
| 191 |
ax.set_xlabel("Longitude (°E)"); ax.set_ylabel("Latitude (°N)")
|