From 12b2dac74709b495d612e27d48e50f9fdb8dabec Mon Sep 17 00:00:00 2001
From: David Blodgett <dblodgett@usgs.gov>
Date: Tue, 24 May 2022 21:02:31 -0500
Subject: [PATCH] fixes #74

---
 workspace/00_enhd_nwm_network.Rmd |  4 ----
 workspace/01_NHD_prep.Rmd         | 12 ++++++++++++
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/workspace/00_enhd_nwm_network.Rmd b/workspace/00_enhd_nwm_network.Rmd
index de77a8b..a7450cd 100644
--- a/workspace/00_enhd_nwm_network.Rmd
+++ b/workspace/00_enhd_nwm_network.Rmd
@@ -49,10 +49,6 @@ RNetCDF::close.nc(nwm_nc)
   
 nhd_net <- nhdplusTools::get_vaa()
 
-degen_networks <- nhd_net[nhd_net$totdasqkm == 0 & nhd_net$terminalfl == 1, ]$terminalpa
-
-nhd_net <- filter(nhd_net, !terminalpa %in% degen_networks)
-
 names(nhd_net) <- tolower(names(nhd_net))
 names(enhd) <- tolower(names(enhd))
 
diff --git a/workspace/01_NHD_prep.Rmd b/workspace/01_NHD_prep.Rmd
index 790460e..1f977dd 100644
--- a/workspace/01_NHD_prep.Rmd
+++ b/workspace/01_NHD_prep.Rmd
@@ -44,6 +44,18 @@ fline <- sf::st_cast(fline, "LINESTRING")
 
 catchment <- sf::read_sf(file.path(data_paths$nhdplus_dir, "reference_catchments.gpkg"))
 
+# we can remove truely degenerate COMIDs 
+# for 0 upstream area and no catchment area
+degen_comid <- fline[fline$TotDASqKM == 0 & 
+                       !fline$COMID %in% catchment$featureid, ]$COMID
+
+# need to make sure we don't disconnect anything.
+keep_tocomid <- fline$toCOMID[!fline$COMID %in% degen_comid]
+
+if(length(degen_comid[degen_comid %in% keep_tocomid]) > 0) stop("this will break the network")
+
+fline <- fline[!fline$COMID %in% degen_comid, ]
+
 for(VPU in vpu_codes) {
   
   rm(rpu_code)
-- 
GitLab