From ff65688774ace75781509affb210d9da599ef40c Mon Sep 17 00:00:00 2001 From: XANTRONIX Industrial Date: Mon, 14 Apr 2025 11:20:38 -0400 Subject: [PATCH] Implement BUFRSounding.valid_by_timestamp() --- lib/xmet/bufr.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/xmet/bufr.py b/lib/xmet/bufr.py index e3d598d..844b47e 100644 --- a/lib/xmet/bufr.py +++ b/lib/xmet/bufr.py @@ -155,3 +155,24 @@ class BUFRSounding(Sounding): return BUFRSounding.request_sounding(request, datatimes[-1].validPeriod) + + @staticmethod + def valid_by_timestamp(station: str, timestamp: datetime.datetime) -> Self: + epoch = timestamp.timestamp() + request = BUFRSounding.create_request(station) + + datatimes = DataAccessLayer.getAvailableTimes(request) + by_delta = dict() + + for datatime in datatimes: + ts = datatime.getRefTime().time / 1000.0 + delta = epoch - ts + + by_delta[delta] = datatime + + for delta in sorted(by_delta.keys()): + if delta < 0: + continue + + return BUFRSounding.request_sounding(request, + by_delta[delta].validPeriod)