Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dieghernan committed Aug 27, 2024
1 parent 9196aa0 commit ca10cbc
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 190 deletions.
14 changes: 0 additions & 14 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -508,18 +508,4 @@ references:
year: '2024'
doi: 10.32614/CRAN.package.testthat
version: '>= 3.0.0'
- type: software
title: tidyterra
abstract: 'tidyterra: ''tidyverse'' Methods and ''ggplot2'' Helpers for ''terra''
Objects'
notes: Suggests
url: https://dieghernan.github.io/tidyterra/
repository: https://CRAN.R-project.org/package=tidyterra
authors:
- family-names: Hernangómez
given-names: Diego
email: [email protected]
orcid: https://orcid.org/0000-0001-8457-4658
year: '2024'
doi: 10.32614/CRAN.package.tidyterra

5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ Suggests:
scales,
sf (>= 0.9.0),
terra,
testthat (>= 3.0.0),
tidyterra
testthat (>= 3.0.0)
VignetteBuilder:
knitr
Config/Needs/website: cpp11, crosstalk, devtools, geofacet, geoR, gifski,
gstat, leaflet, reactable, scales, sf, tidyverse, usethis
gstat, leaflet, reactable, scales, tidyterra, tidyverse, usethis
Config/testthat/edition: 3
Config/testthat/parallel: true
Copyright: © AEMET. See file COPYRIGHTS
Expand Down
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
- `aemet_alerts()` to get current meteorological alerts issued by AEMET.
- Helper function `aemet_alert_zones()` to obtain the zoning defined by
AEMET for the alerts.
- `aemet_forecast_fires()` to get a `SpatRaster` with the forecasted risk
- `aemet_forecast_fires()` to get a `SpatRaster` with the forecast of risk
level of wildfires.
- Increase timeout limit with `httr2::req_timeout()`.
- Better management of non valid/duplicated/empty API keys.
- New packages added to 'Suggest': **terra** and **tidyterra**.
- New packages added to 'Suggest': **terra**.

# climaemet 1.3.0

