Skip to content

Commit c42a8f8

Browse files
committed
Merge branch 'fix_get_cellindex' into 'master'
Fix get cellindex See merge request lpjml/lpjmlkit!94
2 parents e5dafe8 + a1d63fe commit c42a8f8

File tree

8 files changed

+47
-31
lines changed

8 files changed

+47
-31
lines changed

.buildlibrary

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

.github/workflows/check.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212

1313
steps:
14-
- uses: actions/checkout@v3
14+
- uses: actions/checkout@v4
1515

1616
- uses: r-lib/actions/setup-pandoc@v2
1717

@@ -23,7 +23,6 @@ jobs:
2323
- uses: r-lib/actions/setup-r-dependencies@v2
2424
with:
2525
extra-packages: |
26-
gamstransfer=?ignore
2726
any::lucode2
2827
any::covr
2928
any::madrat
@@ -36,7 +35,7 @@ jobs:
3635
# gms, goxygen, GDPuc) will usually have an outdated binary version
3736
# available; by using extra-packages we get the newest version
3837

39-
- uses: actions/setup-python@v4
38+
- uses: actions/setup-python@v5
4039
with:
4140
python-version: 3.9
4241

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.4.1
6-
date-released: '2024-03-28'
5+
version: 1.4.2
6+
date-released: '2024-04-09'
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.4.1
4+
Version: 1.4.2
55
Authors@R: c(
66
person("Jannes", "Breier", , "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-9055-6904")),
77
person("Sebastian","Ostberg", , "[email protected]", role = "aut", comment = c(ORCID = "0000-0002-2368-7015")),
@@ -54,4 +54,4 @@ Suggests:
5454
sf
5555
Config/testthat/edition: 3
5656
VignetteBuilder: knitr
57-
Date: 2024-03-28
57+
Date: 2024-04-09

R/get_cellindex.R

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
#' to correct it.
1010
#'
1111
#' @param grid_filename A string representing the grid file name.
12-
#' @param extent A numeric vector of length 4 representing the extent
13-
#' (lonmin, lonmax, latmin, latmax).
14-
#' @param coordinates A list of two numeric vectors representing the coordinates.
12+
#' @param extent A numeric vector (lonmin, lonmax, latmin, latmax) containing the
13+
#' longitude and latitude boundaries between which values included in the subset.
14+
#' @param coordinates A list of two named (lon, lat) numeric vectors representing the coordinates.
1515
#'
1616
#'
1717
#' @return The cell index from the grid file based on the provided extent or
@@ -22,11 +22,11 @@
2222
#' \dontrun{
2323
#' get_cellindex(
2424
#' grid_filename = "my_grid.bin.json",
25-
#' extent = c(-123.25, -122.75, 49.25, 49.75)
25+
#' extent = c(-123.25, -122.75, 49.25, 49.75) # (lonmin, lonmax, latmin, latmax)
2626
#' )
2727
#' get_cellindex(
2828
#' grid_filename = "my_grid.bin.json",
29-
#' coordinates = list(c(-123.25, -122.75), c(49.25, 49.75))
29+
#' coordinates = list(lon = c(-123.25, -122.75), lat = c(49.25, 49.75))
3030
#' )
3131
#' }
3232
#' @details
@@ -64,7 +64,7 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL) {
6464
extent <- check_extent(extent) %>%
6565
correct_extent()
6666
} else if (!is.null(coordinates)) {
67-
check_coordinates_length(coordinates)
67+
coordinates <- check_coordinates(coordinates)
6868
}
6969

7070
# Read the grid file and create a data frame
@@ -120,7 +120,7 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL) {
120120

121121
grid_cell <- transform(grid_lonlat, "lon_lat")
122122

123-
grid_cell$subset(coordinates = coordinates)
123+
grid_cell$subset(coordinates = lapply(X = coordinates, FUN = as.character))
124124

125125
cells <- c(stats::na.omit(c(grid_cell$data + 1)))
126126

@@ -165,7 +165,6 @@ check_extent <- function(extent) {
165165
}
166166

167167

168-
# Check if the coordinates are a list of two numeric vectors of equal length
169168

170169
# Check if both extent and coordinates are provided
171170
check_extent_and_coordinates <- function(extent, coordinates) {
@@ -193,12 +192,30 @@ correct_extent <- function(extent) {
193192
warning("Swapped values of latmin and latmax.")
194193
}
195194
}
196-
return(extent)
195+
196+
extent
197197
}
198198

199-
# Check the length of coordinates
200-
check_coordinates_length <- function(coordinates) {
199+
# Check if the coordinates are a list of two numeric vectors and of equal length
200+
check_coordinates <- function(coordinates) {
201+
if (!is.list(coordinates) || length(coordinates) != 2) {
202+
stop("coordinates must be a list of two vectors.")
203+
}
204+
205+
coordinates <- lapply(coordinates, function(coord) {
206+
if (!is.numeric(coord)) {
207+
warning("Non-numeric coordinates detected, attempting to convert to numeric.")
208+
coord <- as.numeric(coord)
209+
if (any(is.na(coord))) {
210+
stop("Unable to convert all coordinates to numeric.")
211+
}
212+
}
213+
coord
214+
})
215+
201216
if (length(coordinates[[1]]) != length(coordinates[[2]])) {
202217
stop("The two vectors in coordinates must have the same length.")
203218
}
219+
220+
coordinates
204221
}

README.md

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

3-
R package **lpjmlkit**, version **1.4.1**
3+
R package **lpjmlkit**, version **1.4.2**
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, 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.4.1, <URL: https://github.com/PIK-LPJmL/lpjmlkit>.
79+
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.4.2, <URL: 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 Christoph Müller},
8787
year = {2024},
88-
note = {R package version 1.4.1},
88+
note = {R package version 1.4.2},
8989
doi = {10.5281/zenodo.7773134},
9090
url = {https://github.com/PIK-LPJmL/lpjmlkit},
9191
}

man/get_cellindex.Rd

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-get_cellindex.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ test_that("get_cellindex handles extent values out of order", {
4040
})
4141

4242
test_that("get_cellindex handles valid coordinates", {
43-
expect_error(
43+
expect_warning(
4444
get_cellindex(
4545
"../testdata/output/grid.bin.json",
46-
coordinates = list(c(-87.25, -87.25), c(55.25, 55.75))
46+
coordinates = list(lon = c("-87.25", -87.25), lat = c(55.25, 55.75))
4747
),
48-
"Values for coordinate pairs must be supplied as strings"
48+
"Non-numeric coordinates detected"
4949
)
5050
})
5151

@@ -58,7 +58,7 @@ test_that("get_cellindex returns correct cell index for given extent", {
5858

5959
test_that("get_cellindex returns correct cell index for given coordinates", {
6060
result <- get_cellindex("../testdata/output/grid.bin.json",
61-
coordinates = list(lon = c("-87.25", "-87.25"), lat = c("55.25", "55.75"))
61+
coordinates = list(lon = c(-87.25, -87.25), lat = c(55.25, 55.75))
6262
)
6363
expect_true(length(result) == 2 && result[1] == 10001 && result[2] == 10002)
6464
})

0 commit comments

Comments
 (0)