Skip to content

Commit f793602

Browse files
committed
Create read_world.R
1 parent 9fffa78 commit f793602

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

R/read_world.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#' read_world
2+
#'
3+
#' Reads a worldfile
4+
#'
5+
#' @param worldfile Source worldfile
6+
#' @author Will Burke
7+
#'
8+
#' @export
9+
10+
read_world = function(worldfile) {
11+
12+
# ---------- Parse Worldfile ----------
13+
# parsing the values as characters to retain the exact value/precision
14+
read_world = readLines(worldfile, warn = FALSE, encoding = "UTF-8")
15+
read_world = read_world[nchar(read_world) > 0]
16+
world = strsplit(trimws(read_world), "\\s+")
17+
world = data.frame(matrix(unlist(world), nrow = length(world), byrow = T), stringsAsFactors = FALSE)
18+
names(world) = c("values","vars")
19+
20+
# ---------- Find Levels----------
21+
index_all = which(world$vars == "world_ID" | world$vars == "basin_ID" | world$vars == "hillslope_ID" |
22+
world$vars == "zone_ID" | world$vars == "patch_ID" | world$vars == "canopy_strata_ID")
23+
index_names = gsub("_ID", "", x = world$vars[index_all])
24+
index_max = c(index_all[2:length(index_all)] - 1, length(world$vars))
25+
world$level = unname(unlist(mapply(rep, index_names, (index_max - index_all) + 1 )))
26+
world$ID = unname(unlist(mapply(rep, world$values[index_all], (index_max - index_all) + 1 )))
27+
28+
# get unique ID - useful for queries/parsing later
29+
world$unique_ID = unname(unlist(mapply(rep, c(1:length(index_names)), (index_max - index_all) + 1 )))
30+
31+
return(world)
32+
}

0 commit comments

Comments
 (0)