Skip to content

Commit

Permalink
ENH: Improve the logic for checking input data. [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Taher Chegini committed Jan 3, 2024
1 parent de9166b commit c42895d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions pygeohydro/pygeohydro.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import warnings
import zipfile
from pathlib import Path
from typing import TYPE_CHECKING, Any, Iterator, Literal, Sequence, Tuple, Union, cast
from typing import TYPE_CHECKING, Any, Iterable, Iterator, Literal, Sequence, Tuple, Union, cast
from unittest.mock import patch

import cytoolz.curried as tlz
Expand All @@ -22,6 +22,7 @@
import xarray as xr
from rioxarray import _io as rxr
from shapely.errors import GEOSException

import async_retriever as ar
import pygeoogc as ogc
import pygeoutils as geoutils
Expand Down Expand Up @@ -602,11 +603,13 @@ def inventory_byid(self, federal_ids: list[str]) -> gpd.GeoDataFrame:
>>> nid = NID()
>>> dams = nid.inventory_byid(['KY01232', 'GA02400', 'NE04081', 'IL55070', 'TN05345'])
"""
ids = set(federal_ids) if isinstance(federal_ids, (list, tuple)) else {federal_ids}
ids = {str(i).upper() for i in ids}
urls = [f"{self.base_url}/dams/{i}/inventory" for i in ids]
if len(urls) != len(ids):
raise InputTypeError("dam_ids", "list of Federal IDs")
if not isinstance(federal_ids, Iterable) or isinstance(federal_ids, (str, int)):
raise InputTypeError("federal_ids", "list of str (Federal IDs)")

if not all(isinstance(i, str) for i in federal_ids):
raise InputTypeError("federal_ids", "list of str (Federal IDs)")

urls = [f"{self.base_url}/dams/{i.upper()}/inventory" for i in set(federal_ids)]
return self._to_geodf(pd.DataFrame(self._get_json(urls)).set_index("id"))

def get_suggestions(
Expand Down
2 changes: 1 addition & 1 deletion pygeohydro/watershed.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def irrigation_withdrawals() -> xr.Dataset:
resp = ar.retrieve_text(urls.tolist())
irr = {}
for name, r in zip(urls.index, resp):
df = pd.read_csv(io.StringIO(r), usecols=lambda x: "m3" in x or "huc12t" in x) # type: ignore
df = pd.read_csv(io.StringIO(r), usecols=lambda x: "m3" in x or "huc12t" in x) # pyright: ignore[reportGeneralTypeIssues]
df["huc12t"] = df["huc12t"].str.strip("'")
df = df.rename(columns={"huc12t": "huc12"}).set_index("huc12")
df = df.rename(columns={c: str(c)[:3].capitalize() for c in df})
Expand Down

0 comments on commit c42895d

Please sign in to comment.