Skip to content

Commit d3b4430

Browse files
committed
edit samples request to handle lists
1 parent 7c2cdeb commit d3b4430

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

dataretrieval/samples.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
import warnings
1111
import requests
1212
from requests.models import PreparedRequest
13+
from typing import List, Optional, Tuple, Union
1314
from io import StringIO
1415
from typing import TYPE_CHECKING
1516

1617
import pandas as pd
1718

18-
from utils import BaseMetadata, query
19+
from utils import BaseMetadata, query, to_str
1920

2021
if TYPE_CHECKING:
2122
from pandas import DataFrame
@@ -89,9 +90,8 @@ def get_USGS_samples(
8990
pointLocationLongitude=None,
9091
pointLocationWithinMiles=None,
9192
projectIdentifier=None,
92-
recordIdentifierUserSupplied=None,
93-
mimeType="text/csv"
94-
):
93+
recordIdentifierUserSupplied=None
94+
) -> Tuple[pd.DataFrame, BaseMetadata]:
9595
"""Search Samples database for USGS water quality data.
9696
This is a wrapper function for the Samples database API. All potential
9797
filters are provided as arguments to the function, but please do not
@@ -150,7 +150,7 @@ def get_USGS_samples(
150150
Example: "Suspended Sediment Discharge"
151151
characteristicUserSupplied : string or list of strings, optional
152152
A user supplied characteristic name describing one or more results.
153-
boundingBox: string of four floats, optional
153+
boundingBox: list of four floats, optional
154154
Filters on the the associated monitoring location's point location
155155
by checking if it is located within the specified geographic area.
156156
The logic is inclusive, i.e. it will include locations that overlap
@@ -162,7 +162,7 @@ def get_USGS_samples(
162162
- Southern-most latitude
163163
- Eastern-most longitude
164164
- Northern-most longitude
165-
Example: '-92.8,44.2,-88.9,46.0'
165+
Example: [-92.8,44.2,-88.9,46.0]
166166
countryFips : string or list of strings, optional
167167
Example: "US" (United States)
168168
stateFips : string or list of strings, optional
@@ -224,7 +224,7 @@ def get_USGS_samples(
224224
225225
>>> # Get PFAS results within a bounding box
226226
>>> df, md = dataretrieval.samples.get_USGS_samples(
227-
... boundingBox="-90.2,42.6,-88.7,43.2",
227+
... boundingBox=[-90.2,42.6,-88.7,43.2],
228228
... characteristicGroup="Organics, PFAS"
229229
... )
230230
@@ -242,9 +242,16 @@ def get_USGS_samples(
242242
# Get all not-None inputs
243243
params = {key: value for key, value in locals().items() if value is not None and key not in ['service', 'profile', 'ssl_check']}
244244

245-
if len(params) == 1 and 'mimeType' in params:
245+
if len(params) == 0:
246246
raise TypeError("No filter parameters provided. You must add at least "
247247
"one filter parameter beyond a service, profile, and format argument.")
248+
249+
# Add in file format (could be an input, too, though not sure about other formats)
250+
params['mimeType'] = "text/csv"
251+
252+
# Convert bounding box to a string
253+
if "boundingBox" in params:
254+
params['boundingBox'] = to_str(params['boundingBox'])
248255

249256
# Build URL with service and profile
250257
url = BASE_URL + service + "/" + profile
@@ -255,11 +262,12 @@ def get_USGS_samples(
255262
print(f"Request: {req.url}")
256263

257264
# Make a GET request with the filtered parameters
258-
response = query(url, params, ssl_check=ssl_check)
265+
response = requests.get(url, params=params, verify=ssl_check)
266+
267+
response.raise_for_status
259268

260269
df = pd.read_csv(StringIO(response.text), delimiter=",")
261270

262271
#return response
263272

264273
return df, BaseMetadata(response)
265-

0 commit comments

Comments
 (0)