AEMET service

library(meteospain)
library(ggplot2)
library(ggforce)
library(units)
#> udunits database from /usr/share/udunits/udunits2.xml
library(sf)
#> Linking to GEOS 3.12.0, GDAL 3.8.1, PROJ 9.3.1; sf_use_s2() is TRUE
library(keyring)

AEMET service

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.

AEMET options

Temporal resolution

meteospain offers access to the AEMET API at different temporal resolutions:

  • “current_day”, returning the latest 24 hours of measures for all or selected stations.
  • “daily”, returning the daily aggregated measures for all or selected stations.
  • “monthly”, returning the monthly aggregated measures for only one station.
  • “yearly”, returning the yearly aggregated measures for only one station.

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).

Stations

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 Key

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.

Examples

# 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] "2023-12-19"
#> 
#> $end_date
#> [1] "2023-12-19"
#> 
#> $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-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"
# 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"

AEMET stations info

Accessing station metadata for AEMET is simple:

get_stations_info_from('aemet', api_options)
#> Simple feature collection with 291 features and 5 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -17.91528 ymin: 27.73583 xmax: 4.215556 ymax: 43.78611
#> Geodetic CRS:  WGS 84
#> # A tibble: 291 × 6
#>    service station_id station_name           station_province altitude
#>  * <chr>   <chr>      <chr>                  <chr>                 [m]
#>  1 aemet   0252D      ARENYS DE MAR          BARCELONA              74
#>  2 aemet   0076       BARCELONA AEROPUERTO   BARCELONA               4
#>  3 aemet   0200E      BARCELONA, FABRA       BARCELONA             408
#>  4 aemet   0201D      BARCELONA              BARCELONA               6
#>  5 aemet   0149X      MANRESA                BARCELONA             291
#>  6 aemet   0229I      SABADELL AEROPUERTO    BARCELONA             146
#>  7 aemet   0255B      SANTA SUSANNA          BARCELONA              40
#>  8 aemet   0367       GIRONA AEROPUERTO      GIRONA                143
#>  9 aemet   0370B      GIRONA, ANTIC INSTITUT GIRONA                 95
#> 10 aemet   0372C      PORQUERES              GIRONA                157
#> # ℹ 281 more rows
#> # ℹ 1 more variable: geometry <POINT [°]>

AEMET data

api_options <- aemet_options(
  resolution = 'daily',
  start_date = as.Date('2020-04-25'),
  api_key = key_get('aemet')
)
spain_20200425 <- get_meteo_from('aemet', options = api_options)
#> ℹ © AEMET. Autorizado el uso de la información y su reproducción citando a
#>   AEMET como autora de la misma.
#> https://www.aemet.es/es/nota_legal
spain_20200425
#> Simple feature collection with 237 features and 12 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -17.91528 ymin: 27.81639 xmax: 4.215556 ymax: 43.78611
#> Geodetic CRS:  WGS 84
#> # A tibble: 237 × 13
#>    timestamp           service station_id station_name station_province altitude
#>    <dttm>              <chr>   <chr>      <chr>        <chr>                 [m]
#>  1 2020-04-25 00:00:00 aemet   0016A      REUS AEROPU… TARRAGONA              71
#>  2 2020-04-25 00:00:00 aemet   0076       BARCELONA A… BARCELONA               4
#>  3 2020-04-25 00:00:00 aemet   0149X      MANRESA      BARCELONA             291
#>  4 2020-04-25 00:00:00 aemet   0200E      BARCELONA, … BARCELONA             408
#>  5 2020-04-25 00:00:00 aemet   0201D      BARCELONA    BARCELONA               6
#>  6 2020-04-25 00:00:00 aemet   0252D      ARENYS DE M… BARCELONA              74
#>  7 2020-04-25 00:00:00 aemet   0255B      SANTA SUSAN… BARCELONA              40
#>  8 2020-04-25 00:00:00 aemet   0324A      RIPOLL       GIRONA                675
#>  9 2020-04-25 00:00:00 aemet   0367       GIRONA AERO… GIRONA                143
#> 10 2020-04-25 00:00:00 aemet   0372C      PORQUERES    GIRONA                157
#> # ℹ 227 more rows
#> # ℹ 7 more variables: mean_temperature [°C], min_temperature [°C],
#> #   max_temperature [°C], precipitation [L/m^2], mean_wind_speed [m/s],
#> #   insolation [h], geometry <POINT [°]>

Visually:

spain_20200425 |>
  units::drop_units() |>
  ggplot() +
  geom_sf(aes(colour = mean_temperature)) +
  scale_colour_viridis_c()


spain_20200425 |>
  ggplot() +
  geom_histogram(aes(x = precipitation))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Warning: Removed 17 rows containing non-finite values (`stat_bin()`).