Skip to content

Commit fea831b

Browse files
committed
Merge branch 'speed_up_get_cellindex' into 'master'
Avoid unnecesarry transfomrations of LPJmLData Object. See merge request lpjml/lpjmlkit!103
2 parents a2832b7 + bf60998 commit fea831b

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

.buildlibrary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ValidationKey: '3489222'
1+
ValidationKey: '3512075'
22
AutocreateReadme: yes
33
AcceptedWarnings:
44
- 'Warning: package ''.*'' was built under R version'

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exclude: '^tests/testthat/_snaps/.*$'
44
repos:
55
- repo: https://github.com/pre-commit/pre-commit-hooks
6-
rev: 2c9f875913ee60ca25ce70243dc24d5b6415598c # frozen: v4.6.0
6+
rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.0.0
77
hooks:
88
- id: check-case-conflict
99
- id: check-json
@@ -15,7 +15,7 @@ repos:
1515
- id: mixed-line-ending
1616

1717
- repo: https://github.com/lorenzwalthert/precommit
18-
rev: bae853d82da476eee0e0a57960ee6b741a3b3fb7 # frozen: v0.4.3
18+
rev: 3b70240796cdccbe1474b0176560281aaded97e6 # frozen: v0.4.3.9003
1919
hooks:
2020
- id: parsable-R
2121
- id: deps-in-desc

CITATION.cff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ cff-version: 1.2.0
22
message: If you use this software, please cite it using the metadata from this file.
33
type: software
44
title: 'lpjmlkit: Toolkit for Basic LPJmL Handling'
5-
version: 1.7.4
6-
date-released: '2024-11-26'
5+
version: 1.7.5
6+
date-released: '2024-12-12'
77
abstract: A collection of basic functions to facilitate the work with the Dynamic
88
Global Vegetation Model (DGVM) Lund-Potsdam-Jena managed Land (LPJmL) hosted at
99
the Potsdam Institute for Climate Impact Research (PIK). It provides functions for

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: lpjmlkit
22
Type: Package
33
Title: Toolkit for Basic LPJmL Handling
4-
Version: 1.7.4
4+
Version: 1.7.5
55
Authors@R: c(
66
person("Jannes", "Breier", , "jannesbr@pik-potsdam.de", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-9055-6904")),
77
person("Sebastian","Ostberg", , "ostberg@pik-potsdam.de", role = "aut", comment = c(ORCID = "0000-0002-2368-7015")),
@@ -55,4 +55,4 @@ Suggests:
5555
sf
5656
Config/testthat/edition: 3
5757
VignetteBuilder: knitr
58-
Date: 2024-11-26
58+
Date: 2024-12-12

R/get_cellindex.R

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL, shap
8787

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

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

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

113+
cells <- grid_cell$subset(coordinates = lapply(X = list(lon = cells$lon,
114+
lat = cells$lat),
115+
FUN = as.character))
116+
} else {
117+
cells <- cells$cellindex
118+
}
116119
}
117120

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

151154
if (!is.null(shape)) {
155+
grid_lonlat <- grid_lonlat$transform("lon_lat")
156+
152157
cell_coords <- grid_lonlat |>
153158
as_terra() |>
154159
terra::mask(shape) |>
155160
terra::as.data.frame(xy = TRUE) |>
156161
dplyr::select("x", "y")
157162

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

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

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# <a href=''><img src='inst/img/logo.png' align='right' alt='logo' height=139 /></a> Toolkit for Basic LPJmL Handling
22

3-
R package **lpjmlkit**, version **1.7.4**
3+
R package **lpjmlkit**, version **1.7.5**
44

55
[![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)
66

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

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

79-
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>.
79+
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>.
8080

8181
A BibTeX entry for LaTeX users is
8282

@@ -85,7 +85,7 @@ A BibTeX entry for LaTeX users is
8585
title = {lpjmlkit: Toolkit for Basic LPJmL Handling},
8686
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},
8787
year = {2024},
88-
note = {R package version 1.7.4},
88+
note = {R package version 1.7.5},
8989
url = {https://github.com/PIK-LPJmL/lpjmlkit},
9090
doi = {10.5281/zenodo.7773134},
9191
}

0 commit comments

Comments
 (0)