Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 20 additions & 23 deletions dataretrieval/wqp.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def get_results(

df = pd.read_csv(StringIO(response.text), delimiter=",", low_memory=False)
df = _attach_datetime_columns(df)
return df, WQP_Metadata(response)
return df, WQP_Metadata(response, **kwargs)


def what_sites(
Expand Down Expand Up @@ -210,7 +210,7 @@ def what_sites(

df = pd.read_csv(StringIO(response.text), delimiter=",", low_memory=False)

return df, WQP_Metadata(response)
return df, WQP_Metadata(response, **kwargs)


def what_organizations(
Expand Down Expand Up @@ -261,7 +261,7 @@ def what_organizations(

df = pd.read_csv(StringIO(response.text), delimiter=",", low_memory=False)

return df, WQP_Metadata(response)
return df, WQP_Metadata(response, **kwargs)


def what_projects(ssl_check=True, legacy=True, **kwargs):
Expand Down Expand Up @@ -308,7 +308,7 @@ def what_projects(ssl_check=True, legacy=True, **kwargs):

df = pd.read_csv(StringIO(response.text), delimiter=",", low_memory=False)

return df, WQP_Metadata(response)
return df, WQP_Metadata(response, **kwargs)


def what_activities(
Expand Down Expand Up @@ -372,7 +372,7 @@ def what_activities(

df = pd.read_csv(StringIO(response.text), delimiter=",", low_memory=False)

return df, WQP_Metadata(response)
return df, WQP_Metadata(response, **kwargs)


def what_detection_limits(
Expand Down Expand Up @@ -430,7 +430,7 @@ def what_detection_limits(

df = pd.read_csv(StringIO(response.text), delimiter=",", low_memory=False)

return df, WQP_Metadata(response)
return df, WQP_Metadata(response, **kwargs)


def what_habitat_metrics(
Expand Down Expand Up @@ -481,7 +481,7 @@ def what_habitat_metrics(

df = pd.read_csv(StringIO(response.text), delimiter=",", low_memory=False)

return df, WQP_Metadata(response)
return df, WQP_Metadata(response, **kwargs)


def what_project_weights(ssl_check=True, legacy=True, **kwargs):
Expand Down Expand Up @@ -533,7 +533,7 @@ def what_project_weights(ssl_check=True, legacy=True, **kwargs):

df = pd.read_csv(StringIO(response.text), delimiter=",", low_memory=False)

return df, WQP_Metadata(response)
return df, WQP_Metadata(response, **kwargs)


def what_activity_metrics(ssl_check=True, legacy=True, **kwargs):
Expand Down Expand Up @@ -585,7 +585,7 @@ def what_activity_metrics(ssl_check=True, legacy=True, **kwargs):

df = pd.read_csv(StringIO(response.text), delimiter=",", low_memory=False)

return df, WQP_Metadata(response)
return df, WQP_Metadata(response, **kwargs)


def wqp_url(service):
Expand Down Expand Up @@ -627,10 +627,10 @@ class WQP_Metadata(BaseMetadata):
Response elapsed time
header : httpx.Headers
Response headers
comments : None
Metadata comments. WQP does not return comments.
site_info : tuple[pd.DataFrame, NWIS_Metadata] | None
Site information if the query included `sites`, `site` or `site_no`.
comment : None
WQP does not return comments.
site_info : tuple[pd.DataFrame, WQP_Metadata] | None
Site information (via ``what_sites``) if the query included a ``siteid``.
"""

def __init__(self, response, **parameters) -> None:
Expand Down Expand Up @@ -660,24 +660,21 @@ def __init__(self, response, **parameters) -> None:
def site_info(self) -> tuple[DataFrame, WQP_Metadata] | None:
"""Site information for the query.

Populated when the query included ``sites``, ``site`` or
``site_no`` (in that order of preference); ``None`` otherwise.
Populated (via :func:`dataretrieval.wqp.what_sites`) when the query
included a ``siteid`` (the WQP site identifier, e.g.
``"USGS-05586100"``); ``None`` otherwise.

Returns
-------
df : ``pandas.DataFrame``
Formatted requested data from calling ``wqp.what_sites``.
Site data returned by ``wqp.what_sites``.
md : :obj:`dataretrieval.wqp.WQP_Metadata`
A WQP_Metadata object.
"""
if "sites" in self._parameters:
return what_sites(sites=self._parameters["sites"])
elif "site" in self._parameters:
return what_sites(sites=self._parameters["site"])
elif "site_no" in self._parameters:
return what_sites(sites=self._parameters["site_no"])
else:
siteid = self._parameters.get("siteid")
if siteid is None:
return None
return what_sites(siteid=siteid)


def _check_kwargs(kwargs):
Expand Down
10 changes: 7 additions & 3 deletions tests/wqp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def test_get_results(httpx_mock):
assert md.header.get("mock_header") == "value"
assert md.comment is None
assert df["ActivityStartDateTime"].notna().all()
# Regression: the getter must thread the query kwargs into the metadata
# (it previously built WQP_Metadata(response), dropping them), so that
# md.site_info has a siteid to look up instead of always returning None.
assert md._parameters.get("siteid") == "WIDNR_WQX-10032762"


def test_get_results_WQX3(httpx_mock):
Expand Down Expand Up @@ -269,7 +273,7 @@ def test_wqp_metadata_site_info_is_accessible_property():


def test_wqp_metadata_site_info_routes_to_what_sites(monkeypatch):
"""When the query carried ``sites`` (or ``site``/``site_no``),
"""When the query carried a ``siteid`` (WQP's site identifier),
``site_info`` delegates to ``wqp.what_sites`` with that identifier."""
import dataretrieval.wqp as wqp_mod

Expand All @@ -280,5 +284,5 @@ def fake_what_sites(**kwargs):
return "SENTINEL"

monkeypatch.setattr(wqp_mod, "what_sites", fake_what_sites)
assert _wqp_metadata(sites="USGS-05427718").site_info == "SENTINEL"
assert captured == {"sites": "USGS-05427718"}
assert _wqp_metadata(siteid="USGS-05427718").site_info == "SENTINEL"
assert captured == {"siteid": "USGS-05427718"}
Loading