library(meteospain)
library(ggplot2)
library(ggforce)
library(units)
#> udunits database from /usr/share/xml/udunits/udunits2.xml
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
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.
In “monthly” and “yearly”, only the years in start_date
and
end_date
are used, returning all year monthly or yearly
values (i.e start_date = as.Date("2020-12-01")
is
the same as start_date = as.Date("2020-01-01")
as both will
return all 2020 measures).
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.
The exception for this are “monthly” and “yearly” temporal resolutions. AEMET API only allows for one station to be retrieved.
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:
# current day, all stations
api_options <- aemet_options(
resolution = 'current_day',
api_key = key_get('aemet')
)
api_options
#> $resolution
#> [1] "current_day"
#>
#> $start_date
#> [1] "2024-11-16"
#>
#> $end_date
#> [1] "2024-11-16"
#>
#> $stations
#> NULL
#>
#> $api_key
#> [1] "my_api_key"
# daily, all stations
api_options <- aemet_options(
resolution = 'daily',
start_date = as.Date('2020-04-25'), end_date = as.Date('2020-05-08'),
api_key = key_get('aemet')
)
api_options
#> $resolution
#> [1] "daily"
#>
#> $start_date
#> [1] "2020-04-25"
#>
#> $end_date
#> [1] "2020-05-08"
#>
#> $stations
#> NULL
#>
#> $api_key
#> [1] "my_api_key"
# monthly, only one station because AEMET API limitations
api_options <- aemet_options(
resolution = 'monthly',
start_date = as.Date('2020-04-25'), end_date = as.Date('2020-05-25'),
station = "0149X",
api_key = key_get('aemet')
)
api_options
#> $resolution
#> [1] "monthly"
#>
#> $start_date
#> [1] "2020-01-01"
#>
#> $end_date
#> [1] "2020-12-31"
#>
#> $stations
#> [1] "0149X"
#>
#> $api_key
#> [1] "my_api_key"
Accessing station metadata for AEMET is simple: