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.
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:
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!
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.
.Renviron
fileThis stores your API key permanently on your machine. You can start
editing your .Renviron
running this command:
::edit_r_environ() usethis
Now you can add the following line to you .Renviron
file:
AEMET_API_KEY = YOUR_API_KEY
tidyverse
formatFrom 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: 23 × 25
#> idema lon fint prec alt vmax vv dv lat dmax ubi pres hr stdvv ts pres_nmar
#> <chr> <dbl> <dttm> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 9434 -1.00 2023-05-24 18:00:00 0 249 8.2 5 314 41.7 318 ZARAGOZA… 985. 49 1.1 24.4 1014.
#> 2 9434 -1.00 2023-05-24 19:00:00 0 249 8.2 4.9 323 41.7 313 ZARAGOZA… 986. 52 0.9 22.9 1015.
#> 3 9434 -1.00 2023-05-24 20:00:00 0 249 8.6 4.1 326 41.7 318 ZARAGOZA… 987. 59 0.8 20.5 1016.
#> 4 9434 -1.00 2023-05-24 21:00:00 0 249 7.8 5 322 41.7 320 ZARAGOZA… 988. 62 0.9 19.4 1017
#> 5 9434 -1.00 2023-05-24 22:00:00 0 249 9.5 6.7 314 41.7 315 ZARAGOZA… 988. 64 1.1 17.8 1017.
#> 6 9434 -1.00 2023-05-24 23:00:00 0 249 10.7 7.7 304 41.7 305 ZARAGOZA… 988. 65 0.9 17.5 1017.
#> 7 9434 -1.00 2023-05-25 00:00:00 0 249 11.1 7.6 310 41.7 323 ZARAGOZA… 988. 64 1.1 17.1 1017.
#> 8 9434 -1.00 2023-05-25 01:00:00 0 249 11.3 7.3 300 41.7 308 ZARAGOZA… 987. 66 0.9 16.8 1017.
#> 9 9434 -1.00 2023-05-25 02:00:00 0 249 8.9 5.8 302 41.7 295 ZARAGOZA… 987. 66 0.7 16.5 1017.
#> 10 9434 -1.00 2023-05-25 03:00:00 0 249 9.1 6 307 41.7 293 ZARAGOZA… 987 69 1 16.1 1017.
#> # ℹ 13 more rows
#> # ℹ 9 more variables: 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.
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)
<- aemet_last_obs(return_sf = TRUE)
all_stations # Last hour
<-
all_last %>% filter(fint == all_stations[["fint"]][1])
all_stations
<- max(all_last$fint)
last_hour
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
Other enhancements included on the v1.0.0:
get_metadata_aemet()
.ggclimat_walter_lieth()
. This function is
now the default for climatogram_*
functions (experimental).
Old behavior can be reproduced with options
ggplot2 = FALSE
.verbose
and
...
). Now it is possible to pass colors to the plotting
functions.climaemet::climaemet_9434_climatogram
,
climaemet::climaemet_9434_temp
,
climaemet::climaemet_9434_wind
.