|
11 | 11 | from typing import TYPE_CHECKING, Literal, get_args |
12 | 12 |
|
13 | 13 | import pandas as pd |
| 14 | +import warnings |
14 | 15 | import requests |
15 | 16 | from requests.models import PreparedRequest |
16 | 17 |
|
17 | 18 | from dataretrieval.utils import BaseMetadata, to_str |
| 19 | +from dataretrieval.waterdata import get_codes, get_args, _check_profiles, _BASE_URL, _CODE_SERVICES, _PROFILES, _SERVICES, _PROFILE_LOOKUP |
18 | 20 |
|
19 | 21 | if TYPE_CHECKING: |
20 | 22 | from typing import Optional, Tuple, Union |
21 | 23 |
|
22 | 24 | from pandas import DataFrame |
23 | 25 |
|
24 | | - |
25 | | -_BASE_URL = "https://api.waterdata.usgs.gov/samples-data" |
26 | | - |
27 | | -_CODE_SERVICES = Literal[ |
28 | | - "characteristicgroup", |
29 | | - "characteristics", |
30 | | - "counties", |
31 | | - "countries", |
32 | | - "observedproperty", |
33 | | - "samplemedia", |
34 | | - "sitetype", |
35 | | - "states", |
36 | | -] |
37 | | - |
38 | | - |
39 | | -_SERVICES = Literal["activities", "locations", "organizations", "projects", "results"] |
40 | | - |
41 | | -_PROFILES = Literal[ |
42 | | - "actgroup", |
43 | | - "actmetric", |
44 | | - "basicbio", |
45 | | - "basicphyschem", |
46 | | - "count", |
47 | | - "fullbio", |
48 | | - "fullphyschem", |
49 | | - "labsampleprep", |
50 | | - "narrow", |
51 | | - "organization", |
52 | | - "project", |
53 | | - "projectmonitoringlocationweight", |
54 | | - "resultdetectionquantitationlimit", |
55 | | - "sampact", |
56 | | - "site", |
57 | | -] |
58 | | - |
59 | | -_PROFILE_LOOKUP = { |
60 | | - "activities": ["sampact", "actmetric", "actgroup", "count"], |
61 | | - "locations": ["site", "count"], |
62 | | - "organizations": ["organization", "count"], |
63 | | - "projects": ["project", "projectmonitoringlocationweight"], |
64 | | - "results": [ |
65 | | - "fullphyschem", |
66 | | - "basicphyschem", |
67 | | - "fullbio", |
68 | | - "basicbio", |
69 | | - "narrow", |
70 | | - "resultdetectionquantitationlimit", |
71 | | - "labsampleprep", |
72 | | - "count", |
73 | | - ], |
74 | | -} |
75 | | - |
76 | | - |
77 | | -def get_codes(code_service: _CODE_SERVICES) -> DataFrame: |
78 | | - """Return codes from a Samples code service. |
79 | | - |
80 | | - Parameters |
81 | | - ---------- |
82 | | - code_service : string |
83 | | - One of the following options: "states", "counties", "countries" |
84 | | - "sitetype", "samplemedia", "characteristicgroup", "characteristics", |
85 | | - or "observedproperty" |
86 | | - """ |
87 | | - valid_code_services = get_args(_CODE_SERVICES) |
88 | | - if code_service not in valid_code_services: |
89 | | - raise ValueError( |
90 | | - f"Invalid code service: '{code_service}'. " |
91 | | - f"Valid options are: {valid_code_services}." |
92 | | - ) |
93 | | - |
94 | | - url = f"{_BASE_URL}/codeservice/{code_service}?mimeType=application%2Fjson" |
95 | | - |
96 | | - response = requests.get(url) |
97 | | - |
98 | | - response.raise_for_status() |
99 | | - |
100 | | - data_dict = json.loads(response.text) |
101 | | - data_list = data_dict['data'] |
102 | | - |
103 | | - df = pd.DataFrame(data_list) |
104 | | - |
105 | | - return df |
106 | | - |
107 | 26 | def get_usgs_samples( |
108 | 27 | ssl_check: bool = True, |
109 | 28 | service: _SERVICES = "results", |
@@ -272,26 +191,33 @@ def get_usgs_samples( |
272 | 191 | .. code:: |
273 | 192 |
|
274 | 193 | >>> # Get PFAS results within a bounding box |
275 | | - >>> df, md = dataretrieval.samples.get_samples( |
| 194 | + >>> df, md = dataretrieval.samples.get_usgs_samples( |
276 | 195 | ... boundingBox=[-90.2,42.6,-88.7,43.2], |
277 | 196 | ... characteristicGroup="Organics, PFAS" |
278 | 197 | ... ) |
279 | 198 |
|
280 | 199 | >>> # Get all activities for the Commonwealth of Virginia over a date range |
281 | | - >>> df, md = dataretrieval.samples.get_samples( |
| 200 | + >>> df, md = dataretrieval.samples.get_usgs_samples( |
282 | 201 | ... service="activities", |
283 | 202 | ... profile="sampact", |
284 | 203 | ... activityStartDateLower="2023-10-01", |
285 | 204 | ... activityStartDateUpper="2024-01-01", |
286 | 205 | ... stateFips="US:51") |
287 | 206 |
|
288 | 207 | >>> # Get all pH samples for two sites in Utah |
289 | | - >>> df, md = dataretrieval.samples.get_samples( |
| 208 | + >>> df, md = dataretrieval.samples.get_usgs_samples( |
290 | 209 | ... monitoringLocationIdentifier=['USGS-393147111462301', 'USGS-393343111454101'], |
291 | 210 | ... usgsPCode='00400') |
292 | 211 |
|
293 | 212 | """ |
294 | 213 |
|
| 214 | + warnings.warn("The `get_usgs_samples` function is moving from" \ |
| 215 | + " the samples module to the new waterdata module, where" \ |
| 216 | + " it will be called simply `get_samples`. All of the same" \ |
| 217 | + " functionality will be retained. The samples module is" \ |
| 218 | + " deprecated and will eventually be removed. Switch to the" \ |
| 219 | + " waterdata module as soon as possible, thank you.") |
| 220 | + |
295 | 221 | _check_profiles(service, profile) |
296 | 222 |
|
297 | 223 | params = { |
@@ -320,32 +246,4 @@ def get_usgs_samples( |
320 | 246 |
|
321 | 247 | return df, BaseMetadata(response) |
322 | 248 |
|
323 | | -def _check_profiles( |
324 | | - service: _SERVICES, |
325 | | - profile: _PROFILES, |
326 | | -) -> None: |
327 | | - """Check whether a service profile is valid. |
328 | | -
|
329 | | - Parameters |
330 | | - ---------- |
331 | | - service : string |
332 | | - One of the service names from the "services" list. |
333 | | - profile : string |
334 | | - One of the profile names from "results_profiles", |
335 | | - "locations_profiles", "activities_profiles", |
336 | | - "projects_profiles" or "organizations_profiles". |
337 | | - """ |
338 | | - valid_services = get_args(_SERVICES) |
339 | | - if service not in valid_services: |
340 | | - raise ValueError( |
341 | | - f"Invalid service: '{service}'. " |
342 | | - f"Valid options are: {valid_services}." |
343 | | - ) |
344 | | - |
345 | | - valid_profiles = _PROFILE_LOOKUP[service] |
346 | | - if profile not in valid_profiles: |
347 | | - raise ValueError( |
348 | | - f"Invalid profile: '{profile}' for service '{service}'. " |
349 | | - f"Valid options are: {valid_profiles}." |
350 | | - ) |
351 | 249 |
|
0 commit comments