Skip to content
Snippets Groups Projects
config.R 4.38 KiB
Newer Older
library(nhdplusTools)
library(dplyr)
library(sf)
library(hyRefactor)
library(tidyr)
library(hyfabric)
library(mapview)
rpu_vpu <- readr::read_csv(system.file("extdata/rpu_vpu.csv", package = "hyfabric"), 
                           show_col_types = FALSE)
rpu_vpu_out <- readr::read_csv("cache/rpu_vpu_out.csv", 
                               col_types = c("c", "c", "i" , "i"), show_col_types = FALSE)
Blodgett, David L.'s avatar
Blodgett, David L. committed
if(nchar(Sys.getenv("HYDREG")) > 1) {
  VPU <- Sys.getenv("HYDREG")
}

if(!exists("rpu_code")) {
  if(exists("VPU")) {
    vpu <- VPU
Blodgett, David L.'s avatar
Blodgett, David L. committed
    rpu_codes <- rpu_vpu[rpu_vpu$vpuid %in% vpu, ]
    rpu_code <- rpu_codes$rpuid[1]
Blodgett, David L.'s avatar
Blodgett, David L. committed
  } else {
    vpu <- get_rpu_dependent_vars()
    rpu_code <- vpu$r
    vpu <- vpu$v
Bock, Andy's avatar
Bock, Andy committed
if(!exists("vpu")) {
  vpu <- get_rpu_dependent_vars(rpu_code)$v
if(!nchar(out_agg_gpkg <- Sys.getenv("OUT_GPKG")) > 5) 
  out_agg_gpkg <- file.path("cache", paste0(rpu_code, "_aggregate.gpkg"))

if(!nchar(out_refac_gpkg <- Sys.getenv("OUT_REFACTOR_GPKG")) > 5)
  out_refac_gpkg <- file.path("cache", paste0(rpu_code, "_refactor.gpkg"))

if(!nchar(cache_split <- Sys.getenv("CACHE_SPLIT")) > 5) 
  cache_split <- file.path("temp", paste0(rpu_code, ".rda"))

if(!nchar(lookup_table_file <- Sys.getenv("LOOKUP_TABLE")) > 5) 
  lookup_table_file <- file.path("cache", paste0(rpu_code, "_lookup.csv"))

if(!nchar(vpu_lookup_table_file <- Sys.getenv("VPU_LOOKUP_TABLE")) > 5) 
  vpu_lookup_table_file <- file.path("cache", paste0("lookup_", vpu, ".csv"))


Blodgett, David L.'s avatar
Blodgett, David L. committed
options(scipen = 9999)

# Source Data
crs <- 5070
data_paths <- jsonlite::read_json(file.path("cache", "data_paths.json"))

nhdplus_path(data_paths$nhdplus_gdb)
suppressWarnings(staged_nhd <- stage_national_data())

# Defined and used broadly
Blodgett, David L.'s avatar
Blodgett, David L. committed
nhd_flowline <- "reference_flowline"
nhd_catchment <- "reference_catchment"
nhd_outlet <- "nhd_outlet"
# Created and/or used during 02_Navigate
elev_diff <- 500 # Max difference in elevation within a single segment/POI
Bock, Andy's avatar
Bock, Andy committed
max_elev_TT_DA <- 10 # Min drainage area to consider for elevation/travel_time breaks
travt_diff <- 24 # Max number of hours between adjacent POIS
Blodgett, David L.'s avatar
Blodgett, David L. committed

xwalk_layer <- paste0("HUC12_nhd") # HUC12 - nhdcat crosswalk, built in Nav for VPU 20
Bock, Andy's avatar
Bock, Andy committed
nav_poi_layer <- paste0("POIs_tmp_", vpu) # Rolling Nav POI layer added to/modified througout nav workflow
WBs_layer <-  paste0("WB_", vpu) # Waterbodies within VPU
poi_moved_layer <- paste0("POIs_mv_", vpu) # POIs moved from original COMID assignment
nsegments_layer <- paste0("nsegment_", vpu) # Minimally-sufficient network dissolved by POI_ID
pois_all_layer <- paste0("POIs_", vpu) # All POIs binded together
poi_xwalk_layer <- paste0("poi_xwalk_layer_", vpu) # POIs that changed COMIDS during the navigate part of the workflow
Blodgett, David L.'s avatar
Blodgett, David L. committed
final_poi_layer <- "POIs"
# Settings for refactor workflow
Bock, Andy's avatar
Bock, Andy committed
split_meters <- 10000
combine_meters <- 1000
min_da_km <- 20
Bock, Andy's avatar
Bock, Andy committed
para_reconcile <- 2
para_split_flines <- 2
Bock, Andy's avatar
Bock, Andy committed
keep_cat_points <- 1
Blodgett, David L.'s avatar
Blodgett, David L. committed

Bock, Andy's avatar
Bock, Andy committed
# Created during refactor workflow
Bock, Andy's avatar
Bock, Andy committed
refactored_layer <- "collapsed"
Blodgett, David L.'s avatar
Blodgett, David L. committed
reconciled_layer <- "refactored_flowpaths"
split_layer <- "split_events"
Blodgett, David L.'s avatar
Blodgett, David L. committed
divide_layer <- "refactored_divides"
agg_fline_layer <- "aggregated_flowpaths"
agg_cats_layer <- "aggregated_divides"
Bock, Andy's avatar
Bock, Andy committed
outlets_layer <- "outlets"
Blodgett, David L.'s avatar
Blodgett, David L. committed
mapped_outlets_layer <- "mapped_POIs"
lookup_table_refactor <- "lookup_table"
Bock, Andy's avatar
Bock, Andy committed

Blodgett, David L.'s avatar
Blodgett, David L. committed
# output geopackage file names
nav_gpkg <- file.path("cache", paste0("reference_", vpu,".gpkg"))

rfc_gpkg <- file.path("cache", paste0("refactor_", vpu, ".gpkg"))

gf_gpkg <- file.path("cache", paste0("GF_", vpu, ".gpkg"))
gf_gpkg_conus <- "cache/reference_CONUS.gpkg"
# Defined during NonDend.Rmd
Bock, Andy's avatar
Bock, Andy committed
ND_gpkg <- file.path("cache", paste0("ND_", vpu,".gpkg"))
Blodgett, David L.'s avatar
Blodgett, David L. committed

divides_xwalk <- paste0("divides_nhd")
HRU_layer <- paste0("all_cats")
divides_nd <- paste0("divides_nd")
missing_terms <- paste0("miss_terms")
missing_cats <- paste0("miss_cats")
Bock, Andy's avatar
Bock, Andy committed


# only used in HI?
Bock, Andy's avatar
Bock, Andy committed
inc_pois_layer <- paste0("inc_POIs_", vpu) # layer of POIs used to derive first cut of segments
# Defined at various points for HI workflow
#raw_wbd <- paste0("HU12_", VPU) # Raw HUC12
#gage_loc <- paste0("GageLoc_", VPU) # HI GageLoc
#TE <- paste0("TE_", VPU) # Thermo-electric 
#NID_layer <- paste0("NID_", VPU) # Raw NID points
#gage_events_layer <- paste0("gage_events_", VPU) # gages with flowline index information for possible splitting

# not used?
# huc12_xWalk <- file.path(data_paths$nhdplus_dir, "CrosswalkTable_NHDplus_HU12.csv")
# HUC12 <- file.path(data_paths$nhdplus_dir, "HUC12.rds")
Bock, Andy's avatar
Bock, Andy committed