library(meteospain)
library(ggplot2)
library(ggforce)
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.3.1, PROJ 8.0.1
library(keyring)
AEMET is the Spanish national meteorologic service, and is the national meteorology authority providing quality data for public and research use, as well as prediction products and disaster warning system. meteospain
only access to the automatic meteorological stations network data.
meteospain
offers access to the AEMET API at different temporal resolutions:
In “daily”, a start_date
(and optionally an end_date
) arguments must be provided, indicating the period from which retrieve the data.
meteospain
access the data in the AEMET API collecting all stations. If a character vector of stations codes is supplied in the stations
argument, a filter step is done before returning the data to maintain only the stations supplied.
AEMET API only allow access to the data with a personal API Key. This token must be included in the api_key
argument of aemet_options
function.
To obtain the API Key, please visit https://opendata.aemet.es/centrodedescargas/inicio and follow the instructions at “Obtencion de API Key”.
It is not advisable to use the keys directly in any script shared or publicly available (github…), neither store them in plain text files. One option is using the keyring package for managing and accessing keys:
install.packages('keyring')
library(keyring)
key_set('aemet') # A prompt asking for the secret (the API Key) will appear.
# current day, all stations
<- aemet_options(
api_options resolution = 'current_day',
api_key = key_get('aemet')
) api_options
#> $resolution
#> [1] "current_day"
#>
#> $start_date
#> [1] "2021-11-05"
#>
#> $end_date
#> [1] "2021-11-05"
#>
#> $stations
#> NULL
#>
#> $api_key
#> [1] "my_api_key"
# daily, all stations
<- aemet_options(
api_options resolution = 'daily',
start_date = as.Date('2020-04-25'), end_date = as.Date('2020-05-25'),
api_key = key_get('aemet')
) api_options
#> $resolution
#> [1] "daily"
#>
#> $start_date
#> [1] "2020-04-25"
#>
#> $end_date
#> [1] "2020-05-25"
#>
#> $stations
#> NULL
#>
#> $api_key
#> [1] "my_api_key"
Accessing station metadata for AEMET is simple:
get_stations_info_from('aemet', api_options)
<- aemet_options(
api_options resolution = 'daily',
start_date = as.Date('2020-04-25'),
api_key = key_get('aemet')
)<- get_meteo_from('aemet', options = api_options)
spain_20200425 spain_20200425
Visually:
%>%
spain_20200425 ::drop_units() %>%
unitsggplot() +
geom_sf(aes(colour = mean_temperature)) +
scale_colour_viridis_c()
%>%
spain_20200425 ggplot() +
geom_histogram(aes(x = precipitation))