Skip to content

Commit 8e3d2b8

Browse files
thodson-usgsclaude
andcommitted
refactor(wateruse): snake_case the public date / time-resolution params
Expose idiomatic snake_case parameter names on ``get_wateruse`` and map them to the NWDC's compact wire names when the request is built — the same user-facing convention as ``waterdata.get_samples`` (DOI-USGS#331): timeres -> time_resolution startdate -> start_date enddate -> end_date Only the Python-facing parameters change; the NWDC query still receives ``timeres`` / ``startdate`` / ``enddate`` on the wire. ``wateruse`` is a new, unreleased module, so there are no prior callers to keep compatible and no deprecation shim is needed (unlike DOI-USGS#331, which had to accept the released camelCase aliases). Updates the signature, docstrings, the README and demo-notebook examples, and adds a test pinning the snake_case -> wire-name mapping. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Sjb14HkwuCydKSKMsaXsgd
1 parent d454cd9 commit 8e3d2b8

4 files changed

Lines changed: 47 additions & 24 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ sites, metadata = ngwmn.get_sites(state='Wisconsin')
133133

134134
print(f"Found {len(sites)} NGWMN sites in Wisconsin")
135135

136-
# Pull water levels from the first twenty sites over a time window.
136+
# Pull water levels from the first twenty sites over a time window.
137137
water_levels, metadata = ngwmn.get_water_level(
138138
monitoring_location_id=sites['monitoring_location_id'][:20],
139139
datetime=['2022-01-01', '2024-01-01']
@@ -206,8 +206,8 @@ df, metadata = wateruse.get_wateruse(
206206
model='wu-public-supply-wd',
207207
variable=['pswdtot', 'pswdgw', 'pswdsw'],
208208
state='RI', # name/postal/FIPS; pass a list to fan out over several areas
209-
startdate='2020-01',
210-
timeres='monthly',
209+
start_date='2020-01',
210+
time_resolution='monthly',
211211
)
212212

213213
print(f"Retrieved {len(df)} records across {df['huc12_id'].nunique()} watersheds")

dataretrieval/wateruse.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
model="wu-public-supply-wd",
3636
variable=["pswdtot", "pswdgw", "pswdsw"],
3737
state="RI",
38-
startdate="2020-01",
39-
timeres="monthly",
38+
start_date="2020-01",
39+
time_resolution="monthly",
4040
)
4141
4242
"""
@@ -96,9 +96,9 @@ def get_wateruse(
9696
state: str | int | Iterable[str | int] | None = None,
9797
county: str | Iterable[str] | None = None,
9898
huc: str | Iterable[str] | None = None,
99-
timeres: str | None = None,
100-
startdate: str | None = None,
101-
enddate: str | None = None,
99+
time_resolution: str | None = None,
100+
start_date: str | None = None,
101+
end_date: str | None = None,
102102
intersection: str = "overlap",
103103
limit: int = 600,
104104
ssl_check: bool = True,
@@ -146,15 +146,15 @@ def get_wateruse(
146146
147147
Provide exactly one of ``state``, ``county``, or ``huc`` (each may be a
148148
single value or a list).
149-
timeres : string, optional
149+
time_resolution : string, optional
150150
Temporal resolution: ``"monthly"``, ``"annualcy"`` (annual, calendar
151151
year), or ``"annualwy"`` (annual, water year). See
152152
:data:`TIME_RESOLUTIONS`.
153-
startdate : string, optional
153+
start_date : string, optional
154154
Start of the query window, formatted ``"YYYY"`` for annual data or
155155
``"YYYY-MM"`` for monthly data.
156-
enddate : string, optional
157-
End of the query window, in the same format as ``startdate``.
156+
end_date : string, optional
157+
End of the query window, in the same format as ``start_date``.
158158
intersection : string, optional
159159
How to select HUC12s that straddle the queried-area boundary:
160160
``"overlap"`` (any overlap, the default) or ``"envelop"`` (fully
@@ -199,18 +199,21 @@ def get_wateruse(
199199
... model="wu-public-supply-wd",
200200
... variable=["pswdtot", "pswdgw", "pswdsw"],
201201
... state="RI",
202-
... startdate="2020-01",
203-
... timeres="monthly",
202+
... start_date="2020-01",
203+
... time_resolution="monthly",
204204
... )
205205
206206
"""
207+
# The public parameters are idiomatic snake_case (consistent with
208+
# ``waterdata.get_samples``); the NWDC service expects compact lowercase
209+
# query names, so map to those here as the request is built.
207210
base_params: dict[str, Any] = {
208211
"format": "csv",
209212
"model": model,
210213
"variable": to_str(variable),
211-
"timeres": timeres,
212-
"startdate": startdate,
213-
"enddate": enddate,
214+
"timeres": time_resolution,
215+
"startdate": start_date,
216+
"enddate": end_date,
214217
"intersection": intersection,
215218
"limit": limit,
216219
}

