From a7dc9a2e5922dfb6bfb38042fd28add8d78d0ec7 Mon Sep 17 00:00:00 2001 From: tmharty Date: Fri, 8 Sep 2023 16:25:48 -0700 Subject: [PATCH 1/2] Recalculate RAOB from Wyoming This change allows for a user to request that the RAOB is "Recalculated." This can be required if an earlier user of the site requests the observation before the entire observation is available. This results in an incomplete observation being cached. Recalculation requires additional resources, so the default is to not recalculate. --- src/siphon/simplewebservice/wyoming.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/siphon/simplewebservice/wyoming.py b/src/siphon/simplewebservice/wyoming.py index 639dc8de7..835a9cb0c 100644 --- a/src/siphon/simplewebservice/wyoming.py +++ b/src/siphon/simplewebservice/wyoming.py @@ -28,7 +28,7 @@ def __init__(self): super().__init__('http://weather.uwyo.edu/cgi-bin/sounding') @classmethod - def request_data(cls, time, site_id, **kwargs): + def request_data(cls, time, site_id, recalc=False, **kwargs): r"""Retrieve upper air observations from the Wyoming archive. Parameters @@ -40,6 +40,11 @@ def request_data(cls, time, site_id, **kwargs): The three letter ICAO identifier of the station for which data should be downloaded. + recalc : bool + Whether to request the server recalculate the data (i.e. ignore its cache) before + returning it. Defaults to False. NOTE: This should be used sparingly because it + will increase the load on the service. + kwargs Arbitrary keyword arguments to use to initialize source @@ -50,10 +55,10 @@ def request_data(cls, time, site_id, **kwargs): """ endpoint = cls() - df = endpoint._get_data(time, site_id) + df = endpoint._get_data(time, site_id, recalc=recalc) return df - def _get_data(self, time, site_id): + def _get_data(self, time, site_id, recalc=False): r"""Download and parse upper air observations from an online archive. Parameters @@ -65,12 +70,15 @@ def _get_data(self, time, site_id): The three letter ICAO identifier of the station for which data should be downloaded. + recalc : bool + Returns recalculated data if True. Defaults to False. + Returns ------- `pandas.DataFrame` """ - raw_data = self._get_data_raw(time, site_id) + raw_data = self._get_data_raw(time, site_id, recalc=recalc) soup = BeautifulSoup(raw_data, 'html.parser') tabular_data = StringIO(soup.find_all('pre')[0].contents[0]) col_names = ['pressure', 'height', 'temperature', 'dewpoint', 'direction', 'speed'] @@ -129,7 +137,7 @@ def _get_data(self, time, site_id): return df - def _get_data_raw(self, time, site_id): + def _get_data_raw(self, time, site_id, recalc=False): """Download data from the University of Wyoming's upper air archive. Parameters @@ -138,6 +146,8 @@ def _get_data_raw(self, time, site_id): Date and time for which data should be downloaded site_id : str Site id for which data should be downloaded + recalc : bool + Returns recalculated data if True. Defaults to False. Returns ------- @@ -148,6 +158,8 @@ def _get_data_raw(self, time, site_id): path = ('?region=naconf&TYPE=TEXT%3ALIST' f'&YEAR={time:%Y}&MONTH={time:%m}&FROM={time:%d%H}&TO={time:%d%H}' f'&STNM={site_id}') + if recalc: + path += '&REPLOT=1' resp = self.get_path(path) # See if the return is valid, but has no data From 39deb660cbbd51ddc254e71cd124eb207d193a82 Mon Sep 17 00:00:00 2001 From: tmharty Date: Fri, 8 Sep 2023 17:11:32 -0700 Subject: [PATCH 2/2] Partial test of wyoming recalc This is a partial test of recalc=True in WyomingUpperAir.request_data. Since recalculation only has an appreciable effect based on the unpredictable state of the Wyoming cache, this test only checks if including the recal=True breaks the request. --- tests/fixtures/wyoming_sounding_recalculate | 105 ++++++++++++++++++++ tests/test_wyoming.py | 39 ++++++++ 2 files changed, 144 insertions(+) create mode 100644 tests/fixtures/wyoming_sounding_recalculate diff --git a/tests/fixtures/wyoming_sounding_recalculate b/tests/fixtures/wyoming_sounding_recalculate new file mode 100644 index 000000000..54c8b1256 --- /dev/null +++ b/tests/fixtures/wyoming_sounding_recalculate @@ -0,0 +1,105 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate, br, zstd + Connection: + - keep-alive + User-Agent: + - Siphon (0.9.post414+gc286cbba.d20241105) + method: GET + uri: http://weather.uwyo.edu/cgi-bin/sounding/?region=naconf&TYPE=TEXT%3ALIST&YEAR=1999&MONTH=05&FROM=0400&TO=0400&STNM=OUN&REPLOT=1 + response: + body: + string: "\nUniversity of Wyoming - Radiosonde Data\n\n

72357 OUN Norman Observations at 00Z 04 May 1999

\n
\n-----------------------------------------------------------------------------\n
+        \  PRES   HGHT   TEMP   DWPT   RELH   MIXR   DRCT   SKNT   THTA   THTE   THTV\n
+        \   hPa     m      C      C      %    g/kg    deg   knot     K      K      K
+        \n-----------------------------------------------------------------------------\n
+        1000.0     -7                                                               \n
+        \ 959.0    345   22.2   19.0     82  14.64    160     18  298.9  341.8  301.5\n
+        \ 931.3    610   20.2   17.5     84  13.66    165     40  299.4  339.5  301.9\n
+        \ 925.0    671   19.8   17.1     84  13.44    165     38  299.6  339.0  302.0\n
+        \ 899.3    914   18.4   16.9     91  13.63    175     39  300.5  340.7  303.0\n
+        \ 892.0    984   18.0   16.8     93  13.68    178     39  300.8  341.2  303.3\n
+        \ 867.9   1219   17.4   14.3     82  11.99    190     38  302.6  338.3  304.8\n
+        \ 850.0   1397   17.0   12.5     75  10.82    195     38  303.9  336.5  305.9\n
+        \ 814.0   1766   15.4    5.4     51   6.95    204     37  306.0  327.4  307.3\n
+        \ 807.9   1829   15.4    1.2     38   5.19    205     37  306.7  323.0  307.7\n
+        \ 790.0   2019   15.6  -11.4     14   2.03    211     40  308.9  315.6  309.2\n
+        \ 779.2   2134   14.6  -11.2     16   2.09    215     42  309.0  315.9  309.4\n
+        \ 751.3   2438   12.0  -10.8     19   2.24    215     42  309.5  316.9  309.9\n
+        \ 724.3   2743    9.4  -10.4     24   2.40    220     40  309.9  317.8  310.3\n
+        \ 700.0   3028    7.0  -10.0     29   2.57    220     37  310.2  318.6  310.7\n
+        \ 655.0   3568    2.2  -16.8     23   1.58    220     37  310.7  316.1  311.0\n
+        \ 647.5   3658    1.4  -16.7     25   1.61    220     37  310.8  316.3  311.1\n
+        \ 599.4   4267   -4.2  -15.8     40   1.87    225     39  311.4  317.6  311.7\n
+        \ 554.7   4877   -9.7  -15.0     65   2.16    220     38  311.8  319.0  312.2\n
+        \ 550.0   4943  -10.3  -14.9     69   2.20    220     38  311.8  319.1  312.2\n
+        \ 500.0   5670  -14.9  -18.9     72   1.73    225     36  314.8  320.7  315.1\n
+        \ 472.5   6096  -17.6  -21.0     75   1.52    235     43  316.6  321.8  316.9\n
+        \ 449.0   6480  -20.1  -22.9     78   1.36    235     42  318.1  322.9  318.4\n
+        \ 400.0   7330  -26.7  -30.1     73   0.79    235     40  320.2  323.1  320.4\n
+        \ 383.7   7620  -29.1  -32.6     72   0.65    235     40  320.8  323.2  321.0\n
+        \ 336.4   8534  -36.8  -40.6     68   0.33    235     47  322.6  323.9  322.7\n
+        \ 321.9   8839  -39.4  -43.3     66   0.26    240     36  323.2  324.2  323.2\n
+        \ 308.1   9144  -41.9  -46.0     65   0.20    240     37  323.7  324.5  323.7\n
+        \ 300.0   9330  -43.5  -47.6     64   0.17    245     38  323.9  324.6  324.0\n
+        \ 269.0  10049  -49.0  -53.2     62   0.10    245     73  326.2  326.6  326.2\n
+        \ 268.6  10058  -49.1  -53.2     62   0.10    250     70  326.2  326.6  326.2\n
+        \ 251.0  10505  -52.5  -56.7     60   0.07                327.5  327.8  327.5\n

Station + information and sounding indices

\n                         Station
+        identifier: OUN\n                             Station number: 72357\n                           Observation
+        time: 990504/0000\n                           Station latitude: 35.18\n                          Station
+        longitude: -97.44\n                          Station elevation: 345.0\n                            Showalter
+        index: -6.51\n                               Lifted index: -8.04\n    LIFT
+        computed using virtual temperature: -8.51\n                                SWEAT
+        index: 555.49\n                                    K index: 27.40\n                         Cross
+        totals index: 27.40\n                      Vertical totals index: 31.90\n
+        \                       Totals totals index: 59.30\n      Convective Available
+        Potential Energy: 2252.27\n             CAPE using virtual temperature: 2381.91\n
+        \                     Convective Inhibition: -120.55\n             CINS using
+        virtual temperature: -64.57\n                   Level of Free Convection:
+        712.90\n             LFCT using virtual temperature: 745.24\n                     Bulk
+        Richardson Number: 24.15\n          Bulk Richardson Number using CAPV: 25.54\n
+        \ Temp [K] of the Lifted Condensation Level: 290.20\nPres [hPa] of the Lifted
+        Condensation Level: 895.98\n   Equivalent potential temp [K] of the LCL: 340.04\n
+        \    Mean mixed layer potential temperature: 299.46\n              Mean mixed
+        layer mixing ratio: 13.85\n              1000 hPa to 500 hPa thickness: 5677.00\nPrecipitable
+        water [mm] for entire sounding: 26.86\n
\n

Description of the \ndata columns\nor sounding + indices.\n\n

\n

\n\n\n
\n
\nInterested in graduate studies in atmospheric science?\nCheck + out our program at the\nUniversity + of Wyoming\n\n
\nQuestions about the + weather data provided by this site can be\naddressed to \nLarry + Oolman (ldoolman@uwyo.edu)\n
\n\n\n\n" + headers: + Accept-Ranges: + - bytes + Connection: + - Keep-Alive + Content-Length: + - '5338' + Content-Type: + - text/html; charset=UTF-8 + Date: + - Thu, 12 Dec 2024 20:44:03 GMT + ETag: + - W/"14da-62918c67d52c4" + Keep-Alive: + - timeout=5, max=100 + Last-Modified: + - Thu, 12 Dec 2024 20:44:06 GMT + Server: + - Apache + status: + code: 200 + message: OK +version: 1 diff --git a/tests/test_wyoming.py b/tests/test_wyoming.py index 1fa4d96d7..95df562c0 100644 --- a/tests/test_wyoming.py +++ b/tests/test_wyoming.py @@ -53,6 +53,45 @@ def test_wyoming(): assert df.units['time'] is None +@recorder.use_cassette('wyoming_sounding_recalculate') +def test_wyoming_recalculate(): + """Test that recalculation request returns the same data.""" + df = WyomingUpperAir.request_data( + datetime(1999, 5, 4, 0), 'OUN', recalc=True) + + assert df['time'][0] == datetime(1999, 5, 4, 0) + assert df['station'][0] == 'OUN' + assert df['station_number'][0] == 72357 + assert df['latitude'][0] == 35.18 + assert df['longitude'][0] == -97.44 + assert df['elevation'][0] == 345.0 + + assert_almost_equal(df['pressure'][5], 867.9, 2) + assert_almost_equal(df['height'][5], 1219., 2) + assert_almost_equal(df['height'][30], 10505., 2) + assert_almost_equal(df['temperature'][5], 17.4, 2) + assert_almost_equal(df['dewpoint'][5], 14.3, 2) + assert_almost_equal(df['u_wind'][5], 6.60, 2) + assert_almost_equal(df['v_wind'][5], 37.42, 2) + assert_almost_equal(df['speed'][5], 38.0, 1) + assert_almost_equal(df['direction'][5], 190.0, 1) + + assert df.units['pressure'] == 'hPa' + assert df.units['height'] == 'meter' + assert df.units['temperature'] == 'degC' + assert df.units['dewpoint'] == 'degC' + assert df.units['u_wind'] == 'knot' + assert df.units['v_wind'] == 'knot' + assert df.units['speed'] == 'knot' + assert df.units['direction'] == 'degrees' + assert df.units['latitude'] == 'degrees' + assert df.units['longitude'] == 'degrees' + assert df.units['elevation'] == 'meter' + assert df.units['station'] is None + assert df.units['station_number'] is None + assert df.units['time'] is None + + @recorder.use_cassette('wyoming_sounding_no_station') def test_wyoming_no_station(): """Test that we handle stations with no ID from the Wyoming archive."""