Compatibility between services

library(meteospain)
library(sf)
library(purrr)
library(dplyr)
#> 
#> Adjuntando el paquete: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
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-16'), end_date = as.Date('2020-04-30'),
      api_key = keyring::key_get('aemet')
    )
)
#> Warning: Unable to connect to API at <https://opendata.aemet.es/opendata/sh/b3aa9d28>:
#> Error in curl::curl_fetch_memory(url, handle = handle): Failure when receiving
#> data from the peer [opendata.aemet.es]: OpenSSL SSL_read: OpenSSL/3.5.3:
#> error:0A000126:SSL routines::unexpected eof while reading, errno 0
#> ℹ This usually happens when connection with
#>   <https://opendata.aemet.es/opendata/sh/b3aa9d28> is not possible.
#> ℹ Data received
#> Trying again in 60 seconds (retry 1 of 3)
#> ℹ © AEMET. Autorizado el uso de la información y su reproducción citando a
#>   AEMET como autora de la misma.
#> https://www.aemet.es/es/nota_legal

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

meteogalicia_daily <- get_meteo_from(
  'meteogalicia',
  meteogalicia_options('daily', start_date = as.Date('2020-04-16'), end_date = as.Date('2020-04-30'))
)
#> ℹ A información divulgada a través deste servidor ofrécese gratuitamente aos
#>   cidadáns para que poida ser
#> utilizada libremente por eles, co único compromiso de mencionar expresamente a
#> MeteoGalicia e á
#> Consellería de Medio Ambiente, Territorio e Vivenda da Xunta de Galicia como
#> fonte da mesma cada vez
#> que as utilice para os usos distintos do particular e privado.
#> https://www.meteogalicia.gal/aviso-legal

ria_daily <- get_meteo_from(
  'ria',
  ria_options('daily', start_date = as.Date('2020-04-16'), end_date = as.Date('2020-04-30'))
)
#> Some stations didn't return data for some dates:
#> 11-3
#> 11-8
#> 11-9
#> 14-3
#> 18-4
#> 18-9
#> 21-1
#> 21-104
#> 21-106
#> 21-107
#> 21-12
#> 23-10
#> 23-13
#> 23-9
#> 29-3
#> 29-5
#> 4-3
#> 4-9
#> 41-1
#> 41-14
#> 41-4
#> 41-6
#> ℹ Data provided by Red de Información Agroclimática de Andalucía (RIA)
#> https://www.juntadeandalucia.es/agriculturaypesca/ifapa/riaweb/web/

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()
#> Joining with `by = join_by(timestamp, service, station_id, station_name,
#> station_province, altitude, mean_temperature, min_temperature, max_temperature,
#> mean_relative_humidity, min_relative_humidity, max_relative_humidity,
#> precipitation, mean_wind_speed, max_wind_speed, max_atmospheric_pressure,
#> min_atmospheric_pressure, geometry)`
#> Joining with `by = join_by(timestamp, service, station_id, station_name,
#> station_province, altitude, mean_temperature, min_temperature, max_temperature,
#> mean_relative_humidity, min_relative_humidity, max_relative_humidity,
#> precipitation, mean_wind_speed, max_wind_speed, insolation, geometry,
#> mean_wind_direction, max_wind_direction, mean_atmospheric_pressure,
#> reference_evapotranspiration)`
#> Joining with `by = join_by(timestamp, service, station_id, station_name,
#> station_province, altitude, mean_temperature, min_temperature, max_temperature,
#> mean_relative_humidity, min_relative_humidity, max_relative_humidity,
#> precipitation, mean_wind_speed, max_wind_speed, geometry, mean_wind_direction,
#> max_wind_direction)`

april_2020_spain
#> Simple feature collection with 20984 features and 43 fields (with 75 geometries empty)
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -18.115 ymin: 27.71889 xmax: 4.323889 ymax: 43.78611
#> Geodetic CRS:  WGS 84
#> # A tibble: 20,984 × 44
#>    timestamp           service station_id station_name station_province altitude
#>    <dttm>              <chr>   <chr>      <chr>        <chr>                 [m]
#>  1 2020-04-16 00:00:00 aemet   0009X      ALFORJA      TARRAGONA             406
#>  2 2020-04-16 00:00:00 aemet   0016A      REUS AEROPU… TARRAGONA              71
#>  3 2020-04-16 00:00:00 aemet   0016B      REUS (CENTR… TARRAGONA             118
#>  4 2020-04-16 00:00:00 aemet   0034X      VALLS        TARRAGONA             233
#>  5 2020-04-16 00:00:00 aemet   0061X      PONTONS      BARCELONA             632
#>  6 2020-04-16 00:00:00 aemet   0073X      SITGES       BARCELONA              58
#>  7 2020-04-16 00:00:00 aemet   0076       BARCELONA A… BARCELONA               4
#>  8 2020-04-16 00:00:00 aemet   0092X      BERGA        BARCELONA             682
#>  9 2020-04-16 00:00:00 aemet   0106X      BALSARENY    BARCELONA             361
#> 10 2020-04-16 00:00:00 aemet   0114X      PRATS DE LL… BARCELONA             700
#> # ℹ 20,974 more rows
#> # ℹ 38 more variables: mean_temperature [°C], min_temperature [°C],
#> #   max_temperature [°C], mean_relative_humidity [%],
#> #   min_relative_humidity [%], max_relative_humidity [%],
#> #   precipitation [L/m^2], wind_direction [°], mean_wind_speed [m/s],
#> #   max_wind_speed [m/s], insolation [h], max_atmospheric_pressure [hPa],
#> #   min_atmospheric_pressure [hPa], geometry <POINT [°]>, …

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

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