R/join_TMDL.R

Defines functions join_TMDL

Documented in join_TMDL

#' join_TMDL
#'
#' Join assessments to TMDL actions. used to determine categery 4A and to display action IDs in display
#'
#' @param df dataframe to join. Shoud;d either be final AU decsion table or final GNIS decision table, after
#'            all delist etc completed
#' @param type should be 'AU' or 'GNIS' depending on what you ware matching
#' @returns Returns same datafrane as df, but with action ID and TMDL parameters added
#' @import odeqtmdl
#' @export
#'
#'
#'
join_TMDL <- function(df, type){


  tmdl_actual_periods <- tibble::tribble(
                                              ~Period, ~Actual_period,
                                               "Both",   "year_round",
                                               "Both",     "spawn",
                           "Mixed (Both, year_round)",   "year_round",
                           "Mixed (Both, year_round)",     "spawn"
                           )



  tmdl_names <- odeqtmdl::tmdl_actions |>
    dplyr::select(action_id, TMDL_name) |>
    dplyr::distinct()




  if(type == 'AU'){





    # prep TMDL info --------------------------------------------------------------------------------------------------


    tmdl_au0 <- odeqtmdl::tmdl_au |>
      dplyr::left_join(tmdl_actual_periods, relationship = "many-to-many") |>
      dplyr::rename(TMDL_Period = Period,
                    period = Actual_period) |>
      dplyr::left_join(tmdl_names)


    TMDLs <- tmdl_au0 %>%
      dplyr::filter(TMDL_scope == "TMDL") |>
      dplyr::left_join(odeqtmdl::tmdl_parameters[, c("action_id", "TMDL_wq_limited_parameter", "TMDL_pollutant", "TMDL_status")],
                       by = c("action_id", "TMDL_wq_limited_parameter", "TMDL_pollutant")) %>%
      dplyr::filter(TMDL_status == 'Active') |>
      dplyr::select(AU_ID, TMDL_name, action_id, Pollu_ID, period, TMDL_Period, TMDL_pollutant, TMDL_status) |>
      dplyr::group_by(AU_ID, Pollu_ID, period) |>
      dplyr::summarise(TMDLs =  stringr::str_c(unique(TMDL_name), collapse = "; "),
                       action_ids =  stringr::str_c(unique(action_id), collapse = "; "),
                       TMDL_pollutants = stringr::str_c(unique(TMDL_pollutant), collapse = "; "),
                       TMDL_Periods = stringr::str_c(unique(TMDL_Period), collapse = "; "),
                       TMDL_Periods = stringr::str_c(unique(TMDL_Period), collapse = "; ")) |>
      dplyr::mutate(Pollu_ID = as.character(Pollu_ID))

    AU_display_TMDL <- df |>
      dplyr::left_join(TMDLs) |>
      dplyr::mutate(final_AU_cat = dplyr::case_when(final_AU_cat == '5' & !is.na(action_ids) ~ '4A',
                                                    .default = final_AU_cat))


    return(AU_display_TMDL)
  } else {


    tmdl_au_gnis0 <- odeqtmdl::tmdl_au_gnis |>
      dplyr::left_join(tmdl_actual_periods, relationship = "many-to-many") |>
      dplyr::rename(TMDL_Period = Period,
                    period = Actual_period)|>
      dplyr::left_join(tmdl_names)


    TMDL_GNIS <- tmdl_au_gnis0 |>
      dplyr::filter(TMDL_scope == "TMDL") |>
      dplyr::mutate(AU_GNIS_Name = stringr::str_split_i(AU_GNIS, ";", 2)) |>
      dplyr::left_join(odeqtmdl::tmdl_parameters[, c("action_id", "TMDL_wq_limited_parameter", "TMDL_pollutant", "TMDL_status")],
                       by = c("action_id", "TMDL_wq_limited_parameter", "TMDL_pollutant")) %>%
      dplyr::filter(TMDL_status == 'Active') |>
      dplyr::select(AU_ID, AU_GNIS_Name, TMDL_name, action_id, Pollu_ID, period, TMDL_Period, TMDL_pollutant, TMDL_status) |>
      dplyr::group_by(AU_ID, AU_GNIS_Name, Pollu_ID, period) |>
      dplyr::summarise(TMDLs =  stringr::str_c(unique(TMDL_name), collapse = "; "),
                       action_ids =  stringr::str_c(str_unique(action_id), collapse = "; "),
                       TMDL_pollutants = stringr::str_c(unique(TMDL_pollutant), collapse = "; "),
                       TMDL_Periods = stringr::str_c(str_unique(TMDL_Period), collapse = "; ")) |>
      dplyr::mutate(Pollu_ID = as.character(Pollu_ID))


    WS_GNIS_rollup_delist_TMDL <- df |>
      dplyr::left_join(TMDL_GNIS) |>
      dplyr::mutate(final_GNIS_cat = dplyr::case_when(final_GNIS_cat == '5' & !is.na(action_ids) ~ '4A',
                                                      .default = final_GNIS_cat))

    return(WS_GNIS_rollup_delist_TMDL)


  }


}
TravisPritchardODEQ/IRLibrary documentation built on April 15, 2024, 9:03 a.m.