Skip to content
72 changes: 33 additions & 39 deletions dataretrieval/wqp.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,9 @@ def what_organizations(

kwargs = _check_kwargs(kwargs)

if legacy is True:
url = wqp_url("Organization")
else:
print("WQX3.0 profile not available, returning legacy profile.")
url = wqp_url("Organization")
if not legacy:
_warn_wqx3_unavailable()
url = wqp_url("Organization")

response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)

Expand Down Expand Up @@ -306,11 +304,9 @@ def what_projects(ssl_check=True, legacy=True, **kwargs):

kwargs = _check_kwargs(kwargs)

if legacy is True:
url = wqp_url("Project")
else:
print("WQX3.0 profile not available, returning legacy profile.")
url = wqp_url("Project")
if not legacy:
_warn_wqx3_unavailable()
url = wqp_url("Project")

response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)

Expand Down Expand Up @@ -432,11 +428,9 @@ def what_detection_limits(

kwargs = _check_kwargs(kwargs)

if legacy is True:
url = wqp_url("ResultDetectionQuantitationLimit")
else:
print("WQX3.0 profile not available, returning legacy profile.")
url = wqp_url("ResultDetectionQuantitationLimit")
if not legacy:
_warn_wqx3_unavailable()
url = wqp_url("ResultDetectionQuantitationLimit")

response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)

Expand Down Expand Up @@ -487,11 +481,9 @@ def what_habitat_metrics(

kwargs = _check_kwargs(kwargs)

if legacy is True:
url = wqp_url("BiologicalMetric")
else:
print("WQX3.0 profile not available, returning legacy profile.")
url = wqp_url("BiologicalMetric")
if not legacy:
_warn_wqx3_unavailable()
url = wqp_url("BiologicalMetric")

response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)

Expand All @@ -516,7 +508,7 @@ def what_project_weights(ssl_check=True, legacy=True, **kwargs):
ssl_check : bool
Check the SSL certificate. Default is True.
legacy : bool
Retrun the legacy WQX data profile. Default is True.
Return the legacy WQX data profile. Default is True.
**kwargs : optional
Accepts the same parameters as :obj:`dataretrieval.wqp.get_results`

Expand All @@ -543,11 +535,9 @@ def what_project_weights(ssl_check=True, legacy=True, **kwargs):

kwargs = _check_kwargs(kwargs)

if legacy is True:
url = wqp_url("ProjectMonitoringLocationWeighting")
else:
print("WQX3.0 profile not available, returning legacy profile.")
url = wqp_url("ProjectMonitoringLocationWeighting")
if not legacy:
_warn_wqx3_unavailable()
url = wqp_url("ProjectMonitoringLocationWeighting")

response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)

Expand Down Expand Up @@ -599,15 +589,13 @@ def what_activity_metrics(ssl_check=True, legacy=True, **kwargs):

kwargs = _check_kwargs(kwargs)

if legacy is True:
url = wqp_url("ActivityMetric")
else:
print("WQX3.0 profile not available, returning legacy profile.")
url = wqp_url("ActivityMetric")
if not legacy:
_warn_wqx3_unavailable()
url = wqp_url("ActivityMetric")

response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)

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

return df, WQP_Metadata(response)

Expand All @@ -619,9 +607,8 @@ def wqp_url(service):
_warn_legacy_use()

if service not in services_legacy:
raise TypeError(
"Legacy service not recognized. Valid options are",
f"{services_legacy}.",
raise ValueError(
f"Legacy service not recognized. Valid options are {services_legacy}."
)

return f"{base_url}{service}/Search?"
Expand All @@ -634,9 +621,8 @@ def wqx3_url(service):
_warn_wqx3_use()

if service not in services_wqx3:
raise TypeError(
"WQX3.0 service not recognized. Valid options are",
f"{services_wqx3}.",
raise ValueError(
f"WQX3.0 service not recognized. Valid options are {services_wqx3}."
)

return f"{base_url}{service}/search?"
Expand Down Expand Up @@ -708,7 +694,7 @@ def _check_kwargs(kwargs):
def _warn_wqx3_use():
message = (
"Support for the WQX3.0 profiles is experimental. "
"Queries may be slow or fail intermitttently."
"Queries may be slow or fail intermittently."
)
warnings.warn(message, UserWarning, stacklevel=2)

Expand All @@ -722,3 +708,11 @@ def _warn_legacy_use():
"will remove this warning."
)
warnings.warn(message, DeprecationWarning, stacklevel=2)


def _warn_wqx3_unavailable():
warnings.warn(
"WQX3.0 profile not available, returning legacy profile.",
UserWarning,
stacklevel=3,
)
Comment on lines +693 to +699
13 changes: 13 additions & 0 deletions tests/wqp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,16 @@ def test_check_kwargs():
kwargs = {"mimeType": "foo"}
with pytest.raises(ValueError):
kwargs = _check_kwargs(kwargs)


def test_what_organizations_legacy_false_warns(requests_mock):
"""legacy=False on what_organizations should emit a warning, not print to stdout."""
request_url = (
"https://www.waterqualitydata.us/data/Organization/Search?statecode=US%3A34"
"&characteristicName=Chloride&mimeType=csv"
)
mock_request(requests_mock, request_url, "tests/data/wqp_organizations.txt")
with pytest.warns(UserWarning, match="WQX3.0 profile not available"):
what_organizations(
statecode="US:34", characteristicName="Chloride", legacy=False
)
Loading