From b2e9d24ad978d57887f3595314900839430d7bdd Mon Sep 17 00:00:00 2001
From: mikejohnson51 <jmj00@ucsb.edu>
Date: Tue, 28 May 2024 10:00:17 -0600
Subject: [PATCH] conus-ref-fab: update POIs from GS

---
 runners/02_reference_to_parquet.R  |  7 +++++--
 runners/03_update_hydrolocations.R | 21 +++++++++++++++++----
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/runners/02_reference_to_parquet.R b/runners/02_reference_to_parquet.R
index a48b542..7cd0d9b 100644
--- a/runners/02_reference_to_parquet.R
+++ b/runners/02_reference_to_parquet.R
@@ -17,7 +17,7 @@ ref_hl_out     <- glue('/Users/mjohnson/hydrofabric/v2.2/reference/conus_hydrolo
 fl = as_sqlite(ref_gpkg, 'reference_flowline') %>% 
   select(id = comid, 
          toid = tocomid, 
-         poi_id = POI_ID, 
+         #poi_id = POI_ID, 
          terminalpa, 
          mainstemlp = levelpathi, 
          vpuid, 
@@ -66,13 +66,16 @@ hl = as_sqlite(ref_gpkg, 'poi_data') %>%
 filter(hl, hl_reference == "Gages", hl_link == "06752260") %>% 
   st_as_sf(coords = c("X", "Y"), crs = 5070) %>% 
   mapview::mapview()
+
+filter(net, hl_uri == "Gages-06752260")
+filter(hl,  hl_uri == "Gages-06752260")
   
 net = st_drop_geometry(div) %>% 
   mutate(id = divide_id, vpuid = NULL) %>% 
   full_join(st_drop_geometry(select(fl, -areasqkm)), by = "id") %>% 
   mutate(hf_id = id,  topo = "fl-fl") %>% 
   left_join(select(hl, -vpuid, -X, -Y),
-            by = "poi_id",
+            by = "id",
             relationship = "many-to-many")
 
 mutate(div,
diff --git a/runners/03_update_hydrolocations.R b/runners/03_update_hydrolocations.R
index 558665c..5bff294 100644
--- a/runners/03_update_hydrolocations.R
+++ b/runners/03_update_hydrolocations.R
@@ -75,6 +75,10 @@ gfv20 = open_dataset(glue("{source}/v2.2/reference/conus_hydrolocations")) %>%
   relocate(all_of(schema)) %>% 
   distinct() 
 
+filter(gfv20, hl_reference == "Gages", hl_link == "06752260") %>% 
+  st_as_sf(coords = c("X", "Y"), crs = 5070) %>% 
+  mapview::mapview()
+
 # RouteLink ---------------------------------------------------------------
 
 rl_file = glue('/Users/mjohnson/hydrofabric/RouteLink_CONUS_309.nc')
@@ -321,7 +325,7 @@ lid <- read_sf(glue('{source}/nws_lid.gpkg')) |>
 
 # Final  ------------------------------------------------------------------
 
-hl = rbindlist(list(rl_pois,
+hl_fin = rbindlist(list(rl_pois,
                     nwmlake_pois,
                     nwm_res,
                     calib_poi,
@@ -341,16 +345,25 @@ hl = rbindlist(list(rl_pois,
   left_join(distinct(select(ref_net, hf_id, mainstemlp, vpuid)), 
             by = "hf_id",
             relationship = "many-to-many") %>% 
-  distinct()
+  distinct() %>% 
+  mutate(hl_uri = paste0(hl_reference, "-", hl_link))
+
+filter(hl_fin, hl_reference == "Gages", hl_link == "06752260") %>% 
+  st_as_sf(coords = c("X", "Y"), crs = 5070) %>% 
+  mapview::mapview()
+
+filter(hl_fin, hl_uri == 'Gages-06752260')
 
 # Write Hydrolocation Table
-hl %>% 
+hl_fin %>% 
   group_by(vpuid) %>% 
   arrow::write_dataset(glue("{source}/conus_hl"), version = 2.6)
 
 "aws s3 sync /Users/mjohnson/hydrofabric/conus_hl s3://lynker-spatial/hydrofabric/conus_hl" %>% 
   system()
 
-open_dataset(glue("{source}/conus_hl")) %>% collect()
+open_dataset(glue("{source}/conus_hl")) %>% 
+  filter(hl_uri == 'Gages-06752260') %>% 
+  collect()