Skip to content

Commit 8f89fc5

Browse files
committed
remove code from samples module, etc.
1 parent e5d4da4 commit 8f89fc5

3 files changed

Lines changed: 17 additions & 119 deletions

File tree

dataretrieval/samples.py

Lines changed: 12 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -11,99 +11,18 @@
1111
from typing import TYPE_CHECKING, Literal, get_args
1212

1313
import pandas as pd
14+
import warnings
1415
import requests
1516
from requests.models import PreparedRequest
1617

1718
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
1820

1921
if TYPE_CHECKING:
2022
from typing import Optional, Tuple, Union
2123

2224
from pandas import DataFrame
2325

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-
10726
def get_usgs_samples(
10827
ssl_check: bool = True,
10928
service: _SERVICES = "results",
@@ -272,26 +191,33 @@ def get_usgs_samples(
272191
.. code::
273192
274193
>>> # Get PFAS results within a bounding box
275-
>>> df, md = dataretrieval.samples.get_samples(
194+
>>> df, md = dataretrieval.samples.get_usgs_samples(
276195
... boundingBox=[-90.2,42.6,-88.7,43.2],
277196
... characteristicGroup="Organics, PFAS"
278197
... )
279198
280199
>>> # 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(
282201
... service="activities",
283202
... profile="sampact",
284203
... activityStartDateLower="2023-10-01",
285204
... activityStartDateUpper="2024-01-01",
286205
... stateFips="US:51")
287206
288207
>>> # Get all pH samples for two sites in Utah
289-
>>> df, md = dataretrieval.samples.get_samples(
208+
>>> df, md = dataretrieval.samples.get_usgs_samples(
290209
... monitoringLocationIdentifier=['USGS-393147111462301', 'USGS-393343111454101'],
291210
... usgsPCode='00400')
292211
293212
"""
294213

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+
295221
_check_profiles(service, profile)
296222

297223
params = {
@@ -320,32 +246,4 @@ def get_usgs_samples(
320246

321247
return df, BaseMetadata(response)
322248

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-
)
351249

dataretrieval/waterdata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,21 +271,21 @@ def get_samples(
271271
.. code::
272272
273273
>>> # Get PFAS results within a bounding box
274-
>>> df, md = dataretrieval.samples.get_samples(
274+
>>> df, md = dataretrieval.waterdata.get_samples(
275275
... boundingBox=[-90.2,42.6,-88.7,43.2],
276276
... characteristicGroup="Organics, PFAS"
277277
... )
278278
279279
>>> # Get all activities for the Commonwealth of Virginia over a date range
280-
>>> df, md = dataretrieval.samples.get_samples(
280+
>>> df, md = dataretrieval.waterdata.get_samples(
281281
... service="activities",
282282
... profile="sampact",
283283
... activityStartDateLower="2023-10-01",
284284
... activityStartDateUpper="2024-01-01",
285285
... stateFips="US:51")
286286
287287
>>> # Get all pH samples for two sites in Utah
288-
>>> df, md = dataretrieval.samples.get_samples(
288+
>>> df, md = dataretrieval.waterdata.get_samples(
289289
... monitoringLocationIdentifier=['USGS-393147111462301', 'USGS-393343111454101'],
290290
... usgsPCode='00400')
291291

demos/hydroshare/USGS_dataretrieval_WaterSamples_Examples.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@
389389
],
390390
"metadata": {
391391
"kernelspec": {
392-
"display_name": "dr-test",
392+
"display_name": "hyswap-dev-environment",
393393
"language": "python",
394394
"name": "python3"
395395
},
@@ -403,7 +403,7 @@
403403
"name": "python",
404404
"nbconvert_exporter": "python",
405405
"pygments_lexer": "ipython3",
406-
"version": "3.11.12"
406+
"version": "3.12.7"
407407
}
408408
},
409409
"nbformat": 4,

0 commit comments

Comments
 (0)