修改启动前检查方式
This commit is contained in:
@@ -9,7 +9,6 @@ from app.core.venv_manager import check_virtualenv
|
||||
from app.core.dependency_manager import check_dependencies
|
||||
from app.utils.logger import get_logger
|
||||
from app.utils.thread_pool_manager import block_main_thread, thread_pool_manager
|
||||
from app.core.rainfall_manager import rainfall_manager
|
||||
|
||||
|
||||
class AppLauncher:
|
||||
@@ -52,6 +51,8 @@ class AppLauncher:
|
||||
|
||||
def start():
|
||||
"""启动应用服务"""
|
||||
from app.core.rainfall_manager import rainfall_manager
|
||||
|
||||
logger = get_logger()
|
||||
|
||||
# 启动降雨站点监测
|
||||
|
||||
@@ -8,8 +8,6 @@ from typing import Optional
|
||||
|
||||
from app.utils import thread_pool_manager
|
||||
from app.utils.logger import get_logger
|
||||
from app.repositories.rainfall_repository import rainfall_repository
|
||||
from app.services.rainfall_grid_service import rainfall_grid_service
|
||||
|
||||
|
||||
class RainfallManager:
|
||||
@@ -46,6 +44,8 @@ class RainfallManager:
|
||||
Args:
|
||||
initial_query_time: 初始查询时间
|
||||
"""
|
||||
from app.repositories.rainfall_repository import rainfall_repository
|
||||
|
||||
query_time = initial_query_time
|
||||
|
||||
while True:
|
||||
@@ -83,6 +83,9 @@ class RainfallManager:
|
||||
query_time: 查询时间
|
||||
max_id: 最大ID
|
||||
"""
|
||||
from app.repositories.rainfall_repository import rainfall_repository
|
||||
from app.services.rainfall_grid_service import rainfall_grid_service
|
||||
|
||||
try:
|
||||
self.logger.info(f"开始生成降雨栅格,查询时间: {query_time}, ID: {max_id}")
|
||||
|
||||
|
||||
@@ -8,17 +8,7 @@ from datetime import datetime
|
||||
from typing import Optional, List, Dict, Any, Tuple
|
||||
from io import BytesIO
|
||||
|
||||
import numpy as np
|
||||
from scipy.spatial import Delaunay, ConvexHull
|
||||
from scipy.interpolate import griddata
|
||||
from scipy.ndimage import gaussian_filter
|
||||
from PIL import Image
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.colors import ListedColormap, BoundaryNorm
|
||||
|
||||
from config import settings
|
||||
from app.utils.logger import get_logger
|
||||
from app.utils.redis_helper import redis_helper
|
||||
|
||||
|
||||
class RainfallGridService:
|
||||
@@ -54,7 +44,7 @@ class RainfallGridService:
|
||||
# 栅格分辨率(度)
|
||||
self.grid_resolution = 0.01 # 约1km
|
||||
|
||||
def _create_buffer_points(self, points_array: np.ndarray) -> np.ndarray:
|
||||
def _create_buffer_points(self, points_array) -> 'np.ndarray':
|
||||
"""
|
||||
创建缓冲点:在原始站点外围生成虚拟点以扩展插值区域
|
||||
|
||||
@@ -64,6 +54,8 @@ class RainfallGridService:
|
||||
Returns:
|
||||
缓冲点坐标数组
|
||||
"""
|
||||
import numpy as np
|
||||
|
||||
# 计算站点分布的中心
|
||||
center = np.mean(points_array, axis=0)
|
||||
|
||||
@@ -89,7 +81,7 @@ class RainfallGridService:
|
||||
|
||||
def _calculate_adaptive_max_distance(
|
||||
self,
|
||||
points_array: np.ndarray,
|
||||
points_array,
|
||||
base_distance: float = 0.3,
|
||||
min_distance: float = 0.15,
|
||||
max_distance: float = 0.5
|
||||
@@ -106,11 +98,13 @@ class RainfallGridService:
|
||||
Returns:
|
||||
自适应的最大影响距离
|
||||
"""
|
||||
import numpy as np
|
||||
from scipy.spatial import distance_matrix
|
||||
|
||||
if len(points_array) < 3:
|
||||
return base_distance
|
||||
|
||||
# 计算站点间的平均距离
|
||||
from scipy.spatial import distance_matrix
|
||||
dist_matrix = distance_matrix(points_array, points_array)
|
||||
|
||||
# 排除对角线(自身距离为0)
|
||||
@@ -138,6 +132,10 @@ class RainfallGridService:
|
||||
Returns:
|
||||
插值结果字典
|
||||
"""
|
||||
import numpy as np
|
||||
from scipy.spatial import Delaunay, ConvexHull, distance_matrix
|
||||
from scipy.ndimage import gaussian_filter
|
||||
|
||||
# 提取站点坐标和降雨量
|
||||
points_array = np.array([[s['lon'], s['lat']] for s in station_data])
|
||||
values_array = np.array([s['rainfall'] for s in station_data])
|
||||
@@ -184,7 +182,6 @@ class RainfallGridService:
|
||||
hull_mask = hull_mask.reshape(grid_lon.shape)
|
||||
|
||||
# 计算置信度:基于到最近站点的距离
|
||||
from scipy.spatial import distance_matrix
|
||||
grid_valid = grid_points[hull_mask.ravel()]
|
||||
if len(grid_valid) > 0:
|
||||
dist_to_stations = distance_matrix(grid_valid, points_array)
|
||||
@@ -297,6 +294,12 @@ class RainfallGridService:
|
||||
Returns:
|
||||
PNG文件相对路径,失败返回None
|
||||
"""
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.colors import ListedColormap, BoundaryNorm
|
||||
from PIL import Image
|
||||
from config import settings
|
||||
|
||||
try:
|
||||
grid_values = grid_data['grid_values']
|
||||
lon_range = grid_data['lon_range']
|
||||
@@ -384,6 +387,9 @@ class RainfallGridService:
|
||||
query_time: 查询时间(datetime对象或字符串)
|
||||
station_data: 站点数据
|
||||
"""
|
||||
from config import settings
|
||||
from app.utils.redis_helper import redis_helper
|
||||
|
||||
try:
|
||||
redis_key = settings.REDIS_RAIN_STATION_GRID_KEY
|
||||
redis_identifier_key = settings.REDIS_RAIN_STATION_IDENTIFIER_KEY
|
||||
|
||||
@@ -3,13 +3,15 @@
|
||||
使用 Dynaconf 进行环境隔离配置
|
||||
"""
|
||||
from pathlib import Path
|
||||
from app.core.launcher import AppLauncher
|
||||
|
||||
# 添加项目根目录到Python路径
|
||||
project_root = Path(__file__).parent
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 延迟导入,确保在依赖安装前不会导入第三方库
|
||||
from app.core.launcher import AppLauncher
|
||||
|
||||
# 创建并运行启动器
|
||||
launcher = AppLauncher(project_root)
|
||||
launcher.run()
|
||||
|
||||
Reference in New Issue
Block a user