Compatibility between services

# use eval = NOT_CRAN in the chunks connecting to API, to avoid errors or warnings in CRAN checks
NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN")), "true")
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  purl = NOT_CRAN
)

# env keyring
withr::local_options(list("keyring_backend" = "env"))
library(meteospain)
library(sf)
library(purrr)
library(dplyr)
library(ggplot2)
library(units)

# provide keys for aemet and meteocat if not done already
# keyring::key_set('aemet')
# keyring::key_set('meteocat')

meteospain aims to return stations data in a compatible format between services. This means:

This ease combining data from different services. Let's see an example.

April 2020 daily data

We are gonna download daily data for April, 2020 for all services providing this information, and combine them in one object:

Don't forget to store the keys for AEMET and MeteoCat if not done already (see code above)

aemet_daily <- get_meteo_from(
    'aemet', aemet_options(
      'daily', start_date = as.Date('2020-04-01'), end_date = as.Date('2020-04-30'),
      api_key = keyring::key_get('aemet')
    )
)

meteocat_daily <- get_meteo_from(
  'meteocat',
  meteocat_options('daily', start_date = as.Date('2020-04-01'), api_key = keyring::key_get('meteocat'))
)

meteogalicia_daily <- get_meteo_from(
  'meteogalicia',
  meteogalicia_options('daily', start_date = as.Date('2020-04-01'), end_date = as.Date('2020-04-30'))
)

ria_daily <- get_meteo_from(
  'ria',
  ria_options('daily', start_date = as.Date('2020-04-01'), end_date = as.Date('2020-04-30'))
)

Now we have all daily data for April, lets join them. We are gonna use the purrr package to do it in one pipe.
Here we convert the data to tibble before the join, that way we are not joining by the spatial data, but by timestamp and the stations metadata. After the join we convert back to sf.

april_2020_spain <- list(
  dplyr::as_tibble(aemet_daily),
  dplyr::as_tibble(meteocat_daily),
  dplyr::as_tibble(meteogalicia_daily),
  dplyr::as_tibble(ria_daily)
) |>
  purrr::reduce(dplyr::full_join) |>
  sf::st_as_sf()

april_2020_spain

We can visualize the data, only one day.

By service

april_2020_spain |>
  dplyr::filter(lubridate::day(timestamp) == 25) |>
  units::drop_units() |>
  ggplot(aes(colour = service)) +
  geom_sf() +
  scale_colour_viridis_d()

By one variable

```r april_2020_spain |> dplyr::filter(lubridate::day(timestamp) == 25) |> units::drop_units() |> ggplot(aes(colour = mean_temperature)) + geom_sf() + scale_colour_viridis_c()



Try the meteospain package in your browser

Any scripts or data that you put into this service are public.

meteospain documentation built on May 29, 2024, 1:59 a.m.