From 854cfa65e49496d6ddf22595fdbc09b3b290277e Mon Sep 17 00:00:00 2001 From: XANTRONIX Industrial Date: Mon, 14 Apr 2025 09:16:15 -0400 Subject: [PATCH] Simplify code a bit --- lib/xmet/bufr.py | 74 +++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/lib/xmet/bufr.py b/lib/xmet/bufr.py index cf15db5..2cf6d58 100644 --- a/lib/xmet/bufr.py +++ b/lib/xmet/bufr.py @@ -44,6 +44,39 @@ class BUFRSounding(Sounding): return sample + def sample(self, pressure: float, height: float) -> SoundingSample: + if pressure == -99.99: + sample = self.sample_by_height(height) + else: + sample = self.sample_by_pressure(pressure) + + if height != -9999.0: + sample.height = height + + return sample + + def record_wind(self, + pressure: float, + height: float, + wind_speed: float, + wind_dir: float): + sample = self.sample(pressure, height) + + if wind_speed != -9999.0 and wind_dir != -9999.0: + sample.wind_speed = wind_speed + sample.wind_dir = wind_dir + + def record_temp_dewpoint(self, + pressure: float, + height: float, + temp: float, + dewpoint: float): + sample = self.sample(pressure, height) + + if temp != -9999.0 and dewpoint != -9999.0: + sample.temp = temp + sample.dewpoint = dewpoint + @staticmethod def init(): DataAccessLayer.changeEDEXHost(BUFRSounding.EDEX_HOST) @@ -84,45 +117,28 @@ class BUFRSounding(Sounding): params = item.getParameters() if set(params) & BUFRSounding.BUFR_PARAMS_MAN: - pressure = item.getNumber('prMan') / 100.0 - height = item.getNumber('htMan') - - if pressure == -99.99: - sample = sounding.sample_by_height(height) - else: - sample = sounding.sample_by_pressure(pressure) - - if height != -9999.0: - sample.height = height - + pressure = item.getNumber('prMan') / 100.0 + height = item.getNumber('htMan') wind_dir = item.getNumber('wsMan') wind_speed = item.getNumber('wdMan') - if wind_dir != -9999.0 and wind_speed != -9999.0: - sample.wind_speed = wind_dir - sample.wind_dir = wind_speed + sounding.record_wind(pressure, height, wind_dir, wind_speed) if set(params) & BUFRSounding.BUFR_PARAMS_SIGT: pressure = item.getNumber('prSigT') / 100.0 - sample = sounding.sample_by_pressure(pressure) + height = -9999.0 + temp = celsius(item.getNumber('tpSigT')) + dewpoint = celsius(item.getNumber('tdSigT')) - sample.temp = celsius(item.getNumber('tpSigT')) - sample.dewpoint = celsius(item.getNumber('tdSigT')) + sounding.record_temp_dewpoint(pressure, height, temp, dewpoint) if set(params) & BUFRSounding.BUFR_PARAMS_SIGW: - pressure = item.getNumber('prSigW') / 100.0 - height = item.getNumber('htSigW') + pressure = item.getNumber('prSigW') / 100.0 + height = item.getNumber('htSigW') + wind_speed = item.getNumber('wsSigW') + wind_dir = item.getNumber('wdSigW') - if pressure == -99.99: - sample = sounding.sample_by_height(height) - else: - sample = sounding.sample_by_pressure(pressure) - - if height != -9999.0: - sample.height = height - - sample.wind_speed = item.getNumber('wsSigW') - sample.wind_dir = item.getNumber('wdSigW') + sounding.record_wind(pressure, height, wind_speed, wind_dir) for pressure in sorted(sounding.samples_by_pressure.keys(), reverse=True): sounding.samples.append(sounding.samples_by_pressure[pressure])