Get started with climaemet 1.0.0

Diego Hernangómez

2022-08-13

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

tidyverse 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: 24 × 25
#>    idema   lon fint                 prec   alt  vmax    vv    dv   lat  dmax ubi           pres    hr stdvv    ts
#>    <chr> <dbl> <dttm>              <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>        <dbl> <dbl> <dbl> <dbl>
#>  1 9434  -1.00 2022-08-12 15:00:00     0   249   5.7   2.7    79  41.7   100 ZARAGOZA  A…  981.    14   0.9  45.4
#>  2 9434  -1.00 2022-08-12 16:00:00     0   249   5.7   3.2   118  41.7    53 ZARAGOZA  A…  980.    17   1.1  45.1
#>  3 9434  -1.00 2022-08-12 17:00:00     0   249   6.4   3.6   111  41.7   120 ZARAGOZA  A…  980.    18   0.6  42.3
#>  4 9434  -1.00 2022-08-12 18:00:00     0   249   5.9   3      94  41.7   105 ZARAGOZA  A…  980.    22   0.6  39.6
#>  5 9434  -1.00 2022-08-12 19:00:00     0   249   6.9   4.2   139  41.7   150 ZARAGOZA  A…  980.    19   0.8  37.5
#>  6 9434  -1.00 2022-08-12 20:00:00     0   249   5.7   2.8   195  41.7   153 ZARAGOZA  A…  980.    19   0.8  35.9
#>  7 9434  -1.00 2022-08-12 21:00:00     0   249   5.2   2.1   165  41.7   115 ZARAGOZA  A…  981.    21   0.4  34.4
#>  8 9434  -1.00 2022-08-12 22:00:00     0   249   5.2   2.3   206  41.7   235 ZARAGOZA  A…  981     20   1.1  34.4
#>  9 9434  -1.00 2022-08-12 23:00:00     0   249   4.9   2     296  41.7   128 ZARAGOZA  A…  981.    21   0.7  33.4
#> 10 9434  -1.00 2022-08-13 00:00:00     0   249   5.2   2.4   305  41.7   270 ZARAGOZA  A…  981.    30   0.5  30.8
#> # … with 14 more rows, and 10 more variables: pres_nmar <dbl>, tamin <dbl>, ta <dbl>, tamax <dbl>, tpr <dbl>,
#> #   stddv <dbl>, inso <dbl>, tss5cm <dbl>, pacutp <dbl>, tss20cm <dbl>
#> # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names

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_last_obs(return_sf = TRUE)
# Last hour
all_last <-
  all_stations %>% filter(fint == all_stations[["fint"]][1])

last_hour <- max(all_last$fint)


ggplot(all_last) +
  geom_sf(aes(col = ta),
    shape = 19,
    size = 2
  ) +
  labs(
    title = "Temperature in Spain",
    subtitle = last_hour,
    color = "Max temp.\n(celsius)",
    caption = "Source: AEMET"
  ) +
  scale_colour_gradientn(
    colours = hcl.colors(5, "RdBu", rev = TRUE),
    guide = "legend",
    n.breaks = 7
  ) +
  theme_bw() +
  theme(
    panel.border = element_blank(),
    plot.title = element_text(size = 23, face = "bold"),
    plot.subtitle = element_text(size = 16, face = "italic"),
    plot.caption = element_text(size = 15),
    legend.text = element_text(size = 15),
    legend.title = element_text(size = 15)
  )

plot of chunk spatial

Further enhancements

Other enhancements included on the v1.0.0: