Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to get variables from StreamCat and NHDplus VAA for site classification #89

Closed
leppott opened this issue May 15, 2023 · 2 comments
Labels

Comments

@leppott
Copy link
Owner

leppott commented May 15, 2023

Is your feature request related to a problem? Please describe.
A function to get variables to allow for stream classification for MMI and BCG models.

Describe the solution you'd like
Wrapper function for StreamCatTools and nhdplusTools packages.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Pacific NW model uses slope from NHDplus VAA and elevation from Stream Cat.

Example code below.

# Packages
library(StreamCatTools)
library(nhdplusTools)

# Directories
dn_data <- "data"
dn_results <- "results"

# Data
fn_test1 <- "StreamCat_Test1_Chad.csv"
df_test1 <- read.csv(file.path(dn_data, fn_test1))

fn_test2 <- "StreamCat_Test2_Rob.csv"
df_test2 <- read.csv(file.path(dn_data, fn_test2))

# Munge
## Combine files
df_test1[, "Source"] <- "Test_Chad"
df_test2[, "Source"] <- "Test_Rob"
names(df_test2) <- names(df_test1)
df_sites <- rbind(df_test1, df_test2)

# StreamCat
## COMID
### WGS 84
comid <- sc_get_comid(df_sites
                         , xcoord = "Longitude"
                         , ycoord = "Latitude"
                         , crsys = 4326)


# Add COMID to data
df_sites[, "COMID"] <- strsplit(comid, ",")

## elevation
df_sc <- sc_get_data(comid = paste(df_sites[, "COMID"], collapse = ",")
                      , metric = "elev")
# add elev to sites
df_results <- merge(df_sites
                    , df_sc
                    , by.x = "COMID"
                    , by.y = "COMID"
                    , all.x = TRUE)

# NHDplus
## download VAA
nhdplusTools_data_dir(file.path(dn_results)) # set dir
download_vaa(
    path = get_vaa_path()
    , force = FALSE
    , updated_network = FALSE)
# get_vaa_names() # VAA table names
vaa_names2get <- c("gnis_name"
                   , "ftype"
                   , "fcode"
                   , "streamorde"
                   , "lengthkm"
                   , "areasqkm"
                   , "totdasqkm"
                   , "slope"
                   , "slopelenkm")
nhdplus_vaa <- get_vaa(vaa_names2get)
## merge with sites_sc
df_results <- merge(df_results
                    , nhdplus_vaa
                    , by.x = "COMID"
                    , by.y = "comid"
                    , all.x = TRUE)

# Save
write.table(df_results
            , file = file.path(dn_results, "results_site_var.csv")
            , row.names = FALSE
            , col.names = TRUE
            , sep = ",")
@leppott
Copy link
Owner Author

leppott commented May 15, 2023

Need to account for downloading the VAA file (about 40 seconds). The example code downloads it separately. Can combine in the call to get_vaa.

@leppott
Copy link
Owner Author

leppott commented May 24, 2023

BCGcalc Shiny app includes the code for getting the StreamCatTools and nhdplusTools data and outputing the elevation and slope. Not sure need as a function.

leppott/BCGcalc@6a0ebb5

leppott/BCGcalc@83af05f

leppott/BCGcalc@dbccb2a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant