From 075fb65b2a114e804cfa3f8d8d68c933b5f8eb04 Mon Sep 17 00:00:00 2001 From: mrustl Date: Thu, 30 May 2024 16:25:15 +0200 Subject: [PATCH] Add read_atmosph() for importing ATMOSPH.in data --- DESCRIPTION | 2 +- NAMESPACE | 4 ++++ R/read_atmosph.R | 54 +++++++++++++++++++++++++++++++++++++++++++++ man/read_atmosph.Rd | 20 +++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 R/read_atmosph.R create mode 100644 man/read_atmosph.Rd diff --git a/DESCRIPTION b/DESCRIPTION index fa48168..9312ae0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -43,5 +43,5 @@ Remotes: Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.2 +RoxygenNote: 7.3.1 VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index 9386989..6e179e7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,7 @@ export(get_output_meta) export(get_units_list) export(prepare_atmosphere_input) export(read_alevel) +export(read_atmosph) export(read_meta_general) export(read_runinf) export(read_solute) @@ -31,9 +32,12 @@ importFrom(magrittr,"%>%") importFrom(readr,fwf_widths) importFrom(readr,read_delim) importFrom(readr,read_fwf) +importFrom(readr,read_table) importFrom(rlang,.data) +importFrom(stats,setNames) importFrom(stringr,str_detect) importFrom(stringr,str_pad) +importFrom(stringr,str_remove) importFrom(stringr,str_remove_all) importFrom(stringr,str_replace) importFrom(stringr,str_split) diff --git a/R/read_atmosph.R b/R/read_atmosph.R new file mode 100644 index 0000000..536dc15 --- /dev/null +++ b/R/read_atmosph.R @@ -0,0 +1,54 @@ +#' Read ATMOSPH.in +#' +#' @param path path to ATMOSPH.in (default: system.file("extdata/model/test/ATMOSPH.in", +#' package = "kwb.hydrus1d")) +#' +#' @return list with atmospheric config parameter sublist and time series sublist +#' @export +#' @importFrom readr read_table +#' @importFrom stringr str_remove str_trim str_split +#' @importFrom stats setNames +#' +read_atmosph <- function(path = system.file("extdata/model/test/ATMOSPH.in", + package = "kwb.hydrus1d")) { + +atm <- readLines(path) + +idx_headers <- grep("\\s+tAtm\\s+", atm) +idx_start <- idx_headers + 1 +idx_end <- grep("end\\*\\*\\*", atm) - 1 + +idx_config_start <- grep("ATMOSPHERIC INFORMATION", atm) + 1 +idx_config_end <- idx_headers - 1 + +config_names_vars <- lapply(1:6, function(i) { + atm[idx_config_start:idx_config_end][i] %>% + stringr::str_remove("\\(MaxAL = number of atmospheric data-records\\)") %>% + stringr::str_remove("\\(max. allowed pressure head at the soil surface\\)") %>% + stringr::str_trim(side = "both") %>% + stringr::str_split("\\s+", simplify = TRUE) %>% + as.vector() +}) + + +atm_config <- stats::setNames(sapply(c(config_names_vars[[2]], config_names_vars[[4]], config_names_vars[[6]]), + list), + nm = c(config_names_vars[[1]], config_names_vars[[3]], config_names_vars[[5]])) + + +atm_names <- stringr::str_trim(atm[idx_headers], side = "both") %>% + stringr::str_split("\\s+", simplify = TRUE) %>% + as.vector() + +a_file <- tempfile() + +atm[idx_start:idx_end] %>% + stringr::str_trim(side = "both") %>% + writeLines(a_file) + +atm_dat <- readr::read_table(a_file, col_names = FALSE) +names(atm_dat)[seq_along(atm_names)] <- atm_names + +list(config = atm_config, + data = atm_dat) +} diff --git a/man/read_atmosph.Rd b/man/read_atmosph.Rd new file mode 100644 index 0000000..5001a46 --- /dev/null +++ b/man/read_atmosph.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/read_atmosph.R +\name{read_atmosph} +\alias{read_atmosph} +\title{Read ATMOSPH.in} +\usage{ +read_atmosph( + path = system.file("extdata/model/test/ATMOSPH.in", package = "kwb.hydrus1d") +) +} +\arguments{ +\item{path}{path to ATMOSPH.in (default: system.file("extdata/model/test/ATMOSPH.in", +package = "kwb.hydrus1d"))} +} +\value{ +list with atmospheric config parameter sublist and time series sublist +} +\description{ +Read ATMOSPH.in +}