R/prepare_phreeqc_input.R

Defines functions convert_phreeqc_input_to_wide pivot_wider_by_par_name prepare_phreeqc_input get_phreeqc_data

Documented in convert_phreeqc_input_to_wide get_phreeqc_data prepare_phreeqc_input

#' Get PhreeqC data
#'
#' @param lab_bwb imported BWB lab data as retrieved by \code{\link{read_lab_bwb}}
#' @return tibble with columns solution, par_name_phreeqc (which are not empty or 
#' NA) and numeric_value
#' @export
#' @importFrom dplyr rename select  

get_phreeqc_data <- function(lab_bwb)
{
  dat <- lab_bwb %>%
    dplyr::filter(!kwb.utils::isNaOrEmpty(.data$par_name_phreeqc))
  
  stopifnot(all(
    dat$unit[!dat$par_name_phreeqc %in% c("temp", "pH")] == "mg/l"
  ))
  
  dat %>% 
    dplyr::select(
      .data$probe_nr, 
      .data$par_name_phreeqc, 
      .data$numeric_value
    )
}

#' Prepare PhreeqC input
#'
#' @param lab_bwb_phreeqc selected BWB lab data as retrieved by \code{\link{get_phreeqc_data}}
#' @param title user defined title (default: "") 
#' @return data frame with input structure for kwb.phreeqc
#' @export
#'
#' @importFrom kwb.utils isNaOrEmpty
#' @importFrom dplyr mutate select relocate rename 
#' @importFrom tidyr pivot_wider
#' @importFrom geosalz.phreeqc prepare_solutions_input tidy_samples
prepare_phreeqc_input <- function(lab_bwb_phreeqc, title = "")
{
  lab_bwb_phreeqc %>% 
    dplyr::rename(solution = .data$probe_nr) %>% 
    dplyr::select(
      .data$solution, 
      .data$par_name_phreeqc, 
      .data$numeric_value
    ) %>%
    pivot_wider_by_par_name() %>% 
    dplyr::mutate(units = "ppm") %>% 
    dplyr::relocate(.data$units, .after = .data$solution) %>% 
    geosalz.phreeqc::tidy_samples() %>% 
    dplyr::mutate(
      solution = as.character(.data$solution), 
      outOfLimit = "", 
      numericValue = as.numeric(.data$value)
    ) %>% 
    dplyr::filter(!is.na(.data$numericValue)) %>% 
    geosalz.phreeqc::prepare_solutions_input(title = title)
} 

pivot_wider_by_par_name <- function(data)
{
  tidyr::pivot_wider(
    data,
    names_from = .data$par_name_phreeqc, 
    values_from = .data$numeric_value
  )
}

#' Convert PhreeqC input to "wide" format
#'
#' @param phreeqc_input PhreeqC input as retrieved by \code{\link{get_phreeqc_data}}
#' @return PhreeqC input in "wide" format
#' @export
#' @importFrom tidyr pivot_wider
#' @importFrom dplyr rename
convert_phreeqc_input_to_wide <- function(phreeqc_input)
{
  phreeqc_input %>% 
    pivot_wider_by_par_name() %>%
    dplyr::rename(solution = .data$probe_nr)
}
KWB-R/kwb.geosalz documentation built on March 28, 2024, 9:05 p.m.