-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace find_gridfile() with more generic find_varfile() function
- Loading branch information
Showing
10 changed files
with
139 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Package: lpjmlkit | ||
Type: Package | ||
Title: Toolkit for Basic LPJmL Handling | ||
Version: 1.5.0 | ||
Version: 1.6.0 | ||
Authors@R: c( | ||
person("Jannes", "Breier", , "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-9055-6904")), | ||
person("Sebastian","Ostberg", , "[email protected]", role = "aut", comment = c(ORCID = "0000-0002-2368-7015")), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#' Search for a variable file in a directory | ||
#' | ||
#' Function to search for a file containing a specific variable in a specific | ||
#' directory. | ||
#' | ||
#' @param searchdir Directory where to look for the variable file. | ||
#' @param variable Single character string containing the variable to search for | ||
#' @param strict Boolean. If set to `TRUE`, file must be named "variable.**", | ||
#' where "**" is one or two file extensions with 3 or 4 characters, e.g. | ||
#' "grid.bin.json" if `variable = "grid"`. If set to `FALSE`, the function | ||
#' will first try to match the strict pattern. If unsuccessful, any filename | ||
#' that starts with "variable" will be matched. | ||
#' @return Character string with the file name of a matched file, including the | ||
#' full path. | ||
#' | ||
#' @details This function looks for file names in `searchdir` that match the | ||
#' `pattern` parameter in its [`list.files()`] call. Files of type "meta" are | ||
#' preferred. Files of type "clm" are also accepted. The function returns an | ||
#' error if no suitable file or multiple files are found. | ||
#' @export | ||
find_varfile <- function(searchdir, variable = "grid", strict = FALSE) { | ||
if (length(variable) != 1 || !is.character(variable)) { | ||
stop(col_var("variable"), " must be a single character string") | ||
} | ||
# This will only match file names "variable.*", where * is one or two file | ||
# extensions with 3 or 4 characters, e.g. "grid.bin" or "grid.bin.json". | ||
var_files <- list.files( | ||
path = searchdir, | ||
pattern = paste0("^", variable, "(\\.[[:alpha:]]{3,4})+$"), | ||
full.names = TRUE | ||
) | ||
if (length(var_files) > 0) { | ||
var_types <- sapply(var_files, detect_io_type) # nolint:undesirable_function_linter. | ||
# Prefer "meta" file_type if present | ||
if (length(which(var_types == "meta")) == 1) { | ||
filename <- var_files[match("meta", var_types)] | ||
} else if (length(which(var_types == "clm")) == 1) { | ||
# Second priority "clm" file_type | ||
filename <- var_files[match("clm", var_types)] | ||
} else if (strict) { | ||
# Stop if either multiple files per file type or not the right type have | ||
# been detected | ||
stop( | ||
"Cannot detect ", col_var(variable), " file automatically." | ||
) | ||
} | ||
} else if (strict) { | ||
# Stop if no file name matching pattern detected | ||
stop( | ||
"Cannot detect ", col_var(variable), " file automatically." | ||
) | ||
} else { | ||
# Less strict pattern matching any file name that starts with "grid*". | ||
var_files <- list.files( | ||
path = searchdir, | ||
pattern = paste0("^", variable), | ||
full.names = TRUE | ||
) | ||
if (length(var_files) > 0) { | ||
var_types <- sapply(var_files, detect_io_type) # nolint:undesirable_function_linter. | ||
# Prefer "meta" file_type if present | ||
if (length(which(var_types == "meta")) == 1) { | ||
filename <- var_files[match("meta", var_types)] | ||
} else if (length(which(var_types == "clm")) == 1) { | ||
# Second priority "clm" file_type | ||
filename <- var_files[match("clm", var_types)] | ||
} else { | ||
# Stop if either multiple files per file type or not the right type have | ||
# been detected | ||
stop( | ||
"Cannot detect ", col_var(variable), " file automatically." | ||
) | ||
} | ||
} else { | ||
# Stop if no file name matching pattern detected | ||
stop( | ||
"Cannot detect ", col_var(variable), " file automatically." | ||
) | ||
} | ||
} | ||
filename | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
test_that("test find_varfile", { | ||
|
||
# grid file in directory matching search pattern | ||
expect_match( | ||
find_varfile("../testdata/output", "grid"), | ||
"testdata/output/grid.bin.json" | ||
) | ||
|
||
# Error due to missing grid file | ||
expect_error( | ||
find_varfile(".", "grid"), | ||
"Cannot detect grid file automatically" | ||
) | ||
|
||
}) | ||
|