Get started with climaemet 1.0.0

Diego Hernangómez

Since the last release, this package has been integrated into rOpenSpain, a community of R enthusiasts whose ultimate goal is to create high-quality R packages for data mining public Spanish open sources.

From version 1.0.0 onward, we have introduced some improvements and (breaking) changes on the package, in order to provide a smoother interaction with the AEMET API service.

API Key

Get your API Key

To be able to download data from AEMET you will need a free API key which you can get at https://opendata.aemet.es/centrodedescargas/obtencionAPIKey

Once that 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. Just type:

aemet_api_key("YOUR_API_KEY", install = TRUE)

Using install = TRUE ensures that the API key is stored on your local computer and it would be reloaded every time you load the library. From now on you can forget about API keys!

b. Use an environment variable

This is a temporary alternative. You can set your API key as an environment variable

Sys.setenv(AEMET_API_KEY = "YOUR_API_KEY")

Note that this is only valid for the current session. You would need to re-run this command each time you restart your session.

c. Modify your .Renviron file

This stores your API key permanently on your machine. You can start editing your .Renviron running this command:

usethis::edit_r_environ()

Now you can add the following line to you .Renviron file:

AEMET_API_KEY = YOUR_API_KEY

New features

tibble format

From v1.0.0 onward, climaemet provides its results in tibble format. Also, the functions try to guess the correct format of the fields (i.e. something as a Date/Hour now is an hour, numbers are parsed as double, etc.).

See how a tibble is displayed:

# 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 2024-06-23 06:00:00     0   249  15.5  11.5   297  41.7   298 ZARAGOZA…  989.    66
#>  2 9434  -1.00 2024-06-23 07:00:00     0   249  15.4  11.7   305  41.7   303 ZARAGOZA…  989     64
#>  3 9434  -1.00 2024-06-23 08:00:00     0   249  16.1  10.5   312  41.7   308 ZARAGOZA…  988.    60
#>  4 9434  -1.00 2024-06-23 09:00:00     0   249  16.3  10.7   315  41.7   310 ZARAGOZA…  988.    57
#>  5 9434  -1.00 2024-06-23 10:00:00     0   249  18.2  11.6   316  41.7   310 ZARAGOZA…  988.    52
#>  6 9434  -1.00 2024-06-23 11:00:00     0   249  17.4  10.9   314  41.7   313 ZARAGOZA…  988.    48
#>  7 9434  -1.00 2024-06-23 12:00:00     0   249  17    11.1   311  41.7   303 ZARAGOZA…  987.    47
#>  8 9434  -1.00 2024-06-23 13:00:00     0   249  17.3  11.9   308  41.7   295 ZARAGOZA…  987.    46
#>  9 9434  -1.00 2024-06-23 14:00:00     0   249  17.7  12.4   309  41.7   308 ZARAGOZA…  986.    44
#> 10 9434  -1.00 2024-06-23 15:00:00     0   249  16.9  10.8   315  41.7   308 ZARAGOZA…  986.    44
#> 11 9434  -1.00 2024-06-23 16:00:00     0   249  18.3  11.1   317  41.7   313 ZARAGOZA…  985.    44
#> 12 9434  -1.00 2024-06-23 17:00:00     0   249  16.3  11.7   315  41.7   318 ZARAGOZA…  985.    46
#> 13 9434  -1.00 2024-06-23 18:00:00     0   249  18.9  12.9   314  41.7   328 ZARAGOZA…  985.    49
#> # ℹ 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>

Note that when possible, data representing dates and numbers are converted to the right format.

Spatial objects: sf

Another major change in v1.0.0 is the ability of return information on spatial sf format, using return_sf = TRUE. The coordinate reference system (CRS) used is EPSG 4326, that correspond to the World Geodetic System (WGS) and return coordinates in latitude/longitude (unprojected coordinates):

# You would need to install `sf` if not installed yet
# run install.packages("sf") for installation

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

Further enhancements

Other enhancements included on the v1.0.0: