Spaces:
Runtime error
Runtime error
Commit ·
cbfa1d2
1
Parent(s): 0a69129
add user-defined Vs30 input to intensity prediction and update related functions
Browse files21 unmodified lines22232425262710 unmodified lines38394041424344322 unmodified lines36736836937037137237337437578 unmodified lines45445545645745845946011 unmodified lines4724734744754764774784792 unmodified lines48248348448548648748846 unmodified lines53553653753853954011 unmodified lines55255355455555655755821 unmodified lines# 載入 Vs30 資料集(從 Hugging Face 下載)from huggingface_hub import hf_hub_downloadtry:logger.info("從 Hugging Face 載入 Vs30 資料...")vs30_file = hf_hub_download(10 unmodified linestree = cKDTree(vs30_table[["lat", "lon"]])logger.info("Vs30 資料載入完成")except Exception as e:logger.error(f"Vs30 資料載入失敗: {e}")# 載入目標測站target_file = "station/eew_target.csv"322 unmodified linesreturn datadef get_vs30(lat, lon):distance, i = tree.query([float(lat), float(lon)])vs30 = vs30_table.iloc[i]["Vs30"]return float(vs30)78 unmodified linesreturn figdef predict_intensity(event_name, start_time, end_time, lon, lat):# 1. 載入波形times, data, sampling_rate = load_waveform(event_name)11 unmodified lines[[waveform_processed, waveform_processed, waveform_processed]])waveform_3c = waveform_3c.transpose(0, 2, 1) # (1, 3000, 3)# 準備測站資訊vs30 = get_vs30(lat, lon)station_info_input = np.array([[lat, lon, 100, vs30]]) # elevation 假設 100m# 準備目標測站資訊2 unmodified linesfor target in target_dict[:25]: # 限制25個目標target_list.append([target["latitude"], target["longitude"],target["elevation"],get_vs30(target["latitude"], target["longitude"])])target_names.append(target["station"])# 組合成 tensor46 unmodified lineslon_input = gr.Number(value=121.5, label="經度")lat_input = gr.Number(value=24.0, label="緯度")predict_btn = gr.Button("🔮 執行預測", variant="primary")# 右側:震度分布圖11 unmodified lines# 綁定事件predict_btn.click(fn=predict_intensity,inputs=[event_dropdown, start_slider, end_slider, lon_input, lat_input],outputs=[waveform_plot, intensity_plot, stats_output])21 unmodified lines22232425262728293010 unmodified lines4142434445464748322 unmodified lines37137237337437537637737837938038138238338478 unmodified lines46346446546646746846911 unmodified lines4814824834844854864874882 unmodified lines49149249349449549649746 unmodified lines54454554654754854955055155255355455555611 unmodified lines56856957057157257357421 unmodified lines# 載入 Vs30 資料集(從 Hugging Face 下載)from huggingface_hub import hf_hub_downloadtree = Nonevs30_table = Nonetry:logger.info("從 Hugging Face 載入 Vs30 資料...")vs30_file = hf_hub_download(10 unmodified linestree = cKDTree(vs30_table[["lat", "lon"]])logger.info("Vs30 資料載入完成")except Exception as e:logger.warning(f"Vs30 資料載入失敗: {e}")logger.warning("將使用預設 Vs30 值 (600 m/s)")# 載入目標測站target_file = "station/eew_target.csv"322 unmodified linesreturn datadef get_vs30(lat, lon, user_vs30=600):if tree is None or vs30_table is None:# 如果 Vs30 資料未載入,使用使用者輸入的值logger.info(f"使用使用者輸入的 Vs30 值 ({user_vs30} m/s) for ({lat}, {lon})")return float(user_vs30)distance, i = tree.query([float(lat), float(lon)])vs30 = vs30_table.iloc[i]["Vs30"]logger.info(f"從資料庫查詢到 Vs30 值 ({vs30} m/s) for ({lat}, {lon})")return float(vs30)78 unmodified linesreturn figdef predict_intensity(event_name, start_time, end_time, lon, lat, vs30_input):# 1. 載入波形times, data, sampling_rate = load_waveform(event_name)11 unmodified lines[[waveform_processed, waveform_processed, waveform_processed]])waveform_3c = waveform_3c.transpose(0, 2, 1) # (1, 3000, 3)# 準備測站資訊(使用使用者輸入的 Vs30 或資料庫值)vs30 = get_vs30(lat, lon, vs30_input)station_info_input = np.array([[lat, lon, 100, vs30]]) # elevation 假設 100m# 準備目標測站資訊2 unmodified linesfor target in target_dict[:25]: # 限制25個目標target_list.append([target["latitude"], target["longitude"],target["elevation"],get_vs30(target["latitude"], target["longitude"], vs30_input)])target_names.append(target["station"])# 組合成 tensor46 unmodified lineslon_input = gr.Number(value=121.5, label="經度")lat_input = gr.Number(value=24.0, label="緯度")with gr.Row():vs30_input = gr.Number(value=600,label="Vs30 (m/s)",info="剪力波速度(預設 600 m/s,若有 Vs30 資料會自動覆蓋)")predict_btn = gr.Button("🔮 執行預測", variant="primary")# 右側:震度分布圖11 unmodified lines# 綁定事件predict_btn.click(fn=predict_intensity,inputs=[event_dropdown, start_slider, end_slider, lon_input, lat_input, vs30_input],outputs=[waveform_plot, intensity_plot, stats_output])
app.py+23-7