Skip to content
Snippets Groups Projects
config.R 5.17 KiB
Newer Older
  • Learn to ignore specific revisions
  • library(nhdplusTools)
    library(dplyr)
    library(sf)
    library(hyRefactor)
    library(tidyr)
    library(hyfabric)
    
    
    Blodgett, David L.'s avatar
    Blodgett, David L. committed
    if(nchar(Sys.getenv("RPU_CODE")) == 3) {
      rpu_code <- Sys.getenv("RPU_CODE")
    }
    
    
    Blodgett, David L.'s avatar
    Blodgett, David L. committed
    if(exists("rpu_code") && !exists("VPU")) {
    
      VPU <- substr(rpu_code, 1, 2)
    
    Blodgett, David L.'s avatar
    Blodgett, David L. committed
      
      if(rpu_code %in% c("10a", "10b", "10c", "10d")) {
        VPU <- "10L"
      }
      
      if(rpu_code %in% c("10e", "10f", "10g", "10h", "10i")) {
        VPU <- "10U"
      }
    
    Blodgett, David L.'s avatar
    Blodgett, David L. committed
      
      if(rpu_code == "03g") {
        VPU = "08"
      }
    
    Blodgett, David L.'s avatar
    Blodgett, David L. committed
    }
    
    # NHDPlus V2 Region
    if(!exists("VPU") && !nchar(VPU <- Sys.getenv("HYDREG")) > 1) 
      VPU <- "01"
    
    if(!exists("rpu_code") && !nchar(rpu_code <- Sys.getenv("RPU_CODE")) == 3) 
      rpu_code <- "01a"
    
    
    if(!nchar(out_gpkg <- Sys.getenv("OUT_GPKG")) > 5) 
      out_gpkg <- file.path("cache", paste0(rpu_code, ".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"))
    
    
    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"))
    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
    # Defined at various points
    raw_wbd <- paste0("HU12_", VPU) # Raw HUC12
    gage_loc <- paste0("GageLoc_", VPU) # HI GageLoc
    TE <- paste0("TE_", VPU) # Thermo-electric 
    NID <- paste0("NID_", VPU) # Raw NID points
    gage_events <- paste0("gage_events_", VPU) # gages with flowline index information for possible splitting
    
    
    # Defined during NHD_Navigate.Rmd
    nav_gpkg <- file.path("cache", paste0("GF_", VPU,".gpkg"))
    
    Bock, Andy's avatar
    Bock, Andy committed
    nhd_flowline <- "nhd_flowline"
    nhd_catchment <- "nhd_catchment"
    
    Bock, Andy's avatar
    Bock, Andy committed
    xwalk_layer <- paste0("HUC12_nhd_", VPU) # HUC12 - nhdcat crosswalk, built in Nav for VPU 20
    
    Bock, Andy's avatar
    Bock, Andy committed
    nav_poi_layer <- paste0("pois_", VPU) # Rolling Nav POI layer added to/modified througout nav workflow
    
    Bock, Andy's avatar
    Bock, Andy committed
    WBs_layer <-  paste0("WB_", VPU) # Waterbodies within VPU
    
    poi_moved <- paste0("POIs_mv_", VPU) # POIs moved from original COMID assignment
    
    Bock, Andy's avatar
    Bock, Andy committed
    inc_pois <- paste0("inc_POIs_", VPU) # layer of POIs used to derive first cut of segments
    
    Bock, Andy's avatar
    Bock, Andy committed
    nsegments_layer <- paste0("nsegment_", VPU) # Minimally-sufficient network dissolved by POI_ID
    
    Bock, Andy's avatar
    Bock, Andy committed
    pois_all <- paste0("POIs_", VPU) # All POIs binded together
    poi_xWalk <- paste0("poi_xWalk_", VPU) # POIs that changed COMIDS during the navigate part of the workflow
    
    Bock, Andy's avatar
    Bock, Andy committed
    final_poi_layer <- paste0("final_POIS_", VPU)
    
    pois_merge <- paste0("merPOIs_", VPU) # All POIs binded together
    
    
    Bock, Andy's avatar
    Bock, Andy committed
    # Defined during Refactor
    rpu <- paste0("rpu_", rpu_code)
    split_meters <- 10000
    combine_meters <- 1000
    min_da_km <- 20
    nhd_outlet <- "nhd_outlet"
    nhd_catchment <- "nhd_catchment"
    
    full_catchment_layer <- "full_catchment"
    
    Bock, Andy's avatar
    Bock, Andy committed
    refactored_layer <- "collapsed"
    reconciled_layer <- "reconciled"
    gf_outlets_layer <- "gf_outlets"
    divide_layer <- "divides"
    refactor_table <- "refactor_lookup"
    agg_fline_layer <- "agg_fline"
    agg_cats_layer <- "agg_cats"
    outlets_layer <- "outlets"
    lookup_VPU <- paste0(VPU, "_lookup")
    
    
    Bock, Andy's avatar
    Bock, Andy committed
    # Defined during Nondend
    ND_gpkg <- file.path("cache", paste0("NHD_", VPU,".gpkg"))
    
    Bock, Andy's avatar
    Bock, Andy committed
    
    
    
    # Making an "events" script to address "events" information
    #     i.e. gages, NID, TE Plants, waterbodies
    gage_gpkg <- "cache/Gage_info.gpkg"
    gage_cand <- "Potential_Gages"
    VPU_gage_Table <- file.path("cache", paste0("R", VPU, "_Gages.csv"))
    CONUS_gage_Table <- "data/gages_MDA.rds"
    
    # Defined during NonDend.Rmd
    fin_gpkg <- file.path("cache", paste0(VPU, "_final.gpkg"))
    lookup_miss <- paste0("lookup_missing_", VPU)
    missing_cats <- paste0("miss_cats_", VPU)
    protoHRUs <- paste0("poi_cats_", VPU)
    
    cat_boundary <- paste0("boundaries_", VPU)
    
    
    gf_gpkg <- nav_gpkg
    poi_layer <- paste0("POIs_", VPU)
    
    
    Bock, Andy's avatar
    Bock, Andy committed
    
    
    
    # parallel factor for catchment reconciliation.
    para_reconcile <- 2
    para_split_flines <- 2
    
    data_paths <- jsonlite::read_json(file.path("cache", "data_paths.json"))
    nhdplus_path(data_paths$nhdplus_gdb)
    staged_nhd <- stage_national_data(nhdplus_data = data_paths$nhdplus_gdb, 
                                      output_path = data_paths$nhdplus_dir)
    
    # Output Layers
    
    Bock, Andy's avatar
    Bock, Andy committed
    
    
    nhd_outlet <- "nhd_outlet"
    nhd_catchment <- "nhd_catchment"
    full_catchment <- "full_catchment"
    refactored_layer <- "collapsed"
    reconciled_layer <- "reconciled"
    gf_outlets_layer <- "gf_outlets"
    divide_layer <- "divides"
    refactor_table <- "refactor_lookup"
    agg_fline_layer <- "agg_fline"
    agg_cats_layer <- "agg_cats"
    outlets_layer <- "outlets"
    lookup_VPU <- paste0(VPU, "_lookup")
    
    # Manually verified
    crs_out <- st_crs('PROJCS["NAD83 / Conus Albers",GEOGCS["NAD83",DATUM["North American Datum 1983",SPHEROID["GRS 1980",6378137.0,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[1.0,1.0,-1.0,0.0,0.0,0.0,0.0],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0.0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.017453292519943295],AXIS["Geodetic longitude",EAST],AXIS["Geodetic latitude",NORTH],AUTHORITY["EPSG","4269"]],PROJECTION["Albers Equal Area",AUTHORITY["EPSG","9822"]],PARAMETER["central_meridian",-96.0],PARAMETER["latitude_of_origin",23.0],PARAMETER["standard_parallel_1",29.5],PARAMETER["false_easting",0.0],PARAMETER["false_northing",0.0],PARAMETER["standard_parallel_2",45.5],UNIT["m",1.0],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","5070"]]')