From 2add812edf5cb40961a38d26385a2969e81a9c25 Mon Sep 17 00:00:00 2001
From: Bock <abock@usgs.gov>
Date: Wed, 10 Apr 2024 16:27:46 -0600
Subject: [PATCH] minor mods in sink function

---
 workspace/R/non_dend.R | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)

diff --git a/workspace/R/non_dend.R b/workspace/R/non_dend.R
index b732596..c6f002b 100644
--- a/workspace/R/non_dend.R
+++ b/workspace/R/non_dend.R
@@ -10,7 +10,8 @@ dissolve_holes <- function(divides_poi){
   # HRU layer
   protoHRUs <- filter(divides_poi, !is.na(POI_ID)) %>%
     group_by(POI_ID) %>%
-    summarize(do_union = T)
+    summarize(do_union = T) %>%
+    clean_geometry(., ID = "POI_ID")
   
   agg_noholes <- sfheaders::sf_remove_holes(protoHRUs) %>%
     st_cast("MULTIPOLYGON")
@@ -678,7 +679,7 @@ NHD_sinks <- function(divides_poi, area_thresh, HUC12_table, NHD_sinks){
     filter(HUC_10 %in% divides_poi$HUC_10) %>%
     filter(HU_10_TYPE == "C" | HU_12_TYPE == "C")
   
-  # Determine which HUC12s have only one or less aggregated catcment
+  # Determine which HUC12s have only one or less aggregated catchment
   HUC12_poi <- filter(divides_poi, !is.na(POI_ID)) %>%
     distinct(POI_ID, HUC_12_int) %>%
     group_by(HUC_12_int) %>%
@@ -757,7 +758,7 @@ NHD_sinks <- function(divides_poi, area_thresh, HUC12_table, NHD_sinks){
       sink_cats_nhd <- filter(divides_poi, is.na(POI_ID)) %>%
         filter(grepl("-", .$member_COMID)) 
       
-      agg_cats_small <- filter(agg_cats_poly, area < min_da_km)
+      agg_cats_small <- filter(agg_cats_poly, area < area_thresh)
       
       if (nrow(sink_cats_small) > 0){
         # Get pertinent boundaries
@@ -797,21 +798,27 @@ NHD_sinks <- function(divides_poi, area_thresh, HUC12_table, NHD_sinks){
           mutate(POI_ID = ifelse(!is.na(POI_ID_new), POI_ID_new, POI_ID),
                  aggStep = ifelse(!is.na(POI_ID_new), "HUC12_sinks, small", aggStep)) %>%
           dplyr::select(-POI_ID_new)
+        
+        small_sinks <- select(sink_cats_nhd_table, member_COMID, HUC_12 = HUC_12_int, neighbor_COMID = bound_cat,
+                              neighbor_HUC12 = bound_cat_HUC12, agg_ID = bound_cat_POI_ID, agg_id, aggStep) %>%
+          filter(member_COMID %in% sink_cats_small$member_COMID, !member_COMID %in% sink_cats_final$member_COMID) %>%
+          select(-agg_id) %>%
+          filter(HUC_12 == neighbor_HUC12) %>%
+          mutate(aggStep = "HUC12_sinks, small") %>%
+          st_drop_geometry()
   
       }
     }
-    large_sinks <- select(sink_cats, member_COMID, HUC_12 = HUC_12_int, agg_ID = POI_ID_new, aggStep) %>%
+    large_sinks <- select(sink_cats, member_COMID, HUC_12 = HUC_12_int, POI_ID, aggStep) %>%
       st_drop_geometry()
     
-    small_sinks <- select(sink_cats_nhd_table, member_COMID, HUC_12 = HUC_12_int, neighbor_COMID = bound_cat,
-                                  neighbor_HUC12 = bound_cat_HUC12, agg_ID = bound_cat_POI_ID, agg_id, aggStep) %>%
-      filter(member_COMID %in% sink_cats_small$member_COMID, !member_COMID %in% sink_cats_final$member_COMID) %>%
-      select(-agg_id) %>%
-      filter(HUC_12 == neighbor_HUC12) %>%
-      mutate(aggStep = "HUC12_sinks, small") %>%
-      st_drop_geometry()
+    if(exists("small_sinks")){
+      sinks_table <- data.table::rbindlist(list(large_sinks, small_sinks), fill = TRUE) 
+    } else {
+      sinks_table <- large_sinks
+    }
+    
     
-    sinks_table <- data.table::rbindlist(list(large_sinks, small_sinks), fill = TRUE) 
   }
   return(list(divides_poi = divides_poi, sink_cats_table = sinks_table))
 }
-- 
GitLab