145 lines
5.3 KiB
Python
145 lines
5.3 KiB
Python
"""Collector for NOAA NCEP GFS 10m global wind field (`global_wind_gfs`)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from app.core.logging import get_logger
|
|
from app.core.websocket.broadcaster import broadcaster
|
|
from app.services.collectors.base import BaseCollector
|
|
from app.services.earth_wind import WIND_REGIONS, collect_latest_wind_grid
|
|
|
|
logger = get_logger(__name__, service="collector.wind_gfs")
|
|
|
|
_REGION_COORDS = {
|
|
item["key"]: (
|
|
(float(item["lat_min"]) + float(item["lat_max"])) / 2.0,
|
|
(float(item["lon_min"]) + float(item["lon_max"])) / 2.0,
|
|
)
|
|
for item in WIND_REGIONS
|
|
}
|
|
|
|
|
|
class EarthWindGFSCollector(BaseCollector):
|
|
"""Collect NOAA NCEP GFS global 10m wind field grids and regional gale analytics."""
|
|
|
|
name = "global_wind_gfs"
|
|
priority = "P1"
|
|
module = "L2"
|
|
frequency_hours = 3
|
|
data_type = "wind_field_record"
|
|
fail_on_empty = True
|
|
preserve_snapshot_on_failure = True
|
|
full_snapshot_reconcile = True
|
|
|
|
async def fetch(self) -> list[dict[str, Any]]:
|
|
result = await collect_latest_wind_grid(db=self._db_session)
|
|
return [result]
|
|
|
|
def transform(self, raw_data: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
if not raw_data:
|
|
return []
|
|
bundle = raw_data[0]
|
|
run = bundle.get("run") or {}
|
|
summary = bundle.get("global_summary") or {}
|
|
regions = bundle.get("regions") or []
|
|
cores = bundle.get("storm_cores") or []
|
|
|
|
version = str(run.get("version") or "")
|
|
captured_at = run.get("captured_at")
|
|
forecast_time = run.get("forecast_time")
|
|
|
|
records: list[dict[str, Any]] = [
|
|
{
|
|
"source_id": "wind_snapshot:latest",
|
|
"name": "全球 10m 地面风场快照 (NOAA GFS)",
|
|
"title": f"全球风场 {version}",
|
|
"description": (
|
|
f"全球均速={summary.get('mean_speed_ms')} m/s, 峰值={summary.get('max_speed_ms')} m/s, "
|
|
f"大风核心={summary.get('storm_core_count', 0)} 个"
|
|
),
|
|
"latitude": 0.0,
|
|
"longitude": 0.0,
|
|
"value": summary.get("mean_speed_ms"),
|
|
"unit": "m/s",
|
|
"reference_date": captured_at,
|
|
"metadata": {
|
|
"record_kind": "wind_field_snapshot",
|
|
"version": version,
|
|
"captured_at": captured_at,
|
|
"forecast_time": forecast_time,
|
|
**summary,
|
|
},
|
|
}
|
|
]
|
|
|
|
for reg in regions:
|
|
key = str(reg.get("key") or "")
|
|
center_lat, center_lon = _REGION_COORDS.get(key, (0.0, 0.0))
|
|
records.append(
|
|
{
|
|
"source_id": f"wind_region:{key}",
|
|
"name": str(reg.get("name") or key),
|
|
"title": f"区域风场 · {reg.get('name') or key}",
|
|
"description": (
|
|
f"均速 {reg.get('mean_speed_ms')} m/s, 峰值 {reg.get('max_speed_ms')} m/s, "
|
|
f"主导风向 {reg.get('dominant_cardinal')}"
|
|
),
|
|
"latitude": center_lat,
|
|
"longitude": center_lon,
|
|
"value": reg.get("mean_speed_ms"),
|
|
"unit": "m/s",
|
|
"reference_date": captured_at,
|
|
"metadata": {
|
|
"record_kind": "wind_region_summary",
|
|
"version": version,
|
|
"captured_at": captured_at,
|
|
**reg,
|
|
},
|
|
}
|
|
)
|
|
|
|
for core in cores:
|
|
cid = str(core.get("id") or "")
|
|
records.append(
|
|
{
|
|
"source_id": f"wind_core:{cid}",
|
|
"name": f"大风核心 {cid} ({core.get('region') or ''})",
|
|
"title": f"大风中心 {cid} · {core.get('max_speed_ms')} m/s",
|
|
"description": (
|
|
f"估算面积 {core.get('area_km2')} km², "
|
|
f"蒲福 {core.get('beaufort')} 级"
|
|
),
|
|
"latitude": core.get("lat"),
|
|
"longitude": core.get("lon"),
|
|
"value": core.get("max_speed_ms"),
|
|
"unit": "m/s",
|
|
"reference_date": captured_at,
|
|
"metadata": {
|
|
"record_kind": "wind_storm_core",
|
|
"version": version,
|
|
"captured_at": captured_at,
|
|
**core,
|
|
},
|
|
}
|
|
)
|
|
|
|
return records
|
|
|
|
async def run(self, db) -> dict[str, Any]:
|
|
result = await super().run(db)
|
|
if result.get("status") == "success":
|
|
try:
|
|
await broadcaster.broadcast_earth_update(
|
|
{
|
|
"event": "earth.layer.changed",
|
|
"action": "wind_grid_updated",
|
|
"source": self.name,
|
|
"layers": ["windField"],
|
|
"refresh_strategy": "reload",
|
|
}
|
|
)
|
|
except Exception:
|
|
pass
|
|
return result
|