Get started with climaemet

climaemet provides access to meteorological observations, forecasts, alerts and climatology data from the Spanish Meteorological Agency (AEMET). It is part of rOpenSpain, a community that develops R packages for working with Spanish public data.

API key

Get your API key

To download data from AEMET, you need a free API key, which you can get at https://opendata.aemet.es/centrodedescargas/altaUsuario.

Once you have your API key, you can use any of the following methods:

a. Set API key with aemet_api_key()

This is the recommended option. Run:

aemet_api_key("YOUR_API_KEY", install = TRUE)

Using install = TRUE stores the API key on your local computer so it is available in future R sessions.

b. Use an environment variable

Alternatively, set the API key as an environment variable for the current session:

Sys.setenv(AEMET_API_KEY = "YOUR_API_KEY")

You need to run this command again after restarting R.

c. Modify your .Renviron file

You can also store the API key permanently in .Renviron. Open the file with:

usethis::edit_r_environ()

Then add the following line:

AEMET_API_KEY=YOUR_API_KEY

Data formats

Tabular results

climaemet returns tabular results as tibble objects. The package also infers column types when possible. For example, date and time columns are parsed as date-time objects and numeric columns are parsed as doubles.

The following call returns a tibble:

# See a tibble in action

aemet_last_obs("9434")
#> # A tibble: 13 × 25
#>    idema   lon fint                 prec   alt  vmax    vv    dv   lat  dmax ubi      pres    hr
#>    <chr> <dbl> <dttm>              <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>   <dbl> <dbl>
#>  1 9434  -1.00 2026-06-18 23:00:00     0   249   9.1   6.1   120  41.7   130 ZARAGO…  987.    34
#>  2 9434  -1.00 2026-06-19 00:00:00     0   249   9.5   5.5   120  41.7   133 ZARAGO…  987.    36
#>  3 9434  -1.00 2026-06-19 01:00:00     0   249   7.7   4.7   110  41.7   100 ZARAGO…  987.    40
#>  4 9434  -1.00 2026-06-19 02:00:00     0   249   7.9   4.2   109  41.7   113 ZARAGO…  987.    43
#>  5 9434  -1.00 2026-06-19 03:00:00     0   249   6.6   4     118  41.7   118 ZARAGO…  987.    46
#>  6 9434  -1.00 2026-06-19 04:00:00     0   249   5.3   2.3   115  41.7   120 ZARAGO…  987.    48
#>  7 9434  -1.00 2026-06-19 05:00:00     0   249   3.2   1.9   118  41.7    85 ZARAGO…  987.    51
#>  8 9434  -1.00 2026-06-19 06:00:00     0   249   4.9   1.9   114  41.7   135 ZARAGO…  988.    45
#>  9 9434  -1.00 2026-06-19 07:00:00     0   249   7     4.5   134  41.7   130 ZARAGO…  988.    37
#> 10 9434  -1.00 2026-06-19 08:00:00     0   249   6.5   4.4   112  41.7   113 ZARAGO…  988.    36
#> 11 9434  -1.00 2026-06-19 09:00:00     0   249   8.1   4.5   112  41.7   118 ZARAGO…  988.    34
#> 12 9434  -1.00 2026-06-19 10:00:00     0   249   8.2   5.4   122  41.7   125 ZARAGO…  988.    28
#> 13 9434  -1.00 2026-06-19 11:00:00     0   249   9.1   5.1   105  41.7    88 ZARAGO…  987.    26
#> # ℹ 12 more variables: stdvv <dbl>, ts <dbl>, pres_nmar <dbl>, tamin <dbl>, ta <dbl>,
#> #   tamax <dbl>, tpr <dbl>, stddv <dbl>, inso <dbl>, tss5cm <dbl>, pacutp <dbl>, tss20cm <dbl>

Spatial objects with sf

Data-access functions that support return_sf = TRUE can return spatial sf objects. These objects use the EPSG:4326 coordinate reference system (CRS), corresponding to the World Geodetic System 1984 (WGS 84), with unprojected longitude and latitude coordinates:

# You need to install sf if it is not already installed.
# Run install.packages("sf") to install it.

library(ggplot2)
library(dplyr)

all_stations <- aemet_daily_clim(
  start = "2021-01-08",
  end = "2021-01-08",
  return_sf = TRUE
)

ggplot(all_stations) +
  geom_sf(aes(colour = tmed), shape = 19, size = 2, alpha = 0.95) +
  labs(
    title = "Average temperature in Spain",
    subtitle = "8 Jan 2021",
    color = "Max temp.\n(celsius)",
    caption = "Source: AEMET"
  ) +
  scale_colour_gradientn(
    colours = hcl.colors(10, "RdBu", rev = TRUE),
    breaks = c(-10, -5, 0, 5, 10, 15, 20),
    guide = "legend"
  ) +
  theme_bw() +
  theme(
    panel.border = element_blank(),
    plot.title = element_text(face = "bold"),
    plot.subtitle = element_text(face = "italic")
  )
Example: temperature in Spain

Example: temperature in Spain

Additional features

Other package features include:

  • Data functions are now vectorized where the AEMET API supports it.
  • get_metadata_aemet() retrieves metadata from arbitrary AEMET API endpoints.
  • ggclimat_walter_lieth() creates Walter-Lieth climate diagrams and is the default plotting method used by climatogram_normal() and climatogram_period() Experimental. Set ggplot2 = FALSE to use climatol::diagwl() instead.
  • Plotting functions accept additional options through ....
  • The example datasets climaemet_9434_climatogram, climaemet_9434_temp and climaemet_9434_wind support the plotting examples.