From 8c640b8616293edd112b8a8c32a57da615818612 Mon Sep 17 00:00:00 2001 From: "2569718930@qq.com" <2569718930@qq.com> Date: Mon, 18 May 2026 18:28:18 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=20KNMI=20netCDF=20=E6=96=B0?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=B8=83=E5=B1=80=EF=BC=9A(station,time)=20?= =?UTF-8?q?=E6=9B=BF=E4=BB=A3=20(time,station)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/data_collection/knmi_sources.py | 38 +++++++++++++++++------------ 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/data_collection/knmi_sources.py b/src/data_collection/knmi_sources.py index a88505cb..d942b8d7 100644 --- a/src/data_collection/knmi_sources.py +++ b/src/data_collection/knmi_sources.py @@ -146,23 +146,31 @@ class KnmiSourceMixin: return None data = ta[:] - latest_temp = float(data[-1, idx]) if data.ndim == 2 else float(data[-1]) + # Handle both old (time,station) and new (station,time) layouts + dim_names = [str(d.name).lower() for d in ta.dimensions] + if data.ndim == 2: + if dim_names and dim_names[0] == "station": + latest_temp = float(data[idx, -1]) + else: + latest_temp = float(data[-1, idx]) + else: + latest_temp = float(data[-1]) - # Wind speed - ff = nc.variables.get("ff", None) - wind_ms = None - if ff is not None: - wind_data = ff[:] - wind_ms = float(wind_data[-1, idx] if wind_data.ndim == 2 else wind_data[-1]) + def _read_2d(var, idx: int) -> Optional[float]: + if var is None: + return None + vdata = var[:] + if vdata.ndim == 2: + vdim_names = [str(d.name).lower() for d in var.dimensions] + if vdim_names and vdim_names[0] == "station": + return float(vdata[idx, -1]) + return float(vdata[-1, idx]) + return float(vdata[-1]) - # Pressure - p0 = nc.variables.get("p0", None) - pressure_hpa = None - if p0 is not None: - p_data = p0[:] - pressure_hpa = float(p_data[-1, idx] if p_data.ndim == 2 else p_data[-1]) - if pressure_hpa > 5000: - pressure_hpa = pressure_hpa / 100.0 + wind_ms = _read_2d(nc.variables.get("ff"), idx) + pressure_hpa = _read_2d(nc.variables.get("p0"), idx) + if pressure_hpa is not None and pressure_hpa > 5000: + pressure_hpa = pressure_hpa / 100.0 nc.close()