diff --git a/workspace/R/02_POI_creation_functions.R b/workspace/R/02_POI_creation_functions.R index c1438974b25d80e9cd77239c1275c9477f382e19..e4ed26080afd29fd50a9d98b3448783d54eacf2c 100644 --- a/workspace/R/02_POI_creation_functions.R +++ b/workspace/R/02_POI_creation_functions.R @@ -1546,3 +1546,215 @@ wb_inlet_collapse <- function(tmp_POIs, nhd, events, gages){ return(list(POIs = tmp_POIs, events_ret = NA)) } } + +#' @param tmp_POIs POIs from previous step +#' @param nid_points national inventory of dams data +#' @param flowline flowlines to attach pois to +#' @param poi_name name for poi set +create_nid_pois <- function(tmp_POIs, nid_points, flowline, poi_name) { + # Read in NID shapefile + NID_COMIDs <- nid_points %>% + st_drop_geometry() %>% + filter(EROM != 0, FlowLcomid %in% filter(flowline, dend ==1)$COMID) %>% + rename(COMID = FlowLcomid) %>% + switchDiv(., flowline) %>% + group_by(COMID) %>% + summarize(Type_NID = paste0(unique(NIDID), collapse = " ")) + + # Derive other NID POIs + tmp_POIs <- POI_creation(NID_COMIDs, flowline, poi_name) %>% + addType(., tmp_POIs, poi_name, nexus = FALSE, bind = FALSE) + + list(tmp_POIs = tmp_POIs) +} + +#' @param tmp_POIs POIs from previous step +#' @param flowline flowlines to attach pois to +#' @param all_nhdplus_attributes original attributes from nhdplusv2 +#' @param min_da_km_hw minimum drainage area for creation of a headwater poi +#' @param poi_name name for poi set +create_headwater_pois <- function(tmp_POIs, flowline, + all_nhdplus_attributes, + min_da_km_hw, poi_name = "DA") { + + # derive incremental segments from POIs + inc_segs <- make_incremental_segments(flowline, + filter(st_drop_geometry(flowline), + COMID %in% tmp_POIs$COMID)) %>% + # bring over VAA data + inner_join(select(all_nhdplus_attributes, COMID, + DnHydroseq, VA_MA, TOTMA, LENGTHKM, MAXELEVSMO, + MINELEVSMO, WBAREACOMI, WBAreaType, FTYPE, StartFlag, + AreaSqKM, TotDASqKM), by = "COMID") + + hw_segs <- inc_segs %>% + group_by(POI_ID) %>% + filter(any(StartFlag == 1)) %>% + filter(any(TotDASqKM > max_da_km_hw)) %>% + ungroup() + + att_group <- function(a, athres) { + #cumsum <- 0 + group <- 1 + result <- numeric() + for (i in 1:length(a)) { + #cumsum <- cumsum + a[i] + tot_DA <- a[i] + if (tot_DA > athres) { + group <- group + 1 + athres <- athres + athres + } + result = c(result, group) + } + return (result) + } + + #TODO Add magic numbers to config file + hw_DA_splits <- hw_segs %>% + st_drop_geometry() %>% + #filter(.data$ID %in% cat$ID) %>% + group_by(POI_ID) %>% + arrange(-Hydroseq) %>% + mutate(ind = att_group(TotDASqKM, min_da_km_hw)) %>% + ungroup() %>% + group_by(POI_ID, ind) %>% + mutate(total_length = cumsum(LENGTHKM)) %>% + ungroup() %>% + group_by(POI_ID, ind) %>% + mutate(set = cur_group_id()) %>% + filter(TotDASqKM == max(TotDASqKM) & + total_length > 5) %>% + mutate(da_class = paste(POI_ID, ind, sep = "_")) %>% + ungroup() %>% + select(-ind) %>% + filter(!COMID %in% tmp_POIs$COMID) + + if(nrow(hw_DA_splits) > 0) { + tmp_POIs <- POI_creation(select(hw_DA_splits, COMID, da_class), + filter(flowline, poi == 1), poi_name) %>% + addType(., tmp_POIs, poi_name, nexus = TRUE) + } + + list(tmp_POIs = tmp_POIs) + +} + +#' @param tmp_POIs POIs from previous step +#' @param flowline flowlines to attach pois to +#' @param all_nhdplus_attributes original attributes from nhdplusv2 +#' @param poi_name name for poi set +create_elevation_break_pois <- function(tmp_POIs, flowline, all_nhdplus_attributes, + poi_name = "elev") { + inc_segs <- make_incremental_segments(filter(flowline, minNet == 1), + filter(st_drop_geometry(flowline), + COMID %in% tmp_POIs$COMID)) %>% + # bring over VAA data + inner_join(select(all_nhdplus_attributes, COMID, + DnHydroseq, VA_MA, TOTMA, LENGTHKM, MAXELEVSMO, + MINELEVSMO, WBAREACOMI, WBAreaType, FTYPE, StartFlag, + AreaSqKM, TotDASqKM), by = "COMID") + + elev_fp <- inc_segs %>% + group_by(POI_ID) %>% + arrange(Hydroseq) %>% + # Get elevation info + mutate(MAXELEVSMO = na_if(MAXELEVSMO, -9998), MINELEVSMO = na_if(MINELEVSMO, -9998), + elev_diff_seg = max(MAXELEVSMO) - min(MINELEVSMO), + total_length = cumsum(LENGTHKM)) %>% + filter((max(MINELEVSMO) - min(MINELEVSMO)) > elev_diff) %>% + mutate(inc_elev_diff = c(MINELEVSMO[1], (MINELEVSMO - lag(MINELEVSMO))[-1])) %>% + mutate(inc_elev_diff = ifelse(inc_elev_diff == MINELEVSMO, 0, inc_elev_diff)) %>% + ungroup() + + if(nrow(elev_fp) > 0){ + #TODO Add magic numbers to config file + hw_elev_splits <- elev_fp %>% + st_drop_geometry() %>% + group_by(POI_ID) %>% + arrange(-Hydroseq) %>% + mutate(ind = cs_group(inc_elev_diff, elev_diff/2)) %>% + ungroup() %>% + group_by(POI_ID, ind) %>% + filter(TotDASqKM == max(TotDASqKM) & + total_length > 5) %>% + mutate(elev_class = paste(POI_ID, ind, sep = "_")) %>% + ungroup() %>% + select(-ind) %>% + filter(!COMID %in% tmp_POIs$COMID) + + if(nrow(hw_elev_splits) > 0) { + tmp_POIs <- POI_creation(select(hw_elev_splits, COMID, elev_class), + filter(flowline, poi == 1), + poi_name) %>% + addType(., tmp_POIs, poi_name, nexus = TRUE) + } + } + + list(tmp_POIs = tmp_POIs) +} + +cs_group <- function(a, athres) { + cumsum <- 0 + group <- 1 + result <- numeric() + for (i in 1:length(a)) { + cumsum <- cumsum + a[i] + if (cumsum > athres) { + group <- group + 1 + cumsum <- a[i] + } + result = c(result, group) + } + return (result) +} + +#' @param tmp_POIs POIs from previous step +#' @param flowline flowlines to attach pois to +#' @param all_nhdplus_attributes original attributes from nhdplusv2 +#' @param poi_name name for poi set +create_time_of_travel_pois <- function(tmp_POIs, flowline, all_nhdplus_attributes, poi_name = "Travel") { + + # derive incremental segments from POIs + inc_segs <- make_incremental_segments(flowline, + filter(st_drop_geometry(flowline), + COMID %in% tmp_POIs$COMID, + COMID %in% flowline$COMID)) %>% + # bring over VAA data + inner_join(select(all_nhdplus_attributes, COMID, DnHydroseq, VA_MA, TOTMA, LENGTHKM, + MAXELEVSMO, MINELEVSMO, WBAREACOMI, WBAreaType, FTYPE, + AreaSqKM, TotDASqKM), by = "COMID") + + # TT POIs + tt_pois_split <- inc_segs %>% + arrange(-Hydroseq) %>% + # Should we substitute with a very small value + mutate(VA_MA = ifelse(VA_MA < 0, NA, VA_MA)) %>% + mutate(FL_tt_hrs = (LENGTHKM * ft_per_km)/ VA_MA / s_per_hr ) %>% + group_by(POI_ID) %>% + filter(sum(FL_tt_hrs) > travt_diff, max(TotDASqKM) > max_elev_TT_DA) %>% + mutate(cum_tt = cumsum(FL_tt_hrs), + hrs = sum(FL_tt_hrs)) + + #TODO Add magic numbers to config file + tt_splits <- tt_pois_split %>% + st_drop_geometry() %>% + group_by(POI_ID) %>%ave + arrange(-Hydroseq) %>% + mutate(ind = cs_group(FL_tt_hrs, travt_diff * 0.75)) %>% + ungroup() %>% + group_by(POI_ID, ind) %>% + filter(TotDASqKM == max(TotDASqKM)) %>% + mutate(tt_class = paste(POI_ID, ind, sep = "_")) %>% + ungroup() %>% + select(-ind) %>% + filter(!COMID %in% tmp_POIs$COMID) + + if(nrow(tt_splits) > 0) { + tmp_POIs <- POI_creation(select(tt_splits, COMID, tt_class), + filter(flowline, poi == 1), poi_name) %>% + addType(., tmp_POIs, poi_name, nexus = TRUE) + } + + list(tmp_POIs = tmp_POIs) + +} \ No newline at end of file diff --git a/workspace/_targets/02_POI_creation_targets.R b/workspace/_targets/02_POI_creation_targets.R index 52eb325585cd988b9bba77ffeb6144d51f752d59..f6c19d43be425ce89ab4bab571b2f86ca49947a4 100644 --- a/workspace/_targets/02_POI_creation_targets.R +++ b/workspace/_targets/02_POI_creation_targets.R @@ -2,8 +2,7 @@ library(targets) tar_option_set(packages = c("dplyr", "sf", "hyfabric", "hydroloom", "nhdplusTools"), memory = "transient", garbage_collection = TRUE, error = "null", - storage = "worker", retrieval = "worker", deployment = "main", - debug = "wb_inlet_pois_0a67251eed4e40f7") + storage = "worker", retrieval = "worker", deployment = "main") library(future) library(future.callr) @@ -26,6 +25,8 @@ list(tar_target(data_paths_file, "cache/data_paths.json", format = "file"), ### Base flowline -- gets updated attributes tar_target(flowline, read_sf(nav_gpkg, nhd_flowline), pattern = map(nav_gpkg), deployment = "worker"), + tar_target(all_nhdplus_attributes, + sf::st_drop_geometry(sf::read_sf(data_paths$nhdplus_gdb, "NHDFlowline_Network"))), ### huc12 pois tar_target(huc12_poi, create_hu12_pois(read_sf(data_paths$hu12_points_path, "hu_points"), @@ -122,5 +123,30 @@ list(tar_target(data_paths_file, "cache/data_paths.json", format = "file"), wb_outlet_pois$all_events, updated_flowline_confluence, gages, data_paths, proj_crs, "WBIn"), - pattern = map(confluence_pois, wb_outlet_pois, updated_flowline_confluence), deployment = "worker") - ) \ No newline at end of file + pattern = map(confluence_pois, wb_outlet_pois, updated_flowline_confluence), deployment = "worker"), + + tar_target(nid_points, read_sf(data_paths$NID_points_path, "Final_NID_2018")), + tar_target(nid_pois, create_nid_pois(wb_inlet_pois$tmp_POIs, + nid_points, + updated_flowline_confluence, + "NID"), + pattern = map(wb_inlet_pois, updated_flowline_confluence)), + + tar_target(headwater_pois, create_headwater_pois(nid_pois$tmp_POIs, + updated_flowline_confluence, + all_nhdplus_attributes, + min_da_km_hw, "DA"), + pattern = map(nid_pois, updated_flowline_confluence), deployment = "worker"), + + tar_target(elevation_break_pois, create_elevation_break_pois(headwater_pois$tmp_POIs, + updated_flowline_confluence, + all_nhdplus_attributes, + "elev"), + pattern = map(headwater_pois, updated_flowline_confluence), deployment = "worker"), + + tar_target(time_of_travel_pois, create_time_of_travel_pois(elevation_break_pois$tmp_POIs, + updated_flowline_confluence, + all_nhdplus_attributes, + "Travel"), + pattern = map(elevation_break_pois, updated_flowline_confluence), deployment = "worker") + ) \ No newline at end of file diff --git a/workspace/workflow_runner.R b/workspace/workflow_runner.R index 7716fa5dc2184bf69a2c5599510b4e5538e7cbf1..090e00a77e60da7468df00fdd368d1f4a514b1fb 100644 --- a/workspace/workflow_runner.R +++ b/workspace/workflow_runner.R @@ -59,3 +59,8 @@ tar_make_future(ar_event_pois, workers = 8) tar_make_future(terminal_pois, workers = 8) tar_make_future(updated_flowline_confluence, workers = 8) tar_make_future(wb_inlet_pois, workers = 8) +tar_make_future(nid_pois, workers = 8) +tar_make_future(headwater_pois, workers = 8) +tar_make_future(elevation_break_pois, workers = 8) +tar_make_future(time_of_travel_pois, workers = 8) +