Skip to content

Commit

Permalink
Add read_profile() and improve read_obsnode()
Browse files Browse the repository at this point in the history
naming (using "node_id")
  • Loading branch information
mrustl committed Jun 4, 2024
1 parent 62e511f commit 669ba41
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion R/read_obsnode.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions R/read_profile.R
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions man/read_obsnode.Rd

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

17 changes: 17 additions & 0 deletions man/read_profile.Rd

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

0 comments on commit 669ba41

Please sign in to comment.