From 669ba414c9e70829816b89b5c5511266646f75a5 Mon Sep 17 00:00:00 2001 From: mrustl Date: Tue, 4 Jun 2024 17:55:13 +0200 Subject: [PATCH] Add read_profile() and improve read_obsnode() naming (using "node_id") --- NAMESPACE | 1 + R/read_obsnode.R | 3 ++- R/read_profile.R | 48 +++++++++++++++++++++++++++++++++++++++++++++ man/read_obsnode.Rd | 3 +++ man/read_profile.Rd | 17 ++++++++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 R/read_profile.R create mode 100644 man/read_profile.Rd diff --git a/NAMESPACE b/NAMESPACE index 018656e..09c6819 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,6 +11,7 @@ export(read_alevel) export(read_atmosph) export(read_meta_general) export(read_obsnode) +export(read_profile) export(read_runinf) export(read_solute) export(read_tlevel) diff --git a/R/read_obsnode.R b/R/read_obsnode.R index 13b75b5..bc44a0f 100644 --- a/R/read_obsnode.R +++ b/R/read_obsnode.R @@ -2,6 +2,7 @@ #' #' @param path path to Obs_Node.out #' @param to_longer convert table to longer format (default: TRUE) +#' @return tibble with Obs_Node time series data #' @importFrom stringr str_trim str_split str_replace_all str_remove #' @importFrom readr read_csv #' @export @@ -66,7 +67,7 @@ read_obsnode <- function(path, to_longer = TRUE) { if(to_longer) { dat %>% tidyr::pivot_longer( - time) %>% - tidyr::separate(col = "name", into = c("node", "variable"), sep = "_") %>% + tidyr::separate(col = "name", into = c("node_id", "variable"), sep = "_") %>% dplyr::mutate(node = stringr::str_remove(node, "node") %>% as.integer()) } else { dat diff --git a/R/read_profile.R b/R/read_profile.R new file mode 100644 index 0000000..81f5785 --- /dev/null +++ b/R/read_profile.R @@ -0,0 +1,48 @@ +#' Read PROFILE.out +#' +#' @param path path to PROFILE.out +#' +#' @return tibble with PROFILE.out data +#' @export +#' @importFrom stringr str_replace +read_profile <- function(path) { + + lines <- readLines(paths$profile) + + header_idx <- grep("x", lines) + + dat <- lines[(header_idx+1):length(lines)] %>% + stringr::str_trim() %>% + stringr::str_replace_all("\\s+", ",") + + ncols <- sapply(seq_along(dat), function(i) length(gregexpr(",", dat[i])[[1]])) + 1 + + + header_names_file <- c("node_id", stringr::str_sub(lines[header_idx], + start = gregexpr("x", lines[header_idx])[[1]][1], + end = nchar(lines[header_idx])) %>% + stringr::str_trim() %>% + stringr::str_replace_all("\\s+", " ") %>% + stringr::str_split(" ", simplify = TRUE) %>% + as.vector() %>% tolower()) + + header_clean <- if(median(ncols) > length(header_names_file)) { + string_conc <- sprintf("conc_%d", seq_len(median(ncols) - length(header_names_file))+1) + + c(stringr::str_replace(header_names_file, "conc", "conc_1"), + string_conc) + + } else { + header_names_file + } + + header_clean + + path_profile <- file.path(tempdir(), "profile.csv") + + c(paste0(header_clean, collapse = ","), + dat[ncols == median(ncols)]) %>% + writeLines(path_profile) + + readr::read_csv(path_profile) +} diff --git a/man/read_obsnode.Rd b/man/read_obsnode.Rd index 8a65936..ea2372c 100644 --- a/man/read_obsnode.Rd +++ b/man/read_obsnode.Rd @@ -11,6 +11,9 @@ read_obsnode(path, to_longer = TRUE) \item{to_longer}{convert table to longer format (default: TRUE)} } +\value{ +tibble with Obs_Node time series data +} \description{ Read Obs_Node.out } diff --git a/man/read_profile.Rd b/man/read_profile.Rd new file mode 100644 index 0000000..d5c0e1d --- /dev/null +++ b/man/read_profile.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/read_profile.R +\name{read_profile} +\alias{read_profile} +\title{Read PROFILE.out} +\usage{ +read_profile(path) +} +\arguments{ +\item{path}{path to PROFILE.out} +} +\value{ +tibble with PROFILE.out data +} +\description{ +Read PROFILE.out +}