Skip to content

Commit

Permalink
rcat condition in rme and brat fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jtgilbert authored and philipbaileynar committed Nov 26, 2024
1 parent af0ab3f commit 7859768
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/brat/sqlbrat/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "5.1.0"
__version__ = "5.1.1"
4 changes: 2 additions & 2 deletions packages/brat/sqlbrat/utils/conservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def calc_opportunities(opportunities: dict, risks: dict, risk_id: float, reachco
elif occ_ex >= 5 and occ_ex < 10:
return opportunities['Encourage Beaver Expansion/Colonization']
else:
if ipc_highlu >= 25:
if ipc_highlu >= 25 or ovc_ex >= 5:
return opportunities['Natural or Anthropogenic Limitations']
else:
return opportunities['Address Resource Limitations']
Expand All @@ -216,7 +216,7 @@ def calc_opportunities(opportunities: dict, risks: dict, risk_id: float, reachco
else:
return opportunities['Appropriate for Beaver Mimicry']
else:
if ipc_highlu >= 25:
if ipc_highlu >= 25 or ovc_ex >= 5:
return opportunities['Natural or Anthropogenic Limitations']
else:
return opportunities['Address Resource Limitations']
Expand Down
2 changes: 1 addition & 1 deletion packages/rme/rme/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.1.0"
__version__ = "2.1.1"
13 changes: 7 additions & 6 deletions packages/rme/rme/database/data_metrics/metrics.csv
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ metric_id,name,machine_code,data_type,field_name,description,small,medium,large,
40,Riparian Vegetation Departure,RVD,REAL,RCAT_IGO_riparian_veg_departure,,200,400,1400,,3,1,
41,Agriculture Conversion,AGCONV,REAL,RCAT_IGO_riparian_ag_conversion,,200,400,1400,,3,1,
42,Development,DEVEL,REAL,RCAT_IGO_riparian_develop,,200,400,1400,,3,1,
43,BRAT Capacity,BRATCAP,REAL,BRAT_IGO_capacity,,200,400,1400,,3,1,
44,BRAT Risk,BRATRISK,TEXT,BRAT_IGO_risk,,200,400,1400,,3,1,
45,BRAT Opportunity,BRATOPP,TEXT,BRAT_IGO_opportunity,,200,400,1400,,3,1,
46,Floodplain Count River Left,CNTFLDL,INTEGER,,,200,400,1400,,2,0,
47,Floodplain Count River Right,CNTFLDR,INTEGER,,,200,400,1400,,2,0,
48,Grainsize,GRNSIZE,REAL,,,200,400,1400,,3,0,
43,Riparian Condition,RIPCOND,REAL,RCAT_IGO_riparian_condition,,200,400,1400,,3,1,
44,BRAT Capacity,BRATCAP,REAL,BRAT_IGO_capacity,,200,400,1400,,3,1,
45,BRAT Risk,BRATRISK,TEXT,BRAT_IGO_risk,,200,400,1400,,3,1,
46,BRAT Opportunity,BRATOPP,TEXT,BRAT_IGO_opportunity,,200,400,1400,,3,1,
47,Floodplain Count River Left,CNTFLDL,INTEGER,,,200,400,1400,,2,0,
48,Floodplain Count River Right,CNTFLDR,INTEGER,,,200,400,1400,,2,0,
49,Grainsize,GRNSIZE,REAL,,,200,400,1400,,3,0,
30 changes: 26 additions & 4 deletions packages/rme/rme/metric_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,16 @@ def metric_engine(huc: int, in_flowlines: Path, in_vaa_table: Path, in_counties:
devel = curs.fetchone()[0]
metrics_output[metric['metric_id']] = str(devel)

if 'RIPCOND' in metrics:
metric = metrics['RIPCOND']

with sqlite3.connect(inputs_gpkg) as conn:
curs = conn.cursor()
curs.execute(
f"SELECT Condition FROM rcat_dgo WHERE fid = {dgo_id}")
rip_cond = curs.fetchone()[0]
metrics_output[metric['metric_id']] = str(rip_cond)

if 'BRATCAP' in metrics and brat_dgos:
metric = metrics['BRATCAP']

Expand Down Expand Up @@ -1490,6 +1500,18 @@ def metric_engine(huc: int, in_flowlines: Path, in_vaa_table: Path, in_counties:
curs.execute(
f"INSERT INTO igo_metric_values (igo_id, metric_id, metric_value) VALUES ({igo_id}, {metrics['DEVEL']['metric_id']}, {str(devel_val)})")

if 'RIPCOND' in metrics and rcat_dgos:
with sqlite3.connect(inputs_gpkg) as conn:
curs2 = conn.cursor()
curs2.execute(
f"SELECT Condition, segment_area FROM rcat_dgo WHERE fid IN ({','.join([str(dgo_id) for dgo_id in dgo_ids])})")
ripcond = curs2.fetchall()
ripcond_val = sum(ripcond[i][0] * ripcond[i][1] for i in range(len(ripcond))) / sum([ripcond[i][1]
for i in range(len(ripcond))]) if sum([ripcond[i][1] for i in range(len(ripcond))]) > 0.0 else None
if ripcond_val is not None:
curs.execute(
f"INSERT INTO igo_metric_values (igo_id, metric_id, metric_value) VALUES ({igo_id}, {metrics['RIPCOND']['metric_id']}, {str(ripcond_val)})")

if 'BRATCAP' in metrics and brat_dgos:
with sqlite3.connect(inputs_gpkg) as conn:
curs2 = conn.cursor()
Expand Down Expand Up @@ -1614,7 +1636,7 @@ def metric_engine(huc: int, in_flowlines: Path, in_vaa_table: Path, in_counties:

# Flattnen outputs
log.info('Preparing Final RME Outputs')

field_types = {}
with sqlite3.connect(intermediates_gpkg) as conn:
rme_curs = conn.cursor()
Expand All @@ -1633,13 +1655,13 @@ def metric_engine(huc: int, in_flowlines: Path, in_vaa_table: Path, in_counties:
elif field_type.lower() == 'real':
oft_type = ogr.OFTReal
field_types[row[1].lower()] = oft_type

rme_igos = LayerTypes['RME_OUTPUTS'].sub_layers['IGO_METRICS'].rel_path
rme_dgos = LayerTypes['RME_OUTPUTS'].sub_layers['DGO_METRICS'].rel_path

with GeopackageLayer(intermediates_gpkg, 'vw_igo_metrics') as igo_metrics_layer, \
GeopackageLayer(outputs_gpkg, rme_igos, write=True) as igo_output_layer:

fields = igo_metrics_layer.get_fields()
igo_output_layer.create_layer_from_ref(igo_metrics_layer, create_fields=False)

Expand All @@ -1658,7 +1680,7 @@ def metric_engine(huc: int, in_flowlines: Path, in_vaa_table: Path, in_counties:

with GeopackageLayer(intermediates_gpkg, 'vw_dgo_metrics') as dgo_metrics_layer, \
GeopackageLayer(outputs_gpkg, rme_dgos, write=True) as dgo_output_layer:

fields = dgo_metrics_layer.get_fields()
dgo_output_layer.create_layer_from_ref(dgo_metrics_layer, create_fields=False)

Expand Down
16 changes: 9 additions & 7 deletions scripts/automation/FargateRME.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ rscli download $VBET_DIR --id $VBET_ID \
--file-filter "(vbet\.gpkg|intermediates\.gpkg)" \
--no-input --no-ui --verbose

# rscli download $CONFINEMENT_DIR --id $CONFINEMENT_ID \
# --file-filter "(confinement\.gpkg)" \
# --no-input --no-ui --verbose
rscli download $CONFINEMENT_DIR --id $CONFINEMENT_ID \
--file-filter "(confinement\.gpkg)" \
--no-input --no-ui --verbose

rscli download $ANTHRO_DIR --id $ANTHRO_ID \
--file-filter "(anthro\.gpkg)" \
Expand All @@ -101,9 +101,9 @@ rscli download $RCAT_DIR --id $RCAT_ID \
--file-filter "(rcat\.gpkg)" \
--no-input --no-ui --verbose

# rscli download $BRAT_DIR --id $BRAT_ID \
# --file-filter "(brat\.gpkg)" \
# --no-input --no-ui --verbose
rscli download $BRAT_DIR --id $BRAT_ID \
--file-filter "(brat\.gpkg)" \
--no-input --no-ui --verbose

echo "====================== Initial Disk space usage ======================="
df -h
Expand All @@ -125,8 +125,10 @@ try() {
$RS_CONTEXT_DIR/topography/dem.tif \
$RS_CONTEXT_DIR/topography/dem_hillshade.tif \
$RME_DIR \
--confinement_dgos $CONFINEMENT_DIR/outputs/confinement.gpkg/confinement_dgos \
--anthro_dgos $ANTHRO_DIR/outputs/anthro.gpkg/vwDgos \
--rcat_dgos $RCAT_DIR/outputs/rcat.gpkg/vwDgos \
--brat_dgos $BRAT_DIR/outputs/brat.gpkg/vwDgos \
--meta "Runner=Cybercastor" \
--verbose
if [[ $? != 0 ]]; then return 1; fi
Expand All @@ -135,7 +137,7 @@ try() {
cd /usr/local/src/riverscapes-tools/packages/rme
python3 -m rme.rme_rs \
$RME_DIR/project.rs.xml \
"$RS_CONTEXT_DIR/project.rs.xml,$VBET_DIR/project.rs.xml",$ANTHRO_DIR/project.rs.xml,$RCAT_DIR/project.rs.xml \
"$RS_CONTEXT_DIR/project.rs.xml,$VBET_DIR/project.rs.xml",$CONFINEMENT_DIR/project.rs.xml,$ANTHRO_DIR/project.rs.xml,$RCAT_DIR/project.rs.xml,$BRAT_DIR/project.rs.xml \
# Add these back in when we want confinement and brat: $CONFINEMENT_DIR/project.rs.xml,$BRAT_DIR/project.rs.xml
if [[ $? != 0 ]]; then return 1; fi

Expand Down

0 comments on commit 7859768

Please sign in to comment.