From 09e7b0def53840a316bea86e71ac2a88e13b5b7f Mon Sep 17 00:00:00 2001 From: Jannes Breier Date: Mon, 25 Mar 2024 16:10:43 +0100 Subject: [PATCH] Fix fmt check runner --- .buildlibrary | 2 +- CITATION.cff | 4 +- DESCRIPTION | 4 +- R/write_config.R | 83 ++++++++++++++++++++---------- README.md | 6 +-- man/write_config.Rd | 9 ++-- tests/testthat/test-write_config.R | 1 - 7 files changed, 70 insertions(+), 39 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index ab559cf..b845ea4 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '2613204' +ValidationKey: '2634331' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 0e6aa4b..b6f7112 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,8 +2,8 @@ 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.3.2 -date-released: '2024-03-15' +version: 1.3.3 +date-released: '2024-03-25' 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 the Potsdam Institute for Climate Impact Research (PIK). It provides functions for diff --git a/DESCRIPTION b/DESCRIPTION index 16a70ac..4559e0e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: lpjmlkit Type: Package Title: Toolkit for Basic LPJmL Handling -Version: 1.3.2 +Version: 1.3.3 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")), @@ -54,4 +54,4 @@ Suggests: sf Config/testthat/edition: 3 VignetteBuilder: knitr -Date: 2024-03-15 +Date: 2024-03-25 diff --git a/R/write_config.R b/R/write_config.R index 29a6758..7a63dec 100644 --- a/R/write_config.R +++ b/R/write_config.R @@ -3,7 +3,8 @@ #' Requires a \link[tibble]{tibble} (modern \link[base]{data.frame} class) in a #' specific format (see details & examples) to write the model configuration #' file `"config_*.json"`. Each row in the tibble corresponds to a model run. -#' The generated `"config_*.json"` is based on a js file (e.g. `"lpjml_*.js"`). +#' The generated `"config_*.json"` is based on a cjson file +#' (e.g. `"lpjml_config.cjson"`). #' #' @param x A tibble in a defined format (see details). #' @@ -26,8 +27,8 @@ #' individually. Choose between `"annual"`, `"monthly"` or `"daily"`. #' #' @param output_format Character string defining the format of the output. -#' Defaults to `"raw"`. Further options: `"cdf"` (NetCDF) or `"clm"` -#' (file with header). +#' Defaults to `NULL` (use default from cjson file). Options: `"raw"`, +#' `"cdf"` (NetCDF) or `"clm"` (file with header). #' #' @param cjson_filename Character string providing the name of the main LPJmL #' configuration file to be parsed. Defaults to `"lpjml_config.cjson"`. @@ -268,7 +269,7 @@ write_config <- function(x, sim_path = NULL, output_list = c(), output_list_timestep = "annual", - output_format = "raw", + output_format = NULL, cjson_filename = "lpjml_config.cjson", parallel_cores = 4, debug = FALSE, @@ -622,10 +623,29 @@ mutate_config_output <- function(x, # nolint:cyclocomp_linter. for (x_id in seq_len(length(x[["output"]]))) { # Replace output format in x if defined (e.g. raw, clm, cdf) - if (x[["output"]][[x_id]]$file$fmt != "txt") { + if (!is.null(output_format) && + (is.null(x[["output"]][[x_id]]$file$fmt) || + x[["output"]][[x_id]]$file$fmt != "txt")) { x[["output"]][[x_id]]$file$fmt <- output_format } + # make it backwards compatible for old way of explicitly mentioning the + # file extension in the output file name + if (!is.null(output_format) && is.null(x[["default_fmt"]])) { + new_ext <- switch( + output_format, + raw = ".bin", + clm = ".clm", + cdf = ".nc4" + ) + # Replace file extension of file name in output + x[["output"]][[x_id]]$file$name <- sub( + "\\.[^.]+$", + new_ext, + x[["output"]][[x_id]]$file$name + ) + } + # Replace output path in x x[["output"]][[x_id]]$file$name <- gsub("output/", opath, @@ -641,7 +661,7 @@ mutate_config_output <- function(x, # nolint:cyclocomp_linter. # Empty output and include grid if not done x["output"] <- list(c()) - if (!("grid" %in% output_list) && !("cdf" %in% output_format)) { + if (!("grid" %in% output_list)) { output_list <- append(output_list, "grid", after = 0) output_timestep <- append(output_timestep, NA, after = 0) @@ -666,17 +686,19 @@ mutate_config_output <- function(x, # nolint:cyclocomp_linter. new_output[["id"]] <- output_list[id_ov] new_output[["file"]] <- list() - # Output format three possibilities: netcdf: cdf, raw: bin and clm - new_output[["file"]][["fmt"]] <- ifelse( - length(output_format) == 1 && is.character(output_format), - ifelse(output_list[id_ov] == "globalflux", "txt", output_format), - stop( - "No valid output_format. Please choose in from \"raw\"", - " \"clm\" or \"cdf\" in form of a single character ", - "string." - ) - ) + if (!is.null(output_format)) { + # Output format three possibilities: netcdf: cdf, raw: bin and clm + new_output[["file"]][["fmt"]] <- ifelse( + length(output_format) == 1 && is.character(output_format), + ifelse(output_list[id_ov] == "globalflux", "txt", output_format), + stop( + "No valid output_format. Please choose in from \"raw\"", + " \"clm\" or \"cdf\" in form of a single character ", + "string." + ) + ) + } # Output_timestep could be supplied as a single character string # prescribing a timestep for all outputs or as a character vector # with the length of output_list to assign an individual timestep for @@ -738,17 +760,26 @@ mutate_config_output <- function(x, # nolint:cyclocomp_linter. # Create file name with correct path, corresponding outputvar name and # file extension based on the output_format - new_output[["file"]][["name"]] <- paste0( - opath, - output_list[id_ov], ".", - ifelse(output_list[id_ov] == "globalflux", - "txt", - switch(output_format, - raw = "bin", - clm = "clm", - cdf = "nc4") + # make it backwards compatible for old way of explicitly mentioning the + # file extension in the output file name + if (!is.null(output_format) && is.null(x[["default_fmt"]])) { + new_output[["file"]][["name"]] <- paste0( + opath, + output_list[id_ov], ".", + ifelse(output_list[id_ov] == "globalflux", + "txt", + switch(output_format, + raw = "bin", + clm = "clm", + cdf = "nc4") + ) ) - ) + } else { + new_output[["file"]][["name"]] <- paste0( + opath, + output_list[id_ov] + ) + } # Append new output to output in config x[["output"]] <- append(x[["output"]], list(new_output)) diff --git a/README.md b/README.md index b6c754f..b16a223 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Toolkit for Basic LPJmL Handling -R package **lpjmlkit**, version **1.3.2** +R package **lpjmlkit**, version **1.3.3** [![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) @@ -76,7 +76,7 @@ In case of questions / problems please contact Jannes Breier . +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.3.3, . A BibTeX entry for LaTeX users is @@ -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.3.2}, + note = {R package version 1.3.3}, doi = {10.5281/zenodo.7773134}, url = {https://github.com/PIK-LPJmL/lpjmlkit}, } diff --git a/man/write_config.Rd b/man/write_config.Rd index 749c426..0446dde 100644 --- a/man/write_config.Rd +++ b/man/write_config.Rd @@ -10,7 +10,7 @@ write_config( sim_path = NULL, output_list = c(), output_list_timestep = "annual", - output_format = "raw", + output_format = NULL, cjson_filename = "lpjml_config.cjson", parallel_cores = 4, debug = FALSE, @@ -41,8 +41,8 @@ a vector with the length of \code{output_list} defining each timestep individually. Choose between \code{"annual"}, \code{"monthly"} or \code{"daily"}.} \item{output_format}{Character string defining the format of the output. -Defaults to \code{"raw"}. Further options: \code{"cdf"} (NetCDF) or \code{"clm"} -(file with header).} +Defaults to \code{NULL} (use default from cjson file). Options: \code{"raw"}, +\code{"cdf"} (NetCDF) or \code{"clm"} (file with header).} \item{cjson_filename}{Character string providing the name of the main LPJmL configuration file to be parsed. Defaults to \code{"lpjml_config.cjson"}.} @@ -72,7 +72,8 @@ Run parameters \code{"order"} and \code{"dependency"} are included if defined in Requires a \link[tibble]{tibble} (modern \link[base]{data.frame} class) in a specific format (see details & examples) to write the model configuration file \code{"config_*.json"}. Each row in the tibble corresponds to a model run. -The generated \code{"config_*.json"} is based on a js file (e.g. \code{"lpjml_*.js"}). +The generated \code{"config_*.json"} is based on a cjson file +(e.g. \code{"lpjml_config.cjson"}). } \details{ Supply a \link[tibble]{tibble} for \code{x}, in which each row represents diff --git a/tests/testthat/test-write_config.R b/tests/testthat/test-write_config.R index 869d491..2874f05 100644 --- a/tests/testthat/test-write_config.R +++ b/tests/testthat/test-write_config.R @@ -351,7 +351,6 @@ test_that("include non output defined outputvars", { length(tmp_objects[[1]][["output"]]) ]]$file$name) ) - })