Skip to content

Commit

Permalink
Try another approach
Browse files Browse the repository at this point in the history
  • Loading branch information
dieghernan committed Aug 26, 2024
1 parent a471fe8 commit 9196aa0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
63 changes: 32 additions & 31 deletions R/aemet_forecast_fires.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
#' 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 `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 resulting object has several layers, each one representing the forecast
#' for the upcoming 7 days. It also has additional attributes provided by the
Expand All @@ -41,18 +41,31 @@
#' aemet_forecast_fires(extract_metadata = TRUE)
#'
#' # Extract alerts
#' library(terra)
#' 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)
#' facet_wrap(~lyr, nrow = 2) +
#' scale_fill_manual(
#' values = pal,
#' na.value = "transparent",
#' breaks = names(pal),
#' labels = c("Low", "Moderate", "High", "Very high", "Extreme")
#' )
#'
#'
#' # Zoom in an area
#' cyl <- mapSpain::esp_get_ccaa("Castilla y Leon", epsg = 4326)
Expand All @@ -61,18 +74,20 @@
#'
#' ggplot() +
#' geom_spatraster(data = fires_cyl) +
#' geom_sf(data = cyl, fill = NA, color = "black", linewidth = 0.5) +
#' facet_wrap(~lyr, nrow = 2)
#'
#' 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")
#' )
#' @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))

Check warning on line 90 in R/aemet_forecast_fires.R

View check run for this annotation

Codecov / codecov/patch

R/aemet_forecast_fires.R#L89-L90

Added lines #L89 - L90 were not covered by tests
verb <- ifelse(verbose, 3, 0)



# 2. Download ----

Expand All @@ -91,10 +106,7 @@ aemet_forecast_fires <- function(area = c("p", "c"), verbose = FALSE,
tmp_tar <- tempfile(fileext = ".tar.gzip")
req1 <- httr2::request(feed_url)

Check warning on line 107 in R/aemet_forecast_fires.R

View check run for this annotation

Codecov / codecov/patch

R/aemet_forecast_fires.R#L106-L107

Added lines #L106 - L107 were not covered by tests
# nolint start
response <- httr2::req_perform(req1,
path = tmp_tar,
verbosity = verb
)
response <- httr2::req_perform(req1, path = tmp_tar)

Check warning on line 109 in R/aemet_forecast_fires.R

View check run for this annotation

Codecov / codecov/patch

R/aemet_forecast_fires.R#L109

Added line #L109 was not covered by tests
# nolint end
untar(tmp_tar, exdir = file.path(tempdir(), "fires"))

Check warning on line 111 in R/aemet_forecast_fires.R

View check run for this annotation

Codecov / codecov/patch

R/aemet_forecast_fires.R#L111

Added line #L111 was not covered by tests

Expand Down Expand Up @@ -124,21 +136,10 @@ aemet_forecast_fires <- function(area = c("p", "c"), verbose = FALSE,

# Now create rasters

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

rrast <- lapply(dbase$file, function(f) {
r <- terra::rast(f)
r2 <- tidyterra::as_tibble(r, xy = TRUE)
r2[, 3] <- factor(r2[[3]], levels = seq_len(5))
r2 <- tidyterra::as_spatraster(r2)
terra::coltab(r2) <- ctab
r2
})
rrast <- lapply(dbase$file, terra::rast)

Check warning on line 139 in R/aemet_forecast_fires.R

View check run for this annotation

Codecov / codecov/patch

R/aemet_forecast_fires.R#L139

Added line #L139 was not covered by tests

rrast <- do.call("c", rrast)
rrast[is.nan(rrast)] <- NA
terra::time(rrast) <- dbase$date
names(rrast) <- format(dbase$date, format = "%Y-%m-%d")

Check warning on line 144 in R/aemet_forecast_fires.R

View check run for this annotation

Codecov / codecov/patch

R/aemet_forecast_fires.R#L141-L144

Added lines #L141 - L144 were not covered by tests

Expand Down
39 changes: 29 additions & 10 deletions man/aemet_forecast_fires.Rd

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

0 comments on commit 9196aa0

Please sign in to comment.