Skip to content

Commit

Permalink
Replace find_gridfile() with more generic find_varfile() function
Browse files Browse the repository at this point in the history
  • Loading branch information
sostberg committed May 17, 2024
1 parent 1c65fcc commit 2cda40b
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '2978850'
ValidationKey: '3177440'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'lpjmlkit: Toolkit for Basic LPJmL Handling'
version: 1.5.0
version: 1.6.0
date-released: '2024-05-16'
abstract: A collection of basic functions to facilitate the work with the Dynamic
Global Vegetation Model (DGVM) Lund-Potsdam-Jena managed Land (LPJmL) hosted at
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
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")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(calc_cellarea)
export(check_config)
export(create_header)
export(detect_io_type)
export(find_varfile)
export(get_cellindex)
export(get_datatype)
export(get_header_item)
Expand Down
54 changes: 1 addition & 53 deletions R/LPJmLData.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ LPJmLData <- R6::R6Class( # nolint:object_name_linter
# If user has not supplied any parameters try to find a grid file in the
# same directory as data. This throws an error if no suitable file is
# found.
filename <- find_gridfile(private$.meta$._data_dir_)
filename <- find_varfile(private$.meta$._data_dir_, "grid")

message(
paste0(
Expand Down Expand Up @@ -447,55 +447,3 @@ aggregate_array <- function(x,
}
data
}


#' Search for a grid file in a directory
#'
#' Function to search for a grid file in a specific directory.
#'
#' @param searchdir Directory where to look for a grid file.
#' @return Character string with the file name of a grid file upon success.
#' Function fails if no matching grid file can be detected.
#'
#' @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. Otherwise, the file
#' name of the grid file including the full path is returned.
#' @noRd
find_gridfile <- function(searchdir) {
# The pattern will match any file name that starts with "grid*".
# Alternative stricter pattern: pattern = "^grid(\\.[[:alpha:]]{3,4})+$"
# This will only match file names "grid.*", where * is one or two file
# extensions with 3 or 4 characters, e.g. "grid.bin" or "grid.bin.json".
grid_files <- list.files(
path = searchdir,
pattern = "^grid",
full.names = TRUE
)
if (length(grid_files) > 0) {
grid_types <- sapply(grid_files, detect_io_type) # nolint:undesirable_function_linter.
# Prefer "meta" file_type if present
if (length(which(grid_types == "meta")) == 1) {
filename <- grid_files[match("meta", grid_types)]
} else if (length(which(grid_types == "clm")) == 1) {
# Second priority "clm" file_type
filename <- grid_files[match("clm", grid_types)]
} else {
# Stop if either multiple files per file type or not the right type have
# been detected
stop(
"Cannot detect grid file automatically.\n",
"$add_grid has to be called supplying parameters as for read_io."
)
}
} else {
# Stop if no file name matching pattern detected
stop(
"Cannot detect grid file automatically.\n",
"$add_grid has to be called supplying parameters as for read_io."
)
}

filename
}
82 changes: 82 additions & 0 deletions R/find_varfile.R
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
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Toolkit for Basic LPJmL Handling <a href=''><img src='inst/img/logo.png' align='right' height='139' /></a>

R package **lpjmlkit**, version **1.5.0**
R package **lpjmlkit**, version **1.6.0**

[![CRAN status](https://www.r-pkg.org/badges/version/lpjmlkit)](https://cran.r-project.org/package=lpjmlkit) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.7773134.svg)](https://doi.org/10.5281/zenodo.7773134) [![R build status](https://github.com/PIK-LPJmL/lpjmlkit/workflows/check/badge.svg)](https://github.com/PIK-LPJmL/lpjmlkit/actions) [![codecov](https://codecov.io/gh/PIK-LPJmL/lpjmlkit/branch/master/graph/badge.svg)](https://app.codecov.io/gh/PIK-LPJmL/lpjmlkit) [![r-universe](https://pik-piam.r-universe.dev/badges/lpjmlkit)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -76,7 +76,7 @@ In case of questions / problems please contact Jannes Breier <jannesbr@pik-potsd

To cite package **lpjmlkit** in publications use:

Breier J, Ostberg S, Wirth S, Minoli S, Stenzel F, Müller C (2024). _lpjmlkit: Toolkit for Basic LPJmL Handling_. doi: 10.5281/zenodo.7773134 (URL: https://doi.org/10.5281/zenodo.7773134), R package version 1.5.0, <URL: https://github.com/PIK-LPJmL/lpjmlkit>.
Breier J, Ostberg S, Wirth S, Minoli S, Stenzel F, Müller C (2024). _lpjmlkit: Toolkit for Basic LPJmL Handling_. doi: 10.5281/zenodo.7773134 (URL: https://doi.org/10.5281/zenodo.7773134), R package version 1.6.0, <URL: https://github.com/PIK-LPJmL/lpjmlkit>.

A BibTeX entry for LaTeX users is

Expand All @@ -85,7 +85,7 @@ A BibTeX entry for LaTeX users is
title = {lpjmlkit: Toolkit for Basic LPJmL Handling},
author = {Jannes Breier and Sebastian Ostberg and Stephen Björn Wirth and Sara Minoli and Fabian Stenzel and Christoph Müller},
year = {2024},
note = {R package version 1.5.0},
note = {R package version 1.6.0},
doi = {10.5281/zenodo.7773134},
url = {https://github.com/PIK-LPJmL/lpjmlkit},
}
Expand Down
33 changes: 33 additions & 0 deletions man/find_varfile.Rd

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

16 changes: 0 additions & 16 deletions tests/testthat/test-LPJmLData.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,6 @@ test_that("test print method", {
)
})

test_that("test find_gridfile", {

# grid file in directory matching search pattern
expect_match(
find_gridfile("../testdata/output"),
"testdata/output"
)

# Error due to missing grid file
expect_error(
find_gridfile("."),
"Cannot detect grid file automatically"
)

})

test_that("LPJmLData initialisation", {
# Meta data must be LPJmLMetaData object
expect_error(
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-find_varfile.R
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"
)

})

0 comments on commit 2cda40b

Please sign in to comment.