From bfad51dbd22762fb2e6c9f5c4cf8bcda05b5f548 Mon Sep 17 00:00:00 2001
From: David Blodgett <dblodgett@usgs.gov>
Date: Tue, 10 Dec 2024 08:41:40 -0600
Subject: [PATCH] reconcile divides

---
 workspace/R/03_refactor_functions.R           | 23 +++++++++++++++++
 workspace/_targets/03_refactor_targets.R      | 25 +++++++++++++++++--
 .../{workflow_runner.R => _targets_runner.R}  |  0
 3 files changed, 46 insertions(+), 2 deletions(-)
 rename workspace/{workflow_runner.R => _targets_runner.R} (100%)

diff --git a/workspace/R/03_refactor_functions.R b/workspace/R/03_refactor_functions.R
index 5111bda..e4c227d 100644
--- a/workspace/R/03_refactor_functions.R
+++ b/workspace/R/03_refactor_functions.R
@@ -165,4 +165,27 @@ create_refactor_lookup <- function(refactored_network, flowline, events_refactor
   # }
   
   out <- list(lookup = lookup_table, outlets = outlets_ref_COMID, dups = check_dups_poi)  
+}
+
+reconcile_divides <- function(cats, refactored_network, fdr_fac, para_reconcile, cache_split) {
+  
+  divides <- reconcile_catchment_divides(catchment = cats,
+                                         fline_ref = refactored_network$refactored,
+                                         fline_rec = refactored_network$reconciled,
+                                         fdr = fdr_fac$fdr,
+                                         fac = fdr_fac$fac,
+                                         para = para_reconcile,
+                                         cache = cache_split,
+                                         keep = NULL)
+  
+  load(cache_split)
+  
+  split_cats <- bind_rows(split_cats[lengths(split_cats) > 0])
+  
+  split_cats$areasqkm <- as.numeric(units::set_units(sf::st_area(split_cats), "km^2"))
+  
+  split_cats <- sf::st_transform(split_cats, proj_crs)
+  
+  list(divides = divides, split_cats = split_cats)
+  
 }
\ No newline at end of file
diff --git a/workspace/_targets/03_refactor_targets.R b/workspace/_targets/03_refactor_targets.R
index 3e6fadd..c7c417f 100644
--- a/workspace/_targets/03_refactor_targets.R
+++ b/workspace/_targets/03_refactor_targets.R
@@ -29,6 +29,7 @@ list(tar_target(data_paths_file, "cache/data_paths.json", format = "file"),
      
      tar_target(flowline, sf::st_zm(sf::read_sf(rpu_gpkg, nhd_flowline)),
                 pattern = map(rpu_gpkg), deployment = "worker"),
+
      tar_target(pois, sf::read_sf(vpu_gpkg, poi_geometry_table),
                 pattern = map(vpu_gpkg), deployment = "worker"),
      tar_target(pois_data, sf::read_sf(vpu_gpkg, poi_data_table),
@@ -48,6 +49,26 @@ list(tar_target(data_paths_file, "cache/data_paths.json", format = "file"),
      tar_target(refactor_lookup, create_refactor_lookup(refactored_network, flowline, 
                                                         refactor_input$events_refactor,
                                                         refactor_input$outlets), 
-                pattern = map(refactored_network, flowline, refactor_input), deployment = "worker")
+                pattern = map(refactored_network, flowline, refactor_input), deployment = "worker"),
+     
+     tar_target(fdr_fac, list(fdr = data_paths$fdr[[paste0("rpu_", rpu_codes)]], 
+                              fac = data_paths$fac[[paste0("rpu_", rpu_codes)]]),
+                pattern = rpu_codes, iteration = "list"),
+     
+     tar_target(raster_crs, terra::crs(terra::rast(fdr_fac[[1]]$fdr))),
+     
+     tar_target(cats, sf::st_transform(sf::read_sf(rpu_gpkg, nhd_catchment), raster_crs),
+                pattern = map(rpu_gpkg), deployment = "worker"),
+     
+     tar_target(cache_split, file.path("cache", paste0(rpu_codes, ".rda")),
+                pattern = map(rpu_codes)),
+     
+     tar_target(divides, reconcile_divides(cats,
+                                           refactored_network,
+                                           fdr_fac,
+                                           para_reconcile,
+                                           cache_split),
+                pattern = map(cats, refactored_network, fdr_fac, cache_split),
+                deployment = "worker")
      
-     )
\ No newline at end of file
+)
\ No newline at end of file
diff --git a/workspace/workflow_runner.R b/workspace/_targets_runner.R
similarity index 100%
rename from workspace/workflow_runner.R
rename to workspace/_targets_runner.R
-- 
GitLab