forked from linkong/planet
185 lines
7.2 KiB
Python
185 lines
7.2 KiB
Python
"""Built-in collector for global real-time satellite IR cloud imagery and spatial analytics."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.core.websocket.broadcaster import broadcaster
|
|
from app.models.datasource_config import DataSourceConfig
|
|
from app.services.collectors.base import BaseCollector
|
|
from app.services.earth_clouds import collect_latest_cloud_frame
|
|
|
|
|
|
class EarthCloudMapCollector(BaseCollector):
|
|
"""Collect multi-satellite IR cloud map frames and structured atmospheric analytics."""
|
|
|
|
name = "global_cloud_map"
|
|
priority = "P1"
|
|
module = "L2"
|
|
frequency_hours = 1
|
|
data_type = "cloud_map_record"
|
|
fail_on_empty = True
|
|
preserve_snapshot_on_failure = True
|
|
full_snapshot_reconcile = True
|
|
|
|
async def _load_override_config(self) -> dict[str, Any]:
|
|
if not self._db_session or not callable(getattr(self._db_session, "execute", None)):
|
|
return {}
|
|
try:
|
|
result = await self._db_session.execute(
|
|
select(DataSourceConfig)
|
|
.where(DataSourceConfig.name == self.name)
|
|
.where(DataSourceConfig.is_active.is_(True))
|
|
.limit(1)
|
|
)
|
|
record = result.scalar_one_or_none()
|
|
if record and isinstance(record.config, dict):
|
|
return {
|
|
key: value
|
|
for key, value in record.config.items()
|
|
if key
|
|
in {
|
|
"enabled",
|
|
"primary_source",
|
|
"fallback_source",
|
|
"resolution",
|
|
"ir_threshold",
|
|
"contrast_gamma",
|
|
"convective_threshold",
|
|
"min_cluster_area_km2",
|
|
"polar_feather_enabled",
|
|
"max_history_frames",
|
|
"request_timeout_seconds",
|
|
"geocolor_enabled",
|
|
"relief_shading_strength",
|
|
"optical_flow_enabled",
|
|
}
|
|
and value is not None
|
|
}
|
|
except Exception:
|
|
return {}
|
|
return {}
|
|
|
|
async def fetch(self) -> list[dict[str, Any]]:
|
|
override = await self._load_override_config()
|
|
result = await collect_latest_cloud_frame(self._db_session, override_config=override)
|
|
return [result]
|
|
|
|
def transform(self, raw_data: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
if not raw_data:
|
|
return []
|
|
bundle = raw_data[0]
|
|
frame = bundle.get("frame") or {}
|
|
summary = bundle.get("global_summary") or {}
|
|
regions = bundle.get("regions") or []
|
|
clusters = bundle.get("convective_clusters") or []
|
|
|
|
version = str(frame.get("version") or "")
|
|
captured_at = frame.get("captured_at")
|
|
source_used = str(frame.get("source") or "multi_sat_ir")
|
|
satellites = list(frame.get("satellites") or [])
|
|
degraded = bool(frame.get("degraded", False))
|
|
|
|
records: list[dict[str, Any]] = [
|
|
{
|
|
"source_id": "cloud_snapshot:latest",
|
|
"name": "全球实时红外云图快照",
|
|
"title": f"全球云图 {version}",
|
|
"description": (
|
|
f"来源={source_used}, 全球平均云量={summary.get('global_cloud_cover_pct')}%, "
|
|
f"强对流云团={summary.get('convective_cluster_count', 0)} 个"
|
|
),
|
|
"latitude": 0.0,
|
|
"longitude": 0.0,
|
|
"value": summary.get("global_cloud_cover_pct"),
|
|
"unit": "%",
|
|
"reference_date": captured_at,
|
|
"metadata": {
|
|
"record_kind": "cloud_map_snapshot",
|
|
"version": version,
|
|
"captured_at": captured_at,
|
|
"cloud_source": source_used,
|
|
"satellites": satellites,
|
|
"degraded": degraded,
|
|
"resolution": frame.get("resolution"),
|
|
"image_url": frame.get("image_url"),
|
|
"file_size_bytes": frame.get("file_size_bytes"),
|
|
"sha256": frame.get("sha256"),
|
|
**summary,
|
|
},
|
|
}
|
|
]
|
|
|
|
for region in regions:
|
|
key = str(region.get("key") or "")
|
|
records.append(
|
|
{
|
|
"source_id": f"cloud_region:{key}",
|
|
"name": str(region.get("name") or key),
|
|
"title": f"区域云量 · {region.get('name') or key}",
|
|
"description": (
|
|
f"云量 {region.get('cloud_cover_pct')}%, "
|
|
f"厚云 {region.get('overcast_coverage_pct')}%, "
|
|
f"最低亮温 {region.get('min_brightness_temp_c')}°C"
|
|
),
|
|
"latitude": region.get("center_lat"),
|
|
"longitude": region.get("center_lon"),
|
|
"value": region.get("cloud_cover_pct"),
|
|
"unit": "%",
|
|
"reference_date": captured_at,
|
|
"metadata": {
|
|
"record_kind": "cloud_region_summary",
|
|
"version": version,
|
|
"captured_at": captured_at,
|
|
**region,
|
|
},
|
|
}
|
|
)
|
|
|
|
for cluster in clusters:
|
|
cid = str(cluster.get("cluster_id") or "")
|
|
records.append(
|
|
{
|
|
"source_id": f"cloud_cluster:{cid}",
|
|
"name": f"强对流云团 {cid} ({cluster.get('region') or ''})",
|
|
"title": f"强对流核 {cid} · {cluster.get('min_brightness_temp_c')}°C",
|
|
"description": (
|
|
f"估算面积 {cluster.get('area_km2')} km², "
|
|
f"峰值强度 {cluster.get('peak_intensity')}%, "
|
|
f"最低亮温 {cluster.get('min_brightness_temp_c')}°C"
|
|
),
|
|
"latitude": cluster.get("latitude"),
|
|
"longitude": cluster.get("longitude"),
|
|
"value": cluster.get("area_km2"),
|
|
"unit": "km2",
|
|
"reference_date": captured_at,
|
|
"metadata": {
|
|
"record_kind": "cloud_convective_cluster",
|
|
"version": version,
|
|
"captured_at": captured_at,
|
|
**cluster,
|
|
},
|
|
}
|
|
)
|
|
|
|
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": "cloud_map_updated",
|
|
"source": self.name,
|
|
"layers": ["clouds"],
|
|
"refresh_strategy": "reload",
|
|
}
|
|
)
|
|
except Exception:
|
|
pass
|
|
return result
|