Repository: Unidata/siphon (python)
Issue: Improve IGRA2 date range error
When there are no data available for a particular date range from IGRA2, it would be very helpful to the users to include the range of data that is actually found in the file. This will require actually calculating the min/max date, but we are already scanning through all of them so it shouldn't be too much more effort.
Pull request description: Improve igra2 date error message
<!--
Thanks for contributing a pull request! Please ensure you have taken a look at
the contribution guidelines: https://github.com/Unidata/siphon/blob/main/CONTRIBUTING.md
-->

#### Description Of Changes
We're already scanning the whole archive, just note the max/min date encountered.

<!--
Feel free to remove check-list items aren't relevant to your change

Please use keywords (e.g., Fixes, Closes) to create link to the issues or pull
requests you resolved, so that they will automatically be closed when your pull
request is merged. See https://github.com/blog/1506-closing-issues-via-pull-requests
-->

#### Checklist

- [x] Closes #316
- [x] Tests added

Patch:
diff --git a/examples/upperair/Wyoming_Request.py b/examples/upperair/Wyoming_Request.py
index 323bd7dc..5d5a43ea 100644
--- a/examples/upperair/Wyoming_Request.py
+++ b/examples/upperair/Wyoming_Request.py
@@ -17,8 +17,8 @@ from siphon.simplewebservice.wyoming import WyomingUpperAir
 
 ####################################################
 # Create a datetime object for the sounding and string of the station identifier.
-date = datetime(2024, 9, 27, 0)
-station = 'GSO'
+date = datetime(2017, 9, 10, 6)
+station = 'MFL'
 
 ####################################################
 # Make the request (a pandas dataframe is returned).
diff --git a/src/siphon/simplewebservice/igra2.py b/src/siphon/simplewebservice/igra2.py
index e3dec715..0e61636d 100644
--- a/src/siphon/simplewebservice/igra2.py
+++ b/src/siphon/simplewebservice/igra2.py
@@ -130,6 +130,9 @@ class IGRAUpperAir(HTTPEndPoint):
         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 @@ class IGRAUpperAir(HTTPEndPoint):
                 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)


Test patch:
diff --git a/tests/test_igra2.py b/tests/test_igra2.py
index 2fd0ef34..9535f67b 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)