Skip to content

Commit

Permalink
Merge pull request #141 from Dewberry/bugfix/structures
Browse files Browse the repository at this point in the history
Bugfix/structures
  • Loading branch information
slawler authored Aug 23, 2024
2 parents c4a5c85 + 72bbffb commit 76fc403
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 625 deletions.
23 changes: 13 additions & 10 deletions ripple1d/ops/subset_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ def ripple_river(self) -> gpd.GeoDataFrame:
@property
def ripple_structure(self) -> gpd.GeoDataFrame:
"""Subset structures based on NWM reach."""
gdf = self.subset_gdfs["Structure"]
if len(gdf.loc[gdf["type"] == 6, :]) > 0:
raise NotImplementedError(f"Lateral structures are not currently supported in ripple1d")
return gdf
return self.subset_gdfs["Structure"]

@property
def subset_gdfs(self) -> dict:
Expand Down Expand Up @@ -151,7 +148,15 @@ def write_ripple_gpkg(
os.makedirs(self.dst_project_dir, exist_ok=True)

for layer, gdf in self.subset_gdfs.items():
gdf.to_file(self.ripple_gpkg_file, layer=layer)
# remove lateral structures
if layer == "Structure":
if (gdf["type"] == 6).any():
logging.warning(
f"Lateral structures are not currently supported in ripple1d. The lateral structures will be dropped."
)
gdf = gdf.loc[gdf["type"] != 6, :]
if gdf.shape[0] > 0:
gdf.to_file(self.ripple_gpkg_file, layer=layer)

@property
def min_flow(self) -> float:
Expand Down Expand Up @@ -317,14 +322,12 @@ def adjust_river_stations(self, xs_us_reach, structures_us_reach) -> tuple:
xs_us_reach["ras_data"] = xs_us_reach["ras_data"].apply(
lambda ras_data: self.update_river_station(ras_data, self.xs_ds_reach["river_station"].max())
)
if self.source_structure is not None:
if structures_us_reach is not None:
structures_us_reach["river_station"] = (
structures_us_reach["river_station"] + self.structures_ds_reach["river_station"].max()
structures_us_reach["river_station"] + self.xs_ds_reach["river_station"].max()
)
structures_us_reach["ras_data"] = structures_us_reach["ras_data"].apply(
lambda ras_data: self.update_river_station(
ras_data, self.structures_ds_reach["river_station"].max()
)
lambda ras_data: self.update_river_station(ras_data, self.xs_ds_reach["river_station"].max())
)
return xs_us_reach, structures_us_reach

Expand Down
144 changes: 144 additions & 0 deletions tests/api_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import json
import os
import shutil
import subprocess
import time
import unittest

import pandas as pd
import pytest
import requests

from ripple1d.ras import RasFlowText


def start_server():
return subprocess.Popen(
["ripple1d", "start"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)


def submit_job(process: str, payload: dict):
headers = {"Content-Type": "application/json"}
url = f"http://localhost/processes/{process}/execution"
response = requests.post(url, data=json.dumps(payload), headers=headers)
return json.loads(response.text)


def wait_for_job(job_id: str):
url = f"http://localhost/jobs/{job_id}?tb=true"
while True:
response = requests.get(url)
job_status = response.json().get("status")
if job_status in ["successful", "failed"]:

return job_status
time.sleep(7) # Wait for 10 seconds before checking again


def check_process(func):

def wrapper(self, *args, **kwargs):
process, payload, files = func(self)
response = submit_job(process, payload)
status = wait_for_job(response["jobID"])

self.assertEqual(status, "successful")
for file in files:
self.assertTrue(os.path.exists(file))

return wrapper


@pytest.mark.usefixtures("setup_data")
class TestApi(unittest.TestCase):
# @classmethod
# def setUpClass(cls):
# cls.server_process = start_server()
# time.sleep(10) # Give the server some time to start

@check_process
def test_a_extract_submodel(self):
payload = {
"source_model_directory": self.SOURCE_RAS_MODEL_DIRECTORY,
"submodel_directory": self.SUBMODELS_DIRECTORY,
"nwm_id": self.REACH_ID,
}
process = "extract_submodel"
files = [self.GPKG_FILE]
return process, payload, files

@check_process
def test_b_create_ras_terrain(self):
payload = {"submodel_directory": self.SUBMODELS_DIRECTORY}
process = "create_ras_terrain"
files = [self.TERRAIN_HDF, self.TERRAIN_VRT]
return process, payload, files

@check_process
def test_c_create_model_run_normal_depth(self):
payload = {
"submodel_directory": self.SUBMODELS_DIRECTORY,
"plan_suffix": "ind",
"num_of_discharges_for_initial_normal_depth_runs": 2,
"show_ras": False,
}
process = "create_model_run_normal_depth"
files = [self.RAS_PROJECT_FILE, self.GEOM_FILE, self.PLAN1_FILE, self.FLOW1_FILE, self.RESULT1_FILE]
return process, payload, files

@check_process
def test_d_run_incremental_normal_depth(self):
payload = {
"submodel_directory": self.SUBMODELS_DIRECTORY,
"plan_suffix": "nd",
"depth_increment": 3,
"ras_version": "631",
"show_ras": False,
}
process = "run_incremental_normal_depth"
files = [self.PLAN2_FILE, self.FLOW2_FILE, self.RESULT2_FILE]
return process, payload, files

@check_process
def test_e_run_known_wse(self):
payload = {
"submodel_directory": self.SUBMODELS_DIRECTORY,
"plan_suffix": "kwse",
"min_elevation": self.min_elevation + 40,
"max_elevation": self.min_elevation + 41,
"depth_increment": 1.0,
"ras_version": "631",
"show_ras": False,
}
process = "run_known_wse"
files = [self.PLAN3_FILE, self.FLOW3_FILE, self.RESULT3_FILE]
return process, payload, files

@check_process
def test_f_create_fim_lib(self):
payload = {"submodel_directory": self.SUBMODELS_DIRECTORY, "plans": ["nd", "kwse"]}
process = "create_fim_lib"
files = [self.FIM_LIB_DB, self.DEPTH_GRIDS_ND, self.DEPTH_GRIDS_KWSE]
return process, payload, files

def test_g_check_flows_are_equal(self):
rf2 = pd.DataFrame(RasFlowText(self.FLOW2_FILE).flow_change_locations)
rf3 = pd.DataFrame(RasFlowText(self.FLOW3_FILE).flow_change_locations)
self.assertTrue(len(set(rf3["flows"].iloc[0]) - set(rf2["flows"].iloc[0])) == 0)

@check_process
def test_h_nwm_reach_model_stac(self):
payload = {"ras_project_directory": self.SUBMODELS_DIRECTORY}
process = "nwm_reach_model_stac"
files = [self.MODEL_STAC_ITEM]
return process, payload, files

@check_process
def test_i_fim_lib_stac(self):
payload = {"ras_project_directory": self.SUBMODELS_DIRECTORY, "nwm_reach_id": self.REACH_ID}
process = "fim_lib_stac"
files = [self.FIM_LIB_STAC_ITEM]
return process, payload, files
175 changes: 0 additions & 175 deletions tests/api_tests_Baxter.py

This file was deleted.

Loading

0 comments on commit 76fc403

Please sign in to comment.