Skip to content

Commit

Permalink
Add read_atmosph() for importing ATMOSPH.in data
Browse files Browse the repository at this point in the history
  • Loading branch information
mrustl committed May 30, 2024
1 parent a8d705b commit 075fb65
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ Remotes:
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.2
RoxygenNote: 7.3.1
VignetteBuilder: knitr
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
54 changes: 54 additions & 0 deletions R/read_atmosph.R
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 20 additions & 0 deletions man/read_atmosph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 075fb65

Please sign in to comment.