Skip to content

Commit

Permalink
Gage crosswalk (#41)
Browse files Browse the repository at this point in the history
* improve gage to cat crosswalk

* set geopandas minimum version to 1.0.0

* fix gage cli
  • Loading branch information
JoshCu committed Sep 4, 2024
1 parent ad63070 commit 3028001
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
40 changes: 29 additions & 11 deletions modules/data_processing/gpkg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,34 @@ def get_cat_from_gage_id(gage_id: str, gpkg: Path = file_paths.conus_hydrofabric
"""
gage_id = "".join([x for x in gage_id if x.isdigit()])
logger.info(f"Getting catid for {gage_id}, in {gpkg}")
with sqlite3.connect(gpkg) as con:
sql_query = f"SELECT id FROM hydrolocations WHERE hl_uri = 'Gages-{gage_id}'"
result = con.execute(sql_query).fetchone()
if result is None:
raise IndexError(f"No nexus found for gage ID {gage_id}")
nex_id = con.execute(sql_query).fetchone()[0]
sql_query = f"SELECT divide_id FROM network WHERE toid = '{nex_id}'"
cat_id = con.execute(sql_query).fetchall()
cat_ids = [str(x[0]) for x in cat_id]

return cat_ids
if len(gage_id) < 8:
logger.warning(f"Gages in the hydrofabric are at least 8 digits {gage_id}")
old_gage_id = gage_id
gage_id = f"{int(gage_id):08d}"
logger.warning(f"Converted {old_gage_id} to {gage_id}")

logger.info(f"Getting catid for {gage_id}, in {gpkg}")

# the hydrolocations table seems to have a bunch of errors in it
# use flowpath_attributes instead
# both have errors, cross reference them
with sqlite3.connect(gpkg) as con:
sql_query = f"""SELECT f.id
FROM flowpaths AS f
JOIN hydrolocations AS h ON f.toid = h.id
JOIN flowpath_attributes AS fa ON f.id = fa.id
WHERE h.hl_uri = 'Gages-{gage_id}'
AND fa.rl_gages LIKE '%{gage_id}%'"""
result = con.execute(sql_query).fetchall()
if len(result) == 0:
logger.critical(f"Gage ID {gage_id} is not associated with any waterbodies")
raise IndexError(f"Could not find a waterbody for {gage_id}")
if len(result) > 1:
logger.critical(f"Gage ID {gage_id} is associated with multiple waterbodies")
raise IndexError(f"Could not find a unique waterbody for {gage_id}")

wb_id = result[0][0]
cat_id = wb_id.replace("wb", "cat")

return cat_id
2 changes: 1 addition & 1 deletion modules/ngiab_data_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def get_cat_ids_from_gage_ids(input_file: Path) -> List[str]:
cat_ids = []
for gage_id in gage_ids:
cat_id = get_cat_from_gage_id(gage_id)
cat_ids.extend(cat_id)
cat_ids.append(cat_id)
logging.info(f"Converted {len(gage_ids)} gage IDs to {len(cat_ids)} catchment IDs")
return cat_ids

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies = [
"pyproj==3.6.1",
"Flask==3.0.2",
"Flask-Cors==4.0.1",
"geopandas==0.14.3",
"geopandas>=1.0.0",
"requests==2.32.2",
"igraph==0.11.4",
"s3fs==2024.3.1",
Expand Down

0 comments on commit 3028001

Please sign in to comment.