Skip to content

Commit 02b2e4a

Browse files
committed
fix fetching usgs daily dv (#530)
* fix fetching usgs daily dv * version bump
1 parent 685efd2 commit 02b2e4a

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/teehr/fetching/usgs/usgs.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
USGS_VARIABLE_MAPPER
4545
)
4646

47-
DATETIME_STR_FMT = "%Y-%m-%dT%H:%M:00+0000"
47+
HOURLY_DATETIME_STR_FMT = "%Y-%m-%dT%H:%M:00+0000"
48+
DAILY_DATETIME_STR_FMT = "%Y-%m-%d"
49+
4850
DAYLIGHT_SAVINGS_PAD = timedelta(hours=2)
4951

5052
pd.options.mode.copy_on_write = True
@@ -211,11 +213,22 @@ def _fetch_usgs_streamflow(
211213
) -> pd.DataFrame:
212214
"""Fetch USGS gage data and format to TEEHR format."""
213215
logger.debug("Fetching USGS streamflow data from NWIS.")
214-
start_dt_str = start_date.strftime(DATETIME_STR_FMT)
216+
217+
if service == "iv":
218+
datetime_str_format = HOURLY_DATETIME_STR_FMT
219+
elif service == "dv":
220+
datetime_str_format = DAILY_DATETIME_STR_FMT
221+
else:
222+
err_msg = f"Service '{service}' is not supported. Valid options are 'iv' and 'dv'."
223+
logger.error(err_msg)
224+
raise ValueError(err_msg)
225+
226+
start_dt_str = start_date.strftime(datetime_str_format)
227+
215228
end_dt_str = (
216229
end_date
217230
- timedelta(minutes=1)
218-
).strftime(DATETIME_STR_FMT)
231+
).strftime(datetime_str_format)
219232

220233
# Parse out list of sites.
221234
parsed_sites = _parse_site_id_list(sites)

tests/fetch/test_usgs_fetching_chunkby.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,44 @@ def test_chunkby_all(tmpdir):
197197
assert df["value_time"].max() == pd.Timestamp("2023-02-25 00:00:00")
198198

199199

200+
def test_daily_service(tmpdir):
201+
"""Test chunkby location id."""
202+
usgs_to_parquet(
203+
sites=[
204+
"02449838",
205+
"02450825",
206+
],
207+
start_date=datetime(2023, 2, 20),
208+
end_date=datetime(2023, 2, 21),
209+
output_parquet_dir=Path(tmpdir),
210+
chunk_by="location_id",
211+
overwrite_output=True,
212+
convert_to_si=False,
213+
service="dv"
214+
)
215+
df1 = pd.read_parquet(
216+
Path(
217+
tmpdir,
218+
"02449838.parquet"
219+
)
220+
)
221+
df2 = pd.read_parquet(
222+
Path(
223+
tmpdir,
224+
"02450825.parquet"
225+
)
226+
)
227+
assert len(df1) == 1
228+
assert len(df2) == 1
229+
assert df1["value"].iloc[0] == 157.0 # confirmed with NWIS web
230+
assert df2["value"].iloc[0] == 389.0
231+
232+
200233
if __name__ == "__main__":
201234
with tempfile.TemporaryDirectory(prefix="teehr-") as tempdir:
202235
test_chunkby_location_id(tempfile.mkdtemp(dir=tempdir))
203236
test_chunkby_day(tempfile.mkdtemp(dir=tempdir))
204237
test_chunkby_week(tempfile.mkdtemp(dir=tempdir))
205238
test_chunkby_month(tempfile.mkdtemp(dir=tempdir))
206239
test_chunkby_all(tempfile.mkdtemp(dir=tempdir))
240+
test_daily_service(tempfile.mkdtemp(dir=tempdir))

0 commit comments

Comments
 (0)