Skip to content

Commit 2a837c9

Browse files
committed
add reference tables function
1 parent 7298d3b commit 2a837c9

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

dataretrieval/waterdata/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
get_latest_continuous,
2020
get_latest_daily,
2121
get_monitoring_locations,
22+
get_reference_table,
2223
get_samples,
2324
get_time_series_metadata,
2425
)
@@ -37,6 +38,7 @@
3738
"get_latest_continuous",
3839
"get_latest_daily",
3940
"get_monitoring_locations",
41+
"get_reference_table",
4042
"get_samples",
4143
"get_time_series_metadata",
4244
"_check_profiles",

dataretrieval/waterdata/api.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
from dataretrieval.utils import BaseMetadata, to_str
1717
from dataretrieval.waterdata.types import (
1818
CODE_SERVICES,
19+
METADATA_COLLECTIONS,
1920
PROFILE_LOOKUP,
2021
PROFILES,
2122
SERVICES,
2223
)
23-
from dataretrieval.waterdata.utils import SAMPLES_URL, get_ogc_data
24+
from dataretrieval.waterdata.utils import (
25+
SAMPLES_URL,
26+
get_ogc_data,
27+
_construct_api_requests,
28+
_walk_pages
29+
)
2430

2531
# Set up logger for this module
2632
logger = logging.getLogger(__name__)
@@ -1388,6 +1394,58 @@ def get_field_measurements(
13881394

13891395
return get_ogc_data(args, output_id, service)
13901396

1397+
def get_reference_table(
1398+
collection: str,
1399+
limit: Optional[int] = None,
1400+
) -> Tuple[pd.DataFrame, BaseMetadata]:
1401+
"""Get metadata reference tables for the USGS Water Data API.
1402+
1403+
Reference tables provide the range of allowable values for parameter
1404+
arguments in the waterdata module.
1405+
1406+
Parameters
1407+
----------
1408+
collection : string
1409+
One of the following options: "agency-codes", "altitude-datums",
1410+
"aquifer-codes", "aquifer-types", "coordinate-accuracy-codes",
1411+
"coordinate-datum-codes", "coordinate-method-codes", "counties",
1412+
"hydrologic-unit-codes", "medium-codes", "national-aquifer-codes",
1413+
"parameter-codes", "reliability-codes", "site-types", "states",
1414+
"statistic-codes", "topographic-codes", "time-zone-codes"
1415+
limit : numeric, optional
1416+
The optional limit parameter is used to control the subset of the
1417+
selected features that should be returned in each page. The maximum
1418+
allowable limit is 50000. It may be beneficial to set this number lower
1419+
if your internet connection is spotty. The default (None) will set the
1420+
limit to the maximum allowable limit for the service.
1421+
"""
1422+
valid_code_services = get_args(METADATA_COLLECTIONS)
1423+
if collection not in valid_code_services:
1424+
raise ValueError(
1425+
f"Invalid code service: '{collection}'. "
1426+
f"Valid options are: {valid_code_services}."
1427+
)
1428+
1429+
req = _construct_api_requests(
1430+
service=collection,
1431+
limit=limit,
1432+
skip_geometry=True,
1433+
)
1434+
# Run API request and iterate through pages if needed
1435+
return_list, response = _walk_pages(
1436+
geopd=False, req=req
1437+
)
1438+
1439+
# Give ID column a more meaningful name
1440+
if collection.endswith("s"):
1441+
return_list = return_list.rename(columns={"id": f"{collection[:-1].replace("-", "_")}_id"})
1442+
else:
1443+
return_list = return_list.rename(columns={"id": f"{collection.replace("-", "_")}_id"})
1444+
1445+
# Create metadata object from response
1446+
metadata = BaseMetadata(response)
1447+
return return_list, metadata
1448+
13911449

13921450
def get_codes(code_service: CODE_SERVICES) -> pd.DataFrame:
13931451
"""Return codes from a Samples code service.

dataretrieval/waterdata/types.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,27 @@
1111
"states",
1212
]
1313

14+
METADATA_COLLECTIONS = Literal[
15+
"agency-codes",
16+
"altitude-datums",
17+
"aquifer-codes",
18+
"aquifer-types",
19+
"coordinate-accuracy-codes",
20+
"coordinate-datum-codes",
21+
"coordinate-method-codes",
22+
"counties",
23+
"hydrologic-unit-codes",
24+
"medium-codes",
25+
"national-aquifer-codes",
26+
"parameter-codes",
27+
"reliability-codes",
28+
"site-types",
29+
"states",
30+
"statistic-codes",
31+
"topographic-codes",
32+
"time-zone-codes",
33+
]
34+
1435
SERVICES = Literal[
1536
"activities",
1637
"locations",

0 commit comments

Comments
 (0)