Skip to content

Commit 669ba41

Browse files
committed
Add read_profile() and improve read_obsnode()
naming (using "node_id")
1 parent 62e511f commit 669ba41

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export(read_alevel)
1111
export(read_atmosph)
1212
export(read_meta_general)
1313
export(read_obsnode)
14+
export(read_profile)
1415
export(read_runinf)
1516
export(read_solute)
1617
export(read_tlevel)

R/read_obsnode.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#'
33
#' @param path path to Obs_Node.out
44
#' @param to_longer convert table to longer format (default: TRUE)
5+
#' @return tibble with Obs_Node time series data
56
#' @importFrom stringr str_trim str_split str_replace_all str_remove
67
#' @importFrom readr read_csv
78
#' @export
@@ -66,7 +67,7 @@ read_obsnode <- function(path, to_longer = TRUE) {
6667
if(to_longer) {
6768
dat %>%
6869
tidyr::pivot_longer( - time) %>%
69-
tidyr::separate(col = "name", into = c("node", "variable"), sep = "_") %>%
70+
tidyr::separate(col = "name", into = c("node_id", "variable"), sep = "_") %>%
7071
dplyr::mutate(node = stringr::str_remove(node, "node") %>% as.integer())
7172
} else {
7273
dat

R/read_profile.R

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#' Read PROFILE.out
2+
#'
3+
#' @param path path to PROFILE.out
4+
#'
5+
#' @return tibble with PROFILE.out data
6+
#' @export
7+
#' @importFrom stringr str_replace
8+
read_profile <- function(path) {
9+
10+
lines <- readLines(paths$profile)
11+
12+
header_idx <- grep("x", lines)
13+
14+
dat <- lines[(header_idx+1):length(lines)] %>%
15+
stringr::str_trim() %>%
16+
stringr::str_replace_all("\\s+", ",")
17+
18+
ncols <- sapply(seq_along(dat), function(i) length(gregexpr(",", dat[i])[[1]])) + 1
19+
20+
21+
header_names_file <- c("node_id", stringr::str_sub(lines[header_idx],
22+
start = gregexpr("x", lines[header_idx])[[1]][1],
23+
end = nchar(lines[header_idx])) %>%
24+
stringr::str_trim() %>%
25+
stringr::str_replace_all("\\s+", " ") %>%
26+
stringr::str_split(" ", simplify = TRUE) %>%
27+
as.vector() %>% tolower())
28+
29+
header_clean <- if(median(ncols) > length(header_names_file)) {
30+
string_conc <- sprintf("conc_%d", seq_len(median(ncols) - length(header_names_file))+1)
31+
32+
c(stringr::str_replace(header_names_file, "conc", "conc_1"),
33+
string_conc)
34+
35+
} else {
36+
header_names_file
37+
}
38+
39+
header_clean
40+
41+
path_profile <- file.path(tempdir(), "profile.csv")
42+
43+
c(paste0(header_clean, collapse = ","),
44+
dat[ncols == median(ncols)]) %>%
45+
writeLines(path_profile)
46+
47+
readr::read_csv(path_profile)
48+
}

man/read_obsnode.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/read_profile.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)