From 8711f6b8ac1afd1c93346ef4c82e8c299550ae39 Mon Sep 17 00:00:00 2001 From: mrustl Date: Tue, 18 Jun 2024 13:33:52 +0200 Subject: [PATCH] Adapt to work with multiple concentrations --- R/convert_atmosphere_to_string.R | 3 ++- R/read_atmosph.R | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/R/convert_atmosphere_to_string.R b/R/convert_atmosphere_to_string.R index ce7a835..3dc354f 100644 --- a/R/convert_atmosphere_to_string.R +++ b/R/convert_atmosphere_to_string.R @@ -35,7 +35,8 @@ convert_atmosphere_to_string <- function( do_pad_short <- function(x) stringr::str_pad(x, pad_short, "left") do_pad_long <- function(x) stringr::str_pad(x, pad_long, "left") - headers <- get_atmosphere_headers() + #headers <- get_atmosphere_headers() + headers <- names(atm) headers_short <- "tAtm" headers_long <- setdiff(headers, headers_short) diff --git a/R/read_atmosph.R b/R/read_atmosph.R index 536dc15..4fc79f5 100644 --- a/R/read_atmosph.R +++ b/R/read_atmosph.R @@ -47,6 +47,32 @@ atm[idx_start:idx_end] %>% writeLines(a_file) atm_dat <- readr::read_table(a_file, col_names = FALSE) + +n_unnamed <- ncol(atm_dat)-length(atm_names) + +if(n_unnamed > 0) { + kwb.utils::catAndRun(sprintf("Guessing 'solute concentration' column names for %d unnamed columns", + n_unnamed), + expr = { + +if(n_unnamed %% 2 != 0) { + message(sprintf("and replacing '%s', with 'cTop2'", atm_names[length(atm_names)])) + n_unnamed <- n_unnamed + 1 +} + + atm_names_added <- lapply(seq_len(n_unnamed/2), function(i){ + c(sprintf("cTop%d", i+1), sprintf("cBot%d", i+1)) + }) %>% unlist() + + if(ncol(atm_dat)-n_unnamed-1 < length(atm_names)) { + atm_names <- atm_names[-(ncol(atm_dat)-n_unnamed+1)] + } + + atm_names <- c(atm_names, atm_names_added) + + }) +} + names(atm_dat)[seq_along(atm_names)] <- atm_names list(config = atm_config,