Skip to contents

1 Install R packages

if (! require("remotes")) {
  install.packages("remotes", repos = "https://cloud.r-project.org")
}

remotes::install_github("kwb-r/kwb.geosalz", dependencies = TRUE)

2 Setup the project

2.1 Define paths

# Define paths and resolve placeholders
paths_list <- list(
  servername = Sys.getenv("servername"),
  root_server = "//<servername>/projekte$/GRW_Department/PROJECTS",
  project = "geosalz",
  root_project = "<root_server>/<project>",
  monitoring = "<root_project>/Work-packages/AP2_Monitoring",
  samples_bwb_dir = "<monitoring>/Beprobung",
  version = "1.12",
  samples_bwb = "<samples_bwb_dir>/Hydrochemie_Gesamt_Geosalz_v<version>.xlsx",
  master_data = "<samples_bwb>",
  isotopes_dir =  "<monitoring>/Isotope/data",
  isotopes = "<isotopes_dir>/Isotope.csv",
  phreeqc_dir = "<samples_bwb_dir>/phreeqc",
  phreeqc_txt = "<phreeqc_dir>/phreeqc_output_v%s.txt",
  phreeqc_xlsx = "<phreeqc_dir>/phreeqc_output_v%s.xlsx"
)

paths <- kwb.utils::resolve(paths_list)
#paths <- kwb.utils::resolve(paths, root = "C:/projects")

3 Data import

master_data <- kwb.geosalz::read_master_data(paths$master_data)

lab_bwb <- kwb.geosalz::read_lab_bwb(paths$samples_bwb, sheet = 'phreeqc_analysen')

lab_ufz <- kwb.geosalz::read_isotopes(paths$isotopes)

lab_data <- lab_bwb %>% 
  dplyr::mutate(probe_nr = as.character(.data$probe_nr)) %>%
  dplyr::bind_rows(
    lab_ufz %>%  
      dplyr::mutate(par_val_org = as.character(.data$par_val_org))
  )

str(lab_data)

lab_data_master <- dplyr::left_join(lab_data, master_data, by = "messstelle")

str(lab_data_master)

4 PhreeqC

4.1 Prepare PHREEQC Input File

phreeqc_input <- kwb.geosalz::get_phreeqc_data(lab_data_master)
phreeqc_input

phreeqc_input_wide <- kwb.geosalz::convert_phreeqc_input_to_wide(phreeqc_input)
phreeqc_input_wide 

solutions_input <-  kwb.geosalz::prepare_phreeqc_input(
  phreeqc_input,
  title = sprintf("Based on '%s'", paths$samples_bwb)
)

Content of solutions_input is shown below:

4.2 Run PHREEQC

fs::dir_create(paths$phreeqc_dir)

### Load PHREEQC database "phreeqc.dat"
phreeqc::phrLoadDatabaseString(phreeqc::phreeqc.dat)

### Capture PHREEQC output normally sent to output file into a buffer
phreeqc::phrSetOutputStringsOn(TRUE)

## Run PHREEQC with "solutions_input"
phreeqc::phrRunString(input = solutions_input)

### Retrieves the phreeqc output as a character vector
phreeqc_output <- phreeqc::phrGetOutputStrings()
writeLines(phreeqc_output, paths$phreeqc_txt)

Here is the content of phreeqc_output

4.3 Get Results

sims <- geosalz.phreeqc::read_simulations(phreeqc_output)
str(sims)

sims_list <- geosalz.phreeqc::convert_simulations_to_list(sims)
str(sims_list)

n_components <- length(names(sims_list))

masters_lab <- lab_data %>% 
  dplyr::count(
    .data$probe_nr,
    .data$probenahme_datum, 
    .data$messstelle
  ) %>% 
  dplyr::select(- .data$n) %>% 
  dplyr::left_join(master_data)

sims_list_master <- stats::setNames(
  
  lapply(seq_len(n_components), function(i) {
    
    if (i %in% 1:2) {
      
      sims_list[[i]]
      
    } else {
      
      dplyr::left_join(
        sims_list[[i]],
        masters_lab,
        by = c("solution_name" = "probe_nr")
      )
    }
  }), 
  names(sims_list)
)

