Skip to content

Commit

Permalink
Merge branch 'major_upgrade' into 'master'
Browse files Browse the repository at this point in the history
major update to version 1.0.0, rearranging code, introducing deprecation to some arguments

See merge request lpjml/lpjmlkit!59
  • Loading branch information
stephnwirth committed Mar 20, 2023
2 parents 8b09957 + 276a811 commit ad2f3a7
Show file tree
Hide file tree
Showing 26 changed files with 581 additions and 422 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '1651805'
ValidationKey: '1943600'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "lpjmlkit: Toolkit for basic LPJmL handling",
"version": "0.8.5",
"version": "1.0.0",
"description": "<p>A collection of basic functions to facilitate the work with the\n Dynamic Global Vegetation Model (DGVM) Lund-Potsdam-Jena managed Land\n (LPJmL) hosted at the Potsdam Institute for Climate Impact Research (PIK).\n It provides functions for performing LPJmL simulations, as well as reading,\n processing and writing model-related data such as inputs and outputs or\n configuration files.<\/p>",
"creators": [
{
Expand Down
4 changes: 2 additions & 2 deletions 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: 0.8.5
Version: 1.0.0
Authors@R: c(
person("Jannes", "Breier", , "jannesbr@pik-potsdam.de", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-9055-6904")),
person("Sebastian","Ostberg", , "ostberg@pik-potsdam.de", role = "aut", comment = c(ORCID = "0000-0002-2368-7015")),
Expand Down Expand Up @@ -48,4 +48,4 @@ Suggests:
maps
Config/testthat/edition: 3
VignetteBuilder: knitr
Date: 2023-03-17
Date: 2023-03-20
3 changes: 0 additions & 3 deletions R/LPJmLData.R
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,3 @@ find_gridfile <- function(searchdir) {

filename
}

# Avoid note for "."...
utils::globalVariables(".") # nolint:undesirable_function_linter
48 changes: 24 additions & 24 deletions R/calc_cellarea.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#' @param x `LPJmLData` object with `$grid` attribute, an LPJmLData object of
#' variable `"grid"` (`"LPJGRID"`) or a vector of cell-center latitude
#' coordinates in degrees.
#' @param res_lon Grid resolution in longitude direction in degrees
#' @param cellsize_lon Grid resolution in longitude direction in degrees
#' (default: `0.5`). If `x` is an LPJmLData object the resolution will be
#' taken from the meta data included in `x` if available.
#' @param res_lat Grid resolution in latitude direction in degrees (default:
#' same as `res_lon`). If `x` is an LPJmLData object the resolution will be
#' @param cellsize_lat Grid resolution in latitude direction in degrees (default:
#' same as `cellsize_lon`). If `x` is an LPJmLData object the resolution will be
#' taken from the meta data included in `x` if available.
#' @param earth_radius Radius of the sphere (in \eqn{m}) used to calculate the
#' cell areas.
Expand All @@ -32,8 +32,8 @@
#'
#' @export
calc_cellarea <- function(x, # nolint:cyclocomp_linter.
res_lon = 0.5,
res_lat = res_lon,
cellsize_lon = 0.5,
cellsize_lat = cellsize_lon,
earth_radius = 6371000.785,
return_unit = "m2"
) {
Expand All @@ -58,14 +58,14 @@ calc_cellarea <- function(x, # nolint:cyclocomp_linter.
stop("Grid attribute is missing. Use method add_grid() to add it.")
}

if (!is.null(x$meta$cellsize_lon) && any(res_lon != x$meta$cellsize_lon)) {
res_lon <- x$meta$cellsize_lon
warning("Using x$meta$cellsize_lon instead of supplied res_lon.")
if (!is.null(x$meta$cellsize_lon) && any(cellsize_lon != x$meta$cellsize_lon)) {
cellsize_lon <- x$meta$cellsize_lon
warning("Using x$meta$cellsize_lon instead of supplied cellsize_lon.")
}

if (!is.null(x$meta$cellsize_lat) && any(res_lat != x$meta$cellsize_lat)) {
res_lat <- x$meta$cellsize_lat
warning("Using x$meta$cellsize_lat instead of supplied res_lat.")
if (!is.null(x$meta$cellsize_lat) && any(cellsize_lat != x$meta$cellsize_lat)) {
cellsize_lat <- x$meta$cellsize_lat
warning("Using x$meta$cellsize_lat instead of supplied cellsize_lat.")
}

# Check for format of space dimensions, apply different processing
Expand Down Expand Up @@ -94,22 +94,22 @@ calc_cellarea <- function(x, # nolint:cyclocomp_linter.
}

# Check for irregular grid resolution arguments
if (length(res_lon) > 1) {
warning("res_lon has length ", length(res_lon), ". Using first element.")
res_lon <- res_lon[1]
if (length(cellsize_lon) > 1) {
warning("cellsize_lon has length ", length(cellsize_lon), ". Using first element.")
cellsize_lon <- cellsize_lon[1]
}
if (length(res_lon) == 0 || is.na(res_lon)) {
stop("Invalid longitude grid resolution 'res_lon'")
if (length(cellsize_lon) == 0 || is.na(cellsize_lon)) {
stop("Invalid longitude grid resolution 'cellsize_lon'")
}
res_lon <- as.double(res_lon)
if (length(res_lat) > 1) {
warning("res_lat has length ", length(res_lat), ". Using first element.")
res_lat <- res_lat[1]
cellsize_lon <- as.double(cellsize_lon)
if (length(cellsize_lat) > 1) {
warning("cellsize_lat has length ", length(cellsize_lat), ". Using first element.")
cellsize_lat <- cellsize_lat[1]
}
if (length(res_lat) == 0 || is.na(res_lat)) {
stop("Invalid latitude grid resolution 'res_lat'")
if (length(cellsize_lat) == 0 || is.na(cellsize_lat)) {
stop("Invalid latitude grid resolution 'cellsize_lat'")
}
res_lat <- as.double(res_lat)
cellsize_lat <- as.double(cellsize_lat)

# Check for irregular latitude coordinates
if (any(x < -90 | x > 90, na.rm = TRUE)) {
Expand All @@ -118,7 +118,7 @@ calc_cellarea <- function(x, # nolint:cyclocomp_linter.

cellwidth <- earth_radius * pi / 180

cellwidth * res_lon * cellwidth * res_lat * cos(x / 180 * pi) %>%
cellwidth * cellsize_lon * cellwidth * cellsize_lat * cos(x / 180 * pi) %>%

# Apply conversion factor based on return_unit parameter
switch(return_unit,
Expand Down
34 changes: 22 additions & 12 deletions R/check_config.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
#' @param model_path Character string providing the path to LPJmL
#' (equal to `LPJROOT` environment variable). Defaults to ".".
#'
#' @param output_path Character string providing path where an output, a restart
#' and a configuration folder are created. If `NULL`, `model_path` is used.
#' @param sim_path Character string defining path where all simulation data are
#' written, including output, restart and configuration files. If `NULL`,
#' `model_path` is used. See also [write_config]
#'
#' @param return_output Parameter affecting the output. If `FALSE` print
#' stdout/stderr message. If `TRUE`, return the result of the check.
Expand All @@ -21,6 +22,9 @@
#' @param raise_error Logical. Whether to raise an error if sub-process has
#' non-zero exit status. Defaults to `FALSE`.
#'
#' @param output_path Argument is deprecated as of version 1.0; use sim_path
#' instead.
#'
#' @return NULL.
#'
#' @examples
Expand All @@ -29,7 +33,7 @@
#' library(lpjmlkit)
#'
#' model_path <- "./LPJmL_internal"
#' output_path <-"./my_runs"
#' sim_path <-"./my_runs"
#'
#'
#' # Basic usage
Expand All @@ -42,25 +46,30 @@
#' )
#'
#' config_details <- write_config(
#' params = my_params,
#' x = my_params,
#' model_path = model_path,
#' output_path = output_path
#' sim_path = sim_path
#' )
#'
#' check_config(x = config_details,
#' model_path = model_path,
#' output_path = output_path,
#' sim_path = sim_path,
#' return_output = FALSE
#' )
#' }
#' @export
check_config <- function(x,
model_path = ".",
output_path = NULL,
sim_path = NULL,
return_output = FALSE,
raise_error = FALSE) {
raise_error = FALSE,
output_path = NULL) {

sim_path <- deprecate_arg(new_arg = sim_path,
deprec_arg = output_path,
version = "1.0.0")

if (is.null(output_path)) output_path <- model_path
if (is.null(sim_path)) sim_path <- model_path

# Check if x is character (vector). If so convert to tibble for the following.
if (methods::is(x, "character")) {
Expand All @@ -78,7 +87,7 @@ check_config <- function(x,
config_files <- paste0("config_", x$sim_name, ".json")

if (length(config_files) > 1) {
files <- paste0(output_path,
files <- paste0(sim_path,
"/configurations/",
config_files,
collapse = " ")
Expand All @@ -97,7 +106,7 @@ check_config <- function(x,
} else {
inner_command <- paste0(model_path,
"/bin/lpjcheck ", # nolint:absolute_path_linter.
output_path,
sim_path,
"/configurations/",
config_files)
}
Expand All @@ -107,7 +116,8 @@ check_config <- function(x,
check <- processx::run(command = "sh",
args = c("-c", inner_command),
error_on_status = raise_error,
cleanup_tree = TRUE)
cleanup_tree = TRUE,
wd = sim_path)

if (!return_output) {
return(
Expand Down
55 changes: 0 additions & 55 deletions R/get_git_urlhash.R

This file was deleted.

15 changes: 3 additions & 12 deletions R/read_io.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ read_io <- function( # nolint:cyclocomp_linter.
# Switch off fancy quotes but revert setting when leaving the function
quotes_option <- options(useFancyQuotes = FALSE) # nolint:undesirable_function_linter.
on.exit(options(quotes_option)) # nolint:undesirable_function_linter.

# Detect file_type if not provided by user
if (is.null(file_type)) {
file_type <- detect_io_type(filename)
Expand All @@ -196,6 +197,7 @@ read_io <- function( # nolint:cyclocomp_linter.
"This function can read files of type ", toString(dQuote(supported_types))
)
}

# Check valid dim_order
valid_dim_names <- c("cell", "time", "band")
if (!all(dim_order %in% valid_dim_names)) {
Expand Down Expand Up @@ -620,7 +622,7 @@ read_io_metadata_meta <- function(filename, file_type, band_names,
}
}
# Prepare additional attributes to be added to metadata
additional_attributes <- sapply(
additional_attributes <- sapply( # nolint:undesirable_function_linter.
set_args,
function(x) unname(get(x)),
simplify = FALSE
Expand Down Expand Up @@ -880,14 +882,3 @@ check_year_subset <- function(subset, meta_data, silent = FALSE) {
}
invisible(subset)
}

# Utility function to replace missing attribute with default value
default <- function(value, default) {
if (is.null(value)) {
return(default)
} else {
return(value)
}
}
# file_type options supported by read_io
supported_types <- c("raw", "clm", "meta")
Loading

0 comments on commit ad2f3a7

Please sign in to comment.