Expand Down
79 changes: 35 additions & 44 deletions R/aemet_forecast_fires.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@
#' object.
#'
#' @details
#' The `SpatRaster` provides 5 ([numeric()])levels with the following meaning:
#' - `1`: Low risk.
#' - `2`: Moderate risk.
#' - `3`: High risk.
#' - `4`: Very high risk.
#' - `5`: Extreme risk.
#' The `SpatRaster` provides 5 ([factor()])levels with the following meaning:
#' - `"1"`: Low risk.
#' - `"2"`: Moderate risk.
#' - `"3"`: High risk.
#' - `"4"`: Very high risk.
#' - `"5"`: Extreme risk.
#'
#' The resulting object has several layers, each one representing the forecast
#' for the upcoming 7 days. It also has additional attributes provided by the
#' \CRANpkg{terra} package, such as [terra::time()] and [terra::coltab()].
#'
#'
#'
#' @export
#'
#' @examplesIf aemet_detect_api_key()
Expand All @@ -43,45 +41,22 @@
#' # Extract alerts
#' alerts <- aemet_forecast_fires()
#'
#' # Convert to categorical (factors)
#' library(terra)
#' alerts <- terra::as.factor(alerts)
#'
#' alerts
#'
#' # Nice plotting
#' library(ggplot2)
#' library(tidyterra)
#'
#' pal <- c("#00f6f6", "#00ff00", "#ffff00", "#ff7f00", "#ff0000")
#' names(pal) <- as.character(seq_len(5))
#'
#' ggplot() +
#' geom_spatraster(data = alerts) +
#' facet_wrap(~lyr, nrow = 2) +
#' scale_fill_manual(
#' values = pal,
#' na.value = "transparent",
#' breaks = names(pal),
#' labels = c("Low", "Moderate", "High", "Very high", "Extreme")
#' )
#'
#' # Nice plotting with terra
#' library(terra)
#' plot(alerts)
#'
#' # Zoom in an area
#' cyl <- mapSpain::esp_get_ccaa("Castilla y Leon", epsg = 4326)
#'
#' fires_cyl <- terra::crop(alerts, cyl)
#'
#' ggplot() +
#' geom_spatraster(data = fires_cyl) +
#' geom_sf(data = cyl, color = "black", linewidth = 0.5, fill = NA) +
#' facet_wrap(~lyr, nrow = 2) +
#' scale_fill_manual(
#' values = pal,
#' na.value = "transparent",
#' breaks = names(pal),
#' labels = c("Low", "Moderate", "High", "Very high", "Extreme")
#' )
#' # SpatVector
#' cyl <- vect(cyl)
#'
#' fires_cyl <- crop(alerts, cyl)
#' plot(fires_cyl[[1]])
#' plot(cyl, add = TRUE)
#'
#' @export
aemet_forecast_fires <- function(area = c("p", "c"), verbose = FALSE,
extract_metadata = FALSE) {
Expand Down Expand Up @@ -135,11 +110,27 @@ aemet_forecast_fires <- function(area = c("p", "c"), verbose = FALSE,
dbase$date <- dbase$base_date + dbase$offset

# Now create rasters
rrast <- terra::rast(dbase$file)

rrast <- lapply(dbase$file, terra::rast)

rrast <- do.call("c", rrast)
# To factors and NaNs to NA
rrast[is.nan(rrast)] <- NA
rrast <- terra::as.factor(rrast)

# coltab
ctab <- data.frame(value = seq_len(5), col = c(
"#00f6f6", "#00ff00", "#ffff00",
"#ff7f00", "#ff0000"
))

# iter
it <- seq_len(terra::nlyr(rrast))

for (i in it) {
terra::coltab(rrast, layer = i) <- ctab
}

# Time attributes

terra::time(rrast) <- dbase$date
names(rrast) <- format(dbase$date, format = "%Y-%m-%d")

Expand Down
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,21 @@ library(climaemet)
# See a tibble in action

aemet_last_obs("9434")
#> # A tibble: 13 × 25
#> # A tibble: 12 × 25
#> idema lon fint prec alt vmax vv dv lat dmax
#> <chr> <dbl> <dttm> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 9434 -1.00 2024-08-26 04:00:00 0 249 9.8 6 296 41.7 300
#> 2 9434 -1.00 2024-08-26 05:00:00 0 249 8 5.6 291 41.7 288
#> 3 9434 -1.00 2024-08-26 06:00:00 0 249 8.2 5.6 297 41.7 288
#> 4 9434 -1.00 2024-08-26 07:00:00 0 249 6.1 3.9 306 41.7 308
#> 5 9434 -1.00 2024-08-26 08:00:00 0 249 8.3 5.4 313 41.7 305
#> 6 9434 -1.00 2024-08-26 09:00:00 0 249 8.4 5.3 319 41.7 295
#> 7 9434 -1.00 2024-08-26 10:00:00 0 249 8.1 5 306 41.7 325
#> 8 9434 -1.00 2024-08-26 11:00:00 0 249 7.6 4.6 309 41.7 295
#> 9 9434 -1.00 2024-08-26 12:00:00 0 249 7.5 3.2 307 41.7 278
#> 10 9434 -1.00 2024-08-26 13:00:00 0 249 6 3 317 41.7 305
#> 11 9434 -1.00 2024-08-26 14:00:00 0 249 5.7 2.4 300 41.7 293
#> 12 9434 -1.00 2024-08-26 15:00:00 0 249 5.2 2.2 331 41.7 305
#> 13 9434 -1.00 2024-08-26 16:00:00 0 249 4.6 1.6 295 41.7 350
#> 1 9434 -1.00 2024-08-26 20:00:00 0 249 4 2.8 269 41.7 285
#> 2 9434 -1.00 2024-08-26 21:00:00 0 249 4.4 2.8 285 41.7 280
#> 3 9434 -1.00 2024-08-26 22:00:00 0 249 5.9 4.1 317 41.7 323
#> 4 9434 -1.00 2024-08-26 23:00:00 0 249 7.8 4.2 312 41.7 238
#> 5 9434 -1.00 2024-08-27 00:00:00 0 249 6.3 4.5 292 41.7 288
#> 6 9434 -1.00 2024-08-27 01:00:00 0 249 6.5 4.9 284 41.7 290
#> 7 9434 -1.00 2024-08-27 02:00:00 0 249 6.6 2.8 312 41.7 288
#> 8 9434 -1.00 2024-08-27 03:00:00 0 249 5.2 3.5 281 41.7 288
#> 9 9434 -1.00 2024-08-27 04:00:00 0 249 4.9 3.2 294 41.7 288
#> 10 9434 -1.00 2024-08-27 05:00:00 0 249 5.9 2.1 307 41.7 220
#> 11 9434 -1.00 2024-08-27 06:00:00 0 249 3.6 2 295 41.7 303
#> 12 9434 -1.00 2024-08-27 07:00:00 0 249 3.7 2.5 310 41.7 298
#> # ℹ 15 more variables: ubi <chr>, pres <dbl>, hr <dbl>, stdvv <dbl>, ts <dbl>,
#> # pres_nmar <dbl>, tamin <dbl>, ta <dbl>, tamax <dbl>, tpr <dbl>,
#> # stddv <dbl>, inso <dbl>, tss5cm <dbl>, pacutp <dbl>, tss20cm <dbl>
Expand Down Expand Up @@ -173,12 +172,12 @@ knitr::kable(head(data_observation))

| idema | lon | fint | prec | alt | vmax | vv | dv | lat | dmax | ubi | pres | hr | stdvv | ts | pres_nmar | tamin | ta | tamax | tpr | stddv | inso | tss5cm | pacutp | tss20cm |
|:------|----------:|:--------------------|-----:|----:|-----:|----:|----:|---------:|-----:|:--------------------|------:|----:|------:|-----:|----------:|------:|-----:|------:|-----:|------:|-----:|-------:|-------:|--------:|
| 9434 | -1.004167 | 2024-08-26 04:00:00 | 0 | 249 | 9.8 | 6.0 | 296 | 41.66056 | 300 | ZARAGOZA AEROPUERTO | 990.4 | 64 | 0.9 | 18.1 | 1020.0 | 17.7 | 17.8 | 18.3 | 10.9 | 6 | 0.0 | 23.8 | 0 | 28.2 |
| 9434 | -1.004167 | 2024-08-26 05:00:00 | 0 | 249 | 8.0 | 5.6 | 291 | 41.66056 | 288 | ZARAGOZA AEROPUERTO | 990.3 | 66 | 0.6 | 17.8 | 1019.9 | 17.4 | 17.4 | 17.8 | 11.0 | 6 | 0.0 | 23.5 | 0 | 27.9 |
| 9434 | -1.004167 | 2024-08-26 06:00:00 | 0 | 249 | 8.2 | 5.6 | 297 | 41.66056 | 288 | ZARAGOZA AEROPUERTO | 990.7 | 67 | 0.6 | 17.8 | 1020.4 | 17.1 | 17.2 | 17.4 | 11.0 | 5 | 9.4 | 23.1 | 0 | 27.6 |
| 9434 | -1.004167 | 2024-08-26 07:00:00 | 0 | 249 | 6.1 | 3.9 | 306 | 41.66056 | 308 | ZARAGOZA AEROPUERTO | 991.0 | 65 | 0.4 | 20.7 | 1020.6 | 17.2 | 18.0 | 18.0 | 11.3 | 8 | 60.0 | 23.2 | 0 | 27.3 |
| 9434 | -1.004167 | 2024-08-26 08:00:00 | 0 | 249 | 8.3 | 5.4 | 313 | 41.66056 | 305 | ZARAGOZA AEROPUERTO | 990.9 | 62 | 1.1 | 20.6 | 1020.4 | 18.0 | 19.0 | 19.0 | 11.6 | 9 | 45.4 | 23.9 | 0 | 27.1 |
| 9434 | -1.004167 | 2024-08-26 09:00:00 | 0 | 249 | 8.4 | 5.3 | 319 | 41.66056 | 295 | ZARAGOZA AEROPUERTO | 991.0 | 58 | 0.8 | 24.2 | 1020.3 | 19.0 | 20.7 | 20.7 | 12.1 | 10 | 53.0 | 24.5 | 0 | 26.9 |
| 9434 | -1.004167 | 2024-08-26 20:00:00 | 0 | 249 | 4.0 | 2.8 | 269 | 41.66056 | 285 | ZARAGOZA AEROPUERTO | 986.5 | 49 | 0.2 | 26.9 | 1015.0 | 27.2 | 27.2 | 28.6 | 15.5 | 7 | 0 | 31.3 | 0 | 30.6 |
| 9434 | -1.004167 | 2024-08-26 21:00:00 | 0 | 249 | 4.4 | 2.8 | 285 | 41.66056 | 280 | ZARAGOZA AEROPUERTO | 986.8 | 49 | 0.4 | 26.3 | 1015.4 | 26.1 | 26.6 | 27.2 | 15.0 | 10 | 0 | 30.4 | 0 | 30.7 |
| 9434 | -1.004167 | 2024-08-26 22:00:00 | 0 | 249 | 5.9 | 4.1 | 317 | 41.66056 | 323 | ZARAGOZA AEROPUERTO | 986.9 | 55 | 0.6 | 24.8 | 1015.6 | 24.8 | 24.8 | 26.7 | 15.2 | 9 | 0 | 29.7 | 0 | 30.6 |
| 9434 | -1.004167 | 2024-08-26 23:00:00 | 0 | 249 | 7.8 | 4.2 | 312 | 41.66056 | 238 | ZARAGOZA AEROPUERTO | 987.0 | 61 | 0.9 | 23.4 | 1015.9 | 23.2 | 23.2 | 24.8 | 15.3 | 17 | 0 | 29.0 | 0 | 30.5 |
| 9434 | -1.004167 | 2024-08-27 00:00:00 | 0 | 249 | 6.3 | 4.5 | 292 | 41.66056 | 288 | ZARAGOZA AEROPUERTO | 987.0 | 62 | 0.5 | 22.9 | 1016.0 | 22.5 | 22.5 | 23.2 | 14.9 | 6 | 0 | 28.4 | 0 | 30.3 |
| 9434 | -1.004167 | 2024-08-27 01:00:00 | 0 | 249 | 6.5 | 4.9 | 284 | 41.66056 | 290 | ZARAGOZA AEROPUERTO | 986.8 | 63 | 0.4 | 22.4 | 1015.8 | 22.0 | 22.0 | 22.5 | 14.6 | 6 | 0 | 27.8 | 0 | 30.1 |

``` r

Expand Down
14 changes: 1 addition & 13 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,6 @@
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=testthat"
},
{
"@type": "SoftwareApplication",
"identifier": "tidyterra",
"name": "tidyterra",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=tidyterra"
}
],
"softwareRequirements": {
Expand Down Expand Up @@ -375,7 +363,7 @@
},
"applicationCategory": "Meteorology",
"isPartOf": "https://ropenspain.es/",
"fileSize": "1023.037KB",
"fileSize": "861.704KB",
"citation": [
{
"@type": "SoftwareSourceCode",
Expand Down
52 changes: 14 additions & 38 deletions man/aemet_forecast_fires.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/aemet_forecast_fires.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Errors

Code
aemet_forecast_fires("Idonotexist")
Condition
Error in `match.arg()`:
! 'arg' should be one of "p", "c"

37 changes: 37 additions & 0 deletions tests/testthat/test-aemet_forecast_fires.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
test_that("Errors", {
skip_on_cran()
skip_if_offline()
skip_if_not(aemet_detect_api_key(), message = "No API KEY")

expect_snapshot(aemet_forecast_fires("Idonotexist"), error = TRUE)
})

test_that("Metadata", {
skip_on_cran()
skip_if_offline()
skip_if_not(aemet_detect_api_key(), message = "No API KEY")

meta <- aemet_forecast_fires(extract_metadata = TRUE)
expect_s3_class(meta, "tbl")
})


test_that("rasters", {
skip_on_cran()
skip_if_offline()
skip_if_not(aemet_detect_api_key(), message = "No API KEY")

rr <- aemet_forecast_fires()
expect_s4_class(rr, "SpatRaster")

expect_gt(terra::nlyr(rr), 5)
expect_true(all(terra::is.factor(rr)))
expect_true(all(terra::has.colors(rr)))
expect_true(terra::has.time(rr))

# Should be different for c
cc <- aemet_forecast_fires("c")
expect_s4_class(cc, "SpatRaster")
expect_true(terra::identical(rr, rr))
expect_false(terra::identical(rr, cc))
})
Loading

0 comments on commit ca10cbc

Please sign in to comment.