Skip to content

Commit

Permalink
Merge pull request #283 from zbruick/time_warning
Browse files Browse the repository at this point in the history
Add warning if time range is invalid
  • Loading branch information
dopplershift authored Oct 11, 2019
2 parents c0def67 + 2c1b770 commit dbee352
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
'python': ('https://docs.python.org/3/', None),
'numpy': ('https://docs.scipy.org/doc/numpy/', None),
'matplotlib': ('https://matplotlib.org', None),
'requests': ('https://2.python-requests.org/en/master/', None),
'requests': ('https://requests.kennethreitz.org/en/master/', None),
}

# Add any paths that contain templates here, relative to this directory.
Expand Down
2 changes: 1 addition & 1 deletion examples/ndbc/buoy_met_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
####################################################
# Get a pandas data frame of all of the observations, meteorological data is the default
# observation set to query.
df = NDBC.realtime_observations('41002')
df = NDBC.realtime_observations('46006')
df.head()

####################################################
Expand Down
6 changes: 5 additions & 1 deletion siphon/catalog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2013-2017 Siphon Contributors.
# Copyright (c) 2013-2019 Siphon Contributors.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""
Expand All @@ -11,6 +11,7 @@
from datetime import datetime
import logging
import re
import warnings
import xml.etree.ElementTree as ET # noqa:N814
try:
from urlparse import urljoin, urlparse
Expand Down Expand Up @@ -146,6 +147,9 @@ def filter_time_range(self, start, end, regex=None, strptime=None):
All values corresponding to times within the specified range
"""
if start > end:
warnings.warn('The provided start time comes after the end time. No data will '
'be returned.', UserWarning)
return [item[-1] for item in self._get_datasets_with_times(regex, strptime)
if start <= item[0] <= end]

Expand Down
6 changes: 5 additions & 1 deletion siphon/http_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2013-2015 Siphon Contributors.
# Copyright (c) 2013-2019 Siphon Contributors.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Utility code to support making requests using HTTP."""
Expand All @@ -8,6 +8,7 @@
from io import BytesIO
from itertools import chain
import posixpath
import warnings
try:
from urllib.parse import urlencode, urljoin # noqa
except ImportError:
Expand Down Expand Up @@ -317,6 +318,9 @@ def time_range(self, start, end):
Returns self for chaining calls
"""
if start > end:
warnings.warn('The provided start time comes after the end time. No data will '
'be returned.', UserWarning)
self._set_query(self.time_query, time_start=self._format_time(start),
time_end=self._format_time(end))
return self
Expand Down
14 changes: 13 additions & 1 deletion siphon/tests/test_catalog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2013-2017 Siphon Contributors.
# Copyright (c) 2013-2019 Siphon Contributors.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Test the catalog access API."""
Expand Down Expand Up @@ -232,6 +232,18 @@ def test_datasets_time_range():
'NAM_CONUS_20km_noaaport_20150529_0000.grib1']


@recorder.use_cassette('top_level_20km_rap_catalog')
def test_datasets_bad_time_range():
"""Test warning message for bad time range."""
with pytest.warns(UserWarning):
url = ('http://thredds.ucar.edu/thredds/catalog/grib/NCEP/NAM/'
'CONUS_20km/noaaport/catalog.xml')
cat = TDSCatalog(url)
in_range = cat.catalog_refs.filter_time_range(datetime(2015, 5, 29, 0),
datetime(2015, 5, 28, 0))
assert in_range == []


@recorder.use_cassette('top_level_20km_rap_catalog')
def test_datasets_time_range_regex():
"""Test getting datasets by time range using filenames, with manual regex."""
Expand Down
10 changes: 9 additions & 1 deletion siphon/tests/test_http_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2013-2015 Siphon Contributors.
# Copyright (c) 2013-2019 Siphon Contributors.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Test Siphon's base HTTP helper functionality."""
Expand Down Expand Up @@ -102,6 +102,14 @@ def test_data_query_time_format():
assert query == 'time=2015-06-15T12%3A00%3A00'


def test_data_query_bad_time_range():
"""Test that time queries are properly formatted."""
with pytest.warns(UserWarning):
dt = datetime(2015, 6, 15, 12, 0, 0)
dt2 = datetime(2015, 6, 14, 12, 0, 0)
DataQuery().time_range(dt, dt2)


def test_data_query_spatial_reset():
"""Test that spatial queries reset each other."""
dr = DataQuery().lonlat_box(1, 2, 3, 4).lonlat_point(-1, -2)
Expand Down

0 comments on commit dbee352

Please sign in to comment.