Skip to content

Commit

Permalink
Merge branch 'speed_up_get_cellindex' into 'master'
Browse files Browse the repository at this point in the history
Avoid unnecesarry transfomrations of LPJmLData Object.

See merge request lpjml/lpjmlkit!103
  • Loading branch information
jnnsbrr committed Dec 12, 2024
2 parents a2832b7 + bf60998 commit fea831b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '3489222'
ValidationKey: '3512075'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exclude: '^tests/testthat/_snaps/.*$'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 2c9f875913ee60ca25ce70243dc24d5b6415598c # frozen: v4.6.0
rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.0.0
hooks:
- id: check-case-conflict
- id: check-json
Expand All @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: bae853d82da476eee0e0a57960ee6b741a3b3fb7 # frozen: v0.4.3
rev: 3b70240796cdccbe1474b0176560281aaded97e6 # frozen: v0.4.3.9003
hooks:
- id: parsable-R
- id: deps-in-desc
Expand Down
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.4
date-released: '2024-11-26'
version: 1.7.5
date-released: '2024-12-12'
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.4
Version: 1.7.5
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-11-26
Date: 2024-12-12
21 changes: 13 additions & 8 deletions R/get_cellindex.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL, shap

# Check if extent values are within the longitude and latitude range in the cells
if (!is.null(extent)) {
cells$cellindex <- as.numeric(row.names(cells))
cells$cellindex <- as.numeric(row.names(cells)) + 1

out_of_bounds_lon <- extent[c(1, 2)][extent[c(1, 2)] < lon_range[1] |
extent[c(1, 2)] > lon_range[2]]
Expand All @@ -107,12 +107,15 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL, shap
cells$lon <= extent[2] &
cells$lat >= extent[3] & cells$lat <= extent[4], ]

grid_cell <- transform(grid_lonlat, "lon_lat")

cells <- grid_cell$subset(coordinates = lapply(X = list(lon = cells$lon,
lat = cells$lat),
FUN = as.character))
if (!simplify) {
grid_cell <- transform(grid_lonlat, "lon_lat")

cells <- grid_cell$subset(coordinates = lapply(X = list(lon = cells$lon,
lat = cells$lat),
FUN = as.character))
} else {
cells <- cells$cellindex
}
}

# Check if coordinates are within the longitude and latitude range in the cells
Expand Down Expand Up @@ -149,18 +152,20 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL, shap
}

if (!is.null(shape)) {
grid_lonlat <- grid_lonlat$transform("lon_lat")

cell_coords <- grid_lonlat |>
as_terra() |>
terra::mask(shape) |>
terra::as.data.frame(xy = TRUE) |>
dplyr::select("x", "y")

cells <- grid_lonlat$transform("lon_lat") |>
cells <- grid_lonlat |>
subset(coordinates = lapply(list(lon = cell_coords$x, lat = cell_coords$y),
FUN = as.character))
}

if (simplify) {
if (simplify && is.null(extent)) {
cells <- c(stats::na.omit(c(cells$data + 1)))
}

Expand Down
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.4**
R package **lpjmlkit**, version **1.7.5**

[![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.4, <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.5, <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.4},
note = {R package version 1.7.5},
url = {https://github.com/PIK-LPJmL/lpjmlkit},
doi = {10.5281/zenodo.7773134},
}
Expand Down

0 comments on commit fea831b

Please sign in to comment.