
Forecast database by municipality
Source:R/aemet_forecast_daily.R
, R/aemet_forecast_hourly.R
aemet_forecast.Rd
Get a database of daily or hourly weather forecasts for a given municipality.
Arguments
- x
A vector of municipality codes to extract. For convenience, climaemet provides this data on the dataset aemet_munic (see
municipio
field) as of January 2020.- verbose
Logical
TRUE/FALSE
. Provides information about the flow of information between the client and server.
Value
A nested tibble
. Forecasted values can be extracted with
aemet_forecast_tidy()
. See also Details
Details
Forecasts format provided by the AEMET API have a complex structure.
Although climaemet returns a tibble
, each forecasted value is
provided as a nested tibble
. aemet_forecast_tidy()
helper function can
unnest these values an provide a single unnested tibble
for the requested
variable.
API Key
You need to set your API Key globally using aemet_api_key()
.
See also
aemet_munic for municipality codes.
Other aemet_api_data:
aemet_daily_clim()
,
aemet_extremes_clim()
,
aemet_last_obs()
,
aemet_monthly
,
aemet_normal
,
aemet_stations()
Other forecasts:
aemet_forecast_tidy()
Examples
# Select a city
data("aemet_munic")
library(dplyr)
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
munis <- aemet_munic %>%
filter(municipio_nombre %in% c(
"Santiago de Compostela",
"Lugo"
)) %>%
pull(municipio)
daily <- aemet_forecast_daily(munis)
# Vars available
aemet_forecast_vars_available(daily)
#> [1] "probPrecipitacion" "cotaNieveProv" "estadoCielo"
#> [4] "viento" "rachaMax" "temperatura"
#> [7] "sensTermica" "humedadRelativa"
# This is nested
daily %>%
select(municipio, fecha, nombre, temperatura)
#> # A tibble: 14 × 4
#> municipio fecha nombre temperatura$maxima $minima $dato
#> <chr> <date> <chr> <int> <int> <list>
#> 1 15078 2023-03-29 Santiago de Compostela 20 12 <df>
#> 2 15078 2023-03-30 Santiago de Compostela 17 13 <df>
#> 3 15078 2023-03-31 Santiago de Compostela 15 11 <df>
#> 4 15078 2023-04-01 Santiago de Compostela 16 7 <df>
#> 5 15078 2023-04-02 Santiago de Compostela 15 4 <df>
#> 6 15078 2023-04-03 Santiago de Compostela 18 6 <df>
#> 7 15078 2023-04-04 Santiago de Compostela 21 6 <df>
#> 8 27028 2023-03-29 Lugo 24 12 <df>
#> 9 27028 2023-03-30 Lugo 21 10 <df>
#> 10 27028 2023-03-31 Lugo 16 9 <df>
#> 11 27028 2023-04-01 Lugo 16 4 <df>
#> 12 27028 2023-04-02 Lugo 14 2 <df>
#> 13 27028 2023-04-03 Lugo 17 3 <df>
#> 14 27028 2023-04-04 Lugo 20 2 <df>
# Select and unnest
daily_temp <- aemet_forecast_tidy(daily, "temperatura")
# This is not
daily_temp
#> # A tibble: 14 × 14
#> elaborado municipio nombre provincia id version uvMax fecha
#> <dttm> <chr> <chr> <chr> <chr> <dbl> <int> <date>
#> 1 2023-03-29 12:09:32 15078 Santi… A Coruña 15078 1 6 2023-03-29
#> 2 2023-03-29 12:09:32 15078 Santi… A Coruña 15078 1 5 2023-03-30
#> 3 2023-03-29 12:09:32 15078 Santi… A Coruña 15078 1 5 2023-03-31
#> 4 2023-03-29 12:09:32 15078 Santi… A Coruña 15078 1 5 2023-04-01
#> 5 2023-03-29 12:09:32 15078 Santi… A Coruña 15078 1 6 2023-04-02
#> 6 2023-03-29 12:09:32 15078 Santi… A Coruña 15078 1 NA 2023-04-03
#> 7 2023-03-29 12:09:32 15078 Santi… A Coruña 15078 1 NA 2023-04-04
#> 8 2023-03-29 12:09:43 27028 Lugo Lugo 27028 1 6 2023-03-29
#> 9 2023-03-29 12:09:43 27028 Lugo Lugo 27028 1 5 2023-03-30
#> 10 2023-03-29 12:09:43 27028 Lugo Lugo 27028 1 5 2023-03-31
#> 11 2023-03-29 12:09:43 27028 Lugo Lugo 27028 1 5 2023-04-01
#> 12 2023-03-29 12:09:43 27028 Lugo Lugo 27028 1 6 2023-04-02
#> 13 2023-03-29 12:09:43 27028 Lugo Lugo 27028 1 NA 2023-04-03
#> 14 2023-03-29 12:09:43 27028 Lugo Lugo 27028 1 NA 2023-04-04
#> # ℹ 6 more variables: temperatura_maxima <int>, temperatura_minima <int>,
#> # temperatura_6 <int>, temperatura_12 <int>, temperatura_18 <int>,
#> # temperatura_24 <int>
# Wrangle and plot
daily_temp_end <- daily_temp %>%
select(
elaborado, fecha, municipio, nombre, temperatura_minima,
temperatura_maxima
) %>%
tidyr::pivot_longer(cols = contains("temperatura"))
# Plot
library(ggplot2)
ggplot(daily_temp_end) +
geom_line(aes(fecha, value, color = name)) +
facet_wrap(~nombre, ncol = 1) +
scale_color_manual(
values = c("red", "blue"),
labels = c("max", "min")
) +
scale_x_date(
labels = scales::label_date_short(),
breaks = "day"
) +
scale_y_continuous(
labels = scales::label_comma(suffix = "º")
) +
theme_minimal() +
labs(
x = "", y = "",
color = "",
title = "Forecast: 7-day temperature",
subtitle = paste(
"Forecast produced on",
format(daily_temp_end$elaborado[1], usetz = TRUE)
)
)