Skip to content

Commit af46eea

Browse files
committed
Allow users to specify query parameters for the collections API when calling the get_reference_table function.
1 parent dc9b614 commit af46eea

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

dataretrieval/waterdata/api.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def get_continuous(
234234
with the continuous endpoint. If the "time" input is left blank, the service
235235
will return the most recent year of measurements. Users may request no more
236236
than three years of data with each function call.
237-
237+
238238
Continuous data are collected at a high frequency, typically 15-minute
239239
intervals. Depending on the specific monitoring location, the data may be
240240
transmitted automatically via telemetry and be available on WDFN within
@@ -243,7 +243,7 @@ def get_continuous(
243243
transmit data. Continuous data are described by parameter name and
244244
parameter code (pcode). These data might also be referred to as
245245
"instantaneous values" or "IV".
246-
246+
247247
Parameters
248248
----------
249249
monitoring_location_id : string or list of strings, optional
@@ -482,11 +482,11 @@ def get_monitoring_locations(
482482
is located.
483483
county_code : string or list of strings, optional
484484
The code for the county or county equivalent (parish, borough, etc.) in which
485-
the monitoring location is located. A `list of codes
485+
the monitoring location is located. A `list of codes
486486
<https://help.waterdata.usgs.gov/code/county_query?fmt=html>`_ is available.
487487
county_name : string or list of strings, optional
488488
The name of the county or county equivalent (parish, borough, etc.) in which
489-
the monitoring location is located. A `list of codes
489+
the monitoring location is located. A `list of codes
490490
<https://help.waterdata.usgs.gov/code/county_query?fmt=html>`_ is available.
491491
minor_civil_division_code : string or list of strings, optional
492492
Codes for primary governmental or administrative divisions of the county or
@@ -1421,11 +1421,12 @@ def get_field_measurements(
14211421
def get_reference_table(
14221422
collection: str,
14231423
limit: Optional[int] = None,
1424+
query: Optional[dict] = {},
14241425
) -> Tuple[pd.DataFrame, BaseMetadata]:
14251426
"""Get metadata reference tables for the USGS Water Data API.
14261427
14271428
Reference tables provide the range of allowable values for parameter
1428-
arguments in the waterdata module.
1429+
arguments in the waterdata module.
14291430
14301431
Parameters
14311432
----------
@@ -1442,7 +1443,10 @@ def get_reference_table(
14421443
allowable limit is 50000. It may be beneficial to set this number lower
14431444
if your internet connection is spotty. The default (None) will set the
14441445
limit to the maximum allowable limit for the service.
1445-
1446+
query: dictionary, optional
1447+
The optional args parameter can be used to pass a dictionary of
1448+
query parameters to the collection API call.
1449+
14461450
Returns
14471451
-------
14481452
df : ``pandas.DataFrame`` or ``geopandas.GeoDataFrame``
@@ -1454,7 +1458,7 @@ def get_reference_table(
14541458
medium code values).
14551459
md: :obj:`dataretrieval.utils.Metadata`
14561460
A custom metadata object including the URL request and query time.
1457-
1461+
14581462
Examples
14591463
--------
14601464
.. code::
@@ -1463,24 +1467,30 @@ def get_reference_table(
14631467
>>> ref, md = dataretrieval.waterdata.get_reference_table(
14641468
... collection="parameter-codes"
14651469
... )
1470+
1471+
>>> # Get table of selected USGS parameter codes
1472+
>>> ref, md = dataretrieval.waterdata.get_reference_table(
1473+
... collection="parameter-codes"
1474+
... query={'id': '00001,00002'}
1475+
... )
14661476
"""
14671477
valid_code_services = get_args(METADATA_COLLECTIONS)
14681478
if collection not in valid_code_services:
14691479
raise ValueError(
14701480
f"Invalid code service: '{collection}'. "
14711481
f"Valid options are: {valid_code_services}."
14721482
)
1473-
1483+
14741484
# Give ID column the collection name with underscores
14751485
if collection.endswith("s") and collection != "counties":
14761486
output_id = f"{collection[:-1].replace('-', '_')}"
14771487
elif collection == "counties":
14781488
output_id = "county"
14791489
else:
14801490
output_id = f"{collection.replace('-', '_')}"
1481-
1491+
14821492
return get_ogc_data(
1483-
args={},
1493+
args=query,
14841494
output_id=output_id,
14851495
service=collection
14861496
)

tests/waterdata_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ def test_get_reference_table():
253253
assert hasattr(md, 'url')
254254
assert hasattr(md, 'query_time')
255255

256+
def test_get_reference_table_with_query():
257+
query = {"id": "AK001,AK008"}
258+
df, md = get_reference_table("agency-codes", query=query)
259+
assert "agency_code" in df.columns
260+
assert df.shape[0] == 2
261+
assert hasattr(md, 'url')
262+
assert hasattr(md, 'query_time')
263+
256264
def test_get_reference_table_wrong_name():
257265
with pytest.raises(ValueError):
258266
get_reference_table("agency-cod")

0 commit comments

Comments
 (0)