demos/USGS_WaterUse_Examples.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@
7878
" model=\"wu-public-supply-wd\",\n",
7979
" variable=[\"pswdtot\", \"pswdgw\", \"pswdsw\"],\n",
8080
" state=\"WI\",\n",
81-
" startdate=\"2020-01\",\n",
82-
" enddate=\"2020-12\",\n",
83-
" timeres=\"monthly\",\n",
81+
" start_date=\"2020-01\",\n",
82+
" end_date=\"2020-12\",\n",
83+
" time_resolution=\"monthly\",\n",
8484
")\n",
8585
"\n",
8686
"print(f\"{len(df):,} rows across {df['huc12_id'].nunique():,} HUC12 watersheds\")\n",
@@ -194,8 +194,8 @@
194194
" default) is split across pages; `get_wateruse` follows the links and returns\n",
195195
" a single concatenated frame. Whole-region queries (e.g. `huc=\"04\"`)\n",
196196
" may return many pages and take longer.\n",
197-
"- **Annual data.** Set `timeres=\"annualcy\"` (calendar year) or `\"annualwy\"`\n",
198-
" (water year) and use four-digit years for `startdate`/`enddate`; the time\n",
197+
"- **Annual data.** Set `time_resolution=\"annualcy\"` (calendar year) or `\"annualwy\"`\n",
198+
" (water year) and use four-digit years for `start_date`/`end_date`; the time\n",
199199
" column comes back as `year` instead of `year_month`.\n",
200200
"- **Other models.** Swap `model` and `variable` to retrieve irrigation\n",
201201
" (`wu-irrigation-wd`) or thermoelectric (`wu-thermoelectric`) water use. See\n",

tests/wateruse_test.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def test_get_wateruse_single_page(httpx_mock):
4747
model="wu-public-supply-wd",
4848
variable=["pswdtot", "pswdgw", "pswdsw"],
4949
state="RI",
50-
startdate="2020-01",
51-
timeres="monthly",
50+
start_date="2020-01",
51+
time_resolution="monthly",
5252
)
5353

5454
assert isinstance(df, pd.DataFrame)
@@ -105,6 +105,26 @@ def test_unset_params_are_dropped(httpx_mock):
105105
assert qs["limit"] == ["600"]
106106

107107

108+
def test_snake_case_date_params_map_to_nwdc_wire_names(httpx_mock):
109+
"""The public snake_case params (``start_date`` / ``end_date`` /
110+
``time_resolution``) are sent under the NWDC's compact wire names
111+
(``startdate`` / ``enddate`` / ``timeres``)."""
112+
httpx_mock.add_response(method="GET", url=WU_RE, text=_CSV_PAGE)
113+
114+
get_wateruse(
115+
model="wu-public-supply-wd",
116+
state="RI",
117+
start_date="2020-01",
118+
end_date="2020-12",
119+
time_resolution="monthly",
120+
)
121+
122+
qs = parse_qs(urlsplit(str(httpx_mock.get_requests()[0].url)).query)
123+
assert qs["startdate"] == ["2020-01"]
124+
assert qs["enddate"] == ["2020-12"]
125+
assert qs["timeres"] == ["monthly"]
126+
127+
108128
def test_pagination_follows_link_header_and_concatenates(httpx_mock):
109129
"""Pages are followed via the ``rel="next"`` Link header and concatenated."""
110130
httpx_mock.add_response(

0 commit comments

Comments
 (0)