Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve igra2 date error message #855

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading