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/BWB-lab",
  version = "1.0.6",
  samples_bwb = "<samples_bwb_dir>/Hydrochemie_Gesamt_Geosalz_v<version>.xlsx",
  master_data = "<samples_bwb>",
  isotopes_dir =  "<monitoring>/Isotope",
  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)

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::minteq.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.2.3 (2023-03-15 ucrt)
os Windows Server x64 (build 20348)
system x86_64, mingw32
ui RTerm
language en
collate English_United States.utf8
ctype English_United States.utf8
tz UTC
date 2023-04-15
pandoc 2.19.2 @ C:/HOSTED1/windows/pandoc/2191.2/x64/PANDOC~1.2/ (via rmarkdown)

Packages

#>  package     * version date (UTC) lib source
#>  bslib         0.4.2   2022-12-16 [1] RSPM (R 4.2.0)
#>  cachem        1.0.7   2023-02-24 [1] RSPM (R 4.2.0)
#>  cli           3.6.1   2023-03-23 [1] RSPM (R 4.2.0)
#>  desc          1.4.2   2022-09-08 [1] RSPM (R 4.2.0)
#>  digest        0.6.31  2022-12-11 [1] RSPM (R 4.2.0)
#>  dplyr       * 1.1.1   2023-03-22 [1] RSPM (R 4.2.0)
#>  evaluate      0.20    2023-01-17 [1] RSPM (R 4.2.0)
#>  fansi         1.0.4   2023-01-22 [1] RSPM (R 4.2.0)
#>  fastmap       1.1.1   2023-02-24 [1] RSPM (R 4.2.0)
#>  fs            1.6.1   2023-02-06 [1] RSPM (R 4.2.0)
#>  generics      0.1.3   2022-07-05 [1] RSPM (R 4.2.0)
#>  glue          1.6.2   2022-02-24 [1] RSPM (R 4.2.0)
#>  htmltools     0.5.5   2023-03-23 [1] RSPM (R 4.2.0)
#>  jquerylib     0.1.4   2021-04-26 [1] RSPM (R 4.2.0)
#>  jsonlite      1.8.4   2022-12-06 [1] RSPM (R 4.2.0)
#>  knitr         1.42    2023-01-25 [1] RSPM (R 4.2.0)
#>  kwb.utils     0.14.0  2023-04-15 [1] Github (KWB-R/kwb.utils@56518c7)
#>  lifecycle     1.0.3   2022-10-07 [1] RSPM (R 4.2.0)
#>  magrittr      2.0.3   2022-03-30 [1] RSPM (R 4.2.0)
#>  memoise       2.0.1   2021-11-26 [1] RSPM (R 4.2.0)
#>  pillar        1.9.0   2023-03-22 [1] RSPM (R 4.2.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] RSPM (R 4.2.0)
#>  pkgdown       2.0.7   2022-12-14 [1] RSPM (R 4.2.0)
#>  purrr         1.0.1   2023-01-10 [1] RSPM (R 4.2.0)
#>  R6            2.5.1   2021-08-19 [1] RSPM (R 4.2.0)
#>  ragg          1.2.5   2023-01-12 [1] RSPM (R 4.2.0)
#>  rlang         1.1.0   2023-03-14 [1] RSPM (R 4.2.0)
#>  rmarkdown     2.21    2023-03-26 [1] RSPM (R 4.2.0)
#>  rprojroot     2.0.3   2022-04-02 [1] RSPM (R 4.2.0)
#>  sass          0.4.5   2023-01-24 [1] RSPM (R 4.2.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] RSPM (R 4.2.0)
#>  stringi       1.7.12  2023-01-11 [1] RSPM (R 4.2.0)
#>  stringr       1.5.0   2022-12-02 [1] RSPM (R 4.2.0)
#>  systemfonts   1.0.4   2022-02-11 [1] RSPM (R 4.2.0)
#>  textshaping   0.3.6   2021-10-13 [1] RSPM (R 4.2.0)
#>  tibble        3.2.1   2023-03-20 [1] RSPM (R 4.2.0)
#>  tidyr       * 1.3.0   2023-01-24 [1] RSPM (R 4.2.0)
#>  tidyselect    1.2.0   2022-10-10 [1] RSPM (R 4.2.0)
#>  utf8          1.2.3   2023-01-31 [1] RSPM (R 4.2.0)
#>  vctrs         0.6.1   2023-03-22 [1] RSPM (R 4.2.0)
#>  xfun          0.38    2023-03-24 [1] RSPM (R 4.2.0)
#>  yaml          2.3.7   2023-01-23 [1] RSPM (R 4.2.0)
#> 
#>  [1] D:/a/_temp/Library
#>  [2] C:/R/library

Pandoc

#>                                           pandoc_directory pandoc_version
#> 1 C:/HOSTED~1/windows/pandoc/219~1.2/x64/PANDOC~1.2/pandoc         2.19.2