Spaces:
Sleeping
Sleeping
Create earthquake_fetcher.py
Browse files- src/earthquake_fetcher.py +52 -0
src/earthquake_fetcher.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from obspy.clients.fdsn import Client
|
| 3 |
+
from obspy import UTCDateTime
|
| 4 |
+
|
| 5 |
+
# 使用快取來避免重複向IRIS請求相同時間範圍的資料
|
| 6 |
+
@st.cache_data(ttl=600) # 快取10分鐘
|
| 7 |
+
def get_earthquake_data(start_time, end_time, network="TW", station="*", channel="BHZ,HHZ"):
|
| 8 |
+
"""
|
| 9 |
+
從 IRIS FDSN 服務獲取地震波形和測站元數據。
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
start_time (UTCDateTime): 查詢開始時間
|
| 13 |
+
end_time (UTCDateTime): 查詢結束時間
|
| 14 |
+
network (str): 測站網絡代碼
|
| 15 |
+
station (str): 測站代碼
|
| 16 |
+
channel (str): 頻道代碼
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
tuple: (Stream, Inventory) or (None, None) if no data
|
| 20 |
+
"""
|
| 21 |
+
client = Client("IRIS")
|
| 22 |
+
try:
|
| 23 |
+
st.write(f"正在從 IRIS 獲取 {network} 測站網的資料...")
|
| 24 |
+
stream = client.get_waveforms(
|
| 25 |
+
network=network,
|
| 26 |
+
station=station,
|
| 27 |
+
location="*",
|
| 28 |
+
channel=channel,
|
| 29 |
+
starttime=start_time,
|
| 30 |
+
endtime=end_time,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
st.write("正在獲取測站座標...")
|
| 34 |
+
inventory = client.get_stations(
|
| 35 |
+
network=network,
|
| 36 |
+
station=station,
|
| 37 |
+
location="*",
|
| 38 |
+
channel=channel,
|
| 39 |
+
starttime=start_time,
|
| 40 |
+
endtime=end_time,
|
| 41 |
+
level="station"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if not stream:
|
| 45 |
+
st.warning("在指定時間範圍內未找到任何波形資料。")
|
| 46 |
+
return None, None
|
| 47 |
+
|
| 48 |
+
return stream, inventory
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
st.error(f"資料獲取失敗: {e}")
|
| 52 |
+
return None, None
|