Skip to content

Commit

Permalink
Merge pull request dailyerosion#300 from akrherz/241121
Browse files Browse the repository at this point in the history
Omnibus
  • Loading branch information
akrherz authored Dec 5, 2024
2 parents 85f1afa + 78da5b8 commit 8fc9db2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.7.4"
rev: "v0.8.2"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
41 changes: 29 additions & 12 deletions scripts/import/flowpath_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
from shapely.geometry import LineString
from tqdm import tqdm

from pydep.util import clear_huc12data, get_cli_fname
from pydep.util import (
clear_huc12data,
get_cli_fname,
get_kwfact_class,
get_slope_class,
)

LOG = logger()
print(" * BE CAREFUL! The GeoJSON files may not be 5070, but 26915")
Expand Down Expand Up @@ -258,30 +263,42 @@ def insert_ofe(cursor, gdf, db_fid, ofe, ofe_starts):
)
line = LineString(pts)

bulk_slope = (firstpt["elev"] - lastpt["elev"]) / (
lastpt["len"] - firstpt["len"]
)
res = cursor.execute(
"select id, kwfact from gssurgo where fiscal_year = %s and mukey = %s",
(SOILFY, int(firstpt[SOILCOL])),
)
gssurgo_id, kwfact = res.fetchone()
# dailyerosion/dep/issues/298
groupid = "_".join(
[
str(get_slope_class(bulk_slope * 100.0)),
str(get_kwfact_class(kwfact)),
firstpt["management"][0],
firstpt["GenLU"],
]
)
cursor.execute(
"""
INSERT into flowpath_ofes (flowpath, ofe, geom, bulk_slope, max_slope,
gssurgo_id, fbndid, management, landuse, real_length, field_id)
values (%s, %s, %s, %s, %s,
(select id from gssurgo where fiscal_year = %s and mukey = %s),
%s, %s, %s, %s, %s)
INSERT into flowpath_ofes (flowpath, ofe, geom, bulk_slope, max_slope,
gssurgo_id, fbndid, management, landuse, real_length, field_id, groupid)
values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""",
(
db_fid,
ofe,
f"SRID=5070;{line.wkt}",
(
(firstpt["elev"] - lastpt["elev"])
/ (lastpt["len"] - firstpt["len"])
),
bulk_slope,
gdf["slope"].max(),
SOILFY,
int(firstpt[SOILCOL]),
gssurgo_id,
firstpt["FBndID"],
firstpt["management"],
firstpt["landuse"],
(lastpt["len"] - firstpt["len"]) / 100.0,
firstpt["field_id"],
groupid,
),
)

Expand Down
7 changes: 7 additions & 0 deletions src/pydep/reference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""pydep static reference data."""

# Classification of slope % into classes (right inclusive)
SLOPE_CLASSES = [-99, 2, 4, 6, 12]

# Classification of soil kwfact into classes (right inclusive)
KWFACT_CLASSES = [-99, 0.28, 0.32]
13 changes: 13 additions & 0 deletions src/pydep/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@
import math
from typing import Tuple

import numpy as np
import pandas as pd
from pyiem.database import get_sqlalchemy_conn
from pyiem.iemre import EAST, NORTH, SOUTH, WEST
from sqlalchemy import text

from pydep.reference import KWFACT_CLASSES, SLOPE_CLASSES


def get_kwfact_class(kwfact: float) -> int:
"""Get kwfact class for the given kwfact."""
return np.digitize(kwfact, KWFACT_CLASSES, right=True)


def get_slope_class(slope: float) -> int:
"""Get slope class for the given slope."""
return np.digitize(slope, SLOPE_CLASSES, right=True)


def clear_huc12data(cursor, huc12, scenario) -> Tuple[int, int, int, int]:
"""Clear out database storage for the typical import case.
Expand Down
14 changes: 14 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
from pydep import util


def test_kwfact_class():
"""Test kwfact classification."""
assert util.get_kwfact_class(0) == 1
assert util.get_kwfact_class(0.28) == 1
assert util.get_kwfact_class(0.33) == 3


def test_slope_class():
"""Test slope classification."""
assert util.get_slope_class(0) == 1
assert util.get_slope_class(2) == 1
assert util.get_slope_class(100) == 5


def test_clear_huc12data():
"""Can we clear data?"""
dbconn, cursor = get_dbconnc("idep")
Expand Down

0 comments on commit 8fc9db2

Please sign in to comment.