From 33c23869df5ffba608a20496de846836a922e5ae Mon Sep 17 00:00:00 2001 From: Ryan May Date: Wed, 11 Dec 2024 18:02:40 -0700 Subject: [PATCH] ENH: Improve igra2 date error message (Fixes #316) We're already scanning the whole archive, just note the max/min date encountered. --- src/siphon/simplewebservice/igra2.py | 15 +++++++++------ tests/test_igra2.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/siphon/simplewebservice/igra2.py b/src/siphon/simplewebservice/igra2.py index e3dec7154..0e61636d5 100644 --- a/src/siphon/simplewebservice/igra2.py +++ b/src/siphon/simplewebservice/igra2.py @@ -130,6 +130,9 @@ def _select_date_range(self, lines): num_lev = [] dates = [] + min_date = None + max_date = None + # Get indices of headers, and make a list of dates and num_lev for idx, line in enumerate(lines): if line[0] == '#': @@ -141,18 +144,18 @@ def _select_date_range(self, lines): except ValueError: date = datetime.datetime(year, month, day) + min_date = min(date, min_date) if min_date else date + max_date = min(date, max_date) if max_date else date + # Check date if self.begin_date <= date <= self.end_date: headers.append(idx) num_lev.append(int(line[32:36])) dates.append(date) - if date > self.end_date: - break - if len(dates) == 0: - # Break if no matched dates. - # Could improve this later by showing the date range for the station. - raise ValueError('No dates match selection.') + if not dates: + raise ValueError('No dates match selection. This selection has data from ' + f'{min_date} to {max_date}.') # Compress body of data into a string begin_idx = min(headers) diff --git a/tests/test_igra2.py b/tests/test_igra2.py index 2fd0ef344..9535f67b3 100644 --- a/tests/test_igra2.py +++ b/tests/test_igra2.py @@ -143,3 +143,13 @@ def test_igra2_nonexistent(): IGRAUpperAir.request_data(datetime.now(), 'NOSUCHSTATION') assert 'No data' in str(err.value) + + +@recorder.use_cassette('igra2_sounding', + before_record_response=subset_date(datetime(2010, 6, 1))) +def test_igra2_bad_time_range(): + """Test that we are properly parsing data from the IGRA2 archive.""" + with pytest.raises(ValueError) as err: + IGRAUpperAir.request_data(datetime(2012, 6, 1, 12), 'USM00070026') + + assert '2010-06-01 00:00:00' in str(err.value)