Skip to content

Commit

Permalink
Merge pull request #64 from rOpenSpain/fires
Browse files Browse the repository at this point in the history
Add aemet_forecast_fires
  • Loading branch information
dieghernan authored Aug 27, 2024
2 parents 8d06535 + ca10cbc commit 37837bd
Show file tree
Hide file tree
Showing 27 changed files with 399 additions and 85 deletions.
13 changes: 13 additions & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,19 @@ references:
year: '2024'
doi: 10.32614/CRAN.package.sf
version: '>= 0.9.0'
- type: software
title: terra
abstract: 'terra: Spatial Data Analysis'
notes: Suggests
url: https://rspatial.org/
repository: https://CRAN.R-project.org/package=terra
authors:
- family-names: Hijmans
given-names: Robert J.
email: [email protected]
orcid: https://orcid.org/0000-0001-5872-2872
year: '2024'
doi: 10.32614/CRAN.package.terra
- type: software
title: testthat
abstract: 'testthat: Unit Testing for R'
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ Suggests:
rmarkdown,
scales,
sf (>= 0.9.0),
terra,
testthat (>= 3.0.0)
VignetteBuilder:
knitr
Config/Needs/website: cpp11, crosstalk, devtools, geofacet, geoR, gifski,
gstat, leaflet, reactable, scales, sf, terra, tidyterra, tidyverse,
usethis
gstat, leaflet, reactable, scales, tidyterra, tidyverse, usethis
Config/testthat/edition: 3
Config/testthat/parallel: true
Copyright: © AEMET. See file COPYRIGHTS
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export(aemet_detect_api_key)
export(aemet_extremes_clim)
export(aemet_forecast_beaches)
export(aemet_forecast_daily)
export(aemet_forecast_fires)
export(aemet_forecast_hourly)
export(aemet_forecast_tidy)
export(aemet_forecast_vars_available)
Expand Down Expand Up @@ -57,4 +58,5 @@ importFrom(stats,aggregate)
importFrom(stats,approx)
importFrom(utils,head)
importFrom(utils,tail)
importFrom(utils,untar)
importFrom(utils,unzip)
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +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 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**.

# climaemet 1.3.0

