Skip to content

Commit

Permalink
Merge pull request #855 from dopplershift/igra2-error-message
Browse files Browse the repository at this point in the history
Improve igra2 date error message
  • Loading branch information
lesserwhirls authored Dec 12, 2024
2 parents e820e0f + 33c2386 commit 2200fb1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/siphon/simplewebservice/igra2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] == '#':
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_igra2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2200fb1

Please sign in to comment.