Skip to content

Commit

Permalink
Merge branch 'fix_get_cellindex' into 'master'
Browse files Browse the repository at this point in the history
Fix get cellindex

See merge request lpjml/lpjmlkit!94
  • Loading branch information
jnnsbrr committed Apr 9, 2024
2 parents e5dafe8 + a1d63fe commit c42a8f8
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '2793210'
ValidationKey: '2814724'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

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

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

- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9

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.4.1
date-released: '2024-03-28'
version: 1.4.2
date-released: '2024-04-09'
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.4.1
Version: 1.4.2
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 @@ -54,4 +54,4 @@ Suggests:
sf
Config/testthat/edition: 3
VignetteBuilder: knitr
Date: 2024-03-28
Date: 2024-04-09
39 changes: 28 additions & 11 deletions R/get_cellindex.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#' to correct it.
#'
#' @param grid_filename A string representing the grid file name.
#' @param extent A numeric vector of length 4 representing the extent
#' (lonmin, lonmax, latmin, latmax).
#' @param coordinates A list of two numeric vectors representing the coordinates.
#' @param extent A numeric vector (lonmin, lonmax, latmin, latmax) containing the
#' longitude and latitude boundaries between which values included in the subset.
#' @param coordinates A list of two named (lon, lat) numeric vectors representing the coordinates.
#'
#'
#' @return The cell index from the grid file based on the provided extent or
Expand All @@ -22,11 +22,11 @@
#' \dontrun{
#' get_cellindex(
#' grid_filename = "my_grid.bin.json",
#' extent = c(-123.25, -122.75, 49.25, 49.75)
#' extent = c(-123.25, -122.75, 49.25, 49.75) # (lonmin, lonmax, latmin, latmax)
#' )
#' get_cellindex(
#' grid_filename = "my_grid.bin.json",
#' coordinates = list(c(-123.25, -122.75), c(49.25, 49.75))
#' coordinates = list(lon = c(-123.25, -122.75), lat = c(49.25, 49.75))
#' )
#' }
#' @details
Expand Down Expand Up @@ -64,7 +64,7 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL) {
extent <- check_extent(extent) %>%
correct_extent()
} else if (!is.null(coordinates)) {
check_coordinates_length(coordinates)
coordinates <- check_coordinates(coordinates)
}

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

grid_cell <- transform(grid_lonlat, "lon_lat")

grid_cell$subset(coordinates = coordinates)
grid_cell$subset(coordinates = lapply(X = coordinates, FUN = as.character))

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

Expand Down Expand Up @@ -165,7 +165,6 @@ check_extent <- function(extent) {
}


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

# Check if both extent and coordinates are provided
check_extent_and_coordinates <- function(extent, coordinates) {
Expand Down Expand Up @@ -193,12 +192,30 @@ correct_extent <- function(extent) {
warning("Swapped values of latmin and latmax.")
}
}
return(extent)

extent
}

# Check the length of coordinates
check_coordinates_length <- function(coordinates) {
# Check if the coordinates are a list of two numeric vectors and of equal length
check_coordinates <- function(coordinates) {
if (!is.list(coordinates) || length(coordinates) != 2) {
stop("coordinates must be a list of two vectors.")
}

coordinates <- lapply(coordinates, function(coord) {
if (!is.numeric(coord)) {
warning("Non-numeric coordinates detected, attempting to convert to numeric.")
coord <- as.numeric(coord)
if (any(is.na(coord))) {
stop("Unable to convert all coordinates to numeric.")
}
}
coord
})

if (length(coordinates[[1]]) != length(coordinates[[2]])) {
stop("The two vectors in coordinates must have the same length.")
}

coordinates
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Toolkit for Basic LPJmL Handling <a href=''><img src='inst/img/logo.png' align='right' height='139' /></a>

R package **lpjmlkit**, version **1.4.1**
R package **lpjmlkit**, version **1.4.2**

[![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, 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>.
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>.

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 Christoph Müller},
year = {2024},
note = {R package version 1.4.1},
note = {R package version 1.4.2},
doi = {10.5281/zenodo.7773134},
url = {https://github.com/PIK-LPJmL/lpjmlkit},
}
Expand Down
10 changes: 5 additions & 5 deletions man/get_cellindex.Rd

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

8 changes: 4 additions & 4 deletions tests/testthat/test-get_cellindex.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ test_that("get_cellindex handles extent values out of order", {
})

test_that("get_cellindex handles valid coordinates", {
expect_error(
expect_warning(
get_cellindex(
"../testdata/output/grid.bin.json",
coordinates = list(c(-87.25, -87.25), c(55.25, 55.75))
coordinates = list(lon = c("-87.25", -87.25), lat = c(55.25, 55.75))
),
"Values for coordinate pairs must be supplied as strings"
"Non-numeric coordinates detected"
)
})

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

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

0 comments on commit c42a8f8

Please sign in to comment.