Skip to content

Commit

Permalink
Merge branch 'fix_split_time_names' into 'master'
Browse files Browse the repository at this point in the history
Fix split_time_names to enable negative years

See merge request lpjml/lpjmlkit!102
  • Loading branch information
DavidhoPIK committed Nov 26, 2024
2 parents 9da101e + 1dd9831 commit a2832b7
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '3458270'
ValidationKey: '3489222'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand All @@ -7,3 +7,4 @@ allowLinterWarnings: no
AddInReadme: inst/README.md
AddLogoReadme: inst/img/logo.png
enforceVersionUpdate: no
skipCoverage: no
18 changes: 9 additions & 9 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
any::lucode2
any::covr
any::madrat
any::magclass
any::citation
any::gms
any::goxygen
any::GDPuc
lucode2
covr
madrat
magclass
citation
gms
goxygen
GDPuc
# piam packages also available on CRAN (madrat, magclass, citation,
# gms, goxygen, GDPuc) will usually have an outdated binary version
# available; by using extra-packages we get the newest version
Expand Down Expand Up @@ -63,6 +63,6 @@ jobs:
shell: Rscript {0}
run: |
nonDummyTests <- setdiff(list.files("./tests/testthat/"), c("test-dummy.R", "_snaps"))
if(length(nonDummyTests) > 0) covr::codecov(quiet = FALSE)
if(length(nonDummyTests) > 0 && !lucode2:::loadBuildLibraryConfig()[["skipCoverage"]]) covr::codecov(quiet = FALSE)
env:
NOT_CRAN: "true"
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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.7.3
date-released: '2024-09-24'
version: 1.7.4
date-released: '2024-11-26'
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
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: 1.7.3
Version: 1.7.4
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 Expand Up @@ -55,4 +55,4 @@ Suggests:
sf
Config/testthat/edition: 3
VignetteBuilder: knitr
Date: 2024-09-24
Date: 2024-11-26
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export(read_io)
export(read_meta)
export(run_lpjml)
export(set_header_item)
export(split_time_names)
export(submit_lpjml)
export(subset.LPJmLData)
export(transform)
Expand Down
51 changes: 39 additions & 12 deletions R/time_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ create_time_names <- function(
mm <- {if (is.null(months)) seq_len(12) else months} %>% #nolint
sprintf("%02d", .)

# Daily data: YYYY-MM-DD
if (nstep == 365) {
# Daily data: YYYY-MM-DD
d_mmdd <- paste(
rep(x = mm, times = {
# Cases of months or days being subsetted or not.
Expand All @@ -44,36 +44,63 @@ create_time_names <- function(
), dd, sep = "-"
)

time_dimnames <- paste(rep(years, each = length(dd)),
rep(d_mmdd, times = length(years)), sep = "-")

# Monthly data: YYYY-MM-LastDayOfMonth
time_dimnames <- paste(
rep(years, each = length(dd)),
rep(d_mmdd, times = length(years)),
sep = "-"
)
} else if (nstep == 12) {
# Monthly data: YYYY-MM-LastDayOfMonth
m_mmdd <- paste(mm, ndays_in_month, sep = "-")

time_dimnames <- paste(
rep(years, each = length(ndays_in_month)),
rep(m_mmdd, times = length(years)),
sep = "-"
)

# Annual data: YYYY-12-31
} else if (nstep == 1) {
# Annual data: YYYY-12-31
time_dimnames <- paste(years, 12, 31, sep = "-")

# Currently no support for other (special) nstep cases
} else {
# Currently no support for other (special) nstep cases
stop("Invalid nstep: ", nstep, "\nnstep has to be 1, 12 or 365")
}

return(time_dimnames)
}

#' Split date strings into years, months and days
#'
#' Splits one or several date strings into a list of unique days, months and
#' years.
#'
#' @param time_names Character vector with one or several date strings in the
#' form YYYY-MM-DD.
#'
#' @return List containing unique `year`, `month` and `day` values included in
#' `time_names`.
#'
#' @examples
#' \dontrun{
#' time_names <- split_time_names(c("2024-11-25", "2024-11-26"))
#' time_names
#' # $year
#' # [1] "2024"
#' # $month
#' # [1] "11"
#' # $day
#' # [1] "25" "26"
#' }
#'
#' @md
#' @export
split_time_names <- function(time_names) {

# Split time string "year-month-day" into year, month, day integer vector
time_split <- strsplit(time_names, "-") %>%
lapply(function(x) as.character(as.integer(x)))
time_split <- regmatches(
time_names,
regexec("([-]?[[:digit:]]+)-([[:digit:]]+)-([[:digit:]]+)", time_names)
) %>% lapply(function(x) as.character(as.integer(x[-1])))

# Create corresponding dimnames for disaggregated array by unique entry
matrix(unlist(time_split),
Expand All @@ -83,5 +110,5 @@ split_time_names <- function(time_names) {
c("year", "month", "day"))) %>%
apply(2, unique) %>%
as.list() %>%
return()
return()
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# <a href=''><img src='inst/img/logo.png' align='right' alt='logo' height=139 /></a> Toolkit for Basic LPJmL Handling

R package **lpjmlkit**, version **1.7.3**
R package **lpjmlkit**, version **1.7.4**

[![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, Hötten D, Müller C (2024). _lpjmlkit: Toolkit for Basic LPJmL Handling_. doi:10.5281/zenodo.7773134 <https://doi.org/10.5281/zenodo.7773134>, R package version 1.7.3, <https://github.com/PIK-LPJmL/lpjmlkit>.
Breier J, Ostberg S, Wirth S, Minoli S, Stenzel F, Hötten D, Müller C (2024). _lpjmlkit: Toolkit for Basic LPJmL Handling_. doi:10.5281/zenodo.7773134 <https://doi.org/10.5281/zenodo.7773134>, R package version 1.7.4, <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 David Hötten and Christoph Müller},
year = {2024},
note = {R package version 1.7.3},
note = {R package version 1.7.4},
url = {https://github.com/PIK-LPJmL/lpjmlkit},
doi = {10.5281/zenodo.7773134},
}
Expand Down
33 changes: 33 additions & 0 deletions man/split_time_names.Rd

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

0 comments on commit a2832b7

Please sign in to comment.