From ae940ff3f8705caacfe47d837b4669de6f59a2e9 Mon Sep 17 00:00:00 2001 From: Kevin See Date: Fri, 22 Nov 2024 15:39:36 -0800 Subject: [PATCH 1/2] simplified format of some code without changing functionality --- inst/test_scripts/prep_pkg_data.R | 384 +++++++++++------------------- 1 file changed, 137 insertions(+), 247 deletions(-) diff --git a/inst/test_scripts/prep_pkg_data.R b/inst/test_scripts/prep_pkg_data.R index ffab18d..30ba228 100644 --- a/inst/test_scripts/prep_pkg_data.R +++ b/inst/test_scripts/prep_pkg_data.R @@ -143,7 +143,7 @@ if(root_site_code == "LGR") { #----------------------------------------------------------------- if(root_site_code == "PRA") { configuration = org_config %>% - # # manually add site for Colockum Creek (not in PTAGIS) + # manually add site for Colockum Creek (not in PTAGIS) # bind_rows(tibble(site_code = 'CLK', # config_id = 100, # antenna_id = 'A1', @@ -154,180 +154,105 @@ if(root_site_code == "PRA") { # site_name = 'Colockum Creek', # antenna_group = 'Single Colockum Ck', # site_description = 'Temporary single antenna.', - # site_type_name = 'Instream Remote Detection System', - # rkm = '740.001', - # rkm_total = 741, - # # this puts CLK upstream of RIA - # latitude = 47.3707357269787, - # longitude = -120.25617371760839)) %>% - filter(!(site_code == 'WAN' & site_type == 'MRR'), - !(site_code == 'TMF' & site_type == 'MRR'), - !(site_code == 'PRO' & site_type == 'MRR')) %>% - mutate(node = if_else(site_code %in% c('RIA', 'RRF', 'WEA', 'PRV'), - site_code, - node)) %>% - mutate(node = if_else(site_code == 'PRDLD1', - 'PRA', - node)) %>% - mutate(node = if_else(site_code == "LWB", - "LWE_D", - node), - node = if_else(site_code %in% c('TUF', 'TUMFBY', 'TUM'), - 'TUM', - node), - node = if_else(site_code == 'LNF' & antenna_id %in% c('01'), - 'LNF_U', - node), - node = if_else(site_code == 'LEAV', - 'LNF_U', - node), - node = if_else(site_code == 'CHIWAC', - 'CHW_U', - node), - node = if_else(site_code == 'CHIWAR', - 'CHL_U', - node), - node = if_else(site_code == 'CHIKAC', - 'CHU_U', - node), - node = if_else(site_code == 'WHITER', - 'WTL_U', - node), - node = if_else(site_code == 'LWENAT', - 'LWN_U', - node), - node = if_else(site_code == 'NASONC', - 'NAL_U', - node), - # any fish seen at Dryden dam should also be seen at LWE - node = if_else(site_code == 'DRY', - 'LWE_U', - node), - node = if_else(site_code == 'RRJ', - 'RRF', - node), - node = if_else(site_code == "MAD" & config_id == 110 & antenna_id == "01", - "MAD_U", - node), - node = if_else(site_code == 'EHL', - case_when(config_id == 100 & antenna_id == '02' ~ "EHL_D", - config_id == 100 & antenna_id == '01' ~ "EHL_U", - config_id == 110 & antenna_id == '03' ~ "EHL_D", - config_id == 110 & antenna_id %in% c('01', '02') ~ "EHL_U"), - node), - node = if_else(site_code == "WEH", - if_else(antenna_id == "A2", - "WEH_D", - "WEH_U"), - node), - node = if_else(site_code == "LMB", - "LMR_D", - node), - node = if_else(site_code == 'LBC' & config_id == 100, - 'LBC_D', - node), - node = if_else(site_code == 'MRC', - 'MRC_D', - node), - node = if_else(site_code %in% c('SSC', '18N', 'MHB', 'M3R', 'MWF'), - 'MRC_U', - node), - node = if_else(site_code == 'MSH' & antenna_id == '00', - 'METH_D', - node), - node = if_else(site_code == 'METH', - 'METH_U', - node), - node = if_else(site_code == 'LLC' & config_id == 100, - if_else(antenna_id == 'D3', - 'LLC_D', - 'LLC_U'), - node), - node = if_else(site_code == 'ZSL', - if_else(grepl('Weir 3', antenna_group, ignore.case = T), - 'ZSL_D', - 'ZSL_U'), - node), - node = if_else(site_code == 'BPC' & config_id == 100, - if_else(antenna_id %in% c('C3'), - 'BPC_D', - 'BPC_U'), - node), - node = if_else(site_code == 'PRH', - case_when(antenna_id %in% c('F1', 'F2', 'F3', 'F4') ~ "PRH_D", - antenna_id %in% c('F5', 'F6', '01', '02') ~ "PRH_U"), - node), - node = if_else(site_code == 'PRO' & site_type == 'INT', - 'PRO_D', - node), - # grab all sites upstream of Prosser dam, and assign them to PRO_U - node = if_else(site_code != "PRO" & - as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,1]) == 539 & - as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,2]) >= 76, - "PRO_U", - node), - node = if_else(site_code == 'ICH', - 'ICH_D', - node), - node = if_else(grepl('522\\.', rkm) & rkm_total > 538, - 'ICH_U', - node), - node = if_else(site_code == 'MDR', - 'MDR_D', - node), - node = if_else(site_code %in% c('LWD', 'BGM', 'NBA', 'MCD'), - "MDR_U", - node), - node = if_else(site_code == 'HST', - 'HST_D', - node), - node = if_else(site_code %in% c('BBT', 'COP', 'PAT'), - 'HST_U', - node), - # node = if_else(site_code %in% c('30M', 'BR0', 'JDM', 'SJ1', 'SJ2', 'MJ1', 'RCJ'), - # 'JD1_U', - # node), - node = if_else(as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,1]) == 351, - "JD1_U", - node), - node = if_else(site_code == 'JD1', - 'JD1_D', - node), - node = if_else(site_code != 'JD1' & as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,1]) < 351, - 'JDA', - node)) %>% + # site_type_name = 'Instream Remote Detection System', + # rkm = '740.001', + # rkm_total = 741, + # # this puts CLK upstream of RIA + # latitude = 47.3707357269787, + # longitude = -120.25617371760839)) %>% + # this puts CLK on Colockum Creek, but between PRA and RIA + # latitude = 47.29722788926544, + # longitude = -120.10577913008702)) %>% + filter(!(site_code == 'WAN' & site_type == 'MRR'), + !(site_code == 'TMF' & site_type == 'MRR'), + !(site_code == 'PRO' & site_type == 'MRR')) %>% + mutate(across(node, + ~ case_when(site_code %in% c('RIA', 'RRF', 'WEA', 'PRV', 'PRH') ~ site_code, + site_code == 'PRDLD1' ~ "PRA", + . == "LWE" ~ "LWE_D", + site_code == "LWB" ~ "LWE_D", + # any fish seen at Dryden dam should also be seen at LWE + site_code == 'DRY' ~ "LWE_U", + site_code %in% c('TUF', 'TUMFBY', 'TUM') ~ "TUM", + site_code == 'LNF' & antenna_id %in% c('01', '02') ~ "LNF_U", + site_code == 'LNF' & antenna_id %in% c('03', '04') ~ "LNF_D", + site_code == 'LEAV' ~ "LNF_U", + site_code == 'ICL' & config_id == 100 ~ "ICL_D", + site_code == 'CHIWAC' ~ "CHW_U", + site_code == 'CHIWAR' ~ "CHL_U", + site_code == "CWT" ~ "CHL_U", + site_code == 'WHITER' ~ "WTL_U", + site_code == 'LWENAT' ~ "LWN_U", + site_code == 'NASONC' ~ "NAL_U", + # any fish seen at Chiwawa acclimation pond gets moved to CHL + site_code == 'CHP' ~ 'CHL_U', + site_code == 'EBO' ~ "EBO_D", + site_code == 'RRJ' ~ 'RRF', + site_code == "MAD" & config_id == 110 & antenna_id == "01" ~ "MAD_U", + site_code == 'EHL' & config_id == 100 & antenna_id == '02' ~ 'EHL_D', + site_code == 'EHL' & config_id == 100 & antenna_id == '01' ~ 'EHL_U', + site_code == 'EHL' & config_id == 110 & antenna_id == '03' ~ 'EHL_D', + site_code == 'EHL' & config_id == 110 & antenna_id %in% c('01', '02') ~ 'EHL_U', + # combine a couple sites in the Entiat + site_code %in% c("ENS", "ENM") ~ "ENA_U", + site_code == "WEH" & antenna_id == "A2" ~ "WEH_D", + site_code == "WEH" & antenna_id != "A2" ~ "WEH_U", + site_code == "LMB" ~ "LMR_D", + site_code == 'MRC' ~ 'MRC_D', + site_code %in% c('SSC', '18N', 'MHB', 'M3R', 'MWF') ~ 'MRC_U', + site_code == 'LLC' & config_id == 100 & antenna_id %in% c("D1", "D2") ~ "LLC_U", + # ZSL has definitive up/down antennas in initial configurations, but it gets more complicated after that + site_code == "ZSL" & + str_detect(antenna_group, + "(?i)Weir 3") & + config_id %in% c("100", "110") ~ "ZSL_D", + site_code == "ZSL" & + str_detect(antenna_group, + "(?i)Weir 2") & + config_id %in% c("100", "110") ~ "ZSL_U", + site_code == "ZSL" & + !config_id %in% c("100", "110") ~ "ZSL_D", + site_code == 'BPC' & + config_id == 100 & + antenna_id %in% c("C1", "C2") ~ "BPC_U", + site_code == 'BPC' & + config_id == 100 & + antenna_id %in% c("C3") ~ "BPC_D", + site_code == 'PRO' & site_type == 'INT' ~ 'PRO_D', + # grab all sites upstream of Prosser dam, and assign them to PRO_U + site_code != "PRO" & + as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,1]) == 539 & + as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,2]) >= 76 ~ "PRO_U", + site_code == 'ICH' ~ 'ICH_D', + str_detect(rkm, '522\\.') & rkm_total > 538 ~ 'ICH_U', + site_code == 'MDR' ~ 'MDR_D', + site_code %in% c('LWD', 'BGM', 'NBA', 'MCD') ~ 'MDR_U', + site_code == 'HST' ~ 'HST_D', + site_code %in% c('BBT', 'COP', 'PAT') ~ 'HST_U', + as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,1]) == 351 & + site_code != "JD1" ~ "JD1_U", + site_code == 'JD1' ~ 'JD1_D', + site_code != 'JD1' & + as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,1]) < 351 & + str_detect(site_code, "^COLR", negate = T) ~ 'JDA', + .default = node + ))) |> distinct() %>% # correct a couple rkm values - mutate(rkm = if_else(site_code == 'SA1', - '858.041.003', - rkm), - rkm_total = if_else(site_code == 'SA1', - 902, - rkm_total)) %>% - mutate(rkm = if_else(site_code == 'TON', - '858.133.001', - rkm), - rkm_total = if_else(site_code == 'TON', - 992, - rkm_total)) %>% - mutate(rkm = if_else(grepl('WEH', node), - '829.001', - rkm), - rkm_total = if_else(grepl('WEH', node), - 830, - rkm_total)) %>% - mutate(rkm = if_else(site_code == "MSH", - '843.082', - rkm), - rkm_total = if_else(site_code == "MSH", - 925, - rkm_total), - rkm = if_else(site_code == "METH", - '843.083', - rkm), - rkm_total = if_else(site_code == "METH", - 926, - rkm_total)) + mutate(across(rkm, + ~ case_when(site_code == 'SA1' ~ '858.041.003', + site_code == 'TON' ~ '858.133.001', + str_detect(node, "WEH") ~ '829.001', + site_code == "MSH" ~ '843.082', + site_code == "METH" ~ '843.083', + .default = .)), + across(rkm_total, + ~ case_when(site_code == 'SA1' ~ 902, + site_code == 'TON' ~ 992, + str_detect(node, "WEH") ~ 830, + site_code == "MSH" ~ 925, + site_code == "METH" ~ 926, + .default = .))) sites_df = writeOldNetworks()$PriestRapids |> select(site_code = SiteID, @@ -348,51 +273,32 @@ if(root_site_code == "PRA") { if(root_site_code == "PRO") { # customize some nodes based on DABOM framework configuration = org_config %>% - mutate(node = if_else(site_code %in% c('PRO'), - 'PRO', - node), - node = if_else(site_code %in% c("NFTEAN", "TEANAR", "TEANM", "TEANWF"), - "LMT_U", - node), - node = if_else(site_code == 'ROZ', - if_else(antenna_id %in% c('01', '02', '03'), - node, - as.character(NA)), - node), - node = if_else(site_code == 'TAN' & config_id %in% c(120, 130), - "TAN_D", - node), - node = if_else(site_code %in% c('MC1', 'MC2', 'MCJ', 'MCN'), - 'MCN', - node), - node = if_else(site_code == 'ICH', - 'ICH_D', - node), - node = if_else(grepl('522\\.', rkm) & rkm_total > 538, - 'ICH_U', - node), - node = if_else(site_code == 'JD1', - 'JD1_D', - node), - node = if_else(site_code %in% c('30M', 'BR0', 'JDM', 'SJ1', 'SJ2', 'MJ1'), - 'JD1_U', - node), - node = if_else(site_code != 'JD1' & as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,1]) < 351, - 'JDA', - node), - node = if_else(site_code == 'PRA', - 'PRA_D', - node), - node = if_else(site_code != 'PRA' & as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,1]) >= 639, - 'PRA_U', - node)) %>% + mutate( + across( + node, + ~ case_when(site_code %in% "PRO" ~ "PRO", + site_code %in% c("NFTEAN", "TEANAR", "TEANM", "TEANWF") ~ "LMT_U", + site_code == "ROZ" & !antenna_id %in% c('01', '02', '03') ~ NA_character_, + site_code == 'TAN' & config_id %in% c(120, 130) ~ "TAN_D", + site_code %in% c('MC1', 'MC2', 'MCJ', 'MCN') ~ 'MCN', + site_code == 'ICH' ~ 'ICH_D', + str_detect(rkm, '522\\.') & rkm_total > 538 ~ 'ICH_U', + as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,1]) == 351 & + site_code != "JD1" ~ "JD1_U", + site_code == 'JD1' ~ 'JD1_D', + site_code != 'JD1' & + as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,1]) < 351 & + str_detect(site_code, "^COLR", negate = T) ~ 'JDA', + site_code == 'PRA' ~ 'PRA_D', + site_code != 'PRA' & as.integer(stringr::str_split(rkm, '\\.', simplify = T)[,1]) >= 639 ~ 'PRA_U', + .default = .))) |> # add a missing lat/long - mutate(latitude = if_else(site_code == "SWK", - 47.210348, - latitude), - longitude = if_else(site_code == "SWK", - -120.699021, - longitude)) + mutate(across(latitude, + ~ case_when(site_code == "SWK" ~ 47.210348, + .default = .)), + across(longitude, + ~ case_when(site_code == "SWK" ~ -120.699021, + .default = .))) sites_df = writeOldNetworks()$Prosser |> select(site_code = SiteID, @@ -411,36 +317,20 @@ if(root_site_code == "PRO") { #----------------------------------------------------------------- if(root_site_code == "TUM") { configuration = org_config %>% - mutate(node = ifelse(site_code %in% c('LNF', 'LEAV'), - 'LNF', - node), - node = ifelse(site_code %in% c('TUF', 'TUMFBY', 'TUM'), - 'TUM', - node), - node = ifelse(site_code == 'CHIWAC', - 'CHW_U', - node), - node = ifelse(site_code == 'CHIWAR', - 'CHL_U', - node), - node = ifelse(site_code == 'CHIW', - 'CHL_U', - node), - node = ifelse(site_code == 'CHIKAC', - 'CHU_U', - node), - node = ifelse(site_code == 'WHITER', - 'WTL_U', - node), - node = ifelse(site_code == 'LWENAT', - 'LWN_U', - node), - node = ifelse(node == 'ICL', - 'ICL_D', - node), - node = ifelse(site_code == 'NASONC', - 'NAL_U', - node)) %>% + muate( + across( + node, + ~ case_when(site_code %in% c('LNF', 'LEAV') ~ 'LNF', + site_code %in% c('TUF', 'TUMFBY', 'TUM') ~ 'TUM', + site_code == 'CHIWAC' ~ 'CHW_U', + site_code %in% c('CHIWAR', 'CHIW') ~ 'CHL_U', + site_code == 'CHIKAC' ~ 'CHU_U', + site_code == 'WHITER' ~ 'WTL_U', + site_code == 'LWENAT' ~ 'LWN_U', + site_code == 'NASONC' ~ 'NAL_U', + .default = .) + ) + ) |> distinct() sites_df = writeOldNetworks()$Tumwater |> From 39e1af6393b9ec160f0ed24746ff622122aeddc6 Mon Sep 17 00:00:00 2001 From: Kevin See Date: Fri, 10 Jan 2025 15:56:55 -0800 Subject: [PATCH 2/2] corrected code that had capitalized "Hydroseq" when updates to other functions had made it "hydroseq" --- R/findDwnstrmSite.R | 6 +++--- man/findDwnstrmSite.Rd | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/findDwnstrmSite.R b/R/findDwnstrmSite.R index 26f0263..b5eca56 100644 --- a/R/findDwnstrmSite.R +++ b/R/findDwnstrmSite.R @@ -5,7 +5,7 @@ #' @author Kevin See #' #' @param site_id Site code for the starting detection site. -#' @param sites_joined An `sf` class object containing points of all detection sites. Must contain a column named `site_code` containing the site code of each site, and a column called Hydroseq containing the hydro sequence closest to each site. +#' @param sites_joined An `sf` class object containing points of all detection sites. Must contain a column named `site_code` containing the site code of each site, and a column called hydroseq containing the hydro sequence closest to each site. #' @inheritParams findDwnstrmHydroseg #' #' @import dplyr @@ -23,7 +23,7 @@ findDwnstrmSite = function(site_id = NULL, init_hydseq = sites_joined %>% dplyr::filter(site_code == site_id) %>% - dplyr::pull(Hydroseq) + dplyr::pull(hydroseq) init_sites = sites_joined %>% dplyr::filter(hydroseq == init_hydseq) %>% @@ -36,7 +36,7 @@ findDwnstrmSite = function(site_id = NULL, dwn_hydseq = PITcleanr::findDwnstrmHydroseg(init_hydseq, flow_lines, - sites_joined$Hydroseq) + sites_joined$hydroseq) if(is.na(dwn_hydseq)) { warning(paste("No downstream hydro sequence was found for site", site_id)) diff --git a/man/findDwnstrmSite.Rd b/man/findDwnstrmSite.Rd index 0ce3ecd..d34b525 100644 --- a/man/findDwnstrmSite.Rd +++ b/man/findDwnstrmSite.Rd @@ -16,7 +16,7 @@ findDwnstrmSite( \item{flow_lines}{An `sf` object containing the flowlines (output of `queryFlowlines()`). Must have a column name called Hydroseq with unique identifiers, and a column name called DnHydroseq containing the hydro sequence of the next downstream segment} -\item{sites_joined}{An `sf` class object containing points of all detection sites. Must contain a column named `site_code` containing the site code of each site, and a column called Hydroseq containing the hydro sequence closest to each site.} +\item{sites_joined}{An `sf` class object containing points of all detection sites. Must contain a column named `site_code` containing the site code of each site, and a column called hydroseq containing the hydro sequence closest to each site.} } \value{ character of the site code downstream