# View(sims_list_master$solution_composition)
# View(sims_list_master$description_of_solution)
# View(sims_list_master$redox_couples)
# View(sims_list_master$distribution_of_species)
# View(sims_list_master$saturation_indices)

5 Export to Excel

export_data <- c(
  list(
    "master_data" = master_data, 
    "lab_bwb" = lab_bwb,
    "lab_ufz" = lab_ufz,
    "phreeqc_input" = phreeqc_input_wide
  ),
  sims_list
)

openxlsx::write.xlsx(
  x = export_data,
  file = paths$phreeqc_xlsx,
  overwrite = TRUE
)

6 Session Info

Plattform

name value
version R version 4.4.2 (2024-10-31 ucrt)
os Windows Server 2022 x64 (build 20348)
system x86_64, mingw32
ui RTerm
language en
collate English_United States.utf8
ctype English_United States.utf8
tz UTC
date 2024-12-23
pandoc 3.1.11 @ C:/HOSTED1/windows/pandoc/31F3871.11/x64/PANDOC~1.11/ (via rmarkdown)

Packages

#>  package     * version date (UTC) lib source
#>  bslib         0.8.0   2024-07-29 [1] CRAN (R 4.4.1)
#>  cachem        1.1.0   2024-05-16 [1] CRAN (R 4.4.0)
#>  cli           3.6.3   2024-06-21 [1] RSPM (R 4.4.0)
#>  desc          1.4.3   2023-12-10 [1] CRAN (R 4.4.0)
#>  digest        0.6.37  2024-08-19 [1] RSPM (R 4.4.0)
#>  dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.4.0)
#>  evaluate      1.0.1   2024-10-10 [1] CRAN (R 4.4.1)
#>  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.4.0)
#>  fs            1.6.5   2024-10-30 [1] CRAN (R 4.4.1)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.4.0)
#>  glue          1.8.0   2024-09-30 [1] CRAN (R 4.4.1)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
#>  htmlwidgets   1.6.4   2023-12-06 [1] CRAN (R 4.4.0)
#>  jquerylib     0.1.4   2021-04-26 [1] CRAN (R 4.4.0)
#>  jsonlite      1.8.9   2024-09-20 [1] RSPM (R 4.4.0)
#>  knitr         1.49    2024-11-08 [1] RSPM (R 4.4.0)
#>  kwb.utils     0.15.0  2024-04-25 [1] Github (kwb-r/kwb.utils@4415aa2)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.4.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.4.0)
#>  pillar        1.10.0  2024-12-17 [1] CRAN (R 4.4.2)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.4.0)
#>  pkgdown       2.1.1   2024-09-17 [1] RSPM (R 4.4.0)
#>  purrr         1.0.2   2023-08-10 [1] CRAN (R 4.4.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.4.0)
#>  ragg          1.3.0   2024-03-13 [1] CRAN (R 4.4.0)
#>  rlang         1.1.4   2024-06-04 [1] CRAN (R 4.4.0)
#>  rmarkdown     2.29    2024-11-04 [1] CRAN (R 4.4.2)
#>  sass          0.4.9   2024-03-15 [1] CRAN (R 4.4.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.4.0)
#>  systemfonts   1.1.0   2024-05-15 [1] CRAN (R 4.4.0)
#>  textshaping   0.3.7   2023-10-09 [1] CRAN (R 4.4.0)
#>  tibble        3.2.1   2023-03-20 [1] CRAN (R 4.4.0)
#>  tidyr       * 1.3.1   2024-01-24 [1] CRAN (R 4.4.0)
#>  tidyselect    1.2.1   2024-03-11 [1] CRAN (R 4.4.0)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.4.0)
#>  xfun          0.49    2024-10-31 [1] CRAN (R 4.4.1)
#>  yaml          2.3.10  2024-07-26 [1] RSPM (R 4.4.0)
#> 
#>  [1] D:/a/_temp/Library
#>  [2] C:/R/library

Pandoc

#>                                                pandoc_directory pandoc_version
#> 1 C:/HOSTED~1/windows/pandoc/31F387~1.11/x64/PANDOC~1.11/pandoc         3.1.11