Expand Down
4 changes: 0 additions & 4 deletions R/aemet_alerts.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,11 @@ aemet_alerts <- function(ccaa = NULL, lang = c("es", "en"), verbose = FALSE,
# nocov end
# nolint end

id <- 1

### API Loop ----
for (id in ln) {
this <- db_cuts[id, ]
if (progress) cli::cli_progress_update() # nocov



df <- aemet_hlp_single_alert(this, lang)

final_result <- c(final_result, list(df))
Expand Down
138 changes: 138 additions & 0 deletions R/aemet_forecast_fires.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#' AEMET fires forecast
#'
#' @description
#'
#' Get a [`SpatRaster`][terra::rast()] as provided by \CRANpkg{terra} with the
#' daily meteorological risk level for wildfires.
#'
#' @family aemet_api_data
#' @family forecasts
#'
#' @param area The area, being:
#' - `"p"` for Mainland Spain and Balearic Islands.
#' - `"c"` for Canary Islands.
#' @inheritParams get_data_aemet
#' @inheritParams aemet_daily
#'
#' @source
#'
#' <https://www.aemet.es/en/eltiempo/prediccion/incendios>.
#'
#' @return A [`tibble`][tibble::tibble()] or a [`SpatRaster`][terra::rast()]
#' object.
#'
#' @details
#' 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()
#' aemet_forecast_fires(extract_metadata = TRUE)
#'
#' # Extract alerts
#' alerts <- aemet_forecast_fires()
#'
#' alerts
#'
#' # Nice plotting with terra
#' library(terra)
#' plot(alerts)
#'
#' # Zoom in an area
#' cyl <- mapSpain::esp_get_ccaa("Castilla y Leon", epsg = 4326)
#'
#' # 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) {
# 1. Validate inputs----
area <- match.arg(area)
stopifnot(is.logical(verbose))

# 2. Download ----

## Metadata ----
if (extract_metadata) {
apidest <- "/api/incendios/mapasriesgo/previsto/dia/1/area/p"
final_result <- get_metadata_aemet(apidest = apidest, verbose = verbose)
return(final_result)
}

feed_url <- "https://www.aemet.es/es/api-eltiempo/incendios/download"


# Perform req

tmp_tar <- tempfile(fileext = ".tar.gzip")
req1 <- httr2::request(feed_url)
# nolint start
response <- httr2::req_perform(req1, path = tmp_tar)
# nolint end
untar(tmp_tar, exdir = file.path(tempdir(), "fires"))

# Select files
all_tifs <- list.files(file.path(tempdir(), "fires"),
pattern = ".tif$",
full.names = TRUE
)

area_tifs <- all_tifs[grepl(paste0("_", area, "_"), all_tifs)]

# Create a table with dates, etc
dbase <- dplyr::tibble(file = area_tifs)
# Date of pred
date <- unlist(strsplit(area_tifs[1], "_"))[5]
date <- as.Date(date, "%d%m%Y")
dbase$base_date <- date

# offset
off_all <- lapply(area_tifs, function(x) {
off <- unlist(strsplit(x, "_"))[3]
off <- as.integer(gsub("[^0-9]", "", off)) / 24
})

dbase$offset <- unlist(off_all)
dbase$date <- dbase$base_date + dbase$offset

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

# 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")

rrast
}
2 changes: 1 addition & 1 deletion R/climaemet-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom utils head tail unzip
#' @importFrom utils head tail unzip untar
#' @importFrom grDevices hcl.colors hcl.pals
#' @importFrom stats aggregate approx
#' @importFrom rlang .data
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 03:00:00 0 249 8.6 5.5 292 41.7 293
#> 2 9434 -1.00 2024-08-26 04:00:00 0 249 9.8 6 296 41.7 300
#> 3 9434 -1.00 2024-08-26 05:00:00 0 249 8 5.6 291 41.7 288
#> 4 9434 -1.00 2024-08-26 06:00:00 0 249 8.2 5.6 297 41.7 288
#> 5 9434 -1.00 2024-08-26 07:00:00 0 249 6.1 3.9 306 41.7 308
#> 6 9434 -1.00 2024-08-26 08:00:00 0 249 8.3 5.4 313 41.7 305
#> 7 9434 -1.00 2024-08-26 09:00:00 0 249 8.4 5.3 319 41.7 295
#> 8 9434 -1.00 2024-08-26 10:00:00 0 249 8.1 5 306 41.7 325
#> 9 9434 -1.00 2024-08-26 11:00:00 0 249 7.6 4.6 309 41.7 295
#> 10 9434 -1.00 2024-08-26 12:00:00 0 249 7.5 3.2 307 41.7 278
#> 11 9434 -1.00 2024-08-26 13:00:00 0 249 6 3 317 41.7 305
#> 12 9434 -1.00 2024-08-26 14:00:00 0 249 5.7 2.4 300 41.7 293
#> 13 9434 -1.00 2024-08-26 15:00:00 0 249 5.2 2.2 331 41.7 305
#> 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 03:00:00 | 0 | 249 | 8.6 | 5.5 | 292 | 41.66056 | 293 | ZARAGOZA AEROPUERTO | 990.3 | 63 | 0.6 | 18.5 | 1019.9 | 18.2 | 18.2 | 18.4 | 11.0 | 6 | 0.0 | 24.2 | 0 | 28.5 |
| 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 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: 13 additions & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@
},
"sameAs": "https://CRAN.R-project.org/package=sf"
},
{
"@type": "SoftwareApplication",
"identifier": "terra",
"name": "terra",
"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=terra"
},
{
"@type": "SoftwareApplication",
"identifier": "testthat",
Expand Down Expand Up @@ -351,7 +363,7 @@
},
"applicationCategory": "Meteorology",
"isPartOf": "https://ropenspain.es/",
"fileSize": "1021.885KB",
"fileSize": "861.704KB",
"citation": [
{
"@type": "SoftwareSourceCode",
Expand Down
1 change: 1 addition & 0 deletions man/aemet_alert_zones.Rd

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

1 change: 1 addition & 0 deletions man/aemet_alerts.Rd

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

1 change: 1 addition & 0 deletions man/aemet_beaches.Rd

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

1 change: 1 addition & 0 deletions man/aemet_daily.Rd

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

1 change: 1 addition & 0 deletions man/aemet_extremes_clim.Rd

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

2 changes: 2 additions & 0 deletions man/aemet_forecast.Rd

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

2 changes: 2 additions & 0 deletions man/aemet_forecast_beaches.Rd

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

Loading

0 comments on commit 37837bd

Please sign in to comment.