Skip to content

Commit

Permalink
get gauge catchment from toid
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshCu committed Aug 8, 2024
1 parent 8f79335 commit b000a10
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 4 additions & 2 deletions modules/data_processing/gpkg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def get_table_crs(gpkg: str, table: str) -> str:
return crs


def get_nex_from_gage_id(gage_id: str, gpkg: Path = file_paths.conus_hydrofabric()) -> str:
def get_wb_from_gage_id(gage_id: str, gpkg: Path = file_paths.conus_hydrofabric()) -> str:
"""
Get the nexus id of associated with a gage id.
Expand All @@ -291,6 +291,8 @@ def get_nex_from_gage_id(gage_id: str, gpkg: Path = file_paths.conus_hydrofabric
with sqlite3.connect(gpkg) as con:
sql_query = f"SELECT id FROM hydrolocations WHERE hl_uri = 'Gages-{gage_id}'"
nex_id = con.execute(sql_query).fetchone()[0]
sql_query = f"SELECT id FROM network WHERE toid = '{nex_id}'"
wb_id = con.execute(sql_query).fetchone()[0]
if nex_id is None:
raise IndexError(f"No nexus found for gage ID {gage_id}")
return nex_id
return wb_id
5 changes: 2 additions & 3 deletions modules/map_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
blob_to_geometry,
blob_to_centroid,
get_wbid_from_point,
get_nex_from_gage_id,
get_wb_from_gage_id,
)
from data_processing.create_realization import create_realization
from data_processing.file_paths import file_paths
Expand Down Expand Up @@ -352,8 +352,7 @@ def get_logs():
@main.route("/get_wbid_from_gage_id", methods=["POST"])
def get_wbid_from_gage_id():
gage_id = json.loads(request.data.decode("utf-8"))["gage_id"]
result = get_nex_from_gage_id(gage_id)
result = get_wb_from_gage_id(gage_id)
if result is not None:
result = "wb-" + result.split("-")[1]
return jsonify({"wb_id": result}), 200
return jsonify({"error": "No wbid found for gage id"}), 404
5 changes: 2 additions & 3 deletions modules/ngiab_data_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from colorama import Fore, Style, init

from data_processing.file_paths import file_paths
from data_processing.gpkg_utils import get_wbid_from_point, get_nex_from_gage_id
from data_processing.gpkg_utils import get_wbid_from_point, get_wb_from_gage_id
from data_processing.subset import subset
from data_processing.forcings import create_forcings
from data_processing.create_realization import create_realization
Expand Down Expand Up @@ -289,8 +289,7 @@ def get_wb_ids_from_gage_ids(input_file: Path) -> List[str]:
gage_ids = read_gage_ids(input_file)
wb_ids = []
for gage_id in gage_ids:
nex_id = get_nex_from_gage_id(gage_id)
wb_id = f"wb-{nex_id[4:]}" # Replace 'nex-' with 'wb-'
wb_id = get_wb_from_gage_id(gage_id)
wb_ids.append(wb_id)
return wb_ids

Expand Down

0 comments on commit b000a10

Please sign in to comment.