rOpenSpain / climaemet

R Climate AEMET Tools
https://ropenspain.github.io/climaemet/
GNU General Public License v3.0
41 stars 2 forks source link

Alertas para una comarca predeterminada #62

Closed jesbrz closed 1 month ago

jesbrz commented 2 months ago

Hola. Estoy probando la nueva característica de aemet_alerts para descargar las alertas para una comarca, pero no encuentro la forma. Este es el código que uso:

# Mostrar los nombres de las comarcas disponibles
aemet_alert_zones() %>%
        select(NOM_Z) %>%
        distinct()

# Obtener el mapa base de la comarca específica
basemap <- mapSpain::esp_get_comarca(comarca = c("Bierzo"))

# Obtener las alertas para la Comunidad Autónoma correspondiente
alerts_region <- aemet_alerts(
        ccaa = c("Bierzo"),
        return_sf = TRUE
)

Pero en el tercer paso me falla, claro está porque no es una ccaa:

Error in aemet_alerts(ccaa = c("Bierzo"), return_sf = TRUE) : 
  In ccaa param: No matches
In addition: Warning message:
In mapSpain::esp_dict_region_code(ccaa, destination = "codauto") :
  No match on codauto found for Bierzo

¿Cuál sería el código a incluir? Para liar más la cosa, AEMET le llama a la comarca "Bierzo de León" y mapSpain "Bierzo" lo que intuyo que también puede dificultar las cosas.

Saludos.

dieghernan commented 1 month ago

Hay varias maneras de hacerlo, una puede ser simplemente llamando a todas las alertas con alerts_region <- aemet_alerts(return_sf = TRUE) y luego filtrando por COD_Z según las que necesites.

En cuanto a tu duda sobre los nombres, el problema es que las comarcas en España no son por lo general entidades oficiales (creo que en alguna comunidad si, pero no en todas). Además de que existen diferentes tipos de comarcas (agricolas, ganaderas, históricas, etc.). Lo que hay que tener en cuenta aqui es que la AEMET no usacomarcas, sino "zonas de aviso", asi que en lugar de usar las comarcas de mapSpain habria que usar las zonas de aviso aemet_alert_zones. Mas info en https://www.aemet.es/documentos/es/eltiempo/prediccion/avisos/plan_meteoalerta/METEOALERTA_ANX2_Zonas_aviso.pdf.

Te paso de todas maneras un script modificado.

library(climaemet)
library(dplyr)
#> 
#> Adjuntando el paquete: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# Mostrar los nombres de la zona de aviso filtrando por regex en el nombre (contiene partícula "Bierzo").
bierzo <- aemet_alert_zones() %>%
  distinct() %>%
  filter(grepl("Bierzo", NOM_Z))

glimpse(bierzo)
#> Rows: 1
#> Columns: 6
#> $ COD_Z    <chr> "672402"
#> $ NOM_Z    <chr> "Bierzo de León"
#> $ COD_PROV <chr> "6724"
#> $ NOM_PROV <chr> "León"
#> $ COD_CCAA <chr> "67"
#> $ NOM_CCAA <chr> "Castilla y León"

# Obtener el mapa base de la zona específica pero con climaemet
basemap <- aemet_alert_zones(return_sf = TRUE) %>%
  filter(COD_Z == bierzo$COD_Z)

# Solamente para comprobar
com_auto <- mapSpain::esp_get_prov("Castilla y Leon")

ggplot(basemap) +
  geom_sf(data = com_auto) +
  geom_sf((aes(fill = NOM_Z)))


# Obtener todas las alertas
alerts_region <- aemet_alerts(
  return_sf = TRUE
) %>%
  # Y filtrar por COD_Z
  filter(COD_Z %in% bierzo$COD_Z)

# Hoy no hay
Sys.Date()
#> [1] "2024-08-26"
alerts_region
#> Simple feature collection with 0 features and 28 fields
#> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
#> Geodetic CRS:  WGS 84
#> # A tibble: 0 × 29
#> # ℹ 29 variables: NOM_CCAA <chr>, COD_CCAA <chr>, NOM_PROV <chr>,
#> #   COD_PROV <chr>, NOM_Z <chr>, COD_Z <chr>, language <chr>, category <chr>,
#> #   event <chr>, responseType <chr>, urgency <chr>, severity <chr>,
#> #   certainty <chr>, AEMET-Meteoalerta fenomeno <chr>, effective <dttm>,
#> #   onset <dttm>, expires <dttm>, senderName <chr>, headline <chr>,
#> #   description <chr>, instruction <chr>, web <chr>, contact <chr>,
#> #   AEMET-Meteoalerta nivel <chr>, AEMET-Meteoalerta parametro <chr>, …

