|
16 | 16 | from dataretrieval.utils import BaseMetadata, to_str |
17 | 17 | from dataretrieval.waterdata.types import ( |
18 | 18 | CODE_SERVICES, |
| 19 | + METADATA_COLLECTIONS, |
19 | 20 | PROFILE_LOOKUP, |
20 | 21 | PROFILES, |
21 | 22 | SERVICES, |
22 | 23 | ) |
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 | +) |
24 | 30 |
|
25 | 31 | # Set up logger for this module |
26 | 32 | logger = logging.getLogger(__name__) |
@@ -1388,6 +1394,58 @@ def get_field_measurements( |
1388 | 1394 |
|
1389 | 1395 | return get_ogc_data(args, output_id, service) |
1390 | 1396 |
|
| 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 | + |
1391 | 1449 |
|
1392 | 1450 | def get_codes(code_service: CODE_SERVICES) -> pd.DataFrame: |
1393 | 1451 | """Return codes from a Samples code service. |
|
0 commit comments