Created on 2024-08-26 with reprex v2.1.1

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.4.1 (2024-06-14 ucrt) #> os Windows 11 x64 (build 22631) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate Spanish_Spain.utf8 #> ctype Spanish_Spain.utf8 #> tz Europe/Madrid #> date 2024-08-26 #> pandoc 3.1.12.2 @ C:/PROGRA~1/Pandoc/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> class 7.3-22 2023-05-03 [2] CRAN (R 4.4.1) #> classInt 0.4-10 2023-09-05 [1] CRAN (R 4.4.0) #> cli 3.6.3 2024-06-21 [1] CRAN (R 4.4.1) #> climaemet * 1.3.0.9000 2024-08-26 [1] local #> colorspace 2.1-1 2024-07-26 [1] CRAN (R 4.4.1) #> countrycode 1.6.0 2024-03-22 [1] CRAN (R 4.4.0) #> curl 5.2.2 2024-08-26 [1] CRAN (R 4.4.1) #> DBI 1.2.3 2024-06-02 [1] CRAN (R 4.4.0) #> digest 0.6.37 2024-08-19 [1] CRAN (R 4.4.1) #> dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.4.0) #> e1071 1.7-14 2023-12-06 [1] CRAN (R 4.4.0) #> evaluate 0.24.0 2024-06-10 [1] CRAN (R 4.4.0) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.4.0) #> farver 2.1.2 2024-05-13 [1] CRAN (R 4.4.0) #> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.4.0) #> fs 1.6.4 2024-04-25 [1] CRAN (R 4.4.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.4.0) #> ggplot2 * 3.5.1 2024-04-23 [1] CRAN (R 4.4.0) #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.4.0) #> gtable 0.3.5 2024-04-22 [1] CRAN (R 4.4.0) #> highr 0.11 2024-05-26 [1] CRAN (R 4.4.0) #> hms 1.1.3 2023-03-21 [1] CRAN (R 4.4.0) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0) #> httr2 1.0.3 2024-08-22 [1] CRAN (R 4.4.1) #> KernSmooth 2.23-24 2024-05-17 [1] CRAN (R 4.4.0) #> knitr 1.48 2024-07-07 [1] CRAN (R 4.4.1) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.4.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.4.0) #> mapSpain 0.9.1.9000 2024-08-25 [1] Github (ropenspain/mapSpain@47fb180) #> munsell 0.5.1 2024-04-01 [1] CRAN (R 4.4.0) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.4.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.4.0) #> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.4.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.4.0) #> rappdirs 0.3.3 2021-01-31 [1] CRAN (R 4.4.0) #> Rcpp 1.0.13 2024-07-17 [1] CRAN (R 4.4.1) #> readr 2.1.5 2024-01-10 [1] CRAN (R 4.4.0) #> reprex 2.1.1 2024-07-06 [1] CRAN (R 4.4.1) #> rlang 1.1.4 2024-06-04 [1] CRAN (R 4.4.0) #> rmarkdown 2.28 2024-08-17 [1] CRAN (R 4.4.1) #> rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.4.0) #> scales 1.3.0 2023-11-28 [1] CRAN (R 4.4.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.4.0) #> sf 1.0-16 2024-03-24 [1] CRAN (R 4.4.0) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.4.0) #> tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.4.0) #> tzdb 0.4.0 2023-05-12 [1] CRAN (R 4.4.0) #> units 0.8-5 2023-11-28 [1] CRAN (R 4.4.0) #> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.4.0) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.4.0) #> withr 3.0.1 2024-07-31 [1] CRAN (R 4.4.1) #> xfun 0.47 2024-08-17 [1] CRAN (R 4.4.1) #> xml2 1.3.6 2023-12-04 [1] CRAN (R 4.4.0) #> yaml 2.3.10 2024-07-26 [1] CRAN (R 4.4.1) #> #> [1] C:/Users/diego/AppData/Local/R/win-library/4.4 #> [2] C:/Program Files/R/R-4.4.1/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
jesbrz commented 1 month ago

La verdad es que lo de los nombres es un follón.... esta solución funciona de perlas, gracias.

dieghernan commented 1 month ago

Nueva version en CRAN con esta función incluida #65